您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)java中怎么計(jì)算cpu使用率,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來(lái)看看吧。
/proc/stat 文件內(nèi)容:
[root@Shentar ~]# cat /proc/stat cpu 602 0 2164 11445 2294 0 17 0 0 cpu0 306 0 1232 4553 2125 0 15 0 0 cpu1 295 0 932 6891 169 0 1 0 0 intr 7110 269 7 0 1 1 0 5 0 1 0 0 0 91 0 0 106 0 6521 0 108 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 38984 btime 1368275792 processes 2713 procs_running 1 procs_blocked 0 [root@Shentar ~]#
***行的數(shù)值表示的是CPU總的使用情況,所以我們只要用***行的數(shù)字計(jì)算就可以了。下表解析***行各數(shù)值的含義:
參數(shù) 解析(單位:jiffies)
(jiffies是內(nèi)核中的一個(gè)全局變量,用來(lái)記錄自系統(tǒng)啟動(dòng)一來(lái)產(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é)論:總的cpu時(shí)間totalCpuTime = user + nice + system + idle + iowait + irq + softirq + stealstolen + guest
計(jì)算時(shí),采樣兩個(gè)時(shí)間點(diǎn)的數(shù)據(jù),對(duì)于時(shí)間點(diǎn)1,記錄總的cpu時(shí)間total1,記錄空閑時(shí)間idle1,對(duì)于時(shí)間2,同樣記錄total2和idle2。
菜譜使用率為:cpuusage = 1 – (idle2 – idle1) / (total2 – total1)
注意,如果時(shí)間點(diǎn)1和時(shí)間點(diǎn)2間隔足夠?。ㄐ∮?0ms),則可能出現(xiàn)total2 – total1為0,這樣cpu使用率應(yīng)該為0,而不是采用除法計(jì)算。
java代碼如下:
CPUUsage.java
package com; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class CPUUsage { private static final String procPath = File.separator + "proc" + File.separator + "stat"; public static void main(String[] args) { CPUTime startTime = new CPUTime(); CPUTime endTime = new CPUTime(); getcpuTime(startTime); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } getcpuTime(endTime); double cpuUsage = 0; long totalTime = endTime.getTotalTime() - startTime.getTotalTime(); if (totalTime == 0) { cpuUsage = 0; } else { cpuUsage = 1 - (((double) (endTime.getIdleTime() - startTime.getIdleTime())) / totalTime); } System.out.println("the cpu usage is: " + cpuUsage * 100 + "%"); } private static void getcpuTime(CPUTime t) { BufferedReader fr = null; try { fr = new BufferedReader(new FileReader(new File(procPath))); String oneLine = null; while ((oneLine = fr.readLine()) != null) { if (oneLine.startsWith("cpu ")) { String[] vals = oneLine.substring(4).split(" "); if (vals.length != 10) { System.err.println("read an error line string!"); } else { t.setTotalTime(Long.parseLong(vals[1]) + Long.parseLong(vals[2]) + Long.parseLong(vals[3]) + Long.parseLong(vals[4]) + Long.parseLong(vals[5]) + Long.parseLong(vals[6]) + Long.parseLong(vals[7]) + Long.parseLong(vals[8]) + Long.parseLong(vals[9])); t.setIdleTime(Long.parseLong(vals[4])); break; } } } } catch (NumberFormatException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fr != null) { try { fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
CPUTime.java
package com; public class CPUTime { private long totalTime; private long idleTime; public CPUTime() { totalTime = 0; idleTime = 0; } public long getTotalTime() { return totalTime; } public void setTotalTime(long totalTime) { this.totalTime = totalTime; } public long getIdleTime() { return idleTime; } public void setIdleTime(long idleTime) { this.idleTime = idleTime; } }
以上就是java中怎么計(jì)算cpu使用率,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。