-
Notifications
You must be signed in to change notification settings - Fork 38.7k
threadnames: don't allocate memory in ThreadRename #18848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is called many times, let's use a buffer on the stack instead
4cc45a9 to
9d7c4c4
Compare
|
Why is |
It seems like a more serious bug than memory over-allocation. |
|
hmmm I went into this from a top-down approach. I'm not familiar with the internals of core, but it's possible the tool I'm using (heaptrack) might have false positives or be flat out wrong. all I know is that doing this PR made the ThreadRename heaptrack logs go away. There are a bunch of callsites but none of them look too suspicious... |
|
Uhm, if this is that case, the underlying issue must be addressed, not the symptom. It's supposed to only be called at initial thread creation and bitcoin core's threads are more or less static. Could this also explain why |
|
is there some mechanism where thread jobs would allocate and destroy thread_local heap storage every time or something? I'm not that familiar with thread_local... |
|
It's somewhat of a mystery. It's up to the compiler - OS - libc - architecture combination how Could you try |
|
going to |
|
Ouch. Looks like you're on to something. Which platform are you checking on? On x86_64, Ubuntu 20.04, with gcc |
|
I have the same. Why can't I reproduce this. This is fucking madness. Edit: can you please trap the function in |
|
Tested IBD on x86_64, Linux Mint 19.3 with the patched |
|
I have the feeling it's more subtle. It might still be that it is called many times, it just doesn't show up in this profile. |
|
ok I'm dumb, I think I was reading this pane wrong. It's showing the top-down allocations. so that means it allocated that many times UP to the point it allocated once in ThreadRename. so perhaps the thread_local performance thing is just because LogPrintF has to allocate a bit more to display thread info? Seems like 6 minutes is lot though just for a print line... |
|
this might still be useful for the case where we're logging thread names... but it's pretty minor compared to other huge sources of allocations. going to close this for now. |



In my investigations into heap allocations during IBD, I discovered that ThreadRename is called many many times, in some cases it does multiple string allocations as well. This modifies ThreadRename to not allocate memory, opting for a static thread-local buffer instead.
I also create a new ThreadRenameWithWorker helper to avoid strprintf allocations.
before:

after:
