Credit coarse timer overshoot while bounding stall recovery

This commit is contained in:
Ludovic Marie
2026-10-03 21:15:25 +02:00
parent 4052f2040d
commit 7b3840ea8f
3 changed files with 25 additions and 21 deletions
+2 -1
View File
@@ -16,7 +16,8 @@ the game's primary allocation cost.
The original Horizon hook accumulates fractional waits until at least 250 microseconds
are due, then sleeps with nanosecond precision. Measured timer overshoot earns credit
against later waits. Primitive state is allocated once per worker rather than boxing
against later waits, capped at 50 milliseconds to bound recovery after a long stall.
Primitive state is allocated once per worker rather than boxing
each timestamp. Changes to the duty-cycle setting discard stale debt; an idle worker
also discards it. Interruption during sleep releases debt with the upstream hook's
interruption behavior. The runnable, queues, result, exception and execution order
@@ -3,6 +3,7 @@ package dev.horizonlab.budget;
/** Per-worker primitive state; amortizes short waits without discarding fractional milliseconds. */
public final class WorkerPacer {
public static final long QUANTUM_NS = 250_000;
public static final long MAX_CREDIT_NS = 50_000_000;
private long started;
private long completed;
private double debtNs;
@@ -27,7 +28,7 @@ public final class WorkerPacer {
}
public void pauseCompleted(long now) {
debtNs = Math.max(-QUANTUM_NS, debtNs - Math.max(0, now - completed));
debtNs = Math.max(-MAX_CREDIT_NS, debtNs - Math.max(0, now - completed));
completed = now;
}
@@ -6,27 +6,29 @@ public final class WorkerPacerTest {
public static void main(String[] args) {
for (double ratio : new double[]{0.01, 0.05, 0.1, 0.5, 0.9, 1}) {
for (long work : new long[]{100, 10_000, 1_000_000, 100_000_000}) {
WorkerPacer pacer = new WorkerPacer();
long now = 1;
long active = 0;
long slept = 0;
for (int i = 0; i < 1000; i++) {
pacer.begin(now);
now += work;
active += work;
long delay = pacer.finish(now, ratio);
check(delay == 0 || delay >= WorkerPacer.QUANTUM_NS, "Small waits are batched");
if (delay != 0) {
// Simulate timer overshoot; future tasks must receive credit for it.
delay += 80_000;
now += delay;
slept += delay;
pacer.pauseCompleted(now);
for (long overshoot : new long[]{80_000, 1_000_000, 15_000_000}) {
WorkerPacer pacer = new WorkerPacer();
long now = 1;
long active = 0;
long slept = 0;
for (int i = 0; i < 1000; i++) {
pacer.begin(now);
now += work;
active += work;
long delay = pacer.finish(now, ratio);
check(delay == 0 || delay >= WorkerPacer.QUANTUM_NS, "Small waits are batched");
if (delay != 0) {
// Simulate timer overshoot; future tasks must receive credit for it.
delay += overshoot;
now += delay;
slept += delay;
pacer.pauseCompleted(now);
}
}
double expectedSleep = active * (1.0 / ratio - 1);
check(Math.abs(slept - expectedSleep) <= WorkerPacer.QUANTUM_NS + overshoot + 1,
"Duty cycle retained across job sizes and timer overshoot: " + ratio + "/" + work);
}
double expectedSleep = active * (1.0 / ratio - 1);
check(Math.abs(slept - expectedSleep) <= WorkerPacer.QUANTUM_NS + 80_001,
"Duty cycle retained across job sizes and timer overshoot: " + ratio + "/" + work);
}
}
WorkerPacer pacer = new WorkerPacer();