Search in sources :

Example 56 with MemoryPoolMXBean

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

the class InfoAction method execute.

@Override
public Object execute() throws Exception {
    int maxNameLen;
    RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
    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 {
        Class<?> sunOS = Class.forName("com.sun.management.OperatingSystemMXBean");
        printValue("Process CPU time", maxNameLen, printDuration(getValueAsLong(sunOS, "getProcessCpuTime") / 1000000));
        printValue("Process CPU load", maxNameLen, fmtDec.format(getValueAsDouble(sunOS, "getProcessCpuLoad")));
        printValue("System CPU load", maxNameLen, fmtDec.format(getValueAsDouble(sunOS, "getSystemCpuLoad")));
    } catch (Throwable t) {
    }
    try {
        Class<?> unixOS = Class.forName("com.sun.management.UnixOperatingSystemMXBean");
        printValue("Open file descriptors", maxNameLen, printLong(getValueAsLong(unixOS, "getOpenFileDescriptorCount")));
        printValue("Max file descriptors", maxNameLen, printLong(getValueAsLong(unixOS, "getMaxFileDescriptorCount")));
    } 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);
    }
    if (showMemoryPools) {
        List<MemoryPoolMXBean> memoryPools = ManagementFactory.getMemoryPoolMXBeans();
        System.out.println("Memory Pools");
        printValue("Total Memory Pools", maxNameLen, printLong(memoryPools.size()));
        String spaces4 = "   ";
        for (MemoryPoolMXBean pool : memoryPools) {
            String name = pool.getName();
            MemoryType type = pool.getType();
            printValue(spaces4 + "Pool (" + type + ")", maxNameLen, name);
            // PeakUsage/CurrentUsage
            MemoryUsage peakUsage = pool.getPeakUsage();
            MemoryUsage usage = pool.getUsage();
            if (usage != null && peakUsage != null) {
                long init = peakUsage.getInit();
                long used = peakUsage.getUsed();
                long committed = peakUsage.getCommitted();
                long max = peakUsage.getMax();
                System.out.println(spaces4 + spaces4 + "Peak Usage");
                printValue(spaces4 + spaces4 + spaces4 + "init", maxNameLen, printLong(init));
                printValue(spaces4 + spaces4 + spaces4 + "used", maxNameLen, printLong(used));
                printValue(spaces4 + spaces4 + spaces4 + "committed", maxNameLen, printLong(committed));
                printValue(spaces4 + spaces4 + spaces4 + "max", maxNameLen, printLong(max));
                init = usage.getInit();
                used = usage.getUsed();
                committed = usage.getCommitted();
                max = usage.getMax();
                System.out.println(spaces4 + spaces4 + "Current Usage");
                printValue(spaces4 + spaces4 + spaces4 + "init", maxNameLen, printLong(init));
                printValue(spaces4 + spaces4 + spaces4 + "used", maxNameLen, printLong(used));
                printValue(spaces4 + spaces4 + spaces4 + "committed", maxNameLen, printLong(committed));
                printValue(spaces4 + spaces4 + spaces4 + "max", maxNameLen, printLong(max));
            }
        }
    }
    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) MemoryUsage(java.lang.management.MemoryUsage) MemoryMXBean(java.lang.management.MemoryMXBean) ClassLoadingMXBean(java.lang.management.ClassLoadingMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) HashMap(java.util.HashMap) Map(java.util.Map) MemoryType(java.lang.management.MemoryType)

Example 57 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project MyPerf4J by ThinkpadNC5.

the class JvmMemoryCollector method collectMemoryMetrics.

