/
MonitorTypeCpu.java
103 lines (87 loc) · 3.63 KB
/
MonitorTypeCpu.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
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.monitor;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Method;
import org.jumpmind.extension.IBuiltInExtensionPoint;
import org.jumpmind.symmetric.model.Monitor;
public class MonitorTypeCpu extends AbstractMonitorType implements IBuiltInExtensionPoint {
protected OperatingSystemMXBean osBean;
protected RuntimeMXBean runtimeBean;
public MonitorTypeCpu() {
osBean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
runtimeBean = ManagementFactory.getRuntimeMXBean();
}
@Override
public String getName() {
return "cpu";
}
@Override
public long check(Monitor monitor) {
int availableProcessors = osBean.getAvailableProcessors();
long prevUpTime = runtimeBean.getUptime();
long prevProcessCpuTime = getProcessCpuTime();
try {
Thread.sleep(500);
} catch (Exception ignore) {
}
long upTime = runtimeBean.getUptime();
long processCpuTime = getProcessCpuTime();
long elapsedCpu = processCpuTime - prevProcessCpuTime;
long elapsedTime = upTime - prevUpTime;
return (long) (elapsedCpu / (elapsedTime * 1000f * availableProcessors));
}
protected long getProcessCpuTime() {
long cpuTime = 0;
try {
Method method = osBean.getClass().getMethod("getProcessCpuTime");
method.setAccessible(true);
cpuTime = (Long) method.invoke(osBean);
} catch (Exception ignore) {
}
return cpuTime;
}
protected String getNotificationMessage(long value, long threshold, long period) {
ThreadInfo infos[] = new ThreadInfo[TOP_THREADS];
long cpuUsages[] = new long[TOP_THREADS];
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
for (long threadId : threadBean.getAllThreadIds()) {
ThreadInfo info = threadBean.getThreadInfo(threadId);
if (info.getThreadState() != Thread.State.TERMINATED) {
rankTopUsage(infos, cpuUsages, info, threadBean.getThreadCpuTime(threadId));
}
}
String text = "CPU usage is at " + value;
for (int i = 0; i < infos.length; i++) {
text += "Top #" + (i + 1) + " CPU thread " + infos[i].getThreadId() + " is using " + (cpuUsages[i] / 1000000000f) + "s";
text += logStackTrace(threadBean.getThreadInfo(infos[i].getThreadId(), MAX_STACK_DEPTH));
}
return text;
}
@Override
public boolean requiresClusterLock() {
return false;
}
}