Skip to content

Commit

Permalink
fix scaler
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev committed Sep 8, 2023
1 parent 368a7b9 commit 69aaf3b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
19 changes: 3 additions & 16 deletions godeltaprof/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type BlockProfiler struct {
impl pprof.DeltaMutexProfiler
mutex sync.Mutex
runtimeProfile func([]runtime.BlockProfileRecord) (int, bool)
scaleProfile func(int64, float64) (int64, float64)
scaleProfile pprof.MutexProfileScaler
}

// NewMutexProfiler creates a new BlockProfiler instance for profiling mutex contention.
Expand All @@ -39,7 +39,7 @@ type BlockProfiler struct {
// ...
// err := mp.Profile(someWriter)
func NewMutexProfiler() *BlockProfiler {
return &BlockProfiler{runtimeProfile: runtime.MutexProfile, scaleProfile: scaleMutexProfile}
return &BlockProfiler{runtimeProfile: runtime.MutexProfile, scaleProfile: pprof.ScalerMutexProfile}
}

// NewBlockProfiler creates a new BlockProfiler instance for profiling goroutine blocking events.
Expand All @@ -51,7 +51,7 @@ func NewMutexProfiler() *BlockProfiler {
// ...
// err := bp.Profile(someWriter)
func NewBlockProfiler() *BlockProfiler {
return &BlockProfiler{runtimeProfile: runtime.BlockProfile, scaleProfile: scaleBlockProfile}
return &BlockProfiler{runtimeProfile: runtime.BlockProfile, scaleProfile: pprof.ScalerBlockProfile}
}

func (d *BlockProfiler) Profile(w io.Writer) error {
Expand All @@ -73,16 +73,3 @@ func (d *BlockProfiler) Profile(w io.Writer) error {

return d.impl.PrintCountCycleProfile(w, "contentions", "delay", d.scaleProfile, p)
}

func scaleMutexProfile(cnt int64, ns float64) (int64, float64) {
period := runtime.SetMutexProfileFraction(-1)
return cnt * int64(period), ns * float64(period)
}

func scaleBlockProfile(cnt int64, ns float64) (int64, float64) {
// Do nothing.
// The current way of block profile sampling makes it
// hard to compute the unsampled number. The legacy block
// profile parse doesn't attempt to scale or unsample.
return cnt, ns
}
4 changes: 2 additions & 2 deletions godeltaprof/internal/pprof/delta_mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type DeltaMutexProfiler struct {
// are done because The proto expects count and time (nanoseconds) instead of count
// and the number of cycles for block, contention profiles.
// Possible 'scaler' functions are scaleBlockProfile and scaleMutexProfile.
func (d *DeltaMutexProfiler) PrintCountCycleProfile(w io.Writer, countName, cycleName string, scaler func(int64, float64) (int64, float64), records []runtime.BlockProfileRecord) error {
func (d *DeltaMutexProfiler) PrintCountCycleProfile(w io.Writer, countName, cycleName string, scaler MutexProfileScaler, records []runtime.BlockProfileRecord) error {
// Output profile in protobuf form.
b := newProfileBuilder(w)
b.pbValueType(tagProfile_PeriodType, countName, "count")
Expand All @@ -27,7 +27,7 @@ func (d *DeltaMutexProfiler) PrintCountCycleProfile(w io.Writer, countName, cycl
values := []int64{0, 0}
var locs []uint64
for _, r := range records {
count, nanosec := scaler(r.Count, float64(r.Cycles)/cpuGHz)
count, nanosec := scaleMutexProfile(scaler, r.Count, float64(r.Cycles)/cpuGHz)
inanosec := int64(nanosec)

// do the delta
Expand Down
27 changes: 27 additions & 0 deletions godeltaprof/internal/pprof/mutex_scale_go19.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//go:build go1.16 && !go1.20
// +build go1.16,!go1.20

package pprof

import "runtime"

type MutexProfileScaler struct {
f func(cnt int64, ns float64) (cnt int64, ns float64)

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.16)

duplicate argument cnt

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.16)

duplicate argument ns

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.17)

duplicate argument cnt

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.17)

duplicate argument ns

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.18)

cnt redeclared in this block

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.18)

other declaration of cnt

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.18)

ns redeclared in this block

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.18)

other declaration of ns

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.19)

cnt redeclared in this block

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.19)

other declaration of cnt

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.19)

ns redeclared in this block

Check failure on line 9 in godeltaprof/internal/pprof/mutex_scale_go19.go

View workflow job for this annotation

GitHub Actions / go (1.19)

other declaration of ns
}

func scaleMutexProfile(scaler MutexProfileScaler, cnt int64, ns float64) (int64, float64) {
return scaler.f(cnt, ns)
}

var ScalerMutexProfile = MutexProfileScaler{func(cnt int64, ns float64) (int64, float64) {
period := runtime.SetMutexProfileFraction(-1)
return cnt * int64(period), ns * float64(period)
}}

var ScalerBlockProfile = MutexProfileScaler{func(cnt int64, ns float64) (int64, float64) {
// Do nothing.
// The current way of block profile sampling makes it
// hard to compute the unsampled number. The legacy block
// profile parse doesn't attempt to scale or unsample.
return cnt, ns
}}
15 changes: 15 additions & 0 deletions godeltaprof/internal/pprof/mutex_scale_go20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build go1.20
// +build go1.20

package pprof

type MutexProfileScaler struct {
}

func scaleMutexProfile(_ MutexProfileScaler, cnt int64, ns float64) (int64, float64) {
return cnt, ns
}

var ScalerMutexProfile = MutexProfileScaler{}

var ScalerBlockProfile = MutexProfileScaler{}

0 comments on commit 69aaf3b

Please sign in to comment.