1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 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 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 /*
  25  * @test TestPauseNotifications
  26  * @summary Check that MX notifications are reported for all cycles
  27  * @key gc
  28  *
  29  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  30  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  31  *      -XX:+ShenandoahDegeneratedGC
  32  *      TestPauseNotifications
  33  *
  34  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  35  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  36  *      -XX:-ShenandoahDegeneratedGC
  37  *      TestPauseNotifications
  38  */
  39 
  40 /*
  41  * @test TestPauseNotifications
  42  * @summary Check that MX notifications are reported for all cycles
  43  * @key gc
  44  *
  45  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  46  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
  47  *      TestPauseNotifications
  48  */
  49 
  50 /*
  51  * @test TestPauseNotifications
  52  * @summary Check that MX notifications are reported for all cycles
  53  * @key gc
  54  *
  55  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  56  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
  57  *      TestPauseNotifications
  58  */
  59 
  60 /*
  61  * @test TestPauseNotifications
  62  * @summary Check that MX notifications are reported for all cycles
  63  * @key gc
  64  *
  65  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  66  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
  67  *      TestPauseNotifications
  68  */
  69 
  70 /*
  71  * @test TestPauseNotifications
  72  * @summary Check that MX notifications are reported for all cycles
  73  * @key gc
  74  *
  75  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  76  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  77  *      TestPauseNotifications
  78  */
  79 
  80 /*
  81  * @test TestPauseNotifications
  82  * @summary Check that MX notifications are reported for all cycles
  83  * @key gc
  84  *
  85  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  86  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
  87  *      TestPauseNotifications
  88  *
  89  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  90  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
  91  *      TestPauseNotifications
  92  */
  93 
  94 import java.util.*;
  95 import java.util.concurrent.atomic.*;
  96 import javax.management.*;
  97 import java.lang.management.*;
  98 import javax.management.openmbean.*;
  99 import com.sun.management.GarbageCollectionNotificationInfo;
 100 
 101 public class TestPauseNotifications {
 102 
 103     static final long HEAP_MB = 128;                             // adjust for test configuration above
 104     static final long TARGET_MB = Long.getLong("target", 2_000); // 2 Gb allocation
 105 
 106     static volatile Object sink;
 107 
 108     public static void main(String[] args) throws Exception {
 109         final AtomicLong pausesDuration = new AtomicLong();
 110         final AtomicLong cyclesDuration = new AtomicLong();
 111 
 112         NotificationListener listener = new NotificationListener() {
 113             @Override
 114             public void handleNotification(Notification n, Object o) {
 115                 if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
 116                     GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());
 117 
 118                     long d = info.getGcInfo().getDuration();
 119 
 120                     String name = info.getGcName();
 121                     if (name.contains("Shenandoah")) {
 122                         if (name.equals("Shenandoah Pauses")) {
 123                             pausesDuration.addAndGet(d);
 124                         } else if (name.equals("Shenandoah Cycles")) {
 125                             cyclesDuration.addAndGet(d);
 126                         } else {
 127                             throw new IllegalStateException("Unknown name: " + name);
 128                         }
 129                     }
 130                 }
 131             }
 132         };
 133 
 134         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
 135             ((NotificationEmitter) bean).addNotificationListener(listener, null, null);
 136         }
 137 
 138         final int size = 100_000;
 139         long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size);
 140 
 141         for (int c = 0; c < count; c++) {
 142             sink = new int[size];
 143         }
 144 
 145         // Wait until notifications start arriving, and then wait some more
 146         // to catch the ones arriving late.
 147         while (pausesDuration.get() == 0) {
 148             Thread.sleep(1000);
 149         }
 150         Thread.sleep(5000);
 151 
 152         long pausesActual = pausesDuration.get();
 153         long cyclesActual = cyclesDuration.get();
 154 
 155         long minExpected = 1;
 156         long maxExpected = Long.MAX_VALUE;
 157 
 158         {
 159             String msg = "Pauses expected = [" + minExpected + "; " + maxExpected + "], actual = " + pausesActual;
 160             if (minExpected <= pausesActual && pausesActual <= maxExpected) {
 161                 System.out.println(msg);
 162             } else {
 163                 throw new IllegalStateException(msg);
 164             }
 165         }
 166 
 167         {
 168             String msg = "Cycles expected = [" + minExpected + "; " + maxExpected + "], actual = " + cyclesActual;
 169             if (minExpected <= cyclesActual && cyclesActual <= maxExpected) {
 170                 System.out.println(msg);
 171             } else {
 172                 throw new IllegalStateException(msg);
 173             }
 174         }
 175 
 176         {
 177             String msg = "Cycle duration (" + cyclesActual + "), pause duration (" + pausesActual + ")";
 178             if (pausesActual <= cyclesActual) {
 179                 System.out.println(msg);
 180             } else {
 181                 throw new IllegalStateException(msg);
 182             }
 183         }
 184     }
 185 }