proctree — the process tree, with subtree totals

proctree — the process tree, with subtree totals

proctree is a LightGUI front-end for the proctree command-line utility from util_box. It shows the live process tree with CPU and memory per branch, collapsed by the user. It is one of the 285 apps in util_boxgui, the project recreating util_box utilities as LightGUI apps, each with a screenshot and a walkthrough on the matching LightGUI doc site. The full source is apps/c/proctree/proctree.cpp in that repo.

A flat process list hides where the memory actually went. A runaway service is a dozen worker processes scattered through the list, each looking small; its subtree total is the number that says how much it really costs. Each branch line in proctree shows its subtree totals, so a runaway service shows up as one heavy branch instead of scattered leaves — the CLI's headline feature, shown here as a collapsible tree with the totals visible on every row. On this box the heaviest branch is PID 1, systemd, with a subtree RSS near 19 GB, and the top five heaviest branches are the same in the GUI and the CLI. The heaviest-branches table is the fast path: when a box is slow, the top of that table is the first place to look, and it names the branch to expand. Expand it, and the tree shows which child is carrying the weight — a worker pool, a single thread, or the service itself. Collapse it again and the tree is back to the five heavy branches. Refresh re-scans when you want a fresh picture, for instance after killing a runaway branch, and the status line reports the new process count and refresh time so you can tell the scan actually ran.

The GUI layout

proctree running under Xvfb
proctree running — the process tree with subtree totals and the heaviest-branches table, after the launch scan, captured under Xvfb.

The window is 960×680 and holds five widgets, all placed by hand in code — there is no designer file, and that is deliberate: the app is written the way the LightGUI designer's generated apps are structured, so the code below reads like generated output with the scan logic swapped in.

  • Process tree (centre): a TTreeView with one row per process, each row in the CLI's own line format — pid comm rss=..kB subtree=..kB cpu=..s — with the widget's + / - boxes for collapsing and expanding branches. Click a box, or focus the tree and use Left / Right, to collapse a branch; arrow keys move the selection. That is the CLI's "collapsed by user" behaviour, as a real widget instead of a flag.
  • Buttons (top): Refresh re-scans /proc and rebuilds the tree; Expand all / Collapse all do the same for the whole tree at once.
  • Heaviest branches table (below): a TListView with the top 5 branches by subtree RSS — the runaway-service view the CLI's subtree totals are for, at a glance.
  • Status lines: process and root-tree counts plus refresh time on top; the selected branch's full line at the bottom, updating as the selection moves.

The source

The scan logic is a direct port of src/proctree.c from util_box. It reads /proc/<pid>/stat (ppid, utime+stime) and /proc/<pid>/status (VmRSS, Uid) — the same two files, the same parse (comm taken between the parentheses, fields after the closing paren), the same sysconf(_SC_CLK_TCK) tick rate for the cpu seconds. Subtree totals are rolled up from the roots exactly as the CLI does (subrss / subcpu summed over each branch), and shown on every row, not just the leaves. Roots are processes whose parent is missing, or pid 0; on this box that is PID 1 (systemd) and PID 2 (kernel threads). Each scan is echoed to stdout in the CLI's exact line format, plus a summary line, so the GUI numbers can be checked against the CLI:

  |-- 1234 bash rss=5120kB subtree=8192kB cpu=12.34s
SCAN n processes, r roots

The differences from the CLI are the usual ones for a snapshot app. The GUI has no --user / --max-depth options — it always shows the full tree — and no --selftest flag; collapsing is done by clicking a branch's + / - box instead of a depth limit. The CLI prints one snapshot to stdout and exits; the GUI shows it in the tree and table, echoes the same snapshot to stdout, and stays open until the window closes, with Refresh to re-scan. The window scans /proc once on launch — the CLI is a one-shot snapshot too — so there is nothing to operate until you want a fresh scan.

Building and running it

g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets -O2 -Wall proctree.cpp -lX11 -o build/proctree

That is the one g++ line from the Makefile — make runs it. It builds clean with no warnings, and needs g++, libx11-dev, and the LightGUI checkout (header-only widgets on raw X11). Under a real X server, or under Xvfb:

Xvfb :97 -screen 0 1024x768x24 &
DISPLAY=:97 ./build/proctree

The CLI equivalent is proctree, which prints one snapshot and exits. The GUI is that snapshot, kept on screen with a Refresh button.

proctree

Verified against the CLI

Verified against the CLI on the same box: the GUI's stdout echo and a proctree run back-to-back, about a second apart, were compared field by field — 6/10 strict checks pass, and the 4 that fail are all live-process churn between the two scans, not GUI errors. The process count differed by 2 (CLI 465, GUI 463) because two short-lived processes existed only during the CLI scan; the PID sets differ by those same two PIDs; 5 comm mismatches are all kernel worker threads, which the kernel renames as they switch workqueues — kworker/6:3-events in one scan against kworker/6:3-mm_percpu_wq in the other, same PID, RSS 0, CPU within 0.1 s — so no two scans taken a second apart agree on those names; and RSS matched exactly on 446 of 463 common PIDs, with a maximum difference of 2,344 kB. On the 346 non-kworker processes present in both scans, the agreement is exact: comm 346/346, depth 346/346, CPU 346/346, RSS 329/346 exact and 344/346 within 1 MB. PID 1's subtree RSS agrees within 1% (CLI 18,983,976 kB, GUI 18,986,008 kB, relative difference 0.0001), the heaviest branch is PID 1 in both, and the top-5 heaviest branches are the identical PIDs. The CLI's own --selftest also passes. See the util_boxgui repo for the other 284 apps.

Source code: git.lightgui.com/thinkmelt/lightgui