vmstat 命令详解

来源:互联网 发布:矩阵是什么意思 编辑:程序博客网 时间:2024/04/28 17:11

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

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

root@ubuntu:~# vmstat 2 1procs -----------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表示只采集一次。

实际上,在应用过程中,我们会在一段时间内一直监控,不想监控直接结束vmstat就行了,例如:

复制代码
root@ubuntu:~# vmstat 2  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 3499840 315836 3819660    0    0     0     1    2    0  0  0 100  0 0  0      0 3499584 315836 3819660    0    0     0     0   88  158  0  0 100  0 0  0      0 3499708 315836 3819660    0    0     0     2   86  162  0  0 100  0 0  0      0 3499708 315836 3819660    0    0     0    10   81  151  0  0 100  0 1  0      0 3499732 315836 3819660    0    0     0     2   83  154  0  0 100  0
复制代码

这表示vmstat每2秒采集数据,一直采集,直到我结束程序,这里采集了5次数据我就结束了程序。

好了,命令介绍完毕,现在开始实战讲解每个参数的意思。

r 表示运行队列(就是说多少个进程真的分配到CPU),我测试的服务器目前CPU比较空闲,没什么程序在跑,当这个值超过了CPU数目,就会出现CPU瓶颈了。这个也和top的负载有关系,一般负载超过了3就比较高,超过了5就高,超过了10就不正常了,服务器的状态很危险。top的负载类似每秒的运行队列。如果运行队列过大,表示你的CPU很繁忙,一般会造成CPU使用率很高。

b 表示阻塞的进程,这个不多说,进程阻塞,大家懂的。

swpd 虚拟内存已使用的大小,如果大于0,表示你的机器物理内存不足了,如果不是程序内存泄露的原因,那么你该升级内存了或者把耗内存的任务迁移到其他机器。

free   空闲的物理内存的大小,我的机器内存总共8G,剩余3415M。

buff   Linux/Unix系统是用来存储,目录里面有什么内容,权限等的缓存,我本机大概占用300多M

cache cache直接用来记忆我们打开的文件,给文件做缓冲,我本机大概占用300多M(这里是Linux/Unix的聪明之处,把空闲的物理内存的一部分拿来做文件和目录的缓存,是为了提高 程序执行的性能,当程序使用内存时,buffer/cached会很快地被使用。)

si  每秒从磁盘读入虚拟内存的大小,如果这个值大于0,表示物理内存不够用或者内存泄露了,要查找耗内存进程解决掉。我的机器内存充裕,一切正常。

so  每秒虚拟内存写入磁盘的大小,如果这个值大于0,同上。

bi  块设备每秒接收的块数量,这里的块设备是指系统上所有的磁盘和其他块设备,默认块大小是1024byte,我本机上没什么IO操作,所以一直是0,但是我曾在处理拷贝大量数据(2-3T)的机器上看过可以达到140000/s,磁盘写入速度差不多140M每秒

bo 块设备每秒发送的块数量,例如我们读取文件,bo就要大于0。bi和bo一般都要接近0,不然就是IO过于频繁,需要调整。

in 每秒CPU的中断次数,包括时间中断

cs 每秒上下文切换次数,例如我们调用系统函数,就要进行上下文切换,线程的切换,也要进程上下文切换,这个值要越小越好,太大了,要考虑调低线程或者进程的数目,例如在apache和nginx这种web服务器中,我们一般做性能测试时会进行几千并发甚至几万并发的测试,选择web服务器的进程可以由进程或者线程的峰值一直下调,压测,直到cs到一个比较小的值,这个进程和线程数就是比较合适的值了。系统调用也是,每次调用系统函数,我们的代码就会进入内核空间,导致上下文切换,这个是很耗资源,也要尽量避免频繁调用系统函数。上下文切换次数过多表示你的CPU大部分浪费在上下文切换,导致CPU干正经事的时间少了,CPU没有充分利用,是不可取的。

us 用户CPU时间,我曾经在一个做加密解密很频繁的服务器上,可以看到us接近100,r运行队列达到80(机器在做压力测试,性能表现不佳)。

