Go version
go1.26.4
Output of go env in your module/workspace:
What did you do?
Ran this code:
package main
import (
"io"
"runtime"
"runtime/pprof"
)
func main() {
runtime.SetCPUProfileRate(50)
pprof.StartCPUProfile(io.Discard)
defer pprof.StopCPUProfile()
}
Playground: https://go.dev/play/p/peflpTEukjI
What did you see happen?
I saw the following output to standard error:
runtime: cannot set cpu profile rate until previous profile has finished.
What did you expect to see?
No output. runtime.SetCPUProfileRate is the only way to configure the CPU profiler's sampling frequency, since pprof.StartCPUProfile doesn't provide an option to change the rate. (See #42502 and #74545 for alternative API proposals.) The error is misleading. CPU profiling is working after the second call, sampling at 50 Hz as intended. The runtime.SetCPUProfileRate has a hard-coded print if it's called a second consecutive time with a non-zero sample rate, and pprof.StartCPUProfile calls the function to set the rate to 100. That second call is what's printing the error log.
This log is particularly annoying for a continuous profiling library like Datadog's. We collect profiling data at a regular interval, which means regularly starting and stopping CPU profiling. If we try to set the sample rate, we get the error log every minute. We recently wanted to reduce the sampling frequency for a program which produces quite large CPU profiles. But this hard-coded error log is problematic at the scale at which we run the program.
Go version
go1.26.4
Output of
go envin your module/workspace:What did you do?
Ran this code:
Playground: https://go.dev/play/p/peflpTEukjI
What did you see happen?
I saw the following output to standard error:
What did you expect to see?
No output.
runtime.SetCPUProfileRateis the only way to configure the CPU profiler's sampling frequency, sincepprof.StartCPUProfiledoesn't provide an option to change the rate. (See #42502 and #74545 for alternative API proposals.) The error is misleading. CPU profiling is working after the second call, sampling at 50 Hz as intended. Theruntime.SetCPUProfileRatehas a hard-coded print if it's called a second consecutive time with a non-zero sample rate, andpprof.StartCPUProfilecalls the function to set the rate to 100. That second call is what's printing the error log.This log is particularly annoying for a continuous profiling library like Datadog's. We collect profiling data at a regular interval, which means regularly starting and stopping CPU profiling. If we try to set the sample rate, we get the error log every minute. We recently wanted to reduce the sampling frequency for a program which produces quite large CPU profiles. But this hard-coded error log is problematic at the scale at which we run the program.