Linux vmstat Command Tutorial for Beginners (5 Examples)
If you are looking for a command-line utility that you can use to access information about processes, CPU activity, memory, and more, you'll be glad to know that vmstat does this for you.
In this article, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it's worth mentioning that all examples in this article have been tested on an Ubuntu 20.04 LTS machine, but it will work on CentOS and Debian as well.
Linux vmstat command
The vmstat command in Linux reports virtual memory statistics. Following is its syntax:
vmstat [options] [delay [count]]
And here's how the tool's man page explains it:
vmstat reports information about processes, memory, paging, block IO,
traps, disks and cpu activity.
The first report produced gives averages since the last reboot. Addi?
tional reports give information on a sampling period of length delay.
The process and memory reports are instantaneous in either case.
Following are some Q&A-styled examples that should give you a better idea of how the vmstat command works:
Q1. How to use vmstat?
For basic usage, just execute 'vmstat' sans any option.
vmstat
For example, in my case, the above command produced the following output:
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 401160 100252 1307468 0 0 5 17 49 70 0 0 100 0 0
As the first row indicates, the output is divided into six sections. The tool's man page contains detailed information about these values. Here's the excerpt:
Procs
r: The number of runnable processes (running or waiting for run time).
b: The number of processes in uninterruptible sleep.
Memory
swpd: the amount of virtual memory used.
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
si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).
IO
bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).
System
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
CPU
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.
Q2. How to make vmstat auto-update its output?
By default, vmstat produces output once. However, if you want vmstat to automatically update its output, then you can do that by specifying a numeric value as input to the command.
This numeric value acts as delay (in seconds) after which the output is updated. For example:
vmstat 3
This way, vmstat output will get refreshed every 3 seconds.
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 410928 100276 1307480 0 0 5 17 49 70 0 0 100 0 0
0 0 0 410920 100284 1307480 0 0 0 181 108 143 0 0 100 0 0
0 0 0 410920 100292 1307480 0 0 0 7 92 132 0 0 100 0 0
0 0 0 410920 100292 1307480 0 0 0 0 87 124 0 0 100 0 0
...
...
...
Q3. How to make vmstat display slabinfo?
For slabinfo, you need to use the -m command line option. Note that your Linux distribution's kernel needs to support slabinfo for this option to work. If support is there, then you need to run the command with sudo permissions.
sudo vmstat -m
For example, in my case, here's an excerpt of the output produced:
Cache Num Total Size Pages
ufs_inode_cache 0 0 808 40
qnx4_inode_cache 0 0 680 48
hfsplus_attr_cache 0 0 3840 8
hfsplus_icache 0 0 896 36
hfs_inode_cache 0 0 832 39
minix_inode_cache 0 0 672 48
ntfs_big_inode_cache 0 0 960 34
ntfs_inode_cache 0 0 296 55
jfs_ip 0 0 1280 25
xfs_dqtrx 0 0 528 31
xfs_dquot 0 0 496 33
xfs_buf 0 0 384 42
xfs_rui_item 0 0 696 47
xfs_rud_item 0 0 176 46
xfs_inode 0 0 1024 32
xfs_efd_item 0 0 440 37
xfs_buf_item 0 0 272 30
xfs_trans 0 0 232 35
xfs_da_state 0 0 480 34
xfs_btree_cur 0 0 224 36
...
...
...
Q4. How to make vmstat report disk activity summary stats?
Use the -D command-line option for it.
vmstat -D
Here's the output this command produced on my system:
10 disks
2 partitions
15066 total reads
5937 merged reads
1272794 read sectors
20063 milli reading
155026 writes
103687 merged writes
4602472 written sectors
105279 milli writing
0 inprogress IO
126 milli spent IO
Q5. How to make vmstat append timestamp to each output line?
To make sure the vmstat command appends timestamp to each output line, use the -t command line:
vmstat -t
$ vmstat 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 UTC
2 0 0 410116 100432 1308160 0 0 5 17 49 70 0 0 100 0 0 2020-05-08 09:40:05
0 0 0 410116 100432 1308160 0 0 0 88 100 136 0 0 100 0 0 2020-05-08 09:40:07
0 0 0 410116 100440 1308160 0 0 0 18 90 139 0 0 100 0 0 2020-05-08 09:40:09
0 0 0 410116 100440 1308160 0 0 0 0 93 126 0 0 100 0 0 2020-05-08 09:40:11
0 0 0 410368 100448 1308160 0 0 0 10 107 144 0 0 100 0 0 2020-05-08 09:40:13
0 0 0 410368 100448 1308160 0 0 0 0 86 122 0 0 100 0 0 2020-05-08 09:40:15
0 0 0 410368 100448 1308160 0 0 0 0 101 135 0 0 100 0 0 2020-05-08 09:40:17
The highlighted entries show the timestamp included in each line.
Conclusion
Depending on the kind of work you do on the Linux command line, the vmstat command can prove to be really helpful. Apart from the options discussed here, there are a lot of other command-line options that this tool offers. Head here to learn more.