jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java
author ykantser
Wed, 03 Dec 2014 11:56:02 +0100
changeset 28100 d3f16ee13562
parent 9698 8b66cd6c5ebc
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
8044591: [TESTBUG] com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationp[Content]Test.java fail when -XX:+ExplicitGCInvokesConcurrent Reviewed-by: sla, fparain
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9698
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     1
/*
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     4
 *
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     8
 *
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    13
 * accompanied this code).
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    14
 *
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    18
 *
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    21
 * questions.
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    22
 */
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    23
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    24
/*
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    25
 * @test
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    26
 * @bug     7036199
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    27
 * @summary Check that GarbageCollectionNotification contents are reasonable
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    28
 * @author  Frederic Parain
28100
d3f16ee13562 8044591: [TESTBUG] com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationp[Content]Test.java fail when -XX:+ExplicitGCInvokesConcurrent
ykantser
parents: 9698
diff changeset
    29
 * @requires vm.opt.ExplicitGCInvokesConcurrent == null | vm.opt.ExplicitGCInvokesConcurrent == false
9698
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    30
 * @run     main/othervm GarbageCollectionNotificationContentTest
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    31
 */
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    32
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    33
import java.util.*;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    34
import java.lang.management.*;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    35
import java.lang.reflect.*;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    36
import javax.management.*;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    37
import javax.management.openmbean.*;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    38
import com.sun.management.GarbageCollectionNotificationInfo;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    39
import com.sun.management.GcInfo;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    40
import java.security.AccessController;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    41
import java.security.PrivilegedAction;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    42
import java.lang.reflect.Field;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    43
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    44
public class GarbageCollectionNotificationContentTest {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    45
    private static HashMap<String,GarbageCollectionNotificationInfo> listenerInvoked
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    46
        = new HashMap<String,GarbageCollectionNotificationInfo>();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    47
    static volatile long count = 0;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    48
    static volatile long number = 0;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    49
    static Object synchronizer = new Object();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    50
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    51
    static class GcListener implements NotificationListener {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    52
        public void handleNotification(Notification notif, Object handback) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    53
            String type = notif.getType();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    54
            if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    55
                GarbageCollectionNotificationInfo gcNotif =
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    56
                    GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    57
                String source = ((ObjectName)notif.getSource()).getCanonicalName();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    58
                synchronized(synchronizer) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    59
                    if(listenerInvoked.get(source) == null) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    60
                            listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    61
                            count++;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    62
                            if(count >= number) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    63
                                synchronizer.notify();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    64
                            }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    65
                    }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    66
                }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    67
            }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    68
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    69
    }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    70
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    71
    public static void main(String[] args) throws Exception {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    72
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    73
        final Boolean isNotificationSupported = AccessController.doPrivileged (new PrivilegedAction<Boolean>() {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    74
                public Boolean run() {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    75
                    try {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    76
                        Class cl = Class.forName("sun.management.VMManagementImpl");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    77
                        Field f = cl.getDeclaredField("gcNotificationSupport");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    78
                        f.setAccessible(true);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    79
                        return f.getBoolean(null);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    80
                    } catch(ClassNotFoundException e) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    81
                        return false;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    82
                    } catch(NoSuchFieldException e) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    83
                        return false;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    84
                    } catch(IllegalAccessException e) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    85
                        return false;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    86
                    }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    87
                }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    88
            });
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    89
        if(!isNotificationSupported) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    90
            System.out.println("GC Notification not supported by the JVM, test skipped");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    91
            return;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    92
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    93
        final ObjectName gcMXBeanPattern =
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    94
                new ObjectName("java.lang:type=GarbageCollector,*");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    95
        Set<ObjectName> names =
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    96
                mbs.queryNames(gcMXBeanPattern, null);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    97
        if (names.isEmpty())
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    98
            throw new Exception("Test incorrect: no GC MXBeans");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    99
        number = names.size();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   100
        for (ObjectName n : names) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   101
            if(mbs.isInstanceOf(n,"javax.management.NotificationEmitter")) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   102
                listenerInvoked.put(n.getCanonicalName(),null);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   103
                GcListener listener = new GcListener();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   104
                mbs.addNotificationListener(n, listener, null, null);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   105
            }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   106
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   107
        // Invocation of System.gc() to trigger major GC
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   108
        System.gc();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   109
        // Allocation of many short living and small objects to trigger minor GC
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   110
        Object data[] = new Object[32];
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   111
        for(int i = 0; i<100000000; i++) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   112
            data[i%32] = new int[8];
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   113
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   114
        int wakeup = 0;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   115
        synchronized(synchronizer) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   116
            while(count != number) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   117
                synchronizer.wait(10000);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   118
                wakeup++;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   119
                if(wakeup > 10)
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   120
                    break;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   121
            }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   122
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   123
        for (GarbageCollectionNotificationInfo notif : listenerInvoked.values() ) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   124
            checkGarbageCollectionNotificationInfoContent(notif);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   125
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   126
        System.out.println("Test passed");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   127
    }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   128
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   129
    private static void checkGarbageCollectionNotificationInfoContent(GarbageCollectionNotificationInfo notif) throws Exception {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   130
        System.out.println("GC notification for "+notif.getGcName());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   131
        System.out.print("Action: "+notif.getGcAction());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   132
        System.out.println(" Cause: "+notif.getGcCause());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   133
        GcInfo info = notif.getGcInfo();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   134
        System.out.print("GC Info #" + info.getId());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   135
        System.out.print(" start:" + info.getStartTime());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   136
        System.out.print(" end:" + info.getEndTime());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   137
        System.out.println(" (" + info.getDuration() + "ms)");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   138
        Map<String, MemoryUsage> usage = info.getMemoryUsageBeforeGc();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   139
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   140
        List<String> pnames = new ArrayList<String>();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   141
        for (Map.Entry entry : usage.entrySet() ) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   142
            String poolname = (String) entry.getKey();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   143
            pnames.add(poolname);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   144
            MemoryUsage busage = (MemoryUsage) entry.getValue();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   145
            MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   146
            if (ausage == null) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   147
                throw new RuntimeException("After Gc Memory does not exist" +
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   148
                    " for " + poolname);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   149
            }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   150
            System.out.println("Usage for pool " + poolname);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   151
            System.out.println("   Before GC: " + busage);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   152
            System.out.println("   After GC: " + ausage);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   153
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   154
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   155
        // check if memory usage for all memory pools are returned
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   156
        List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   157
        for (MemoryPoolMXBean p : pools ) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   158
            if (!pnames.contains(p.getName())) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   159
                throw new RuntimeException("GcInfo does not contain " +
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   160
                    "memory usage for pool " + p.getName());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   161
            }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   162
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   163
    }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   164
}