Переглянути джерело

添加获取 JVM 信息和状态的工具类

reghao 2 роки тому
батько
коміт
f1d68ac1dd

+ 75 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/JVM.java

@@ -0,0 +1,75 @@
+package cn.reghao.jutil.jdk.jvm;
+
+import cn.reghao.jutil.jdk.jvm.model.*;
+import cn.reghao.jutil.jdk.converter.DateTimeConverter;
+
+import java.lang.management.*;
+import java.util.*;
+
+/**
+ * JVM 信息和状态
+ *
+ * @author reghao
+ * @date 2020-09-10 18:31:16
+ */
+public class JVM {
+    private final RuntimeMXBean rtBean;
+    private final ClassLoadingMXBean classLoadingBean;
+    private final ThreadMXBean threadBean;
+    private final MemoryMXBean memoryBean;
+    private final List<MemoryPoolMXBean> memoryPoolBeans;
+    private final List<BufferPoolMXBean> bufferPoolBeans;
+    private final List<GarbageCollectorMXBean> gcBeans;
+
+    public JVM() {
+        this.rtBean = ManagementFactory.getRuntimeMXBean();
+        this.classLoadingBean = ManagementFactory.getClassLoadingMXBean();
+        this.threadBean = ManagementFactory.getThreadMXBean();
+        this.memoryBean = ManagementFactory.getMemoryMXBean();
+        this.memoryPoolBeans = ManagementFactory.getMemoryPoolMXBeans();
+        this.bufferPoolBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
+        this.gcBeans = ManagementFactory.getGarbageCollectorMXBeans();
+    }
+
+    public JvmInfo info() {
+        JvmInfo jvmInfo = new JvmInfo();
+        jvmInfo.setOsName(System.getProperty("os.name"));
+        jvmInfo.setOsVersion(System.getProperty("os.version"));
+
+        jvmInfo.setJvmName(System.getProperty("java.vm.name"));
+        jvmInfo.setJvmVersion(System.getProperty("java.vm.version"));
+
+        // TODO pid 不一定准确
+        jvmInfo.setJvmPid(Integer.parseInt(rtBean.getName().split("@")[0]));
+        jvmInfo.setJvmStartTime(DateTimeConverter.msTimestampFormat(rtBean.getStartTime()));
+
+        return jvmInfo;
+    }
+
+    private List<BufferPoolStat> bufferPoolStat() {
+        List<BufferPoolStat> list = new ArrayList<>();
+        bufferPoolBeans.forEach(bufferPoolBean -> list.add(new BufferPoolStat(bufferPoolBean)));
+        return list;
+    }
+
+    private Map<String, MemoryPoolStat> memoryPoolStat() {
+        Map<String, MemoryPoolStat> map = new HashMap<>();
+        memoryPoolBeans.forEach(
+                // TODO name 是否唯一?
+                memoryPoolBean -> map.put(memoryPoolBean.getName(), new MemoryPoolStat(memoryPoolBean)));
+        return map;
+    }
+
+    private List<GarbageCollectorStat> gcStat() {
+        List<GarbageCollectorStat> list = new ArrayList<>();
+        gcBeans.forEach(gcBean -> list.add(new GarbageCollectorStat(gcBean)));
+        return list;
+    }
+
+    private List<ThreadStat> threadStat() {
+        List<ThreadStat> list = new ArrayList<>();
+        Arrays.stream(threadBean.getThreadInfo(threadBean.getAllThreadIds()))
+                .forEach(threadInfo -> list.add(new ThreadStat(threadInfo)));
+        return list;
+    }
+}

+ 41 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/model/BufferPoolStat.java

@@ -0,0 +1,41 @@
+package cn.reghao.jutil.jdk.jvm.model;
+
+import cn.reghao.jutil.jdk.converter.ByteConverter;
+import cn.reghao.jutil.jdk.converter.ByteType;
+
+import java.lang.management.BufferPoolMXBean;
+
+/**
+ * @author reghao
+ * @date 2020-10-21 15:49:56
+ */
+public class BufferPoolStat {
+    private String poolName;
+    private long bufferCount;
+    private String totalMemory;
+    private String usedMemory;
+
+    public BufferPoolStat(BufferPoolMXBean bufferPoolBean) {
+        ByteConverter converter = new ByteConverter();
+        this.poolName = bufferPoolBean.getName();
+        this.bufferCount = bufferPoolBean.getCount();
+        this.totalMemory = converter.convert(ByteType.Bytes, bufferPoolBean.getTotalCapacity());
+        this.usedMemory = converter.convert(ByteType.Bytes, bufferPoolBean.getMemoryUsed());
+    }
+
+    public String getPoolName() {
+        return poolName;
+    }
+
+    public long getBufferCount() {
+        return bufferCount;
+    }
+
+    public String getTotalMemory() {
+        return totalMemory;
+    }
+
+    public String getUsedMemory() {
+        return usedMemory;
+    }
+}

