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 * @test LevelTransitionTest
26 * @library /testlibrary /test/lib /
27 * @modules java.base/jdk.internal.misc
28 * java.management
29 * @ignore 8067651
30 * @build TransitionsTestExecutor LevelTransitionTest
31 * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
32 * @run main/othervm/timeout=240 -Xmixed -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
33 * -XX:+WhiteBoxAPI -XX:+TieredCompilation
34 * -XX:CompileCommand=compileonly,compiler.whitebox.SimpleTestCaseHelper::*
35 * -XX:CompileCommand=compileonly,ExtendedTestCase$CompileMethodHolder::*
36 * TransitionsTestExecutor LevelTransitionTest
37 * @summary Test the correctness of compilation level transitions for different methods
38 */
39
40 import java.lang.reflect.Executable;
41 import java.lang.reflect.Method;
42 import java.util.Objects;
43 import java.util.concurrent.Callable;
44 import compiler.whitebox.CompilerWhiteBoxTest;
45 import compiler.whitebox.SimpleTestCase;
46
47 public class LevelTransitionTest extends TieredLevelsTest {
48 /** Shows if method was profiled by being executed on levels 2 or 3 */
49 protected boolean isMethodProfiled;
50 private int transitionCount;
51
52 public static void main(String[] args) throws Throwable {
53 assert (!CompilerWhiteBoxTest.skipOnTieredCompilation(false));
54
55 CompilerWhiteBoxTest.main(LevelTransitionTest::new, args);
56 // run extended test cases
57 for (TestCase testCase : ExtendedTestCase.values()) {
58 new LevelTransitionTest(testCase).runTest();
59 }
60 }
61
62 protected LevelTransitionTest(TestCase testCase) {
63 super(testCase);
64 isMethodProfiled = testCase.isOsr(); // OSR methods were already profiled by warmup
65 transitionCount = 0;
66 }
67
68 @Override
86 boolean finish = false;
87 while (!finish) {
88 System.out.printf("Level transition #%d%n", ++transitionCount);
89 int newLevel;
90 int current = getCompLevel();
91 int expected = getNextLevel(current);
92 if (current == expected) {
93 // if we are on expected level, just execute it more
94 // to ensure that the level won't change
95 System.out.printf("Method %s is already on expected level %d%n", method, expected);
96 compile();
97 newLevel = getCompLevel();
98 finish = true;
99 } else {
100 newLevel = changeCompLevel();
101 finish = false;
102 }
103 System.out.printf("Method %s is compiled on level %d. Expected level is %d%n", method, newLevel, expected);
104 checkLevel(expected, newLevel);
105 printInfo();
106 };
107 }
108
109 /**
110 * Gets next expected level for the test case on each transition.
111 *
112 * @param currentLevel a level the test case is compiled on
113 * @return expected compilation level
114 */
115 protected int getNextLevel(int currentLevel) {
116 int nextLevel = currentLevel;
117 switch (currentLevel) {
118 case CompilerWhiteBoxTest.COMP_LEVEL_NONE:
119 nextLevel = isMethodProfiled ? CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION
120 : CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE;
121 break;
122 case CompilerWhiteBoxTest.COMP_LEVEL_LIMITED_PROFILE:
123 case CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE:
124 nextLevel = CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION;
125 isMethodProfiled = true;
126 break;
127 }
178 return method;
179 }
180
181 /**
182 * Gets {@link Callable} that invokes given method from the given object
183 *
184 * @param object the object the specified method is invoked from
185 * @param name the name of the method
186 */
187 public static Callable<Integer> getCallable(Object object, String name) {
188 Method method = getMethod(object.getClass(), name);
189 return () -> {
190 try {
191 return Objects.hashCode(method.invoke(object));
192 } catch (ReflectiveOperationException e) {
193 throw new Error("TESTBUG: Invocation failure", e);
194 }
195 };
196 }
197 }
198 }
199
200 enum ExtendedTestCase implements CompilerWhiteBoxTest.TestCase {
201 ACCESSOR_TEST("accessor"),
202 NONTRIVIAL_METHOD_TEST("nonTrivialMethod"),
203 TRIVIAL_CODE_TEST("trivialCode");
204
205 private final Executable executable;
206 private final Callable<Integer> callable;
207
208 @Override
209 public Executable getExecutable() {
210 return executable;
211 }
212
213 @Override
214 public Callable<Integer> getCallable() {
215 return callable;
216 }
217
218 @Override
219 public boolean isOsr() {
220 return false;
221 }
222
223 private ExtendedTestCase(String methodName) {
224 this.executable = LevelTransitionTest.Helper.getMethod(CompileMethodHolder.class, methodName);
225 this.callable = LevelTransitionTest.Helper.getCallable(new CompileMethodHolder(), methodName);
226 }
227
228 private static class CompileMethodHolder {
229 private final int iter = 10;
230 private int field = 42;
231
232 /** Non-trivial method for threshold policy: contains loops */
233 public int nonTrivialMethod() {
234 int acc = 0;
235 for (int i = 0; i < iter; i++) {
236 acc += i;
237 }
238 return acc;
239 }
240
241 /** Field accessor method */
242 public int accessor() {
243 return field;
244 }
245
246 /** Method considered as trivial by amount of code */
247 public int trivialCode() {
248 int var = 0xBAAD_C0DE;
249 var *= field;
250 return var;
251 }
252 }
253 }
|
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 * @test LevelTransitionTest
26 * @summary Test the correctness of compilation level transitions for different methods
27 * @library /testlibrary /test/lib /
28 * @modules java.base/jdk.internal.misc
29 * java.management
30 *
31 * @ignore 8067651
32 * @build compiler.tiered.TransitionsTestExecutor compiler.tiered.LevelTransitionTest
33 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
34 * sun.hotspot.WhiteBox$WhiteBoxPermission
35 * @run main/othervm/timeout=240 -Xmixed -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
36 * -XX:+WhiteBoxAPI -XX:+TieredCompilation -XX:-UseCounterDecay
37 * -XX:CompileCommand=compileonly,compiler.whitebox.SimpleTestCaseHelper::*
38 * -XX:CompileCommand=compileonly,compiler.tiered.LevelTransitionTest$ExtendedTestCase$CompileMethodHolder::*
39 * compiler.tiered.TransitionsTestExecutor
40 * compiler.tiered.LevelTransitionTest
41 */
42
43 package compiler.tiered;
44
45 import compiler.whitebox.CompilerWhiteBoxTest;
46 import compiler.whitebox.SimpleTestCase;
47
48 import java.lang.reflect.Executable;
49 import java.lang.reflect.Method;
50 import java.util.Objects;
51 import java.util.concurrent.Callable;
52
53 public class LevelTransitionTest extends TieredLevelsTest {
54 /**
55 * Shows if method was profiled by being executed on levels 2 or 3
56 */
57 protected boolean isMethodProfiled;
58 private int transitionCount;
59
60 public static void main(String[] args) throws Throwable {
61 assert (!CompilerWhiteBoxTest.skipOnTieredCompilation(false));
62
63 CompilerWhiteBoxTest.main(LevelTransitionTest::new, args);
64 // run extended test cases
65 for (TestCase testCase : ExtendedTestCase.values()) {
66 new LevelTransitionTest(testCase).runTest();
67 }
68 }
69
70 protected LevelTransitionTest(TestCase testCase) {
71 super(testCase);
72 isMethodProfiled = testCase.isOsr(); // OSR methods were already profiled by warmup
73 transitionCount = 0;
74 }
75
76 @Override
94 boolean finish = false;
95 while (!finish) {
96 System.out.printf("Level transition #%d%n", ++transitionCount);
97 int newLevel;
98 int current = getCompLevel();
99 int expected = getNextLevel(current);
100 if (current == expected) {
101 // if we are on expected level, just execute it more
102 // to ensure that the level won't change
103 System.out.printf("Method %s is already on expected level %d%n", method, expected);
104 compile();
105 newLevel = getCompLevel();
106 finish = true;
107 } else {
108 newLevel = changeCompLevel();
109 finish = false;
110 }
111 System.out.printf("Method %s is compiled on level %d. Expected level is %d%n", method, newLevel, expected);
112 checkLevel(expected, newLevel);
113 printInfo();
114 }
115 ;
116 }
117
118 /**
119 * Gets next expected level for the test case on each transition.
120 *
121 * @param currentLevel a level the test case is compiled on
122 * @return expected compilation level
123 */
124 protected int getNextLevel(int currentLevel) {
125 int nextLevel = currentLevel;
126 switch (currentLevel) {
127 case CompilerWhiteBoxTest.COMP_LEVEL_NONE:
128 nextLevel = isMethodProfiled ? CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION
129 : CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE;
130 break;
131 case CompilerWhiteBoxTest.COMP_LEVEL_LIMITED_PROFILE:
132 case CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE:
133 nextLevel = CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION;
134 isMethodProfiled = true;
135 break;
136 }
187 return method;
188 }
189
190 /**
191 * Gets {@link Callable} that invokes given method from the given object
192 *
193 * @param object the object the specified method is invoked from
194 * @param name the name of the method
195 */
196 public static Callable<Integer> getCallable(Object object, String name) {
197 Method method = getMethod(object.getClass(), name);
198 return () -> {
199 try {
200 return Objects.hashCode(method.invoke(object));
201 } catch (ReflectiveOperationException e) {
202 throw new Error("TESTBUG: Invocation failure", e);
203 }
204 };
205 }
206 }
207
208 private static enum ExtendedTestCase implements CompilerWhiteBoxTest.TestCase {
209 ACCESSOR_TEST("accessor"),
210 NONTRIVIAL_METHOD_TEST("nonTrivialMethod"),
211 TRIVIAL_CODE_TEST("trivialCode");
212
213 private final Executable executable;
214 private final Callable<Integer> callable;
215
216 @Override
217 public Executable getExecutable() {
218 return executable;
219 }
220
221 @Override
222 public Callable<Integer> getCallable() {
223 return callable;
224 }
225
226 @Override
227 public boolean isOsr() {
228 return false;
229 }
230
231 private ExtendedTestCase(String methodName) {
232 this.executable = LevelTransitionTest.Helper.getMethod(CompileMethodHolder.class, methodName);
233 this.callable = LevelTransitionTest.Helper.getCallable(new CompileMethodHolder(), methodName);
234 }
235
236 private static class CompileMethodHolder {
237 private final int iter = 10;
238 private int field = 42;
239
240 /**
241 * Non-trivial method for threshold policy: contains loops
242 */
243 public int nonTrivialMethod() {
244 int acc = 0;
245 for (int i = 0; i < iter; i++) {
246 acc += i;
247 }
248 return acc;
249 }
250
251 /**
252 * Field accessor method
253 */
254 public int accessor() {
255 return field;
256 }
257
258 /**
259 * Method considered as trivial by amount of code
260 */
261 public int trivialCode() {
262 int var = 0xBAAD_C0DE;
263 var *= field;
264 return var;
265 }
266 }
267 }
268
|