Vmstat命令详解

一、前言

vmstat命令:用来查看并获取进程,虚拟内存,页面交换空间以及CPU活动状况的信息,综合这些信息可以分析系统当前的负载情况。

二、使用vmstat

1、用法

vmstat - Report virtual memory statistics

   -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.
		  显示从系统启动至今的fork数量
   -m, --slabs
          Displays slabinfo.
		  显示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.
		  使用指定单位显示。参数有 k 、K 、m 、M ,分别代表1000、1024、1000000、1048576字节(byte)。默认单位为K(1024 bytes)
   -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.
		  宽输出模式(适用于内存较大的系统,默认输出模式会导致不希望的列中断)。输出的宽度超过每行80个字符
   -V, --version
          Display version information and exit.
		  显示版本信息
   -h, --help
          Display help and exit.

2、各部分的含义

名称

标志

说明

Procs:进程

r: The number of runnable processes。等待执行的任务数

展示了正在执行和等待cpu资源的任务个数。当这个值超过了cpu个数,就会出现cpu瓶颈

b: The number of processes in uninterruptible sleep.等待IO的进程数量

这列的值如果长时间大于1,则需要进行关注了

名称

标志

说明

Memory:内存

swpd: the amount of virtual memory used.正在使用虚拟的内存大小,单位k

free: the amount of idle memory.空闲内存大小

buff: the amount of memory used as buffers.已用的buff大小,对块设备的读写进行缓冲

即将写入磁盘的缓冲大小

cache: the amount of memory used as cache.已用的cache大小,文件系统的cache

从磁盘中读取的缓存大小

inact: the amount of inactive memory. (-a option)非活跃内存大小,即被标明可回收的内存,区别于free和active

active: the amount of active memory. (-a option)活跃的内存大小

名称

标志

说明

Swap

si: Amount of memory swapped in from disk (/s).每秒从交换区写入内存的大小(单位:kb/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

us: Time spent running non-kernel code.用户进程执行消耗cpu时间(user time)

us的值比较高时,说明用户进程消耗的cpu时间多,但是如果长期超过50%的使用,那么我们就该考虑优化程序算法或其他措施了

sy: Time spent running kernel code. 系统进程消耗cpu时间(system time)

sys的值过高时,说明系统内核消耗的cpu资源多,这个不是良性的表现,我们应该检查原因。

id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.空闲时间(包括IO等待时间)

wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle.等待IO时间

Wa过高时,说明io等待比较严重,这可能是由于磁盘大量随机访问造成的,也有可能是磁盘的带宽出现瓶颈。

st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.从虚拟机窃取的时间

一般都为0,不用关注

三、具体应用场景

1、第一个参数是采样的时间间隔数,单位是秒,第二个参数是采样的次数

vmstat 3 3

运行结果:

虚拟机centos查找内存大的文件_centos

每3秒进行一次采样,总计三次

2、vmstat每3秒采集数据,一直采集,直到结束程序

vmstat 3

运行结果:

虚拟机centos查找内存大的文件_centos_02