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