Skip to content
Visual explainer 3h to learn 5 steps

Performance: top, vmstat, iostat & free

Tell whether a slow server is short of CPU, memory or disk I/O.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 11 minutes to read.

Topic 35 of 42

In short

When a server is slow, the first job is to find which resource is saturated: CPU, memory, disk I/O or network. top shows load and the busiest processes, free shows memory, vmstat shows run queues, swapping and I/O wait over time, and iostat shows per-disk utilisation and latency.

Explained simply

When someone says the server is slow, your first job is not to fix it. Your first job is to find out what it is short of. A computer has four main resources: the CPU (the brain), memory (RAM), the disks, and the network. Usually one of them is overloaded.

Each tool answers one question. top shows which programs use the most CPU and memory right now. free shows how much memory is really available. vmstat shows, second by second, whether programs are waiting for the CPU or for the disk. iostat shows how busy each disk is.

Linux uses spare memory to cache files, so free memory often looks low on a healthy server. That is normal and good. The number that matters is available, which includes cache the system can give back instantly.

Once you know the bottleneck, the fix becomes clearer: stop or slow down a greedy process, add memory, move work to a faster disk, or apply a tuning profile with tuned-adm.

Press Play on a diagram to walk through it one step at a time.

MapWhich resource is the bottleneck?
Server feels slow
  • CPU
    • Load average above core count
    • High us or sy in top
    • vmstat r column high
  • Memory
    • Low available in free -h
    • vmstat si and so non-zero
    • OOM kills in the journal
  • Disk I/O
    • High wa in top
    • iostat -xz: high %util and await
  • Network
    • ip -s link errors or drops
    • ss shows many stuck connections
CompareTools and what they are best at
top / htop
  • Live view of processes
  • CPU states and load
  • Sort by CPU or memory
  • Snapshot, not history
vmstat / free
  • System-wide numbers
  • Run queue and blocked tasks
  • Swap activity si and so
  • Good for spotting trends
iostat / sar
  • Per-disk latency and queue
  • From the sysstat package
  • sar keeps history
  • Answers: was it slow last night?

In depth

Load average, explained properly

uptime and top print three load averages for the last 1, 5 and 15 minutes. On Linux, load counts tasks that are running or waiting for a CPU, plus tasks stuck in uninterruptible sleep, which is usually waiting for disk or network storage. So load is not only CPU.

Compare it with the number of CPUs from nproc. A load of 4 on a 4-core machine means the CPUs are fully used with no queue. A load of 12 on 4 cores means tasks are waiting. If load is high but CPU idle is also high, the waiting tasks are probably blocked on I/O, so check wa and iostat next. Read the three numbers as a trend: 9.0 5.0 2.0 means it is getting worse.

  • Load below the CPU count: no CPU queue
  • High load with high wa: disk or NFS is the problem
  • cat /proc/pressure/cpu shows how often tasks waited (PSI)

Reading the CPU line in top

The %Cpu(s) line splits CPU time into states. us is user programs, sy is kernel work for them, ni is low-priority (niced) programs, and id is idle. wa is time the CPU was idle while waiting for I/O to finish. hi and si are hardware and software interrupts, often network traffic. st is steal: time a virtual machine wanted the CPU but the hypervisor gave it to another VM.

High us points at an application. High sy points at many system calls or context switches. High st means the host is overcommitted, and nothing inside the VM will fix it. Press 1 in top to see each CPU, P to sort by CPU, M to sort by memory, and c to show full command lines.

MapWhat each CPU state points to
%Cpu(s) in top
  • us / ni
    • Application code
    • Profile or scale the app
  • sy
    • Kernel work, syscalls
    • Too many small reads or forks
  • wa
    • Waiting for disk
    • Check iostat -xz
  • hi / si
    • Interrupts
    • Often heavy network traffic
  • st
    • Steal on a VM
    • Host is overloaded

Memory: free, swap and the OOM killer

In free -h, used is memory held by programs, buff/cache is file cache, and available estimates how much new programs could get without swapping. When available stays low and vmstat shows non-zero si and so (swap in and swap out) every second, the server is short of memory, and everything slows down because the disk becomes part of memory.

Some swap used with si and so at zero is fine: idle pages were moved out once. If memory runs out completely, the kernel's OOM killer ends a process to save the system. journalctl -k | grep -i 'out of memory' shows which process was killed. ps -eo pid,comm,rss --sort=-rss | head lists the biggest users by resident memory.

Disk I/O with vmstat and iostat

In vmstat 1, the r column counts tasks waiting for a CPU and b counts tasks blocked, usually on I/O. bi and bo are blocks read and written per second. Always ignore the first line: it is an average since boot.

iostat -xz 1 (from the sysstat package) shows each disk. r_await and w_await are the average milliseconds per read and write, including queue time. aqu-sz is the average queue length. %util is the share of time the device was busy. On a single spinning disk, %util near 100 means saturated. On SSDs, NVMe and RAID, which handle many requests in parallel, 100% util can still have room, so trust await and the queue more.

  • await of a few ms on SSD is normal; hundreds of ms is trouble
  • iotop or pidstat -d 1 shows which process does the I/O
  • sar -d shows history if sysstat collection is enabled
