Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

92
LINES

< > BotCompany Repo | #1001404 // Per-Thread CPU monitor

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (169L/2K/6K).

!747
!pcall {

m {
  static Map<Long, Thread> idToThread;
  static new WeakHashMap<Thread, Info> infoMap;
  
  static class Info {
    long cpuTime, measuredWhen; // all in milliseconds
    double percentage; // percentage of CPU used
  }
  
  static ThreadMXBean mx;
  static boolean cpuTimeSupported;
  
  static double totalPercentage; // all threads combined
  
  p {
    mx = ManagementFactory.getThreadMXBean();
    cpuTimeSupported = mx.isThreadCpuTimeSupported();
    if (!cpuTimeSupported) fail("No CPU time info available on platform.");
    
    while (true) {
      pcall {
        monitor();
        printMostActiveThread();
      }
      sleepSeconds(1);
    }
  }
    
  static void monitor() {    
    L<Thread> threads = getAllThreads();
    new WeakHashMap<Thread, Info> newInfoMap;
    totalPercentage = 0;
    
    for (Thread t : threads) {
      Info info = infoMap.get(t);
      if (info == null) info = new Info;
      newInfoMap.put(t, info);
      
      long nano = 0;
      pcall { nano = mx.getThreadCpuTime(t.getId()); }
      long measured = now();
      long ms = nano/1000000;
      
      if (info.measuredWhen != 0) {
        long elapsed = measured - info.measuredWhen;
        long used = ms-info.cpuTime;
        info.percentage = Math.max(0, Math.min(100, used*100.0/elapsed));
        totalPercentage += info.percentage;
      }
      
      info.measuredWhen = measured;
      info.cpuTime = ms;
    }
    
    infoMap = newInfoMap;
  }
  
  static void printMostActiveThread() {
    Thread mostActive = null;
    double highest = 0.0;
    
    for (Map.Entry<Thread, Info> e : infoMap.entrySet()) {
      Thread t = e.getKey();
      Info info = e.getValue();
      if (mostActive == null || info.percentage > highest) {
        mostActive = t;
        highest = info.percentage;
      }
    }
    
    if (mostActive == null)
      print("No info");
    else
      print("Total CPU used by this VM: " + formatDouble(totalPercentage, 1) + "%. Most active thread: " + mostActive.getName() + " (" + formatDouble(highest, 1) + "% CPU)");
  }
  
  static L<Thread> getAllThreads() {
    ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
    ThreadGroup parentGroup;
    while ( ( parentGroup = rootGroup.getParent() ) != null )
      rootGroup = parentGroup;
    Thread[] threads = new Thread[rootGroup.activeCount()];
    int n;
    while ((n = rootGroup.enumerate(threads, true)) == threads.length)       threads = new Thread[threads.length*2];
    L<Thread> l = new ArrayList<Thread>(n);
    for (int i = 0; i < n; i++) l.add(threads[i]);
    ret l;
  }
}

Author comment

Began life as a copy of #1001403

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dhtvkmknsjym, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001404
Snippet name: Per-Thread CPU monitor
Eternal ID of this version: #1001404/1
Text MD5: eb23f1cc56eee2a081f863112aec442a
Transpilation MD5: 960bc5d27023c97e2a5c1c19306907f6
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-14 18:05:30
Source code size: 2702 bytes / 92 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 599 / 954
Referenced in: [show references]