Repository navigation
feat(profiler): script-level function profiler — niu --profile <script> (#815) - #829
Merged
Merged
Conversation
…t> (#815) Deterministic interpreter instrumentation (no sampling): a pure-observer call-tree profiler riding the executor's existing dispatch seams. Seams (GNU anchors, vendored bash.git @b4608166 / Bash-5.3 patch 15): - function entry/exit: execute_function wrapper, execute_cmd.c:5200 (Drop guard attributes every exit form — return/exit/expansion-failure — exactly once, mirroring unwind_protect_int frame restore) - simple-command dispatch: execute_prepared_command, execute_cmd.c:624; the frame is armed TENTATIVE and classified by the real dispatcher (function/external dispatch cancels it, everything else confirms as a builtin) — no second admission list (rubash#117) - external spawn face: execute_external + the pipeline-stage/concurrent pipeline spawn paths - (subshell) frame: execute_in_subshell body, execute_cmd.c:1576 - $(comsub) frame: subst.c:7143 command_substitute; entered at the &self and mut-typed dispatch entries with nearest-live-ancestor dedupe so the engine's several comsub routes frame exactly once Runtime: thread-local frame stack; self = elapsed - children, elapsed charged to the nearest live ancestor (cancelled tentative tombstones are skipped, so dispatch overhead stays in the parent's self time). Unique collapsed-stack paths accumulate count/self/total. Attribution three-way split (builtin / external spawn / subshell+comsub) is a SELF-time split, so faces sum to 100% of wall. Outputs: text call tree (self/children/calls, sorted by self) + collapsed-stack lines (flamegraph/speedscope-renderable). Reports are written to --profile-output (default: process temp dir, never the script directory) and NEVER to stdout/stderr. Off path: one relaxed AtomicBool load per seam. Interleaved A/B against the c6f4a9b binary across the #607-#609 probe families: every probe within +/-1.6% (noise); profiler-on overhead 1.9-5.0% on the hot probes. Acceptance (rubash#815 checklist): - 83-suite GNU differential re-run with profiler ON: 82/83 suites byte-identical diff counts vs the profiler-off baseline ledger; the 83rd (read) is a pre-existing environment-bound /dev/tty flake that flips 0<->2 on the master binary and profiler-off identically (scripts/check-profile-differential.sh scripts the full run) - profiler-on stdout/stderr/rc byte-identity pinned in tests/profiler_regressions.rs over a seam-covering probe set - text-tree + collapsed fixtures committed (times masked, structure and counts exact)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #815.
Architecture (one paragraph)
Seams. The profiler is deterministic interpreter instrumentation (no sampling): a pure observer riding the executor's existing dispatch seams, each an Drop-guard so every exit form attributes exactly once — function entry/exit (
execute_functionwrapper, GNUexecute_cmd.c:5200, covering normal return /return/exit/ expansion-failure aborts via stack unwinding, mirroringunwind_protect_int's frame restore), the simple-command dispatch point (execute_prepared_command,execute_cmd.c:624), the external spawn face (execute_external+ the pipeline-stage and concurrent-pipeline spawn paths), the flat subshell body (execute_cmd.c:1576), and command substitution (subst.c:7143) entered at the engine's comsub dispatch entries with nearest-live-ancestor dedupe so the several$(...)routes (&selfwrapper, mut typed dispatcher, typed child-executor path) frame exactly once. Classification without a second admission list (rubash#117): the dispatch-point frame is armed tentative; the real dispatcher classifies it — a function/external dispatch cancels the frame (tombstone, skipped in keys and charged past), everything else confirms as a builtin on drop. Runtime. A thread-local frame stack: on pop, self = elapsed − children, elapsed charged to the nearest live ancestor (tombstones pass through, so dispatch overhead stays in the parent's self time and never double-counts); each unique collapsed-stack path accumulates count/self/total. Attribution three-way separation (builtin / external spawn / subshell+comsub) is a self-time split, so the four faces sum to 100% of wall. Outputs. Text call tree (self/children/calls, sorted by self) + collapsed-stack (path;segments self_ns, flamegraph/speedscope-renderable). Reports go to--profile-output <base>(text<base>+ collapsed<base>.collapsed, swapped for a.collapsedsuffix; default: the process temp directory — never the script directory, so a report file cannot leak into*globs) and never to stdout/stderr.Overhead (measured, debug builds, interleaved runs, median of 9/7)
c6f4a9b3binary across the probe families — loop:, arith(( )), strconcat, fncall, cmdsub, external, pipeline, expansion, scalar/prefix assign, spawn — every probe within ±1.6% (median ratios 0.971–1.016). Off-path cost is one relaxedAtomicBoolload + not-taken branch per seam.Signature acceptance — 83-suite GNU differential with profiler ON
scripts/check-profile-differential.shscripts the full run: it wrapsscripts/true-baseline.shverbatim (frozen methodology,RUB_OVERRIDElauncher adds--profile --profile-output <scratch>/last.collapsed, report path in native Windows form perdocs/windows-path-contract.md, absolute shebang because the harness runs a restricted PATH).readsuite's 2 lines)readdelta is a pre-existing environment-bound flake:read2.subrunsread -t 1 a < /dev/ttywith no controlling tty in the harness. Flip-flop evidence on this machine, same session: master binary0,2,2; branch binary profiler-off2,0,2; branch profiler-on2,0,2— both modes flip identically, independent of this change.Semantic zero-disturbance is additionally pinned in-process:
tests/profiler_regressions.rs::profile_mode_is_byte_identical_to_profile_offruns a seam-covering probe set (functions+recursion, comsub/subshell, pipelines incl. inline arms and the concurrent path, externals, abort forms, arith loops) and asserts stdout/stderr/rc byte-identical with--profile.Outputs (samples,
tests/fixtures/profiler/demo.sh)Text tree (full sample in
tests/fixtures/profiler/demo.txt.masked):Collapsed stack (
demo.collapsed.masked, weights = leaf self ns):inferno-flamegraph/flamegraph.pl --countname=nsrender the.collapsedfile directly.Interface
niu --profile <script> [--profile-output <f>]— engine meta flags handled before the GNU option parser (the--identity/--internal-*precedent), soLONG_OPTIONSkeeps its exact GNU 5.3 table bytes and unknown-long-option rc-2 behavior.--profileconsumes the script operand through the normal launcher path;--profile-outputwithout--profileis inert.-c/-s/interactive forms are unprofiled in M1 (per ticket; interactive toggle is M2).Notes
returnlands inside the function frame,exitinside the enclosing subshell frame (pinned byabort_forms_attribute_correctly).readsuite flake and its flip evidence are recorded here rather than "fixed" — it predates this change and is not a profiler regression; happy to open a separate issue for the/dev/tty-under-harness behavior if the owner wants it tracked.Rebased on
3c611299(#822). Work done in the isolated worktreeD:/repo/rubash-wt-815.