trace2: add stopwatch timers
[alt-git.git] / trace2 / tr2_tls.h
bloba064b66e4cc14dc876fa3dcde13127d66fb75319
1 #ifndef TR2_TLS_H
2 #define TR2_TLS_H
4 #include "strbuf.h"
5 #include "trace2/tr2_tmr.h"
7 /*
8 * Notice: the term "TLS" refers to "thread-local storage" in the
9 * Trace2 source files. This usage is borrowed from GCC and Windows.
10 * There is NO relation to "transport layer security".
14 * Arbitry limit for thread names for column alignment.
16 #define TR2_MAX_THREAD_NAME (24)
18 struct tr2tls_thread_ctx {
19 const char *thread_name;
20 uint64_t *array_us_start;
21 size_t alloc;
22 size_t nr_open_regions; /* plays role of "nr" in ALLOC_GROW */
23 int thread_id;
24 struct tr2_timer_block timer_block;
25 unsigned int used_any_timer:1;
26 unsigned int used_any_per_thread_timer:1;
30 * Create thread-local storage for the current thread.
32 * The first thread in the process will have:
33 * { .thread_id=0, .thread_name="main" }
34 * Subsequent threads are given a non-zero thread_id and a thread_name
35 * constructed from the id and a thread base name (which is usually just
36 * the name of the thread-proc function). For example:
37 * { .thread_id=10, .thread_name="th10:fsm-listen" }
38 * This helps to identify and distinguish messages from concurrent threads.
39 * The ctx.thread_name field is truncated if necessary to help with column
40 * alignment in printf-style messages.
42 * In this and all following functions the term "self" refers to the
43 * current thread.
45 struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
46 uint64_t us_thread_start);
49 * Get the thread-local storage pointer of the current thread.
51 struct tr2tls_thread_ctx *tr2tls_get_self(void);
54 * return true if the current thread is the main thread.
56 int tr2tls_is_main_thread(void);
59 * Free the current thread's thread-local storage.
61 void tr2tls_unset_self(void);
64 * Begin a new nested region and remember the start time.
66 void tr2tls_push_self(uint64_t us_now);
69 * End the innermost nested region.
71 void tr2tls_pop_self(void);
74 * Pop any extra (above the first) open regions on the current
75 * thread and discard. During a thread-exit, we should only
76 * have region[0] that was pushed in trace2_thread_start() if
77 * the thread exits normally.
79 void tr2tls_pop_unwind_self(void);
82 * Compute the elapsed time since the innermost region in the
83 * current thread started and the given time (usually now).
85 uint64_t tr2tls_region_elasped_self(uint64_t us);
88 * Compute the elapsed time since the main thread started
89 * and the given time (usually now). This is assumed to
90 * be the absolute run time of the process.
92 uint64_t tr2tls_absolute_elapsed(uint64_t us);
95 * Initialize thread-local storage for Trace2.
97 void tr2tls_init(void);
100 * Free all Trace2 thread-local storage resources.
102 void tr2tls_release(void);
105 * Protected increment of an integer.
107 int tr2tls_locked_increment(int *p);
110 * Capture the process start time and do nothing else.
112 void tr2tls_start_process_clock(void);
115 * Explicitly lock/unlock our mutex.
117 void tr2tls_lock(void);
118 void tr2tls_unlock(void);
120 #endif /* TR2_TLS_H */