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 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 18.04 LTS machine.
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 on 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
2 0 805624 770924 41792 838568 2 10 35 57 392 225 10 2 86 1 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 824320 1067960 20500 507612 2 10 36 56 412 296 11 2 86 1 0
0 0 824320 1068116 20532 507176 0 0 0 39 2648 6093 4 1 93 1 0
0 0 824320 1062420 20548 507160 0 0 0 48 2603 5895 5 1 93 1 0
0 0 824320 1060180 20556 507152 0 0 0 289 2815 6092 4 1 94 0 0
1 0 824320 1059940 20580 509216 0 0 0 31 2578 5937 5 1 93 1 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
ext4_groupinfo_4k 1568 1568 144 28
drm_i915_gem_request 112 112 576 28
i915_vma 934 1012 704 23
drm_i915_gem_object 896 1050 768 21
RAWv6 140 140 1152 28
UDPv6 104 104 1216 26
tw_sock_TCPv6 17 17 240 17
request_sock_TCPv6 0 0 304 26
TCPv6 60 60 2176 15
kcopyd_job 0 0 3312 9
dm_uevent 0 0 2632 12
cfq_io_cq 442 442 120 34
cfq_queue 408 408 240 17
bsg_cmd 0 0 216 18
mqueue_inode_cache 68 68 960 17
fuse_request 40 40 400 20
fuse_inode 19 19 832 19
ecryptfs_inode_cache 0 0 1024 16
...
...
...
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:
21 disks
8 partitions
158637 total reads
31589 merged reads
7376734 read sectors
2099724 milli reading
198763 writes
553378 merged writes
11743120 written sectors
6396664 milli writing
0 inprogress IO
1248 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 IST
1 0 822528 855556 36820 689220 1 9 34 54 62 68 11 2 86 1 0 2018-09-27 16:29:40
0 0 822528 849612 36828 692792 0 0 0 30 2894 7141 7 2 91 1 0 2018-09-27 16:29:42
1 0 822528 851820 36856 697288 0 0 0 276 3081 7072 17 2 80 1 0 2018-09-27 16:29:44
0 0 822528 850968 36856 697420 0 0 0 0 3025 8067 7 2 91 0 0 2018-09-27 16:29:46
0 0 822528 853480 36856 693360 0 0 0 0 2762 6285 5 1 93 0 0 2018-09-27 16:29:48
0 0 822528 853128 36872 692796 0 0 0 140 2881 6534 6 1 91 2 0 2018-09-27 16:29:50
0 0 822528 846924 36872 697108 0 0 0 0 2843 6637 6 2 93 0 0 2018-09-27 16:29:52
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.