1. 程式人生 > >Linux 執行程序實時監控pidstat命令詳解

Linux 執行程序實時監控pidstat命令詳解

簡介

 pidstat主要用於監控全部或指定程序佔用系統資源的情況,如CPU,記憶體、裝置IO、任務切換、執行緒等。pidstat首次執行時顯示自系統啟動開始的各項統計資訊,之後執行pidstat將顯示自上次執行該命令以後的統計資訊。使用者可以通過指定統計的次數和時間來獲得所需的統計資訊。

例項講解

預設引數

執行pidstat,將輸出系統啟動後所有活動程序的cpu統計資訊:

複製程式碼
linux:~ # pidstat
Linux 2.6.32.12-0.7-default (linux)             06/18/12        _x86_64_

11:37:19          PID    %usr %system  %guest    %CPU   CPU  Command
……
11:37:19 11452 0.00 0.00 0.00 0.00 2 bash 11:37:19 11509 0.00 0.00 0.00 0.00 3 dd
複製程式碼

指定取樣週期和取樣次數

pidstat命令指定取樣週期和取樣次數,命令形式為”pidstat [option] interval [count]”,以下pidstat輸出以2秒為取樣週期,輸出10次cpu使用統計資訊:

pidstat 2 10

cpu使用情況統計(-u)

使用-u選項,pidstat將顯示各活動程序的cpu使用統計,執行”pidstat -u”與單獨執行”pidstat”的效果一樣。

記憶體使用情況統計(-r)

使用-r選項,pidstat將顯示各活動程序的記憶體使用統計:

複製程式碼
linux:~ # pidstat -r -p 13084 1
Linux 2.6.32.12-0.7-default (linux)             06/18/12        _x86_64_

15:08:18          PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
15:08:19        13084 133835.00      0.00 15720284 15716896  96.26  mmmm
15:08:20        13084
35807.00 0.00 15863504 15849756 97.07 mmmm 15:08:21 13084 19273.87 0.00 15949040 15792944 96.72 mmmm
複製程式碼

以上各列輸出的含義如下:

minflt/s: 每秒次缺頁錯誤次數(minor page faults),次缺頁錯誤次數意即虛擬記憶體地址對映成實體記憶體地址產生的page fault次數
majflt/s: 每秒主缺頁錯誤次數(major page faults),當虛擬記憶體地址對映成實體記憶體地址時,相應的page在swap中,這樣的page fault為major page fault,一般在記憶體使用緊張時產生
VSZ:      該程序使用的虛擬記憶體(以kB為單位)
RSS:      該程序使用的實體記憶體(以kB為單位)
%MEM:     該程序使用記憶體的百分比
Command:  拉起程序對應的命令

IO情況統計(-d)

使用-d選項,我們可以檢視程序IO的統計資訊:

複製程式碼
linux:~ # pidstat -d 1 2
Linux 2.6.32.12-0.7-default (linux)             06/18/12        _x86_64_

17:11:36          PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
17:11:37        14579 124988.24      0.00      0.00  dd

17:11:37          PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
17:11:38        14579 105441.58      0.00      0.00  dd
複製程式碼

輸出資訊含義

kB_rd/s: 每秒程序從磁碟讀取的資料量(以kB為單位)
kB_wr/s: 每秒程序向磁碟寫的資料量(以kB為單位)
Command: 拉起程序對應的命令

針對特定程序統計(-p)

使用-p選項,我們可以檢視特定程序的系統資源使用情況:

複製程式碼
linux:~ # pidstat -r -p 1 1
Linux 2.6.32.12-0.7-default (linux)             06/18/12        _x86_64_

18:26:17          PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
18:26:18            1      0.00      0.00   10380    640   0.00  init
18:26:19            1      0.00      0.00   10380    640   0.00  init
……
複製程式碼

pidstat常用命令

使用pidstat進行問題定位時,以下命令常被用到:

pidstat -u 1

pidstat -r 1

pidstat -d 1

以上命令以1秒為資訊採集週期,分別獲取cpu、記憶體和磁碟IO的統計資訊。