Repository navigation
vm: the interpreter services a safepoint, so a spinning thread stops freezing the interpreter - #942
tannevaled wants to merge 2 commits into
Conversation
…freezing it #940 measured it: `Thread.new { while true; end }` and Thread.new ITSELF never returns. eagerStart parks the spawner on a channel until the new thread blocks or finishes, and firstPark -- the only sender -- has exactly those two callers. A bare loop takes neither. The safepoint machinery was already complete: serviceSafepoint is called from 19 sites, every one in thread.go or queue.go, every one a path that blocks or parks. The interpreter called it from none, and thread.go's own comment said a backward jump was meant to be one. ## TWO doors, and wiring one was measured to do nothing Door 1, the dispatch loop's backward edges (OpJump, OpBranchIf, OpBranchUnless, OpBranchNil, guarded by in.A <= pc). That alone made `while true; end` agree with MRI byte for byte. Door 2, callBlockSelf. And `loop { }` went on hanging EXACTLY as before, byte for byte against the control, because Kernel#loop, Integer#times and Array#each spin in a GO loop calling the block: their bytecode has no backward jump, so door 1 never fires. callBlockSelf is the funnel every native iterator passes through. A fix that does not move the measurement is not a fix, and this one did not until door 2 was wired. Both feed one vm.tickSafepoint. ## Measured with reference, control and treatment cmd/oracle, three engines: MRI 4.0.7, rbgo at main, this branch. Thread.new { while true; end } MRI 88 bytes / main KILLED 9 bytes / branch AGREES byte for byte Thread.new { loop { } } MRI 104 bytes / main KILLED 9 bytes / branch AGREES byte for byte So Thread.new returns, the main thread runs beside a spinning one, and Thread#kill reaches a CPU-bound Ruby loop. ## What this does NOT fix, measured rather than assumed #803 Timeout.timeout is byte-identical between control and treatment: timeout.go is still the stub whose comment says enforcement needs scheduler work. This is that work, and Timeout now has its primitive, but nothing here implements it -- its own change, so a Timeout regression is never diagnosed through a hot-loop change. #933's library/socket hangs are the other half: native calls where the GVL IS released and a canceller per site is missing. ## The performance measurement is OWED This touches the hottest loop plus every block invocation. safepointTicks = 4096 is a starting point, NOT a measured value, and two designs need measuring against each other: a counter paid per tick (this one) versus MRI's timer-set flag and a single load. The 1-minute load average here has been 36-90 all morning, where a throughput figure cannot tell one design from another. ⛔ Do not merge on the green suite alone. Also corrected: vm.go's GVL comment ended at "released only inside blocking native methods", and that word was the whole defect -- the list was accurate and its COMPLETENESS was what nobody checked. Refs #940, #803, #933 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…e change exists for CI on 0627f09 was RED, and the gate named it precisely: -- NEWLY BELOW 100% (1) -- internal/vm/thread.go interpreterSafepoint 66.7% (was fully covered) 2 blocks of 3, and the missing one is `vm.threadPass(runtime.Gosched)` under `if len(vm.threads) > 1` -- the branch that actually hands the GVL to another thread. The fast path (one live thread, skip the pass) was exercised by every existing test that spins a Ruby loop; the slow path, which is the whole point of the change, was not. ⛔ This was a real gap, not a coverage chore. The three-engine oracle probes on #940 did exercise it -- that is how `Thread.new { loop { } }` started working -- but those probes are not in this suite, so nothing here would have noticed the branch breaking. And my local `go test ./internal/vm` was green on exactly that gap, because the ratchet is a separate CI step: running the package's tests is not running the gate. ## The two tests TestInterpreterSafepointHandsTheGVLOverWhenAnotherThreadIsLive spins the MAIN thread through 200_000 backward jumps (~48 safepoints against safepointTicks = 4096) while a second thread is parked in sleep, which is the only shape that reaches the branch. It also asserts the peer is STILL asleep afterwards: passing the GVL to a parked thread must not wake it, run it or kill it -- a safepoint is a hand-off offer, not a resumption. TestInterpreterSafepointIsATransparentNoOpWithOneThread is the companion that makes the fast path defensible rather than merely cheap: the same loop with one thread must behave identically. ## Verified with the gate's own command, not with a passing test A passing test says nothing about coverage, so: gotest -coverprofile=... -run '<the two>' ./internal/vm go tool cover -func=... covratchet -profile ... -require interpreterSafepoint,tickSafepoint,backJump required 3 function(s), matched 3 record(s) in a profile of 7357 all required functions at 100% The denominator is printed, so a run that had read nothing could not pass for clean -- which is why `-require` exists. ## One thing I am NOT claiming The same CI run also reported three functions "further below than recorded", which that gate reports without failing on (#816): builtins.go bootstrap 99.6%, recorded 99.7% filestat.go registerFileStat 99.3%, recorded 99.4% nethttp.go registerNetHTTP 92.3%, recorded 96.0% I have NOT attributed those to this change and will not guess. Two are tenth-of-a-point drifts; the nethttp one is 3.7 points and its tests drive real servers, on a run where internal/vm took 2922 s against ~293 s locally. A timing-sensitive path taking a different branch on a very slow runner is as consistent with that as anything I did -- and saying so needs a control I have not run. Refs #940, #816 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Half the owed measurement, taken: allocations are unchanged. The time half is still owed, and the noise points where a cost would be.
What load does not distort —
|
The owed time measurement: attempted for two hours under the gate, and NOT obtained. Here is the state so nobody repeats the wait blindly.
Its own closing line: 1 round of 8. Four attempts in two hours; three were taken above the ⭐ Zero or one kept rounds is the GOOD failure mode: no partial series. A What n=1 is worth, in benchstat's own words⛔ So +1.21% is not the cost of this change and I am not quoting it as one. ⚠ Note it also sits on the opposite side of the earlier ungated run, which hinted What IS settled, by the measure that load does not distortFrom the n=10 ungated run (allocations are exact under load, times are not): and this n=1 gated round agrees: That confirms the fast path works as designed: with one live thread ⇒ This pull request stays open, and the condition is unchangedIts body says not to merge on a green suite alone. CI is green What the next attempt needs, stated so it is not re-derived:
The binaries and the base worktree are still on disk 🤖 Generated with Claude Code |
vm: the interpreter services a safepoint, so a spinning thread stops freezing the interpreter
#940 measured it:
Thread.new { while true; end }andThread.newitselfnever returns. The eager-start handshake (
eagerStart,thread.go) parks thespawner on a channel until the new thread blocks or finishes, and
firstPark— the only sender — has exactly those two callers. A bare loop takes neither, so
the spawner waits for ever.
The safepoint machinery was already complete:
serviceSafepointis called from19 sites, every one in
thread.goorqueue.go, every one a path thatblocks or parks. The interpreter called it from none.
⛔ TWO doors, and fixing one was measured to do nothing
This is the part worth reading, because the first attempt looked finished and
was not.
Door 1 — the dispatch loop's backward edges.
OpJump,OpBranchIf,OpBranchUnless,OpBranchNil, guarded byin.A <= pc(the only jumps that canloop). That alone made
while true; endagree with MRI byte for byte.Door 2 —
callBlockSelf. Andloop { }went on hanging exactly asbefore, byte for byte against the control, because
Kernel#loop,Integer#timesandArray#eachspin in a Go loop that calls the block: theirbytecode has no backward jump at all, so door 1 never fires.
callBlockSelfisthe funnel every native iterator passes through.
A fix that does not move the measurement is not a fix, and this one did not
until door 2 was wired. Both counters feed one
vm.tickSafepoint.Measured: reference, control, treatment
cmd/oraclewith three engines — MRI 4.0.7, rbgo atmain, and this branch —which is the shape the measurement rules ask for.
Thread.new { while true; end }, then the main thread works, sleeps and kills:mainThread.new { loop { } }, same shape:mainSo
Thread.newreturns, the main thread runs beside a spinning one, andThread#killreaches a CPU-bound Ruby loop — the three things #940 measured asbroken.
⛔
$stdout.sync = trueand a marker per step are load-bearing in those probes.An earlier version produced zero bytes before being killed, which cannot say
where it stopped: an absent line can be an unflushed line.
What this does NOT fix, measured rather than assumed
#803
Timeout.timeoutis byte-identical between control and treatment.timeout.gois still the stub whose own comment says enforcement "needs thescheduler work planned for a later round" — this is that work, and Timeout now
has the primitive it was waiting for, but nothing here implements it. Its own
change, so that a Timeout regression is never diagnosed through a hot-loop
change.
#933's
library/sockethangs are a different half. Those are native callswhere the GVL is released and the missing piece is a canceller per site.
Conflating the two would send the next reader to the wrong file.
⛔ The performance measurement is OWED, and I have not taken it
This touches the interpreter's hottest loop plus every block invocation. The
repository's rule is that performance is part of every change, measured single
core against the reference.
This machine's 1-minute load average has been between 36 and 90 all morning.
A throughput figure taken there cannot tell one design from another, and
reporting it would be worse than reporting none. So:
safepointTicks = 4096is a starting point, not a measured value.deliberate so the second is not foreclosed: a counter paid on every tick
(this one), versus MRI's, where the timer thread sets a flag and the loop
pays a single load (
rb_thread_schedule).blocked
accept: an interrupt arrives within 0.1 s, as in MRI.⛔ Please do not merge this on the green suite alone. It wants a quiet
machine and a before/after on a clean clone of the tag, the way this repository
measures everything else.
Checks
gofmt -l .silent.internal/vmsuite green. ⛔ Two honest caveats on that run, both ofwhich make CI the authority rather than my laptop:
work was done on top of that branch and moved afterwards. This branch's
exact tree has not had a local suite run — the socket change is three
unrelated files and independently green, but "nearly the same tree" is not
the same tree.
this change — 24 % faster, in the direction that would flatter the change.
That is load variation on a machine between 36 and 90, not a speedup, and it
is exactly why neither number is quoted as a measurement.
interpreterSafepointskips the GVL passentirely when
len(vm.threads) == 1, because there is nobody to hand it to andthreadReleasewould still pay an unlock, aGosched, a relock and twocontext copies.
serviceSafepointstill runs — three field reads when nothingis queued — since that is what delivers a kill or a raise.
Refs #940, #803, #933, #935, #939
(#940 is what this measures and does not close on its own: the performance
question above is part of it. #803 and #933 stay open and are explicitly not
touched.)
🤖 Generated with Claude Code