2019-02-22 23:25:01 +01:00
|
|
|
#ifndef TR2_TLS_H
|
|
|
|
#define TR2_TLS_H
|
|
|
|
|
|
|
|
#include "strbuf.h"
|
2022-10-24 15:41:07 +02:00
|
|
|
#include "trace2/tr2_ctr.h"
|
2022-10-24 15:41:06 +02:00
|
|
|
#include "trace2/tr2_tmr.h"
|
2019-02-22 23:25:01 +01:00
|
|
|
|
2022-10-24 15:41:01 +02:00
|
|
|
/*
|
|
|
|
* Notice: the term "TLS" refers to "thread-local storage" in the
|
|
|
|
* Trace2 source files. This usage is borrowed from GCC and Windows.
|
|
|
|
* There is NO relation to "transport layer security".
|
|
|
|
*/
|
|
|
|
|
2019-02-22 23:25:01 +01:00
|
|
|
/*
|
|
|
|
* Arbitry limit for thread names for column alignment.
|
|
|
|
*/
|
|
|
|
#define TR2_MAX_THREAD_NAME (24)
|
|
|
|
|
|
|
|
struct tr2tls_thread_ctx {
|
2022-10-24 15:41:05 +02:00
|
|
|
const char *thread_name;
|
2019-02-22 23:25:01 +01:00
|
|
|
uint64_t *array_us_start;
|
2022-10-24 15:41:00 +02:00
|
|
|
size_t alloc;
|
|
|
|
size_t nr_open_regions; /* plays role of "nr" in ALLOC_GROW */
|
2019-02-22 23:25:01 +01:00
|
|
|
int thread_id;
|
2022-10-24 15:41:06 +02:00
|
|
|
struct tr2_timer_block timer_block;
|
2022-10-24 15:41:07 +02:00
|
|
|
struct tr2_counter_block counter_block;
|
2022-10-24 15:41:06 +02:00
|
|
|
unsigned int used_any_timer:1;
|
|
|
|
unsigned int used_any_per_thread_timer:1;
|
2022-10-24 15:41:07 +02:00
|
|
|
unsigned int used_any_counter:1;
|
|
|
|
unsigned int used_any_per_thread_counter:1;
|
2019-02-22 23:25:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2022-10-24 15:41:01 +02:00
|
|
|
* Create thread-local storage for the current thread.
|
2019-02-22 23:25:01 +01:00
|
|
|
*
|
2022-10-24 15:41:04 +02:00
|
|
|
* The first thread in the process will have:
|
|
|
|
* { .thread_id=0, .thread_name="main" }
|
|
|
|
* Subsequent threads are given a non-zero thread_id and a thread_name
|
|
|
|
* constructed from the id and a thread base name (which is usually just
|
|
|
|
* the name of the thread-proc function). For example:
|
|
|
|
* { .thread_id=10, .thread_name="th10:fsm-listen" }
|
|
|
|
* This helps to identify and distinguish messages from concurrent threads.
|
|
|
|
* The ctx.thread_name field is truncated if necessary to help with column
|
|
|
|
* alignment in printf-style messages.
|
2019-02-22 23:25:01 +01:00
|
|
|
*
|
|
|
|
* In this and all following functions the term "self" refers to the
|
|
|
|
* current thread.
|
|
|
|
*/
|
2022-10-24 15:41:03 +02:00
|
|
|
struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
|
2019-04-15 22:39:43 +02:00
|
|
|
uint64_t us_thread_start);
|
2019-02-22 23:25:01 +01:00
|
|
|
|
|
|
|
/*
|
2022-10-24 15:41:01 +02:00
|
|
|
* Get the thread-local storage pointer of the current thread.
|
2019-02-22 23:25:01 +01:00
|
|
|
*/
|
|
|
|
struct tr2tls_thread_ctx *tr2tls_get_self(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return true if the current thread is the main thread.
|
|
|
|
*/
|
|
|
|
int tr2tls_is_main_thread(void);
|
|
|
|
|
|
|
|
/*
|
2022-10-24 15:41:01 +02:00
|
|
|
* Free the current thread's thread-local storage.
|
2019-02-22 23:25:01 +01:00
|
|
|
*/
|
|
|
|
void tr2tls_unset_self(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Begin a new nested region and remember the start time.
|
|
|
|
*/
|
|
|
|
void tr2tls_push_self(uint64_t us_now);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End the innermost nested region.
|
|
|
|
*/
|
|
|
|
void tr2tls_pop_self(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pop any extra (above the first) open regions on the current
|
|
|
|
* thread and discard. During a thread-exit, we should only
|
|
|
|
* have region[0] that was pushed in trace2_thread_start() if
|
|
|
|
* the thread exits normally.
|
|
|
|
*/
|
|
|
|
void tr2tls_pop_unwind_self(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the elapsed time since the innermost region in the
|
|
|
|
* current thread started and the given time (usually now).
|
|
|
|
*/
|
|
|
|
uint64_t tr2tls_region_elasped_self(uint64_t us);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the elapsed time since the main thread started
|
|
|
|
* and the given time (usually now). This is assumed to
|
|
|
|
* be the absolute run time of the process.
|
|
|
|
*/
|
|
|
|
uint64_t tr2tls_absolute_elapsed(uint64_t us);
|
|
|
|
|
|
|
|
/*
|
2022-10-24 15:41:01 +02:00
|
|
|
* Initialize thread-local storage for Trace2.
|
2019-02-22 23:25:01 +01:00
|
|
|
*/
|
|
|
|
void tr2tls_init(void);
|
|
|
|
|
|
|
|
/*
|
2022-10-24 15:41:01 +02:00
|
|
|
* Free all Trace2 thread-local storage resources.
|
2019-02-22 23:25:01 +01:00
|
|
|
*/
|
|
|
|
void tr2tls_release(void);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Protected increment of an integer.
|
|
|
|
*/
|
|
|
|
int tr2tls_locked_increment(int *p);
|
|
|
|
|
2019-04-15 22:39:43 +02:00
|
|
|
/*
|
|
|
|
* Capture the process start time and do nothing else.
|
|
|
|
*/
|
|
|
|
void tr2tls_start_process_clock(void);
|
|
|
|
|
2022-10-24 15:41:06 +02:00
|
|
|
/*
|
|
|
|
* Explicitly lock/unlock our mutex.
|
|
|
|
*/
|
|
|
|
void tr2tls_lock(void);
|
|
|
|
void tr2tls_unlock(void);
|
|
|
|
|
2019-02-22 23:25:01 +01:00
|
|
|
#endif /* TR2_TLS_H */
|