--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/gc/g1/humongousObjects/ClassLoaderGenerator.java Thu Apr 21 20:11:40 2016 +0000
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+package gc.g1.humongousObjects;
+
+import gc.testlibrary.Helpers;
+
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * Generates non-humongous and humongous class loaders.
+ * Since the generation depends on current host architecture it cannot be done as part of pre-compilation step
+ */
+public class ClassLoaderGenerator {
+ public static void main(String[] args) throws IOException {
+
+ if (args.length != 1) {
+ throw new Error("Test Bug: Expected region size wasn't provided as command line argument");
+ }
+
+ long regionSize = Long.parseLong(args[0]) * 1024 * 1024;
+
+ Path wrkDir = Paths.get("");
+ generateClassLoader(regionSize, wrkDir);
+
+ }
+
+ public static void generateClassLoader(long regionSize, Path wrkDir) throws IOException {
+ // Generating simple classloader
+ String finalSimpleClassLoaderPrototype = TestHumongousClassLoader.GENERIC_PROTOTYPE
+ .replace("${Methods}",
+ TestHumongousClassLoader.LOAD_CLASS_METHOD_PROTOTYPE
+ .replace("${ClassLoadFilter}",
+ "fileName.equals(\"" + TestHumongousClassLoader.HUMONGOUS_CLASSLOADER_NAME
+ + "\")"))
+ .replace("${ClassHeader}", TestHumongousClassLoader.CLASS_HEADER)
+ .replace("${ConstructorClause}", TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE);
+
+ Helpers.generateByTemplateAndCompile(TestHumongousClassLoader.SIMPLE_CLASSLOADER_NAME, "ClassLoader",
+ finalSimpleClassLoaderPrototype, TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE, regionSize / 4,
+ wrkDir, TestHumongousClassLoader.SIMPLE_CLASSLOADER_NAME + "Base");
+
+
+ // Preparations for generating humongous classloader
+
+ // Generating condition for loadClass method of generated class loader
+ // We want the generated class loader to load only classes from G1SampleClass enum
+ // All other classes should be loaded by parent classloader
+ // As result we get full loadClass method
+ StringBuilder classesToLoadBuilder = new StringBuilder();
+ for (G1SampleClass g1SampleClass : G1SampleClass.values()) {
+ if (classesToLoadBuilder.length() != 0) {
+ classesToLoadBuilder.append(" || ");
+ }
+ classesToLoadBuilder.append("fileName.startsWith(\"" + Helpers.enumNameToClassName(g1SampleClass.name())
+ + "\")");
+ }
+
+ // Generating final class loader prototype - with specified methods,header and constructor
+ String finalHumongousClassLoaderPrototype = TestHumongousClassLoader.GENERIC_PROTOTYPE
+ .replace("${Methods}",
+ TestHumongousClassLoader.LOAD_CLASS_METHOD_PROTOTYPE
+ .replace("${ClassLoadFilter}", classesToLoadBuilder))
+ .replace("${ClassHeader}", TestHumongousClassLoader.CLASS_HEADER)
+ .replace("${ConstructorClause}", TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE);
+
+
+ // Generating humongous classloader with specified name, base class, final class prototype and
+ // constructor prototype for filler classes
+ // Generated class instance should be humongous since we specify size of (regionSize * 3 / 4)
+ Helpers.generateByTemplateAndCompile(TestHumongousClassLoader.HUMONGOUS_CLASSLOADER_NAME, "ClassLoader",
+ finalHumongousClassLoaderPrototype, TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE,
+ regionSize * 3 / 4,
+ wrkDir, TestHumongousClassLoader.HUMONGOUS_CLASSLOADER_NAME + "Base");
+
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/gc/g1/humongousObjects/TestHumongousClassLoader.java Thu Apr 21 20:11:40 2016 +0000
@@ -0,0 +1,220 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+package gc.g1.humongousObjects;
+
+import gc.testlibrary.Helpers;
+import jdk.test.lib.Asserts;
+import sun.hotspot.WhiteBox;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+/**
+ * @test gc.g1.humongousObjects.TestHumongousClassLoader
+ * @summary Checks that unreachable classes and unreachable humongous class loader are unloaded after GC
+ * @requires vm.gc=="G1" | vm.gc=="null"
+ * @requires vm.opt.G1HeapRegionSize == "null" | vm.opt.G1HeapRegionSize == "1M"
+ * @library /testlibrary /test/lib /
+ * @modules java.management
+ * @build sun.hotspot.WhiteBox
+ * gc.testlibrary.Helpers
+ * gc.g1.humongousObjects.G1SampleClass
+ * gc.g1.humongousObjects.ClassLoaderGenerator
+ * gc.g1.humongousObjects.TestHumongousClassLoader
+ *
+ * @run driver ClassFileInstaller sun.hotspot.WhiteBox
+ * sun.hotspot.WhiteBox$WhiteBoxPermission
+ *
+ * @run main/othervm/timeout=240 -Xms256M -Xmx256M -XX:+WhiteBoxAPI -Xbootclasspath/a:.
+ * gc.g1.humongousObjects.ClassLoaderGenerator 1
+ *
+ * @run main/othervm -Xms256M -Xmx256M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
+ * -Xlog:classload,classunload=debug:file=TestHumongousClassLoader_Full_GC.log
+ * -XX:G1HeapRegionSize=1M
+ * gc.g1.humongousObjects.TestHumongousClassLoader FULL_GC
+ *
+ * @run main/othervm -Xms256M -Xmx256M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
+ * -Xlog:classload,classunload=debug:file=TestHumongousClassLoader_Full_GC_Mem_Pressure.log
+ * -XX:G1HeapRegionSize=1M
+ * gc.g1.humongousObjects.TestHumongousClassLoader FULL_GC_MEMORY_PRESSURE
+ *
+ *@run main/othervm -Xms256M -Xmx256M -XX:+UseG1GC -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:.
+ * -Xlog:classload,classunload=debug:file=TestHumongousClassLoader_CMC.log
+ * -XX:G1HeapRegionSize=1M -XX:MaxTenuringThreshold=1
+ * gc.g1.humongousObjects.TestHumongousClassLoader CMC
+ *
+ */
+
+public class TestHumongousClassLoader {
+
+ private static final WhiteBox WB = WhiteBox.getWhiteBox();
+ private static final String SAMPLE_CLASS_NAME_PREFIX = "SampleClassFiller";
+ public static final String SIMPLE_CLASSLOADER_NAME = "SimpleClassLoader";
+ public static final String HUMONGOUS_CLASSLOADER_NAME = "HumongousClassLoader";
+
+
+ public static final String LOAD_CLASS_METHOD_PROTOTYPE =
+ " @Override\n"
+ + " public Class loadClass(String fileName) throws ClassNotFoundException {\n"
+ + " if (${ClassLoadFilter}) {\n"
+ + " System.out.println(\"Loading class \" + fileName);\n"
+ + " byte[] b = null;\n"
+ + " try {\n"
+ + " b = Files.readAllBytes(new File(fileName + \".class\").toPath());\n"
+ + " } catch (IOException e) {\n"
+ + " e.printStackTrace();\n"
+ + " }\n"
+ + " Class c = defineClass(fileName, b, 0, b.length);\n"
+ + " resolveClass(c);\n"
+ + " return c;\n"
+ + " } else {\n"
+ + " return super.loadClass(fileName);\n"
+ + " }\n"
+ + "\n"
+ + "\n"
+ + " }\n";
+
+ public static final String CLASS_HEADER = "import java.io.File;\n"
+ + "import java.io.IOException;\n"
+ + "import java.nio.file.Files;\n"
+ + "import java.nio.file.Paths;\n";
+
+ public static final String GENERIC_PROTOTYPE = "${ClassHeader}\n"
+ + "public class ${ClassName} extends ${BaseClass}{\n"
+ + " ${ConstructorClause}\n"
+ + " ${Methods}\n"
+ + " ${Fields}\n"
+ + "}\n";
+
+ public static final String CONSTUCTOR_PROTOTYPE = "public ${ClassName}(ClassLoader parent) { super(parent);}\n";
+
+ private enum GC {
+ FULL_GC {
+ @Override
+ public void provoke() {
+ System.gc();
+ }
+ },
+ CMC {
+ @Override
+ public void provoke() {
+ // We need 2 young gc to promote class loader to old gen
+ // Otherwise it will not be unloaded after CMC
+ WHITE_BOX.youngGC();
+ Helpers.waitTillCMCFinished(WHITE_BOX, 0);
+ WHITE_BOX.youngGC();
+ Helpers.waitTillCMCFinished(WHITE_BOX, 0);
+ WHITE_BOX.g1StartConcMarkCycle();
+ Helpers.waitTillCMCFinished(WHITE_BOX, 0);
+ }
+ },
+ FULL_GC_MEMORY_PRESSURE {
+ @Override
+ public void provoke() {
+ WHITE_BOX.fullGC();
+ }
+ };
+ private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
+
+ public abstract void provoke();
+ }
+
+ public static void main(String[] args) throws ClassNotFoundException, InstantiationException,
+ IllegalAccessException, IOException, NoSuchMethodException, InvocationTargetException {
+
+ if (args.length != 1) {
+ throw new Error("Test Bug: Expected GC type wasn't provided as command line argument");
+ }
+ GC gc = GC.valueOf(args[0]);
+
+ Path wrkDir = Paths.get("");
+ URL[] url = {wrkDir.toUri().toURL()};
+ URLClassLoader urlLoader = new URLClassLoader(url);
+
+ Class<?> simpleClassLoaderClass = urlLoader.loadClass(SIMPLE_CLASSLOADER_NAME);
+
+ ClassLoader simpleClassLoader = (ClassLoader) simpleClassLoaderClass
+ .getConstructor(java.lang.ClassLoader.class)
+ .newInstance(TestHumongousClassLoader.class.getClassLoader());
+
+ // Sanity check
+ Asserts.assertEquals(WB.g1IsHumongous(simpleClassLoader), false,
+ "Test Bug: simpleClassLoader is expected to be non-humongous but it's humongous");
+
+
+ Class<?> humongousClassLoaderClass = simpleClassLoader.loadClass(HUMONGOUS_CLASSLOADER_NAME);
+
+ ClassLoader humongousClassLoader = (ClassLoader) humongousClassLoaderClass
+ .getConstructor(java.lang.ClassLoader.class)
+ .newInstance(simpleClassLoader);
+
+ // Sanity check
+ Asserts.assertEquals(WB.g1IsHumongous(humongousClassLoader), true,
+ "Test Bug: humongousClassLoader is expected to be humongous but it's non-humongous");
+
+ //Asserts.assertEquals(1,0);
+
+ Object[] loadedClasses = new Object[]{
+ G1SampleClass.LARGEST_NON_HUMONGOUS.getCls(humongousClassLoader, wrkDir, SAMPLE_CLASS_NAME_PREFIX)
+ .newInstance(),
+ G1SampleClass.SMALLEST_HUMONGOUS.getCls(humongousClassLoader, wrkDir, SAMPLE_CLASS_NAME_PREFIX)
+ .newInstance(),
+ G1SampleClass.ONE_REGION_HUMONGOUS.getCls(humongousClassLoader, wrkDir, SAMPLE_CLASS_NAME_PREFIX)
+ .newInstance(),
+ G1SampleClass.TWO_REGION_HUMONGOUS.getCls(humongousClassLoader, wrkDir, SAMPLE_CLASS_NAME_PREFIX)
+ .newInstance(),
+ };
+
+ // forgetting references to loaded classes
+ for (int i = 0; i < loadedClasses.length; ++i) {
+ loadedClasses[i] = null;
+ }
+
+ // forgetting referencies to classloaders
+ humongousClassLoader = null;
+ humongousClassLoaderClass = null;
+
+ simpleClassLoader = null;
+ simpleClassLoaderClass = null;
+
+ gc.provoke();
+
+ // Test checks
+ Asserts.assertEquals(WB.isClassAlive(HUMONGOUS_CLASSLOADER_NAME), false,
+ String.format("Classloader class %s is loaded after we forget all references to it",
+ HUMONGOUS_CLASSLOADER_NAME));
+
+ for (G1SampleClass sampleClass : G1SampleClass.values()) {
+ String className = Helpers.enumNameToClassName(sampleClass.name()) + "Class";
+ Asserts.assertEquals(WB.isClassAlive(className), false,
+ String.format("Class %s is loaded after we forget all references to it", className));
+ }
+ }
+
+}
--- a/hotspot/test/gc/testlibrary/Helpers.java Thu Apr 21 15:59:07 2016 -0400
+++ b/hotspot/test/gc/testlibrary/Helpers.java Thu Apr 21 20:11:40 2016 +0000
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.nio.file.Paths;
public class Helpers {
@@ -45,14 +46,14 @@
/**
* According class file format theoretical amount of fields in class is u2 which is (256 * 256 - 1).
- * Some service info takes place in constant pool and we really could make a class with only (256 * 256 - 29)
- * fields.
+ * Some service info takes place in constant pool and we really could make a class with lesser amount of fields.
+ *
* Since the exact value is not so important and I would like to avoid issues that may be caused by future changes/
- * different archs etc I selected (256 * 256 - 32) for this constant.
+ * different archs etc I selected (256 * 256 - 1024) for this constant.
* The test works with other values too but the smaller the number the more classes we need to generate and it takes
* more time
*/
- private static final int MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS = 256 * 256 - 32;
+ private static final int MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS = 256 * 256 - 1024;
/**
* Detects amount of extra bytes required to allocate a byte array.
@@ -140,6 +141,28 @@
}
/**
+ * Generates specified amount of long fields
+ * Result string will looks like this:
+ * <p>
+ * long f0;
+ * ...
+ * long fNNN;
+ *
+ * @param fieldCount count of long fields
+ * @return generated fields
+ */
+ private static String fieldsGenerator(long fieldCount) {
+ StringBuilder fieldsBuilder = new StringBuilder();
+
+ for (int i = 0; i < fieldCount; ++i) {
+ fieldsBuilder.append(String.format("long f%d;\n", i));
+ }
+
+ return fieldsBuilder.toString();
+ }
+
+
+ /**
* Changes string from enum notation to class notation - i.e. "VERY_SMALL_CAT" to "VerySmallCat"
*
* @param enumName string in enum notation
@@ -216,22 +239,121 @@
// for the last generated class we use specified class name
String clsName = (i == generatedClassesCount - 1) ? className : prefix + i;
- Helpers.compileClass(clsName, workDir,
- Helpers.generate(
- clsName,
- // for first generated class we don't have 'extends'
- (i == 0 ? null : prefix + (i - 1)),
- null,
- // for the last generated class we use different field count
- (i == generatedClassesCount - 1) ? fieldsInLastClassCount
- : MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS));
+ // If we already have a file with the same name we do not create it again
+ if (Files.notExists(Paths.get(clsName + ".java"))) {
+ Helpers.compileClass(clsName, workDir,
+ Helpers.generate(
+ clsName,
+ // for first generated class we don't have 'extends'
+ (i == 0 ? null : prefix + (i - 1)),
+ null,
+ // for the last generated class we use different field count
+ (i == generatedClassesCount - 1) ? fieldsInLastClassCount
+ : MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS));
+ } else {
+ System.out.println("Class " + clsName +
+ ".java already exists, skipping class' generation and compilation");
+ }
+
}
return classLoader.loadClass(className);
}
/**
+ * Creates a class which instances will be approximately of the requested size.
+ * This method produces a java source from a class template by substituting values instead of parameters.
+ * Then the obtained source is compiled.
+ * Generated class will looks like this:
+ * classTemplate
+ * constructorTemplate
+ * long f0;
+ * ...
+ * long fNNN;
+ * <p>
+ * }
+ *
+ * @param className generated class name
+ * @param baseClass base class
+ * @param classTemplate class template - the first part of class. ${ClassName} and ${BaseClass} will be replaced
+ * with values from className and baseClass,one entry of ${Fields} will be replaced with
+ * generated long fields. Class template should look like this:
+ * imports;
+ * public class ${ClassName} extends ${BaseClass} {
+ * public ${ClassName} { some code here;}
+ * some methods
+ * ${Fields}
+ *
+ * }
+ * @param constructorTemplate constructor template, ${ClassName} would be replaced on actual class name
+ * @param instanceSize size of generated class' instance. Size should be aligned by 8 bytes
+ * @param workDir working dir where generated classes are put and compiled
+ * @param prefix prefix for service classes (ones we use to create chain of inheritance).
+ * The names will be prefix_1, prefix_2,.., prefix_n
+ * @return Class object of generated and compiled class loaded in specified class loader
+ * @throws IOException if cannot write or read to workDir
+ */
+ public static void generateByTemplateAndCompile(String className, String baseClass, String classTemplate,
+ String constructorTemplate, long instanceSize, Path workDir,
+ String prefix) throws IOException {
+
+ if (instanceSize % SIZE_OF_LONG != 0L) {
+ throw new Error(String.format("Test bug: only sizes aligned by %d bytes are supported and %d was specified",
+ SIZE_OF_LONG, instanceSize));
+ }
+
+ int instanceSizeWithoutObjectHeaderInWords =
+ (int) (instanceSize - WhiteBox.getWhiteBox().getObjectSize(new Object())) / SIZE_OF_LONG;
+
+ if (instanceSizeWithoutObjectHeaderInWords <= 0) {
+ throw new Error(String.format("Test bug: specified instance size is too small - %d."
+ + " Cannot generate any classes", instanceSize));
+ }
+
+ int sizeOfLastFile = instanceSizeWithoutObjectHeaderInWords % MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS;
+ int generatedClassesCount = instanceSizeWithoutObjectHeaderInWords / MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS;
+
+ // Do all the classes have the maximum number of fields?
+ int fieldsInLastClassCount;
+
+ if (sizeOfLastFile == 0) {
+ fieldsInLastClassCount = MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS;
+ } else {
+ generatedClassesCount++;
+ fieldsInLastClassCount = sizeOfLastFile;
+ }
+
+ // first (generatedClassesCount - 1) classes are just fillers - just long fields and constructor
+ for (int i = 0; i < generatedClassesCount - 1; i++) {
+ String clsName = prefix + i;
+
+ Helpers.compileClass(clsName, workDir,
+ Helpers.generate(
+ clsName,
+ // first generated class extends base class
+ (i == 0 ? baseClass : prefix + (i - 1)),
+ constructorTemplate.replace("${ClassName}", clsName),
+ MAXIMUM_AMOUNT_OF_FIELDS_IN_CLASS));
+ }
+
+ // generating last class - the one with specified className
+ Helpers.compileClass(className, workDir,
+ classTemplate.replaceAll("\\$\\{ClassName\\}", className)
+ // if no fillers were generated (generatedClassesCount == 1)
+ // the last class should extends baseClass
+ // otherwise it should extend last generated filler class which name is
+ // prefix + (generatedClassesCount - 2)
+ // generatedClassesCount is always not smaller than 1
+ .replace("${BaseClass}",
+ generatedClassesCount == 1 ? baseClass :
+ prefix + (generatedClassesCount - 2))
+ .replace("${Fields}", fieldsGenerator(fieldsInLastClassCount))
+ );
+ }
+
+ /**
* Waits until Concurent Mark Cycle finishes
- * @param wb Whitebox instance
+ *
+ * @param wb Whitebox instance
* @param sleepTime sleep time
*/
public static void waitTillCMCFinished(WhiteBox wb, int sleepTime) {