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 id=aot
 27  * @requires vm.cds.supports.aot.class.linking
 28  * @comment work around JDK-8345635
 29  * @requires !vm.jvmci.enabled
 30  * @library /test/jdk/lib/testlibrary /test/lib
 31  * @modules java.management
 32  * @build EndTrainingWithAOTCacheMXBean
 33  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar MyTestApp ShouldBeCached ShouldNotBeCached
 34  * @run driver EndTrainingWithAOTCacheMXBean AOT
 35  */
 36 
 37 /*
 38  * @test id=leyden
 39  * @requires vm.cds.supports.aot.class.linking
 40  * @comment work around JDK-8345635
 41  * @requires !vm.jvmci.enabled
 42  * @library /test/jdk/lib/testlibrary /test/lib
 43  * @modules jdk.management
 44  * @build EndTrainingWithAOTCacheMXBean
 45  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar MyTestApp ShouldBeCached ShouldNotBeCached
 46  * @run driver EndTrainingWithAOTCacheMXBean LEYDEN
 47  */
 48 
 49 import jdk.test.lib.cds.CDSAppTester;
 50 import jdk.test.lib.helpers.ClassFileInstaller;
 51 import jdk.test.lib.process.OutputAnalyzer;
 52 import java.lang.management.ManagementFactory;
 53 import jdk.management.AOTCacheMXBean;
 54 
 55 public class EndTrainingWithAOTCacheMXBean {
 56     static final String appJar = ClassFileInstaller.getJarPath("app.jar");
 57     static final String mainClass = "MyTestApp";
 58 
 59     public static void main(String[] args) throws Exception {
 60         // We want to test the entry count implementation in both interpreter and compiler.
 61         new Tester().run(args);
 62     }
 63 
 64     static class Tester extends CDSAppTester {
 65 
 66         public Tester() {
 67             super(mainClass);
 68         }
 69 
 70         @Override
 71         public String classpath(RunMode runMode) {
 72             return appJar;
 73         }
 74 
 75         public String[] vmArgs(RunMode runMode) {
 76             return new String[] {
 77                 "-Xlog:cds+class=debug",
 78                 "-Xlog:aot+class=debug",
 79                 "--add-modules=jdk.management"
 80             };
 81         }
 82 
 83         @Override
 84         public String[] appCommandLine(RunMode runMode) {
 85             return new String[] {
 86                 mainClass, runMode.name(),
 87             };
 88         }
 89 
 90         @Override
 91         public void checkExecution(OutputAnalyzer out, RunMode runMode) {
 92             var name = runMode.name();
 93             if (runMode.isApplicationExecuted()) {
 94                 out.shouldContain("Hello Leyden " + name);
 95                 out.shouldContain("ShouldBeCached.dummy()");
 96                 out.shouldContain("ShouldNotBeCached.dummy()");
 97                 if(runMode == RunMode.TRAINING || runMode == RunMode.TRAINING0) {
 98                     if (isLeydenWorkflow()) {
 99                         out.shouldContain("AOTMode = auto");
100                     } else {
101                         out.shouldContain("AOTMode = record");
102                     }
103                     out.shouldContain("Confirmed is recording");
104                     out.shouldContain("Confirmed recording duration > 0");
105                     out.shouldContain("Stopped recording successfully after an additional 10ms");
106                     out.shouldContain("Last recording duration > than previous duration");
107                     out.shouldContain("Confirmed recording stopped");
108                     out.shouldContain("Confirmed recording duration has not changed after 10ms");
109                 } else if (runMode == RunMode.TRAINING1 || runMode == RunMode.ASSEMBLY) {
110                     out.shouldNotContain("Hello Leyden ");
111                 } else if (runMode == RunMode.PRODUCTION) {
112                     if (isLeydenWorkflow()) {
113                         out.shouldContain("AOTMode = auto");
114                     } else {
115                         out.shouldContain("AOTMode = on");
116                     }
117                     out.shouldContain("Confirmed is not recording");
118                     out.shouldContain("Confirmed recording duration == 0");
119                 }
120                 out.shouldNotContain("Thread interrupted");
121                 out.shouldNotContain("Failed to stop recording");
122             }
123             if (isDumping(runMode)) {
124                 if (isAOTWorkflow()) {
125                     out.shouldMatch("aot,class.* ShouldBeCached");
126                     out.shouldNotMatch("aot,class.* ShouldNotBeCached");
127                 } else {
128                     out.shouldMatch("cds,class.* ShouldBeCached");
129                     out.shouldNotMatch("cds,class.* ShouldNotBeCached");
130                 }
131             }
132         }
133     }
134 }
135 
136 class MyTestApp {
137     public static void main(String args[]) throws Exception {
138         System.out.println("Hello Leyden " + args[0]);
139         var aotBean = ManagementFactory.getPlatformMXBean(AOTCacheMXBean.class);
140         if (aotBean == null) {
141             System.out.println("AOTCacheMXBean is not available");
142             return;
143         }
144         ShouldBeCached.dummy();
145         System.out.println("AOTMode = " + aotBean.getMode());
146         if (aotBean.isRecording()) {
147             try {
148                 System.out.println("Confirmed is recording");
149                 var initialDuration = aotBean.getRecordingDuration();
150                 System.out.println("Confirmed recording duration > 0");
151                 Thread.sleep(10);
152                 if (aotBean.endRecording()) {
153                     System.out.println("Stopped recording successfully after an additional 10ms");
154                     if (!aotBean.isRecording()) {
155                         System.out.println("Confirmed recording stopped");
156                     }
157                     var recordingDuration = aotBean.getRecordingDuration();
158                     if (recordingDuration > initialDuration) {
159                         System.out.println("Last recording duration > than previous duration");
160                     }
161                     Thread.sleep(10);
162                     var lastDuration = aotBean.getRecordingDuration();
163                     if (lastDuration == recordingDuration) {
164                         System.out.println("Confirmed recording duration has not changed after 10ms");
165                     }
166                 } else {
167                     System.out.println("Failed to stop recording");
168                 }
169             } catch (InterruptedException e) {
170                 System.out.println("Thread interrupted");
171             }
172         } else {
173             System.out.println("Confirmed is not recording");
174             var recordingDuration = aotBean.getRecordingDuration();
175             if (recordingDuration == 0) {
176                 System.out.println("Confirmed recording duration == 0");
177             }
178         }
179         ShouldNotBeCached.dummy();
180     }
181 }
182 
183 class ShouldBeCached {
184     static void dummy() {
185         System.out.println("ShouldBeCached.dummy()");
186     }
187 }
188 
189 class ShouldNotBeCached {
190     static void dummy() {
191         System.out.println("ShouldNotBeCached.dummy()");
192     }
193 }