11 “vmstat” command examples on Linux – View cpu, memory, io statistics

By | September 15, 2023

In this article, we want to introduce you to 'vmstat' command, which is one of the top monitoring tools that Linux/UNIX users should know of. The vmstat command is used to retrieve the statistics of CPU, virtual memory, paging, processes, threads, disks, traps, I/O blocks, and so on.

This useful command will help you a lot in managing, diagnosing, and troubleshooting system resources.

Installation

To use vmstat, your Linux/UNIX systems need to have "sysstat" package installed. In case your operating system does not have it installed yet, run the following command:

For Debian/Ubuntu

$ sudo apt-get install sysstat

On CentOS/RedHat

$ sudo yum install sysstat

or

$ sudo dnf install sysstat

Command Syntax

To use vmstat, refer to the command syntax below:

$ vmstat [options] [delay [count]]
  • [options]: There are some options you can use with vmstat command, depending on the use cases. For instance, run command 'man vmstat' to get detailed information about vmstat and its versions.
  • [delay]: Displays the delay between updates in seconds. If no delay is specified, only one report is printed with the average values since boot.
  • [count]: Outputs number of updates. If no count is specified when delay is defined, the default value is infinite.

Using the vmstat command with examples

Let's take a look at how we can use vmstat command in some example cases.

1. Display memory, process and CPU statistics

By running command vmstat only without any options, output will give us information about processes, memory, swap, IO, system, and CPU.

These outputs are the average for each statistic since the computer was last rebooted. They are not statistics at the time when executing the command.

$ vmstat
procs  -----------memory----------- ---swap--- ----io---- --system-- ------cpu------
 r  b  swpd  free     buff  cache    si   so    bi    bo   in   cs   us sy id  wa st
 0  0     0  2165936  66984 1087708   0    0    57    13   78  104   0  0  99  1  0

Output explanation

- Procs (Process) column

  • r: Number of executable processes. These are processes that have been running or waiting for a new cycle.
  • b: The number of processes blocked, waiting for I/O to complete. These processes are in a continuous sleep state. In fact, these processes are not "sleeping", but they are just blocking the system call. When the program calls a function or a service located in the OS kernel, it cannot be interrupted until the operation is completed.

- Memory column

  • swpd: the amount of swap memory used
  • free: the amount of idle memory
  • buff: the amount of memory used as buffers
  • cache: the amount of memory used as cache

- Swap column

  • si: Amount of memory swapped in from disk
  • so: Amount of memory swapped to disk

- IO column

  • bi: Blocks are received from a block device. Number of data blocks used to swap virtual memory back to RAM (KiB/s)
  • bo: Blocks sent to a block device. Number of data blocks used to swap virtual memory out of RAM and into swap space (KiB/s)

- System column

  • in: The number of interrupts per second, including the clock
  • cs: The number of context switches per second

- CPU column

  • us: Time spent running non-kernel code (user time, including nice time)
  • sy: Time spent running kernel code
  • id: Time spent idle
  • wa: Time spent waiting for IO
  • st: Time stolen from a virtual machine. This is the time a virtual machine has to wait for the hypervisor to finish servicing other virtual machines before it can come back and process this virtual machine.

2. Display active and inactive memory

You can get the active or inactive memory information by using '-a' or '--active' option.

$ vmstat -a
procs -----------memory----------   ---swap-- -----io---- --system-- ------cpu-----
r  b   swpd  free   inact   active   si   so   bi    bo    in   cs   us sy id  wa st
0  0      0 2165936 1105960 410608    0    0   68    15    80  107   0  0  99  1  0

3. To monitor statistics in real-time

The syntax of vmstat command has delay value to output statistics in specific intervals of time (second). You can combine delay with option '-t' or '--timestamp' to monitor system resources in every 2 seconds.

