Arthas 查看当前JVM信息

系列

  • Arthas入门篇
  • Arthas功能介绍
  • Arthas 启动过程分析
  • Arthas使用Idea调试
  • Arthas Command处理流程
  • Arthas类查找和反编译原理
  • Arthas内存动态编译原理
  • Arthas动态重新加载类
  • Arthas导出加载类
  • Arthas classloader类加载器

开篇

  • Arthas提供jvm命令获取当前jvm的信息,包含JVM的堆信息、垃圾回收信息、线程信息等。
  • jvm参数的获取基于JMX(Java Management Extensions)技术,JMX是用以提供监控和管理的一种技术。


JMX技术介绍

JMX uses a three-level architecture:

  1. The Probe level - also called the Instrumentation level - contains the probes (called MBeans) instrumenting the resources
  2. The Agent level, or MBeanServer - the core of JMX. It acts as an intermediary between the MBean and the applications.
  3. The Remote Management level enables remote applications to access the MBeanServer through connectors and adaptors. A connector provides full remote access to the MBeanServer API using various communication (RMI, IIOP, JMS, WS-* …), while an adaptor adapts the API to another protocol (SNMP, …) or to Web-based GUI (HTML/HTTP, WML/HTTP, …).
Arthas 查看当前JVM信息_第1张图片

Managed Bean

  • A managed bean - sometimes simply referred to as an MBean - is a type of JavaBean, created with dependency injection. Managed Beans are particularly used in the Java Management Extensions technology. But with Java EE 6, the specification provides for a more detailed meaning of a managed bean.

  • The MBean represents a resource running in the Java virtual machine, such as an application or a Java EE technical service (transactional monitor, JDBC driver, etc.). They can be used for collecting statistics on concerns like performance, resources usage, or problems (pull); for getting and setting application configurations or properties (push/pull); and notifying events like faults or state changes (push).

Types
There are two basic types of MBean:

  • Standard MBeans implement a business interface containing setters and getters for the attributes and the operations (i.e., methods).

  • Dynamic MBeans implement the javax.management.DynamicMBean interface that provides a way to list the attributes and operations, and to get and set the attribute values.

  • Additional types are Open MBeans, Model MBeans and Monitor MBeans. Open MBeans are dynamic MBeans that rely on the basic data types. They are self-explanatory and more user-friendly. Model MBeans are dynamic MBeans that can be configured during runtime. A generic MBean class is also provided for dynamically configuring the resources during program runtime.

  • An MXBean (Platform MBean) is a special type of MBean that reifies Java Virtual Machine subsystems such as garbage collection, JIT compilation, memory pools, multi-threading, etc.

通过MXBean的接口获取对应的各类jvm相关的采集参数。


源码解析

public class JvmCommand extends AnnotatedCommand {

    private final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    private final ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();
    private final CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
    private final Collection garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
    private final Collection memoryManagerMXBeans = ManagementFactory.getMemoryManagerMXBeans();
    private final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    //    private final Collection memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    private final OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();


