// `umtool pool [stats|prune|verify] [--json]` -- the render pool // (report-to-video render-pool.mjs): what it holds, its hits and misses per // kind, the biggest objects; an eviction down to its cap; a check of a sample. // // umtool pool [stats] [--since 7d] [--json] // umtool pool prune [--max-gb N] [--dry-run] [--json] // umtool pool verify [--sample N | --all] [--json] // // `--pool ` (or UMTOOL_RENDER_POOL) names another pool. import { MEDIA_ROOT } from "../paths.mjs"; import { POOL_KINDS, poolFor, poolMaxBytes, poolPrune, poolStats, poolVerify } from "umtool-report-to-video/render-pool"; const USAGE = "usage: umtool pool [stats|prune|verify] [--pool DIR] [--since 7d] [--max-gb N] [--dry-run] [--sample N | --all] [--json]"; const mib = (b) => `${(b / (1 << 20)).toFixed(0)} MiB`; /** `7d`, `12h`, `30m` -> ms; null when absent. */ function sinceMs(v) { const m = /^(\d+(?:\.\d+)?)([dhm])$/.exec(String(v ?? "")); if (!m) return null; return Number(m[1]) * { d: 86_400_000, h: 3_600_000, m: 60_000 }[m[2]]; } /** @param {string[]} args @returns {Promise} the exit code */ export async function poolCommand(args, { log = console.log, env = process.env } = {}) { const flag = (n) => { const i = args.indexOf(n); return i >= 0 ? args[i + 1] : undefined; }; const json = args.includes("--json"); const sub = args[0] && !args[0].startsWith("--") ? args[0] : "stats"; const pool = flag("--pool") ?? (await poolFor(MEDIA_ROOT, env)); if (!pool) { log("the render pool is off (UMTOOL_RENDER_POOL=0)"); return 0; } if (sub === "stats") { const since = sinceMs(flag("--since")); const s = await poolStats(pool, { since: since ? Date.now() - since : 0 }); if (json) log(JSON.stringify(s, null, 2)); else { log(`render pool ${pool}: ${s.objects} object(s), ${mib(s.bytes)}; ${mib(s.onlyPool)} held by the pool alone (cap ${mib(s.maxOnlyPool)})`); for (const [k, v] of Object.entries(s.kinds)) { const all = v.hits + v.misses; log(` ${k.padEnd(6)} ${String(v.objects).padStart(7)} ${POOL_KINDS[k].noun.padEnd(20)} ${mib(v.bytes).padStart(10)} ${v.hits} hit(s), ${v.misses} miss(es)${all ? ` (${Math.round((100 * v.hits) / all)} %)` : ""}`); } if (s.biggest.length) log(" biggest:"); for (const b of s.biggest) log(` ${mib(b.bytes).padStart(9)} ${b.kind} ${b.path}${b.linked ? " (a take links it)" : ""}`); } return 0; } if (sub === "prune") { const gb = flag("--max-gb"); const maxBytes = gb !== undefined ? Number(gb) * 1024 ** 3 : poolMaxBytes(env); if (!Number.isFinite(maxBytes) || maxBytes < 0) { log(USAGE); return 2; } const r = await poolPrune(pool, { maxBytes, dryRun: args.includes("--dry-run") }); if (json) log(JSON.stringify(r, null, 2)); else log(`render pool: ${args.includes("--dry-run") ? "would evict" : "evicted"} ${r.evicted} object(s), ${mib(r.bytes)}; held by the pool alone ${mib(r.before)} -> ${mib(r.after)} (cap ${mib(r.maxBytes)}; an object a take links is never evicted)`); return 0; } if (sub === "verify") { const r = await poolVerify(pool, { sample: Number(flag("--sample") ?? 50), all: args.includes("--all") }); if (json) log(JSON.stringify(r, null, 2)); else { log(`render pool: checked ${r.checked} of ${r.of} object(s): ${r.problems.length ? `${r.problems.length} problem(s)` : "all as put in"}`); for (const p of r.problems) log(` ${p}`); } return r.problems.length ? 1 : 0; } log(USAGE); return 2; }