Skip to content

Commit

Permalink
profiler: clamp CPU duration at profile period (#1486)
Browse files Browse the repository at this point in the history
If a user sets a profile period shorter than the default CPU duration,
but neglects to update the CPU duration, CPU profiling will still take
the default duration, exceeding the expected profile period. Add a check
to clamp the CPU duration at the configured period.
  • Loading branch information
nsrip-dd committed Sep 29, 2022
1 parent f148b10 commit 31366dd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 3 additions & 0 deletions profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func newProfiler(opts ...Option) (*profiler, error) {
return nil, fmt.Errorf("unknown profile type: %d", pt)
}
}
if cfg.cpuDuration > cfg.period {
cfg.cpuDuration = cfg.period
}
if cfg.logStartup {
logStartup(cfg)
}
Expand Down
2 changes: 0 additions & 2 deletions profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ func TestStopLatency(t *testing.T) {
Start(
WithAgentAddr(server.Listener.Addr().String()),
WithPeriod(time.Second),
CPUDuration(time.Second),
WithUploadTimeout(time.Hour),
)

Expand Down Expand Up @@ -412,7 +411,6 @@ func TestCorrectTags(t *testing.T) {
HeapProfile,
),
WithPeriod(10*time.Millisecond),
CPUDuration(10*time.Millisecond),
WithService("xyz"),
WithEnv("testing"),
WithTags("foo:bar", "baz:bonk"),
Expand Down

0 comments on commit 31366dd

Please sign in to comment.