2009-03-11 12 views

risposta

1

Sotto Linux è possibile utilizzare Runtime. exec() per eseguire "uptime" e valutare l'output. Non c'è un modo migliore sotto Linux, e non credo che ci sia un modo altrettanto "conveniente" sotto Windows.

2

Su Linux è possibile leggere il file/proc/loadavg, dove i primi tre valori rappresentano le medie del carico. Probabilmente per Windows è necessario attenersi a JNI.

5

Ciò comporta JNI ma esiste una libreria GPL di Hyperic denominata Sigar che fornisce queste informazioni per tutte le principali piattaforme, oltre a una serie di altre statistiche dipendenti dal sistema operativo come l'utilizzo del disco. Ha funzionato alla grande per noi.

0

Se si utilizza il JRockit JVM è possibile utilizzare JMAPI. Funziona con JDK 1.4, 1.5 e 1.6.

System.out.println("Total CPU-usage:" + JVMFactory.getJVM().getMachine().getCPULoad()); 

System.out.println("Total JVM-load :" + JVMFactory.getJVM().getJVMLoad()); 

for(Iterator it = JVMFactory.getJVM().getMachine().getCPUs().iterator(); it.hasNext();) 
{ 
    CPU cpu = (CPU)it.next(); 
    System.out.println("CPU Description: " + cpu.getDescription()); 
    System.out.println("CPU Clock Frequency: " + cpu.getClockFrequency()); 
    System.out.println("CPU Load: " + cpu.getLoad()); 
    System.out.println(); 
} 
4

getSystemLoadAverage() dà apprezzi di più di 1 minuto di tempo (aggiornata ogni secondo) e dà il valore per il sistema operativo in generale. Più panoramica in tempo reale dovrebbe essere eseguita monitorando ogni thread separatamente. Importante è anche notare l'intervallo di aggiornamento del monitoraggio - più spesso si controlla il valore, più precisi è in un dato momento e se lo si fa ogni millisecondo, è in genere 0 o 100 (o più a seconda di quante CPU ci sono). Ma se permettiamo il lasso di tempo (per esempio 1 secondo), otteniamo lo sviluppo medio in questo periodo di tempo e otteniamo risultati più informativi. Inoltre, è importante notare che è altamente improbabile che solo un thread occupi più di una CPU (core).

seguito attuazione permette di utilizzare 3 metodi:

  • getTotalUsage() - Carico totale da tutti i fili in JVM
  • getAvarageUsagePerCPU() - Carico Avarage per CPU (core)
  • getUsageByThread (Discussione t) - Carico totale per thread specificato

    import java.lang.management.ManagementFactory; 
    import java.lang.management.OperatingSystemMXBean; 
    import java.lang.management.ThreadMXBean; 
    import java.util.Collection; 
    import java.util.HashMap; 
    import java.util.HashSet; 
    import java.util.Map; 
    import java.util.Set; 
    
    public class MonitoringThread extends Thread { 
    
        private long refreshInterval; 
        private boolean stopped; 
    
        private Map<Long, ThreadTime> threadTimeMap = new HashMap<Long, ThreadTime>(); 
        private ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); 
        private OperatingSystemMXBean opBean = ManagementFactory.getOperatingSystemMXBean(); 
    
        public MonitoringThread(long refreshInterval) { 
         this.refreshInterval = refreshInterval; 
    
         setName("MonitoringThread"); 
    
         start(); 
        } 
    
        @Override 
        public void run() { 
         while(!stopped) { 
          Set<Long> mappedIds; 
          synchronized (threadTimeMap) { 
           mappedIds = new HashSet<Long>(threadTimeMap.keySet()); 
          } 
    
          long[] allThreadIds = threadBean.getAllThreadIds(); 
    
          removeDeadThreads(mappedIds, allThreadIds); 
    
          mapNewThreads(allThreadIds); 
    
          Collection<ThreadTime> values; 
          synchronized (threadTimeMap) { 
           values = new HashSet<ThreadTime>(threadTimeMap.values());  
          } 
    
          for (ThreadTime threadTime : values) { 
           synchronized (threadTime) { 
            threadTime.setCurrent(threadBean.getThreadCpuTime(threadTime.getId())); 
           } 
          } 
    
          try { 
           Thread.sleep(refreshInterval); 
          } catch (InterruptedException e) { 
           throw new RuntimeException(e); 
          } 
    
          for (ThreadTime threadTime : values) { 
           synchronized (threadTime) { 
            threadTime.setLast(threadTime.getCurrent());  
           } 
          } 
         } 
        } 
    
        private void mapNewThreads(long[] allThreadIds) { 
         for (long id : allThreadIds) { 
          synchronized (threadTimeMap) { 
           if(!threadTimeMap.containsKey(id)) 
            threadTimeMap.put(id, new ThreadTime(id)); 
          } 
         } 
        } 
    
        private void removeDeadThreads(Set<Long> mappedIds, long[] allThreadIds) { 
         outer: for (long id1 : mappedIds) { 
          for (long id2 : allThreadIds) { 
           if(id1 == id2) 
            continue outer; 
          } 
          synchronized (threadTimeMap) { 
           threadTimeMap.remove(id1); 
          } 
         } 
        } 
    
        public void stopMonitor() { 
         this.stopped = true; 
        } 
    
        public double getTotalUsage() { 
         Collection<ThreadTime> values; 
         synchronized (threadTimeMap) { 
          values = new HashSet<ThreadTime>(threadTimeMap.values());  
         } 
    
         double usage = 0D; 
         for (ThreadTime threadTime : values) { 
          synchronized (threadTime) { 
           usage += (threadTime.getCurrent() - threadTime.getLast())/(refreshInterval * 10000); 
          } 
         } 
         return usage; 
        } 
    
        public double getAvarageUsagePerCPU() { 
         return getTotalUsage()/opBean.getAvailableProcessors(); 
        } 
    
        public double getUsageByThread(Thread t) { 
         ThreadTime info; 
         synchronized (threadTimeMap) { 
          info = threadTimeMap.get(t.getId()); 
         } 
    
         double usage = 0D; 
         if(info != null) { 
          synchronized (info) { 
           usage = (info.getCurrent() - info.getLast())/(refreshInterval * 10000); 
          } 
         } 
         return usage; 
        } 
    
        static class ThreadTime { 
    
         private long id; 
         private long last; 
         private long current; 
    
         public ThreadTime(long id) { 
          this.id = id; 
         } 
    
         public long getId() { 
          return id; 
         } 
    
         public long getLast() { 
          return last; 
         } 
    
         public void setLast(long last) { 
          this.last = last; 
         } 
    
         public long getCurrent() { 
          return current; 
         } 
    
         public void setCurrent(long current) { 
          this.current = current; 
         } 
        } 
    } 
    
Problemi correlati