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 gc.testlibrary.Helpers;
  28 import jdk.test.lib.Asserts;
  29 import jdk.test.lib.Utils;
  30 import sun.hotspot.WhiteBox;
  31 
  32 import java.util.Calendar;
  33 import java.util.LinkedList;
  34 import java.util.Random;
  35 
  36 /**
  37  * @test TestNoAllocationsInHRegions
  38  * @summary Checks that no additional allocations are made in humongous regions
  39  * @requires vm.gc=="G1" | vm.gc=="null"
  40  * @library /testlibrary /test/lib /
  41  * @modules java.management java.base/jdk.internal.misc
  42  * @build sun.hotspot.WhiteBox
  43  *        gc.testlibrary.Helpers
  44  *        gc.g1.humongousObjects.TestNoAllocationsInHRegions
  45  *
  46  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  47  *      sun.hotspot.WhiteBox$WhiteBoxPermission
  48  *
  49  * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  50  *                   -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m
  51  *                   -Xlog:gc=trace:file=TestNoAllocationsInHRegions10.log
  52  *                   gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 10
  53  *
  54  * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  55  *                   -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m
  56  *                   -Xlog:gc=trace:file=TestNoAllocationsInHRegions50.log
  57  *                   gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 50
  58  *
  59  * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  60  *                   -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m
  61  *                   -Xlog:gc=trace:file=TestNoAllocationsInHRegions80.log
  62  *                   gc.g1.humongousObjects.TestNoAllocationsInHRegions 30 80
  63  */
  64 public class TestNoAllocationsInHRegions {
  65     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  66     private static final Random RND = Utils.getRandomInstance();
  67     private static final int G1_REGION_SIZE = WB.g1RegionSize();
  68     private static final int[] HUMONGOUS_SIZES = {G1_REGION_SIZE / 2, G1_REGION_SIZE + 1, G1_REGION_SIZE * 2 + 1};
  69     private static final int ARRAY_ALLOCATION_OVERHEAD = Helpers.detectByteArrayAllocationOverhead();
  70     private static final int ALLOCATORS_COUNT = 5;
  71 
  72     private static volatile boolean shouldStop = false;
  73     private static volatile Error error = null;
  74 
  75     static class Allocator implements Runnable {
  76         @Override
  77         public void run() {
  78             while (!shouldStop) {
  79                 byte[] allocation = new byte[RND.nextInt(G1_REGION_SIZE / 2 - ARRAY_ALLOCATION_OVERHEAD)];
  80 
  81                 // Sanity check
  82                 if (WB.g1IsHumongous(allocation)) {
  83                     error = new Error(String.format("Test Bug: Byte array of size"
  84                         + " %d is expected to be non-humongous but it is humongous", allocation.length));
  85                     break;
  86                 }
  87 
  88                 // Test check
  89                 if (WB.g1BelongsToHumongousRegion(WB.getObjectAddress(allocation))) {
  90                     String errorMessage = String.format("Non-humongous allocation of byte array of length %d and"
  91                         + " size %d with address %d was made in Humongous Region",
  92                             allocation.length, WB.getObjectSize(allocation), WB.getObjectAddress(allocation));
  93                     error = new Error(errorMessage);
  94                     break;
  95                 }
  96                 Thread.yield();
  97             }
  98         }
  99     }
 100 
 101     public static void main(String[] args) {
 102         if (args.length != 2) {
 103             throw new Error("Test Bug: Expected duration (in seconds) and per cent of allocated regions were not "
 104                     + "provided as command line argument");
 105         }
 106         long duration = Integer.parseInt(args[0]) * 1000L;
 107 
 108 
 109         Calendar calendar = Calendar.getInstance();
 110         long startTime = calendar.getTimeInMillis();
 111         long initialFreeRegionsCount = WB.g1NumFreeRegions();
 112 
 113         int regionsToAllocate =  (int) ((double) initialFreeRegionsCount / 100.0 * Integer.parseInt(args[1]));
 114 
 115         long freeRegionLeft = initialFreeRegionsCount - regionsToAllocate;
 116 
 117         System.out.println("Regions to allocate: " + regionsToAllocate + "; regions to left free: " + freeRegionLeft);
 118 
 119         // We fill specified part of heap with humongous objects
 120         LinkedList<byte[]> humongousAllocations = new LinkedList<>();
 121 
 122         while (WB.g1NumFreeRegions() > freeRegionLeft) {
 123             try {
 124                 humongousAllocations.add(new byte[HUMONGOUS_SIZES[RND.nextInt(HUMONGOUS_SIZES.length)]]);
 125             } catch (OutOfMemoryError oom) {
 126                 //We got OOM trying to fill heep with humongous objects
 127                 //It probably means that heap is fragmented which is strange since the test logic should avoid it
 128                 System.out.println("Warning: OOM while allocating humongous objects - it likely means "
 129                         + "that heap is fragmented");
 130                 break;
 131             }
 132         }
 133 
 134         System.out.println("Initial free regions " + initialFreeRegionsCount + "; Free regions left "
 135                 + WB.g1NumFreeRegions());
 136 
 137         LinkedList<Thread> threads = new LinkedList<>();
 138 
 139         for (int i = 0; i < ALLOCATORS_COUNT; i++) {
 140             threads.add(new Thread(new Allocator()));
 141         }
 142 
 143         threads.stream().forEach(Thread::start);
 144 
 145         while ((Calendar.getInstance().getTimeInMillis() - startTime < duration) && error == null) {
 146             Thread.yield();
 147         }
 148 
 149         shouldStop = true;
 150         System.out.println("Finished test");
 151         if (error != null) {
 152             throw error;
 153         }
 154     }
 155 }