Use the vmstat command on Linux
1. Virtual memory
Computers are equipped with a finite amount of physical memory called random access memory (RAM). This memory is managed by the kernel and shared between the operating system and any running applications.
Linux and Unix-like operating systems like macOS can use hard drive space to help manage memory needs. A dedicated area for hard drive space is called “swap space,” which can be used as an extension of RAM. This is virtual memory.
Vmstat command => How to use vmstat command on Linux
The vmstat command gives data statistics of physical memory, CPU and hard disk.
If you type the vmstat command with no parameters, it will display a set of values. These values are the average for each statistic since the last boot of the computer.
After typing the above statement, the screen will appear a short table of values.
run the command
The table includes columns of procs, memory, swap, io, system and CPU. The rightmost column contains data related to the CPU.
The following is the comment of data items in columns.
– r: Number of processes that can be run. These may be processes that are already running, are running, or are waiting for the next CPU run. Using unused process is the process
– b: Number of uninterrupted sleep processes. In essence, these processes do not “sleep” but rather block a system call and are not interrupted until the current process is completed. Typically, this process takes place when the driver is waiting for some free resources. Any interruptions to that process are handled as the process continues to function as usual.
– swpd: the amount of virtual memory used or the amount of memory swapped.
– free: currently unused memory capacity.
– buff: the amount of memory used as buffer => cache.
– cache: the amount of memory used as cache => cache.
– si: The amount of virtual memory to be swapped in from swap space.
– so: The amount of virtual memory that is swapped out into swap space.
– Ball: Block received from a block device. The amount of data blocks used to swap virtual memory back to RAM.
– bo: Block sent to a block device. The amount of data blocks used for swap – virtual memory out of RAM and into swap space.
– the block that remains untranslated is the block
– in: Number of interruptions per second.
– cs: Number of context switches per second. Context Switch is when the kernel switches from processing system mode to processing user mode.
– Context Switch is a process ….
– us: Non-kernel runtime code.
– sy: Runtime code kernel.
– id: Free time.
– wa: Time to wait for input or output.
– st: Time from a virtual machine. This is the time a virtual machine has to wait for the hypervisor virtual machine monitoring system to finish serving other virtual machines before it can come back and work on this virtual machine => review this section
– hypervisor: virtual machine monitoring software
2. Use statistical period
You can ask vmstat to regularly update the statistics using the value delay. This value is provided in seconds. To update the statistics every 5 seconds, use the following command:
Every 5 seconds vmstat will add another row of data to the table. To stop this, press Ctrl + C.
3. Use the count value
Using too low delay value will cause congestion on your system. If you want to update faster, use the value count.
Value count Let vmstat indicate how many updates it needs to perform before exiting and return you to the command prompt. If you do not provide value count, vmstat will run until it is used by the key combination Ctrl + C.
To vmstat provide an update every 5 seconds, but in only four updates, use the following command:
vmstat 5 4
After four updates, vmstat will stop.
4. Change of units
To update the display of memory and swap statistics in kilobytes or megabytes, use the -S option (unit characters). This option must be followed by the letters k, K, m or M. Specifically they represent:
– k: 1000 bytes
– K: 1024 bytes
– m: 1000000 bytes
– M: 1048576 bytes
To update the memory statistics and swap every 10 seconds in megabytes, click the following command:
vmstat 10 -S M
5. Display active and inactive memory
If you use the -a (active) option, the buffer memory and cache columns are replaced with the “inact” and “active” columns.
To view these two columns instead of the buff and cache columns, include the -a option, like the following command:
vmstat 5 -a -S M
The “inact” and “active” columns are affected by the -S option (unit characters).
Count the number of Forks
The -f switch displays the number of forks that have occurred since the computer was started.
In other words, this command shows the number of tasks that have been launched (most of them have been closed) since the system was started. Each process launched from the command line will increase. When a new task or process arises this number continues to increase.
The fork screen does not update.
The kernel has a separate memory management system for the operating system and all applications.
The kernel allocates and frees up memory multiple times for the various types of data objects it has to handle. To make this as efficient as possible, it uses a system called slab. This is a form of cache recording.
To view statistics for slabs, use the -m (slabs) option. Now you need to use sudo and enter the password. Because the output can be quite long, switch it over less.
sudo vmstat -m | less
The output has five columns. That is:
– Cache: The name of the cache.
– num: Number of currently active objects in this cache.
– total: Total number of objects available in the cache.
– size: The size of each object in the cache.
– pages: The total number of memory pages with (at least) one object currently associated with the cache.
Press q to exit the mode less.
6. Display timer and memory statistics
To display a page of timer and memory statistics, use the option -S (stats). Note the word “S“lowercase.
Although most statistics are reported as the same information that creates default vmstat output, some of them are still divided in more detail.
For example, the default output combines both user nice and non-nice CPU time in the us column. The -s (stats) screen lists these statistics separately.
7. Display drive statistics
You can get a list of similar disk statistics by using the -d (disk) option.
vmstat -d | less
For each disk, three columns are displayed, which are reads, writes and IO.
IO is the rightmost column. Note that the sec column in IO is measured in seconds but statistics are based on the time in the reads and writes column is measured in milliseconds.
The meaning of the columns:
– total: Total number of drive reads.
– merged: The total number of reads grouped.
– sectors: The total number of sectors that have been read.
– ms: Total amount of time in milliseconds that has been used to read data from the drive.
– total: Total number of drive writes.
– merged: The total number of writes is grouped.
– sectors: The total number of sectors to be written.
– ms = Total amount of time in milliseconds that has been used to write data to the drive.
– cur: The number of current reads and writes.
– sec: Time in seconds for any read or write is in progress.
8. Display summary record data
To view a quick view of the summary burner data, use the -D (disk-sum) option. Note the capital “D”.
The number of drives can be abnormally high. The computer used in this article is running Ubuntu. With Ubuntu, every time you install an application from Snap, a file system pseudo-filesystem squashfs generated will attach to a / dev / loop device.
9. Show partition statistics
To view statistics related to a specific partition, use the -p (partition) option and provide the partition identifier as a command-line parameter.
Here we will look at the sda1 partition. The first digit indicates that this is the first partition on the sda device, which is the main hard drive for this computer.
vmstat -p sda1
The information returned shows the total number of read and write drives to and from that partition and the number of sectors included in the read and write drive actions.
With the above analysis, you already know how to use the Vmstat command on Linux, now let’s start to see the usage statistics for real memory, CPU and disk.
Above are some ways to use vmstat command on Linux. Hopefully Taimienphi.vn article has provided you with useful information. In addition, if you have any questions or need answers like the list Linux command Basic for new users …, readers can leave your comments in the comment section below the article.