test/compiler/whitebox/CompilerWhiteBoxTest.java

Fri, 26 Apr 2013 07:21:41 -0700

author
iignatyev
date
Fri, 26 Apr 2013 07:21:41 -0700
changeset 5032
d1c9384eecb4
parent 4951
4b2eebe03f93
child 5512
11237ee74aae
permissions
-rw-r--r--

8012322: Tiered: CompilationPolicy::can_be_compiled(CompLevel_all) mistakenly return false
Reviewed-by: kvn, vlivanov

iignatyev@4592 1 /*
iignatyev@4592 2 * Copyright (c) 2013, 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@4592 27 import sun.management.ManagementFactoryHelper;
iignatyev@4592 28
iignatyev@4951 29 import java.lang.reflect.Constructor;
iignatyev@4951 30 import java.lang.reflect.Executable;
iignatyev@4592 31 import java.lang.reflect.Method;
iignatyev@4951 32 import java.util.Objects;
iignatyev@4951 33 import java.util.concurrent.Callable;
iignatyev@4592 34
iignatyev@4951 35 /**
iignatyev@4951 36 * Abstract class for WhiteBox testing of JIT.
iignatyev@4951 37 *
iignatyev@4592 38 * @author igor.ignatyev@oracle.com
iignatyev@4592 39 */
iignatyev@4592 40 public abstract class CompilerWhiteBoxTest {
iignatyev@4951 41 /** {@code CompLevel::CompLevel_none} -- Interpreter */
iignatyev@4951 42 protected static int COMP_LEVEL_NONE = 0;
iignatyev@4951 43 /** {@code CompLevel::CompLevel_any}, {@code CompLevel::CompLevel_all} */
iignatyev@4951 44 protected static int COMP_LEVEL_ANY = -1;
iignatyev@5032 45 /** {@code CompLevel::CompLevel_simple} -- C1 */
iignatyev@5032 46 protected static int COMP_LEVEL_SIMPLE = 1;
iignatyev@5032 47 /** {@code CompLevel::CompLevel_full_optimization} -- C2 or Shark */
iignatyev@5032 48 protected static int COMP_LEVEL_FULL_OPTIMIZATION = 4;
iignatyev@5032 49
iignatyev@4951 50 /** Instance of WhiteBox */
iignatyev@4592 51 protected static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
iignatyev@4951 52 /** Value of {@code -XX:CompileThreshold} */
iignatyev@4592 53 protected static final int COMPILE_THRESHOLD
iignatyev@4592 54 = Integer.parseInt(getVMOption("CompileThreshold", "10000"));
iignatyev@4951 55 /** Value of {@code -XX:BackgroundCompilation} */
iignatyev@4766 56 protected static final boolean BACKGROUND_COMPILATION
iignatyev@4766 57 = Boolean.valueOf(getVMOption("BackgroundCompilation", "true"));
iignatyev@4951 58 /** Value of {@code -XX:TieredCompilation} */
iignatyev@4908 59 protected static final boolean TIERED_COMPILATION
iignatyev@4908 60 = Boolean.valueOf(getVMOption("TieredCompilation", "false"));
iignatyev@4951 61 /** Value of {@code -XX:TieredStopAtLevel} */
iignatyev@4951 62 protected static final int TIERED_STOP_AT_LEVEL
iignatyev@4951 63 = Integer.parseInt(getVMOption("TieredStopAtLevel", "0"));
iignatyev@4592 64
iignatyev@4951 65 /**
iignatyev@4951 66 * Returns value of VM option.
iignatyev@4951 67 *
iignatyev@4951 68 * @param name option's name
iignatyev@4951 69 * @return value of option or {@code null}, if option doesn't exist
iignatyev@4951 70 * @throws NullPointerException if name is null
iignatyev@4951 71 */
iignatyev@4951 72 protected static String getVMOption(String name) {
iignatyev@4951 73 Objects.requireNonNull(name);
iignatyev@4951 74 HotSpotDiagnosticMXBean diagnostic
iignatyev@4951 75 = ManagementFactoryHelper.getDiagnosticMXBean();
iignatyev@4951 76 VMOption tmp;
iignatyev@4592 77 try {
iignatyev@4951 78 tmp = diagnostic.getVMOption(name);
iignatyev@4951 79 } catch (IllegalArgumentException e) {
iignatyev@4951 80 tmp = null;
iignatyev@4592 81 }
iignatyev@4951 82 return (tmp == null ? null : tmp.getValue());
iignatyev@4592 83 }
iignatyev@4592 84
iignatyev@4951 85 /**
iignatyev@4951 86 * Returns value of VM option or default value.
iignatyev@4951 87 *
iignatyev@4951 88 * @param name option's name
iignatyev@4951 89 * @param defaultValue default value
iignatyev@4951 90 * @return value of option or {@code defaultValue}, if option doesn't exist
iignatyev@4951 91 * @throws NullPointerException if name is null
iignatyev@4951 92 * @see #getVMOption(String)
iignatyev@4951 93 */
iignatyev@4766 94 protected static String getVMOption(String name, String defaultValue) {
iignatyev@4766 95 String result = getVMOption(name);
iignatyev@4592 96 return result == null ? defaultValue : result;
iignatyev@4592 97 }
iignatyev@4592 98
iignatyev@5032 99 /** copy of is_c1_compile(int) from utilities/globalDefinitions.hpp */
iignatyev@5032 100 protected static boolean isC1Compile(int compLevel) {
iignatyev@5032 101 return (compLevel > COMP_LEVEL_NONE)
iignatyev@5032 102 && (compLevel < COMP_LEVEL_FULL_OPTIMIZATION);
iignatyev@5032 103 }
iignatyev@5032 104
iignatyev@5032 105 /** copy of is_c2_compile(int) from utilities/globalDefinitions.hpp */
iignatyev@5032 106 protected static boolean isC2Compile(int compLevel) {
iignatyev@5032 107 return compLevel == COMP_LEVEL_FULL_OPTIMIZATION;
iignatyev@5032 108 }
iignatyev@5032 109
iignatyev@4951 110 /** tested method */
iignatyev@4951 111 protected final Executable method;
iignatyev@4951 112 private final Callable<Integer> callable;
iignatyev@4951 113
iignatyev@4951 114 /**
iignatyev@4951 115 * Constructor.
iignatyev@4951 116 *
iignatyev@4951 117 * @param testCase object, that contains tested method and way to invoke it.
iignatyev@4951 118 */
iignatyev@4951 119 protected CompilerWhiteBoxTest(TestCase testCase) {
iignatyev@4951 120 Objects.requireNonNull(testCase);
iignatyev@4951 121 System.out.println("TEST CASE:" + testCase.name());
iignatyev@4951 122 method = testCase.executable;
iignatyev@4951 123 callable = testCase.callable;
iignatyev@4951 124 }
iignatyev@4951 125
iignatyev@4951 126 /**
iignatyev@4951 127 * Template method for testing. Prints tested method's info before
iignatyev@4951 128 * {@linkplain #test()} and after {@linkplain #test()} or on thrown
iignatyev@4951 129 * exception.
iignatyev@4951 130 *
iignatyev@4951 131 * @throws RuntimeException if method {@linkplain #test()} throws any
iignatyev@4951 132 * exception
iignatyev@4951 133 * @see #test()
iignatyev@4951 134 */
iignatyev@4951 135 protected final void runTest() {
iignatyev@4592 136 if (ManagementFactoryHelper.getCompilationMXBean() == null) {
iignatyev@4592 137 System.err.println(
iignatyev@4592 138 "Warning: test is not applicable in interpreted mode");
iignatyev@4592 139 return;
iignatyev@4592 140 }
iignatyev@4592 141 System.out.println("at test's start:");
iignatyev@4951 142 printInfo();
iignatyev@4592 143 try {
iignatyev@4592 144 test();
iignatyev@4592 145 } catch (Exception e) {
iignatyev@4592 146 System.out.printf("on exception '%s':", e.getMessage());
iignatyev@4951 147 printInfo();
iignatyev@4766 148 e.printStackTrace();
iignatyev@4951 149 if (e instanceof RuntimeException) {
iignatyev@4951 150 throw (RuntimeException) e;
iignatyev@4951 151 }
iignatyev@4592 152 throw new RuntimeException(e);
iignatyev@4592 153 }
iignatyev@4592 154 System.out.println("at test's end:");
iignatyev@4951 155 printInfo();
iignatyev@4592 156 }
iignatyev@4592 157
iignatyev@4951 158 /**
iignatyev@4951 159 * Checks, that {@linkplain #method} is not compiled.
iignatyev@4951 160 *
iignatyev@4951 161 * @throws RuntimeException if {@linkplain #method} is in compiler queue or
iignatyev@4951 162 * is compiled, or if {@linkplain #method} has zero
iignatyev@4951 163 * compilation level.
iignatyev@4951 164 */
iignatyev@4951 165 protected final void checkNotCompiled() {
iignatyev@4908 166 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
iignatyev@4908 167 throw new RuntimeException(method + " must not be in queue");
iignatyev@4908 168 }
iignatyev@4592 169 if (WHITE_BOX.isMethodCompiled(method)) {
iignatyev@4592 170 throw new RuntimeException(method + " must be not compiled");
iignatyev@4592 171 }
iignatyev@4592 172 if (WHITE_BOX.getMethodCompilationLevel(method) != 0) {
iignatyev@4592 173 throw new RuntimeException(method + " comp_level must be == 0");
iignatyev@4592 174 }
iignatyev@4592 175 }
iignatyev@4592 176
iignatyev@4951 177 /**
iignatyev@4951 178 * Checks, that {@linkplain #method} is compiled.
iignatyev@4951 179 *
iignatyev@4951 180 * @throws RuntimeException if {@linkplain #method} isn't in compiler queue
iignatyev@4951 181 * and isn't compiled, or if {@linkplain #method}
iignatyev@4951 182 * has nonzero compilation level
iignatyev@4951 183 */
iignatyev@4951 184 protected final void checkCompiled() {
iignatyev@4592 185 final long start = System.currentTimeMillis();
iignatyev@4951 186 waitBackgroundCompilation();
iignatyev@4592 187 if (WHITE_BOX.isMethodQueuedForCompilation(method)) {
iignatyev@4592 188 System.err.printf("Warning: %s is still in queue after %dms%n",
iignatyev@4592 189 method, System.currentTimeMillis() - start);
iignatyev@4592 190 return;
iignatyev@4592 191 }
iignatyev@4592 192 if (!WHITE_BOX.isMethodCompiled(method)) {
iignatyev@4592 193 throw new RuntimeException(method + " must be compiled");
iignatyev@4592 194 }
iignatyev@4592 195 if (WHITE_BOX.getMethodCompilationLevel(method) == 0) {
iignatyev@4592 196 throw new RuntimeException(method + " comp_level must be != 0");
iignatyev@4592 197 }
iignatyev@4592 198 }
iignatyev@4592 199
iignatyev@4951 200 /**
iignatyev@4951 201 * Waits for completion of background compilation of {@linkplain #method}.
iignatyev@4951 202 */
iignatyev@4951 203 protected final void waitBackgroundCompilation() {
iignatyev@4766 204 if (!BACKGROUND_COMPILATION) {
iignatyev@4766 205 return;
iignatyev@4766 206 }
iignatyev@4592 207 final Object obj = new Object();
iignatyev@4951 208 for (int i = 0; i < 10
iignatyev@4951 209 && WHITE_BOX.isMethodQueuedForCompilation(method); ++i) {
iignatyev@4951 210 synchronized (obj) {
iignatyev@4951 211 try {
iignatyev@4951 212 obj.wait(1000);
iignatyev@4951 213 } catch (InterruptedException e) {
iignatyev@4951 214 Thread.currentThread().interrupt();
iignatyev@4592 215 }
iignatyev@4592 216 }
iignatyev@4592 217 }
iignatyev@4592 218 }
iignatyev@4592 219
iignatyev@4951 220 /**
iignatyev@4951 221 * Prints information about {@linkplain #method}.
iignatyev@4951 222 */
iignatyev@4951 223 protected final void printInfo() {
iignatyev@4592 224 System.out.printf("%n%s:%n", method);
iignatyev@4592 225 System.out.printf("\tcompilable:\t%b%n",
iignatyev@4592 226 WHITE_BOX.isMethodCompilable(method));
iignatyev@4592 227 System.out.printf("\tcompiled:\t%b%n",
iignatyev@4592 228 WHITE_BOX.isMethodCompiled(method));
iignatyev@4592 229 System.out.printf("\tcomp_level:\t%d%n",
iignatyev@4592 230 WHITE_BOX.getMethodCompilationLevel(method));
iignatyev@4592 231 System.out.printf("\tin_queue:\t%b%n",
iignatyev@4592 232 WHITE_BOX.isMethodQueuedForCompilation(method));
iignatyev@4592 233 System.out.printf("compile_queues_size:\t%d%n%n",
iignatyev@4592 234 WHITE_BOX.getCompileQueuesSize());
iignatyev@4592 235 }
iignatyev@4592 236
iignatyev@4951 237 /**
iignatyev@4951 238 * Executes testing.
iignatyev@4951 239 */
iignatyev@4592 240 protected abstract void test() throws Exception;
iignatyev@4592 241
iignatyev@4951 242 /**
iignatyev@4951 243 * Tries to trigger compilation of {@linkplain #method} by call
iignatyev@4951 244 * {@linkplain #callable} enough times.
iignatyev@4951 245 *
iignatyev@4951 246 * @return accumulated result
iignatyev@4951 247 * @see #compile(int)
iignatyev@4951 248 */
iignatyev@4592 249 protected final int compile() {
iignatyev@4908 250 return compile(Math.max(COMPILE_THRESHOLD, 150000));
iignatyev@4908 251 }
iignatyev@4908 252
iignatyev@4951 253 /**
iignatyev@4951 254 * Tries to trigger compilation of {@linkplain #method} by call
iignatyev@4951 255 * {@linkplain #callable} specified times.
iignatyev@4951 256 *
iignatyev@4951 257 * @param count invocation count
iignatyev@4951 258 * @return accumulated result
iignatyev@4951 259 */
iignatyev@4908 260 protected final int compile(int count) {
iignatyev@4592 261 int result = 0;
iignatyev@4951 262 Integer tmp;
iignatyev@4766 263 for (int i = 0; i < count; ++i) {
iignatyev@4951 264 try {
iignatyev@4951 265 tmp = callable.call();
iignatyev@4951 266 } catch (Exception e) {
iignatyev@4951 267 tmp = null;
iignatyev@4951 268 }
iignatyev@4951 269 result += tmp == null ? 0 : tmp;
iignatyev@4592 270 }
iignatyev@4766 271 System.out.println("method was invoked " + count + " times");
iignatyev@4592 272 return result;
iignatyev@4592 273 }
iignatyev@4951 274 }
iignatyev@4592 275
iignatyev@4951 276 /**
iignatyev@4951 277 * Utility structure containing tested method and object to invoke it.
iignatyev@4951 278 */
iignatyev@4951 279 enum TestCase {
iignatyev@4951 280 /** constructor test case */
iignatyev@4951 281 CONSTRUCTOR_TEST(Helper.CONSTRUCTOR, Helper.CONSTRUCTOR_CALLABLE),
iignatyev@4951 282 /** method test case */
iignatyev@4951 283 METOD_TEST(Helper.METHOD, Helper.METHOD_CALLABLE),
iignatyev@4951 284 /** static method test case */
iignatyev@4951 285 STATIC_TEST(Helper.STATIC, Helper.STATIC_CALLABLE);
iignatyev@4951 286
iignatyev@4951 287 /** tested method */
iignatyev@4951 288 final Executable executable;
iignatyev@4951 289 /** object to invoke {@linkplain #executable} */
iignatyev@4951 290 final Callable<Integer> callable;
iignatyev@4951 291
iignatyev@4951 292 private TestCase(Executable executable, Callable<Integer> callable) {
iignatyev@4951 293 this.executable = executable;
iignatyev@4951 294 this.callable = callable;
iignatyev@4951 295 }
iignatyev@4951 296
iignatyev@4951 297 private static class Helper {
iignatyev@4951 298 private static final Callable<Integer> CONSTRUCTOR_CALLABLE
iignatyev@4951 299 = new Callable<Integer>() {
iignatyev@4951 300 @Override
iignatyev@4951 301 public Integer call() throws Exception {
iignatyev@4951 302 return new Helper(1337).hashCode();
iignatyev@4951 303 }
iignatyev@4951 304 };
iignatyev@4951 305
iignatyev@4951 306 private static final Callable<Integer> METHOD_CALLABLE
iignatyev@4951 307 = new Callable<Integer>() {
iignatyev@4951 308 private final Helper helper = new Helper();
iignatyev@4951 309
iignatyev@4951 310 @Override
iignatyev@4951 311 public Integer call() throws Exception {
iignatyev@4951 312 return helper.method();
iignatyev@4951 313 }
iignatyev@4951 314 };
iignatyev@4951 315
iignatyev@4951 316 private static final Callable<Integer> STATIC_CALLABLE
iignatyev@4951 317 = new Callable<Integer>() {
iignatyev@4951 318 @Override
iignatyev@4951 319 public Integer call() throws Exception {
iignatyev@4951 320 return staticMethod();
iignatyev@4951 321 }
iignatyev@4951 322 };
iignatyev@4951 323
iignatyev@4951 324 private static final Constructor CONSTRUCTOR;
iignatyev@4951 325 private static final Method METHOD;
iignatyev@4951 326 private static final Method STATIC;
iignatyev@4951 327
iignatyev@4951 328 static {
iignatyev@4951 329 try {
iignatyev@4951 330 CONSTRUCTOR = Helper.class.getDeclaredConstructor(int.class);
iignatyev@4951 331 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 332 throw new RuntimeException(
iignatyev@4951 333 "exception on getting method Helper.<init>(int)", e);
iignatyev@4951 334 }
iignatyev@4951 335 try {
iignatyev@4951 336 METHOD = Helper.class.getDeclaredMethod("method");
iignatyev@4951 337 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 338 throw new RuntimeException(
iignatyev@4951 339 "exception on getting method Helper.method()", e);
iignatyev@4951 340 }
iignatyev@4951 341 try {
iignatyev@4951 342 STATIC = Helper.class.getDeclaredMethod("staticMethod");
iignatyev@4951 343 } catch (NoSuchMethodException | SecurityException e) {
iignatyev@4951 344 throw new RuntimeException(
iignatyev@4951 345 "exception on getting method Helper.staticMethod()", e);
iignatyev@4951 346 }
iignatyev@4951 347 }
iignatyev@4951 348
iignatyev@4951 349 private static int staticMethod() {
iignatyev@4951 350 return 1138;
iignatyev@4951 351 }
iignatyev@4951 352
iignatyev@4951 353 private int method() {
iignatyev@4951 354 return 42;
iignatyev@4951 355 }
iignatyev@4951 356
iignatyev@4951 357 private final int x;
iignatyev@4951 358
iignatyev@4951 359 public Helper() {
iignatyev@4951 360 x = 0;
iignatyev@4951 361 }
iignatyev@4951 362
iignatyev@4951 363 private Helper(int x) {
iignatyev@4951 364 this.x = x;
iignatyev@4951 365 }
iignatyev@4951 366
iignatyev@4951 367 @Override
iignatyev@4951 368 public int hashCode() {
iignatyev@4951 369 return x;
iignatyev@4951 370 }
iignatyev@4592 371 }
iignatyev@4592 372 }

mercurial