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 import java.util.*;
  66 import java.util.concurrent.atomic.*;
  67 import javax.management.*;
  68 import java.lang.management.*;
  69 import javax.management.openmbean.*;
  70 
  71 import com.sun.management.GarbageCollectionNotificationInfo;
  72 
  73 public class TestPauseNotifications {
  74 
  75     static final long HEAP_MB = 128;                           // adjust for test configuration above
  76     static final long TARGET_MB = Long.getLong("target", 8_000); // 8 Gb allocation
  77 
  78     static volatile Object sink;
  79 
  80     public static void main(String[] args) throws Exception {
  81         final AtomicLong pausesDuration = new AtomicLong();
  82         final AtomicLong cyclesDuration = new AtomicLong();
  83 
  84         NotificationListener listener = new NotificationListener() {
  85             @Override
  86             public void handleNotification(Notification n, Object o) {
  87                 if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
  88                     GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());
  89 
  90                     System.out.println(info.getGcInfo().toString());
  91                     System.out.println(info.getGcName());
  92                     System.out.println();
  93 
  94                     long d = info.getGcInfo().getDuration();
  95 
  96                     String name = info.getGcName();
  97                     if (name.contains("Shenandoah")) {
  98                         if (name.equals("Shenandoah Pauses")) {
  99                             pausesDuration.addAndGet(d);
 100                         } else if (name.equals("Shenandoah Cycles")) {
 101                             cyclesDuration.addAndGet(d);
 102                         } else {
 103                             throw new IllegalStateException("Unknown name: " + name);
 104                         }
 105                     }
 106                 }
 107             }
 108         };
 109 
 110         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
 111             ((NotificationEmitter) bean).addNotificationListener(listener, null, null);
 112         }
 113 
 114         final int size = 100_000;
 115         long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size);
 116 
 117         for (int c = 0; c < count; c++) {
 118             sink = new int[size];
 119         }
 120 
 121         Thread.sleep(1000);
 122 
 123         long pausesActual = pausesDuration.get();
 124         long cyclesActual = cyclesDuration.get();
 125 
 126         long minExpected = 1;
 127         long maxExpected = Long.MAX_VALUE;
 128 
 129         {
 130             String msg = "Pauses expected = [" + minExpected + "; " + maxExpected + "], actual = " + pausesActual;
 131             if (minExpected < pausesActual && pausesActual < maxExpected) {
 132                 System.out.println(msg);
 133             } else {
 134                 throw new IllegalStateException(msg);
 135             }
 136         }
 137 
 138         {
 139             String msg = "Cycles expected = [" + minExpected + "; " + maxExpected + "], actual = " + cyclesActual;
 140             if (minExpected < cyclesActual && cyclesActual < maxExpected) {
 141                 System.out.println(msg);
 142             } else {
 143                 throw new IllegalStateException(msg);
 144             }
 145         }
 146 
 147         {
 148             String msg = "Cycle duration (" + cyclesActual + "), pause duration (" + pausesActual + ")";
 149             if (pausesActual < cyclesActual) {
 150                 System.out.println(msg);
 151             } else {
 152                 throw new IllegalStateException(msg);
 153             }
 154         }
 155     }
 156 }