2828import jdk .test .lib .process .ProcessTools ;
2929import jdk .test .lib .process .OutputAnalyzer ;
3030import jdk .test .lib .StringArrayUtils ;
31+ import jdk .test .whitebox .WhiteBox ;
3132import jtreg .SkippedException ;
3233
3334/*
@@ -43,6 +44,7 @@ abstract public class CDSAppTester {
4344 private final String staticArchiveFileLog ;
4445 private final String dynamicArchiveFile ;
4546 private final String dynamicArchiveFileLog ;
47+ private final String tempBaseArchiveFile ;
4648 private int numProductionRuns = 0 ;
4749
4850 public CDSAppTester (String name ) {
@@ -58,6 +60,7 @@ public CDSAppTester(String name) {
5860 staticArchiveFileLog = staticArchiveFile + ".log" ;
5961 dynamicArchiveFile = name () + ".dynamic.jsa" ;
6062 dynamicArchiveFileLog = dynamicArchiveFile + ".log" ;
63+ tempBaseArchiveFile = name () + ".temp-base.jsa" ;
6164 }
6265
6366 private String productionRunLog () {
@@ -189,9 +192,37 @@ private OutputAnalyzer dumpStaticArchive() throws Exception {
189192 return executeAndCheck (cmdLine , runMode , staticArchiveFile , staticArchiveFileLog );
190193 }
191194
195+ // Creating a dynamic CDS archive (with -XX:ArchiveClassesAtExit=<foo>.jsa) requires that the current
196+ // JVM process is using a static archive (which is usually the default CDS archive included in the JDK).
197+ // However, if the JDK doesn't include a default CDS archive that's compatible with the set of
198+ // VM options used by this test, we need to create a temporary static archive to be used with -XX:ArchiveClassesAtExit.
199+ private String getBaseArchiveForDynamicArchive () throws Exception {
200+ WhiteBox wb = WhiteBox .getWhiteBox ();
201+ if (wb .isSharingEnabled ()) {
202+ // This current JVM is able to use a default CDS archive included by the JDK, so
203+ // if we launch a JVM child process (with the same set of options as the current JVM),
204+ // that process is also able to use the same default CDS archive for creating
205+ // a dynamic archive.
206+ return null ;
207+ } else {
208+ // This current JVM is unable to use a default CDS archive, so let's create a temporary
209+ // static archive to be used with -XX:ArchiveClassesAtExit.
210+ File f = new File (tempBaseArchiveFile );
211+ if (!f .exists ()) {
212+ CDSOptions opts = new CDSOptions ();
213+ opts .setArchiveName (tempBaseArchiveFile );
214+ opts .addSuffix ("-Djava.class.path=" );
215+ OutputAnalyzer out = CDSTestUtils .createArchive (opts );
216+ CDSTestUtils .checkBaseDump (out );
217+ }
218+ return tempBaseArchiveFile ;
219+ }
220+ }
221+
192222 private OutputAnalyzer dumpDynamicArchive () throws Exception {
193223 RunMode runMode = RunMode .DUMP_DYNAMIC ;
194224 String [] cmdLine = new String [0 ];
225+ String baseArchive = getBaseArchiveForDynamicArchive ();
195226 if (isDynamicWorkflow ()) {
196227 // "classic" dynamic archive
197228 cmdLine = StringArrayUtils .concat (vmArgs (runMode ),
@@ -204,6 +235,9 @@ private OutputAnalyzer dumpDynamicArchive() throws Exception {
204235 "cds+resolve=debug" ,
205236 "class+load=debug" ));
206237 }
238+ if (baseArchive != null ) {
239+ cmdLine = StringArrayUtils .concat (cmdLine , "-XX:SharedArchiveFile=" + baseArchive );
240+ }
207241 cmdLine = StringArrayUtils .concat (cmdLine , appCommandLine (runMode ));
208242 return executeAndCheck (cmdLine , runMode , dynamicArchiveFile , dynamicArchiveFileLog );
209243 }
@@ -227,9 +261,9 @@ public OutputAnalyzer productionRun(String[] extraVmArgs, String[] extraAppArgs)
227261 logToFile (productionRunLog (), "cds" ));
228262
229263 if (isStaticWorkflow ()) {
230- cmdLine = StringArrayUtils .concat (cmdLine , "-XX:SharedArchiveFile=" + staticArchiveFile );
264+ cmdLine = StringArrayUtils .concat (cmdLine , "-Xshare:on" , "- XX:SharedArchiveFile=" + staticArchiveFile );
231265 } else if (isDynamicWorkflow ()) {
232- cmdLine = StringArrayUtils .concat (cmdLine , "-XX:SharedArchiveFile=" + dynamicArchiveFile );
266+ cmdLine = StringArrayUtils .concat (cmdLine , "-Xshare:on" , "- XX:SharedArchiveFile=" + dynamicArchiveFile );
233267 }
234268
235269 if (extraVmArgs != null ) {
0 commit comments