http: update curl http/2 info matching for curl 8.3.0
[git.git] / trace2 / tr2_tls.c
blob04900bb4c3a88cb43e5a79f7d6554e2236e52db8
1 #include "cache.h"
2 #include "thread-utils.h"
3 #include "trace2/tr2_tls.h"
5 /*
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.
9 */
10 #define TR2_REGION_NESTING_INITIAL_SIZE (100)
12 static struct tr2tls_thread_ctx *tr2tls_thread_main;
13 static uint64_t tr2tls_us_start_process;
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 void tr2tls_start_process_clock(void)
22 if (tr2tls_us_start_process)
23 return;
26 * Keep the absolute start time of the process (i.e. the main
27 * process) in a fixed variable since other threads need to
28 * access it. This allows them to do that without a lock on
29 * main thread's array data (because of reallocs).
31 tr2tls_us_start_process = getnanotime() / 1000;
34 struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
35 uint64_t us_thread_start)
37 struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx));
38 struct strbuf buf = STRBUF_INIT;
41 * Implicitly "tr2tls_push_self()" to capture the thread's start
42 * time in array_us_start[0]. For the main thread this gives us the
43 * application run time.
45 ctx->alloc = TR2_REGION_NESTING_INITIAL_SIZE;
46 ctx->array_us_start = (uint64_t *)xcalloc(ctx->alloc, sizeof(uint64_t));
47 ctx->array_us_start[ctx->nr_open_regions++] = us_thread_start;
49 ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
51 strbuf_init(&buf, 0);
52 if (ctx->thread_id)
53 strbuf_addf(&buf, "th%02d:", ctx->thread_id);
54 strbuf_addstr(&buf, thread_base_name);
55 if (buf.len > TR2_MAX_THREAD_NAME)
56 strbuf_setlen(&buf, TR2_MAX_THREAD_NAME);
57 ctx->thread_name = strbuf_detach(&buf, NULL);
59 pthread_setspecific(tr2tls_key, ctx);
61 return ctx;
64 struct tr2tls_thread_ctx *tr2tls_get_self(void)
66 struct tr2tls_thread_ctx *ctx;
68 if (!HAVE_THREADS)
69 return tr2tls_thread_main;
71 ctx = pthread_getspecific(tr2tls_key);
74 * If the current thread's thread-proc did not call
75 * trace2_thread_start(), then the thread will not have any
76 * thread-local storage. Create it now and silently continue.
78 if (!ctx)
79 ctx = tr2tls_create_self("unknown", getnanotime() / 1000);
81 return ctx;
84 int tr2tls_is_main_thread(void)
86 if (!HAVE_THREADS)
87 return 1;
89 return pthread_getspecific(tr2tls_key) == tr2tls_thread_main;
92 void tr2tls_unset_self(void)
94 struct tr2tls_thread_ctx *ctx;
96 ctx = tr2tls_get_self();
98 pthread_setspecific(tr2tls_key, NULL);
100 free((char *)ctx->thread_name);
101 free(ctx->array_us_start);
102 free(ctx);
105 void tr2tls_push_self(uint64_t us_now)
107 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
109 ALLOC_GROW(ctx->array_us_start, ctx->nr_open_regions + 1, ctx->alloc);
110 ctx->array_us_start[ctx->nr_open_regions++] = us_now;
113 void tr2tls_pop_self(void)
115 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
117 if (!ctx->nr_open_regions)
118 BUG("no open regions in thread '%s'", ctx->thread_name);
120 ctx->nr_open_regions--;
123 void tr2tls_pop_unwind_self(void)
125 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
127 while (ctx->nr_open_regions > 1)
128 tr2tls_pop_self();
131 uint64_t tr2tls_region_elasped_self(uint64_t us)
133 struct tr2tls_thread_ctx *ctx;
134 uint64_t us_start;
136 ctx = tr2tls_get_self();
137 if (!ctx->nr_open_regions)
138 return 0;
140 us_start = ctx->array_us_start[ctx->nr_open_regions - 1];
142 return us - us_start;
145 uint64_t tr2tls_absolute_elapsed(uint64_t us)
147 if (!tr2tls_thread_main)
148 return 0;
150 return us - tr2tls_us_start_process;
153 void tr2tls_init(void)
155 tr2tls_start_process_clock();
157 pthread_key_create(&tr2tls_key, NULL);
158 init_recursive_mutex(&tr2tls_mutex);
160 tr2tls_thread_main =
161 tr2tls_create_self("main", tr2tls_us_start_process);
164 void tr2tls_release(void)
166 tr2tls_unset_self();
167 tr2tls_thread_main = NULL;
169 pthread_mutex_destroy(&tr2tls_mutex);
170 pthread_key_delete(tr2tls_key);
173 int tr2tls_locked_increment(int *p)
175 int current_value;
177 pthread_mutex_lock(&tr2tls_mutex);
178 current_value = *p;
179 *p = current_value + 1;
180 pthread_mutex_unlock(&tr2tls_mutex);
182 return current_value;
185 void tr2tls_lock(void)
187 pthread_mutex_lock(&tr2tls_mutex);
190 void tr2tls_unlock(void)
192 pthread_mutex_unlock(&tr2tls_mutex);