vmstat 是一个标准的工具,它会报告 Linux 系统的虚拟内存统计。vmstat 会报告有关进程、内存、分页、块 IO、陷阱(中断)和 cpu 活动的信息。它可以帮助 Linux 管理员在解决问题时识别系统瓶颈。
-- Magesh Maruthamuthu

编译自: http://www.2daygeek.com/linux-vmstat-command-examples-tool-report-virtual-memory-statistics/
作者: Magesh Maruthamuthu
译者: geekpi
什么是 RAM?
在智能手机世界,我们每一个人都知道 RAM。因此,我不想深入介绍,这样我就简要概括下。RAM 代表“随机访问内存Random Access Memory”,是一种计算机数据存储,它会存储经常使用的程序来提升系统性能。
什么是虚拟内存?
虚拟内存是一种内存管理方式,计算机通过临时将最近未使用的程序数据从 RAM 转移到硬盘,以平衡或管理内存的短缺。
什么是 vmstat?
vmstat 是一个标准的工具,它会报告 Linux 系统的虚拟内存统计。vmstat 会报告有关进程、内存、分页、块 IO、陷阱(中断)和 cpu 活动的信息。它可以帮助 Linux 管理员在解决问题时识别系统瓶颈。
在 Linux 中安装 Sysstat
Linux 中没有独立的 vmstat 包。它与 sysstat 绑定在一起,并在大多数发行版的默认仓库上都有。如果还没有安装,只要基于你的发行版输入下面的命令。
  1. [在CentOS/RHEL 中安装vmstat]
  2. $ sudoyum install sysstat
  3. [在Fedora中安装vmstat]
  4. $ sudo dnf install sysstat
  5. [在Debian/Ubuntu中安装vmstat]
  6. $ sudoapt-get install sysstat
  7. [在ArchLinux中安装vmstat]
  8. $ sudo pacman -S sysstat
  9. [在Mageia中安装vmstat]
  10. $ sudo urpmi sysstat
  11. [在 openSUSE 中安装vmstat]
  12. $ sudo zypper install sysstat
不带参数运行 vmstat
假设你已经成功安装 vmstat,在终端中不带参数运行 vmstat,它会向你展示 vmstat 的默认结果。
  1. #vmstat
  2. procs -----------memory-------------swap-------io-----system------cpu----
  3. r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
  4. 2079496161412013924078792800231000111880
当你看到上面的输出,你可能已经大致了解了它是什么以及它的目的。不要担心,我们将深入解释每个参数,以便你可以了解 vmstat 的用途和目的。
procs:procs 中有 r 和 b 列,它报告进程统计信息。在上面的输出中,在运行队列(r)中有两个进程在等待 CPU 并有零个休眠进程(b)。通常,它不应该超过处理器(或核心)的数量,如果你发现异常,最好使用 top 命令[1]进一步地排除故障。
  • r:等待运行的进程数。
  • b:休眠状态下的进程数。
memory: memory 下有报告内存统计的 swpdfreebuff 和 cache 列。你可以用 free -m 命令看到同样的信息。在上面的内存统计中,统计数据以千字节表示,这有点难以理解,最好添加 M参数来看到以兆字节为单位的统计数据。
  • swpd:使用的虚拟内存量。
  • free:空闲内存量。
  • buff:用作缓冲区的内存量。
  • cache:用作高速缓存的内存量。
  • inact:非活动内存的数量。
  • active:活动内存量。
swap:swap 有 si 和 so 列,用于报告交换内存统计信息。你可以用 free -m 命令看到相同的信息。
  • si:从磁盘交换的内存量(换入,从 swap 移到实际内存的内存)。
  • so:交换到磁盘的内存量(换出,从实际内存移动到 swap 的内存)。
I/O:I/O 有 bi 和 bo 列,它以“块读取”和“块写入”的单位来报告每秒磁盘读取和写入的块的统计信息。如果你发现有巨大的 I/O 读写,最好使用 iotop[2] 和 iostat[3] 命令来查看。
  • bi:从块设备接收的块数。
  • bo:发送到块设备的块数。
system:system 有 in 和 cs 列,它报告每秒的系统操作。
  • in:每秒的系统中断数,包括时钟中断。
  • cs:发送到块设备的块数。
