Search in sources :

Example 6 with ClassLoadingMXBean

use of java.lang.management.ClassLoadingMXBean in project karaf by apache.

the class InfoAction method execute.

@Override
public Object execute() throws Exception {
    int maxNameLen;
    RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
    ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
    ClassLoadingMXBean cl = ManagementFactory.getClassLoadingMXBean();
    //
    // print Karaf informations
    //
    maxNameLen = 25;
    System.out.println("Karaf");
    printValue("Karaf version", maxNameLen, System.getProperty("karaf.version"));
    printValue("Karaf home", maxNameLen, System.getProperty("karaf.home"));
    printValue("Karaf base", maxNameLen, System.getProperty("karaf.base"));
    String osgi = getOsgiFramework();
    if (osgi != null) {
        printValue("OSGi Framework", maxNameLen, osgi);
    }
    System.out.println();
    System.out.println("JVM");
    printValue("Java Virtual Machine", maxNameLen, runtime.getVmName() + " version " + runtime.getVmVersion());
    printValue("Version", maxNameLen, System.getProperty("java.version"));
    printValue("Vendor", maxNameLen, runtime.getVmVendor());
    printValue("Pid", maxNameLen, getPid());
    printValue("Uptime", maxNameLen, printDuration(runtime.getUptime()));
    try {
        printValue("Process CPU time", maxNameLen, printDuration(getSunOsValueAsLong(os, "getProcessCpuTime") / 1000000));
    } catch (Throwable t) {
    }
    printValue("Total compile time", maxNameLen, printDuration(ManagementFactory.getCompilationMXBean().getTotalCompilationTime()));
    System.out.println("Threads");
    printValue("Live threads", maxNameLen, Integer.toString(threads.getThreadCount()));
    printValue("Daemon threads", maxNameLen, Integer.toString(threads.getDaemonThreadCount()));
    printValue("Peak", maxNameLen, Integer.toString(threads.getPeakThreadCount()));
    printValue("Total started", maxNameLen, Long.toString(threads.getTotalStartedThreadCount()));
    System.out.println("Memory");
    printValue("Current heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getUsed()));
    printValue("Maximum heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getMax()));
    printValue("Committed heap size", maxNameLen, printSizeInKb(mem.getHeapMemoryUsage().getCommitted()));
    printValue("Pending objects", maxNameLen, Integer.toString(mem.getObjectPendingFinalizationCount()));
    for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
        String val = "Name = '" + gc.getName() + "', Collections = " + gc.getCollectionCount() + ", Time = " + printDuration(gc.getCollectionTime());
        printValue("Garbage collector", maxNameLen, val);
    }
    System.out.println("Classes");
    printValue("Current classes loaded", maxNameLen, printLong(cl.getLoadedClassCount()));
    printValue("Total classes loaded", maxNameLen, printLong(cl.getTotalLoadedClassCount()));
    printValue("Total classes unloaded", maxNameLen, printLong(cl.getUnloadedClassCount()));
    System.out.println("Operating system");
    printValue("Name", maxNameLen, os.getName() + " version " + os.getVersion());
    printValue("Architecture", maxNameLen, os.getArch());
    printValue("Processors", maxNameLen, Integer.toString(os.getAvailableProcessors()));
    try {
        printValue("Total physical memory", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getTotalPhysicalMemorySize")));
        printValue("Free physical memory", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getFreePhysicalMemorySize")));
        printValue("Committed virtual memory", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getCommittedVirtualMemorySize")));
        printValue("Total swap space", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getTotalSwapSpaceSize")));
        printValue("Free swap space", maxNameLen, printSizeInKb(getSunOsValueAsLong(os, "getFreeSwapSpaceSize")));
    } catch (Throwable t) {
    }
    //Display Information from external information providers.
    Map<String, Map<Object, Object>> properties = new HashMap<>();
    if (infoProviders != null) {
        // dump all properties to Map, KARAF-425
        for (InfoProvider provider : infoProviders) {
            if (!properties.containsKey(provider.getName())) {
                properties.put(provider.getName(), new Properties());
            }
            properties.get(provider.getName()).putAll(provider.getProperties());
        }
        List<String> sections = new ArrayList<>(properties.keySet());
        Collections.sort(sections);
        for (String section : sections) {
            List<Object> keys = new ArrayList<>(properties.get(section).keySet());
            if (keys.size() > 0) {
                System.out.println(section);
                keys.sort(Comparator.comparing(String::valueOf));
                for (Object key : keys) {
                    printValue(String.valueOf(key), maxNameLen, String.valueOf(properties.get(section).get(key)));
                }
            }
        }
    }
    return null;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) HashMap(java.util.HashMap) InfoProvider(org.apache.karaf.shell.commands.info.InfoProvider) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) ArrayList(java.util.ArrayList) RuntimeMXBean(java.lang.management.RuntimeMXBean) Properties(java.util.Properties) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) HashMap(java.util.HashMap) Map(java.util.Map) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 7 with ClassLoadingMXBean

use of java.lang.management.ClassLoadingMXBean in project hazelcast by hazelcast.

the class TimedMemberStateFactoryHelper method createRuntimeProps.

static void createRuntimeProps(MemberStateImpl memberState) {
    Runtime runtime = Runtime.getRuntime();
    ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
    ClassLoadingMXBean clMxBean = ManagementFactory.getClassLoadingMXBean();
    MemoryMXBean memoryMxBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage heapMemory = memoryMxBean.getHeapMemoryUsage();
    MemoryUsage nonHeapMemory = memoryMxBean.getNonHeapMemoryUsage();
    Map<String, Long> map = new HashMap<String, Long>();
    map.put("runtime.availableProcessors", Integer.valueOf(runtime.availableProcessors()).longValue());
    map.put("date.startTime", runtimeMxBean.getStartTime());
    map.put("seconds.upTime", runtimeMxBean.getUptime());
    map.put("memory.maxMemory", runtime.maxMemory());
    map.put("memory.freeMemory", runtime.freeMemory());
    map.put("memory.totalMemory", runtime.totalMemory());
    map.put("memory.heapMemoryMax", heapMemory.getMax());
    map.put("memory.heapMemoryUsed", heapMemory.getUsed());
    map.put("memory.nonHeapMemoryMax", nonHeapMemory.getMax());
    map.put("memory.nonHeapMemoryUsed", nonHeapMemory.getUsed());
    map.put("runtime.totalLoadedClassCount", clMxBean.getTotalLoadedClassCount());
    map.put("runtime.loadedClassCount", Integer.valueOf(clMxBean.getLoadedClassCount()).longValue());
    map.put("runtime.unloadedClassCount", clMxBean.getUnloadedClassCount());
    map.put("runtime.totalStartedThreadCount", threadMxBean.getTotalStartedThreadCount());
    map.put("runtime.threadCount", Integer.valueOf(threadMxBean.getThreadCount()).longValue());
    map.put("runtime.peakThreadCount", Integer.valueOf(threadMxBean.getPeakThreadCount()).longValue());
    map.put("runtime.daemonThreadCount", Integer.valueOf(threadMxBean.getDaemonThreadCount()).longValue());
    OperatingSystemMXBean osMxBean = ManagementFactory.getOperatingSystemMXBean();
    map.put("osMemory.freePhysicalMemory", get(osMxBean, "getFreePhysicalMemorySize", 0L));
    map.put("osMemory.committedVirtualMemory", get(osMxBean, "getCommittedVirtualMemorySize", 0L));
    map.put("osMemory.totalPhysicalMemory", get(osMxBean, "getTotalPhysicalMemorySize", 0L));
    map.put("osSwap.freeSwapSpace", get(osMxBean, "getFreeSwapSpaceSize", 0L));
    map.put("osSwap.totalSwapSpace", get(osMxBean, "getTotalSwapSpaceSize", 0L));
    map.put("os.maxFileDescriptorCount", get(osMxBean, "getMaxFileDescriptorCount", 0L));
    map.put("os.openFileDescriptorCount", get(osMxBean, "getOpenFileDescriptorCount", 0L));
    map.put("os.processCpuLoad", get(osMxBean, "getProcessCpuLoad", -1L));
    map.put("os.systemLoadAverage", get(osMxBean, "getSystemLoadAverage", -1L));
    map.put("os.systemCpuLoad", get(osMxBean, "getSystemCpuLoad", -1L));
    map.put("os.processCpuTime", get(osMxBean, "getProcessCpuTime", 0L));
    map.put("os.availableProcessors", get(osMxBean, "getAvailableProcessors", 0L));
    memberState.setRuntimeProps(map);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) MemoryMXBean(java.lang.management.MemoryMXBean) HashMap(java.util.HashMap) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) MemoryUsage(java.lang.management.MemoryUsage) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 8 with ClassLoadingMXBean

use of java.lang.management.ClassLoadingMXBean in project spring-boot by spring-projects.

the class SystemPublicMetrics method addClassLoadingMetrics.

/**
	 * Add class loading metrics.
	 * @param result the result
	 */
protected void addClassLoadingMetrics(Collection<Metric<?>> result) {
    ClassLoadingMXBean classLoadingMxBean = ManagementFactory.getClassLoadingMXBean();
    result.add(new Metric<>("classes", (long) classLoadingMxBean.getLoadedClassCount()));
    result.add(new Metric<>("classes.loaded", classLoadingMxBean.getTotalLoadedClassCount()));
    result.add(new Metric<>("classes.unloaded", classLoadingMxBean.getUnloadedClassCount()));
}
Also used : ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean)

Example 9 with ClassLoadingMXBean

use of java.lang.management.ClassLoadingMXBean in project commons by twitter.

the class JvmStats method export.

/**
   * Exports stats related to the JVM and runtime environment.
   */
public static void export() {
    final OperatingSystemMXBean osMbean = ManagementFactory.getOperatingSystemMXBean();
    if (osMbean instanceof com.sun.management.OperatingSystemMXBean) {
        final com.sun.management.OperatingSystemMXBean sunOsMbean = (com.sun.management.OperatingSystemMXBean) osMbean;
        Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("system_free_physical_memory_mb") {

            @Override
            public Long read() {
                return sunOsMbean.getFreePhysicalMemorySize() / BYTES_PER_MB;
            }
        }).add(new StatImpl<Long>("system_free_swap_mb") {

            @Override
            public Long read() {
                return sunOsMbean.getFreeSwapSpaceSize() / BYTES_PER_MB;
            }
        }).add(Rate.of(new StatImpl<Long>("process_cpu_time_nanos") {

            @Override
            public Long read() {
                return sunOsMbean.getProcessCpuTime();
            }
        }).withName("process_cpu_cores_utilized").withScaleFactor(SECS_PER_NANO).build()).build());
    }
    if (osMbean instanceof com.sun.management.UnixOperatingSystemMXBean) {
        final com.sun.management.UnixOperatingSystemMXBean unixOsMbean = (com.sun.management.UnixOperatingSystemMXBean) osMbean;
        Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("process_max_fd_count") {

            @Override
            public Long read() {
                return unixOsMbean.getMaxFileDescriptorCount();
            }
        }).add(new StatImpl<Long>("process_open_fd_count") {

            @Override
            public Long read() {
                return unixOsMbean.getOpenFileDescriptorCount();
            }
        }).build());
    }
    final Runtime runtime = Runtime.getRuntime();
    final ClassLoadingMXBean classLoadingBean = ManagementFactory.getClassLoadingMXBean();
    final MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    final RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
    Stats.exportAll(ImmutableList.<Stat<? extends Number>>builder().add(new StatImpl<Long>("jvm_time_ms") {

        @Override
        public Long read() {
            return System.currentTimeMillis();
        }
    }).add(new StatImpl<Integer>("jvm_available_processors") {

        @Override
        public Integer read() {
            return runtime.availableProcessors();
        }
    }).add(new StatImpl<Long>("jvm_memory_free_mb") {

        @Override
        public Long read() {
            return runtime.freeMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_max_mb") {

        @Override
        public Long read() {
            return runtime.maxMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_mb_total") {

        @Override
        public Long read() {
            return runtime.totalMemory() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Integer>("jvm_class_loaded_count") {

        @Override
        public Integer read() {
            return classLoadingBean.getLoadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_class_total_loaded_count") {

        @Override
        public Long read() {
            return classLoadingBean.getTotalLoadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_class_unloaded_count") {

        @Override
        public Long read() {
            return classLoadingBean.getUnloadedClassCount();
        }
    }).add(new StatImpl<Long>("jvm_gc_collection_time_ms") {

        @Override
        public Long read() {
            long collectionTimeMs = 0;
            for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
                collectionTimeMs += bean.getCollectionTime();
            }
            return collectionTimeMs;
        }
    }).add(new StatImpl<Long>("jvm_gc_collection_count") {

        @Override
        public Long read() {
            long collections = 0;
            for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
                collections += bean.getCollectionCount();
            }
            return collections;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_used") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getUsed() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_committed") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getCommitted() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_heap_mb_max") {

        @Override
        public Long read() {
            return memoryBean.getHeapMemoryUsage().getMax() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_used") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getUsed() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_committed") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getCommitted() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_memory_non_heap_mb_max") {

        @Override
        public Long read() {
            return memoryBean.getNonHeapMemoryUsage().getMax() / BYTES_PER_MB;
        }
    }).add(new StatImpl<Long>("jvm_uptime_secs") {

        @Override
        public Long read() {
            return runtimeMXBean.getUptime() / 1000;
        }
    }).add(new StatImpl<Double>("system_load_avg") {

        @Override
        public Double read() {
            return osMbean.getSystemLoadAverage();
        }
    }).add(new StatImpl<Integer>("jvm_threads_peak") {

        @Override
        public Integer read() {
            return threads.getPeakThreadCount();
        }
    }).add(new StatImpl<Long>("jvm_threads_started") {

        @Override
        public Long read() {
            return threads.getTotalStartedThreadCount();
        }
    }).add(new StatImpl<Integer>("jvm_threads_daemon") {

        @Override
        public Integer read() {
            return threads.getDaemonThreadCount();
        }
    }).add(new StatImpl<Integer>("jvm_threads_active") {

        @Override
        public Integer read() {
            return threads.getThreadCount();
        }
    }).build());
    // Export per memory pool gc time and cycle count like Ostrich
    // This is based on code in Bridcage: https://cgit.twitter.biz/birdcage/tree/ \
    // ostrich/src/main/scala/com/twitter/ostrich/stats/StatsCollection.scala
    Stats.exportAll(Iterables.transform(ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() {

        @Override
        public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) {
            return new StatImpl<Long>("jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_count") {

                @Override
                public Long read() {
                    return gcMXBean.getCollectionCount();
                }
            };
        }
    }));
    Stats.exportAll(Iterables.transform(ManagementFactory.getGarbageCollectorMXBeans(), new Function<GarbageCollectorMXBean, Stat<? extends Number>>() {

        @Override
        public Stat<? extends Number> apply(final GarbageCollectorMXBean gcMXBean) {
            return new StatImpl<Long>("jvm_gc_" + Stats.normalizeName(gcMXBean.getName()) + "_collection_time_ms") {

                @Override
                public Long read() {
                    return gcMXBean.getCollectionTime();
                }
            };
        }
    }));
    Stats.exportString(new StatImpl<String>("jvm_input_arguments") {

        @Override
        public String read() {
            return runtimeMXBean.getInputArguments().toString();
        }
    });
    for (final String property : System.getProperties().stringPropertyNames()) {
        Stats.exportString(new StatImpl<String>("jvm_prop_" + Stats.normalizeName(property)) {

            @Override
            public String read() {
                return System.getProperty(property);
            }
        });
    }
    for (final Map.Entry<String, String> environmentVariable : System.getenv().entrySet()) {
        Stats.exportString(new StatImpl<String>("system_env_" + Stats.normalizeName(environmentVariable.getKey())) {

            @Override
            public String read() {
                return environmentVariable.getValue();
            }
        });
    }
}
Also used : GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) Function(com.google.common.base.Function) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) ThreadMXBean(java.lang.management.ThreadMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) Map(java.util.Map) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean)

