歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> Linux監控工具介紹系列——vmstat,linuxvmstat

Linux監控工具介紹系列——vmstat,linuxvmstat

日期:2017/3/3 17:59:42   编辑:學習Linux

Linux監控工具介紹系列——vmstat,linuxvmstat

Linux監控工具介紹系列——vmstat,linuxvmstat


說來慚愧,玩Linux這麼久了,居然沒有玩轉vmstat這個命令,對很多指標的具體意義都有點模糊不清,花了點時間好好學習、整理一下這個命令的相關資料。因為這個命令確實比較重要,而且頻繁用到。

命令簡介:

vmstat是Virtual Meomory Statistics(虛擬內存統計)的縮寫,可對操作系統的虛擬內存、進程、IO讀寫、CPU活動等進行監視。它是對系統的整體情況進行統計,不足之處是無法對某個進程進行深入分析。

指令所在路徑:/usr/bin/vmstat

注意事項: 本文實驗、總結環境為RHEL 5.7,vmstat在不同版本的Unix與Linux有所差別,使用時,最好先參考對應操作系統的vmstat版本。

命令手冊中的介紹如下:

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.

命令語法:

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]

命令參數:

此命令參數是Red Hat Enterprise Linux Server release 5.7下vmstat命令參數,不同版本Linux的cp命令參數有可能不同。不過默認應該都是[ delay [ count ] ] ,delay是間隔,count顯示多少次信息。可以和上面的某些參數結合使用。

參數

英文描述

描敘

delay

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

count

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

-a

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

開啟顯示active/inactive memory。

-f

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.

顯示此系統啟動以來的forks的總數,包括fork、vfork和clone system calls

-m

The -m displays slabinfo

顯示slabinfo信息

-n

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

只顯示頭信息,不周期性顯示.也就是說開啟這個參數,只顯示頭部信息一次。

-s

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

顯示各種事件計數器表和內存統計信息,這顯示不重復。

-d

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

顯示磁盤統計數據(內核要求2.5.70 或以上)

-w

The -w enlarges field width for big memory sizes

可以擴大字段長度,當內存較大時,默認長度不夠完全展示內存。

-p

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

顯示磁盤分區數據(disk partition statistics )

-S

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

參數S控制輸出性能指標的單位,k(1000) K(1024) 或 M(1048576) 默認單位為K(1024 bytes)

-V

The -V switch results in displaying version information.

查看vmstat命令的版本

輸出字段意義:

Procs

r: The number of processes waiting for run time.

等待運行的進程數。如果等待運行的進程數越多,意味著CPU非常繁忙。另外,如果該參數長期大於和等於邏輯cpu個數,則CPU資源可能存在較大的瓶頸。

b: The number of processes in uninterruptible sleep.

處在非中斷睡眠狀態的進程數。意味著進程被阻塞。主要是指被資源阻塞的進程對列數(比如IO資源、頁面調度等),當這個值較大時,需要根據應用程序來進行分析,比如數據庫產品,中間件應用等。

Memory

swpd: the amount of virtual memory used.

已使用的虛擬內存大小。如果虛擬內存使用較多,可能系統的物理內存比較吃緊,需要采取合適的方式來減少物理內存的使用。swapd不為0,並不意味物理內存吃緊,如果swapd沒變化,si、so的值長期為0,這也是沒有問題的

free: the amount of idle memory.

空閒的物理內存的大小

buff: the amount of memory used as buffers.

用來做buffer(緩存,主要用於塊設備緩存)的內存數,單位:KB

cache: the amount of memory used as cache.

用來做cache(緩存,主要用於緩存文件)的內存,單位:KB

inact: the amount of inactive memory. (-a option)

inactive memory的總量

active: the amount of active memory. (-a option)

active memroy的總量。

Swap

si: Amount of memory swapped in from disk (/s).

從磁盤交換到內存的交換頁數量,單位:KB/秒。

so: Amount of memory swapped to disk (/s).

從內存交換到磁盤的交換頁數量,單位:KB/秒

內存夠用的時候,這2個值都是0,如果這2個值長期大於0時,系統性能會受到影響,磁盤IO和CPU資源都會被消耗。

當看到空閒內存(free)很少的或接近於0時,就認為內存不夠用了,這個是不正確的。不能光看這一點,還要結合si和so,

如果free很少,但是si和so也很少(大多時候是0),那麼不用擔心,系統性能這時不會受到影響的。

當內存的需求大於RAM的數量,服務器啟動了虛擬內存機制,通過虛擬內存,可以將RAM段移到SWAP DISK的特殊磁盤段上,

這樣會 出現虛擬內存的頁導出和頁導入現象,頁導出並不能說明RAM瓶頸,虛擬內存系統經常會對內存段進行頁導出,

但頁導入操作就表明了服務器需要更多的內存了, 頁導入需要從SWAP DISK上將內存段復制回RAM,導致服務器速度變慢。

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資源,這個數值應該越小越好。

CPU

These are percentages of total CPU time.

us: Time spent running non-kernel code. (user time, including nice time)

用戶CPU時間(非內核進程占用時間)(單位為百分比)。 us的值比較高時,說明用戶進程消耗的CPU時間多

sy: Time spent running kernel code. (system time)

