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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
| package com.alibaba.datax.common.statistics;
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.lang.management.GarbageCollectorMXBean; import java.lang.management.MemoryPoolMXBean; import java.lang.management.OperatingSystemMXBean; import java.lang.management.RuntimeMXBean; import java.lang.reflect.Method; import java.util.HashMap; import java.util.List; import java.util.Map;
public class VMInfo { private static final Logger LOG = LoggerFactory.getLogger(VMInfo.class); static final long MB = 1024 * 1024; static final long GB = 1024 * 1024 * 1024; public static Object lock = new Object(); private static VMInfo vmInfo;
public static VMInfo getVmInfo() { if (vmInfo == null) { synchronized (lock) { if (vmInfo == null) { try { vmInfo = new VMInfo(); } catch (Exception e) { LOG.warn("no need care, the fail is ignored : vmInfo init failed " + e.getMessage(), e); } } }
} return vmInfo; }
private final OperatingSystemMXBean osMXBean; private final RuntimeMXBean runtimeMXBean; private final List<GarbageCollectorMXBean> garbageCollectorMXBeanList; private final List<MemoryPoolMXBean> memoryPoolMXBeanList;
private final String osInfo; private final String jvmInfo;
private final int totalProcessorCount;
private final PhyOSStatus startPhyOSStatus; private final ProcessCpuStatus processCpuStatus = new ProcessCpuStatus(); private final ProcessGCStatus processGCStatus = new ProcessGCStatus(); private final ProcessMemoryStatus processMomoryStatus = new ProcessMemoryStatus(); private long lastUpTime = 0; private long lastProcessCpuTime = 0;
private VMInfo() { osMXBean = java.lang.management.ManagementFactory.getOperatingSystemMXBean(); runtimeMXBean = java.lang.management.ManagementFactory.getRuntimeMXBean(); garbageCollectorMXBeanList = java.lang.management.ManagementFactory.getGarbageCollectorMXBeans(); memoryPoolMXBeanList = java.lang.management.ManagementFactory.getMemoryPoolMXBeans();
osInfo = runtimeMXBean.getVmVendor() + " " + runtimeMXBean.getSpecVersion() + " " + runtimeMXBean.getVmVersion(); jvmInfo = osMXBean.getName() + " " + osMXBean.getArch() + " " + osMXBean.getVersion(); totalProcessorCount = osMXBean.getAvailableProcessors();
startPhyOSStatus = new PhyOSStatus(); LOG.info("VMInfo# operatingSystem class => " + osMXBean.getClass().getName()); if (VMInfo.isSunOsMBean(osMXBean)) { { startPhyOSStatus.totalPhysicalMemory = VMInfo.getLongFromOperatingSystem(osMXBean, "getTotalPhysicalMemorySize"); startPhyOSStatus.freePhysicalMemory = VMInfo.getLongFromOperatingSystem(osMXBean, "getFreePhysicalMemorySize"); startPhyOSStatus.maxFileDescriptorCount = VMInfo.getLongFromOperatingSystem(osMXBean, "getMaxFileDescriptorCount"); startPhyOSStatus.currentOpenFileDescriptorCount = VMInfo.getLongFromOperatingSystem(osMXBean, "getOpenFileDescriptorCount"); } }
for (GarbageCollectorMXBean garbage : garbageCollectorMXBeanList) { GCStatus gcStatus = new GCStatus(); gcStatus.name = garbage.getName(); processGCStatus.gcStatusMap.put(garbage.getName(), gcStatus); }
if (memoryPoolMXBeanList != null && !memoryPoolMXBeanList.isEmpty()) { for (MemoryPoolMXBean pool : memoryPoolMXBeanList) { MemoryStatus memoryStatus = new MemoryStatus(); memoryStatus.name = pool.getName(); memoryStatus.initSize = pool.getUsage().getInit(); memoryStatus.maxSize = pool.getUsage().getMax(); processMomoryStatus.memoryStatusMap.put(pool.getName(), memoryStatus); } } }
public String toString() { return "the machine info => \n\n" + "\tosInfo:\t" + osInfo + "\n" + "\tjvmInfo:\t" + jvmInfo + "\n" + "\tcpu num:\t" + totalProcessorCount + "\n\n" + startPhyOSStatus.toString() + "\n" + processGCStatus.toString() + "\n" + processMomoryStatus.toString() + "\n"; }
public String totalString() { return (processCpuStatus.getTotalString() + processGCStatus.getTotalString()); }
public void getDelta() { getDelta(true); }
public synchronized void getDelta(boolean print) {
try { if (VMInfo.isSunOsMBean(osMXBean)) { long curUptime = runtimeMXBean.getUptime(); long curProcessTime = getLongFromOperatingSystem(osMXBean, "getProcessCpuTime"); if ((curUptime > lastUpTime) && (curProcessTime >= lastProcessCpuTime)) { float curDeltaCpu = (float) (curProcessTime - lastProcessCpuTime) / ((curUptime - lastUpTime) * totalProcessorCount * 10000); processCpuStatus.setMaxMinCpu(curDeltaCpu); processCpuStatus.averageCpu = (float) curProcessTime / (curUptime * totalProcessorCount * 10000);
lastUpTime = curUptime; lastProcessCpuTime = curProcessTime; } }
for (GarbageCollectorMXBean garbage : garbageCollectorMXBeanList) {
GCStatus gcStatus = processGCStatus.gcStatusMap.get(garbage.getName()); if (gcStatus == null) { gcStatus = new GCStatus(); gcStatus.name = garbage.getName(); processGCStatus.gcStatusMap.put(garbage.getName(), gcStatus); }
long curTotalGcCount = garbage.getCollectionCount(); gcStatus.setCurTotalGcCount(curTotalGcCount);
long curtotalGcTime = garbage.getCollectionTime(); gcStatus.setCurTotalGcTime(curtotalGcTime); }
if (memoryPoolMXBeanList != null && !memoryPoolMXBeanList.isEmpty()) { for (MemoryPoolMXBean pool : memoryPoolMXBeanList) {
MemoryStatus memoryStatus = processMomoryStatus.memoryStatusMap.get(pool.getName()); if (memoryStatus == null) { memoryStatus = new MemoryStatus(); memoryStatus.name = pool.getName(); processMomoryStatus.memoryStatusMap.put(pool.getName(), memoryStatus); } memoryStatus.commitedSize = pool.getUsage().getCommitted(); memoryStatus.setMaxMinUsedSize(pool.getUsage().getUsed()); long maxMemory = memoryStatus.commitedSize > 0 ? memoryStatus.commitedSize : memoryStatus.maxSize; memoryStatus.setMaxMinPercent(maxMemory > 0 ? (float) 100 * memoryStatus.usedSize / maxMemory : -1); } }
if (print) { LOG.info(processCpuStatus.getDeltaString() + processMomoryStatus.getDeltaString() + processGCStatus.getDeltaString()); }
} catch (Exception e) { LOG.warn("no need care, the fail is ignored : vmInfo getDelta failed " + e.getMessage(), e); } }
public static boolean isSunOsMBean(OperatingSystemMXBean operatingSystem) { final String className = operatingSystem.getClass().getName();
return "com.sun.management.UnixOperatingSystem".equals(className); }
public static long getLongFromOperatingSystem(OperatingSystemMXBean operatingSystem, String methodName) { try { final Method method = operatingSystem.getClass().getMethod(methodName, (Class<?>[]) null); method.setAccessible(true); return (Long) method.invoke(operatingSystem, (Object[]) null); } catch (final Exception e) { LOG.info(String.format("OperatingSystemMXBean %s failed, Exception = %s ", methodName, e.getMessage())); }
return -1; }
private class PhyOSStatus { long totalPhysicalMemory = -1; long freePhysicalMemory = -1; long maxFileDescriptorCount = -1; long currentOpenFileDescriptorCount = -1;
public String toString() { return String.format("\ttotalPhysicalMemory:\t%,.2fG\n" + "\tfreePhysicalMemory:\t%,.2fG\n" + "\tmaxFileDescriptorCount:\t%s\n" + "\tcurrentOpenFileDescriptorCount:\t%s\n", (float) totalPhysicalMemory / GB, (float) freePhysicalMemory / GB, maxFileDescriptorCount, currentOpenFileDescriptorCount); } }
private class ProcessCpuStatus { float maxDeltaCpu = -1; float minDeltaCpu = -1; float curDeltaCpu = -1; float averageCpu = -1;
public void setMaxMinCpu(float curCpu) { this.curDeltaCpu = curCpu; if (maxDeltaCpu < curCpu) { maxDeltaCpu = curCpu; }
if (minDeltaCpu == -1 || minDeltaCpu > curCpu) { minDeltaCpu = curCpu; } }
public String getDeltaString() { StringBuilder sb = new StringBuilder(); sb.append("\n\t [delta cpu info] => \n"); sb.append("\t\t"); sb.append(String.format("%-30s | %-30s | %-30s | %-30s \n", "curDeltaCpu", "averageCpu", "maxDeltaCpu", "minDeltaCpu")); sb.append("\t\t"); sb.append(String.format("%-30s | %-30s | %-30s | %-30s \n", String.format("%,.2f%%", processCpuStatus.curDeltaCpu), String.format("%,.2f%%", processCpuStatus.averageCpu), String.format("%,.2f%%", processCpuStatus.maxDeltaCpu), String.format("%,.2f%%\n", processCpuStatus.minDeltaCpu)));
return sb.toString(); }
public String getTotalString() { StringBuilder sb = new StringBuilder(); sb.append("\n\t [total cpu info] => \n"); sb.append("\t\t"); sb.append(String.format("%-30s | %-30s | %-30s \n", "averageCpu", "maxDeltaCpu", "minDeltaCpu")); sb.append("\t\t"); sb.append(String.format("%-30s | %-30s | %-30s \n", String.format("%,.2f%%", processCpuStatus.averageCpu), String.format("%,.2f%%", processCpuStatus.maxDeltaCpu), String.format("%,.2f%%\n", processCpuStatus.minDeltaCpu)));
return sb.toString(); }
}
private class ProcessGCStatus { final Map<String, GCStatus> gcStatusMap = new HashMap<String, GCStatus>();
public String toString() { return "\tGC Names\t" + gcStatusMap.keySet() + "\n"; }
public String getDeltaString() { StringBuilder sb = new StringBuilder(); sb.append("\n\t [delta gc info] => \n"); sb.append("\t\t "); sb.append(String.format("%-20s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s \n", "NAME", "curDeltaGCCount", "totalGCCount", "maxDeltaGCCount", "minDeltaGCCount", "curDeltaGCTime", "totalGCTime", "maxDeltaGCTime", "minDeltaGCTime")); for (GCStatus gc : gcStatusMap.values()) { sb.append("\t\t "); sb.append(String.format("%-20s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s \n", gc.name, gc.curDeltaGCCount, gc.totalGCCount, gc.maxDeltaGCCount, gc.minDeltaGCCount, String.format("%,.3fs",(float)gc.curDeltaGCTime/1000), String.format("%,.3fs",(float)gc.totalGCTime/1000), String.format("%,.3fs",(float)gc.maxDeltaGCTime/1000), String.format("%,.3fs",(float)gc.minDeltaGCTime/1000)));
} return sb.toString(); }
public String getTotalString() { StringBuilder sb = new StringBuilder(); sb.append("\n\t [total gc info] => \n"); sb.append("\t\t "); sb.append(String.format("%-20s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s \n", "NAME", "totalGCCount", "maxDeltaGCCount", "minDeltaGCCount", "totalGCTime", "maxDeltaGCTime", "minDeltaGCTime")); for (GCStatus gc : gcStatusMap.values()) { sb.append("\t\t "); sb.append(String.format("%-20s | %-18s | %-18s | %-18s | %-18s | %-18s | %-18s \n", gc.name, gc.totalGCCount, gc.maxDeltaGCCount, gc.minDeltaGCCount, String.format("%,.3fs",(float)gc.totalGCTime/1000), String.format("%,.3fs",(float)gc.maxDeltaGCTime/1000), String.format("%,.3fs",(float)gc.minDeltaGCTime/1000)));
} return sb.toString(); } }
private class ProcessMemoryStatus { final Map<String, MemoryStatus> memoryStatusMap = new HashMap<String, MemoryStatus>();
public String toString() { StringBuilder sb = new StringBuilder(); sb.append("\t"); sb.append(String.format("%-30s | %-30s | %-30s \n", "MEMORY_NAME", "allocation_size", "init_size")); for (MemoryStatus ms : memoryStatusMap.values()) { sb.append("\t"); sb.append(String.format("%-30s | %-30s | %-30s \n", ms.name, String.format("%,.2fMB", (float) ms.maxSize / MB), String.format("%,.2fMB", (float) ms.initSize / MB))); } return sb.toString(); }
public String getDeltaString() { StringBuilder sb = new StringBuilder(); sb.append("\n\t [delta memory info] => \n"); sb.append("\t\t "); sb.append(String.format("%-30s | %-30s | %-30s | %-30s | %-30s \n", "NAME", "used_size", "used_percent", "max_used_size", "max_percent")); for (MemoryStatus ms : memoryStatusMap.values()) { sb.append("\t\t "); sb.append(String.format("%-30s | %-30s | %-30s | %-30s | %-30s \n", ms.name, String.format("%,.2f", (float) ms.usedSize / MB) + "MB", String.format("%,.2f", (float) ms.percent) + "%", String.format("%,.2f", (float) ms.maxUsedSize / MB) + "MB", String.format("%,.2f", (float) ms.maxpercent) + "%"));
} return sb.toString(); } }
private class GCStatus { String name; long maxDeltaGCCount = -1; long minDeltaGCCount = -1; long curDeltaGCCount; long totalGCCount = 0; long maxDeltaGCTime = -1; long minDeltaGCTime = -1; long curDeltaGCTime; long totalGCTime = 0;
public void setCurTotalGcCount(long curTotalGcCount) { this.curDeltaGCCount = curTotalGcCount - totalGCCount; this.totalGCCount = curTotalGcCount;
if (maxDeltaGCCount < curDeltaGCCount) { maxDeltaGCCount = curDeltaGCCount; }
if (minDeltaGCCount == -1 || minDeltaGCCount > curDeltaGCCount) { minDeltaGCCount = curDeltaGCCount; } }
public void setCurTotalGcTime(long curTotalGcTime) { this.curDeltaGCTime = curTotalGcTime - totalGCTime; this.totalGCTime = curTotalGcTime;
if (maxDeltaGCTime < curDeltaGCTime) { maxDeltaGCTime = curDeltaGCTime; }
if (minDeltaGCTime == -1 || minDeltaGCTime > curDeltaGCTime) { minDeltaGCTime = curDeltaGCTime; } } }
private class MemoryStatus { String name; long initSize; long maxSize; long commitedSize; long usedSize; float percent; long maxUsedSize = -1; float maxpercent = 0;
void setMaxMinUsedSize(long curUsedSize) { if (maxUsedSize < curUsedSize) { maxUsedSize = curUsedSize; } this.usedSize = curUsedSize; }
void setMaxMinPercent(float curPercent) { if (maxpercent < curPercent) { maxpercent = curPercent; } this.percent = curPercent; } }
}
|