From 7f6dfcd44c0add21bff6ad24538239b8889469a5 Mon Sep 17 00:00:00 2001 From: Anna Babu Palathingal <148897727+annaibm@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:37:03 -0400 Subject: [PATCH] Generate classes_jdk.jar instead of classes.jar - Include Java version number as in classes_jdk8.jar while generating the classes.jar file. closes: https://github.com/eclipse-openj9/openj9/issues/19582 Signed-off-by: Anna Babu Palathingal --- .../net/openj9/stf/SharedClasses.java | 16 +- .../net/openj9/test/sc/JavaGen.java | 229 ++++++++++-------- 2 files changed, 141 insertions(+), 104 deletions(-) diff --git a/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/stf/SharedClasses.java b/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/stf/SharedClasses.java index 81e8140..ca13537 100644 --- a/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/stf/SharedClasses.java +++ b/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/stf/SharedClasses.java @@ -186,16 +186,21 @@ public void setUp(StfCoreExtension test, StfSharedClassesExtension sharedClasses // See whether the test data (20000 classes and jars) already exist. // If not, create them in the first -systemtest-prereqs directory (or the default location if no -systemtest-prereqs were supplied). DirectoryRef sharedClassesDataDir = null; // This will be initialised when we find it. + int javaVersion = test.env().primaryJvm().getJavaVersion(); + System.out.println("java.version = " + javaVersion); String dataSubdir = "sharedClassesTestData/v1"; ArrayList prereqRoots = test.env().getPrereqRoots(); + String classFileName = "classes_jdk" + javaVersion + ".jar"; + FileRef sharedClassesJar = null; int found = 0; for (int i = 0 ; (i < prereqRoots.size()) && ( found == 0 ); i++ ) { sharedClassesDataDir = prereqRoots.get(i).childDirectory(dataSubdir); - if (!sharedClassesDataDir.exists()) { - System.out.println(sharedClassesDataDir.getSpec() + " does not exist"); + sharedClassesJar = sharedClassesDataDir.childFile(classFileName); + if (!sharedClassesJar.exists()) { + System.out.println(sharedClassesJar.getSpec() + " does not exist"); } else { - System.out.println(sharedClassesDataDir.getSpec() + " exists"); + System.out.println(sharedClassesJar.getSpec() + " exists"); found = 1; } } @@ -211,7 +216,8 @@ public void setUp(StfCoreExtension test, StfSharedClassesExtension sharedClasses .addProjectToClasspath("openj9.test.sharedClasses") .runClass(JavaGen.class) .addArg(sharedClassesDataDir.getSpec()) - .addArg("10000")); + .addArg("10000") + .addArg(String.valueOf(javaVersion))); } // Copy the shared classes jar/s from the systemtest_prereqs directory to /tmp. @@ -221,7 +227,7 @@ public void setUp(StfCoreExtension test, StfSharedClassesExtension sharedClasses DirectoryRef localSharedClassesJarsDir = test.doCpDir("Copy sharedClasses jars", appsSharedClassesJarsDir, test.env().getTmpDir().childDirectory("jars")); localSharedClassesResources = localSharedClassesJarsDir.getSpec(); } else { - FileRef sharedClassesJar = sharedClassesDataDir.childFile("classes.jar"); + sharedClassesJar = sharedClassesDataDir.childFile(classFileName); FileRef localSharedClassesJar = test.doCp("Copy sharedClasses jar", sharedClassesJar, test.env().getTmpDir()); localSharedClassesResources = localSharedClassesJar.getSpec(); } diff --git a/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/test/sc/JavaGen.java b/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/test/sc/JavaGen.java index ce3b092..47e01fd 100644 --- a/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/test/sc/JavaGen.java +++ b/openj9.test.sharedClasses/src/test.sharedClasses/net/openj9/test/sc/JavaGen.java @@ -3,16 +3,16 @@ * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which accompanies this distribution -* and is available at http://eclipse.org/legal/epl-2.0 or the Apache License, -* Version 2.0 which accompanies this distribution and is available at +* and is available at http://eclipse.org/legal/epl-2.0 or the Apache License, +* Version 2.0 which accompanies this distribution and is available at * https://www.apache.org/licenses/LICENSE-2.0. -* +* * This Source Code may also be made available under the following Secondary -* Licenses when the conditions for such availability set forth in the +* Licenses when the conditions for such availability set forth in the * Eclipse Public License, v. 2.0 are satisfied: GNU General Public License, * version 2 with the GNU Classpath Exception [1] and GNU General Public License, * version 2 with the OpenJDK Assembly Exception [2]. -* +* * [1] https://www.gnu.org/software/classpath/license.html * [2] https://openjdk.org/legal/assembly-exception.html * @@ -48,9 +48,9 @@ public class JavaGen { static SimpleDateFormat dateFormatter = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); - + List fileNames = new ArrayList(); - + static final int COMPILE_BATCH_SIZE = 100; public static void main(String[] args) @@ -59,9 +59,10 @@ public static void main(String[] args) // arg 2 number of files String path = args[0]; String num = args[1]; - + int javaVersion = Integer.parseInt(args[2]); + log("Generating jar files in " + path); - + int numArg = 0; try { @@ -73,7 +74,7 @@ public static void main(String[] args) } File destination = new File(path); - + if (!destination.exists()) { boolean b = destination.mkdirs(); @@ -91,49 +92,57 @@ public static void main(String[] args) JavaGen generator = new JavaGen(); - generator.go(path, numArg); - + generator.go(path, numArg, javaVersion); + } - - void go(String path, int numArg) { + + void go(String path, int numArg, int javaVersion) { log("Creating source files"); - makeJavas(path, numArg); //Make java source files + makeJavas(path, numArg,javaVersion); //Make java source files log("Compiling java files"); compileJavas(path); //Compile Source to .class files log("Source files compiled, creating jar files"); - makeJars(path, numArg); //Create jar files + makeJars(path, numArg,javaVersion); //Create jar files log("Jar files created"); return; } - + /** * Prints a message to stdout with a timestamp */ public static void log(String message) { System.out.println(dateFormatter.format(new Date()) + ": " + message); } - + /** * Creates a large number of java files. - * + * * @param directory String, the directory to write them into. * @param count int, the number of tests to generate. */ - private void makeJavas(String dir, int count) + private void makeJavas(String dir, int count, int javaVersion) { + System.out.println("dir:" + dir); String sl = System.getProperty("file.separator"); String directory = dir + sl + "net" + sl + "openj9" + sl + "sc" + sl + "classes"; - + File fileDir = new File(directory); + if (fileDir.exists()) { + try { + deleteDirectory(fileDir); + } catch (IOException e) { + throw new RuntimeException("Failed to delete existing directory: " + e.getMessage()); + } + } boolean mkDirBool = fileDir.mkdirs(); - + if (!mkDirBool) { throw new RuntimeException("Failed to create destination directory for source files"); } - + int nameLen; - + if (("" + (count-1)).length() < ("" + count).length()) { nameLen = ("" + (count-1)).length(); @@ -141,14 +150,14 @@ private void makeJavas(String dir, int count) { nameLen = ("" + count).length(); } - + String format = new String(""); while (nameLen > 0) { format = format + "0"; nameLen--; } - + // Create Init class for locking the large jar files try { @@ -174,26 +183,26 @@ private void makeJavas(String dir, int count) bWrite.write("}"); bWrite.newLine(); - + for (int j = 0; j < 100; j++) { writeFunc(j,bWrite); } - + bWrite.write("public int getID(){"); bWrite.newLine(); bWrite.write("return id;"); bWrite.newLine(); bWrite.write("}"); bWrite.newLine(); - + bWrite.write("public String getName(){"); bWrite.newLine(); bWrite.write("return name;"); bWrite.newLine(); bWrite.write("}"); bWrite.newLine(); - + bWrite.write("}"); bWrite.flush(); bWrite.close(); @@ -204,7 +213,7 @@ private void makeJavas(String dir, int count) } DecimalFormat nameFormat = new DecimalFormat(format); - + try { for (int i = 0; i < count; i++) @@ -215,8 +224,8 @@ private void makeJavas(String dir, int count) //log("Creating " + fName); BufferedWriter bWrite = new BufferedWriter(new FileWriter(fName)); bWrite.write("package net.openj9.sc.classes;"); - bWrite.newLine(); - + bWrite.newLine(); + // ***************************************************** // This writes code which generates a random string when the code is executed. // This increases the length of the test case execution dramatically, so @@ -236,7 +245,7 @@ private void makeJavas(String dir, int count) // bWrite.write("myString = makeString(500);"); // bWrite.newLine(); // ************************************************* - + // ***************************************************** // Use the same string each time. // The string length has been chosen so that we can fit more classes into the shared class cache. @@ -251,7 +260,7 @@ private void makeJavas(String dir, int count) //bWrite.write("\"w1UrYHQ149753246J3tNgm9hNOsY8cbn6s12N74s7KGknU7C9071531IbQblk9bsYnQ1ZbKP2hk9A0NT19dn03r8K0jX6xr76HamH4X9gQ1uwusEllk9sN57R8rbiIt2F2t1jr254q4e7Nq0\" +"); //bWrite.newLine(); //bWrite.write("\"ZRp5rYkSXnt3btmW16e8pV6tPHt1f44Bem486PpS3XksQ8QK4127E4831odDLoZWZh8FH3621gy7O20JRDrOMLTmMd9gIb593zgGw6W12yRlgvbd724M5tfpZlF897qoVwC1J8ilLn1DqZ8u\";"); - //bWrite.newLine(); + //bWrite.newLine(); bWrite.write("public Test_Init_" + nameFormat.format(i) + "(){}"); bWrite.newLine(); bWrite.write("public int getID(){return " + (0-i) + ";}"); @@ -259,10 +268,10 @@ private void makeJavas(String dir, int count) bWrite.write("public String getName(){"); bWrite.newLine(); // ************************************************* - + // This writes the rest of the class out writeStringFunctions(bWrite); - + bWrite.flush(); bWrite.close(); } @@ -271,7 +280,7 @@ private void makeJavas(String dir, int count) e.printStackTrace(); System.exit(1); } - + try { for (int i = 0; i < count; i++) @@ -283,7 +292,7 @@ private void makeJavas(String dir, int count) BufferedWriter bWrite = new BufferedWriter(new FileWriter(fName)); bWrite.write("package net.openj9.sc.classes;"); bWrite.newLine(); - + // ***************************************************** // This writes code which generates a random string when the code is executed. // This increases the length of the test case execution dramatically, so @@ -303,13 +312,13 @@ private void makeJavas(String dir, int count) // bWrite.write("myString = makeString(50);"); // bWrite.newLine(); // ************************************************* - + // ***************************************************** // Use the same string each time bWrite.write("public class TestClass_" + nameFormat.format(i) + " implements net.openj9.test.sc.classes.Dummy{"); bWrite.newLine(); bWrite.write("private String myString = \"ZRp5rYkSXnt3btmW16e8pV6tPHt1f44Bem486PpS3XksQ8QK4127E4831odDLoZWZh8FH3621gy7O20JRDrOMLTmMd9gIb593zgGw6W12yRlgvbd724M5tfpZlF897qoVwC1J8ilLn1DqZ8u\";"); - bWrite.newLine(); + bWrite.newLine(); bWrite.write("public TestClass_" + nameFormat.format(i) + "(){}"); bWrite.newLine(); bWrite.write("public int getID(){return " + (0-i) + ";}"); @@ -318,10 +327,10 @@ private void makeJavas(String dir, int count) bWrite.newLine(); // // ************************************************* - + // this writes the rest of the class out writeStringFunctions(bWrite); - + bWrite.flush(); bWrite.close(); } @@ -330,14 +339,14 @@ private void makeJavas(String dir, int count) e.printStackTrace(); System.exit(1); } - + } - + private void compileJavas(String dir) { Iterator iFnames = fileNames.iterator(); - + int count = 0; int logCount = 0; while (iFnames.hasNext()) @@ -347,9 +356,9 @@ private void compileJavas(String dir) } JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); - + List sourceFiles = new ArrayList(); - + while ((count < COMPILE_BATCH_SIZE) && iFnames.hasNext()) { File f = new File(iFnames.next() + ".java"); @@ -358,7 +367,7 @@ private void compileJavas(String dir) count++; logCount++; } - + ArrayList javacOptions = new ArrayList(); String javacOutDirOption = "-d"; javacOptions.add(javacOutDirOption); @@ -370,16 +379,16 @@ private void compileJavas(String dir) javacOptions.add(dir); } - + Iterable compileList = fileManager.getJavaFileObjectsFromFiles(sourceFiles); CompilationTask compileTask = compiler.getTask(null, fileManager, null, javacOptions, null, compileList); boolean result = compileTask.call(); - + if (!result) { System.out.println("Compilation failed"); - - try + + try { fileManager.close(); } catch (IOException e) @@ -387,11 +396,11 @@ private void compileJavas(String dir) e.printStackTrace(); System.exit(1); } - + System.exit(1); } - - try + + try { fileManager.close(); } catch (IOException e) @@ -403,22 +412,22 @@ private void compileJavas(String dir) count = 0; } - - + + } - - private void makeJars(String directory, int numJars) + + private void makeJars(String directory, int numJars, int javaVersion) { String sl = System.getProperty("file.separator"); Iterator fNomIt = fileNames.iterator(); - + boolean isWindows = (System.getProperty("os.name").toLowerCase().indexOf("win")) > -1; - + // Make one big jar file - try + try { log("Creating one large jar file"); - String jarFileName = directory + sl + "classes.jar"; + String jarFileName = directory + sl + "classes_jdk" + javaVersion + ".jar"; FileOutputStream fOutStr = new FileOutputStream(jarFileName); JarOutputStream jOutStr = new JarOutputStream(fOutStr); @@ -433,50 +442,58 @@ private void makeJars(String directory, int numJars) { entryName = entryName.replace('\\', '/'); } - + JarEntry clasJE = new JarEntry(entryName + ".class"); jOutStr.putNextEntry(clasJE); - + FileInputStream fIn = new FileInputStream(name + ".class"); BufferedInputStream bIn = new BufferedInputStream(fIn); - + byte[] buffer = new byte[8192]; int dataLength = 0; while ((dataLength = bIn.read(buffer)) != -1) { jOutStr.write(buffer, 0, dataLength); } - + jOutStr.flush(); jOutStr.closeEntry(); bIn.close(); } - + jOutStr.close(); - + } catch (IOException e) { System.out.println("Failed making one big jar"); e.printStackTrace(); System.exit(1); } - - + + // Make individual jar files - try + try { File jarDir = new File(directory + sl + "jars"); + if (jarDir.exists()) { + // Attempt to delete it + try { + deleteDirectory(jarDir); + } catch (IOException e) { + throw new RuntimeException("Failed to delete existing directory: " + e.getMessage()); + } + } boolean mkDirBool = jarDir.mkdirs(); - + if (!mkDirBool) { System.out.println("Failed to create destination directory for jar files"); System.exit(1); } - + int nameLen; - + if (("" + (numJars-1)).length() < ("" + numJars).length()) { nameLen = ("" + (numJars-1)).length(); @@ -484,20 +501,20 @@ private void makeJars(String directory, int numJars) { nameLen = ("" + numJars).length(); } - + String format = new String(""); while (nameLen > 0) { format = format + "0"; nameLen--; } - + DecimalFormat nameFormat = new DecimalFormat(format); - + int count = 0; fNomIt = fileNames.iterator(); int total = 0; - + while (fNomIt.hasNext()) { count = 0; @@ -505,7 +522,7 @@ private void makeJars(String directory, int numJars) while ((count < 100) && fNomIt.hasNext()) { String name = fNomIt.next(); - + if (name.contains("Init")) { continue; @@ -515,7 +532,7 @@ private void makeJars(String directory, int numJars) log("Created " + myCount + " individual jar files"); } myCount++; - + String jarFileName = jarDir + sl + name.substring(name.lastIndexOf(sl)) + ".jar"; //log("Adding " + name + " to " + jarFileName); File jarFile = new File(jarFileName); @@ -523,36 +540,36 @@ private void makeJars(String directory, int numJars) JarOutputStream jOutStr = new JarOutputStream(fOutStr); String entryName = name.substring(name.indexOf("net")); // Start of net/openj9 or net\openj9 - + if (isWindows) { entryName = entryName.replace('\\', '/'); } - + // Insert Named Class JarEntry clasJE = new JarEntry(entryName + ".class"); jOutStr.putNextEntry(clasJE); - + File clsFile = new File(name + ".class"); clsFile.deleteOnExit(); FileInputStream fIn = new FileInputStream(clsFile); BufferedInputStream bIn = new BufferedInputStream(fIn); - + byte[] buffer = new byte[8192]; int dataLength = 0; while ((dataLength = bIn.read(buffer)) != -1) { jOutStr.write(buffer, 0, dataLength); } - + jOutStr.flush(); jOutStr.closeEntry(); fIn.close(); - + // Insert Init class JarEntry clasJInit = new JarEntry(entryName.substring(0, entryName.lastIndexOf('/') + 1) + "Test_Init_" + nameFormat.format(total) + ".class"); jOutStr.putNextEntry(clasJInit); - + File initClass = new File(name.substring(0, name.lastIndexOf(sl) + 1) + "Test_Init_" + nameFormat.format(total) + ".class"); initClass.deleteOnExit(); FileInputStream fInInit = new FileInputStream(initClass); @@ -562,21 +579,21 @@ private void makeJars(String directory, int numJars) { jOutStr.write(buffer, 0, dataLength); } - + jOutStr.flush(); jOutStr.closeEntry(); fInInit.close(); bInInit.close(); - + jOutStr.close(); fOutStr.close(); bIn.close(); - + total++; - + } } - + } catch (IOException e) { System.out.println("Failed making individual jar files"); @@ -584,7 +601,21 @@ private void makeJars(String directory, int numJars) System.exit(1); } } - + + public static void deleteDirectory(File directory) throws IOException { + if (directory.isDirectory()) { + File[] entries = directory.listFiles(); + if (entries != null) { + for (File entry : entries) { + deleteDirectory(entry); + } + } + } + if (!directory.delete()) { + throw new IOException("Failed to delete " + directory); + } + } + private void writeStringFunctions(BufferedWriter bWrite) throws IOException { bWrite.write("if(Math.random() > 0.5){stringCopyAndChangeTest();}"); @@ -646,7 +677,7 @@ private void writeStringFunctions(BufferedWriter bWrite) throws IOException bWrite.newLine(); bWrite.write("private char selectLowercaseCharacter(){return (char)(Math.random() * 26 + 97);}"); bWrite.newLine(); - bWrite.write("private char selectDigit(){return (char)(Math.random() * 10 + 48);}"); + bWrite.write("private char selectDigit(){return (char)(Math.random() * 10 + 48);}"); bWrite.newLine(); bWrite.write("private String makeString(int length){"); bWrite.newLine(); @@ -666,10 +697,10 @@ private void writeStringFunctions(BufferedWriter bWrite) throws IOException bWrite.newLine(); bWrite.write("}"); } - + private void writeFunc(int num, BufferedWriter bWriter) { - try + try { bWriter.write("public int getMe" + num + "(){"); bWriter.newLine(); @@ -684,7 +715,7 @@ private void writeFunc(int num, BufferedWriter bWriter) System.exit(1); } } - + static boolean isWindows() { return ((System.getProperty("os.name").toLowerCase().indexOf("win")) > -1); }