Path: blob/master/test/jdk/com/sun/management/GarbageCollectorMXBean/GarbageCollectionNotificationContentTest.java
41155 views
/*1* Copyright (c) 2011, 2018, 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 GarbageCollectionNotification contents are reasonable27* @author Frederic Parain28* @requires vm.opt.ExplicitGCInvokesConcurrent == null | vm.opt.ExplicitGCInvokesConcurrent == false29* @modules java.management/sun.management30* jdk.management31* @run main/othervm -Xms64m -Xmx64m GarbageCollectionNotificationContentTest32*/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 GarbageCollectionNotificationContentTest {46private static HashMap<String,GarbageCollectionNotificationInfo> listenerInvoked47= new HashMap<String,GarbageCollectionNotificationInfo>();48static volatile long count = 0;49static volatile long number = 0;50static Object synchronizer = new Object();5152static class GcListener implements NotificationListener {53public void handleNotification(Notification notif, Object handback) {54String type = notif.getType();55if (type.equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {56GarbageCollectionNotificationInfo gcNotif =57GarbageCollectionNotificationInfo.from((CompositeData) notif.getUserData());58String source = ((ObjectName)notif.getSource()).getCanonicalName();59synchronized(synchronizer) {60if(listenerInvoked.get(source) == null) {61listenerInvoked.put(((ObjectName)notif.getSource()).getCanonicalName(),gcNotif);62count++;63if(count >= number) {64synchronizer.notify();65}66}67}68}69}70}7172public static void main(String[] args) throws Exception {73MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();74final boolean isNotificationSupported =75sun.management.ManagementFactoryHelper.getVMManagement().isGcNotificationSupported();7677if(!isNotificationSupported) {78System.out.println("GC Notification not supported by the JVM, test skipped");79return;80}81final ObjectName gcMXBeanPattern =82new ObjectName("java.lang:type=GarbageCollector,*");83Set<ObjectName> names =84mbs.queryNames(gcMXBeanPattern, null);85if (names.isEmpty())86throw new Exception("Test incorrect: no GC MXBeans");87number = names.size();88for (ObjectName n : names) {89if(mbs.isInstanceOf(n,"javax.management.NotificationEmitter")) {90listenerInvoked.put(n.getCanonicalName(),null);91GcListener listener = new GcListener();92mbs.addNotificationListener(n, listener, null, null);93}94}95// Invocation of System.gc() to trigger major GC96System.gc();97// Allocation of many short living and small objects to trigger minor GC98Object data[] = new Object[32];99for(int i = 0; i<10000000; i++) {100data[i%32] = new int[8];101}102int wakeup = 0;103synchronized(synchronizer) {104while(count != number) {105synchronizer.wait(10000);106wakeup++;107if(wakeup > 10)108break;109}110}111for (GarbageCollectionNotificationInfo notif : listenerInvoked.values() ) {112checkGarbageCollectionNotificationInfoContent(notif);113}114System.out.println("Test passed");115}116117private static void checkGarbageCollectionNotificationInfoContent(GarbageCollectionNotificationInfo notif) throws Exception {118System.out.println("GC notification for "+notif.getGcName());119System.out.print("Action: "+notif.getGcAction());120System.out.println(" Cause: "+notif.getGcCause());121GcInfo info = notif.getGcInfo();122System.out.print("GC Info #" + info.getId());123System.out.print(" start:" + info.getStartTime());124System.out.print(" end:" + info.getEndTime());125System.out.println(" (" + info.getDuration() + "ms)");126Map<String, MemoryUsage> usage = info.getMemoryUsageBeforeGc();127128List<String> pnames = new ArrayList<String>();129for (Map.Entry entry : usage.entrySet() ) {130String poolname = (String) entry.getKey();131pnames.add(poolname);132MemoryUsage busage = (MemoryUsage) entry.getValue();133MemoryUsage ausage = (MemoryUsage) info.getMemoryUsageAfterGc().get(poolname);134if (ausage == null) {135throw new RuntimeException("After Gc Memory does not exist" +136" for " + poolname);137}138System.out.println("Usage for pool " + poolname);139System.out.println(" Before GC: " + busage);140System.out.println(" After GC: " + ausage);141142checkMemoryUsage(poolname, busage, ausage);143}144145// check if memory usage for all memory pools are returned146List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();147for (MemoryPoolMXBean p : pools ) {148if (!pnames.contains(p.getName())) {149throw new RuntimeException("GcInfo does not contain " +150"memory usage for pool " + p.getName());151}152}153}154155private static void checkMemoryUsage(String poolname, MemoryUsage busage, MemoryUsage ausage) throws Exception {156if (poolname.contains("Eden Space") && busage.getUsed() > 0) {157// Used size at Eden Space should be decreased or158if (busage.getUsed() <= ausage.getUsed()) {159throw new RuntimeException("Used size at Eden Space should be decreased.");160}161}162}163}164165166