]> git.ipfire.org Git - thirdparty/git.git/blob - trace2/tr2_tls.c
t9001: fix indentation in test_no_confirm()
[thirdparty/git.git] / trace2 / tr2_tls.c
1 #include "cache.h"
2 #include "thread-utils.h"
3 #include "trace2/tr2_tls.h"
4
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)
11
12 static struct tr2tls_thread_ctx *tr2tls_thread_main;
13 static uint64_t tr2tls_us_start_process;
14
15 static pthread_mutex_t tr2tls_mutex;
16 static pthread_key_t tr2tls_key;
17
18 static int tr2_next_thread_id; /* modify under lock */
19
20 void tr2tls_start_process_clock(void)
21 {
22 if (tr2tls_us_start_process)
23 return;
24
25 /*
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).
30 */
31 tr2tls_us_start_process = getnanotime() / 1000;
32 }
33
34 struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
35 uint64_t us_thread_start)
36 {
37 struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx));
38 struct strbuf buf = STRBUF_INIT;
39
40 /*
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.
44 */
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;
48
49 ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
50
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);
58
59 pthread_setspecific(tr2tls_key, ctx);
60
61 return ctx;
62 }
63
64 struct tr2tls_thread_ctx *tr2tls_get_self(void)
65 {
66 struct tr2tls_thread_ctx *ctx;
67
68 if (!HAVE_THREADS)
69 return tr2tls_thread_main;
70
71 ctx = pthread_getspecific(tr2tls_key);
72
73 /*
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.
77 */
78 if (!ctx)
79 ctx = tr2tls_create_self("unknown", getnanotime() / 1000);
80
81 return ctx;
82 }
83
84 int tr2tls_is_main_thread(void)
85 {
86 if (!HAVE_THREADS)
87 return 1;
88
89 return pthread_getspecific(tr2tls_key) == tr2tls_thread_main;
90 }
91
92 void tr2tls_unset_self(void)
93 {
94 struct tr2tls_thread_ctx *ctx;
95
96 ctx = tr2tls_get_self();
97
98 pthread_setspecific(tr2tls_key, NULL);
99
100 free((char *)ctx->thread_name);
101 free(ctx->array_us_start);
102 free(ctx);
103 }
104
105 void tr2tls_push_self(uint64_t us_now)
106 {
107 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
108
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;
111 }
112
113 void tr2tls_pop_self(void)
114 {
115 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
116
117 if (!ctx->nr_open_regions)
118 BUG("no open regions in thread '%s'", ctx->thread_name);
119
120 ctx->nr_open_regions--;
121 }
122
123 void tr2tls_pop_unwind_self(void)
124 {
125 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
126
127 while (ctx->nr_open_regions > 1)
128 tr2tls_pop_self();
129 }
130
131 uint64_t tr2tls_region_elasped_self(uint64_t us)
132 {
133 struct tr2tls_thread_ctx *ctx;
134 uint64_t us_start;
135
136 ctx = tr2tls_get_self();
137 if (!ctx->nr_open_regions)
138 return 0;
139
140 us_start = ctx->array_us_start[ctx->nr_open_regions - 1];
141
142 return us - us_start;
143 }
144
145 uint64_t tr2tls_absolute_elapsed(uint64_t us)
146 {
147 if (!tr2tls_thread_main)
148 return 0;
149
150 return us - tr2tls_us_start_process;
151 }
152
153 void tr2tls_init(void)
154 {
155 tr2tls_start_process_clock();
156
157 pthread_key_create(&tr2tls_key, NULL);
158 init_recursive_mutex(&tr2tls_mutex);
159
160 tr2tls_thread_main =
161 tr2tls_create_self("main", tr2tls_us_start_process);
162 }
163
164 void tr2tls_release(void)
165 {
166 tr2tls_unset_self();
167 tr2tls_thread_main = NULL;
168
169 pthread_mutex_destroy(&tr2tls_mutex);
170 pthread_key_delete(tr2tls_key);
171 }
172
173 int tr2tls_locked_increment(int *p)
174 {
175 int current_value;
176
177 pthread_mutex_lock(&tr2tls_mutex);
178 current_value = *p;
179 *p = current_value + 1;
180 pthread_mutex_unlock(&tr2tls_mutex);
181
182 return current_value;
183 }
184
185 void tr2tls_lock(void)
186 {
187 pthread_mutex_lock(&tr2tls_mutex);
188 }
189
190 void tr2tls_unlock(void)
191 {
192 pthread_mutex_unlock(&tr2tls_mutex);
193 }