Skip to content

vm: the interpreter services a safepoint, so a spinning thread stops freezing the interpreter - #942

Open
tannevaled wants to merge 2 commits into
mainfrom
vm-interpreter-safepoint
Open

tannevaled wants to merge 2 commits into
mainfrom
vm-interpreter-safepoint

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

vm: the interpreter services a safepoint, so a spinning thread stops freezing the interpreter

#940 measured it: Thread.new { while true; end } and Thread.new itself
never returns
. The eager-start handshake (eagerStart, thread.go) 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, so
the spawner waits for ever.

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.

⛔ 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 by in.A <= pc (the only jumps that can
loop). 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 that calls the block: their
bytecode has no backward jump at all, 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 counters feed one vm.tickSafepoint.

Measured: reference, control, treatment

cmd/oracle with three engines — MRI 4.0.7, rbgo at main, 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:

MRI 4.0.7 rbgo main this branch
exit 0 KILLED, unfinished 0
stdout 88 bytes, 5 markers 9 bytes AGREES, byte for byte

Thread.new { loop { } }, same shape:

MRI 4.0.7 rbgo main this branch
exit 0 KILLED, unfinished 0
stdout 104 bytes, 5 markers 9 bytes 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 — the three things #940 measured as
broken.

⛔ $stdout.sync = true and 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.timeout is byte-identical between control and treatment.
timeout.go is still the stub whose own comment says enforcement "needs the
scheduler 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/socket hangs are a different half. Those are native calls
where 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 = 4096 is a starting point, not a measured value.
  • Two designs need measuring against each other, and the constant's name is
    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).
  • The latency bar to hold while the constant grows is the one socket: Thread#kill can interrupt a blocked accept — the unblock function #935 set for a
    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.
  • Full internal/vm suite green. ⛔ Two honest caveats on that run, both of
    which make CI the authority rather than my laptop:
    • It ran on a tree that also carried socket: the three open doors #931 named and nothing wired #939's three socket files, because the
      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.
    • It took 293.5 s, against 386.5 s for the previous full run without
      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.
  • One cheap guard already in place: interpreterSafepoint skips the GVL pass
    entirely when len(vm.threads) == 1, because there is nobody to hand it to and
    threadRelease would still pay an unlock, a Gosched, a relock and two
    context copies. serviceSafepoint still runs — three field reads when nothing
    is 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

tannevaled and others added 2 commits November 15, 2026 12:02
…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>
@tannevaled

Copy link
Copy Markdown
Contributor Author

Half the owed measurement, taken: allocations are unchanged. The time half is still owed, and the noise points where a cost would be.