    private void addRuntimeInfo(JvmModel jvmModel) {
        String bootClassPath = "";
        try {
            bootClassPath = runtimeMXBean.getBootClassPath();
        } catch (Exception e) {
            // under jdk9 will throw UnsupportedOperationException, ignore
        }
        String group = "RUNTIME";
        jvmModel.addItem(group,"MACHINE-NAME", runtimeMXBean.getName());
        jvmModel.addItem(group, "JVM-START-TIME", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(runtimeMXBean.getStartTime())));
        jvmModel.addItem(group, "MANAGEMENT-SPEC-VERSION", runtimeMXBean.getManagementSpecVersion());
        jvmModel.addItem(group, "SPEC-NAME", runtimeMXBean.getSpecName());
        jvmModel.addItem(group, "SPEC-VENDOR", runtimeMXBean.getSpecVendor());
        jvmModel.addItem(group, "SPEC-VERSION", runtimeMXBean.getSpecVersion());
        jvmModel.addItem(group, "VM-NAME", runtimeMXBean.getVmName());
        jvmModel.addItem(group, "VM-VENDOR", runtimeMXBean.getVmVendor());
        jvmModel.addItem(group, "VM-VERSION", runtimeMXBean.getVmVersion());
        jvmModel.addItem(group, "INPUT-ARGUMENTS", runtimeMXBean.getInputArguments());
        jvmModel.addItem(group, "CLASS-PATH", runtimeMXBean.getClassPath());
        jvmModel.addItem(group, "BOOT-CLASS-PATH", bootClassPath);
        jvmModel.addItem(group, "LIBRARY-PATH", runtimeMXBean.getLibraryPath());
    }

    private void addClassLoading(JvmModel jvmModel) {
        String group = "CLASS-LOADING";
        jvmModel.addItem(group, "LOADED-CLASS-COUNT", classLoadingMXBean.getLoadedClassCount());
        jvmModel.addItem(group, "TOTAL-LOADED-CLASS-COUNT", classLoadingMXBean.getTotalLoadedClassCount());
        jvmModel.addItem(group, "UNLOADED-CLASS-COUNT", classLoadingMXBean.getUnloadedClassCount());
        jvmModel.addItem(group, "IS-VERBOSE", classLoadingMXBean.isVerbose());
    }

    private void addCompilation(JvmModel jvmModel) {
        String group = "COMPILATION";
        jvmModel.addItem(group, "NAME", compilationMXBean.getName());
        if (compilationMXBean.isCompilationTimeMonitoringSupported()) {
            jvmModel.addItem(group, "TOTAL-COMPILE-TIME", compilationMXBean.getTotalCompilationTime(), "time (ms)");
        }
    }

    private void addGarbageCollectors(JvmModel jvmModel) {
        String group = "GARBAGE-COLLECTORS";
        for (GarbageCollectorMXBean gcMXBean : garbageCollectorMXBeans) {
            Map gcInfo = new LinkedHashMap();
            gcInfo.put("name", gcMXBean.getName());
            gcInfo.put("collectionCount", gcMXBean.getCollectionCount());
            gcInfo.put("collectionTime", gcMXBean.getCollectionTime());

            jvmModel.addItem(group, gcMXBean.getName(), gcInfo, "count/time (ms)");
        }
    }

    private void addMemoryManagers(JvmModel jvmModel) {
        String group = "MEMORY-MANAGERS";
        for (final MemoryManagerMXBean memoryManagerMXBean : memoryManagerMXBeans) {
            if (memoryManagerMXBean.isValid()) {
                final String name = memoryManagerMXBean.isValid()
                        ? memoryManagerMXBean.getName()
                        : memoryManagerMXBean.getName() + "(Invalid)";
                jvmModel.addItem(group, name, memoryManagerMXBean.getMemoryPoolNames());
            }
        }
    }

    private void addMemory(JvmModel jvmModel) {
        String group = "MEMORY";
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        Map heapMemoryInfo = getMemoryUsageInfo("heap", heapMemoryUsage);
        jvmModel.addItem(group, "HEAP-MEMORY-USAGE", heapMemoryInfo, "memory in bytes");
        MemoryUsage nonHeapMemoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        Map nonheapMemoryInfo = getMemoryUsageInfo("nonheap", nonHeapMemoryUsage);
        jvmModel.addItem(group,"NO-HEAP-MEMORY-USAGE", nonheapMemoryInfo, "memory in bytes");
        jvmModel.addItem(group,"PENDING-FINALIZE-COUNT", memoryMXBean.getObjectPendingFinalizationCount());
    }

    private Map getMemoryUsageInfo(String name, MemoryUsage heapMemoryUsage) {
        Map memoryInfo = new LinkedHashMap();
        memoryInfo.put("name", name);
        memoryInfo.put("init", heapMemoryUsage.getInit());
        memoryInfo.put("used", heapMemoryUsage.getUsed());
        memoryInfo.put("committed", heapMemoryUsage.getCommitted());
        memoryInfo.put("max", heapMemoryUsage.getMax());
        return memoryInfo;
    }

    private void addOperatingSystem(JvmModel jvmModel) {
        String group = "OPERATING-SYSTEM";
        jvmModel.addItem(group,"OS", operatingSystemMXBean.getName())
                .addItem(group,"ARCH", operatingSystemMXBean.getArch())
                .addItem(group,"PROCESSORS-COUNT", operatingSystemMXBean.getAvailableProcessors())
                .addItem(group,"LOAD-AVERAGE", operatingSystemMXBean.getSystemLoadAverage())
                .addItem(group,"VERSION", operatingSystemMXBean.getVersion());
    }

    private void addThread(JvmModel jvmModel) {
        String group = "THREAD";
        jvmModel.addItem(group, "COUNT", threadMXBean.getThreadCount())
                .addItem(group, "DAEMON-COUNT", threadMXBean.getDaemonThreadCount())
                .addItem(group, "PEAK-COUNT", threadMXBean.getPeakThreadCount())
                .addItem(group, "STARTED-COUNT", threadMXBean.getTotalStartedThreadCount())
                .addItem(group, "DEADLOCK-COUNT",getDeadlockedThreadsCount(threadMXBean));
    }
}
  • ManagementFactory返回各类MXBean对象,通过MXBean获取各类采集信息。

你可能感兴趣的:(Arthas 查看当前JVM信息)