vmstat命令 的含义为显示虚拟内存状态(“Viryual Memor Statics”),但是它可以报告关于进程、内存、I/O等系统整体运行状态。
语法
vmstat(选项)(参数)
选项
-a:显示活动内页;
-f:显示启动后创建的进程总数;
-m:显示slab信息;
-n:头信息仅显示一次;
-s:以表格方式显示事件计数器和内存状态;
-d:报告磁盘状态;
-p:显示指定的硬盘分区状态;
-S:输出信息的单位。
参数
- 事件间隔:状态信息刷新的时间间隔
- 次数:采样次数
一般vmstat工具的使用是通过两个数字参数来完成的,如:
root@ubuntu:~# vmstat 2 1
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
1 0 0 3498472 315836 3819540 0 0 0 1 2 0 0 0 100 0
2表示每个两秒采集一次服务器状态,1表示只采集一次。
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.
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.
虚拟机指标
ref:
Linux vmstat命令--监控CPU 性能分析 - duanxz - 博客园
vmstat 命令,Linux vmstat 命令详解:显示虚拟内存状态 - Linux 命令搜索引擎
Linux vmstat命令实战详解 - ggjucheng - 博客园
Linux Performance Measurements using vmstat - Thomas-Krenn-Wiki