Search in sources :

Example 81 with MemoryPoolMXBean

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

the class TestMemoryPoolMXBean method testSetCollectionUsageThresholdAttribute.

@Test
public void testSetCollectionUsageThresholdAttribute() {
    MemoryPoolMXBean testImpl = testBean;
    if (testImpl.isCollectionUsageThresholdSupported()) {
        try {
            long originalCUT = (Long) mbs.getAttribute(objName, "CollectionUsageThreshold");
            long newCUT = originalCUT + 1024;
            Attribute newCUTAttr = new Attribute("CollectionUsageThreshold", new Long(newCUT));
            mbs.setAttribute(objName, newCUTAttr);
            AssertJUnit.assertEquals(new Long(newCUT), (Long) mbs.getAttribute(objName, "CollectionUsageThreshold"));
        } catch (AttributeNotFoundException e) {
            e.printStackTrace();
            Assert.fail("Unexpected AttributeNotFoundException occurred: " + e.getMessage());
        } catch (InstanceNotFoundException e) {
            e.printStackTrace();
            Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
        } catch (MBeanException e) {
            e.printStackTrace();
            Assert.fail("Unexpected MBeanException occurred: " + e.getMessage());
        } catch (ReflectionException e) {
            e.printStackTrace();
            Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
        } catch (InvalidAttributeValueException e) {
            e.printStackTrace();
            Assert.fail("Unexpected InvalidAttributeValueException occurred: " + e.getMessage());
        }
    } else {
        try {
            Attribute newCUTAttr = new Attribute("CollectionUsageThreshold", new Long(100 * 1024));
            mbs.setAttribute(objName, newCUTAttr);
            Assert.fail("Unreacheable code: should have thrown exception!");
        } catch (Exception e) {
            AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
            logger.debug("Exception occurred, as expected: cannot set attribute (CollectionUsageThreshold).");
        }
    }
// end else collection usage threshold is not supported
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) Attribute(javax.management.Attribute) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) AttributeNotFoundException(javax.management.AttributeNotFoundException) IntrospectionException(javax.management.IntrospectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ReflectionException(javax.management.ReflectionException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException) Test(org.testng.annotations.Test)

Example 82 with MemoryPoolMXBean

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

the class TestMemoryPoolMXBean method testSetAttributes.

@Test
public final void testSetAttributes() {
    // Only two attributes can be set for this platform bean type
    // - UsageThreshold and CollectionUsageThreshold
    MemoryPoolMXBean testImpl = testBean;
    AttributeList attList = new AttributeList();
    Attribute newUT = new Attribute("UsageThreshold", new Long(100 * 1024));
    attList.add(newUT);
    AttributeList setAttrs = null;
    try {
        setAttrs = mbs.setAttributes(objName, attList);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
    }
    AssertJUnit.assertNotNull(setAttrs);
    AssertJUnit.assertTrue(setAttrs.size() <= 1);
    if (setAttrs.size() == 1) {
        AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getName().equals("UsageThreshold"));
        AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getValue() instanceof Long);
        long recoveredValue = (Long) ((Attribute) (setAttrs.get(0))).getValue();
        AssertJUnit.assertEquals(100 * 1024, recoveredValue);
    }
    attList = new AttributeList();
    Attribute newCUT = new Attribute("CollectionUsageThreshold", new Long(250 * 1024));
    attList.add(newCUT);
    try {
        setAttrs = mbs.setAttributes(objName, attList);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
    }
    AssertJUnit.assertNotNull(setAttrs);
    AssertJUnit.assertTrue(setAttrs.size() <= 1);
    if (setAttrs.size() == 1) {
        AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getName().equals("CollectionUsageThreshold"));
        AssertJUnit.assertTrue(((Attribute) (setAttrs.get(0))).getValue() instanceof Long);
        long recoveredValue = (Long) ((Attribute) (setAttrs.get(0))).getValue();
        AssertJUnit.assertEquals(250 * 1024, recoveredValue);
    }
    // A failure scenario - a non-existent attribute...
    AttributeList badList = new AttributeList();
    Attribute garbage = new Attribute("Bantry", new Long(2888));
    badList.add(garbage);
    try {
        setAttrs = mbs.setAttributes(objName, badList);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
    }
    AssertJUnit.assertNotNull(setAttrs);
    AssertJUnit.assertTrue(setAttrs.size() == 0);
    // Another failure scenario - a non-writable attribute...
    badList = new AttributeList();
    try {
        setAttrs = mbs.setAttributes(objName, badList);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
    }
    badList.add(garbage);
    try {
        setAttrs = mbs.setAttributes(objName, badList);
    } catch (InstanceNotFoundException e) {
        // An unlikely exception - if this occurs, we can't proceed with the test.
        Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
    } catch (ReflectionException e) {
        // An unlikely exception - if this occurs, we can't proceed with the test.
        Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
    }
    AssertJUnit.assertNotNull(setAttrs);
    AssertJUnit.assertTrue(setAttrs.size() == 0);
    // Yet another failure scenario - a wrongly-typed attribute...
    badList = new AttributeList();
    garbage = new Attribute("CollectionUsageThreshold", new Boolean(true));
    badList.add(garbage);
    try {
        setAttrs = mbs.setAttributes(objName, badList);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getCause().getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException occurred: " + e.getMessage());
    }
    AssertJUnit.assertNotNull(setAttrs);
    AssertJUnit.assertTrue(setAttrs.size() == 0);
}
Also used : ReflectionException(javax.management.ReflectionException) Attribute(javax.management.Attribute) AttributeList(javax.management.AttributeList) InstanceNotFoundException(javax.management.InstanceNotFoundException) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Test(org.testng.annotations.Test)

