Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(progress): treat CPU charged to the futex-blocked thread as a vCPU stall, not only visible steal

garrytandb98e339f739
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eaa9ab5df0..992755d58f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,7 @@ identifiers and attribution are available in the pre-removal Git revision
- `src/core/minions/worker.ts` (+3 lines, ceiling 1777): `gracefulShutdown` records its reason; `launchJob` aborts a job it registers after the shutdown fired with that reason, so the job takes the same abort path as one that was already in flight (attempt burned, `delayed`; `UnrecoverableError` dead).
- `test/worker-shutdown-error-matrix.test.ts`: a forced case holds the committed claim's result until the watchdog has fired (both engines; fails on the previous worker, passes now).
- Docs: `docs/architecture/key-files/core-minions-2.md` worker entry.
+- `test/progress.test.ts` cpu_ms flake, second mechanism: run 38588222510 billed 105 ms to the thread inside its 200 ms wait with the VM-wide steal counter at 0. KVM counts only runnable waiting as steal; a vCPU asleep in the host (a guest page the host still had to back, under host memory throttling) advances the guest clock while the guest thread is current and is billed to that thread with no steal recorded. Reproduced on a nested-KVM guest (virtio-balloon cycle to leave guest pages host-unbacked, host `memory.high` on the VM): master's test failed 4/10 runs with the CI signature, `steal 0 ms` included. The window check now also treats CPU charged to the thread while it was blocked in the futex (≥ 10 ms; 0.2–2.2 ms across 500 clean windows on three hosts) as a stall and re-measures; the bound is unchanged, and a run with no clean window in 5 attempts fails with each window's charge, wait overrun and steal. Doc sentence on `cpu_ms` widened accordingly.
- `test/e2e/supervisor-stop-drain-5062.test.ts` flake: the test read each barrier pid file as soon as it existed, but `echo $$ > f` creates the file before the pid lands in it, so an empty read gave pid 0 and `process.kill(0, 0)` (this test's own process group) reported it alive after the drain. The reader now waits for a parsed pid, `alive` refuses a non-positive pid, and the barrier job forces the window (create, 300 ms, write) on every run. Root cause reported from the Cat 40 Hard wave (#6668).
## [0.60.223.0] - 2026-10-14
diff --git a/docs/progress-events.md b/docs/progress-events.md
index 8c694ef35e..82fb83a86b 100644
--- a/docs/progress-events.md
+++ b/docs/progress-events.md
@@ -99,9 +99,11 @@ Fields:
(garbage collection, JIT) and in other processes (a Postgres server, a
provider). The scale tier's import-rate gate times PGLite imports with it.
Under a hypervisor whose guest kernel does not subtract steal time (Ubuntu's
- generic kernels), it also includes vCPU time the host took away while this
- thread was current, so on an oversubscribed VM it can exceed the thread's
- real work.
+ generic kernels), it also includes any vCPU stall that landed while this
+ thread was current, whether the host reports it as steal (the vCPU waiting
+ for a CPU) or not (the vCPU asleep in the host, as on a page the host had to
+ back or reclaim first), so on an oversubscribed VM it can exceed the
+ thread's real work.
- `total` — total items, if known. Omitted when the scan doesn't have a
total up front (e.g. a streaming iterator).
- `pct` — `done/total * 100`, one decimal. Omitted when `total` is unknown.
diff --git a/test/progress.test.ts b/test/progress.test.ts
index f9440c3a0a..2c747787c3 100644
--- a/test/progress.test.ts
+++ b/test/progress.test.ts
@@ -151,12 +151,17 @@ describe('progress reporter', () => {
});
test('json tick cpu_ms counts CPU work since the phase started, not time spent waiting', () => {
- // Under a hypervisor whose guest kernel lacks CONFIG_PARAVIRT_TIME_ACCOUNTING (Ubuntu's generic kernels, the CI
- // runners included), vCPU time the host steals is billed to whichever thread was current, so a steal burst that
- // starts right after the wait wakes lands in this thread's CPU clock (run 38467234783: 268 ms billed inside a
- // 200 ms wait on a runner with 28 % steal). Such a window is an invalid instrument reading, not CPU work: the
- // measurement is repeated until the VM-wide steal counter did not move across it, and the bound below is
- // asserted only on that window. Without /proc/stat (macOS) the first window is asserted as before.
+ // The thread's CPU clock is the scheduler's runtime for it: whatever the guest clock advanced while the thread
+ // was current. Under a hypervisor whose guest kernel lacks CONFIG_PARAVIRT_TIME_ACCOUNTING (Ubuntu's generic
+ // kernels, the CI runners included) that includes every vCPU stall that lands while this thread is current, and
+ // the stall right after the wait wakes is billed inside the wait: run 38467234783 billed 268 ms inside a 200 ms
+ // wait on a runner with 28 % steal, run 38588222510 billed 105 ms with the steal counter at 0 (a stall KVM does
+ // not count as steal: the vCPU thread asleep in the host, as on a page the host had to back or reclaim first).
+ // A thread blocked in a futex executes nothing, so CPU charged to it across the wait is a stall, not work, and
+ // that window is an invalid instrument reading: the measurement is repeated until a window shows no steal and
+ // no CPU charged while blocked, and the bound below is asserted only on that window. Without /proc/stat (macOS)
+ // the steal column is unknown and only the charged-while-blocked check applies.
+ const STALL_MS = 10; // a futex-blocked thread measured 0.2–2.2 ms across 500 clean windows on three hosts
const stealJiffies = (): number | null => {
try {
const cpuLine = readFileSync('/proc/stat', 'utf8').split('\n')[0] ?? '';
@@ -177,9 +182,11 @@ describe('progress reporter', () => {
const stealBefore = stealJiffies();
const usageBefore = process.resourceUsage();
const cpuBeforeWait = cpuMs();
+ const wallBeforeWait = performance.now();
// Block the thread for 200 ms without yielding: an await would let other queued main-thread work (an earlier
// test's timer, a GC pause) run and be billed to the phase, and the test means "the thread sat idle".
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 200);
+ const wallInWait = performance.now() - wallBeforeWait;
const cpuInWait = cpuMs() - cpuBeforeWait;
const usageAfter = process.resourceUsage();
p.tick(1);
@@ -187,27 +194,28 @@ describe('progress reporter', () => {
const stealAfter = stealJiffies();
const [, worked, waited] = parseJsonl(read()) as Array<{ cpu_ms: number; elapsed_ms: number }>;
const stealMs = stealBefore === null || stealAfter === null ? null : (stealAfter - stealBefore) * 10;
- return { worked, waited, cpuInWait, usageBefore, usageAfter, stealMs };
+ return { worked, waited, cpuInWait, wallInWait, usageBefore, usageAfter, stealMs, stalled: (stealMs ?? 0) !== 0 || cpuInWait >= STALL_MS };
};
const attempts: ReturnType<typeof measure>[] = [];
- for (let attempt = 0; attempt < 5; attempt++) {
- attempts.push(measure());
- if (attempts[attempts.length - 1]!.stealMs === null || attempts[attempts.length - 1]!.stealMs === 0) break;
- }
- const { worked, waited, cpuInWait, usageBefore, usageAfter, stealMs } = attempts[attempts.length - 1]!;
+ for (let attempt = 0; attempt < 5 && (attempts.length === 0 || attempts[attempts.length - 1]!.stalled); attempt++) attempts.push(measure());
+ const { worked, waited, cpuInWait, wallInWait, usageBefore, usageAfter, stealMs } = attempts[attempts.length - 1]!;
+ const describeAttempt = (a: ReturnType<typeof measure>) =>
+ `${a.cpuInWait.toFixed(1)} ms charged while blocked (wait overran by ${(a.wallInWait - 200).toFixed(1)} ms), steal ${a.stealMs === null ? 'unknown' : `${a.stealMs} ms`}`;
expect(worked.cpu_ms).toBeGreaterThanOrEqual(60);
expect(waited.elapsed_ms - worked.elapsed_ms).toBeGreaterThanOrEqual(190);
const waitedCpuMs = waited.cpu_ms - worked.cpu_ms;
expect(
waitedCpuMs,
`the second tick billed ${waitedCpuMs.toFixed(1)} ms of thread CPU (first tick, after the 60 ms spin: ${worked.cpu_ms.toFixed(1)} ms): ` +
- `${cpuInWait.toFixed(1)} ms inside the 200 ms Atomics.wait, ${(waitedCpuMs - cpuInWait).toFixed(1)} ms in the tick calls around it; ` +
+ `${cpuInWait.toFixed(1)} ms inside the 200 ms Atomics.wait (which returned after ${wallInWait.toFixed(1)} ms), ${(waitedCpuMs - cpuInWait).toFixed(1)} ms in the tick calls around it; ` +
`process during the wait: ${usageAfter.minorPageFault - usageBefore.minorPageFault} minor and ` +
`${usageAfter.majorPageFault - usageBefore.majorPageFault} major page faults, ` +
`${usageAfter.involuntaryContextSwitches - usageBefore.involuntaryContextSwitches} involuntary context switches, ` +
`${((usageAfter.userCPUTime + usageAfter.systemCPUTime - usageBefore.userCPUTime - usageBefore.systemCPUTime) / 1000).toFixed(1)} ms CPU on all threads; ` +
`hypervisor steal across the window: ${stealMs === null ? 'unknown (no /proc/stat)' : `${stealMs} ms`}` +
- `${attempts.length > 1 ? ` (attempt ${attempts.length} of 5; steal per attempt: ${attempts.map((a) => `${a.stealMs} ms`).join(', ')})` : ''}`,
+ (attempts[attempts.length - 1]!.stalled
+ ? `; no window without a vCPU stall in ${attempts.length} attempts (${attempts.map(describeAttempt).join('; ')})`
+ : attempts.length > 1 ? ` (attempt ${attempts.length} of 5; earlier windows stalled: ${attempts.slice(0, -1).map(describeAttempt).join('; ')})` : ''),
).toBeLessThan(100);
});