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