Load is a queue, not a percentage
The load average is the number of processes that are running or waiting for a CPU, averaged over one, five, and fifteen minutes. The kernel also counts processes that are in uninterruptible sleep, usually waiting for the disk. The number is not a percentage. A load of 4 on a four-core machine means the queue is about as long as the number of cores, and a load of 8 means that half of the work is waiting.
The current load is in the file /proc/loadavg, and the uptime command prints the same three numbers with the time since boot. Compare the load with the core count, which you can read from the nproc command. A load that stays above the core count for minutes is a sign of CPU pressure.
cat /proc/loadavg; nprocMemory: free and available
free reports the memory of the whole machine. The -m option prints megabytes, which is easier to read than kibibytes. The most important column is available, not free. Linux uses spare memory as a cache for files, so free memory is normally small on a healthy machine. Available memory is the estimate of how much a new program could get without swapping, and it is the number that tells you whether memory is short.
free -mThe row labelled Mem shows the physical memory, and the Swap row shows the swap space on disk. Any use of swap is a sign that the machine once ran short, and heavy swap use while the machine is busy is a sign of real memory pressure.
vmstat: a sample over time
vmstat prints a line of statistics for each interval, which makes it the best tool for seeing change. Give it an interval and a count. The first line is an average since boot, and the following lines cover the interval. The columns are grouped under headings. Under procs, r is the number of runnable processes and b is the number blocked on I/O. Under memory, swpd is swap in use. Under swap, si and so are the amounts swapped in and out per second. Under io, bi and bo are blocks read and written. Under cpu, us, sy, id, wa, and st are the shares of user, system, idle, I/O wait, and stolen time.
vmstat 1 2Read the line for the interval, not the average. Persistent r values above the core count point at CPU pressure. A persistent wa value shows the CPU waiting for storage, and nonzero si and so values show memory pressure that forces swapping.
top: the process table
top shows the same resources and then ranks the processes that use them. Interactive top refreshes the screen, which is awkward in a file. The -b option selects batch mode, which writes plain text, and -n 1 asks for a single frame. Batch mode is the form you want in scripts and in reports.
top -b -n 1 | head -n 12The header lines repeat the load, the task counts, the CPU shares, and the memory totals. The table below it has one row for each process, with the PID, the user, the CPU and memory percentages, and the command. Sort order matters. The interactive keys choose the sort column, and in batch mode the process list is in the default order, so read the %CPU and %MEM columns for the largest values.
Pressure stall information
Newer kernels expose pressure stall information in /proc/pressure. Each file reports the share of time that tasks waited because of a resource, as averages over ten, sixty, and three hundred seconds. A value near zero means no waiting, and a high value means that tasks were stalled. The lab kernel may or may not provide these files, so check that they exist before you rely on them.
Read the resources in a fixed order each time: load first, then memory, then swap, then I/O wait. A habit of the same order makes it easy to see which number changed.
Reading the three together
A machine with a high load, a small available value, and steady swap activity is short of memory, and the processes that use the most memory are the first suspects. A machine with a high load, plenty of available memory, and low I/O wait is short of CPU. A machine with high wait and a long b column is waiting on storage. The three numbers select the next tool to use, which is the purpose of this lesson.
The lab machine has no man pages. Use the --help option of each command to check the flags, and read the proc filesystem lesson in the Linux module for the files that these commands read.
Caches make free look full
Linux keeps recently used file data in memory, and it gives that memory back as soon as a program asks for it. As a result, a healthy machine often shows little free memory and a large buff/cache column. The used column in free excludes the cache, and the available column adds back the part of the cache that can be dropped at once. Reading the free column alone is the most common way to mistake a busy but healthy machine for one that is out of memory.
Swap is not always a symptom
Some swap use is normal on a machine that has been busy for a long time, because the kernel moves rarely used pages out of memory to make room for the cache. The question is whether swap activity is happening now. A nonzero swpd with zero si and so is old, and it is harmless. Nonzero si and so values during a slow task mean that the machine is swapping under load, which is the pressure that makes everything feel slow.
Reading the processes that use memory
When top shows a process with a large %MEM value, check whether the value grows over time or stays flat. A flat value is a large but stable program, and it may be fine. A value that rises between two samples is the one to investigate, because a leak or a runaway cache looks like that. Two batch frames a few seconds apart are enough to see the trend, and the same method works with ps sorted by memory.