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