游乐游手机版
首页/系统平台/文章详情

Unix vmstat命令详解与系统性能监控指南

时间:2026-06-20 10:26
在多CPU系统中,vmstat命令会汇总所有CPU的平均数据,并按进程进行统计。如果不加任何参数直接运行,其输出的每一行都代表自系统启动以来虚拟内存活动的累计情况。简而言之,vmstat是系统级性能监控的基础工具,适用于日常巡检与故障排查,是Linux运维人员必备的命令。 要了解vmstat的完整语

在多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中的rbcpu列的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资源。

内存方面,应重点关注交换区的siso,理想状态下它们应接近零。si表示每秒从磁盘换入内存的数据量,so表示每秒换出到磁盘的数据量。若siso长期不为0,则表明系统频繁使用swap,内存容量不足。

通常情况下,若内存占用率很高而CPU占用很低,需排查是否有大量应用占用内存却不释放。需特别说明:Linux的内存管理机制不会主动释放已使用的内存,因此看到内存使用率高属正常现象。判断内存是否紧张,不应仅看free列,而应关注swap的交换频率——只要siso基本为0,内存即处于健康状态。

来源:https://www.jb51.net/os/Unix/34606.html
上一篇FreeBSD网络重启命令详解与操作指南 下一篇UNIX文件系统深度分析与对比研究
本站内容用于信息整理与展示,如有侵权或内容问题请及时联系处理。

相关推荐

补充同频道和同主题内容,方便继续浏览更多相关内容。

同类最新

继续查看同栏目最近更新的文章。

更多
微软详解Win11时间点还原 默认每24小时创建恢复点
系统平台 · 2026-06-30

微软详解Win11时间点还原 默认每24小时创建恢复点

微软今日推送了最新的 6 月可选更新,并发布博客详细解读了 Win11 全新的“时间点还原”(Point-in-time restore)功能——这一功能本质上是对系统恢复体验的一次全面升级,旨在让用户更轻松地应对电脑故障。 微软表示,面向 Windows 11 客户端用户的“时间点还原”功能现已正

Win11 26H1六月可选更新KB5095091 优化放大镜改善装机体验
系统平台 · 2026-06-30

Win11 26H1六月可选更新KB5095091 优化放大镜改善装机体验

微软今天推送了Windows 11 26H1设备的6月可选更新KB5095091,安装完成后系统版本号会升级到Build 28000 2340。值得一提的是,这次更新并非面向所有设备,而是专门为搭载高通骁龙X2系列芯片的机型准备的——包括骁龙X2 Plus、X2 Elite和X2 Elite Ext

Win11六月可选更新KB5095093修复回收站弹窗异常
系统平台 · 2026-06-30

Win11六月可选更新KB5095093修复回收站弹窗异常

微软已悄然推送Windows 11六月可选更新,编号KB5095093。本次更新覆盖两个版本:24H2用户安装后版本号升级至Build 26100 8737,而25H2用户则更新至Build 26200 8737。 本次更新并非仅是小修小补,而是带来了多项实质性新功能。下面我们就来详细解析这些更新内

苹果macOS 27 Beta2封堵Siri AI跳过候补名单漏洞
系统平台 · 2026-06-30

苹果macOS 27 Beta2封堵Siri AI跳过候补名单漏洞

科技媒体 Cult of Mac 昨日(6月23日)发布博文指出,苹果在 macOS 27 Beta 2 更新中悄然封堵了一个此前可用的后门——用户曾能通过一条终端命令绕过候补名单,直接启用新版 Siri AI,如今这一方法已失效。 简要回顾一下:在 macOS 27 Beta 1 阶段,只需在 M

微软加速Win11 25H2推送 覆盖所有符合条件家用PC
系统平台 · 2026-06-30

微软加速Win11 25H2推送 覆盖所有符合条件家用PC

近日(6月23日),科技媒体 Windows Latest 发布了一则值得关注的动态:微软已进一步扩大 Windows 11 25H2 的推送范围,所有满足硬件要求、且不受 IT 部门管理的家庭版和专业版设备,现在均可顺利接收本次更新。 此次升级有一个显著特点——采用“启用包”(eKB)方式进行推送