使用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 bash11:37:19 11509 0.00 0.00 0.00 0.00 3 dd
以上輸出,除最開頭一行顯示核心版本、主機名、日期和cpu架構外,主要列含義如下:
· 11:37:19: pidstat擷取資訊時間點
· PID: 程序pid
· %usr: 程序在使用者态運作所占cpu時間比率
· %system: 程序在核心态運作所占cpu時間比率
· %CPU: 程序運作所占cpu時間比率
· CPU: 訓示程序在哪個核運作
· Command: 拉起程序對應的指令
指定采樣周期和采樣次數
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 Command15:08:19 13084 133835.00 0.00 15720284 15716896 96.26 mmmm15:08:20 13084 35807.00 0.00 15863504 15849756 97.07 mmmm15: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 Command17:11:37 14579 124988.24 0.00 0.00 dd
17:11:37 PID kB_rd/s kB_wr/s kB_ccwr/s Command17: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 Command18:26:18 1 0.00 0.00 10380 640 0.00 init18: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的統計資訊。
轉載于:https://blog.51cto.com/ugali/2092226