Wed, 21 May 2014 10:56:41 -0700
Added tag jdk8u20-b15 for changeset 8c785f9bde6f
iignatyev@4592 | 1 | /* |
iignatyev@6353 | 2 | * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved. |
iignatyev@4592 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
iignatyev@4592 | 4 | * |
iignatyev@4592 | 5 | * This code is free software; you can redistribute it and/or modify it |
iignatyev@4592 | 6 | * under the terms of the GNU General Public License version 2 only, as |
iignatyev@4592 | 7 | * published by the Free Software Foundation. |
iignatyev@4592 | 8 | * |
iignatyev@4592 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
iignatyev@4592 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
iignatyev@4592 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
iignatyev@4592 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
iignatyev@4592 | 13 | * accompanied this code). |
iignatyev@4592 | 14 | * |
iignatyev@4592 | 15 | * You should have received a copy of the GNU General Public License version |
iignatyev@4592 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
iignatyev@4592 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
iignatyev@4592 | 18 | * |
iignatyev@4592 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
iignatyev@4592 | 20 | * or visit www.oracle.com if you need additional information or have any |
iignatyev@4592 | 21 | * questions. |
iignatyev@4592 | 22 | */ |
iignatyev@4592 | 23 | |
iignatyev@4951 | 24 | import com.sun.management.HotSpotDiagnosticMXBean; |
iignatyev@4951 | 25 | import com.sun.management.VMOption; |
iignatyev@4592 | 26 | import sun.hotspot.WhiteBox; |
iignatyev@6525 | 27 | import sun.hotspot.code.NMethod; |
iignatyev@4592 | 28 | import sun.management.ManagementFactoryHelper; |
iignatyev@4592 | 29 | |
iignatyev@4951 | 30 | import java.lang.reflect.Constructor; |
iignatyev@4951 | 31 | import java.lang.reflect.Executable; |
iignatyev@4592 | 32 | import java.lang.reflect.Method; |
iignatyev@4951 | 33 | import java.util.Objects; |
iignatyev@4951 | 34 | import java.util.concurrent.Callable; |
iignatyev@6211 | 35 | import java.util.function.Function; |
iignatyev@4592 | 36 | |
iignatyev@4951 | 37 | /** |
iignatyev@4951 | 38 | * Abstract class for WhiteBox testing of JIT. |
iignatyev@4951 | 39 | * |
iignatyev@4592 | 40 | * @author igor.ignatyev@oracle.com |
iignatyev@4592 | 41 | */ |
iignatyev@4592 | 42 | public abstract class CompilerWhiteBoxTest { |
iignatyev@4951 | 43 | /** {@code CompLevel::CompLevel_none} -- Interpreter */ |
iignatyev@4951 | 44 | protected static int COMP_LEVEL_NONE = 0; |
iignatyev@4951 | 45 | /** {@code CompLevel::CompLevel_any}, {@code CompLevel::CompLevel_all} */ |
iignatyev@4951 | 46 | protected static int COMP_LEVEL_ANY = -1; |
iignatyev@5032 | 47 | /** {@code CompLevel::CompLevel_simple} -- C1 */ |
iignatyev@5032 | 48 | protected static int COMP_LEVEL_SIMPLE = 1; |
iignatyev@5541 | 49 | /** {@code CompLevel::CompLevel_limited_profile} -- C1, invocation & backedge counters */ |
iignatyev@5541 | 50 | protected static int COMP_LEVEL_LIMITED_PROFILE = 2; |
iignatyev@5541 | 51 | /** {@code CompLevel::CompLevel_full_profile} -- C1, invocation & backedge counters + mdo */ |
iignatyev@5541 | 52 | protected static int COMP_LEVEL_FULL_PROFILE = 3; |
iignatyev@5032 | 53 | /** {@code CompLevel::CompLevel_full_optimization} -- C2 or Shark */ |
iignatyev@5032 | 54 | protected static int COMP_LEVEL_FULL_OPTIMIZATION = 4; |
iignatyev@6211 | 55 | /** Maximal value for CompLevel */ |
iignatyev@5541 | 56 | protected static int COMP_LEVEL_MAX = COMP_LEVEL_FULL_OPTIMIZATION; |
iignatyev@5032 | 57 | |
iignatyev@4951 | 58 | /** Instance of WhiteBox */ |
iignatyev@4592 | 59 | protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox(); |
iignatyev@4951 | 60 | /** Value of {@code -XX:CompileThreshold} */ |
iignatyev@4592 | 61 | protected static final int COMPILE_THRESHOLD |
iignatyev@4592 | 62 | = Integer.parseInt(getVMOption("CompileThreshold", "10000")); |
iignatyev@4951 | 63 | /** Value of {@code -XX:BackgroundCompilation} */ |
iignatyev@4766 | 64 | protected static final boolean BACKGROUND_COMPILATION |
iignatyev@4766 | 65 | = Boolean.valueOf(getVMOption("BackgroundCompilation", "true")); |
iignatyev@4951 | 66 | /** Value of {@code -XX:TieredCompilation} */ |
iignatyev@4908 | 67 | protected static final boolean TIERED_COMPILATION |
iignatyev@4908 | 68 | = Boolean.valueOf(getVMOption("TieredCompilation", "false")); |
iignatyev@4951 | 69 | /** Value of {@code -XX:TieredStopAtLevel} */ |
iignatyev@4951 | 70 | protected static final int TIERED_STOP_AT_LEVEL |
iignatyev@4951 | 71 | = Integer.parseInt(getVMOption("TieredStopAtLevel", "0")); |
iignatyev@5512 | 72 | /** Flag for verbose output, true if {@code -Dverbose} specified */ |
iignatyev@5512 | 73 | protected static final boolean IS_VERBOSE |
iignatyev@5512 | 74 | = System.getProperty("verbose") != null; |
iignatyev@5541 | 75 | /** count of invocation to triger compilation */ |
iignatyev@5541 | 76 | protected static final int THRESHOLD; |
iignatyev@5541 | 77 | /** count of invocation to triger OSR compilation */ |
iignatyev@5541 | 78 | protected static final long BACKEDGE_THRESHOLD; |
iignatyev@5796 | 79 | /** Value of {@code java.vm.info} (interpreted|mixed|comp mode) */ |
iignatyev@6211 | 80 | protected static final String MODE = System.getProperty("java.vm.info"); |
iignatyev@5541 | 81 | |
iignatyev@5541 | 82 | static { |
iignatyev@5541 | 83 | if (TIERED_COMPILATION) { |
iignatyev@5983 | 84 | BACKEDGE_THRESHOLD = THRESHOLD = 150000; |
iignatyev@5541 | 85 | } else { |
iignatyev@5541 | 86 | THRESHOLD = COMPILE_THRESHOLD; |
iignatyev@5541 | 87 | BACKEDGE_THRESHOLD = COMPILE_THRESHOLD * Long.parseLong(getVMOption( |
iignatyev@5541 | 88 | "OnStackReplacePercentage")); |
iignatyev@5541 | 89 | } |
iignatyev@5541 | 90 | } |
iignatyev@4592 | 91 | |
iignatyev@4951 | 92 | /** |
iignatyev@4951 | 93 | * Returns value of VM option. |
iignatyev@4951 | 94 | * |
iignatyev@4951 | 95 | * @param name option's name |
iignatyev@4951 | 96 | * @return value of option or {@code null}, if option doesn't exist |
iignatyev@4951 | 97 | * @throws NullPointerException if name is null |
iignatyev@4951 | 98 | */ |
iignatyev@4951 | 99 | protected static String getVMOption(String name) { |
iignatyev@4951 | 100 | Objects.requireNonNull(name); |
iignatyev@4951 | 101 | HotSpotDiagnosticMXBean diagnostic |
iignatyev@4951 | 102 | = ManagementFactoryHelper.getDiagnosticMXBean(); |
iignatyev@4951 | 103 | VMOption tmp; |
iignatyev@4592 | 104 | try { |
iignatyev@4951 | 105 | tmp = diagnostic.getVMOption(name); |
iignatyev@4951 | 106 | } catch (IllegalArgumentException e) { |
iignatyev@4951 | 107 | tmp = null; |
iignatyev@4592 | 108 | } |
iignatyev@4951 | 109 | return (tmp == null ? null : tmp.getValue()); |
iignatyev@4592 | 110 | } |
iignatyev@4592 | 111 | |
iignatyev@4951 | 112 | /** |
iignatyev@4951 | 113 | * Returns value of VM option or default value. |
iignatyev@4951 | 114 | * |
iignatyev@4951 | 115 | * @param name option's name |
iignatyev@4951 | 116 | * @param defaultValue default value |
iignatyev@4951 | 117 | * @return value of option or {@code defaultValue}, if option doesn't exist |
iignatyev@4951 | 118 | * @throws NullPointerException if name is null |
iignatyev@4951 | 119 | * @see #getVMOption(String) |
iignatyev@4951 | 120 | */ |
iignatyev@4766 | 121 | protected static String getVMOption(String name, String defaultValue) { |
iignatyev@4766 | 122 | String result = getVMOption(name); |
iignatyev@4592 | 123 | return result == null ? defaultValue : result; |
iignatyev@4592 | 124 | } |
iignatyev@4592 | 125 | |
iignatyev@5032 | 126 | /** copy of is_c1_compile(int) from utilities/globalDefinitions.hpp */ |
iignatyev@5032 | 127 | protected static boolean isC1Compile(int compLevel) { |
iignatyev@5032 | 128 | return (compLevel > COMP_LEVEL_NONE) |
iignatyev@5032 | 129 | && (compLevel < COMP_LEVEL_FULL_OPTIMIZATION); |
iignatyev@5032 | 130 | } |
iignatyev@5032 | 131 | |
iignatyev@5032 | 132 | /** copy of is_c2_compile(int) from utilities/globalDefinitions.hpp */ |
iignatyev@5032 | 133 | protected static boolean isC2Compile(int compLevel) { |
iignatyev@5032 | 134 | return compLevel == COMP_LEVEL_FULL_OPTIMIZATION; |
iignatyev@5032 | 135 | } |
iignatyev@5032 | 136 | |
iignatyev@6211 | 137 | protected static void main( |
iignatyev@6211 | 138 | Function<TestCase, CompilerWhiteBoxTest> constructor, |
iignatyev@6211 | 139 | String[] args) { |
iignatyev@6211 | 140 | if (args.length == 0) { |
iignatyev@6211 | 141 | for (TestCase test : SimpleTestCase.values()) { |
iignatyev@6211 | 142 | constructor.apply(test).runTest(); |
iignatyev@6211 | 143 | } |
iignatyev@6211 | 144 | } else { |
iignatyev@6211 | 145 | for (String name : args) { |
iignatyev@6211 | 146 | constructor.apply(SimpleTestCase.valueOf(name)).runTest(); |
iignatyev@6211 | 147 | } |
iignatyev@6211 | 148 | } |
iignatyev@6211 | 149 | } |
iignatyev@6211 | 150 | |
iignatyev@4951 | 151 | /** tested method */ |
iignatyev@4951 | 152 | protected final Executable method; |
iignatyev@5541 | 153 | protected final TestCase testCase; |
iignatyev@4951 | 154 | |
iignatyev@4951 | 155 | /** |
iignatyev@4951 | 156 | * Constructor. |
iignatyev@4951 | 157 | * |
iignatyev@4951 | 158 | * @param testCase object, that contains tested method and way to invoke it. |
iignatyev@4951 | 159 | */ |
iignatyev@4951 | 160 | protected CompilerWhiteBoxTest(TestCase testCase) { |
iignatyev@4951 | 161 | Objects.requireNonNull(testCase); |
iignatyev@4951 | 162 | System.out.println("TEST CASE:" + testCase.name()); |
iignatyev@6211 | 163 | method = testCase.getExecutable(); |
iignatyev@5541 | 164 | this.testCase = testCase; |
iignatyev@4951 | 165 | } |
iignatyev@4951 | 166 | |
iignatyev@4951 | 167 | /** |
iignatyev@4951 | 168 | * Template method for testing. Prints tested method's info before |
iignatyev@4951 | 169 | * {@linkplain #test()} and after {@linkplain #test()} or on thrown |
iignatyev@4951 | 170 | * exception. |
iignatyev@4951 | 171 | * |
iignatyev@4951 | 172 | * @throws RuntimeException if method {@linkplain #test()} throws any |
iignatyev@4951 | 173 | * exception |
iignatyev@4951 | 174 | * @see #test() |
iignatyev@4951 | 175 | */ |
iignatyev@4951 | 176 | protected final void runTest() { |
iignatyev@4592 | 177 | if (ManagementFactoryHelper.getCompilationMXBean() == null) { |
iignatyev@4592 | 178 | System.err.println( |
iignatyev@4592 | 179 | "Warning: test is not applicable in interpreted mode"); |
iignatyev@4592 | 180 | return; |
iignatyev@4592 | 181 | } |
iignatyev@4592 | 182 | System.out.println("at test's start:"); |
iignatyev@4951 | 183 | printInfo(); |
iignatyev@4592 | 184 | try { |
iignatyev@4592 | 185 | test(); |
iignatyev@4592 | 186 | } catch (Exception e) { |
iignatyev@4592 | 187 | System.out.printf("on exception '%s':", e.getMessage()); |
iignatyev@4951 | 188 | printInfo(); |
iignatyev@4766 | 189 | e.printStackTrace(); |
iignatyev@4951 | 190 | if (e instanceof RuntimeException) { |
iignatyev@4951 | 191 | throw (RuntimeException) e; |
iignatyev@4951 | 192 | } |
iignatyev@4592 | 193 | throw new RuntimeException(e); |
iignatyev@4592 | 194 | } |
iignatyev@4592 | 195 | System.out.println("at test's end:"); |
iignatyev@4951 | 196 | printInfo(); |
iignatyev@4592 | 197 | } |
iignatyev@4592 | 198 | |
iignatyev@4951 | 199 | /** |
neliasso@6612 | 200 | * Checks, that {@linkplain #method} is not compiled at the given compilation |
neliasso@6612 | 201 | * level or above. |
neliasso@6612 | 202 | * |
neliasso@6612 | 203 | * @param compLevel |
neliasso@6612 | 204 | * |
neliasso@6612 | 205 | * @throws RuntimeException if {@linkplain #method} is in compiler queue or |
neliasso@6612 | 206 | * is compiled, or if {@linkplain #method} has zero |
neliasso@6612 | 207 | * compilation level. |
neliasso@6612 | 208 | */ |
neliasso@6612 | 209 | |
neliasso@6612 | 210 | protected final void checkNotCompiled(int compLevel) { |
neliasso@6612 | 211 | if (WHITE_BOX.isMethodQueuedForCompilation(method)) { |
neliasso@6612 | 212 | throw new RuntimeException(method + " must not be in queue"); |
neliasso@6612 | 213 | } |
neliasso@6612 | 214 | if (WHITE_BOX.getMethodCompilationLevel(method, false) >= compLevel) { |
neliasso@6612 | 215 | throw new RuntimeException(method + " comp_level must be >= maxCompLevel"); |
neliasso@6612 | 216 | } |
neliasso@6612 | 217 | if (WHITE_BOX.getMethodCompilationLevel(method, true) >= compLevel) { |
neliasso@6612 | 218 | throw new RuntimeException(method + " osr_comp_level must be >= maxCompLevel"); |
neliasso@6612 | 219 | } |
neliasso@6612 | 220 | } |
neliasso@6612 | 221 | |
neliasso@6612 | 222 | /** |
iignatyev@4951 | 223 | * Checks, that {@linkplain #method} is not compiled. |
iignatyev@4951 | 224 | * |
iignatyev@4951 | 225 | * @throws RuntimeException if {@linkplain #method} is in compiler queue or |
iignatyev@4951 | 226 | * is compiled, or if {@linkplain #method} has zero |
iignatyev@4951 | 227 | * compilation level. |
iignatyev@4951 | 228 | */ |
iignatyev@4951 | 229 | protected final void checkNotCompiled() { |
iignatyev@4908 | 230 | if (WHITE_BOX.isMethodQueuedForCompilation(method)) { |
iignatyev@4908 | 231 | throw new RuntimeException(method + " must not be in queue"); |
iignatyev@4908 | 232 | } |
iignatyev@5541 | 233 | if (WHITE_BOX.isMethodCompiled(method, false)) { |
iignatyev@4592 | 234 | throw new RuntimeException(method + " must be not compiled"); |
iignatyev@4592 | 235 | } |
iignatyev@5541 | 236 | if (WHITE_BOX.getMethodCompilationLevel(method, false) != 0) { |
iignatyev@4592 | 237 | throw new RuntimeException(method + " comp_level must be == 0"); |
iignatyev@4592 | 238 | } |
iignatyev@5541 | 239 | if (WHITE_BOX.isMethodCompiled(method, true)) { |
iignatyev@5541 | 240 | throw new RuntimeException(method + " must be not osr_compiled"); |
iignatyev@5541 | 241 | } |
iignatyev@5541 | 242 | if (WHITE_BOX.getMethodCompilationLevel(method, true) != 0) { |
iignatyev@5541 | 243 | throw new RuntimeException(method + " osr_comp_level must be == 0"); |
iignatyev@5541 | 244 | } |
iignatyev@6211 | 245 | } |
iignatyev@4592 | 246 | |
iignatyev@4951 | 247 | /** |
iignatyev@4951 | 248 | * Checks, that {@linkplain #method} is compiled. |
iignatyev@4951 | 249 | * |
iignatyev@4951 | 250 | * @throws RuntimeException if {@linkplain #method} isn't in compiler queue |
iignatyev@4951 | 251 | * and isn't compiled, or if {@linkplain #method} |
iignatyev@4951 | 252 | * has nonzero compilation level |
iignatyev@4951 | 253 | */ |
iignatyev@4951 | 254 | protected final void checkCompiled() { |
iignatyev@4592 | 255 | final long start = System.currentTimeMillis(); |
iignatyev@4951 | 256 | waitBackgroundCompilation(); |
iignatyev@4592 | 257 | if (WHITE_BOX.isMethodQueuedForCompilation(method)) { |
iignatyev@4592 | 258 | System.err.printf("Warning: %s is still in queue after %dms%n", |
iignatyev@4592 | 259 | method, System.currentTimeMillis() - start); |
iignatyev@4592 | 260 | return; |
iignatyev@4592 | 261 | } |
iignatyev@6211 | 262 | if (!WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) { |
iignatyev@5541 | 263 | throw new RuntimeException(method + " must be " |
iignatyev@6211 | 264 | + (testCase.isOsr() ? "osr_" : "") + "compiled"); |
iignatyev@4592 | 265 | } |
iignatyev@6211 | 266 | if (WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr()) |
iignatyev@6211 | 267 | == 0) { |
iignatyev@5541 | 268 | throw new RuntimeException(method |
iignatyev@6211 | 269 | + (testCase.isOsr() ? " osr_" : " ") |
iignatyev@5541 | 270 | + "comp_level must be != 0"); |
iignatyev@4592 | 271 | } |
iignatyev@4592 | 272 | } |
iignatyev@4592 | 273 | |
iignatyev@5541 | 274 | protected final void deoptimize() { |
iignatyev@6211 | 275 | WHITE_BOX.deoptimizeMethod(method, testCase.isOsr()); |
iignatyev@6211 | 276 | if (testCase.isOsr()) { |
iignatyev@5541 | 277 | WHITE_BOX.deoptimizeMethod(method, false); |
iignatyev@5541 | 278 | } |
iignatyev@5541 | 279 | } |
iignatyev@5541 | 280 | |
iignatyev@5541 | 281 | protected final int getCompLevel() { |
iignatyev@6525 | 282 | NMethod nm = NMethod.get(method, testCase.isOsr()); |
iignatyev@6525 | 283 | return nm == null ? COMP_LEVEL_NONE : nm.comp_level; |
iignatyev@5541 | 284 | } |
iignatyev@5541 | 285 | |
iignatyev@5541 | 286 | protected final boolean isCompilable() { |
iignatyev@5541 | 287 | return WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, |
iignatyev@6211 | 288 | testCase.isOsr()); |
iignatyev@5541 | 289 | } |
iignatyev@5541 | 290 | |
iignatyev@5541 | 291 | protected final boolean isCompilable(int compLevel) { |
iignatyev@6211 | 292 | return WHITE_BOX |
iignatyev@6211 | 293 | .isMethodCompilable(method, compLevel, testCase.isOsr()); |
iignatyev@5541 | 294 | } |
iignatyev@5541 | 295 | |
iignatyev@5541 | 296 | protected final void makeNotCompilable() { |
iignatyev@5541 | 297 | WHITE_BOX.makeMethodNotCompilable(method, COMP_LEVEL_ANY, |
iignatyev@6211 | 298 | testCase.isOsr()); |
iignatyev@5541 | 299 | } |
iignatyev@5541 | 300 | |
iignatyev@5541 | 301 | protected final void makeNotCompilable(int compLevel) { |
iignatyev@6211 | 302 | WHITE_BOX.makeMethodNotCompilable(method, compLevel, testCase.isOsr()); |
iignatyev@5541 | 303 | } |
iignatyev@5541 | 304 | |
iignatyev@4951 | 305 | /** |
iignatyev@4951 | 306 | * Waits for completion of background compilation of {@linkplain #method}. |
iignatyev@4951 | 307 | */ |
iignatyev@4951 | 308 | protected final void waitBackgroundCompilation() { |
iignatyev@4766 | 309 | if (!BACKGROUND_COMPILATION) { |
iignatyev@4766 | 310 | return; |
iignatyev@4766 | 311 | } |
iignatyev@4592 | 312 | final Object obj = new Object(); |
iignatyev@4951 | 313 | for (int i = 0; i < 10 |
iignatyev@4951 | 314 | && WHITE_BOX.isMethodQueuedForCompilation(method); ++i) { |
iignatyev@4951 | 315 | synchronized (obj) { |
iignatyev@4951 | 316 | try { |
iignatyev@4951 | 317 | obj.wait(1000); |
iignatyev@4951 | 318 | } catch (InterruptedException e) { |
iignatyev@4951 | 319 | Thread.currentThread().interrupt(); |
iignatyev@4592 | 320 | } |
iignatyev@4592 | 321 | } |
iignatyev@4592 | 322 | } |
iignatyev@4592 | 323 | } |
iignatyev@4592 | 324 | |
iignatyev@4951 | 325 | /** |
iignatyev@4951 | 326 | * Prints information about {@linkplain #method}. |
iignatyev@4951 | 327 | */ |
iignatyev@4951 | 328 | protected final void printInfo() { |
iignatyev@4592 | 329 | System.out.printf("%n%s:%n", method); |
iignatyev@4592 | 330 | System.out.printf("\tcompilable:\t%b%n", |
iignatyev@5541 | 331 | WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, false)); |
iignatyev@4592 | 332 | System.out.printf("\tcompiled:\t%b%n", |
iignatyev@5541 | 333 | WHITE_BOX.isMethodCompiled(method, false)); |
iignatyev@4592 | 334 | System.out.printf("\tcomp_level:\t%d%n", |
iignatyev@5541 | 335 | WHITE_BOX.getMethodCompilationLevel(method, false)); |
iignatyev@5541 | 336 | System.out.printf("\tosr_compilable:\t%b%n", |
iignatyev@5541 | 337 | WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true)); |
iignatyev@5541 | 338 | System.out.printf("\tosr_compiled:\t%b%n", |
iignatyev@5541 | 339 | WHITE_BOX.isMethodCompiled(method, true)); |
iignatyev@5541 | 340 | System.out.printf("\tosr_comp_level:\t%d%n", |
iignatyev@5541 | 341 | WHITE_BOX.getMethodCompilationLevel(method, true)); |
iignatyev@6211 | 342 | System.out.printf("\tin_queue:\t%b%n", |
iignatyev@4592 | 343 | WHITE_BOX.isMethodQueuedForCompilation(method)); |
iignatyev@4592 | 344 | System.out.printf("compile_queues_size:\t%d%n%n", |
iignatyev@4592 | 345 | WHITE_BOX.getCompileQueuesSize()); |
iignatyev@4592 | 346 | } |
iignatyev@4592 | 347 | |
iignatyev@4951 | 348 | /** |
iignatyev@4951 | 349 | * Executes testing. |
iignatyev@4951 | 350 | */ |
iignatyev@4592 | 351 | protected abstract void test() throws Exception; |
iignatyev@4592 | 352 | |
iignatyev@4951 | 353 | /** |
iignatyev@4951 | 354 | * Tries to trigger compilation of {@linkplain #method} by call |
iignatyev@6211 | 355 | * {@linkplain TestCase#getCallable()} enough times. |
iignatyev@4951 | 356 | * |
iignatyev@4951 | 357 | * @return accumulated result |
iignatyev@4951 | 358 | * @see #compile(int) |
iignatyev@4951 | 359 | */ |
iignatyev@4592 | 360 | protected final int compile() { |
iignatyev@6211 | 361 | if (testCase.isOsr()) { |
iignatyev@5541 | 362 | return compile(1); |
iignatyev@5541 | 363 | } else { |
iignatyev@5541 | 364 | return compile(THRESHOLD); |
iignatyev@5541 | 365 | } |
iignatyev@4908 | 366 | } |
iignatyev@4908 | 367 | |
iignatyev@4951 | 368 | /** |
iignatyev@4951 | 369 | * Tries to trigger compilation of {@linkplain #method} by call |
iignatyev@6211 | 370 | * {@linkplain TestCase#getCallable()} specified times. |
iignatyev@4951 | 371 | * |
iignatyev@4951 | 372 | * @param count invocation count |
iignatyev@4951 | 373 | * @return accumulated result |
iignatyev@4951 | 374 | */ |
iignatyev@4908 | 375 | protected final int compile(int count) { |
iignatyev@4592 | 376 | int result = 0; |
iignatyev@4951 | 377 | Integer tmp; |
iignatyev@4766 | 378 | for (int i = 0; i < count; ++i) { |
iignatyev@4951 | 379 | try { |
iignatyev@6211 | 380 | tmp = testCase.getCallable().call(); |
iignatyev@4951 | 381 | } catch (Exception e) { |
iignatyev@4951 | 382 | tmp = null; |
iignatyev@4951 | 383 | } |
iignatyev@4951 | 384 | result += tmp == null ? 0 : tmp; |
iignatyev@4592 | 385 | } |
iignatyev@5512 | 386 | if (IS_VERBOSE) { |
iignatyev@5512 | 387 | System.out.println("method was invoked " + count + " times"); |
iignatyev@5512 | 388 | } |
iignatyev@4592 | 389 | return result; |
iignatyev@4592 | 390 | } |
iignatyev@6211 | 391 | |
iignatyev@6211 | 392 | /** |
iignatyev@6211 | 393 | * Utility interface provides tested method and object to invoke it. |
iignatyev@6211 | 394 | */ |
iignatyev@6211 | 395 | public interface TestCase { |
iignatyev@6211 | 396 | /** the name of test case */ |
iignatyev@6211 | 397 | String name(); |
iignatyev@6211 | 398 | |
iignatyev@6211 | 399 | /** tested method */ |
iignatyev@6211 | 400 | Executable getExecutable(); |
iignatyev@6211 | 401 | |
iignatyev@6211 | 402 | /** object to invoke {@linkplain #getExecutable()} */ |
iignatyev@6211 | 403 | Callable<Integer> getCallable(); |
iignatyev@6211 | 404 | |
iignatyev@6211 | 405 | /** flag for OSR test case */ |
iignatyev@6211 | 406 | boolean isOsr(); |
iignatyev@6211 | 407 | } |
iignatyev@6353 | 408 | |
iignatyev@6353 | 409 | /** |
iignatyev@6353 | 410 | * @return {@code true} if the current test case is OSR and the mode is |
iignatyev@6353 | 411 | * Xcomp, otherwise {@code false} |
iignatyev@6353 | 412 | */ |
iignatyev@6353 | 413 | protected boolean skipXcompOSR() { |
iignatyev@6353 | 414 | boolean result = testCase.isOsr() |
iignatyev@6353 | 415 | && CompilerWhiteBoxTest.MODE.startsWith("compiled "); |
iignatyev@6353 | 416 | if (result && IS_VERBOSE) { |
iignatyev@6353 | 417 | System.err.printf("Warning: %s is not applicable in %s%n", |
iignatyev@6353 | 418 | testCase.name(), CompilerWhiteBoxTest.MODE); |
iignatyev@6353 | 419 | } |
iignatyev@6353 | 420 | return result; |
iignatyev@6353 | 421 | } |
iignatyev@4951 | 422 | } |
iignatyev@4592 | 423 | |
iignatyev@6211 | 424 | enum SimpleTestCase implements CompilerWhiteBoxTest.TestCase { |
iignatyev@4951 | 425 | /** constructor test case */ |
iignatyev@5541 | 426 | CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE, false), |
iignatyev@4951 | 427 | /** method test case */ |
iignatyev@5541 | 428 | METOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false), |
iignatyev@4951 | 429 | /** static method test case */ |
iignatyev@5541 | 430 | STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE, false), |
iignatyev@5541 | 431 | /** OSR constructor test case */ |
iignatyev@5541 | 432 | OSR_CONSTRUCTOR_TEST(Helper.OSR_CONSTRUCTOR, |
iignatyev@5541 | 433 | Helper.OSR_CONSTRUCTOR_CALLABLE, true), |
iignatyev@5983 | 434 | /** OSR method test case */ |
iignatyev@5541 | 435 | OSR_METOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true), |
iignatyev@5541 | 436 | /** OSR static method test case */ |
iignatyev@5541 | 437 | OSR_STATIC_TEST(Helper.OSR_STATIC, Helper.OSR_STATIC_CALLABLE, true); |
iignatyev@4951 | 438 | |
iignatyev@6211 | 439 | private final Executable executable; |
iignatyev@6211 | 440 | private final Callable<Integer> callable; |
iignatyev@6211 | 441 | private final boolean isOsr; |
iignatyev@4951 | 442 | |
iignatyev@6211 | 443 | private SimpleTestCase(Executable executable, Callable<Integer> callable, |
iignatyev@5541 | 444 | boolean isOsr) { |
iignatyev@4951 | 445 | this.executable = executable; |
iignatyev@4951 | 446 | this.callable = callable; |
iignatyev@5541 | 447 | this.isOsr = isOsr; |
iignatyev@4951 | 448 | } |
iignatyev@4951 | 449 | |
iignatyev@6211 | 450 | @Override |
iignatyev@6211 | 451 | public Executable getExecutable() { |
iignatyev@6211 | 452 | return executable; |
iignatyev@6211 | 453 | } |
iignatyev@6211 | 454 | |
iignatyev@6211 | 455 | @Override |
iignatyev@6211 | 456 | public Callable<Integer> getCallable() { |
iignatyev@6211 | 457 | return callable; |
iignatyev@6211 | 458 | } |
iignatyev@6211 | 459 | |
iignatyev@6211 | 460 | @Override |
iignatyev@6211 | 461 | public boolean isOsr() { |
iignatyev@6211 | 462 | return isOsr; |
iignatyev@6211 | 463 | } |
iignatyev@6211 | 464 | |
iignatyev@4951 | 465 | private static class Helper { |
iignatyev@5541 | 466 | |
iignatyev@4951 | 467 | private static final Callable<Integer> CONSTRUCTOR_CALLABLE |
iignatyev@4951 | 468 | = new Callable<Integer>() { |
iignatyev@4951 | 469 | @Override |
iignatyev@4951 | 470 | public Integer call() throws Exception { |
iignatyev@4951 | 471 | return new Helper(1337).hashCode(); |
iignatyev@4951 | 472 | } |
iignatyev@4951 | 473 | }; |
iignatyev@4951 | 474 | |
iignatyev@4951 | 475 | private static final Callable<Integer> METHOD_CALLABLE |
iignatyev@4951 | 476 | = new Callable<Integer>() { |
iignatyev@4951 | 477 | private final Helper helper = new Helper(); |
iignatyev@4951 | 478 | |
iignatyev@4951 | 479 | @Override |
iignatyev@4951 | 480 | public Integer call() throws Exception { |
iignatyev@4951 | 481 | return helper.method(); |
iignatyev@4951 | 482 | } |
iignatyev@4951 | 483 | }; |
iignatyev@4951 | 484 | |
iignatyev@4951 | 485 | private static final Callable<Integer> STATIC_CALLABLE |
iignatyev@4951 | 486 | = new Callable<Integer>() { |
iignatyev@4951 | 487 | @Override |
iignatyev@4951 | 488 | public Integer call() throws Exception { |
iignatyev@4951 | 489 | return staticMethod(); |
iignatyev@4951 | 490 | } |
iignatyev@4951 | 491 | }; |
iignatyev@4951 | 492 | |
iignatyev@5541 | 493 | private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE |
iignatyev@5541 | 494 | = new Callable<Integer>() { |
iignatyev@5541 | 495 | @Override |
iignatyev@5541 | 496 | public Integer call() throws Exception { |
iignatyev@5541 | 497 | return new Helper(null).hashCode(); |
iignatyev@5541 | 498 | } |
iignatyev@5541 | 499 | }; |
iignatyev@5541 | 500 | |
iignatyev@5541 | 501 | private static final Callable<Integer> OSR_METHOD_CALLABLE |
iignatyev@5541 | 502 | = new Callable<Integer>() { |
iignatyev@5541 | 503 | private final Helper helper = new Helper(); |
iignatyev@5541 | 504 | |
iignatyev@5541 | 505 | @Override |
iignatyev@5541 | 506 | public Integer call() throws Exception { |
iignatyev@5541 | 507 | return helper.osrMethod(); |
iignatyev@5541 | 508 | } |
iignatyev@5541 | 509 | }; |
iignatyev@5541 | 510 | |
iignatyev@5541 | 511 | private static final Callable<Integer> OSR_STATIC_CALLABLE |
iignatyev@5541 | 512 | = new Callable<Integer>() { |
iignatyev@5541 | 513 | @Override |
iignatyev@5541 | 514 | public Integer call() throws Exception { |
iignatyev@5541 | 515 | return osrStaticMethod(); |
iignatyev@5541 | 516 | } |
iignatyev@5541 | 517 | }; |
iignatyev@5541 | 518 | |
iignatyev@4951 | 519 | private static final Constructor CONSTRUCTOR; |
iignatyev@5541 | 520 | private static final Constructor OSR_CONSTRUCTOR; |
iignatyev@4951 | 521 | private static final Method METHOD; |
iignatyev@4951 | 522 | private static final Method STATIC; |
iignatyev@5541 | 523 | private static final Method OSR_METHOD; |
iignatyev@5541 | 524 | private static final Method OSR_STATIC; |
iignatyev@4951 | 525 | |
iignatyev@4951 | 526 | static { |
iignatyev@4951 | 527 | try { |
iignatyev@4951 | 528 | CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class); |
iignatyev@4951 | 529 | } catch (NoSuchMethodException | SecurityException e) { |
iignatyev@4951 | 530 | throw new RuntimeException( |
iignatyev@4951 | 531 | "exception on getting method Helper.<init>(int)", e); |
iignatyev@4951 | 532 | } |
iignatyev@4951 | 533 | try { |
iignatyev@5541 | 534 | OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor( |
iignatyev@5541 | 535 | Object.class); |
iignatyev@4951 | 536 | } catch (NoSuchMethodException | SecurityException e) { |
iignatyev@4951 | 537 | throw new RuntimeException( |
iignatyev@5541 | 538 | "exception on getting method Helper.<init>(Object)", e); |
iignatyev@4951 | 539 | } |
iignatyev@5541 | 540 | METHOD = getMethod("method"); |
iignatyev@5541 | 541 | STATIC = getMethod("staticMethod"); |
iignatyev@5541 | 542 | OSR_METHOD = getMethod("osrMethod"); |
iignatyev@5541 | 543 | OSR_STATIC = getMethod("osrStaticMethod"); |
iignatyev@5541 | 544 | } |
iignatyev@5541 | 545 | |
iignatyev@5541 | 546 | private static Method getMethod(String name) { |
iignatyev@4951 | 547 | try { |
iignatyev@5541 | 548 | return Helper.class.getDeclaredMethod(name); |
iignatyev@4951 | 549 | } catch (NoSuchMethodException | SecurityException e) { |
iignatyev@4951 | 550 | throw new RuntimeException( |
iignatyev@5541 | 551 | "exception on getting method Helper." + name, e); |
iignatyev@4951 | 552 | } |
iignatyev@5541 | 553 | |
iignatyev@4951 | 554 | } |
iignatyev@4951 | 555 | |
iignatyev@4951 | 556 | private static int staticMethod() { |
iignatyev@4951 | 557 | return 1138; |
iignatyev@4951 | 558 | } |
iignatyev@4951 | 559 | |
iignatyev@4951 | 560 | private int method() { |
iignatyev@4951 | 561 | return 42; |
iignatyev@4951 | 562 | } |
iignatyev@4951 | 563 | |
iignatyev@5541 | 564 | private static int osrStaticMethod() { |
iignatyev@5541 | 565 | int result = 0; |
iignatyev@5541 | 566 | for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) { |
iignatyev@5541 | 567 | result += staticMethod(); |
iignatyev@5541 | 568 | } |
iignatyev@5541 | 569 | return result; |
iignatyev@5541 | 570 | } |
iignatyev@5541 | 571 | |
iignatyev@5541 | 572 | private int osrMethod() { |
iignatyev@5541 | 573 | int result = 0; |
iignatyev@5541 | 574 | for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) { |
iignatyev@5541 | 575 | result += method(); |
iignatyev@5541 | 576 | } |
iignatyev@5541 | 577 | return result; |
iignatyev@5541 | 578 | } |
iignatyev@5541 | 579 | |
iignatyev@4951 | 580 | private final int x; |
iignatyev@4951 | 581 | |
iignatyev@5541 | 582 | // for method and OSR method test case |
iignatyev@4951 | 583 | public Helper() { |
iignatyev@4951 | 584 | x = 0; |
iignatyev@4951 | 585 | } |
iignatyev@4951 | 586 | |
iignatyev@5541 | 587 | // for OSR constructor test case |
iignatyev@5541 | 588 | private Helper(Object o) { |
iignatyev@5541 | 589 | int result = 0; |
iignatyev@5541 | 590 | for (long i = 0; i < CompilerWhiteBoxTest.BACKEDGE_THRESHOLD; ++i) { |
iignatyev@5541 | 591 | result += method(); |
iignatyev@5541 | 592 | } |
iignatyev@5541 | 593 | x = result; |
iignatyev@5541 | 594 | } |
iignatyev@5541 | 595 | |
iignatyev@5541 | 596 | // for constructor test case |
iignatyev@4951 | 597 | private Helper(int x) { |
iignatyev@4951 | 598 | this.x = x; |
iignatyev@4951 | 599 | } |
iignatyev@4951 | 600 | |
iignatyev@4951 | 601 | @Override |
iignatyev@4951 | 602 | public int hashCode() { |
iignatyev@4951 | 603 | return x; |
iignatyev@4951 | 604 | } |
iignatyev@4592 | 605 | } |
iignatyev@4592 | 606 | } |