go pprof是否随机选择一个线程接收信号?

huangapple go评论88阅读模式
英文:

Is go pprof random pick one thread receiving signal

问题

在这篇文章中(https://www.datadoghq.com/blog/engineering/profiling-improvements-in-go-1-18/),有一句话让我感到困惑:“top 命令显示使用了 20 个 CPU 核心,预期的信号速率应该是每秒 2,000 个信号。然而,生成的性能分析报告只包含了平均每秒 240 个堆栈跟踪。”。

我感到困惑的是,在函数 runtime.SetCPUProfileRate 中,只将当前线程的 profileHz 设置为 100。
而且我知道在 Linux 中,信号会随机选择一个线程来执行。
因此,profileHz 不为零的线程每秒只会接收大约 2000 / 20 = 100 个信号。
所以平均每秒的堆栈跟踪应该是大约 100 个。

为什么这篇文章说每秒会产生 240 甚至更多的堆栈跟踪?

英文:

In this article: https://www.datadoghq.com/blog/engineering/profiling-improvements-in-go-1-18/.
Below word confusing me:reporting 20 CPU cores being used by top, the expected signal rate should have been 2,000 signals per second. However, the resulting profile only contained an average of 240 stack traces per second. .

What i am confusing is that in func runtime.SetCPUProfileRate, only set the current thread profileHz to 100.
And I know that in linux, the signal is randomly pick one thread to execute.
So thread which profileHz is not zero will only receive about 2000 / 20 = 100 signal percond.
So the average stack traces is about 100 per second.

Why this article said that the stack trace will produce 240 and even more stack traces per second?

答案1

得分: 1

似乎在初始化时为所有线程添加了sigmask:

// 在初始化新的 m 时调用 minitSignalStack 来设置备用信号栈。
// 如果线程没有设置备用信号栈(正常情况),则将备用信号栈设置为 gsignal 栈。
// 如果线程设置了备用信号栈(当非 Go 线程设置备用信号栈并调用 Go 函数时),则将 gsignal 栈设置为备用信号栈。
// 如果没有使用 cgo(无论是否已经设置),也将备用信号栈设置为 gsignal 栈。
// 记录在 newSigstack 中做出的选择,以便在 unminit 中可以撤消。
func minitSignalStack() {

因此,只有启动 pprof 的线程才能接收到 sigprof 信号。

英文:

It seems that go add sigmask for all thread in init:

// minitSignalStack is called when initializing a new m to set the
// alternate signal stack. If the alternate signal stack is not set
// for the thread (the normal case) then set the alternate signal
// stack to the gsignal stack. If the alternate signal stack is set
// for the thread (the case when a non-Go thread sets the alternate
// signal stack and then calls a Go function) then set the gsignal
// stack to the alternate signal stack. We also set the alternate
// signal stack to the gsignal stack if cgo is not used (regardless
// of whether it is already set). Record which choice was made in
// newSigstack, so that it can be undone in unminit.
func minitSignalStack() {

so only the thread that start pprof can receive sigprof signal.

huangapple
  • 本文由 发表于 2022年6月30日 14:30:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/72811192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定