Search in sources :

Example 36 with MemoryPoolMXBean

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

the class TestMemoryPoolMXBean method testInvoke.

@Test
public final void testInvoke() {
    MemoryPoolMXBean testImpl = testBean;
    // usage to the current memory usage.
    try {
        Object retVal = null;
        try {
            retVal = mbs.invoke(objName, "resetPeakUsage", new Object[] {}, null);
        } catch (InstanceNotFoundException e) {
            // InstanceNotFoundException is an unlikely exception - if this occurs, we
            // can proceed with the test.
            Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
        }
        MemoryUsage currentMU = testImpl.getUsage();
        MemoryUsage peakMU = testImpl.getPeakUsage();
        logger.debug("currentMU : " + currentMU);
        logger.debug("peakMU    : " + peakMU);
        AssertJUnit.assertNull(retVal);
    } catch (Exception e) {
        Assert.fail("Unexpected exception: " + e.getMessage());
    }
    // Try and invoke a non-existent method...
    try {
        Object retVal = mbs.invoke(objName, "madeupMethod", new Object[] { "fibber" }, new String[] { String.class.getName() });
        Assert.fail("Unreacheable code: should have thrown an exception calling a non-existent operation");
    } catch (MBeanException e) {
        Assert.fail("Unexpected MBeanException occurred: " + e.getMessage());
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException occurred: " + e.getMessage());
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof ReflectionException);
        logger.debug("ReflectionException occurred, as expected: attempted to invoke non-existent method.");
    }
}
Also used : ReflectionException(javax.management.ReflectionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) MBeanException(javax.management.MBeanException) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) 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) Test(org.testng.annotations.Test)

Example 37 with MemoryPoolMXBean

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

the class TestMemoryPoolMXBean method testGetAttribute.

