Path: blob/master/test/hotspot/jtreg/gc/shenandoah/mxbeans/TestPauseNotifications.java
41153 views
/*1* Copyright (c) 2018, Red Hat, Inc. 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*22*/2324/*25* @test TestPauseNotifications26* @summary Check that MX notifications are reported for all cycles27* @requires vm.gc.Shenandoah28*29* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions30* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive31* -XX:+ShenandoahDegeneratedGC32* TestPauseNotifications33*34* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions35* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive36* -XX:-ShenandoahDegeneratedGC37* TestPauseNotifications38*/3940/*41* @test TestPauseNotifications42* @summary Check that MX notifications are reported for all cycles43* @requires vm.gc.Shenandoah44*45* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions46* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive47* TestPauseNotifications48*/4950/*51* @test TestPauseNotifications52* @summary Check that MX notifications are reported for all cycles53* @requires vm.gc.Shenandoah54*55* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions56* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive57* TestPauseNotifications58*/5960/*61* @test TestPauseNotifications62* @summary Check that MX notifications are reported for all cycles63* @requires vm.gc.Shenandoah64*65* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions66* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static67* TestPauseNotifications68*/6970/*71* @test TestPauseNotifications72* @summary Check that MX notifications are reported for all cycles73* @requires vm.gc.Shenandoah74*75* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions76* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact77* TestPauseNotifications78*/7980/*81* @test TestPauseNotifications82* @summary Check that MX notifications are reported for all cycles83* @requires vm.gc.Shenandoah84*85* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions86* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive87* TestPauseNotifications88*89* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions90* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu91* TestPauseNotifications92*/9394import java.util.*;95import java.util.concurrent.atomic.*;96import javax.management.*;97import java.lang.management.*;98import javax.management.openmbean.*;99100import com.sun.management.GarbageCollectionNotificationInfo;101102public class TestPauseNotifications {103104static final long HEAP_MB = 128; // adjust for test configuration above105static final long TARGET_MB = Long.getLong("target", 2_000); // 2 Gb allocation106107static volatile Object sink;108109public static void main(String[] args) throws Exception {110final AtomicLong pausesDuration = new AtomicLong();111final AtomicLong cyclesDuration = new AtomicLong();112113NotificationListener listener = new NotificationListener() {114@Override115public void handleNotification(Notification n, Object o) {116if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {117GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());118119System.out.println(info.getGcInfo().toString());120System.out.println(info.getGcName());121System.out.println();122123long d = info.getGcInfo().getDuration();124125String name = info.getGcName();126if (name.contains("Shenandoah")) {127if (name.equals("Shenandoah Pauses")) {128pausesDuration.addAndGet(d);129} else if (name.equals("Shenandoah Cycles")) {130cyclesDuration.addAndGet(d);131} else {132throw new IllegalStateException("Unknown name: " + name);133}134}135}136}137};138139for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {140((NotificationEmitter) bean).addNotificationListener(listener, null, null);141}142143final int size = 100_000;144long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size);145146for (int c = 0; c < count; c++) {147sink = new int[size];148}149150// Wait until notifications start arriving, and then wait some more151// to catch the ones arriving late.152while (pausesDuration.get() == 0) {153Thread.sleep(1000);154}155Thread.sleep(5000);156157long pausesActual = pausesDuration.get();158long cyclesActual = cyclesDuration.get();159160long minExpected = 1;161long maxExpected = Long.MAX_VALUE;162163{164String msg = "Pauses expected = [" + minExpected + "; " + maxExpected + "], actual = " + pausesActual;165if (minExpected <= pausesActual && pausesActual <= maxExpected) {166System.out.println(msg);167} else {168throw new IllegalStateException(msg);169}170}171172{173String msg = "Cycles expected = [" + minExpected + "; " + maxExpected + "], actual = " + cyclesActual;174if (minExpected <= cyclesActual && cyclesActual <= maxExpected) {175System.out.println(msg);176} else {177throw new IllegalStateException(msg);178}179}180181{182String msg = "Cycle duration (" + cyclesActual + "), pause duration (" + pausesActual + ")";183if (pausesActual <= cyclesActual) {184System.out.println(msg);185} else {186throw new IllegalStateException(msg);187}188}189}190}191192193