CPU:CPU 有 csussyid 和 wa 列,报告(所用的) CPU 资源占总 CPU 时间的百分比。如果你发现异常,最好使用 top 和 free 命令。
  • cs:每秒的系统中断数,包括时钟。
  • us:发送到块设备的块数。
  • sy:用作高速缓存的内存量。
  • id:非活动内存量。
  • wa:活动内存量。
以 MB 方式输出
默认情况下,vmstat 以千字节为单位显示内存统计,这是非常难以理解的,最好添加 -S m 参数以获取以兆字节为单位的统计。
  1. #vmstat-S m
  2. procs -----------memory-------------swap-------io-----system------cpu----
  3. r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
  4. 10103371406211600401500111870
以延迟方式运行 vmstat 获取更好的统计信息
默认情况下,vmstat 的单次统计信息不足以进一步进行故障排除,因此,添加更新延迟(延迟是更新之间的延迟,以秒为单位)以定期捕获活动。如果你想以 2 秒延迟运行 vmstat ,只需使用下面的命令(如果你想要更长的延迟,你可以根据你的愿望改变)。
以下命令将每 2 秒运行一次,直到退出。
  1. #vmstat2
  2. procs -----------memory-------------swap-------io-----system------cpu----
  3. r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
  4. 10105500325776416016216691200401500111870
  5. 0010550032564441601621669200001310831174111870
  6. 0010550030864841602421669280011615591453162820
  7. 00105500285948416032216693200012934100391900
  8. 00105500326620416040216694000127922106891900
  9. 0010550036670441604821669440001783595591900
  10. 0010550036645641605621669480012285991891900
  11. 0010550036645641605621669480001515391504172810
  12. 001055003652244160602166996001199841097111880
带延迟和计数运行 vmstat
或者,你可以带延迟和特定计数运行 vmstat,一旦达到给定的计数,然后自动退出。
以下命令将每 2 秒运行一次,10 次后自动退出。
  1. #vmstat210
  2. procs -----------memory-------------swap-------io-----system------cpu----
  3. r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
  4. 1079496158191615738081041200231001111880
  5. 20794961559464157380810416001118211749212770
  6. 007949615837681573848104160014668179991900
  7. 20794961556364157384810428001113921545152830
  8. 00794961583272157384810428001013071448142840
  9. 207949615820321573848104280014142460541960
  10. 10794961575848157384810428001019122407262710
  11. 007949615828841573848104360016967882591900
  12. 2079496156936815739281043200112692096991900
  13. 107949615836121574008104440073920012530202770
显示活动和非活动内存
默认情况下,vmstat 会显示除活动和非活动内存之外的内存统计信息。如果要查看活动和非活动内存统计信息,请在 vmstat 后添加 -a 参数。
  1. #vmstat-a
  2. procs -----------memory-------------swap-------io-----system------cpu----
  3. r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa
  4. 10105500238759241514858411200401501111870
打印磁盘统计
在 vmstat 后面添加 -d 参数会以每个磁盘一行的方式显示统计(包含读、写和 IO)。
  1. #vmstat-d
  2. disk-------------reads------------------------writes----------------IO------
  3.       total merged sectors      ms  total merged sectors      ms    cur    sec
  4. ram0       0000000000
  5. ram1       0000000000
  6. ram2       0000000000
  7. ram3       0000000000
  8. ram4       0000000000
  9. ram5       0000000000
  10. ram6       0000000000
  11. ram7       0000000000
  12. ram8       0000000000
  13. ram9       0000000000
  14. ram10      0000000000
  15. ram11      0000000000
  16. ram12      0000000000
  17. ram13      0000000000
  18. ram14      0000000000
  19. ram15      0000000000
  20. loop0      0000000000
  21. loop1      0000000000
  22. loop2      0000000000
  23. loop3      0000000000
  24. loop4      0000000000
  25. loop5      0000000000
  26. loop6      0000000000
  27. loop7      0000000000
  28. fd0        0000000000
  29. sda   1660405090449725948821905745573230037054280937702160032056118189160040915
  30. sdb   25735757747998531247122045772353208502519128323736645890112509480182336
