性能调优命令之vmstat

简介

vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存、进程、IO读写、CPU活动等进行监视。它是对系统的整体情况进行统计,不足之处是无法对某个进程进行深入分析。
指令所在路径:/usr/bin/vmstat

输出字段意义

image
Procs:

  • r: The number of processes waiting for run time.
    等待运行的进程数。如果等待运行的进程数越多,意味着CPU非常繁忙。另外,如果该参数长期大于和等于逻辑cpu个数,则CPU资源可能存在较大的瓶颈。

  • b: The number of processes in uninterruptible sleep.
    处在非中断睡眠状态的进程数。意味着进程被阻塞。主要是指被资源阻塞的进程对列数(比如IO资源、页面调度等),通常意味着这些进程在等待I/O(磁盘、网络,用户输入等),当这个值较大时,需要根据应用程序来进行分析,比如数据库产品,中间件应用等。

Memory:

  • swpd: the amount of virtual memory used.
    已使用的虚拟内存大小。如果虚拟内存使用较多,可能系统的物理内存比较吃紧,需要采取合适的方式来减少物理内存的使用。swapd不为0,并不意味物理内存吃紧,如果swapd没变化,si、so的值长期为0,这也是没有问题的

  • free: the amount of idle memory.
    空闲的物理内存的大小

  • buff: the amount of memory used as buffers.
    用来做buffer(缓存,主要用于块设备缓存)的内存数,单位:KB

  • cache: the amount of memory used as cache.
    用来做cache(缓存,主要用于缓存文件)的内存,单位:KB
    频繁访问的文件都会被cached,如果cache值较大,说明cached的文件数较多,如果此时IO中bi比较小,说明文件系统效率比较好。

  • inact: the amount of inactive memory. (-a option)
    inactive memory的总量

  • active: the amount of active memory. (-a option)
    active memroy的总量。

Swap:

  • si: Amount of memory swapped in from disk (/s).
    从磁盘交换到swap虚拟内存的交换页数量,表示由磁盘调入内存,也就是内存进入内存交换区的数量,单位:KB/秒。如果这个值大于0,表示物理内存不够用或者内存泄露了

  • so: Amount of memory swapped to disk (/s).
    从swap虚拟内存交换到磁盘的交换页数量,列表示由内存调入磁盘,也就是内存交换区进入内存的数量。单位:KB/秒,如果这个值大于0,表示物理内存不够用或者内存泄露了

    内存够用的时候,这2个值都是0,如果这2个值长期大于0时,系统性能会受到影响,磁盘IO和CPU资源都会被消耗。

    当看到空闲内存(free)很少的或接近于0时,就认为内存不够用了,这个是不正确的。不能光看这一点,还要结合si和so,如果free很少,但是si和so也很少(大多时候是0),那么不用担心,系统性能这时不会受到影响的。

    当内存的需求大于RAM的数量,服务器启动了虚拟内存机制,通过虚拟内存,可以将RAM段移到SWAP DISK的特殊磁盘段上,这样会 出现虚拟内存的页导出和页导入现象,页导出并不能说明RAM瓶颈,虚拟内存系统经常会对内存段进行页导出,但页导入操作就表明了服务器需要更多的内存了, 页导入需要从SWAP DISK上将内存段复制回RAM,导致服务器速度变慢。

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资源,这个数值应该越小越好。

CPU:

These are percentages of total CPU time.

  • us: Time spent running non-kernel code. (user time, including nice time)
    用户CPU时间(非内核进程占用时间)(单位为百分比)。 us的值比较高时,说明用户进程消耗的CPU时间多

  • sy: Time spent running kernel code. (system time)
    系统使用的CPU时间(单位为百分比)。sy的值高时,说明系统内核消耗的CPU资源多,这并不是良性表现,我们应该检查原因。

  • id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
    空闲的CPU的时间(百分比),在Linux 2.5.41之前,这部分包含IO等待时间。

  • wa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero.
    等待IO的CPU时间,在Linux 2.5.41之前,这个值为0 .这个指标意味着CPU在等待硬盘读写操作的时间,用百分比表示。wait越大则机器io性能就越差。说明IO等待比较严重,这可能由于磁盘大量作随机访问造成,也有可能磁盘出现瓶颈(块操作)。

  • st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.
    虚拟机指标

使用示例

1: 查看vmstat命令的帮助信息

[root@DB-Server ~]# man vmstat

2: 显示活动(active)与非活动(inactive)的内存

[root@DB-Server ~]# vmstat -a 2 10
procs   -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 0  0 242752  56264 1294680 2365840    0    0     1    18    2    2  0  2 97  0  0
 1  0 242752  56504 1294676 2365736    0    0     0     0 1010  511  0  1 100 0  0
 0  0 242752  55844 1294716 2366616    0    0     0    16 1011  768  1  5 94  0  0
 0  0 242752  56760 1294716 2365888    0    0     0   190 1015  554  0  1 99  0  0
 0  0 242752  55472 1294744 2366636    0    0     0     0 1007  751  1  6 94  0  0
 0  0 242752  56636 1294748 2365904    0    0     0    16 1009  554  0  1 99  0  0
 0  0 242752  55844 1294772 2366656    0    0     0   178 1020  746  1  6 93  0  0
 0  0 242752  56884 1294768 2365940    0    0     0     0 1007  543  0  1 99  0  0
 1  0 242752  55208 1294816 2367220    0    0     0   206 1021  726  0  4 95  0  0
 0  0 242752  56760 1294796 2365960    0    0     0    16 1009  606  0  2 98  0  0 

3:不加任何参数,vmstat命令只输出一条记录,这个数据是自系统上次重启之后到现在的平均数值。

