procstate — process counts by state, graphed live

procstate — process counts by state, graphed live

procstate is a LightGUI front-end for the procstate command-line utility from util_box. It counts processes by state — R running, S sleeping, D disk sleep, Z zombie, T stopped — and graphs them live. 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/procstate/procstate.cpp in that repo.

A total process count hides the interesting part. A box with 400 processes is fine if 380 of them are sleeping; it is in trouble if 40 of them are stuck in D, uninterruptible disk sleep, or if zombies are piling up because a parent is not reaping its children. The state counts are the numbers that say which of those is happening. On this box the table is dominated by sleeping processes — roughly 330 S, roughly 120 OTHER, mostly kernel idle threads in state I, roughly 20 R, and the occasional 1 in D or Z as short-lived processes come and go. A growing D count means IO trouble; a growing Z count means a parent is not reaping children — exactly like the CLI. The OTHER bucket deserves a word of its own: it holds every state the five named buckets do not, and on a Linux box that is mostly state I, the kernel's idle threads. A box with 120 OTHER is not a box with 120 unknown processes — it is a box whose kernel threads are parked, which is normal. What matters is the shape of the table over time, which is why the GUI keeps the running total range (min .. max) in the table: a total that creeps upward sample by sample is a leak or a fork loop, while a total that jumps and falls back is a batch job starting and finishing. The gauge's 0–1000 range is generous on purpose — a healthy server sits in the low hundreds, and the needle has room to move before it runs out of dial.

The GUI layout

procstate running under Xvfb
procstate running — the per-state history lines and the state table after 8 seconds of live samples, captured under Xvfb.

The window 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 sampling logic swapped in.

  • Total gauge (top left): an InstAnalogGauge needle dial showing the current total process count, range 0–1000.
  • History graph (top right): an XGraph line chart of the per-state counts over the last 60 samples — R running in green, S sleeping in blue, D disk sleep in amber, Z zombie in red, T stopped in purple, OTHER in grey. This is the CLI's end-of-run sparkline, as a live per-state chart.
  • Stats table (below): a TListView with the current count for each state, the total process count, the sample count, and the running total range (min .. max).
  • Status lines: sample count and interval on top, last-update time and the full per-state breakdown at the bottom, and the note line under the title carrying the CLI's own messages — a D-state warning when any process is in uninterruptible sleep, and a zombie warning when a parent is not reaping children.

The source

The sampling logic is a direct port of src/procstate.c from util_box. Each sample is one scan of /proc/PID/stat per second, taking the state character after the closing ) of the comm field — the same parse and the same R/S/D/Z/T/other buckets as the CLI, at the same 1-second default interval. The CLI keeps up to 512 totals and draws a Unicode sparkline of the total at the end of the run; the GUI keeps the same 60-sample ring the other monitor apps use and draws every state's history as an XGraph line series, with the total range shown in the table. Each sample is echoed to stdout in CLI form, so the GUI numbers can be checked against the CLI:

FRAME n R S D Z T OTHER TOTAL

The differences from the CLI are the usual ones for a monitor app. The GUI has no --interval / --seconds / --count options — fixed 1-second interval, runs until the window closes. The CLI prints one row per interval to stdout; the GUI shows it in the gauge, graph, and table and echoes the same row to stdout. There are no controls to operate: the window refreshes itself once a second from the moment it opens, and closing the window quits.

Building and running it

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

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 :202 -screen 0 1024x768x24 &
DISPLAY=:202 ./build/procstate

The CLI equivalent is procstate --count 6 --interval 1, which prints six rows, one per second. The GUI is those rows, kept live.

procstate --count 6 --interval 1

Verified against the CLI

Verified against the CLI on the same box: the GUI and procstate --count 6 --interval 1 run concurrently, frame-by-frame (GUI frame N against CLI row N-1, same wall-clock second), agree on all 42/42 state-count checks — 6 samples by 7 columns: R, S, D, Z, T, OTHER, TOTAL — each within plus or minus 5, with a maximum deviation of 4, on R in one sample. The residual jitter is real process churn: short-lived processes appear and disappear between the two scans a second apart, and D and Z flip between 0 and 1 in both logs independently. The note line carries the CLI's own warnings, a D-state warning when any process is stuck in disk sleep, or a zombie warning when a parent is not reaping children. See the util_boxgui repo for the other 284 apps.

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