Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/mxbeans/TestChurnNotifications.java
32285 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223/*24* @test TestChurnNotifications25* @summary Check that MX notifications are reported for all cycles26* @key gc27*28* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions29* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive30* -XX:+ShenandoahDegeneratedGC -Dprecise=true31* TestChurnNotifications32*33* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions34* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive35* -XX:-ShenandoahDegeneratedGC -Dprecise=true36* TestChurnNotifications37*/3839/*40* @test TestChurnNotifications41* @summary Check that MX notifications are reported for all cycles42* @key gc43*44* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions45* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive46* -Dprecise=false47* TestChurnNotifications48*/4950/*51* @test TestChurnNotifications52* @summary Check that MX notifications are reported for all cycles53* @key gc54*55* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions56* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive57* -Dprecise=false58* TestChurnNotifications59*/6061/*62* @test TestChurnNotifications63* @summary Check that MX notifications are reported for all cycles64* @key gc65*66* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions67* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static68* -Dprecise=false69* TestChurnNotifications70*/7172/*73* @test TestChurnNotifications74* @summary Check that MX notifications are reported for all cycles75* @key gc76*77* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions78* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact79* -Dprecise=false80* TestChurnNotifications81*/8283/*84* @test TestChurnNotifications85* @summary Check that MX notifications are reported for all cycles86* @key gc87*88* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions89* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive90* -Dprecise=false91* TestChurnNotifications92*93* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions94* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu95* -Dprecise=false96* TestChurnNotifications97*/9899import java.util.*;100import java.util.concurrent.atomic.*;101import javax.management.*;102import java.lang.management.*;103import javax.management.openmbean.*;104105import com.sun.management.GarbageCollectionNotificationInfo;106107public class TestChurnNotifications {108109static final long HEAP_MB = 128; // adjust for test configuration above110static final long TARGET_MB = Long.getLong("target", 2_000); // 2 Gb allocation111112// Should we track the churn precisely?113// Precise tracking is only reliable when GC is fully stop-the-world. Otherwise,114// we cannot tell, looking at heap used before/after, what was the GC churn.115static final boolean PRECISE = Boolean.getBoolean("precise");116117static final long M = 1024 * 1024;118119static volatile Object sink;120121public static void main(String[] args) throws Exception {122final AtomicLong churnBytes = new AtomicLong();123124NotificationListener listener = new NotificationListener() {125@Override126public void handleNotification(Notification n, Object o) {127if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {128GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());129Map<String, MemoryUsage> mapBefore = info.getGcInfo().getMemoryUsageBeforeGc();130Map<String, MemoryUsage> mapAfter = info.getGcInfo().getMemoryUsageAfterGc();131132MemoryUsage before = mapBefore.get("Shenandoah");133MemoryUsage after = mapAfter.get("Shenandoah");134135if ((before != null) && (after != null)) {136long diff = before.getUsed() - after.getUsed();137if (diff > 0) {138churnBytes.addAndGet(diff);139}140}141}142}143};144145for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {146((NotificationEmitter) bean).addNotificationListener(listener, null, null);147}148149final int size = 100_000;150long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size);151152long mem = count * (16 + 4 * size);153154for (int c = 0; c < count; c++) {155sink = new int[size];156}157158System.gc();159160Thread.sleep(1000);161162long actual = churnBytes.get();163164long minExpected = PRECISE ? (mem - HEAP_MB * 1024 * 1024) : 1;165long maxExpected = mem + HEAP_MB * 1024 * 1024;166167String msg = "Expected = [" + minExpected / M + "; " + maxExpected / M + "] (" + mem / M + "), actual = " + actual / M;168if (minExpected <= actual && actual <= maxExpected) {169System.out.println(msg);170} else {171throw new IllegalStateException(msg);172}173}174}175176177