Skip to content

Commit

Permalink
profiler: add options for block and mutex rates (#814)
Browse files Browse the repository at this point in the history
* add options for block and mutex profile rates

* Update options.go
  • Loading branch information
pmbauer authored Jan 14, 2021
1 parent 9892197 commit f35bc1d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
30 changes: 28 additions & 2 deletions profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (

const (
// DefaultMutexFraction specifies the mutex profile fraction to be used with the mutex profiler.
// For more information or for changing this value, check runtime.SetMutexProfileFraction.
// For more information or for changing this value, check MutexProfileFraction
DefaultMutexFraction = 10

// DefaultBlockRate specifies the default block profiling rate used by the block profiler.
// For more information or for changing this value, check runtime.SetBlockProfileRate.
// For more information or for changing this value, check BlockProfileRate.
DefaultBlockRate = 100

// DefaultPeriod specifies the default period at which profiles will be collected.
Expand Down Expand Up @@ -217,6 +217,32 @@ func CPUDuration(d time.Duration) Option {
}
}

// 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.
// Setting an aggressive rate can hurt performance.
// For more information on this value, check runtime.SetMutexProfileFraction.
func MutexProfileFraction(rate int) Option {
return func(cfg *config) {
cfg.addProfileType(MutexProfile)
cfg.mutexFraction = rate
}
}

// BlockProfileRate turns on block profiles with the given rate.
// The profiler samples an average of one blocking event per rate nanoseconds spent blocked.
// For example, set rate to 1000000000 (aka int(time.Second.Nanoseconds())) to
// record one sample per second a goroutine is blocked.
// A rate of 1 catches every event.
// Setting an aggressive rate can hurt performance.
// For more information on this value, check runtime.SetBlockProfileRate.
func BlockProfileRate(rate int) Option {
return func(cfg *config) {
cfg.addProfileType(BlockProfile)
cfg.blockRate = rate
}
}

// WithProfileTypes specifies the profile types to be collected by the profiler.
func WithProfileTypes(types ...ProfileType) Option {
return func(cfg *config) {
Expand Down
14 changes: 14 additions & 0 deletions profiler/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,20 @@ func TestOptions(t *testing.T) {
assert.Equal(t, 3*time.Second, cfg.cpuDuration)
})

t.Run("MutexProfileFraction", func(t *testing.T) {
var cfg config
MutexProfileFraction(1)(&cfg)
assert.Equal(t, 1, cfg.mutexFraction)
assert.Contains(t, cfg.types, MutexProfile)
})

t.Run("BlockProfileRate", func(t *testing.T) {
var cfg config
BlockProfileRate(1)(&cfg)
assert.Equal(t, 1, cfg.blockRate)
assert.Contains(t, cfg.types, BlockProfile)
})

t.Run("WithProfileTypes", func(t *testing.T) {
var cfg config
WithProfileTypes(HeapProfile)(&cfg)
Expand Down

0 comments on commit f35bc1d

Please sign in to comment.