总结磁盘统计
在 vmstat 后面添加 -D 会显示全局统计(包括全部的磁盘、分区、全部读、合并的读、读取的扇区、写、合并的写、写入的扇区和 IO)。
  1. #vmstat-D
  2. 27 disks
  3. 3 partitions
  4. 275754028 total reads
  5. 1388030 merged reads
  6. 5751195976 read sectors
  7. 638710116 milli reading
  8. 38795040 writes
  9. 29520659 merged writes
  10. 2209820333 written sectors
  11. 130210652 milli writing
  12. 0 inprogress IO
  13. 224704 milli spent IO
打印指定分区统计
vmstat 添加 -p 参数后面跟上设备名会显示指定分区统计(包括读、读取的扇区、写以及请求的写)。
  1. #vmstat-p /dev/sdb1
  2. sdb1          reads   read sectors  writes    requested writes
  3. 311527890839453206728016
vmstat 统计信息带上时间戳
当你想在特定时间区间内找到内存尖峰时,用 vmstat 命令添加 -t 参数,后跟延迟和计数。
注意:此组合不适用于基于 Debian 的系统。
  1. #vmstat-t 15
  2. procs -----------memory-------------swap-------io------system-------cpu---------timestamp---
  3. r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
  4. 00069814161813242458860400010000100002017-01-1115:42:15 MST
  5. 2006981276181324245886040000914000100002017-01-1115:42:16 MST
  6. 00069820161813242458860400007511600100002017-01-1115:42:17 MST
  7. 0006982016181324245886040000433900100002017-01-1115:42:18 MST
  8. 000698228018132424588604000011318500100002017-01-1115:42:19 MST
打印更多统计
vmstat 后面跟上 -s 参数会显示不同统计的总结。
  1. #vmstat-s
  2. 32849392  total memory
  3. 25864128  used memory
  4. 16468180  active memory
  5. 8320888  inactive memory
  6. 6985264  free memory
  7. 181324  buffer memory
  8. 24588612  swap cache
  9. 20970492  total swap
  10. 0  used swap
  11. 20970492  free swap
  12. 891075 non-nice user cpu ticks
  13. 6532nice user cpu ticks
  14. 1507099 system cpu ticks
  15. 18925265601 idle cpu ticks
  16. 113043 IO-wait cpu ticks
  17. 108 IRQ cpu ticks
  18. 4185 softirq cpu ticks
  19. 0 stolen cpu ticks
  20. 4071862 pages paged in
  21. 216759718 pages paged out
  22. 0 pages swapped in
  23. 0 pages swapped out
  24. 369611221 interrupts
  25. 477861261 CPU context switches
  26. 1478258826 boot time
  27. 2196121 forks
打印 slab 统计
vmstat 后面跟上 -m 参数会显示 slab 信息。
  1. #vmstat-m
  2. CacheNumTotalSizePages
  3. nf_conntrack_expect           0024016
  4. nf_conntrack_ffffffff81b2a920     186031212
  5. fib6_nodes                   24596459
  6. ip6_dst_cache                163038410
  7. ndisc_cache                   73025615
  8. ip6_mrt_cache                 0012830
  9. RAWv6353510887
  10. UDPLITEv60010244
  11. UDPv641210244
  12. tw_sock_TCPv6                 0032012
  13. request_sock_TCPv6            0019220
  14. TCPv64619202
  15. fat_inode_cache               566726
  16. fat_cache                     0032112
  17. ioat2                      4096414012830
  18. ext4_inode_cache          343223436410004
  19. ext4_xattr                    008844
  20. .
  21. .
  22. .
阅读更多关于 vmstat
如果你想了解关于 vmstat 的更多选项,请阅读手册。
  1. #vmstat--help
  2. 或者
  3. #manvmstat

作者简介:
Magesh Maruthamuthu,热爱玩所有的 Linux 发行版

作者:Magesh Maruthamuthu[4] 译者:geekpi 校对:wxy
本文由 LCTT[5] 原创编译,Linux中国 荣誉推出
推荐文章
< 左右滑动查看相关文章 >
输入文章 ID 或长按二维码直达
[1]: http://www.2daygeek.com/top-command-examples-to-monitor-server-

[2]: http://www.2daygeek.com/monitor-disk-io-activity-using-iotop-

[3]: http://www.2daygeek.com/monitor-disk-io-activity-using-iotop-

[4]: http://www.2daygeek.com/author/magesh/

[5]: https://github.com/LCTT/TranslateProject
继续阅读
阅读原文