abbench 10 alternating rounds, base = 94a1afe9 (this branch's merge base),
head = 0627f096, GOMAXPROCS=1, darwin/arm64, Apple M4 Max.

What load does not distort — allocs/op and B/op

This repository's own rule is that allocation counts are exact under load and
times are not
, so this is the part that can be settled today.

             │   base.txt    │               head.txt               │
             │   allocs/op   │  allocs/op   vs base                 │
Fib            242.9k ± 0%     242.9k ± 0%       ~ (p=0.098 n=10)
LoopSum        598.9k ± 0%     598.9k ± 0%       ~ (p=1.000 n=10) ¹
SmallIntLoop    0.000 ± 0%      0.000 ± 0%       ~ (p=1.000 n=10) ¹
BlockEach      198.9k ± 0%     198.9k ± 0%       ~ (p=1.000 n=10) ¹
TimesSmall      4.000 ± 0%      4.000 ± 0%       ~ (p=1.000 n=10) ¹
TimesLarge     3.979k ± 0%     3.979k ± 0%       ~ (p=1.000 n=10) ¹
ProcCall       198.9k ± 0%     198.9k ± 0%       ~ (p=1.000 n=10) ¹
ArrayMap       38.44k ± 0%     38.44k ± 0%       ~ (p=1.000 n=10) ¹
geomean                    ²                +0.00%
¹ all samples are equal

+0.00%, with six of eight rows byte-identical across all ten rounds. B/op
likewise: Fib 7.411Mi both sides, LoopSum 4.569Mi both, BlockEach 1.518Mi both,
ProcCall 1.518Mi both, ArrayMap 698.8Ki both ("all samples are equal"),
TimesSmall 352.0 both, TimesLarge 31.41Ki both — every one at ±0%.

That confirms the prediction the fast path was written for: in a single-threaded
benchmark len(vm.threads) == 1, so interpreterSafepoint skips threadPass
entirely and does three field reads. Nothing is allocated.

(The B/op geomean reads +1.20% purely from SmallIntLoop, whose figure is
5 bytes at ±120% / ±573% — noise on an effectively-zero allocation, not a
signal.)

⛔ What load DOES distort, and why I am not quoting it as a result

The sec/op column from the same run:

Fib            58.88m ±  20%   66.91m ± 551%        ~ (p=0.436 n=10)
LoopSum        39.57m ±  10%   46.09m ± 361%  +16.46% (p=0.035 n=10)
SmallIntLoop   61.85m ±  17%   63.98m ± 432%        ~ (p=0.529 n=10)
BlockEach      13.26m ±   9%   14.73m ±  68%        ~ (p=0.075 n=10)
geomean         6.689m          7.204m          +7.71%

±551%, ±432%, ±361%. Samples spanning orders of magnitude are not a
measurement, and benchstat's p-values are computed over exactly those samples.
This run had no --max-load gate on purpose — it was taken for the allocation
columns, with the host between 10 and 26.

⚠ But I am not dismissing it either, and the reason is specific. The one row
that reached p < 0.05 is LoopSum — the pure bytecode-loop benchmark, which is
precisely what door 1 (vm.backJump at the four backward edges) touches. An
increment and a compare per backward jump is exactly the cost that would land
there. So the noise neither confirms nor refutes a regression; it leaves a
named hypothesis the real measurement must test:

does LoopSum regress, and by how much, with the host under 8?

That is a better position than before this run, and it is still not a result.

What the real measurement needs, and why it has not happened

abbench --max-load 8 --wait 2h ran for 41 minutes earlier and kept zero
rounds
: base.txt and head.txt were 0 bytes. Its log is a monotone decline
from 44.43 to 9.33 and then a plateau between 10 and 16 with spikes — the
host never crossed 8.

⭐ Worth stating because it is the good outcome: zero kept rounds means no
partial series
. A benchmark series with a hole in it is not a smaller series,
it is a biased one, and benchstat would have compared it without a word.

So this needs a quiet host, not more patience here. When it happens, the two
designs to compare are still the ones in the PR body: the counter paid per tick
(this branch) against MRI's timer-set flag and a single load.

🤖 Generated with Claude Code

@tannevaled

Copy link
Copy Markdown
Contributor Author

The owed time measurement: attempted for two hours under the gate, and NOT obtained. Here is the state so nobody repeats the wait blindly.

abbench --max-load 8 --wait 2h --rounds 8 --gomaxprocs 1, base = 94a1afe9
(this branch's merge base), head = 0627f096, darwin/arm64, Apple M4 Max.

Its own closing line:

abbench: wait budget 2h0m0s spent with 1 of 8 rounds kept (3 discarded):
  load average above the limit: 33.04 > 8.00

1 round of 8. Four attempts in two hours; three were taken above the
threshold and discarded as pairs, one survived. The log is a monotone
decline from 44.43 to a floor around 9.3, then a plateau between 10 and 16 with
spikes to 50+.

⭐ Zero or one kept rounds is the GOOD failure mode: no partial series. A
benchmark series with a hole in it is not a smaller series, it is a biased one,
and benchstat would compare it without a word. I checked the pair is complete
rather than assuming it: 6 benchmark lines on each side, equal.

What n=1 is worth, in benchstat's own words

Fib            53.81m ± ∞ ¹   52.45m ± ∞ ¹   ~ (p=1.000 n=1) ²
LoopSum        36.90m ± ∞ ¹   37.73m ± ∞ ¹   ~ (p=1.000 n=1) ²
geomean        15.39m         15.58m         +1.21%
¹ need >= 6 samples for confidence interval at level 0.95
² need >= 4 samples to detect a difference at alpha level 0.05

⛔ So +1.21% is not the cost of this change and I am not quoting it as one.
benchstat refuses a verdict at n=1 and says exactly why; a geomean over one pair
of single runs taken at the edge of the gate carries no information about
throughput. It is recorded here only so the next attempt knows what was seen.

⚠ Note it also sits on the opposite side of the earlier ungated run, which hinted
LoopSum +16.46%. Two hints pointing different ways is what noise looks like,
and it is why neither is a result.

What IS settled, by the measure that load does not distort

From the n=10 ungated run (allocations are exact under load, times are not):

allocs/op   +0.00% geomean, six of eight rows "all samples are equal"
B/op        identical on every real figure, ±0% both sides

and this n=1 gated round agrees: allocs/op +0.00%, B/op +0.00%, "all samples
are equal" on three rows.

That confirms the fast path works as designed: with one live thread
interpreterSafepoint skips threadPass and does three field reads, so nothing
is allocated.

⇒ This pull request stays open, and the condition is unchanged

Its body says not to merge on a green suite alone. CI is green
(PASS SUCCESS:28 at b350493, after the coverage fix for
interpreterSafepoint's multi-thread branch), and that is still not the missing
evidence.

What the next attempt needs, stated so it is not re-derived:

  • A genuinely quiet host. This one, with ~25 concurrent sessions, offered
    four windows in two hours and only one was clean. That is a property of the
    machine, not of the measurement.
  • The two designs compared against each other, not just head against base:
    the counter paid per tick (this branch) versus MRI's timer-set flag and a
    single load (rb_thread_schedule). safepointTicks = 4096 is a starting
    point, not a measured value.
  • The latency bar held while the constant grows: an interrupt arriving
    within 0.1 s, which is what socket: Thread#kill can interrupt a blocked accept — the unblock function #935 established for a blocked accept.
  • LoopSum and SmallIntLoop for door 1 (backward jumps), BlockEach,
    TimesLarge, ProcCall, ArrayMap for door 2 (callBlockSelf), Fib as a
    near-control. Keeping them separate is what would attribute a cost to a door.

The binaries and the base worktree are still on disk
(rbgo-safepoint-base.test, rbgo-safepoint-head.test,
rbgo-safepoint-base/), so a later attempt needs no rebuild — and a rebuild is
itself load, which closed the window once today.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant