forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flamegraph.ts
143 lines (128 loc) · 3.9 KB
/
flamegraph.ts
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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { Logger } from 'kibana/server';
import { StackTraceID, StackFrameID, FileID, StackTrace, StackFrame, Executable } from './types';
function checkIfStringHasParentheses(s: string) {
return /\(|\)/.test(s);
}
function getFunctionName(frame: any) {
return frame.FunctionName !== '' && !checkIfStringHasParentheses(frame.FunctionName)
? `${frame.FunctionName}()`
: frame.FunctionName;
}
export class FlameGraph {
// sampleRate is 1/5^N, with N being the downsampled index the events were fetched from.
// N=0: full events table (sampleRate is 1)
// N=1: downsampled by 5 (sampleRate is 0.2)
// ...
sampleRate: number;
// totalCount is the sum(Count) of all events in the filter range in the
// downsampled index we were looking at.
// To estimate how many events we have in the full events index: totalCount / sampleRate.
// Do the same for single entries in the events array.
totalCount: number;
events: Map<StackTraceID, number>;
stacktraces: Map<StackTraceID, StackTrace>;
stackframes: Map<StackFrameID, StackFrame>;
executables: Map<FileID, Executable>;
private readonly logger: Logger;
constructor(
sampleRate: number,
totalCount: number,
events: Map<StackTraceID, number>,
stackTraces: Map<StackTraceID, StackTrace>,
stackFrames: Map<StackFrameID, StackFrame>,
executables: Map<FileID, Executable>,
logger: Logger
) {
this.sampleRate = sampleRate;
this.totalCount = totalCount;
this.events = events;
this.stacktraces = stackTraces;
this.stackframes = stackFrames;
this.executables = executables;
this.logger = logger;
}
private getExeFileName(exe: any, type: number) {
if (exe?.FileName === undefined) {
this.logger.warn('missing executable FileName');
return '';
}
if (exe.FileName !== '') {
return exe.FileName;
}
switch (type) {
case 0:
return '<unsymbolized frame>';
case 1:
return 'Python';
case 2:
return 'PHP';
case 3:
return 'Native';
case 4:
return 'Kernel';
case 5:
return 'JVM/Hotspot';
case 6:
return 'Ruby';
case 7:
return 'Perl';
case 8:
return 'JavaScript';
default:
return '';
}
}
// Generates the label for a flamegraph node
//
// This is slightly modified from the original code in elastic/prodfiler_ui
private getLabel(frame: any, executable: any, type: number) {
if (frame.FunctionName !== '') {
return `${this.getExeFileName(executable, type)}: ${getFunctionName(frame)} in #${
frame.LineNumber
}`;
}
return this.getExeFileName(executable, type);
}
toElastic() {
const leaves = [];
let n = 0;
for (const trace of this.stacktraces.values()) {
const path = ['root'];
for (let i = trace.FrameID.length - 1; i >= 0; i--) {
const label = this.getLabel(
this.stackframes.get(trace.FrameID[i]),
this.executables.get(trace.FileID[i]),
parseInt(trace.Type[i], 10)
);
if (label.length === 0) {
path.push(trace.FrameID[i]);
} else {
path.push(label);
}
}
const leaf = {
id: path[path.length - 1],
value: 1,
depth: trace.FrameID.length,
pathFromRoot: Object.fromEntries(path.map((item, i) => [i, item])),
};
leaves.push(leaf);
n++;
if (n >= 1000) {
// just don't overload the Kibana flamechart
break;
}
}
return { leaves };
}
toPixi() {
return {};
}
}