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