2 #include "thread-utils.h"
3 #include "trace2/tr2_tls.h"
6 * Initialize size of the thread stack for nested regions.
7 * This is used to store nested region start times. Note that
8 * this stack is per-thread and not per-trace-key.
10 #define TR2_REGION_NESTING_INITIAL_SIZE (100)
12 static struct tr2tls_thread_ctx *tr2tls_thread_main;
13 static uint64_t tr2tls_us_start_main;
15 static pthread_mutex_t tr2tls_mutex;
16 static pthread_key_t tr2tls_key;
18 static int tr2_next_thread_id; /* modify under lock */
20 struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name)
22 uint64_t us_now = getnanotime() / 1000;
23 struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx));
26 * Implicitly "tr2tls_push_self()" to capture the thread's start
27 * time in array_us_start[0]. For the main thread this gives us the
28 * application run time.
30 ctx->alloc = TR2_REGION_NESTING_INITIAL_SIZE;
31 ctx->array_us_start = (uint64_t *)xcalloc(ctx->alloc, sizeof(uint64_t));
32 ctx->array_us_start[ctx->nr_open_regions++] = us_now;
34 ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
36 strbuf_init(&ctx->thread_name, 0);
38 strbuf_addf(&ctx->thread_name, "th%02d:", ctx->thread_id);
39 strbuf_addstr(&ctx->thread_name, thread_name);
40 if (ctx->thread_name.len > TR2_MAX_THREAD_NAME)
41 strbuf_setlen(&ctx->thread_name, TR2_MAX_THREAD_NAME);
43 pthread_setspecific(tr2tls_key, ctx);
48 struct tr2tls_thread_ctx *tr2tls_get_self(void)
50 struct tr2tls_thread_ctx *ctx;
53 return tr2tls_thread_main;
55 ctx = pthread_getspecific(tr2tls_key);
58 * If the thread-proc did not call trace2_thread_start(), we won't
59 * have any TLS data associated with the current thread. Fix it
60 * here and silently continue.
63 ctx = tr2tls_create_self("unknown");
68 int tr2tls_is_main_thread(void)
73 return pthread_getspecific(tr2tls_key) == tr2tls_thread_main;
76 void tr2tls_unset_self(void)
78 struct tr2tls_thread_ctx *ctx;
80 ctx = tr2tls_get_self();
82 pthread_setspecific(tr2tls_key, NULL);
84 free(ctx->array_us_start);
88 void tr2tls_push_self(uint64_t us_now)
90 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
92 ALLOC_GROW(ctx->array_us_start, ctx->nr_open_regions + 1, ctx->alloc);
93 ctx->array_us_start[ctx->nr_open_regions++] = us_now;
96 void tr2tls_pop_self(void)
98 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
100 if (!ctx->nr_open_regions)
101 BUG("no open regions in thread '%s'", ctx->thread_name.buf);
103 ctx->nr_open_regions--;
106 void tr2tls_pop_unwind_self(void)
108 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
110 while (ctx->nr_open_regions > 1)
114 uint64_t tr2tls_region_elasped_self(uint64_t us)
116 struct tr2tls_thread_ctx *ctx;
119 ctx = tr2tls_get_self();
120 if (!ctx->nr_open_regions)
123 us_start = ctx->array_us_start[ctx->nr_open_regions - 1];
125 return us - us_start;
128 uint64_t tr2tls_absolute_elapsed(uint64_t us)
130 if (!tr2tls_thread_main)
133 return us - tr2tls_us_start_main;
136 void tr2tls_init(void)
138 pthread_key_create(&tr2tls_key, NULL);
139 init_recursive_mutex(&tr2tls_mutex);
141 tr2tls_thread_main = tr2tls_create_self("main");
143 * Keep a copy of the absolute start time of the main thread
144 * in a fixed variable since other threads need to access it.
145 * This also eliminates the need to lock accesses to the main
146 * thread's array (because of reallocs).
148 tr2tls_us_start_main = tr2tls_thread_main->array_us_start[0];
151 void tr2tls_release(void)
154 tr2tls_thread_main = NULL;
156 pthread_mutex_destroy(&tr2tls_mutex);
157 pthread_key_delete(tr2tls_key);
160 int tr2tls_locked_increment(int *p)
164 pthread_mutex_lock(&tr2tls_mutex);
166 *p = current_value + 1;
167 pthread_mutex_unlock(&tr2tls_mutex);
169 return current_value;