runq — run queue and load average, live

runq — run queue and load average, live

runq is a LightGUI front-end for the runq command-line utility from util_box. It watches the run queue — how many threads are runnable and waiting for CPU — and the load average, and shows them as a needle gauge, two history graphs, and a stats table. 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/runq/runq.cpp in that repo.

A run queue that stays above the core count means threads are waiting for CPU — the box has more work ready than it has cores to run it on, and everything queues. Load average says the same thing in a different unit, but the run queue is the direct count: runnable threads right now. On this box — 8 cores, heavily loaded by parallel batch workers while the screenshot was taken — the 1-minute load sat around 23, so the note fires, the gauge reads roughly 23 runnable, and both graphs show real variation instead of a flat line. That is the honest result for a busy box, not a broken reading: the gauge range starts at twice the core count and grows with the observed maximum run queue, so the needle never pegs at the maximum.

The GUI layout

runq running under Xvfb
runq running — load around 23 on 8 cores, the queueing note firing, after 6 live samples under heavy load, captured under Xvfb.

The window holds six 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.

  • Run queue gauge (top left): an InstAnalogGauge dial showing the current runnable-thread count. Its range starts at twice the core count and grows with the observed maximum run queue, so the needle never pegs at the maximum.
  • Load graph (top right): an XGraph line chart of the 1-minute load over the last 60 samples — the CLI's load1 history sparkline, as a real chart.
  • Run queue graph (middle): an XGraph line chart of the runnable-thread count over the last 60 samples — the CLI's runq history sparkline, as a real chart.
  • Stats table (below): a TListView with the running numbers — load 1/5/15 min, runnable / total threads, procs_running, procs_blocked, cores, average and maximum load1, maximum run queue, sample count.
  • Status lines: sample count and interval on top, last-update time with current load and run queue at the bottom, and the note line under the title showing the CLI's own message — a warning when the 1-minute load exceeds the core count ("threads are queueing for CPU").

The two thread counts in the table answer different questions, and it is worth keeping them apart. procs_running is the threads that are runnable or running right now — the queue itself. procs_blocked is the threads waiting on IO, which do not consume CPU but do hold the work they belong to. A high running count with a low blocked count is a CPU-bound box; a high blocked count with a low running count is an IO-bound box, and adding CPU will not help it. The table carries both, with the load averages, so the two causes stay distinguishable at a glance instead of collapsing into one number.

The source

The sampling logic is a direct port of src/runq.c from util_box. Each sample is one read of /proc/loadavg (1/5/15-minute load plus running/total thread counts) and one read of /proc/stat (procs_running / procs_blocked) — the same two reads, the same field order, the same 1-second default interval as the CLI. Cores are counted from /proc/cpuinfo exactly as the CLI counts them. The CLI keeps a 60-sample ring and draws two Unicode sparklines; the GUI keeps the same 60-sample ring and draws it as two XGraph line series. Each sample is echoed to stdout in CLI form, so the GUI numbers can be checked against the CLI:

FRAME n load: ... runq: ... procs_running: ... blocked: ... cores: ...

One fix in this app is worth recording, because the first screenshot caught it. Load and run queue are unbounded, and XGraph draws out-of-range points outside the plot area — they land on neighbouring widgets — so a fixed scale would clip under heavy load. The first screenshot caught exactly that, with load 22.2 against a 0–16 scale, and the graphs were switched to auto scale with set_auto_y(true), the pattern loadhist / iolat / diskrate already use. The differences from the CLI are the usual ones for a monitor app: the GUI has no --interval / --count / --seconds options — fixed 1-second interval, runs until the window closes. The CLI prints one frame per interval to stdout; the GUI shows it in the gauge, graphs, and table and echoes the same frame 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 runq.cpp -lX11 -o build/runq

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/runq

The CLI equivalent is runq --count 5, which prints five frames, one per second. The GUI is those frames, kept live.

runq --count 5

Verified against the CLI

Verified against the CLI on the same box: 25/25 scripted checks pass — CLI --count 5 (5 frames, exit 0), --selftest (3 samples, exit 0), --help (exit 0), unknown option (exit 2); every CLI and GUI frame field (load 1/5/15, runnable, total threads, procs_running, blocked, cores = 8) within tolerance of direct /proc/loadavg + /proc/stat reads taken during the run, with a worst CLI delta of 0.00 and a worst GUI delta of 1.00 on the fast-moving thread counts; GUI frame numbers 1..N in order; and the queueing note fires in the CLI while the note condition (1-minute load above cores) holds for every GUI frame. The screenshot's table was also cross-checked against the GUI's own frame log at capture time (6 samples): average load1 23.39, maximum load1 23.70, maximum run queue 26, and the current load / runnable / procs_running / blocked / cores row all match the 6th frame exactly. See the util_boxgui repo for the other 284 apps.

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