Example 10 with ClassLoadingMXBean

use of java.lang.management.ClassLoadingMXBean in project opennms by OpenNMS.

the class JavaReportPlugin method getEntries.

@Override
public Map<String, Resource> getEntries() {
    final TreeMap<String, Resource> map = new TreeMap<String, Resource>();
    map.put("Class Version", getResourceFromProperty("java.class.version"));
    map.put("Compiler", getResourceFromProperty("java.compiler"));
    map.put("Home", getResourceFromProperty("java.home"));
    map.put("Version", getResourceFromProperty("java.version"));
    map.put("Vendor", getResourceFromProperty("java.vendor"));
    map.put("VM Version", getResourceFromProperty("java.vm.version"));
    map.put("VM Name", getResourceFromProperty("java.vm.name"));
    MemoryMXBean memoryBean = getBean(ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
    if (memoryBean == null) {
        LOG.info("falling back to local VM MemoryMXBean");
        memoryBean = ManagementFactory.getMemoryMXBean();
    }
    addGetters(memoryBean, map);
    RuntimeMXBean runtimeBean = getBean(ManagementFactory.RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
    if (runtimeBean == null) {
        LOG.info("falling back to local VM RuntimeMXBean");
        runtimeBean = ManagementFactory.getRuntimeMXBean();
    }
    addGetters(runtimeBean, map);
    ClassLoadingMXBean classBean = getBean(ManagementFactory.CLASS_LOADING_MXBEAN_NAME, ClassLoadingMXBean.class);
    if (classBean == null) {
        LOG.info("falling back to local VM ClassLoadingMXBean");
        classBean = ManagementFactory.getClassLoadingMXBean();
    }
    addGetters(classBean, map);
    return map;
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) Resource(org.springframework.core.io.Resource) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) RuntimeMXBean(java.lang.management.RuntimeMXBean) TreeMap(java.util.TreeMap)

Aggregations

ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)12 MemoryMXBean (java.lang.management.MemoryMXBean)5 RuntimeMXBean (java.lang.management.RuntimeMXBean)5 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)4 ThreadMXBean (java.lang.management.ThreadMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)3 MemoryUsage (java.lang.management.MemoryUsage)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Function (com.google.common.base.Function)1 UnixOperatingSystemMXBean (com.sun.management.UnixOperatingSystemMXBean)1 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 MBeanInfo (javax.management.MBeanInfo)1 ObjectName (javax.management.ObjectName)1 Gauge (org.apache.flink.metrics.Gauge)1 InfoProvider (org.apache.karaf.shell.commands.info.InfoProvider)1 Resource (org.springframework.core.io.Resource)1