From d374a95f8219afe17f115d6bc8751b87f00ea81c Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Tue, 13 Aug 2019 18:21:34 -0700 Subject: [PATCH] Record the current run ID Each synchronous block of Scheduler work is given a unique run ID. This is different than a task ID because a single task will have more than one run if it yields with a continuation. --- packages/scheduler/src/SchedulerProfiling.js | 11 ++++--- .../src/__tests__/SchedulerProfiling-test.js | 29 ++++++++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/packages/scheduler/src/SchedulerProfiling.js b/packages/scheduler/src/SchedulerProfiling.js index 6b820497b8df..cd13676f8bed 100644 --- a/packages/scheduler/src/SchedulerProfiling.js +++ b/packages/scheduler/src/SchedulerProfiling.js @@ -15,7 +15,7 @@ import {NoPriority} from './SchedulerPriorities'; let runIdCounter: number = 0; let mainThreadIdCounter: number = 0; -const profilingStateSize = 3; +const profilingStateSize = 4; export const sharedProfilingBuffer = // $FlowFixMe Flow doesn't know about SharedArrayBuffer typeof SharedArrayBuffer === 'function' @@ -29,7 +29,8 @@ const profilingState = enableProfiling const PRIORITY = 0; const CURRENT_TASK_ID = 1; -const QUEUE_SIZE = 2; +const CURRENT_RUN_ID = 2; +const QUEUE_SIZE = 3; if (enableProfiling && profilingState !== null) { profilingState[PRIORITY] = NoPriority; @@ -162,13 +163,14 @@ export function markTaskRun( time: number, ) { if (enableProfiling) { + runIdCounter++; + if (profilingState !== null) { profilingState[PRIORITY] = task.priorityLevel; profilingState[CURRENT_TASK_ID] = task.id; + profilingState[CURRENT_RUN_ID] = runIdCounter; } - runIdCounter++; - if (eventLog !== null) { logEvent([TaskRunEvent, time, task.id, runIdCounter]); } @@ -180,6 +182,7 @@ export function markTaskYield(task: {id: number}, time: number) { if (profilingState !== null) { profilingState[PRIORITY] = NoPriority; profilingState[CURRENT_TASK_ID] = 0; + profilingState[CURRENT_RUN_ID] = 0; } if (eventLog !== null) { diff --git a/packages/scheduler/src/__tests__/SchedulerProfiling-test.js b/packages/scheduler/src/__tests__/SchedulerProfiling-test.js index b9ae2b066a02..cfb4c73de3c3 100644 --- a/packages/scheduler/src/__tests__/SchedulerProfiling-test.js +++ b/packages/scheduler/src/__tests__/SchedulerProfiling-test.js @@ -76,8 +76,13 @@ describe('Scheduler', () => { // shouldYield = Scheduler.unstable_shouldYield; }); + const PRIORITY = 0; + const CURRENT_TASK_ID = 1; + const CURRENT_RUN_ID = 2; + const QUEUE_SIZE = 3; + afterEach(() => { - if (sharedProfilingArray[2] !== 0) { + if (sharedProfilingArray[QUEUE_SIZE] !== 0) { throw Error( 'Test exited, but the shared profiling buffer indicates that a task ' + 'is still running', @@ -241,9 +246,6 @@ describe('Scheduler', () => { return '\n' + result; } - const PRIORITY = 0; - const CURRENT_TASK_ID = 1; - const QUEUE_SIZE = 2; function getProfilingInfo() { const queueSize = sharedProfilingArray[QUEUE_SIZE]; if (queueSize === 0) { @@ -253,11 +255,12 @@ describe('Scheduler', () => { if (priorityLevel === 0) { return 'Suspended, Queue Size: ' + queueSize; } - return `Current Task: ${ - sharedProfilingArray[QUEUE_SIZE] - }, Priority: ${priorityLevelToString(priorityLevel)}, Queue Size: ${ - sharedProfilingArray[CURRENT_TASK_ID] - }`; + return ( + `Task: ${sharedProfilingArray[CURRENT_TASK_ID]}, ` + + `Run: ${sharedProfilingArray[CURRENT_RUN_ID]}, ` + + `Priority: ${priorityLevelToString(priorityLevel)}, ` + + `Queue Size: ${sharedProfilingArray[QUEUE_SIZE]}` + ); } it('creates a basic flamegraph', () => { @@ -287,13 +290,13 @@ describe('Scheduler', () => { {label: 'Foo'}, ); expect(Scheduler).toFlushAndYieldThrough([ - 'Current Task: 1, Priority: Normal, Queue Size: 1', + 'Task: 1, Run: 1, Priority: Normal, Queue Size: 1', 'Yield', ]); Scheduler.unstable_advanceTime(100); expect(Scheduler).toFlushAndYield([ - 'Current Task: 2, Priority: User-blocking, Queue Size: 2', - 'Current Task: 1, Priority: Normal, Queue Size: 1', + 'Task: 2, Run: 2, Priority: User-blocking, Queue Size: 2', + 'Task: 1, Run: 3, Priority: Normal, Queue Size: 1', ]); expect(getProfilingInfo()).toEqual('Empty Queue'); @@ -321,7 +324,7 @@ Task 1 [Normal] │ ████████░░░░░░░ }); expect(Scheduler).toFlushAndYieldThrough([ - 'Current Task: 1, Priority: Normal, Queue Size: 1', + 'Task: 1, Run: 1, Priority: Normal, Queue Size: 1', 'Yield', ]); Scheduler.unstable_advanceTime(100);