Repository navigation
feat: session memory over the whole process tree (PSS + swap on Linux), and the tmux window of each session - #193
Open
alebiscotto wants to merge 2 commits into
Open
alebiscotto wants to merge 2 commits into
alebiscotto wants to merge 2 commits into
Conversation
…ap on Linux) The MEM column, the header Σ total and `mem_mb` in `--json` used the RSS of the agent process alone, ignoring the MCP servers, shells and helpers each agent spawns. A session's memory is now the sum over the agent process and all its descendants, the same tree as the children panel: what closing the session gives back. On Linux each process counts its PSS in RAM and in swap (`Pss:` + `SwapPss:` of /proc/<pid>/smaps_rollup). PSS splits pages shared between processes (node runtimes, shared libraries) among them instead of counting them in full for each, and the swap share matters on a loaded host, where much of an idle agent can sit in swap. When a process' smaps_rollup cannot be read (no permission, gone) its RSS is used. Other platforms sum RSS over the tree. smaps_rollup is costlier than stat (the kernel walks every mapping), so it is read only for pids in session trees: pids not seen yet at once, so a new session never shows 0, and every known pid again once per slow period (5 ticks). Those re-reads are spread over the period's ticks by pid, so no single tick pays for all of them. Like the git stats, the memory is filled in by MultiCollector; collectors report whether a session owns its process tree (`owns_process_tree`, false for Codex threads served by a shared app-server, which keep 0 as before). A session can run inside another one's tree (e.g. `codex exec` launched from Claude Code's Bash tool). Each row still shows its own whole tree, but the aggregate skips sessions whose pid is a descendant of another session, so every process is counted once in the total. Demo sessions' memory now includes their listed children.
With many agents spread over tmux windows, abtop could not tell which window a session runs in, e.g. to see what each window costs next to the process tree memory. On each slow tick, one `tmux list-panes -a` call lists every pane of the default server (or the one in $TMUX), so it also works when abtop runs outside tmux; no tmux binary, no server or an error just mean no location. The pane list is cached, and every tick each session is mapped to the pane whose process is its agent pid or an ancestor of it (the descent the tmux jump already uses), so a session started in a known pane is placed at once. Session and window names are user text and go through sanitize_terminal_text. tmux passes window names through raw, tabs and newlines included, so no separator character is safe: a name could fake a field or another pane's whole record. Each field is therefore preceded by a nonce made for that call (16 hex digits from the randomly keyed std hasher), and the output is split on it, five fields per pane; a name cannot hold a nonce it never saw. Unlike tabs, which tmux turns into `_` for a client whose locale is not UTF-8, the nonce also survives tmux's sanitizing of its output. The call gets 2 s: a tmux server that does not answer would otherwise freeze the tick, and with it the TUI and its keys. On expiry the tmux client is killed and reaped, and the panes listed before are kept rather than dropping every location. Its output goes to a temporary file, so a listing longer than a pipe buffer cannot stall it. The location is exposed as `AgentSession::tmux` (session name, window index and name, pane index), as an optional Tmux column in the sessions table showing `session:window_name` (the window index when the name is empty), and as `tmux_session`, `tmux_window_index`, `tmux_window_name` and `tmux_pane_index` in `--json`, null outside tmux. Like the other optional columns, the Tmux column is width-gated: it is shown only when some session has a location and the table is at least 140 columns wide, since from 110 columns on the existing columns already fill the row. Narrower tables, 80 columns included, are unchanged. Demo sessions launched from the CLI get a location.
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.
Implements the two proposals of #189, one commit each. The second builds on the first, so they are in one pull request; I can split them if you prefer to take them separately.
1. Session memory = the whole process tree (PSS + swap on Linux)
The MEM column, the header Σ and
mem_mbin--jsonused the RSS of the agent process alone. The MCP servers, shells and helpers each agent spawns (about 10 processes per session here) were listed in the children panel but not counted.Pss+SwapPssfrom/proc/<pid>/smaps_rollup: pages shared between processes (node runtimes, shared libraries) are split among them instead of being counted in full for each, and the part of an idle agent that sits in swap is counted too. A process whosesmaps_rollupcannot be read falls back to its RSS. Other platforms sum RSS over the tree; nothing else changes there.smaps_rollupis read only for pids in session trees. New pids are read at once (a new session never shows 0), known pids are re-read once per slow period (5 ticks), and those re-reads are spread over the ticks by pid so no single tick pays for all of them.codex execlaunched from Claude Code's Bash tool): each row shows its own whole tree, and the aggregate skips sessions whose pid is a descendant of another session, so every process is counted once in the total.owns_process_tree).Measured on a Linux host with 28 Claude Code sessions (317 processes in their trees, swap full):
Every per-session figure matches an independent script that sums
Pss+SwapPssfrom/proc(25 of 28 exact, the rest within 3 MiB because processes changed between the two runs). One full refresh of the 317 pids takes about 0.46 s on that (loaded) host; spread over the 5 ticks it is about 0.1 s per tick.2. The tmux window each session lives in
tmux list-panes -acall on the slow tick lists every pane of the default server (or the one in$TMUX), so it also works when abtop runs outside tmux. No tmux binary, no server or an error just mean "no location".AgentSession::tmux, as an optional Tmux column showingsession:window_name(the window index when the name is empty), and astmux_session,tmux_window_index,tmux_window_name,tmux_pane_indexin--json(null outside tmux). No existing field is renamed or removed.sanitize_terminal_text.Validation
cargo clippy --all-targets -- -D warningsandcargo teston each commit separately (232 and 248 tests).smaps_rollupparsing, the tree sum with the RSS fallback, the spread re-reads, the nested-session aggregate, the tmux output parsing (including forged records inside window names), the bounded command (timeout, failure, output larger than a pipe), the column at wide and 80-column widths, the JSON fields.tmux list-panes -a, also withTMUXunset and with a C locale; no server and no tmux binary give no location and nothing on stderr; a stopped server returns after 2 s and leaves no client behind.Not verified: macOS and Windows builds (I only have a Linux toolchain; the non-Linux paths are the RSS sum and "no location" when tmux cannot be run), tmux versions other than 3.6 (the format uses only plain variables, no modifiers), and a live nested session (unit-tested only).
Notes for the review:
AgentSessiongains two public fields (owns_process_tree,tmux) andmodel::TmuxLocationis new.cargo fmt --checkreports the same pre-existing hunks as onmainand nothing in the new code.Closes #189