--- a/test/hotspot/jtreg/runtime/appcds/TestCommon.java Fri May 17 10:48:02 2019 -0400
+++ b/test/hotspot/jtreg/runtime/appcds/TestCommon.java Fri May 17 08:29:55 2019 -0700
@@ -32,10 +32,28 @@
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.net.URI;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
+import java.util.Enumeration;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipOutputStream;
+import jtreg.SkippedException;
+import cdsutils.DynamicDumpHelper;
+
/**
* This is a test utility class for common AppCDS test functionality.
@@ -51,7 +69,7 @@
*/
public class TestCommon extends CDSTestUtils {
private static final String JSA_FILE_PREFIX = System.getProperty("user.dir") +
- File.separator + "appcds-";
+ File.separator;
private static final SimpleDateFormat timeStampFormat =
new SimpleDateFormat("HH'h'mm'm'ss's'SSS");
@@ -64,8 +82,7 @@
// Call this method to start new archive with new unique name
public static void startNewArchiveName() {
deletePriorArchives();
- currentArchiveName = JSA_FILE_PREFIX +
- timeStampFormat.format(new Date()) + ".jsa";
+ currentArchiveName = getNewArchiveName();
}
// Call this method to get current archive name
@@ -73,6 +90,18 @@
return currentArchiveName;
}
+ public static String getNewArchiveName() {
+ return getNewArchiveName(null);
+ }
+
+ public static String getNewArchiveName(String stem) {
+ if (stem == null) {
+ stem = "appcds";
+ }
+ return JSA_FILE_PREFIX + stem + "-" +
+ timeStampFormat.format(new Date()) + ".jsa";
+ }
+
// Attempt to clean old archives to preserve space
// Archives are large artifacts (20Mb or more), and much larger than
// most other artifacts created in jtreg testing.
@@ -92,7 +121,6 @@
}
}
-
// Create AppCDS archive using most common args - convenience method
// Legacy name preserved for compatibility
public static OutputAnalyzer dump(String appJar, String classList[],
@@ -110,10 +138,12 @@
return createArchive(opts);
}
+ // Simulate -Xshare:dump with -XX:ArchiveClassesAtExit. See comments around patchJarForDynamicDump()
+ private static final Class tmp = DynamicDumpHelper.class;
+
// Create AppCDS archive using appcds options
public static OutputAnalyzer createArchive(AppCDSOptions opts)
throws Exception {
-
ArrayList<String> cmd = new ArrayList<String>();
startNewArchiveName();
@@ -122,23 +152,73 @@
if (opts.appJar != null) {
cmd.add("-cp");
cmd.add(opts.appJar);
+ File jf = new File(opts.appJar);
+ if (DYNAMIC_DUMP && !jf.isDirectory()) {
+ patchJarForDynamicDump(opts.appJar);
+ }
} else {
cmd.add("-Djava.class.path=");
}
- cmd.add("-Xshare:dump");
-
- if (opts.archiveName == null)
+ if (opts.archiveName == null) {
opts.archiveName = getCurrentArchiveName();
-
- cmd.add("-XX:SharedArchiveFile=" + opts.archiveName);
-
- if (opts.classList != null) {
- File classListFile = makeClassList(opts.classList);
- cmd.add("-XX:ExtraSharedClassListFile=" + classListFile.getPath());
}
- for (String s : opts.suffix) cmd.add(s);
+ if (DYNAMIC_DUMP) {
+ cmd.add("-Xshare:on");
+ cmd.add("-XX:ArchiveClassesAtExit=" + opts.archiveName);
+
+ cmd.add("-Xlog:cds");
+ cmd.add("-Xlog:cds+dynamic");
+ boolean mainModuleSpecified = false;
+ boolean patchModuleSpecified = false;
+ for (String s : opts.suffix) {
+ if (s.length() == 0) {
+ continue;
+ }
+ if (s.equals("-m")) {
+ mainModuleSpecified = true;
+ }
+ if (s.startsWith("--patch-module=")) {
+ patchModuleSpecified = true;
+ }
+ cmd.add(s);
+ }
+
+ if (opts.appJar != null) {
+ // classlist is supported only when we have a Jar file to patch (to insert
+ // cdsutils.DynamicDumpHelper)
+ if (opts.classList == null) {
+ throw new RuntimeException("test.dynamic.dump requires classList file");
+ }
+
+ if (!mainModuleSpecified && !patchModuleSpecified) {
+ cmd.add("cdsutils.DynamicDumpHelper");
+ File classListFile = makeClassList(opts.classList);
+ cmd.add(classListFile.getPath());
+ }
+ } else {
+ if (!mainModuleSpecified && !patchModuleSpecified) {
+ // If you have an empty classpath, you cannot specify a classlist!
+ if (opts.classList != null && opts.classList.length > 0) {
+ throw new RuntimeException("test.dynamic.dump not supported empty classpath with non-empty classlist");
+ }
+ cmd.add("-version");
+ }
+ }
+ } else {
+ // static dump
+ cmd.add("-Xshare:dump");
+ cmd.add("-XX:SharedArchiveFile=" + opts.archiveName);
+
+ if (opts.classList != null) {
+ File classListFile = makeClassList(opts.classList);
+ cmd.add("-XX:ExtraSharedClassListFile=" + classListFile.getPath());
+ }
+ for (String s : opts.suffix) {
+ cmd.add(s);
+ }
+ }
String[] cmdLine = cmd.toArray(new String[cmd.size()]);
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, cmdLine);
@@ -154,6 +234,93 @@
// Some AppCDS tests are not compatible with this mode. See the group
// hotspot_appcds_with_jfr in ../../TEST.ROOT for details.
private static final boolean RUN_WITH_JFR = Boolean.getBoolean("test.cds.run.with.jfr");
+ // This method simulates -Xshare:dump with -XX:ArchiveClassesAtExit. This way, we
+ // can re-use many tests (outside of the ./dynamicArchive directory) for testing
+ // general features of JDK-8215311 (JEP 350: Dynamic CDS Archives).
+ //
+ // We insert the cdsutils/DynamicDumpHelper.class into the first Jar file in
+ // the classpath. We use this class to load all the classes specified in the classlist.
+ //
+ // There's no need to change the run-time command-line: in this special mode, two
+ // archives are involved. The command-line specifies only the top archive. However,
+ // the location of the base archive is recorded in the top archive, so it can be
+ // determined by the JVM at runtime start-up.
+ //
+ // To run in this special mode, specify the following in your jtreg command-line
+ // -Dtest.dynamic.cds.archive=true
+ //
+ // Note that some tests are not compatible with this special mode, including
+ // + Tests in ./dynamicArchive: these tests are specifically written for
+ // dynamic archive, and do not use TestCommon.createArchive(), which works
+ // together with patchJarForDynamicDump().
+ // + Tests related to cached objects and shared strings: dynamic dumping
+ // does not support these.
+ // + Custom loader tests: DynamicDumpHelper doesn't support the required
+ // classlist syntax. (FIXME).
+ // + Extra symbols and extra strings.
+ // See the hotspot_appcds_dynamic in ../../TEST.ROOT for details.
+ //
+ // To run all tests that are compatible with this mode:
+ // cd test/hotspot/jtreg
+ // jtreg -Dtest.dynamic.cds.archive=true :hotspot_appcds_dynamic
+ //
+ private static void patchJarForDynamicDump(String cp) throws Exception {
+ System.out.println("patchJarForDynamicDump: classpath = " + cp);
+ String firstJar = cp;
+ int n = firstJar.indexOf(File.pathSeparator);
+ if (n > 0) {
+ firstJar = firstJar.substring(0, n);
+ }
+ String classDir = System.getProperty("test.classes");
+ String expected1 = classDir + File.separator;
+ String expected2 = System.getProperty("user.dir") + File.separator;
+
+ if (!firstJar.startsWith(expected1) && !firstJar.startsWith(expected2)) {
+ throw new RuntimeException("FIXME: jar file not at a supported location ('"
+ + expected1 + "', or '" + expected2 + "'): " + firstJar);
+ }
+
+ String replaceJar = firstJar + ".tmp";
+ String patchClass = "cdsutils/DynamicDumpHelper.class";
+ ZipFile zipFile = new ZipFile(firstJar);
+ byte[] buf = new byte[1024];
+ int len;
+ if (zipFile.getEntry(patchClass) == null) {
+ FileOutputStream fout = new FileOutputStream(replaceJar);
+ final ZipOutputStream zos = new ZipOutputStream(fout);
+
+ zos.putNextEntry(new ZipEntry(patchClass));
+ InputStream is = new FileInputStream(classDir + File.separator + patchClass);
+ while ((len = (is.read(buf))) > 0) {
+ zos.write(buf, 0, len);
+ }
+ zos.closeEntry();
+ is.close();
+
+ for (Enumeration e = zipFile.entries(); e.hasMoreElements(); ) {
+ ZipEntry entryIn = (ZipEntry) e.nextElement();
+ zos.putNextEntry(entryIn);
+ is = zipFile.getInputStream(entryIn);
+ while ((len = is.read(buf)) > 0) {
+ zos.write(buf, 0, len);
+ }
+ zos.closeEntry();
+ is.close();
+ }
+
+ zos.close();
+ fout.close();
+ zipFile.close();
+
+ File oldFile = new File(firstJar);
+ File newFile = new File(replaceJar);
+ oldFile.delete();
+ newFile.renameTo(oldFile);
+ System.out.println("firstJar = " + firstJar + " Modified");
+ } else {
+ System.out.println("firstJar = " + firstJar);
+ }
+ }
// Execute JVM using AppCDS archive with specified AppCDSOptions
public static OutputAnalyzer runWithArchive(AppCDSOptions opts)
@@ -260,12 +427,18 @@
return runWithArchive(opts);
}
-
// A common operation: dump, then check results
public static OutputAnalyzer testDump(String appJar, String classList[],
String... suffix) throws Exception {
OutputAnalyzer output = dump(appJar, classList, suffix);
- output.shouldContain("Loading classes to share");
+ if (DYNAMIC_DUMP) {
+ if (isUnableToMap(output)) {
+ throw new SkippedException(UnableToMapMsg);
+ }
+ output.shouldContain("Written dynamic archive");
+ } else {
+ output.shouldContain("Loading classes to share");
+ }
output.shouldHaveExitValue(0);
return output;
}
@@ -301,7 +474,6 @@
return output;
}
-
// Convenience concatenation utils
public static String[] list(String ...args) {
return args;
@@ -336,6 +508,15 @@
return list.toArray(new String[list.size()]);
}
+ public static String[] concat(String prefix, String[] extra) {
+ ArrayList<String> list = new ArrayList<String>();
+ list.add(prefix);
+ for (String s : extra) {
+ list.add(s);
+ }
+
+ return list.toArray(new String[list.size()]);
+ }
// ===================== Concatenate paths
public static String concatPaths(String... paths) {
@@ -384,4 +565,29 @@
}
return true;
}
+
+ static Pattern pattern;
+
+ static void findAllClasses(ArrayList<String> list) throws Throwable {
+ // Find all the classes in the jrt file system
+ pattern = Pattern.compile("/modules/[a-z.]*[a-z]+/([^-]*)[.]class");
+ FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
+ Path base = fs.getPath("/modules/");
+ findAllClassesAtPath(base, list);
+ }
+
+ private static void findAllClassesAtPath(Path p, ArrayList<String> list) throws Throwable {
+ try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
+ for (Path entry: stream) {
+ Matcher matcher = pattern.matcher(entry.toString());
+ if (matcher.find()) {
+ String className = matcher.group(1);
+ list.add(className);
+ }
+ try {
+ findAllClassesAtPath(entry, list);
+ } catch (Throwable t) {}
+ }
+ }
+ }
}