Table of Contents
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
- r: The number of processes waiting for run time.
- b: The number of processes in uninterruptible sleep.
- 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)
- si: Amount of memory swapped in from disk (/s).
- so: Amount of memory swapped to disk (/s).
- bi: Blocks received from a block device (blocks/s).
- bo: Blocks sent to a block device (blocks/s).
- in: The number of interrupts per second, including the clock.
- cs: The number of context switches per second.
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
--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
$ vmstat --active
2. vmstat to display the number of forks since boot
You can view the number of forks since boot using
--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
$ vmstat --forks
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
--slabs switch helps to display slabinfo. You will need root permission to view slabs information.
$ sudo vmstat -m
$ sudo vmstat --slabs
- 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
--disk option is used to display disk statistics.
$ vmstat -d
$ vmstat --disk
It shows the following information.
- total: Total reads completed successfully
- merged: grouped reads (resulting in one I/O)
- sectors: Sectors read successfully
- ms: milliseconds spent reading
- total: Total writes completed successfully
- merged: grouped writes (resulting in one I/O)
- sectors: Sectors written successfully
- ms: milliseconds spent writing
- cur: I/O in progress
- s: seconds spent for I/O
5. vmstat command to summarize disk statistics
--disk-sum option displays the summary of disk statistics.
$ vmstat -D
$ vmstat --disk-sum
6. vmstat command to display event counter statistics
--stats option displays a table of various event counters and memory statistics.
$ vmstat -s
$ vmstat --stats
7. Display statistics of specific partition using vmstat command
--partition option, you can specify the partition to view its statistics.
$ vmstat -p partition_name
$ vmstat --partition partition_name
- 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
--timestamp option shows the timestamp in the output.
$ vmstat -t
$ vmstat --timestamp
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
-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
The following command reports information every 3 seconds and displays 7 reports altogether.
11. vmstat command to display wide output
--wide option enlarges field width for big memory sizes.
$ vmstat -w
$ vmstat --wide
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]
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
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.