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 package gc.g1.humongousObjects; 26 27 import jdk.test.lib.Utils; 28 import sun.hotspot.WhiteBox; 29 30 import java.util.LinkedList; 31 import java.util.List; 32 import java.util.Random; 33 import java.util.stream.Collectors; 34 35 /** 36 * @test TestNoAllocationsInHRegions 37 * @summary Checks that no additional allocations are made in humongous regions 38 * @requires vm.gc.G1 39 * @library /test/lib / 40 * @modules java.management java.base/jdk.internal.misc 41 * @build sun.hotspot.WhiteBox 42 * @run driver ClassFileInstaller sun.hotspot.WhiteBox 43 * sun.hotspot.WhiteBox$WhiteBoxPermission 44 * 45 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. 46 * -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m -XX:MaxTenuringThreshold=0 47 * -Xlog:gc=trace:file=TestNoAllocationsInHRegions10.log 48 * gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 10 49 * 50 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. 51 * -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m -XX:MaxTenuringThreshold=0 52 * -Xlog:gc=trace:file=TestNoAllocationsInHRegions50.log 53 * gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 50 54 * 55 * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. 56 * -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m -XX:MaxTenuringThreshold=0 57 * -Xlog:gc=trace:file=TestNoAllocationsInHRegions70.log 58 * gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 70 59 */ 60 public class TestNoAllocationsInHRegions { 61 private static final WhiteBox WB = WhiteBox.getWhiteBox(); 62 private static final Random RND = Utils.getRandomInstance(); 63 private static final int G1_REGION_SIZE = WB.g1RegionSize(); 64 private static final int[] HUMONGOUS_SIZES = {G1_REGION_SIZE / 2, G1_REGION_SIZE + 1, G1_REGION_SIZE * 2 + 1}; 65 private static final int ALLOC_THREAD_COUNT = 5; 66 67 // We fill specified part of heap with humongous objects - we need public static to prevent escape analysis to 68 // collect this field 69 public static LinkedList<byte[]> humongousAllocations = new LinkedList<>(); 70 71 private static volatile boolean shouldStop = false; 72 private static volatile Error error = null; 73 74 static class Allocator implements Runnable { 75 76 private final List<byte[]> liveObjects = new LinkedList<>(); 77 private int usedMemory = 0; 78 public final Runnable[] actions; 79 80 /** 81 * Maximum size of simple allocation 82 */ 83 private static final int MAX_ALLOCATION_SIZE = (int) (G1_REGION_SIZE / 2 * 0.9); 84 85 /** 86 * Maximum size of dead (i.e. one which is made unreachable right after allocation) object 87 */ 88 private static final int DEAD_OBJECT_MAX_SIZE = G1_REGION_SIZE / 10; 89 90 public Allocator(int maxAllocationMemory) { 91 92 actions = new Runnable[]{ 93 // Allocation 94 () -> { 95 if (maxAllocationMemory - usedMemory != 0) { 96 int arraySize = RND.nextInt(Math.min(maxAllocationMemory - usedMemory, 97 MAX_ALLOCATION_SIZE)); 98 99 if (arraySize != 0) { 100 byte[] allocation = new byte[arraySize]; 101 liveObjects.add(allocation); 102 usedMemory += arraySize; 103 104 // Sanity check 105 if (WB.g1IsHumongous(allocation)) { 106 String errorMessage = String.format("Test Bug: Byte array of size" 107 + " %d is expected to be non-humongous but it is humongous", 108 allocation.length); 109 110 System.out.println(errorMessage); 111 error = new Error(errorMessage); 112 shouldStop = true; 113 } 114 115 // Test check 116 if (WB.g1BelongsToHumongousRegion(WB.getObjectAddress(allocation))) { 117 String errorMessage = String.format("Non-humongous allocation of byte array of " 118 + "length %d and size %d with address %d was made in Humongous Region", 119 allocation.length, WB.getObjectSize(allocation), 120 WB.getObjectAddress(allocation)); 121 122 System.out.println(errorMessage); 123 error = new Error(errorMessage); 124 shouldStop = true; 125 } 126 } 127 } 128 }, 129 130 // Deallocation 131 () -> { 132 if (liveObjects.size() != 0) { 133 int elementNum = RND.nextInt(liveObjects.size()); 134 int shouldFree = liveObjects.get(elementNum).length; 135 liveObjects.remove(elementNum); 136 usedMemory -= shouldFree; 137 } 138 }, 139 140 // Dead object allocation 141 () -> { 142 int size = RND.nextInt(DEAD_OBJECT_MAX_SIZE); 143 byte[] deadObject = new byte[size]; 144 }, 145 146 // Check 147 () -> { 148 List<byte[]> wrongHumongousAllocations = liveObjects.stream() 149 .filter(WB::g1IsHumongous) 150 .collect(Collectors.toList()); 151 152 if (wrongHumongousAllocations.size() > 0) { 153 wrongHumongousAllocations.stream().forEach(a -> 154 System.out.format("Non-humongous allocation of byte array of length %d and" 155 + " size %d with address %d was made in Humongous Region", 156 a.length, WB.getObjectSize(a), WB.getObjectAddress(a))); 157 error = new Error("Some non-humongous allocations were made to humongous region"); 158 shouldStop = true; 159 } 160 } 161 }; 162 } 163 164 @Override 165 public void run() { 166 while (!shouldStop) { 167 actions[RND.nextInt(actions.length)].run(); 168 Thread.yield(); 169 } 170 } 171 } 172 173 public static void main(String[] args) { 174 if (args.length != 2) { 175 throw new Error("Test Bug: Expected duration (in seconds) and percent of allocated regions were not " 176 + "provided as command line argument"); 177 } 178 179 // test duration 180 long duration = Integer.parseInt(args[0]) * 1000L; 181 // part of heap preallocated with humongous objects (in percents) 182 int percentOfAllocatedHeap = Integer.parseInt(args[1]); 183 184 long startTime = System.currentTimeMillis(); 185 186 long initialFreeRegionsCount = WB.g1NumFreeRegions(); 187 int regionsToAllocate = (int) ((double) initialFreeRegionsCount / 100.0 * percentOfAllocatedHeap); 188 long freeRegionLeft = initialFreeRegionsCount - regionsToAllocate; 189 190 System.out.println("Regions to allocate: " + regionsToAllocate + "; regions to left free: " + freeRegionLeft); 191 192 int maxMemoryPerAllocThread = (int) ((Runtime.getRuntime().freeMemory() / 100.0 193 * (100 - percentOfAllocatedHeap)) / ALLOC_THREAD_COUNT * 0.5); 194 195 System.out.println("Using " + maxMemoryPerAllocThread / 1024 + "KB for each of " + ALLOC_THREAD_COUNT 196 + " allocation threads"); 197 198 while (WB.g1NumFreeRegions() > freeRegionLeft) { 199 try { 200 humongousAllocations.add(new byte[HUMONGOUS_SIZES[RND.nextInt(HUMONGOUS_SIZES.length)]]); 201 } catch (OutOfMemoryError oom) { 202 //We got OOM trying to fill heap with humongous objects 203 //It probably means that heap is fragmented which is strange since the test logic should avoid it 204 System.out.println("Warning: OOM while allocating humongous objects - it likely means " 205 + "that heap is fragmented"); 206 break; 207 } 208 } 209 210 System.out.println("Initial free regions " + initialFreeRegionsCount + "; Free regions left " 211 + WB.g1NumFreeRegions()); 212 213 LinkedList<Thread> threads = new LinkedList<>(); 214 215 for (int i = 0; i < ALLOC_THREAD_COUNT; i++) { 216 threads.add(new Thread(new Allocator(maxMemoryPerAllocThread))); 217 } 218 219 threads.stream().forEach(Thread::start); 220 221 while ((System.currentTimeMillis() - startTime < duration) && error == null) { 222 Thread.yield(); 223 } 224 225 shouldStop = true; 226 System.out.println("Finished test"); 227 if (error != null) { 228 throw error; 229 } 230 } 231 }