-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathflamegraph_tables.go
More file actions
336 lines (311 loc) · 9.18 KB
/
flamegraph_tables.go
File metadata and controls
336 lines (311 loc) · 9.18 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (C) 2021-2025 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
package flamegraph
import (
"fmt"
"log/slog"
"math"
"perfspect/internal/extract"
"perfspect/internal/script"
"perfspect/internal/table"
"regexp"
"strconv"
"strings"
)
// flamegraph table names
const (
FlameGraphTableName = "Flamegraph"
)
// flamegraph tables
var tableDefinitions = map[string]table.TableDefinition{
FlameGraphTableName: {
Name: FlameGraphTableName,
MenuLabel: FlameGraphTableName,
ScriptNames: []string{
script.FlameGraphScriptName,
},
FieldsFunc: flameGraphTableValues},
}
func flameGraphTableValues(outputs map[string]script.ScriptOutput) []table.Field {
fields := []table.Field{
{Name: "Native Stacks", Values: []string{nativeFoldedFromOutput(outputs)}},
{Name: "Java Stacks", Values: []string{javaFoldedFromOutput(outputs)}},
{Name: "Maximum Render Depth", Values: []string{maxRenderDepthFromOutput(outputs)}},
{Name: "Perf Event", Values: []string{perfEventFromOutput(outputs)}},
{Name: "Asprof Arguments", Values: []string{asprofArgumentsFromOutput(outputs)}},
}
return fields
}
func javaFoldedFromOutput(outputs map[string]script.ScriptOutput) string {
if outputs[script.FlameGraphScriptName].Stdout == "" {
slog.Warn("collapsed call stack output is empty")
return ""
}
sections := extract.GetSectionsFromOutput(outputs[script.FlameGraphScriptName].Stdout)
if len(sections) == 0 {
slog.Warn("no sections in collapsed call stack output")
return ""
}
javaFolded := make(map[string]string)
re := regexp.MustCompile(`^async-profiler (\d+) (.*)$`)
for header, stacks := range sections {
match := re.FindStringSubmatch(header)
if match == nil {
continue
}
pid := match[1]
processName := match[2]
if stacks == "" {
slog.Warn("no stacks for java process", slog.String("header", header))
continue
}
if strings.HasPrefix(stacks, "Failed to inject profiler") {
slog.Error("profiling data error", slog.String("header", header))
continue
}
_, ok := javaFolded[processName]
if processName == "" {
processName = "java (" + pid + ")"
} else if ok {
processName = processName + " (" + pid + ")"
}
javaFolded[processName] = stacks
}
folded, err := mergeJavaFolded(javaFolded)
if err != nil {
slog.Error("failed to merge java stacks", slog.String("error", err.Error()))
}
return folded
}
func nativeFoldedFromOutput(outputs map[string]script.ScriptOutput) string {
if outputs[script.FlameGraphScriptName].Stdout == "" {
slog.Warn("collapsed call stack output is empty")
return ""
}
sections := extract.GetSectionsFromOutput(outputs[script.FlameGraphScriptName].Stdout)
if len(sections) == 0 {
slog.Warn("no sections in collapsed call stack output")
return ""
}
var dwarfFolded, fpFolded string
for header, content := range sections {
switch header {
case "perf_dwarf":
dwarfFolded = content
case "perf_fp":
fpFolded = content
}
}
if dwarfFolded == "" && fpFolded == "" {
// "event syntax error: 'foo'" indicates that the perf event specified is invalid/unsupported
if strings.Contains(outputs[script.FlameGraphScriptName].Stderr, "event syntax error") {
slog.Error("unsupported perf event specified", slog.String("error", outputs[script.FlameGraphScriptName].Stderr))
}
return ""
}
folded, err := mergeSystemFolded(fpFolded, dwarfFolded)
if err != nil {
slog.Error("failed to merge native stacks", slog.String("error", err.Error()))
}
return folded
}
func maxRenderDepthFromOutput(outputs map[string]script.ScriptOutput) string {
if outputs[script.FlameGraphScriptName].Stdout == "" {
slog.Warn("collapsed call stack output is empty")
return ""
}
sections := extract.GetSectionsFromOutput(outputs[script.FlameGraphScriptName].Stdout)
if len(sections) == 0 {
slog.Warn("no sections in collapsed call stack output")
return ""
}
for header, content := range sections {
if header == "maximum depth" {
return strings.TrimSpace(content)
}
}
return ""
}
func perfEventFromOutput(outputs map[string]script.ScriptOutput) string {
if outputs[script.FlameGraphScriptName].Stdout == "" {
slog.Warn("collapsed call stack output is empty")
return ""
}
sections := extract.GetSectionsFromOutput(outputs[script.FlameGraphScriptName].Stdout)
if len(sections) == 0 {
slog.Warn("no sections in collapsed call stack output")
return ""
}
for header, content := range sections {
if header == "perf_event" {
return strings.TrimSpace(content)
}
}
return ""
}
func asprofArgumentsFromOutput(outputs map[string]script.ScriptOutput) string {
if outputs[script.FlameGraphScriptName].Stdout == "" {
slog.Warn("collapsed call stack output is empty")
return ""
}
sections := extract.GetSectionsFromOutput(outputs[script.FlameGraphScriptName].Stdout)
if len(sections) == 0 {
slog.Warn("no sections in collapsed call stack output")
return ""
}
for header, content := range sections {
if header == "asprof_arguments" {
return strings.TrimSpace(content)
}
}
return ""
}
// ProcessStacks ...
// [processName][callStack]=count
type ProcessStacks map[string]Stacks
type Stacks map[string]int
// example folded stack:
// swapper;secondary_startup_64_no_verify;start_secondary;cpu_startup_entry;arch_cpu_idle_enter 10523019
func (p *ProcessStacks) parsePerfFolded(folded string) (err error) {
re := regexp.MustCompile(`^([\w,\-, ,\.]+);(.+) (\d+)$`)
for line := range strings.SplitSeq(folded, "\n") {
match := re.FindStringSubmatch(line)
if match == nil {
continue
}
processName := match[1]
stack := match[2]
count, err := strconv.Atoi(match[3])
if err != nil {
continue
}
if _, ok := (*p)[processName]; !ok {
(*p)[processName] = make(Stacks)
}
(*p)[processName][stack] = count
}
return
}
func (p *ProcessStacks) parseAsyncProfilerFolded(folded string, processName string) (err error) {
for line := range strings.SplitSeq(folded, "\n") {
splitAt := strings.LastIndex(line, " ")
if splitAt == -1 {
continue
}
stack := line[:splitAt]
count, err := strconv.Atoi(line[splitAt+1:])
if err != nil {
continue
}
if _, ok := (*p)[processName]; !ok {
(*p)[processName] = make(Stacks)
}
(*p)[processName][stack] = count
}
return
}
func (p *ProcessStacks) totalSamples() (count int) {
count = 0
for _, stacks := range *p {
for _, stackCount := range stacks {
count += stackCount
}
}
return
}
func (p *ProcessStacks) scaleCounts(ratio float64) {
for processName, stacks := range *p {
for stack, stackCount := range stacks {
(*p)[processName][stack] = int(math.Round(float64(stackCount) * ratio))
}
}
}
func (p *ProcessStacks) averageDepth(processName string) (average float64) {
if _, ok := (*p)[processName]; !ok {
average = 0
return
}
total := 0
count := 0
for stack := range (*p)[processName] {
total += len(strings.Split(stack, ";"))
count += 1
}
if count == 0 {
return
}
average = float64(total) / float64(count)
return
}
func (p *ProcessStacks) dumpFolded() (folded string) {
var sb strings.Builder
for processName, stacks := range *p {
for stack, stackCount := range stacks {
fmt.Fprintf(&sb, "%s;%s %d\n", processName, stack, stackCount)
}
}
folded = sb.String()
return
}
// helper functions below
// mergeJavaFolded -- merge profiles from N java processes
func mergeJavaFolded(javaFolded map[string]string) (merged string, err error) {
javaStacks := make(ProcessStacks)
for processName, stacks := range javaFolded {
err = javaStacks.parseAsyncProfilerFolded(stacks, processName)
if err != nil {
continue
}
}
merged = javaStacks.dumpFolded()
return
}
// mergeSystemFolded -- merge the two sets of system perf stacks into one set
// For every process, get the average depth of stacks from Fp and Dwarf.
// The stacks with the deepest average (per process) will be retained in the
// merged set.
// The Dwarf stack counts will be scaled to the FP stack counts.
func mergeSystemFolded(perfFp string, perfDwarf string) (merged string, err error) {
fpStacks := make(ProcessStacks)
err = fpStacks.parsePerfFolded(perfFp)
if err != nil {
return
}
dwarfStacks := make(ProcessStacks)
err = dwarfStacks.parsePerfFolded(perfDwarf)
if err != nil {
return
}
fpSampleCount := fpStacks.totalSamples()
dwarfSampleCount := dwarfStacks.totalSamples()
if fpSampleCount == 0 && dwarfSampleCount == 0 {
err = fmt.Errorf("both fp and dwarf sample counts cannot be zero")
return
}
if fpSampleCount == 0 {
slog.Warn("no fp samples; using dwarf stacks")
merged = dwarfStacks.dumpFolded()
return
}
if dwarfSampleCount == 0 {
slog.Warn("no dwarf samples; using fp stacks")
merged = fpStacks.dumpFolded()
return
}
fpToDwarfScalingRatio := float64(fpSampleCount) / float64(dwarfSampleCount)
dwarfStacks.scaleCounts(fpToDwarfScalingRatio)
// for every process in fpStacks, get the average stack depth from
// fpStacks and dwarfStacks, choose the deeper stack for the merged set
mergedStacks := make(ProcessStacks)
for processName := range fpStacks {
fpDepth := fpStacks.averageDepth(processName)
dwarfDepth := dwarfStacks.averageDepth(processName)
if fpDepth >= dwarfDepth {
mergedStacks[processName] = fpStacks[processName]
} else {
mergedStacks[processName] = dwarfStacks[processName]
}
}
merged = mergedStacks.dumpFolded()
return
}