計算總的 CPU 使用率 TOTALCPUUSE1)採樣兩個足夠短的時間間隔的 CPU 快照,即讀取 /proc/stat 檔案,擷取兩個時間點的下列資料:CPUT1 (user1, nice1, system1, idle1, iowait1, irq1, softirq1, stealstolen1, guest1);CPUT2 (user2, nice2, system2, idle2, iowait2, irq2, softirq2, stealstolen2, guest2);2)計算總的 CPU 時間 totalCPUTime:CPUTime1 = user1 + nice1 + system1 + idle1 + iowait1 + irq1 + softirq1 + stealstolen1 + guest1;CPUTime2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + stealstolen2 + guest2;totalCPUTime = CPUTime2 – CPUTime1;3)計算 CPU 空閑時間 idleCPUTime:idleCPUTime = idle2 – idle1;4)計算總的 CPU 使用率 totalCPUUse:totalCPUUse = (totalCPUTime – idleCPUTime) / totalCPUTime;計算某一進程的 CPU 使用率 PROCESSCPUUSE1)採樣兩個足夠短的時間間隔的 CPU 快照和對應的進程快照,即讀取 /proc/stat 檔案,擷取兩個時間點的下列資料:CPUT1 (user1, nice1, system1, idle1, iowait1, irq1, softirq1, stealstolen1, guest1);CPUT2 (user2, nice2, system2, idle2, iowait2, irq2, softirq2, stealstolen2, guest2);讀取 /proc/[PID]/stat 檔案,擷取兩個時間點的下列資料:ProcessT1 (utime1, stime1, cutime1, cstime1);ProcessT2 (utime2, stime2, cutime2, cstime2);2)計算總的 CPU 時間 totalCPUTime 和進程時間 processTime:CPUTime1 = user1 + nice1 + system1 + idle1 + iowait1 + irq1 + softirq1 + stealstolen1 + guest1;CPUTime2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + stealstolen2 + guest2;totalCPUTime = CPUTime2 – CPUTime1;processTime1 = utime1 + stime1 + cutime1 + cstime1;processTime2 = utime2 + stime2 + cutime1 + cstime2;processTime = processTime2 – processTime1;3)計算該進程的 CPU 使用率 processCPUUse:processCPUUse = processTime / totalCPUTime;計算某一線程的 CPU 使用率 threadCPUUse1)採樣兩個足夠短的時間間隔的 CPU 快照和對應的線程快照,即讀取 /proc/stat 檔案,擷取兩個時間點的下列資料:CPUT1 (user1, nice1, system1, idle1, iowait1, irq1, softirq1, stealstolen1, guest1);CPUT2 (user2, nice2, system2, idle2, iowait2, irq2, softirq2, stealstolen2, guest2);讀取 /proc/[PID]/task/[TID]/stat 檔案,擷取兩個時間點的下列資料:threadT1 (utime1, stime1);threadT2 (utime2, stime2);2)計算總的 CPU 時間 totalCPUTime 和線程時間 threadTime:CPUTime1 = user1 + nice1 + system1 + idle1 + iowait1 + irq1 + softirq1 + stealstolen1 + guest1;CPUTime2 = user2 + nice2 + system2 + idle2 + iowait2 + irq2 + softirq2 + stealstolen2 + guest2;totalCPUTime = CPUTime2 – CPUTime1;threadTime1 = utime1 + stime1;threadTime2 = utime2 + stime2;threadTime = threadTime2 – threadTime1;3)計算該線程的 CPU 使用率 threadCPUUse:threadCPUUse = threadTime / totalCPUTime;2.5、多核情況下 CPU 使用率的計算2.5.1、基本思想首先,通過讀取 /proc/stat 檔案擷取總的 CPU 時間,讀取 /proc/[PID]/stat 擷取進程 CPU 時間,讀取 /proc/[PID]/task/[TID]/stat 擷取線程 CPU 時間,讀取 /proc/cpuinfo 擷取 CPU 個數。在多核情況下計算進程或線程的 CPU 使用率,用上面的方式得到的通常是相對於 CPU 所有核的總共時間的佔用率,而我們通常習慣得到進程或線程對某一個單核的佔用率。所以我們可以按上面的方式計算得到 CPU 佔用率,然後把結果乘上 CPU 的核心數,即可得到進程或線程相對於一個單核的佔用率。2.5.2、計算總的 CPU 使用率同 2.4.2。2.5.3、計算某一進程的 CPU 使用率 mProcessCPUUse1)同 2.4.3 計算某一進程的 CPU 使用率 processCPUUse;2)讀取 /proc/cpuinfo 檔案擷取邏輯 CPU(processor) 的個數(參見 1.1): processorNum3)多核情況下該進程的 CPU 使用率 mProcessCPUUse:mProcessCPUUse = processCPUUse * processorNum;2.5.4、計算某一線程的 CPU 使用率 mThreadCPUUse1)同 2.4.4 計算某一線程的 CPU 使用率 threadCPUUse;2)讀取 /proc/cpuinfo 檔案擷取邏輯 CPU(processor) 的個數(參見 1.1): processorNum3)多核情況下該線程的 CPU 使用率 mThreadCPUUse:mThreadCPUUse = threadCPUUse * processorNum;