test/java/lang/management/MemoryMXBean/CollectionUsageThreshold.java

Print this page
rev 5700 : 7195557: NPG: Unexpected number of memory pools
Summary: Update management tests to work with a VM without a permanent generation memory pool
Reviewed-by:


  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 final int EXPECTED_NUM_POOLS = 2;
  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;


 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                 PoolRecord pr = new PoolRecord(p);
 133                 result.put(p.getName(), pr);
 134                 if (result.size() == EXPECTED_NUM_POOLS) {
 135                     break;
 136                 }
 137             }
 138         }
 139         if (result.size() != EXPECTED_NUM_POOLS) {
 140             throw new RuntimeException("Unexpected number of selected pools");
 141         }
 142 
 143         try {
 144             // This test creates a checker thread responsible for checking
 145             // the low memory notifications.  It blocks until a permit
 146             // from the signals semaphore is available.
 147             checker = new Checker("Checker thread");
 148             checker.setDaemon(true);
 149             checker.start();
 150 
 151             for (PoolRecord pr : result.values()) {
 152                 pr.getPool().setCollectionUsageThreshold(THRESHOLD);
 153                 System.out.println("Collection usage threshold of " +
 154                     pr.getPool().getName() + " set to " + THRESHOLD);
 155             }
 156 
 157             SensorListener listener = new SensorListener();
 158             NotificationEmitter emitter = (NotificationEmitter) mm;
 159             emitter.addNotificationListener(listener, null, null);


 192         mm.gc();
 193 
 194         if (trace) {
 195             for (PoolRecord pr : result.values()) {
 196                 System.out.println("Usage after GC for: " + pr.getPool().getName());
 197                 MemoryUtil.printMemoryUsage(pr.getPool().getUsage());
 198             }
 199         }
 200     }
 201 
 202     static class Checker extends Thread {
 203         private boolean checkerReady = false;
 204         private int waiters = 0;
 205         private boolean readyToCheck = false;
 206         Checker(String name) {
 207             super(name);
 208         };
 209         public void run() {
 210             while (true) {
 211                 try {
 212                     signals.acquire(EXPECTED_NUM_POOLS);
 213                     checkResult();
 214                 } catch (InterruptedException e) {
 215                     throw new RuntimeException(e);
 216                 } catch (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                 }




  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;


 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);


 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                 }