public static JvmMemoryMetrics collectMemoryMetrics() {
    long oldGenUsed = 0L, oldGenMax = 0L;
    long permGenUsed = 0L, permGenMax = 0L;
    long edenUsed = 0L, edenMax = 0L;
    long survivorUsed = 0L, survivorMax = 0L;
    long metaspaceUsed = 0L, metaSpaceMax = 0L;
    long codeCacheUsed = 0L, codeCacheMax = 0L;
    List<MemoryPoolMXBean> mxBeanList = ManagementFactory.getMemoryPoolMXBeans();
    for (int i = 0; i < mxBeanList.size(); i++) {
        MemoryPoolMXBean memoryPool = mxBeanList.get(i);
        MemoryUsage usage = memoryPool.getUsage();
        String poolName = memoryPool.getName();
        if (poolName.endsWith("Perm Gen")) {
            permGenUsed = usage.getUsed() >> 10;
            permGenMax = usage.getMax() >> 10;
        } else if (poolName.endsWith("Metaspace")) {
            metaspaceUsed = usage.getUsed() >> 10;
            metaSpaceMax = usage.getMax() >> 10;
        } else if (poolName.endsWith("Code Cache")) {
            codeCacheUsed = usage.getUsed() >> 10;
            codeCacheMax = usage.getMax() >> 10;
        } else if (poolName.endsWith("Old Gen")) {
            oldGenUsed = usage.getUsed() >> 10;
            oldGenMax = usage.getMax() >> 10;
        } else if (poolName.endsWith("Eden Space")) {
            edenUsed = usage.getUsed() >> 10;
            edenMax = usage.getMax() >> 10;
        } else if (poolName.endsWith("Survivor Space")) {
            survivorUsed = usage.getUsed() >> 10;
            survivorMax = usage.getMax() >> 10;
        }
    }
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    MemoryUsage nonHeapMem = memoryMXBean.getNonHeapMemoryUsage();
    long nonHeapUsed = nonHeapMem.getUsed() >> 10;
    long nonHeapMax = nonHeapMem.getMax() >> 10;
    MemoryUsage heapMem = memoryMXBean.getHeapMemoryUsage();
    long heapUsed = heapMem.getUsed() >> 10;
    long heapMax = heapMem.getMax() >> 10;
    return new JvmMemoryMetrics(heapUsed, heapMax, nonHeapUsed, nonHeapMax, permGenUsed, permGenMax, metaspaceUsed, metaSpaceMax, codeCacheUsed, codeCacheMax, oldGenUsed, oldGenMax, edenUsed, edenMax, survivorUsed, survivorMax);
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage) JvmMemoryMetrics(cn.myperf4j.base.metric.JvmMemoryMetrics)

Example 58 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project qpid-broker-j by apache.

the class BrokerAttributeInjector method getInjectedAttributes.

@Override
public Collection<ConfiguredObjectInjectedAttribute<?, ?>> getInjectedAttributes() {
    List<ConfiguredObjectInjectedAttribute<?, ?>> attributes = new ArrayList<>();
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        String poolName = memoryPoolMXBean.getName().replace(" ", "");
        String attributeName = "jvmMemoryMaximum" + poolName;
        try {
            Method getMemoryPoolMaximum = BrokerAttributeInjector.class.getDeclaredMethod("getMemoryPoolMaximum", Broker.class, MemoryPoolMXBean.class);
            final ConfiguredObjectInjectedAttribute<?, ?> injectedStatistic = new ConfiguredDerivedInjectedAttribute<>(attributeName, getMemoryPoolMaximum, new Object[] { memoryPoolMXBean }, false, false, "", false, "", "Maximum size of memory pool " + memoryPoolMXBean.getName(), _typeValidator);
            attributes.add(injectedStatistic);
        } catch (NoSuchMethodException e) {
            LOGGER.warn("Failed to inject attribute '{}'", attributeName, e);
        }
    }
    return attributes;
}
Also used : ArrayList(java.util.ArrayList) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Method(java.lang.reflect.Method)

Example 59 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project jvm-profiler by uber-common.

the class CpuAndMemoryProfiler method profile.

