Skip to content

Commit

Permalink
Record the current run ID
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acdlite committed Aug 14, 2019
1 parent f738966 commit 0f2cc1a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
11 changes: 7 additions & 4 deletions packages/scheduler/src/SchedulerProfiling.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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;
Expand Down Expand Up @@ -160,13 +161,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]);
}
Expand All @@ -178,6 +180,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) {
Expand Down
29 changes: 16 additions & 13 deletions packages/scheduler/src/__tests__/SchedulerProfiling-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0f2cc1a

Please sign in to comment.