+ 41 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/model/GarbageCollectorStat.java

@@ -0,0 +1,41 @@
+package cn.reghao.jutil.jdk.jvm.model;
+
+import java.lang.management.GarbageCollectorMXBean;
+import java.util.Arrays;
+
+/**
+ * @author reghao
+ * @date 2020-10-21 15:49:56
+ */
+public class GarbageCollectorStat {
+    private String gcName;
+    // GC 管理的内存池
+    private String memoryPools;
+    // 自 JVM 启动后总共发生的 GC 次数
+    private long gcCount;
+    // 自 JVM 启动后 GC 总共耗费的时间
+    private long gcTime;
+
+    public GarbageCollectorStat(GarbageCollectorMXBean gcBean) {
+        this.gcName = gcBean.getName();
+        this.memoryPools = Arrays.toString(gcBean.getMemoryPoolNames()).replace("[", "").replace("]", "");
+        this.gcCount = gcBean.getCollectionCount();
+        this.gcTime = gcBean.getCollectionCount();
+    }
+
+    public String getGcName() {
+        return gcName;
+    }
+
+    public String getMemoryPools() {
+        return memoryPools;
+    }
+
+    public long getGcCount() {
+        return gcCount;
+    }
+
+    public long getGcTime() {
+        return gcTime;
+    }
+}

+ 62 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/model/JvmInfo.java

@@ -0,0 +1,62 @@
+package cn.reghao.jutil.jdk.jvm.model;
+
+/**
+ * @author reghao
+ * @date 2020-10-21 10:43:37
+ */
+public class JvmInfo {
+    private String osName;
+    private String osVersion;
+    private String jvmName;
+    private String jvmVersion;
+    private int jvmPid;
+    private String jvmStartTime;
+
+    public void setOsName(String osName) {
+        this.osName = osName;
+    }
+
+    public String getOsName() {
+        return osName;
+    }
+
+    public void setOsVersion(String osVersion) {
+        this.osVersion = osVersion;
+    }
+
+    public String getOsVersion() {
+        return osVersion;
+    }
+
+    public void setJvmName(String jvmName) {
+        this.jvmName = jvmName;
+    }
+
+    public String getJvmName() {
+        return jvmName;
+    }
+
+    public void setJvmVersion(String jvmVersion) {
+        this.jvmVersion = jvmVersion;
+    }
+
+    public String getJvmVersion() {
+        return jvmVersion;
+    }
+
+    public void setJvmPid(int jvmPid) {
+        this.jvmPid = jvmPid;
+    }
+
+    public int getJvmPid() {
+        return jvmPid;
+    }
+
+    public void setJvmStartTime(String jvmStartTime) {
+        this.jvmStartTime = jvmStartTime;
+    }
+
+    public String getJvmStartTime() {
+        return jvmStartTime;
+    }
+}

+ 36 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/model/JvmStat.java

@@ -0,0 +1,36 @@
+package cn.reghao.jutil.jdk.jvm.model;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2020-10-22 15:06:29
+ */
+public class JvmStat {
+    /* 类加载情况 */
+    private long jvmClassesLoaded;
+    private long jvmClassesUnloaded;
+    private long jvmClassesTotal;
+
+    /* 堆区和非堆区总体内存使用情况 */
+    private String jvmMemoryHeapInit;
+    private String jvmMemoryHeapMax;
+    private String jvmMemoryHeapUsed;
+    private String jvmMemoryHeapCommitted;
+
+    private String jvmMemoryNonheapInit;
+    private String jvmMemoryNonheapMax;
+    private String jvmMemoryNonheapUsed;
+    private String jvmMemoryNonheapCommitted;
+
+    private List<BufferPoolStat> jvmBufferPools;
+    private Map<String, MemoryPoolStat> jvmMemoryPools;
+    private List<GarbageCollectorStat> jvmGarbageCollectors;
+
+    /* 线程使用情况 */
+    private int jvmThreadsLive;
+    private int jvmThreadsDaemon;
+    private int jvmThreadsPeak;
+    private List<ThreadStat> jvmThreads;
+}

