-
Notifications
You must be signed in to change notification settings - Fork 47.9k
/
Copy pathpreprocessData.js
471 lines (430 loc) · 13.3 KB
/
preprocessData.js
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import {
importFromChromeTimeline,
Flamechart as SpeedscopeFlamechart,
} from '@elg/speedscope';
import type {TimelineEvent} from '@elg/speedscope';
import type {
Milliseconds,
BatchUID,
Flamechart,
ReactLane,
ReactMeasureType,
ReactProfilerData,
} from '../types';
import {REACT_TOTAL_NUM_LANES} from '../constants';
import InvalidProfileError from './InvalidProfileError';
type MeasureStackElement = {|
type: ReactMeasureType,
depth: number,
index: number,
startTime: Milliseconds,
stopTime?: Milliseconds,
|};
type ProcessorState = {|
nextRenderShouldGenerateNewBatchID: boolean,
batchUID: BatchUID,
uidCounter: BatchUID,
measureStack: MeasureStackElement[],
|};
// Exported for tests
export function getLanesFromTransportDecimalBitmask(
laneBitmaskString: string,
): ReactLane[] {
const laneBitmask = parseInt(laneBitmaskString, 10);
// As negative numbers are stored in two's complement format, our bitmask
// checks will be thrown off by them.
if (laneBitmask < 0) {
return [];
}
const lanes = [];
let powersOfTwo = 0;
while (powersOfTwo <= REACT_TOTAL_NUM_LANES) {
if ((1 << powersOfTwo) & laneBitmask) {
lanes.push(powersOfTwo);
}
powersOfTwo++;
}
return lanes;
}
function getLastType(stack: $PropertyType<ProcessorState, 'measureStack'>) {
if (stack.length > 0) {
const {type} = stack[stack.length - 1];
return type;
}
return null;
}
function getDepth(stack: $PropertyType<ProcessorState, 'measureStack'>) {
if (stack.length > 0) {
const {depth, type} = stack[stack.length - 1];
return type === 'render-idle' ? depth : depth + 1;
}
return 0;
}
function markWorkStarted(
type: ReactMeasureType,
startTime: Milliseconds,
lanes: ReactLane[],
currentProfilerData: ReactProfilerData,
state: ProcessorState,
) {
const {batchUID, measureStack} = state;
const index = currentProfilerData.measures.length;
const depth = getDepth(measureStack);
state.measureStack.push({depth, index, startTime, type});
currentProfilerData.measures.push({
type,
batchUID,
depth,
lanes,
timestamp: startTime,
duration: 0,
});
}
function markWorkCompleted(
type: ReactMeasureType,
stopTime: Milliseconds,
currentProfilerData: ReactProfilerData,
stack: $PropertyType<ProcessorState, 'measureStack'>,
) {
if (stack.length === 0) {
console.error(
'Unexpected type "%s" completed at %sms while stack is empty.',
type,
stopTime,
);
// Ignore work "completion" user timing mark that doesn't complete anything
return;
}
const last = stack[stack.length - 1];
if (last.type !== type) {
console.error(
'Unexpected type "%s" completed at %sms before "%s" completed.',
type,
stopTime,
last.type,
);
}
const {index, startTime} = stack.pop();
const measure = currentProfilerData.measures[index];
if (!measure) {
console.error('Could not find matching measure for type "%s".', type);
}
// $FlowFixMe This property should not be writable outside of this function.
measure.duration = stopTime - startTime;
}
function throwIfIncomplete(
type: ReactMeasureType,
stack: $PropertyType<ProcessorState, 'measureStack'>,
) {
const lastIndex = stack.length - 1;
if (lastIndex >= 0) {
const last = stack[lastIndex];
if (last.stopTime === undefined && last.type === type) {
throw new InvalidProfileError(
`Unexpected type "${type}" started before "${last.type}" completed.`,
);
}
}
}
function processTimelineEvent(
event: TimelineEvent,
/** Finalized profiler data up to `event`. May be mutated. */
currentProfilerData: ReactProfilerData,
/** Intermediate processor state. May be mutated. */
state: ProcessorState,
) {
const {cat, name, ts, ph} = event;
if (cat !== 'blink.user_timing') {
return;
}
const startTime = (ts - currentProfilerData.startTime) / 1000;
// React Events - schedule
if (name.startsWith('--schedule-render-')) {
const [laneBitmaskString, ...splitComponentStack] = name
.substr(18)
.split('-');
currentProfilerData.events.push({
type: 'schedule-render',
lanes: getLanesFromTransportDecimalBitmask(laneBitmaskString),
componentStack: splitComponentStack.join('-'),
timestamp: startTime,
});
} else if (name.startsWith('--schedule-forced-update-')) {
const [
laneBitmaskString,
componentName,
...splitComponentStack
] = name.substr(25).split('-');
const isCascading = !!state.measureStack.find(
({type}) => type === 'commit',
);
currentProfilerData.events.push({
type: 'schedule-force-update',
lanes: getLanesFromTransportDecimalBitmask(laneBitmaskString),
componentName,
componentStack: splitComponentStack.join('-'),
timestamp: startTime,
isCascading,
});
} else if (name.startsWith('--schedule-state-update-')) {
const [
laneBitmaskString,
componentName,
...splitComponentStack
] = name.substr(24).split('-');
const isCascading = !!state.measureStack.find(
({type}) => type === 'commit',
);
currentProfilerData.events.push({
type: 'schedule-state-update',
lanes: getLanesFromTransportDecimalBitmask(laneBitmaskString),
componentName,
componentStack: splitComponentStack.join('-'),
timestamp: startTime,
isCascading,
});
} // eslint-disable-line brace-style
// React Events - suspense
else if (name.startsWith('--suspense-suspend-')) {
const [id, componentName, ...splitComponentStack] = name
.substr(19)
.split('-');
currentProfilerData.events.push({
type: 'suspense-suspend',
id,
componentName,
componentStack: splitComponentStack.join('-'),
timestamp: startTime,
});
} else if (name.startsWith('--suspense-resolved-')) {
const [id, componentName, ...splitComponentStack] = name
.substr(20)
.split('-');
currentProfilerData.events.push({
type: 'suspense-resolved',
id,
componentName,
componentStack: splitComponentStack.join('-'),
timestamp: startTime,
});
} else if (name.startsWith('--suspense-rejected-')) {
const [id, componentName, ...splitComponentStack] = name
.substr(20)
.split('-');
currentProfilerData.events.push({
type: 'suspense-rejected',
id,
componentName,
componentStack: splitComponentStack.join('-'),
timestamp: startTime,
});
} // eslint-disable-line brace-style
// React Measures - render
else if (name.startsWith('--render-start-')) {
if (state.nextRenderShouldGenerateNewBatchID) {
state.nextRenderShouldGenerateNewBatchID = false;
state.batchUID = ((state.uidCounter++: any): BatchUID);
}
const laneBitmaskString = name.substr(15);
const lanes = getLanesFromTransportDecimalBitmask(laneBitmaskString);
throwIfIncomplete('render', state.measureStack);
if (getLastType(state.measureStack) !== 'render-idle') {
markWorkStarted(
'render-idle',
startTime,
lanes,
currentProfilerData,
state,
);
}
markWorkStarted('render', startTime, lanes, currentProfilerData, state);
} else if (
name.startsWith('--render-stop') ||
name.startsWith('--render-yield')
) {
markWorkCompleted(
'render',
startTime,
currentProfilerData,
state.measureStack,
);
} else if (name.startsWith('--render-cancel')) {
state.nextRenderShouldGenerateNewBatchID = true;
markWorkCompleted(
'render',
startTime,
currentProfilerData,
state.measureStack,
);
markWorkCompleted(
'render-idle',
startTime,
currentProfilerData,
state.measureStack,
);
} // eslint-disable-line brace-style
// React Measures - commits
else if (name.startsWith('--commit-start-')) {
state.nextRenderShouldGenerateNewBatchID = true;
const laneBitmaskString = name.substr(15);
const lanes = getLanesFromTransportDecimalBitmask(laneBitmaskString);
markWorkStarted('commit', startTime, lanes, currentProfilerData, state);
} else if (name.startsWith('--commit-stop')) {
markWorkCompleted(
'commit',
startTime,
currentProfilerData,
state.measureStack,
);
markWorkCompleted(
'render-idle',
startTime,
currentProfilerData,
state.measureStack,
);
} // eslint-disable-line brace-style
// React Measures - layout effects
else if (name.startsWith('--layout-effects-start-')) {
const laneBitmaskString = name.substr(23);
const lanes = getLanesFromTransportDecimalBitmask(laneBitmaskString);
markWorkStarted(
'layout-effects',
startTime,
lanes,
currentProfilerData,
state,
);
} else if (name.startsWith('--layout-effects-stop')) {
markWorkCompleted(
'layout-effects',
startTime,
currentProfilerData,
state.measureStack,
);
} // eslint-disable-line brace-style
// React Measures - passive effects
else if (name.startsWith('--passive-effects-start-')) {
const laneBitmaskString = name.substr(24);
const lanes = getLanesFromTransportDecimalBitmask(laneBitmaskString);
markWorkStarted(
'passive-effects',
startTime,
lanes,
currentProfilerData,
state,
);
} else if (name.startsWith('--passive-effects-stop')) {
markWorkCompleted(
'passive-effects',
startTime,
currentProfilerData,
state.measureStack,
);
} // eslint-disable-line brace-style
// Other user timing marks/measures
else if (ph === 'R' || ph === 'n') {
// User Timing mark
currentProfilerData.otherUserTimingMarks.push({
name,
timestamp: startTime,
});
} else if (ph === 'b') {
// TODO: Begin user timing measure
} else if (ph === 'e') {
// TODO: End user timing measure
} // eslint-disable-line brace-style
// Unrecognized event
else {
throw new InvalidProfileError(
`Unrecognized event ${JSON.stringify(
event,
)}! This is likely a bug in this profiler tool.`,
);
}
}
function preprocessFlamechart(rawData: TimelineEvent[]): Flamechart {
let parsedData;
try {
parsedData = importFromChromeTimeline(rawData, 'react-devtools');
} catch (error) {
// Assume any Speedscope errors are caused by bad profiles
const errorToRethrow = new InvalidProfileError(error.message);
errorToRethrow.stack = error.stack;
throw errorToRethrow;
}
const profile = parsedData.profiles[0]; // TODO: Choose the main CPU thread only
const speedscopeFlamechart = new SpeedscopeFlamechart({
getTotalWeight: profile.getTotalWeight.bind(profile),
forEachCall: profile.forEachCall.bind(profile),
formatValue: profile.formatValue.bind(profile),
getColorBucketForFrame: () => 0,
});
const flamechart: Flamechart = speedscopeFlamechart.getLayers().map(layer =>
layer.map(({start, end, node: {frame: {name, file, line, col}}}) => ({
name,
timestamp: start / 1000,
duration: (end - start) / 1000,
scriptUrl: file,
locationLine: line,
locationColumn: col,
})),
);
return flamechart;
}
export default function preprocessData(
timeline: TimelineEvent[],
): ReactProfilerData {
const flamechart = preprocessFlamechart(timeline);
const profilerData: ReactProfilerData = {
startTime: 0,
duration: 0,
events: [],
measures: [],
flamechart,
otherUserTimingMarks: [],
};
// Sort `timeline`. JSON Array Format trace events need not be ordered. See:
// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#heading=h.f2f0yd51wi15
timeline = timeline.filter(Boolean).sort((a, b) => (a.ts > b.ts ? 1 : -1));
// Events displayed in flamechart have timestamps relative to the profile
// event's startTime. Source: https://github.com/v8/v8/blob/44bd8fd7/src/inspector/js_protocol.json#L1486
//
// We'll thus expect there to be a 'Profile' event; if there is not one, we
// can deduce that there are no flame chart events. As we expect React
// scheduling profiling user timing marks to be recorded together with browser
// flame chart events, we can futher deduce that the data is invalid and we
// don't bother finding React events.
const indexOfProfileEvent = timeline.findIndex(
event => event.name === 'Profile',
);
if (indexOfProfileEvent === -1) {
return profilerData;
}
// Use Profile event's `startTime` as the start time to align with flame chart.
// TODO: Remove assumption that there'll only be 1 'Profile' event. If this
// assumption does not hold, the chart may start at the wrong time.
profilerData.startTime = timeline[indexOfProfileEvent].args.data.startTime;
profilerData.duration =
(timeline[timeline.length - 1].ts - profilerData.startTime) / 1000;
const state: ProcessorState = {
batchUID: 0,
uidCounter: 0,
nextRenderShouldGenerateNewBatchID: true,
measureStack: [],
};
timeline.forEach(event => processTimelineEvent(event, profilerData, state));
// Validate that all events and measures are complete
const {measureStack} = state;
if (measureStack.length > 0) {
console.error('Incomplete events or measures', measureStack);
}
return profilerData;
}