1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. 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  * @test
  26  * @bug     4959889 6992968
  27  * @summary Basic unit test of memory management testing:
  28  *          1) setCollectionUsageThreshold() and getCollectionUsageThreshold()
  29  *          2) test notification emitted for two different memory pools.
  30  *
  31  * @author  Mandy Chung
  32  *
  33  * @library /lib/testlibrary/ /test/lib
  34  * @modules jdk.management
  35  * @build jdk.testlibrary.* CollectionUsageThreshold MemoryUtil RunUtil
  36  * @requires vm.opt.ExplicitGCInvokesConcurrent == "false" | vm.opt.ExplicitGCInvokesConcurrent == "null"
  37  * @build sun.hotspot.WhiteBox
  38  * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
  39  * @run main/othervm/timeout=300 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. CollectionUsageThreshold
  40  */
  41 
  42 import java.util.*;
  43 import java.util.concurrent.*;
  44 import java.util.concurrent.atomic.AtomicInteger;
  45 import javax.management.*;
  46 import javax.management.openmbean.CompositeData;
  47 import java.lang.management.*;
  48 import static java.lang.management.MemoryNotificationInfo.*;;
  49 import static java.lang.management.ManagementFactory.*;
  50 
  51 import sun.hotspot.code.Compiler;
  52 
  53 public class CollectionUsageThreshold {
  54     private static final MemoryMXBean mm = getMemoryMXBean();
  55     private static final Map<String, PoolRecord> result = new HashMap<>();
  56     private static boolean trace = false;
  57     private static volatile int numMemoryPools = 1;
  58     private static final int NUM_GCS = 3;
  59     private static final int THRESHOLD = 10;
  60     private static volatile int numGCs = 0;
  61 
  62     // semaphore to signal the arrival of a low memory notification
  63     private static final Semaphore signals = new Semaphore(0);
  64     // barrier for the main thread to wait until the checker thread
  65     // finishes checking the low memory notification result
  66     private static final CyclicBarrier barrier = new CyclicBarrier(2);
  67 
  68     /**
  69      * Run the test multiple times with different GC versions.
  70      * First with default command line specified by the framework.
  71      * Then with GC versions specified by the test.
  72      */
  73     public static void main(String a[]) throws Throwable {
  74         final String main = "CollectionUsageThreshold$TestMain";
  75         RunUtil.runTestKeepGcOpts(main);
  76         RunUtil.runTestClearGcOpts(main, "-XX:+UseSerialGC");
  77         RunUtil.runTestClearGcOpts(main, "-XX:+UseParallelGC");
  78         RunUtil.runTestClearGcOpts(main, "-XX:+UseG1GC", "-XX:-G1UseLegacyMonitoring");
  79         RunUtil.runTestClearGcOpts(main, "-XX:+UseG1GC", "-XX:+G1UseLegacyMonitoring");
  80         if (!Compiler.isGraalEnabled()) { // Graal does not support CMS
  81             RunUtil.runTestClearGcOpts(main, "-XX:+UseConcMarkSweepGC");
  82         }
  83     }
  84 
  85     static class PoolRecord {
  86         private final MemoryPoolMXBean pool;
  87         private final AtomicInteger listenerInvoked = new AtomicInteger(0);
  88         private volatile long notifCount = 0;
  89         PoolRecord(MemoryPoolMXBean p) {
  90             this.pool = p;
  91         }
  92         int getListenerInvokedCount() {
  93             return listenerInvoked.get();
  94         }
  95         long getNotifCount() {
  96             return notifCount;
  97         }
  98         MemoryPoolMXBean getPool() {
  99             return pool;
 100         }
 101         void addNotification(MemoryNotificationInfo minfo) {
 102             listenerInvoked.incrementAndGet();
 103             notifCount = minfo.getCount();
 104         }
 105     }
 106 
 107     static class SensorListener implements NotificationListener {
 108         @Override
 109         public void handleNotification(Notification notif, Object handback) {
 110             String type = notif.getType();
 111             if (MEMORY_THRESHOLD_EXCEEDED.equals(type) ||
 112                 MEMORY_COLLECTION_THRESHOLD_EXCEEDED.equals(type)) {
 113                 MemoryNotificationInfo minfo = MemoryNotificationInfo.
 114                     from((CompositeData) notif.getUserData());
 115 
 116                 MemoryUtil.printMemoryNotificationInfo(minfo, type);
 117                 PoolRecord pr = (PoolRecord) result.get(minfo.getPoolName());
 118                 if (pr == null) {
 119                     throw new RuntimeException("Pool " + minfo.getPoolName() +
 120                         " is not selected");
 121                 }
 122                 if (!MEMORY_COLLECTION_THRESHOLD_EXCEEDED.equals(type)) {
 123                     throw new RuntimeException("Pool " + minfo.getPoolName() +
 124                         " got unexpected notification type: " +
 125                         type);
 126                 }
 127                 pr.addNotification(minfo);
 128                 System.out.println("notifying the checker thread to check result");
 129                 signals.release();
 130             }
 131         }
 132     }
 133 
 134     private static class TestMain {
 135         public static void main(String args[]) throws Exception {
 136             if (args.length > 0 && args[0].equals("trace")) {
 137                 trace = true;
 138             }
 139 
 140             List<MemoryPoolMXBean> pools = getMemoryPoolMXBeans();
 141             List<MemoryManagerMXBean> managers = getMemoryManagerMXBeans();
 142 
 143             if (trace) {
 144                 MemoryUtil.printMemoryPools(pools);
 145                 MemoryUtil.printMemoryManagers(managers);
 146             }
 147 
 148             // Find the Old generation which supports low memory detection
 149             for (MemoryPoolMXBean p : pools) {
 150                 if (p.isUsageThresholdSupported() && p.isCollectionUsageThresholdSupported()) {
 151                     if (p.getName().toLowerCase().contains("perm")) {
 152                         // if we have a "perm gen" pool increase the number of expected
 153                         // memory pools by one.
 154                         numMemoryPools++;
 155                     }
 156                     PoolRecord pr = new PoolRecord(p);
 157                     result.put(p.getName(), pr);
 158                     if (result.size() == numMemoryPools) {
 159                         break;
 160                     }
 161                 }
 162             }
 163             if (result.size() != numMemoryPools) {
 164                 throw new RuntimeException("Unexpected number of selected pools");
 165             }
 166 
 167             try {
 168                 // This test creates a checker thread responsible for checking
 169                 // the low memory notifications.  It blocks until a permit
 170                 // from the signals semaphore is available.
 171                 Checker checker = new Checker("Checker thread");
 172                 checker.setDaemon(true);
 173                 checker.start();
 174 
 175                 for (PoolRecord pr : result.values()) {
 176                     pr.getPool().setCollectionUsageThreshold(THRESHOLD);
 177                     System.out.println("Collection usage threshold of " +
 178                         pr.getPool().getName() + " set to " + THRESHOLD);
 179                 }
 180 
 181                 SensorListener listener = new SensorListener();
 182                 NotificationEmitter emitter = (NotificationEmitter) mm;
 183                 emitter.addNotificationListener(listener, null, null);
 184 
 185                 // The main thread invokes GC to trigger the VM to perform
 186                 // low memory detection and then waits until the checker thread
 187                 // finishes its work to check for a low-memory notification.
 188                 //
 189                 // At GC time, VM will issue low-memory notification and invoke
 190                 // the listener which will release a permit to the signals semaphore.
 191                 // When the checker thread acquires the permit and finishes
 192                 // checking the low-memory notification, it will also call
 193                 // barrier.await() to signal the main thread to resume its work.
 194                 for (int i = 0; i < NUM_GCS; i++) {
 195                     invokeGC();
 196                     barrier.await();
 197                 }
 198             } finally {
 199                 // restore the default
 200                 for (PoolRecord pr : result.values()) {
 201                     pr.getPool().setCollectionUsageThreshold(0);
 202                 }
 203             }
 204             System.out.println(RunUtil.successMessage);
 205         }
 206 
 207 
 208         private static void invokeGC() {
 209             System.out.println("Calling System.gc()");
 210             numGCs++;
 211             mm.gc();
 212 
 213             if (trace) {
 214                 for (PoolRecord pr : result.values()) {
 215                     System.out.println("Usage after GC for: " + pr.getPool().getName());
 216                     MemoryUtil.printMemoryUsage(pr.getPool().getUsage());
 217                 }
 218             }
 219         }
 220     }
 221 
 222     static class Checker extends Thread {
 223         Checker(String name) {
 224             super(name);
 225         };
 226         @Override
 227         public void run() {
 228             while (true) {
 229                 try {
 230                     signals.acquire(numMemoryPools);
 231                     checkResult();
 232                 } catch (InterruptedException | BrokenBarrierException e) {
 233                     throw new RuntimeException(e);
 234                 }
 235             }
 236         }
 237         private void checkResult() throws InterruptedException, BrokenBarrierException {
 238             for (PoolRecord pr : result.values()) {
 239                 if (pr.getListenerInvokedCount() != numGCs) {
 240                     fail("Listeners invoked count = " +
 241                          pr.getListenerInvokedCount() + " expected to be " +
 242                          numGCs);
 243                 }
 244                 if (pr.getNotifCount() != numGCs) {
 245                     fail("Notif Count = " +
 246                          pr.getNotifCount() + " expected to be " +
 247                          numGCs);
 248                 }
 249 
 250                 long count = pr.getPool().getCollectionUsageThresholdCount();
 251                 if (count != numGCs) {
 252                     fail("CollectionUsageThresholdCount = " +
 253                          count + " expected to be " + numGCs);
 254                 }
 255                 if (!pr.getPool().isCollectionUsageThresholdExceeded()) {
 256                     fail("isCollectionUsageThresholdExceeded" +
 257                          " expected to be true");
 258                 }
 259             }
 260             // wait until the main thread is waiting for notification
 261             barrier.await();
 262             System.out.println("notifying main thread to continue - result checking finished");
 263         }
 264 
 265         private void fail(String msg) {
 266             // reset the barrier to cause BrokenBarrierException to avoid hanging
 267             barrier.reset();
 268             throw new RuntimeException(msg);
 269         }
 270     }
 271 }