CPU usage analyses: vmstat

Performance Diagnosis
Technical articles
vmstat
uses information from the following files:
  • /proc/meminfo
  • /proc/stat
  • /proc/*/stat
where * represents the process IDs

​​CPU usage and vmstat
vmstat does more than give virtual memory statistics. 

Note, information on   vmstat and memory usage is available in this article .

vmstat can show:
  • How many processes running
  • How the CPU is used
  • How many interrupts the CPU receives
  • How many context switches the scheduler performs
    
The modes of vmstat
It is useful to think of vmstat having two modes:
  • Average mode
  • Sampling mode

The first line is average mode.

If delay parameter is specified, subsequent lines are sample mode. vmstat is most useful in sampling mode. 
delay: The amount of seconds delay before the next line is displayed
count: The amount of lines displayed
    
The best arguments for CPU usage analyses are:
vmstat [-n] [-s] [delay [count]]

  • -n Useful for scripting or redirecting the output for a file that will be analyzed in a spreadsheet. The header is only displayed once. Without -n, the header is displayed periodically to make reading the data easier over time.
  • -s Cannot be run in sample mode. Prints out vm table, which is memory and CPU statistics since the system was booted
    

If the number of  context switches is less than the number of timer interrupts (the "in" column),  that probably means that most of the time when the timer interrupt fired, the scheduler did not have any work to do.
    

vmstat Output
vmstat output has a header that takes up two lines. The first line indicate categories of output and the second line the specific fields within those categories.

The categories and their associated fields that are useful for CPU analyses are:
  • procs (processes)
    • r: run queue (number of processes waiting for run time)
    • b: number of processes in uninterruptable sleep (i.e., blocked for resources such as I/O, paging, et cetera)​
  • system (number of times kernel switches into kernel code)
    • in: the number of interupts per second
    • cs: the number of context switches per second
  • cpu (percentages of total cpu time)
    • us: user time (running non-kernel code)
    • sy: system time (running kernel code) us: user time (running non-kernel code)
    • id: idle time
    • wa: waiting for I/O
    • st: stolen from a virtual machine

The categories and their associated fields that are useful for other analyses are:
  • memory (RAM usage)
    • swpd: amount of virtual memory used
    • free: amount of idle memory
    • buff: amount of memory used as buffers
    • cache: amount of memory used as cache
    • inact: amount of inactive memory (appears with -a option)
    • active: amount of active memory (appears with -a option)
  • swap (pages pulled in and out of swap)
    • si: amount of memory swapped in from disk (/s) (per second)
    • so: amount of memory swapped out from disk (/s) (per second)
  • io (input/output, i.e., disk usage)
    • bi: blocks received from a block device (blocks/s)
    • bo: blocks sent to a block device (blocks/s)


Suggestions for Future Learning
This information will be in the second edition of UNIX For Application Support Staff . The ETA for the second edition is December 1, 2016.



Performance Diagnosis
Technical articles