Use the Vmstat command on Linux

Use the Vmstat command on Linux


Operating systems such as Linux, Unix-like and macOS use virtual memory to manage the amount of physical memory, the CPU, and the drive. Many times users want to determine the amount of usage of the above components but struggling to know how. Taimienphi.vn will show you a way to statistically monitor all these activities with vmstat command.


Use the vmstat command on Linux

Table of Contents:
1. Virtual memory
2. Use statistical period.
3. Use the count value.
4. Change of units.
5. Display active and inactive memory.
6. Display timer and memory statistics.
7. Display drive statistics.
8. Display summary record data.
9. Show partition statistics.

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.

vmstat

Use vmstat command on linux

After typing the above statement, the screen will appear a short table of values.
run the command

Using vmstat command on linux 2

The table includes columns of procs, memory, swap, io, system and CPU. The rightmost column contains data related to the CPU.

Using vmstat command on linux 3

The following is the comment of data items in columns.

Procs
– 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.

Memory
– 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.

Swap
– 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.

IO
– 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

System
– 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 ….

CPU
– 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:
vmstat 5

Using vmstat command on linux 4

Every 5 seconds vmstat will add another row of data to the table. To stop this, press Ctrl + C.

Using vmstat command on linux 5

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

Using vmstat command on linux 6

After four updates, vmstat will stop.

Using vmstat command on linux 7

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

Using vmstat command on linux 8

Using vmstat command on linux 9

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

Using vmstat command on linux 10

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.
vmstat -f

Using vmstat command on linux 11

The fork screen does not update.

Display Slabinfo

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

Using vmstat command on linux 12

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.

Using vmstat command on linux 13

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.
vmstat -s

Using vmstat command on linux 14

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.

Using vmstat command on linux 15

7. Display drive statistics

You can get a list of similar disk statistics by using the -d (disk) option.
vmstat -d | less

Using vmstat command on linux 16

For each disk, three columns are displayed, which are reads, writes and IO.

Using vmstat command on linux 17

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.

Using vmstat command on linux 18

The meaning of the columns:

Reads
– 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.

Writes
– 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.

IO
– 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”.
vmstat -D

Using vmstat command on linux 19

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.

Using vmstat command on linux 20

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

Using vmstat command on linux 21

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.

Using vmstat command on linux 22

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.

https://thuthuat.taimienphi.vn/su-dung-lenh-vmstat-tren-linux-55846n.aspx
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.

.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *