MCPcopy
hub / github.com/felixge/fgprof / Start

Function Start

fgprof.go:33–75  ·  view source on GitHub ↗

Start begins profiling the goroutines of the program and returns a function that needs to be invoked by the caller to stop the profiling and write the results to w using the given format.

(w io.Writer, format Format)

Source from the content-addressed store, hash-verified

31// that needs to be invoked by the caller to stop the profiling and write the
32// results to w using the given format.
33func Start(w io.Writer, format Format) func() error {
34 startTime := time.Now()
35
36 // Go's CPU profiler uses 100hz, but 99hz might be less likely to result in
37 // accidental synchronization with the program we're profiling.
38 const hz = 99
39 ticker := time.NewTicker(time.Second / hz)
40 stopCh := make(chan struct{})
41 prof := &profiler{}
42 profile := newWallclockProfile()
43
44 var sampleCount int64
45
46 go func() {
47 defer ticker.Stop()
48
49 for {
50 select {
51 case <-ticker.C:
52 sampleCount++
53
54 stacks := prof.GoroutineProfile()
55 profile.Add(stacks)
56 case <-stopCh:
57 return
58 }
59 }
60 }()
61
62 return func() error {
63 stopCh <- struct{}{}
64 endTime := time.Now()
65 profile.Ignore(prof.SelfFrames()...)
66
67 // Compute actual sample rate in case, due to performance issues, we
68 // were not actually able to sample at the given hz. Converting
69 // everything to float avoids integers being rounded in the wrong
70 // direction and improves the correctness of times in profiles.
71 duration := endTime.Sub(startTime)
72 actualHz := float64(sampleCount) / (float64(duration) / 1e9)
73 return profile.Export(w, format, int(math.Round(actualHz)), startTime, endTime)
74 }
75}
76
77// profiler provides a convenient and performant way to access
78// runtime.GoroutineProfile().

Callers 2

HandlerFunction · 0.85
TestStartFunction · 0.85

Calls 6

GoroutineProfileMethod · 0.95
SelfFramesMethod · 0.95
newWallclockProfileFunction · 0.85
AddMethod · 0.80
IgnoreMethod · 0.80
ExportMethod · 0.80

Tested by 1

TestStartFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…