Search in sources :

Example 76 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project incubator-skywalking by apache.

the class MemoryPoolModule method getMemoryPoolMetricList.

@Override
public List<MemoryPool> getMemoryPoolMetricList() {
    List<MemoryPool> poolList = new LinkedList<MemoryPool>();
    for (MemoryPoolMXBean bean : beans) {
        String name = bean.getName();
        PoolType type;
        if (contains(getCodeCacheNames(), name)) {
            type = PoolType.CODE_CACHE_USAGE;
        } else if (contains(getEdenNames(), name)) {
            type = PoolType.NEWGEN_USAGE;
        } else if (contains(getOldNames(), name)) {
            type = PoolType.OLDGEN_USAGE;
        } else if (contains(getSurvivorNames(), name)) {
            type = PoolType.SURVIVOR_USAGE;
        } else if (contains(getMetaspaceNames(), name)) {
            type = PoolType.METASPACE_USAGE;
        } else if (contains(getPermNames(), name)) {
            type = PoolType.PERMGEN_USAGE;
        } else {
            continue;
        }
        MemoryUsage usage = bean.getUsage();
        poolList.add(MemoryPool.newBuilder().setType(type).setInit(usage.getInit()).setMax(usage.getMax()).setCommited(usage.getCommitted()).setUsed(usage.getUsed()).build());
    }
    return poolList;
}
Also used : PoolType(org.apache.skywalking.apm.network.proto.PoolType) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage) LinkedList(java.util.LinkedList) MemoryPool(org.apache.skywalking.apm.network.proto.MemoryPool)

Example 77 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project closure-compiler by google.

the class JvmMetrics method writeMemoryMetrics.

private static void writeMemoryMetrics(PrintStream out, boolean verbose, boolean pretty) {
    if (pretty) {
        out.println("\nMemory usage");
    }
    // only show overall stats in verbose mode
    if (verbose) {
        MemoryMXBean overallMemBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage usage = overallMemBean.getHeapMemoryUsage();
        writeOverallMemoryUsage(out, usage, "Heap", pretty);
        usage = overallMemBean.getNonHeapMemoryUsage();
        writeOverallMemoryUsage(out, usage, "Non-heap", pretty);
    }
    if (verbose) {
        List<MemoryPoolMXBean> mpBeans = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean mpBean : mpBeans) {
            MemoryUsage currentUsage = mpBean.getUsage();
            MemoryUsage peakUsage = mpBean.getPeakUsage();
            if (pretty) {
                out.println("\tPool " + mpBean.getName());
                writePoolMemoryUsage(out, currentUsage, peakUsage, null, true);
            } else {
                writePoolMemoryUsage(out, currentUsage, peakUsage, "mem-pool-" + normalizeName(mpBean.getName()), false);
            }
        }
    } else {
        long available = 0;
        long current = 0;
        long peak = 0;
        List<MemoryPoolMXBean> mpBeans = ManagementFactory.getMemoryPoolMXBeans();
        for (MemoryPoolMXBean mpBean : mpBeans) {
            MemoryUsage currentUsage = mpBean.getUsage();
            available += currentUsage.getMax();
            current += currentUsage.getUsed();
            MemoryUsage peakUsage = mpBean.getPeakUsage();
            peak += peakUsage.getUsed();
        }
        MemoryUsage summaryUsage = new MemoryUsage(0, current, current, available);
        MemoryUsage summaryPeakUsage = new MemoryUsage(0, peak, peak, peak);
        if (pretty) {
            out.format("\tAggregate of %d memory pools\n", mpBeans.size());
            writePoolMemoryUsage(out, summaryUsage, summaryPeakUsage, null, true);
        } else {
            writePoolMemoryUsage(out, summaryUsage, summaryPeakUsage, "mem", false);
        }
    }
}
Also used : MemoryMXBean(java.lang.management.MemoryMXBean) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 78 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project openj9 by eclipse.

the class MemoryManagerMXBeanImpl method getMemoryPoolNames.

