Day 3: Where the numbers come from – procfs, sar and PSI
Goal: understand where
vmstat,iostatand co. get their numbers from, read the raw counters yourself, look into the past with the sar recording and interpret PSI correctly – and clear up a misinterpreted load average · Duration: about 2.5 hours · Prerequisite: Days 0–2
Where we start
perf1 is quiet, /daten is empty, and sar has been
recording every minute since Day 0.
# on perf1
ls /datenExpected:
lost+found
# on perf1
systemctl is-active nachtlaufExpected:
inactive
sar creates a file saNN for each day, where
NN is the day of the month. Which day it is today is shown
by date +%d (two digits):
# on perf1
date +%dExpected (on the 30th of the month):
30
# on perf1
ls /var/log/sysstat/sa30Put in your number instead of 30.
Expected:
/var/log/sysstat/sa30
If ls reports No such file or directory,
recording is not running – back to Day 0, Drill 5.
Introduction
So far you have trusted tools. Today you look over their shoulder.
Almost every number that vmstat, iostat,
mpstat or top show is not read out for you by
the kernel; it keeps them as counters in text files
under /proc and /sys. The tools read these
files twice, take the difference and divide by the time. That is
all.
Knowing this gives you three advantages. First, you can recalculate every number yourself if a tool is missing or shows something strange. Second, you understand why some values are snapshots and others averages – and why they sometimes contradict each other. Third, you find counters that no standard tool shows, such as the waiting time of a single service.
Then you learn to read the long-term recording with sar
and take a closer look at PSI, the most modern tool for saturation. The
diagnosis task shows how to use this knowledge to interpret a load
average correctly that everybody takes for a CPU problem.
Today you learn:
- the difference between counters, profiling and tracing and what each costs,
- to see with
stracewhich files a tool reads, - to read
/proc/stat,/proc/loadavg,/proc/diskstats,/proc/net/devand/proc/PID/*directly and to calculate values from them, - what the load average really measures on Linux,
- to query any time window of the recording with
sar, - to read PSI system-wide and per service,
- to follow a high load with an idle CPU down to its cause.
Background
The picture for today: lab results and the medical record
The instruments from Day 2 do not measure themselves. They read what the kernel’s laboratory records anyway – like a doctor who reads the lab report instead of analyzing blood herself.
/procand/sysare the lab report. It contains raw values: counter readings that only grow since start, like an electricity meter.vmstatreads the meter twice and calculates the difference.- sar is the medical record. It keeps the history – including what happened while nobody was looking.
- PSI is the waiting-time statistic: what share of the time did patients have to wait?
- The load average counts everybody in the building – those being treated, those waiting and those in the recovery room who do not react to anything right now (state D). That is why a high load alone does not say that the doctor is overloaded.
The terms
| Term | Meaning | In the picture |
|---|---|---|
| procfs, sysfs | files under /proc and /sys with the
kernel’s raw counters |
lab report |
| counter | value that only grows since start; the difference is what matters | electricity meter reading |
| snapshot | value at the time of the query (for example requests in progress) | a single photo |
| sar recording | data points every minute in /var/log/sysstat/saNN |
medical record |
| PSI | share of time in which tasks had to wait | waiting-time statistic |
| load average | runnable plus uninterruptibly waiting threads, damped average | all patients in the building |
Remember: Tools only calculate differences from counters in
/proc. If you can read the counters, you do not depend on any tool.
Counters, profiling, tracing
There are three ways to observe a running system. They differ in what they deliver and what they cost.
Counters are numbers that the kernel keeps anyway: how many reads a disk had, how much CPU time a process used, how many packets arrived. They only rise and are reset to zero at reboot. Reading them costs almost nothing, because they are counted anyway. All tools of Day 2 work with counters. Their limit: they say how much, but not who exactly or why.
Profiling takes samples at fixed intervals: a
hundred times a second it looks at which function is running on each CPU
right now. This produces a picture of where the time goes. The cost is
low and predictable because it depends on the sampling rate, not on the
load. Day 5 works with perf as a profiler.
Tracing records every event of a
kind: every system call, every disk request, every context switch. That
provides the most precise data – and can become very expensive, because
the cost grows with the number of events. strace, which you
use briefly today, is a tracer with particularly high cost; Days 13 and
14 show gentler ones.
The rule of thumb: start with counters, narrow down with profiling, prove specifically with tracing.
procfs and sysfs
/proc and /sys are not real directories on
a disk but pseudo file systems: the kernel creates
their content at the moment someone reads. Every file is an interface to
data in the kernel.
/proc(procfs) is the older one. It contains one directory per process (/proc/1234/) and system-wide files such as/proc/stat,/proc/meminfoor/proc/diskstats. Their format is meant for humans and has grown over time; every file has its own./sys(sysfs) is newer and stricter: one value per file, ordered by devices and kernel parts./sys/block/sdb/statcontains the counters of the disksdb,/sys/fs/cgroup/the cgroups.
The load average on Linux
The load average is the best-known and most frequently misunderstood value. On classic Unix systems it counted processes that are running or waiting for a CPU – a pure CPU metric. Linux additionally counts processes in state D, that is, those waiting uninterruptibly for something, usually for I/O. This was introduced in 1993 so that the load also shows disk bottlenecks.
The consequence: a high load on Linux can have three causes – too much computing work, a lot of waiting for I/O, or waiting for locks in the kernel. Whoever reflexively adds CPUs at high load is often wrong.
The three values are exponentially damped averages over 1, 5 and 15 minutes. They react slowly: if the load jumps from 0 to 4, the 1-minute value needs about one minute to reach almost two thirds of it. For the question “What is going on now?” the load is too sluggish; for “Is it rising or falling?” it is good.
PSI – Pressure Stall Information
PSI has existed since kernel 4.20. It answers a question the load cannot answer: how much time was lost because tasks had to wait for a resource? For CPU, memory and I/O there is one file each with two lines:
some– share of the time in which at least one task was waiting. Others could still work during that time.full– share of the time in which all non-idle tasks were waiting at the same time. During that time the system did nothing useful.
avg10, avg60 and avg300 are
averages over 10, 60 and 300 seconds, in percent; total is
the total waiting time since start in microseconds. Because PSI measures
in time, it is comparable regardless of the number of CPUs – 30 % is
just as bad on two cores as on two hundred.
And PSI exists not only system-wide under
/proc/pressure/, but for every cgroup:
every systemd service has its own files cpu.pressure,
memory.pressure and io.pressure under
/sys/fs/cgroup/…/. With them you see which service
is waiting.
Drill 1: Looking over the tools’ shoulder
Where you are: on perf1.
What you have afterwards: seen which files
vmstatreads, and calculated the CPU line yourself from the raw counters.
Step 1 – Which files does vmstat read?
strace shows every system call a program makes. You start
vmstat under strace and look only at file
accesses:
# on perf1
strace -f -e trace=%file -o /tmp/vm.trace vmstat 1 2 > /dev/null| Item | Meaning |
|---|---|
strace |
starts the program after it and logs its system calls |
-f |
also follows child processes |
-e trace=%file |
only calls that receive a file name (openat,
stat and relatives) – without this option it would be
hundreds of lines |
-o /tmp/vm.trace |
log into a file instead of the screen |
vmstat 1 2 > /dev/null |
the observed program; its own output is of no interest here |
strace itself prints nothing; the log is now in
/tmp/vm.trace.
Step 2 – Extract the paths.
# on perf1
grep -o '"/proc[^"]*"\|"/sys[^"]*"' /tmp/vm.tracegrep -oprints only the matching part of a line, not the whole line.- The pattern looks for paths in quotes that start with
/procor/sys.[^"]*means “any number of characters except quotes”,\|separates the two patterns.
Expected:
"/proc/vmstat"
"/proc/self/status"
"/sys/devices/system/node"
"/sys/devices/system/cpu/possible"
"/proc/self/status"
"/proc/cpuinfo"
"/proc/stat"
"/proc/meminfo"
"/proc/uptime"
Every path once, only "/proc/self/status" twice.
That is vmstat: /proc/stat for CPU times,
processes and context switches, /proc/meminfo for memory,
/proc/vmstat for swap and paging, /proc/uptime
for the time. Each file is opened only once, although
vmstat prints two lines: the program keeps them open and
rereads them from the start before each line. There is nothing more to
it.
Step 3 – In which unit does the kernel count?
# on perf1
getconf CLK_TCKgetconf CLK_TCK prints the unit in which the kernel
counts CPU times: in ticks per second.
Expected:
100
So one tick is 1/100 second.
Step 4 – Read /proc/stat twice. head -1
prints only the first line of a file:
# on perf1
head -1 /proc/statWait a moment and run the same command again. In the lab about three seconds lay in between; for you it is the time you need to run it again – that does not matter for the calculation below, because you calculate shares.
Expected (your numbers are different; first and second run):
cpu 326968 1197 183803 13658122 168910 0 20455 57999 0 0
cpu 326969 1197 183803 13659320 168910 0 20455 58003 0 0
The cpu line sums up all CPUs (below it
cpu0, cpu1 … would be listed individually).
The fields are times in ticks of 1/100 second each, in this order:
user, nice, system,
idle, iowait, irq,
softirq, steal, guest,
guest_nice – exactly the columns you know from
mpstat.
Calculate the difference between the two lines:
| Field | before | after | difference |
|---|---|---|---|
| user | 326968 | 326969 | 1 |
| nice | 1197 | 1197 | 0 |
| system | 183803 | 183803 | 0 |
| idle | 13658122 | 13659320 | 1198 |
| iowait | 168910 | 168910 | 0 |
| steal | 57999 | 58003 | 4 |
| Sum | 1203 |
1203 ticks in a little over three seconds: four CPUs × 300 ticks,
plus a few ticks for the time the commands themselves needed. For you
the sum depends on how much time lay between the queries. The idle share
is 1198 ÷ 1203 = 99.6 %. That is exactly how
mpstat calculates – you have just determined
%idle by hand.
The reading since start is interesting too: steal =
58,003 ticks is almost ten minutes in which this VM’s CPUs wanted to run
but the hypervisor gave them to someone else. Day 4 comes back to
this.
Remember:
vmstatreads/proc/stattwice and calculates the difference.%idleis nothing but idle ticks divided by all ticks.
If it doesn’t work
strace: command not found:sudo apt-get install -y strace.- The list is much longer: you forgot
-e trace=%fileor left out the%.
Save your progress? Sign up for free
Drill 2: Raw counters for load, disk, network and processes
Where you are: on perf1.
What you have afterwards: read and interpreted the most important counter files once – including a counter that none of the tools of Day 2 shows.
Step 1 – /proc/loadavg.
# on perf1
cat /proc/loadavgExpected:
0.21 0.60 0.61 1/160 173592
The three load values, then 1/160: one thread is running
right now, 160 exist in total. The last number is the most recently
assigned process ID – if it rises quickly, new processes are being
started all the time, which can be suspicious on a server (for example a
script that calls programs in a loop).
Step 2 – /proc/diskstats.
# on perf1
grep -w sdb /proc/diskstatsgrep -w searches for sdb as a whole word so
that sdb1 does not match as well.
Expected:
8 16 sdb 1186165 0 9501266 728359 225985 184039 8374504 138610 0 133431 891032 21 0 41943048 4 56482 24057
After the device number (8 16) and name come 17
counters. The most important ones, in order:
- completed reads (1,186,165) – 2. merged reads – 3. sectors read of 512 bytes – 4. milliseconds the reads took,
- to 8. the same for writes,
- requests currently in progress (0) – the only value that is not a counter but a snapshot,
- milliseconds in which at least one request was running –
iostatcalculates%utilfrom this, - weighted milliseconds (running time × number of concurrent requests)
– from this
aqu-sz, - to 15. discard, 16. and 17. flush requests and their duration.
iostat reads this line twice and calculates:
await = difference of the milliseconds ÷ difference of the
accesses, %util = difference of field 10 ÷ elapsed time.
The same numbers are in /sys/block/sdb/stat without
names.
Step 3 – /proc/net/dev.
# on perf1
grep eth0 /proc/net/devExpected:
eth0: 20151103093 13420021 0 53100 0 0 0 0 10951680521 4310609 0 0 0 0 0 0
Eight counters for receiving, eight for sending: bytes, packets,
errors, dropped packets, FIFO overruns, frame errors,
compressed packets, multicast. On the receive side there are
53,100 dropped packets here – a lead that
sar -n DEV did not show at all on Day 2. A packet is
dropped when the kernel cannot accept it, for example because a buffer
was full. Whether 53,100 is a lot is only told by comparison: 0.4 % of
13.4 million packets, accumulated over two days with many load tests.
For the USE method this is the error column of the network;
sar -n EDEV shows it as a rate (Day 11).
Step 4 – A single process. Every process has a
directory /proc/PID/. You start a writing process and look
inside while it is running. So that you have enough time for that, it
writes 10,000 MiB – on perf1 that took 24 seconds. In the first
terminal:
# on perf1, terminal 1
dd if=/dev/zero of=/daten/probe bs=1M count=10000 oflag=direct status=nonedd if=/dev/zero of=/daten/probe bs=1M count=10000writes 10,000 blocks of 1 MiB of zeros into a file.oflag=directwrites past the page cache directly to the disk so that the process really waits.status=nonesuppresses the final message.
In the second terminal, while dd is running, first the
process ID:
# on perf1, terminal 2
pgrep -x ddExpected:
74396
Yours shows a different number. Put it in below instead of
PID.
# on perf1, terminal 2
grep -E '^(Name|State|Threads|VmRSS|voluntary_ctxt_switches|nonvoluntary_ctxt_switches)' /proc/PID/status/proc/PID/status contains the state of the process in
readable form. grep -E with the pattern
^(A|B|…) selects the six lines that start with one of these
names.
# on perf1, terminal 2
cat /proc/PID/io/proc/PID/io counts its input and output.
Expected (both commands one after the other, about
eight seconds after starting dd; the counters depend on
when you look):
Name: dd
State: D (disk sleep)
VmRSS: 3088 kB
Threads: 1
voluntary_ctxt_switches: 4368
nonvoluntary_ctxt_switches: 4
rchar: 4582284208
wchar: 4581228544
syscr: 4380
syscw: 4369
read_bytes: 0
write_bytes: 4582277120
cancelled_write_bytes: 0
State: D (disk sleep)– the state D of Day 2, here with its full name.VmRSS– the memory the process actually occupies right now (resident set size), 3 MB.voluntary_ctxt_switches: 4368– this is how oftenddgave up the CPU voluntarily because it had to wait for something: for the disk at every block (about 4,370 blocks so far).nonvoluntary(4) counts how often the kernel took the CPU away from it because someone else was next. Many involuntary switches mean: CPU saturation. Many voluntary ones mean: the process waits a lot. No tool of Day 2 shows this distinction per process so directly.rchar/wchar– bytes that the process read and wrote via system calls, no matter where to. It read 4.6 GB from/dev/zero– that is not a disk, which is whyread_bytes(actually read from disks) is 0.write_bytesare the bytes that really went to a disk. With real programs, the difference betweenrcharandread_bytesshows how much the page cache intercepted.syscr/syscw– number of read and write calls.
When dd has finished, you delete the test file:
# on perf1
rm -f /daten/probeRemember: Every process has its own lab sheet under
/proc/PID/: state, memory, voluntary and involuntary context switches, input and output./proc/net/devshows dropped packets thatsar -n DEVdoes not show.
If it doesn’t work
grep: /proc/…/status: No such file or directory:ddhad already finished beforegrepran, or you mistyped the process ID. Startddagain.State: R (running): you forgotoflag=direct; thenddwrites into the page cache and does not have to wait.
Save your progress? Sign up for free
Drill 3: Looking into the past with sar
Where you are: on perf1.
What you have afterwards: found the load tests from Days 1 and 2 again in the recording – only with sar and a time window.
Since Day 0, sar has written a data point every minute. If you worked through Days 1 and 2 today, your load tests are in it. Choose a time window that contains them; in the author’s lab that was 12:10 to 12:25.
Step 1 – CPU over time.
# on perf1
sar -u -s 12:10:00 -e 12:25:00-uselects CPU utilization.-s(start) and-e(end) limit the time window. Without an interval argumentsarreads today’s file instead of measuring live.
Expected:
Linux 6.8.0-151-generic (cp1) 11/30/26 _x86_64_ (4 CPU)
12:10:08 CPU %user %nice %system %iowait %steal %idle
12:11:04 all 0.04 0.00 0.07 0.01 0.17 99.71
12:12:04 all 0.06 0.00 0.08 0.01 0.15 99.71
12:13:08 all 65.35 0.00 0.51 0.01 0.17 33.96
12:14:04 all 68.72 0.00 0.12 0.00 0.09 31.08
12:15:08 all 19.83 0.00 0.67 0.62 0.50 78.38
12:16:03 all 35.53 0.00 0.37 0.01 0.32 63.77
12:17:01 all 10.22 0.00 0.31 0.02 0.17 89.27
12:18:08 all 0.02 0.00 0.04 0.01 0.13 99.80
...
12:22:04 all 1.29 0.00 4.18 9.12 1.79 83.63
12:23:08 all 5.99 0.00 12.29 24.19 4.30 53.23
12:24:01 all 13.43 0.00 5.04 0.02 1.34 80.18
12:25:00 all 1.22 0.00 8.67 25.52 3.05 61.54
Average: all 14.73 0.00 2.24 4.12 0.87 78.03
You recognize the days: at 12:13 and 12:14 the stress-ng runs from
Day 1 (over 65 % %user), 12:15 to 12:17 the “foreign” load
from Drill 3 of Day 1, from 12:22 the diagnosis task of Day 2 with high
%iowait. Two things stand out: - No value reaches
100 %, although the CPUs were at times full on Day 1. Each line
is the average over one minute, and the runs lasted only 40 seconds.
Short peaks always become flatter in the recording – the longer the
interval, the more so. - The header says
(cp1), not perf1. The daily file was
created at midnight, when the author’s VM still had a different name,
and sar writes the host name into the file when it is created. Yours
shows the name the VM had at midnight.
Step 2 – Queue and load.
# on perf1
sar -q -s 12:10:00 -e 12:25:00-q (queue) shows run queue and load
average.
Expected (shortened):
12:10:08 runq-sz plist-sz ldavg-1 ldavg-5 ldavg-15 blocked
12:12:04 1 147 0.00 0.00 0.04 0
12:13:08 8 161 1.93 0.59 0.24 0
12:14:04 0 152 3.30 1.32 0.52 0
...
12:23:08 1 160 2.51 1.21 0.68 0
12:24:01 2 158 1.45 1.14 0.68 0
12:25:00 3 161 2.89 1.52 0.84 0
runq-sz is the number of runnable threads at the
moment of measurement, plist-sz the number of all
processes and threads, blocked the number of processes in
state D – also at the moment of measurement. At 12:13:08 eight threads
were in the run queue (the eight workers from Day 1), one minute later
zero, because the run had just ended. And blocked
never shows anything, although on Day 2 processes hung
in D for several minutes: snapshots only see what happens exactly at the
time of measurement. The load values next to it are averages and show
the history more reliably.
Step 3 – The disk and PSI in the recording.
# on perf1
sar -d -p --dev=sdb -s 12:18:00 -e 12:25:00-d selects the disk statistics, -p writes
device names (sdb) instead of device numbers,
--dev=sdb restricts it to one disk.
Expected (shortened):
12:18:08 tps rkB/s wkB/s dkB/s areq-sz aqu-sz await %util DEV
12:21:00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 sdb
12:22:04 527.43 0.00 3923.40 0.00 7.44 0.41 0.64 19.49 sdb
12:23:08 1746.12 0.00 12547.14 0.00 7.19 1.06 0.51 53.09 sdb
12:24:01 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 sdb
12:25:00 1324.38 0.00 9661.16 0.00 7.29 0.93 0.59 45.76 sdb
# on perf1
sar -q PSI -s 12:10:00 -e 12:25:00-q PSI shows the PSI values that sar has also recorded
since sysstat 12.6.
Expected (shortened):
12:10:08 %scpu-10 %scpu-60 %scpu-300 %scpu
12:13:08 0.90 0.16 0.03 2.21
12:14:04 19.54 36.89 12.04 68.48
12:15:08 46.95 39.07 16.94 37.37
12:16:03 0.56 19.48 15.61 9.38
The disk shows the diagnosis task of Day 2 (and its second run) with
7 KiB per request – the pattern is clearly visible even in the
one-minute average. The PSI lines show the CPU saturation from Day 1:
%scpu is the share of the minute with waiting tasks
(some), the other three columns the values from
/proc/pressure/cpu at the time of measurement.
sar -q PSI has further blocks for memory
(%smem, %fmem) and I/O (%sio,
%fio).
Step 4 – Another day.
# on perf1
sar -u -f /var/log/sysstat/sa29 | tail -4-f reads a specific file. sa29 is the 29th
of the month; Ubuntu keeps older days for 28 days.
Expected (for you: a day on which the VM was already running):
23:30:07 all 3.81 0.00 2.96 0.01 2.84 90.38
23:40:08 all 3.72 0.00 2.90 0.01 2.42 90.95
23:50:08 all 3.82 0.00 2.95 0.01 2.39 90.82
Average: all 2.54 0.00 1.27 0.02 1.05 95.11
Back then, ten minutes lay between the data points; the one-minute setting only came with Day 0.
Remember: sar is the medical record: one-minute averages smooth out short peaks, and snapshots such as
blockedmiss almost everything. Averages and PSI are good for the history.
If it doesn’t work
- No lines in the time window: choose a window in
which you worked on Day 1 or 2.
sar -u | headshows from when today’s file has data.
Save your progress? Sign up for free
Drill 4: PSI per service
Where you are: on perf1.
What you have afterwards: found the PSI files of a single service.
Step 1 – System-wide.
# on perf1
ls /proc/pressureExpected:
cpu
io
memory
Step 2 – For one service.
# on perf1
ls /sys/fs/cgroup/system.slice/ssh.service | grep pressure| grep pressure shows only the names from the long list
that contain pressure.
Expected:
cgroup.pressure
cpu.pressure
io.pressure
memory.pressure
System-wide there are three files; every service – here the SSH
server – has the same three in its cgroup, plus
cgroup.pressure, a file for switching the measurement on
and off for this cgroup. The path follows what you saw on Day 1 with
ps -o cgroup: /sys/fs/cgroup +
/system.slice/ssh.service. How useful that is, the
diagnosis task shows.
Remember: PSI exists system-wide and per cgroup. Only the service’s file tells you who is waiting.
Save your progress? Sign up for free
Diagnosis task
The monitoring alert reports: “perf1: load average above 4 on 4 CPUs.” The colleague on the night shift has already acted and created a ticket “perf1 needs more CPUs”.
Setup. First create a file that will be read in a moment:
# on perf1
dd if=/dev/urandom of=/daten/archiv.bin bs=1M count=512 status=nonedd fills a 512 MiB file with random data
(/dev/urandom).
# on perf1
syncsync writes everything buffered to the disk.
# on perf1
echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/nullThis discards the page cache so that the file is really read from the
disk in a moment. tee writes into the file with root rights
(a sudo echo 3 > would not work because the redirection
> runs as a normal user); > /dev/null
discards the copy that tee would write to the screen. This
is a tool for the lab, not for production servers.
The fault itself is a small program that starts six readers at the same time. Create it:
# on perf1
mkdir -p ~/tag03# on perf1
nano ~/tag03/archivpruefung.sh#!/bin/sh
# archivpruefung.sh – sechs Leser gleichzeitig (Tag 3)
dd if=/daten/archiv.bin of=/dev/null bs=1M iflag=direct status=none &
dd if=/daten/archiv.bin of=/dev/null bs=1M iflag=direct status=none &
dd if=/daten/archiv.bin of=/dev/null bs=1M iflag=direct status=none &
dd if=/daten/archiv.bin of=/dev/null bs=1M iflag=direct status=none &
dd if=/daten/archiv.bin of=/dev/null bs=1M iflag=direct status=none &
dd if=/daten/archiv.bin of=/dev/null bs=1M iflag=direct status=none &
waitAnd it is started as a service. You do not need to understand the command yet – the solution explains it:
# on perf1
sudo systemd-run --unit=archivpruefung -p "IOReadBandwidthMax=/dev/sdb 2M" sh /home/ubuntu/tag03/archivpruefung.shThe full path is needed because the service does not start in your home directory.
Expected:
Running as unit: archivpruefung.service; invocation ID: 5bcbadd4c3654bf48f56704c6c0231c0
Wait a good minute so that the load rises.
Task (20 minutes): check whether the ticket is right. What drives the load? Which resource is really the bottleneck – and where exactly does it sit?
Solution path
Confirm the load. uptime shows:
23:05:33 up 10:04, 1 user, load average: 4.51, 3.72, 3.09
Above 4 and rising – the alert is correct. The question is what the load consists of.
Is anybody computing here?
vmstat 1 3:
procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
1 6 0 11923800 5304 116016 72 72 2229 4898 949 1 2 1 95 1 0 0
0 6 0 11923800 5304 116016 0 0 2048 0 152 101 0 0 0 100 0 0
0 6 0 11923800 5304 116016 0 0 2048 0 126 88 0 0 0 100 0 0
r = 0: nobody wants to compute. b = 6: six
processes blocked. us and sy at 0,
wa at 100: the CPUs have nothing to do and are waiting for
I/O. The load consists entirely of processes in state
D. More CPUs would not change anything about that. The ticket is
refuted.
Exactly 2048 KiB per second are read (bi). Keep this
round number in mind.
Who is waiting?
# on perf1
ps -eo state,pid,comm | awk '$1=="D"'awk '$1=="D"' prints only lines whose first column is
exactly D.
D 74659 dd
D 74660 dd
D 74661 dd
D 74662 dd
D 74663 dd
D 74664 dd
D 74673 bash
Six dd processes. In the lab the last line was a shell
that waited briefly for the disk at the moment of measurement – an
instant, not a permanent state. It may not show up for you.
Is the disk overloaded?
iostat -sxz 1 2, second output:
Device tps kB/s rqm/s await areq-sz aqu-sz %util
sdb 4.00 2048.00 0.00 1.50 512.00 0.01 0.20
And here it gets interesting: sdb is 0.2
% utilized, with a latency of 1.5 ms and a practically empty
queue. The disk is bored. Six processes are waiting for I/O, but not for
the disk – the requests do not even reach it. The smooth rate of exactly
2048 KiB/s fits this: it looks like a limit, not like a
device at its limit.
PSI system-wide:
# on perf1
cat /proc/pressure/iosome avg10=98.96 avg60=75.73 avg300=30.64 total=1020348624
full avg10=98.77 avg60=75.63 avg300=30.33 total=991780918
# on perf1
cat /proc/pressure/cpusome avg10=0.00 avg60=0.14 avg300=0.55 total=343292345
full avg10=0.00 avg60=0.00 avg300=0.00 total=0
I/O pressure at 99 %, CPU pressure at 0. Unambiguous.
Where does the limit sit? The process’s cgroup leads to the service:
The process ID of the oldest dd:
# on perf1
pgrep -o -x ddExpected:
74659
The smallest number from the list above. Put in yours.
# on perf1
ps -o pid,cgroup:55,args -p 74659 PID CGROUP COMMAND
74659 0::/system.slice/archivpruefung.service dd if=/daten/archiv.bin of=/dev/null bs=1M iflag=direct status=none
And in this service’s cgroup there are PSI and – the limit:
# on perf1
cat /sys/fs/cgroup/system.slice/archivpruefung.service/io.pressuresome avg10=98.96 avg60=71.89 avg300=23.21 total=78748752
full avg10=98.95 avg60=71.88 avg300=23.20 total=78736008
# on perf1
cat /sys/fs/cgroup/system.slice/archivpruefung.service/io.maxio.max contains the I/O limits of the cgroup, one line
per device.
8:16 rbps=2000000 wbps=max riops=max wiops=max
# on perf1
lsblk -no MAJ:MIN /dev/sdblsblk -no MAJ:MIN prints the device number of
sdb (-n without header, -o only
this column) so that you can match the line in io.max.
8:16
The pressure sits in this service. And io.max says: for
device 8:16 – that is sdb – this cgroup may
read at most 2,000,000 bytes per
second (rbps), everything else is unlimited. That
is the 2048 KiB/s from vmstat (more precisely 1953 KiB/s;
the throttling works in bursts). Six processes share 2 MB/s and spend
almost all their time in the queue.
Now you can read the setup command:
systemd-run -p "IOReadBandwidthMax=/dev/sdb 2M" sets a read
limit of 2 MB/s on sdb for the service – systemd translates
this into the line in io.max. Caution: for systemd
2M is 2,000,000 bytes, not 2 MiB. Day 15 shows how to use
such limits deliberately. Here a (too narrow) limit created a load that
looked like CPU.
Where exactly does a process wait? As a final
confirmation you can look into the kernel stack of a
dd:
Take one of the six process IDs from the list above, in the example the highest:
# on perf1
sudo cat /proc/74664/stack/proc/PID/stack shows the functions in the kernel in
which the process is stuck right now; only root may read it.
[<0>] blk_io_schedule+0x22/0x40
[<0>] __iomap_dio_rw+0x4fb/0x770
[<0>] iomap_dio_rw+0x11/0x60
[<0>] ext4_file_read_iter+0x162/0x210
[<0>] vfs_read+0x208/0x3a0
[<0>] ksys_read+0x73/0x100
[<0>] __x64_sys_read+0x19/0x30
[<0>] x64_sys_call+0x1bf0/0x25a0
[<0>] do_syscall_64+0x87/0x180
[<0>] entry_SYSCALL_64_after_hwframe+0x78/0x80
Read from bottom to top: system call read, ext4 reads
past the cache (direct I/O, iomap_dio_rw) and
waits in blk_io_schedule for the block request to complete.
No computing, no lock – pure waiting for I/O.
Fix and check.
# on perf1
sudo systemctl stop archivpruefung# on perf1
sudo systemctl reset-failed archivpruefungIn the lab this reported
Failed to reset failed state of unit archivpruefung.service: Unit archivpruefung.service not loaded.
– the service had ended cleanly when stopped and was already forgotten.
Then there is nothing to do; the command does no harm.
# on perf1
rm -f /daten/archiv.bin# on perf1
vmstat 1 2 | tail -1tail -1 shows only the last line – the current
measurement.
Expected:
0 0 0 11911264 8268 124232 0 0 0 0 252 127 0 0 100 0 0 0
b = 0, wa = 0. The answer to the ticket:
the load came neither from the CPU nor from an overloaded disk, but from
an I/O limit in the cgroup of a single service. More CPUs would have
cost money and changed nothing.
Remember: High load with an idle CPU and an idle disk means: something is waiting in front of the disk. PSI per cgroup and the service’s
io.maxshow the limit.
If it doesn’t work
vmstatshowsbifar above 2048 andb= 0: the file was still in the page cache because the flush was missing. Withiflag=directthis should not happen; check whether you typediflag=directtoo.io.maxis empty: the service is no longer running (six readers with 512 MiB each need about 27 minutes together at 2 MB/s) or you left out the parameter-pin the setup.
Challenge
Write a one-liner that calculates the %util of
sdb over five seconds yourself from
/sys/block/sdb/stat, without iostat. Hint:
field 10 is milliseconds with a request in progress.
Solution path: without a script, in three steps. Read the counter line:
# on perf1
cat /sys/block/sdb/statNote the tenth field (milliseconds in which at least one request was running). Wait five seconds – ideally by the clock – and read the line again:
# on perf1
cat /sys/block/sdb/statIn the lab, at rest (first and second reading):
4611012 36 152826378 8427950 1124071 251829 342427048 3659373 0 1366307 12154432 0 0 0 0 184098 67108
4611012 36 152826378 8427950 1124073 251838 342427136 3659375 0 1366309 12154434 0 0 0 0 184099 67108
Then you calculate: (second value − first value) × 100 ÷ 5000
milliseconds = %util. Here: (1366309 − 1366307) × 100 ÷
5000 = 0.04 % – two milliseconds of work in five
seconds; if you let the fio command from Day 0 run alongside, you get a
value close to that of iostat. Exactly this calculation is
what iostat does for you.
Save your progress? Sign up for free
Checkpoint
# on perf1
systemctl is-active archivpruefungExpected: inactive.
# on perf1
ls /datenExpected: lost+found.
# on perf1
cat /proc/pressure/ioExpected: in both lines avg10 close to 0, for example
avg10=0.00.
# on perf1
sar -q PSI | head -3head -3 shows only the first three lines.
Expected (date and name differ for you):
Linux 6.8.0-151-generic (cp1) 11/30/26 _x86_64_ (4 CPU)
00:00:08 %scpu-10 %scpu-60 %scpu-300 %scpu
In the third line the header with %scpu-10 … %scpu – the
recording contains PSI. (The first line shows the host name at the start
of the daily file; in the lab that was still the VM’s old name
cp1.)
Test yourself
- Where does
vmstatget its numbers from, and why does it open each file only once? - How do you calculate
%idlefrom two readings of/proc/stat? - What distinguishes voluntary from involuntary context switches, and what does a high value of each reveal?
- Why does
blockedinsar -qalmost never show anything, although processes hung in D for minutes? - Load 4.4 on 4 CPUs,
r= 0,b= 6, disk 0.2 % utilized. What is going on, and where do you look?
Answers:
- From
/proc/stat,/proc/meminfo,/proc/vmstatand/proc/uptime. It keeps the files open and rereads them before each line. - Difference of the
idlefield divided by the difference of all fields. - Voluntary: the process gives up the CPU because it waits – many of them mean “waits a lot”. Involuntary: the kernel takes it away – many of them mean CPU saturation.
- It is a snapshot at the time of measurement; it only sees what happens exactly then.
- The load consists of waiting processes, but they are not waiting at
the disk – a limit in front of it. PSI and
io.maxin the service’s cgroup show it.
Interview Note
“What does the load average measure on Linux?” is a question that
separates candidates. The complete answer: runnable threads
plus threads in uninterruptible sleep, as an
exponentially damped average over 1, 5 and 15 minutes – so not a pure
CPU metric. If you connect this with a case (“high load, CPU idle, six
processes in D, behind them an I/O limit”), you show practical
experience. Bonus points for PSI as a more modern alternative and for
pointing out that tools read counters from /proc that you
can evaluate yourself if needed.
Enterprise Note
Many monitoring systems still alert on the load average. On Linux
this leads to exactly the ticket from the diagnosis task – and to
expensive resizing without effect. Better: alerts on PSI
(some over 10 or 60 seconds) for CPU, memory and I/O,
separately; Prometheus exporters and most agents can read it. Where
services run with resource limits (systemd, containers, Kubernetes), PSI
per cgroup belongs in monitoring, because that is where
you see which service is hanging at its limit. And: keep sar. If central
monitoring only has five-minute averages after an incident, the
per-minute recording on the host is often the only thing that still
shows the history.
Summary
- Counters cost almost nothing, profiling little and predictably, tracing a lot – start with counters.
- Tools read counter files from
/procand/systwice and divide the difference by the time.strace -e trace=%fileshows which ones. /proc/stat(CPU times in ticks),/proc/loadavg,/proc/diskstatsor/sys/block/X/stat(17 disk counters),/proc/net/dev(including dropped packets) and/proc/PID/status,io,stackprovide raw data – some of which no standard tool shows.- The load average on Linux also counts processes in state D. High load ≠ CPU problem.
- sar shows any time window with
-s/-e; one-minute averages smooth out short peaks, snapshots such asrunq-szandblockedonly see the moment of measurement. - PSI measures time lost to waiting, system-wide and
per cgroup.
fullfor I/O means: the system did nothing useful. - Diagnosis today: load 4.4 with an idle CPU and an idle disk – caused
by an I/O limit (
io.max) in the cgroup of a service.
Commands at a glance
| Command | What it does | Parameters |
|---|---|---|
strace -f -e trace=%file -o FILE COMMAND |
log a program’s file accesses | -f children, -e trace= selection,
-o file |
head -1 /proc/stat |
CPU times of all CPUs in ticks | – |
getconf CLK_TCK |
ticks per second | – |
cat /proc/loadavg |
load, running/all threads, last PID | – |
grep -w sdb /proc/diskstats |
17 counters of a disk | -w whole word |
grep eth0 /proc/net/dev |
network counters including errors and drops | – |
cat /proc/PID/status, /io,
/stack |
state, I/O counters, kernel stack of a process | stack only with sudo |
sar -u -s 12:10:00 -e 12:25:00 |
CPU from the recording | -s start, -e end |
sar -q / sar -q PSI |
run queue, load / PSI from the recording | – |
sar -d -p --dev=sdb |
disk statistics from the recording | -p names, --dev device |
sar -u -f /var/log/sysstat/saNN |
read another day | -f file |
cat /sys/fs/cgroup/…/io.pressure,
io.max |
PSI and I/O limits of a service | – |
lsblk -no MAJ:MIN /dev/sdb |
device number of a disk | -n without header, -o column |
echo 3 \| sudo tee /proc/sys/vm/drop_caches |
flush the page cache (lab only) | 3 = cache and metadata |