-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpu.go
136 lines (109 loc) · 2.99 KB
/
cpu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package goprofui
import (
"bytes"
"errors"
"io"
"runtime"
"runtime/pprof"
"time"
"github.com/wirelessregistry/goprofui/internal/profile"
)
var ErrProfileFailed = errors.New("Could not start profiler")
type Profile struct {
TotalSamples uint32 // count of samples
TotalFuncCalls uint32 // count of all funcs in all sample
FuncFrequency map[string]uint32
UniqueFuncFrequency map[string]uint32
FuncDict map[uint64]string
Prof *profile.Profile
}
func EmptyProfile(prof *profile.Profile) *Profile {
return &Profile{
TotalSamples: 0,
TotalFuncCalls: 0,
FuncFrequency: make(map[string]uint32),
UniqueFuncFrequency: make(map[string]uint32),
FuncDict: nil,
Prof: prof,
}
}
func NewProfile(p *profile.Profile) *Profile {
fp := EmptyProfile(p)
fp.FuncDict = LocationsToFuncNames(p.Location)
for _, sample := range p.Sample {
nSamples := uint32(sample.Value[0])
fp.TotalSamples += nSamples
fp.TotalFuncCalls += nSamples * uint32(len(sample.Location))
seenFunc := make(map[string]bool)
for _, loc := range sample.Location {
fp.FuncFrequency[fp.FuncDict[loc.ID]] += nSamples
if seenFunc[fp.FuncDict[loc.ID]] == false {
fp.UniqueFuncFrequency[fp.FuncDict[loc.ID]] += 1
seenFunc[fp.FuncDict[loc.ID]] = true
}
}
}
return fp
}
func LocationsToFuncNames(locations []*profile.Location) map[uint64]string {
funcs := make(map[uint64]string)
for _, loc := range locations {
funcs[loc.ID] = runtime.FuncForPC(uintptr(loc.Address)).Name()
}
return funcs
}
/*
The D3 Flame Graph is at: https://github.com/spiermar/d3-flame-graph
The D3 lib expects the following tree struct:
1) root --> *children
2) child --> *children
Invariant:
If a Func name appears at the same stack level in two samples and
the call stack prefix is also the same, then
there is exactly one node at that level in the tree to denote Func.
*/
func (p *Profile) ParseForD3FlameGraph(w io.Writer) error {
root := &Node{
Name: "root",
Value: int64(0),
Children: make(map[string]*Node),
}
for _, sample := range p.Prof.Sample {
frames := make([]string, len(sample.Location))
for i, loc := range sample.Location {
frames[i] = p.FuncDict[loc.ID]
}
reverse(frames)
root.Add(frames, sample.Value[0])
}
out, err := root.MarshalText()
if err != nil {
return err
}
w.Write(out)
return nil
}
func reverse(words []string) {
for i, j := 0, len(words)-1; i < j; i, j = i+1, j-1 {
words[i], words[j] = words[j], words[i]
}
}
// Non-blocking function to get a profile
func CPUProfile(duration time.Duration) chan *Profile {
var buf bytes.Buffer
ch := make(chan *Profile, 1)
go func() {
err := pprof.StartCPUProfile(&buf)
if err != nil {
// StartCPUProfile failed, so no writes yet.
// Can change header back to text content
// and send error code.
ch <- nil
}
time.Sleep(duration)
pprof.StopCPUProfile()
p, _ := profile.Parse(&buf)
ch <- NewProfile(p)
}()
return ch
}