]> git.ipfire.org Git - thirdparty/git.git/blob - trace2/tr2_tls.h
submodule-config.c: strengthen URL fsck check
[thirdparty/git.git] / trace2 / tr2_tls.h
1 #ifndef TR2_TLS_H
2 #define TR2_TLS_H
3
4 #include "strbuf.h"
5 #include "trace2/tr2_ctr.h"
6 #include "trace2/tr2_tmr.h"
7
8 /*
9 * Notice: the term "TLS" refers to "thread-local storage" in the
10 * Trace2 source files. This usage is borrowed from GCC and Windows.
11 * There is NO relation to "transport layer security".
12 */
13
14 /*
15 * Arbitry limit for thread names for column alignment.
16 */
17 #define TR2_MAX_THREAD_NAME (24)
18
19 struct tr2tls_thread_ctx {
20 const char *thread_name;
21 uint64_t *array_us_start;
22 size_t alloc;
23 size_t nr_open_regions; /* plays role of "nr" in ALLOC_GROW */
24 int thread_id;
25 struct tr2_timer_block timer_block;
26 struct tr2_counter_block counter_block;
27 unsigned int used_any_timer:1;
28 unsigned int used_any_per_thread_timer:1;
29 unsigned int used_any_counter:1;
30 unsigned int used_any_per_thread_counter:1;
31 };
32
33 /*
34 * Create thread-local storage for the current thread.
35 *
36 * The first thread in the process will have:
37 * { .thread_id=0, .thread_name="main" }
38 * Subsequent threads are given a non-zero thread_id and a thread_name
39 * constructed from the id and a thread base name (which is usually just
40 * the name of the thread-proc function). For example:
41 * { .thread_id=10, .thread_name="th10:fsm-listen" }
42 * This helps to identify and distinguish messages from concurrent threads.
43 * The ctx.thread_name field is truncated if necessary to help with column
44 * alignment in printf-style messages.
45 *
46 * In this and all following functions the term "self" refers to the
47 * current thread.
48 */
49 struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
50 uint64_t us_thread_start);
51
52 /*
53 * Get the thread-local storage pointer of the current thread.
54 */
55 struct tr2tls_thread_ctx *tr2tls_get_self(void);
56
57 /*
58 * return true if the current thread is the main thread.
59 */
60 int tr2tls_is_main_thread(void);
61
62 /*
63 * Free the current thread's thread-local storage.
64 */
65 void tr2tls_unset_self(void);
66
67 /*
68 * Begin a new nested region and remember the start time.
69 */
70 void tr2tls_push_self(uint64_t us_now);
71
72 /*
73 * End the innermost nested region.
74 */
75 void tr2tls_pop_self(void);
76
77 /*
78 * Pop any extra (above the first) open regions on the current
79 * thread and discard. During a thread-exit, we should only
80 * have region[0] that was pushed in trace2_thread_start() if
81 * the thread exits normally.
82 */
83 void tr2tls_pop_unwind_self(void);
84
85 /*
86 * Compute the elapsed time since the innermost region in the
87 * current thread started and the given time (usually now).
88 */
89 uint64_t tr2tls_region_elasped_self(uint64_t us);
90
91 /*
92 * Compute the elapsed time since the main thread started
93 * and the given time (usually now). This is assumed to
94 * be the absolute run time of the process.
95 */
96 uint64_t tr2tls_absolute_elapsed(uint64_t us);
97
98 /*
99 * Initialize thread-local storage for Trace2.
100 */
101 void tr2tls_init(void);
102
103 /*
104 * Free all Trace2 thread-local storage resources.
105 */
106 void tr2tls_release(void);
107
108 /*
109 * Protected increment of an integer.
110 */
111 int tr2tls_locked_increment(int *p);
112
113 /*
114 * Capture the process start time and do nothing else.
115 */
116 void tr2tls_start_process_clock(void);
117
118 /*
119 * Explicitly lock/unlock our mutex.
120 */
121 void tr2tls_lock(void);
122 void tr2tls_unlock(void);
123
124 #endif /* TR2_TLS_H */