Example 83 with MemoryPoolMXBean

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

the class TestMisc method testWhoSupportsMemoryUsageThresholds.

@Test
public final void testWhoSupportsMemoryUsageThresholds() {
    List<MemoryPoolMXBean> allPools = ManagementFactory.getMemoryPoolMXBeans();
    Iterator<MemoryPoolMXBean> it = allPools.iterator();
    while (it.hasNext()) {
        MemoryPoolMXBean pool = it.next();
        logger.debug("Pool " + pool.getName() + ", supports usage" + " threshold ? " + pool.isUsageThresholdSupported() + ", " + "supports collection usage threshold ? " + pool.isCollectionUsageThresholdSupported());
    }
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Test(org.testng.annotations.Test)

Example 84 with MemoryPoolMXBean

use of java.lang.management.MemoryPoolMXBean in project Terasology by MovingBlocks.

the class HeapAllocationMode method getMetrics.

@Override
public String getMetrics() {
    // Memory stats without using Runtime.getRuntime() for client side
    builder.setLength(0);
    builder.append(getName());
    builder.append("\n");
    for (MemoryPoolMXBean mpBean : ManagementFactory.getMemoryPoolMXBeans()) {
        if (mpBean.getType() == MemoryType.HEAP) {
            MemoryUsage usage = mpBean.getUsage();
            builder.append(String.format("Memory Heap: %s - Memory Usage: %.2f MB, Max Memory: %.2f MB \n", mpBean.getName(), usage.getUsed() / MB_SIZE, usage.getMax() / MB_SIZE));
        }
    }
    return builder.toString();
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) MemoryUsage(java.lang.management.MemoryUsage)

Example 85 with MemoryPoolMXBean

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

the class LlapDaemon method getTotalHeapSize.

public static long getTotalHeapSize() {
    // runtime.getMax() gives a very different number from the actual Xmx sizing.
    // you can iterate through the
    // http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryPoolMXBean.html
    // from java.lang.management to figure this out, but the hard-coded params in the llap run.sh
    // result in 89% usable heap (-XX:NewRatio=8) + a survivor region which is technically not
    // in the usable space.
    long total = 0;
    for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
        long sz = mp.getUsage().getMax();
        if (mp.getName().contains("Survivor")) {
            // there are 2 survivor spaces
            sz *= 2;
        }
        if (mp.getType().equals(MemoryType.HEAP)) {
            total += sz;
        }
    }
    // round up to the next MB
    total += (total % (1024 * 1024));
    return total;
}
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