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                 "--add-modules=jdk.management"
 79             };
 80         }
 81 
 82         @Override
 83         public String[] appCommandLine(RunMode runMode) {
 84             return new String[] {
 85                 mainClass, runMode.name(),
 86             };
 87         }
 88 
 89         @Override
 90         public void checkExecution(OutputAnalyzer out, RunMode runMode) {
 91             var name = runMode.name();
 92             if (runMode.isApplicationExecuted()) {
 93                 out.shouldContain("Hello Leyden " + name);
 94                 out.shouldContain("ShouldBeCached.dummy()");
 95                 out.shouldContain("ShouldNotBeCached.dummy()");
 96                 if(runMode == RunMode.TRAINING || runMode == RunMode.TRAINING0) {
 97                     if (isLeydenWorkflow()) {
 98                         out.shouldContain("AOTMode = auto");
 99                     } else {
100                         out.shouldContain("AOTMode = record");
101                     }
102                     out.shouldContain("Confirmed is recording");
103                     out.shouldContain("Confirmed recording duration > 0");
104                     out.shouldContain("Stopped recording successfully after an additional 10ms");
105                     out.shouldContain("Last recording duration > than previous duration");
106                     out.shouldContain("Confirmed recording stopped");
107                     out.shouldContain("Confirmed recording duration has not changed after 10ms");
108                 } else if (runMode == RunMode.TRAINING1 || runMode == RunMode.ASSEMBLY) {
109                     out.shouldNotContain("Hello Leyden ");
110                 } else if (runMode == RunMode.PRODUCTION) {
111                     if (isLeydenWorkflow()) {
112                         out.shouldContain("AOTMode = auto");
113                     } else {
114                         out.shouldContain("AOTMode = on");
115                     }
116                     out.shouldContain("Confirmed is not recording");
117                     out.shouldContain("Confirmed recording duration == 0");
118                 }
119                 out.shouldNotContain("Thread interrupted");
120                 out.shouldNotContain("Failed to stop recording");
121             }
122             if (isDumping(runMode)) {
123                 out.shouldMatch("cds,class.* ShouldBeCached");
124                 out.shouldNotMatch("cds,class.* ShouldNotBeCached");
125             }
126         }
127     }
128 }
129 
130 class MyTestApp {
131     public static void main(String args[]) throws Exception {
132         System.out.println("Hello Leyden " + args[0]);
133         var aotBean = ManagementFactory.getPlatformMXBean(AOTCacheMXBean.class);
134         if (aotBean == null) {
135             System.out.println("AOTCacheMXBean is not available");
136             return;
137         }
138         ShouldBeCached.dummy();
139         System.out.println("AOTMode = " + aotBean.getMode());
140         if (aotBean.isRecording()) {
141             try {
142                 System.out.println("Confirmed is recording");
143                 var initialDuration = aotBean.getRecordingDuration();
144                 System.out.println("Confirmed recording duration > 0");
145                 Thread.sleep(10);
146                 if (aotBean.endRecording()) {
147                     System.out.println("Stopped recording successfully after an additional 10ms");
148                     if (!aotBean.isRecording()) {
149                         System.out.println("Confirmed recording stopped");
150                     }
151                     var recordingDuration = aotBean.getRecordingDuration();
152                     if (recordingDuration > initialDuration) {
153                         System.out.println("Last recording duration > than previous duration");
154                     }
155                     Thread.sleep(10);
156                     var lastDuration = aotBean.getRecordingDuration();
157                     if (lastDuration == recordingDuration) {
158                         System.out.println("Confirmed recording duration has not changed after 10ms");
159                     }
160                 } else {
161                     System.out.println("Failed to stop recording");
162                 }
163             } catch (InterruptedException e) {
164                 System.out.println("Thread interrupted");
165             }
166         } else {
167             System.out.println("Confirmed is not recording");
168             var recordingDuration = aotBean.getRecordingDuration();
169             if (recordingDuration == 0) {
170                 System.out.println("Confirmed recording duration == 0");
171             }
172         }
173         ShouldNotBeCached.dummy();
174     }
175 }
176 
177 class ShouldBeCached {
178     static void dummy() {
179         System.out.println("ShouldBeCached.dummy()");
180     }
181 }
182 
183 class ShouldNotBeCached {
184     static void dummy() {
185         System.out.println("ShouldNotBeCached.dummy()");
186     }
187 }