$ vmstat -t 2
procs -----------memory----------   ---swap-- -----io---- --system-- ------cpu-----   -----timestamp-----
r  b   swpd   free   buff  cache     si   so    bi    bo   in   cs   us sy id  wa st                  +07
2  0      0 1956760  72860 1304724    0    0    40    13   75   96    0  0 99   1  0  2023-08-28 14:23:57
0  0      0 1956760  72860 1304724    0    0     0     0  152  172    0  0 100  0  0  2023-08-28 14:23:59
0  0      0 1956760  72860 1304724    0    0     0     0  163  187    0  0 100  0  0  2023-08-28 14:24:01
0  0      0 1956760  72860 1304724    0    0     0     0  165  203    0  0 100  0  0  2023-08-28 14:24:03
0  0      0 1956760  72860 1304724    0    0     0     0  156  178    0  0 100  0  0  2023-08-28 14:24:05

To limit the number output for reporting, assume you only need 7 records in every 5 seconds, run command with 'count' value after 'delay' value:

$ vmstat -t 5 7
procs -----------memory----------   ---swap-- -----io---- --system-- ------cpu-----   -----timestamp-----
 r  b   swpd   free   buff  cache     si   so    bi    bo   in   cs  us sy id  wa st                  +07
 0  0      0 1986580  72884 1304724    0    0    39    12   75   96   0  0 99   1  0  2023-08-28 14:29:17
 0  0      0 1986580  72884 1304724    0    0     0     0  143  175   0  0 100  0  0  2023-08-28 14:29:22
 0  0      0 1986580  72884 1304724    0    0     0     0  129  163   0  0 100  0  0  2023-08-28 14:29:27
 0  0      0 1986580  72884 1304724    0    0     0     0  131  170   0  0 100  0  0  2023-08-28 14:29:32
 2  0      0 1986580  72884 1304724    0    0     0     0  132  168   0  0 100  0  0  2023-08-28 14:29:37
 1  0      0 1986580  72884 1304724    0    0     0     0  138  171   0  0 100  0  0  2023-08-28 14:29:42
 0  0      0 1986580  72884 1304724    0    0     0     0  140  167   0  0 100  0  0  2023-08-28 14:29:47

4. Change output units between kilobyte or megabyte

You can use "-S " (S is uppercase) or "--unit " option to change output unit of memory sizes.

  • k: 1000 bytes
  • K: 1024 bytes
  • m: 1000000 bytes
  • M: 1048576 bytes
$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs  us sy id wa st
1  0      0 1976780  73060 1304752  0    0    37    12   75   95   0  0 99  1  0
$ 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 st
2  0      0   2024     74   1336    0    0    37    12   75   95   0  0 99  1  0
$ 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 st
1  0      0   1930     71   1274    0    0    37    12   75   95   0  0 99  1  0
$ 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 st
2  0      0 2023964  74813 1336066  0    0    37    12   75   95   0  0 99  1  0
$ 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 st
1  0      0 1976528  73060 1304752  0    0    37    12   75   95   0  0 99  1  0

5. Retrieving disks read/write and i/o statistics

'vmstat' command provides option "-d" (d is lowercase) or "–disk" to report disk statistics. Run command as following:

$ vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
loop0     14      0      34     178      0      0       0       0      0      0
loop1     52      0    2156    3633      0      0       0       0      0      2
loop2     59      0    2172    2872      0      0       0       0      0      2
loop3     57      0    2188    2582      0      0       0       0      0      2
loop4     81      0    2302    1078      0      0       0       0      0      1
loop5     83      0     880     676      0      0       0       0      0      0
loop6     48      0     718    2637      0      0       0       0      0      2
loop7     43      0     690     794      0      0       0       0      0      0
sda    26306   9352 2073482  613971   8499  10980  683138   12540      0    121
sr0       27      0    2096    3494      0      0       0       0      0      3
dm-0   34116      0 2043738 1656604  19321      0  683128   18900      0    121
dm-1     194      0    8616     328      0      0       0       0      0      0
loop8     50      0     720    6787      0      0       0       0      0      6
loop9     54      0    2138    2307      0      0       0       0      0      1
loop10   360      0    8612    4899      0      0       0       0      0      3