@Override
public synchronized void profile() {
    Double processCpuLoad = null;
    Double systemCpuLoad = null;
    Long processCpuTime = null;
    AttributeList cpuAttributes = getCpuAttributes();
    if (cpuAttributes != null && cpuAttributes.size() > 0) {
        Attribute att = (Attribute) cpuAttributes.get(ATTRIBUTE_INDEX_ProcessCpuLoad);
        processCpuLoad = (Double) att.getValue();
        if (processCpuLoad == Double.NaN) {
            processCpuLoad = null;
        }
        att = (Attribute) cpuAttributes.get(ATTRIBUTE_INDEX_SystemCpuLoad);
        systemCpuLoad = (Double) att.getValue();
        if (systemCpuLoad == Double.NaN) {
            systemCpuLoad = null;
        }
        att = (Attribute) cpuAttributes.get(ATTRIBUTE_INDEX_ProcessCpuTime);
        processCpuTime = (Long) att.getValue();
    }
    Double heapMemoryTotalUsed = null;
    Double heapMemoryCommitted = null;
    Double heapMemoryMax = null;
    Double nonHeapMemoryTotalUsed = null;
    Double nonHeapMemoryCommitted = null;
    Double nonHeapMemoryMax = null;
    if (memoryMXBean != null) {
        MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
        heapMemoryTotalUsed = new Double(memoryUsage.getUsed());
        heapMemoryCommitted = new Double(memoryUsage.getCommitted());
        heapMemoryMax = new Double(memoryUsage.getMax());
        memoryUsage = memoryMXBean.getNonHeapMemoryUsage();
        nonHeapMemoryTotalUsed = new Double(memoryUsage.getUsed());
        nonHeapMemoryCommitted = new Double(memoryUsage.getCommitted());
        nonHeapMemoryMax = new Double(memoryUsage.getMax());
    }
    List<Map<String, Object>> gcMetrics = new ArrayList<>();
    List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
    if (gcMXBeans != null) {
        for (GarbageCollectorMXBean gcMXBean : gcMXBeans) {
            Map<String, Object> gcMap = new HashMap<>();
            gcMap.put("name", gcMXBean.getName());
            gcMap.put("collectionCount", new Long(gcMXBean.getCollectionCount()));
            gcMap.put("collectionTime", new Long(gcMXBean.getCollectionTime()));
            gcMetrics.add(gcMap);
        }
    }
    List<Map<String, Object>> memoryPoolsMetrics = new ArrayList<>();
    for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
        Map<String, Object> memoryPoolMap = new HashMap<>();
        memoryPoolMap.put("name", pool.getName());
        memoryPoolMap.put("type", pool.getType().toString());
        memoryPoolMap.put("usageCommitted", pool.getUsage().getCommitted());
        memoryPoolMap.put("usageMax", pool.getUsage().getMax());
        memoryPoolMap.put("usageUsed", pool.getUsage().getUsed());
        memoryPoolMap.put("peakUsageCommitted", pool.getPeakUsage().getCommitted());
        memoryPoolMap.put("peakUsageMax", pool.getPeakUsage().getMax());
        memoryPoolMap.put("peakUsageUsed", pool.getPeakUsage().getUsed());
        memoryPoolsMetrics.add(memoryPoolMap);
    }
    List<Map<String, Object>> bufferPoolsMetrics = new ArrayList<>();
    List<BufferPoolMXBean> bufferPools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
    if (bufferPools != null) {
        for (BufferPoolMXBean pool : bufferPools) {
            Map<String, Object> bufferPoolMap = new HashMap<>();
            bufferPoolMap.put("name", pool.getName());
            bufferPoolMap.put("count", new Long(pool.getCount()));
            bufferPoolMap.put("memoryUsed", new Long(pool.getMemoryUsed()));
            bufferPoolMap.put("totalCapacity", new Long(pool.getTotalCapacity()));
            bufferPoolsMetrics.add(bufferPoolMap);
        }
    }
    // See http://man7.org/linux/man-pages/man5/proc.5.html for details about proc status
    Map<String, String> procStatus = ProcFileUtils.getProcStatus();
    Long procStatusVmRSS = ProcFileUtils.getBytesValue(procStatus, "VmRSS");
    Long procStatusVmHWM = ProcFileUtils.getBytesValue(procStatus, "VmHWM");
    Long procStatusVmSize = ProcFileUtils.getBytesValue(procStatus, "VmSize");
    Long procStatusVmPeak = ProcFileUtils.getBytesValue(procStatus, "VmPeak");
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("epochMillis", System.currentTimeMillis());
    map.put("name", getProcessName());
    map.put("host", getHostName());
    map.put("processUuid", getProcessUuid());
    map.put("appId", getAppId());
    if (getTag() != null) {
        map.put("tag", getTag());
    }
    if (getCluster() != null) {
        map.put("cluster", getCluster());
    }
    if (getRole() != null) {
        map.put("role", getRole());
    }
    map.put("processCpuLoad", processCpuLoad);
    map.put("systemCpuLoad", systemCpuLoad);
    map.put("processCpuTime", processCpuTime);
    map.put("heapMemoryTotalUsed", heapMemoryTotalUsed);
    map.put("heapMemoryCommitted", heapMemoryCommitted);
    map.put("heapMemoryMax", heapMemoryMax);
    map.put("nonHeapMemoryTotalUsed", nonHeapMemoryTotalUsed);
    map.put("nonHeapMemoryCommitted", nonHeapMemoryCommitted);
    map.put("nonHeapMemoryMax", nonHeapMemoryMax);
    map.put("gc", gcMetrics);
    map.put("memoryPools", memoryPoolsMetrics);
    map.put("bufferPools", bufferPoolsMetrics);
    if (procStatusVmRSS != null) {
        map.put("vmRSS", procStatusVmRSS);
    }
    if (procStatusVmHWM != null) {
        map.put("vmHWM", procStatusVmHWM);
    }
    if (procStatusVmSize != null) {
        map.put("vmSize", procStatusVmSize);
    }
    if (procStatusVmPeak != null) {
        map.put("vmPeak", procStatusVmPeak);
    }
    if (reporter != null) {
        reporter.report(PROFILER_NAME, map);
    }
}
Also used : Attribute(javax.management.Attribute) HashMap(java.util.HashMap) AttributeList(javax.management.AttributeList) ArrayList(java.util.ArrayList) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) MemoryUsage(java.lang.management.MemoryUsage) BufferPoolMXBean(java.lang.management.BufferPoolMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) HashMap(java.util.HashMap) Map(java.util.Map)

