@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
109 lines • 4.51 kB
JavaScript
/**
* What the operating system says about a running daemon: its resident memory, its open descriptors
* and the children it has not reaped.
*
* The numbers describe the whole PROCESS TREE rooted at the registered pid, because that is what a
* daemon costs: the pid a client can prove ownership of is the lifecycle SUPERVISOR, and the host
* that holds every session is its child. Reporting only the supervisor would answer "3 MB" for a
* daemon holding two gigabytes.
*
* The table itself is read through the kernel (`host-process-table.ts`), never by spawning `ps`:
* a daemon-control surface polled at client cadence must not run one probe child per read - on a
* runtime whose `execFile` does not reap, that is the #1507/omo-desktop#594 zombie machine, one
* permanent defunct child per status request. Where the kernel reader is unavailable (Node,
* unsupported platforms) every field is `null`, which means "this platform does not publish it
* here" rather than zero: a status that reported 0 open descriptors on a platform it cannot count
* them on would be worse than saying nothing. A failing read never fails the status - an operator
* asking what a daemon is doing must still get its identity and its sessions.
*/
import { readdir } from "node:fs/promises";
import { loadProcessTableReader } from "./host-process-table.js";
const UNOBSERVED = {
rss_mb: null,
host_rss_mb: null,
host_footprint_mb: null,
open_fds: null,
zombies: null,
};
/** Resolved once per platform; `undefined` on runtimes that cannot read the table without spawning. */
const readers = new Map();
/**
* The footprint reader is loaded on the first status read, not imported: this module sits in the lifecycle
* supervisor's static graph (host-lifecycle -> ... -> host-generations -> here), and `core/` is outside that
* graph's allowlist (test/supervisor-import-graph.test.ts).
*/
let footprintReader;
function loadFootprintReader() {
footprintReader ??= import("../../core/process-footprint.js").then((module) => module.readProcessFootprint);
return footprintReader;
}
function tableReader(platform) {
const cached = readers.get(platform);
if (cached !== undefined)
return cached;
const loading = loadProcessTableReader(platform);
readers.set(platform, loading);
return loading;
}
export async function readHostProcessMetrics(pid, platform = process.platform) {
if (platform === "win32")
return UNOBSERVED;
const read = await tableReader(platform);
if (read === undefined)
return UNOBSERVED;
const rows = read();
if (rows === undefined)
return UNOBSERVED;
const tree = descendants(rows, pid);
if (tree.length === 0)
return UNOBSERVED;
const hostRows = tree.filter((row) => row.pid === pid || row.ppid === pid);
return {
rss_mb: residentMegabytes(tree),
host_rss_mb: residentMegabytes(hostRows),
host_footprint_mb: footprintMegabytes(hostRows, await loadFootprintReader()),
open_fds: platform === "linux" ? await openDescriptors(tree) : null,
zombies: tree.filter((row) => row.state.startsWith("Z")).length,
};
}
function residentMegabytes(rows) {
return Math.round(rows.reduce((total, row) => total + row.rssKb, 0) / KILOBYTES_PER_MEGABYTE);
}
function footprintMegabytes(rows, readProcessFootprint) {
let bytes = 0;
for (const row of rows) {
const reading = readProcessFootprint(row.pid);
if (reading === undefined)
return null;
bytes += reading.bytes;
}
return Math.round(bytes / BYTES_PER_MEGABYTE);
}
const BYTES_PER_MEGABYTE = 1024 * 1024;
function descendants(rows, root) {
const tree = rows.filter((row) => row.pid === root);
if (tree.length === 0)
return tree;
for (let index = 0; index < tree.length; index++) {
const parent = tree[index].pid;
for (const row of rows) {
if (row.ppid === parent && !tree.includes(row))
tree.push(row);
}
}
return tree;
}
const KILOBYTES_PER_MEGABYTE = 1024;
async function openDescriptors(tree) {
let total = 0;
let counted = false;
for (const row of tree) {
const entries = await readdir(`/proc/${row.pid}/fd`).catch(() => undefined);
if (entries === undefined)
continue;
counted = true;
total += entries.length;
}
return counted ? total : null;
}
//# sourceMappingURL=host-process-metrics.js.map