Skip to content

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
graykode:mainfrom
alebiscotto:feat/tree-memory-tmux
Open

alebiscotto wants to merge 2 commits into
graykode:mainfrom
alebiscotto:feat/tree-memory-tmux

Conversation

@alebiscotto

@alebiscotto alebiscotto commented Nov 26, 2026 •

Copy link
Copy Markdown

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_mb in --json used 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.

  • A session's memory is now the sum over the agent process and all its descendants, the same tree as the children panel.
  • On Linux each process counts Pss + SwapPss from /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 whose smaps_rollup cannot be read falls back to its RSS. Other platforms sum RSS over the tree; nothing else changes there.
  • Cost: smaps_rollup is 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.
  • Nested sessions (e.g. codex exec launched 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.
  • Codex threads served by a shared app-server keep 0 as before (owns_process_tree).

Measured on a Linux host with 28 Claude Code sessions (317 processes in their trees, swap full):

total
before (agent process RSS) 6.7 GiB
RSS summed over the trees 15.9 GiB of RAM alone (shared pages counted in full for every process)
PSS over the trees 9.1 GiB
PSS + SwapPss over the trees (this PR) 15.6 GiB

Every per-session figure matches an independent script that sums Pss + SwapPss from /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

  • One tmux list-panes -a call 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".
  • 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).
  • Exposed as AgentSession::tmux, as an optional Tmux column showing session:window_name (the window index when the name is empty), and as tmux_session, tmux_window_index, tmux_window_name, tmux_pane_index in --json (null outside tmux). No existing field is renamed or removed.
  • Like the other optional columns the Tmux column is width-gated: shown only when some session has a location and the table is at least 140 columns wide. Narrower tables, 80 columns included, render exactly as before (there is a test for the 80-column case).
  • Window names are user text that tmux prints raw, tabs and newlines included, so each field is preceded by a per-call nonce and the output is split on it; a name cannot fake a field or another pane's record. Names go through sanitize_terminal_text.
  • The call is bounded to 2 s: on expiry the tmux client is killed and reaped and the previous pane list is kept, so a tmux server that does not answer cannot freeze the UI.

Validation

  • cargo clippy --all-targets -- -D warnings and cargo test on each commit separately (232 and 248 tests).
  • New unit tests: smaps_rollup parsing, 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.
  • On Linux with tmux 3.6: all 28 sessions get the same location as tmux list-panes -a, also with TMUX unset 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:

  • AgentSession gains two public fields (owns_process_tree, tmux) and model::TmuxLocation is new.
  • The children panel still shows per-process RSS, so its rows can add up to more than the session's MEM. I left it alone to keep the change small; it could read PSS from the same cache.
  • cargo fmt --check reports the same pre-existing hunks as on main and nothing in the new code.

Closes #189

…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.
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.

Per-session memory: count the agent's whole process tree (PSS on Linux), and show its tmux window

1 participant