10.6 linux下常见的性能分析工具
10.6.1 vmstat
虚拟内存统计。
vmstat [-V] [-n] [delay [count] ]
各个选项及参数意义如下:
-V 打印版本
-n周期性循环输出时,输出的头部信息仅显示一次
delay 表示两次输出之间的时间间隔(秒)
count 按照delay指定的时间间隔统计的次数,默认为连续输出不间断
例如:
vmstat 1
表示每3秒更新一次输出信息,循环输出,ctrl+c停止输出
vmstat 3 5
表示每3秒更新一次输出信息,统计5次后停止
下面是vmstat命令在某个系统的输出结果:
$ vmstat 2 3
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
4 0 119736 3520744 477816 10162532 0 0 2 20 0 0 3 1 96 0
4 0 119736 3520416 477816 10162532 0 0 4 132 2449 64597 15 10 75 0
8 0 119736 3520968 477816 10162532 0 0 0 0 2411 63536 16 10 75 0
输出项的解释如下:
procs
* r列表示运行和等待cpu时间片段的进程数,这个值如果长期大约系统cpu个数,说明cpu不足
* b列表示在等待资源的进程数,比如正在等待IO或者内存交换等等
memory
* swap列表示切换到交换区的内存大小(KB为单位),如果swap的值不为0或者比较大,只要si和so长期为0,一般不是性能问题
* free列表示当前空闲的物理内存数量(以KB为单位)
* buff列表示buffers cache的内存数量,一般对块设备的读写才需要缓冲
* cache列表示page cached的内存数量,一般作为文件系统进行缓存,频繁访问的文件都会被缓存。如果cache值较大,说明缓存文件较多,如果此时io中的bi比较小,说明文件系统效率比较好。
swap
* si列表示由磁盘调入内存,也就是由内存进入内存交换区的内存大小,单位KB/秒
* so列表示由内存调入磁盘,也就是由内存交换区进入内存的大小,单位KB/秒。
在一般情况下,si、so的值都为0,如果si、so值长期不为0,则表示系统内存不足,需要增加系统内存。
io
io项显示磁盘读写情况
bi列表示从块设备读入数据的总量(即读磁盘)(kb/s)
bo列表示写到块设备的数据总量(即写磁盘)(kb/s)
bi+bo的参考值为1000,如果超过1000,而且wa值较大,则表示系统磁盘IO有问题,应该考虑提高磁盘的读写性能。
system
显示采集间隔内发生的中断数
in列表示在某一时间间隔内观测到的每秒设备中断数
cs列表示每秒产生的上下文切换次数
上面的两个值越大,由内核消耗的CPU时间越多。
CPU
显示了CPU的使用状态,此列是关注的重点。
us列显示了用户进程消耗的CPU时间百分比。us的值比较高时,说明用户进程消耗的CPU时间多,但是如果长期大约50%,就需要考虑优化算法或程序。
sy列显示了内核进程消耗的CPU时间百分比。sy的值较高时,说明内核消耗的CPU资源很多。
根据经验,us+sy的参考值为80%,如果us+sy大约80%,说明可能存在CPU资源不足。
id列显示了CPU处在空闲时间的时间百分比。
wa列显示了IO等待所占用的CPU时间百分比。wa值越高,说明IO等待越严重。根据经验,wa的参考值为20%,如果wa超过20%,说明IO等待严重,引起IO等待的原因可能是磁盘大量随机读写造成的,也可能是磁盘或者磁盘控制器的带宽瓶颈(主要是块操作)造成的。
综上所述,在对CPU的评估中,需要重点注意procs项中r列的值和CPU项中us、sy和id列的值。
上面是从书中摘抄的,然后又从网上查找了一下,
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, shown as zero.