Skip to content
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

runtime/cgo: Assign a thread name for C-created threads #47909

Open
antJack opened this issue Aug 23, 2021 · 1 comment
Open

runtime/cgo: Assign a thread name for C-created threads #47909

antJack opened this issue Aug 23, 2021 · 1 comment
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

Comments

@antJack
Copy link

antJack commented Aug 23, 2021

What version of Go are you using (go version)?

$ go version
go version go1.14.13 linux/amd64

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.

image

The execution path is:

runtime.main 
-> startTemplateThread 
-> newm
-> newm1 
-> _cgo_thread_start 
-> x_cgo_thread_start 
-> _cgo_sys_thread_start 
-> _cgo_try_pthread_create 
-> pthread_create

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.

@toothrot toothrot added this to the Backlog milestone Aug 24, 2021
@toothrot toothrot added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 24, 2021
@ianlancetaylor
Copy link
Contributor

Any suggestions as to what the thread name should be?

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
Status: Triage Backlog
Development

No branches or pull requests

4 participants