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