Libraryless. Click here for Pure Java version (169L/2K/6K).
1 | !747 |
2 | !pcall { |
3 | |
4 | m { |
5 | static Map<Long, Thread> idToThread; |
6 | static new WeakHashMap<Thread, Info> infoMap; |
7 | |
8 | static class Info { |
9 | long cpuTime, measuredWhen; // all in milliseconds |
10 | double percentage; // percentage of CPU used |
11 | } |
12 | |
13 | static ThreadMXBean mx; |
14 | static boolean cpuTimeSupported; |
15 | |
16 | static double totalPercentage; // all threads combined |
17 | |
18 | p { |
19 | mx = ManagementFactory.getThreadMXBean(); |
20 | cpuTimeSupported = mx.isThreadCpuTimeSupported(); |
21 | if (!cpuTimeSupported) fail("No CPU time info available on platform."); |
22 | |
23 | while (true) { |
24 | pcall { |
25 | monitor(); |
26 | printMostActiveThread(); |
27 | } |
28 | sleepSeconds(1); |
29 | } |
30 | } |
31 | |
32 | static void monitor() { |
33 | L<Thread> threads = getAllThreads(); |
34 | new WeakHashMap<Thread, Info> newInfoMap; |
35 | totalPercentage = 0; |
36 | |
37 | for (Thread t : threads) { |
38 | Info info = infoMap.get(t); |
39 | if (info == null) info = new Info; |
40 | newInfoMap.put(t, info); |
41 | |
42 | long nano = 0; |
43 | pcall { nano = mx.getThreadCpuTime(t.getId()); } |
44 | long measured = now(); |
45 | long ms = nano/1000000; |
46 | |
47 | if (info.measuredWhen != 0) { |
48 | long elapsed = measured - info.measuredWhen; |
49 | long used = ms-info.cpuTime; |
50 | info.percentage = Math.max(0, Math.min(100, used*100.0/elapsed)); |
51 | totalPercentage += info.percentage; |
52 | } |
53 | |
54 | info.measuredWhen = measured; |
55 | info.cpuTime = ms; |
56 | } |
57 | |
58 | infoMap = newInfoMap; |
59 | } |
60 | |
61 | static void printMostActiveThread() { |
62 | Thread mostActive = null; |
63 | double highest = 0.0; |
64 | |
65 | for (Map.Entry<Thread, Info> e : infoMap.entrySet()) { |
66 | Thread t = e.getKey(); |
67 | Info info = e.getValue(); |
68 | if (mostActive == null || info.percentage > highest) { |
69 | mostActive = t; |
70 | highest = info.percentage; |
71 | } |
72 | } |
73 | |
74 | if (mostActive == null) |
75 | print("No info"); |
76 | else |
77 | print("Total CPU used by this VM: " + formatDouble(totalPercentage, 1) + "%. Most active thread: " + mostActive.getName() + " (" + formatDouble(highest, 1) + "% CPU)"); |
78 | } |
79 | |
80 | static L<Thread> getAllThreads() { |
81 | ThreadGroup rootGroup = Thread.currentThread().getThreadGroup(); |
82 | ThreadGroup parentGroup; |
83 | while ( ( parentGroup = rootGroup.getParent() ) != null ) |
84 | rootGroup = parentGroup; |
85 | Thread[] threads = new Thread[rootGroup.activeCount()]; |
86 | int n; |
87 | while ((n = rootGroup.enumerate(threads, true)) == threads.length) threads = new Thread[threads.length*2]; |
88 | L<Thread> l = new ArrayList<Thread>(n); |
89 | for (int i = 0; i < n; i++) l.add(threads[i]); |
90 | ret l; |
91 | } |
92 | } |
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: | 601 / 956 |
Referenced in: | [show references] |