procps 是一个很强大的工具包,其中包含有free、sysctl 、ps、top 等常用工具,也包含 slabtop、pamp等高级分析工具,还包含了一个重要的工具vmstat 。

一、基本用法

直接使用vmstat会只输出一次当前主机的一些使用情况,后面也可以跟两个数字,每一个数字为多少秒刷新一次结果,后面一个数字代面输出几次。

1$ vmstat 1 5
2procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
3 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
4 3  0      0  44712 110052 623096    0    0    30    28  217  888 13  3 83  1  0
5 0  0      0  44408 110052 623096    0    0     0     0   88 1446 31  4 65  0  0
6 0  0      0  44524 110052 623096    0    0     0     0   84  872 11  2 87  0  0
7 0  0      0  44516 110052 623096    0    0     0     0  149 1429 18  5 77  0  0
8 0  0      0  44524 110052 623096    0    0     0     0   60  431 14  1 85  0  0

如上所示,每秒输出一次,共输出五次系统相关指标的情况。每项指标的意思如下:

  • 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, included in idle.
    • st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.

二、vmstat与CPU load

测试开始前使用vmstat 查看到的主机的总体情况如下;

1# vmstat 1 -w
2procs -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu--------
3 r  b         swpd         free         buff        cache   si   so    bi    bo   in   cs  us  sy  id  wa  st
4 1  0            0      4205708         2256      3197224    0    0     0     2    9    0   0   0  99   0   0
5 0  0            0      4205732         2256      3197224    0    0     0     0  292  560   0   0 100   0   0
6 0  0            0      4205700         2256      3197224    0    0     0     0  308  577   0   0 100   0   0
7 0  0            0      4205732         2256      3197224    0    0     0     0  294  556   0   0 100   0   0
8 0  0            0      4205732         2256      3197224    0    0     0     4  303  581   0   0 100   0   0
9 0  0            0      4205732         2256      3197224    0    0     0     0  323  606   0   0 100   0   0

从图上可以看到整个CPU都是idle的,在使用dd命令进行如下测试后,会发现CPU的sy项会上去

 1# dd if=/dev/urandom of=500MBfile bs=1M count=500
 2# vmstat 1 -w
 3procs -----------------------memory---------------------- ---swap-- -----io---- -system-- --------cpu--------
 4 r  b         swpd         free         buff        cache   si   so    bi    bo   in   cs  us  sy  id  wa  st
 5 1  0            0      4504584         2256      2897140    0    0     0     0 1525 1144   0  50  50   0   0
 6 1  0            0      4493120         2256      2908748    0    0     0     0 1508 1117   0  50  50   0   0
 7 1  0            0      4481604         2256      2920360    0    0     0     0 1526 1135   0  50  50   0   0
 8 1  0            0      4469112         2256      2932972    0    0     0     0 1523 1132   0  50  50   0   0
 9 1  0            0      4457548         2256      2944556    0    0     0     0 1524 1128   0  50  49   0   0
10 1  0            0      4445868         2256      2956252    0    0     0     0 1520 1134   0  50  50   0   0
11 1  0            0      4434344         2256      2967620    0    0     0    36 1494 1081   0  50  50   0   0
12 1  0            0      4422696         2256      2979120    0    0     0     0 1518 1134   0  50  50   0   0
13 1  0            0      4410048         2256      2992040    0    0     0     1 1518 1134   0  50  50   0   0
14 1  0            0      4398500         2256      3003520    0    0     0     0 1515 1123   1  50  50   0   0
15 1  0            0      4387068         2256      3015188    0    0     0     0 1519 1133   0  50  50   0   0
1620  0            0      4375216         2256      3026632    0    0     0  8704 1498 1012   1  53  46   0   0
17 4  0            0      4374288         2256      3027732    0    0     0 106504  306  298   0 100   0   0   0
1815  0            0      4374112         2256      3027920    0    0     0 98312  267  225   0 100   0   0   0
1918  0            0      4374160         2256      3027872    0    0     0 98312  265  214   0 100   0   0   0
20 3  0            0      4367092         2256      3035024    0    0     0 35304  916  478   0  54  46   0   0

上面这个操作其实是分两部分的,前面一部分从/dev/urandom中取随机数会非常耗CPU的system占用,取完在写IO的过程中,会发现io项的block out(即写IO)也会出现增加。

三、vmstat与高IO read

测试前vmstat查看到的主机资源情况如下:

