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 TestPauseNotifications
  27  * @summary Check that MX notifications are reported for all cycles
  28  * @key gc
  29  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  30  *
  31  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  32  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  33  *      -XX:+ShenandoahDegeneratedGC
  34  *      TestPauseNotifications
  35  *
  36  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  37  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  38  *      -XX:-ShenandoahDegeneratedGC
  39  *      TestPauseNotifications
  40  */
  41 
  42 /*
  43  * @test TestPauseNotifications
  44  * @summary Check that MX notifications are reported for all cycles
  45  * @key gc
  46  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  47  *
  48  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  49  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  50  *      TestPauseNotifications
  51  *
  52  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  53  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
  54  *      TestPauseNotifications
  55  *
  56  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  57  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
  58  *      TestPauseNotifications
  59  *
  60  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  61  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  62  *      TestPauseNotifications
  63  */
  64 
  65 /*
  66  * @test TestPauseNotifications
  67  * @summary Check that MX notifications are reported for all cycles
  68  * @key gc
  69  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  70  *
  71  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  72  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
  73  *      TestPauseNotifications
  74  *
  75  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  76  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
  77  *      TestPauseNotifications
  78  */
  79 
  80 import java.util.*;
  81 import java.util.concurrent.atomic.*;
  82 import javax.management.*;
  83 import java.lang.management.*;
  84 import javax.management.openmbean.*;
  85 
  86 import com.sun.management.GarbageCollectionNotificationInfo;
  87 
  88 public class TestPauseNotifications {
  89 
  90     static final long HEAP_MB = 128;                           // adjust for test configuration above
  91     static final long TARGET_MB = Long.getLong("target", 8_000); // 8 Gb allocation
  92 
  93     static volatile Object sink;
  94 
  95     public static void main(String[] args) throws Exception {
  96         final AtomicLong pausesDuration = new AtomicLong();
  97         final AtomicLong cyclesDuration = new AtomicLong();
  98 
  99         NotificationListener listener = new NotificationListener() {
 100             @Override
 101             public void handleNotification(Notification n, Object o) {
 102                 if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
 103                     GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());
 104 
 105                     System.out.println(info.getGcInfo().toString());
 106                     System.out.println(info.getGcName());
 107                     System.out.println();
 108 
 109                     long d = info.getGcInfo().getDuration();
 110 
 111                     String name = info.getGcName();
 112                     if (name.contains("Shenandoah")) {
 113                         if (name.equals("Shenandoah Pauses")) {
 114                             pausesDuration.addAndGet(d);
 115                         } else if (name.equals("Shenandoah Cycles")) {
 116                             cyclesDuration.addAndGet(d);
 117                         } else {
 118                             throw new IllegalStateException("Unknown name: " + name);
 119                         }
 120                     }
 121                 }
 122             }
 123         };
 124 
 125         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
 126             ((NotificationEmitter) bean).addNotificationListener(listener, null, null);
 127         }
 128 
 129         final int size = 100_000;
 130         long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size);
 131 
 132         for (int c = 0; c < count; c++) {
 133             sink = new int[size];
 134         }
 135 
 136         Thread.sleep(1000);
 137 
 138         long pausesActual = pausesDuration.get();
 139         long cyclesActual = cyclesDuration.get();
 140 
 141         long minExpected = 1;
 142         long maxExpected = Long.MAX_VALUE;
 143 
 144         {
 145             String msg = "Pauses expected = [" + minExpected + "; " + maxExpected + "], actual = " + pausesActual;
 146             if (minExpected < pausesActual && pausesActual < maxExpected) {
 147                 System.out.println(msg);
 148             } else {
 149                 throw new IllegalStateException(msg);
 150             }
 151         }
 152 
 153         {
 154             String msg = "Cycles expected = [" + minExpected + "; " + maxExpected + "], actual = " + cyclesActual;
 155             if (minExpected < cyclesActual && cyclesActual < maxExpected) {
 156                 System.out.println(msg);
 157             } else {
 158                 throw new IllegalStateException(msg);
 159             }
 160         }
 161 
 162         {
 163             String msg = "Cycle duration (" + cyclesActual + "), pause duration (" + pausesActual + ")";
 164             if (pausesActual < cyclesActual) {
 165                 System.out.println(msg);
 166             } else {
 167                 throw new IllegalStateException(msg);
 168             }
 169         }
 170     }
 171 }