1 /*
  2  * Copyright (c) 2023, 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 
 25 import java.io.FileWriter;
 26 import java.nio.file.Path;
 27 import java.nio.file.Paths;
 28 import java.util.regex.Matcher;
 29 import java.util.regex.Pattern;
 30 
 31 import jdk.test.lib.cds.CDSOptions;
 32 import jdk.test.lib.cds.CDSTestUtils;
 33 import jdk.test.lib.helpers.ClassFileInstaller;
 34 import jdk.test.lib.process.OutputAnalyzer;
 35 
 36 
 37 public class IndyTestBase {
 38     static Pattern testOnlyPattern = null; // matches testNumber
 39     static Pattern testSkipPattern = null; // matches testNumber    
 40     static Pattern forceSkipPattern = null; // Force some tests to be disabled during development.
 41     static int testNumber = 0;
 42     static String appJar;
 43     static OutputAnalyzer output = null; // after test() returns, this is the output of the final run
 44     static OutputAnalyzer staticDumpOutput = null;
 45     static String jtregTestCase = "testcase";
 46 
 47     // bits for runMode
 48     static final int RUN_STATIC = 1;   // Run with static archive only (no AOT)
 49     static final int RUN_AOT    = 2;   // Run with AOT (which uses dynamic archive)
 50     static final int RUN_BENCH  = 4;   // Run in benchmark mode (no logs)
 51 
 52     static void setup(String appJar) throws Exception {
 53       setup(null, appJar);
 54     }
 55 
 56     static void setup(String forceSkip, String appJar) throws Exception {
 57         String testName = System.getProperty("test.name");
 58         if (testName != null) {
 59             Path p = Paths.get(testName);
 60             String file = p.getFileName().toString();
 61             int i = file.indexOf(".");
 62             jtregTestCase = file.substring(0, i);
 63         }
 64         IndyTestBase.appJar = appJar;
 65         String testOnly = System.getProperty(jtregTestCase + ".test.only");
 66         if (testOnly != null) {
 67             testOnlyPattern = Pattern.compile("^(" + testOnly + ")$");
 68         }
 69         String testSkip = System.getProperty(jtregTestCase + ".test.skip");
 70         if (testSkip != null) {
 71             testSkipPattern = Pattern.compile("^(" + testSkip + ")$");
 72         }
 73 
 74         if (forceSkip != null) {
 75             forceSkipPattern = Pattern.compile("^(" + forceSkip + ")$");
 76         }
 77     }
 78 
 79     static boolean shouldTest(String s) {
 80         if (testOnlyPattern != null) {
 81             Matcher matcher = testOnlyPattern.matcher(s);
 82             if (!matcher.find()) {
 83                 return false;
 84             }
 85         }
 86         if (testSkipPattern != null) {
 87             Matcher matcher = testSkipPattern.matcher(s);
 88             if (matcher.find()) {
 89                 return false;
 90             }
 91         }
 92 
 93         return true;
 94     }
 95 
 96     // Run the test program with the same arg for both training run and production run
 97     static void test(String testNote, String mainClass, String arg) throws Exception {
 98         test(testNote, mainClass, arg, arg, RUN_STATIC);
 99     }
100 
101     static void test(String testNote, String mainClass, String trainingArg, String productionArg) throws Exception {
102         test(testNote, mainClass, trainingArg, productionArg, RUN_STATIC);
103     }
104 
105     static void test(String testNote, String mainClass, String trainingArg, String productionArg, int runMode) throws Exception {
106         output = null;
107         testNumber ++;
108         String skipBy = null;
109 
110         if (forceSkipPattern != null) {
111             Matcher matcher = forceSkipPattern.matcher(testNote);
112             if (matcher.find()) {
113                 skipBy = " ***** (hard coded) Skipped by test note";
114             }
115         }
116         if (skipBy == null && !shouldTest("" + testNumber)) {
117             skipBy = " ***** Skipped by test number";
118         }
119 
120         if (skipBy != null) {
121             System.out.println("         Test : #" + testNumber + ", " + testNote + skipBy);
122             return;
123         }
124 
125         System.out.println("==================================================================");
126         System.out.println("         Test : #" + testNumber + ", " + testNote);
127         System.out.println("  trainingArg : " + trainingArg);
128         System.out.println("productionArg : " + productionArg);
129         System.out.println("      runMode : " + runModeString(runMode));
130         System.out.println("vvvv==========================================================vvvv");
131         String s = jtregTestCase + "-" + testNumber;
132         String classList = s + ".classlist";
133         String archiveName = s + ".jsa";
134 
135         // Create classlist
136         System.out.println("#" + testNumber + ": Create classlist");
137         CDSTestUtils.dumpClassList(classList, "-cp", appJar, mainClass, trainingArg);
138 
139         // Dump static archive
140         System.out.println("#" + testNumber + ": Dump static archive");
141         CDSOptions opts = (new CDSOptions())
142             .addPrefix("-XX:SharedClassListFile=" + classList,
143                        "-XX:+ArchiveInvokeDynamic",
144                        "-XX:+ArchiveDynamicProxies",
145                        "-XX:+ArchiveReflectionData",
146                        "-cp", appJar,
147                        "-Xlog:cds+heap",
148                        "-Xlog:cds+resolve=trace",
149                        "-Xlog:cds,cds+class=debug")
150             .setArchiveName(archiveName);
151         staticDumpOutput = CDSTestUtils.createArchiveAndCheck(opts);
152         TestCommon.checkExecReturn(staticDumpOutput, 0, true);
153 
154         if ((runMode & RUN_STATIC) != 0) {
155             // Run with static archive
156             System.out.println("#" + testNumber + ": Run with static archive (no AOT)");
157             CDSOptions runOpts = (new CDSOptions())
158                 .addPrefix("-cp", appJar)
159                 .setArchiveName(archiveName)
160                 .setUseVersion(false)
161                 .addSuffix(mainClass)
162                 .addSuffix(productionArg);
163             if ((runMode & RUN_BENCH) != 0) {
164                 runOpts.setBenchmarkMode(true);
165             } else {
166                 runOpts.addPrefix("-Xlog:class+load", "-Xlog:cds=debug", "-Xlog:cds+heap=debug",
167                                   "-Xlog:methodhandles");
168             }
169             output = CDSTestUtils.runWithArchive(runOpts);
170             TestCommon.checkExecReturn(output, 0, true);
171         }
172 
173         if ((runMode & RUN_AOT) != 0) {
174             System.out.println("#" + testNumber +
175                                ": (STEP 3 of 5) Run with static archive and dump profile in dynamic archive (With Training Data Replay)");
176             String dynamicArchiveName = s + "-dyn.jsa";
177             CDSOptions dynDumpOpts = (new CDSOptions())
178                 .addPrefix("-cp", appJar)
179                 .setArchiveName(archiveName)
180                 .setUseVersion(false)
181                 .addSuffix("-XX:ArchiveClassesAtExit=" + dynamicArchiveName)
182                 .addSuffix("-XX:+RecordTraining");
183             if ((runMode & RUN_BENCH) != 0) {
184                 dynDumpOpts.setBenchmarkMode(true);
185                 dynDumpOpts.addPrefix("-Xlog:cds=debug", "-Xlog:scc");
186             } else {
187                 dynDumpOpts.addPrefix("-Xlog:class+load", "-Xlog:cds=debug",
188                                       "-Xlog:cds+class=debug");
189             }
190             // The main class name and arguments
191             dynDumpOpts
192                 .addSuffix(mainClass)
193                 .addSuffix(productionArg);
194             output = CDSTestUtils.runWithArchive(dynDumpOpts);
195             TestCommon.checkExecReturn(output, 0, true);
196 
197             //======================================================================
198             System.out.println("#" + testNumber +
199                                ": (STEP 4 of 5) Run with dynamic archive and generate AOT code");
200             String sharedCodeArchive = s + ".scc";
201             CDSOptions aotOpts = (new CDSOptions())
202                 .addPrefix("-cp", appJar)
203                 .setArchiveName(archiveName)
204                 .setUseVersion(false)
205                 .addSuffix("-XX:+StoreCachedCode")
206                 .addSuffix("-XX:CachedCodeFile=" + sharedCodeArchive)
207                 .addSuffix("-XX:CachedCodeMaxSize=100M");
208 
209             if ((runMode & RUN_BENCH) != 0) {
210                 aotOpts.setBenchmarkMode(true);
211                 aotOpts.addPrefix("-Xlog:cds=debug", "-Xlog:scc");
212             } else {
213                 // Tell CDSTestUtils to not add the -XX:VerifyArchivedFields=1 flag, which seems to be causing a crash
214                 // in the AOT code.
215                 aotOpts.setBenchmarkMode(true);
216 
217                 aotOpts.addPrefix("-Xlog:class+load", "-Xlog:cds=debug", "-Xlog:scc");
218                 if (mainClass.equals("ConcatA")) {
219                     // Hard-code the printing of loopA for now
220                     dynDumpOpts
221                         .addSuffix("-XX:CompileCommand=print,*::loopA")
222                         .addSuffix("-XX:+PrintAssembly");
223                 }
224             }
225             // The main class name and arguments
226             aotOpts
227                 .addSuffix(mainClass)
228                 .addSuffix(productionArg);
229             output = CDSTestUtils.runWithArchive(aotOpts);
230             TestCommon.checkExecReturn(output, 0, true);
231 
232             //======================================================================
233             System.out.println("#" + testNumber + ": (STEP 5 of 5) Run with dynamic archive and AOT cache");
234             CDSOptions runOpts = (new CDSOptions())
235                 .addPrefix("-cp", appJar)
236                 .setArchiveName(dynamicArchiveName)
237                 .addSuffix("-XX:+LoadCachedCode")
238                 .addSuffix("-XX:CachedCodeFile=" + sharedCodeArchive)
239                 .setUseVersion(false)
240                 .addSuffix(mainClass)
241                 .addSuffix(productionArg);
242 
243             if ((runMode & RUN_BENCH) != 0) {
244                 runOpts.setBenchmarkMode(true);
245             } else {
246                 // Tell CDSTestUtils to not add the -XX:VerifyArchivedFields=1 flag, which seems to be causing a crash
247                 // in the AOT code.
248                 runOpts.setBenchmarkMode(true);
249 
250                 runOpts.addPrefix("-Xlog:class+load", "-Xlog:cds=debug");
251                 if (mainClass.equals("ConcatA")) {
252                     // Hard-code the printing of loopA for now
253                     dynDumpOpts
254                         .addSuffix("-XX:CompileCommand=print,*::loopA")
255                         .addSuffix("-XX:+PrintAssembly");
256                 }
257             }
258 
259             output = CDSTestUtils.runWithArchive(runOpts);
260             TestCommon.checkExecReturn(output, 0, true);
261         }
262     }
263 
264     static void checkExec(String expectedOutput) throws Exception {
265         checkExec(expectedOutput, /* lambdaFormsMustBeArchived*/ true);
266     }
267 
268     static void checkExec(String expectedOutput, boolean lambdaFormsMustBeArchived)  throws Exception {
269         if (output == null) { // test may be skipped
270             return;
271         }
272         if (expectedOutput != null) {
273             TestCommon.checkExecReturn(output, 0, true, "OUTPUT = " + expectedOutput);
274         }
275         if (lambdaFormsMustBeArchived) {
276             output.shouldMatch("LambdaForm[$]((MH)|(DMH))/0x[0-9]+ source: shared objects file");
277             output.shouldNotMatch("LambdaForm[$]MH/0x[0-9]+ source: __JVM_LookupDefineClass__");
278             output.shouldNotMatch("LambdaForm[$]DMH/0x[0-9]+ source: __JVM_LookupDefineClass__");
279         }
280     }
281 
282     static void shouldMatch(String pattern) throws Exception {
283         if (output != null) { // test may be skipped
284             output.shouldMatch(pattern);
285         }
286     }
287 
288     static String runModeString(int runMode) {
289         String prefix = "";
290         String s = "";
291         if ((runMode & RUN_STATIC) != 0) {
292             s += "RUN_STATIC";
293             prefix = " | ";
294         }
295         if ((runMode & RUN_AOT) != 0) {
296             s += prefix;
297             s += "RUN_AOT";
298             prefix = " | ";
299         }
300         return s;
301     }
302 }