Skip to content

Commit 24a4c45

Browse files
jeffhostetlergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3124793 commit 24a4c45

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

trace2/tr2_tgt_event.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static void event_fmt_prepare(const char *event_name, const char *file,
9090

9191
jw_object_string(jw, "event", event_name);
9292
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);
9494

9595
/*
9696
* In brief mode, only emit <time> on these 2 event types.

trace2/tr2_tgt_perf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static void perf_fmt_prepare(const char *event_name,
108108

109109
strbuf_addf(buf, "d%d | ", tr2_sid_depth());
110110
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,
112112
event_name);
113113

114114
len = buf->len + TR2FMT_PERF_REPO_WIDTH;

trace2/tr2_tls.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_base_name,
3535
uint64_t us_thread_start)
3636
{
3737
struct tr2tls_thread_ctx *ctx = xcalloc(1, sizeof(*ctx));
38+
struct strbuf buf = STRBUF_INIT;
3839

3940
/*
4041
* 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,
4748

4849
ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
4950

50-
strbuf_init(&ctx->thread_name, 0);
51+
strbuf_init(&buf, 0);
5152
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);
5658

5759
pthread_setspecific(tr2tls_key, ctx);
5860

@@ -95,7 +97,7 @@ void tr2tls_unset_self(void)
9597

9698
pthread_setspecific(tr2tls_key, NULL);
9799

98-
strbuf_release(&ctx->thread_name);
100+
free((char *)ctx->thread_name);
99101
free(ctx->array_us_start);
100102
free(ctx);
101103
}
@@ -113,7 +115,7 @@ void tr2tls_pop_self(void)
113115
struct tr2tls_thread_ctx *ctx = tr2tls_get_self();
114116

115117
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);
117119

118120
ctx->nr_open_regions--;
119121
}

trace2/tr2_tls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define TR2_MAX_THREAD_NAME (24)
1616

1717
struct tr2tls_thread_ctx {
18-
struct strbuf thread_name;
18+
const char *thread_name;
1919
uint64_t *array_us_start;
2020
size_t alloc;
2121
size_t nr_open_regions; /* plays role of "nr" in ALLOC_GROW */

0 commit comments

Comments
 (0)