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