This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Introduction of the command

Vmstat is short for Virtual Meomory Statistics. It monitors the Virtual memory, process, and CPU activities of an OPERATING system (OS). He is to the overall situation of the system statistics, the deficiency of a process can not be in-depth analysis.

The statistics provided by the vmstat command are mainly from the /proc/meminfo, /proc/stat, and /proc/*/stat files maintained by the system kernel.

The command format

vmstat [-a] [-n] [-S unit] [delay [ count]] vmstat [-s] [-n] [-S unit] vmstat [-m] [-n] [delay [ count]] vmstat [-d] [-n] [delay [count]] vmstat [-p disk partition] [-n] [delay [count]] vmstat [-f] vmstat [-v] Commercial reproduced please contact the author for authorization, non-commercial please keep the original link: https://www.yiibai.com/linux/vmstat.htmlCopy the code

The command parameter

parameter explain
-a Displays statistics for active/inactive memory.
-d
-f
-m Display cache allocation information of the system.
-n Display field header information only once, not periodically.
-p partition Displays detailed statistics for the specified device partition.
-s Displays various event counters and memory statistics in a tabular manner. Note that the display cannot be repeated consecutively with this option.
-S unit In the specified byte unitK (1000).,K (1024).,M. (10002).orM. (10242).Display statistics.
-t Add time information to the displayed statistics.
-V Display the version information of the command and exit.
-w Increase the width of the display field so that larger memory data can be displayed.

VM mode output field description

Procs (Process status information)

field explain
r The number of processes in the runqueue waiting to run.
b The number of processes that are in an uninterruptible sleep while waiting for I/O resource or page scheduling.

Memory (Memory usage)

field explain
swpd The amount of virtual memory in use.
free The amount of free memory.
buff The amount of memory used as buffer.
cache The amount of memory used as cache.
inact Amount of inactive memory (-aOption).
active Amount of active memory (-aOption).

Swap memory disk Swap

field explain
si The amount of memory swapped from disk to memory per second.
so The amount of memory swapped to disk per second.

IO

field explain
bi The number of data blocks read from a block device per second.
bo The number of data blocks written to a block device per second.

System

field explain
in Number of interrupts per second (including clock interrupts).
cs Number of context switches per second.

CPU (percentage of total CPU time allocated)

field explain
us User mode elapsed time (user time) as a percentage of total CPU time.
sy Kernel mode elapsed time (system time) as a percentage of total CPU time.
id The percentage of the total CPU time occupied by the system idle time.
wa Percentage of the total CPU time spent waiting for I/O.
st Percentage of total CPU time that was stolen from the virtual machine.

Disk mode Output field description

Reads

field explain
total Total number of successful disk reads.
merged The number of logical read operations to be combined into one read operation.
sectors Number of sectors successfully read.
ms Time spent reading disks (milliseconds).

Writes

field explain
total Total number of successful disk writes.
merged The number of logical writes that are combined into a single write operation.
sectors Number of sectors successfully written to the disk.
ms Disk write time (milliseconds).

IO

field explain
cur Number of I/ OS currently being executed.
s Time spent reading and writing disks (seconds).

Disk partition mode Output field description

field explain
reads Total number of disk partition reads.
read sectors Read the total number of sectors of disk partitions.
writes Total number of times that disk partitions are written.
requested writes Total number of disk partition write requests.

Output fields in SLAB mode

field explain
cache Name of the cache.
num The number of currently active objects.
total The sum of available objects.
size Size of each object.
pages The number of pages with at least one live object.
totpages The total number of pages allocated.
pslab Number of pages per slab structure.

Physical memory and virtual memory

This section comes from the vmstat command

We know that reading and writing data directly from physical memory is much faster than reading and writing data from hard disk. Therefore, we want all data to be read and written in memory, and memory is limited. This introduces the concept of physical memory and virtual memory. Physical memory is the amount of memory provided by the system hardware, which is the real memory. Relative to physical memory, there is a concept of virtual memory under Linux. Virtual memory is a strategy proposed to meet the shortage of physical memory. The disk Space used for virtual memory is called Swap Space. As an extension of the physical memory, Linux can be in physical memory, virtual memory use the swap partition, more detailed said, is the kernel will temporarily unused memory block write information exchange of space, so that physical memory is released, the memory can be used for other purposes, when the content of the need to use the original, This information is read back into physical memory from the swap space. The memory management of Linux adopts the paging access mechanism. In order to make full use of the physical memory, the kernel will automatically swap the infrequently used data blocks in the physical memory to the virtual memory at the appropriate time, and reserve the frequently used information to the physical memory.

To understand the Linux memory mechanism, you need to know the following aspects:

  • First, Linux will swap pages from time to time to keep as much free physical memory as possible, even if there is no need for memory, Linux will swap pages of memory that are not used for a while. This avoids the time required to wait for an exchange.
  • Not all pages are swapped to virtual memory when not in use. The Linux kernel swaps only infrequently used page files to virtual memory according to the “most recently used” algorithm. Sometimes we see this phenomenon: There’s a lot more Linux physical memory, but swap space is also used a lot. In fact, this is not surprising, for example, a process takes up a lot of memory, it requires a lot of memory resources, at this time there will be some not commonly used page file be traded to the virtual memory, but then the end of this process takes up a lot of memory resources and release a lot of memory, just be swapped out the page file does not automatically switch into physical memory, Unless this is necessary, the system will have a lot of free physical memory and swap space will be used at the same time, and this phenomenon will occur. Don’t worry about that, just know what it is.
  • Finally, swap space page in use will first be traded to physical memory, if there is not enough physical memory to accommodate these pages, they will be swapped out immediately, so, the virtual memory may not be enough space to store these exchange page, eventually lead to Linux problems such as abnormal dead, service, Linux can recover itself over a period of time, but the restored system is essentially unusable. Therefore, it is very important to plan and design Linux memory usage properly.

Principle of Virtual Memory

Every process running on the system needs to use memory, but not every process needs to use the memory space allocated by the system all the time. When the memory required to run the system exceeds the actual physical memory, the kernel frees some or all of the unused physical memory occupied by some processes, stores this data on disk until the next time the process is called, and makes the freed memory available to other processes that need it.

In Linux memory management, the above memory scheduling is done mainly through “Paging” and “exchanging Considerations”. Paging algorithm is to move the recently infrequently used pages in memory to disk, leaving active pages in memory for the process to use. Swap is a technique that swaps entire processes, not just parts of pages, onto disk. Paging (Page) The process of writing to disk is called page-out, and the process of paging (Page) from disk back to memory is called page-in. A Page Fault occurs when the kernel needs a Page that is not in physical memory (because it has been page-out).

When the kernel detects that runnable memory is running low, it frees some of the physical memory through page-out. Page-out doesn’t happen very often, but if page-out happens frequently and repeatedly, until the kernel manages paging for longer than the program can run, system performance deteriorates dramatically. The system is already running very slowly or in a suspended state, also known as thrashing.

Reference documentation

  • The vmstat command
  • “Linux Common Commands concise Manual” — Xing Guoqing edited