1 /* 2 * Copyright (c) 2016, 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 TestStressG1Humongous 26 * @key gc 27 * @key stress 28 * @summary Stress G1 by humongous allocations in situation near OOM 29 * Fails intermittently on 32-bit VMs due to 8160827 so quarantine 30 * it on those platforms: 31 * @requires vm.bits != "32" 32 * @requires vm.gc.G1 33 * @requires !vm.flightRecorder 34 * @run main/othervm/timeout=200 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=4m 35 * -Dtimeout=120 -Dthreads=3 -Dhumongoussize=1.1 -Dregionsize=4 TestStressG1Humongous 36 * @run main/othervm/timeout=200 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=16m 37 * -Dtimeout=120 -Dthreads=5 -Dhumongoussize=2.1 -Dregionsize=16 TestStressG1Humongous 38 * @run main/othervm/timeout=200 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=32m 39 * -Dtimeout=120 -Dthreads=4 -Dhumongoussize=0.6 -Dregionsize=32 TestStressG1Humongous 40 * @run main/othervm/timeout=700 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=1m 41 * -Dtimeout=600 -Dthreads=7 -Dhumongoussize=0.6 -Dregionsize=1 TestStressG1Humongous 42 */ 43 44 import java.util.ArrayList; 45 import java.util.List; 46 import java.util.Collections; 47 import java.util.concurrent.CountDownLatch; 48 import java.util.concurrent.ExecutorService; 49 import java.util.concurrent.Executors; 50 import java.util.concurrent.atomic.AtomicInteger; 51 52 public class TestStressG1Humongous { 53 54 // Timeout in seconds 55 private static final int TIMEOUT = Integer.getInteger("timeout", 60); 56 private static final int THREAD_COUNT = Integer.getInteger("threads", 2); 57 private static final int REGION_SIZE = Integer.getInteger("regionsize", 1) * 1024 * 1024; 58 private static final int HUMONGOUS_SIZE = (int) (REGION_SIZE * Double.parseDouble(System.getProperty("humongoussize", "1.5"))); 59 60 private volatile boolean isRunning; 61 private final ExecutorService threadExecutor; 62 private final AtomicInteger alocatedObjectsCount; 63 private CountDownLatch countDownLatch; 64 public static final List<Object> GARBAGE = Collections.synchronizedList(new ArrayList<>()); 65 66 public static void main(String[] args) throws InterruptedException { 67 new TestStressG1Humongous().run(); 68 } 69 70 public TestStressG1Humongous() { 71 isRunning = true; 72 threadExecutor = Executors.newFixedThreadPool(THREAD_COUNT + 1); 73 alocatedObjectsCount = new AtomicInteger(0); 74 } 75 76 private void run() throws InterruptedException { 77 threadExecutor.submit(new Timer()); 78 int checkedAmountOfHObjects = getExpectedAmountOfObjects(); 79 while (isRunning()) { 80 countDownLatch = new CountDownLatch(THREAD_COUNT); 81 startAllocationThreads(checkedAmountOfHObjects); 82 countDownLatch.await(); 83 GARBAGE.clear(); 84 System.out.println("Allocated " + alocatedObjectsCount.get() + " objects."); 85 alocatedObjectsCount.set(0); 86 } 87 threadExecutor.shutdown(); 88 System.out.println("Done!"); 89 } 90 91 /** 92 * Tries to fill available memory with humongous objects to get expected amount. 93 * @return expected amount of humongous objects 94 */ 95 private int getExpectedAmountOfObjects() { 96 long maxMem = Runtime.getRuntime().maxMemory(); 97 int expectedHObjects = (int) (maxMem / HUMONGOUS_SIZE); 98 // Will allocate 1 region less to give some free space for VM. 99 int checkedAmountOfHObjects = checkHeapCapacity(expectedHObjects) - 1; 100 return checkedAmountOfHObjects; 101 } 102 103 /** 104 * Starts several threads to allocate the requested amount of humongous objects. 105 * @param totalObjects total amount of object that will be created 106 */ 107 private void startAllocationThreads(int totalObjects) { 108 int objectsPerThread = totalObjects / THREAD_COUNT; 109 int objectsForLastThread = objectsPerThread + totalObjects % THREAD_COUNT; 110 for (int i = 0; i < THREAD_COUNT - 1; ++i) { 111 threadExecutor.submit(new AllocationThread(countDownLatch, objectsPerThread, alocatedObjectsCount)); 112 } 113 threadExecutor.submit(new AllocationThread(countDownLatch, objectsForLastThread, alocatedObjectsCount)); 114 } 115 116 /** 117 * Creates a humongous object of the predefined size. 118 */ 119 private void createObject() { 120 GARBAGE.add(new byte[HUMONGOUS_SIZE]); 121 } 122 123 /** 124 * Tries to create the requested amount of humongous objects. 125 * In case of OOME, stops creating and cleans the created garbage. 126 * @param expectedObjects amount of objects based on heap size 127 * @return amount of created objects 128 */ 129 private int checkHeapCapacity(int expectedObjects) { 130 int allocated = 0; 131 try { 132 while (isRunning() && allocated < expectedObjects) { 133 createObject(); 134 ++allocated; 135 } 136 } catch (OutOfMemoryError oome) { 137 GARBAGE.clear(); 138 } 139 return allocated; 140 } 141 142 private void setDone() { 143 isRunning = false; 144 } 145 146 private boolean isRunning() { 147 return isRunning; 148 } 149 150 /** 151 * Thread which allocates requested amount of humongous objects. 152 */ 153 private class AllocationThread implements Runnable { 154 155 private final int totalObjects; 156 private final CountDownLatch cdl; 157 private final AtomicInteger allocationCounter; 158 159 /** 160 * Creates allocation thread 161 * @param cdl CountDownLatch 162 * @param objects amount of objects to allocate 163 * @param counter 164 */ 165 public AllocationThread(CountDownLatch cdl, int objects, AtomicInteger counter) { 166 totalObjects = objects; 167 this.cdl = cdl; 168 allocationCounter = counter; 169 } 170 171 @Override 172 public void run() { 173 int allocatedObjects = 0; 174 try { 175 while (isRunning && allocatedObjects < totalObjects) { 176 createObject(); 177 allocatedObjects++; 178 allocationCounter.incrementAndGet(); 179 } 180 181 } catch (OutOfMemoryError oome) { 182 GARBAGE.clear(); 183 System.out.print("OOME was caught."); 184 System.out.println(" Allocated in thread: " + allocatedObjects + " . Totally allocated: " + allocationCounter.get() + "."); 185 } finally { 186 cdl.countDown(); 187 } 188 } 189 } 190 191 /** 192 * Simple Runnable which waits TIMEOUT and sets isRunning to false. 193 */ 194 class Timer implements Runnable { 195 196 @Override 197 public void run() { 198 try { 199 Thread.sleep(TIMEOUT * 1000); 200 } catch (InterruptedException ex) { 201 } 202 setDone(); 203 } 204 } 205 }