vmstat的分析

Linux系統性能分析工具vmstat

vmstat是一個相當全面的性能分析工具,可以觀察到系統的進程狀態、內存使用、虛擬內存使用、磁盤的IO、中斷、上下問切換、CPU使用等性能信息。在系統性能分析工具中,還有功能強大的Sysstat工具包,free,top,ps等,本人使用最多的是這個。
     掌握Linux 的性能分析,理解vmstat 輸出內容的各項含義,那麼我認爲系統性能分析的能力就基本具備了。

以Linux2.4內核版本,輸出如下信息,例如Redhat9

引用   procs                       memory       swap           io     system       cpu
r   b   w   swpd   free   buff   cache   si   so     bi     bo   in     cs us sy id
0   0   0       0   21516 162240 253120     0     0     0     0 1364   544   2   1 97
0   0   0       0   21524 162236 253128     0     0     0     0 1774   720   1   2 97
0   0   0       0   21532 162236 253128     0     0     0     0 1718   713   0   2 98
0   0   0       0   21540 162204 253164     0     0     0   232 1904   867   1   2 97
0   0   0       0   21548 162204 253168     0     0     0     0 1668   810   1   1 98

而對Linux2.6內核版本,稍微有一點點不同,以RHEL4爲例,輸出以下信息


引用procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r   b   swpd   free   buff   cache   si   so     bi     bo   in     cs us sy id wa
0   0     208 508684   95980 770080     0     0     0     0 2252   1064 27   3 70   0
2   0     208 506956   95980 770080     0     0     0     32 2413   1081 11   2 88   0
2   0     208 494348   95980 770080     0     0     0     0 3198   1651 28   4 68   0
0   0     208 493708   95980 770080     0     0     0     0 2569   1072 21   3 77   0
1   0     208 508676   95992 770068     0     0     0   520 2943   1275 11   2 85   1
1   0     208 505988   95996 770324     0     0   160     0 3025   1422 13   3 83   0

相信現在大家都用linux2.6內核了,所以本文以linux2.6內核版本爲例來加以說明。


vmstat的用法如下:
引用usage: vmstat [-V] [-n] [delay [count]]
               -V prints version.           
               -n causes the headers not to be reprinted regularly.
               -a print inactive/active page stats.
               -d prints disk statistics
               -D prints disk table
               -p prints disk partition statistics
               -s prints vm table
               -m prints slabinfo
               -S unit size
               delay is the delay between updates in seconds.
               unit size k:1000 K:1024 m:1000000 M:1048576 (default is K)
               count is the number of updates.-V表示打印出版本信息;
-n表示在週期性循環輸出時,輸出的頭部信息僅顯示一次;
delay是兩次輸出之間的延遲時間;如1表示每隔1s輸出一次信息。
count是指按照這個時間間隔統計的次數。如4則表示輸出總共統計4次,輸出結束。
對於vmstat輸出各字段的含義,可以man一下

引用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.
英文對於某些同志理解有困難,用中文比較容易讓人理解,下面逐個解釋其含義


r 運行的和等待(CPU時間片)運行的進程數,這個值也可以判斷是否需要增加CPU(長期大於1)

b 處於不可中斷狀態的進程數,常見的情況是由IO引起的
w 可以進入運行隊列但被替換的進程

memoy
swap 現時可用的交換內存(k表示)
free 空閒的內存(k表示)

pages
re 回收的頁面
mf 非嚴重錯誤的頁面
pi 進入頁面數(k表示)
po 出頁面數(k表示)
fr 空餘的頁面數(k表示)
de 提前讀入的頁面中的未命中數
sr 通過時鐘算法掃描的頁面
disk 顯示每秒的磁盤操作。
fault 顯示每秒的中斷數
in設備中斷
sy系統中斷
cycpu交換
cpu 表示cpu的使用狀態
cs用戶進程使用的時間
sy系統進程使用的時間
idcpu空閒的時間


案例分析
假設輸出的信息中 

r經常大於 3-4 ,且id經常少於50,表cpu的負荷很重。
pi,po 長期不等於0,表示內存不足。
disk 經常不等於0, 且在 b中的隊列大於2-3, 表示 io性能不好。
Procs r: 運行的進程比較多,系統很繁忙
Io bo: 磁盤寫的數據量稍大
Cpu us: 持續大於50-60,服務高峯期可以接受
Cpu wa: 稍微有些高
Cpu id:持續小於50,服務高峯期可以接受

   有些參數具體怎麼理解,還得依靠自己去掌握,我認爲掌握一些常用的參數含義就行了
                                       --EOF--
 


中斷和context switch都是內核的一種機制,這些數字過高表明內核把大量CPU時間都浪費在這些個機制上,至於爲什麼會這樣,還是應該從你的應用特點下手,我個人認爲這種現象應該是你程序的進程或線程數量過多導致。
所以如果是你的應用確實需要如此多的進程or線程併發運行的話,那說明你硬件不夠滿足你的應用了,如果這些進程or線程是濫用的話,說明你的應用設計有問題。

 

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