1/*
2 * Copyright (c) 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
24package gc.g1.humongousObjects;
25
26import gc.testlibrary.Helpers;
27
28import java.io.IOException;
29import java.nio.file.Path;
30import java.nio.file.Paths;
31
32/**
33 * Generates non-humongous and humongous class loaders.
34 * Since the generation depends on current host architecture it cannot be done as part of pre-compilation step
35 */
36public class ClassLoaderGenerator {
37    public static void main(String[] args) throws IOException {
38
39        if (args.length != 1) {
40            throw new Error("Test Bug: Expected region size wasn't provided as command line argument");
41        }
42
43        long regionSize = Long.parseLong(args[0]) * 1024 * 1024;
44
45        Path wrkDir = Paths.get("");
46        generateClassLoader(regionSize, wrkDir);
47
48    }
49
50    public static void generateClassLoader(long regionSize, Path wrkDir) throws IOException {
51        // Generating simple classloader
52        String finalSimpleClassLoaderPrototype = TestHumongousClassLoader.GENERIC_PROTOTYPE
53                .replace("${Methods}",
54                        TestHumongousClassLoader.LOAD_CLASS_METHOD_PROTOTYPE
55                                .replace("${ClassLoadFilter}",
56                                        "fileName.equals(\"" + TestHumongousClassLoader.HUMONGOUS_CLASSLOADER_NAME
57                                                + "\")"))
58                .replace("${ClassHeader}", TestHumongousClassLoader.CLASS_HEADER)
59                .replace("${ConstructorClause}", TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE);
60
61        Helpers.generateByTemplateAndCompile(TestHumongousClassLoader.SIMPLE_CLASSLOADER_NAME, "ClassLoader",
62                finalSimpleClassLoaderPrototype, TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE, regionSize / 4,
63                wrkDir, TestHumongousClassLoader.SIMPLE_CLASSLOADER_NAME + "Base");
64
65
66        // Preparations for generating humongous classloader
67
68        // Generating condition for loadClass method of generated class loader
69        // We want the generated class loader to load only classes from G1SampleClass enum
70        // All other classes should be loaded by parent classloader
71        // As result we get full loadClass method
72        StringBuilder classesToLoadBuilder = new StringBuilder();
73        for (G1SampleClass g1SampleClass : G1SampleClass.values()) {
74            if (classesToLoadBuilder.length() != 0) {
75                classesToLoadBuilder.append(" || ");
76            }
77            classesToLoadBuilder.append("fileName.startsWith(\"" + Helpers.enumNameToClassName(g1SampleClass.name())
78                    + "\")");
79        }
80
81        // Generating final class loader prototype - with specified methods,header and constructor
82        String finalHumongousClassLoaderPrototype = TestHumongousClassLoader.GENERIC_PROTOTYPE
83                .replace("${Methods}",
84                        TestHumongousClassLoader.LOAD_CLASS_METHOD_PROTOTYPE
85                                .replace("${ClassLoadFilter}", classesToLoadBuilder))
86                .replace("${ClassHeader}", TestHumongousClassLoader.CLASS_HEADER)
87                .replace("${ConstructorClause}", TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE);
88
89
90        // Generating humongous classloader with specified name, base class, final class prototype and
91        // constructor prototype for filler classes
92        // Generated class instance should be humongous since we specify size of (regionSize * 3 / 4)
93        Helpers.generateByTemplateAndCompile(TestHumongousClassLoader.HUMONGOUS_CLASSLOADER_NAME, "ClassLoader",
94                finalHumongousClassLoaderPrototype, TestHumongousClassLoader.CONSTUCTOR_PROTOTYPE,
95                regionSize * 3 / 4,
96                wrkDir, TestHumongousClassLoader.HUMONGOUS_CLASSLOADER_NAME + "Base");
97
98
99    }
100}
101