runtime/cgo: Assign a thread name for C-created threads #47909
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What did you do?
Profiling an embedding Go program. That is, calling go functions from C++ environment through cgo.
However, when I tried to profile my program with perf (since pprof cannot provide as much info about C++ as perf), I found that some of the executions of Go worker pool were laid on a C++ thread, as shown in the left part of the screenshot. It confused me why those worker pool codes ran on my C++ thread and did waste a lot of time.
Finally, I found that the newly created thread shares the same thread name as its father, i.e., the C++ worker thread, which causes the perf to draw the Go part on C++ worker thread.
The execution path is:
And the newly created thread inherits the thread name from its parent thread, and that causes the flame graph to misbehave as shown above. The solution to this problem is also simple: call pthread_setname_np just after the creation of new thread.
What did you expect to see?
The newly created cgo template thread should use a different thread name from its parent.
What did you see instead?
cgo template thread uses the same thread name as its parent, which causes misleading when perf-ing the program.
The text was updated successfully, but these errors were encountered: