What is vmstat command?

In this article, we’ll demonstrate how we can use the vmstat(View memory status) command as an indispensable memory measurement tool.
Although the primary intended purpose of vmstat command displays virtual memory statistics about processes, memory, and paging, it also provides information about block I/O, traps, and CPU activity.

Verifying that vmstat is availability

The vmstat command is installed as a part of the procps package which installs a set of utilities that are used to report system information. This package will be installed on a Red Hat based system by default even if you’ve performed a minimal type installation of the operating system.

We will verify that vmstat is available in our system by executing the following commands:

[sahil@linuxnix:~] $ which vmstat
/usr/bin/vmstat
[sahil@linuxnix:~] $ rpm -qf /usr/bin/vmstat
procps-3.2.8-36.el6.x86_64

Now that we have verified that vmstat is installed on our system let’s explore some of it’s options.

Getting help on vmstat command

To view a list of the different options available with the vmstat command type vmstat –help.

[sahil@linuxnix:~] $ vmstat --help
usage: vmstat [-V] [-n] [delay [count]]
-V prints version.
-n causes the headers not to be reprinted regularly.
-a print inactive/active page stats.
-d prints disk statistics
-D prints disk table
-p prints disk partition statistics
-s prints vm table
-m prints slabinfo
-t add timestamp to output
-S unit size
delay is the delay between updates in seconds.
unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)
count is the number of updates.
[sahil@linuxnix:~] $

Learn vmstat command with some Examples

Example 1: Executing vmstat without any options:

[sahil@linuxnix:~] $ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
12 8 0 213972992 2251752 113406800 0 0 2122 198 7 7 14 3 81 2 0

Executing the command without any options reveals a single line of output showing current utilization data for the metrics.
Inorder to understand vmstat, we have to understand what each of the above coded strings mean. Given below is a brief description of these metrics.

Procs
The procs data reports the number of processing jobs waiting to run and allows you to determine if there are processes “blocking” your system from operating smoothly.

r: The number of processes waiting for run time.
b: The number of processes in uninterruptible sleep.

The non-zero value for ‘r’ would indicate that the system is loaded because ideally, we should not have processes waiting for CPU time and if they are then that implies that the system is loaded.

Memory
The information displayed in the memory section provides the same data about memory usage as the command free.

swpd: the amount of virtual memory used i.e. the amount of memory that has been swapped out to a file/disk.
free: the amount of idle memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
inact: the amount of inactive memory. (-a option)
active: the amount of active memory. (-a option)

Swap
The swap section reports the rate that memory is sent to or retrieved from the swap system.

The si column reports the amount of memory that is moved from swap to “real” memory per second.
The so column reports the amount of memory that is moved to swap from “real” memory per second.

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

I/O
The io section reports the amount of input and output activity per second in terms of blocks read and blocks written.

The bi column reports the number of blocks received or read from a disk per second. The bo column reports the number of blocks sent or written to a disk per second.

bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).

System
The system section reports data that reflects the number of system operations per second.

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

CPU
The cpu section reports on the use of the system’s CPU resources.

These are percentages of total CPU time.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.
st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.

 

Now after a basic understanding of the meaning of the fields reported by the vmstat command we’ll now proceed to perform some examples.

Example 2: Run vmstat a given number of times with specified time interval.

Like many other performance reporting commands available in Linux we can run vmstat a certain number of times with a defined time interval (in seconds) within each execution of the command.

The syntax is: vmstat [interval] [count]

Here’s a demo:

[sahil@linuxnix:~] $ vmstat 2 4
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
13 3 0 202408816 2270264 114450464 0 0 2164 203 14 5 14 3 80 2 0
13 5 0 202183872 2270264 114450480 0 0 134756 468 142732 102427 37 5 54 5 0
8 7 0 202256464 2270268 114450480 0 0 256835 267 127566 95934 27 3 62 8 0
11 4 0 202245424 2270268 114450496 0 0 252395 312 134198 99872 26 4 61 9 0

The above runs vmstat four times with an interval of 2 seconds between each execution of the command.

Example 3: Run vmstat command and display timestamp of each execution.

If we add the -t option in the previous example, vmstat will now display a timestamp along with it’s output.

[sahil@linuxnix:~] $ vmstat 2 4 -t
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
r b swpd free buff cache si so bi bo in cs us sy id wa st
14 3 0 204200560 2272644 114593640 0 0 2172 203 10 13 14 3 80 2 0 2017-11-06 07:17:43 UTC
7 6 0 204390768 2272648 114593648 0 0 173904 226 160711 125645 26 7 61 6 0 2017-11-06 07:17:45 UTC
55 6 0 204273680 2272648 114593664 0 0 125179 760 132721 126417 25 16 55 4 0 2017-11-06 07:17:47 UTC
11 3 0 204214160 2272648 114594464 0 0 116895 173 138681 104620 23 7 66 4 0 2017-11-06 07:17:49 UTC

Example 4: Listing memory as active and inactive.

Active memory refers to memory that is currently is used by processes running on the system.
Inactive memory refers to memory that was allocated to a process which is no longer running.
So inactive memory isn’t actively in use but is not freed up either.
The information regarding inactive memory is used by the kernel to designate which memory pages to swap out in memory contention situations.

To view this information via vmstat we use the -a option.

