在多CPU系统中,vmstat命令会汇总所有CPU的平均数据,并按进程进行统计。如果不加任何参数直接运行,其输出的每一行都代表自系统启动以来虚拟内存活动的累计情况。简而言之,vmstat是系统级性能监控的基础工具,适用于日常巡检与故障排查,是Linux运维人员必备的命令。
要了解vmstat的完整语法和选项,最直接的方式是通过man命令。例如,你可以将帮助文档导出到文件中仔细研读:
# man vmstat|col -b > /u01/tianlesoftware.txt
当然,直接在终端执行man vmstat也能查看完整手册,下面整理了关键内容,供快速参考。
vmstat命令用法详解
VMSTAT(8) Linux Administrator's Manual VMSTAT(8)
NAME
vmstat - Report virtual memory statistics
SYNOPSIS
vmstat [-a] [-n] [delay [ count]]
vmstat [-f] [-s] [-m]
vmstat [-S unit]
vmstat [-d]
vmstat [-p disk partition]
vmstat [-V]
DESCRIPTION
vmstat reports information about processes, memory, paging, block IO,
traps, and cpu activity.
The first report produced gives a verages since the last reboot. Addi-
tional reports give information on a sampling period of length delay.
The process and memory reports are instantaneous in either case.
Options
-a Displays active/inactive memory (kernel 2.5.41 or better).
-f Displays the number of forks since boot. This includes fork,
vfork, and clone system calls, and is equivalent to the total
number of tasks created. Each process is represented by one or
more tasks, depending on thread usage. This display does not repeat.
-m Displays slabinfo.
-n Causes the header to be displayed only once, rather than periodically.
-s Displays a table of various event counters and memory statistics.
This display does not repeat.
delay The delay between updates in seconds. If no delay is specified,
only one report is printed with the a verage values since boot.
count The number of updates. If no count is specified and delay is defined,
count defaults to infinity.
-d Reports disk statistics (kernel 2.5.70 or above required).
-p Followed by a partition name for detailed statistics (kernel 2.5.70 or above required).
-S Followed by k, K, m, or M to set output unit (1000, 1024, 1000000, or 1048576 bytes).
-V Displays version information.
FIELD DESCRIPTION FOR VM MODE
Procs
r: The number of processes waiting for run time.
b: The number of processes in uninterruptible sleep.
Memory
swpd: the amount of virtual memory used.
free: the amount of idle memory.
buff: the amount of memory used as buffers.
cache: the amount of memory used as cache.
inact: the amount of inactive memory. (-a option)
active: the amount of active memory. (-a option)
Swap
si: Amount of memory swapped in from disk (/s).
so: Amount of memory swapped to disk (/s).
IO
bi: Blocks received from a block device (blocks/s).
bo: Blocks sent to a block device (blocks/s).
System
in: The number of interrupts per second, including the clock.
cs: The number of context switches per second.
CPU
These are percentages of total CPU time.
us: Time spent running non-kernel code. (user time, including nice time)
sy: Time spent running kernel code. (system time)
id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
wa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero.
FIELD DESCRIPTION FOR DISK MODE
Reads
total: Total reads completed successfully
merged: grouped reads (resulting in one I/O)
sectors: Sectors read successfully
ms: milliseconds spent reading
Writes
total: Total writes completed successfully
merged: grouped writes (resulting in one I/O)
sectors: Sectors written successfully
ms: milliseconds spent writing
IO
cur: I/O in progress
s: seconds spent for I/O
FIELD DESCRIPTION FOR DISK PARTITION MODE
reads: Total number of reads issued to this partition
read sectors: Total read sectors for partition
writes: Total number of writes issued to this partition
requested writes: Total number of write requests made for partition
FIELD DESCRIPTION FOR SLAB MODE
cache: Cache name
num: Number of currently active objects
total: Total number of a vailable objects
size: Size of each object
pages: Number of pages with at least one active object
totpages: Total number of allocated pages
pslab: Number of pages per slab
NOTES
vmstat does not require special permissions.
These reports are intended to help identify system bottlenecks.
Linux vmstat does not count itself as a running process.
All linux blocks are currently 1024 bytes. Old kernels may report
blocks as 512 bytes, 2048 bytes, or 4096 bytes.
Since procps 3.1.9, vmstat lets you choose units (k, K, m, M) default
is K (1024 bytes) in the default mode.
vmstat uses slabinfo 1.1
FILES
/proc/meminfo
/proc/stat
/proc/*/stat
SEE ALSO
iostat(1), sar(1), mpstat(1), ps(1), top(1), free(1)
BUGS
Does not tabulate the block io per device or count the number of system calls.
AUTHORS
Written by Henry Ware .
Fabian Frédérick (diskstat, slab, partitions...)
Throatwobbler Ginkgo Labs 27 July 1994 VMSTAT(8)
vmstat使用示例
最常见的用法是每隔2秒采样一次,共显示10次:
# vmstat 2 10
procs -----------memory---------------------- -----swap--- -----io---- ---system--- ----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa
1 1 208 28960 38128 268204 0 0 278 40 1050 370 5 46 44 5
1 0 208 28896 38128 268204 0 0 128 0 1054 310 4 38 58 1
2 0 208 28896 38136 268196 0 0 257 55 1056 301 4 39 52 5
2 0 208 24896 38136 268196 0 0 257 1 1042 593 6 87 7 0
0 0 208 28672 38136 268196 0 0 257 25 1052 372 4 46 45 5
0 0 208 28824 38140 268192 0 0 129 111 1052 316 3 32 64 1
1 0 208 28768 38148 268184 0 0 257 109 1050 286 9 70 21 1
1 0 208 28512 38148 268184 0 0 257 1 1046 327 6 48 43 2
0 0 208 28832 38148 268184 0 0 257 1 1047 313 5 42 52 1
0 0 208 28832 38152 268180 0 0 257 109 1050 308 4 33 59 5
每2秒采样一次,共显示10次。输出中各列均有明确含义,后续将详细解读vmstat输出字段。
Solaris (Unix) 平台下的vmstat示例
-bash-3.00$ vmstat 2 10
kthr memory page disk faults cpu
r b w swap free re mf pi po fr de sr s0 s1 -- -- in sy cs us sy id
0 0 0 5016344 4200040 126 512 35 0 0 0 0 0 1 0 0 731 1416 723 3 0 97
0 0 0 2065576 2375112 14 54 0 0 0 0 0 0 0 0 0 870 625 950 3 0 96
0 0 0 2065512 2375048 11 50 0 0 0 0 0 0 0 0 0 951 529 1066 3 0 97
0 0 0 2065512 2375048 11 53 0 0 0 0 0 0 0 0 0 852 548 934 3 0 97
0 0 0 2065512 2375048 4 40 0 0 0 0 0 0 0 0 0 836 483 907 3 0 97
0 0 0 2065512 2375048 14 46 0 0 0 0 0 0 0 0 0 873 632 958 3 0 97
0 0 0 2065512 2375048 0 0 0 0 0 0 0 0 0 0 0 812 417 863 3 0 97
0 0 0 2065512 2375048 4 40 0 0 0 0 0 0 0 0 0 810 481 874 3 0 97
0 0 0 2065384 2374920 0 0 0 0 0 0 0 0 0 1 0 0 801 406 862 3 0 96
0 0 0 2065384 2374920 14 46 0 0 0 0 0 0 0 0 0 817 545 898 3 0 97
Solaris下的输出字段略有不同,但核心思路一致——kthr中的r和b、cpu列的us/sy/id均为关键性能指标。
vmstat关键指标解读与经验法则
关于CPU性能,以下经验法则值得参考:
1) 若进程等待队列(r)持续超过CPU核心数,则表明大量进程排队等待CPU,系统响应变慢。
2) 当r的值超过可用CPU数量的4倍时,基本可判断CPU资源严重匮乏或主频过低,导致多数进程处于等待状态。
3) 若空闲时间(id)长期为0,且系统时间(sy)达到用户时间(us)的两倍,同样表明CPU资源紧张。
简单总结
运行队列(r)和阻塞队列(b)通常不应超过5。若r经常大于4且id经常低于40%,则CPU负载已相当沉重。
用户态与系统态之和(us+sy)不宜超过80%。超过此阈值时,进程将在运行队列中长时间等待,导致响应时间和吞吐量显著下降。
等待I/O(wa)应控制在25以下。若wa超过40,通常表明磁盘I/O存在瓶颈或磁盘操作过于频繁。
实际上,CPU资源不足的根源往往在于应用程序,例如低效的SQL查询或循环中频繁的系统调用,都会耗尽CPU资源。
内存方面,应重点关注交换区的si和so,理想状态下它们应接近零。si表示每秒从磁盘换入内存的数据量,so表示每秒换出到磁盘的数据量。若si和so长期不为0,则表明系统频繁使用swap,内存容量不足。
通常情况下,若内存占用率很高而CPU占用很低,需排查是否有大量应用占用内存却不释放。需特别说明:Linux的内存管理机制不会主动释放已使用的内存,因此看到内存使用率高属正常现象。判断内存是否紧张,不应仅看free列,而应关注swap的交换频率——只要si和so基本为0,内存即处于健康状态。
