Skip to content

Commit 0f2cc1a

Browse files
committed
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.
1 parent f738966 commit 0f2cc1a

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

packages/scheduler/src/SchedulerProfiling.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {NoPriority} from './SchedulerPriorities';
1515
let runIdCounter: number = 0;
1616
let mainThreadIdCounter: number = 0;
1717

18-
const profilingStateSize = 3;
18+
const profilingStateSize = 4;
1919
export const sharedProfilingBuffer =
2020
// $FlowFixMe Flow doesn't know about SharedArrayBuffer
2121
typeof SharedArrayBuffer === 'function'
@@ -29,7 +29,8 @@ const profilingState = enableProfiling
2929

3030
const PRIORITY = 0;
3131
const CURRENT_TASK_ID = 1;
32-
const QUEUE_SIZE = 2;
32+
const CURRENT_RUN_ID = 2;
33+
const QUEUE_SIZE = 3;
3334

3435
if (enableProfiling && profilingState !== null) {
3536
profilingState[PRIORITY] = NoPriority;
@@ -160,13 +161,14 @@ export function markTaskRun(
160161
time: number,
161162
) {
162163
if (enableProfiling) {
164+
runIdCounter++;
165+
163166
if (profilingState !== null) {
164167
profilingState[PRIORITY] = task.priorityLevel;
165168
profilingState[CURRENT_TASK_ID] = task.id;
169+
profilingState[CURRENT_RUN_ID] = runIdCounter;
166170
}
167171

168-
runIdCounter++;
169-
170172
if (eventLog !== null) {
171173
logEvent([TaskRunEvent, time, task.id, runIdCounter]);
172174
}
@@ -178,6 +180,7 @@ export function markTaskYield(task: {id: number}, time: number) {
178180
if (profilingState !== null) {
179181
profilingState[PRIORITY] = NoPriority;
180182
profilingState[CURRENT_TASK_ID] = 0;
183+
profilingState[CURRENT_RUN_ID] = 0;
181184
}
182185

183186
if (eventLog !== null) {

packages/scheduler/src/__tests__/SchedulerProfiling-test.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ describe('Scheduler', () => {
7676
// shouldYield = Scheduler.unstable_shouldYield;
7777
});
7878

79+
const PRIORITY = 0;
80+
const CURRENT_TASK_ID = 1;
81+
const CURRENT_RUN_ID = 2;
82+
const QUEUE_SIZE = 3;
83+
7984
afterEach(() => {
80-
if (sharedProfilingArray[2] !== 0) {
85+
if (sharedProfilingArray[QUEUE_SIZE] !== 0) {
8186
throw Error(
8287
'Test exited, but the shared profiling buffer indicates that a task ' +
8388
'is still running',
@@ -241,9 +246,6 @@ describe('Scheduler', () => {
241246
return '\n' + result;
242247
}
243248

244-
const PRIORITY = 0;
245-
const CURRENT_TASK_ID = 1;
246-
const QUEUE_SIZE = 2;
247249
function getProfilingInfo() {
248250
const queueSize = sharedProfilingArray[QUEUE_SIZE];
249251
if (queueSize === 0) {
@@ -253,11 +255,12 @@ describe('Scheduler', () => {
253255
if (priorityLevel === 0) {
254256
return 'Suspended, Queue Size: ' + queueSize;
255257
}
256-
return `Current Task: ${
257-
sharedProfilingArray[QUEUE_SIZE]
258-
}, Priority: ${priorityLevelToString(priorityLevel)}, Queue Size: ${
259-
sharedProfilingArray[CURRENT_TASK_ID]
260-
}`;
258+
return (
259+
`Task: ${sharedProfilingArray[CURRENT_TASK_ID]}, ` +
260+
`Run: ${sharedProfilingArray[CURRENT_RUN_ID]}, ` +
261+
`Priority: ${priorityLevelToString(priorityLevel)}, ` +
262+
`Queue Size: ${sharedProfilingArray[QUEUE_SIZE]}`
263+
);
261264
}
262265

263266
it('creates a basic flamegraph', () => {
@@ -287,13 +290,13 @@ describe('Scheduler', () => {
287290
{label: 'Foo'},
288291
);
289292
expect(Scheduler).toFlushAndYieldThrough([
290-
'Current Task: 1, Priority: Normal, Queue Size: 1',
293+
'Task: 1, Run: 1, Priority: Normal, Queue Size: 1',
291294
'Yield',
292295
]);
293296
Scheduler.unstable_advanceTime(100);
294297
expect(Scheduler).toFlushAndYield([
295-
'Current Task: 2, Priority: User-blocking, Queue Size: 2',
296-
'Current Task: 1, Priority: Normal, Queue Size: 1',
298+
'Task: 2, Run: 2, Priority: User-blocking, Queue Size: 2',
299+
'Task: 1, Run: 3, Priority: Normal, Queue Size: 1',
297300
]);
298301

299302
expect(getProfilingInfo()).toEqual('Empty Queue');
@@ -321,7 +324,7 @@ Task 1 [Normal] │ ████████░░░░░░░
321324
});
322325

323326
expect(Scheduler).toFlushAndYieldThrough([
324-
'Current Task: 1, Priority: Normal, Queue Size: 1',
327+
'Task: 1, Run: 1, Priority: Normal, Queue Size: 1',
325328
'Yield',
326329
]);
327330
Scheduler.unstable_advanceTime(100);

0 commit comments

Comments
 (0)