Part 2 · System administration · Lesson 02 · 40 minutes
Find out why a server is slow
- Read the load average, memory, CPU, disk and network numbers and say what each means
- Use uptime, free, vmstat, iostat and top to see which resource is the limit
- Decide from saved measurements whether a machine is limited by CPU, memory or disk
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
Users say that web7 is slow. Your lead asks one question: "Is it the processor, the memory or the disk?" She does not want a guess. She wants numbers and a one-line answer. You first learn each measuring tool on your practice system, where you create a load yourself. Then you read the saved measurements of web7 and answer her.
Recall
Which command prints the number of processors that your processes may use?
nproc. It is the number to compare with the load average.
Which file holds the memory counters that the kernel keeps, and in which unit?
/proc/meminfo. The numbers are in KiB, and the file writes the unit as kB.
The idea
A system has four resources that can run short: the processor, the memory, the disk and the network. A slow system waits for one of them. The skill is to find which one, and the tools are the same on every Linux.
- Load is high → CPU busy?: start: uptime, top
- CPU busy? → Memory short?: yes: CPU. no: free, vmstat
- Memory short? → Disk waiting?: yes: memory. no: iostat
- Disk waiting? → Network full?: yes: disk. no: ip -s link
The first number to read is the load average. It is the average number of processes that wanted the processor, or waited for the disk, over the last 1, 5 and 15 minutes. One CPU can serve one process at a time. A load of 4.0 on 4 CPUs means that all are busy and nobody waits. A load of 8.0 on 4 CPUs means that, on average, four processes wait. So you always divide the load by nproc.
| Tool | What it shows | Numbers to read |
|---|---|---|
uptime | Time, users, load average | The three loads, against nproc |
free | Memory and swap | available, not free |
vmstat | One line each second | r (waiting for CPU), si/so (swap), wa (waiting for disk) |
iostat | Disk traffic | tps, kB_wrtn/s, and %util, r_await and w_await with -x |
top | Live list of processes | %Cpu(s) and the biggest users |
ip -s link | Bytes, packets, errors on a network card | errors and dropped |
Worked example
The package sysstat holds iostat. If the command is missing, install it with sudo apt install sysstat. The numbers below come from a test virtual machine with 4 CPUs and 4 GiB of memory. Yours will differ.
1. Read the load average
uptime prints the time, how long the system has run, the number of logged-in users and the load average. The same three numbers are in /proc/loadavg. nproc prints the number of CPUs.
uptime
nproc 19:37:35 up 4 min, 1 user, load average: 0.21, 0.08, 0.03
4Divide the first number by nproc: 0.21 / 4 is about 0.05, so the CPUs are nearly idle. The order of the three numbers shows the trend: 0.21, 0.08, 0.03 means that the load is rising (the last minute is busier than the minutes before), and 0.03, 0.08, 0.21 would mean that it is falling.
2. Read the memory
free shows memory and swap. -m prints MiB, and -h (human-readable) chooses a unit for each number. Swap is disk space that the kernel uses as slow extra memory.
free -m total used free shared buff/cache available
Mem: 3824 375 3144 23 543 3449
Swap: 0 0 0free is memory that nothing uses. buff/cache is memory that holds file data. The kernel gives it back when a program needs it. So the number that tells you the truth is available: what a new program can get. A system with free near zero and a large available is healthy.
3. Watch the system once a second
vmstat 1 3 (virtual memory statistics) prints a line each second, three times. The first line is the average since boot, so read the later lines.
vmstat 1 3procs -----------memory---------- ---swap-- -----io---- -system-- -------cpu-------
r b swpd free buff cache si so bi bo in cs us sy id wa st gu
1 0 0 3219628 16644 540244 0 0 1255 748 430 0 1 1 98 0 0 0
0 0 0 3219628 16652 540380 0 0 0 448 140 118 0 0 100 0 0 0
0 0 0 3219628 16652 540380 0 0 0 0 75 76 0 0 100 0 0 0The first line is the average since boot (note the bi of 1255). The next two lines show the machine now: r is 0 and id is 100, so the machine is idle. On a busy machine r stays above the number of CPUs. r is the number of processes that wait for a CPU, and b the number that wait for the disk. si and so are the memory swapped in and out. If they are not zero all the time, the system lacks memory. The last columns split the CPU time in per cent: us user programs, sy the kernel, id idle, wa waiting for the disk, st stolen by the host of a virtual machine.
4. Create a load and find it
yes prints the letter y without end. Sent to /dev/null (a file that throws away what it gets), it keeps one CPU busy. & starts a command in the background. top -b -n 1 (batch mode, one round) prints one screen of top and ends. Start two, wait a minute, look, and stop them with kill and the job numbers.
yes > /dev/null &
yes > /dev/null &
sleep 60
uptime
top -b -n 1 | head -n 10
kill %1 %2[1] 1022
[2] 1023
21:06:12 up 1:10, 0 user, load average: 1.35, 0.43, 0.15
top - 21:06:12 up 1:10, 0 user, load average: 1.35, 0.43, 0.15
Tasks: 133 total, 3 running, 130 sleeping, 0 stopped, 0 zombie
%Cpu(s): 27.1 us, 25.0 sy, 0.0 ni, 45.8 id, 0.0 wa, 0.0 hi, 0.0 si, 2.1 st
MiB Mem : 3824.8 total, 3528.5 free, 343.2 used, 172.3 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 3481.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1023 you 20 0 8716 1868 1748 R 100.0 0.0 0:52.01 yes
1022 you 20 0 8716 1876 1756 R 90.9 0.0 0:52.01 yes
1 root 20 0 21664 12940 9584 S 0.0 0.3 0:03.58 systemdThe shell prints the job numbers and PIDs ([1] 1022) when it starts each job. The two yes processes use about 100% of one CPU each. The load average rises slowly: after about a minute the 1-minute value is 1.35 (it climbs towards 2, the number of busy processes), the 5-minute value is 0.43, and the 15-minute value is 0.15. That is the rising trend of step 1. Without -b, top runs live: P sorts by CPU, M by memory, 1 shows each CPU, and q quits. Lesson 5 uses top to control processes.
5. Read the disk
iostat -d DEVICE 1 2 prints the traffic of a disk, once a second, two times. The first report is the average since boot. Read the second. tps is transfers per second, and the two kB columns are the data read and written each second. On your system the disk is sda, vda or nvme0n1: find it with lsblk. This machine has sda.
iostat -d sda 1 2Device tps kB_read/s kB_wrtn/s kB_dscd/s kB_read kB_wrtn kB_dscd
sda 2.00 0.00 8.00 0.00 0 8 0This is the second report, shortened, of an idle disk: 2 transfers and 8 kB written in the second. A busy disk shows large numbers here.
-x adds the extended columns: r_await and w_await (the average wait of a read and of a write request, in ms) and %util (how busy the disk is; near 100 means saturated). There are 22 columns, and their places change between versions, so the awk script below finds each column by its heading. In the Device line it saves the place of every heading in col; n == 2 keeps the second report; $col["r_await"] is the field at the place saved under that name; and printf lines the columns up. The output comes from another machine, whose disk is nvme0n1, and is not idle. Use your own disk name; your numbers differ.
iostat -dx nvme0n1 1 2 | awk '/^Device/ {for (i = 1; i <= NF; i++) col[$i] = i; n++} n == 2 && NF {printf "%-10s %8s %8s %6s\n", $1, $col["r_await"], $col["w_await"], $col["%util"]}'Device r_await w_await %util
nvme0n1 0.13 4.21 17.50This disk answers a read in 0.13 ms and a write in 4.21 ms, and it is busy 17.5 per cent of the time. A waiting time of tens of milliseconds, or a %util near 100, would point to the disk. The CPU line %iowait of iostat -c is the same number as wa of vmstat.
6. Read the network
ip -s link show NAME prints the counters of one network card: bytes and packets that came in (RX) and went out (TX), with the errors and the dropped packets. ss -s (socket statistics) counts the connections.
List the names of the network cards with ls /sys/class/net. lo is the loopback card. Use the other name: it is enp5s0 here, and it can be eth0, enp0s3, ens33 or another one on your system.
ip -s link show enp5s0 | tail -n 4
ss -s | head -n 3 RX: bytes packets errors dropped missed mcast
1493441 598 0 0 0 0
TX: bytes packets errors dropped carrier collsns
40768 553 0 0 0 0
Total: 121
TCP: 4 (estab 0, closed 0, orphaned 0, timewait 0)The counters count from boot. Read them twice with a pause, and subtract. Errors and drops that grow point to a faulty cable, a full buffer or an overloaded link. Lesson 1 of Part 3, "Inspect the network from the shell", goes deeper.
Your turn
You get the saved measurements of web7, taken while users complained. The machine has 4 CPUs. Save the three files under these names in ~/krlab/linux-admin/load, or take them from the lab files of the course. They are the task data.
load/uptime.txt
10:42:07 up 12 days, 3:14, 2 users, load average: 6.20, 5.80, 3.10load/vmstat.txt
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 6 0 614512 10240 612844 0 0 18200 9800 900 1700 4 3 40 53 0
0 7 0 612980 10240 613120 0 0 20480 8700 880 1650 3 2 38 57 0
1 5 0 610044 10244 613500 0 0 17900 10200 910 1800 5 3 47 45 0
0 8 0 609120 10244 613990 0 0 19500 9100 890 1720 4 3 48 45 0load/free.txt
total used free shared buff/cache available
Mem: 8000 3300 600 10 4100 4400
Swap: 2000 0 2000Read the three files with the tools of this lesson. You may calculate with awk (lesson 5 of Part 1): for example awk 'NR > 2 {s += $16} END {print s / (NR - 2)}' FILE adds the column 16 of every line after the second and prints the mean. Then answer.
Last, write the one-line answer for your lead into ~/krlab/linux-admin/load/answer.txt, with the numbers that prove it.
Check yourself
A server has 8 CPUs and the load average 8.0, 7.5, 2.0. What do you conclude?
All CPUs are busy now, and the load rose in the last 15 minutes. With 8 CPUs, a load of 8 means no process waits yet, so the server is full but not overloaded. A load that stays above 8 is the warning.
free shows 100 MiB in the "free" column and 3,000 MiB in "available". Is the memory short?
No. The kernel uses idle memory for the file cache and gives it back when needed. available counts that memory. Memory is short when available is small and si and so in vmstat stay above zero.
The load average is 12 on 4 CPUs, but top shows 90% idle CPU and vmstat shows a high wa. What waits?
The disk. On Linux the load average counts processes that wait for the disk too. A high load with idle CPUs and a high wa means a slow or saturated disk (or a network disk).
Why do you read the second report of vmstat and iostat, and not the first?
The first report is the average since boot. It hides what happens now. The second report covers the one second that you waited.
Field notes
- Measure first, then change. A change without a number before and after is a guess. Save the output of
uptime,free -m,vmstat 1 5andiostat -dx 1 3with the time, and you can compare. - Look at the trend, not one point.
sar(also insysstat) records these numbers all day, so that you can read last night's peak. Enable it, because a slow hour is gone when someone reports it. topdoes not show short processes that start and end between two screens.vmstatrandcs(context switches) can show their effect.- A sudden rise of CPU, network traffic or processes on a quiet server is also a security signal, for example a crypto-miner or a data copy. MITRE ATT&CK names resource hijacking (T1496).
- In a container (Docker, LXD), the load average belongs to the whole host, and
freeandtopmay show the host's numbers. Compare a load only with the CPUs that you may use (nproc), and check the limit of the container before you size a service.