1 /* 2 * Copyright (c) 2018, Red Hat, Inc. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 /* 26 * @test TestChurnNotifications 27 * @summary Check that MX notifications are reported for all cycles 28 * @requires vm.gc.Shenandoah & !vm.graal.enabled 29 * 30 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 31 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 32 * -XX:+ShenandoahDegeneratedGC -Dprecise=true 33 * TestChurnNotifications 34 * 35 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 36 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive 37 * -XX:-ShenandoahDegeneratedGC -Dprecise=true 38 * TestChurnNotifications 39 */ 40 41 /* 42 * @test TestChurnNotifications 43 * @summary Check that MX notifications are reported for all cycles 44 * @requires vm.gc.Shenandoah & !vm.graal.enabled 45 * 46 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 47 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive 48 * -Dprecise=false 49 * TestChurnNotifications 50 * 51 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 52 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive 53 * -Dprecise=false 54 * TestChurnNotifications 55 * 56 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 57 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static 58 * -Dprecise=false 59 * TestChurnNotifications 60 * 61 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 62 * -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact 63 * -Dprecise=false 64 * TestChurnNotifications 65 */ 66 67 /* 68 * @test TestChurnNotifications 69 * @summary Check that MX notifications are reported for all cycles 70 * @requires vm.gc.Shenandoah & !vm.graal.enabled 71 * 72 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 73 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive 74 * -Dprecise=false 75 * TestChurnNotifications 76 * 77 * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions 78 * -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu 79 * -Dprecise=false 80 * TestChurnNotifications 81 */ 82 83 import java.util.*; 84 import java.util.concurrent.atomic.*; 85 import javax.management.*; 86 import java.lang.management.*; 87 import javax.management.openmbean.*; 88 89 import com.sun.management.GarbageCollectionNotificationInfo; 90 91 public class TestChurnNotifications { 92 93 static final long HEAP_MB = 128; // adjust for test configuration above 94 static final long TARGET_MB = Long.getLong("target", 8_000); // 8 Gb allocation 95 96 // Should we track the churn precisely? 97 // Precise tracking is only reliable when GC is fully stop-the-world. Otherwise, 98 // we cannot tell, looking at heap used before/after, what was the GC churn. 99 static final boolean PRECISE = Boolean.getBoolean("precise"); 100 101 static final long M = 1024 * 1024; 102 103 static volatile Object sink; 104 105 public static void main(String[] args) throws Exception { 106 final AtomicLong churnBytes = new AtomicLong(); 107 108 NotificationListener listener = new NotificationListener() { 109 @Override 110 public void handleNotification(Notification n, Object o) { 111 if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) { 112 GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData()); 113 Map<String, MemoryUsage> mapBefore = info.getGcInfo().getMemoryUsageBeforeGc(); 114 Map<String, MemoryUsage> mapAfter = info.getGcInfo().getMemoryUsageAfterGc(); 115 116 MemoryUsage before = mapBefore.get("Shenandoah"); 117 MemoryUsage after = mapAfter.get("Shenandoah"); 118 119 if ((before != null) && (after != null)) { 120 long diff = before.getUsed() - after.getUsed(); 121 if (diff > 0) { 122 churnBytes.addAndGet(diff); 123 } 124 } 125 } 126 } 127 }; 128 129 for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) { 130 ((NotificationEmitter) bean).addNotificationListener(listener, null, null); 131 } 132 133 final int size = 100_000; 134 long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size); 135 136 long mem = count * (16 + 4 * size); 137 138 for (int c = 0; c < count; c++) { 139 sink = new int[size]; 140 } 141 142 System.gc(); 143 144 Thread.sleep(1000); 145 146 long actual = churnBytes.get(); 147 148 long minExpected = PRECISE ? (mem - HEAP_MB * 1024 * 1024) : 1; 149 long maxExpected = mem + HEAP_MB * 1024 * 1024; 150 151 String msg = "Expected = [" + minExpected / M + "; " + maxExpected / M + "] (" + mem / M + "), actual = " + actual / M; 152 if (minExpected < actual && actual < maxExpected) { 153 System.out.println(msg); 154 } else { 155 throw new IllegalStateException(msg); 156 } 157 } 158 }