jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.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 GarbageCollection notification are thrown by every GarbageCollectorMXBean
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 GarbageCollectionNotificationTest
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 GarbageCollectionNotificationTest {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    45
    private static HashMap<String,Boolean> listenerInvoked = new HashMap<String,Boolean>();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    46
    static volatile long count = 0;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    47
    static volatile long number = 0;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    48
    static Object synchronizer = new Object();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    49
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    50
    static class GcListener implements NotificationListener {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    51
        public void handleNotification(Notification notif, Object handback) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    52
            String type = notif.getType();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    53
            if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    54
                GarbageCollectionNotificationInfo gcNotif =
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    55
                    GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    56
                String source = ((ObjectName)notif.getSource()).getCanonicalName();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    57
                synchronized(synchronizer) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    58
                    if(!listenerInvoked.get(source)) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    59
                            listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    60
                            count++;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    61
                            if(count >= number) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    62
                                synchronizer.notify();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    63
                            }
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
    public static void main(String[] args) throws Exception {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    71
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    72
        final Boolean isNotificationSupported = AccessController.doPrivileged (new PrivilegedAction<Boolean>() {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    73
                public Boolean run() {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    74
                    try {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    75
                        Class cl = Class.forName("sun.management.VMManagementImpl");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    76
                        Field f = cl.getDeclaredField("gcNotificationSupport");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    77
                        f.setAccessible(true);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    78
                        return f.getBoolean(null);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    79
                    } catch(ClassNotFoundException e) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    80
                        return false;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    81
                    } catch(NoSuchFieldException e) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    82
                        return false;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    83
                    } catch(IllegalAccessException e) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    84
                        return false;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    85
                    }
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
        if(!isNotificationSupported) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    89
            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
    90
            return;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    91
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    92
        final ObjectName gcMXBeanPattern =
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    93
                new ObjectName("java.lang:type=GarbageCollector,*");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    94
        Set<ObjectName> names =
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    95
                mbs.queryNames(gcMXBeanPattern, null);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    96
        if (names.isEmpty())
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    97
            throw new Exception("Test incorrect: no GC MXBeans");
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    98
        number = names.size();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
    99
        for (ObjectName n : names) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   100
            if(mbs.isInstanceOf(n,"javax.management.NotificationEmitter")) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   101
                listenerInvoked.put(n.getCanonicalName(),false);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   102
                GcListener listener = new GcListener();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   103
                mbs.addNotificationListener(n, listener, null, null);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   104
            }
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
        // Invocation of System.gc() to trigger major GC
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   107
        System.gc();
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   108
        // 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
   109
        Object data[] = new Object[32];
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   110
        for(int i = 0; i<100000000; i++) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   111
            data[i%32] = new int[8];
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   112
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   113
        int wakeup = 0;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   114
        synchronized(synchronizer) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   115
            while(count != number) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   116
                synchronizer.wait(10000);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   117
                wakeup++;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   118
                if(wakeup > 10)
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   119
                    break;
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   120
            }
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
        for (String source : listenerInvoked.keySet()) {
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   123
            if(!listenerInvoked.get(source))
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   124
                throw new Exception("Test incorrect: notifications have not been sent for "
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   125
                                    + source);
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   126
        }
8b66cd6c5ebc 7036199: Adding a notification to the implementation of GarbageCollectorMXBeans
fparain
parents:
diff changeset
   127
        System.out.println("Test passed");
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
}