Linux vmstat Command
What is the vmstat command
vmstat (Virtual Memory Statistics) is a powerful performance monitoring tool on Linux systems that can report key performance indicators in real time, such as system processes, memory, swap partitions, I/O blocks, and CPU activity.
Core Features
- Memory Monitoring: Displays the usage of physical memory and swap space
- CPU Usage: Shows the proportion of user processes, system processes, and idle time
- Process Status: Reports the number of running, waiting, and blocked processes
- I/O Statistics: Provides detailed information on block device read/write operations
Basic Syntax and Common Parameters
Basic Syntax Format
vmstat [选项] [时间间隔] [次数]
Common Parameter Description
| Parameter | Description |
|---|---|
-a |
Display active and inactive memory |
-d |
Display disk statistics |
-p |
Display statistics for a specified disk partition |
-s |
Display a summary of memory-related statistics |
-m |
Display slab information |
-t |
Add a timestamp to the output |
-S |
Specify the display unit (k/K/m/M, representing 1000/1024/1000000/1048576 respectively) |
Output Field Details
ExecutevmstatAfter the command, the output is usually divided into the following parts:
1. Process Information
procs r: 运行队列中的进程数量 b: 等待 I/O 的进程数量
2. Memory Information
memory swpd: 使用的虚拟内存大小(KB) free: 空闲的物理内存大小(KB) buff: 用作缓冲区的内存大小(KB) cache: 用作缓存的内存大小(KB)
3. Swap Partition Information
swap si: 每秒从磁盘读入交换区的内存大小(KB/s) so: 每秒写入磁盘的交换区内存大小(KB/s)
4. I/O Information
io bi: 每秒从块设备接收的块数(blocks/s) bo: 每秒发送到块设备的块数(blocks/s)
5. System Information
system in: 每秒中断数(包括时钟中断) cs: 每秒上下文切换次数
6. CPU Usage
cpu us: 用户进程占用 CPU 时间百分比 sy: 系统进程占用 CPU 时间百分比 id: 空闲 CPU 时间百分比 wa: 等待 I/O 的 CPU 时间百分比 st: 被虚拟机偷取的时间百分比
Practical Examples Explained
Example 1: Basic Monitoring (refresh every 2 seconds)
Example
Output example:
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 123456 78900 456789 0 0 5 12 345 6789 10 5 85 0 0
Example 2: Display Disk Statistics
Example
The output will show read/write statistics for each disk, including the number of read/write operations and sectors.
Example 3: Monitoring with Timestamps (every 5 seconds, 3 times total)
Example
The output adds a timestamp before each line, making it easier to record and analyze.
Performance Issue Diagnosis Guide
1. CPU Bottleneck Determination
- High us value: User processes consume a large amount of CPU; the application may need to be optimized.
- High sy value: The system kernel consumes a large amount of CPU; there may be too many system calls.
- High wa value: Long I/O wait time; possibly a disk bottleneck.
2. Memory Problem Diagnosis
- si/so continuously non-zero: Indicates the system is using the swap partition; physical memory may be insufficient.
- free value continuously decreasing: Possible memory leak.
3. Process Blocking Issues
- Large value in column b: Indicates multiple processes are waiting for I/O operations.
Best Practice Suggestions
- Monitoring interval selection: For production environments, a 5-10 second interval is recommended to avoid affecting system performance.
- Long-term monitoring: Combine with
nohupredirect the output to a filenohup vmstat 10 360 > vmstat.log &
- Comprehensive analysis: Combine with
top、iostatand other tools for use together - Benchmark testing: Record baseline data when the system is normal for comparison when abnormalities occur.
FAQ
Q1: What is the difference between the vmstat and top commands?
topProvides detailed information at the process levelvmstatProvides an overview of overall system resource usage
Q2: What does it mean when si/so values are very high?
It indicates the system is frequently using the swap partition, possibly due to insufficient physical memory. It is recommended to add more memory or optimize the application's memory usage.
Q3: How to monitor a specific disk?
Use the-pparameter to specify the disk partition:
Example
By mastering the vmstat command, you will be able to quickly diagnose performance bottlenecks in Linux systems and provide data support for system optimization. It is recommended to combine it with other monitoring tools in actual work to obtain a more comprehensive view of system performance.
Other Extensions
Linux Command Reference