proc文件系統(tǒng)/proc文件系統(tǒng)是一個偽文件系統(tǒng),它只存在內(nèi)存當中,而不占用外存空間。它以文件系統(tǒng)的方式為內(nèi)核與進程提供通信的接口。用戶和應用程序可以通過/proc得到系統(tǒng)的信息,并可以改變內(nèi)核的某些參數(shù)。由于系統(tǒng)的信息,如進程,是動態(tài)改變的,所以用戶或應用程序讀取/proc目錄中的文件時,proc文件系統(tǒng)是動態(tài)從系統(tǒng)內(nèi)核讀出所需信息并提交的。 /proc目錄中有一些以數(shù)字命名的目錄,它們是進程目錄。系統(tǒng)中當前運行的每一個進程在/proc下都對應一個以進程號為目錄名的目錄/proc/pid,它們是讀取進程信息的接口。此外,在Linux 2.6.0-test6以上的版本中/proc/pid目錄中有一個task目錄,/proc/pid/task目錄中也有一些以該進程所擁有的線程的線程號命名的目錄/proc/pid/task/tid,它們是讀取線程信息的接口。 /proc/cpuinfo文件該文件中存放了有關 cpu的相關信息(型號,緩存大小等)。 [zhengangen@buick ~]$ cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 15 model : 4 model name : Intel(R) Xeon(TM) CPU 3.00GHz stepping : 10 cpu MHz : 3001.177 cache size : 2048 KB physical id : 0 siblings : 2 core id : 0 cpu cores : 1 fdiv_bug : no hlt_bug : no f00f_bug : no coma_bug : no fpu : yes fpu_exception : yes cpuid level : 5 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe lm pni monitor ds_cpl cid xtpr bogomips : 6004.52
說明:以下只解釋對我們計算Cpu使用率有用的相關參數(shù)。 參數(shù) 解釋 processor (0) cpu的一個物理標識
結論1:可以通過該文件根據(jù)processor出現(xiàn)的次數(shù)統(tǒng)計cpu的邏輯個數(shù)(包括多核、超線程)。 /proc/stat文件該文件包含了所有CPU活動的信息,該文件中的所有值都是從系統(tǒng)啟動開始累計到當前時刻。不同內(nèi)核版本中該文件的格式可能不大一致,以下通過實例來說明數(shù)據(jù)該文件中各字段的含義。 實例數(shù)據(jù):2.6.24-24版本上的
fjzag@fjzag-desktop:~$ cat /proc/stat cpu 38082 627 27594 893908 12256 581 895 0 0 cpu0 22880 472 16855 430287 10617 576 661 0 0 cpu1 15202 154 10739 463620 1639 4 234 0 0 intr 120053 222 2686 0 1 1 0 5 0 3 0 0 0 47302 0 0 34194 29775 0 5019 845 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ctxt 1434984 btime 1252028243 processes 8113 procs_running 1 procs_blocked 0 第一行的數(shù)值表示的是CPU總的使用情況,所以我們只要用第一行的數(shù)字計算就可以了。下表解析第一行各數(shù)值的含義: 參數(shù) 解析(單位:jiffies) (jiffies是內(nèi)核中的一個全局變量,用來記錄自系統(tǒng)啟動一來產(chǎn)生的節(jié)拍數(shù),在linux中,一個節(jié)拍大致可理解為操作系統(tǒng)進程調(diào)度的最小時間片,不同linux內(nèi)核可能值有不同,通常在1ms到10ms之間) user (38082) 從系統(tǒng)啟動開始累計到當前時刻,處于用戶態(tài)的運行時間,不包含 nice值為負進程。 nice (627) 從系統(tǒng)啟動開始累計到當前時刻,nice值為負的進程所占用的CPU時間 system (27594) 從系統(tǒng)啟動開始累計到當前時刻,處于核心態(tài)的運行時間 idle (893908) 從系統(tǒng)啟動開始累計到當前時刻,除IO等待時間以外的其它等待時間iowait (12256) 從系統(tǒng)啟動開始累計到當前時刻,IO等待時間(since 2.5.41) irq (581) 從系統(tǒng)啟動開始累計到當前時刻,硬中斷時間(since 2.6.0-test4) softirq (895) 從系統(tǒng)啟動開始累計到當前時刻,軟中斷時間(since 2.6.0-test4)stealstolen(0) which is the time spent in other operating systems when running in a virtualized environment(since 2.6.11) guest(0) which is the time spent running a virtual CPU for guest operating systems under the control of the Linux kernel(since 2.6.24)
結論2:總的cpu時間totalCpuTime = user + nice + system + idle + iowait + irq + softirq + stealstolen + guest /proc/<pid>/stat文件該文件包含了某一進程所有的活動的信息,該文件中的所有值都是從系統(tǒng)啟動開始累計 到當前時刻。以下通過實例數(shù)據(jù)來說明該文件中各字段的含義。
[zhengangen@buick ~]# cat /proc/6873/stat 6873 (a.out) R 6723 6873 6723 34819 6873 8388608 77 0 0 0 41958 31 0 0 25 0 3 0 5882654 1409024 56 4294967295 134512640 134513720 3215579040 0 2097798 0 0 0 0 0 0 0 17 0 0 0
說明:以下只解釋對我們計算Cpu使用率有用相關參數(shù) 參數(shù) 解釋 pid=6873 進程號 utime=1587 該任務在用戶態(tài)運行的時間,單位為jiffies stime=41958 該任務在核心態(tài)運行的時間,單位為jiffies cutime=0 所有已死線程在用戶態(tài)運行的時間,單位為jiffies cstime=0 所有已死在核心態(tài)運行的時間,單位為jiffies
結論3:進程的總Cpu時間processCpuTime = utime + stime + cutime + cstime,該值包括其所有線程的cpu時間。 /proc/<pid>/task/<tid>/stat文件該文件包含了某一進程所有的活動的信息,該文件中的所有值都是從系統(tǒng)啟動開始累計到當前時刻。該文件的內(nèi)容格式以及各字段的含義同/proc/<pid>/stat文件。 注意,該文件中的tid字段表示的不再是進程號,而是linux中的輕量級進程(lwp),即我們通常所說的線程。
結論4:線程Cpu時間threadCpuTime = utime + stime 系統(tǒng)中有關進程cpu使用率的常用命令ps 命令通過ps命令可以查看系統(tǒng)中相關進程的Cpu使用率的信息。以下在linux man文檔中對ps命令輸出中有關cpu使用率的解釋:
CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to exactly 100%.
%cpu cpu utilization of the process in "##.#" format. It is the CPU time used divided by the time the process has been running (cputime/realtime ratio), expressed as a percentage. It will not add up to 100% unless you are lucky.
結論5:ps命令算出來的cpu使用率相對于進程啟動時的平均值,隨著進程運行時間的增大,該值會趨向于平緩。 top命令通過top命令可以查看系統(tǒng)中相關進程的實時信息(cpu使用率等)。以下是man文檔中對top命令輸出中有關進程cpu使用率的解釋。
#C -- Last used CPU (SMP) A number representing the last used processor. In a true SMP environment this will likely change frequently since the kernel intentionally uses weak affinity. Also, the very act of running top may break this weak affinity and cause more processes to change CPUs more often (because of the extra demand for cpu time).
%CPU -- CPU usage The task’s share of the elapsed CPU time since the last screen update, expressed as a percent-age of total CPU time. In a true SMP environment, if Irix mode is Off, top will operate in Solaris mode where a task’s cpu usage will be divided by the total number of CPUs.
結論6:某一個線程在其運行期間其所使用的cpu可能會發(fā)生變化。 結論7:在多核的情況下top命令輸出的cpu使用率實質(zhì)是按cpu個數(shù)*100%計算的。 單核情況下Cpu使用率的計算基本思想通過讀取/proc/stat 、/proc/<pid>/stat、/proc/<pid>/task/<tid>/stat以及/proc/cpuinfo這幾個文件獲取總的Cpu時間、進程的Cpu時間、線程的Cpu時間以及Cpu的個數(shù)的信息,然后通過一定的算法進行計算(采樣兩個足夠短的時間間隔的Cpu快照與進程快照來計算進程的Cpu使用率)。
總的Cpu使用率計算計算方法:1、 采樣兩個足夠短的時間間隔的Cpu快照,分別記作t1,t2,其中t1、t2的結構均為: (user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元組; 2、 計算總的Cpu時間片totalCpuTime a) 把第一次的所有cpu使用情況求和,得到s1; b) 把第二次的所有cpu使用情況求和,得到s2; c) s2 - s1得到這個時間間隔內(nèi)的所有時間片,即totalCpuTime = j2 - j1 ; 3、計算空閑時間idle idle對應第四列的數(shù)據(jù),用第二次的第四列 - 第一次的第四列即可 idle=第二次的第四列 - 第一次的第四列 6、計算cpu使用率 pcpu =100* (total-idle)/total 某一進程Cpu使用率的計算計算方法:1. 采樣兩個足夠短的時間間隔的cpu快照與進程快照, a) 每一個cpu快照均為(user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元組; b) 每一個進程快照均為 (utime、stime、cutime、cstime)的4元組; 2. 分別根據(jù)結論2、結論3計算出兩個時刻的總的cpu時間與進程的cpu時間,分別記作:totalCpuTime1、totalCpuTime2、processCpuTime1、processCpuTime2 3. 計算該進程的cpu使用率pcpu = 100*( processCpuTime2 – processCpuTime1) / (totalCpuTime2 – totalCpuTime1) (按100%計算,如果是多核情況下還需乘以cpu的個數(shù)); 實驗數(shù)據(jù)
某一線程Cpu使用率的計算計算方法:1. 采樣兩個足夠短的時間隔的cpu快照與線程快照, a) 每一個cpu快照均為(user、nice、system、idle、iowait、irq、softirq、stealstealon、guest)的9元組; b) 每一個線程快照均為 (utime、stime)的2元組; 2. 分別根據(jù)結論2、結論4計算出兩個時刻的總的cpu時間與線程的cpu時間,分別記作:totalCpuTime1、totalCpuTime2、threadCpuTime1、threadCpuTime2 3. 計算該線程的cpu使用率pcpu = 100*( threadCpuTime2 – threadCpuTime1) / (totalCpuTime2 – totalCpuTime1) (按100%計算,如果是多核情況下還需乘以cpu的個數(shù)); 實驗數(shù)據(jù)
多核情況下cpu使用率的計算以下通過實驗數(shù)據(jù)來說明多核情況下某一進程cpu使用率是按cpu個數(shù)*100%計算的. 實驗一描述:在雙核的情況下作的一組實驗,第一組數(shù)據(jù)是通過ps -eLo pid,lwp,pcpu | grep 9140命令查看進程號為9140的進程中各線程的詳細信息。第二組數(shù)據(jù)是通過 ps命令查看進程號為9140進程的cpu使用率。 數(shù)據(jù)一:pid lwp %cpu 9140 9140 0.0 以上除了紅色標注出來的兩個線程以外,其他的線程都是后臺線程。 數(shù)據(jù)二:pid %cpu 9140 193 實驗二描述:在單核的情況下作的一組實驗,第一組數(shù)據(jù)是通過ps -eLo pid,lwp,pcpu | grep 6137命令查看進程號為6137的進程中各線程的詳細信息。第二組數(shù)據(jù)是通過 ps命令查看進程號為6137進程的cpu使用率。 數(shù)據(jù)一:pid lwp %cpu 6137 6137 0.0 6137 6138 0.1 6137 6143 0.0 6137 6144 0.0 6137 6145 0.0 6137 6146 0.0 6137 6147 0.0 6137 6148 0.0 6137 6149 0.0 6137 6150 46.9 空循環(huán)線程 6137 6151 46.9 空循環(huán)線程 以上除了紅色標注出來的兩個線程以外,其他的線程都是后臺線程。 數(shù)據(jù)二pid %cpu 6137 92.9 主要問題:1. 不同內(nèi)核版本/proc/stat文件格式不大一致。/proc/stat文件中第一行為總的cpu使用情況。 各個版本都有的4個字段: user、nice、system、idle 2.5.41版本新增字段:iowait 2.6.0-test4新增字段:irq、softirq 2.6.11新增字段:stealstolen : which is the time spent in other operating systems when running in a virtualized environment 2.6.24新增字段:guest: which is the time spent running a virtual CPU for guest operating systems under the control of the Linux kernel 2./proc/pid/task目錄是Linux 2.6.0-test6之后才有的功能。
3.關于出現(xiàn)cpu使用率為負的情況,目前想到的解決方案是如果出現(xiàn)負值,連續(xù)采樣計算cpu使用率直到為非負。 4. 有些線程生命周期較短,可能在我們采樣期間就已經(jīng)死掉了. |
|