Example 60 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project presto by prestodb.

the class CodeCacheGcTrigger method installCodeCacheGcTrigger.

public void installCodeCacheGcTrigger() {
    if (installed.getAndSet(true)) {
        return;
    }
    // Hack to work around bugs in java 8 (8u45+) related to code cache management.
    // See http://openjdk.5641.n7.nabble.com/JIT-stops-compiling-after-a-while-java-8u45-td259603.html for more info.
    MemoryPoolMXBean codeCacheMbean = findCodeCacheMBean();
    Thread gcThread = new Thread(() -> {
        while (!Thread.currentThread().isInterrupted()) {
            long used = codeCacheMbean.getUsage().getUsed();
            long max = codeCacheMbean.getUsage().getMax();
            if (used > 0.95 * max) {
                log.error("Code Cache is more than 95% full. JIT may stop working.");
            }
            if (used > (max * collectionThreshold) / 100) {
                // Due to some obscure bug in hotspot (java 8), once the code cache fills up the JIT stops compiling
                // By forcing a GC, we let the code cache evictor make room before the cache fills up.
                log.info("Triggering GC to avoid Code Cache eviction bugs");
                System.gc();
            }
            try {
                TimeUnit.MILLISECONDS.sleep(interval.toMillis());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    });
    gcThread.setDaemon(true);
    gcThread.setName("Code-Cache-GC-Trigger");
    gcThread.start();
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Aggregations

MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)112 MemoryUsage (java.lang.management.MemoryUsage)46 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)16 ArrayList (java.util.ArrayList)15 MemoryMXBean (java.lang.management.MemoryMXBean)12 Test (org.testng.annotations.Test)11 InstanceNotFoundException (javax.management.InstanceNotFoundException)9 ReflectionException (javax.management.ReflectionException)9 NotificationEmitter (javax.management.NotificationEmitter)8 MemoryType (java.lang.management.MemoryType)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 AttributeNotFoundException (javax.management.AttributeNotFoundException)7 IntrospectionException (javax.management.IntrospectionException)7 MBeanException (javax.management.MBeanException)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Attribute (javax.management.Attribute)6 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)6 BufferPoolMXBean (java.lang.management.BufferPoolMXBean)5 ThreadMXBean (java.lang.management.ThreadMXBean)5