test/compiler/whitebox/CompilerWhiteBoxTest.java

Tue, 07 Aug 2018 11:55:44 -0400

author
dbuck
date
Tue, 07 Aug 2018 11:55:44 -0400
changeset 9479
b4ee249eb1c4
parent 7321
f5f752e74840
child 9572
624a0741915c
child 9689
89dcef434423
permissions
-rw-r--r--

8204966: [TESTBUG] hotspot/test/compiler/whitebox/IsMethodCompilableTest.java test fails with -XX:CompileThreshold=1
Summary: enforce lower bound on number of loop iterations used to trigger OSR
Reviewed-by: kvn, iignatyev

iignatyev@4592 1 /*
dbuck@9479 2 * Copyright (c) 2013, 2018, 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;
thartmann@7318 75 /** invocation count to trigger compilation */
iignatyev@5541 76 protected static final int THRESHOLD;
thartmann@7318 77 /** invocation count to trigger 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;
dbuck@9479 87 BACKEDGE_THRESHOLD = Math.max(10000, COMPILE_THRESHOLD *
dbuck@9479 88 Long.parseLong(getVMOption("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 protected final void checkNotCompiled(int compLevel) {
neliasso@6612 210 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
neliasso@6612 211 throw new RuntimeException(method + " must not be in queue");
neliasso@6612 212 }
neliasso@6612 213 if (WHITE_BOX.getMethodCompilationLevel(method, false) >= compLevel) {
neliasso@6612 214 throw new RuntimeException(method + " comp_level must be >= maxCompLevel");
neliasso@6612 215 }
neliasso@6612 216 if (WHITE_BOX.getMethodCompilationLevel(method, true) >= compLevel) {
neliasso@6612 217 throw new RuntimeException(method + " osr_comp_level must be >= maxCompLevel");
neliasso@6612 218 }
neliasso@6612 219 }
neliasso@6612 220
neliasso@6612 221 /**
iignatyev@4951 222 * Checks, that {@linkplain #method} is not compiled.
iignatyev@4951 223 *
iignatyev@4951 224 * @throws RuntimeException if {@linkplain #method} is in compiler queue or
iignatyev@4951 225 * is compiled, or if {@linkplain #method} has zero
iignatyev@4951 226 * compilation level.
iignatyev@4951 227 */
iignatyev@4951 228 protected final void checkNotCompiled() {
thartmann@7321 229 checkNotCompiled(true);
thartmann@7321 230 checkNotCompiled(false);
thartmann@7318 231 }
thartmann@7318 232
thartmann@7321 233 /**
thartmann@7321 234 * Checks, that {@linkplain #method} is not (OSR-)compiled.
thartmann@7321 235 *
thartmann@7321 236 * @param isOsr Check for OSR compilation if true
thartmann@7321 237 * @throws RuntimeException if {@linkplain #method} is in compiler queue or
thartmann@7321 238 * is compiled, or if {@linkplain #method} has zero
thartmann@7321 239 * compilation level.
thartmann@7321 240 */
thartmann@7321 241 protected final void checkNotCompiled(boolean isOsr) {
thartmann@7321 242 waitBackgroundCompilation();
thartmann@7318 243 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
thartmann@7318 244 throw new RuntimeException(method + " must not be in queue");
thartmann@7318 245 }
thartmann@7321 246 if (WHITE_BOX.isMethodCompiled(method, isOsr)) {
thartmann@7321 247 throw new RuntimeException(method + " must not be " +
thartmann@7321 248 (isOsr ? "osr_" : "") + "compiled");
iignatyev@5541 249 }
thartmann@7321 250 if (WHITE_BOX.getMethodCompilationLevel(method, isOsr) != 0) {
thartmann@7321 251 throw new RuntimeException(method + (isOsr ? " osr_" : " ") +
thartmann@7321 252 "comp_level must be == 0");
iignatyev@5541 253 }
iignatyev@6211 254 }
iignatyev@4592 255
iignatyev@4951 256 /**
iignatyev@4951 257 * Checks, that {@linkplain #method} is compiled.
iignatyev@4951 258 *
iignatyev@4951 259 * @throws RuntimeException if {@linkplain #method} isn't in compiler queue
iignatyev@4951 260 * and isn't compiled, or if {@linkplain #method}
iignatyev@4951 261 * has nonzero compilation level
iignatyev@4951 262 */
iignatyev@4951 263 protected final void checkCompiled() {
iignatyev@4592 264 final long start = System.currentTimeMillis();
iignatyev@4951 265 waitBackgroundCompilation();
iignatyev@4592 266 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
iignatyev@4592 267 System.err.printf("Warning: %s is still in queue after %dms%n",
iignatyev@4592 268 method, System.currentTimeMillis() - start);
iignatyev@4592 269 return;
iignatyev@4592 270 }
iignatyev@6211 271 if (!WHITE_BOX.isMethodCompiled(method, testCase.isOsr())) {
iignatyev@5541 272 throw new RuntimeException(method + " must be "
iignatyev@6211 273 + (testCase.isOsr() ? "osr_" : "") + "compiled");
iignatyev@4592 274 }
iignatyev@6211 275 if (WHITE_BOX.getMethodCompilationLevel(method, testCase.isOsr())
iignatyev@6211 276 == 0) {
iignatyev@5541 277 throw new RuntimeException(method
iignatyev@6211 278 + (testCase.isOsr() ? " osr_" : " ")
iignatyev@5541 279 + "comp_level must be != 0");
iignatyev@4592 280 }
iignatyev@4592 281 }
iignatyev@4592 282
iignatyev@5541 283 protected final void deoptimize() {
iignatyev@6211 284 WHITE_BOX.deoptimizeMethod(method, testCase.isOsr());
iignatyev@6211 285 if (testCase.isOsr()) {
iignatyev@5541 286 WHITE_BOX.deoptimizeMethod(method, false);
iignatyev@5541 287 }
iignatyev@5541 288 }
iignatyev@5541 289
iignatyev@5541 290 protected final int getCompLevel() {
iignatyev@6525 291 NMethod nm = NMethod.get(method, testCase.isOsr());
iignatyev@6525 292 return nm == null ? COMP_LEVEL_NONE : nm.comp_level;
iignatyev@5541 293 }
iignatyev@5541 294
iignatyev@5541 295 protected final boolean isCompilable() {
iignatyev@5541 296 return WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY,
iignatyev@6211 297 testCase.isOsr());
iignatyev@5541 298 }
iignatyev@5541 299
iignatyev@5541 300 protected final boolean isCompilable(int compLevel) {
iignatyev@6211 301 return WHITE_BOX
iignatyev@6211 302 .isMethodCompilable(method, compLevel, testCase.isOsr());
iignatyev@5541 303 }
iignatyev@5541 304
iignatyev@5541 305 protected final void makeNotCompilable() {
iignatyev@5541 306 WHITE_BOX.makeMethodNotCompilable(method, COMP_LEVEL_ANY,
iignatyev@6211 307 testCase.isOsr());
iignatyev@5541 308 }
iignatyev@5541 309
iignatyev@5541 310 protected final void makeNotCompilable(int compLevel) {
iignatyev@6211 311 WHITE_BOX.makeMethodNotCompilable(method, compLevel, testCase.isOsr());
iignatyev@5541 312 }
iignatyev@5541 313
iignatyev@4951 314 /**
iignatyev@4951 315 * Waits for completion of background compilation of {@linkplain #method}.
iignatyev@4951 316 */
iignatyev@4951 317 protected final void waitBackgroundCompilation() {
thartmann@7318 318 waitBackgroundCompilation(method);
thartmann@7318 319 }
thartmann@7318 320
thartmann@7318 321 /**
thartmann@7318 322 * Waits for completion of background compilation of the given executable.
thartmann@7318 323 *
thartmann@7318 324 * @param executable Executable
thartmann@7318 325 */
thartmann@7318 326 protected static final void waitBackgroundCompilation(Executable executable) {
iignatyev@4766 327 if (!BACKGROUND_COMPILATION) {
iignatyev@4766 328 return;
iignatyev@4766 329 }
iignatyev@4592 330 final Object obj = new Object();
iignatyev@4951 331 for (int i = 0; i < 10
thartmann@7318 332 && WHITE_BOX.isMethodQueuedForCompilation(executable); ++i) {
iignatyev@4951 333 synchronized (obj) {
iignatyev@4951 334 try {
iignatyev@4951 335 obj.wait(1000);
iignatyev@4951 336 } catch (InterruptedException e) {
iignatyev@4951 337 Thread.currentThread().interrupt();
iignatyev@4592 338 }
iignatyev@4592 339 }
iignatyev@4592 340 }
iignatyev@4592 341 }
iignatyev@4592 342
iignatyev@4951 343 /**
iignatyev@4951 344 * Prints information about {@linkplain #method}.
iignatyev@4951 345 */
iignatyev@4951 346 protected final void printInfo() {
iignatyev@4592 347 System.out.printf("%n%s:%n", method);
iignatyev@4592 348 System.out.printf("\tcompilable:\t%b%n",
iignatyev@5541 349 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, false));
iignatyev@4592 350 System.out.printf("\tcompiled:\t%b%n",
iignatyev@5541 351 WHITE_BOX.isMethodCompiled(method, false));
iignatyev@4592 352 System.out.printf("\tcomp_level:\t%d%n",
iignatyev@5541 353 WHITE_BOX.getMethodCompilationLevel(method, false));
iignatyev@5541 354 System.out.printf("\tosr_compilable:\t%b%n",
iignatyev@5541 355 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true));
iignatyev@5541 356 System.out.printf("\tosr_compiled:\t%b%n",
iignatyev@5541 357 WHITE_BOX.isMethodCompiled(method, true));
iignatyev@5541 358 System.out.printf("\tosr_comp_level:\t%d%n",
iignatyev@5541 359 WHITE_BOX.getMethodCompilationLevel(method, true));
iignatyev@6211 360 System.out.printf("\tin_queue:\t%b%n",
iignatyev@4592 361 WHITE_BOX.isMethodQueuedForCompilation(method));
iignatyev@4592 362 System.out.printf("compile_queues_size:\t%d%n%n",
iignatyev@4592 363 WHITE_BOX.getCompileQueuesSize());
iignatyev@4592 364 }
iignatyev@4592 365
iignatyev@4951 366 /**
iignatyev@4951 367 * Executes testing.
iignatyev@4951 368 */
iignatyev@4592 369 protected abstract void test() throws Exception;
iignatyev@4592 370
iignatyev@4951 371 /**
iignatyev@4951 372 * Tries to trigger compilation of {@linkplain #method} by call
iignatyev@6211 373 * {@linkplain TestCase#getCallable()} enough times.
iignatyev@4951 374 *
iignatyev@4951 375 * @return accumulated result
iignatyev@4951 376 * @see #compile(int)
iignatyev@4951 377 */
iignatyev@4592 378 protected final int compile() {
iignatyev@6211 379 if (testCase.isOsr()) {
iignatyev@5541 380 return compile(1);
iignatyev@5541 381 } else {
iignatyev@5541 382 return compile(THRESHOLD);
iignatyev@5541 383 }
iignatyev@4908 384 }
iignatyev@4908 385
iignatyev@4951 386 /**
iignatyev@4951 387 * Tries to trigger compilation of {@linkplain #method} by call
iignatyev@6211 388 * {@linkplain TestCase#getCallable()} specified times.
iignatyev@4951 389 *
iignatyev@4951 390 * @param count invocation count
iignatyev@4951 391 * @return accumulated result
iignatyev@4951 392 */
iignatyev@4908 393 protected final int compile(int count) {
iignatyev@4592 394 int result = 0;
iignatyev@4951 395 Integer tmp;
iignatyev@4766 396 for (int i = 0; i < count; ++i) {
iignatyev@4951 397 try {
iignatyev@6211 398 tmp = testCase.getCallable().call();
iignatyev@4951 399 } catch (Exception e) {
iignatyev@4951 400 tmp = null;
iignatyev@4951 401 }
iignatyev@4951 402 result += tmp == null ? 0 : tmp;
iignatyev@4592 403 }
iignatyev@5512 404 if (IS_VERBOSE) {
iignatyev@5512 405 System.out.println("method was invoked " + count + " times");
iignatyev@5512 406 }
iignatyev@4592 407 return result;
iignatyev@4592 408 }
iignatyev@6211 409
iignatyev@6211 410 /**
iignatyev@6211 411 * Utility interface provides tested method and object to invoke it.
iignatyev@6211 412 */
iignatyev@6211 413 public interface TestCase {
iignatyev@6211 414 /** the name of test case */
iignatyev@6211 415 String name();
iignatyev@6211 416
iignatyev@6211 417 /** tested method */
iignatyev@6211 418 Executable getExecutable();
iignatyev@6211 419
iignatyev@6211 420 /** object to invoke {@linkplain #getExecutable()} */
iignatyev@6211 421 Callable<Integer> getCallable();
iignatyev@6211 422
iignatyev@6211 423 /** flag for OSR test case */
iignatyev@6211 424 boolean isOsr();
iignatyev@6211 425 }
iignatyev@6353 426
iignatyev@6353 427 /**
iignatyev@6353 428 * @return {@code true} if the current test case is OSR and the mode is
iignatyev@6353 429 * Xcomp, otherwise {@code false}
iignatyev@6353 430 */
iignatyev@6353 431 protected boolean skipXcompOSR() {
iignatyev@6353 432 boolean result = testCase.isOsr()
iignatyev@6353 433 && CompilerWhiteBoxTest.MODE.startsWith("compiled ");
iignatyev@6353 434 if (result && IS_VERBOSE) {
iignatyev@6353 435 System.err.printf("Warning: %s is not applicable in %s%n",
iignatyev@6353 436 testCase.name(), CompilerWhiteBoxTest.MODE);
iignatyev@6353 437 }
iignatyev@6353 438 return result;
iignatyev@6353 439 }
iignatyev@4951 440 }
iignatyev@4592 441
iignatyev@6211 442 enum SimpleTestCase implements CompilerWhiteBoxTest.TestCase {
iignatyev@4951 443 /** constructor test case */
iignatyev@5541 444 CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE, false),
iignatyev@4951 445 /** method test case */
thartmann@7318 446 METHOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false),
iignatyev@4951 447 /** static method test case */
iignatyev@5541 448 STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE, false),
iignatyev@5541 449 /** OSR constructor test case */
iignatyev@5541 450 OSR_CONSTRUCTOR_TEST(Helper.OSR_CONSTRUCTOR,
iignatyev@5541 451 Helper.OSR_CONSTRUCTOR_CALLABLE, true),
iignatyev@5983 452 /** OSR method test case */
thartmann@7318 453 OSR_METHOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true),
iignatyev@5541 454 /** OSR static method test case */
iignatyev@5541 455 OSR_STATIC_TEST(Helper.OSR_STATIC, Helper.OSR_STATIC_CALLABLE, true);
iignatyev@4951 456
iignatyev@6211 457 private final Executable executable;
iignatyev@6211 458 private final Callable<Integer> callable;
iignatyev@6211 459 private final boolean isOsr;
iignatyev@4951 460
iignatyev@6211 461 private SimpleTestCase(Executable executable, Callable<Integer> callable,
iignatyev@5541 462 boolean isOsr) {
iignatyev@4951 463 this.executable = executable;
iignatyev@4951 464 this.callable = callable;
iignatyev@5541 465 this.isOsr = isOsr;
iignatyev@4951 466 }
iignatyev@4951 467
iignatyev@6211 468 @Override
iignatyev@6211 469 public Executable getExecutable() {
iignatyev@6211 470 return executable;
iignatyev@6211 471 }
iignatyev@6211 472
iignatyev@6211 473 @Override
iignatyev@6211 474 public Callable<Integer> getCallable() {
iignatyev@6211 475 return callable;
iignatyev@6211 476 }
iignatyev@6211 477
iignatyev@6211 478 @Override
iignatyev@6211 479 public boolean isOsr() {
iignatyev@6211 480 return isOsr;
iignatyev@6211 481 }
iignatyev@6211 482
iignatyev@4951 483 private static class Helper {
iignatyev@5541 484
iignatyev@4951 485 private static final Callable<Integer> CONSTRUCTOR_CALLABLE
iignatyev@4951 486 = new Callable<Integer>() {
iignatyev@4951 487 @Override
iignatyev@4951 488 public Integer call() throws Exception {
iignatyev@4951 489 return new Helper(1337).hashCode();
iignatyev@4951 490 }
iignatyev@4951 491 };
iignatyev@4951 492
iignatyev@4951 493 private static final Callable<Integer> METHOD_CALLABLE
iignatyev@4951 494 = new Callable<Integer>() {
iignatyev@4951 495 private final Helper helper = new Helper();
iignatyev@4951 496
iignatyev@4951 497 @Override
iignatyev@4951 498 public Integer call() throws Exception {
iignatyev@4951 499 return helper.method();
iignatyev@4951 500 }
iignatyev@4951 501 };
iignatyev@4951 502
iignatyev@4951 503 private static final Callable<Integer> STATIC_CALLABLE
iignatyev@4951 504 = new Callable<Integer>() {
iignatyev@4951 505 @Override
iignatyev@4951 506 public Integer call() throws Exception {
iignatyev@4951 507 return staticMethod();
iignatyev@4951 508 }
iignatyev@4951 509 };
iignatyev@4951 510
iignatyev@5541 511 private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE
iignatyev@5541 512 = new Callable<Integer>() {
iignatyev@5541 513 @Override
iignatyev@5541 514 public Integer call() throws Exception {
thartmann@7320 515 return new Helper(null, CompilerWhiteBoxTest.BACKEDGE_THRESHOLD).hashCode();
iignatyev@5541 516 }
iignatyev@5541 517 };
iignatyev@5541 518
iignatyev@5541 519 private static final Callable<Integer> OSR_METHOD_CALLABLE
iignatyev@5541 520 = new Callable<Integer>() {
iignatyev@5541 521 private final Helper helper = new Helper();
iignatyev@5541 522
iignatyev@5541 523 @Override
iignatyev@5541 524 public Integer call() throws Exception {
thartmann@7320 525 return helper.osrMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
iignatyev@5541 526 }
iignatyev@5541 527 };
iignatyev@5541 528
iignatyev@5541 529 private static final Callable<Integer> OSR_STATIC_CALLABLE
iignatyev@5541 530 = new Callable<Integer>() {
iignatyev@5541 531 @Override
iignatyev@5541 532 public Integer call() throws Exception {
thartmann@7320 533 return osrStaticMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
iignatyev@5541 534 }
iignatyev@5541 535 };
iignatyev@5541 536
iignatyev@4951 537 private static final Constructor CONSTRUCTOR;
iignatyev@5541 538 private static final Constructor OSR_CONSTRUCTOR;
iignatyev@4951 539 private static final Method METHOD;
iignatyev@4951 540 private static final Method STATIC;
iignatyev@5541 541 private static final Method OSR_METHOD;
iignatyev@5541 542 private static final Method OSR_STATIC;
iignatyev@4951 543
iignatyev@4951 544 static {
iignatyev@4951 545 try {
iignatyev@4951 546 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
iignatyev@4951 547 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 548 throw new RuntimeException(
iignatyev@4951 549 "exception on getting method Helper.<init>(int)", e);
iignatyev@4951 550 }
iignatyev@4951 551 try {
iignatyev@5541 552 OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor(
thartmann@7318 553 Object.class, long.class);
iignatyev@4951 554 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 555 throw new RuntimeException(
thartmann@7318 556 "exception on getting method Helper.<init>(Object, long)", e);
iignatyev@4951 557 }
iignatyev@5541 558 METHOD = getMethod("method");
iignatyev@5541 559 STATIC = getMethod("staticMethod");
thartmann@7318 560 OSR_METHOD = getMethod("osrMethod", long.class);
thartmann@7318 561 OSR_STATIC = getMethod("osrStaticMethod", long.class);
iignatyev@5541 562 }
iignatyev@5541 563
thartmann@7318 564 private static Method getMethod(String name, Class<?>... parameterTypes) {
iignatyev@4951 565 try {
thartmann@7318 566 return Helper.class.getDeclaredMethod(name, parameterTypes);
iignatyev@4951 567 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 568 throw new RuntimeException(
iignatyev@5541 569 "exception on getting method Helper." + name, e);
iignatyev@4951 570 }
iignatyev@4951 571 }
iignatyev@4951 572
iignatyev@4951 573 private static int staticMethod() {
iignatyev@4951 574 return 1138;
iignatyev@4951 575 }
iignatyev@4951 576
iignatyev@4951 577 private int method() {
iignatyev@4951 578 return 42;
iignatyev@4951 579 }
iignatyev@4951 580
thartmann@7320 581 /**
thartmann@7320 582 * Deoptimizes all non-osr versions of the given executable after
thartmann@7320 583 * compilation finished.
thartmann@7320 584 *
thartmann@7320 585 * @param e Executable
thartmann@7320 586 * @throws Exception
thartmann@7320 587 */
thartmann@7320 588 private static void waitAndDeoptimize(Executable e) {
thartmann@7320 589 CompilerWhiteBoxTest.waitBackgroundCompilation(e);
thartmann@7320 590 if (WhiteBox.getWhiteBox().isMethodQueuedForCompilation(e)) {
thartmann@7320 591 throw new RuntimeException(e + " must not be in queue");
thartmann@7320 592 }
thartmann@7320 593 // Deoptimize non-osr versions of executable
thartmann@7320 594 WhiteBox.getWhiteBox().deoptimizeMethod(e, false);
thartmann@7320 595 }
thartmann@7320 596
thartmann@7320 597 /**
thartmann@7320 598 * Executes the method multiple times to make sure we have
thartmann@7320 599 * enough profiling information before triggering an OSR
thartmann@7320 600 * compilation. Otherwise the C2 compiler may add uncommon traps.
thartmann@7320 601 *
thartmann@7320 602 * @param m Method to be executed
thartmann@7320 603 * @return Number of times the method was executed
thartmann@7320 604 * @throws Exception
thartmann@7320 605 */
thartmann@7320 606 private static int warmup(Method m) throws Exception {
thartmann@7320 607 waitAndDeoptimize(m);
thartmann@7320 608 Helper helper = new Helper();
thartmann@7320 609 int result = 0;
thartmann@7320 610 for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
thartmann@7320 611 result += (int)m.invoke(helper, 1);
thartmann@7320 612 }
thartmann@7320 613 // Wait to make sure OSR compilation is not blocked by
thartmann@7320 614 // non-OSR compilation in the compile queue
thartmann@7320 615 CompilerWhiteBoxTest.waitBackgroundCompilation(m);
thartmann@7320 616 return result;
thartmann@7320 617 }
thartmann@7320 618
thartmann@7320 619 /**
thartmann@7320 620 * Executes the constructor multiple times to make sure we
thartmann@7320 621 * have enough profiling information before triggering an OSR
thartmann@7320 622 * compilation. Otherwise the C2 compiler may add uncommon traps.
thartmann@7320 623 *
thartmann@7320 624 * @param c Constructor to be executed
thartmann@7320 625 * @return Number of times the constructor was executed
thartmann@7320 626 * @throws Exception
thartmann@7320 627 */
thartmann@7320 628 private static int warmup(Constructor c) throws Exception {
thartmann@7320 629 waitAndDeoptimize(c);
thartmann@7320 630 int result = 0;
thartmann@7320 631 for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
thartmann@7320 632 result += c.newInstance(null, 1).hashCode();
thartmann@7320 633 }
thartmann@7320 634 // Wait to make sure OSR compilation is not blocked by
thartmann@7320 635 // non-OSR compilation in the compile queue
thartmann@7320 636 CompilerWhiteBoxTest.waitBackgroundCompilation(c);
thartmann@7320 637 return result;
thartmann@7320 638 }
thartmann@7320 639
thartmann@7320 640 private static int osrStaticMethod(long limit) throws Exception {
thartmann@7320 641 int result = 0;
thartmann@7319 642 if (limit != 1) {
thartmann@7320 643 result = warmup(OSR_STATIC);
thartmann@7319 644 }
thartmann@7319 645 // Trigger osr compilation
thartmann@7318 646 for (long i = 0; i < limit; ++i) {
iignatyev@5541 647 result += staticMethod();
iignatyev@5541 648 }
iignatyev@5541 649 return result;
iignatyev@5541 650 }
iignatyev@5541 651
thartmann@7320 652 private int osrMethod(long limit) throws Exception {
thartmann@7320 653 int result = 0;
thartmann@7319 654 if (limit != 1) {
thartmann@7320 655 result = warmup(OSR_METHOD);
thartmann@7319 656 }
thartmann@7319 657 // Trigger osr compilation
thartmann@7318 658 for (long i = 0; i < limit; ++i) {
iignatyev@5541 659 result += method();
iignatyev@5541 660 }
iignatyev@5541 661 return result;
iignatyev@5541 662 }
iignatyev@5541 663
iignatyev@4951 664 private final int x;
iignatyev@4951 665
iignatyev@5541 666 // for method and OSR method test case
iignatyev@4951 667 public Helper() {
iignatyev@4951 668 x = 0;
iignatyev@4951 669 }
iignatyev@4951 670
iignatyev@5541 671 // for OSR constructor test case
thartmann@7320 672 private Helper(Object o, long limit) throws Exception {
thartmann@7320 673 int result = 0;
thartmann@7319 674 if (limit != 1) {
thartmann@7320 675 result = warmup(OSR_CONSTRUCTOR);
thartmann@7319 676 }
thartmann@7319 677 // Trigger osr compilation
thartmann@7318 678 for (long i = 0; i < limit; ++i) {
iignatyev@5541 679 result += method();
iignatyev@5541 680 }
iignatyev@5541 681 x = result;
iignatyev@5541 682 }
iignatyev@5541 683
iignatyev@5541 684 // for constructor test case
iignatyev@4951 685 private Helper(int x) {
iignatyev@4951 686 this.x = x;
iignatyev@4951 687 }
iignatyev@4951 688
iignatyev@4951 689 @Override
iignatyev@4951 690 public int hashCode() {
iignatyev@4951 691 return x;
iignatyev@4951 692 }
iignatyev@4592 693 }
iignatyev@4592 694 }

mercurial