proc文件系統(tǒng)/proc文件系統(tǒng)是一個(gè)偽文件系統(tǒng),它只存在內(nèi)存當(dāng)中,而不占用外存空間。它以文件系統(tǒng)的方式為內(nèi)核與進(jìn)程提供通信的接口。用戶和應(yīng)用程序可以通過/proc得到系統(tǒng)的信息,并可以改變內(nèi)核的某些參數(shù)。由于系統(tǒng)的信息,如進(jìn)程,是動(dòng)態(tài)改變的,所以用戶或應(yīng)用程序讀取/proc目錄中的文件時(shí),proc文件系統(tǒng)是動(dòng)態(tài)從系統(tǒng)內(nèi)核讀出所需信息并提交的。 /proc目錄中有一些以數(shù)字命名的目錄,它們是進(jìn)程目錄。系統(tǒng)中當(dāng)前運(yùn)行的每一個(gè)進(jìn)程在/proc下都對(duì)應(yīng)一個(gè)以進(jìn)程號(hào)為目錄名的目錄/proc/pid,它們是讀取進(jìn)程信息的接口。此外,在Linux 2.6.0-test6以上的版本中/proc/pid目錄中有一個(gè)task目錄,/proc/pid/task目錄中也有一些以該進(jìn)程所擁有的線程的線程號(hào)命名的目錄/proc/pid/task/tid,它們是讀取線程信息的接口。 /proc/cpuinfo文件該文件中存放了有關(guān) cpu的相關(guān)信息(型號(hào),緩存大小等)。 [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
說明:以下只解釋對(duì)我們計(jì)算Cpu使用率有用的相關(guān)參數(shù)。 參數(shù) 解釋 processor (0) cpu的一個(gè)物理標(biāo)識(shí)
結(jié)論1:可以通過該文件根據(jù)processor出現(xiàn)的次數(shù)統(tǒng)計(jì)cpu的邏輯個(gè)數(shù)(包括多核、超線程)。 /proc/stat文件該文件包含了所有CPU活動(dòng)的信息,該文件中的所有值都是從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻。不同內(nèi)核版本中該文件的格式可能不大一致,以下通過實(shí)例來說明數(shù)據(jù)該文件中各字段的含義。 實(shí)例數(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ù)字計(jì)算就可以了。下表解析第一行各數(shù)值的含義: 參數(shù) 解析(單位:jiffies) (jiffies是內(nèi)核中的一個(gè)全局變量,用來記錄自系統(tǒng)啟動(dòng)一來產(chǎn)生的節(jié)拍數(shù),在linux中,一個(gè)節(jié)拍大致可理解為操作系統(tǒng)進(jìn)程調(diào)度的最小時(shí)間片,不同linux內(nèi)核可能值有不同,通常在1ms到10ms之間) user (38082) 從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻,處于用戶態(tài)的運(yùn)行時(shí)間,不包含 nice值為負(fù)進(jìn)程。 nice (627) 從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻,nice值為負(fù)的進(jìn)程所占用的CPU時(shí)間 system (27594) 從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻,處于核心態(tài)的運(yùn)行時(shí)間 idle (893908) 從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻,除IO等待時(shí)間以外的其它等待時(shí)間iowait (12256) 從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻,IO等待時(shí)間(since 2.5.41) irq (581) 從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻,硬中斷時(shí)間(since 2.6.0-test4) softirq (895) 從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻,軟中斷時(shí)間(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)
結(jié)論2:總的cpu時(shí)間totalCpuTime = user + nice + system + idle + iowait + irq + softirq + stealstolen + guest /proc/<pid>/stat文件該文件包含了某一進(jìn)程所有的活動(dòng)的信息,該文件中的所有值都是從系統(tǒng)啟動(dòng)開始累計(jì) 到當(dāng)前時(shí)刻。以下通過實(shí)例數(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
說明:以下只解釋對(duì)我們計(jì)算Cpu使用率有用相關(guān)參數(shù) 參數(shù) 解釋 pid=6873 進(jìn)程號(hào) utime=1587 該任務(wù)在用戶態(tài)運(yùn)行的時(shí)間,單位為jiffies stime=41958 該任務(wù)在核心態(tài)運(yùn)行的時(shí)間,單位為jiffies cutime=0 所有已死線程在用戶態(tài)運(yùn)行的時(shí)間,單位為jiffies cstime=0 所有已死在核心態(tài)運(yùn)行的時(shí)間,單位為jiffies
結(jié)論3:進(jìn)程的總Cpu時(shí)間processCpuTime = utime + stime + cutime + cstime,該值包括其所有線程的cpu時(shí)間。 /proc/<pid>/task/<tid>/stat文件該文件包含了某一進(jìn)程所有的活動(dòng)的信息,該文件中的所有值都是從系統(tǒng)啟動(dòng)開始累計(jì)到當(dāng)前時(shí)刻。該文件的內(nèi)容格式以及各字段的含義同/proc/<pid>/stat文件。 注意,該文件中的tid字段表示的不再是進(jìn)程號(hào),而是linux中的輕量級(jí)進(jìn)程(lwp),即我們通常所說的線程。
結(jié)論4:線程Cpu時(shí)間threadCpuTime = utime + stime 系統(tǒng)中有關(guān)進(jìn)程cpu使用率的常用命令ps 命令通過ps命令可以查看系統(tǒng)中相關(guān)進(jìn)程的Cpu使用率的信息。以下在linux man文檔中對(duì)ps命令輸出中有關(guān)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.
結(jié)論5:ps命令算出來的cpu使用率相對(duì)于進(jìn)程啟動(dòng)時(shí)的平均值,隨著進(jìn)程運(yùn)行時(shí)間的增大,該值會(huì)趨向于平緩。 top命令通過top命令可以查看系統(tǒng)中相關(guān)進(jìn)程的實(shí)時(shí)信息(cpu使用率等)。以下是man文檔中對(duì)top命令輸出中有關(guān)進(jìn)程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.
結(jié)論6:某一個(gè)線程在其運(yùn)行期間其所使用的cpu可能會(huì)發(fā)生變化。 結(jié)論7:在多核的情況下top命令輸出的cpu使用率實(shí)質(zhì)是按cpu個(gè)數(shù)*100%計(jì)算的。 單核情況下Cpu使用率的計(jì)算基本思想通過讀取/proc/stat 、/proc/<pid>/stat、/proc/<pid>/task/<tid>/stat以及/proc/cpuinfo這幾個(gè)文件獲取總的Cpu時(shí)間、進(jìn)程的Cpu時(shí)間、線程的Cpu時(shí)間以及Cpu的個(gè)數(shù)的信息,然后通過一定的算法進(jìn)行計(jì)算(采樣兩個(gè)足夠短的時(shí)間間隔的Cpu快照與進(jìn)程快照來計(jì)算進(jìn)程的Cpu使用率)。
總的Cpu使用率計(jì)算計(jì)算方法:1、 采樣兩個(gè)足夠短的時(shí)間間隔的Cpu快照,分別記作t1,t2,其中t1、t2的結(jié)構(gòu)均為: (user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元組; 2、 計(jì)算總的Cpu時(shí)間片totalCpuTime a) 把第一次的所有cpu使用情況求和,得到s1; b) 把第二次的所有cpu使用情況求和,得到s2; c) s2 - s1得到這個(gè)時(shí)間間隔內(nèi)的所有時(shí)間片,即totalCpuTime = j2 - j1 ; 3、計(jì)算空閑時(shí)間idle idle對(duì)應(yīng)第四列的數(shù)據(jù),用第二次的第四列 - 第一次的第四列即可 idle=第二次的第四列 - 第一次的第四列 6、計(jì)算cpu使用率 pcpu =100* (total-idle)/total 某一進(jìn)程Cpu使用率的計(jì)算計(jì)算方法:1. 采樣兩個(gè)足夠短的時(shí)間間隔的cpu快照與進(jìn)程快照, a) 每一個(gè)cpu快照均為(user、nice、system、idle、iowait、irq、softirq、stealstolen、guest)的9元組; b) 每一個(gè)進(jìn)程快照均為 (utime、stime、cutime、cstime)的4元組; 2. 分別根據(jù)結(jié)論2、結(jié)論3計(jì)算出兩個(gè)時(shí)刻的總的cpu時(shí)間與進(jìn)程的cpu時(shí)間,分別記作:totalCpuTime1、totalCpuTime2、processCpuTime1、processCpuTime2 3. 計(jì)算該進(jìn)程的cpu使用率pcpu = 100*( processCpuTime2 – processCpuTime1) / (totalCpuTime2 – totalCpuTime1) (按100%計(jì)算,如果是多核情況下還需乘以cpu的個(gè)數(shù)); 實(shí)驗(yàn)數(shù)據(jù)
某一線程Cpu使用率的計(jì)算計(jì)算方法:1. 采樣兩個(gè)足夠短的時(shí)間隔的cpu快照與線程快照, a) 每一個(gè)cpu快照均為(user、nice、system、idle、iowait、irq、softirq、stealstealon、guest)的9元組; b) 每一個(gè)線程快照均為 (utime、stime)的2元組; 2. 分別根據(jù)結(jié)論2、結(jié)論4計(jì)算出兩個(gè)時(shí)刻的總的cpu時(shí)間與線程的cpu時(shí)間,分別記作:totalCpuTime1、totalCpuTime2、threadCpuTime1、threadCpuTime2 3. 計(jì)算該線程的cpu使用率pcpu = 100*( threadCpuTime2 – threadCpuTime1) / (totalCpuTime2 – totalCpuTime1) (按100%計(jì)算,如果是多核情況下還需乘以cpu的個(gè)數(shù)); 實(shí)驗(yàn)數(shù)據(jù)
多核情況下cpu使用率的計(jì)算以下通過實(shí)驗(yàn)數(shù)據(jù)來說明多核情況下某一進(jìn)程cpu使用率是按cpu個(gè)數(shù)*100%計(jì)算的. 實(shí)驗(yàn)一描述:在雙核的情況下作的一組實(shí)驗(yàn),第一組數(shù)據(jù)是通過ps -eLo pid,lwp,pcpu | grep 9140命令查看進(jìn)程號(hào)為9140的進(jìn)程中各線程的詳細(xì)信息。第二組數(shù)據(jù)是通過 ps命令查看進(jìn)程號(hào)為9140進(jìn)程的cpu使用率。 數(shù)據(jù)一:pid lwp %cpu 9140 9140 0.0 以上除了紅色標(biāo)注出來的兩個(gè)線程以外,其他的線程都是后臺(tái)線程。 數(shù)據(jù)二:pid %cpu 9140 193 實(shí)驗(yàn)二描述:在單核的情況下作的一組實(shí)驗(yàn),第一組數(shù)據(jù)是通過ps -eLo pid,lwp,pcpu | grep 6137命令查看進(jìn)程號(hào)為6137的進(jìn)程中各線程的詳細(xì)信息。第二組數(shù)據(jù)是通過 ps命令查看進(jìn)程號(hào)為6137進(jìn)程的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)線程 以上除了紅色標(biāo)注出來的兩個(gè)線程以外,其他的線程都是后臺(tái)線程。 數(shù)據(jù)二pid %cpu 6137 92.9 主要問題:1. 不同內(nèi)核版本/proc/stat文件格式不大一致。/proc/stat文件中第一行為總的cpu使用情況。 各個(gè)版本都有的4個(gè)字段: 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.關(guān)于出現(xiàn)cpu使用率為負(fù)的情況,目前想到的解決方案是如果出現(xiàn)負(fù)值,連續(xù)采樣計(jì)算cpu使用率直到為非負(fù)。 4. 有些線程生命周期較短,可能在我們采樣期間就已經(jīng)死掉了. |
|