!7 // All numbers per core import com.sun.management.OperatingSystemMXBean; static boolean verbose = false; static long interval = 1000; // ms sbool measureThreads = true; static Map idToThread; static new WeakHashMap infoMap; static class Info { long cpuTime, measuredWhen; // all in milliseconds double percentage; // percentage of CPU used } static ThreadMXBean mx; static boolean cpuTimeSupported; static volatile double totalPercentage; // all threads combined static long lastMeasured; // info about HotSpot compilation static double compilationPercentage; static long lastCompilationTime, compilationTimeMeasured; p { mx = ManagementFactory.getThreadMXBean(); cpuTimeSupported = mx.isThreadCpuTimeSupported(); if (!cpuTimeSupported) fail("No CPU time info available on platform."); makeAndroid3("CPU Monitor Bot."); thread "Java CPU Monitor" { while (licensed()) { pcall { monitor(); if (verbose && measureThreads) printMostActiveThread(); } sleep(interval); } } } svoid monitor { lock programLock(); long ct = hotSpotCompilationTime(); long ctm = now(); if (compilationTimeMeasured != 0) compilationPercentage = 100*doubleRatio(ct-lastCompilationTime, ctm-compilationTimeMeasured); compilationTimeMeasured = ctm; lastCompilationTime = ct; if (!measureThreads) ret; L threads = allThreads_fast(); new WeakHashMap newInfoMap; double _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 = max(0, min(100, used*100.0/elapsed)); _totalPercentage += info.percentage; } info.measuredWhen = measured; info.cpuTime = ms; } totalPercentage = _totalPercentage; lastMeasured = now(); infoMap = newInfoMap; } static Thread getMostActiveThread() { lock programLock(); Thread mostActive = null; double highest = 0.0; for (Map.Entry e : infoMap.entrySet()) { Thread t = e.getKey(); Info info = e.getValue(); if (mostActive == null || info.percentage > highest) { mostActive = t; highest = info.percentage; } } return mostActive; } static void printMostActiveThread() { lock programLock(); Thread mostActive = getMostActiveThread(); if (mostActive == null) print("No info"); else print("Total CPU used by this VM: " + formatDouble(totalPercentage, 1) + "%. Most active thread: " + mostActive.getName() + " (" + formatDouble(infoMap.get(mostActive).percentage, 1) + "% CPU)"); } answer { lock programLock(); if (match("cpu", s) || match("cpu usage", s)) { S a = formatDouble(totalPercentage, 2) + "% Java threads"; pcall { OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class); double x = osBean.getProcessCpuLoad()*numberOfCores()*100, y = osBean.getSystemCpuLoad()*numberOfCores()*100; a += ". " + formatDouble(x, 1) + "% this process, " + formatDouble(max(0, y-x), 1) + "% other processes"; } return a; } if (match3("most active thread", s)) { Thread t = getMostActiveThread(); return t == null ? "Sorry, no info." : format3("Thread name: *, cpu: *", t.getName(), formatDouble(totalPercentage, 2) + "%"); } if (match3("cpu: get measurement interval", s)) ret interval + " ms"; if (match3("cpu: are you measuring", s)) ret "Yes."; if (match3("cpu: get last measurement time", s)) ret "" + lastMeasured; if "cpu: list all threads" { new L l; for (Map.Entry e : infoMap.entrySet()) { Thread t = e.getKey(); Info info = e.getValue(); l.add(formatDouble(info.percentage, 1) + "% " + format("*", t.getName())); } ret slackSnippet(fromLines(l)); } if "system load" ret "System load: " + formatDouble(systemLoad(), 2); if "cpu: processors" { java.lang.management.OperatingSystemMXBean bean = ManagementFactory.getOperatingSystemMXBean(); ret "I have " + n(bean.getAvailableProcessors(), bean.getArch() + " processor") + "."; } } // in percent static double getInProcessCPU() { ret totalPercentage; }