Linux Vmstat command

王林
王林forward
2024-03-20 13:49:111247browse

Linux Vmstat命令

In this guide, we will demonstrate various ways to use the “vmstat” command in Linux.

prerequisites:

To perform the steps demonstrated in this guide, you will need the following components:

  • Configure the correct Linux system. For testing purposes, consider using a Linux VM.
  • Access to non-root users with sudo privileges
  • Basic understanding of command line interface

Virtual memory in Linux

RAM, or physical memory, is a limited resource allocated by the operating system to running programs. All programs, including the operating system itself, need to occupy this space.

When memory requirements exceed available memory, the system may crash or a program may request more memory space. In most cases, this situation is undesirable. Hence the concept of virtual memory, which allows the system to store part of the data on the hard disk so that it can be swapped to RAM when needed. In this way, the system can manage memory resources more efficiently, ensuring that programs run normally without crashing due to insufficient memory. The use of virtual memory makes the system face memory requirements

Virtual memory is a technology in computer systems that uses space on a hard drive or solid-state drive to expand the capacity of memory to act as additional memory when needed. Virtual memory is actually a way of using part of your hard drive space as temporary storage so that it can be replenished when the processor needs more memory. In UNIX and Linux operating systems, this technology is called swap space and is used to manage the allocation and exchange of memory resources. Although virtual memory is not real physical memory, it can improve the performance and stability of the system and ensure that the system can handle larger workloads.

The Linux kernel moves blocks of memory to swap space and retrieves them back to RAM when necessary.

Virtual memory is usually slower than physical memory and depends on the performance of the storage device. But under certain hardware settings (such as using NVMe SSD), the performance of virtual memory may be comparable to RAM.

vmstat command

The "vmstat" command is a utility tool for monitoring virtual memory related information. It is available on all Linux systems as part of the "sysstat" package.

The command structure of "vmstat" is as follows:

$vmstat

Basic usage

If run without any parameters, "vmstat" will print system information since the last startup:

$vmstat

The output is divided into six parts:

  • procs: Statistics of currently running processes
    • r: Number of active processes
    • b: Number of sleep processes
  • memory: memory usage statistics
    • Swpd: Total virtual memory (swap space)
    • Free: available swap space
    • buff: Amount of swap space used as temporary buffer memory
    • Cache: Total cache memory
  • swap: Statistics about swap space
    • si: exchange rate
    • So: swap-out ratio
  • io: I/O statistics
    • bi: number of blocks received from block device
    • bo: Number of blocks sent to the block device
  • System: Schedule Statistics
    • in: System interrupt count
    • cs: context switch rate
  • CPU: Various CPU statistics
    • United States: CPU time spent on non-kernel processes
    • sy: CPU time spent on kernel processes
    • id: CPU spends time idle
    • wa: The CPU spends time waiting for the I/O operation to complete
    • st: CPU time used by the virtual machine

Change display unit

By default, "vmstat" reports memory values ​​in bytes. To change units, use the "-S" flag:

$vmstat-S

Here, "vmstat" prints the value in MB.

There are several memory units available:

  • M: 1048576 bytes (2^20 bytes)
  • M: 1000000 bytes (1000 kilobytes)
  • K: 1024 bytes (1 megabyte)
  • k: 1000 bytes (1 byte)

Continuous statistics updates

By default, "vmstat" prints a report once. However, we can instruct "vmstat" to provide continuous reports at specified intervals (in seconds).

The command structure is as follows:

$vmstat

For example, to obtain updated statistics every 2 seconds, the command is as follows:

$vmstat 2

Output will not stop unless manually terminated using "Ctrl C".

Alternatively, we can specify "vmstat" to provide statistics for a specific number of times:

$vmstat

For example, to obtain statistics updated every 2 seconds, the command is as follows:

$vmstat 2 5

Active and inactive memory

Active memory refers to the memory space currently used by the process. On the other hand, inactive memory refers to the memory space allocated to processes that are no longer running.

Using "vmstat" we can check the amount of active and inactive memory being used:

$vmstat—a

Here, the "buff" and "cache" columns are replaced by the "inact" and "active" columns respectively.

Memory and Scheduling

To get a more detailed report on memory and scheduling, use the following command:

$vmstat—s

here:

  • Section 1: This section introduces basic system information, such as total physical memory, active/inactive memory, free/buffer/cache memory, etc.
  • Section 2: Various CPU Statistics
    • Non-good CPU beats: The number of times a high-priority process uses the CPU.
    • NICE CPU TICK: The number of times low-priority processes use the CPU.
    • System CPU count: The number of times the kernel process uses the CPU.
    • Idle CPU ticks: The number of times the CPU is idle.
    • IO—wait CPU ticks: The number of times the CPU waits for I/O management.
    • IRQ: The number of times the CPU receives interrupt requests.
    • softirq: The number of times the CPU receives software interrupt requests.
    • Stolen CPU Time: The number of times the VM stole CPU time.
  • Section 3: Memory paging status
  • Section 4: Event Counter

Post-boot fork

Forks refer to processes spawned from existing processes. To get statistics on fork count, run the following command:

$vmstat-f

Disk and Partition Statistics

The "vmstat" command can also provide information about disk activity. To get a quick summary of disk activity, run the following command:

$vmstat—D

To get a more detailed report of disk activity (including read/write statistics), use the following command instead:

$vmstat—d

here:

  • reading
    • total: total disk reads
    • Mergeed: Total number of group reads
    • sectors: Total number of sectors read
    • ms: Total time to read data from disk (milliseconds)
  • wrote
    • total: disk write count
    • Mergeed: Total number of group writes
    • sectors: Total number of sectors written
    • ms: Total time to write to disk (milliseconds)
  • io
    • cur: Current total disk read/write count
    • Seconds: The amount of time (seconds) spent on the ongoing read/write operation

The "vmstat" command can also generate reports for specific disk partitions. To obtain a partition report, use the following command structure:

$vmstat-p

Plate statistics

Board allocation is an efficient object memory allocation mechanism. Slab allocation provides reduced memory fragmentation (caused by memory allocation and deallocation) compared to previous mechanisms.

To check the slab statistics of the system, use the following "vmstat" command:

$sudo vmstat—m

Please note that it requires root access to view statistics.

here:

  • Cache: Name of cached data
  • Num: The number of active objects in the Num cache
  • Total: Total number of objects in a specific cache
  • Size: The size of the cache object
  • Pages: Memory page count containing cached objects

in conclusion

In this guide, we show various ways of using the "vmstat" command. In addition to virtual memory, "vmstat" can also report disk statistics, forks, shards, etc.

Interested in learning about other system monitoring tools? Learn more about HTOP, KILL, PS and more.

Happy computing!

The above is the detailed content of Linux Vmstat command. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete