Libraryless. Click here for Pure Java version (6903L/48K/160K).
1 | !7 |
2 | |
3 | // All numbers per core |
4 | |
5 | import com.sun.management.OperatingSystemMXBean; |
6 | |
7 | static boolean verbose = false; |
8 | static long interval = 1000; // ms |
9 | sbool measureThreads = true; |
10 | |
11 | static Map<Long, Thread> idToThread; |
12 | static new WeakHashMap<Thread, Info> infoMap; |
13 | |
14 | static class Info { |
15 | long cpuTime, measuredWhen; // all in milliseconds |
16 | double percentage; // percentage of CPU used |
17 | } |
18 | |
19 | static ThreadMXBean mx; |
20 | static boolean cpuTimeSupported; |
21 | |
22 | static volatile double totalPercentage; // all threads combined |
23 | static long lastMeasured; |
24 | |
25 | // info about HotSpot compilation |
26 | static double compilationPercentage; |
27 | static long lastCompilationTime, compilationTimeMeasured; |
28 | |
29 | p { |
30 | mx = ManagementFactory.getThreadMXBean(); |
31 | cpuTimeSupported = mx.isThreadCpuTimeSupported(); |
32 | if (!cpuTimeSupported) fail("No CPU time info available on platform."); |
33 | |
34 | makeAndroid3("CPU Monitor Bot."); |
35 | |
36 | thread "Java CPU Monitor" { |
37 | while (licensed()) { |
38 | pcall { |
39 | monitor(); |
40 | if (verbose && measureThreads) |
41 | printMostActiveThread(); |
42 | } |
43 | sleep(interval); |
44 | } |
45 | } |
46 | } |
47 | |
48 | svoid monitor { |
49 | lock programLock(); |
50 | |
51 | long ct = hotSpotCompilationTime(); |
52 | long ctm = now(); |
53 | if (compilationTimeMeasured != 0) |
54 | compilationPercentage = 100*doubleRatio(ct-lastCompilationTime, ctm-compilationTimeMeasured); |
55 | compilationTimeMeasured = ctm; |
56 | lastCompilationTime = ct; |
57 | |
58 | if (!measureThreads) ret; |
59 | L<Thread> threads = allThreads_fast(); |
60 | new WeakHashMap<Thread, Info> newInfoMap; |
61 | double _totalPercentage = 0; |
62 | |
63 | for (Thread t : threads) { |
64 | Info info = infoMap.get(t); |
65 | if (info == null) info = new Info; |
66 | newInfoMap.put(t, info); |
67 | |
68 | long nano = 0; |
69 | pcall { nano = mx.getThreadCpuTime(t.getId()); } |
70 | long measured = now(); |
71 | long ms = nano/1000000; |
72 | |
73 | if (info.measuredWhen != 0) { |
74 | long elapsed = measured - info.measuredWhen; |
75 | long used = ms-info.cpuTime; |
76 | info.percentage = max(0, min(100, used*100.0/elapsed)); |
77 | _totalPercentage += info.percentage; |
78 | } |
79 | |
80 | info.measuredWhen = measured; |
81 | info.cpuTime = ms; |
82 | } |
83 | |
84 | totalPercentage = _totalPercentage; |
85 | lastMeasured = now(); |
86 | infoMap = newInfoMap; |
87 | } |
88 | |
89 | static Thread getMostActiveThread() { |
90 | lock programLock(); |
91 | Thread mostActive = null; |
92 | double highest = 0.0; |
93 | |
94 | for (Map.Entry<Thread, Info> e : infoMap.entrySet()) { |
95 | Thread t = e.getKey(); |
96 | Info info = e.getValue(); |
97 | if (mostActive == null || info.percentage > highest) { |
98 | mostActive = t; |
99 | highest = info.percentage; |
100 | } |
101 | } |
102 | |
103 | return mostActive; |
104 | } |
105 | |
106 | static void printMostActiveThread() { |
107 | lock programLock(); |
108 | Thread mostActive = getMostActiveThread(); |
109 | if (mostActive == null) |
110 | print("No info"); |
111 | else |
112 | print("Total CPU used by this VM: " + formatDouble(totalPercentage, 1) + "%. Most active thread: " + mostActive.getName() + " (" + formatDouble(infoMap.get(mostActive).percentage, 1) + "% CPU)"); |
113 | } |
114 | |
115 | answer { |
116 | lock programLock(); |
117 | if (match("cpu", s) || match("cpu usage", s)) { |
118 | S a = formatDouble(totalPercentage, 2) + "% Java threads"; |
119 | pcall { |
120 | OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class); |
121 | |
122 | double x = osBean.getProcessCpuLoad()*numberOfCores()*100, y = osBean.getSystemCpuLoad()*numberOfCores()*100; |
123 | a += ". " + formatDouble(x, 1) + "% this process, " |
124 | + formatDouble(max(0, y-x), 1) + "% other processes"; |
125 | } |
126 | return a; |
127 | } |
128 | |
129 | if (match3("most active thread", s)) { |
130 | Thread t = getMostActiveThread(); |
131 | return t == null ? "Sorry, no info." : format3("Thread name: *, cpu: *", t.getName(), formatDouble(totalPercentage, 2) + "%"); |
132 | } |
133 | |
134 | if (match3("cpu: get measurement interval", s)) |
135 | ret interval + " ms"; |
136 | |
137 | if (match3("cpu: are you measuring", s)) |
138 | ret "Yes."; |
139 | |
140 | if (match3("cpu: get last measurement time", s)) |
141 | ret "" + lastMeasured; |
142 | |
143 | if "cpu: list all threads" { |
144 | new L<S> l; |
145 | for (Map.Entry<Thread, Info> e : infoMap.entrySet()) { |
146 | Thread t = e.getKey(); |
147 | Info info = e.getValue(); |
148 | l.add(formatDouble(info.percentage, 1) + "% " + format("*", t.getName())); |
149 | } |
150 | ret slackSnippet(fromLines(l)); |
151 | } |
152 | |
153 | if "system load" |
154 | ret "System load: " + formatDouble(systemLoad(), 2); |
155 | |
156 | if "cpu: processors" { |
157 | java.lang.management.OperatingSystemMXBean bean = ManagementFactory.getOperatingSystemMXBean(); |
158 | ret "I have " + n(bean.getAvailableProcessors(), bean.getArch() + " processor") + "."; |
159 | } |
160 | } |
161 | |
162 | // in percent |
163 | static double getInProcessCPU() { |
164 | ret totalPercentage; |
165 | } |
Began life as a copy of #1001404
download show line numbers debug dex old transpilations
Travelled to 18 computer(s): ajlfxifxfcul, aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, dhtvkmknsjym, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1001405 |
Snippet name: | Java CPU Monitor Bot (+ System Load) |
Eternal ID of this version: | #1001405/11 |
Text MD5: | f436293bce4ba1887238817a29caffd9 |
Transpilation MD5: | 409c8569772205864d23cc15402bcc88 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-07-26 10:17:06 |
Source code size: | 4745 bytes / 165 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 757 / 8093 |
Version history: | 10 change(s) |
Referenced in: | [show references] |