@Test
public final void testGetAttribute() {
    MemoryPoolMXBean testImpl = testBean;
    try {
        // The 14 good public attributes...
        {
            // null return here.
            try {
                CompositeData cd = (CompositeData) mbs.getAttribute(objName, "CollectionUsage");
                if (cd != null) {
                    logger.debug("CollectionUsage : cd=" + cd);
                    MemoryUsage mu = MemoryUsage.from(cd);
                    AssertJUnit.assertTrue(mu.getCommitted() != -1);
                    AssertJUnit.assertTrue(mu.getUsed() != -1);
                    logger.debug("CollectionUsage : " + mu);
                }
            } catch (Exception e) {
                // logger.debug("**Error TestMemoryPoolMXBean testImpl: "+ testImpl.getName() +", testGetAttribute() Exception: "+ e.getClass().getName());
                Assert.fail("Unexpected exception : " + e.getClass().getName());
            }
        }
        try {
            if (testImpl.isCollectionUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThreshold");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThreshold");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (collection usage threshold not supported).");
                }
            }
        // end else collection usage threshold is not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isCollectionUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThresholdCount");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "CollectionUsageThresholdCount");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (collection usage threshold count not supported).");
                }
            }
        // end else collection usage threshold is not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            String[] names = (String[]) mbs.getAttribute(objName, "MemoryManagerNames");
            AssertJUnit.assertNotNull(names);
            for (int i = 0; i < names.length; i++) {
                String string = names[i];
                AssertJUnit.assertNotNull(string);
                AssertJUnit.assertTrue(string.length() > 0);
                logger.debug("MemoryManagerNames[" + i + "] = " + string);
            }
        // end for
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            String name = (String) mbs.getAttribute(objName, "Name");
            AssertJUnit.assertNotNull(name);
            AssertJUnit.assertTrue(name.length() > 0);
            logger.debug("Name is " + name);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            CompositeData cd = (CompositeData) mbs.getAttribute(objName, "PeakUsage");
            if (testImpl.isValid()) {
                AssertJUnit.assertNotNull(cd);
                MemoryUsage mu = MemoryUsage.from(cd);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("PeakUsage : " + mu);
            } else {
                AssertJUnit.assertNull(cd);
            }
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            String name = (String) mbs.getAttribute(objName, "Type");
            AssertJUnit.assertNotNull(name);
            AssertJUnit.assertTrue(name.length() > 0);
            logger.debug("Type is " + name);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            CompositeData cd = (CompositeData) mbs.getAttribute(objName, "Usage");
            if (testImpl.isValid()) {
                AssertJUnit.assertNotNull(cd);
                MemoryUsage mu = MemoryUsage.from(cd);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("Usage : " + mu);
            } else {
                AssertJUnit.assertNull(cd);
            }
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "UsageThreshold");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
                logger.debug("Usage threshold = " + l);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "UsageThreshold");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (usage threshold not supported).");
                }
            }
        // end else usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isUsageThresholdSupported()) {
                Long l = (Long) mbs.getAttribute(objName, "UsageThresholdCount");
                AssertJUnit.assertNotNull(l);
                AssertJUnit.assertTrue(l > -1);
                logger.debug("Usage threshold count = " + l);
            } else {
                try {
                    Long l = (Long) mbs.getAttribute(objName, "UsageThresholdCount");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (usage threshold count not supported).");
                }
            }
        // end else usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isCollectionUsageThresholdSupported()) {
                Boolean b = (Boolean) mbs.getAttribute(objName, "CollectionUsageThresholdExceeded");
                AssertJUnit.assertNotNull(b);
                logger.debug("CollectionUsageThresholdExceeded is " + b);
            } else {
                try {
                    Boolean b = (Boolean) mbs.getAttribute(objName, "CollectionUsageThresholdExceeded");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: " + "as expected (collection usage threshold exceeded not supported).");
                }
            }
        // end else collection usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            Boolean b = (Boolean) mbs.getAttribute(objName, "CollectionUsageThresholdSupported");
            AssertJUnit.assertNotNull(b);
            logger.debug("CollectionUsageThresholdSupported is " + b);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            if (testImpl.isUsageThresholdSupported()) {
                Boolean b = (Boolean) mbs.getAttribute(objName, "UsageThresholdExceeded");
                AssertJUnit.assertNotNull(b);
                logger.debug("UsageThresholdExceeded is " + b);
            } else {
                try {
                    Boolean b = (Boolean) mbs.getAttribute(objName, "UsageThresholdExceeded");
                    Assert.fail("Unreacheable code: should have thrown exception");
                } catch (Exception e) {
                    AssertJUnit.assertTrue(e instanceof javax.management.RuntimeMBeanException);
                    logger.debug("Exception occurred: as expected (usage threshold exceeded not supported).");
                }
            }
        // end else usage threshold not supported
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            Boolean b = (Boolean) mbs.getAttribute(objName, "UsageThresholdSupported");
            AssertJUnit.assertNotNull(b);
            logger.debug("UsageThresholdSupported is " + b);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        try {
            Boolean b = (Boolean) mbs.getAttribute(objName, "Valid");
            AssertJUnit.assertNotNull(b);
            logger.debug("Valid is " + b);
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
        // The 1 good IBM attribute ...
        try {
            // If not supported on the VM then permissible to get a
            // null return value.
            CompositeData cd = (CompositeData) mbs.getAttribute(objName, "PreCollectionUsage");
            if (null != cd) {
                MemoryUsage mu = MemoryUsage.from(cd);
                AssertJUnit.assertTrue(mu.getCommitted() != -1);
                AssertJUnit.assertTrue(mu.getUsed() != -1);
                logger.debug("(IBM) PreCollectionUsage : " + mu);
            }
        } catch (InstanceNotFoundException e) {
            Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
        }
    } catch (AttributeNotFoundException e) {
        Assert.fail("Unexpected AttributeNotFoundException : " + e.getMessage());
    } catch (MBeanException e) {
        Assert.fail("Unexpected MBeanException : " + e.getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException : " + e.getMessage());
    }
    // A nonexistent attribute should throw an AttributeNotFoundException
    try {
        long rpm = ((Long) (mbs.getAttribute(objName, "RPM")));
        Assert.fail("Unreacheable code: should have thrown an AttributeNotFoundException.");
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof AttributeNotFoundException);
        logger.debug("Exception occurred: as expected, no such attribute found (RPM).");
    }
    // Type mismatch should result in a casting exception
    try {
        Long bad = (Long) (mbs.getAttribute(objName, "Name"));
        Assert.fail("Unreacheable code: should have thrown a ClassCastException");
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof ClassCastException);
        logger.debug("Exception occurred: as expected, invalid type cast attempted.");
    }
}
Also used : ReflectionException(javax.management.ReflectionException) AttributeNotFoundException(javax.management.AttributeNotFoundException) 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) MBeanException(javax.management.MBeanException) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Test(org.testng.annotations.Test)

Example 38 with MemoryPoolMXBean

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

the class TestMemoryPoolMXBean method testGetMBeanInfo.

