每天一個命令(17)vmstat - Report virtual memory statistics

vmstat - Report virtual memory statistics 虛擬內存統計報告

man vmstat 查看命令文檔

使用情況
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.  Additional 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 specified, 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 pro-
       cess.

       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

vmstat options參數說明

-a:顯示活躍和非活躍內存

-f:顯示從系統啓動至今的fork數量 。

-m:顯示slabinfo

-n:只在開始時顯示一次各字段名稱。

-s:顯示內存相關統計信息及多種系統活動數量。

delay:刷新時間間隔。如果不指定,只顯示一條結果。

count:刷新次數。如果不指定刷新次數,但指定了刷新時間間隔,這時刷新次數爲無窮。

-d:顯示磁盤相關統計信息。

-p:顯示指定磁盤分區統計信息

-S:使用指定單位顯示。參數有 k 、K 、m 、M ,分別代表1000102410000001048576字節(byte)。默認單位爲K1024 bytes)
-V:顯示vmstat版本信息。

默認不帶參數

root@nginx02  ~
# vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 230688  75464  58496    0    0     2     1   20   26  0  0 100  0  0   

vmstat 多少秒 執行次數 如果沒有執行次數,一直執行

root@nginx02  ~
# 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 st
 0  0      0 230680  75464  58496    0    0     2     1   20   26  0  0 100  0  0   

root@nginx02  ~
# vmstat 2 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 230680  75464  58496    0    0     2     1   20   26  0  0 100  0  0   
 0  0      0 230664  75464  58496    0    0     0     0   15   10  0  1 99  0  0    
 0  0      0 230664  75464  58496    0    0     0     0   13    9  0  0 100  0  0   

root@nginx02  ~
# 

顯示內容中文字段說明

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時間

一般 r < 5 , b = 0
## 如 r 經常大於3或4,且id經常少於50,則表示cpu負載過大
## 如 si、so長期不等於0,表示內存不足
## disk 經常不等於0,且在b中的隊列大於2或3,表示io的性能不好
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章