[root@DB-Server ~]# vmstat 
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 242752  32496 112680 2724840    0    0     1    18    3    2  0  2 97  0  0

4:显示各种事件计数器表和内存统计信息,这显示不重复。

[oracle@mylnx01 bdump]$ vmstat -s
     33011144  total memory
     32799072  used memory
     24606736  active memory
      6175700  inactive memory
       212072  free memory
        52288  buffer memory
     30158708  swap cache
     12582904  total swap
       610348  used swap
     11972556  free swap
     44159969 non-nice user cpu ticks
         8172 nice user cpu ticks
      6077972 system cpu ticks
    389217442 idle cpu ticks
     40807984 IO-wait cpu ticks
       123964 IRQ cpu ticks
       383333 softirq cpu ticks
            0 stolen cpu ticks
  10331447387 pages paged in
   2287459081 pages paged out
      1524480 pages swapped in
      1433512 pages swapped out
   2358479992 interrupts
   1876082783 CPU context switches
   1481100317 boot time
     15573677 forks

5:可以扩大字段长度,当内存较大时,默认长度不够完全展示内存时,会导致字段值偏移,导致查看不便

[root@DB-Server ~]# vmstat -w 2 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     243852      73556     110908    2678492    0    0     1    18    3    3   0  2  97  0  0
 0  0     243852      72252     110916    2678484    0    0     0   172 1016  701   0  4  95  0  0
 0  0     243852      73556     110916    2678544    0    0     0     0 1005  636   0  2  98  0  0
 0  0     243852      72004     110916    2678540    0    0     0    16 1005  694   0  5  95  0  0
 0  0     243852      73432     110924    2678580    0    0     0   192 1015  629   0  2  98  0  0

如下所示,由于有些字段值较大,导致出现下面偏移,不便查看。

[root@DB-Server ~]# vmstat  2 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 243852  79284 110928 2678640    0    0     1    18    3    3  0  2 97  0  0
 0  0 243852  78988 110928 2678648    0    0     0     0 1006  753  0  5 95  0  0
 0  0 243852  80400 110936 2678696    0    0     0   194 1015  565  0  1 99  0  0
 0  0 243852  78352 110936 2678748    0    0     0    16 1008  680  0  5 95  0  0
 0  0 243852  79532 110936 2678748    0    0     0     0 1007  669  0  2 98  0  0
[root@DB-Server ~]# 

6:显示磁盘分区数据(disk partition statistics )

[oracle@mylnx01 ~]$ vmstat -p sdc5 2 10
sdc5          reads   read sectors  writes    requested writes
            54270570 7234336956    8939045  276196850
            54270570 7234336956    8939045  276196850
            54270570 7234336956    8939050  276196978
            54270570 7234336956    8939053  276197074
            54270574 7234337260    8939053  276197074
            54270577 7234337292    8939066  276197346
            54270622 7234339700    8939066  276197346
            54270622 7234339700    8939069  276197442
            54270859 7234342828    8939078  276197634
            54271074 7234345452    8939080  276197666

性能分析举例

例1:

r b swpd free buff cache si so bi bo in cs us sy wa id
2 1 207740 98476 81344 180972 0 0 2496 0 900 2883 4 12 57 84
0 1 207740 96448 83304 180984 0 0 1968 328 810 2559 8 9 90 83
0 1 207740 94404 85348 180984 0 0 2044 0 829 2879 9 6 88 85
0 1 207740 92576 87176 180984 0 0 1828 0 689 2088 3 9 78 88
2 0 207740 91300 88452 180984 0 0 1276 0 565 2182 7 6 83 87
3 1 207740 90124 89628 180984 0 0 1176 0 551 2219 2 7 91 91
4 2 207740 89240 90512 180984 0 0 880 520 443 907 22 10 77 67
5 3 207740 88056 91680 180984 0 0 1168 0 628 1248 12 11 87 77
4 2 207740 86852 92880 180984 0 0 1200 0 654 1505 6 7 77 87
6 1 207740 85736 93996 180984 0 0 1116 0 526 1512 5 10 75 85
0 1 207740 84844 94888 180984 0 0 892 0 438 1556 6 4 80 90

根据观察值,我们可以得到以下结论: 
1.上下文切换数目高于中断数目,说明 kernel 中相当数量的时间都开销在上下文切换线程. 
2.大量的上下文切换将导致 CPU 利用率分类不均衡.很明显实际上等待 io 请求的百分比(wa)非常高,以及user time 百分比非常低(us). 
3.因为 CPU 都阻塞在 IO 请求上,所以运行队列里也有相当数目的可运行状态线程在等待执行.

例2:

r b swpd free buff cache si so bi bo in cs us sy id wa
17 0 1250 3248 45820 1488472 30 132 992 0 2437 7657 23 77 0 23
11 0 1376 3256 45820 1488888 57 245 416 0 2391 7173 10 90 0 0
12 0 1582 1688 45828 1490228 63 131 1348 76 2432 7315 10 90 0 10
12 2 3981 1848 45468 1489824 185 56 2300 68 2478 9149 15 25 60 73
14 2 10385 2400 44484 1489732 0 87 1112 20 2515 11620 0 15 85 88
14 2 12671 2280 43644 1488816 76 51 1812 204 2546 11407 20 45 35 35

根据观察值,我们可以得到以下结论:

  1. 大量的读请求回内存(bi),导致了空闲内存在不断的减少(free).这就使得系统写入 swap device 的块数目(so)和 swap 空间(swap)在不断增加.
  2. si、so的值长期不为0,则表示系统内存不足,同时看到 CPU Wati I/O time(wa)百分比很大.这表明 I/O 请求已经导致 CPU 开始效率低下.
posted @ 2018-03-20 23:14  尘世风  阅读(10077)  评论(2编辑  收藏  举报
*/