Linux vmstat命令
简介
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存、进程、CPU活动进行监控。是对系统的整体情况进行统计,不足之处是无法对某个进程进行深入分析。
vmstat命令是最常见的Linux/Unix监控工具,属于sysstat包。可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况。这个命令是我查看Linux/Unix最喜爱的命令,一个是Linux/Unix都支持,二是相比top,我可以看到整个机器的CPU,内存,IO的使用情况,而不是单单看到各个进程的CPU使用率和内存使用率(使用场景不一样)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | [root@docker35 ~]# man vmstat VMSTAT(8) System Administration VMSTAT(8) NAME vmstat - Report virtual memory statistics SYNOPSIS vmstat [options] [delay [count]] DESCRIPTION vmstat reports information about processes, memory, paging, block IO, traps, disks and cpu activity. The first report produced gives averages since the last reboot. Additional reports give information on a sampling period of length delay. The process and memory reports are instantaneous in either case. OPTIONS delay The delay between updates in seconds. If no delay is specified, only one report is printed with the average values since boot. count Number of updates. In absence of count, when delay is defined, default is infinite. -a, --active Display active and inactive memory, given a 2.5.41 kernel or better. -f, --forks The -f switch displays the number of forks since boot. This includes the 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, --slabs Displays slabinfo. -n, --one-header Display the header only once rather than periodically. -s, --stats Displays a table of various event counters and memory statistics. This display does not repeat. -d, --disk Report disk statistics (2.5.70 or above required). -D, --disk-sum Report some summary statistics about disk activity. -p, --partition device Detailed statistics about partition (2.5.70 or above required). -S, --unit character Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes. Note this does not change the swap (si/so) or block (bi/bo) fields. -t, --timestamp Append timestamp to each line -w, --wide Wide output mode (useful for systems with higher amount of memory, where the default output mode suffers from unwanted column breakage). The output is wider than 80 characters per line. -V, --version Display version information and exit. -h, --help Display help and exit. FIELD DESCRIPTION FOR VM MODE Procs r: The number of runnable processes (running or 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, included in idle. st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown. 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 available objects size: Size of each object pages: Number of pages with at least one active object 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 free(1), iostat(1), mpstat(1), ps(1), sar(1), top(1) |
安装
1 | yum install -y sysstat |
一般vmstat工具的使用是通过两个数字参数来完成的,第一个参数是采样的时间间隔数,单位是秒,第二个参数是采样的次数,如:
1 2 3 4 5 | 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 31620 147096 135020 528532 0 0 15 49 130 91 5 1 94 0 0 0 0 31620 151040 135040 528540 0 0 0 600 1031 1420 9 3 86 3 0 0 0 31620 151040 135044 528540 0 0 0 40 411 612 2 1 97 1 0 |
2表示每个两秒采集一次服务器状态,1表示只采集一次。
实际上,在应用过程中,我们会在一段时间内一直监控,不想监控直接结束vmstat就行了,例如:
1 2 3 4 5 6 7 8 9 10 11 | [root@lhrblog ~]# vmstat 2 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 30592 200888 170500 620792 0 0 16 49 126 82 5 1 94 0 0 0 0 30592 200428 170500 620788 0 0 0 6 435 648 2 1 97 0 0 0 0 30592 200512 170500 620788 0 0 0 24 337 512 2 1 98 0 0 0 0 30592 200424 170500 620788 0 0 0 0 333 510 2 1 98 0 0 0 0 30592 200512 170500 620788 0 0 0 94 365 553 2 1 97 0 0 0 0 30592 200452 170500 620788 0 0 0 0 386 554 2 1 97 0 0 0 0 30592 200448 170500 620788 0 0 0 0 345 513 2 1 97 0 0 ^C |
这表示vmstat每2秒采集数据,一直采集,直到我结束程序。
vmstat字段含义说明
procs(进程)
r,表示等待执行的任务数,展示了正在执行和等待cpu资源的任务个数。当这个值超过了cpu个数,就会出现cpu瓶颈。
b,表示等待IO的进程数量
memory
swpd:正在使用虚拟的内存大小,单位k
free:空闲内存大小
buff:已用的buff大小,对块设备的读写进行缓冲
cache:已用的cache大小,文件系统的cache
swap
swap(交换空间,单位:KB);内存够用的时候,这2个值都是0,如果这2个值长期大于0时,系统性能会受到影响,磁盘IO和CPU资源都会被消耗。有时我们看到空闲内存(free)很少的或接近于0时,就认为内存不够用了,不能光看这一点,还要结合si和so,如果free很少,但是si和so也很少(大多时候是0),那么不用担心,系统性能这时不会受到影响的;
si 即swap in,表示每秒从交换区写入内存的大小(单位:kb/s)
so 即swap out,表示每秒从内存写到交换区的大小(单位:kb/s)
一般在内存充足的情况下是不应有大量的 si,so的。
io
bi:每秒读取的块数(读磁盘),块设备每秒接收的块数量,单位是block,这里的块设备是指系统上所有的磁盘和其他块设备,现在的Linux版本块的大小为1024bytes
bo:每秒写入的块数(写磁盘),块设备每秒发送的
system
in:每秒中断数,包括时钟中断,这两个值越大,会看到由内核消耗的cpu时间sy会越多
cs:每秒上下文切换数,例如我们调用系统函数,就要进行上下文切换,线程的切换,也要进程上下文切换,这个值要越小越好,太大了,要考虑调低线程或者进程的数目
cpu
- us:用户进程执行消耗cpu时间(user time),us的值比较高时,说明用户进程消耗的cpu时间多,但是如果长期超过50%的使用,那么我们就该考虑优化程序算法或其他措施了
- sy:系统进程消耗cpu时间(system time),sys的值过高时,说明系统内核消耗的cpu资源多,这个不是良性的表现,我们应该检查原因。这里us + sy的参考值为80%,如果us+sy 大于 80%说明可能存在CPU不足
- Id:空闲时间(包括IO等待时间),一般来说 us+sy+id=100
- wa:等待IO时间,wa过高时,说明io等待比较严重,这可能是由于磁盘大量随机访问造成的,也有可能是磁盘的带宽出现瓶颈。
- st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown. 来自于虚拟机偷取的CPU所占的百分比
简单分析
- procs r: 运行的进程比较多,系统很繁忙,展示了正在执行和等待cpu资源的任务个数。当这个值超过了cpu个数,就会出现cpu瓶颈。
- swap:如果swpd的值不为0,但是SI,SO的值长期为0,这种情况不会影响系统性能。
- si,so:这两个值越大,会看到由内核消耗的cpu时间会越多。注意:内存够用的时候,这2个值都是0,如果这2个值长期大于0时,系统性能会受到影响,磁盘IO和CPU资源都会被消耗。有些朋友看到空闲内存(free)很少的或接近于0时,就认为内存不够用了,不能光看这一点,还要结合si和so,如果free很少,但是si和so也很少(大多时候是0),那么不用担心,系统性能这时不会受到影响的。
- bi/bo::磁盘写的数据量稍大,如果是大文件的写,10M以内基本不用担心,如果是小文件写2M以内基本正常
- cpu us::us的值比较高时,说明用户进程消耗的cpu时间多,如果持续大于50%,服务高峰期可以接受, 如果长期大于50 ,可以考虑优化
- cpu sy::现实内核进程所占的百分比,sys的值过高时,说明系统内核消耗的cpu资源多,这里us + sy的参考值为80%,如果us+sy 大于 80%说明可能存在CPU不足。
- cpu wa: 列显示了IO等待所占用的CPU时间的百分比。这里wa的参考值为30%,如果wa超过30%,说明IO等待严重,这可能是磁盘大量随机访问造成的,也可能磁盘或者磁盘访问控制器的带宽瓶颈造成的(主要是块操作)。
- cpu id: CPU 空闲时所占百分比 平常持续小于50,服务高峰期可以接受
vmstat其它命令
以MB为单位输出统计信息
vmstat的-S和-M参数(大写和MB)将会以MB为单位输出。vmstat默认以KB为单位输出统计信息。
1 2 3 4 5 6 7 8 9 | [root@tecmint ~]# vmstat -S M 1 5 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 0 0 0 346 53 476 0 0 95 8 42 55 0 2 96 2 0 0 0 0 346 53 476 0 0 0 0 12 15 0 0 100 0 0 0 0 0 346 53 476 0 0 0 0 32 62 0 0 100 0 0 0 0 0 346 53 476 0 0 0 0 15 13 0 0 100 0 0 0 0 0 346 53 476 0 0 0 0 34 61 0 1 99 0 0 |
磁盘统计信息
vmstat的-d参数将会输出所有磁盘的统计信息。
1 2 3 4 5 | [root@lhrblog ~]# vmstat -d disk- ------------reads------------ ------------writes----------- -----IO------ total merged sectors ms total merged sectors ms cur sec vda 180369 485 9242190 727905 1350194 708714 29594697 4822112 0 1087 [root@lhrblog ~]# |
统计各种计数器
vmstat命令的-s参数,将输出各种事件计数器和内存的统计信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@lhrblog ~]# vmstat -s 3880196 K total memory 3061248 K used memory 1806036 K active memory 971920 K inactive memory 153368 K free memory 135572 K buffer memory 530008 K swap cache 6291452 K total swap 31620 K used swap 6259832 K free swap 1370218 non-nice user cpu ticks 89 nice user cpu ticks 417191 system cpu ticks 28402099 idle cpu ticks 129331 IO-wait cpu ticks 0 IRQ cpu ticks 15575 softirq cpu ticks 0 stolen cpu ticks 4621355 pages paged in 14808448 pages paged out 106 pages swapped in 7991 pages swapped out 82557583 interrupts 113768356 CPU context switches 1627892122 boot time 343383 forks |
带时间戳的vmstat命令
带-t参数执行vmstat命令,该命令将会在每一行输出后都带一个时间戳,如下所示。
1 2 3 4 5 6 7 8 | [root@lhrblog ~]# vmstat -t 1 5 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- -----timestamp----- r b swpd free buff cache si so bi bo in cs us sy id wa st CST 1 0 31620 153168 135580 530112 0 0 15 49 131 92 5 1 94 0 0 2021-08-04 10:37:26 0 0 31620 153164 135596 530112 0 0 0 584 605 913 2 1 94 4 0 2021-08-04 10:37:27 0 0 31620 153128 135596 530112 0 0 0 0 433 581 4 1 95 0 0 2021-08-04 10:37:28 0 0 31620 153288 135596 530112 0 0 0 0 300 540 0 1 100 0 0 2021-08-04 10:37:29 0 0 31620 153080 135596 530112 0 0 0 192 416 567 4 1 96 0 0 2021-08-04 10:37:30 |
列出活动和非活动的内存
vmstat的man页面中解析的每一列的意义。最重要的是内存中的free属性和交换分区中的si和so属性。
1 2 3 4 | [root@lhrblog ~]# vmstat -a procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free inact active si so bi bo in cs us sy id wa st 1 0 31620 162464 972004 1794968 0 0 15 49 131 92 5 1 94 0 0 |