Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/mxbeans/TestPauseNotifications.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 TestPauseNotifications25* @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:+ShenandoahDegeneratedGC31* TestPauseNotifications32*33* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions34* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive35* -XX:-ShenandoahDegeneratedGC36* TestPauseNotifications37*/3839/*40* @test TestPauseNotifications41* @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* TestPauseNotifications47*/4849/*50* @test TestPauseNotifications51* @summary Check that MX notifications are reported for all cycles52* @key gc53*54* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions55* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive56* TestPauseNotifications57*/5859/*60* @test TestPauseNotifications61* @summary Check that MX notifications are reported for all cycles62* @key gc63*64* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions65* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static66* TestPauseNotifications67*/6869/*70* @test TestPauseNotifications71* @summary Check that MX notifications are reported for all cycles72* @key gc73*74* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions75* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact76* TestPauseNotifications77*/7879/*80* @test TestPauseNotifications81* @summary Check that MX notifications are reported for all cycles82* @key gc83*84* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions85* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive86* TestPauseNotifications87*88* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions89* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu90* TestPauseNotifications91*/9293import java.util.*;94import java.util.concurrent.atomic.*;95import javax.management.*;96import java.lang.management.*;97import javax.management.openmbean.*;98import com.sun.management.GarbageCollectionNotificationInfo;99100public class TestPauseNotifications {101102static final long HEAP_MB = 128; // adjust for test configuration above103static final long TARGET_MB = Long.getLong("target", 2_000); // 2 Gb allocation104105static volatile Object sink;106107public static void main(String[] args) throws Exception {108final AtomicLong pausesDuration = new AtomicLong();109final AtomicLong cyclesDuration = new AtomicLong();110111NotificationListener listener = new NotificationListener() {112@Override113public void handleNotification(Notification n, Object o) {114if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {115GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());116117long d = info.getGcInfo().getDuration();118119String name = info.getGcName();120if (name.contains("Shenandoah")) {121if (name.equals("Shenandoah Pauses")) {122pausesDuration.addAndGet(d);123} else if (name.equals("Shenandoah Cycles")) {124cyclesDuration.addAndGet(d);125} else {126throw new IllegalStateException("Unknown name: " + name);127}128}129}130}131};132133for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {134((NotificationEmitter) bean).addNotificationListener(listener, null, null);135}136137final int size = 100_000;138long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size);139140for (int c = 0; c < count; c++) {141sink = new int[size];142}143144Thread.sleep(1000);145146long pausesActual = pausesDuration.get();147long cyclesActual = cyclesDuration.get();148149long minExpected = 1;150long maxExpected = Long.MAX_VALUE;151152{153String msg = "Pauses expected = [" + minExpected + "; " + maxExpected + "], actual = " + pausesActual;154if (minExpected <= pausesActual && pausesActual <= maxExpected) {155System.out.println(msg);156} else {157throw new IllegalStateException(msg);158}159}160161{162String msg = "Cycles expected = [" + minExpected + "; " + maxExpected + "], actual = " + cyclesActual;163if (minExpected <= cyclesActual && cyclesActual <= maxExpected) {164System.out.println(msg);165} else {166throw new IllegalStateException(msg);167}168}169170{171String msg = "Cycle duration (" + cyclesActual + "), pause duration (" + pausesActual + ")";172if (pausesActual <= cyclesActual) {173System.out.println(msg);174} else {175throw new IllegalStateException(msg);176}177}178}179}180181182