Repository navigation
native: scan the process table once per tick, not once per pane - #134
Conversation
Linux: every pane's reader read /proc/<pid>/stat of every process in the
PID namespace about 2.5 times a second (8 panes, 275 processes: 18% of a
core on an Intel N100). Two changes, finding the same members:
1. A cheaper scan. Each stat is read into a reused buffer and parsed in
place (bytes, no String, no Vec of fields); the children and the
group/session indexes are built once per snapshot instead of filtering
the whole map per call.
2. One snapshot per tick. A holder-wide procs::Shared hands every pane's
periodic look the same scan if it began less than 250 ms ago and not
before the pane was spawned. The stop paths (soft_stop,
await_tree_gone, release) still take their own fresh Tree::refresh,
and signals still re-read the pid's start time. Lock order: a pane's
tree, then Shared's lock, which is a leaf. macOS keeps its per-question
kernel calls (Shared is a no-op there).
One difference in what is found: a process whose command name is not
valid UTF-8 was invisible to the old read_to_string-based parser (so it
was never in a tree and never signalled); the byte parser sees it.
Docs: operations.md says the periodic look may use a 250 ms old read.
Verified (Linux x86_64 build host, no cargo on the laptop):
- cargo fmt --check, clippy --all-targets -D warnings: clean.
- cargo test, whole suite, 2 runs at the final tree: 981 lib + all
integration tests pass, 0 failed (3 more runs before the last
test-only fixture fix, same). No existing assertion was edited.
- New tests: parser on a comm with spaces/parentheses, 100 KB comm, non
UTF-8 comm, zombie, cut lines, a real zombie and a pid that vanished
between readdir and read; fixture tree finds exactly its 4 members via
refresh and refresh_shared; age limit and not_before; 8 trees share one
scan; 8 panes cost <= 14 scans in 1.5 s; stop kills a child the shared
snapshot missed.
- Equivalence: while the old text parser and BFS were kept as a test
oracle, the new snapshot matched it (start, children, peers on the
fixture, start of every common process, same member set); the oracle is
deleted in this commit and the fixture tests assert explicit members.
- Mutation check: soft_stop/await_tree_gone/release switched to
refresh_shared makes a_stop_scans_afresh_and_ends_a_child_the_shared_
snapshot_missed fail ("outlived kill: the stop used a stale snapshot");
reverted.
- 50 runs of the process, stop and contract tests with 4 busy loops: 50/50.
- Idle holder, 8 panes, 300 s, ps %cpu: base 15.2 (18.05 earlier run),
new 1.8. Front restart, 8 full panes, 6 runs each: wall to quiet
0.55-0.74 s new, 0.51-0.68 s base (not changed; neither reproduces the
3.1-3.9 s of the report).
- macOS not built here (data volume has under 5 GiB free): hosted CI.
macOS clippy -D warnings flagged them as dead code (their only callers are Linux-only tests). Checked: cargo fmt --check and clippy --all-targets -D warnings clean on a Linux x86_64 host and on macOS; native::host tests pass on both.
A /proc scan that cannot list the directory or read a process (for example when file descriptors run out) is now marked incomplete. The shared snapshot does not cache an incomplete scan, and Tree::apply keeps each member it cannot check instead of pruning it. A member whose pid shows another start time is still dropped. Items addressed: - (a) Degraded scans: Snapshot::build/take track completeness; Shared::snapshot_at caches only complete scans; Tree::apply prunes only on a complete snapshot. Tests: a_degraded_snapshot_prunes_no_pane_member, a_degraded_snapshot_is_not_cached. - (b) Scan count: eight_pollers_take_exactly_one_scan_per_window asserts exactly six scans in 1.5 s for eight 250 ms pollers on a supplied clock, and a_snapshot_is_reused_until_the_age_limit_and_no_longer pins the 250 ms limit. The real-PTY eight_panes_share_one_scan_per_tick now asserts 2 to 7 scans, because panes started together cluster on the 200 ms reader wake and gave 3. - (c) Docs: adr/native-core.md and operations.md state the Linux window as 500 ms nominal and at most about 700 ms (macOS about 450 ms), including the owner-decision paragraph. - (d) CHANGELOG: latency change, and processes whose command name is not UTF-8 are now tracked. - (f) Removed the unused Default impl for Shared, documented that the Linux snapshot ignores leader and tty (parameters kept: the signature is shared with macOS), and corrected the parse_stat and read_loop comments. Tests (cargo fmt --check; cargo clippy --locked --all-targets -- -D warnings; cargo test --locked): - macOS build host: 1019 passed, 0 failed, 6 ignored (12 test binaries) - Linux build host: 1039 passed, 0 failed, 6 ignored (12 test binaries) Refs #3
|
Important CHANGES REQUESTED What This PR DoesOn Linux, every native pane reader used to scan all of How It Works
Findings
Finding Details🟡 F1: README describes the obsolete detection windowThe CHANGELOG, ADR and Requested change: update both README passages to the Linux nominal/max figures and the macOS figure, or drop the timing from the README and link to the Known limits in 🟡 F2: The real-PTY scan-count test is timing-sensitiveThe test sleeps 700 ms, then 1.5 s, and asserts The exact invariant is already pinned by the supplied-clock test Requested change: keep the supplied-clock test as the invariant. For the real-PTY test, assert only what proves sharing without a wall-clock floor (for example 🟡 F3: Parse failure should not count as "gone"
Requested change: map a parse failure to 🟡 F4: Queued readers can serialize full rescans
Requested change: let a reader that waited for the lock accept the scan that finished while it waited. An incomplete snapshot can safely be reused for the rest of that window, because 🟡 F5: Document the memory and degraded-scan behaviour, and qualify the numbers
🟢 F6: Stop paths stay freshAll three stop call sites use 🟢 F7: Degraded-scan semantics are correctOnly errors other than 🟢 F8: Deterministic testsThe supplied-clock seam in Baseline Check
Points Raised and Resolved During Review
What's Good (🟢)
5️⃣ Three Reasons We Might Not Need This PR
Suggestions, not blocking: the non-UTF-8 command-name fix is independent of the sharing design and could be split out, so it survives if the perf change is ever reverted. Making stop paths structurally unable to reach |
… the window A /proc stat line that cannot be parsed is now a failed read, so the snapshot is incomplete and keeps the member. Before, it was read as an exited process, and a complete snapshot could drop a live member. A zombie or exited process is still treated as gone. Items addressed (from the bot review on the previous head): - Unparseable stat line: parse_stat returns Result; an error maps to StatRead::Failed. Test: an_unparseable_line_is_a_failed_read_not_a_gone_process. - README: the periodic-look and fast-escape passages state the Linux nominal and worst-case bounds and the macOS bound. - docs/operations.md: the snapshot memory grows with the host process count, and a degraded scan keeps every member it cannot check. - Real-PTY scan count: the test asserts 1 to 7 scans in 1.5 s (at most one per 250 ms window). The exact count stays in eight_pollers_take_exactly_one_scan_per_window. - CHANGELOG: removed the uncontrolled CPU figures. - Not changed: readers queue on the shared lock when a scan is incomplete or takes longer than 250 ms. Reusing an incomplete snapshot would reverse the approved degraded-scan rule, so this is recorded as a follow-up. Tests (cargo fmt --check; cargo clippy --locked --all-targets -- -D warnings; cargo test --locked): - macOS build host: 1019 passed, 0 failed, 6 ignored (12 test binaries) - Linux build host: 1040 passed, 0 failed, 6 ignored (12 test binaries) Refs #3
Fixes for the second council pass on aeb7fc2. - Degraded scans: Snapshot::take reads through take_in(root). Two new Linux tests make a real failure: a listing that cannot be read, and a stat path that cannot be read (EISDIR). Both leave the snapshot incomplete, and both prune no pane member. - Exact scan count: eight_panes_share_one_scan_per_tick replays every look the real pane readers made against the rule of one scan per 250 ms window (a literal, so changing SNAPSHOT_MAX_AGE fails it), and asserts the scan count that rule predicts for those looks. The 700 ms sleep before counting is gone, so the pane-creation burst is measured. - Linux bound: docs/adr/native-core.md, docs/operations.md, README.md and CHANGELOG.md state the about 700 ms worst case with its condition: each scan must finish within the 250 ms age limit. A longer scan, or a wait on the shared lock, adds its own time. The nominal figure is 500 ms. The bare "nominal" wording is replaced with the stated bound. - Shared::snapshot doc comment: an incomplete scan does not replace an older complete one, so a reader may still reuse that older one. - CHANGELOG: a process whose command name is not valid UTF-8 is now trackable, so it is found and signalled on stop. - Left unchanged: the leader and tty parameters of the Linux Snapshot::take. The signature is shared with macOS, where both are used. Tests (cargo fmt --check; cargo clippy --locked --all-targets -- -D warnings; cargo test --locked --no-fail-fast): - macOS build host: 1019 passed, 0 failed, 6 ignored. - Linux build host: 1042 passed, 0 failed, 6 ignored. Refs #3
…ed cost Refs #3 Addressed from the pass-3 review of 4f56bc1: - Snapshot age (procs.rs, Linux Shared): the clock is read after the shared lock is taken, in reuse_or_take, so a reader that waited for another pane's scan judges the snapshot when it uses it. snapshot_at, the supplied-clock seam, is now test-only. Without this gate the lib build fails under -D warnings on Linux. - Docs (docs/operations.md, docs/adr/native-core.md): the memory sentence gives the measured values, about 720 KiB more than the isolated base run and about 864 KiB more than the concurrent base run, at 302 processes. The degraded-scan rule now states its cost: while a read keeps failing, no scan is kept, so each pane's look takes its own scan. - Formatting (README.md, CHANGELOG.md, docs/operations.md, src/native/host/ local.rs): over-long lines rewrapped; a dangling line fragment in README.md removed. Unchanged: the test that replays the scan window (eight_panes_share_one_scan_ per_tick) keeps its complete-scan assumption, which holds on a healthy /proc. Tests, run on the head of this commit: - macOS build host (Apple Silicon): cargo fmt --check, cargo clippy --locked --all-targets -- -D warnings, cargo test --locked --no-fail-fast. Result: 1019 passed, 0 failed, 6 ignored. - Linux build host (x86_64): the same three commands. Result: 1042 passed, 0 failed, 6 ignored, including the Linux-only tests that read /proc.
Refs #3
On Linux, the holder scans the process table once per 250 ms tick for all of its panes, instead of once per pane. Each scan is also cheaper:
/proc/<pid>/statis read into a reused buffer and parsed from bytes, and the child and process-group indexes are built once per snapshot. macOS behaviour is unchanged.Measured head
The numbers below were measured at commit
a10d7c5, the commit that introduced the shared scan. They were not re-measured on later commits. Commits05f27b0,aeb7fc2and4f56bc1changed the degraded-scan path and its tests, and84ffce4changed the age check and the docs.fcc8434changed only test gating.Numbers
Linux build host: Intel N100, 4 cores, 8 panes, 302 processes in the namespace. Idle holder, 300 s,
ps -o %cpu,rssevery 15 s (cumulative %cpu).The base and this branch ran at the same time by accident (two tool calls overlapped), so each one loaded the other. Treat the pair as a rough comparison, not a controlled one.
The "well under 1 percent" target is not met. One scan per 250 ms tick sets a floor of about 1.8 percent on this host. Going lower needs a back-off when quiet (recommendation 3 in the measurement notes, not in this PR).
Base figure versus the recorded baseline. The baseline record gives 18.05 % mean for the same base commit, 0d62828. That run was measured alone, with 342 processes in the namespace, 19 of them unrelated openab-shepherd hold processes from another job. The pair above ran with 302 processes. Scaling 18.05 by 302/342 gives about 15.9 %, near the 14.9 % measured here. About one point of the gap is not explained by the records, and it was not re-measured. Compare this branch against 14.9 %, because it comes from the same concurrent pair as the 1.86 % figure. The 18.05 % figure comes from a different table size and is not comparable.
Peak RSS. The base peaked at 4432 KiB and this branch at 5296 KiB, a difference of +864 KiB (about +20 %) in the same concurrent pair. The isolated baseline recorded 4576 KiB for the same base commit, so against it the increase is +720 KiB. The likely cause is the retained snapshot plus the child and peer indexes built over the whole process table. Those parts were not measured separately.
Front restart, 8 full panes, wall time from SIGUSR2 until the new front's CPU is quiet, 3 runs:
Runs 1 and 2 match between the two rows at the reported 0.01 s resolution. The measurement record does not say whether the two rows came from separate harness invocations, so that match is not explained. The isolated baseline gave 0.53, 0.70 and 0.69 s. The restart was not re-measured on the later commits.
Degraded scans
A
/proclisting that fails, a read that fails (for example, out of file descriptors), or a stat line that cannot be parsed marks the scan incomplete. An incomplete scan is not cached and prunes no member: a member is dropped only when its pid shows another start time, or when a complete scan no longer lists it. A zombie or exited process is still treated as gone. While a read keeps failing, no scan is kept, so each pane's look takes its own scan, as it did before the shared scan. The tests use real failures: a missing listing, and a stat path that cannot be read.Detection window
On Linux, a descendant that detaches is seen within about 500 ms in the nominal case (250 ms snapshot age plus a 250 ms look interval), and at most about 700 ms provided each scan finishes within the 250 ms age limit. The snapshot age is read after the shared lock is taken, so a reader that waited for another pane's scan judges the snapshot when it uses it. A longer scan adds its own time. On macOS the bound is about 450 ms. The ADR, the operations guide, the README and the CHANGELOG state these bounds.
Processes whose command name is not valid UTF-8 are now trackable, so they are found and, on
remove_paneor shutdown, signalled. The CHANGELOG records this.Stop path
soft_stop,await_tree_goneandreleasetake a fresh scan, and signals still re-read the pid and its start time before sending. Only the periodic look may use a snapshot up to 250 ms old.Tests
Commands, run on the head of this PR:
cargo fmt --check,cargo clippy --locked --all-targets -- -D warnings, andcargo test --locked --no-fail-fast./proc.eight_panes_share_one_scan_per_tickasserts the exact scan count that the one-scan-per-window rule predicts.eight_pollers_take_exactly_one_scan_per_windowpins six scans in 1.5 s.a_snapshot_is_reused_until_the_age_limit_and_no_longerpins the 250 ms age limit.Not in this PR
Recommendation 3 (back-off when quiet) and recommendation 4 (
/procchildren lookup) remain options. Readers that wait on the shared lock rescan one after another when a scan is incomplete or takes longer than 250 ms, and each holds its pane's tree lock meanwhile. Reusing an incomplete snapshot would be a cache change, so that stays a follow-up. The CPU cost of teardown, when stop paths scan the whole table, is not measured.