Skip to content

Commit

Permalink
compute actual sample rate for profile export (#26)
Browse files Browse the repository at this point in the history
In cases where due to either an excessive number of goroutines, or excessive
load on the system, we are not able to sample at the given rate, we can compute
the actual rate we were able to sample and use that when exporting the profile
so that timing information in the profile is more accurate and sensible.

With this code in place, a function which is on the stack for the entirety of a
27 second profile, will always display 27s when viewed, whereas previously it
could display less time.
  • Loading branch information
jaffee committed Oct 9, 2023
1 parent ececf76 commit 949cd9f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions fgprof.go
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

0 comments on commit 949cd9f

Please sign in to comment.