Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationTest.java
38855 views
/*1* Copyright (c) 2011, 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* @run main/othervm GarbageCollectionNotificationTest29*/3031import java.util.*;32import java.lang.management.*;33import java.lang.reflect.*;34import javax.management.*;35import javax.management.openmbean.*;36import com.sun.management.GarbageCollectionNotificationInfo;37import com.sun.management.GcInfo;38import java.security.AccessController;39import java.security.PrivilegedAction;40import java.lang.reflect.Field;4142public class GarbageCollectionNotificationTest {43private static HashMap<String,Boolean> listenerInvoked = new HashMap<String,Boolean>();44static volatile long count = 0;45static volatile long number = 0;46static Object synchronizer = new Object();4748static class GcListener implements NotificationListener {49public void handleNotification(Notification notif, Object handback) {50String type = notif.getType();51if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {52GarbageCollectionNotificationInfo gcNotif =53GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());54String source = ((ObjectName)notif.getSource()).getCanonicalName();55synchronized(synchronizer) {56if(!listenerInvoked.get(source)) {57listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),true);58count++;59if(count >= number) {60synchronizer.notify();61}62}63}64}65}66}6768public static void main(String[] args) throws Exception {69MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();70final Boolean isNotificationSupported = AccessController.doPrivileged (new PrivilegedAction<Boolean>() {71public Boolean run() {72try {73Class cl = Class.forName("sun.management.VMManagementImpl");74Field f = cl.getDeclaredField("gcNotificationSupport");75f.setAccessible(true);76return f.getBoolean(null);77} catch(ClassNotFoundException e) {78return false;79} catch(NoSuchFieldException e) {80return false;81} catch(IllegalAccessException e) {82return false;83}84}85});86if(!isNotificationSupported) {87System.out.println("GC Notification not supported by the JVM, test skipped");88return;89}90final ObjectName gcMXBeanPattern =91new ObjectName("java.lang:type=GarbageCollector,*");92Set<ObjectName> names =93mbs.queryNames(gcMXBeanPattern, null);94if (names.isEmpty())95throw new Exception("Test incorrect: no GC MXBeans");96number = names.size();97for (ObjectName n : names) {98if(mbs.isInstanceOf(n,"javax.management.NotificationEmitter")) {99listenerInvoked.put(n.getCanonicalName(),false);100GcListener listener = new GcListener();101mbs.addNotificationListener(n, listener, null, null);102}103}104// Invocation of System.gc() to trigger major GC105System.gc();106// Allocation of many short living and small objects to trigger minor GC107Object data[] = new Object[32];108for(int i = 0; i<100000000; i++) {109data[i%32] = new int[8];110}111int wakeup = 0;112synchronized(synchronizer) {113while(count != number) {114synchronizer.wait(10000);115wakeup++;116if(wakeup > 10)117break;118}119}120for (String source : listenerInvoked.keySet()) {121if(!listenerInvoked.get(source))122throw new Exception("Test incorrect: notifications have not been sent for "123+ source);124}125System.out.println("Test passed");126}127}128129130