perf: replace ps spawn with /proc direct reads on Linux#61
Closed
tbouquet wants to merge 2 commits intograykode:mainfrom
Closed
perf: replace ps spawn with /proc direct reads on Linux#61tbouquet wants to merge 2 commits intograykode:mainfrom
tbouquet wants to merge 2 commits intograykode:mainfrom
Conversation
This was referenced Apr 20, 2026
graykode
approved these changes
Apr 21, 2026
Owner
graykode
left a comment
There was a problem hiding this comment.
lgtm — security clean (read-only /proc, libc 0.2 is the canonical FFI crate, the two unsafe sysconf calls are inherently safe). Build/clippy/tests green on macOS fallback path; Linux compile is covered by CI's ubuntu-latest.
Noticed while reviewing:
- Cargo.lock not updated. Adding
libctoCargo.tomlregenerates the lockfile on first build. Worth committing the lockfile change here so CI doesn't see lockfile churn. - CPU% semantic change is fine for the Working/Waiting threshold (which is mtime-based, not cpu-based) but is a UI-visible diff vs the old
psinstantaneous sample. You already flagged this in the PR body — just confirming it's an acceptable trade. - No unit tests for the
/proc/{pid}/statparser, even though it's the trickiest part (therfind(')')trick around comm with spaces/parens). A couple ofparse_stat()tests with crafted strings would catch future regressions cheaply.
None of these block merge.
graykode
approved these changes
Apr 21, 2026
graykode
added a commit
that referenced
this pull request
Apr 21, 2026
> **Depends on #60 + #61** - apply on top of those PRs > Part 3/3 of Phase 1 /proc optimization (#60 -> #61 -> this) ## Summary On Linux, parse `/proc/net/tcp` + `/proc/net/tcp6` for LISTEN sockets and match inodes via `scan_proc_fds()` instead of spawning `lsof -i -P -n -sTCP:LISTEN`. Eliminates the last fork+exec from the port discovery path (~400 syscalls, ~30ms). Falls back to `lsof` on macOS where `/proc` is not available. ## How it works 1. Parse `/proc/net/tcp[6]` - state `0A` = LISTEN, extract port (hex) and inode 2. Scan `/proc/[pid]/fd` symlinks via `scan_proc_fds()` (shared helper from #61) 3. Match `socket:[inode]` targets to listening port inodes 4. Build pid -> ports map ## Phase 1 complete With all three PRs merged, the result on Linux: | Spawn | Before | After | |-------|--------|-------| | `ps` | fork+exec every 2s tick | `/proc` direct read | | `lsof` (Codex) | fork+exec every tick | `/proc/pid/fd` readlink | | `lsof` (ports) | fork+exec every 5 ticks | `/proc/net/tcp` parse | | **Total** | ~1500 syscalls, ~50ms | ~200 syscalls, ~5ms | Remaining spawns: `git status` (slow tick) and `sqlite3` (OpenCode). ## Test plan - [x] `cargo clippy -- -D warnings` passes - [x] `cargo test` - 35/35 pass - [x] macOS fallback preserved via `#[cfg(not(target_os = "linux"))]`
Owner
|
Closing as superseded by #62. Since #62 was stacked on this branch, merging #62 already brought all of this PR's content into Thanks for the perf work — this and #62 together gave us the full /proc-based path on Linux. |
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.
Summary
On Linux, read
/proc/{pid}/stat+/proc/{pid}/cmdlinedirectly instead of spawningps -ww -eo pid,ppid,rss,%cpu,command. Eliminates one fork+exec per tick (~500 syscalls, ~15ms).Falls back to
pson macOS where/procis not available.What's parsed from /proc
/proc/{pid}/stat/proc/{pid}/cmdline/proc/uptimeCPU% is computed as lifetime average
(utime+stime) / elapsed_ticks * 100. This differs fromps's instantaneous sample but works correctly for abtop's Working/Waiting threshold (cpu_pct > 1.0) since long-idle processes see their average decline below 1% over time.New shared utility
Introduces
scan_proc_fds(pid) -> Vec<PathBuf>inprocess.rs- resolves all/proc/{pid}/fdsymlinks. Used by both this PR's port discovery and #60's Codex JSONL discovery, eliminating duplicated readdir+readlink loops.Dependencies
libc = "0.2"(Linux-only, forsysconfto get CLK_TCK and page size)Test plan
cargo clippy -- -D warningspassescargo test- 35/35 pass#[cfg(not(target_os = "linux"))]