|
| 1 | +import * as Effect from "effect/Effect"; |
| 2 | +import * as Layer from "effect/Layer"; |
| 3 | + |
| 4 | +import * as ServerConfig from "../config.ts"; |
| 5 | +import { writeHeapSnapshot } from "./HeapSnapshot.ts"; |
| 6 | + |
| 7 | +const GiB = 1024 ** 3; |
| 8 | +const MiB = 1024 ** 2; |
| 9 | +const FIRST_REPORT_BYTES = 2 * GiB; |
| 10 | +const REPORT_STEP_BYTES = 1 * GiB; |
| 11 | +const SNAPSHOT_BYTES = 4 * GiB; |
| 12 | +const SAMPLE_INTERVAL = "1 minute"; |
| 13 | + |
| 14 | +export interface MemoryWatchState { |
| 15 | + readonly nextReportBytes: number; |
| 16 | + readonly snapshotTaken: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +export const initialState: MemoryWatchState = { |
| 20 | + nextReportBytes: FIRST_REPORT_BYTES, |
| 21 | + snapshotTaken: false, |
| 22 | +}; |
| 23 | + |
| 24 | +/** |
| 25 | + * Decides one sample: report each new 1 GiB high-water mark past 2 GiB, and |
| 26 | + * write one heap snapshot per process past 4 GiB. |
| 27 | + */ |
| 28 | +export const step = (state: MemoryWatchState, rssBytes: number) => { |
| 29 | + const report = rssBytes >= state.nextReportBytes; |
| 30 | + const snapshot = !state.snapshotTaken && rssBytes >= SNAPSHOT_BYTES; |
| 31 | + return { |
| 32 | + report, |
| 33 | + snapshot, |
| 34 | + next: { |
| 35 | + nextReportBytes: report |
| 36 | + ? rssBytes - (rssBytes % REPORT_STEP_BYTES) + REPORT_STEP_BYTES |
| 37 | + : state.nextReportBytes, |
| 38 | + snapshotTaken: state.snapshotTaken || snapshot, |
| 39 | + }, |
| 40 | + }; |
| 41 | +}; |
| 42 | + |
| 43 | +/** |
| 44 | + * Logs the server's memory breakdown at each new high-water mark, so a leak |
| 45 | + * leaves a durable record of which pool grew and when. `external` and |
| 46 | + * `arrayBuffers` grow outside the V8 heap limit; a heap snapshot names what |
| 47 | + * retains them. See RSI-Software/t3code-hyprws#1717. |
| 48 | + */ |
| 49 | +export const layer = Layer.effectDiscard( |
| 50 | + Effect.gen(function* () { |
| 51 | + const { logsDir } = yield* ServerConfig.ServerConfig; |
| 52 | + let state = initialState; |
| 53 | + const tick = Effect.gen(function* () { |
| 54 | + const usage = process.memoryUsage(); |
| 55 | + const decision = step(state, usage.rss); |
| 56 | + state = decision.next; |
| 57 | + if (decision.report) { |
| 58 | + yield* Effect.logWarning(`server memory reached ${Math.round(usage.rss / MiB)} MiB`, { |
| 59 | + rssMb: Math.round(usage.rss / MiB), |
| 60 | + heapUsedMb: Math.round(usage.heapUsed / MiB), |
| 61 | + heapTotalMb: Math.round(usage.heapTotal / MiB), |
| 62 | + externalMb: Math.round(usage.external / MiB), |
| 63 | + arrayBuffersMb: Math.round(usage.arrayBuffers / MiB), |
| 64 | + }); |
| 65 | + } |
| 66 | + if (decision.snapshot) { |
| 67 | + yield* writeHeapSnapshot(logsDir); |
| 68 | + } |
| 69 | + }); |
| 70 | + yield* Effect.sleep(SAMPLE_INTERVAL).pipe( |
| 71 | + Effect.andThen(tick), |
| 72 | + Effect.forever, |
| 73 | + Effect.forkScoped, |
| 74 | + ); |
| 75 | + }), |
| 76 | +); |
0 commit comments