Zhang Jiuan’ Notes

vmstat工具

vmstat 
vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写, 是实时系统监控工具。该命令通过使用knlist子程序和/dev/kmen伪设备驱动器访问这些数据,输出信息直接打印在屏幕。vmstat反馈的与CPU相关的信息包括:
(1)多少任务在运行
(2)CPU使用的情况
(3)CPU收到多少中断
(4)发生多少上下文切换

下面只介绍 Vmstat与CPU相关的参数
vmstat的语法如下:
  vmstat [delay [count]]
参数的含义如下:
参数 解释
delay 相邻的两次采样的间隔时间
count 采样的次数,count只能和delay一起使用
当没有参数时,vmstat则显示系统启动以后所有信息的平均值。有delay时,第一行的信息自系统启动以来的平均信息。从第二行开始,输出为前一个delay时间段的平均信息。当系统有多个CPU时,输出为所有CPU的平均值。
与CPU有关的输出的含义 (采用进一法)
参数 解释 从/proc/stat获得数据
任务的信息
r 在internal时间段里,运行队列里等待CPU的任务(任务)的个数,即不包含vmstat进程 procs_running-1
b 在internal时间段里,被资源阻塞的任务数(I/0,页面调度,等等.) ,通常情况下是接近0的 procs_blocked
CPU信息 所有值取整(四舍五入)
user 在internal时间段里,用户态的CPU时间(%) ,不包含 nice值为负进程 usr/total*100
nice 在internal时间段里,nice值为负进程的CPU时间(%) nice/total*100
sys 在internal时间段里,核心时间(%) (system + irq + softirq)/total*100
iowait 在internal时间段里,硬盘IO等待时间(%) iowait/total*100
idle 在internal时间段里, CPU除去等待磁盘IO操作外的因为任何原因而空闲的时间闲置时间 (%)idle/total*100
系统信息
intr/interval in 在internal时间段里,每秒发生中断的次数
cs 在internal时间段里,每秒上下文切换的次数,即每秒内核任务交换的次数 ctxt/interval
CODE:
total_cur=user+system+nice+idle+iowait+irq+softirq
total_pre=pre_user+ pre_system+ pre_nice+ pre_idle+ pre_iowait+ pre_irq+ pre_softirq
total=total_cur-total_pre
范例1:average mode (粗略信息)
当vmstat不带参数时,对应的输出值是从系统启动以来的平均值,而r和b则对应的是完成这一命令时,系统的值。从下面例子,可以看出系统基本出去闲置状态(idle)。自启动以来,CPU在用户态消耗时间为5%,在核心态消耗为本1%,剩下的为闲置时间。需要指出的是:这里的用户态时间包括nice值为负的进程的时间。
CODE:
[root@server ~]# cat 1.img &
[2] 6960
[1]   Done                    cat 1.img
[root@server ~]# vmstat
procs ———–memory———- —swap– —–io—- –system– —–cpu——
r b   swpd   free   buff cache   si   so    bi    bo   in   cs us sy id wa st
3 0    100   5588    992 374612    0    0    48    41 513   83 0 1 99 0 0
[root@server ~]#
范例2:average mode (详细信息)
命令格式:
vmstat –s
这里只讨论与CPU相关信息。“CPU ticks”表示自系统启动CPU运行时间,这里以tick为时间单位。用tick来西安市us,sy id 和wa的时间;forks指自从系统启动以来,所创建的新任务的个数。这些信息从/proc/stat 的第一行和”processes”行获得。
CODE:
[root@localhost ~]# vmstat -s
255280 total memory
244216 used memory
206624 active memory
21208 inactive memory
11064 free memory
628 buffer memory
91396 swap cache
255992 total swap
24 used swap
255968 free swap
973400 non-nice user cpu ticks
477 nice user cpu ticks
206168 system cpu ticks
43567714 idle cpu ticks
373234 IO-wait cpu ticks
62732 IRQ cpu ticks
1972 softirq cpu ticks
22366502 pages paged in
88756936 pages paged out
0 pages swapped in
0 pages swapped out
135634319 interrupts
137288441 CPU context switches
1134440368 boot time
208990 forks
[root@localhost ~]#

结果解释
参数 描述 /proc/stat
non-nice user cpu ticks 自系统启动以来,CPU在用户态下运行非nice进程的时间,单位为jiffies user
nice user cpu ticks        自系统启动以来,CPU在用户态下运行nice进程的时间,单位为jiffies nice
system cpu ticks           自系统启动以来,CPU处于系统状态的时间,单位为jiffies sys
idle cpu ticks                 自系统启动以来,CPU处于闲置状态的时间,单位为jiffies idle
IO-wait cpu ticks            自系统启动以来,CPU处理IO中断的时间,单位为jiffies iowait
IRQ cpu ticks                 自系统启动以来,CPU处理硬中断的时间,单位为jiffies irq
softirg cpu ticks            自系统启动以来,CPU处理软中断的时间,单位为jiffies Softirq
interrupts                       自系统启动以来,发生的所有的中断的次数目 Intr
CPU context switches     自系统启动以来,发生的上下文交换的次数 Ctxt
boot time                        自系统启动以来到现在运行的时间,单位为秒。 btime
forks                               自系统启动以来所创建的任务的个数目。 Process

范例3:定期采样(delay [count])
      定期采样数据是指每隔delay时间,采样一次。当count 为0时,vmstat 将不停地定期报告信息;否则当报告count次后,vmstat 命令停止运行。
     第一行的信息如同范例1,是自系统启动以来的平均信息。从第二行开始,每行的意思是:r和b采样那一时刻系统运行队列和等待队列的情况;而usystem参数(in,cs)以及CPU参数(us,sy,id,wa)对应的输出值是系统在前一个delay的情况。
      从下面例子可以看出上下文交换的次数小于中断的发生次数。当系统大部分时间是空闲并且中断大部分是时间中断时,这种现象极可能发生。当时间中断发生时, 因为调度器没有什么任务可调度,所以很少发生上下文切换。
CODE:
[root@localhost ~]# vmstat 2 4
procs ———–memory———- —swap– —–io—- –system– —-cpu—-
r b    swpd free buff cache        si so       bi bo    in cs    us sy id wa
1 0     24 11032 652 91396        0 0         49 6     15 19    2 1 96 1
0 0     24 11032 652 91396        0 0         0 0     377 464   1 0 99 0
0 0     24 11024 652 91396        0 0         0 0     387 476   1 0 100 0
0 0     24 11024 652 91396        0 0         0 0     323 377   0 0 100 0
[root@localhost ~]#

最后贴个man文档:

VMSTAT(8)                Linux Administrator?. Manual                VMSTAT(8)

 

NAME
       vmstat - Report virtual memory statistics

SYNOPSIS
       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]

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 -m 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 -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)

If you enjoyed this post, make sure you subscribe to my RSS feed!

No Comments, Comment or Ping

Reply to “vmstat工具”

You must be logged in to post a comment.

返回顶部