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 <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Oct 24, 2022 at 13:41 UTC 24a4c45da9e1255df43a87bec1f646b1efa69209
4 files changed +12 -10
trace2/tr2_tgt_event.c
+1 -1
@@ -90,7 +90,7 @@ static void event_fmt_prepare(const char *event_name, const char *file,
90
91 jw_object_string(jw, "event", event_name);
92 jw_object_string(jw, "sid", tr2_sid_get());
93 - jw_object_string(jw, "thread", ctx->thread_name.buf);
93 + jw_object_string(jw, "thread", ctx->thread_name);
94
95 /*
96 * In brief mode, only emit <time> on these 2 event types.
trace2/tr2_tgt_perf.c
+1 -1
@@ -108,7 +108,7 @@ static void perf_fmt_prepare(const char *event_name,
108
109 strbuf_addf(buf, "d%d | ", tr2_sid_depth());
110 strbuf_addf(buf, "%-*s | %-*s | ", TR2_MAX_THREAD_NAME,
111 - ctx->thread_name.buf, TR2FMT_PERF_MAX_EVENT_NAME,
111 + ctx->thread_name, TR2FMT_PERF_MAX_EVENT_NAME,
112 event_name);
113
114 len = buf->len + TR2FMT_PERF_REPO_WIDTH;
trace2/tr2_tls.c
+9 -7
@@ -35,6 +35,7 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
35 uint64_t us_thread_start)
36 {
37 struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx));
38 + struct strbuf buf = STRBUF_INIT;
39
40 /*
41 * Implicitly "tr2tls_push_self()" to capture the thread's start
@@ -47,12 +48,13 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
48
49 ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
50
50 - strbuf_init(&ctx->thread_name, 0);
51 + strbuf_init(&buf, 0);
52 if (ctx->thread_id)
52 - strbuf_addf(&ctx->thread_name, "th%02d:", ctx->thread_id);
53 - strbuf_addstr(&ctx->thread_name, thread_base_name);
54 - if (ctx->thread_name.len > TR2_MAX_THREAD_NAME)
55 - strbuf_setlen(&ctx->thread_name, TR2_MAX_THREAD_NAME);
53 + strbuf_addf(&buf, "th%02d:", ctx->thread_id);
54 + strbuf_addstr(&buf, thread_base_name);
55 + if (buf.len > TR2_MAX_THREAD_NAME)
56 + strbuf_setlen(&buf, TR2_MAX_THREAD_NAME);
57 + ctx->thread_name = strbuf_detach(&buf, NULL);
58
59 pthread_setspecific(tr2tls_key, ctx);
60
@@ -95,7 +97,7 @@ void tr2tls_unset_self(void)
97
98 pthread_setspecific(tr2tls_key, NULL);
99
98 - strbuf_release(&ctx->thread_name);
100 + free((char *)ctx->thread_name);
101 free(ctx->array_us_start);
102 free(ctx);
103 }
@@ -113,7 +115,7 @@ void tr2tls_pop_self(void)
115 struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
116
117 if (!ctx->nr_open_regions)
116 - BUG("no open regions in thread '%s'", ctx->thread_name.buf);
118 + BUG("no open regions in thread '%s'", ctx->thread_name);
119
120 ctx->nr_open_regions--;
121 }
trace2/tr2_tls.h
+1 -1
@@ -15,7 +15,7 @@
15 #define TR2_MAX_THREAD_NAME (24)
16
17 struct tr2tls_thread_ctx {
18 - struct strbuf thread_name;
18 + const char *thread_name;
19 uint64_t *array_us_start;
20 size_t alloc;
21 size_t nr_open_regions; /* plays role of "nr" in ALLOC_GROW */