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