1 /*
   2  * Copyright (c) 2018, Red Hat, Inc. and/or its affiliates.
   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 ChurnNotifications
  26  * @summary Check that MX notifications are reported for all cycles
  27  *
  28  * @run main/othervm -Xmx128m -XX:+UseShenandoahGC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=passive      -XX:+ShenandoahDegeneratedGC -Dprecise=true  ChurnNotifications
  29  * @run main/othervm -Xmx128m -XX:+UseShenandoahGC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=passive      -XX:-ShenandoahDegeneratedGC -Dprecise=true  ChurnNotifications
  30  *
  31  * @run main/othervm -Xmx128m -XX:+UseShenandoahGC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=adaptive     -Dprecise=false ChurnNotifications
  32  * @run main/othervm -Xmx128m -XX:+UseShenandoahGC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=static       -Dprecise=false ChurnNotifications
  33  * @run main/othervm -Xmx128m -XX:+UseShenandoahGC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=compact      -Dprecise=false ChurnNotifications
  34  * @run main/othervm -Xmx128m -XX:+UseShenandoahGC -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:ShenandoahGCHeuristics=aggressive   -Dprecise=false ChurnNotifications
  35  */
  36 
  37 import java.util.*;
  38 import java.util.concurrent.atomic.*;
  39 import javax.management.*;
  40 import java.lang.management.*;
  41 import javax.management.openmbean.*;
  42 import com.sun.management.GarbageCollectionNotificationInfo;
  43 
  44 public class ChurnNotifications {
  45 
  46   static final long HEAP_MB   = 128;                           // adjust for test configuration above
  47   static final long TARGET_MB = Long.getLong("target", 8_000); // 8 Gb allocation
  48 
  49   // Should we track the churn precisely?
  50   // Precise tracking is only reliable when GC is fully stop-the-world. Otherwise,
  51   // we cannot tell, looking at heap used before/after, what was the GC churn.
  52   static final boolean PRECISE = Boolean.getBoolean("precise");
  53 
  54   static final long M = 1024*1024;
  55 
  56   static volatile Object sink;
  57 
  58   public static void main(String[] args) throws Exception {
  59     final AtomicLong churnBytes = new AtomicLong();
  60 
  61     NotificationListener listener = new NotificationListener() {
  62       @Override
  63       public void handleNotification(Notification n, Object o) {
  64         if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
  65           GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData)n.getUserData());
  66           Map<String, MemoryUsage> mapBefore = info.getGcInfo().getMemoryUsageBeforeGc();
  67           Map<String, MemoryUsage> mapAfter = info.getGcInfo().getMemoryUsageAfterGc();
  68 
  69           MemoryUsage before = mapBefore.get("Shenandoah");
  70           MemoryUsage after  = mapAfter.get("Shenandoah");
  71 
  72           if ((before != null) && (after != null)) {
  73             long diff = before.getUsed() - after.getUsed();
  74             if (diff > 0) {
  75               churnBytes.addAndGet(diff);
  76             }
  77           }
  78         }
  79       }
  80     };
  81 
  82     for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
  83       ((NotificationEmitter) bean).addNotificationListener(listener, null, null);
  84     }
  85 
  86     final int size = 100_000;
  87     long count = TARGET_MB * 1024 * 1024 / (16 + 4*size);
  88 
  89     long mem = count * (16 + 4*size);
  90 
  91     for (int c = 0; c < count; c++) {
  92        sink = new int[size];
  93     }
  94 
  95     System.gc();
  96 
  97     Thread.sleep(1000);
  98 
  99     long actual = churnBytes.get();
 100 
 101     long minExpected = PRECISE ? (mem - HEAP_MB*1024*1024) : 1;
 102     long maxExpected = mem + HEAP_MB*1024*1024;
 103 
 104     String msg = "Expected = [" + minExpected/M +"; " + maxExpected/M + "] (" + mem/M + "), actual = " + actual/M;
 105     if (minExpected < actual && actual < maxExpected) {
 106       System.out.println(msg);
 107     } else {
 108       throw new IllegalStateException(msg);
 109     }
 110   }
 111 }
 112 
 113