-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathCpuTest.java
153 lines (130 loc) · 5.59 KB
/
CpuTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cpu;
import com.sun.management.OperatingSystemMXBean;
import com.sun.management.ThreadMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This is a example code about how to get the process cpu and thread cpu in java internal app
*/
public class CpuTest {
public static void main(String[] args) {
final AtomicInteger seq = new AtomicInteger(0);
ScheduledExecutorService es = Executors.newScheduledThreadPool(20, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread th = new Thread(r);
th.setName("consumingthreads-" + seq.incrementAndGet());
return th;
}
});
for (int i = 0; i < 200; i++) {
es.scheduleAtFixedRate(new ConsumingCpuTask(), 0, 10, TimeUnit.MILLISECONDS);
}
// not terminate the es
// another thread to print host cpu
ScheduledExecutorService printer = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread th = new Thread(r);
th.setName("printer");
return th;
}
});
// print every 10 seconds
printer.scheduleAtFixedRate(new PrintCurrentProcessCpuTask(), 0, 10, TimeUnit.SECONDS);
printer.scheduleAtFixedRate(new PrintThreadCpuTask(), 0, 10, TimeUnit.SECONDS);
}
static final int PROCESSOR_COUNT = Runtime.getRuntime().availableProcessors();
// notice here is com.sun.management.OperatingSystemMXBean and it's not java.lang.management.OperatingSystemMXBean
static final OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
/**
* get process cpu in nanoseconds
*/
static double getProcessCpuTime() {
return bean.getProcessCpuTime();
}
/**
* A task to simulate consuming cpu
*/
static class ConsumingCpuTask implements Runnable {
@Override
public void run() {
AtomicInteger integer = new AtomicInteger(0);
for (int i = 0; i < 10000; i++) {
integer.incrementAndGet();
}
}
}
// it's com.sun.management.ThreadMXBean
static ThreadMXBean threadMXBean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
static class PrintThreadCpuTask implements Runnable {
// not consider thread safe here
Map<Long, Long> threadId2CpuTime = null;
Map<Long, String> threadId2Name = null;
private long collectTime = 0;
@Override
public void run() {
if (threadId2CpuTime == null) {
threadId2CpuTime = new HashMap<>();
threadId2Name = new HashMap<>();
long threads[] = threadMXBean.getAllThreadIds();
long cpuTimes[] = threadMXBean.getThreadCpuTime(threads);
for (int i = 0; i < threads.length; i++) {
threadId2CpuTime.put(threads[i], cpuTimes[i]);
// get the thread name
// maybe null, if not exists any more
ThreadInfo info = threadMXBean.getThreadInfo(threads[i]);
if (info != null) {
threadId2Name.put(threads[i], info.getThreadName());
}
}
collectTime = System.currentTimeMillis();
}
else {
long threads[] = threadMXBean.getAllThreadIds();
long cpuTimes[] = threadMXBean.getThreadCpuTime(threads);
Map<Long, Long> newthreadId2CpuTime = new HashMap<>();
for (int i = 0; i < threads.length; i++) {
newthreadId2CpuTime.put(threads[i], cpuTimes[i]);
}
long newCollectTime = System.currentTimeMillis();
threadId2CpuTime.entrySet().forEach(en -> {
long threadId = en.getKey();
Long time = en.getValue();
Long newTime = newthreadId2CpuTime.get(threadId);
if (newTime != null) {
double cpu = (newTime - time) * 1.0d / (newCollectTime - collectTime) / 1000000L / PROCESSOR_COUNT;
System.out.println(String.format("\t\tThread %s cpu is: %.2f %%", threadId2Name.get(threadId), cpu * 100));
}
threadId2CpuTime.put(threadId, newTime);
});
}
}
}
static class PrintCurrentProcessCpuTask implements Runnable {
double cpuTime = 0;
long collectTime = 0;
@Override
public void run() {
if (cpuTime == 0) {
cpuTime = getProcessCpuTime();
collectTime = System.currentTimeMillis();
}
else {
double newCpuTime = getProcessCpuTime();
long newCollectTime = System.currentTimeMillis();
double cpu = (newCpuTime - cpuTime) * 1.0d / (newCollectTime - collectTime) / 1000_000 / PROCESSOR_COUNT;
cpuTime = newCpuTime;
collectTime = newCollectTime;
System.out.println(String.format("Process cpu is: %.2f %%", cpu * 100));
}
}
}
}