Skip to content
12 changes: 8 additions & 4 deletions packages/bun-types/overrides.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ declare global {
* of polling.
*
* On macOS `level` distinguishes `"warning"` from `"critical"` based on
* the kernel's memorystatus thresholds. On Linux (PSI) and Windows the
* event is always emitted with `"critical"`. On Linux, the underlying
* PSI trigger requires `CAP_SYS_RESOURCE` on kernels before 6.6; when
* unavailable the event is never emitted.
* the kernel's memorystatus thresholds. On Linux, reclaim at one of the
* process's cgroup memory limits (the `low`, `high` and `max` counters
* of cgroup v2 `memory.events`) is emitted as `"warning"`, an OOM event
* in that cgroup and a system-wide PSI memory stall as `"critical"`.
* The PSI trigger requires `CAP_SYS_RESOURCE` on kernels before 6.4, and
* the cgroup counters only exist on cgroup v2. On Windows the event is
* always emitted with `"critical"`. When no source is available the
* event is never emitted.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
*
* This listener does not keep the event loop alive.
*/
Expand Down
26 changes: 18 additions & 8 deletions src/js/internal-for-testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,19 +673,29 @@ export const isMemoryPressureWatcherInstalled: () => boolean = $newCppFunction(
0,
);

// True when the installed watcher registered a real OS source (a PSI trigger
// on Linux). The watcher installs silently without one when the kernel
// refuses the trigger, so isMemoryPressureWatcherInstalled() cannot tell.
export const memoryPressureWatcherHasOsBackend: () => boolean = $newRustFunction(
"memory_pressure.rs",
"jsWatcherHasOsBackend",
0,
);
// The OS sources the installed watcher holds: "psi" and/or "cgroup" on Linux,
// "memorystatus" on macOS, "notification" on Windows. The watcher installs
// silently with no source when the OS refuses every one of them, so
// isMemoryPressureWatcherInstalled() cannot tell.
Comment thread
robobun marked this conversation as resolved.
Resolved
export const memoryPressureArmedSources: () => ("psi" | "cgroup" | "memorystatus" | "notification")[] =
$newRustFunction("memory_pressure.rs", "jsArmedSources", 0);

// The exact bytes Bun writes to /proc/pressure/memory to arm its trigger.
// null where there is no PSI backend (everything except Linux).
export const memoryPressurePsiTrigger: () => Buffer | null = $newRustFunction("memory_pressure.rs", "jsPsiTrigger", 0);

// Linux. Feed a memory.events file body to the installed "cgroup" source as if
// the kernel had signalled it. atMs is the holdoff clock reading for this
// notification, on a clock that starts at the first call, so a test drives
// the holdoff without waiting. The listener sees exactly what the real
// notification would produce. Returns false when no "cgroup" source is
// installed.
Comment thread
robobun marked this conversation as resolved.
Resolved
export const memoryPressureInjectCgroupEvents: (text: string, atMs?: number) => boolean = $newRustFunction(
"memory_pressure.rs",
"jsInjectCgroupEvents",
2,
);

export const getEventLoopStats: () => {
activeTasks: number;
tasks: number;
Expand Down
Loading
Loading