2012-03-05 15 views

回答

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

希望對您的回覆

感謝,您可以使用它來提取不同的指標。我也用這個我的應用程序,你可以參考以下鏈接

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

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

你也可以看看其他的監測方法,希望能對大家有所幫助!

編輯:(新性能監視器)

​​
+0

將它給我準確的結果? – 2012-03-05 12:09:18

+0

是的,我認爲,我沒有其他的比較基礎,但我添加使用它與內存管理和結果在與JVM比較相當準確的例子。這可能對CPU使用率太好 – 2012-03-05 12:17:17

+0

但我沒有得到任何輸出後運行此代碼? – 2012-03-05 12:19:31

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

Source