blob: 601c9e5036fb27a7f88957559f03821c2fffa4a2 [file] [log] [blame]
Elijah Newren36bf1952023-02-24 00:09:24 +00001#include "git-compat-util.h"
Jeff Hostetleree4512e2019-02-22 14:25:01 -08002#include "thread-utils.h"
Elijah Newren36bf1952023-02-24 00:09:24 +00003#include "trace.h"
Jeff Hostetleree4512e2019-02-22 14:25:01 -08004#include "trace2/tr2_tls.h"
5
6/*
7 * Initialize size of the thread stack for nested regions.
8 * This is used to store nested region start times. Note that
9 * this stack is per-thread and not per-trace-key.
10 */
11#define TR2_REGION_NESTING_INITIAL_SIZE (100)
12
13static struct tr2tls_thread_ctx *tr2tls_thread_main;
Jeff Hostetlera0897242019-04-15 13:39:43 -070014static uint64_t tr2tls_us_start_process;
Jeff Hostetleree4512e2019-02-22 14:25:01 -080015
16static pthread_mutex_t tr2tls_mutex;
17static pthread_key_t tr2tls_key;
18
19static int tr2_next_thread_id; /* modify under lock */
20
Jeff Hostetlera0897242019-04-15 13:39:43 -070021void tr2tls_start_process_clock(void)
Jeff Hostetleree4512e2019-02-22 14:25:01 -080022{
Jeff Hostetlera0897242019-04-15 13:39:43 -070023 if (tr2tls_us_start_process)
24 return;
25
26 /*
27 * Keep the absolute start time of the process (i.e. the main
28 * process) in a fixed variable since other threads need to
29 * access it. This allows them to do that without a lock on
30 * main thread's array data (because of reallocs).
31 */
32 tr2tls_us_start_process = getnanotime() / 1000;
33}
34
Jeff Hostetlera70839c2022-10-24 13:41:03 +000035struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
Jeff Hostetlera0897242019-04-15 13:39:43 -070036 uint64_t us_thread_start)
37{
Jeff Hostetleree4512e2019-02-22 14:25:01 -080038 struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx));
Jeff Hostetler24a4c452022-10-24 13:41:05 +000039 struct strbuf buf = STRBUF_INIT;
Jeff Hostetleree4512e2019-02-22 14:25:01 -080040
41 /*
42 * Implicitly "tr2tls_push_self()" to capture the thread's start
43 * time in array_us_start[0]. For the main thread this gives us the
44 * application run time.
45 */
46 ctx->alloc = TR2_REGION_NESTING_INITIAL_SIZE;
47 ctx->array_us_start = (uint64_t *)xcalloc(ctx->alloc, sizeof(uint64_t));
Jeff Hostetlera0897242019-04-15 13:39:43 -070048 ctx->array_us_start[ctx->nr_open_regions++] = us_thread_start;
Jeff Hostetleree4512e2019-02-22 14:25:01 -080049
50 ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
51
Jeff Hostetler24a4c452022-10-24 13:41:05 +000052 strbuf_init(&buf, 0);
Jeff Hostetleree4512e2019-02-22 14:25:01 -080053 if (ctx->thread_id)
Jeff Hostetler24a4c452022-10-24 13:41:05 +000054 strbuf_addf(&buf, "th%02d:", ctx->thread_id);
55 strbuf_addstr(&buf, thread_base_name);
56 if (buf.len > TR2_MAX_THREAD_NAME)
57 strbuf_setlen(&buf, TR2_MAX_THREAD_NAME);
58 ctx->thread_name = strbuf_detach(&buf, NULL);
Jeff Hostetleree4512e2019-02-22 14:25:01 -080059
60 pthread_setspecific(tr2tls_key, ctx);
61
62 return ctx;
63}
64
65struct tr2tls_thread_ctx *tr2tls_get_self(void)
66{
Jeff Hostetler5fdae9d2019-05-21 12:33:59 -070067 struct tr2tls_thread_ctx *ctx;
68
69 if (!HAVE_THREADS)
70 return tr2tls_thread_main;
71
72 ctx = pthread_getspecific(tr2tls_key);
Jeff Hostetleree4512e2019-02-22 14:25:01 -080073
74 /*
Jeff Hostetler5bbb9252022-10-24 13:41:01 +000075 * If the current thread's thread-proc did not call
76 * trace2_thread_start(), then the thread will not have any
77 * thread-local storage. Create it now and silently continue.
Jeff Hostetleree4512e2019-02-22 14:25:01 -080078 */
79 if (!ctx)
Jeff Hostetlera0897242019-04-15 13:39:43 -070080 ctx = tr2tls_create_self("unknown", getnanotime() / 1000);
Jeff Hostetleree4512e2019-02-22 14:25:01 -080081
82 return ctx;
83}
84
85int tr2tls_is_main_thread(void)
86{
Jeff Hostetler5fdae9d2019-05-21 12:33:59 -070087 if (!HAVE_THREADS)
88 return 1;
Jeff Hostetleree4512e2019-02-22 14:25:01 -080089
Jeff Hostetler5fdae9d2019-05-21 12:33:59 -070090 return pthread_getspecific(tr2tls_key) == tr2tls_thread_main;
Jeff Hostetleree4512e2019-02-22 14:25:01 -080091}
92
93void tr2tls_unset_self(void)
94{
95 struct tr2tls_thread_ctx *ctx;
96
97 ctx = tr2tls_get_self();
98
99 pthread_setspecific(tr2tls_key, NULL);
100
Jeff Hostetler24a4c452022-10-24 13:41:05 +0000101 free((char *)ctx->thread_name);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800102 free(ctx->array_us_start);
103 free(ctx);
104}
105
106void tr2tls_push_self(uint64_t us_now)
107{
108 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
109
110 ALLOC_GROW(ctx->array_us_start, ctx->nr_open_regions + 1, ctx->alloc);
111 ctx->array_us_start[ctx->nr_open_regions++] = us_now;
112}
113
114void tr2tls_pop_self(void)
115{
116 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
117
118 if (!ctx->nr_open_regions)
Jeff Hostetler24a4c452022-10-24 13:41:05 +0000119 BUG("no open regions in thread '%s'", ctx->thread_name);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800120
121 ctx->nr_open_regions--;
122}
123
124void tr2tls_pop_unwind_self(void)
125{
126 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
127
128 while (ctx->nr_open_regions > 1)
129 tr2tls_pop_self();
130}
131
132uint64_t tr2tls_region_elasped_self(uint64_t us)
133{
134 struct tr2tls_thread_ctx *ctx;
135 uint64_t us_start;
136
137 ctx = tr2tls_get_self();
138 if (!ctx->nr_open_regions)
139 return 0;
140
141 us_start = ctx->array_us_start[ctx->nr_open_regions - 1];
142
143 return us - us_start;
144}
145
146uint64_t tr2tls_absolute_elapsed(uint64_t us)
147{
148 if (!tr2tls_thread_main)
149 return 0;
150
Jeff Hostetlera0897242019-04-15 13:39:43 -0700151 return us - tr2tls_us_start_process;
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800152}
153
154void tr2tls_init(void)
155{
Jeff Hostetlera0897242019-04-15 13:39:43 -0700156 tr2tls_start_process_clock();
157
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800158 pthread_key_create(&tr2tls_key, NULL);
159 init_recursive_mutex(&tr2tls_mutex);
160
Jeff Hostetlera0897242019-04-15 13:39:43 -0700161 tr2tls_thread_main =
162 tr2tls_create_self("main", tr2tls_us_start_process);
Jeff Hostetleree4512e2019-02-22 14:25:01 -0800163}
164
165void tr2tls_release(void)
166{
167 tr2tls_unset_self();
168 tr2tls_thread_main = NULL;
169
170 pthread_mutex_destroy(&tr2tls_mutex);
171 pthread_key_delete(tr2tls_key);
172}
173
174int tr2tls_locked_increment(int *p)
175{
176 int current_value;
177
178 pthread_mutex_lock(&tr2tls_mutex);
179 current_value = *p;
180 *p = current_value + 1;
181 pthread_mutex_unlock(&tr2tls_mutex);
182
183 return current_value;
184}
Jeff Hostetler8ad57562022-10-24 13:41:06 +0000185
186void tr2tls_lock(void)
187{
188 pthread_mutex_lock(&tr2tls_mutex);
189}
190
191void tr2tls_unlock(void)
192{
193 pthread_mutex_unlock(&tr2tls_mutex);
194}