FlowA 60-second triage
  1. 1
    uptime and nproc

    Load vs CPU count, rising or falling?

  2. 2
    top

    CPU states and top processes

  3. 3
    free -h

    Is available memory low?

  4. 4
    vmstat 1 5

    r, b, si, so, wa over time

  5. 5
    iostat -xz 1 3

    await and %util per disk

  6. 6
    Name the bottleneck

    Then find the process behind it

Priorities and tuned profiles

Two knobs change how resources are shared. nice sets CPU priority from -20 (most favoured) to 19 (least). Start a batch job with nice -n 10 tar ..., or lower a running one with renice -n 10 -p PID. Only root can raise priority. ionice -c3 makes a process use the disk only when nobody else needs it.

tuned applies whole sets of kernel settings. tuned-adm list shows profiles, tuned-adm recommend suggests one for this hardware, and tuned-adm profile NAME applies it and keeps it after reboot. virtual-guest suits VMs, throughput-performance suits busy servers, latency-performance favours fast response, and balanced is a general default. Setting a tuned profile is an RHCSA objective.

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    Read load average and CPU states~35m
  2. 2
    Check memory and swap with free~35m
  3. 3
    Spot I/O wait with vmstat and iostat~35m
  4. 4
    Find top consumers with ps --sort~35m
  5. 5
    Apply profiles with tuned-adm~35m

Key points

  • Load average counts runnable tasks and tasks in uninterruptible I/O wait, so compare it with the CPU count.
  • Linux uses free RAM for cache; look at the available column, not free.
  • Run vmstat 1 or iostat -xz 1 for a few seconds; the first line is an average since boot.
  • tuned-adm applies tuned profiles such as virtual-guest or throughput-performance.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
uptime; nproc
top -b -n 1 | head -15
free -h
vmstat 1 5
iostat -xz 1 3
ps -eo pid,comm,%cpu,%mem --sort=-%mem | head
sudo tuned-adm recommend && sudo tuned-adm active

Triage: CPU, memory, I/O, top consumers and tuning profile.

High load but idle CPUs

A 4-core VM shows load 14 but users say the web app is only slow, not dead. Step 1: top shows id at 70% and wa at 25%, so the CPUs are mostly waiting. Step 2: vmstat shows b around 10, many blocked tasks. Step 3: iostat shows sdb with w_await of 400 ms. Step 4: pidstat -d finds a backup job writing heavily to sdb. Fix: lower its I/O priority now, and move the backup to a quieter time.

bash
uptime; nproc
top -b -n 1 | sed -n 3p        # %Cpu(s) line
vmstat 1 5                       # watch b and wa
iostat -xz 1 3                   # find the slow disk
pidstat -d 1 5                   # which process writes
sudo ionice -c3 -p 4321          # idle I/O class for the backup
sudo renice -n 15 -p 4321

Is it really out of memory?

free -h shows only 300 MB free on a 16 GB server, and someone wants to add RAM. Step 1: look at available, not free; it shows 9 GB, because most memory is file cache. Step 2: vmstat shows si and so at 0, so nothing is swapping. Step 3: the server is fine on memory; the slowness is elsewhere. If available had been near zero with steady swapping, the next step would be finding the biggest process by RSS.

bash
free -h
#        total  used  free  shared  buff/cache  available
# Mem:    15Gi  6.1Gi 300Mi  200Mi      9.2Gi       9.0Gi
vmstat 1 5        # si and so should be 0
ps -eo pid,comm,rss --sort=-rss | head -5
journalctl -k --since today | grep -i 'out of memory'

Quick reference

  • Compare load average with nproc; the 1/5/15 trend matters more than one value
  • top keys: 1 per CPU, P sort by CPU, M sort by memory, c full command
  • free -h: read the available column, not free
  • vmstat 1 5: skip line one; watch r, b, si, so and wa
  • iostat -xz 1: r_await, w_await and aqu-sz show disk trouble
  • ps -eo pid,comm,%cpu,%mem --sort=-%cpu | head lists top consumers
  • nice -n 10 CMD starts low priority; renice -n 10 -p PID changes it
  • tuned-adm recommend, tuned-adm profile virtual-guest, tuned-adm active

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1An 8-core server has load averages 3.1 2.9 3.0. What does that suggest?

Question 2

2In top, which value means the hypervisor is giving your VM's CPU time to others?

Question 3

3Which sign shows a real memory shortage?

Question 4

4Why should you ignore the first line of vmstat 1?

Question 5

5Which command makes a tuning profile active and keeps it after reboot?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Print the 5 processes using the most memory and the 5 using the most CPU.

  2. Exercise 2medium

    Create CPU load on purpose and watch it in vmstat and top, then end it.

  3. Exercise 3hard

    Show the recommended tuned profile, switch to it, and confirm. Then start a long tar job at the lowest CPU and I/O priority.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is a good load average?
It depends on the CPU count. As a rule of thumb, load at or below nproc is fine; well above it means tasks are queuing, for CPU or for I/O.
Why does Linux show almost no free memory?
It uses spare RAM as file cache to speed things up and gives it back when programs need it. Look at the available column instead.
Is using swap bad?
A little swap used is normal. Constant swapping in and out, seen as non-zero si and so in vmstat, is the real problem.
Where do I get iostat and sar?
Both come from the sysstat package: dnf install sysstat or apt install sysstat. Enable the sysstat service so sar collects history.

Go deeper

ShareXLinkedInWhatsApp