2012-03-05 6 views

答えて

2

その中にお返事を

感謝をネット

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

また、他の監視方法を見てみることができ、それが助けいただければ幸いです!

EDIT:(新しいパフォーマンスモニタ)

​​
+0

溶液であってもよいです? –

+0

はい、私は他の比較ベースを持っていないと思いますが、私はメモリ管理と結果を使用して、JVMとの比較でかなり正確な例を追加します。それはおそらくCPU使用量も良いです –

+0

しかし、私はこのコードを実行した後に任意の出力を取得していない??? –

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

関連する問題