6. Retrieving specific partition read/write statistics

To view statistics regarding a particular partition, use the "-p" or "--partition" option including partition identifier as parameter. With the example below, we try to get the information of "sda3" partition:

$ vmstat -p sda3
sda3            reads      read sectors      writes  requested writes
                26001           2055050        9114            691584

The information returned the total number of 'reads' and 'writes' issued to that partition, as well as the number of 'sectors' included.

7. Display summarization of disk statistics

To quickly display summary statistics for disk activity, use the "-D" (D is uppercase) or "--disk-sum" option.

$ vmstat -D
           31 disks
            3 partitions
        63988 total reads
         9352 merged reads
      4240974 read sectors
      2351606 milli reading
        29764 writes
        11408 merged writes
      1387402 written sectors
        32699 milli writing
            0 inprogress IO
          303 milli spent IO

8. Display slab information

A slab represents a contiguous piece of memory, usually made of several virtually contiguous pages. The slab is the actual container of data associated with objects of a specific kind of the containing cache. To retrieve slab information, use the "-m" or "--slabs" option as shown in the following example.

Note: this option is required to be run as sudo privileges.

$ vmstat -m
vmstat: your kernel does not support slabinfo or your permissions are insufficient
$ sudo vmstat -m
Cache                       Num  Total   Size  Pages
isofs_inode_cache            23     23    688     23
ovl_inode                    66     66    728     22
nf_conntrack                 97    160    256     16
kvm_async_pf                  0      0    136     30
kvm_vcpu                      0      0  11520      1
kvm_mmu_page_header           0      0    184     22
x86_emulator                  0      0   2688     12
ext4_groupinfo_4k           374    374    184     22
…..
  • cache: Cache name
  • num: Number of currently active objects in the cache
  • total: Total number of available objects in the cache
  • size: Size of each object in the cache
  • pages: Number of memory pages with at least one active object currently associated with the cache

9. Displays a table of various event counters and memory statistics

To display a page of event counters and memory statistics, use the "-s" (s is lowercase) or "--stats" option.

$ vmstat -s
      4005240 K total memory
      1133636 K used memory
       665568 K active memory
      1558884 K inactive memory
      1347348 K free memory
        70840 K buffer memory
      1453416 K swap cache
      3805180 K total swap
            0 K used swap
      3805180 K free swap
         3029 non-nice user cpu ticks
          642 nice user cpu ticks
         4449 system cpu ticks
       206506 idle cpu ticks
        20692 IO-wait cpu ticks
            0 IRQ cpu ticks
          453 softirq cpu ticks
            0 stolen cpu ticks
      1258429 pages paged in
       436673 pages paged out
            0 pages swapped in
            0 pages swapped out
       300233 interrupts
       564701 CPU context switches
   1693303116 boot time
         5055 forks

Although the reported statistics are largely the same as those generated from the default vmstat command, some of the data is broken down in more detail. For example, the default output combines both 'nice user' and 'non-nice user' CPU times into the 'us' column, while the "--stats" option lists these statistics separately.

$ vmstat
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 1347348  71024 1453428   0    0   278   97   89  162    1  1 93  5  0

10. Display fork information

A fork is a Unix or Linux system call to create a new process from an existing running process. To display the number of forks since system is started, run command vmstat with option "-f" or "--forks".

$ vmstat -f
        27108 forks

11. Combine vmstat with other commands to modify the output

In some situations, you want to get some specific statistics of output only from vmstat command. By combining vmstat with other commands like 'grep', 'sed' or 'awk', you can display specific output. For example:

Display all detail statistics of only CPU and Memory

