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 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() { | 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 * @requires vm.gc.G1 30 * @requires !vm.flightRecorder 31 * @run main/othervm/timeout=200 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=4m 32 * -Dtimeout=120 -Dthreads=3 -Dhumongoussize=1.1 -Dregionsize=4 TestStressG1Humongous 33 * @run main/othervm/timeout=200 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=16m 34 * -Dtimeout=120 -Dthreads=5 -Dhumongoussize=2.1 -Dregionsize=16 TestStressG1Humongous 35 * @run main/othervm/timeout=200 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=32m 36 * -Dtimeout=120 -Dthreads=4 -Dhumongoussize=0.6 -Dregionsize=32 TestStressG1Humongous 37 * @run main/othervm/timeout=700 -Xlog:gc=debug -Xmx1g -XX:+UseG1GC -XX:G1HeapRegionSize=1m 38 * -Dtimeout=600 -Dthreads=7 -Dhumongoussize=0.6 -Dregionsize=1 TestStressG1Humongous 39 */ 40 41 import java.util.ArrayList; 42 import java.util.List; 43 import java.util.Collections; 44 import java.util.concurrent.CountDownLatch; 45 import java.util.concurrent.ExecutorService; 46 import java.util.concurrent.Executors; 47 import java.util.concurrent.atomic.AtomicInteger; 48 75 int checkedAmountOfHObjects = getExpectedAmountOfObjects(); 76 while (isRunning()) { 77 countDownLatch = new CountDownLatch(THREAD_COUNT); 78 startAllocationThreads(checkedAmountOfHObjects); 79 countDownLatch.await(); 80 GARBAGE.clear(); 81 System.out.println("Allocated " + alocatedObjectsCount.get() + " objects."); 82 alocatedObjectsCount.set(0); 83 } 84 threadExecutor.shutdown(); 85 System.out.println("Done!"); 86 } 87 88 /** 89 * Tries to fill available memory with humongous objects to get expected amount. 90 * @return expected amount of humongous objects 91 */ 92 private int getExpectedAmountOfObjects() { 93 long maxMem = Runtime.getRuntime().maxMemory(); 94 int expectedHObjects = (int) (maxMem / HUMONGOUS_SIZE); 95 // Will allocate 2 region less to give some free space for VM. 96 int checkedAmountOfHObjects = checkHeapCapacity(expectedHObjects) - 2; 97 if (checkedAmountOfHObjects <= 0) { 98 throw new RuntimeException("Cannot start testing because there is no available memory."); 99 } 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() { |