Who eats the CPU and memory
ps takes a snapshot, and its %CPU is the average over the whole life of
the process. Who loads the CPU right now shows in top or pidstat -u 1.
The order of checks for "the server is slow": CPU, memory, disk, kernel and network — what shows each resource.
uptime; nproc
load and CPU count
vmstat 1 5
queue, memory, swap, disk, CPU
top
processes: P — by CPU, M — by memory
free -h
memory: look at available
iostat -dxs 1 3
disks: await, aqu-sz, %util
sudo pidstat -d 1 3
who reads and writes the disk
sudo journalctl -k -p err -b
kernel errors of this boot
mpstat -P ALL 1 3
load of each CPU
"The server is slow" means some resource has run out: CPU, memory, disk or network. First find which one with a command or two, then look into it in detail.
vmstat comes from the procps package; iostat, pidstat, mpstat and
sar from sysstat. The training machine has both.
| Resource | Command | Sign of trouble |
|---|---|---|
| CPU | uptime, top |
load above nproc, id near 0 |
| memory | free -h, vmstat 1 |
little available, si and so grow, OOM in the log |
| disk | vmstat 1, iostat -dxs 1 |
high wa, b above 0, %util near 100 |
| kernel | sudo journalctl -k -p err -b |
filesystem and disk errors, OOM, hung tasks |
| network | ss -s, sar -n DEV 1 |
queues and errors on an interface |
Each resource in detail: the CPU queue and state D — load-average,
memory and swap — memory-swap, OOM — oom-killer, disks and the
processes using them — pidstat-iostat, the kernel log — journalctl, sockets
and connections — ss.
vmstat 1 prints a line every second: the task queue, memory, swap, disk and
CPU. The first line holds averages since boot; the following lines show the
current picture.
Two lines from the training machine — two different problems:
| Picture | r | b | free | cache | si | so | bi | bo | us | sy | id | wa |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| two tasks share one CPU | 2 | 0 | 19180 | 289936 | 0 | 0 | 0 | 8 | 100 | 0 | 0 | 0 |
dd writes to disk |
0 | 1 | 45064 | 268508 | 0 | 0 | 0 | 32716 | 1 | 18 | 0 | 81 |
| Column | Meaning |
|---|---|
r |
tasks in state R: running and waiting for a CPU |
b |
tasks waiting for disk I/O |
swpd |
used swap, KiB |
free, buff, cache |
free memory, buffers and page cache, KiB |
si, so |
reading from swap and writing to swap, KiB/s |
bi, bo |
reading from disks and writing to disks, KiB/s |
in, cs |
interrupts and task switches per second |
us, sy, id, wa, st |
shares of CPU time, as in top |
gu |
guest VM time, if the machine is a hypervisor itself |
b and wa count only waiting for a device. Processes waiting for a frozen
filesystem show in load average but not in b (see
load-average).
mpstat -P ALL 1 shows each CPU separately: %usr, %sys, %iowait,
%steal, %idle. On a two-CPU machine an average id of 50 % can mean one
CPU fully busy and the other idle. A single-threaded program never uses more
than one CPU.
sar -u 1 3 shows the CPU right now, like mpstat. The point of sar is the
history of past hours, but Ubuntu ships with collection off:
To turn collection on, set ENABLED="true" in /etc/default/sysstat, then
run sudo systemctl restart sysstat. After that sar -u, sar -r (memory)
and sar -d (disks) show today by intervals.
ps takes a snapshot, and its %CPU is the average over the whole life of
the process. Who loads the CPU right now shows in top or pidstat -u 1.
One file with the time, load, memory and process list — attach it to a ticket or compare it with a similar snapshot later.
Tasks are in state D. High wa and b above 0 — they wait for the disk;
continue with iostat -dxs 1 and sudo pidstat -d 1. wa and b at zero —
they wait for something other than a device; see
load-average.
Without sudo, pidstat -d shows only your own processes: only root can
read the I/O counters of other users' processes. iotop is not installed on
the training machine. The fields of iostat and pidstat are in
pidstat-iostat.
The kernel keeps free memory busy with the page cache and gives it to
programs on demand. The memory reserve is the available column of
free -h; see memory-swap.
History collection is off: ENABLED="false" in /etc/default/sysstat
(section "History: sar" above). Once turned on, collection shows data from
that moment — the past cannot be recovered.
pidstat prints only processes that did something during the interval.
Other users' processes appear in pidstat -d only for root — run it with
sudo.
R and D, the %Cpu(s) line.iostat, pidstat.df and du.Found an inaccuracy? Report an error in the article