Linux vmstat command tutorial

Linux vmstat command is a useful tool for monitoring performance in Linux system. It comes standard in Ubuntu and Slackware installation. We can get information about virtual memory(vmstat is a short form of virtual memory) and some other things such as Linux processes, cpu activity and block IO.

Here is the Linux vmstat command synopsis from the manual page:

SYNOPSIS
       vmstat [-a] [-n] [delay [ count]]
       vmstat [-f] [-s] [-m]
       vmstat [-S unit]
       vmstat [-d]
       vmstat [-p disk partition]
       vmstat [-V]

Let's see some options(switch) available with vmstat command and examples of the command's output.

Linux vmstat command with '-a' option runs in Ubuntu Linux:

root@ubuntu:~# vmstat -a
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa
 2  0   7376   6228  19944 326180    0    0    19     9  162  345  4  1 94  1

The -a option displays active/inactive memory average values since boot. Here are the meaning of all columns:

The 'procs' field has 2 columns:

  • r - The number of processes waiting for run time.
  • b - The number of processes in uninterruptible sleep (blocked processes).

The 'memory' field has 4 columns:

  • swpd - The amount of used swap space(virtual memory) used.
  • free - The amount of idle memory(free RAM).
  • inact - The amount of inactive memory.
  • active - The amount of active memory.

The 'swap' field has 2 columns:

  • si - Amount of memory swapped in from disk (/s).
  • so - Amount of memory swapped to disk (/s).

The 'io' field has 2 columns:

  • bi - Blocks received from a block device (blocks in).
  • bo - Blocks sent to a block device (blocks out).

The 'system' field has 2 columns:

  • in - The number of interrupts per second, including the clock (System interrupts).
  • cs - The number of context switches per second (Process context switches).

The 'cpu' field has only 4 columns:

  • us: Time spent running non-kernel code. (user time, including nice time).
  • sy: Time spent running kernel code. (system time).
  • id: Time spent idle.
  • wa: Time spent waiting for IO.

We can also use vmstat command with '-a' combined with other options such as in the first command synopsis. See example below:

root@ubuntu:~# vmstat -a -n 10 3
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa
 6  0  10560   7060  20192 324916    0    1    16     8  156  331  3  1 95  1
 1  0  10560   7052  20176 324956    0    0     0     4  117  206  0  0 100  0
 0  0  10560   7052  20200 324956    0    0     0     4  119  213  0  0 100  0
root@ubuntu:~#

The vmstat example above means we want it to delay 10 seconds before the next update and count the update 3 times. The '-n' option is used so that the header does not displayed in every update.

Linux vmstat command with '-f' option runs in Ubuntu Linux:

root@ubuntu:~# vmstat -f
        10109 forks

The -f option prints the number of forks since boot.

Linux vmstat command with '-s' option runs in Ubuntu Linux:

root@ubuntu:~# vmstat -s
       504628 K total memory
       497548 K used memory
       325892 K active memory
        19284 K inactive memory
         7080 K free memory
         9812 K buffer memory
        80136 K swap cache
       995988 K total swap
        12512 K used swap
       983476 K free swap
       104488 non-nice user cpu ticks
          321 nice user cpu ticks
        27624 system cpu ticks
      2521995 idle cpu ticks
        22965 IO-wait cpu ticks
          240 IRQ cpu ticks
         1121 softirq cpu ticks
            0 stolen cpu ticks
       349578 pages paged in
       204564 pages paged out
          309 pages swapped in
         3378 pages swapped out
      4377716 interrupts
      9386803 CPU context switches
   1257512796 boot time
        10114 forks

The -s option prints a table of various event counters and memory statistics.

Linux vmstat command with '-m' option runs in Ubuntu Linux:

