test/compiler/whitebox/CompilerWhiteBoxTest.java

Wed, 05 Jun 2019 03:07:31 +0100

author
xliu
date
Wed, 05 Jun 2019 03:07:31 +0100
changeset 9690
61d955db2a5b
parent 9689
89dcef434423
child 9703
2fdf635bcf28
permissions
-rw-r--r--

8222670: pathological case of JIT recompilation and code cache bloat
Summary: Prevent downgraded compilation tasks from recompiling.
Reviewed-by: sgehwolf, thartmann, andrew

iignatyev@4592 1 /*
xliu@9690 2 * Copyright (c) 2013, 2019, 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 */
xliu@9689 44 protected static final int COMP_LEVEL_NONE = 0;
iignatyev@4951 45 /** {@code CompLevel::CompLevel_any}, {@code CompLevel::CompLevel_all} */
xliu@9689 46 protected static final int COMP_LEVEL_ANY = -1;
iignatyev@5032 47 /** {@code CompLevel::CompLevel_simple} -- C1 */
xliu@9689 48 protected static final int COMP_LEVEL_SIMPLE = 1;
iignatyev@5541 49 /** {@code CompLevel::CompLevel_limited_profile} -- C1, invocation & backedge counters */
xliu@9689 50 protected static final int COMP_LEVEL_LIMITED_PROFILE = 2;
iignatyev@5541 51 /** {@code CompLevel::CompLevel_full_profile} -- C1, invocation & backedge counters + mdo */
xliu@9689 52 protected static final int COMP_LEVEL_FULL_PROFILE = 3;
iignatyev@5032 53 /** {@code CompLevel::CompLevel_full_optimization} -- C2 or Shark */
xliu@9689 54 protected static final int COMP_LEVEL_FULL_OPTIMIZATION = 4;
iignatyev@6211 55 /** Maximal value for CompLevel */
xliu@9689 56 protected static final 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();
xliu@9690 331 for (int i = 0; i < 100
thartmann@7318 332 && WHITE_BOX.isMethodQueuedForCompilation(executable); ++i) {
iignatyev@4951 333 synchronized (obj) {
iignatyev@4951 334 try {
xliu@9690 335 obj.wait(100);
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));
xliu@9689 350 boolean isCompiled = WHITE_BOX.isMethodCompiled(method, false);
xliu@9689 351 System.out.printf("\tcompiled:\t%b%n", isCompiled);
xliu@9689 352 if (isCompiled) {
xliu@9689 353 System.out.printf("\tcompile_id:\t%d%n",
xliu@9689 354 NMethod.get(method, false).compile_id);
xliu@9689 355 }
iignatyev@4592 356 System.out.printf("\tcomp_level:\t%d%n",
iignatyev@5541 357 WHITE_BOX.getMethodCompilationLevel(method, false));
iignatyev@5541 358 System.out.printf("\tosr_compilable:\t%b%n",
iignatyev@5541 359 WHITE_BOX.isMethodCompilable(method, COMP_LEVEL_ANY, true));
xliu@9689 360 isCompiled = WHITE_BOX.isMethodCompiled(method, true);
xliu@9689 361 System.out.printf("\tosr_compiled:\t%b%n", isCompiled);
xliu@9689 362 if (isCompiled) {
xliu@9689 363 System.out.printf("\tosr_compile_id:\t%d%n",
xliu@9689 364 NMethod.get(method, true).compile_id);
xliu@9689 365 }
iignatyev@5541 366 System.out.printf("\tosr_comp_level:\t%d%n",
iignatyev@5541 367 WHITE_BOX.getMethodCompilationLevel(method, true));
iignatyev@6211 368 System.out.printf("\tin_queue:\t%b%n",
iignatyev@4592 369 WHITE_BOX.isMethodQueuedForCompilation(method));
iignatyev@4592 370 System.out.printf("compile_queues_size:\t%d%n%n",
iignatyev@4592 371 WHITE_BOX.getCompileQueuesSize());
iignatyev@4592 372 }
iignatyev@4592 373
iignatyev@4951 374 /**
iignatyev@4951 375 * Executes testing.
iignatyev@4951 376 */
iignatyev@4592 377 protected abstract void test() throws Exception;
iignatyev@4592 378
iignatyev@4951 379 /**
iignatyev@4951 380 * Tries to trigger compilation of {@linkplain #method} by call
iignatyev@6211 381 * {@linkplain TestCase#getCallable()} enough times.
iignatyev@4951 382 *
iignatyev@4951 383 * @return accumulated result
iignatyev@4951 384 * @see #compile(int)
iignatyev@4951 385 */
iignatyev@4592 386 protected final int compile() {
iignatyev@6211 387 if (testCase.isOsr()) {
iignatyev@5541 388 return compile(1);
iignatyev@5541 389 } else {
iignatyev@5541 390 return compile(THRESHOLD);
iignatyev@5541 391 }
iignatyev@4908 392 }
iignatyev@4908 393
iignatyev@4951 394 /**
iignatyev@4951 395 * Tries to trigger compilation of {@linkplain #method} by call
iignatyev@6211 396 * {@linkplain TestCase#getCallable()} specified times.
iignatyev@4951 397 *
iignatyev@4951 398 * @param count invocation count
iignatyev@4951 399 * @return accumulated result
iignatyev@4951 400 */
iignatyev@4908 401 protected final int compile(int count) {
iignatyev@4592 402 int result = 0;
iignatyev@4951 403 Integer tmp;
iignatyev@4766 404 for (int i = 0; i < count; ++i) {
iignatyev@4951 405 try {
iignatyev@6211 406 tmp = testCase.getCallable().call();
iignatyev@4951 407 } catch (Exception e) {
iignatyev@4951 408 tmp = null;
iignatyev@4951 409 }
iignatyev@4951 410 result += tmp == null ? 0 : tmp;
iignatyev@4592 411 }
iignatyev@5512 412 if (IS_VERBOSE) {
iignatyev@5512 413 System.out.println("method was invoked " + count + " times");
iignatyev@5512 414 }
iignatyev@4592 415 return result;
iignatyev@4592 416 }
iignatyev@6211 417
iignatyev@6211 418 /**
iignatyev@6211 419 * Utility interface provides tested method and object to invoke it.
iignatyev@6211 420 */
iignatyev@6211 421 public interface TestCase {
iignatyev@6211 422 /** the name of test case */
iignatyev@6211 423 String name();
iignatyev@6211 424
iignatyev@6211 425 /** tested method */
iignatyev@6211 426 Executable getExecutable();
iignatyev@6211 427
iignatyev@6211 428 /** object to invoke {@linkplain #getExecutable()} */
iignatyev@6211 429 Callable<Integer> getCallable();
iignatyev@6211 430
iignatyev@6211 431 /** flag for OSR test case */
iignatyev@6211 432 boolean isOsr();
iignatyev@6211 433 }
iignatyev@6353 434
iignatyev@6353 435 /**
iignatyev@6353 436 * @return {@code true} if the current test case is OSR and the mode is
iignatyev@6353 437 * Xcomp, otherwise {@code false}
iignatyev@6353 438 */
iignatyev@6353 439 protected boolean skipXcompOSR() {
iignatyev@6353 440 boolean result = testCase.isOsr()
iignatyev@6353 441 && CompilerWhiteBoxTest.MODE.startsWith("compiled ");
iignatyev@6353 442 if (result && IS_VERBOSE) {
iignatyev@6353 443 System.err.printf("Warning: %s is not applicable in %s%n",
iignatyev@6353 444 testCase.name(), CompilerWhiteBoxTest.MODE);
iignatyev@6353 445 }
iignatyev@6353 446 return result;
iignatyev@6353 447 }
xliu@9689 448
xliu@9689 449 /**
xliu@9689 450 * Skip the test for the specified value of Tiered Compilation
xliu@9689 451 * @param value of TieredCompilation the test should not run with
xliu@9689 452 * @return {@code true} if the test should be skipped,
xliu@9689 453 * {@code false} otherwise
xliu@9689 454 */
xliu@9689 455 protected static boolean skipOnTieredCompilation(boolean value) {
xliu@9689 456 if (value == CompilerWhiteBoxTest.TIERED_COMPILATION) {
xliu@9689 457 System.err.println("Test isn't applicable w/ "
xliu@9689 458 + (value ? "enabled" : "disabled")
xliu@9689 459 + "TieredCompilation. Skip test.");
xliu@9689 460 return true;
xliu@9689 461 }
xliu@9689 462 return false;
xliu@9689 463 }
iignatyev@4951 464 }
iignatyev@4592 465
iignatyev@6211 466 enum SimpleTestCase implements CompilerWhiteBoxTest.TestCase {
iignatyev@4951 467 /** constructor test case */
iignatyev@5541 468 CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE, false),
iignatyev@4951 469 /** method test case */
thartmann@7318 470 METHOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE, false),
iignatyev@4951 471 /** static method test case */
iignatyev@5541 472 STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE, false),
iignatyev@5541 473 /** OSR constructor test case */
iignatyev@5541 474 OSR_CONSTRUCTOR_TEST(Helper.OSR_CONSTRUCTOR,
iignatyev@5541 475 Helper.OSR_CONSTRUCTOR_CALLABLE, true),
iignatyev@5983 476 /** OSR method test case */
thartmann@7318 477 OSR_METHOD_TEST(Helper.OSR_METHOD, Helper.OSR_METHOD_CALLABLE, true),
iignatyev@5541 478 /** OSR static method test case */
iignatyev@5541 479 OSR_STATIC_TEST(Helper.OSR_STATIC, Helper.OSR_STATIC_CALLABLE, true);
iignatyev@4951 480
iignatyev@6211 481 private final Executable executable;
iignatyev@6211 482 private final Callable<Integer> callable;
iignatyev@6211 483 private final boolean isOsr;
iignatyev@4951 484
iignatyev@6211 485 private SimpleTestCase(Executable executable, Callable<Integer> callable,
iignatyev@5541 486 boolean isOsr) {
iignatyev@4951 487 this.executable = executable;
iignatyev@4951 488 this.callable = callable;
iignatyev@5541 489 this.isOsr = isOsr;
iignatyev@4951 490 }
iignatyev@4951 491
iignatyev@6211 492 @Override
iignatyev@6211 493 public Executable getExecutable() {
iignatyev@6211 494 return executable;
iignatyev@6211 495 }
iignatyev@6211 496
iignatyev@6211 497 @Override
iignatyev@6211 498 public Callable<Integer> getCallable() {
iignatyev@6211 499 return callable;
iignatyev@6211 500 }
iignatyev@6211 501
iignatyev@6211 502 @Override
iignatyev@6211 503 public boolean isOsr() {
iignatyev@6211 504 return isOsr;
iignatyev@6211 505 }
iignatyev@6211 506
iignatyev@4951 507 private static class Helper {
iignatyev@5541 508
iignatyev@4951 509 private static final Callable<Integer> CONSTRUCTOR_CALLABLE
iignatyev@4951 510 = new Callable<Integer>() {
iignatyev@4951 511 @Override
iignatyev@4951 512 public Integer call() throws Exception {
iignatyev@4951 513 return new Helper(1337).hashCode();
iignatyev@4951 514 }
iignatyev@4951 515 };
iignatyev@4951 516
iignatyev@4951 517 private static final Callable<Integer> METHOD_CALLABLE
iignatyev@4951 518 = new Callable<Integer>() {
iignatyev@4951 519 private final Helper helper = new Helper();
iignatyev@4951 520
iignatyev@4951 521 @Override
iignatyev@4951 522 public Integer call() throws Exception {
iignatyev@4951 523 return helper.method();
iignatyev@4951 524 }
iignatyev@4951 525 };
iignatyev@4951 526
iignatyev@4951 527 private static final Callable<Integer> STATIC_CALLABLE
iignatyev@4951 528 = new Callable<Integer>() {
iignatyev@4951 529 @Override
iignatyev@4951 530 public Integer call() throws Exception {
iignatyev@4951 531 return staticMethod();
iignatyev@4951 532 }
iignatyev@4951 533 };
iignatyev@4951 534
iignatyev@5541 535 private static final Callable<Integer> OSR_CONSTRUCTOR_CALLABLE
iignatyev@5541 536 = new Callable<Integer>() {
iignatyev@5541 537 @Override
iignatyev@5541 538 public Integer call() throws Exception {
thartmann@7320 539 return new Helper(null, CompilerWhiteBoxTest.BACKEDGE_THRESHOLD).hashCode();
iignatyev@5541 540 }
iignatyev@5541 541 };
iignatyev@5541 542
iignatyev@5541 543 private static final Callable<Integer> OSR_METHOD_CALLABLE
iignatyev@5541 544 = new Callable<Integer>() {
iignatyev@5541 545 private final Helper helper = new Helper();
iignatyev@5541 546
iignatyev@5541 547 @Override
iignatyev@5541 548 public Integer call() throws Exception {
thartmann@7320 549 return helper.osrMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
iignatyev@5541 550 }
iignatyev@5541 551 };
iignatyev@5541 552
iignatyev@5541 553 private static final Callable<Integer> OSR_STATIC_CALLABLE
iignatyev@5541 554 = new Callable<Integer>() {
iignatyev@5541 555 @Override
iignatyev@5541 556 public Integer call() throws Exception {
thartmann@7320 557 return osrStaticMethod(CompilerWhiteBoxTest.BACKEDGE_THRESHOLD);
iignatyev@5541 558 }
iignatyev@5541 559 };
iignatyev@5541 560
iignatyev@4951 561 private static final Constructor CONSTRUCTOR;
iignatyev@5541 562 private static final Constructor OSR_CONSTRUCTOR;
iignatyev@4951 563 private static final Method METHOD;
iignatyev@4951 564 private static final Method STATIC;
iignatyev@5541 565 private static final Method OSR_METHOD;
iignatyev@5541 566 private static final Method OSR_STATIC;
iignatyev@4951 567
iignatyev@4951 568 static {
iignatyev@4951 569 try {
iignatyev@4951 570 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
iignatyev@4951 571 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 572 throw new RuntimeException(
iignatyev@4951 573 "exception on getting method Helper.<init>(int)", e);
iignatyev@4951 574 }
iignatyev@4951 575 try {
iignatyev@5541 576 OSR_CONSTRUCTOR = Helper.class.getDeclaredConstructor(
thartmann@7318 577 Object.class, long.class);
iignatyev@4951 578 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 579 throw new RuntimeException(
thartmann@7318 580 "exception on getting method Helper.<init>(Object, long)", e);
iignatyev@4951 581 }
iignatyev@5541 582 METHOD = getMethod("method");
iignatyev@5541 583 STATIC = getMethod("staticMethod");
thartmann@7318 584 OSR_METHOD = getMethod("osrMethod", long.class);
thartmann@7318 585 OSR_STATIC = getMethod("osrStaticMethod", long.class);
iignatyev@5541 586 }
iignatyev@5541 587
thartmann@7318 588 private static Method getMethod(String name, Class<?>... parameterTypes) {
iignatyev@4951 589 try {
thartmann@7318 590 return Helper.class.getDeclaredMethod(name, parameterTypes);
iignatyev@4951 591 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 592 throw new RuntimeException(
iignatyev@5541 593 "exception on getting method Helper." + name, e);
iignatyev@4951 594 }
iignatyev@4951 595 }
iignatyev@4951 596
iignatyev@4951 597 private static int staticMethod() {
iignatyev@4951 598 return 1138;
iignatyev@4951 599 }
iignatyev@4951 600
iignatyev@4951 601 private int method() {
iignatyev@4951 602 return 42;
iignatyev@4951 603 }
iignatyev@4951 604
thartmann@7320 605 /**
thartmann@7320 606 * Deoptimizes all non-osr versions of the given executable after
thartmann@7320 607 * compilation finished.
thartmann@7320 608 *
thartmann@7320 609 * @param e Executable
thartmann@7320 610 * @throws Exception
thartmann@7320 611 */
thartmann@7320 612 private static void waitAndDeoptimize(Executable e) {
thartmann@7320 613 CompilerWhiteBoxTest.waitBackgroundCompilation(e);
thartmann@7320 614 if (WhiteBox.getWhiteBox().isMethodQueuedForCompilation(e)) {
thartmann@7320 615 throw new RuntimeException(e + " must not be in queue");
thartmann@7320 616 }
thartmann@7320 617 // Deoptimize non-osr versions of executable
thartmann@7320 618 WhiteBox.getWhiteBox().deoptimizeMethod(e, false);
thartmann@7320 619 }
thartmann@7320 620
thartmann@7320 621 /**
thartmann@7320 622 * Executes the method multiple times to make sure we have
thartmann@7320 623 * enough profiling information before triggering an OSR
thartmann@7320 624 * compilation. Otherwise the C2 compiler may add uncommon traps.
thartmann@7320 625 *
thartmann@7320 626 * @param m Method to be executed
thartmann@7320 627 * @return Number of times the method was executed
thartmann@7320 628 * @throws Exception
thartmann@7320 629 */
thartmann@7320 630 private static int warmup(Method m) throws Exception {
thartmann@7320 631 waitAndDeoptimize(m);
thartmann@7320 632 Helper helper = new Helper();
thartmann@7320 633 int result = 0;
thartmann@7320 634 for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
thartmann@7320 635 result += (int)m.invoke(helper, 1);
thartmann@7320 636 }
thartmann@7320 637 // Wait to make sure OSR compilation is not blocked by
thartmann@7320 638 // non-OSR compilation in the compile queue
thartmann@7320 639 CompilerWhiteBoxTest.waitBackgroundCompilation(m);
thartmann@7320 640 return result;
thartmann@7320 641 }
thartmann@7320 642
thartmann@7320 643 /**
thartmann@7320 644 * Executes the constructor multiple times to make sure we
thartmann@7320 645 * have enough profiling information before triggering an OSR
thartmann@7320 646 * compilation. Otherwise the C2 compiler may add uncommon traps.
thartmann@7320 647 *
thartmann@7320 648 * @param c Constructor to be executed
thartmann@7320 649 * @return Number of times the constructor was executed
thartmann@7320 650 * @throws Exception
thartmann@7320 651 */
thartmann@7320 652 private static int warmup(Constructor c) throws Exception {
thartmann@7320 653 waitAndDeoptimize(c);
thartmann@7320 654 int result = 0;
thartmann@7320 655 for (long i = 0; i < CompilerWhiteBoxTest.THRESHOLD; ++i) {
thartmann@7320 656 result += c.newInstance(null, 1).hashCode();
thartmann@7320 657 }
thartmann@7320 658 // Wait to make sure OSR compilation is not blocked by
thartmann@7320 659 // non-OSR compilation in the compile queue
thartmann@7320 660 CompilerWhiteBoxTest.waitBackgroundCompilation(c);
thartmann@7320 661 return result;
thartmann@7320 662 }
thartmann@7320 663
thartmann@7320 664 private static int osrStaticMethod(long limit) throws Exception {
thartmann@7320 665 int result = 0;
thartmann@7319 666 if (limit != 1) {
thartmann@7320 667 result = warmup(OSR_STATIC);
thartmann@7319 668 }
thartmann@7319 669 // Trigger osr compilation
thartmann@7318 670 for (long i = 0; i < limit; ++i) {
iignatyev@5541 671 result += staticMethod();
iignatyev@5541 672 }
iignatyev@5541 673 return result;
iignatyev@5541 674 }
iignatyev@5541 675
thartmann@7320 676 private int osrMethod(long limit) throws Exception {
thartmann@7320 677 int result = 0;
thartmann@7319 678 if (limit != 1) {
thartmann@7320 679 result = warmup(OSR_METHOD);
thartmann@7319 680 }
thartmann@7319 681 // Trigger osr compilation
thartmann@7318 682 for (long i = 0; i < limit; ++i) {
iignatyev@5541 683 result += method();
iignatyev@5541 684 }
iignatyev@5541 685 return result;
iignatyev@5541 686 }
iignatyev@5541 687
iignatyev@4951 688 private final int x;
iignatyev@4951 689
iignatyev@5541 690 // for method and OSR method test case
iignatyev@4951 691 public Helper() {
iignatyev@4951 692 x = 0;
iignatyev@4951 693 }
iignatyev@4951 694
iignatyev@5541 695 // for OSR constructor test case
thartmann@7320 696 private Helper(Object o, long limit) throws Exception {
thartmann@7320 697 int result = 0;
thartmann@7319 698 if (limit != 1) {
thartmann@7320 699 result = warmup(OSR_CONSTRUCTOR);
thartmann@7319 700 }
thartmann@7319 701 // Trigger osr compilation
thartmann@7318 702 for (long i = 0; i < limit; ++i) {
iignatyev@5541 703 result += method();
iignatyev@5541 704 }
iignatyev@5541 705 x = result;
iignatyev@5541 706 }
iignatyev@5541 707
iignatyev@5541 708 // for constructor test case
iignatyev@4951 709 private Helper(int x) {
iignatyev@4951 710 this.x = x;
iignatyev@4951 711 }
iignatyev@4951 712
iignatyev@4951 713 @Override
iignatyev@4951 714 public int hashCode() {
iignatyev@4951 715 return x;
iignatyev@4951 716 }
iignatyev@4592 717 }
iignatyev@4592 718 }

mercurial