Repository navigation
Expand file tree
/
Copy pathmemory-machine.ts
More file actions
151 lines (124 loc) · 5.69 KB
/
Copy pathmemory-machine.ts
File metadata and controls
151 lines (124 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Main's memory diagnostics. `on` writes one sample per interval to the run's JSONL file: `waiting` for the timer,
* then `sampling` while the sample is collected and appended. A sample of an earlier attempt, or of a run that turned
* off, writes nothing. `probing` lists the `WebContents.id`s whose renderer probe has not settled, across runs, so a
* hung renderer gets one evaluation at a time however often diagnostics turn off and on. `issued` numbers runs and
* samples alike.
*/
export type MemoryDiagnosticsState =
| { readonly status: "off"; readonly issued: number; readonly probing: ReadonlySet<number> }
| {
readonly status: "on"
readonly issued: number
readonly run: number
readonly sampling: number | undefined
readonly probing: ReadonlySet<number>
}
| { readonly status: "disposed" }
export type MemoryDiagnosticsEvent =
/** Whether diagnostics should run: the environment asks for them, or the debug bar's stored toggle does. */
| { readonly type: "synced"; readonly enabled: boolean }
| { readonly type: "tick"; readonly run: number }
| { readonly type: "probe.started"; readonly contentsId: number }
| { readonly type: "probe.settled"; readonly contentsId: number }
| { readonly type: "sample.collected"; readonly attempt: number; readonly line: string }
| { readonly type: "sample.written"; readonly attempt: number }
| { readonly type: "sample.failed"; readonly attempt: number; readonly error: string }
| { readonly type: "disposed" }
export type MemoryDiagnosticsEffect =
/** Opens the run's file and, where the platform has it, the heap snapshot signal. */
| { readonly type: "log.start"; readonly run: number }
| { readonly type: "log.stop"; readonly run: number }
| { readonly type: "timer.schedule"; readonly run: number }
| { readonly type: "timer.cancel" }
| { readonly type: "sample.collect"; readonly attempt: number }
| { readonly type: "sample.write"; readonly attempt: number; readonly line: string }
| { readonly type: "sample.report"; readonly error: string }
export type MemoryDiagnosticsTransition = {
readonly state: MemoryDiagnosticsState
readonly effects: readonly MemoryDiagnosticsEffect[]
}
export function createMemoryDiagnosticsState(): MemoryDiagnosticsState {
return { status: "off", issued: 0, probing: new Set() }
}
export function transitionMemoryDiagnostics(
state: MemoryDiagnosticsState,
event: MemoryDiagnosticsEvent,
): MemoryDiagnosticsTransition {
if (state.status === "disposed") return { state, effects: [] }
if (event.type === "disposed") {
return { state: { status: "disposed" }, effects: state.status === "on" ? stop(state.run) : [] }
}
if (event.type === "probe.started") {
if (state.probing.has(event.contentsId)) return { state, effects: [] }
return { state: { ...state, probing: new Set([...state.probing, event.contentsId]) }, effects: [] }
}
if (event.type === "probe.settled") {
if (!state.probing.has(event.contentsId)) return { state, effects: [] }
return {
state: { ...state, probing: new Set([...state.probing].filter((id) => id !== event.contentsId)) },
effects: [],
}
}
if (event.type === "synced") {
if (event.enabled === (state.status === "on")) return { state, effects: [] }
if (state.status === "on") {
return { state: { status: "off", issued: state.issued, probing: state.probing }, effects: stop(state.run) }
}
const run = state.issued + 1
return {
state: { status: "on", issued: run, run, sampling: undefined, probing: state.probing },
effects: [
{ type: "log.start", run },
{ type: "timer.schedule", run },
],
}
}
if (state.status === "off") return { state, effects: [] }
if (event.type === "tick") {
if (event.run !== state.run || state.sampling !== undefined) return { state, effects: [] }
const attempt = state.issued + 1
return { state: { ...state, issued: attempt, sampling: attempt }, effects: [{ type: "sample.collect", attempt }] }
}
if (event.attempt !== state.sampling) return { state, effects: [] }
if (event.type === "sample.collected") {
return { state, effects: [{ type: "sample.write", attempt: event.attempt, line: event.line }] }
}
return {
state: { ...state, sampling: undefined },
effects: [
...(event.type === "sample.failed" ? [{ type: "sample.report" as const, error: event.error }] : []),
{ type: "timer.schedule", run: state.run },
],
}
}
/** Whether diagnostics run, as the debug bar shows them. */
export function isMemoryDiagnosticsOn(state: MemoryDiagnosticsState) {
return state.status === "on"
}
/** Whether a renderer's earlier probe is still unanswered, so the next sample skips it. */
export function isRendererProbing(state: MemoryDiagnosticsState, contentsId: number) {
return state.status !== "disposed" && state.probing.has(contentsId)
}
function stop(run: number): MemoryDiagnosticsEffect[] {
return [{ type: "timer.cancel" }, { type: "log.stop", run }]
}
// --- The debug bar in a window ---
/**
* `pinned` keeps the bar open while memory diagnostics run, so they are never silently on; it has no close button
* then, and the Knobs workspace follows the bar rather than the stored visibility.
*/
export type DebugBarVisibility = "hidden" | "shown" | "pinned"
export function debugBarView(
bar: { readonly visible: boolean; readonly motion: boolean },
memory: { readonly on: boolean } | undefined,
) {
const visibility: DebugBarVisibility = memory?.on ? "pinned" : bar.visible ? "shown" : "hidden"
return {
visibility,
open: visibility !== "hidden",
closable: visibility === "shown",
togglable: visibility !== "pinned",
motion: visibility !== "hidden" && bar.motion,
}
}