Linux vmstat命令實戰詳解


vmstat命令是最常見的Linux/Unix監控工具,可以展現給定時間間隔的服務器的狀態值,包括服務器的CPU使用率,內存使用,虛擬內存交換情況,IO讀寫情況。這個命令是我查看Linux/Unix最喜愛的命令,一個是Linux/Unix都支持,二是相比top,我可以看到整個機器的CPU,內存,IO的使用情況,而不是單單看到各個進程的CPU使用率和內存使用率(使用場景不一樣)。

一般vmstat工具的使用是通過兩個數字參數來完成的,第一個參數是採樣的時間間隔數,單位是秒,第二個參數是採樣的次數,如:

root@ubuntu:~# vmstat  
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo      cs us sy id wa

2表示每個兩秒採集一次服務器狀態,1表示只採集一次。

實際上,在應用過程中,我們會在一段時間內一直監控,不想監控直接結束vmstat就行了,例如:

複製代碼

root@ubuntu:~# vmstat   
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo      cs us sy id wa

複製代碼

這表示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時間。

轉自:http://www.cnblogs.com/ggjucheng/archive/2012/01/05/2312625.html


vmstat


VMSTAT(8)                Linux Administrator’s Manual                VMSTAT(8)

NAME

       vmstat - Report virtual memory statistics

SYNOPSIS

       vmstat [-a] [-n] [delay [ count]]       vmstat [-f] [-s] [-m]       vmstat [-S unit]       vmstat [-d]       vmstat [-p disk partition]       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, shown as zero.

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 <[email protected]>.
       Fabian Frédérick <[email protected]> (diskstat, slab, partitions...)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章