1 /* 2 * Copyright (c) 2014, 2015, 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 import java.lang.reflect.Executable; 25 import java.lang.reflect.Method; 26 import java.util.Objects; 27 import java.util.concurrent.Callable; 28 29 /** 30 * @test LevelTransitionTest 31 * @library /testlibrary /../../test/lib /compiler/whitebox 32 * @modules java.base/sun.misc 33 * java.management 34 * @ignore 8067651 35 * @build TransitionsTestExecutor LevelTransitionTest 36 * @run main ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission 37 * @run main/othervm/timeout=240 -Xmixed -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions 38 * -XX:+WhiteBoxAPI -XX:+TieredCompilation 39 * -XX:CompileCommand=compileonly,SimpleTestCase$Helper::* 40 * -XX:CompileCommand=compileonly,ExtendedTestCase$CompileMethodHolder::* 41 * TransitionsTestExecutor LevelTransitionTest 42 * @summary Test the correctness of compilation level transitions for different methods 43 */ 44 public class LevelTransitionTest extends TieredLevelsTest { 45 /** Shows if method was profiled by being executed on levels 2 or 3 */ 46 protected boolean isMethodProfiled; 47 private int transitionCount; 48 49 public static void main(String[] args) throws Throwable { 50 assert (!CompilerWhiteBoxTest.skipOnTieredCompilation(false)); 51 52 CompilerWhiteBoxTest.main(LevelTransitionTest::new, args); 53 // run extended test cases 54 for (TestCase testCase : ExtendedTestCase.values()) { 55 new LevelTransitionTest(testCase).runTest(); 56 } 57 } 58 59 protected LevelTransitionTest(TestCase testCase) { 60 super(testCase); 61 isMethodProfiled = testCase.isOsr(); // OSR methods were already profiled by warmup 62 transitionCount = 0; 63 } 64 65 @Override 66 protected void test() throws Exception { 67 checkTransitions(); 68 deoptimize(); 69 printInfo(); 70 if (testCase.isOsr()) { 71 // deoptimization makes the following transitions be unstable 72 // methods go to level 3 before 4 because of uncommon_trap and reprofile 73 return; 74 } 75 checkTransitions(); 76 } 77 78 /** 79 * Makes and verifies transitions between compilation levels 80 */ 81 protected void checkTransitions() { 82 checkNotCompiled(); 83 boolean finish = false; 84 while (!finish) { 85 System.out.printf("Level transition #%d%n", ++transitionCount); 86 int newLevel; 87 int current = getCompLevel(); 88 int expected = getNextLevel(current); 89 if (current == expected) { 90 // if we are on expected level, just execute it more 91 // to ensure that the level won't change 92 System.out.printf("Method %s is already on expected level %d%n", method, expected); 93 compile(); 94 newLevel = getCompLevel(); 95 finish = true; 96 } else { 97 newLevel = changeCompLevel(); 98 finish = false; 99 } 100 System.out.printf("Method %s is compiled on level %d. Expected level is %d%n", method, newLevel, expected); 101 checkLevel(expected, newLevel); 102 printInfo(); 103 }; 104 } 105 106 /** 107 * Gets next expected level for the test case on each transition. 108 * 109 * @param currentLevel a level the test case is compiled on 110 * @return expected compilation level 111 */ 112 protected int getNextLevel(int currentLevel) { 113 int nextLevel = currentLevel; 114 switch (currentLevel) { 115 case CompilerWhiteBoxTest.COMP_LEVEL_NONE: 116 nextLevel = isMethodProfiled ? CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION 117 : CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE; 118 break; 119 case CompilerWhiteBoxTest.COMP_LEVEL_LIMITED_PROFILE: 120 case CompilerWhiteBoxTest.COMP_LEVEL_FULL_PROFILE: 121 nextLevel = CompilerWhiteBoxTest.COMP_LEVEL_FULL_OPTIMIZATION; 122 isMethodProfiled = true; 123 break; 124 } 125 nextLevel = isTrivial() ? CompilerWhiteBoxTest.COMP_LEVEL_SIMPLE : nextLevel; 126 return Math.min(nextLevel, CompilerWhiteBoxTest.TIERED_STOP_AT_LEVEL); 127 } 128 129 /** 130 * Determines if tested method should be handled as trivial 131 * 132 * @return {@code true} for trivial methods, {@code false} otherwise 133 */ 134 protected boolean isTrivial() { 135 return testCase == ExtendedTestCase.ACCESSOR_TEST 136 || testCase == SimpleTestCase.METHOD_TEST 137 || testCase == SimpleTestCase.STATIC_TEST 138 || (testCase == ExtendedTestCase.TRIVIAL_CODE_TEST && isMethodProfiled); 139 } 140 141 /** 142 * Invokes {@linkplain #method} until its compilation level is changed. 143 * Note that if the level won't change, it will be an endless loop 144 * 145 * @return compilation level the {@linkplain #method} was compiled on 146 */ 147 protected int changeCompLevel() { 148 int currentLevel = getCompLevel(); 149 int newLevel = currentLevel; 150 int result = 0; 151 while (currentLevel == newLevel) { 152 result = compile(1); 153 if (WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) { 154 newLevel = getCompLevel(); 155 } 156 } 157 return newLevel; 158 } 159 160 protected static class Helper { 161 /** 162 * Gets method from a specified class using its name 163 * 164 * @param aClass type method belongs to 165 * @param name the name of the method 166 * @return {@link Method} that represents corresponding class method 167 */ 168 public static Method getMethod(Class<?> aClass, String name) { 169 Method method; 170 try { 171 method = aClass.getDeclaredMethod(name); 172 } catch (NoSuchMethodException e) { 173 throw new Error("TESTBUG: Unable to get method " + name, e); 174 } 175 return method; 176 } 177 178 /** 179 * Gets {@link Callable} that invokes given method from the given object 180 * 181 * @param object the object the specified method is invoked from 182 * @param name the name of the method 183 */ 184 public static Callable<Integer> getCallable(Object object, String name) { 185 Method method = getMethod(object.getClass(), name); 186 return () -> { 187 try { 188 return Objects.hashCode(method.invoke(object)); 189 } catch (ReflectiveOperationException e) { 190 throw new Error("TESTBUG: Invocation failure", e); 191 } 192 }; 193 } 194 } 195 } 196 197 enum ExtendedTestCase implements CompilerWhiteBoxTest.TestCase { 198 ACCESSOR_TEST("accessor"), 199 NONTRIVIAL_METHOD_TEST("nonTrivialMethod"), 200 TRIVIAL_CODE_TEST("trivialCode"); 201 202 private final Executable executable; 203 private final Callable<Integer> callable; 204 205 @Override 206 public Executable getExecutable() { 207 return executable; 208 } 209 210 @Override 211 public Callable<Integer> getCallable() { 212 return callable; 213 } 214 215 @Override 216 public boolean isOsr() { 217 return false; 218 } 219 220 private ExtendedTestCase(String methodName) { 221 this.executable = LevelTransitionTest.Helper.getMethod(CompileMethodHolder.class, methodName); 222 this.callable = LevelTransitionTest.Helper.getCallable(new CompileMethodHolder(), methodName); 223 } 224 225 private static class CompileMethodHolder { 226 private final int iter = 10; 227 private int field = 42; 228 229 /** Non-trivial method for threshold policy: contains loops */ 230 public int nonTrivialMethod() { 231 int acc = 0; 232 for (int i = 0; i < iter; i++) { 233 acc += i; 234 } 235 return acc; 236 } 237 238 /** Field accessor method */ 239 public int accessor() { 240 return field; 241 } 242 243 /** Method considered as trivial by amount of code */ 244 public int trivialCode() { 245 int var = 0xBAAD_C0DE; 246 var *= field; 247 return var; 248 } 249 } 250 }