如何获得CPU负荷的Linux百分之百精确和简单的方法?

如何获得CPU负荷的Linux百分之百精确和简单的方法?,第1张

概述如何获得CPU负荷的Linux百分之百精确和简单的方法

您好我想获得cpu负载,但我想只得到cpu百分比的负载。 我有我的代码如下所示什么最简单的方法来获取它,因为我尝试使用networking代码埠

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);

希望你的回复

提前致谢

windows的安装程序

使用https而不是http

“URI不是分层的”+私钥

阻止linux内核从真正的大堆中杀死Java进程

Swing,远程桌面exception

ColdFusion可以访问windows内存值

有没有在Java中使用windows索引器信息的方法?

如何为我的跨平台Jar调用OS X特定的方法?

在linux和windows上的算术运算执行速度

如何让壳牌总是在SWT上?

最好使用Sigar API,您可以使用它来提取不同的指标。 我也用这个为我的应用程序,你可以参考下面的链接

http://support.hyperic.com/display/SIGAR/Home

创建一个计时器,并每秒获取所有线程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() 。

这个使用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); } } }

资源

你可以使用这个类:

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; } } }

你也可以看看其他的监控方法,希望能帮到你!

编辑:(新的性能监视器)

import java.lang.management.ManagementFactory; public class PerformanceMonitor { static long lastSystemTime = 0; static long lastProcesscpuTime = 0; public static int availableProcessors = ManagementFactory.getoperatingSystemMXBean().getAvailableProcessors(); public synchronized double getcpuUsage() { ManagementFactory.getThreadMXBean().setThreadcpuTimeEnabled(true); if ( lastSystemTime == 0 ) { baselineCounters(); // return ; } long systemTime = System.nanoTime(); long processcpuTime = 0; processcpuTime = ManagementFactory.getThreadMXBean().getCurrentThreadcpuTime(); double cpuUsage = (double) (processcpuTime - lastProcesscpuTime ) / ( systemTime - lastSystemTime )*100.0; lastSystemTime = systemTime; lastProcesscpuTime = processcpuTime; return cpuUsage / availableProcessors; } private voID baselineCounters() { lastSystemTime = System.nanoTime(); lastProcesscpuTime = ManagementFactory.getThreadMXBean().getCurrentThreadcpuTime(); } }

总结

以上是内存溢出为你收集整理的如何获得CPU负荷的Linux百分之百精确和简单的方法?全部内容,希望文章能够帮你解决如何获得CPU负荷的Linux百分之百精确和简单的方法?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1154086.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存