Path: blob/master/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.java
41155 views
/*1* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 703619926* @summary Check that GarbageCollection notification are thrown by every GarbageCollectorMXBean27* @author Frederic Parain28* @requires vm.opt.ExplicitGCInvokesConcurrent == null | vm.opt.ExplicitGCInvokesConcurrent == false29* @modules java.management/sun.management30* jdk.management31* @run main/othervm GarbageCollectionNotificationTest32*/3334import java.util.*;35import java.lang.management.*;36import java.lang.reflect.*;37import javax.management.*;38import javax.management.openmbean.*;39import com.sun.management.GarbageCollectionNotificationInfo;40import com.sun.management.GcInfo;41import java.security.AccessController;42import java.security.PrivilegedAction;43import java.lang.reflect.Field;4445public class GarbageCollectionNotificationTest {46private static HashMap<String,Boolean> listenerInvoked = new HashMap<String,Boolean>();47static volatile long count = 0;48static volatile long number = 0;49static Object synchronizer = new Object();5051static class GcListener implements NotificationListener {52public void handleNotification(Notification notif, Object handback) {53String type = notif.getType();54if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {55GarbageCollectionNotificationInfo gcNotif =56GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());57String source = ((ObjectName)notif.getSource()).getCanonicalName();58synchronized(synchronizer) {59if(!listenerInvoked.get(source)) {60listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);61count++;62if(count >= number) {63synchronizer.notify();64}65}66}67}68}69}7071public static void main(String[] args) throws Exception {72MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();73final boolean isNotificationSupported =74sun.management.ManagementFactoryHelper.getVMManagement().isGcNotificationSupported();7576if(!isNotificationSupported) {77System.out.println("GC Notification not supported by the JVM, test skipped");78return;79}80final ObjectName gcMXBeanPattern =81new ObjectName("java.lang:type=GarbageCollector,*");82Set<ObjectName> names =83mbs.queryNames(gcMXBeanPattern, null);84if (names.isEmpty())85throw new Exception("Test incorrect: no GC MXBeans");86number = names.size();87for (ObjectName n : names) {88if(mbs.isInstanceOf(n,"javax.management.NotificationEmitter")) {89listenerInvoked.put(n.getCanonicalName(),false);90GcListener listener = new GcListener();91mbs.addNotificationListener(n, listener, null, null);92}93}94// Invocation of System.gc() to trigger major GC95System.gc();96// Allocation of many short living and small objects to trigger minor GC97Object data[] = new Object[32];98for(int i = 0; i<100000000; i++) {99data[i%32] = new int[8];100}101int wakeup = 0;102synchronized(synchronizer) {103while(count != number) {104synchronizer.wait(10000);105wakeup++;106if(wakeup > 10)107break;108}109}110for (String source : listenerInvoked.keySet()) {111if(!listenerInvoked.get(source))112throw new Exception("Test incorrect: notifications have not been sent for "113+ source);114}115System.out.println("Test passed");116}117}118119120