嗨我想獲得CPU負載,但我想只得到CPU負載百分比。我有我的代碼如下所示,什麼最簡單的方法得到它,因爲我嘗試這個代碼BU提前如何獲得cpu加載linux的百分比準確和簡單的方法嗎?
2
A
回答
2
其更好地使用SIGAR API使用net
OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
method.setAccessible(true);
if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
Object value;
try {
value = method.invoke(operatingSystemMXBean);
} catch (Exception e) {
value = e;
} // try
System.out.print(method.getName() + " = " + value);
希望對您的回覆
感謝,您可以使用它來提取不同的指標。我也用這個我的應用程序,你可以參考以下鏈接
1
創建一個定時器,並採取所有線程CPU時間每秒的總和。也許是這樣的:
long cpuTime = 0;
for (long id : ManagementFactory.getThreadMXBean().getAllThreadIds())
{
cpuTime += ManagementFactory.getThreadMXBean().getThreadCpuTime (id);
}
的CPU百分比是再過去和當前的第二通過時間戳差除以之間的相對CPU時間。
這裏有一個CpuStats
類的簡單示例實現:
public class CpuStats
{
private final long threadId;
private long lastCpuTime = 0;
private long lastPoll = 0;
/**
* Creates a CpuStats object for a single thread.
* @param threadId The id of the thread to monitor
*
*/
public CpuStats (long threadId)
{
this.threadId = threadId;
lastCpuTime = getTotalTime();
lastPoll = System.nanoTime();
}
/**
* Creates a CpuStatus object for all threads. The supplied statistics affect
* all threads in the current VM.
*/
public CpuStats()
{
threadId = -1;
lastCpuTime = getTotalTime();
lastPoll = System.nanoTime();
}
private long getRelativeTime()
{
long currentCpuTime = getTotalTime();
long ret = currentCpuTime - lastCpuTime;
lastCpuTime = currentCpuTime;
return ret;
}
public double getUsage()
{
long timeBefore = this.lastPoll;
lastPoll = System.nanoTime();
long relTime = getRelativeTime();
return Math.max ((double)relTime/(double)(lastPoll - timeBefore), 0.0);
}
private long getTotalTime()
{
if (threadId == -1)
{
long cpuTime = 0;
for (long id : ManagementFactory.getThreadMXBean().getAllThreadIds())
{
cpuTime += ManagementFactory.getThreadMXBean().getThreadCpuTime (id);
}
return cpuTime;
}
else
{
return ManagementFactory.getThreadMXBean().getThreadCpuTime (threadId);
}
}
}
只需定期檢索getUsage()
。
0
你可以使用這個類:
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;
public class PerformanceMonitor {
static long lastSystemTime = 0;
static long lastProcessCpuTime = 0;
static int availableProcessors = ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors();
public synchronized double getCpuUsage()
{
if (lastSystemTime == 0)
{
baselineCounters();
// return ;
}
long systemTime = System.nanoTime();
long processCpuTime = 0;
if (ManagementFactory.getOperatingSystemMXBean() instanceof com.sun.management.OperatingSystemMXBean)
{
processCpuTime = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getProcessCpuTime();
}
double cpuUsage = (double) (processCpuTime - lastProcessCpuTime)/(systemTime - lastSystemTime)*100.0;
lastSystemTime = systemTime;
lastProcessCpuTime = processCpuTime;
return cpuUsage/availableProcessors;
}
private void baselineCounters()
{
lastSystemTime = System.nanoTime();
if (ManagementFactory.getOperatingSystemMXBean() instanceof com.sun.management.OperatingSystemMXBean)
{
lastProcessCpuTime = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getProcessCpuTime();
}
}
}
然後調用:
public class Main {
public static PerformanceMonitor monitor = null;
public static void main(String[] args) {
monitor = new PerformanceMonitor();
for(int i=0 ; i<10000 ; i++){
start();
double usage = monitor.getCpuUsage();
if(usage!=0)System.out.println("Current CPU usage in pourcentage : "+usage);
}
}
private static void start() {
int count=0;
for(int i=0 ; i<100000 ; i++){
count=(int) Math.random()*100;
}
}
}
你也可以看看其他的監測方法,希望能對大家有所幫助!
編輯:(新性能監視器)
1
該代碼使用mpstat
可能是一個解決方案
import java.io.*;
public class CpuLoad {
public static void main(String args[]) {
int i=1;
float finalres;
try{
// execute the linux command
Process p=Runtime.getRuntime().exec("mpstat");
BufferedReader in=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=null;
//read the row corresponding to cpu idle
while((line=in.readLine())!=null && i<4){
i++;
}
String res=line.substring(line.length()-5);
finalres=Float.parseFloat(res);
//convert the idle to cpuload
System.out.println("CPU load:"+(100-finalres)+"%");
}
catch(Exception e){
System.out.println(e);
}
}
}
相關問題
- 1. Weka - 獲得「準確度百分比」?
- 2. 我沒有得到cpu的百分比linux的負載java
- 3. 如何獲得在InfluxDB收集的CPU使用百分比
- 4. Powershell獲取CPU百分比
- 5. 有更快的方法來獲得百分比變化嗎?
- 6. 如何獲得ajax事件的當前加載百分比?
- 7. HTA監控CPU使用率與WMI的百分比 - 不準確
- 8. 準確計算CPU使用率在Linux中以百分比給出?
- 9. 獲得參加人數的百分比
- 10. 如何獲得下載的文件的百分比IOS
- 11. 如何獲得SQL中的精確百分比calc
- 12. 簡單百分比更新
- 13. 如何以百分比的方式獲得佈局的高度?
- 14. 如何在sql中獲得百分比?
- 15. SpotFire - 如何獲得累積百分比
- 16. 如何獲得相似度百分比?
- 17. 如何獲得百分比值
- 18. 獲取CPU使用百分比
- 19. 如何計算的百分比在linux
- 20. 有沒有簡單的方法來獲得最後x分鐘成功讀取的百分比?
- 21. 如何直接從MySQL查詢中獲得兩個百分比的百分比?
- 22. 獲得組內計數和百分比
- 23. 碼頭CPU百分比
- 24. 獲得結果的百分比和賬單總數
- 25. 簡單的jQuery進度條百分比
- 26. SQL中的簡單百分比列
- 27. Android簡單的百分比佈局?
- 28. 如何使用PowerShell命令確定平均CPU百分比並將CPU百分比輸出爲無標籤的數字百分比
- 29. 如何準確計算有限總數的百分比
- 30. 如何獲得計算單個sql查詢的增加或減少百分比?
將它給我準確的結果? – 2012-03-05 12:09:18
是的,我認爲,我沒有其他的比較基礎,但我添加使用它與內存管理和結果在與JVM比較相當準確的例子。這可能對CPU使用率太好 – 2012-03-05 12:17:17
但我沒有得到任何輸出後運行此代碼? – 2012-03-05 12:19:31