Introduction to vmstat command
vmstat command in Linux system is used to report information about processes, memory, paging, block IO, traps, and CPU activity. The first report produced gives average statistics since the last reboot. The process and memory reports are instantaneous in either case. vmstat does not count itself as a running process.
How to Install vmstat
vmstat is a part of sysstat package, which contains performance monitoring tools for Linux. If vmstat is not already installed in the system, you can use the following command to install it.
Install vmstat on CentOS, Fedora, RHEL, Rocky Linux, Alma Linux
$ sudo yum install sysstat
Install vmstat on Ubuntu, Debian, and Linux Mint
$ sudo apt install sysstat
Field Description For vmstat Output
When vmstat command is used without any option, it displays the following information.
ubuntu@golinux:~$ 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 1292 137408 47476 920004 0 0 158 68 572 424 2 1 97 0 0
1. procs
- r: The number of processes waiting for run time.
- b: The number of processes in uninterruptible sleep.
2. 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)
3. swap
- si: Amount of memory swapped in from disk (/s).
- so: Amount of memory swapped to disk (/s).
4. io
- bi: Blocks received from a block device (blocks/s).
- bo: Blocks sent to a block device (blocks/s).
5. system
- in: The number of interrupts per second, including the clock.
- cs: The number of context switches per second.
6. 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.
- st: Time stolen from a virtual machine.
Different examples to use vmstat command
The basic syntax of vmstat command is:
vmstat [option] [delay [count]]
Some options available in vmstat command are:
- -a, --active : active/inactive memory
- -f, --forks : number of forks since boot
- -m, --slabs : slabinfo
- -s, --stats : event counter statistics
- -d, --disk : disk statistics
Now, let's have a look at some practical examples of vmstat command in a Linux system.
1. vmstat command to display active and inactive memory
-a
or --active
option displays active and inactive memory in the system. Active memory is a memory that is in use by a particular process. Inactive memory is a memory that was allocated to a process that is no longer running.
$ vmstat -a
OR
$ vmstat --active
Sample Output:
2. vmstat to display the number of forks since boot
You can view the number of forks since boot using -f
or --forks
switch. It includes the fork, vfork, and clone system calls. Each process is represented by one or more tasks, depending on thread usage.
$ vmstat -f
OR
$ vmstat --forks
Sample Output:
Below output displays the number of forks. It is equivalent to the total number of tasks created. So, the output does not repeat.
ubuntu@golinux:~$ vmstat -f 2887 forks ubuntu@golinux:~$ vmstat -f 2906 forks
3. Display slabs information with vmstat command
-m
or --slabs
switch helps to display slabinfo. You will need root permission to view slabs information.
$ sudo vmstat -m
OR
$ sudo vmstat --slabs
Sample Output:
Here,
- 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
- totpages: Total number of allocated pages
- pslab: Number of pages per slab
4. vmstat command to display disk statistics
-d
or --disk
option is used to display disk statistics.
$ vmstat -d
OR
$ vmstat --disk
Sample Output:
It shows the following information.
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
5. vmstat command to summarize disk statistics
-D
or --disk-sum
option displays the summary of disk statistics.
$ vmstat -D
OR
$ vmstat --disk-sum
Sample Output:
6. vmstat command to display event counter statistics
-s
or --stats
option displays a table of various event counters and memory statistics.
$ vmstat -s
OR
$ vmstat --stats
Sample Output:
7. Display statistics of specific partition using vmstat command
With -p
or --partition
option, you can specify the partition to view its statistics.
$ vmstat -p partition_name
OR
$ vmstat --partition partition_name
Sample Output:
Here,
- reads: Total number of reads issued to this partition
- read sectors: Total read sectors for partition
- writes : Total number of writes issued to this partition
- requested writes: Total number of write requests made for partition
8. vmstat command to add timestamp
-t
or --timestamp
option shows the timestamp in the output.
$ vmstat -t
OR
$ vmstat --timestamp
Sample Output:
9. vmstat command to update information every seconds
You can specify a number after vmstat command which indicates the delay between updates in seconds. It displays the information continuously if the count is not defined.
$ vmstat delay
Sample Output:
Here, -t
option is used to track time.
10. vmstat command to display M number of reports for every N seconds
If we specify another number after delay, it indicates the number of updates. So, the specified count of information is only displayed.
$ vmstat delay count
Sample Output:
The following command reports information every 3 seconds and displays 7 reports altogether.
11. vmstat command to display wide output
-w
or --wide
option enlarges field width for big memory sizes.
$ vmstat -w
OR
$ vmstat --wide
Sample Output:
12. Change output units using vmstat command
The default output value is displayed in kilobytes (1024 bytes). You can change it by using -S
option followed by:
- k: 1000 bytes
- K: 1024 bytes
- m: 1000000 bytes
- M: 1048576 bytes
$ vmstat -S [k|K|m|M]
Sample Output:
For example, to display values in hexadecimal megabytes, you can use -S
option followed by M.
ubuntu@golinux:~$ 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
0 0 1 109 45 896 0 0 187 79 570 449 2 1 97 0 0
To display values in decimal megabytes, you can use -S
option followed by m.
ubuntu@golinux:~$ 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
3 0 1 114 48 940 0 0 183 78 574 450 2 1 97 0 0
Conclusion
vmstat command is an important command which provides useful information about processes, memory, paging, block IO, traps, and CPU activity. We hope you have learned to use vmstat command from this article. If you still have any confusion, ask us in the comment section.
What's Next
20 sar command examples in Linux [Cheat Sheet]
Further Reading