1 /*
  2  * Copyright (c) 2025, 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 /**
 26  * @test
 27  * @summary Sanity test of combinations of the diagnostic flags [+-]AOTRecordTraining and [+-]AOTReplayTraining
 28  * @requires vm.cds
 29  * @requires vm.cds.supports.aot.class.linking
 30  * @requires vm.flagless
 31  * @library /test/lib /test/setup_aot /test/hotspot/jtreg/runtime/cds/appcds/test-classes
 32  * @build AOTProfileFlags JavacBenchApp Hello
 33  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar
 34  *                 JavacBenchApp
 35  *                 JavacBenchApp$ClassFile
 36  *                 JavacBenchApp$FileManager
 37  *                 JavacBenchApp$SourceFile
 38  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar hello.jar Hello
 39  * @run driver/timeout=480 AOTProfileFlags
 40  */
 41 
 42 import jdk.test.lib.cds.CDSTestUtils;
 43 import jdk.test.lib.cds.SimpleCDSAppTester;
 44 import jdk.test.lib.helpers.ClassFileInstaller;
 45 import jdk.test.lib.Platform;
 46 import jdk.test.lib.process.OutputAnalyzer;
 47 import jdk.test.lib.process.ProcessTools;
 48 
 49 public class AOTProfileFlags {
 50     public static void testDiagnosticFlags() throws Exception {
 51         printTestCase("Diagnostic Flags");
 52         for (int i = 0; i < 2; i++) {
 53             for (int j = 0; j < 2; j ++) {
 54                 SimpleCDSAppTester.of("AOTProfileFlags" + i + "" + j)
 55                     .addVmArgs("-XX:+UnlockDiagnosticVMOptions",
 56                                "-XX:" + (i == 0 ? "-" : "+") + "AOTRecordTraining",
 57                                "-XX:" + (j == 0 ? "-" : "+") + "AOTReplayTraining")
 58                     .classpath("app.jar")
 59                     .appCommandLine("JavacBenchApp", "10")
 60                     .runAOTWorkflow();
 61             }
 62         }
 63     }
 64     static void trainAndRun(String testName, String trainingFlags, String productionFlags, String errorPattern) throws Exception {
 65         printTestCase("Flags mismatch " + testName);
 66 
 67         String appJar = ClassFileInstaller.getJarPath("hello.jar");
 68         String aotConfigFile = "hello.aotconfig";
 69         String aotCacheFile = "hello.aot";
 70         String helloClass = "Hello";
 71 
 72         ProcessBuilder pb;
 73         OutputAnalyzer out;
 74 
 75         // first make sure we have a valid aotConfigFile with default value of TypeProfileLevel
 76         pb = ProcessTools.createLimitedTestJavaProcessBuilder(
 77             "-Xlog:aot",
 78             "-XX:AOTMode=record",
 79             "-XX:AOTConfiguration=" + aotConfigFile,
 80             "-XX:+UnlockExperimentalVMOptions",
 81             trainingFlags,
 82             "-cp", appJar, helloClass);
 83 
 84         out = CDSTestUtils.executeAndLog(pb, "train");
 85         out.shouldHaveExitValue(0);
 86 
 87         pb = ProcessTools.createLimitedTestJavaProcessBuilder(
 88             "-Xlog:aot",
 89             "-XX:AOTMode=create",
 90             "-XX:AOTConfiguration=" + aotConfigFile,
 91             "-XX:AOTCache=" + aotCacheFile,
 92             "-XX:+UnlockExperimentalVMOptions",
 93             trainingFlags,
 94             "-cp", appJar);
 95 
 96         out = CDSTestUtils.executeAndLog(pb, "assemble");
 97         out.shouldHaveExitValue(0);
 98 
 99         pb = ProcessTools.createLimitedTestJavaProcessBuilder(
100             "-Xlog:aot",
101             "-XX:AOTCache=" + aotCacheFile,
102             "-XX:+UnlockExperimentalVMOptions",
103             trainingFlags,
104             "-cp", appJar, helloClass);
105 
106         out = CDSTestUtils.executeAndLog(pb, "production_success");
107         out.shouldNotMatch(errorPattern);
108         out.shouldHaveExitValue(0);
109 
110         pb = ProcessTools.createLimitedTestJavaProcessBuilder(
111             "-Xlog:aot",
112             "-XX:AOTCache=" + aotCacheFile,
113             "-XX:+UnlockExperimentalVMOptions",
114             productionFlags,
115             "-cp", appJar, helloClass);
116 
117         out = CDSTestUtils.executeAndLog(pb, "production_failure");
118         out.shouldMatch(errorPattern);
119         out.shouldHaveExitValue(0);
120     }
121 
122     public static void testFlagsMismatch() throws Exception {
123         String errorPattern = ".*Profile.* setting .* does not equal the current .*Profile.* setting.*";
124         trainAndRun("TypeProfileLevel", "-XX:TypeProfileLevel=222", "-XX:TypeProfileLevel=111", errorPattern);
125         trainAndRun("TypeProfileArgsLimit", "-XX:TypeProfileArgsLimit=2", "-XX:TypeProfileArgsLimit=3", errorPattern);
126         trainAndRun("TypeProfileParamsLimit", "-XX:TypeProfileParmsLimit=2", "-XX:TypeProfileParmsLimit=3", errorPattern);
127         trainAndRun("TypeProfileWidth", "-XX:TypeProfileWidth=2", "-XX:TypeProfileWidth=3", errorPattern);
128         if (Platform.isDebugBuild()) {
129           trainAndRun("ProfileTraps", "-XX:+ProfileTraps", "-XX:-ProfileTraps", errorPattern);
130           trainAndRun("TypeProfileCasts", "-XX:+TypeProfileCasts", "-XX:-TypeProfileCasts", errorPattern);
131         }
132         errorPattern = "SpecTrapLimitExtraEntries setting .* does not equal the current SpecTrapLimitExtraEntries setting";
133         trainAndRun("SpecTrapLimitExtraEntries", "-XX:SpecTrapLimitExtraEntries=2", "-XX:SpecTrapLimitExtraEntries=3", errorPattern);
134     }
135 
136     static int testNum = 0;
137     static void printTestCase(String s) {
138         System.out.println("vvvvvvv TEST CASE " + testNum + ": " + s + " starts here vvvvvvv");
139         testNum++;
140     }
141     public static void main(String... args) throws Exception {
142         testDiagnosticFlags();
143         testFlagsMismatch();
144     }
145 }