Repository navigation
Commit edb3c5f
vm: TracePoint — line, class/end, call/return, b_call/b_return and rescue (core/tracepoint 0 → 69) (#654)
* vm,bytecode: TracePoint — the object, the event set, and hooks in exec
There was no TracePoint code in the tree; core/tracepoint was 0 of 75.
Read before designing (ruby/ruby v3_4_0): include/ruby/internal/event.h
for the RUBY_EVENT_* bit set, vm_trace.c for tracepoint_new_s (:1490),
tracepoint_enable_m (:1375), tracepoint_disable_m (:1424),
tracepoint_inspect (:1521), rb_tracearg_* (:862-1080),
rb_tracepoint_enable_for_target (:1234), tracepoint_allow_reentry
(:1607), get_path_and_lineno (:694), hook_list_connect and
rb_exec_event_hooks (:412); iseq.c for rb_iseq_trace_set (:3981) and
rb_iseq_add_local_tracepoint_recursively (:3912); vm_insnhelper.c for
vm_trace_hook (:7054).
Events raised: line, class, end, call, return, b_call, b_return, rescue.
Left out with reasons in supportedTraceEvents: c_call/c_return,
raise, script_compiled, thread_begin/thread_end/fiber_switch — each
needs a hook in a file outside this change.
The hot path. MRI never tests a flag per instruction: it emits distinct
trace instructions and rb_iseq_trace_set rewrites their encodings, so an
untraced program executes none. rbgo has no instruction to rewrite, so
the gate is one word — vm.traceEvents, zero unless a TracePoint is
enabled — tested once per instruction beside the pc publication that was
already there. Everything past it, including the line-map search, is
behind the gate.
Order is MRI's, and it is not registration order: global hooks first,
then the ISeq's local (target:) ones, each list walked most-recently-
enabled first, because hook_list_connect prepends. enable_spec pins all
three combinations.
core/tracepoint: 0 -> 69 of 75.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* vm: whitebox tests for TracePoint, and a bounds guard on the event path
Every branch of tracepoint.go, driven through Ruby where Ruby can reach
it and called directly where it cannot — the tpOf refusals, the
non-Thread and already-set target_thread: refusals, allow_reentry's
block check, the C-call inspect shape, the reentry guard, and the
frame-typing table.
frameTracePath now clamps its frame index: frameFileLabel indexes the
frame stacks unguarded, and exec can legitimately outrun them (the
bounds note at the pc publication says why), so an event raised from a
frame the stacks no longer cover reported a panic rather than the
script. scriptLabel is frameFileLabel's no-file answer, split out so the
clamped path can reach it with no frame at all.
Measured beside the specs: 15 differential probes (events, accessors,
inspect shapes, enable/disable returns, target:/target_line: including
every refusal, hook order, allow_reentry, thread filter, rescue) are
byte-identical to ruby 4.0.5. That second witness matters here because
the corpus shim defines ShouldProxy#equal but not #equal?, so a spec
written `x.should.equal?(y)` asserts nothing.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* vm: drop the TracePoint branches production cannot reach
Three pieces claimed something nothing could check, so they are gone
rather than covered by a test written to reach them:
- traceEventsNamed rendered an event mask for no caller but its own
test;
- #parameters carried MRI's C-call arm (rb_unnamed_parameters over the
method arity), and rbgo raises no :c_call;
- #inspect carried MRI's C-call and thread shapes, and rbgo raises
none of :c_call, :c_return, :thread_begin or :thread_end.
Each is noted where it was, so it comes back WITH its event rather than
ahead of it. traceArg.hasData went too: it was written at two sites and
read at none.
Re-measured after: core/tracepoint still 69, the 15 differential probes
still byte-identical to ruby 4.0.5.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* vm: raise the exit event for a frame that does not finish
A differential probe against ruby 4.0.5 found it: rbgo raised :return
only on normal completion, and MRI raises it for a frame an exception
unwinds past as well. `def a2; raise; end` called from a1 under a
:return TracePoint reports [:return, :a2, nil] then [:return, :a1, nil]
in MRI; rbgo reported neither. Same for a non-local return out of a
block, which MRI reports as the block's :b_return.
MRI's rule, read off hook_before_rewind and confirmed by measuring: a
frame that leaves by unwinding reports #return_value as nil, the frame
that CATCHES a non-local return reports the returned value, and a block
broken out of reports the break value.
Three sites, all gated on tracing being on:
- a deferred fire for the exception/throw case, registered BEFORE the
returnSignal recover so it RUNS after it and sees exitFired set;
- the returnSignal recover itself, before it truncates this frame's
own stack entries away;
- OpBreak and the non-local arm of OpReturn, which know their value
and must fire before they panic.
exitFired keeps it to exactly one event per frame.
Probes: 22 now, all byte-identical to ruby 4.0.5. core/tracepoint stays
69 — no spec exercises an unwinding return, which is why only a probe
could find this.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* vm: cover the mid-frame enable, and read target: as MRI's NIL_P
Two gaps the coverage list named, both real behaviour rather than dead
code.
A TracePoint enabled from INSIDE a frame must still raise that frame's
own :rescue and exit events — MRI rewrites the running iseq's trace
instructions when the hook goes on, rbgo builds the frame description on
first need — and neither lazy rebuild had a test. It does now, measured
against ruby 4.0.5 in all three shapes (top level, method, block).
enable's target_thread: default tested `target == nil` where MRI tests
NIL_P(target), so an explicit `target: nil` lost the current-thread
default that MRI keeps. Probe 23 pins both directions: `target: nil`
confines the trace to the calling thread, `target_thread: nil` does not.
internal/vm and internal/bytecode are back to the below-100 LIST main
carries, with no entry added. 24 probes, all byte-identical.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
* vm: stop the exit-event defer costing every frame
The first shape paid per FRAME as well as per instruction: exec
registered the unwind exit-event defer unconditionally and its closures
captured one more variable. bench/blocks.rb — 10M tiny block calls, this
VM's worst case for anything paid per frame, and the bench the frameCode
comment already names for exactly that — read +3.65% median / +5.22%
best-of against the base binary.
Two changes, measured: gate the defer on vm.traceEvents != 0 (an
open-coded defer inside an `if` is still open-coded, so an untraced
program does not register it), and move the one-shot flag out of exec's
locals into the frame description the four raise sites already hold.
blocks.rb: +0.75% median / -0.93% best-of. A direct A/B of the two
shapes puts the gain at -2.76% median on blocks.rb; dispatch.rb came
down with it, +2.60% -> +1.59%.
What is left is the per-instruction gate — one load and one not-taken
branch — which loop.rb, a single frame and 20M instructions, reads at
+0.4% best-of / +1.4% median.
The gating costs one corner: a frame already running when the TracePoint
was enabled raises no exit event if it then leaves by UNWINDING, because
it registered no defer. Its normal exit still does, and that is tested.
Re-verified on this tree: full suite green, below-100 coverage list
exactly main's, corpus 22296 with the same +69, 24/24 probes
byte-identical to ruby 4.0.5.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>1 parent 857d637 commit edb3c5f
5 files changed
Lines changed: 2052 additions & 0 deletions
File tree
- internal
- bytecode
- vm
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
396 | 396 | | |
397 | 397 | | |
398 | 398 | | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
0 commit comments