forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflamegraph.go
203 lines (180 loc) · 5.11 KB
/
flamegraph.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package profile
import (
"fmt"
"math"
"sort"
"github.com/google/pprof/profile"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/texttree"
)
type flamegraphNode struct {
cumValue int64
children map[uint64]*flamegraphNode
name string
}
func newFlamegraphNode() *flamegraphNode {
return &flamegraphNode{
cumValue: 0,
children: make(map[uint64]*flamegraphNode),
name: "",
}
}
// add the value from a sample into the flamegraph DAG.
// This method should only be called on the root node.
func (n *flamegraphNode) add(sample *profile.Sample) {
// FIXME: we take the last sample value by default, but some profiles have multiple samples.
// - allocs: alloc_objects, alloc_space, inuse_objects, inuse_space
// - block: contentions, delay
// - cpu: samples, cpu
// - heap: alloc_objects, alloc_space, inuse_objects, inuse_space
// - mutex: contentions, delay
value := sample.Value[len(sample.Value)-1]
if value == 0 {
return
}
locs := sample.Location
for {
n.cumValue += value
if len(locs) == 0 {
return
}
// The previous implementation in TiDB identify nodes using location ID,
// but `go tool pprof` identify nodes using function ID. Should we follow?
loc := locs[len(locs)-1]
locID := loc.ID
child, ok := n.children[locID]
if !ok {
child = newFlamegraphNode()
n.children[locID] = child
if len(loc.Line) > 0 && loc.Line[0].Function != nil {
child.name = locs[len(locs)-1].Line[0].Function.Name
}
}
locs = locs[:len(locs)-1]
n = child
}
}
// collectFuncUsage collect the value by given function name
func (n *flamegraphNode) collectFuncUsage(name string) int64 {
if n.name == name {
return n.cumValue
}
if len(n.children) == 0 {
return 0
}
var usage int64 = 0
for _, child := range n.children {
usage = child.collectFuncUsage(name) + usage
}
return usage
}
type flamegraphNodeWithLocation struct {
*flamegraphNode
locID uint64
}
// sortedChildren returns a list of children of this node, sorted by each
// child's cumulative value.
func (n *flamegraphNode) sortedChildren() []flamegraphNodeWithLocation {
children := make([]flamegraphNodeWithLocation, 0, len(n.children))
for locID, child := range n.children {
children = append(children, flamegraphNodeWithLocation{
flamegraphNode: child,
locID: locID,
})
}
sort.Slice(children, func(i, j int) bool {
a, b := children[i], children[j]
if a.cumValue != b.cumValue {
return a.cumValue > b.cumValue
}
return a.locID < b.locID
})
return children
}
type flamegraphCollector struct {
rows [][]types.Datum
locations map[uint64]*profile.Location
total int64
rootChild int
}
func newFlamegraphCollector(p *profile.Profile) *flamegraphCollector {
locations := make(map[uint64]*profile.Location, len(p.Location))
for _, loc := range p.Location {
locations[loc.ID] = loc
}
return &flamegraphCollector{locations: locations}
}
func (c *flamegraphCollector) locationName(locID uint64) (funcName, fileLine string) {
loc := c.locations[locID]
if len(loc.Line) == 0 {
return "<unknown>", "<unknown>"
}
line := loc.Line[0]
funcName = line.Function.Name
fileLine = fmt.Sprintf("%s:%d", line.Function.Filename, line.Line)
return
}
func (c *flamegraphCollector) collectChild(
node flamegraphNodeWithLocation,
depth int64,
indent string,
parentCumValue int64,
isLastChild bool,
) {
funcName, fileLine := c.locationName(node.locID)
c.rows = append(c.rows, types.MakeDatums(
texttree.PrettyIdentifier(funcName, indent, isLastChild),
percentage(node.cumValue, c.total),
percentage(node.cumValue, parentCumValue),
c.rootChild,
depth,
fileLine,
))
if len(node.children) == 0 {
return
}
indent4Child := texttree.Indent4Child(indent, isLastChild)
children := node.sortedChildren()
for i, child := range children {
c.collectChild(child, depth+1, indent4Child, node.cumValue, i == len(children)-1)
}
}
func (c *flamegraphCollector) collect(root *flamegraphNode) {
c.rows = append(c.rows, types.MakeDatums("root", "100%", "100%", 0, 0, "root"))
if len(root.children) == 0 {
return
}
c.total = root.cumValue
indent4Child := texttree.Indent4Child("", false)
children := root.sortedChildren()
for i, child := range children {
c.rootChild = i + 1
c.collectChild(child, 1, indent4Child, root.cumValue, i == len(children)-1)
}
}
func percentage(value, total int64) string {
var ratio float64
if total != 0 {
ratio = math.Abs(float64(value)/float64(total)) * 100
}
switch {
case ratio >= 99.95 && ratio <= 100.05:
return "100%"
case ratio >= 1.0:
return fmt.Sprintf("%.2f%%", ratio)
default:
return fmt.Sprintf("%.2g%%", ratio)
}
}