$ vmstat -s | grep -i 'cpu\|memory' | sed 's/ *//'
4005240 K total memory
1171764 K used memory
1228184 K active memory
1427588 K inactive memory
848032 K free memory
103044 K buffer memory
12399 non-nice user cpu ticks
4805 nice user cpu ticks
14775 system cpu ticks
1260667 idle cpu ticks
33472 IO-wait cpu ticks
0 IRQ cpu ticks
1064 softirq cpu ticks
0 stolen cpu ticks
2210691 CPU context switches

Display some specific statistics like process (r), memory (free), io (bi, bo), cpu (us, sy, id, wa) and change its header name

$ vmstat | awk ' END {print "No_process_waiting_for_CPU: " $1, "\nMemory_Idle: " $4, "\n\nSwapped_In_memory: " $9, "\nSwapped_Out_Memory: " $10, "\n\nCPU_time_runnning_user_code: " $15, "\nCPU_time_runnning_system_code: " $16, "\n\nInterupts_per_sec: " $13, "\nContext_switches_per_sec: " $14} '| column -t
No_process_waiting_for_CPU:     2
Memory_Idle:                    848032
Swapped_In_memory:              161
Swapped_Out_Memory:             258
CPU_time_runnning_user_code:    95
CPU_time_runnning_system_code:  2
Interupts_per_sec:              1
Context_switches_per_sec:       1

Batch Mode

In batch mode, vmstat will print only the data rows in real-time without any headers. This is useful when you need to continuously monitor the values of various system parameters. Here are some examples:

vmstat -n 1 | (while read; do echo "$(date +%Y%m%d.%H%M%S) $REPLY"; done) | tail -n +3
$ vmstat -n 1 | (while read; do echo "$(date +%Y%m%d.%H%M%S) $REPLY"; done) | tail -n +3
20230902.113628  2  0 9376224 4176940 270532 4831568    2   10    23    89  353   41 32  8 60  0  0
20230902.113629  2  0 9376224 4168888 270532 4842960    0    0     0   276 2533 9369  7  4 88  0  0
20230902.113630  1  0 9376224 4175212 270532 4834124    4    0     4     0 2571 8564 10  5 85  0  0
20230902.113631  2  0 9376224 4201604 270532 4833476    0    0     0   152 2483 8038  9  3 88  0  0
20230902.113632  1  0 9376224 4200876 270532 4830404    0    0     0     0 2413 8063  8  3 89  0  0
^C
$

Another command example to print data in batch mode:

while sleep 1 ; do echo `date '+%Y%m%d.%H%M%S'` `vmstat | head -3 | tail -1` ; done
$ while sleep 1 ; do echo `date '+%Y%m%d.%H%M%S'` `vmstat | head -3 | tail -1` ; done
20230902.113715 1 0 9375968 4171416 271128 4843600 2 10 23 89 353 41 32 8 60 0 0
20230902.113716 1 0 9375968 4195572 271128 4830948 2 10 23 89 353 41 32 8 60 0 0
20230902.113717 1 0 9375968 4194060 271128 4828620 2 10 23 89 353 41 32 8 60 0 0
20230902.113718 1 0 9375968 4212476 271136 4828676 2 10 23 89 353 41 32 8 60 0 0
20230902.113719 2 0 9375968 4203164 271136 4824796 2 10 23 89 353 41 32 8 60 0 0
^C
$

Batch mode printing is useful for logging the output for future examination or in the event of some kind of system problem. Then can be logged to a file, or even a database.

Print CPU Usage

Here is a special usecase of vmstat that prints the average cpu usage:

echo $[100-$(vmstat 1 2|tail -1|awk '{print $15}')]
16

This command can be useful to detect the load on the cpu at any given time.

Conclusion

vmstat command can be a useful tool for performance problems related to system resources. We hope you enjoyed and will have more knowledge in the management of Linux systems after going through the examples explained in this article.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

Leave a Reply

Your email address will not be published. Required fields are marked *