1
>
/*---------------------------------------------------------------------------------------------
executionGraph.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
/**
7
>
* Plain, renderer-friendly description of an execution history produced by a
8
>
* traced scheduler. These types have no dependency on the tracing or
9
>
* scheduling implementation — they can be built by hand in tests or by the
10
>
* `buildHistoryFromTasks` adapter below.
11
>
*/
12
>
13
>
export interface ExecutionRoot {
14
>
readonly label: string;
15
>
}
16
>
17
>
export interface ExecutionEvent {
18
>
/** Relative time (e.g. ms since startTime). Must be >= 0 and non-decreasing in history order. */
19
>
readonly time: number;
20
>
readonly label: string;
21
>
readonly root: ExecutionRoot;
22
>
/** `undefined` means this event is a direct child of its root. */
23
>
readonly parent: ExecutionEvent | undefined;
24
>
/** Caller frame extracted from the scheduling stack trace. */
25
>
readonly detail?: string;
26
>
}
27
>
28
>
export interface ExecutionHistory {
29
>
/** Roots in first-appearance order (column order for renderers). */
30
>
readonly roots: readonly ExecutionRoot[];
31
>
/** Events in time order. */
32
>
readonly events: readonly ExecutionEvent[];
33
>
}
34
>
35
>
// -----------------------------------------------------------------------------
36
>
// Adapter: ScheduledTask[] -> ExecutionHistory
37
>
// -----------------------------------------------------------------------------
38
>
39
>
interface TraceLike {
40
>
readonly parent: TraceLike | undefined;
41
>
readonly root: { readonly label: string };
42
>
}
43
>
44
>
interface ScheduledTaskLike {
45
>
readonly time: number;
46
>
readonly source: { toString(): string; readonly stackTrace?: string };
47
>
readonly trace?: TraceLike;
48
>
}
49
>
50
>
/**
51
>
* A log entry to weave into the history alongside scheduled tasks. Each log is
52
>
* tagged with the trace that was current when it was emitted.
53
>
*/
54
>
export interface LogEntryLike {
55
>
readonly trace: TraceLike;
56
>
readonly message: string;
57
>
}
58
>
59
>
/**
60
>
* Convert a list of scheduled tasks (each carrying a causal `trace`) into a
61
>
* plain `ExecutionHistory`. Untraced tasks are dropped. A task's parent event
62
>
* is the most recent earlier task whose `trace` is `task.trace.parent`; if
63
>
* `task.trace.parent` is the trace root itself, the event has no parent event
64
>
* (it is a direct child of the root).
65
>
*
66
>
* `logs` (if given) are interleaved as synthetic events: each log's parent is
67
>
* the task event whose trace matches the log's current trace at emission
68
>
* time (or the nearest ancestor task event), and its time is inherited from
69
>
* that parent. Within a single parent task, logs are kept in emission order
70
>
* and inserted directly after the parent event.
71
>
*/
72
>
export function buildHistoryFromTasks(
73
tasks: readonly ScheduledTaskLike[],
74
startTime: number,