2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
13 #define LWT_BUFFER_SCALE 20
14 #define LWT_BUFFER_SIZE (1 << LWT_BUFFER_SCALE)
15 #define LWT_BUFFER_MASK (LWT_BUFFER_SIZE - 1)
17 volatile int halt_ = 0;
19 typedef struct lwt_record {
26 typedef struct lwt_buffer {
31 lwt_buffer_t *lwt_buf_[MAX_NUM_THREADS] = {};
32 char flag_state_[256] = {};
33 static const char *flags_ = "";
35 void lwt_thread_init (int thread_id)
37 assert(thread_id < MAX_NUM_THREADS);
38 if (lwt_buf_[thread_id] == NULL) {
39 lwt_buf_[thread_id] = (lwt_buffer_t *)nbd_malloc(sizeof(lwt_buffer_t) + sizeof(lwt_record_t) * LWT_BUFFER_SIZE);
40 memset(lwt_buf_[thread_id], 0, sizeof(lwt_buffer_t));
44 void lwt_set_trace_level (const char *flags)
46 assert(strlen(flags) % 2 == 0); // a well formed <flags> should be an even number of characters long
48 memset(flag_state_, 0, sizeof(flag_state_));
49 for (int i = 0; flags[i]; i+=2) {
50 flag_state_[(unsigned)flags[i]] = flags[i+1];
54 static void dump_record (FILE *file, int thread_id, lwt_record_t *r, uint64_t offset)
56 // print the record if its trace category is enabled at a high enough level
57 int flag = r->format >> 56;
58 int level = (r->format >> 48) & 0xFF;
59 if (flag_state_[(unsigned)flag] >= level) {
60 char s[3] = {flag, level, '\0'};
61 fprintf(file, "%09llu %d %s ", ((uint64_t)r->timestamp - offset) >> 5, thread_id, s);
62 const char *format = (const char *)(size_t)(r->format & MASK(48)); // strip out the embedded flags
63 fprintf(file, format, r->value1, r->value2);
68 static void dump_buffer (FILE *file, int thread_id, uint64_t offset)
70 lwt_buffer_t *tb = lwt_buf_[thread_id];
72 if (tb->head > LWT_BUFFER_SIZE) {
73 for (int i = tb->head & LWT_BUFFER_MASK; i < LWT_BUFFER_SIZE; ++i) {
74 dump_record(file, thread_id, tb->x + i, offset);
78 for (int i = 0; i < (tb->head & LWT_BUFFER_MASK); ++i) {
79 dump_record(file, thread_id, tb->x + i, offset);
83 void lwt_halt (void) {
87 void lwt_dump (const char *file_name)
90 uint64_t offset = (uint64_t)-1;
92 for (int i = 0; i < MAX_NUM_THREADS; ++i) {
93 if (lwt_buf_[i] != NULL && lwt_buf_[i]->head != 0) {
94 uint64_t x = lwt_buf_[i]->x[0].timestamp;
98 if (lwt_buf_[i]->head > LWT_BUFFER_SIZE)
100 x = lwt_buf_[i]->x[lwt_buf_[i]->head & LWT_BUFFER_MASK].timestamp;
108 if (offset != (uint64_t)-1) {
109 FILE *file = fopen(file_name, "w");
111 for (int i = 0; i < MAX_NUM_THREADS; ++i) {
112 if (lwt_buf_[i] != NULL) {
113 dump_buffer(file, i, offset);
121 void lwt_trace_i (uint64_t format, size_t value1, size_t value2) {
123 LOCALIZE_THREAD_LOCAL(tid_, int);
124 lwt_buffer_t *tb = lwt_buf_[tid_];
127 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (u));
128 uint64_t timestamp = ((uint64_t)u << 32) | l;
129 lwt_record_t temp = { timestamp, format, value1, value2 };
131 tb->x[tb->head++ & LWT_BUFFER_MASK] = temp;