Skip to content

feat(profiler): script-level function profiler — niu --profile <script> (#815) - #829

Merged
caomengxuan666 merged 1 commit into
masterfrom
feat/815-profiler
Oct 18, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
feat/815-profiler

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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_function wrapper, GNU execute_cmd.c:5200, covering normal return / return / exit / expansion-failure aborts via stack unwinding, mirroring unwind_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 (&self wrapper, 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 .collapsed suffix; 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)

Signature acceptance — 83-suite GNU differential with profiler ON

scripts/check-profile-differential.sh scripts the full run: it wraps scripts/true-baseline.sh verbatim (frozen methodology, RUB_OVERRIDE launcher adds --profile --profile-output <scratch>/last.collapsed, report path in native Windows form per docs/windows-path-contract.md, absolute shebang because the harness runs a restricted PATH).

  • profiler-off baseline ledger: 83 suites, 26 with nonzero diffs, 482 diff lines
  • profiler-on ledger: 83 suites, 82/83 byte-identical per-suite diff counts and the identical failure set (484 lines; delta = the read suite's 2 lines)
  • the read delta is a pre-existing environment-bound flake: read2.sub runs read -t 1 a < /dev/tty with no controlling tty in the harness. Flip-flop evidence on this machine, same session: master binary 0,2,2; branch binary profiler-off 2,0,2; branch profiler-on 2,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_off runs 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):

== attribution (self-time split; faces sum to total wall) ==
interpreter self       25.58ms   38.0%
builtins                1.25ms    1.9%  (21 calls)
external commands      30.20ms   44.9%  (1 spawns)
subshell + comsub      10.22ms   15.2%  (10 frames)

== functions by self time ==
self        self%  children    calls  total      function
    7.39ms   11.0%    30.49ms       9    37.89ms  fib
    2.61ms    3.9%    49.06ms       1    51.67ms  wrap

== call tree ==
main [1] self 15.58ms children 51.67ms
  wrap [1] self 2.61ms children 49.06ms
    external:ls [1] self 30.20ms children 0ns
    $(comsub) [1] self 818.8us children 16.89ms
      fib [1] self 1.42ms children 15.47ms
        $(comsub) [2] self 2.36ms children 13.04ms
          fib [2] self 2.01ms children 11.04ms
            ...
    builtin:local [1] self 398.1us children 0ns
    (subshell) [1] self 314.6us children 211.6us
      builtin:echo [1] self 211.6us children 0ns

Collapsed stack (demo.collapsed.masked, weights = leaf self ns):

main 4999100
main;wrap 3170100
main;wrap;$(comsub) 1329600
main;wrap;$(comsub);fib 3058900
main;wrap;$(comsub);fib;$(comsub) 3413500
main;wrap;$(comsub);fib;$(comsub);fib;$(comsub);fib;$(comsub);fib 2229800
main;wrap;(subshell);builtin:echo 199700
main;wrap;external:ls 26885100

inferno-flamegraph / flamegraph.pl --countname=ns render the .collapsed file directly.

Interface

niu --profile <script> [--profile-output <f>] — engine meta flags handled before the GNU option parser (the --identity/--internal-* precedent), so LONG_OPTIONS keeps its exact GNU 5.3 table bytes and unknown-long-option rc-2 behavior. --profile consumes the script operand through the normal launcher path; --profile-output without --profile is inert. -c/-s/interactive forms are unprofiled in M1 (per ticket; interactive toggle is M2).

Notes

  • Abort attribution: the Drop guards fire on every unwind shape; return lands inside the function frame, exit inside the enclosing subshell frame (pinned by abort_forms_attribute_correctly).
  • The read suite 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.
  • No GPL code mirrored: the design (tentative-frame classification, self-time faces, comsub dedupe) is original; vendored bash anchors are citations only.

Rebased on 3c611299 (#822). Work done in the isolated worktree D:/repo/rubash-wt-815.

…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)
@caomengxuan666
caomengxuan666 merged commit ec53ea7 into master Oct 18, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the feat/815-profiler branch October 21, 2026 07:59
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.

[feature][P1] script profiler: function-level hotspot distribution — niu --profile script.sh (deterministic interpreter instrumentation)

1 participant