git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 6/9] trace2: convert ctx.thread_name to flex array

From
JHJeff Hostetler <git@jeffhostetler.com>
Date
Oct 10, 2022, 18:31 UTC
Message-ID
<8e3524e7-5e92-99d6-294e-4c309a3d44ee@jeffhostetler.com>
In-Reply-To
<221005.86y1tus9ps.gmgdl@evledraar.gmail.com>
On 10/5/22 7:14 AM, Ævar Arnfjörð Bjarmason wrote:
Show 46 quoted lines
> 
> On Tue, Oct 04 2022, Jeff Hostetler via GitGitGadget wrote:
> 
>> From: Jeff Hostetler <jeffhost@microsoft.com>
>>
>> Convert the `tr2tls_thread_ctx.thread_name` field from a `strbuf`
>> to a "flex array" at the end of the context structure.
>>
>> 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.
> 
> I think it's been long enough that we could use a reminder about the
> "some confusion", i.e. if it was a bug report or something else.
> 
>> That usage was not intended.  Changing it to a "flex array" will
>> hopefully make the intent more clear.
> 
> I see we had some back & forth back in the original submission, although
> honestly I skimmed this this time around, had forgetten about that, and
> had this pop out at me, and then found my earlier comments.
> 
> I see that exchange didn't end as well as I'd hoped[1], and hopefully we
> can avoid that here. So having looked at this with fresh eyes maybe
> these comments/questions help:
> 
>   * I'm unable to bridge the cap from (paraphrased) "we must change the
>     type" to "mak[ing] the [read-only] intent more clear".
> 
>     I.e. if you go across the codebase and look at various non-const
>     "char name[FLEX_ARRAY]" and add a "const" to them you'll find cases
>     where we re-write the "FLEX_ARRAY" string, e.g. the one in archive.c
>     is one of those (the first grep hit, I stopped looking for others at
>     that point).
> 
>     Making it "const" will yield:
>     
>        archive.c: In function ‘queue_directory’:
>     archive.c:206:29: error: passing argument 1 of ‘xsnprintf’ discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers]
>       206 |         d->len = xsnprintf(d->path, len, "%.*s%s/", (int)base->len, base->buf, filename);
> 
>     So aside from anything else (and I may be misunderstanding this) why
>     does changing it to a FLEX_ARRAY give us the connotation in the
>     confused API user's mind that it shouldn't be messed with that the
>     "strbuf" doesn't give us?
[...]

My change in how we store the thread-name in the thread context was JUST to clarify that it should be treated as a constant string and that code should not try to modify it. There was a comment to that effect last year -- that having it be a strbuf invited one to modify it, when that was not the intent.

That was all I was trying to do here. Just make it "not be a strbuf". Perhaps I lept too far by making it a flex-array. I probably could have just changed the field to a "char *" and detached it from the (now local) strbuf. That would give the same impression, right?

