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