-
Notifications
You must be signed in to change notification settings - Fork 47.5k
/
Copy pathSchedulingProfiler.js
387 lines (339 loc) · 11.2 KB
/
SchedulingProfiler.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
/**
* 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 type {Lane, Lanes} from './ReactFiberLane.old';
import type {Fiber} from './ReactInternalTypes';
import type {Wakeable} from 'shared/ReactTypes';
import {
enableNewReconciler,
enableSchedulingProfiler,
} from 'shared/ReactFeatureFlags';
import ReactVersion from 'shared/ReactVersion';
import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import {SCHEDULING_PROFILER_VERSION} from 'react-devtools-timeline/src/constants';
import {
getLabelForLane as getLabelForLane_old,
TotalLanes as TotalLanes_old,
} from 'react-reconciler/src/ReactFiberLane.old';
import {
getLabelForLane as getLabelForLane_new,
TotalLanes as TotalLanes_new,
} from 'react-reconciler/src/ReactFiberLane.new';
const getLabelForLane = enableNewReconciler
? getLabelForLane_new
: getLabelForLane_old;
const TotalLanes = enableNewReconciler ? TotalLanes_new : TotalLanes_old;
/**
* If performance exists and supports the subset of the User Timing API that we
* require.
*/
const supportsUserTiming =
typeof performance !== 'undefined' &&
typeof performance.mark === 'function' &&
typeof performance.clearMarks === 'function';
let supportsUserTimingV3 = false;
if (enableSchedulingProfiler) {
if (supportsUserTiming) {
const CHECK_V3_MARK = '__v3';
const markOptions = {};
// $FlowFixMe: Ignore Flow complaining about needing a value
Object.defineProperty(markOptions, 'startTime', {
get: function() {
supportsUserTimingV3 = true;
return 0;
},
set: function() {},
});
try {
// $FlowFixMe: Flow expects the User Timing level 2 API.
performance.mark(CHECK_V3_MARK, markOptions);
} catch (error) {
// Ignore
} finally {
performance.clearMarks(CHECK_V3_MARK);
}
}
}
const laneLabels: Array<string> = [];
export function getLaneLabels(): Array<string> {
if (laneLabels.length === 0) {
let lane = 1;
for (let index = 0; index < TotalLanes; index++) {
laneLabels.push(((getLabelForLane(lane): any): string));
lane *= 2;
}
}
return laneLabels;
}
function markLaneToLabelMetadata() {
getLaneLabels();
markAndClear(`--react-lane-labels-${laneLabels.join(',')}`);
}
function markAndClear(name) {
performance.mark(name);
performance.clearMarks(name);
}
function markVersionMetadata() {
markAndClear(`--react-version-${ReactVersion}`);
markAndClear(`--profiler-version-${SCHEDULING_PROFILER_VERSION}`);
}
function markInternalModuleRanges() {
/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.getInternalModuleRanges === 'function'
) {
const ranges = __REACT_DEVTOOLS_GLOBAL_HOOK__.getInternalModuleRanges();
for (let i = 0; i < ranges.length; i++) {
const [startStackFrame, stopStackFrame] = ranges[i];
markAndClear(`--react-internal-module-start-${startStackFrame}`);
markAndClear(`--react-internal-module-stop-${stopStackFrame}`);
}
}
}
export function markCommitStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear(`--commit-start-${lanes}`);
// Certain types of metadata should be logged infrequently.
// Normally we would log this during module init,
// but there's no guarantee a user is profiling at that time.
// Commits happen infrequently (less than renders or state updates)
// so we log this extra information along with a commit.
// It will likely be logged more than once but that's okay.
//
// TODO Once DevTools supports starting/stopping the profiler,
// we can log this data only once (when started) and remove the per-commit logging.
markVersionMetadata();
markLaneToLabelMetadata();
markInternalModuleRanges();
}
}
}
export function markCommitStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--commit-stop');
}
}
}
export function markComponentRenderStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
// TODO (timeline) Add component stack id
markAndClear(`--component-render-start-${componentName}`);
}
}
}
export function markComponentRenderStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--component-render-stop');
}
}
}
export function markComponentPassiveEffectMountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
// TODO (timeline) Add component stack id
markAndClear(`--component-passive-effect-mount-start-${componentName}`);
}
}
}
export function markComponentPassiveEffectMountStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--component-passive-effect-mount-stop');
}
}
}
export function markComponentPassiveEffectUnmountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
// TODO (timeline) Add component stack id
markAndClear(`--component-passive-effect-unmount-start-${componentName}`);
}
}
}
export function markComponentPassiveEffectUnmountStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--component-passive-effect-unmount-stop');
}
}
}
export function markComponentLayoutEffectMountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
// TODO (timeline) Add component stack id
markAndClear(`--component-layout-effect-mount-start-${componentName}`);
}
}
}
export function markComponentLayoutEffectMountStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--component-layout-effect-mount-stop');
}
}
}
export function markComponentLayoutEffectUnmountStarted(fiber: Fiber): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
// TODO (timeline) Add component stack id
markAndClear(`--component-layout-effect-unmount-start-${componentName}`);
}
}
}
export function markComponentLayoutEffectUnmountStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--component-layout-effect-unmount-stop');
}
}
}
export function markComponentErrored(
fiber: Fiber,
thrownValue: mixed,
lanes: Lanes,
): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
const phase = fiber.alternate === null ? 'mount' : 'update';
let message = '';
if (
thrownValue !== null &&
typeof thrownValue === 'object' &&
typeof thrownValue.message === 'string'
) {
message = thrownValue.message;
} else if (typeof thrownValue === 'string') {
message = thrownValue;
}
// TODO (timeline) Add component stack id
markAndClear(`--error-${componentName}-${phase}-${message}`);
}
}
}
const PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
// $FlowFixMe: Flow cannot handle polymorphic WeakMaps
const wakeableIDs: WeakMap<Wakeable, number> = new PossiblyWeakMap();
let wakeableID: number = 0;
function getWakeableID(wakeable: Wakeable): number {
if (!wakeableIDs.has(wakeable)) {
wakeableIDs.set(wakeable, wakeableID++);
}
return ((wakeableIDs.get(wakeable): any): number);
}
export function markComponentSuspended(
fiber: Fiber,
wakeable: Wakeable,
lanes: Lanes,
): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const eventType = wakeableIDs.has(wakeable) ? 'resuspend' : 'suspend';
const id = getWakeableID(wakeable);
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
const phase = fiber.alternate === null ? 'mount' : 'update';
// Following the non-standard fn.displayName convention,
// frameworks like Relay may also annotate Promises with a displayName,
// describing what operation/data the thrown Promise is related to.
// When this is available we should pass it along to the Timeline.
const displayName = (wakeable: any).displayName || '';
// TODO (timeline) Add component stack id
markAndClear(
`--suspense-${eventType}-${id}-${componentName}-${phase}-${lanes}-${displayName}`,
);
wakeable.then(
() => markAndClear(`--suspense-resolved-${id}-${componentName}`),
() => markAndClear(`--suspense-rejected-${id}-${componentName}`),
);
}
}
}
export function markLayoutEffectsStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear(`--layout-effects-start-${lanes}`);
}
}
}
export function markLayoutEffectsStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--layout-effects-stop');
}
}
}
export function markPassiveEffectsStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear(`--passive-effects-start-${lanes}`);
}
}
}
export function markPassiveEffectsStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--passive-effects-stop');
}
}
}
export function markRenderStarted(lanes: Lanes): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear(`--render-start-${lanes}`);
}
}
}
export function markRenderYielded(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--render-yield');
}
}
}
export function markRenderStopped(): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear('--render-stop');
}
}
}
export function markRenderScheduled(lane: Lane): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
markAndClear(`--schedule-render-${lane}`);
}
}
}
export function markForceUpdateScheduled(fiber: Fiber, lane: Lane): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
// TODO (timeline) Add component stack id
markAndClear(`--schedule-forced-update-${lane}-${componentName}`);
}
}
}
export function markStateUpdateScheduled(fiber: Fiber, lane: Lane): void {
if (enableSchedulingProfiler) {
if (supportsUserTimingV3) {
const componentName = getComponentNameFromFiber(fiber) || 'Unknown';
// TODO (timeline) Add component stack id
markAndClear(`--schedule-state-update-${lane}-${componentName}`);
}
}
}