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