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