跳到主要内容

SchedulerProfiling (调度器的性能分析)

该文件包含了一些调度器的性能分析及。

一、 标记任务开始

export function markTaskStart(
task: {
id: number;
priorityLevel: PriorityLevel;
// ...
},
ms: number,
): void {
if (enableProfiling) {
if (eventLog !== null) {
// performance.now 返回一个浮点数,表示毫秒。
// performance.now returns a float, representing milliseconds. When the
// 当事件被记录时,它会被强制转换为整数。
// event is logged, it's coerced to an int. Convert to microseconds to
// maintain extra degrees of precision.
// 为了保持额外的精度,将其转换为微秒。
// TaskStartEvent = 1
logEvent([TaskStartEvent, ms * 1000, task.id, task.priorityLevel]);
}
}
}

二、 标记任务已完成

export function markTaskCompleted(
task: {
id: number;
priorityLevel: PriorityLevel;
// ...
},
ms: number,
) {
if (enableProfiling) {
if (eventLog !== null) {
// TaskCompleteEvent = 2
logEvent([TaskCompleteEvent, ms * 1000, task.id]);
}
}
}

三、标记任务已取消

export function markTaskCanceled(
task: {
id: number;
priorityLevel: PriorityLevel;
// ...
},
ms: number,
) {
if (enableProfiling) {
if (eventLog !== null) {
// TaskCancelEvent = 4
logEvent([TaskCancelEvent, ms * 1000]);
}
}
}

四、 标记任务出错

export function markTaskCanceled(
task: {
id: number;
priorityLevel: PriorityLevel;
// ...
},
ms: number,
) {
if (enableProfiling) {
if (eventLog !== null) {
// TaskErrorEvent = 3
logEvent([TaskErrorEvent, ms * 1000, task.id]);
}
}
}

五、 标记任务执行

export function markTaskRun(
task: {
id: number;
priorityLevel: PriorityLevel;
// ...
},
ms: number,
) {
if (enableProfiling) {
runIdCounter++;

if (eventLog !== null) {
// TaskRunEvent = 5
logEvent([TaskRunEvent, ms * 1000, task.id, runIdCounter]);
}
}
}

六、 标记任务暂定

export function markTaskYield(task: { id: number }, ms: number) {
if (enableProfiling) {
if (eventLog !== null) {
// TaskYieldEvent = 6
logEvent([TaskYieldEvent, ms * 1000, task.id, runIdCounter]);
}
}
}

七、 标记调度器为暂定

export function markSchedulerSuspended(ms: number) {
if (enableProfiling) {
mainThreadIdCounter++;

if (eventLog !== null) {
// SchedulerSuspendEvent = 7
logEvent([SchedulerSuspendEvent, ms * 1000, mainThreadIdCounter]);
}
}
}

八、标记调度器为未暂停

export function markSchedulerUnsuspended(ms: number) {
if (enableProfiling) {
if (eventLog !== null) {
// SchedulerResumeEvent = 8
logEvent([SchedulerResumeEvent, ms * 1000, mainThreadIdCounter]);
}
}
}

九、 工具

信息

事件日志在主逻辑代码中貌似并没有被使用,大概率是新增的功能未完善。

1. 开始记录分析事件

export function startLoggingProfilingEvents(): void {
eventLogSize = INITIAL_EVENT_LOG_SIZE;
eventLogBuffer = new ArrayBuffer(eventLogSize * 4);
eventLog = new Int32Array(eventLogBuffer);
eventLogIndex = 0;
}

2. 停止记录分析事件

export function stopLoggingProfilingEvents(): ArrayBuffer | null {
const buff = eventLogBuffer;
eventLogSize = 0;
eventLogSize = null;
eventLog = null;
eventLogIndex = 0;
return buffer;
}

事件日志

function logEvent(entries: Array<number | PriorityLevel>) {
/** 实现逻辑 */
}

十、其他

在文件头部,还定义了其他的本文件内函数使用的变量及常量。

let runIdCounter: number = 0;
let mainThreadIdCounter: number = 0;

// Bytes per element is 4
const INITIAL_EVENT_LOG_SIZE = 131072;
const MAX_EVENT_LOG_SIZE = 524288; // Equivalent to 2 megabytes

let eventLogSize = 0;
let eventLogBuffer = null;
let eventLog = null;
let eventLogIndex = 0;

const TaskStartEvent = 1;
const TaskCompleteEvent = 2;
const TaskErrorEvent = 3;
const TaskCancelEvent = 4;
const TaskRunEvent = 5;
const TaskYieldEvent = 6;
const SchedulerSuspendEvent = 7;
const SchedulerResumeEvent = 8;