Repository navigation
Expand file tree
/
Copy pathmemory-log.ts
More file actions
65 lines (56 loc) · 2.52 KB
/
Copy pathmemory-log.ts
File metadata and controls
65 lines (56 loc) · 2.52 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
import { mkdir } from "node:fs/promises"
import path from "node:path"
import { app, webContents } from "electron"
import { Schema } from "effect"
import { memoryRecord, RendererMemory, type RendererRecord } from "./memory-record"
const RENDERER_PROBE_TIMEOUT_MS = 2_000
// Runs only while diagnostics are on; no preload hooks, observers, or store subscriptions.
const probe = `(() => {
const memory = performance.memory;
return {
jsHeap: memory ? { used: memory.usedJSHeapSize, total: memory.totalJSHeapSize, limit: memory.jsHeapSizeLimit } : null,
domElements: document.getElementsByTagName('*').length
};
})()`
/** A new JSONL file in the desktop logs, which exported debug logs include. */
export function memoryLogFile() {
return path.join(app.getPath("userData"), "logs", `memory-${new Date().toISOString().replace(/[:.]/g, "")}.jsonl`)
}
/**
* Collects one sample line for `file`, creating its folder, so appending it is the only step left. A renderer whose
* earlier probe has not settled is skipped, and a probe that takes too long is reported without waiting for it.
*/
export async function collectMemorySample(
file: string,
precise: boolean,
probes: {
readonly pending: (contentsId: number) => boolean
readonly started: (contentsId: number) => void
readonly settled: (contentsId: number) => void
},
) {
const metrics = app.getAppMetrics()
const renderers = await Promise.all(
webContents.getAllWebContents().map(async (contents): Promise<RendererRecord> => {
const identity = { id: contents.id, pid: contents.getOSProcessId(), type: contents.getType() }
if (probes.pending(contents.id)) return { ...identity, error: "Previous renderer sample is still pending" }
probes.started(contents.id)
let deadline: ReturnType<typeof setTimeout> | undefined
const reading = contents.executeJavaScript(probe).then(Schema.decodeUnknownSync(RendererMemory))
void reading.finally(() => probes.settled(contents.id)).catch(() => undefined)
return Promise.race([
reading,
new Promise<never>((_, reject) => {
deadline = setTimeout(() => reject(new Error("Renderer sample timed out")), RENDERER_PROBE_TIMEOUT_MS)
}),
])
.then(
(sample) => ({ ...identity, sample }),
(error: Error) => ({ ...identity, error: error.message }),
)
.finally(() => clearTimeout(deadline))
}),
)
await mkdir(path.dirname(file), { recursive: true })
return `${JSON.stringify(memoryRecord(metrics, renderers, precise))}\n`
}