
Recall first from rsync, snapshots and safe scheduling
- rsync -a preserves metadata; --link-dest hard-links unchanged files into cheap snapshots.
- Lock scheduled jobs with flock; cron can overlap runs, systemd timers do not.
- A trailing slash on the source copies contents, not the directory; rehearse --delete with --dry-run before trusting it.
Why systems get slow, and how to prove it
3 min readThe previous lesson set up scheduled jobs with cron and systemd timers, which can themselves cause slowness at odd hours. This lesson answers why systems get slow and how load average and CPU tools prove the cause.
"The board is slow." That sentence starts most troubleshooting sessions, and it contains almost no information. Slow at what? Since when? Under which load? Your job is to replace the feeling with numbers. A the one resource that limits how fast the whole system can go is like the narrowest pipe in a plumbing system: everything upstream of it queues up.
The USE method, popularised by performance engineer Brendan Gregg, gives you three questions per resource. Utilization: how busy is it, as a fraction of its capacity? Saturation: how much work is queued, waiting for it? work that has arrived but cannot be served yet, so it waits in a queue is often the more honest signal: a disk can look only half busy while requests wait milliseconds in line. Errors: is it reporting failures, such as dropped packets or I/O errors?
Run the checklist over the four resources that matter on almost every Linux machine:
| Resource | Utilization | Saturation | Errors |
|---|---|---|---|
| CPU | per-core busy time (top, mpstat) |
tasks waiting for CPU (vmstat r) |
machine checks in dmesg (rare) |
| Memory | used versus available (free) |
swap activity (vmstat si/so) |
out-of-memory kills in the kernel log |
| Disk | busy time (iostat %util) |
queue depth and await (iostat -x) |
I/O errors in dmesg |
| Network | throughput versus link speed | drops, retransmits, queue backlog | ip -s link error counters |
You do not need exotic tools. Most of the evidence lives in a virtual filesystem where the kernel exports live statistics as plain files, which means the same commands work on a server, a container and a tiny board. The sysstat package adds mpstat, pidstat and iostat; install it with sudo apt install sysstat iotop where it is missing.
This lesson also connects to the previous one. The backup job you scheduled with cron or a timer can itself be the culprit: an rsync run or a deep find walk shows up as disk load at 02:30. By the end you will know how to catch it, and how to put a ceiling on it so it never starves the service it shares the board with.
Load average: a queue length, not a percentage
4 min readStart with the command everyone runs first:
uptime
nproc
Typical output (yours will differ):
14:22:03 up 6 days, 3:41, 2 users, load average: 0.42, 0.35, 0.31
nproc prints the number of processing units available, for example 4. The an exponentially smoothed count of tasks that are running, ready to run, or stuck in uninterruptible wait is the three numbers at the end: one, five and fifteen minute averages. Read them as a trend. If the one-minute value is far above the fifteen-minute value, load is rising right now; if it is far below, the spike is already over.
The rule of thumb: compare load with nproc. On a four-core board, a load of 4 means the CPUs are, on average, fully occupied with no queue. A load of 8 means roughly twice as much work as the CPUs can absorb, so tasks wait. A load of 0.4 on four cores is a quiet machine. Load is a count, so it has no upper bound and it is never a percentage.
1Read the raw source
The same numbers live in a file, plus two extras:
cat /proc/loadavg
Typical output (yours will differ):
0.42 0.35 0.31 2/612 4251
The fourth field is 2/612: two currently runnable scheduling entities out of 612 that exist. The fifth is the most recently created process ID. Scripts that poll /proc/loadavg are cheap; the kernel keeps it current.
2Explain high load with an idle CPU
Here is the surprise that catches everyone. Linux counts not only runnable tasks but also tasks in uninterruptible sleep, the D state: a task waiting inside the kernel for something it cannot be interrupted from, most often disk I/O (or an NFS server that stopped answering). A board whose SD card has stalled can show a load of 20 while top reports the CPU almost entirely idle. The tasks are not computing; they are queued on storage.
So high load alone never proves a CPU problem. Pair it with the CPU breakdown in the next section: high load plus high us/sy means compute pressure; high load plus high wa and idle time means I/O pressure.
Going deeperHow the kernel computes load
The code lives in kernel/sched/loadavg.c. About every five seconds the kernel samples the number of runnable plus uninterruptible tasks and folds each sample into three exponentially decaying averages, so recent minutes weigh more than older ones. This is why a spike fades from the one-minute number long before it leaves the fifteen-minute number. Counting D state tasks is a Linux choice; some other Unix systems count only runnable threads, which is why load figures do not compare across operating systems. The decay constants are fixed-point approximations of 1/e for each window, so each update is a multiply and a shift rather than floating-point math.
CPU: top, mpstat, and the run queue
4 min readRun top and look at the summary line near the top. Typical form (yours will differ):
%Cpu(s): 3.1 us, 1.2 sy, 0.0 ni, 95.2 id, 0.4 wa, 0.0 hi, 0.1 si, 0.0 st
Each field is a share of CPU time: us user code, sy kernel code on behalf of processes, ni user code at raised nice values (lower priority), id idle, wa I/O wait, hi hardware interrupt handling, si software interrupt handling (network packet processing often lands here), st steal time, which only appears under a hypervisor and means the host gave your CPU to someone else.
Two readings matter most. High us with low id: computation is the load. High wa: the CPU is idle while I/O is in flight, which points at storage, not compute. Be careful with wa: it is idle time, so a fast CPU that finishes its work and waits shows wa too. It is a hint, and you confirm it with the disk tools below.
1Go per-core
One average can hide a single pegged core. Press 1 inside top to expand one line per CPU. A single-threaded program at 100% on one of four cores shows as 25% in the aggregate line. For a scripted view, use mpstat from sysstat:
mpstat -P ALL 1
It prints one row per CPU per second. Watch the %idle and %iowait columns per core; a lone busy core with idle siblings is a parallelism problem, not a capacity problem.
2Find the process and the queue
Inside top, press P (shift+p) to sort by CPU usage and M to sort by memory. The per-process %CPU is in Irix mode by default: 100% means one whole core, so a four-thread worker can legitimately show 400%. Press I (shift+i) to switch to Solaris mode, which divides by the core count. From a script, ps does the same job:
ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head -6
Saturation shows up in the the set of tasks that are ready to run and waiting for a CPU. vmstat 1 prints it in the r column, with b counting tasks blocked in uninterruptible sleep:
vmstat 1
Typical output (yours will differ; the first row is the average since boot, so judge the rows after it):
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 3521440 182332 2051824 0 0 12 4 88 150 3 1 96 0 0
3 0 0 3521408 182332 2051824 0 0 0 0 620 1180 78 2 20 0 0
On a four-core machine, r sustained above 4 means tasks wait for CPU. htop is a friendlier interactive view with the same numbers; install it if you prefer it.
mpstat, pidstat and iostat come from the sysstat package; iotop and htop are separate packages. Minimal images and the default WSL2 image ship none of them: sudo apt install sysstat iotop htop. On WSL2, remember that all these numbers describe the Linux VM, not Windows; the Windows-side vmmem process is that VM's memory. BusyBox top and ps on small boards show fewer columns and no --sort; the underlying /proc files are still there.
Key takeaways
- Load average counts runnable plus uninterruptible tasks; compare it with nproc, never with 100.
- High wa with idle CPU points at storage, not compute.
- mpstat -P ALL (or pressing 1 in top) reveals per-core load; a lone pegged core with idle siblings is a parallelism problem, not a capacity problem.
- vmstat's r column counts the run queue and b counts blocked tasks; r sustained above nproc means tasks wait for CPU.
Reflect
- Explain to a colleague, in two sentences: apply the USE method to CPU, memory, disk and network, and name the saturated resource with evidence.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Man pageproc(5): process and system information pseudo-filesystem
- ArticleThe USE Method, Brendan Gregg
Official documentation first. Links checked when the lesson was published.