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