1# vmstat 1 -w
2procs -------------------memory------------------ ---swap-- -----io---- --system-- -----cpu-------
3 r  b       swpd       free       buff      cache   si   so    bi    bo   in   cs  us sy  id wa st
4 0  0     137636     246760     187628    1129464    0    0     2    26    7    6  14  4  82  0  0
5 0  0     137636     246760     187628    1129460    0    0     0     0  106  173   0  0  99  0  0
6 0  0     137636     246760     187628    1129460    0    0     0     0  106  203   0  0 100  0  0
7 0  0     137636     244208     187632    1129472    0    0     4    56  477  516   3  3  94  1  0
8 0  0     137636     244208     187632    1129472    0    0     0     0  126  224   0  1 100  0  0
9 0  0     137636     244208     187632    1129472    0    0     0     0  160  347   0  0  99  0  0

使用dd命令读取一个大文件,并将其内容写入/dev/null

 1# dd if=361way.com_redhat_dvd.iso of=/dev/null bs=1M
 2# vmstat 1 -w
 3procs -------------------memory------------------ ---swap-- -----io---- --system-- -----cpu-------
 4 r  b       swpd       free       buff      cache   si   so    bi    bo   in   cs  us sy  id wa st
 5 0  1     137764     148004     184180    1243028    0    0 195840     0 1793 2767   0 16  47 36  0
 6 1  0     137764     138208     184180    1255772    0    0 204672     0 1992 2906   0 18  48 34  0
 7 1  0     137764     149368     184180    1248132    0    0 206208     4 1932 2882   0 18  48 35  0
 8 0  1     137764     129156     184180    1271188    0    0 211200    32 1891 3096   1 17  48 35  0
 9 0  1     137764     117748     184180    1285796    0    0 210176     0 1947 2945   1 17  48 34  0
10 0  1     137764     133000     184180    1273800    0    0 207616     0 2062 3064   0 18  47 34  0
11 1  0     137764     125932     184180    1283836    0    0 216064     0 2089 3194   0 17  48 35  0
12 0  1     137764     129156     184180    1283624    0    0 224768     0 2201 3417   0 18  47 35  0

四、CPU 与CPU wait

In the following example, an updatedb process is already running. The updatedb utility is part of mlocate. It examines the entire file system and accordingly creates the database for the locate command (by means of which file searches can be performed very quickly). Because updatedb reads all of the file names from the entire file system, the CPU must wait to get data from the IO system (the hard disk). For that reason, vmstat running in parallel will display large values for wa (waiting for IO):

1$ vmstat 1 5
2procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
3 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
4 2  1 403256 602848  17836 400356    5   15    50    50  207  861 13  3 83  1  0
5 1  0 403256 601568  18892 400496    0    0  1048   364  337 1903  5  7  0 88  0
6 0  1 403256 600816  19640 400568    0    0   748     0  259 1142  6  4  0 90  0
7 0  1 403256 600300  20116 400800    0    0   476     0  196  630  8  5  0 87  0
8 0  1 403256 599328  20792 400792    0    0   676     0  278 1401  7  5  0 88  0

五、vmstat其他

vmstat帮助信息:

 1$ vmstat --help
 2usage: vmstat [-V] [-n] [delay [count]]
 3              -V prints version.
 4              -n causes the headers not to be reprinted regularly.
 5              -a print inactive/active page stats.
 6              -d prints disk statistics
 7              -D prints disk table
 8              -p prints disk partition statistics
 9              -s prints vm table
10              -m prints slabinfo
11              -S unit size
12              delay is the delay between updates in seconds.
13              unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)
14              count is the number of updates

vmstat -a结果

1$ vmstat -a
2procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
3 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
4 3  0  14960  38024 988284 461704    0    1   506    81  726 1372 12  4 81  3  0

vmstat -d结果

 1$ vmstat -d
 2disk- ------------reads------------ ------------writes----------- -----IO------
 3       total merged sectors      ms  total merged sectors      ms    cur    sec
 4ram0       0      0       0       0      0      0       0       0      0      0
 5ram1       0      0       0       0      0      0       0       0      0      0
 6ram2       0      0       0       0      0      0       0       0      0      0
 7ram3       0      0       0       0      0      0       0       0      0      0
 8ram4       0      0       0       0      0      0       0       0      0      0
 9ram5       0      0       0       0      0      0       0       0      0      0
10ram6       0      0       0       0      0      0       0       0      0      0
11ram7       0      0       0       0      0      0       0       0      0      0
12ram8       0      0       0       0      0      0       0       0      0      0
13ram9       0      0       0       0      0      0       0       0      0      0
14ram10      0      0       0       0      0      0       0       0      0      0
15ram11      0      0       0       0      0      0       0       0      0      0
16ram12      0      0       0       0      0      0       0       0      0      0
17ram13      0      0       0       0      0      0       0       0      0      0
18ram14      0      0       0       0      0      0       0       0      0      0
19ram15      0      0       0       0      0      0       0       0      0      0
20sda   136909  31536 13893867 1197609  58190 219323 2233264 7688807      0    677
21sda1   35703   6048 1326394  511477   6728  16136  182984  419232      0    222
22sda2      85   1489    2935     653    141   3603   29952    5254      0      1
23sda3  101111  23961 12564154  685330  51321 199584 2020328 7264321      0    512

