Search in sources :

Example 1 with InvalidKeyException

use of javax.management.openmbean.InvalidKeyException in project dsl-devkit by dsldevkit.

the class VirtualMachineTracer method start.

/**
 * Installs a listener that will publish all full GC events as {@link FullGarbageCollectionEvent} objects.
 */
public void start() {
    // This code only works with Oracle HotSpot JVM as there is no standard API to retrieve information about GC events
    if (!isHotSpotVM()) {
        return;
    }
    long vmStartTime = getApproximateNanoStartTime();
    for (GarbageCollectorMXBean gcBean : ManagementFactory.getGarbageCollectorMXBeans()) {
        Class<? extends TraceEvent> eventType = // $NON-NLS-1$ //$NON-NLS-2$
        gcBean.getName().equals("ConcurrentMarkSweep") || gcBean.getName().equals("MarkSweepCompact") ? FullGarbageCollectionEvent.class : MinorGarbageCollectionEvent.class;
        NotificationEmitter emitter = (NotificationEmitter) gcBean;
        NotificationListener listener = new NotificationListener() {

            @Override
            public void handleNotification(final Notification notification, final Object handback) {
                try {
                    // we only handle GARBAGE_COLLECTION_NOTIFICATION notifications here
                    if (notification.getType().equals("com.sun.management.gc.notification")) {
                        // $NON-NLS-1$
                        CompositeData cd = (CompositeData) notification.getUserData();
                        // $NON-NLS-1$
                        String gcAction = (String) cd.get("gcAction");
                        // $NON-NLS-1$
                        String gcCause = (String) cd.get("gcCause");
                        // $NON-NLS-1$
                        CompositeData gcInfo = (CompositeData) cd.get("gcInfo");
                        // $NON-NLS-1$
                        long startTime = TimeUnit.NANOSECONDS.convert((Long) gcInfo.get("startTime"), TimeUnit.MILLISECONDS);
                        // $NON-NLS-1$
                        long duration = TimeUnit.NANOSECONDS.convert((Long) gcInfo.get("duration"), TimeUnit.MILLISECONDS);
                        if (duration > 0) {
                            // "startTime" and "duration" are relative to VM start time
                            traceSet.started(eventType, vmStartTime + startTime, gcAction, gcCause);
                            traceSet.ended(eventType, vmStartTime + startTime + duration);
                        }
                    }
                } catch (InvalidKeyException e) {
                // ignore
                }
            }
        };
        emitter.addNotificationListener(listener, null, null);
        gcListenerMap.put(emitter, listener);
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) CompositeData(javax.management.openmbean.CompositeData) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) InvalidKeyException(javax.management.openmbean.InvalidKeyException) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener)

Example 2 with InvalidKeyException

use of javax.management.openmbean.InvalidKeyException in project openj9 by eclipse.

the class TestManagementUtils method testToStackTraceElementCompositeData.

/*
	 * Test method for 'com.ibm.lang.management.ManagementUtils.toStackTraceElementCompositeData(StackTraceElement)'
	 */
@Test
public final void testToStackTraceElementCompositeData() {
    // Null file name
    StackTraceElement st = new StackTraceElement("foo.bar.DeclaringClass", "methodName", null, 42);
    CompositeData cd = TestUtil.toCompositeData(st);
    int numValues = TestMisc.isJava9 ? 8 : 5;
    // Examine the returned CompositeData
    AssertJUnit.assertEquals(numValues, cd.values().size());
    AssertJUnit.assertEquals("foo.bar.DeclaringClass", cd.get("className"));
    AssertJUnit.assertEquals("methodName", cd.get("methodName"));
    AssertJUnit.assertNull(cd.get("fileName"));
    AssertJUnit.assertEquals(42, cd.get("lineNumber"));
    AssertJUnit.assertFalse((Boolean) cd.get("nativeMethod"));
    try {
        AssertJUnit.assertNull(cd.get("madeUpAttribute"));
        Assert.fail("Should have thrown an exception");
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof InvalidKeyException);
    }
    // Non-null file name
    st = new StackTraceElement("foo.bar.DeclaringClass", "methodName", "DeclaringClass.java", -1);
    cd = TestUtil.toCompositeData(st);
    AssertJUnit.assertEquals(numValues, cd.values().size());
    AssertJUnit.assertEquals("foo.bar.DeclaringClass", cd.get("className"));
    AssertJUnit.assertEquals("methodName", cd.get("methodName"));
    AssertJUnit.assertNotNull(cd.get("fileName"));
    AssertJUnit.assertEquals("DeclaringClass.java", cd.get("fileName"));
    AssertJUnit.assertEquals(-1, cd.get("lineNumber"));
    AssertJUnit.assertFalse((Boolean) cd.get("nativeMethod"));
    try {
        AssertJUnit.assertNull(cd.get("madeUpAttribute"));
        Assert.fail("Should have thrown an exception");
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof InvalidKeyException);
    }
    // Native method
    st = new StackTraceElement("foo.bar.DeclaringClass", "methodName", "DeclaringClass.java", -2);
    cd = TestUtil.toCompositeData(st);
    // Examine the returned CompositeData
    AssertJUnit.assertEquals(numValues, cd.values().size());
    AssertJUnit.assertEquals("foo.bar.DeclaringClass", cd.get("className"));
    AssertJUnit.assertEquals("methodName", cd.get("methodName"));
    AssertJUnit.assertNotNull(cd.get("fileName"));
    AssertJUnit.assertEquals("DeclaringClass.java", cd.get("fileName"));
    AssertJUnit.assertEquals(-2, cd.get("lineNumber"));
    AssertJUnit.assertTrue((Boolean) cd.get("nativeMethod"));
    try {
        AssertJUnit.assertNull(cd.get("madeUpAttribute"));
        Assert.fail("Should have thrown an exception");
    } catch (Exception e) {
        AssertJUnit.assertTrue(e instanceof InvalidKeyException);
    }
}
Also used : CompositeData(javax.management.openmbean.CompositeData) InvalidKeyException(javax.management.openmbean.InvalidKeyException) InvalidKeyException(javax.management.openmbean.InvalidKeyException) Test(org.testng.annotations.Test)

Aggregations

CompositeData (javax.management.openmbean.CompositeData)2 InvalidKeyException (javax.management.openmbean.InvalidKeyException)2 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)1 Notification (javax.management.Notification)1 NotificationEmitter (javax.management.NotificationEmitter)1 NotificationListener (javax.management.NotificationListener)1 Test (org.testng.annotations.Test)1