| 1 | package org.jtoolkit.essence.utils; |
| 2 | |
| 3 | import org.jtoolkit.essence.app.pojo.DataValue; |
| 4 | import org.jtoolkit.essence.app.pojo.impl.DataValueClass; |
| 5 | import static sun.management.ManagementFactory.*; |
| 6 | |
| 7 | import java.lang.management.*; |
| 8 | |
| 9 | public class JvmMetrics extends DataValue { |
| 10 | private static final Runtime runtime = Runtime.getRuntime(); |
| 11 | private static final String PROCESS_CPU_TIME = "processCpuTime"; |
| 12 | private static final String COLLECTION_COUNT = "collectionCount"; |
| 13 | private static final String COLLECTION_TIME = "collectionTime"; |
| 14 | |
| 15 | public static final int NOT_AVAILABLE = -1; |
| 16 | |
| 17 | public final long elapseTime; |
| 18 | public final long memoryUsed; |
| 19 | public final long currentThreadCpuTime; |
| 20 | public final int peakThreadCount; |
| 21 | public final long totalStartedThreadCount; |
| 22 | public final long processCpuTime; |
| 23 | public final long committedVirtualMemorySize; |
| 24 | public final int objectPendingFinalizationCount; |
| 25 | public final int copyCollectionCount; |
| 26 | public final int copyCollectionTime; |
| 27 | public final int threadCount; |
| 28 | public final int mbeanCount; |
| 29 | public final double systemLoadAverage; |
| 30 | |
| 31 | public JvmMetrics() { |
| 32 | this(true); |
| 33 | } |
| 34 | |
| 35 | public JvmMetrics(boolean reset) { |
| 36 | elapseTime = System.nanoTime(); |
| 37 | memoryUsed = runtime.totalMemory() - runtime.freeMemory(); |
| 38 | ThreadMXBean threadMXBean = getThreadMXBean(); |
| 39 | currentThreadCpuTime = threadMXBean.getCurrentThreadCpuTime(); |
| 40 | if (reset) |
| 41 | threadMXBean.resetPeakThreadCount(); |
| 42 | peakThreadCount = threadMXBean.getPeakThreadCount(); |
| 43 | totalStartedThreadCount = threadMXBean.getTotalStartedThreadCount(); |
| 44 | OperatingSystemMXBean operatingSystemMXBean = getOperatingSystemMXBean(); |
| 45 | |
| 46 | long processCpuTime; |
| 47 | try { |
| 48 | processCpuTime = ((Number) DataValueClass.getField(operatingSystemMXBean, PROCESS_CPU_TIME)).longValue(); |
| 49 | } catch (Exception ignored) { |
| 50 | processCpuTime = NOT_AVAILABLE; |
| 51 | } |
| 52 | this.processCpuTime = processCpuTime; |
| 53 | |
| 54 | long committedVirtualMemorySize; |
| 55 | try { |
| 56 | committedVirtualMemorySize = ((Number) DataValueClass.getField(operatingSystemMXBean, "committedVirtualMemorySize")).longValue(); |
| 57 | } catch (Exception ignored) { |
| 58 | committedVirtualMemorySize = NOT_AVAILABLE; |
| 59 | } |
| 60 | this.committedVirtualMemorySize = committedVirtualMemorySize; |
| 61 | |
| 62 | double systemLoadAverage; |
| 63 | try { |
| 64 | systemLoadAverage = ((Number) DataValueClass.getField(operatingSystemMXBean, "systemLoadAverage")).doubleValue(); |
| 65 | } catch (Exception ignored) { |
| 66 | systemLoadAverage = NOT_AVAILABLE; |
| 67 | } |
| 68 | this.systemLoadAverage = systemLoadAverage; |
| 69 | |
| 70 | MemoryMXBean memoryMXBean = getMemoryMXBean(); |
| 71 | objectPendingFinalizationCount = memoryMXBean.getObjectPendingFinalizationCount(); |
| 72 | |
| 73 | int copyCollectionCount = NOT_AVAILABLE; |
| 74 | int copyCollectionTime = NOT_AVAILABLE; |
| 75 | for (MemoryManagerMXBean memoryManagerMXBean : getMemoryManagerMXBeans()) { |
| 76 | if ("Copy".equals(memoryManagerMXBean.getName())) |
| 77 | try { |
| 78 | copyCollectionCount = ((Number) DataValueClass.getField(memoryManagerMXBean, COLLECTION_COUNT)).intValue(); |
| 79 | copyCollectionTime = ((Number) DataValueClass.getField(memoryManagerMXBean, COLLECTION_TIME)).intValue(); |
| 80 | } catch (Exception ignored) { |
| 81 | // ignored |
| 82 | } |
| 83 | |
| 84 | if ("Copy".equals(memoryManagerMXBean.getName())) |
| 85 | try { |
| 86 | copyCollectionCount = ((Number) DataValueClass.getField(memoryManagerMXBean, COLLECTION_COUNT)).intValue(); |
| 87 | copyCollectionTime = ((Number) DataValueClass.getField(memoryManagerMXBean, COLLECTION_TIME)).intValue(); |
| 88 | } catch (Exception ignored) { |
| 89 | // ignored |
| 90 | } |
| 91 | } |
| 92 | this.copyCollectionCount = copyCollectionCount; |
| 93 | this.copyCollectionTime = copyCollectionTime; |
| 94 | threadCount = Thread.activeCount(); |
| 95 | Integer mBeanCount = ManagementFactory.getPlatformMBeanServer().getMBeanCount(); |
| 96 | mbeanCount = mBeanCount == null ? NOT_AVAILABLE : mBeanCount; |
| 97 | } |
| 98 | |
| 99 | @SuppressWarnings({"MethodWithTooManyParameters"}) |
| 100 | public JvmMetrics(long elapseTime, long memoryUsed, long currentThreadCpuTime, |
| 101 | int peakThreadCount, long totalStartedThreadCount, long processCpuTime, |
| 102 | long committedVirtualMemorySize, int objectPendingFinalizationCount, |
| 103 | int copyCollectionCount, int copyCollectionTime, int threadCount, int mbeanCount, |
| 104 | double systemLoadAverage) { |
| 105 | this.elapseTime = elapseTime; |
| 106 | this.memoryUsed = memoryUsed; |
| 107 | this.currentThreadCpuTime = currentThreadCpuTime; |
| 108 | this.peakThreadCount = peakThreadCount; |
| 109 | this.totalStartedThreadCount = totalStartedThreadCount; |
| 110 | this.processCpuTime = processCpuTime; |
| 111 | this.committedVirtualMemorySize = committedVirtualMemorySize; |
| 112 | this.objectPendingFinalizationCount = objectPendingFinalizationCount; |
| 113 | this.copyCollectionCount = copyCollectionCount; |
| 114 | this.copyCollectionTime = copyCollectionTime; |
| 115 | this.threadCount = threadCount; |
| 116 | this.mbeanCount = mbeanCount; |
| 117 | this.systemLoadAverage = systemLoadAverage; |
| 118 | } |
| 119 | |
| 120 | public JvmMetrics delta() { |
| 121 | return new JvmMetrics(false).minus(this); |
| 122 | } |
| 123 | |
| 124 | public JvmMetrics minus(JvmMetrics sm) { |
| 125 | return new JvmMetrics( |
| 126 | elapseTime - sm.elapseTime, |
| 127 | memoryUsed - sm.memoryUsed, |
| 128 | currentThreadCpuTime - sm.currentThreadCpuTime, |
| 129 | peakThreadCount - sm.peakThreadCount, |
| 130 | totalStartedThreadCount - sm.totalStartedThreadCount, |
| 131 | processCpuTime - sm.processCpuTime, |
| 132 | committedVirtualMemorySize - sm.committedVirtualMemorySize, |
| 133 | objectPendingFinalizationCount - sm.objectPendingFinalizationCount, |
| 134 | copyCollectionCount - sm.copyCollectionCount, |
| 135 | copyCollectionTime - sm.copyCollectionTime, |
| 136 | threadCount - sm.threadCount, |
| 137 | mbeanCount - sm.mbeanCount, |
| 138 | systemLoadAverage - sm.systemLoadAverage); |
| 139 | } |
| 140 | |
| 141 | @SuppressWarnings({"CallToSystemGC"}) |
| 142 | public static void waitForGC() { |
| 143 | try { |
| 144 | long freeMemory = runtime.freeMemory(); |
| 145 | long target = 256/2; // doubled before first use. |
| 146 | long diff; |
| 147 | do { |
| 148 | System.gc(); |
| 149 | Thread.sleep(25); |
| 150 | long freeMemoryPrev = freeMemory; |
| 151 | freeMemory = runtime.freeMemory(); |
| 152 | diff = freeMemory > freeMemoryPrev ? freeMemory - freeMemoryPrev : freeMemoryPrev - freeMemory; |
| 153 | target <<= 1; |
| 154 | } while (diff > target); |
| 155 | } catch (InterruptedException ignored) { |
| 156 | Thread.currentThread().interrupt(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public double getPercentCPUConsumed() { |
| 161 | return (double) (processCpuTime / (elapseTime / 1000 + 1)) / 10; |
| 162 | } |
| 163 | /* |
| 164 | public static void main(String... args) { |
| 165 | System.out.println(asStringByLine("ClassLoadingMXBean:", getClassLoadingMXBean())); |
| 166 | System.out.println(asStringByLine("CompilationMXBean: ", getCompilationMXBean())); |
| 167 | for (GarbageCollectorMXBean garbageCollectorMXBean : getGarbageCollectorMXBeans()) |
| 168 | System.out.println(asStringByLine("GarbageCollectorMXBean: ", garbageCollectorMXBean)); |
| 169 | for (MemoryManagerMXBean memoryManagerMXBean : getMemoryManagerMXBeans()) |
| 170 | System.out.println(asStringByLine("MemoryManagerMXBean: ", memoryManagerMXBean)); |
| 171 | System.out.println(asStringByLine("MemoryMXBean: ", getMemoryMXBean())); |
| 172 | for (MemoryPoolMXBean memoryPoolMXBean : getMemoryPoolMXBeans()) |
| 173 | System.out.println(asStringByLine("MemoryPoolMXBean: ", memoryPoolMXBean)); |
| 174 | System.out.println(asStringByLine("OperatingSystemMXBean: ", getOperatingSystemMXBean())); |
| 175 | System.out.println(asStringByLine("PlatformMBeanServer: ", java.lang.management.ManagementFactory.getPlatformMBeanServer())); |
| 176 | System.out.println(asStringByLine("RuntimeMXBean: ", getRuntimeMXBean())); |
| 177 | System.out.println(asStringByLine("ThreadMXBean: ", getThreadMXBean())); |
| 178 | |
| 179 | System.out.println(asStringByLine("HotspotClassLoadingMBean: ", getHotspotClassLoadingMBean())); |
| 180 | for(Counter counter : getHotspotClassLoadingMBean().getInternalClassLoadingCounters()) |
| 181 | System.out.println(asStringByLine("HotspotClassLoadingMBean counter "+counter.getName(), counter)); |
| 182 | System.out.println(asStringByLine("HotspotCompilationMBean: ", getHotspotCompilationMBean())); |
| 183 | for(Counter counter : getHotspotCompilationMBean().getInternalCompilerCounters()) |
| 184 | System.out.println(asStringByLine("HotspotCompilationMBean counter "+counter.getName(), counter)); |
| 185 | for(Counter counter: getHotspotMemoryMBean().getInternalMemoryCounters()) |
| 186 | System.out.println(asStringByLine("HotspotMemoryMBean counter "+counter.getName(), counter)); |
| 187 | System.out.println(asStringByLine("HotspotRuntimeMBean: ", getHotspotRuntimeMBean())); |
| 188 | for(Counter counter : getHotspotRuntimeMBean().getInternalRuntimeCounters()) |
| 189 | System.out.println(asStringByLine("HotspotRuntimeMBean counter "+counter.getName(), counter)); |
| 190 | System.out.println(asStringByLine("HotspotThreadMBean: ", getHotspotThreadMBean())); |
| 191 | // System.out.println(asStringByLine("DiagnosticMXBean: ", getDiagnosticMXBean())); |
| 192 | |
| 193 | JvmMetrics sm = new JvmMetrics(); |
| 194 | System.gc(); |
| 195 | Thread.yield(); |
| 196 | System.out.println(asStringByLine("SystemMetric:", sm)); |
| 197 | System.gc(); |
| 198 | Thread.yield(); |
| 199 | System.out.println(asStringByLine("SystemMetric delta:", sm.delta())); |
| 200 | } |
| 201 | */ |
| 202 | } |