Repository navigation
Expand file tree
/
Copy pathmain.ts
More file actions
417 lines (354 loc) · 16.5 KB
/
Copy pathmain.ts
File metadata and controls
417 lines (354 loc) · 16.5 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { app, contentTracing, screen, type BrowserWindow } from "electron"
import { Schema } from "effect"
import { appendFile, mkdir, readFile, stat, writeFile } from "node:fs/promises"
import path from "node:path"
import { getHeapSpaceStatistics, getHeapStatistics, writeHeapSnapshot } from "node:v8"
import type { Plugin } from "@opencode/plugin/gui/main"
import { HeapSnapshots, MemoryDiagnostics } from "./contract"
import {
createHeapCaptureState,
transitionHeapCapture,
type HeapCaptureEffect,
type HeapCaptureEvent,
type HeapCaptureResult,
} from "./heap-machine"
import { summarize, traceConfig } from "./memory-infra"
import { collectMemorySample, memoryLogFile } from "./memory-log"
import {
createMemoryDiagnosticsState,
isMemoryDiagnosticsOn,
isRendererProbing,
transitionMemoryDiagnostics,
type MemoryDiagnosticsEffect,
type MemoryDiagnosticsEvent,
} from "./memory-machine"
import { debugBuild, memoryLogInterval } from "./options"
import { captureServer } from "./server-heap"
import { signalIncludesServer, snapshotDirectory } from "./signal"
import { ProcessSignal } from "@opencode/util/process-signal"
/** A desktop process the manifest lists: the file written, or why there is none. */
type Snapshot = { readonly process: "main" | "window"; readonly pid: number; readonly window?: number } & (
| { readonly file: string }
| { readonly error: string }
)
/** How long a step that waits on another process may take before the manifest records it as failed. */
const SNAPSHOT_TIMEOUT_MS = 5 * 60_000
/** What a window's page holds, read from inside it. */
const PageStats = Schema.Struct({
elements: Schema.Number,
images: Schema.Struct({ count: Schema.Number, decodedMiB: Schema.Number }),
canvases: Schema.Struct({ count: Schema.Number, MiB: Schema.Number }),
svgUses: Schema.Number,
devicePixelRatio: Schema.Number,
viewport: Schema.Struct({ width: Schema.Number, height: Schema.Number }),
jsHeap: Schema.optional(
Schema.Struct({ usedJSHeapSize: Schema.Number, totalJSHeapSize: Schema.Number, jsHeapSizeLimit: Schema.Number }),
),
})
const decodePageStats = Schema.decodeUnknownPromise(PageStats)
// Runs in the app window. Pixel sizes estimate decoded memory at 4 bytes per pixel.
const pageStatsScript = `(() => {
const mib = (bytes) => Math.round(bytes / 1048576 * 10) / 10
const images = [...document.images]
const canvases = [...document.querySelectorAll("canvas")]
const memory = performance.memory
return {
elements: document.getElementsByTagName("*").length,
images: { count: images.length, decodedMiB: mib(images.reduce((t, i) => t + i.naturalWidth * i.naturalHeight * 4, 0)) },
canvases: { count: canvases.length, MiB: mib(canvases.reduce((t, c) => t + c.width * c.height * 4, 0)) },
svgUses: document.getElementsByTagName("use").length,
devicePixelRatio: window.devicePixelRatio,
viewport: { width: innerWidth, height: innerHeight },
jsHeap: memory && { usedJSHeapSize: memory.usedJSHeapSize, totalJSHeapSize: memory.totalJSHeapSize, jsHeapSizeLimit: memory.jsHeapSizeLimit },
}
})()`
const setup: Plugin.MainSetup = (ctx) => {
const capture = async (root: string, includeServer: boolean) => {
const directory = path.join(root, `opencode-heap-${new Date().toISOString().replace(/[:.]/g, "")}`)
// Snapshots hold prompts, file contents and credentials. Recursive, because Downloads can be missing on Linux.
await mkdir(directory, { recursive: true, mode: 0o700 })
ctx.log.write("info", "capturing heap snapshots", { directory, includeServer })
// Measurements first: writing a heap snapshot raises the memory of the process it serializes.
const before = {
metrics: app.getAppMetrics(),
mainMemory: process.memoryUsage(),
mainHeap: getHeapStatistics(),
mainHeapSpaces: getHeapSpaceStatistics(),
system: process.getSystemMemoryInfo(),
}
const native = await recordNative(directory)
const windows = ctx.windows.list()
const pages = await Promise.all(windows.map((window) => describeWindow(window)))
// The server is another process, so it serializes its heap while the windows do. Windows go one at a time: a
// snapshot can double a process's memory, and a leaking app may not have room for every window at once. Main goes
// last: writing its own heap blocks the loop that receives the others.
const [serverSnapshot, windowSnapshots] = await Promise.all([
includeServer
? captureServer(
ctx.serverEndpoints.get("sidecar"),
directory,
AbortSignal.any([ctx.signal, AbortSignal.timeout(SNAPSHOT_TIMEOUT_MS)]),
)
: undefined,
(async () => {
const list: Snapshot[] = []
for (const window of windows) list.push(await captureWindow(window, directory))
return list
})(),
])
const main = await Promise.resolve()
.then(() => writeHeapSnapshot(path.join(directory, `main-${process.pid}.heapsnapshot`)))
.then(
(file): Snapshot => ({ process: "main", pid: process.pid, file: path.basename(file) }),
(error: Error): Snapshot => ({ process: "main", pid: process.pid, error: error.message }),
)
const snapshots = await Promise.all(
[...(serverSnapshot ? [serverSnapshot] : []), ...windowSnapshots, main].map(async (snapshot) => {
if ("error" in snapshot) return snapshot
const file = "file" in snapshot ? path.join(directory, snapshot.file) : snapshot.path
const bytes = await stat(file).then(
(info) => info.size,
() => null,
)
ctx.log.write("info", "heap snapshot written", { file, bytes })
return { ...snapshot, bytes }
}),
)
const gpu = await within(app.getGPUInfo("basic"), 15_000, "The GPU process did not answer within 15 seconds.").catch(
(error: Error) => ({ error: error.message }),
)
// One readable answer to "where is the memory", per process, in MiB.
await write(directory, "memory.json", {
unit: "MiB",
native: "summary" in native ? native.summary : native,
server:
!serverSnapshot || "error" in serverSnapshot || !serverSnapshot.memory
? undefined
: mibs(serverSnapshot.memory),
pages,
note: "native: Chromium's memory-infra dump per process, before the heap snapshots. Some allocators overlap: cc, skia, gpu and web_cache report memory that malloc, discardable or shared_memory also count. pages: what each app window's page held. server: the server's process.memoryUsage().",
})
await write(directory, "gpu.json", gpu)
// Written last, so a script can wait for it.
await write(directory, "manifest.json", {
created: new Date().toISOString(),
includeServer,
app: {
name: app.getName(),
version: app.getVersion(),
packaged: app.isPackaged,
uptimeSeconds: process.uptime(),
platform: process.platform,
arch: process.arch,
versions: process.versions,
},
files: {
"memory.json": "Where the memory is, per process, in MiB. Start here.",
"memory-infra.json": "Chromium's raw memory dump trace. Open it in https://ui.perfetto.dev.",
"gpu.json": "The GPU and driver.",
"*.heapsnapshot": "Heap snapshots. Open them in the Memory panel of Chrome DevTools.",
},
snapshots,
// Per-process memory as the OS sees it: a process that grows while its heap does not leaks native memory.
metricsMemoryUnit: "KiB",
before,
after: { metrics: app.getAppMetrics(), mainMemory: process.memoryUsage() },
})
const complete = snapshots.every((snapshot) => !("error" in snapshot)) && !("error" in native)
ctx.log.write(complete ? "info" : "warn", "heap snapshots captured", { directory, complete })
return { directory, complete }
}
let heapState = createHeapCaptureState()
const dispatchHeap = (event: HeapCaptureEvent) => {
const next = transitionHeapCapture(heapState, event)
heapState = next.state
next.effects.forEach(
(effect) =>
void Promise.try(() => runHeap(effect)).catch((cause: unknown) =>
ctx.log.write("error", "heap snapshot queue effect failed", { error: String(cause) }),
),
)
}
const runHeap = (effect: HeapCaptureEffect) => {
if (effect.type === "replies.settle") {
const outcome = effect.outcome
return effect.replies.forEach((reply) => (outcome.ok ? reply.resolve(outcome.result) : reply.reject(outcome.error)))
}
if (effect.type === "signal.failed") {
return ctx.log.write("error", "heap snapshot capture failed", { error: effect.error })
}
void Promise.try(() => capture(effect.root, effect.includeServer)).then(
(result) => dispatchHeap({ type: "capture.succeeded", attempt: effect.attempt, result }),
(cause: unknown) =>
dispatchHeap({
type: "capture.failed",
attempt: effect.attempt,
error: cause instanceof Error ? cause : new Error(String(cause)),
}),
)
}
ctx.onCleanup(() => dispatchHeap({ type: "disposed" }))
ctx.provide(HeapSnapshots, {
// A window that closes meanwhile does not cancel the capture.
write: () =>
new Promise<HeapCaptureResult>((resolve, reject) =>
dispatchHeap({
type: "command.requested",
root: app.getPath("downloads"),
includeServer: true,
reply: { resolve, reject },
}),
),
})
if (!debugBuild(ctx.app)) return
// The environment turns diagnostics on for this launch; otherwise the debug bar's stored toggle does.
const envInterval = memoryLogInterval(process.env.OPENCODE_DESKTOP_MEMORY_LOG)
const interval = envInterval ?? 30_000
// Without the launch switch, Chromium quantizes renderer heap sizes and refreshes them rarely.
const precise = app.commandLine.hasSwitch("enable-precise-memory-info")
// Whether the debug bar turned memory diagnostics on.
const stored = ctx.storage.store("memory", { schema: Schema.Boolean, initial: false })
const directory = process.platform === "win32" ? undefined : snapshotDirectory(process.env)
let memoryState = createMemoryDiagnosticsState()
let memoryTimer: ReturnType<typeof setTimeout> | undefined
let memoryFile = ""
let unsubscribeSignal: (() => void) | undefined
// Signals coalesce in the queue; repeated signals must not fill the disk with queued dumps.
const onSignal = () => {
if (!directory) return
dispatchHeap({ type: "signal.received", root: directory, includeServer: signalIncludesServer(process.env) })
}
const dispatchMemory = (event: MemoryDiagnosticsEvent) => {
const next = transitionMemoryDiagnostics(memoryState, event)
memoryState = next.state
next.effects.forEach(
(effect) =>
void Promise.try(() => runMemory(effect)).catch((cause: unknown) =>
ctx.log.write("error", "memory diagnostics effect failed", { error: String(cause) }),
),
)
}
const runMemory = (effect: MemoryDiagnosticsEffect) => {
if (effect.type === "log.start") {
memoryFile = memoryLogFile()
ctx.log.write("info", "memory diagnostics started", { file: memoryFile, intervalMs: interval, precise })
if (!directory) return
// Installed the first time diagnostics turn on, and kept: a script's late signal while they are off, or while
// the plugin reloads, is logged and ignored instead of killing the app.
ProcessSignal.install("SIGUSR2", () => ctx.log.write("warn", "heap snapshot signal ignored: diagnostics are off"))
unsubscribeSignal = ProcessSignal.subscribe("SIGUSR2", onSignal)
ctx.log.write("info", "heap snapshot signal enabled", { pid: process.pid, signal: "SIGUSR2", directory })
return
}
if (effect.type === "log.stop") {
unsubscribeSignal?.()
unsubscribeSignal = undefined
ctx.log.write("info", "memory diagnostics stopped", { file: memoryFile })
return
}
if (effect.type === "timer.schedule") {
memoryTimer = setTimeout(() => dispatchMemory({ type: "tick", run: effect.run }), interval)
return
}
if (effect.type === "timer.cancel") return clearTimeout(memoryTimer)
if (effect.type === "sample.report") {
return ctx.log.write("error", "memory diagnostics sample failed", { error: effect.error })
}
const failed = (cause: unknown) =>
dispatchMemory({
type: "sample.failed",
attempt: effect.attempt,
error: cause instanceof Error ? cause.message : String(cause),
})
if (effect.type === "sample.write") {
void Promise.try(() => appendFile(memoryFile, effect.line, { mode: 0o600 })).then(
() => dispatchMemory({ type: "sample.written", attempt: effect.attempt }),
failed,
)
return
}
void Promise.try(() =>
collectMemorySample(memoryFile, precise, {
pending: (contentsId) => isRendererProbing(memoryState, contentsId),
started: (contentsId) => dispatchMemory({ type: "probe.started", contentsId }),
settled: (contentsId) => dispatchMemory({ type: "probe.settled", contentsId }),
}),
).then((line) => dispatchMemory({ type: "sample.collected", attempt: effect.attempt, line }), failed)
}
const sync = () => dispatchMemory({ type: "synced", enabled: envInterval !== undefined || stored.value })
// Started before the Ipc is provided, so the first state windows receive already says whether diagnostics run.
sync()
ctx.onCleanup(() => dispatchMemory({ type: "disposed" }))
const provider = ctx.provide(MemoryDiagnostics, {
state: () => ({ on: isMemoryDiagnosticsOn(memoryState), env: envInterval !== undefined }),
set: (enabled) => {
stored.set(enabled)
sync()
provider.changed()
},
})
}
// Chromium's own breakdown of every process's memory: what a heap snapshot cannot show (images, layout, compositor
// layers, GPU). Fails when something else is tracing, such as DevTools recording a performance profile.
async function recordNative(directory: string) {
const file = path.join(directory, "memory-infra.json")
return within(
contentTracing
.startRecording(traceConfig)
// The first detailed dump arrives within a periodic interval of starting.
.then(() => new Promise((resolve) => setTimeout(resolve, 2_500)))
.then(() => contentTracing.stopRecording(file))
.then(async () => ({ file: path.basename(file), summary: summarize(await readFile(file, "utf8")) })),
15_000,
"The memory trace did not finish within 15 seconds.",
).catch((error: Error) => ({ error: error.message }))
}
async function describeWindow(window: BrowserWindow) {
const contents = window.webContents
// A hung or busy renderer never answers, and the snapshots wait for this step, so give up after a few seconds.
const page = await within(
contents.executeJavaScript(pageStatsScript).then(decodePageStats),
3_000,
"The window did not answer within 3 seconds.",
).catch((error: Error) => ({ error: error.message }))
return {
window: window.id,
pid: contents.getOSProcessId(),
title: window.getTitle(),
contentSize: window.getContentSize(),
scaleFactor: screen.getDisplayMatching(window.getBounds()).scaleFactor,
zoomFactor: contents.getZoomFactor(),
page,
}
}
function captureWindow(window: BrowserWindow, directory: string) {
const contents = window.webContents
const pid = contents.getOSProcessId()
const file = `window-${window.id}-${pid}.heapsnapshot`
// A hung renderer is recorded as failed, so the other windows and main are still captured.
return within(
contents.takeHeapSnapshot(path.join(directory, file)),
SNAPSHOT_TIMEOUT_MS,
"The window did not write its heap snapshot within 5 minutes.",
).then(
(): Snapshot => ({ process: "window", pid, window: window.id, file }),
(error: Error): Snapshot => ({ process: "window", pid, window: window.id, error: error.message }),
)
}
/** Rejects with `message` when `promise` takes longer than `ms`. */
function within<Value>(promise: Promise<Value>, ms: number, message: string) {
let deadline: ReturnType<typeof setTimeout> | undefined
return Promise.race([
promise,
new Promise<never>((_, reject) => {
deadline = setTimeout(() => reject(new Error(message)), ms)
}),
]).finally(() => clearTimeout(deadline))
}
function write<Value>(directory: string, file: string, value: Value) {
return writeFile(path.join(directory, file), JSON.stringify(value, null, 2), { mode: 0o600 })
}
function mibs(bytes: Readonly<Record<string, number>>) {
return Object.fromEntries(Object.entries(bytes).map(([key, value]) => [key, Math.round((value / 1048576) * 10) / 10]))
}
export default setup