vmstat -D

 1$ vmstat -D
 2           22 disks
 3            0 partitions
 4       273820 total reads
 5        63034 merged reads
 6     27787446 read sectors
 7      2395193 milli reading
 8       116450 writes
 9       438666 merged writes
10      4467248 written sectors
11     15377932 milli writing
12            0 inprogress IO
13         1412 milli spent IO

vmstat -p

1vmstat -p /dev/sda9
2sda9          reads   read sectors  writes    requested writes
3               23420     411365      24464     530801

vmstat -s

 1$ vmstat -s
 2      1553972  total memory
 3      1516180  used memory
 4       461892  active memory
 5       988304  inactive memory
 6        37792  free memory
 7         6644  buffer memory
 8      1069388  swap cache
 9      1052248  total swap
10        14960  used swap
11      1037288  free swap
12       161467 non-nice user cpu ticks
13         7586 nice user cpu ticks
14        46310 system cpu ticks
15      1108919 idle cpu ticks
16        46832 IO-wait cpu ticks
17         2694 IRQ cpu ticks
18         2452 softirq cpu ticks
19            0 stolen cpu ticks
20      6947021 pages paged in
21      1116896 pages paged out
22          183 pages swapped in
23         3744 pages swapped out
24      9985406 interrupts
25     18852586 CPU context switches
26   1239004323 boot time
27        15072 forks

vmstat -m

 1$ vmstat -m
 2Cache                       Num  Total   Size  Pages
 3fuse_request                 11     11    368     11
 4fuse_inode                    9      9    448      9
 5rpc_inode_cache               8      8    512      8
 6nf_conntrack_expect           0      0    168     24
 7nf_conntrack                 26     80    248     16
 8dm_uevent                     0      0   2464      3
 9UDPv6                        22     22    704     11
10TCPv6                         6      6   1344      6
11kmalloc_dma-512               8      8    512      8
12sgpool-128                   12     12   2048      4
13scsi_io_context               0      0    104     39
14ext3_inode_cache           6822   8360    496      8
15ext3_xattr                   85     85     48     85
16journal_handle              170    170     24    170
17journal_head                 76    219     56     73
18revoke_record               256    256     16    256
19flow_cache                    0      0     80     51
20bsg_cmd                       0      0    288     14
21mqueue_inode_cache            7      7    576      7
22isofs_inode_cache             0      0    376     10
23hugetlbfs_inode_cache        11     11    344     11
24dquot                         0      0    128     32
25shmem_inode_cache          1058   1071    448      9
26xfrm_dst_cache                0      0    320     12
27UDP                          19     21    576      7
28TCP                          17     24   1216      6
29blkdev_queue                 21     21   1080      7
30biovec-256                    2      2   3072      2
31biovec-128                    5      5   1536      5
32biovec-64                     7     10    768      5
33sock_inode_cache            619    650    384     10
34file_lock_cache              39     39    104     39
35Acpi-Operand               2935   2958     40    102
36Acpi-Namespace             1700   1700     24    170
37Cache                       Num  Total   Size  Pages
38taskstats                    25     26    312     13
39proc_inode_cache            233    242    360     11
40sigqueue                     28     28    144     28
41radix_tree_node            7888   8606    296     13
42bdev_cache                   24     24    512      8
43inode_cache                 370    462    344     11
44dentry                     6592  15390    136     30
45names_cache                   2      2   4096      2
46avc_node                     73     73     56     73
47selinux_inode_security     9888  10030     48     85
48idr_layer_cache             627    644    144     28
49buffer_head                2308   2688     64     64
50mm_struct                   659    693    448      9
51vm_area_struct            11110  11592     88     46
52files_cache                 115    130    384     10
53sighand_cache               141    150   1344      6
54task_struct                 246    248   3696      2
55anon_vma                   4778   5120     16    256
56kmalloc-4096                 95    112   4096      8
57kmalloc-2048                272    304   2048     16
58kmalloc-1024                518    524   1024      4
59kmalloc-512                 764    888    512      8
60kmalloc-256                 198    208    256     16
61kmalloc-128                 629    832    128     32
62kmalloc-64                 4322   5568     64     64
63kmalloc-32                 1554   1664     32    128
64kmalloc-16                 2644   3584     16    256
65kmalloc-8                  3561   3584      8    512
66kmalloc-192                6349   6930    192     21
67kmalloc-96                  885   1176     96     42