vmstatu

vmstatu

vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况。
相比top,可以看到整个机器的CPU,内存,IO的使用情况,而不是单单看到各个进程的CPU使用率和内存使用率(使用场景不一样)。

1
2
3
4
$vmstat
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 0 215220 0 771404 0 0 2 15 0 1 0 0 100 0 0

一般vmstat工具的使用是通过两个数字参数来完成的,第一个参数是采样的时间间隔数,单位是秒,第二个参数是采样的次数

1
2
3
4
5
$vmstat 2 2
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 202924 0 785780 0 0 2 15 0 1 0 0 100 0 0
0 0 0 203032 0 785812 0 0 0 155 748 1382 0 0 100 0 0

第二个参数如果没有,就会一直采集(ctrl+c 结束)

1
2
3
4
5
6
7
8
$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 0 194712 0 794728 0 0 2 15 0 1 0 0 100 0 0
0 0 0 194696 0 794768 0 0 0 50 782 1368 0 0 100 0 0
0 0 0 193828 0 794776 0 0 0 108 752 1156 0 0 100 0 0
0 0 0 193952 0 794804 0 0 0 4 601 997 0 0 100 0 0
^C

字段

procs

  • r 等待运行的进程数
  • b 处在非中断睡眠状态的进程数

memory (KB)

  • swpd 虚拟内存使用大小

注意:如果swpd的值不为0,但是SI,SO的值长期为0,这种情况不会影响系统性能。

  • free 空闲的内存
  • buff 用作缓冲的内存大小
  • cache 用作缓存的内存大小

注意:如果cache的值大的时候,说明cache处的文件数多,如果频繁访问到的文件都能被cache处,那么磁盘的读IO bi会非常小。

swap

  • si 从交换区写到内存的大小
  • so 每秒写入交换区的内存大小

内存够用的时候,这2个值都是0,如果这2个值长期大于0时,系统性能会受到影响,磁盘IO和CPU资源都会被消耗。有些朋友看到空闲内存(free)很少的或接近于0时,就认为内存不够用了,不能光看这一点,还要结合si和so,如果free很少,但是si和so也很少(大多时候是0),那么不用担心,系统性能这时不会受到影响的。

io

  • bi 每秒读取的块数
  • bo 每秒写入的块数

注意:随机磁盘读写的时候,这2个值越大(如超出1024k),能看到CPU在IO等待的值也会越大。

system

  • in 每秒中断数,包括时钟中断。
  • cs 每秒上下文切换数。

注意:上面2个值越大,会看到由内核消耗的CPU时间会越大。

cpu

  • us 用户进程执行时间(user time)

注意: us的值比较高时,说明用户进程消耗的CPU时间多,但是如果长期超50%的使用,那么我们就该考虑优化程序算法或者进行加速。

  • sy 系统进程执行时间(system time)

注意:sy的值高时,说明系统内核消耗的CPU资源多,这并不是良性表现,我们应该检查原因。

  • id 空闲时间(包括IO等待时间),中央处理器的空闲时间 。以百分比表示。

  • wa 等待IO时间百分比

注意:wa的值高时,说明IO等待比较严重,这可能由于磁盘大量作随机访问造成,也有可能磁盘出现瓶颈(块操作)。

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
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.