Skip to content

runtime/pprof: misleading error log when starting CPU profiling after calling runtime.SetCPUProfileRate #79900

Description

@nsrip-dd

Go version

go1.26.4

Output of go env in your module/workspace:

N/A

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.

Metadata

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Fields

No fields configured for issues without a type.

Projects

Status
Done

Relationships

None yet

Development

No branches or pull requests

Issue actions