[...]
Show 22 quoted lines
>>   	/*
>>   	 * Implicitly "tr2tls_push_self()" to capture the thread's start
>> @@ -45,15 +56,6 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *name_hint,
>>   	ctx->array_us_start = (uint64_t *)xcalloc(ctx->alloc, sizeof(uint64_t));
>>   	ctx->array_us_start[ctx->nr_open_regions++] = us_thread_start;
>>   
>> -	ctx->thread_id = tr2tls_locked_increment(&tr2_next_thread_id);
>> -
>> -	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, name_hint);
>> -	if (ctx->thread_name.len > TR2_MAX_THREAD_NAME)
>> -		strbuf_setlen(&ctx->thread_name, TR2_MAX_THREAD_NAME);
>> -
>>   	pthread_setspecific(tr2tls_key, ctx);
>>   
>>   	return ctx;
> 
> I found this quote hard to follow because there's functional changes
> there mixed up with code re-arangement, consider leading with a commit
> like:
[...]

sorry about that. yes, there's a bit of churn here because i needed to reorder the thread-name construction to be before we allocated the context so that we'd know the buffer size.

and yes, i accidentally mixed in a function change to move the truncation to the perf backend.

i'll redo all of this.
[...]
Show 15 quoted lines
> <tries it out>
> 
> Anyway, if this area was actually performance critical and we *really
> cared* about avoiding allocations wouldn't we want to skip both the
> "strbuf" there and the "FLEX_ARRAY", and just save away the
> "thread_hint" (which the caller hardcodes) and "thread_nr", and then
> append on-the-fly?
> 
> I came up with the below to do that, it passes all tests, but contains
> micro-optimizations that I don't think we need (e.g. I understood you
> wanted to avoid printf, so it does that).
> 
> But I think it's a useful point of discussion. What test(s) do you have
> where the "master" version, FLEX_ARRAY version, and just not strbuf
> formatting the thing at all differ?
[...]

none of this was about micro-optimization. i was just trying to get the buffer away from a strbuf. i still want it pre-formatted once at thread-start, but that's it.

FWIW, I don't think having it formatted in each event helps anything. it would have to go thru sprintf on every message. it's much better to just format it once in the thread-start.

[...]
> 	diff --git a/json-writer.c b/json-writer.c
[...] 	
> 	+void jw_strbuf_add_thread_name(struct strbuf *out, const char *thread_hint,
> 	+			       int thread_id, int max_len)
> 	+{
[...]
Show 5 quoted lines
> 	+}
> 	+
> 	+void jw_object_thread(struct json_writer *jw, const char *thread_hint,
> 	+		      int thread_id)
> 	+{
[...]
> 	+}
[...]

We should not do this. Just format the name in thread-start and let json-writer print the string as we have been.

Adding thread formatting to json-writer also violates a separation of concerns.

I'll re-roll this commit completely.

thanks Jeff

Previous: Jeff HostetlerNext: Junio C Hamano
Message 10 of 73 in “Trace2 timers and counters and some cleanup”
  1. 0/9 Trace2 timers and counters and some cleanupJeff Hostetler via GitGitGadget, Oct 4, 2022
  2. 1/9 builtin/merge-file: fix compiler warning on MacOS with clang 11.0.0Jeff Hostetler via GitGitGadget, Oct 4, 2022
  3. 2/9 builtin/unpack-objects.c: fix compiler warning on MacOS with clang 11.0.0Jeff Hostetler via GitGitGadget, Oct 4, 2022
  4. 3/9 trace2: use size_t alloc,nr_open_regions in tr2tls_thread_ctxJeff Hostetler via GitGitGadget, Oct 4, 2022
  5. 4/9 tr2tls: clarify TLS terminologyJeff Hostetler via GitGitGadget, Oct 4, 2022
  6. 5/9 trace2: rename trace2 thread_name argument as name_hintJeff Hostetler via GitGitGadget, Oct 4, 2022
  7. 6/9 trace2: convert ctx.thread_name to flex arrayJeff Hostetler via GitGitGadget, Oct 4, 2022
  8. Ævar Arnfjörð BjarmasonOct 5, 2022
  9. Jeff HostetlerOct 6, 2022
  10. Jeff HostetlerOct 10, 2022
  11. Junio C HamanoOct 5, 2022
  12. Ævar Arnfjörð BjarmasonOct 6, 2022
  13. Junio C HamanoOct 6, 2022
  14. trace2 API: don't save a copy of constant "thread_name"Ævar Arnfjörð Bjarmason, Oct 7, 2022
  15. Junio C HamanoOct 7, 2022
  16. Ævar Arnfjörð BjarmasonOct 7, 2022
  17. Jeff HostetlerOct 10, 2022
  18. Ævar Arnfjörð BjarmasonOct 11, 2022
  19. Jeff HostetlerOct 12, 2022
  20. Jeff HostetlerOct 10, 2022
  21. Ævar Arnfjörð BjarmasonOct 11, 2022
  22. Junio C HamanoOct 11, 2022
  23. Jeff HostetlerOct 10, 2022
  24. 7/9 api-trace2.txt: elminate section describing the public trace2 APIJeff Hostetler via GitGitGadget, Oct 4, 2022
  25. 8/9 trace2: add stopwatch timersJeff Hostetler via GitGitGadget, Oct 4, 2022
  26. 9/9 trace2: add global counter mechanismJeff Hostetler via GitGitGadget, Oct 4, 2022
  27. Ævar Arnfjörð BjarmasonOct 5, 2022
  28. Jeff HostetlerOct 6, 2022
  29. Derrick StoleeOct 6, 2022
  30. 0/7 Trace2 timers and counters and some cleanupJeff Hostetler via GitGitGadget, Oct 12, 2022
  31. 1/7 trace2: use size_t alloc,nr_open_regions in tr2tls_thread_ctxJeff Hostetler via GitGitGadget, Oct 12, 2022
  32. 2/7 tr2tls: clarify TLS terminologyJeff Hostetler via GitGitGadget, Oct 12, 2022
  33. Junio C HamanoOct 13, 2022
  34. 3/7 api-trace2.txt: elminate section describing the public trace2 APIJeff Hostetler via GitGitGadget, Oct 12, 2022
  35. 4/7 trace2: rename the thread_name argument to trace2_thread_startJeff Hostetler via GitGitGadget, Oct 12, 2022
  36. Ævar Arnfjörð BjarmasonOct 12, 2022
  37. Jeff HostetlerOct 20, 2022
  38. Junio C HamanoOct 13, 2022
  39. 5/7 trace2: convert ctx.thread_name from strbuf to pointerJeff Hostetler via GitGitGadget, Oct 12, 2022
  40. Junio C HamanoOct 13, 2022
  41. 7/7 trace2: add global counter mechanismJeff Hostetler via GitGitGadget, Oct 12, 2022
  42. 6/7 trace2: add stopwatch timersJeff Hostetler via GitGitGadget, Oct 12, 2022
  43. Junio C HamanoOct 13, 2022
  44. Jeff HostetlerOct 20, 2022
  45. 0/8 Trace2 timers and counters and some cleanupJeff Hostetler via GitGitGadget, Oct 20, 2022
  46. 1/8 trace2: use size_t alloc,nr_open_regions in tr2tls_thread_ctxJeff Hostetler via GitGitGadget, Oct 20, 2022
  47. 2/8 tr2tls: clarify TLS terminologyJeff Hostetler via GitGitGadget, Oct 20, 2022
  48. 4/8 trace2: rename the thread_name argument to trace2_thread_startJeff Hostetler via GitGitGadget, Oct 20, 2022
  49. 3/8 api-trace2.txt: elminate section describing the public trace2 APIJeff Hostetler via GitGitGadget, Oct 20, 2022
  50. 5/8 trace2: improve thread-name documentation in the thread-contextJeff Hostetler via GitGitGadget, Oct 20, 2022
  51. Ævar Arnfjörð BjarmasonOct 20, 2022
  52. Jeff HostetlerOct 20, 2022
  53. 6/8 trace2: convert ctx.thread_name from strbuf to pointerJeff Hostetler via GitGitGadget, Oct 20, 2022
  54. 7/8 trace2: add stopwatch timersJeff Hostetler via GitGitGadget, Oct 20, 2022
  55. Junio C HamanoOct 20, 2022
  56. Jeff HostetlerOct 20, 2022
  57. Junio C HamanoOct 20, 2022
  58. Jeff HostetlerOct 21, 2022
  59. 8/8 trace2: add global counter mechanismJeff Hostetler via GitGitGadget, Oct 20, 2022
  60. 0/8 Trace2 timers and counters and some cleanupJeff Hostetler via GitGitGadget, Oct 24, 2022
  61. 1/8 trace2: use size_t alloc,nr_open_regions in tr2tls_thread_ctxJeff Hostetler via GitGitGadget, Oct 24, 2022
  62. Junio C HamanoOct 24, 2022
  63. Derrick StoleeOct 25, 2022
  64. Junio C HamanoOct 25, 2022
  65. 3/8 api-trace2.txt: elminate section describing the public trace2 APIJeff Hostetler via GitGitGadget, Oct 24, 2022
  66. 7/8 trace2: add stopwatch timersJeff Hostetler via GitGitGadget, Oct 24, 2022
  67. 2/8 tr2tls: clarify TLS terminologyJeff Hostetler via GitGitGadget, Oct 24, 2022
  68. 8/8 trace2: add global counter mechanismJeff Hostetler via GitGitGadget, Oct 24, 2022
  69. 6/8 trace2: convert ctx.thread_name from strbuf to pointerJeff Hostetler via GitGitGadget, Oct 24, 2022
  70. 5/8 trace2: improve thread-name documentation in the thread-contextJeff Hostetler via GitGitGadget, Oct 24, 2022
  71. 4/8 trace2: rename the thread_name argument to trace2_thread_startJeff Hostetler via GitGitGadget, Oct 24, 2022
  72. Derrick StoleeOct 25, 2022
  73. Junio C HamanoOct 25, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.