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

profiler: add CPUProfileRate option #1243

Merged
merged 3 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type config struct {
types map[ProfileType]struct{}
period time.Duration
cpuDuration time.Duration
cpuProfileRate int
uploadTimeout time.Duration
maxGoroutinesWait int
mutexFraction int
Expand Down Expand Up @@ -126,6 +127,7 @@ func logStartup(c *config) {
ProfilePeriod string `json:"profile_period"`
EnabledProfiles []string `json:"enabled_profiles"`
CPUDuration string `json:"cpu_duration"`
CPUProfileRate int `json:"cpu_profile_rate"`
BlockProfileRate int `json:"block_profile_rate"`
MutexProfileFraction int `json:"mutex_profile_fraction"`
MaxGoroutinesWait int `json:"max_goroutines_wait"`
Expand All @@ -146,6 +148,7 @@ func logStartup(c *config) {
Tags: c.tags,
ProfilePeriod: c.period.String(),
CPUDuration: c.cpuDuration.String(),
CPUProfileRate: c.cpuProfileRate,
BlockProfileRate: c.blockRate,
MutexProfileFraction: c.mutexFraction,
MaxGoroutinesWait: c.maxGoroutinesWait,
Expand Down Expand Up @@ -346,6 +349,21 @@ func CPUDuration(d time.Duration) Option {
}
}

// CPUProfileRate sets the sampling frequency for CPU profiling. A sample will
// be taken once for every (1 / hz) seconds of on-CPU time. If not given,
// profiling will use the default rate from the runtime/pprof.StartCPUProfile
// function, which is 100 as of Go 1.0.
//
// Setting a different profile rate will result in a spurious warning every time
// CPU profling is started, like "cannot set cpu profile rate until previous
// profile has finished". This is a known issue, but the rate will still be set
// correctly and CPU profiling will work.
func CPUProfileRate(hz int) Option {
return func(cfg *config) {
cfg.cpuProfileRate = hz
}
}

// MutexProfileFraction turns on mutex profiles with rate indicating the fraction
// of mutex contention events reported in the mutex profile.
// On average, 1/rate events are reported.
Expand Down
7 changes: 7 additions & 0 deletions profiler/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func TestOptions(t *testing.T) {
assert.Equal(t, 3*time.Second, cfg.cpuDuration)
})

t.Run("CPUProfileRate", func(t *testing.T) {
var cfg config
CPUProfileRate(1000)(&cfg)
assert.Equal(t, 1000, cfg.cpuProfileRate)
})

t.Run("MutexProfileFraction", func(t *testing.T) {
var cfg config
MutexProfileFraction(1)(&cfg)
Expand Down Expand Up @@ -330,6 +336,7 @@ func TestDefaultConfig(t *testing.T) {
assert.True(ok)
assert.Equal(DefaultPeriod, cfg.period)
assert.Equal(DefaultDuration, cfg.cpuDuration)
assert.Equal(0, cfg.cpuProfileRate)
assert.Equal(DefaultMutexFraction, cfg.mutexFraction)
assert.Equal(DefaultBlockRate, cfg.blockRate)
assert.Contains(cfg.tags, "runtime-id:"+globalconfig.RuntimeID())
Expand Down
7 changes: 7 additions & 0 deletions profiler/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ var profileTypes = map[ProfileType]profileType{
Filename: "cpu.pprof",
Collect: func(_ profileType, p *profiler) ([]byte, error) {
var buf bytes.Buffer
if p.cfg.cpuProfileRate != 0 {
// The profile has to be set each time before
// profiling is started. Otherwise,
// runtime/pprof.StartCPUProfile will set the
// rate itself.
runtime.SetCPUProfileRate(p.cfg.cpuProfileRate)
}
if err := startCPUProfile(&buf); err != nil {
return nil, err
}
Expand Down