sy 系统CPU时间,如果太高,表示系统调用时间长,例如是IO操作频繁。

id  空闲 CPU时间,一般来说,id + us + sy = 100,一般我认为id是空闲CPU使用率,us是用户CPU使用率,sy是系统CPU使用率。

wt 等待IO CPU时间。


vmstat是一个十分有用的Linux系统监控工具,使用vmstat命令可以得到关于进程、内存、内存分页、堵塞IO、traps及CPU活动的信息。

一、前言

很显然从名字中我们就可以知道vmstat是一个查看虚拟内存(Virtual Memory)使用状况的工具,但是怎样通过vmstat来发现系统中的瓶颈呢?在回答这个问题前,还是让我们回顾一下Linux中关于虚拟内存相关内容。

二、虚拟内存运行原理

在系统中运行的每个进程都需要使用到内存,但不是每个进程都需要每时每刻使用系统分配的内存空间。当系统运行所需内存超过实际的物理内存,内核会释放某些进程所占用但未使用的部分或所有物理内存,将这部分资料存储在磁盘上直到进程下一次调用,并将释放出的内存提供给有需要的进程使用。

在Linux内存管理中,主要是通过“调页Paging”和“交换Swapping”来完成上述的内存调度。调页算法是将内存中最近不常使用的页面换到磁盘上,把活动页面保留在内存中供进程使用。交换技术是将整个进程,而不是部分页面,全部交换到磁盘上。

分页(Page)写入磁盘的过程被称作Page-Out,分页(Page)从磁盘重新回到内存的过程被称作Page-In。当内核需要一个分页时,但发现此分页不在物理内存中(因为已经被Page-Out了),此时就发生了分页错误(Page Fault)。

当系统内核发现可运行内存变少时,就会通过Page-Out来释放一部分物理内存。经管Page-Out不是经常发生,但是如果Page-out频繁不断的发生,直到当内核管理分页的时间超过运行程式的时间时,系统效能会急剧下降。这时的系统已经运行非常慢或进入暂停状态,这种状态亦被称作thrashing(颠簸)。

三、使用vmstat

1.用法

vmstat [-a] [-n] [-S unit] [delay [ count]]

vmstat [-s] [-n] [-S unit]

vmstat [-m] [-n] [delay [ count]]

vmstat [-d] [-n] [delay [ count]]

vmstat [-p disk partition] [-n] [delay [ count]]

vmstat [-f]

vmstat [-V]

-a:显示活跃和非活跃内存

-f:显示从系统启动至今的fork数量 。引申閱讀: http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html

-m:显示slabinfo

-n:只在开始时显示一次各字段名称。

-s:显示内存相关统计信息及多种系统活动数量。

delay:刷新时间间隔。如果不指定,只显示一条结果。

count:刷新次数。如果不指定刷新次数,但指定了刷新时间间隔,这时刷新次数为无穷。

-d:显示磁盘相关统计信息。

-p:显示指定磁盘分区统计信息

-S:使用指定单位显示。参数有 k 、K 、m 、M ,分别代表1000、1024、1000000、1048576字节(byte)。默认单位为K(1024 bytes)

-V:显示vmstat版本信息。

2.使用说明

例子1:每2秒输出一条结果

字段说明:

Procs(进程):

r: 运行队列中进程数量

b: 等待IO的进程数量

Memory(内存):

swpd: 使用虚拟内存大小

free: 可用内存大小

buff: 用作缓冲的内存大小

cache: 用作缓存的内存大小

Swap:

si: 每秒从交换区写到内存的大小

so: 每秒写入交换区的内存大小

IO:(现在的Linux版本块的大小为1024bytes)

bi: 每秒读取的块数

bo: 每秒写入的块数

系统:

in: 每秒中断数,包括时钟中断。

cs: 每秒上下文切换数。

CPU(以百分比表示):

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

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

id: 空闲时间(包括IO等待时间)

wa: 等待IO时间

例子2:显示活跃和非活跃内存

