test/compiler/whitebox/CompilerWhiteBoxTest.java

Sat, 10 Aug 2013 10:01:12 +0400

author
iignatyev
date
Sat, 10 Aug 2013 10:01:12 +0400
changeset 5512
11237ee74aae
parent 5032
d1c9384eecb4
child 5541
f99558245e5c
permissions
-rw-r--r--

8019915: whitebox testClearMethodStateTest fails with tiered on sparc
Summary: 'compileonly' directive has beens added to each 'compiler/whitebox' test
Reviewed-by: kvn

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

mercurial