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