-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP][Scheduler] Mark user-timing events
Marks when Scheduler starts and stops running a task. Also marks when a task is initially scheduled, and when Scheduler is waiting for a callback, which can't be inferred from a sample-based JavaScript CPU profile alone. The plan is to use the user-timing events to build a Scheduler profiler that shows how the lifetime of tasks interact with each other and with unscheduled main thread work. The test suite works by printing an text representation of a Scheduler flamegraph.
- Loading branch information
Showing
7 changed files
with
625 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
export type PriorityLevel = 1 | 2 | 3 | 4 | 5; | ||
|
||
// TODO: Use symbols? | ||
export const ImmediatePriority = 1; | ||
export const UserBlockingPriority = 2; | ||
export const NormalPriority = 3; | ||
export const LowPriority = 4; | ||
export const IdlePriority = 5; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* 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 {PriorityLevel} from './SchedulerPriorities'; | ||
|
||
import {enableProfiling as enableProfilingFeatureFlag} from './SchedulerFeatureFlags'; | ||
import { | ||
ImmediatePriority, | ||
UserBlockingPriority, | ||
NormalPriority, | ||
LowPriority, | ||
IdlePriority, | ||
} from './SchedulerPriorities'; | ||
|
||
const enableProfiling = | ||
enableProfilingFeatureFlag && | ||
typeof performance !== 'undefined' && | ||
typeof performance.mark === 'function' && | ||
typeof performance.clearMarks === 'function' && | ||
typeof performance.measure === 'function' && | ||
typeof performance.clearMeasures === 'function'; | ||
|
||
let runIdCounter: number = 0; | ||
let mainThreadIdCounter: number = 0; | ||
|
||
export const TaskCompleted = 0; | ||
export const TaskErrored = 1; | ||
export const TaskCanceled = 2; | ||
|
||
function priorityLevelToString(priorityLevel) { | ||
if (enableProfiling) { | ||
switch (priorityLevel) { | ||
case ImmediatePriority: | ||
return 'Immediate'; | ||
case UserBlockingPriority: | ||
return 'User-blocking'; | ||
case NormalPriority: | ||
return 'Normal'; | ||
case LowPriority: | ||
return 'Low'; | ||
case IdlePriority: | ||
return 'Idle'; | ||
} | ||
} | ||
} | ||
|
||
export function markTaskStart(task: {id: number, _didStart: true | void}) { | ||
if (enableProfiling) { | ||
// Use extra field to track if delayed task starts. | ||
task._didStart = true; | ||
performance.mark(`SchedulerTask-Start-${task.id}`); | ||
} | ||
} | ||
|
||
export function markTaskEnd(task: { | ||
id: number, | ||
_didStart: true | void, | ||
priorityLevel: PriorityLevel, | ||
}) { | ||
if (enableProfiling) { | ||
if (task._didStart !== true) { | ||
// This is a delayed task that never started running. In this case, | ||
// there is not start mark that we can measure. | ||
// TODO: Better model for delayed tasks. Mark when they are first posted | ||
// and when delay elapses. | ||
return; | ||
} | ||
const info = JSON.stringify({priorityLevel: task.priorityLevel}); | ||
const taskEndMark = `SchedulerTask-End-${task.id}-${info}`; | ||
performance.mark(taskEndMark); | ||
|
||
// Record that the task finished | ||
const taskStartMark = `SchedulerTask-Start-${task.id}`; | ||
const priorityString = priorityLevelToString(task.priorityLevel); | ||
const taskMeasure = `[${task.id}] ${ | ||
priorityString === undefined ? 'Unknown' : priorityString | ||
} priority`; | ||
try { | ||
// This could throw if one of the marks was already cleared, either | ||
// because of a bug in Scheduler or code elsewhere. | ||
performance.measure(taskMeasure, taskStartMark, taskEndMark); | ||
} catch (e) { | ||
// Ignore the error. Print the message to the console in dev. | ||
if (__DEV__) { | ||
console.error(e.message); | ||
} | ||
} | ||
performance.clearMarks(taskStartMark); | ||
performance.clearMarks(taskEndMark); | ||
performance.clearMeasures(taskMeasure); | ||
} | ||
} | ||
|
||
export function markTaskRun(task: {id: number}) { | ||
if (enableProfiling) { | ||
runIdCounter++; | ||
const runMark = `SchedulerTask-Run-${task.id}-${runIdCounter}`; | ||
performance.mark(runMark); | ||
performance.clearMarks(runMark); | ||
} | ||
} | ||
|
||
export function markTaskYield(task: {id: number}) { | ||
if (enableProfiling) { | ||
const yieldMark = `SchedulerTask-Yield-${task.id}`; | ||
performance.mark(yieldMark); | ||
performance.clearMarks(yieldMark); | ||
} | ||
} | ||
|
||
export function markSchedulerSuspended() { | ||
if (enableProfiling) { | ||
mainThreadIdCounter++; | ||
const suspendStartMark = 'SchedulerSuspended-Start-' + mainThreadIdCounter; | ||
performance.mark(suspendStartMark); | ||
performance.clearMarks(suspendStartMark); | ||
} | ||
} | ||
|
||
export function markSchedulerUnsuspended() { | ||
if (enableProfiling) { | ||
const suspendedEndMark = 'SchedulerSuspended-End-' + mainThreadIdCounter; | ||
performance.mark(suspendedEndMark); | ||
performance.clearMarks(suspendedEndMark); | ||
} | ||
} |
Oops, something went wrong.