[sahil@linuxnix:~] $ 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
7 10 0 204408000 104538992 30714124 0 0 2247 205 11 14 14 3 80 2 0

Example 5: View disk statistics.

We can use the -d option with the vmstat command to view disk statistics for the system.

[sahil@linuxnix:~] $ 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
loop0 0 0 0 0 0 0 0 0 0 0
loop1 0 0 0 0 0 0 0 0 0 0
loop2 0 0 0 0 0 0 0 0 0 0
loop3 0 0 0 0 0 0 0 0 0 0
loop4 0 0 0 0 0 0 0 0 0 0
loop5 0 0 0 0 0 0 0 0 0 0
loop6 0 0 0 0 0 0 0 0 0 0
loop7 0 0 0 0 0 0 0 0 0 0
sdc 36666 11 192566 29780 12181 0 12181 12192 0 41
sda 17698 11 1511250 18932 1788999 0 21192032 639775 0 544
sde 818 11 11643 355 836 0 734052 9373 0 9
sdf 839 11 10402 604 944 0 738772 11687 0 11
sdj 478617 11 29707457 561915 6782 0 198133 24529 0 556
--------------------output truncated for brevity-----------------------------------

Given below is a description of the fields reported in the above output:

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

Example 6: Get a summary of IO operations.

We use -D option with vmstat to report a summary of disk operations that have been performed by the system.

[sahil@linuxnix:~] $ vmstat -D
1493 disks
0 partitions
707725049 total reads
19519656 merged reads
44080672963 read sectors
865497051 milli reading
124849899 writes
17682870 merged writes
4082213297 written sectors
1787151677 milli writing
0 inprogress IO
741039 milli spent IO

Example 7: Display statistics in megabytes.

We can use the -s option with the M argument to display the output of the fields in megabytes instead of kilobytes which is the default.

[sahil@linuxnix:~] $ vmstat -S M 1 2 -t
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
r b swpd free buff cache si so bi bo in cs us sy id wa st
17 17 0 250730 2353 79110 0 0 2328 205 9 13 15 3 79 3 0 2017-11-06 08:05:44 UTC
21 11 0 250905 2353 79110 0 0 461275 36671 209594 152483 51 8 30 11 0 2017-11-06 08:05:45 UTC

Example 8: Display table of event counters and memory statistics.

We use the -s option with vmstat to display a table of event counters and memory statistics.

[sahil@linuxnix:~] $ vmstat -s
1058397440 total memory
813986816 used memory
27804004 active memory
72559568 inactive memory
244410608 free memory
2306836 buffer memory
77805496 swap cache
16777212 total swap
0 used swap
16777212 free swap
46217307 non-nice user cpu ticks
108 nice user cpu ticks
9677364 system cpu ticks
249396934 idle cpu ticks
8117130 IO-wait cpu ticks
618 IRQ cpu ticks
1075081 softirq cpu ticks
0 stolen cpu ticks
7341134666 pages paged in
651585437 pages paged out
0 pages swapped in
0 pages swapped out
2916766333 interrupts
1630479998 CPU context switches
1509857100 boot time
73504771 forks

Example 9: Display information about kernel objects stored in slab layer cache.

Using the vmstat command with the -m option displays the names and sizes of various kernel objects stored in a cache known as the slab layer. Slab info refers to caches of frequently used objects in the kernel, such as buffer heads, inodes, dentries, and so on. Vmstat displays information about some of these caches.

[sahil@linuxnix:~] $ vmstat -m
Cache Num Total Size Pages
acfs_dio_flags_cache 0 0 32 112
acfs_dlm_lock_cache 0 0 240 16
acfs_ofsioerr_cache 0 0 152 25
acfs_ofsiocb_cache 0 0 48 77
acfs_metadata_buffer_cache 0 0 512 8
acfs_ofsbuf_cache 0 0 208 19
acfs_inode_cache 0 0 1296 3
nfs_write_data 41 44 960 4
nfs_read_data 0 0 896 4
nfs_inode_cache 8773 8859 1048 3
nfs_page 18 90 128 30
fscache_cookie_jar 6478 7104 80 48
rpc_buffers 12 12 2048 2
rpc_tasks 13 90 256 15
rpc_inode_cache 36 40 832 4
ext4_inode_cache 1100540 1125156 1000 4
Cache Num Total Size Pages
ext4_xattr 8 88 88 44
ext4_free_block_extents 8 67 56 67
ext4_alloc_context 35 84 136 28
ext4_prealloc_space 212 370 104 37
--------------------output truncated for brevity-------

Given below is a brief description of the fields reported in the above output:

cache: Cache name
num: Number of currently active objects
total: Total number of available objects
size: Size of each object
pages: Number of pages with at least one active object

Conclusion:

This concludes our exploration of the different uses of the vmstat command.
We would like to mention that like many other performance monitoring tools in Linux, vmstat retrieves the information it displays from files in the /proc file system.
Some of the files accessed by vmstat are /proc/meminfo and /proc/stat.

The following two tabs change content below.

Sahil Suri

He started his career in IT in 2011 as a system administrator. He has since worked with HP-UX, Solaris and Linux operating systems along with exposure to high availability and virtualization solutions. He has a keen interest in shell, Python and Perl scripting and is learning the ropes on AWS cloud, DevOps tools, and methodologies. He enjoys sharing the knowledge he's gained over the years with the rest of the community.