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 jdk.test.lib.Asserts;
  28 import sun.hotspot.WhiteBox;
  29 
  30 import java.io.IOException;
  31 import java.net.URL;
  32 import java.net.URLClassLoader;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 
  37 /**
  38  * @test gc.g1.humongousObjects.TestHumongousNonArrayAllocation
  39  * @summary Checks that huge class' instances (ie with huge amount of fields) are allocated successfully
  40  * @requires vm.gc.G1
  41  * @requires vm.opt.G1HeapRegionSize == "null" | vm.opt.G1HeapRegionSize == "1M"
  42  * @library /testlibrary /test/lib /
  43  * @modules java.base/jdk.internal.misc
  44  * @modules java.management
  45  * @build sun.hotspot.WhiteBox
  46  *        gc.testlibrary.Helpers
  47  *        gc.g1.humongousObjects.G1SampleClass
  48  *        gc.g1.humongousObjects.TestHumongousNonArrayAllocation
  49  *
  50  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  51  *                                sun.hotspot.WhiteBox$WhiteBoxPermission
  52  *
  53  * @run main/othervm -Xms128M -Xmx128M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  54  *                   -XX:G1HeapRegionSize=1M
  55  *                   gc.g1.humongousObjects.TestHumongousNonArrayAllocation LARGEST_NON_HUMONGOUS
  56  *
  57  * @run main/othervm -Xms128M -Xmx128M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  58  *                   -XX:G1HeapRegionSize=1M
  59  *                   gc.g1.humongousObjects.TestHumongousNonArrayAllocation SMALLEST_HUMONGOUS
  60  *
  61  * @run main/othervm -Xms128M -Xmx128M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  62  *                   -XX:G1HeapRegionSize=1M
  63  *                   gc.g1.humongousObjects.TestHumongousNonArrayAllocation ONE_REGION_HUMONGOUS
  64  *
  65  * @run main/othervm -Xms128M -Xmx128M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  66  *                   -XX:G1HeapRegionSize=1M
  67  *                   gc.g1.humongousObjects.TestHumongousNonArrayAllocation TWO_REGION_HUMONGOUS
  68  *
  69  * @run main/othervm -Xms128M -Xmx128M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
  70  *                   -XX:G1HeapRegionSize=1M
  71  *                   gc.g1.humongousObjects.TestHumongousNonArrayAllocation MORE_THAN_TWO_REGION_HUMONGOUS
  72  *
  73  */
  74 
  75 /**
  76  * The test for objects which are instances of classes with a huge amount of fields. It's an alternative way to create
  77  * a humongous object rather to allocate a long array.
  78  * The size of a class object depends on the field declared in the class. So, the tests generates such classes to cover
  79  * the following cases:
  80  * largest non-humongous object (exactly half a region)
  81  * smallest humongous object (half a region + sizeof(long))
  82  * humongous object that takes exactly one region
  83  * humongous object that takes more than one region (region + sizeof(long))
  84  * humongous object that takes more than two regions (region * 2 + sizeof(long))
  85  *
  86  */
  87 public class TestHumongousNonArrayAllocation {
  88     private static final WhiteBox WB = WhiteBox.getWhiteBox();
  89     private static final String CLASS_NAME_PREFIX = TestHumongousNonArrayAllocation.class.getSimpleName() + "_";
  90 
  91     public static void main(String[] args) throws ClassNotFoundException, InstantiationException,
  92             IllegalAccessException, IOException {
  93 
  94         if (args.length != 1) {
  95             throw new Error("Test Bug: Expected class name wasn't provided as command line argument");
  96         }
  97         G1SampleClass sampleClass = G1SampleClass.valueOf(args[0]);
  98 
  99         Path wrkDir = Files.createTempDirectory(Paths.get(""), CLASS_NAME_PREFIX);
 100         URL[] url = {wrkDir.toUri().toURL()};
 101         URLClassLoader urlLoader = new URLClassLoader(url);
 102 
 103         Object sampleObject;
 104         try {
 105             sampleObject = sampleClass.getCls(urlLoader, wrkDir, CLASS_NAME_PREFIX).newInstance();
 106         } catch (Throwable throwable) {
 107             throw new AssertionError("Test Bug: Cannot create object of provided class", throwable);
 108         }
 109 
 110         boolean isHumongous = WB.g1IsHumongous(sampleObject);
 111         boolean shouldBeHumongous = (sampleClass.expectedInstanceSize() > (WB.g1RegionSize() / 2));
 112 
 113         // Sanity check
 114         Asserts.assertEquals(WB.getObjectSize(sampleObject), sampleClass.expectedInstanceSize(),
 115                 String.format("Test Bug: Object of class %s is expected to take %d bytes but it takes %d.",
 116                         sampleClass.name(), sampleClass.expectedInstanceSize(), WB.getObjectSize(sampleObject)));
 117 
 118         // Test check
 119         Asserts.assertEquals(isHumongous, shouldBeHumongous,
 120                 String.format("Object of class %s is expected to be %shumongous but it is not",
 121                         sampleClass.name(), (shouldBeHumongous ? "" : "non-")));
 122     }
 123 
 124 }