-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathflame-graph.ts
More file actions
178 lines (155 loc) · 5.54 KB
/
Copy pathflame-graph.ts
File metadata and controls
178 lines (155 loc) · 5.54 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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import type { Milliseconds } from 'firefox-profiler/types/units';
import type {
CategoryList,
IndexIntoCategoryList,
IndexIntoFrameTable,
IndexIntoStackTable,
Profile,
} from 'firefox-profiler/types/profile';
import {
getEmptyProfile,
getEmptyThread,
} from 'firefox-profiler/profile-logic/data-structures';
import { GlobalDataCollector } from 'firefox-profiler/profile-logic/global-data-collector';
import { ensureExists } from 'firefox-profiler/utils/types';
/**
* The flamegraph.pl format is a plain text format where each line represents
* a collapsed stack trace followed by a count. This format is commonly used
* as input for flamegraph.pl and similar flame graph visualization tools.
*
* Format: "frame1;frame2;frame3 count"
* Example: "java.lang.Thread.run;MyClass.method_[j];helper 42"
*/
export function isFlameGraphFormat(profile: string): boolean {
if (profile.startsWith('{')) {
// Make sure we don't accidentally match JSON.
return false;
}
const firstLine = profile.substring(0, profile.indexOf('\n'));
return !!firstLine.match(/[^;]*(?:;[^;]*)* [0-9]+/);
}
const CATEGORIES: CategoryList = [
{ name: 'Java', color: 'yellow', subcategories: ['Other'] },
{ name: 'Native', color: 'blue', subcategories: ['Other'] },
];
const JAVA_CATEGORY_INDEX: IndexIntoCategoryList = 0;
const NATIVE_CATEGORY_INDEX: IndexIntoCategoryList = 1;
/**
* Convert the flamegraph.pl input text format into the processed profile format.
*/
export function convertFlameGraphProfile(profileText: string): Profile {
const profile = getEmptyProfile();
profile.meta.product = 'Flamegraph';
profile.meta.categories = CATEGORIES;
const globalDataCollector = new GlobalDataCollector();
const stringTable = globalDataCollector.getStringTable();
const thread = getEmptyThread({
name: 'Program',
pid: '0',
tid: 0,
});
const frameTable = globalDataCollector.getFrameTable();
const stackTable = globalDataCollector.getStackTableBuilder();
const { samples } = thread;
// Maps to deduplicate stacks, frames, and functions.
const stackMap = new Map<string, IndexIntoStackTable>();
const frameMap = new Map<string, IndexIntoFrameTable>();
function getOrCreateStack(
frameIndex: IndexIntoFrameTable,
prefix: IndexIntoStackTable | null
): IndexIntoStackTable {
const key = prefix === null ? `${frameIndex}` : `${frameIndex},${prefix}`;
let stack = stackMap.get(key);
if (stack === undefined) {
stack = stackTable.length;
stackTable.frame.push(frameIndex);
stackTable.prefix.push(prefix);
stackTable.length++;
stackMap.set(key, stack);
}
return stack;
}
function getOrCreateFrame(frameString: string): IndexIntoFrameTable {
let frameIndex = frameMap.get(frameString);
if (frameIndex !== undefined) {
return frameIndex;
}
// Clean the frame name by removing the _[j] suffix.
const cleanedName = frameString.replace(/_\[j\]$/, '');
// Categorize frames based on common conventions in Java profilers.
// _[j] suffix: Java frames (used by async-profiler and similar tools).
let category: IndexIntoCategoryList = NATIVE_CATEGORY_INDEX;
if (frameString.endsWith('_[j]')) {
category = JAVA_CATEGORY_INDEX;
}
// Create or get function.
const nameIndex = stringTable.indexForString(cleanedName);
const funcIndex = globalDataCollector.indexForFunc(
nameIndex,
false,
false,
-1,
null,
null,
null
);
// Create frame.
frameIndex = frameTable.length;
frameTable.address.push(-1);
frameTable.inlineDepth.push(0);
frameTable.category.push(category);
frameTable.subcategory.push(0);
frameTable.func.push(funcIndex);
frameTable.nativeSymbol.push(null);
frameTable.innerWindowID.push(null);
frameTable.line.push(null);
frameTable.column.push(null);
frameTable.originalLocation.push(null);
frameTable.length++;
frameMap.set(frameString, frameIndex);
return frameIndex;
}
function addSample(time: Milliseconds, stackArray: string[]): void {
const stack = stackArray.reduce<IndexIntoStackTable | null>(
(prefix, stackFrame) => {
const frameIndex = getOrCreateFrame(stackFrame);
return getOrCreateStack(frameIndex, prefix);
},
null
);
samples.stack.push(stack);
ensureExists(samples.time).push(time);
samples.length++;
}
// Parse the flamegraph text format.
const lines = profileText.split('\n');
// The flamegraph.pl format doesn't include timestamp information.
// Each line only contains a collapsed stack and a count of how many times
// that stack was observed. To convert this to the profiler's sample-based
// format, we generate fake sequential timestamps for each sample.
let timeStamp: Milliseconds = 0;
for (const line of lines) {
if (line.trim() === '') {
continue;
}
const matched = line.match(/([^;]*(?:;[^;]*)*) ([0-9]+)/);
if (!matched) {
console.warn('unexpected line format', line);
continue;
}
const [, frames, duration] = matched;
const stack = frames.split(';');
let count = parseInt(duration, 10);
while (count-- > 0) {
addSample(timeStamp++, stack);
}
}
// Finalize the profile.
profile.threads.push(thread);
const { shared } = globalDataCollector.finish();
profile.shared = shared;
return profile;
}