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

compute actual sample rate for profile export #26

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all 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
15 changes: 13 additions & 2 deletions fgprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package fgprof
import (
"fmt"
"io"
"math"
"runtime"
"sort"
"strings"
Expand Down Expand Up @@ -37,16 +38,19 @@ func Start(w io.Writer, format Format) func() error {
const hz = 99
ticker := time.NewTicker(time.Second / hz)
stopCh := make(chan struct{})

prof := &profiler{}
profile := newWallclockProfile()

var sampleCount int64

go func() {
defer ticker.Stop()

for {
select {
case <-ticker.C:
sampleCount++

stacks := prof.GoroutineProfile()
profile.Add(stacks)
case <-stopCh:
Expand All @@ -59,7 +63,14 @@ func Start(w io.Writer, format Format) func() error {
stopCh <- struct{}{}
endTime := time.Now()
profile.Ignore(prof.SelfFrames()...)
return profile.Export(w, format, hz, startTime, endTime)

// Compute actual sample rate in case, due to performance issues, we
// were not actually able to sample at the given hz. Converting
// everything to float avoids integers being rounded in the wrong
// direction and improves the correctness of times in profiles.
duration := endTime.Sub(startTime)
actualHz := float64(sampleCount) / (float64(duration) / 1e9)
return profile.Export(w, format, int(math.Round(actualHz)), startTime, endTime)
}
}

Expand Down
Loading