root@ubuntu:~# vmstat -m
Cache                       Num  Total   Size  Pages
RAWv6                        23     23    704     23
TCPv6                        24     24   1344     12
ext3_inode_cache           2994   4448    496     16
ext3_xattr                    0      0     48     85
journal_handle              340    340     24    170
journal_head                147    219     56     73
revoke_record               512    512     16    256
kmalloc_dma-512              16     16    512     16
sgpool-128                   24     24   2560     12
sgpool-64                    24     24   1280     12
sgpool-32                    24     24    640     12
scsi_io_context               0      0    104     39
fuse_request                 21     21    376     21
fuse_inode                   18     18    448     18
flow_cache                    0      0     80     51
mqueue_inode_cache           14     14    576     14
hugetlbfs_inode_cache        23     23    344     23
dquot                         0      0    128     32
shmem_inode_cache          5146   5148    448     18
xfrm_dst_cache               24     24    320     12
TCP                          26     26   1216     13
Cache                       Num  Total   Size  Pages
blkdev_queue                 39     39   1200     13
blkdev_requests              48     80    200     20
biovec-256                   10     10   3072     10
biovec-128                   23     42   1536     21
biovec-64                    44     63    768     21
sock_inode_cache            588    588    384     21
file_lock_cache              78     78    104     39
Acpi-Operand               1713   1734     40    102
taskstats                    24     24    328     12
page_cgroup               87220  87890     24    170
proc_inode_cache             68    176    368     22
sigqueue                     56     56    144     28
radix_tree_node            2273   3432    296     13
bdev_cache                   32     32    512     16
sysfs_dir_cache           14684  14705     48     85
inode_cache                 285    805    344     23
dentry                     9863  15660    136     30
buffer_head                3948   4224     64     64
mm_struct                   626    630    448     18
vm_area_struct            13378  13570     88     46
files_cache                 125    147    384     21
Cache                       Num  Total   Size  Pages
signal_cache                160    168    576     14
sighand_cache               146    156   1344     12
task_struct                 250    260   3216     10
anon_vma                   4846   4864     16    256
idr_layer_cache             595    598    152     26
kmalloc-4096                176    184   4096      8
kmalloc-2048                358    368   2048     16
kmalloc-1024                320    336   1024     16
kmalloc-512                1256   1280    512     16
kmalloc-256                 105    112    256     16
kmalloc-128                 759    832    128     32
kmalloc-64                 4155   4480     64     64
kmalloc-32                 1587   1664     32    128
kmalloc-16                 3621   3840     16    256
kmalloc-8                  6638   6656      8    512
kmalloc-192                5902   5985    192     21
kmalloc-96                  625    672     96     42
root@ubuntu:~# 

The -m displays slabinfo.

Linux vmstat command with '-S' option runs in Ubuntu Linux:

root@ubuntu:~# vmstat -S K
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 1  0  12528   5960  11592  79112    0    0    12     7  163  347  4  1 94  1
root@ubuntu:~# vmstat -S M
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 2  0     12      5     11     77    0    0    12     7  163  347  4  1 94  1
root@ubuntu:~# 

The -S followed by k or K or m or M option outputs between 1000, 1024, 1000000, or 1048576 bytes.

Linux vmstat command with '-d' option runs in Ubuntu Linux:

root@ubuntu:~# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
ram0       0      0       0       0      0      0       0       0      0      0
ram1       0      0       0       0      0      0       0       0      0      0
ram2       0      0       0       0      0      0       0       0      0      0
ram3       0      0       0       0      0      0       0       0      0      0
ram4       0      0       0       0      0      0       0       0      0      0
ram5       0      0       0       0      0      0       0       0      0      0
ram6       0      0       0       0      0      0       0       0      0      0
ram7       0      0       0       0      0      0       0       0      0      0
ram8       0      0       0       0      0      0       0       0      0      0
ram9       0      0       0       0      0      0       0       0      0      0
ram10      0      0       0       0      0      0       0       0      0      0
ram11      0      0       0       0      0      0       0       0      0      0
ram12      0      0       0       0      0      0       0       0      0      0
ram13      0      0       0       0      0      0       0       0      0      0
ram14      0      0       0       0      0      0       0       0      0      0
ram15      0      0       0       0      0      0       0       0      0      0
sr0        0      0       0       0      0      0       0       0      0      0
sda    26829  10390  699300  289056  27979  25655  429144 2716764      0    207
sda1     117    695    4186     852    134   3272   27256    4012      0      3
sda2       3      0       6     120      0      0       0       0      0      0
sda3   13846   5123  546682  139176   8400   3794   97608   18872      0     75
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
sda4   12775   3796  146464  147852  19445  18589  304280 2693880      0    148
sda5      67    758    1650     692      0      0       0       0      0      0
root@ubuntu:~# 

The -d reports disk statistics. Here is the meaning of all the columns available in each field:

Reads:

  • total: Total reads completed successfully
  • merged: grouped reads (resulting in one I/O)
  • sectors: Sectors read successfully
  • ms: milliseconds spent reading

Writes:

  • total: Total writes completed successfully
  • merged: grouped writes (resulting in one I/O)
  • sectors: Sectors written successfully
  • ms: milliseconds spent writing

IO:

  • cur: I/O in progress
  • s: seconds spent for I/O

That's all.