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 TestChurnNotifications
  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 -Dprecise=true
  32  *      TestChurnNotifications
  33  *
  34  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  35  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
  36  *      -XX:-ShenandoahDegeneratedGC -Dprecise=true
  37  *      TestChurnNotifications
  38  */
  39 
  40 /*
  41  * @test TestChurnNotifications
  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  *      -Dprecise=false
  48  *      TestChurnNotifications
  49  */
  50 
  51 /*
  52  * @test TestChurnNotifications
  53  * @summary Check that MX notifications are reported for all cycles
  54  * @key gc
  55  *
  56  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  57  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
  58  *      -Dprecise=false
  59  *      TestChurnNotifications
  60  */
  61 
  62 /*
  63  * @test TestChurnNotifications
  64  * @summary Check that MX notifications are reported for all cycles
  65  * @key gc
  66  *
  67  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  68  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
  69  *      -Dprecise=false
  70  *      TestChurnNotifications
  71  */
  72 
  73 /*
  74  * @test TestChurnNotifications
  75  * @summary Check that MX notifications are reported for all cycles
  76  * @key gc
  77  *
  78  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  79  *      -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
  80  *      -Dprecise=false
  81  *      TestChurnNotifications
  82  */
  83 
  84 /*
  85  * @test TestChurnNotifications
  86  * @summary Check that MX notifications are reported for all cycles
  87  * @key gc
  88  *
  89  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  90  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
  91  *      -Dprecise=false
  92  *      TestChurnNotifications
  93  *
  94  * @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
  95  *      -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu
  96  *      -Dprecise=false
  97  *      TestChurnNotifications
  98  */
  99 
 100 import java.util.*;
 101 import java.util.concurrent.atomic.*;
 102 import javax.management.*;
 103 import java.lang.management.*;
 104 import javax.management.openmbean.*;
 105 
 106 import com.sun.management.GarbageCollectionNotificationInfo;
 107 
 108 public class TestChurnNotifications {
 109 
 110     static final long HEAP_MB = 128;                           // adjust for test configuration above
 111     static final long TARGET_MB = Long.getLong("target", 2_000); // 2 Gb allocation
 112 
 113     // Should we track the churn precisely?
 114     // Precise tracking is only reliable when GC is fully stop-the-world. Otherwise,
 115     // we cannot tell, looking at heap used before/after, what was the GC churn.
 116     static final boolean PRECISE = Boolean.getBoolean("precise");
 117 
 118     static final long M = 1024 * 1024;
 119 
 120     static volatile Object sink;
 121 
 122     public static void main(String[] args) throws Exception {
 123         final AtomicLong churnBytes = new AtomicLong();
 124 
 125         NotificationListener listener = new NotificationListener() {
 126             @Override
 127             public void handleNotification(Notification n, Object o) {
 128                 if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
 129                     GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());
 130                     Map<String, MemoryUsage> mapBefore = info.getGcInfo().getMemoryUsageBeforeGc();
 131                     Map<String, MemoryUsage> mapAfter = info.getGcInfo().getMemoryUsageAfterGc();
 132 
 133                     MemoryUsage before = mapBefore.get("Shenandoah");
 134                     MemoryUsage after = mapAfter.get("Shenandoah");
 135 
 136                     if ((before != null) && (after != null)) {
 137                         long diff = before.getUsed() - after.getUsed();
 138                         if (diff > 0) {
 139                             churnBytes.addAndGet(diff);
 140                         }
 141                     }
 142                 }
 143             }
 144         };
 145 
 146         for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
 147             ((NotificationEmitter) bean).addNotificationListener(listener, null, null);
 148         }
 149 
 150         final int size = 100_000;
 151         long count = TARGET_MB * 1024 * 1024 / (16 + 4 * size);
 152 
 153         long mem = count * (16 + 4 * size);
 154 
 155         for (int c = 0; c < count; c++) {
 156             sink = new int[size];
 157         }
 158 
 159         System.gc();
 160 
 161         Thread.sleep(1000);
 162 
 163         long actual = churnBytes.get();
 164 
 165         long minExpected = PRECISE ? (mem - HEAP_MB * 1024 * 1024) : 1;
 166         long maxExpected = mem + HEAP_MB * 1024 * 1024;
 167 
 168         String msg = "Expected = [" + minExpected / M + "; " + maxExpected / M + "] (" + mem / M + "), actual = " + actual / M;
 169         if (minExpected <= actual && actual <= maxExpected) {
 170             System.out.println(msg);
 171         } else {
 172             throw new IllegalStateException(msg);
 173         }
 174     }
 175 }