From 545ddca0c32cb3ccdffed4aa9f063c84b432940c Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Mon, 24 Oct 2022 13:41:00 +0000 Subject: trace2: use size_t alloc,nr_open_regions in tr2tls_thread_ctx Use "size_t" rather than "int" for the "alloc" and "nr_open_regions" fields in the "tr2tls_thread_ctx". These are used by ALLOC_GROW(). Signed-off-by: Jeff Hostetler Signed-off-by: Junio C Hamano --- trace2/tr2_tls.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/trace2/tr2_tls.h b/trace2/tr2_tls.h index b1e327a928e..a90bd639d48 100644 --- a/trace2/tr2_tls.h +++ b/trace2/tr2_tls.h @@ -11,8 +11,8 @@ struct tr2tls_thread_ctx { struct strbuf thread_name; uint64_t *array_us_start; - int alloc; - int nr_open_regions; /* plays role of "nr" in ALLOC_GROW */ + size_t alloc; + size_t nr_open_regions; /* plays role of "nr" in ALLOC_GROW */ int thread_id; }; -- cgit v1.2.3 From 5bbb9251378b0e62bb06e72bda0574e06dee4933 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Mon, 24 Oct 2022 13:41:01 +0000 Subject: tr2tls: clarify TLS terminology Reduce or eliminate use of the term "TLS" in the Trace2 code. The term "TLS" has two popular meanings: "thread-local storage" and "transport layer security". In the Trace2 source, the term is associated with the former. There was concern on the mailing list about it refering to the latter. Update the source and documentation to eliminate the use of the "TLS" term or replace it with the phrase "thread-local storage" to reduce ambiguity. Signed-off-by: Jeff Hostetler Signed-off-by: Junio C Hamano --- Documentation/technical/api-trace2.txt | 8 ++++---- trace2.c | 2 +- trace2.h | 10 +++++----- trace2/tr2_tls.c | 6 +++--- trace2/tr2_tls.h | 18 +++++++++++------- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Documentation/technical/api-trace2.txt b/Documentation/technical/api-trace2.txt index 2afa28bb5aa..431d424f9d5 100644 --- a/Documentation/technical/api-trace2.txt +++ b/Documentation/technical/api-trace2.txt @@ -685,8 +685,8 @@ The "exec_id" field is a command-unique id and is only useful if the `"thread_start"`:: This event is generated when a thread is started. It is - generated from *within* the new thread's thread-proc (for TLS - reasons). + generated from *within* the new thread's thread-proc (because + it needs to access data in the thread's thread-local storage). + ------------ { @@ -698,7 +698,7 @@ The "exec_id" field is a command-unique id and is only useful if the `"thread_exit"`:: This event is generated when a thread exits. It is generated - from *within* the thread's thread-proc (for TLS reasons). + from *within* the thread's thread-proc. + ------------ { @@ -1206,7 +1206,7 @@ worked on 508 items at offset 2032. Thread "th04" worked on 508 items at offset 508. + This example also shows that thread names are assigned in a racy manner -as each thread starts and allocates TLS storage. +as each thread starts. Config (def param) Events:: diff --git a/trace2.c b/trace2.c index 0c0a11e07d5..c1244e45ace 100644 --- a/trace2.c +++ b/trace2.c @@ -52,7 +52,7 @@ static struct tr2_tgt *tr2_tgt_builtins[] = * Force (rather than lazily) initialize any of the requested * builtin TRACE2 targets at startup (and before we've seen an * actual TRACE2 event call) so we can see if we need to setup - * the TR2 and TLS machinery. + * private data structures and thread-local storage. * * Return the number of builtin targets enabled. */ diff --git a/trace2.h b/trace2.h index 88d906ea830..af3c11694cc 100644 --- a/trace2.h +++ b/trace2.h @@ -73,8 +73,7 @@ void trace2_initialize_clock(void); /* * Initialize TRACE2 tracing facility if any of the builtin TRACE2 * targets are enabled in the system config or the environment. - * This includes setting up the Trace2 thread local storage (TLS). - * Emits a 'version' message containing the version of git + * This emits a 'version' message containing the version of git * and the Trace2 protocol. * * This function should be called from `main()` as early as possible in @@ -302,7 +301,8 @@ void trace2_exec_result_fl(const char *file, int line, int exec_id, int code); /* * Emit a 'thread_start' event. This must be called from inside the - * thread-proc to set up the trace2 TLS data for the thread. + * thread-proc to allow the thread to create its own thread-local + * storage. * * Thread names should be descriptive, like "preload_index". * Thread names will be decorated with an instance number automatically. @@ -315,8 +315,8 @@ void trace2_thread_start_fl(const char *file, int line, /* * Emit a 'thread_exit' event. This must be called from inside the - * thread-proc to report thread-specific data and cleanup TLS data - * for the thread. + * thread-proc so that the thread can access and clean up its + * thread-local storage. */ void trace2_thread_exit_fl(const char *file, int line); diff --git a/trace2/tr2_tls.c b/trace2/tr2_tls.c index 7da94aba522..8d2182fbdbb 100644 --- a/trace2/tr2_tls.c +++ b/trace2/tr2_tls.c @@ -69,9 +69,9 @@ struct tr2tls_thread_ctx *tr2tls_get_self(void) ctx = pthread_getspecific(tr2tls_key); /* - * If the thread-proc did not call trace2_thread_start(), we won't - * have any TLS data associated with the current thread. Fix it - * here and silently continue. + * If the current thread's thread-proc did not call + * trace2_thread_start(), then the thread will not have any + * thread-local storage. Create it now and silently continue. */ if (!ctx) ctx = tr2tls_create_self("unknown", getnanotime() / 1000); diff --git a/trace2/tr2_tls.h b/trace2/tr2_tls.h index a90bd639d48..1297509fd23 100644 --- a/trace2/tr2_tls.h +++ b/trace2/tr2_tls.h @@ -3,6 +3,12 @@ #include "strbuf.h" +/* + * 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". + */ + /* * Arbitry limit for thread names for column alignment. */ @@ -17,9 +23,7 @@ struct tr2tls_thread_ctx { }; /* - * Create TLS data for the current thread. This gives us a place to - * put per-thread data, such as thread start time, function nesting - * and a per-thread label for our messages. + * Create thread-local storage for the current thread. * * We assume the first thread is "main". Other threads are given * non-zero thread-ids to help distinguish messages from concurrent @@ -35,7 +39,7 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name, uint64_t us_thread_start); /* - * Get our TLS data. + * Get the thread-local storage pointer of the current thread. */ struct tr2tls_thread_ctx *tr2tls_get_self(void); @@ -45,7 +49,7 @@ struct tr2tls_thread_ctx *tr2tls_get_self(void); int tr2tls_is_main_thread(void); /* - * Free our TLS data. + * Free the current thread's thread-local storage. */ void tr2tls_unset_self(void); @@ -81,12 +85,12 @@ uint64_t tr2tls_region_elasped_self(uint64_t us); uint64_t tr2tls_absolute_elapsed(uint64_t us); /* - * Initialize the tr2 TLS system. + * Initialize thread-local storage for Trace2. */ void tr2tls_init(void); /* - * Free all tr2 TLS resources. + * Free all Trace2 thread-local storage resources. */ void tr2tls_release(void); -- cgit v1.2.3 From 8e8c5ad27a0a35f1be31af48050a8c81892c452d Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Mon, 24 Oct 2022 13:41:02 +0000 Subject: api-trace2.txt: elminate section describing the public trace2 API Eliminate the mostly obsolete `Public API` sub-section from the `Trace2 API` section in the documentation. Strengthen the referral to `trace2.h`. Most of the technical information in this sub-section was moved to `trace2.h` in 6c51cb525d (trace2: move doc to trace2.h, 2019-11-17) to be adjacent to the function prototypes. The remaining text wasn't that useful by itself. Furthermore, the text would need a bit of overhaul to add routines that do not immediately generate a message, such as stopwatch timers. So it seemed simpler to just get rid of it. Signed-off-by: Jeff Hostetler Signed-off-by: Junio C Hamano --- Documentation/technical/api-trace2.txt | 61 ++++------------------------------ 1 file changed, 7 insertions(+), 54 deletions(-) diff --git a/Documentation/technical/api-trace2.txt b/Documentation/technical/api-trace2.txt index 431d424f9d5..9d43909d068 100644 --- a/Documentation/technical/api-trace2.txt +++ b/Documentation/technical/api-trace2.txt @@ -148,20 +148,18 @@ filename collisions). == Trace2 API -All public Trace2 functions and macros are defined in `trace2.h` and -`trace2.c`. All public symbols are prefixed with `trace2_`. +The Trace2 public API is defined and documented in `trace2.h`; refer to it for +more information. All public functions and macros are prefixed +with `trace2_` and are implemented in `trace2.c`. There are no public Trace2 data structures. The Trace2 code also defines a set of private functions and data types in the `trace2/` directory. These symbols are prefixed with `tr2_` -and should only be used by functions in `trace2.c`. +and should only be used by functions in `trace2.c` (or other private +source files in `trace2/`). -== Conventions for Public Functions and Macros - -The functions defined by the Trace2 API are declared and documented -in `trace2.h`. It defines the API functions and wrapper macros for -Trace2. +=== Conventions for Public Functions and Macros Some functions have a `_fl()` suffix to indicate that they take `file` and `line-number` arguments. @@ -172,52 +170,7 @@ take a `va_list` argument. Some functions have a `_printf_fl()` suffix to indicate that they also take a `printf()` style format with a variable number of arguments. -There are CPP wrapper macros and `#ifdef`s to hide most of these details. -See `trace2.h` for more details. The following discussion will only -describe the simplified forms. - -== Public API - -All Trace2 API functions send a message to all of the active -Trace2 Targets. This section describes the set of available -messages. - -It helps to divide these functions into groups for discussion -purposes. - -=== Basic Command Messages - -These are concerned with the lifetime of the overall git process. -e.g: `void trace2_initialize_clock()`, `void trace2_initialize()`, -`int trace2_is_enabled()`, `void trace2_cmd_start(int argc, const char **argv)`. - -=== Command Detail Messages - -These are concerned with describing the specific Git command -after the command line, config, and environment are inspected. -e.g: `void trace2_cmd_name(const char *name)`, -`void trace2_cmd_mode(const char *mode)`. - -=== Child Process Messages - -These are concerned with the various spawned child processes, -including shell scripts, git commands, editors, pagers, and hooks. - -e.g: `void trace2_child_start(struct child_process *cmd)`. - -=== Git Thread Messages - -These messages are concerned with Git thread usage. - -e.g: `void trace2_thread_start(const char *thread_name)`. - -=== Region and Data Messages - -These are concerned with recording performance data -over regions or spans of code. e.g: -`void trace2_region_enter(const char *category, const char *label, const struct repository *repo)`. - -Refer to trace2.h for details about all trace2 functions. +CPP wrapper macros are defined to hide most of these details. == Trace2 Target Formats -- cgit v1.2.3 From a70839cf36c9cad10e514f75fa1bec2e6180dbda Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Mon, 24 Oct 2022 13:41:03 +0000 Subject: trace2: rename the thread_name argument to trace2_thread_start Rename the `thread_name` argument in `tr2tls_create_self()` and `trace2_thread_start()` to be `thread_base_name` to make it clearer that the passed argument is a component used in the construction of the actual `struct tr2tls_thread_ctx.thread_name` variable. The base name will be used along with the thread id to create a unique thread name. This commit does not change how the `thread_name` field is allocated or stored within the `tr2tls_thread_ctx` structure. Signed-off-by: Jeff Hostetler Signed-off-by: Junio C Hamano --- trace2.c | 6 +++--- trace2.h | 11 ++++++----- trace2/tr2_tls.c | 4 ++-- trace2/tr2_tls.h | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/trace2.c b/trace2.c index c1244e45ace..165264dc79a 100644 --- a/trace2.c +++ b/trace2.c @@ -466,7 +466,7 @@ void trace2_exec_result_fl(const char *file, int line, int exec_id, int code) file, line, us_elapsed_absolute, exec_id, code); } -void trace2_thread_start_fl(const char *file, int line, const char *thread_name) +void trace2_thread_start_fl(const char *file, int line, const char *thread_base_name) { struct tr2_tgt *tgt_j; int j; @@ -488,14 +488,14 @@ void trace2_thread_start_fl(const char *file, int line, const char *thread_name) */ trace2_region_enter_printf_fl(file, line, NULL, NULL, NULL, "thread-proc on main: %s", - thread_name); + thread_base_name); return; } us_now = getnanotime() / 1000; us_elapsed_absolute = tr2tls_absolute_elapsed(us_now); - tr2tls_create_self(thread_name, us_now); + tr2tls_create_self(thread_base_name, us_now); for_each_wanted_builtin (j, tgt_j) if (tgt_j->pfn_thread_start_fl) diff --git a/trace2.h b/trace2.h index af3c11694cc..74cdb1354f7 100644 --- a/trace2.h +++ b/trace2.h @@ -304,14 +304,15 @@ void trace2_exec_result_fl(const char *file, int line, int exec_id, int code); * thread-proc to allow the thread to create its own thread-local * storage. * - * Thread names should be descriptive, like "preload_index". - * Thread names will be decorated with an instance number automatically. + * The thread base name should be descriptive, like "preload_index" or + * taken from the thread-proc function. A unique thread name will be + * created from the given base name and the thread id automatically. */ void trace2_thread_start_fl(const char *file, int line, - const char *thread_name); + const char *thread_base_name); -#define trace2_thread_start(thread_name) \ - trace2_thread_start_fl(__FILE__, __LINE__, (thread_name)) +#define trace2_thread_start(thread_base_name) \ + trace2_thread_start_fl(__FILE__, __LINE__, (thread_base_name)) /* * Emit a 'thread_exit' event. This must be called from inside the diff --git a/trace2/tr2_tls.c b/trace2/tr2_tls.c index 8d2182fbdbb..4f7c516ecb6 100644 --- a/trace2/tr2_tls.c +++ b/trace2/tr2_tls.c @@ -31,7 +31,7 @@ void tr2tls_start_process_clock(void) tr2tls_us_start_process = getnanotime() / 1000; } -struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name, +struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name, uint64_t us_thread_start) { struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx)); @@ -50,7 +50,7 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name, strbuf_init(&ctx->thread_name, 0); if (ctx->thread_id) strbuf_addf(&ctx->thread_name, "th%02d:", ctx->thread_id); - strbuf_addstr(&ctx->thread_name, thread_name); + strbuf_addstr(&ctx->thread_name, thread_base_name); if (ctx->thread_name.len > TR2_MAX_THREAD_NAME) strbuf_setlen(&ctx->thread_name, TR2_MAX_THREAD_NAME); diff --git a/trace2/tr2_tls.h b/trace2/tr2_tls.h index 1297509fd23..d4e725f430b 100644 --- a/trace2/tr2_tls.h +++ b/trace2/tr2_tls.h @@ -35,7 +35,7 @@ struct tr2tls_thread_ctx { * In this and all following functions the term "self" refers to the * current thread. */ -struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name, +struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name, uint64_t us_thread_start); /* -- cgit v1.2.3 From 31247936041318d441de5a2681f9bcb79459c726 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Mon, 24 Oct 2022 13:41:04 +0000 Subject: trace2: improve thread-name documentation in the thread-context Improve the documentation of the tr2tls_thread_ctx.thread_name field and its relation to the tr2tls_thread_ctx.thread_id field. Signed-off-by: Jeff Hostetler Signed-off-by: Junio C Hamano --- trace2/tr2_tls.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/trace2/tr2_tls.h b/trace2/tr2_tls.h index d4e725f430b..3ac4380d829 100644 --- a/trace2/tr2_tls.h +++ b/trace2/tr2_tls.h @@ -25,12 +25,15 @@ struct tr2tls_thread_ctx { /* * Create thread-local storage for the current thread. * - * We assume the first thread is "main". Other threads are given - * non-zero thread-ids to help distinguish messages from concurrent - * threads. - * - * Truncate the thread name if necessary to help with column alignment - * in printf-style messages. + * 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. * * In this and all following functions the term "self" refers to the * current thread. -- cgit v1.2.3 From 24a4c45da9e1255df43a87bec1f646b1efa69209 Mon Sep 17 00:00:00 2001 From: Jeff Hostetler Date: Mon, 24 Oct 2022 13:41:05 +0000 Subject: trace2: convert ctx.thread_name from strbuf to pointer Convert the `tr2tls_thread_ctx.thread_name` field from a `strbuf` to a "const char*" pointer. The `thread_name` field is a constant string that is constructed when the context is created. Using a (non-const) `strbuf` structure for it caused some confusion in the past because it implied that someone could rename a thread after it was created. That usage was not intended. Change it to a const pointer to make the intent more clear. Signed-off-by: Jeff Hostetler Signed-off-by: Junio C Hamano --- trace2/tr2_tgt_event.c | 2 +- trace2/tr2_tgt_perf.c | 2 +- trace2/tr2_tls.c | 16 +++++++++------- trace2/tr2_tls.h | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/trace2/tr2_tgt_event.c b/trace2/tr2_tgt_event.c index 37a3163be12..52f9356c695 100644 --- a/trace2/tr2_tgt_event.c +++ b/trace2/tr2_tgt_event.c @@ -90,7 +90,7 @@ static void event_fmt_prepare(const char *event_name, const char *file, jw_object_string(jw, "event", event_name); jw_object_string(jw, "sid", tr2_sid_get()); - jw_object_string(jw, "thread", ctx->thread_name.buf); + jw_object_string(jw, "thread", ctx->thread_name); /* * In brief mode, only emit