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