diff --git a/AGENTS.md b/AGENTS.md index 0349c2e..5d095ac 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -134,6 +134,16 @@ The mechanism was selected by experiment, and the negative result is the load-be - **The scheduler must hold a strong reference to every preempted fiber and drain them before shutdown.** Dropping one is fatal, and uninstalling the hook first does not help. - **Each forked worker re-arms its own timer** — `setitimer` intervals are cleared in the child. +- **The clock is stopped for the idle poll, and the re-arm is a `finally`.** A free-running 10 ms + timer wakes an idle preemptive process ~100 times a second to preempt nobody, so + `Scheduler::pollIdle()` brackets the one blocking call with `Preemptor::pauseSlicing()` and + `resumeSlicing()`. Two conditions make that safe and neither is negotiable: **idle means the + poller is about to block**, never "the run queue looks empty" — a coroutine about to run must + still be sliceable — and the re-arm lives in a `finally` spanning the whole `poll()`, so no way + out of it (a readiness, a timeout, the EINTR retry the poller does internally, a throw) can leave + the process silently cooperative. Pausing is not disarming: the hook stays installed, SIGALRM + stays ours, and `isArmed()` goes on saying yes. A missed re-arm reports nothing and fails nowhere, + which is why it is structural rather than careful. - **10 ms is a target, not a guarantee.** A single internal opcode is not interruptible: `sort()` over 4M ints delayed preemption by 1.6–2.0 s. Never document or assume a bounded slice; document the caveat with it. diff --git a/README.md b/README.md index 056f960..a4b6a8d 100644 --- a/README.md +++ b/README.md @@ -516,6 +516,10 @@ z-engine requires it, and z-engine is a hard dependency of this package. - **Preemption is opt-in** (`new Runtime(preemptive: true)`) and, once armed, makes coroutine lifetimes the scheduler's business: a preempted coroutine is suspended inside an engine callback, so it is drained rather than discarded when a run ends. +- **An idle preemptive runtime is as quiet as a cooperative one.** The slice clock is stopped for + exactly the time the process spends blocked in the poller — there is no coroutine to take the CPU + away from — and started again on every way out of it, so a server waiting for work does not pay a + hundred wakeups a second. Every forked worker gets the same treatment for its own inbox wait. - **`workers: 0` maps no arena at all.** The shared surface is then refused with a message naming the remedy rather than half-composed — a cooperative runtime stays exactly as cheap as it was. diff --git a/src/Preemption/Preemptor.php b/src/Preemption/Preemptor.php index 84919c5..c090a38 100644 --- a/src/Preemption/Preemptor.php +++ b/src/Preemption/Preemptor.php @@ -43,6 +43,15 @@ * worker. Leaving the outermost section re-raises the interrupt so the deferred preemption is * taken immediately rather than up to a slice later. * + * # The clock stops while the process is idle + * + * A slice measures the CPU a coroutine is holding, so there is nothing to measure while no + * coroutine is running. {@see self::pauseSlicing()} and {@see self::resumeSlicing()} let the + * scheduler stop the clock for exactly the time it spends blocked in the poller, which is what + * keeps an idle preemptive runtime from waking a hundred times a second to preempt nobody. Pausing + * is not disarming: the hook stays installed, the signal stays ours, and this class keeps reporting + * itself armed. + * * # What preemption does not promise * * A time slice is a target, not a bound. The interrupt check sits between opcodes, so a single @@ -82,6 +91,15 @@ final class Preemptor private int $criticalDepth = 0; + /** + * The clock is stopped because the process is blocked in the poller with nothing running. + * + * Distinct from {@see self::$armed}, and the distinction is the whole point: a paused preemptor + * is still armed — the hook is installed, the signal is ours, `shouldPreempt()` still answers — + * it simply has no clock ticking while there is nothing on the CPU to take away. + */ + private bool $slicingPaused = false; + private int $preemptions = 0; private bool $shutdownDrainRegistered = false; @@ -162,7 +180,9 @@ public function arm(): void * Stop preempting, and leave nothing suspended inside the engine callback behind. * * The drain happens *first*, while the clock is still running: a coroutine resumed here may be - * in a loop that never yields, and only a live timer guarantees that resuming it returns. + * in a loop that never yields, and only a live timer guarantees that resuming it returns. That + * is also why the idle pause is lifted before the drain rather than left to the ordinary path — + * a teardown reached from inside a blocking poll would otherwise drain against a stopped clock. */ public function disarm(): void { @@ -170,11 +190,13 @@ public function disarm(): void return; } + $this->resumeSlicing(); $this->scheduler->drainPreempted(); $this->clock->disarm(); - $this->armed = false; - $this->requested = false; + $this->armed = false; + $this->requested = false; + $this->slicingPaused = false; if (extension_loaded('pcntl')) { $previous = $this->previousSignalHandler; @@ -200,7 +222,8 @@ public function rearmAfterFork(): void return; } - $this->requested = false; + $this->requested = false; + $this->slicingPaused = false; pcntl_async_signals(true); pcntl_signal(SIGALRM, $this->onTick(...)); @@ -208,6 +231,68 @@ public function rearmAfterFork(): void $this->clock->rearmAfterFork(); } + /** + * Stop the clock on the way into a blocking poll, without giving up preemption. + * + * A free-running interval timer raises SIGALRM about a hundred times a second whether or not + * there is anything to preempt, and every one of those signals cuts the poller's + * `stream_select()` short. Correctness survives it — the poller retries with what is *left* of + * the timeout — but an idle preemptive server pays a hundred wakeups a second to suspend + * nobody. + * + * Nothing is given up by stopping the clock there, because "there" means no coroutine is + * running: the run queue is empty, every due timer has fired, and the process is about to block + * in the kernel. What is emphatically **not** the condition for this is "the run queue looks + * empty" — a coroutine that is about to run must still be sliceable, which is why the pause + * brackets the blocking call itself and is undone before anything is dequeued. + * + * Only the clock stops. The interrupt hook stays installed, SIGALRM stays ours, a preemption + * that was requested and not yet taken stays pending, and {@see self::isArmed()} goes on saying + * yes — the runtime is still preemptive, it is merely not counting. + */ + public function pauseSlicing(): void + { + if (!$this->armed || $this->slicingPaused) { + return; + } + + $this->slicingPaused = true; + + $this->clock->disarm(); + } + + /** + * Start the clock again on the way out of the poll — on *every* way out. + * + * The caller owes this a `finally`. A readiness, a timeout, a signal that cut the wait short + * and sent it round the EINTR retry, and a poller that throws must all end with the clock + * running again, because the next thing the scheduler does is resume a coroutine. A missed + * re-arm fails nowhere and reports nothing: it leaves the process cooperative for the rest of + * its life behind a preemptor that still calls itself armed. + * + * The interval restarts from now rather than resuming the grid the pause interrupted, so the + * coroutine that runs next gets a whole slice instead of whatever was left of one. + */ + public function resumeSlicing(): void + { + if (!$this->slicingPaused) { + return; + } + + $this->slicingPaused = false; + + // Checked rather than assumed: the shutdown drain disarms, and it can run from anywhere. + if ($this->armed) { + $this->clock->arm(); + } + } + + /** Whether the clock is stopped for an idle poll; still armed, just not ticking. */ + public function isSlicingPaused(): bool + { + return $this->slicingPaused; + } + /** * Open a section that must run to its end without being preempted. * @@ -324,6 +409,9 @@ private function registerShutdownDrain(): void $this->shutdownDrainRegistered = true; register_shutdown_function(function (): void { + // Shutdown can be reached from anywhere, an idle poll included, and the drain below + // needs a running clock to be sure a resumed coroutine ever comes back. + $this->resumeSlicing(); $this->scheduler->drainPreempted(); $this->clock->disarm(); $this->armed = false; diff --git a/src/Scheduler.php b/src/Scheduler.php index c6108df..434f847 100644 --- a/src/Scheduler.php +++ b/src/Scheduler.php @@ -31,6 +31,9 @@ * A coroutine parked as *externally wakeable* is excluded from that conclusion: its wakeup was * never the scheduler's to produce. That exclusion is what keeps an idle server from reporting * itself deadlocked every time it has nothing to do. + * + * The blocking poll of step 2 is also the only moment a preemptive process has nothing on the CPU, + * so it is where the slice clock is stopped and started again — see {@see self::pollIdle()}. */ final class Scheduler implements SchedulerInterface { @@ -335,13 +338,49 @@ private function drive(?CoroutineInterface $until): void return; } - $this->poller->poll($timeout); + $this->pollIdle($timeout); } } finally { $this->current = null; } } + /** + * Block in the poller, with the slice clock stopped for exactly as long as that lasts. + * + * This is the one place in the process where nothing is running: the run queue is empty, every + * due timer has fired, and the next thing that happens is a syscall. A preemption timer left + * free-running over it raises SIGALRM ~100 times a second, cuts each `stream_select()` short + * and has nothing to suspend when it does — the correctness is fine (the poller retries with + * the remaining timeout) and the wakeups are pure waste. + * + * Two things make this safe, and both are structural rather than careful: + * + * - **Idle is "the poller is about to block", not "the queue looks empty".** The pause opens + * after the queue has drained and the timers have fired, and closes before anything is + * dequeued, so a coroutine that is about to run is never the one that lost its slice. + * - **The re-arm is a `finally` over the whole call.** Every way out of `poll()` — a readiness, + * a timeout, the EINTR retry that a signal sends it round, a `RuntimeException` from a broken + * descriptor — leaves the clock running again. A missed re-arm would not throw anywhere: it + * would quietly leave the process cooperative behind a preemptor still reporting itself + * armed. + * + * The preemptor is read once into a local, so the pause and the re-arm are always the same + * object's, whatever a callback fired from inside the poll does with the attachment. + */ + private function pollIdle(?float $timeout): void + { + $preemptor = $this->preemptor; + + $preemptor?->pauseSlicing(); + + try { + $this->poller->poll($timeout); + } finally { + $preemptor?->resumeSlicing(); + } + } + private function runNext(): void { $coroutine = $this->runQueue->dequeue(); diff --git a/src/StreamPoller.php b/src/StreamPoller.php index 307394c..9f86a94 100644 --- a/src/StreamPoller.php +++ b/src/StreamPoller.php @@ -46,6 +46,18 @@ final class StreamPoller implements PollerInterface /** @var array */ private array $watches = []; + /** + * How many times a blocking wait in this poller has ended, for any reason. + * + * A diagnostic, and the only honest way to ask "is this process actually idling?". A signal + * that cuts a `stream_select()` short and is retried counts here as its own wakeup, because + * from the kernel's point of view that is exactly what it was — the process woke up, did + * nothing, and went back to sleep. Counting only the returns of {@see self::poll()} would + * report an idle preemptive runtime waking once a second when it is really waking a hundred + * times. + */ + private int $wakeups = 0; + public function __construct(private readonly SchedulerInterface $scheduler) {} public function awaitReadable($stream): void @@ -152,6 +164,19 @@ public function hasWatches(): bool return $this->readWaiters !== [] || $this->writeWaiters !== [] || $this->watches !== []; } + /** + * How many times this poller has come back from a blocking wait. + * + * One per `stream_select()` return — a readiness, a timeout, or a signal that cut it short and + * sent it round the retry — plus one per descriptor-less idle sleep. An idle process should + * report roughly one wakeup per thing it is actually waiting for, and a count that scales with + * elapsed time instead is the signature of something waking it for nothing. + */ + public function wakeups(): int + { + return $this->wakeups; + } + /** * Drop every registration without waking anybody. * @@ -283,6 +308,8 @@ private function select(array $read, array $write, ?float $timeout): array error_clear_last(); $count = @stream_select($readSet, $writeSet, $exceptSet, $seconds, $microseconds); + ++$this->wakeups; + if ($count !== false) { return ['read' => self::readyIds($readSet), 'write' => self::readyIds($writeSet)]; } @@ -352,6 +379,8 @@ private function idle(float $seconds): void if ($microseconds > 0) { usleep($microseconds); + + ++$this->wakeups; } } diff --git a/tests/Functional/testACallFreeLoopIsStillPreemptedAfterTheRuntimeHasBeenIdle.phpt b/tests/Functional/testACallFreeLoopIsStillPreemptedAfterTheRuntimeHasBeenIdle.phpt new file mode 100644 index 0000000..99c911c --- /dev/null +++ b/tests/Functional/testACallFreeLoopIsStillPreemptedAfterTheRuntimeHasBeenIdle.phpt @@ -0,0 +1,64 @@ +--TEST-- +A call-free loop that starts after an idle stretch is preempted exactly as one that never idled +--INI-- +ffi.enable=1 +opcache.jit=off +error_reporting=E_ALL & ~E_DEPRECATED +--FILE-- +ticks = 0; +$state->ticksSeenByTheLoop = -1; + +$runtime = new Runtime(preemptive: true); + +$runtime->run(static function () use ($state): void { + // Nothing else is runnable and nothing is registered with the poller, so the scheduler really + // does go idle here rather than taking a turn round the run queue. + Coroutine::sleep(IDLE_SECONDS); + + Coroutine::spawn(static function () use ($state): void { + $sum = 0; + + for ($index = 0; $index < ITERATIONS; $index++) { + $sum += $index % 7; + } + + $state->ticksSeenByTheLoop = $state->ticks; + }); + + Coroutine::spawn(static function () use ($state): void { + for ($round = 0; $round < 10_000; $round++) { + $state->ticks++; + Coroutine::yield(); + } + }); + + for ($round = 0; $round < 3; $round++) { + Coroutine::yield(); + } +}); + +echo 'the ticker ran while the loop was still running: ', + $state->ticksSeenByTheLoop >= 1 ? 'yes' : 'no', PHP_EOL; +echo 'the loop was preempted at least once: ', + ($runtime->preemptor()?->preemptions() ?? 0) >= 1 ? 'yes' : 'no', PHP_EOL; +?> +--EXPECT-- +the ticker ran while the loop was still running: yes +the loop was preempted at least once: yes diff --git a/tests/Functional/testAnIdlePreemptiveRunCostsNoMoreCpuThanACooperativeOne.phpt b/tests/Functional/testAnIdlePreemptiveRunCostsNoMoreCpuThanACooperativeOne.phpt new file mode 100644 index 0000000..2f7cb6c --- /dev/null +++ b/tests/Functional/testAnIdlePreemptiveRunCostsNoMoreCpuThanACooperativeOne.phpt @@ -0,0 +1,84 @@ +--TEST-- +Idling with preemption armed costs about as much CPU as idling cooperatively +--INI-- +ffi.enable=1 +opcache.jit=off +error_reporting=E_ALL & ~E_DEPRECATED +--FILE-- +run(static function () use ($readEnd): void { + Coroutine::spawn(static function () use ($readEnd): void { + Io::awaitReadable($readEnd); + + echo 'the reader must never wake', PHP_EOL; + }); + + Coroutine::sleep(IDLE_SECONDS); + }); + + $spent = [consumedCpuSeconds() - $cpuBefore, (hrtime(true) - $wallBefore) / 1_000_000_000]; + + fclose($writeEnd); + fclose($readEnd); + + return $spent; +} + +[$cooperativeCpu, $cooperativeWall] = idleRun(false); +[$preemptiveCpu, $preemptiveWall] = idleRun(true); + +echo 'both runs idled for the full time: ', + $cooperativeWall >= IDLE_SECONDS && $preemptiveWall >= IDLE_SECONDS ? 'yes' : 'no', PHP_EOL; +echo 'the cooperative run burned almost nothing: ', + $cooperativeCpu < $cooperativeWall / 100 ? 'yes' : 'no', PHP_EOL; +echo 'preemption cost no more than the budget on top of it: ', + $preemptiveCpu - $cooperativeCpu < CPU_BUDGET + ? 'yes' + : 'NO (' . number_format($preemptiveCpu - $cooperativeCpu, 6) . 's)', PHP_EOL; +?> +--EXPECT-- +both runs idled for the full time: yes +the cooperative run burned almost nothing: yes +preemption cost no more than the budget on top of it: yes diff --git a/tests/Functional/testAnIdlePreemptiveRuntimeDoesNotWakeThePollerEverySlice.phpt b/tests/Functional/testAnIdlePreemptiveRuntimeDoesNotWakeThePollerEverySlice.phpt new file mode 100644 index 0000000..640668d --- /dev/null +++ b/tests/Functional/testAnIdlePreemptiveRuntimeDoesNotWakeThePollerEverySlice.phpt @@ -0,0 +1,62 @@ +--TEST-- +An idle preemptive runtime blocks in one poll instead of waking once per slice +--INI-- +ffi.enable=1 +opcache.jit=off +error_reporting=E_ALL & ~E_DEPRECATED +--FILE-- +run(static function (TaskRuntime $self) use ($readEnd, &$armed): void { + $armed = $self->preemptor()?->isArmed() ?? false; + + // Nothing is ever written to this descriptor. It is here so the idle turn is a real blocking + // stream_select() with something in its read set — the shape a server waiting for work has. + Coroutine::spawn(static function () use ($readEnd): void { + Io::awaitReadable($readEnd); + + echo 'the reader must never wake', PHP_EOL; + }); + + Coroutine::sleep(IDLE_SECONDS); +}); + +$wall = (hrtime(true) - $wallBefore) / 1_000_000_000; +$poller = $runtime->scheduler()->poller(); +$wakeups = $poller instanceof StreamPoller ? $poller->wakeups() : PHP_INT_MAX; + +echo 'the runtime was slicing throughout: ', $armed ? 'yes' : 'no', PHP_EOL; +echo 'it idled for the whole second: ', $wall >= 0.99 ? 'yes' : 'no', PHP_EOL; +echo 'poller wakeups over that second: ', $wakeups <= BOUND ? 'far below 100' : 'NO (' . $wakeups . ')', PHP_EOL; + +fclose($writeEnd); +fclose($readEnd); +?> +--EXPECT-- +the runtime was slicing throughout: yes +it idled for the whole second: yes +poller wakeups over that second: far below 100 diff --git a/tests/Functional/testAnIdleWorkerInAPreemptivePoolStopsItsOwnSliceTimerToo.phpt b/tests/Functional/testAnIdleWorkerInAPreemptivePoolStopsItsOwnSliceTimerToo.phpt new file mode 100644 index 0000000..24e23f2 --- /dev/null +++ b/tests/Functional/testAnIdleWorkerInAPreemptivePoolStopsItsOwnSliceTimerToo.phpt @@ -0,0 +1,53 @@ +--TEST-- +A worker waiting for its next record wakes as rarely as an idle single-process runtime +--INI-- +ffi.enable=1 +opcache.jit=off +error_reporting=E_ALL & ~E_DEPRECATED +--FILE-- +publishTask($probe); + +$runtime->run(static function (TaskRuntime $self) use ($probe): void { + Timer::after(45.0, static function (): void { + throw new RuntimeException('deadline: the worker never reported its wakeups'); + }); + + $wakeups = $self->spawnParallel($probe)->await(); + + echo 'the worker reported a count: ', is_int($wakeups) && $wakeups >= 0 ? 'yes' : 'no', PHP_EOL; + echo 'its wakeups over an idle second: ', + is_int($wakeups) && $wakeups <= BOUND ? 'far below 100' : 'NO (' . get_debug_type($wakeups) . ' ' . $wakeups . ')', + PHP_EOL; +}); + +echo 'children left: ', parallelChildrenLeft(), PHP_EOL; +?> +--EXPECT-- +the worker reported a count: yes +its wakeups over an idle second: far below 100 +children left: none diff --git a/tests/Functional/testTheSliceTimerIsArmedAgainAfterASignalCutAPollShort.phpt b/tests/Functional/testTheSliceTimerIsArmedAgainAfterASignalCutAPollShort.phpt new file mode 100644 index 0000000..7dacb50 --- /dev/null +++ b/tests/Functional/testTheSliceTimerIsArmedAgainAfterASignalCutAPollShort.phpt @@ -0,0 +1,122 @@ +--TEST-- +A signal that cuts an idle poll short still leaves slicing armed for whatever runs next +--INI-- +ffi.enable=1 +opcache.jit=off +error_reporting=E_ALL & ~E_DEPRECATED +--FILE-- +ticks = 0; +$state->ticksSeenByTheLoop = -1; +$state->kernelInterval = -1; +$state->paused = true; + +$runtime = new Runtime(preemptive: true); + +$wallBefore = hrtime(true); + +$runtime->run(static function (TaskRuntime $self) use ($readEnd, $state): void { + Coroutine::spawn(static function () use ($readEnd): void { + Io::awaitReadable($readEnd); + + echo 'the reader must never wake', PHP_EOL; + }); + + // One idle poll, interrupted partway through by the helper's SIGUSR1 and retried from inside + // the poller with the remaining timeout. + Coroutine::sleep(IDLE_SECONDS); + + // Read from the kernel rather than from the preemptor's own bookkeeping: getitimer(2) is the + // only witness that can tell a timer this process armed from one it merely believes in. + $state->kernelInterval = $self->preemptor()?->clock()->kernelIntervalMicroseconds() ?? -1; + $state->paused = $self->preemptor()?->isSlicingPaused() ?? true; + + // And the behaviour the interval exists for: a call-free loop must still lose the CPU. + Coroutine::spawn(static function () use ($state): void { + $sum = 0; + + for ($index = 0; $index < ITERATIONS; $index++) { + $sum += $index % 7; + } + + $state->ticksSeenByTheLoop = $state->ticks; + }); + + Coroutine::spawn(static function () use ($state): void { + for ($round = 0; $round < 10_000; $round++) { + $state->ticks++; + Coroutine::yield(); + } + }); + + for ($round = 0; $round < 3; $round++) { + Coroutine::yield(); + } +}); + +$wall = (hrtime(true) - $wallBefore) / 1_000_000_000; + +pcntl_waitpid($helper, $status); + +echo 'the poll was cut short by a signal: ', $interrupted > 0 ? 'yes' : 'no', PHP_EOL; +echo 'it was retried for the rest of the timeout: ', $wall >= IDLE_SECONDS ? 'yes' : 'no', PHP_EOL; +echo 'the kernel interval afterwards, in us: ', $state->kernelInterval, PHP_EOL; +echo 'slicing was left paused: ', $state->paused ? 'yes' : 'no', PHP_EOL; +echo 'a call-free loop was still preempted afterwards: ', + $state->ticksSeenByTheLoop >= 1 && ($runtime->preemptor()?->preemptions() ?? 0) >= 1 ? 'yes' : 'no', PHP_EOL; + +fclose($writeEnd); +fclose($readEnd); +?> +--EXPECT-- +the poll was cut short by a signal: yes +it was retried for the rest of the timeout: yes +the kernel interval afterwards, in us: 10000 +slicing was left paused: no +a call-free loop was still preempted afterwards: yes diff --git a/tests/Support/shared.php b/tests/Support/shared.php index 6d623ee..c648e46 100644 --- a/tests/Support/shared.php +++ b/tests/Support/shared.php @@ -27,6 +27,7 @@ use Lisachenko\NativePhpCoroutines\Coroutine; use Lisachenko\NativePhpCoroutines\Parallel\Task; +use Lisachenko\NativePhpCoroutines\StreamPoller; use Lisachenko\NativePhpCoroutines\TaskRuntime; /** @@ -301,6 +302,37 @@ public function run(TaskRuntime $runtime): mixed } } +/** + * Reports how many times the executing worker's own poller woke while the task did nothing. + * + * A worker waiting for its next inbox record is the state a pool spends most of its life in, and it + * is a *preemptive* idle whenever the pool is: each child arms its own slice timer after the fork. + * The count is taken in the worker and returned as an int, because "did the child stop the clock + * for its own idle poll?" cannot be observed from the parent — the parent's poller is a different + * poller and the child's timer is a different timer. + */ +final class ReportsIdlePollerWakeupsTask implements Task +{ + public function __construct(private readonly float $seconds = 1.0) {} + + public function run(TaskRuntime $runtime): mixed + { + $poller = $runtime->scheduler()->poller(); + + if (!$poller instanceof StreamPoller) { + return -1; + } + + $before = $poller->wakeups(); + + // Nothing else is runnable in this worker, so the whole sleep is spent in one blocking poll + // over the control socket and the arena's wake pipe. + Coroutine::sleep($this->seconds); + + return $poller->wakeups() - $before; + } +} + /** * Reports whether the executing process can reach its own preemptor through the task surface. *