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