Path: blob/master/test/hotspot/jtreg/vmTestbase/gc/g1/unloading/bytecode/HumongousTemplateClassGen.java
41161 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc.g1.unloading.bytecode;2425import java.io.BufferedWriter;26import java.io.File;27import java.io.FileWriter;28import java.io.IOException;29import java.util.ArrayList;30import java.util.List;31import java.util.Random;32import jdk.test.lib.Utils;3334/*35* This class is compiled and invoke due the build to produce36* HumongousTemplateClass.java. The size of generated file is37* too large to store it in the repository.38*/3940public class HumongousTemplateClassGen {4142private static final String CLASS_NAME = "HumongousTemplateClass";43private static final String PKG_NAME = "gc.g1.unloading.bytecode";44private static final String PKG_DIR_NAME = PKG_NAME.replace(".",45File.separator);46private static final int ITERATIONS = 1075;47private static final double MG = (Math.pow(1024, 2));48private static final int RECORD_COUNT = 16 * ITERATIONS + 10;4950public static void addFileTop(List<String> records) {51records.add("package " + PKG_NAME + ";\n");52records.add("\n");53records.add("import java.util.*;\n");54records.add("\n");55records.add("public class " + CLASS_NAME + " {\n");56records.add(" public static void main() {\n");57records.add(" System.out.println(\"In humongous class \");\n");58records.add(" }");59records.add("\n");60}6162public static void addIteration(int itNum, List<String> records) {63Random rng = Utils.getRandomInstance();64records.add(" public static Object public_static_object_" + itNum65+ " = new Object();\n");66records.add(" protected static Object protected_static_object_" + itNum67+ " = new Object();\n");68records.add(" private static Object private_static_Object_" + itNum69+ " = new Object();\n");70records.add("\n");71records.add(" public static long public_static_long_" + itNum + ";\n");72records.add(" protected static long protected_static_long_" + itNum73+ " = " + rng.nextLong() + "L;\n");74records.add(" private static long private_static_long_" + itNum75+ " = 42;\n");76records.add("\n");77records.add(" public Object public_object_" + itNum78+ " = new Object();\n");79records.add(" protected Object protected_object_" + itNum80+ " = new Object();\n");81records.add(" private Object private_Object_" + itNum82+ " = new Object();\n");83records.add("\n");84records.add(" public long public_long_" + itNum + " = 43;\n");85records.add(" protected long protected_long_" + itNum + " = 44;\n");86records.add(" private long private_long_" + itNum87+ " = " + rng.nextLong() + "L;\n");88}89public static void main(String[] args) throws Exception {90if (args.length < 1) {91System.out.println("Usage: HumongousTemplateClassGen "92+ "<vm-testbase_src_folder>");93return;94}9596List<String> records = new ArrayList<String>(RECORD_COUNT);97addFileTop(records);98for (int i = 1; i < ITERATIONS; i++) {99addIteration(i, records);100}101records.add("}");102writeBuffered(records, (int) (MG * 1), args[0]);103}104105private static void writeBuffered(List<String> records, int bufSize,106String srcDir) throws IOException {107String path = srcDir + File.separator + PKG_DIR_NAME + File.separator108+ CLASS_NAME + ".java";109System.out.println("Path="+path);110File file = new File (path);111file.getParentFile().mkdirs();112file.createNewFile();113long start = System.currentTimeMillis();114FileWriter writer = new FileWriter(file);115BufferedWriter bufferedWriter = new BufferedWriter(writer, bufSize);116117for (String record: records) {118bufferedWriter.write(record);119}120bufferedWriter.flush();121bufferedWriter.close();122long end = System.currentTimeMillis();123System.out.println((end - start) / 1000f + " seconds");124}125}126127128