1 /*
2  * Copyright (c) 2015, 2020, 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 package gc.g1.unloading.bytecode;
25 
26 import java.io.BufferedWriter;
27 import java.io.File;
28 import java.io.FileWriter;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Random;
33 import jdk.test.lib.Utils;
34 
35 /*
36  *  This class is compiled and invoke due the build to produce
37  *  HumongousTemplateClass.java. The size of generated file is
38  *  too large to store it in the repository.
39  */
40 
41 public class HumongousTemplateClassGen {
42 
43 private static final String CLASS_NAME = "HumongousTemplateClass";
44 private static final String PKG_NAME = "gc.g1.unloading.bytecode";
45 private static final String PKG_DIR_NAME = PKG_NAME.replace(".",
46         File.separator);
47 private static final int ITERATIONS = 1075;
48 private static final double MG = (Math.pow(1024, 2));
49 private static final int RECORD_COUNT = 16 * ITERATIONS + 10;
50 
addFileTop(List<String> records)51 public static void addFileTop(List<String> records) {
52     records.add("package " + PKG_NAME + ";\n");
53     records.add("\n");
54     records.add("import java.util.*;\n");
55     records.add("\n");
56     records.add("public class " + CLASS_NAME + " {\n");
57     records.add("    public static void main() {\n");
58     records.add("        System.out.println(\"In humongous class \");\n");
59     records.add("    }");
60     records.add("\n");
61 }
62 
addIteration(int itNum, List<String> records)63 public static void addIteration(int itNum, List<String> records) {
64     Random rng = Utils.getRandomInstance();
65     records.add("    public static Object public_static_object_" + itNum
66             + " = new Object();\n");
67     records.add("    protected static Object protected_static_object_" + itNum
68             + " = new Object();\n");
69     records.add("    private static Object private_static_Object_" + itNum
70             + " = new Object();\n");
71     records.add("\n");
72     records.add("    public static long public_static_long_" + itNum + ";\n");
73     records.add("    protected static long protected_static_long_" + itNum
74             + " = " + rng.nextLong() + "L;\n");
75     records.add("    private static long private_static_long_" + itNum
76             + " = 42;\n");
77     records.add("\n");
78     records.add("    public Object public_object_" + itNum
79             + " = new Object();\n");
80     records.add("    protected Object protected_object_" + itNum
81             + " = new Object();\n");
82     records.add("    private Object private_Object_" + itNum
83             + " = new Object();\n");
84     records.add("\n");
85     records.add("    public long public_long_" + itNum + " = 43;\n");
86     records.add("    protected long protected_long_" + itNum + " = 44;\n");
87     records.add("    private long private_long_" + itNum
88             + " = " + rng.nextLong() + "L;\n");
89 }
main(String[] args)90 public static void main(String[] args) throws Exception {
91     if (args.length < 1) {
92         System.out.println("Usage: HumongousTemplateClassGen "
93                         + "<vm-testbase_src_folder>");
94         return;
95     }
96 
97     List<String> records = new ArrayList<String>(RECORD_COUNT);
98     addFileTop(records);
99     for (int i = 1; i < ITERATIONS; i++) {
100         addIteration(i, records);
101     }
102     records.add("}");
103     writeBuffered(records, (int) (MG * 1), args[0]);
104 }
105 
writeBuffered(List<String> records, int bufSize, String srcDir)106 private static void writeBuffered(List<String> records, int bufSize,
107         String srcDir) throws IOException {
108     String path = srcDir + File.separator + PKG_DIR_NAME + File.separator
109             + CLASS_NAME + ".java";
110     System.out.println("Path="+path);
111     File file = new File (path);
112     file.getParentFile().mkdirs();
113     file.createNewFile();
114     long start = System.currentTimeMillis();
115     FileWriter writer = new FileWriter(file);
116     BufferedWriter bufferedWriter = new BufferedWriter(writer, bufSize);
117 
118     for (String record: records) {
119        bufferedWriter.write(record);
120     }
121     bufferedWriter.flush();
122     bufferedWriter.close();
123     long end = System.currentTimeMillis();
124     System.out.println((end - start) / 1000f + " seconds");
125 }
126 }
127