Skip to content

Linux Foundations / Manage a Linux host ยท Step 36 of 50

Lesson: Monitoring the CPUSwitch
โ† Linux Foundations

System administration

Monitoring the CPU

Read load averages and CPU time breakdowns to judge how busy a machine is.

Two questions matter when a machine is slow: how much work is waiting, and what are the CPUs spending their time on?

Load average

bash
$ uptime
 10:30:12 up 3 days,  2:26,  2 users,  load average: 1.20, 0.85, 0.60
$ nproc
4

The three numbers are averages over the last 1, 5 and 15 minutes. On Linux, load counts tasks that are running or waiting for a CPU, and tasks in uninterruptible sleep, often waiting on disk. It is not a percentage.

Compare it with the number of CPUs. A load of 4 on 4 CPUs is roughly full; the same load on 1 CPU means work is queuing. Because disk waits count too, a high load does not always mean the CPU is the problem.

If the 1 minute figure is well above the 15 minute figure, load is rising.

Where the time goes

bash
$ 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
 2  0      0 214000  91000 2800000   0    0    12    30  210  400  5  2 92  1  0
 3  0      0 213500  91000 2800000   0    0     0     0 1900 2100 70  8 22  0  0
 2  0      0 213400  91000 2800000   0    0     0    16 1850 2050 72  6 22  0  0

The first line is an average since boot, so look at the later ones. r is the number of runnable tasks. The CPU columns are:

  • us user code
  • sy kernel code
  • id idle
  • wa idle while waiting for I/O
  • st time stolen by a hypervisor in a virtual machine

Newer versions may show extra columns.

Per CPU and per process

The sysstat package, often not installed by default, adds:

bash
mpstat -P ALL 1     # each CPU separately
pidstat 1           # CPU use per process

One CPU at 100% while others idle often means a single-threaded program is the limit. Without sysstat, press 1 in top, or run:

bash
ps aux --sort=-%cpu | head -5

Note that ps reports CPU time divided by the process's whole lifetime, so it can differ from what top shows right now.

Try it

Run nproc and uptime. Then start yes > /dev/null &, wait a minute, and check uptime and vmstat 1 5 again. Stop it with kill %1. The processes and cron lab gives you a shell for this.

Check your understanding

Monitoring the CPU

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.