1 /* 2 * Copyright (c) 2024, 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 // Experimental feature to allow special instrumentation code to be executed during training run: 26 // 27 // Example: app.jar -- Contains the classes for the real application. 28 // Entry point = the "App" class 29 // test.jar -- Instrumentation code to exercise the app or the framework. 30 // Entry point = the "TestDriver" class 31 // 32 // Training run: 33 // 34 // rm app.cds 35 // java -XX:CacheDataStore=app.cds -cp app.jar:test.jar -XX:CacheOnlyClassesIn=app.jar TestDriver 36 // 37 // All classes in test.jar will be excluded from app.cds 38 // 39 // Production run: 40 // 41 // java -XX:CacheDataStore=app.cds -cp app.jar:test.jar App 42 // 43 // TODO: 44 // Allow "-cp app.jar" to be used in production run, so test classes don't leaked into 45 // the production run. 46 47 /* 48 * @test id=static 49 * @summary test the -XX:CacheOnlyClassesIn flag with the classic static archive workflow 50 * @requires vm.cds 51 * @library /test/lib 52 * @run driver CacheOnlyClassesIn STATIC 53 */ 54 55 /* 56 * @test id=dynamic 57 * @summary test the -XX:CacheOnlyClassesIn flag with the classic dynamic archive workflow 58 * @requires vm.cds 59 * @library /test/lib 60 * @run driver CacheOnlyClassesIn DYNAMIC 61 */ 62 63 /* 64 * @test id=leyden 65 * @summary test the -XX:CacheOnlyClassesIn flag with Leyden workflow 66 * @requires vm.cds 67 * @requires vm.cds.write.archived.java.heap 68 * @library /test/lib 69 * @run driver CacheOnlyClassesIn LEYDEN 70 */ 71 72 import java.io.File; 73 import jdk.test.lib.cds.CDSAppTester; 74 import jdk.test.lib.helpers.ClassFileInstaller; 75 import jdk.test.lib.process.OutputAnalyzer; 76 77 public class CacheOnlyClassesIn { 78 static String mainClass = CacheOnlyClassesInApp.class.getName(); 79 static String appJar, testJar; 80 81 public static void main(String args[]) throws Exception { 82 appJar = ClassFileInstaller.writeJar("app.jar", 83 "CacheOnlyClassesInApp"); 84 testJar = ClassFileInstaller.writeJar("test.jar", 85 "TestHarness"); 86 MyTester tester = new MyTester(); 87 tester.run(args); 88 } 89 90 static class MyTester extends CDSAppTester { 91 public MyTester() { 92 super("CacheOnlyClassesInApp"); 93 } 94 95 @Override 96 public String classpath(RunMode runMode) { 97 return appJar + File.pathSeparator + testJar; 98 } 99 100 @Override 101 public String[] vmArgs(RunMode runMode) { 102 return new String[] { 103 "-XX:CacheOnlyClassesIn=" + appJar, 104 }; 105 } 106 107 @Override 108 public String[] appCommandLine(RunMode runMode) { 109 return new String[] { 110 mainClass, 111 }; 112 } 113 114 @Override 115 public void checkExecution(OutputAnalyzer out, RunMode runMode) throws Exception { 116 if (runMode == RunMode.DUMP_STATIC || 117 runMode == RunMode.DUMP_DYNAMIC || 118 runMode == RunMode.TRAINING || 119 runMode == RunMode.TRAINING0) { 120 out.shouldContain("Skipping TestHarness: excluded via -XX:CacheOnlyClassesIn"); 121 } 122 } 123 } 124 } 125 126 class CacheOnlyClassesInApp { 127 public static void main(String args[]) { 128 System.out.println("Hello World = " + TestHarness.doit()); 129 } 130 131 static volatile int x; 132 public static int testLoop() { 133 for (int i = 0; i < 100000000; i++) { 134 x ^= (x + 39); 135 } 136 return x; 137 } 138 } 139 140 class TestHarness { 141 static int doit() { 142 return CacheOnlyClassesInApp.testLoop() + myTestLoop(); 143 } 144 145 static volatile int x; 146 public static int myTestLoop() { 147 for (int i = 0; i < 100000000; i++) { 148 x ^= (x + 39); 149 } 150 return x; 151 } 152 }