使用-a选项显示活跃和非活跃内存时,所显示的内容除增加inact和active外,其他显示内容与例子1相同。

字段说明:

Memory(内存):

inact: 非活跃内存大小(当使用-a选项时显示)

active: 活跃的内存大小(当使用-a选项时显示)


[root@jiratest ~]# man vmstat
VMSTAT(8)                Linux Administrator鈥檚 Manual                VMSTAT(8)


NAME
       vmstat - Report virtual memory statistics


SYNOPSIS
       vmstat [-a] [-n] [-t] [-S unit] [delay [ count]]
       vmstat [-s] [-n] [-S unit]
       vmstat [-m] [-n] [delay [ count]]
       vmstat [-d] [-n] [delay [ count]]
       vmstat [-p disk partition] [-n] [delay [ count]]
       vmstat [-f]
       vmstat [-V]


DESCRIPTION
       vmstat  reports  information about processes, memory, paging, block IO,
       traps, and cpu activity.


       The first report produced gives averages since the last reboot.   Addi-
       tional  reports  give information on a sampling period of length delay.
       The process and memory reports are instantaneous in either case.


   Options
       The -a switch displays active/inactive memory, given a 2.5.41 kernel or
       better.


       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.


       The -t switch adds timestamp to the output.


       The -m switch displays slabinfo.


       The -n switch causes the header to be displayed only once  rather  than
       periodically.


       The  -s  switch  displays  a table of various event counters and memory
       statistics. This display does not repeat.


       delay is the delay between updates in seconds.  If no delay  is  speci-
       fied, only one report is printed with the average values since boot.


       count  is the number of updates.  If no count is specified and delay is
       defined, count defaults to infinity.


       The -d reports disk statistics (2.5.70 or above required)


       The -w enlarges field width for big memory sizes


       The -p followed by some partition name for detailed statistics  (2.5.70
       or above required)


       The  -S  followed  by  k  or K or m or M switches outputs between 1000,
       1024, 1000000, or 1048576 bytes


       The -V switch results in displaying version information.


FIELD DESCRIPTION FOR VM MODE
   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.


FIELD DESCRIPTION FOR DISK MODE
   Reads
       total: Total reads completed successfully
       merged: grouped reads (resulting in one I/O)
       sectors: Sectors read successfully
       ms: milliseconds spent reading


   Writes
       total: Total writes completed successfully
       merged: grouped writes (resulting in one I/O)
       sectors: Sectors written successfully
       ms: milliseconds spent writing


   IO
       cur: I/O in progress
       s: seconds spent for I/O


FIELD DESCRIPTION FOR DISK PARTITION MODE
       reads: Total number of reads issued to this partition
       read sectors: Total read sectors for partition
       writes : Total number of writes issued to this partition
       requested writes: Total number of write requests made for partition


FIELD DESCRIPTION FOR SLAB MODE
       cache: Cache name
       num: Number of currently active objects
       total: Total number of available objects
       size: Size of each object
       pages: Number of pages with at least one active object
       totpages: Total number of allocated pages
       pslab: Number of pages per slab


NOTES
       vmstat does not require special permissions.


       These reports are intended to help identify system bottlenecks.   Linux
       vmstat does not count itself as a running process.


       All  linux  blocks  are  currently  1024  bytes. Old kernels may report
       blocks as 512 bytes, 2048 bytes, or 4096 bytes.






       Since procps 3.1.9, vmstat lets you choose units (k, K, m,  M)  default
       is K (1024 bytes) in the default mode


       vmstat uses slabinfo 1.1    FIXME


FILES
       /proc/meminfo
       /proc/stat
       /proc/*/stat


SEE ALSO
       iostat(1), sar(1), mpstat(1), ps(1), top(1), free(1)


BUGS
       Does not tabulate the block io per device or count the number of system
       calls.


AUTHORS
       Written by Henry Ware <al172@yfn.ysu.edu>.
       Fabian Fr茅d茅rick <ffrederick@users.sourceforge.net> (diskstat, slab, paitions...)



0 0
原创粉丝点击