-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathinsert-stack-labels.ts
More file actions
291 lines (271 loc) · 9.35 KB
/
Copy pathinsert-stack-labels.ts
File metadata and controls
291 lines (271 loc) · 9.35 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
/* 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 {
IndexIntoFrameTable,
IndexIntoStackTable,
RawStackTable,
IndexIntoFuncTable,
Profile,
Category,
} from '../types/profile';
import {
shallowCloneFrameTable,
shallowCloneFuncTable,
} from 'firefox-profiler/profile-logic/data-structures';
import { StringTable } from 'firefox-profiler/utils/string-table';
import { updateRawThreadStacks } from 'firefox-profiler/profile-logic/profile-data';
export type LabelDescription = {
name: string;
funcPrefixes: string[];
};
/**
* Takes a profile and creates one which contains "stack labels".
*
* ## Example
*
* Before:
*
* ```
* - BaselineIC: Call.CallNative
* - mozilla::dom::Element_Binding::getBoundingClientRect <- matches a funcPrefix
* - nsIContent::GetPrimaryFrame
* - mozilla::PresShell::DoFlushLayout
* - mozilla::PresShell::ProcessReflowCommands <- matches a funcPrefix
* ```
*
* After:
*
* ```
* - BaselineIC: Call.CallNative
* - Element.getBoundingClientRect <== NEW
* - mozilla::dom::Element_Binding::getBoundingClientRect
* - nsIContent::GetPrimaryFrame
* - mozilla::PresShell::DoFlushLayout
* - Update layout <== NEW
* - mozilla::PresShell::ProcessReflowCommands
* ```
*
* The label frames are inserted as new parent stack nodes for the matched
* stack node. The caller supplies the list of labels and their matchers
* (as function name prefixes).
*
* ## No "duplicate labels"
*
* This implementation avoids duplicate labels. This is best explained with
* an example. Let "Label A" apply to prefix "a" and "Label B" apply to prefix "b".
*
* Input:
*
* ```
* - a1
* - a2
* - b1
* - a2
* ```
*
* Then we get:
*
* ```
* - Label A
* - a1
* - a2
* - Label B
* - b1
* - Label A
* - a2
* ```
*
* Notably there is no extra "Label A" frame between a1 and a2, even though a2
* also matches. We avoid it in order to keep the tree simple; and in the JS-only
* call tree, the samples at a1,a2 are already accounted to the Label A node which
* is all we wanted to achieve.
*/
export function insertStackLabels(
profile: Profile,
labelDescriptions: LabelDescription[]
): Profile {
const labelCategoryIndex = profile.meta.categories!.length;
const newCategories: Category[] = [
...profile.meta.categories!,
{
name: 'Label',
color: 'blue',
subcategories: ['Other'],
},
];
const {
funcTable: oldFuncTable,
frameTable: oldFrameTable,
stackTable: oldStackTable,
sources,
stringArray,
} = profile.shared;
const frameTable = shallowCloneFrameTable(oldFrameTable);
const funcTable = shallowCloneFuncTable(oldFuncTable);
const stringTable = StringTable.withBackingArray(stringArray);
const rootLabelName = 'Root (unaccounted / catch-all)';
const rootLabelFrameIndex = frameTable.length;
const labelFramesStartIndex = rootLabelFrameIndex + 1;
const allLabelNames = [
rootLabelName,
...labelDescriptions.map((label) => label.name),
];
// First, add the label frames and funcs to the frameTable + funcTable.
for (let i = 0; i < allLabelNames.length; i++) {
const labelName = allLabelNames[i];
const funcIndex = funcTable.length++;
funcTable.name[funcIndex] = stringTable.indexForString(labelName);
funcTable.resource[funcIndex] = -1;
funcTable.source[funcIndex] = null;
funcTable.lineNumber[funcIndex] = null;
funcTable.columnNumber[funcIndex] = null;
funcTable.originalLocation[funcIndex] = null;
funcTable.isJS[funcIndex] = false;
funcTable.relevantForJS[funcIndex] = true;
const frameIndex = frameTable.length++;
frameTable.func[frameIndex] = funcIndex;
frameTable.category[frameIndex] = labelCategoryIndex;
frameTable.subcategory[frameIndex] = 0;
frameTable.nativeSymbol[frameIndex] = null;
frameTable.address[frameIndex] = 0;
frameTable.inlineDepth[frameIndex] = 0;
frameTable.line[frameIndex] = null;
frameTable.column[frameIndex] = null;
frameTable.originalLocation[frameIndex] = null;
frameTable.innerWindowID[frameIndex] = null;
}
// Run the function name against the substring matchers and return the first
// match.
function getLabelIndexForFunc(funcIndex: IndexIntoFuncTable): number | null {
let nameString = stringArray[funcTable.name[funcIndex]];
// Include the filename (in brackets), if present. This allows matchers
// like `onStateChange (chrome://browser/content/tabbrowser/`
const sourceIndex = funcTable.source[funcIndex];
if (sourceIndex !== null) {
const filenameString = stringArray[sources.filename[sourceIndex]];
nameString += ` (${filenameString})`;
}
// Check against every funcPrefix of every label. Return the first match.
for (
let labelIndex = 0;
labelIndex < labelDescriptions.length;
labelIndex++
) {
const labelDescription = labelDescriptions[labelIndex];
for (
let prefixIndex = 0;
prefixIndex < labelDescription.funcPrefixes.length;
prefixIndex++
) {
const funcNamePrefix = labelDescription.funcPrefixes[prefixIndex];
if (nameString.startsWith(funcNamePrefix)) {
return labelIndex;
}
}
}
return null;
}
// Compute the label frame index (if any) for every func.
const funcIndexToLabelFrameIndex = new Array<number | null>(funcTable.length);
for (let funcIndex = 0; funcIndex < funcTable.length; funcIndex++) {
const labelIndex = getLabelIndexForFunc(funcIndex);
const labelFrameIndex =
labelIndex !== null ? labelFramesStartIndex + labelIndex : null;
funcIndexToLabelFrameIndex[funcIndex] = labelFrameIndex;
}
// Now compute where in the stack table we need labels.
const labelFrameIndexToInsertAtStack = new Array<number | null>(
oldStackTable.length
);
const inheritedLabelFrameIndexAtStack = new Array<IndexIntoFrameTable | null>(
oldStackTable.length
);
let stacksToInsertCount = 0;
for (let stackIndex = 0; stackIndex < oldStackTable.length; stackIndex++) {
const parentStackIndex = oldStackTable.prefix[stackIndex];
const inheritedLabelFrameIndex =
parentStackIndex !== null
? inheritedLabelFrameIndexAtStack[parentStackIndex]
: null;
const frameIndex = oldStackTable.frame[stackIndex];
const funcIndex = oldFrameTable.func[frameIndex];
const labelFrameIndex = funcIndexToLabelFrameIndex[funcIndex];
if (
labelFrameIndex !== null &&
labelFrameIndex !== inheritedLabelFrameIndex
) {
labelFrameIndexToInsertAtStack[stackIndex] = labelFrameIndex;
inheritedLabelFrameIndexAtStack[stackIndex] = labelFrameIndex;
stacksToInsertCount++;
} else if (
funcTable.isJS[funcIndex] ||
funcTable.relevantForJS[funcIndex]
) {
labelFrameIndexToInsertAtStack[stackIndex] = null;
inheritedLabelFrameIndexAtStack[stackIndex] = null;
} else if (parentStackIndex === null) {
labelFrameIndexToInsertAtStack[stackIndex] = rootLabelFrameIndex;
inheritedLabelFrameIndexAtStack[stackIndex] = rootLabelFrameIndex;
stacksToInsertCount++;
} else {
labelFrameIndexToInsertAtStack[stackIndex] = null;
inheritedLabelFrameIndexAtStack[stackIndex] = inheritedLabelFrameIndex;
}
}
// Now compute the new stack table.
const newStackCount = oldStackTable.length + stacksToInsertCount;
const newPrefixCol = new Array<IndexIntoStackTable | null>(newStackCount);
const newFrameCol = new Array<IndexIntoFrameTable>(newStackCount);
const oldStackToNewStackPlusOne = new Int32Array(oldStackTable.length);
let nextNewStackIndex = 0;
for (
let oldStackIndex = 0;
oldStackIndex < oldStackTable.length;
oldStackIndex++
) {
const labelFrameIndexToInsert =
labelFrameIndexToInsertAtStack[oldStackIndex];
const oldPrefix = oldStackTable.prefix[oldStackIndex];
let newPrefix =
oldPrefix !== null ? oldStackToNewStackPlusOne[oldPrefix] - 1 : null;
const frameIndex = oldStackTable.frame[oldStackIndex];
if (labelFrameIndexToInsert !== null) {
const insertedStackIndex = nextNewStackIndex++;
newPrefixCol[insertedStackIndex] = newPrefix;
newFrameCol[insertedStackIndex] = labelFrameIndexToInsert;
newPrefix = insertedStackIndex;
}
const newStackIndex = nextNewStackIndex++;
newPrefixCol[newStackIndex] = newPrefix;
newFrameCol[newStackIndex] = frameIndex;
oldStackToNewStackPlusOne[oldStackIndex] = newStackIndex + 1;
}
if (nextNewStackIndex !== newStackCount) {
console.error('Unexpected new stack count!', {
nextNewStackIndex,
newStackCount,
stacksToInsertCount,
});
}
const stackTable: RawStackTable = {
prefix: newPrefixCol,
frame: newFrameCol,
length: newStackCount,
};
const newShared = { ...profile.shared, stackTable, frameTable, funcTable };
const newThreads = updateRawThreadStacks(profile.threads, (oldStack) =>
oldStack !== null ? oldStackToNewStackPlusOne[oldStack] - 1 : null
);
const newMeta = {
...profile.meta,
categories: newCategories,
};
const newProfile: Profile = {
...profile,
meta: newMeta,
shared: newShared,
threads: newThreads,
};
return newProfile;
}