-
Notifications
You must be signed in to change notification settings - Fork 1
/
ylog.c
498 lines (427 loc) · 11.7 KB
/
ylog.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include "ylog.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define CACHE_LINE_BYTES 64
struct trace_manager *g_trace_manager = NULL;
uint64_t
trace_cpu_time_now(void)
{
union {
uint64_t v;
struct {
uint32_t lo;
uint32_t hi;
};
} tsc;
asm volatile ("rdtsc":"=a" (tsc.lo), "=d"(tsc.hi));
return tsc.v;
}
double
get_cpu_mhz(void)
{
FILE *f;
char buf[256];
double mhz = 0.0;
f = fopen("/proc/cpuinfo", "r");
if (!f) {
fprintf(stderr, "Can not open cpuinfo file\n");
return 0;
}
while (fgets(buf, sizeof(buf), f)) {
double m;
int rc;
rc = sscanf(buf, "cpu MHz : %lf", &m);
if (rc > 0) {
if (mhz == 0.0) {
mhz = m;
break;
}
}
}
fclose(f);
return mhz;
}
static struct trace_manager *
map_shm(uint32_t key)
{
struct trace_manager *tm;
if (key <= 0) {
fprintf(stderr, "value of key is less than zero\n");
return NULL;
}
key_t shm_key = (key_t) key;
int shm_id = shmget(shm_key, sizeof(struct trace_manager), IPC_CREAT);
if (shm_id == -1) {
perror("shm create failed\n");
return NULL;
}
tm = (struct trace_manager *)shmat(shm_id, NULL, 0);
return tm;
}
void
trace_manager_init(uint32_t key)
{
g_trace_manager = map_shm(key);
if (g_trace_manager == NULL) {
fprintf(stderr, "map shared memory failed\n");
exit(0);
}
memset(g_trace_manager, 0, sizeof(struct trace_manager));
/*
* Initialize value other than 0
*/
g_trace_manager->cpu_freq_mhz = get_cpu_mhz();
}
void
trace_manager_destroy(struct trace_manager **tm)
{
if (tm == NULL || *tm == NULL) {
fprintf(stderr, "trace manager is NULL\n");
return;
}
if (shmdt(*tm) == -1) {
perror("shm detach error\n");
return;
}
*tm = NULL;
}
static void
tp_buffer_remap(struct trace_manager *tm)
{
int i = 0;
for (; i < TRACE_POINT_LIST_SIZE; i++) {
tm->trace_point_list[i].view_buffer = tm->buffer[i];
}
}
struct trace_manager *
trace_manager_connect(uint32_t key)
{
struct trace_manager *tm = map_shm(key);
if (tm == NULL) {
fprintf(stderr, "map shared memory failed\n");
exit(0);
}
tp_buffer_remap(tm);
return tm;
}
struct trace_point *
trace_point_create(const char *name)
{
/*
* starts from 0
*/
uint32_t index =
__sync_fetch_and_add(&g_trace_manager->trace_point_num, 1);
if (g_trace_manager->trace_point_num > TRACE_POINT_LIST_SIZE) {
fprintf(stderr, "No more space, can not create trace point%s\n", name);
return NULL;
}
struct trace_point *tp = &g_trace_manager->trace_point_list[index];
tp->trace_point_id = index;
tp->track_id = 0;
tp->event_left = CIRCULAR_BUFFER_SIZE / 2;
tp->trigger_id = 0;
tp->cr_fn = NULL;
tp->tr_fn = NULL;
tp->pthread_id = pthread_self();
tp->event_seq = 0;
tp->target_buffer =
(struct shared_mem_block *)&(g_trace_manager->buffer[index]);
tp->first_triggered_time = 0;
uint32_t name_len = strlen(name);
if (name_len >= TRACE_POINT_NAME_LEN) {
name_len = TRACE_POINT_NAME_LEN - 1;
}
strncpy(tp->name, name, name_len);
return tp;
}
void
set_trace_point(struct trace_point *tp, uint32_t track, const char *format,
const char *file, const int line, const char *func)
{
tp->track_id = track;
uint8_t format_len = strlen(format);
if (format_len >= TRACE_TYPE_FORMAT_LEN) {
format_len = TRACE_TYPE_FORMAT_LEN - 1;
fprintf(stderr, "Format buffer too short!");
}
strncpy(tp->type.format, format, format_len);
tp->track_id = track;
snprintf(tp->location, sizeof(tp->location), "%s%s%d%s%s",
file, ": ", line, ", in ", func);
}
void
register_content_retrieve_fn(struct trace_point *tp, content_retrieve fn)
{
tp->cr_fn = fn;
}
void
register_trigger_fn(struct trace_point *tp, is_triggered_fn fn)
{
tp->tr_fn = fn;
}
static int
track_id_exists(uint32_t track_id, uint32_t * array, uint32_t num)
{
int i = 0;
for (; i < num; i++) {
if (track_id == array[i]) {
return 1;
}
}
return 0;
}
void
find_all_tracks(struct trace_manager *tm, uint32_t * array, uint32_t * num)
{
if (array == NULL || num == NULL) {
fprintf(stderr, "invalid parameter for return\n");
return;
}
int i = 0;
uint32_t track_num = 0;
uint32_t t_track_id;
for (; i < tm->trace_point_num; i++) {
t_track_id = tm->trace_point_list[i].track_id;
if (!track_id_exists(t_track_id, array, track_num)) {
array[track_num] = t_track_id;
track_num++;
}
}
*num = track_num;
}
void
find_tp_by_type(struct trace_manager *tm,
const char *type, struct trace_point **tps, uint32_t * num)
{
if (tps == NULL || num == NULL) {
fprintf(stderr, "invalid parameter for return\n");
return;
}
int i = 0;
uint32_t tmp_num = 0;
for (; i < tm->trace_point_num; i++) {
if (!strcmp((const char *)tm->trace_point_list[i].type.format, type)) {
tps[tmp_num] = &tm->trace_point_list[i];
tmp_num++;
}
}
*num = tmp_num;
}
void
find_tp_by_track(struct trace_manager *tm,
uint32_t track, struct trace_point **tps, uint32_t * num)
{
if (tps == NULL || num == NULL) {
fprintf(stderr, "invalid parameter for return\n");
return;
}
int i = 0;
uint32_t tmp_num = 0;
for (; i < tm->trace_point_num; i++) {
if (track == tm->trace_point_list[i].track_id) {
tps[tmp_num] = &tm->trace_point_list[i];
tmp_num++;
}
}
*num = tmp_num;
}
void
find_all_tps(struct trace_manager *tm,
struct trace_point **tps, uint32_t * num)
{
if (tps == NULL || num == NULL) {
fprintf(stderr, "invalid parameter for return\n");
return;
}
int i = 0;
for (; i < tm->trace_point_num; i++) {
tps[i] = &tm->trace_point_list[i];
}
*num = tm->trace_point_num;
}
static void
print_trace_point(struct trace_point *tp, char *output)
{
static char *status_string[2] = { "Disabled", "Enabled" };
sprintf(output, "TP_ID: %4d\t%8s\t%8s\t%d\t%8ld\t%s",
tp->trace_point_id, tp->name,
status_string[tp->is_enabled], tp->track_id,
tp->event_seq, tp->location);
}
#define PRINT_TRACE_POINT_TABLE_FIRST_LINE \
sprintf(output[0], "Trace Point\tName\t\tStatus\t\tTrack\tEvent Records\tLocation"); \
sprintf(output[1], "-------------------------------------\
-----------------------------------------"); \
void
list_all_trace_point(struct trace_manager *tm, char **output)
{
PRINT_TRACE_POINT_TABLE_FIRST_LINE;
int i = 0;
for (; i < tm->trace_point_num; i++) {
print_trace_point(&tm->trace_point_list[i], output[TITLE_LINES + i]);
}
}
#define CHECK_ENABLE_MASK_BIT(tm, index, mod) \
( (tm->enabled_trace_point_mask[index] >> mod) & 1 )
static void
list_en_dis_trace_point(struct trace_manager *tm, uint8_t is_enable,
char **output)
{
PRINT_TRACE_POINT_TABLE_FIRST_LINE;
int i = 0;
for (; i < tm->trace_point_num; i++) {
uint32_t index = i / 8;
uint32_t mod = i % 8;
if (is_enable) {
if (CHECK_ENABLE_MASK_BIT(tm, index, mod)) {
print_trace_point(&tm->trace_point_list[i],
output[i + TITLE_LINES]);
}
} else {
if (!CHECK_ENABLE_MASK_BIT(tm, index, mod)) {
print_trace_point(&tm->trace_point_list[i],
output[i + TITLE_LINES]);
}
}
}
}
void
list_enabled_trace_point(struct trace_manager *tm, char **output)
{
list_en_dis_trace_point(tm, 1, output);
}
void
list_disabled_trace_point(struct trace_manager *tm, char **output)
{
list_en_dis_trace_point(tm, 0, output);
}
void
list_point(struct trace_point **tps, uint32_t num, char **output)
{
PRINT_TRACE_POINT_TABLE_FIRST_LINE;
if (tps == NULL) {
sprintf(output[3], "invalid parameter, tps is NULL\n");
return;
}
int i = 0;
for (; i < num; i++) {
if (tps[i] != NULL) {
print_trace_point(tps[i], output[i + TITLE_LINES]);
} else {
sprintf(output[i + TITLE_LINES], "point num is incorrect\n");
break;
}
}
}
inline static struct shared_mem_block *
get_next_buffer_block(struct trace_point *tp)
{
/*
* event_seq will start with 1
*/
tp->event_seq++;
uint32_t next_index = tp->event_seq % CIRCULAR_BUFFER_SIZE;
return (&tp->target_buffer[next_index]);
}
inline static struct shared_mem_block *
get_prev_buffer_block(struct trace_point *tp)
{
uint64_t seq = tp->event_seq - 1;
uint32_t prev_index = seq % CIRCULAR_BUFFER_SIZE;
return (&tp->target_buffer[prev_index]);
}
static struct shared_mem_block *get_last_buffer_block(struct trace_point *tp)
__attribute__ ((unused));
static struct shared_mem_block *
get_last_buffer_block(struct trace_point *tp)
{
uint64_t seq = tp->event_seq;
uint32_t prev_index = seq % CIRCULAR_BUFFER_SIZE;
return (&tp->target_buffer[prev_index]);
}
struct shared_mem_block **
get_life_cycle(struct trace_point *tp)
{
/*
* TODO
*/
return NULL;
}
void *
get_data_block(struct trace_point *tp)
{
struct shared_mem_block *b = get_next_buffer_block(tp);
b->event.trace_point_id = tp->trace_point_id;
b->event.timestamp = trace_cpu_time_now();
return (void *)b->data;
}
void *
get_prev_data_block(struct trace_point *tp)
{
struct shared_mem_block *b = get_prev_buffer_block(tp);
return (void *)b->data;
}
struct trace_point *
get_first_tp_by_track(struct trace_manager *tm, uint32_t track)
{
int i = 0;
for (; i < tm->trace_point_num; i++) {
if (tm->trace_point_list[i].track_id == track) {
return &tm->trace_point_list[i];
}
}
return NULL;
}
struct monitor_point *
monitor_point_create(char *name)
{
uint32_t index =
__sync_fetch_and_add(&g_trace_manager->monitor_point_num, 1);
if (g_trace_manager->monitor_point_num > MP_LIST_LEN) {
fprintf(stderr, "No more space, can not create monitor point%s\n",
name);
return NULL;
}
struct monitor_point *mp = &g_trace_manager->mp_list[index];
return mp;
}
void
set_monitor_point(struct monitor_point *mp, const char *file, const int line,
const char *func, const char *name)
{
snprintf(mp->string_buffer, sizeof(mp->string_buffer), "%s%s%s%s%s",
file, ", ", func, ": ", name);
}
struct perf_point *
perf_point_create(char *name)
{
uint32_t index = __sync_fetch_and_add(&g_trace_manager->perf_point_num, 1);
if (g_trace_manager->perf_point_num > PERF_POINT_LIST_LEN) {
fprintf(stderr, "No more space, can not create perf point%s\n", name);
return NULL;
}
struct perf_point *pp = &g_trace_manager->perf_list[index];
return pp;
}
void
set_perf_point(struct perf_point *pp, const char *file, const int line,
const char *func, const char *name, const char *unit)
{
snprintf(pp->string_buffer, sizeof(pp->string_buffer), "%s%s%s%s%s",
file, ", ", func, ": ", name);
snprintf(pp->unit, sizeof(pp->unit), "%s", unit);
pp->trigger_times = 0;
}
uint32_t *
get_perf_point_data_block(struct perf_point *pp)
{
struct perf_point_data *ppd =
&pp->data[pp->trigger_times % PERF_POINT_DATA_LEN];
pp->trigger_times++;
ppd->timestamp = trace_cpu_time_now();
return &ppd->count;
}