/**
 * {@inheritDoc}
 */
@Override
public String[] getMemoryPoolNames() {
    String[] names = new String[managedPoolList.size()];
    int idx = 0;
    for (MemoryPoolMXBean bean : managedPoolList) {
        names[idx++] = bean.getName();
    }
    return names;
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Example 79 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project openj9 by eclipse.

the class TestManagementFactory method testMemoryPoolMXBeanProxy.

@Test
public void testMemoryPoolMXBeanProxy() {
    List<MemoryPoolMXBean> allBeans = ManagementFactory.getMemoryPoolMXBeans();
    AssertJUnit.assertNotNull(allBeans);
    MemoryPoolMXBean realBean = allBeans.get(0);
    String beanName = realBean.getName();
    MemoryPoolMXBean proxy = null;
    try {
        proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=MemoryPool,name=" + beanName, MemoryPoolMXBean.class);
        MemoryPoolMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        AssertJUnit.assertNotNull(proxy.toString());
        AssertJUnit.assertEquals("java.lang:type=MemoryPool,name=" + beanName, proxy.getObjectName().toString());
        AssertJUnit.assertEquals(realBean.getName(), proxy.getName());
        // query via the proxy first because it takes a significantly
        // longer path and is likely to affect usage
        MemoryUsage proxyUsage = proxy.getCollectionUsage();
        MemoryUsage directUsage = realBean.getCollectionUsage();
        validateMemoryUsage(proxyUsage);
        validateMemoryUsage(directUsage);
        AssertJUnit.assertEquals(directUsage, proxyUsage);
        if (realBean.isCollectionUsageThresholdSupported()) {
            AssertJUnit.assertEquals(realBean.getCollectionUsageThreshold(), proxy.getCollectionUsageThreshold());
            AssertJUnit.assertEquals(realBean.getCollectionUsageThresholdCount(), proxy.getCollectionUsageThresholdCount());
            AssertJUnit.assertEquals(realBean.isCollectionUsageThresholdExceeded(), proxy.isCollectionUsageThresholdExceeded());
            realBean.setCollectionUsageThreshold(200 * 1024);
            AssertJUnit.assertEquals(200 * 1024, proxy.getCollectionUsageThreshold());
        } else {
            try {
                realBean.getCollectionUsageThreshold();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                proxy.getCollectionUsageThreshold();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                realBean.getCollectionUsageThresholdCount();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                proxy.getCollectionUsageThresholdCount();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                realBean.isCollectionUsageThresholdExceeded();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                proxy.isCollectionUsageThresholdExceeded();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                realBean.setCollectionUsageThreshold(300 * 1024);
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                realBean.setCollectionUsageThreshold(300 * 1024);
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
        }
        // end else
        String[] realMgrs = realBean.getMemoryManagerNames();
        String[] proxyMgrs = proxy.getMemoryManagerNames();
        AssertJUnit.assertTrue(Arrays.equals(realMgrs, proxyMgrs));
        // query via the proxy first because it takes a significantly
        // longer path and is likely to affect peak usage
        proxyUsage = proxy.getPeakUsage();
        directUsage = realBean.getPeakUsage();
        validateMemoryUsage(proxyUsage);
        validateMemoryUsage(directUsage);
        // the committed size should, however, be non-decreasing
        if (directUsage != null && proxyUsage != null) {
            AssertJUnit.assertEquals("init should be the same", directUsage.getInit(), proxyUsage.getInit());
            AssertJUnit.assertTrue("committed should be non-decreasing", directUsage.getCommitted() >= proxyUsage.getCommitted());
            AssertJUnit.assertEquals("max should be the same", directUsage.getMax(), proxyUsage.getMax());
        } else {
            // if either is null, the other should be also
            AssertJUnit.assertEquals(directUsage, proxyUsage);
        }
        AssertJUnit.assertEquals(realBean.getType(), proxy.getType());
        /* It is unfair to compare usages obtained by realBean and proxy; the gap
			 * between calls may lead to disparity in the values they report.
			 */
        logger.debug("Memory usage by the current memory pool (bean instance): " + realBean.getUsage());
        logger.debug("Memory usage by the current memory (bean proxy): " + proxy.getUsage());
        if (realBean.isUsageThresholdSupported()) {
            AssertJUnit.assertEquals(realBean.getUsageThreshold(), proxy.getUsageThreshold());
            AssertJUnit.assertEquals(realBean.getUsageThresholdCount(), proxy.getUsageThresholdCount());
            AssertJUnit.assertEquals(realBean.isCollectionUsageThresholdExceeded(), proxy.isCollectionUsageThresholdExceeded());
            realBean.setUsageThreshold(200 * 1024);
            AssertJUnit.assertEquals(200 * 1024, proxy.getUsageThreshold());
            try {
                proxy.setUsageThreshold(-23);
                Assert.fail("Should have thrown IllegalArgumentException");
            } catch (IllegalArgumentException e) {
                logger.debug("IllegalArgumentException occurred, as expected: " + e.getMessage());
            }
        } else {
            try {
                realBean.getUsageThreshold();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                proxy.getUsageThreshold();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                realBean.getUsageThresholdCount();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                proxy.getUsageThresholdCount();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                realBean.isUsageThresholdExceeded();
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                proxy.isUsageThresholdExceeded();
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                realBean.setUsageThreshold(300 * 1024);
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
            try {
                proxy.setUsageThreshold(300 * 1024);
                Assert.fail("Should have thrown an exception");
            } catch (UnsupportedOperationException e) {
                logger.debug("UnsupportedOperationException occurred, as expected: " + e.getMessage());
            }
        }
        // end else
        AssertJUnit.assertEquals(realBean.isValid(), proxy.isValid());
        proxy.resetPeakUsage();
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Test(org.testng.annotations.Test)

Example 80 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project openj9 by eclipse.

the class TestMemoryPoolMXBean method testGetAttributes.

@Test
@SuppressWarnings("unchecked")
public final void testGetAttributes() {
    MemoryPoolMXBean testImpl = testBean;
    try {
        AttributeList attributes = mbs.getAttributes(objName, attribs.keySet().toArray(new String[] {}));
        AssertJUnit.assertNotNull(attributes);
        AssertJUnit.assertTrue(attributes.size() <= attribs.size());
        logger.debug("Number of retrieved attributes = " + attributes.size());
        // Check through the returned values
        Iterator it = attributes.iterator();
        while (it.hasNext()) {
            Attribute element = (Attribute) it.next();
            AssertJUnit.assertNotNull(element);
            String name = element.getName();
            Object value = element.getValue();
            if (name.equals("CollectionUsage")) {
                // the method.
                if (value != null) {
                    AssertJUnit.assertTrue(value instanceof CompositeData);
                    MemoryUsage mu = MemoryUsage.from((CompositeData) value);
                    AssertJUnit.assertTrue(mu.getCommitted() != -1);
                    AssertJUnit.assertTrue(mu.getUsed() != -1);
                    logger.debug("CollectionUsage : " + mu);
                }
            } else if (name.equals("CollectionUsageThreshold")) {
                AssertJUnit.assertTrue(value instanceof Long);
                AssertJUnit.assertTrue(((Long) (value)) > -1);
            } else if (name.equals("CollectionUsageThresholdCount")) {
                AssertJUnit.assertTrue(value instanceof Long);
                AssertJUnit.assertTrue(((Long) (value)) > -1);
                logger.debug("CollectionUsageThresholdCount = " + value);
            } else if (name.equals("MemoryManagerNames")) {
                AssertJUnit.assertTrue(value instanceof String[]);
                String[] names = (String[]) value;
                AssertJUnit.assertTrue(names.length > 0);
                for (int i = 0; i < names.length; i++) {
                    String string = names[i];
                    AssertJUnit.assertNotNull(string);
                    AssertJUnit.assertTrue(string.length() > 0);
                    logger.debug("Memory manager[" + i + "] = " + string);
                }
            // end for
            } else if (name.equals("Name")) {
                AssertJUnit.assertTrue(value instanceof String);
                String str = (String) value;
                AssertJUnit.assertNotNull(str);
                logger.debug("Name = " + str);
            } else if (name.equals("PeakUsage")) {
                AssertJUnit.assertTrue(value instanceof CompositeData);
                MemoryUsage mu = MemoryUsage.from((CompositeData) value);
                AssertJUnit.assertNotNull(mu);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("Peak usage : " + mu);
            } else if (name.equals("Type")) {
                AssertJUnit.assertTrue(value instanceof String);
                String str = (String) value;
                AssertJUnit.assertNotNull(str);
                logger.debug("Type = " + str);
            } else if (name.equals("Usage")) {
                AssertJUnit.assertTrue(value instanceof CompositeData);
                MemoryUsage mu = MemoryUsage.from((CompositeData) value);
                AssertJUnit.assertNotNull(mu);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("Usage : " + mu);
            } else if (name.equals("UsageThreshold")) {
                AssertJUnit.assertTrue(value instanceof Long);
                AssertJUnit.assertTrue(((Long) (value)) > -1);
                logger.debug("Usage threshold = " + value);
            } else if (name.equals("UsageThresholdCount")) {
                AssertJUnit.assertTrue(value instanceof Long);
                AssertJUnit.assertTrue(((Long) (value)) > -1);
                logger.debug("UsageThresholdCount = " + value);
            } else if (name.equals("CollectionUsageThresholdExceeded")) {
                AssertJUnit.assertTrue(value instanceof Boolean);
                logger.debug("CollectionUsageThresholdExceeded = " + value);
            } else if (name.equals("CollectionUsageThresholdSupported")) {
                AssertJUnit.assertTrue(value instanceof Boolean);
                logger.debug("CollectionUsageThresholdSupported = " + value);
            } else if (name.equals("UsageThresholdExceeded")) {
                AssertJUnit.assertTrue(value instanceof Boolean);
                logger.debug("UsageThresholdExceeded = " + value);
            } else if (name.equals("UsageThresholdSupported")) {
                AssertJUnit.assertTrue(value instanceof Boolean);
                logger.debug("UsageThresholdSupported = " + value);
            } else if (name.equals("Valid")) {
                AssertJUnit.assertTrue(value instanceof Boolean);
                logger.debug("Valid = " + value);
            } else if (name.equals("PreCollectionUsage")) {
                // the method.
                if (value != null) {
                    AssertJUnit.assertTrue(value instanceof CompositeData);
                    MemoryUsage mu = MemoryUsage.from((CompositeData) value);
                    AssertJUnit.assertTrue(mu.getCommitted() != -1);
                    AssertJUnit.assertTrue(mu.getUsed() != -1);
                    logger.debug("Pre-collection usage (IBM) : " + mu);
                }
            } else {
                Assert.fail("Unexpected attribute returned : " + name + " !!");
            }
        }
    // end while
    } catch (Exception e) {
        Assert.fail("Caught Exception : " + e.getCause().getMessage());
        e.printStackTrace();
    }
    // A failing scenario - pass in an attribute that is not part of
    // the management interface.
    String[] badNames = { "Cork", "Galway" };
    AttributeList attributes = null;
    try {
        attributes = mbs.getAttributes(objName, badNames);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
    }
    AssertJUnit.assertNotNull(attributes);
    // No attributes will have been returned.
    AssertJUnit.assertTrue(attributes.size() == 0);
}
Also used : ReflectionException(javax.management.ReflectionException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) CompositeData(javax.management.openmbean.CompositeData) InstanceNotFoundException(javax.management.InstanceNotFoundException) MemoryUsage(java.lang.management.MemoryUsage) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) Iterator(java.util.Iterator) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Test(org.testng.annotations.Test)

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