@Test
public final void testGetMBeanInfo() {
    MemoryPoolMXBean testImpl = testBean;
    MBeanInfo mbi = null;
    try {
        mbi = mbs.getMBeanInfo(objName);
    } catch (InstanceNotFoundException e) {
        Assert.fail("Unexpected InstanceNotFoundException : " + e.getMessage());
    } catch (ReflectionException e) {
        Assert.fail("Unexpected ReflectionException : " + e.getMessage());
    } catch (IntrospectionException e) {
        Assert.fail("Unexpected IntrospectionException : " + e.getMessage());
    }
    AssertJUnit.assertNotNull(mbi);
    // Now make sure that what we got back is what we expected.
    // Class name
    AssertJUnit.assertTrue(mbi.getClassName().equals(testImpl.getClass().getName()));
    // No public constructors
    MBeanConstructorInfo[] constructors = mbi.getConstructors();
    AssertJUnit.assertNotNull(constructors);
    AssertJUnit.assertTrue(constructors.length == 0);
    // One public operation - resetPeakUsage
    MBeanOperationInfo[] operations = mbi.getOperations();
    AssertJUnit.assertNotNull(operations);
    AssertJUnit.assertTrue(operations.length == 1);
    AssertJUnit.assertEquals("resetPeakUsage", operations[0].getName());
    // No notifications
    MBeanNotificationInfo[] notifications = mbi.getNotifications();
    AssertJUnit.assertNotNull(notifications);
    AssertJUnit.assertTrue(notifications.length == 0);
    // Print description and the class name (not necessarily identical).
    logger.debug("MBean description for " + testImpl.getClass().getName() + ": " + mbi.getDescription());
    // Sixteen attributes - only two are writable.
    MBeanAttributeInfo[] attributes = mbi.getAttributes();
    AssertJUnit.assertNotNull(attributes);
    AssertJUnit.assertTrue(attributes.length == 17);
    for (int i = 0; i < attributes.length; i++) {
        MBeanAttributeInfo info = attributes[i];
        AssertJUnit.assertNotNull(info);
        AllManagementTests.validateAttributeInfo(info, TestMemoryPoolMXBean.ignoredAttributes, attribs);
    }
// end for
}
Also used : ReflectionException(javax.management.ReflectionException) MBeanInfo(javax.management.MBeanInfo) MBeanOperationInfo(javax.management.MBeanOperationInfo) InstanceNotFoundException(javax.management.InstanceNotFoundException) IntrospectionException(javax.management.IntrospectionException) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) MBeanConstructorInfo(javax.management.MBeanConstructorInfo) MBeanNotificationInfo(javax.management.MBeanNotificationInfo) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Test(org.testng.annotations.Test)

Example 39 with MemoryPoolMXBean

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

the class TestMemoryPoolMXBean method testSetUsageThresholdAttribute.

@Test
public void testSetUsageThresholdAttribute() {
    MemoryPoolMXBean testImpl = testBean;
    if (testImpl.isUsageThresholdSupported()) {
        try {
            long originalUT = (Long) mbs.getAttribute(objName, "UsageThreshold");
            long newUT = originalUT + 1024;
            Attribute newUTAttr = new Attribute("UsageThreshold", new Long(newUT));
            mbs.setAttribute(objName, newUTAttr);
            AssertJUnit.assertEquals(new Long(newUT), (Long) mbs.getAttribute(objName, "UsageThreshold"));
        } 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 newUTAttr = new Attribute("UsageThreshold", new Long(100 * 1024));
            mbs.setAttribute(objName, newUTAttr);
            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 (UsageThreshold).");
        }
    }
// end else 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 40 with MemoryPoolMXBean

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

the class TestManagementFactory method testGetMemoryPoolMXBeans.

@Test
public final void testGetMemoryPoolMXBeans() {
    List<MemoryPoolMXBean> allBeans = ManagementFactory.getMemoryPoolMXBeans();
    AssertJUnit.assertNotNull(allBeans);
    AssertJUnit.assertTrue(allBeans.size() > 0);
    MemoryPoolMXBean mmb = allBeans.get(0);
    AssertJUnit.assertNotNull(mmb);
}
Also used : MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean) Test(org.testng.annotations.Test)

Aggregations

MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)107 MemoryUsage (java.lang.management.MemoryUsage)43 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)16 ArrayList (java.util.ArrayList)14 Test (org.testng.annotations.Test)11 MemoryMXBean (java.lang.management.MemoryMXBean)10 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 AttributeNotFoundException (javax.management.AttributeNotFoundException)7 IntrospectionException (javax.management.IntrospectionException)7 MBeanException (javax.management.MBeanException)7 Map (java.util.Map)6 Attribute (javax.management.Attribute)6 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)5 ThreadMXBean (java.lang.management.ThreadMXBean)5 Test (org.junit.Test)5