-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSysInfo.java
More file actions
300 lines (259 loc) · 10.3 KB
/
SysInfo.java
File metadata and controls
300 lines (259 loc) · 10.3 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright 2008-2014 by Emeric Vernat
*
* This file is part of Java Melody.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.gitblit.sysinfo;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
/**
* This code was extracted from JavaMelody and refactored.
*
* @author Emeric Vernat
* @author James Moger
*/
public class SysInfo implements Serializable {
private static final long serialVersionUID = 1L;
private static final Date START_DATE = new Date();
private final MemoryInfo memoryInfo;
private final long processCpuTimeMillis;
private final double systemLoadAverage;
private final long unixOpenFileDescriptorCount;
private final long unixMaxFileDescriptorCount;
private final String host;
private final String os;
private final int availableProcessors;
private final String javaVersion;
private final String jvmVersion;
private final String pid;
private final Date startDate;
private final String jvmArguments;
private final long freeDiskSpaceInTemp;
private final int threadCount;
private final int peakThreadCount;
private final long totalStartedThreadCount;
private final List<ThreadInfo> threadInfoList;
private final List<ProcessInfo> processInfoList;
public SysInfo() {
this(true, true);
}
public SysInfo(boolean collectThreadInfo, boolean collectProcessInfo) {
startDate = START_DATE;
host = Parameters.getHostName() + '@' + Parameters.getHostAddress();
os = buildOS();
availableProcessors = Runtime.getRuntime().availableProcessors();
javaVersion = System.getProperty("java.runtime.name") + ", " + System.getProperty("java.runtime.version");
jvmVersion = System.getProperty("java.vm.name") + ", " + System.getProperty("java.vm.version") + ", " + System.getProperty("java.vm.info");
jvmArguments = buildJvmArguments();
pid = PID.getPID();
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
threadCount = threadBean.getThreadCount();
peakThreadCount = threadBean.getPeakThreadCount();
totalStartedThreadCount = threadBean.getTotalStartedThreadCount();
threadInfoList = collectThreadInfo ? ThreadInfo.buildThreadInfoList() : Collections.emptyList();
processInfoList = collectProcessInfo ? ProcessInfo.buildProcessInfoList() : Collections.emptyList();
memoryInfo = new MemoryInfo();
systemLoadAverage = buildSystemLoadAverage();
processCpuTimeMillis = buildProcessCpuTimeMillis();
unixOpenFileDescriptorCount = buildOpenFileDescriptorCount();
unixMaxFileDescriptorCount = buildMaxFileDescriptorCount();
freeDiskSpaceInTemp = Parameters.TEMPORARY_DIRECTORY.getFreeSpace();
}
private static String buildOS() {
String name = System.getProperty("os.name");
String version = System.getProperty("os.version");
String patchLevel = System.getProperty("sun.os.patch.level");
String arch = System.getProperty("os.arch");
String bits = System.getProperty("sun.arch.data.model");
StringBuilder sb = new StringBuilder();
sb.append(name).append(", ");
if (!name.toLowerCase(Locale.ENGLISH).contains("windows")) {
// version is "6.1" and useless for os.name "Windows 7",
// and can be "2.6.32-358.23.2.el6.x86_64" for os.name "Linux"
sb.append(version).append(' ');
}
if (!"unknown".equals(patchLevel)) {
// patchLevel is "unknown" and useless on Linux,
// and can be "Service Pack 1" on Windows
sb.append(patchLevel);
}
sb.append(", ").append(arch).append('/').append(bits);
return sb.toString();
}
private long buildProcessCpuTimeMillis() {
OperatingSystemMXBean operatingSystem = ManagementFactory.getOperatingSystemMXBean();
if (isSunOsMBean(operatingSystem)) {
// nanoseconds to milliseconds
return MemoryInfo.getLongFromOperatingSystem(operatingSystem, "getProcessCpuTime") / 1000000;
}
return -1;
}
private long buildOpenFileDescriptorCount() {
OperatingSystemMXBean operatingSystem = ManagementFactory.getOperatingSystemMXBean();
if (isSunOsMBean(operatingSystem) && isSunUnixMBean(operatingSystem)) {
try {
return MemoryInfo.getLongFromOperatingSystem(operatingSystem, "getOpenFileDescriptorCount");
} catch (Error e) {
// pour issue 16 (using jsvc on ubuntu or debian)
return -1;
}
}
return -1;
}
private long buildMaxFileDescriptorCount() {
OperatingSystemMXBean operatingSystem = ManagementFactory.getOperatingSystemMXBean();
if (isSunOsMBean(operatingSystem) && isSunUnixMBean(operatingSystem)) {
try {
return MemoryInfo.getLongFromOperatingSystem(operatingSystem, "getMaxFileDescriptorCount");
} catch (Error e) {
// pour issue 16 (using jsvc on ubuntu or debian)
return -1;
}
}
return -1;
}
private double buildSystemLoadAverage() {
// System load average for the last minute.
// The system load average is the sum of
// the number of runnable entities queued to the available processors
// and the number of runnable entities running on the available processors
// averaged over a period of time.
OperatingSystemMXBean operatingSystem = ManagementFactory.getOperatingSystemMXBean();
if (operatingSystem.getSystemLoadAverage() >= 0) {
return operatingSystem.getSystemLoadAverage();
}
return -1;
}
private static String buildJvmArguments() {
StringBuilder jvmArgs = new StringBuilder();
for (String jvmArg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
jvmArgs.append(jvmArg).append('\n');
}
if (jvmArgs.length() > 0) {
jvmArgs.deleteCharAt(jvmArgs.length() - 1);
}
return jvmArgs.toString();
}
boolean isSunOsMBean(OperatingSystemMXBean operatingSystem) {
// on ne teste pas operatingSystem instanceof com.sun.management.OperatingSystemMXBean
// car le package com.sun n'existe à priori pas sur une jvm tierce
String className = operatingSystem.getClass().getName();
return "com.sun.management.OperatingSystem".equals(className)
|| "com.sun.management.UnixOperatingSystem".equals(className)
// sun.management.OperatingSystemImpl pour java 8
|| "sun.management.OperatingSystemImpl".equals(className);
}
boolean isSunUnixMBean(OperatingSystemMXBean operatingSystem) {
for (Class<?> inter : operatingSystem.getClass().getInterfaces()) {
if ("com.sun.management.UnixOperatingSystemMXBean".equals(inter.getName())) {
return true;
}
}
return false;
}
public MemoryInfo getMemoryInfo() {
return memoryInfo;
}
public long getProcessCpuTimeMillis() {
return processCpuTimeMillis;
}
public double getSystemLoadAverage() {
return systemLoadAverage;
}
public long getUnixOpenFileDescriptorCount() {
return unixOpenFileDescriptorCount;
}
public long getUnixMaxFileDescriptorCount() {
return unixMaxFileDescriptorCount;
}
public double getUnixOpenFileDescriptorPercentage() {
if (unixOpenFileDescriptorCount >= 0) {
return 100d * unixOpenFileDescriptorCount / unixMaxFileDescriptorCount;
}
return -1d;
}
public String getHost() {
return host;
}
public String getOs() {
return os;
}
public int getAvailableProcessors() {
return availableProcessors;
}
public String getJavaVersion() {
return javaVersion;
}
public String getJvmVersion() {
return jvmVersion;
}
public String getPid() {
return pid;
}
public Date getStartDate() {
return startDate;
}
public String getJvmArguments() {
return jvmArguments;
}
public long getFreeDiskSpaceInTemp() {
return freeDiskSpaceInTemp;
}
public int getThreadCount() {
return threadCount;
}
public int getPeakThreadCount() {
return peakThreadCount;
}
public long getTotalStartedThreadCount() {
return totalStartedThreadCount;
}
public List<ThreadInfo> getThreadInfoList() {
// on trie sur demande (si affichage)
List<ThreadInfo> result = new ArrayList<>(threadInfoList);
Collections.sort(result);
return Collections.unmodifiableList(result);
}
public List<ProcessInfo> getProcessInfoList() {
// on trie sur demande (si affichage)
List<ProcessInfo> result = new ArrayList<>(processInfoList);
Collections.sort(result);
return Collections.unmodifiableList(result);
}
public boolean isStackTraceEnabled() {
for (ThreadInfo threadInformations : threadInfoList) {
List<StackTraceElement> stackTrace = threadInformations.getStackTrace();
if (stackTrace != null && !stackTrace.isEmpty()) {
return true;
}
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return getClass().getSimpleName() + "[pid=" + getPid() + ", host=" + getHost()
+ ", javaVersion=" + getJavaVersion() + ']';
}
}