forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrace_service.c
181 lines (165 loc) · 3.94 KB
/
trace_service.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "trace_service.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <stdint.h>
#define HASH_SIZE 32
#if defined(__APPLE__)
#include <mach/task.h>
#include <mach/mach.h>
#endif
void
diff_time(struct timespec *ti, uint32_t *sec, uint32_t *nsec) {
struct timespec end;
#if !defined(__APPLE__)
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end);
#else
struct task_thread_times_info aTaskInfo;
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount));
end.tv_sec = aTaskInfo.user_time.seconds;
end.tv_nsec = aTaskInfo.user_time.microseconds * 1000;
#endif
int diffsec = end.tv_sec - ti->tv_sec;
assert(diffsec>=0);
int diffnsec = end.tv_nsec - ti->tv_nsec;
if (diffnsec < 0) {
--diffsec;
diffnsec += NANOSEC;
}
*nsec += diffnsec;
if (*nsec > NANOSEC) {
++*sec;
*nsec -= NANOSEC;
}
*sec += diffsec;
}
struct trace_info {
int session;
struct trace_info * prev;
struct trace_info * next;
struct timespec ti;
uint32_t ti_sec;
uint32_t ti_nsec;
};
struct trace_pool {
struct trace_info * current;
struct trace_info * slot[HASH_SIZE];
};
struct trace_pool *
trace_create() {
struct trace_pool * p = malloc(sizeof(*p));
memset(p, 0, sizeof(*p));
return p;
}
static void
_free_slot(struct trace_info *t) {
while (t) {
struct trace_info *next = t->next;
free(t);
t = next;
}
}
void
trace_release(struct trace_pool *p) {
int i;
for (i=0;i<HASH_SIZE;i++) {
_free_slot(p->slot[i]);
}
free(p->current);
}
struct trace_info *
trace_new(struct trace_pool *p) {
assert(p->current == NULL);
struct trace_info *t = malloc(sizeof(*t));
p->current = t;
t->session = 0;
t->prev = NULL;
t->next = NULL;
t->ti_sec = 0;
t->ti_nsec = 0;
#if !defined(__APPLE__)
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t->ti);
#else
struct task_thread_times_info aTaskInfo;
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount));
t->ti.tv_sec = aTaskInfo.user_time.seconds;
t->ti.tv_nsec = aTaskInfo.user_time.microseconds * 1000;
#endif
return t;
}
void
trace_register(struct trace_pool *p, int session) {
struct trace_info *t = p->current;
if (t == NULL) {
return;
}
int hash = session % HASH_SIZE;
assert(t->session == 0 && session > 0);
t->session = session;
t->prev = NULL;
t->next = p->slot[hash];
if (p->slot[hash]) {
p->slot[hash]->prev = t;
}
p->slot[hash] = t;
}
struct trace_info *
trace_yield(struct trace_pool *p) {
struct trace_info *t = p->current;
if (t == NULL)
return NULL;
diff_time(&t->ti,&t->ti_sec,&t->ti_nsec);
if (t->session == 0) {
return t;
} else {
p->current = NULL;
return NULL;
}
}
void
trace_switch(struct trace_pool *p, int session) {
assert(p->current == NULL && session > 0);
struct trace_info *t = p->slot[session % HASH_SIZE];
struct trace_info *prev = NULL;
while (t) {
if (t->session == session) {
p->current = t;
t->session = 0;
if (prev == NULL) {
p->slot[session % HASH_SIZE] = t->next;
} else {
prev->next = t->next;
}
if (t->next) {
t->next->prev = prev;
}
#if !defined(__APPLE__)
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t->ti);
#else
struct task_thread_times_info aTaskInfo;
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount));
t->ti.tv_sec = aTaskInfo.user_time.seconds;
t->ti.tv_nsec = aTaskInfo.user_time.microseconds * 1000;
#endif
return;
}
prev = t;
t=t->next;
}
}
double
trace_delete(struct trace_pool *p, struct trace_info *t) {
assert(p->current == t);
p->current = NULL;
if (t) {
double ti = (double)t->ti_sec + (double)t->ti_nsec / NANOSEC;
free(t);
return ti;
} else {
return 0;
}
}