+ 29 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/model/MemoryPoolStat.java

@@ -0,0 +1,29 @@
+package cn.reghao.jutil.jdk.jvm.model;
+
+import java.lang.management.MemoryPoolMXBean;
+import java.lang.management.MemoryUsage;
+
+/**
+ * @author reghao
+ * @date 2020-10-21 15:49:56
+ */
+public class MemoryPoolStat {
+    private String poolName;
+    private String memoryType;
+    private MemoryStat poolUsage;
+    // 内存池峰值时的使用情况
+    private MemoryStat poolPeakUsage;
+    // 最近一次 GC 后的内存池的使用情况
+    private MemoryStat afterGcUsage;
+
+    public MemoryPoolStat(MemoryPoolMXBean memoryPoolBean) {
+        this.poolName = memoryPoolBean.getName();
+        this.memoryType = memoryPoolBean.getType().name();
+        this.poolUsage = new MemoryStat(memoryPoolBean.getUsage());
+        this.poolPeakUsage = new MemoryStat(memoryPoolBean.getPeakUsage());
+        MemoryUsage collectionMemoryUsage = memoryPoolBean.getCollectionUsage();
+        if (collectionMemoryUsage != null) {
+            this.afterGcUsage = new MemoryStat(collectionMemoryUsage);
+        }
+    }
+}

+ 25 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/model/MemoryStat.java

@@ -0,0 +1,25 @@
+package cn.reghao.jutil.jdk.jvm.model;
+
+import cn.reghao.jutil.jdk.converter.ByteConverter;
+import cn.reghao.jutil.jdk.converter.ByteType;
+
+import java.lang.management.MemoryUsage;
+
+/**
+ * @author reghao
+ * @date 2020-10-21 15:49:56
+ */
+public class MemoryStat {
+    private String init;
+    private String max;
+    private String used;
+    private String committed;
+
+    public MemoryStat(MemoryUsage memoryUsage) {
+        ByteConverter convert = new ByteConverter();
+        this.init = convert.convertStr(ByteType.Bytes, ByteType.MiB, memoryUsage.getInit());
+        this.max = convert.convertStr(ByteType.Bytes, ByteType.MiB, memoryUsage.getMax());
+        this.used = convert.convertStr(ByteType.Bytes, ByteType.MiB, memoryUsage.getUsed());
+        this.committed = convert.convertStr(ByteType.Bytes, ByteType.MiB, memoryUsage.getCommitted());
+    }
+}

+ 33 - 0
jdk/src/main/java/cn/reghao/jutil/jdk/jvm/model/ThreadStat.java

@@ -0,0 +1,33 @@
+package cn.reghao.jutil.jdk.jvm.model;
+
+import java.lang.management.ThreadInfo;
+
+/**
+ * @author reghao
+ * @date 2020-10-21 17:31:47
+ */
+public class ThreadStat {
+    private String threadName;
+    private String threadState;
+    private long blockedTime;
+    private long blockedCount;
+    private long waitedTime;
+    private long waitedCount;
+    private String lockName;
+    private String lockOwnerName;
+    private int lockedMonitorCount;
+    private int lockedSynchronizerCount;
+
+    public ThreadStat(ThreadInfo threadInfo) {
+        this.threadName = threadInfo.getThreadName();
+        this.threadState = threadInfo.getThreadState().name();
+        this.blockedTime = threadInfo.getBlockedTime();
+        this.blockedCount = threadInfo.getBlockedCount();
+        this.waitedTime = threadInfo.getWaitedTime();
+        this.waitedCount = threadInfo.getWaitedCount();
+        this.lockName = threadInfo.getLockName();
+        this.lockOwnerName = threadInfo.getLockOwnerName();
+        this.lockedMonitorCount = threadInfo.getLockedMonitors().length;
+        this.lockedSynchronizerCount = threadInfo.getLockedSynchronizers().length;
+    }
+}