1 /* 2 * Copyright (c) 2003, 2018, 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 4530538 27 * @summary Basic unit test of memory management testing: 28 * 1) setUsageThreshold() and getUsageThreshold() 29 * 2) test low memory detection on the old generation. 30 * @author Mandy Chung 31 * 32 * @requires vm.gc == "null" 33 * @requires vm.opt.ExplicitGCInvokesConcurrent != "true" 34 * @requires vm.opt.DisableExplicitGC != "true" 35 * @library /test/lib 36 * 37 * @build LowMemoryTest MemoryUtil RunUtil 38 * @build sun.hotspot.WhiteBox 39 * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 40 * @run main/othervm/timeout=600 -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. LowMemoryTest 41 */ 42 43 import java.lang.management.*; 44 import java.util.*; 45 import java.util.concurrent.Phaser; 46 import javax.management.*; 47 import javax.management.openmbean.CompositeData; 48 import jdk.test.lib.JDKToolFinder; 49 import jdk.test.lib.process.ProcessTools; 50 import jdk.test.lib.Utils; 51 52 import sun.hotspot.code.Compiler; 53 54 public class LowMemoryTest { 55 private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean(); 56 private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); 57 private static final Phaser phaser = new Phaser(2); 58 private static MemoryPoolMXBean mpool = null; 59 private static boolean trace = false; 60 private static boolean testFailed = false; 61 private static final int NUM_TRIGGERS = 5; 62 private static final int NUM_CHUNKS = 2; 63 private static final int YOUNG_GEN_SIZE = 8 * 1024 * 1024; 64 private static long chunkSize; 65 private static final String classMain = "LowMemoryTest$TestMain"; 66 67 /** 68 * Run the test multiple times with different GC versions. 69 * First with default command line specified by the framework. 70 * Then with GC versions specified by the test. 71 */ 72 public static void main(String a[]) throws Throwable { 73 // Use a low young gen size to ensure that the 74 // allocated objects are put in the old gen. 75 final String nmFlag = "-Xmn" + YOUNG_GEN_SIZE; 76 // Using large pages will change the young gen size, 77 // make sure we don't use them for this test. 78 final String lpFlag = "-XX:-UseLargePages"; 79 // Prevent G1 from selecting a large heap region size, 80 // since that would change the young gen size. 81 final String g1Flag = "-XX:G1HeapRegionSize=1m"; 82 83 // Runs the test collecting subprocess I/O while it's running. 84 traceTest(classMain + ", -XX:+UseSerialGC", nmFlag, lpFlag, "-XX:+UseSerialGC"); 85 traceTest(classMain + ", -XX:+UseParallelGC", nmFlag, lpFlag, "-XX:+UseParallelGC"); 86 traceTest(classMain + ", -XX:+UseG1GC -XX:-G1UseLegacyMonitoring", nmFlag, lpFlag, 87 "-XX:+UseG1GC", "-XX:-G1UseLegacyMonitoring", g1Flag); 88 traceTest(classMain + ", -XX:+UseG1GC -XX:+G1UseLegacyMonitoring", nmFlag, lpFlag, 89 "-XX:+UseG1GC", "-XX:+G1UseLegacyMonitoring", g1Flag); 90 if (!Compiler.isGraalEnabled()) { // Graal does not support CMS 91 traceTest(classMain + ", -XX:+UseConcMarkSweepGC", nmFlag, lpFlag, 92 "-XX:+UseConcMarkSweepGC"); 93 } 94 } 95 96 /* 97 * Creating command-line for running subprocess JVM: 98 * 99 * JVM command line is like: 100 * {test_jdk}/bin/java {defaultopts} -cp {test.class.path} {testopts} main 101 * 102 * {defaultopts} are the default java options set by the framework. 103 * 104 * @param testOpts java options specified by the test. 105 */ 106 private static List<String> buildCommandLine(String... testOpts) { 107 List<String> opts = new ArrayList<>(); 108 opts.add(JDKToolFinder.getJDKTool("java")); 109 opts.addAll(Arrays.asList(Utils.getTestJavaOpts())); 110 opts.add("-cp"); 111 opts.add(System.getProperty("test.class.path", "test.class.path")); 112 opts.add("-Xlog:gc*=debug"); 113 opts.addAll(Arrays.asList(testOpts)); 114 opts.add(classMain); 115 116 return opts; 117 } 118 119 /** 120 * Runs LowMemoryTest$TestMain with the passed options and redirects subprocess 121 * standard I/O to the current (parent) process. This provides a trace of what 122 * happens in the subprocess while it is runnning (and before it terminates). 123 * 124 * @param prefixName the prefix string for redirected outputs 125 * @param testOpts java options specified by the test. 126 */ 127 private static void traceTest(String prefixName, 128 String... testOpts) 129 throws Throwable { 130 131 // Building command-line 132 List<String> opts = buildCommandLine(testOpts); 133 134 // We activate all tracing in subprocess 135 opts.add("trace"); 136 137 // Launch separate JVM subprocess 138 String[] optsArray = opts.toArray(new String[0]); 139 ProcessBuilder pb = new ProcessBuilder(optsArray); 140 System.out.println("\n========= Tracing of subprocess " + prefixName + " ========="); 141 Process p = ProcessTools.startProcess(prefixName, pb); 142 143 // Handling end of subprocess 144 try { 145 int exitCode = p.waitFor(); 146 if (exitCode != 0) { 147 throw new RuntimeException( 148 "Subprocess unexpected exit value of [" + exitCode + "]. Expected 0.\n"); 149 } 150 } catch (InterruptedException e) { 151 throw new RuntimeException("Parent process interrupted with exception : \n " + e + " :" ); 152 } 153 154 155 } 156 157 private static volatile boolean listenerInvoked = false; 158 static class SensorListener implements NotificationListener { 159 @Override 160 public void handleNotification(Notification notif, Object handback) { 161 String type = notif.getType(); 162 if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) || 163 type.equals(MemoryNotificationInfo. 164 MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) { 165 166 MemoryNotificationInfo minfo = MemoryNotificationInfo. 167 from((CompositeData) notif.getUserData()); 168 169 MemoryUtil.printMemoryNotificationInfo(minfo, type); 170 listenerInvoked = true; 171 } 172 } 173 } 174 175 static class TestListener implements NotificationListener { 176 private boolean isRelaxed = false; 177 private int triggers = 0; 178 private final long[] count = new long[NUM_TRIGGERS * 2]; 179 private final long[] usedMemory = new long[NUM_TRIGGERS * 2]; 180 181 public TestListener() { 182 isRelaxed = ManagementFactory.getRuntimeMXBean().getInputArguments().contains("-XX:+UseConcMarkSweepGC"); 183 } 184 185 @Override 186 public void handleNotification(Notification notif, Object handback) { 187 MemoryNotificationInfo minfo = MemoryNotificationInfo. 188 from((CompositeData) notif.getUserData()); 189 count[triggers] = minfo.getCount(); 190 usedMemory[triggers] = minfo.getUsage().getUsed(); 191 triggers++; 192 } 193 public void checkResult() throws Exception { 194 if (!checkValue(triggers, NUM_TRIGGERS)) { 195 throw new RuntimeException("Unexpected number of triggers = " + 196 triggers + " but expected to be " + NUM_TRIGGERS); 197 } 198 199 for (int i = 0; i < triggers; i++) { 200 if (!checkValue(count[i], i + 1)) { 201 throw new RuntimeException("Unexpected count of" + 202 " notification #" + i + 203 " count = " + count[i] + 204 " but expected to be " + (i+1)); 205 } 206 if (usedMemory[i] < newThreshold) { 207 throw new RuntimeException("Used memory = " + 208 usedMemory[i] + " is less than the threshold = " + 209 newThreshold); 210 } 211 } 212 } 213 214 private boolean checkValue(int value, int target) { 215 return checkValue((long)value, target); 216 } 217 218 private boolean checkValue(long value, int target) { 219 if (!isRelaxed) { 220 return value == target; 221 } else { 222 return value >= target; 223 } 224 } 225 } 226 227 private static long newThreshold; 228 229 private static class TestMain { 230 public static void main(String args[]) throws Exception { 231 if (args.length > 0 && args[0].equals("trace")) { 232 trace = true; 233 } 234 235 // Find the Old generation which supports low memory detection 236 ListIterator iter = pools.listIterator(); 237 while (iter.hasNext()) { 238 MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); 239 if (p.getType() == MemoryType.HEAP && 240 p.isUsageThresholdSupported()) { 241 if (!p.getName().equals("G1 Old Space")) { 242 // In G1, humongous objects are tracked in the old space only in 243 // legacy monitoring mode. In default mode, G1 tracks humongous 244 // objects in the humongous space, which latter also supports a 245 // usage threshold. Since we're allocating humongous objects in 246 // this test, in default mode the old space doesn't change. For 247 // this test, we use the old space in legacy mode (it's called 248 // "G1 Old Gen" and the humongous space in default mode. If we 249 // used "G1 Old Space" in default mode, notification would never 250 // happen. 251 mpool = p; 252 if (trace) { 253 System.out.println("Selected memory pool for low memory " + 254 "detection."); 255 MemoryUtil.printMemoryPool(mpool); 256 } 257 break; 258 } 259 } 260 } 261 if (mpool == null) { 262 throw new RuntimeException("TEST FAILED: No heap pool found"); 263 } 264 265 TestListener listener = new TestListener(); 266 SensorListener l2 = new SensorListener(); 267 NotificationEmitter emitter = (NotificationEmitter) mm; 268 emitter.addNotificationListener(listener, null, null); 269 emitter.addNotificationListener(l2, null, null); 270 271 Thread allocator = new AllocatorThread(); 272 Thread sweeper = new SweeperThread(); 273 274 // The chunk size needs to be larger than YOUNG_GEN_SIZE, 275 // otherwise we will get intermittent failures when objects 276 // end up in the young gen instead of the old gen. 277 final long epsilon = 1024; 278 chunkSize = YOUNG_GEN_SIZE + epsilon; 279 280 MemoryUsage mu = mpool.getUsage(); 281 newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS); 282 283 // Sanity check. Make sure the new threshold isn't too large. 284 // Tweak the test if this fails. 285 final long headRoom = chunkSize * 2; 286 final long max = mu.getMax(); 287 if (max != -1 && newThreshold > max - headRoom) { 288 throw new RuntimeException("TEST FAILED: newThreshold: " + newThreshold + 289 " is too near the maximum old gen size: " + max + 290 " used: " + mu.getUsed() + " headRoom: " + headRoom); 291 } 292 293 System.out.println("Setting threshold for " + mpool.getName() + 294 " from " + mpool.getUsageThreshold() + " to " + newThreshold + 295 ". Current used = " + mu.getUsed()); 296 297 mpool.setUsageThreshold(newThreshold); 298 299 if (mpool.getUsageThreshold() != newThreshold) { 300 throw new RuntimeException("TEST FAILED: " + 301 "Threshold for Memory pool " + mpool.getName() + 302 "is " + mpool.getUsageThreshold() + " but expected to be" + 303 newThreshold); 304 } 305 306 307 allocator.start(); 308 // Force Allocator start first 309 phaser.arriveAndAwaitAdvance(); 310 sweeper.start(); 311 312 313 try { 314 allocator.join(); 315 // Wait until AllocatorThread's done 316 phaser.arriveAndAwaitAdvance(); 317 sweeper.join(); 318 } catch (InterruptedException e) { 319 System.out.println("Unexpected exception:" + e); 320 testFailed = true; 321 } 322 323 listener.checkResult(); 324 325 if (testFailed) 326 throw new RuntimeException("TEST FAILED."); 327 328 System.out.println(RunUtil.successMessage); 329 } 330 } 331 332 private static void goSleep(long ms) { 333 try { 334 Thread.sleep(ms); 335 } catch (InterruptedException e) { 336 System.out.println("Unexpected exception:" + e); 337 testFailed = true; 338 } 339 } 340 341 private static final List<Object> objectPool = new ArrayList<>(); 342 static class AllocatorThread extends Thread { 343 public void doTask() { 344 int iterations = 0; 345 int numElements = (int) (chunkSize / 4); // minimal object size 346 while (!listenerInvoked || mpool.getUsage().getUsed() < mpool.getUsageThreshold()) { 347 iterations++; 348 if (trace) { 349 System.out.println(" Iteration " + iterations + 350 ": before allocation " + 351 mpool.getUsage().getUsed()); 352 } 353 354 Object[] o = new Object[numElements]; 355 if (iterations <= NUM_CHUNKS) { 356 // only hold a reference to the first NUM_CHUNKS 357 // allocated objects 358 objectPool.add(o); 359 } 360 361 if (trace) { 362 System.out.println(" " + 363 " after allocation " + 364 mpool.getUsage().getUsed()); 365 } 366 goSleep(100); 367 } 368 } 369 @Override 370 public void run() { 371 for (int i = 1; i <= NUM_TRIGGERS; i++) { 372 // Sync with SweeperThread's second phase. 373 phaser.arriveAndAwaitAdvance(); 374 System.out.println("AllocatorThread is doing task " + i + 375 " phase " + phaser.getPhase()); 376 doTask(); 377 // Sync with SweeperThread's first phase. 378 phaser.arriveAndAwaitAdvance(); 379 System.out.println("AllocatorThread done task " + i + 380 " phase " + phaser.getPhase()); 381 if (testFailed) { 382 return; 383 } 384 } 385 } 386 } 387 388 static class SweeperThread extends Thread { 389 private void doTask() { 390 int iterations = 0; 391 if (trace) { 392 System.out.println("SweeperThread clearing allocated objects."); 393 } 394 395 for (; mpool.getUsage().getUsed() >= 396 mpool.getUsageThreshold();) { 397 // clear all allocated objects and invoke GC 398 objectPool.clear(); 399 mm.gc(); 400 401 if (trace) { 402 iterations++; 403 System.out.println("SweeperThread called " + iterations + 404 " time(s) MemoryMXBean.gc()."); 405 } 406 407 goSleep(100); 408 } 409 } 410 411 @Override 412 public void run() { 413 for (int i = 1; i <= NUM_TRIGGERS; i++) { 414 // Sync with AllocatorThread's first phase. 415 phaser.arriveAndAwaitAdvance(); 416 System.out.println("SweeperThread is doing task " + i + 417 " phase " + phaser.getPhase()); 418 419 doTask(); 420 421 listenerInvoked = false; 422 423 // Sync with AllocatorThread's second phase. 424 phaser.arriveAndAwaitAdvance(); 425 System.out.println("SweeperThread done task " + i + 426 " phase " + phaser.getPhase()); 427 if (testFailed) return; 428 } 429 } 430 } 431 }