1 /*
   2  * Copyright (c) 2015, 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.io.PrintStream;
  33 import java.math.BigInteger;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 import java.util.Random;
  37 import java.util.stream.Collectors;
  38 
  39 /**
  40  * @test TestHumongousMovement
  41  * @summary Checks that Humongous objects are not moved during GC
  42  * @requires vm.gc.G1
  43  * @library /test/lib /
  44  * @modules java.base/jdk.internal.misc
  45  * @modules java.management
  46  * @build sun.hotspot.WhiteBox
  47  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  48  *      sun.hotspot.WhiteBox$WhiteBoxPermission
  49  * @run main/othervm -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  50  *                   -XX:G1HeapRegionSize=1M -Xms200m -Xmx200m -XX:InitiatingHeapOccupancyPercent=100
  51  *                   -Xlog:gc=info:file=TestHumongousMovement.log
  52  *                   gc.g1.humongousObjects.TestHumongousMovement
  53  */
  54 
  55 public class TestHumongousMovement {
  56 
  57     private static class AllocationData {
  58         private final byte[] allocation;
  59         public final BigInteger objectAddress;
  60 
  61         public AllocationData(byte[] byteArray) {
  62             allocation = byteArray;
  63             objectAddress = new BigInteger(Long.toUnsignedString((WB.getObjectAddress(allocation))));
  64         }
  65 
  66         public boolean isAddressChanged() {
  67             return !new BigInteger(Long.toUnsignedString((WB.getObjectAddress(allocation)))).equals(objectAddress);
  68         }
  69 
  70         public void printDetails(PrintStream out) {
  71             BigInteger objectAddressAfterGC =
  72                     new BigInteger(Long.toUnsignedString((WB.getObjectAddress(allocation))));
  73 
  74             out.println(String.format("Object stored address = %d\nObject address after GC = %d\n"
  75                             + "They are %sequals", objectAddress,
  76                     objectAddressAfterGC, !objectAddress.equals(objectAddressAfterGC) ? "not " : ""));
  77         }
  78 
  79     }
  80 
  81     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  82 
  83     // How many allocated humongous objects should be deleted
  84     private static final double HUMONGOUS_OBJECTS_DELETED_FACTOR = 0.5D;
  85     // How many of free g1 regions should be used for humongous allocations
  86     private static final double ALLOCATED_HUMONGOUS_REGIONS_FACTOR = 0.25D;
  87 
  88     public static void main(String[] args) {
  89 
  90         int g1RegionSize = WB.g1RegionSize();
  91         int byteArrayMemoryOverhead = Helpers.detectByteArrayAllocationOverhead();
  92 
  93         System.out.println("Total " + WB.g1NumMaxRegions() + " regions");
  94         System.out.println("Total " + WB.g1NumFreeRegions() + " free regions");
  95 
  96         int regionsToAllocate = (int) (WB.g1NumFreeRegions() * ALLOCATED_HUMONGOUS_REGIONS_FACTOR);
  97 
  98         // Sanity check
  99         Asserts.assertGreaterThan(regionsToAllocate, 0, "Test Bug: no regions to allocate");
 100 
 101         System.out.println("Allocating " + regionsToAllocate + " humongous objects, each 90% of g1 region size");
 102 
 103         List<AllocationData> allocations = new ArrayList<>();
 104 
 105         // 90 % of maximum byte[] that takes one region
 106         int hSize = (int) ((g1RegionSize - byteArrayMemoryOverhead) * 0.9);
 107 
 108         // Sanity check
 109         Asserts.assertGreaterThan(hSize, g1RegionSize / 2, "Test Bug: allocation size is not humongous");
 110 
 111         for (int i = 0; i < regionsToAllocate; ++i) {
 112             allocations.add(new AllocationData(new byte[hSize]));
 113         }
 114 
 115         Random rnd = Utils.getRandomInstance();
 116 
 117         int toDelete = (int) (allocations.size() * HUMONGOUS_OBJECTS_DELETED_FACTOR);
 118 
 119         for (int i = 0; i < toDelete; ++i) {
 120             allocations.remove(rnd.nextInt(allocations.size()));
 121         }
 122 
 123         WB.fullGC();
 124 
 125         List<AllocationData> movedObjects = allocations.stream()
 126                 .filter(AllocationData::isAddressChanged)
 127                 .collect(Collectors.toList());
 128 
 129         if (movedObjects.size() > 0) {
 130             System.out.println("Test failed - some humongous objects moved after Full GC");
 131             movedObjects.stream().forEach(a -> a.printDetails(System.out));
 132             throw new Error("Test failed - some humongous objects moved after Full GC");
 133         } else {
 134             System.out.println("Passed");
 135         }
 136     }
 137 }