Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 1 | #ifndef TR2_TLS_H |
| 2 | #define TR2_TLS_H |
| 3 | |
| 4 | #include "strbuf.h" |
| 5 | |
| 6 | /* |
| 7 | * Arbitry limit for thread names for column alignment. |
| 8 | */ |
| 9 | #define TR2_MAX_THREAD_NAME (24) |
| 10 | |
| 11 | struct tr2tls_thread_ctx { |
| 12 | struct strbuf thread_name; |
| 13 | uint64_t *array_us_start; |
| 14 | int alloc; |
| 15 | int nr_open_regions; /* plays role of "nr" in ALLOC_GROW */ |
| 16 | int thread_id; |
| 17 | }; |
| 18 | |
| 19 | /* |
| 20 | * Create TLS data for the current thread. This gives us a place to |
| 21 | * put per-thread data, such as thread start time, function nesting |
| 22 | * and a per-thread label for our messages. |
| 23 | * |
| 24 | * We assume the first thread is "main". Other threads are given |
| 25 | * non-zero thread-ids to help distinguish messages from concurrent |
| 26 | * threads. |
| 27 | * |
| 28 | * Truncate the thread name if necessary to help with column alignment |
| 29 | * in printf-style messages. |
| 30 | * |
| 31 | * In this and all following functions the term "self" refers to the |
| 32 | * current thread. |
| 33 | */ |
Jeff Hostetler | a089724 | 2019-04-15 13:39:43 -0700 | [diff] [blame] | 34 | struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name, |
| 35 | uint64_t us_thread_start); |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Get our TLS data. |
| 39 | */ |
| 40 | struct tr2tls_thread_ctx *tr2tls_get_self(void); |
| 41 | |
| 42 | /* |
| 43 | * return true if the current thread is the main thread. |
| 44 | */ |
| 45 | int tr2tls_is_main_thread(void); |
| 46 | |
| 47 | /* |
| 48 | * Free our TLS data. |
| 49 | */ |
| 50 | void tr2tls_unset_self(void); |
| 51 | |
| 52 | /* |
| 53 | * Begin a new nested region and remember the start time. |
| 54 | */ |
| 55 | void tr2tls_push_self(uint64_t us_now); |
| 56 | |
| 57 | /* |
| 58 | * End the innermost nested region. |
| 59 | */ |
| 60 | void tr2tls_pop_self(void); |
| 61 | |
| 62 | /* |
| 63 | * Pop any extra (above the first) open regions on the current |
| 64 | * thread and discard. During a thread-exit, we should only |
| 65 | * have region[0] that was pushed in trace2_thread_start() if |
| 66 | * the thread exits normally. |
| 67 | */ |
| 68 | void tr2tls_pop_unwind_self(void); |
| 69 | |
| 70 | /* |
| 71 | * Compute the elapsed time since the innermost region in the |
| 72 | * current thread started and the given time (usually now). |
| 73 | */ |
| 74 | uint64_t tr2tls_region_elasped_self(uint64_t us); |
| 75 | |
| 76 | /* |
| 77 | * Compute the elapsed time since the main thread started |
| 78 | * and the given time (usually now). This is assumed to |
| 79 | * be the absolute run time of the process. |
| 80 | */ |
| 81 | uint64_t tr2tls_absolute_elapsed(uint64_t us); |
| 82 | |
| 83 | /* |
| 84 | * Initialize the tr2 TLS system. |
| 85 | */ |
| 86 | void tr2tls_init(void); |
| 87 | |
| 88 | /* |
| 89 | * Free all tr2 TLS resources. |
| 90 | */ |
| 91 | void tr2tls_release(void); |
| 92 | |
| 93 | /* |
| 94 | * Protected increment of an integer. |
| 95 | */ |
| 96 | int tr2tls_locked_increment(int *p); |
| 97 | |
Jeff Hostetler | a089724 | 2019-04-15 13:39:43 -0700 | [diff] [blame] | 98 | /* |
| 99 | * Capture the process start time and do nothing else. |
| 100 | */ |
| 101 | void tr2tls_start_process_clock(void); |
| 102 | |
Jeff Hostetler | ee4512e | 2019-02-22 14:25:01 -0800 | [diff] [blame] | 103 | #endif /* TR2_TLS_H */ |