系統使用的CPU時間(單位為百分比)。sy的值高時,說明系統內核消耗的CPU資源多,這並不是良性表現,我們應該檢查原因。

id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.

空閒的CPU的時間(百分比),在Linux 2.5.41之前,這部分包含IO等待時間。

wa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero.

等待IO的CPU時間,在Linux 2.5.41之前,這個值為0 .這個指標意味著CPU在等待硬盤讀寫操作的時間,用百分比表示。wait越大則機器io性能就越差。說明IO等待比較嚴重,這可能由於磁盤大量作隨機訪問造成,也有可能磁盤出現瓶頸(塊操作)。

st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.

使用示例:

1: 查看vmstat命令的幫助信息

[root@DB-Server ~]# man vmstat

2: 顯示活動(active)與非活動(inactive)的內存

[root@DB-Server ~]# vmstat -a 2 10
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 0  0 242752  56264 1294680 2365840    0    0     1    18    2    2  0  2 97  0  0
 1  0 242752  56504 1294676 2365736    0    0     0     0 1010  511  0  1 100  0  0
 0  0 242752  55844 1294716 2366616    0    0     0    16 1011  768  1  5 94  0  0
 0  0 242752  56760 1294716 2365888    0    0     0   190 1015  554  0  1 99  0  0
 0  0 242752  55472 1294744 2366636    0    0     0     0 1007  751  1  6 94  0  0
 0  0 242752  56636 1294748 2365904    0    0     0    16 1009  554  0  1 99  0  0
 0  0 242752  55844 1294772 2366656    0    0     0   178 1020  746  1  6 93  0  0
 0  0 242752  56884 1294768 2365940    0    0     0     0 1007  543  0  1 99  0  0
 1  0 242752  55208 1294816 2367220    0    0     0   206 1021  726  0  4 95  0  0
 0  0 242752  56760 1294796 2365960    0    0     0    16 1009  606  0  2 98  0  0

3:不加任何參數,vmstat命令只輸出一條記錄,這個數據是自系統上次重啟之後到現在的平均數值。

[root@DB-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
 0  0 242752  32496 112680 2724840    0    0     1    18    3    2  0  2 97  0  0

4:顯示各種事件計數器表和內存統計信息,這顯示不重復。

[oracle@mylnx01 bdump]$ vmstat -s
     33011144  total memory
     32799072  used memory
     24606736  active memory
      6175700  inactive memory
       212072  free memory
        52288  buffer memory
     30158708  swap cache
     12582904  total swap
       610348  used swap
     11972556  free swap
     44159969 non-nice user cpu ticks
         8172 nice user cpu ticks
      6077972 system cpu ticks
    389217442 idle cpu ticks
     40807984 IO-wait cpu ticks
       123964 IRQ cpu ticks
       383333 softirq cpu ticks
            0 stolen cpu ticks
  10331447387 pages paged in
   2287459081 pages paged out
      1524480 pages swapped in
      1433512 pages swapped out
   2358479992 interrupts
   1876082783 CPU context switches
   1481100317 boot time
     15573677 forks

5:可以擴大字段長度,當內存較大時,默認長度不夠完全展示內存時,會導致字段值便宜,導致查看不便

[root@DB-Server ~]# vmstat -w 2 5
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     243852      73556     110908    2678492    0    0     1    18    3    3   0  2  97  0  0
 0  0     243852      72252     110916    2678484    0    0     0   172 1016  701   0  4  95  0  0
 0  0     243852      73556     110916    2678544    0    0     0     0 1005  636   0  2  98  0  0
 0  0     243852      72004     110916    2678540    0    0     0    16 1005  694   0  5  95  0  0
 0  0     243852      73432     110924    2678580    0    0     0   192 1015  629   0  2  98  0  0
[root@DB-Server ~]# vmstat  2 5
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 243852  79284 110928 2678640    0    0     1    18    3    3  0  2 97  0  0
 0  0 243852  78988 110928 2678648    0    0     0     0 1006  753  0  5 95  0  0
 0  0 243852  80400 110936 2678696    0    0     0   194 1015  565  0  1 99  0  0
 0  0 243852  78352 110936 2678748    0    0     0    16 1008  680  0  5 95  0  0
 0  0 243852  79532 110936 2678748    0    0     0     0 1007  669  0  2 98  0  0
[root@DB-Server ~]# 

如下所示,由於有些字段值較大,導致出現下面偏移,不便查看。

6:顯示磁盤分區數據(disk partition statistics )

[oracle@mylnx01 ~]$ vmstat -p sdc5 2 10
sdc5          reads   read sectors  writes    requested writes
            54270570 7234336956    8939045  276196850
            54270570 7234336956    8939045  276196850
            54270570 7234336956    8939050  276196978
            54270570 7234336956    8939053  276197074
            54270574 7234337260    8939053  276197074
            54270577 7234337292    8939066  276197346
            54270622 7234339700    8939066  276197346
            54270622 7234339700    8939069  276197442
            54270859 7234342828    8939078  276197634
            54271074 7234345452    8939080  276197666

參考資料:

http://www.linuxcommand.org/man_pages/vmstat8.html

http://www.ha97.com/4512.html

http://xxxxxx/Linuxjc/1181646.html TechArticle

Copyright © Linux教程網 All Rights Reserved