|
| 1 | +/* |
| 2 | + * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | + /* |
| 25 | + * @test |
| 26 | + * @bug 8275908 |
| 27 | + * @summary Record null_check traps for calls and array_check traps in the interpreter |
| 28 | + * |
| 29 | + * @requires vm.compiler2.enabled & vm.compMode != "Xcomp" |
| 30 | + * |
| 31 | + * @library /test/lib |
| 32 | + * @build jdk.test.whitebox.WhiteBox |
| 33 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 34 | + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
| 35 | + * -XX:+UseSerialGC -Xbatch -XX:-UseOnStackReplacement -XX:-TieredCompilation |
| 36 | + * -XX:CompileCommand=compileonly,compiler.exceptions.OptimizeImplicitExceptions::throwImplicitException |
| 37 | + * compiler.exceptions.OptimizeImplicitExceptions |
| 38 | + */ |
| 39 | + |
| 40 | +package compiler.exceptions; |
| 41 | + |
| 42 | +import java.lang.reflect.Method; |
| 43 | +import java.util.HashMap; |
| 44 | + |
| 45 | +import jdk.test.lib.Asserts; |
| 46 | +import jdk.test.whitebox.WhiteBox; |
| 47 | + |
| 48 | +public class OptimizeImplicitExceptions { |
| 49 | + // ImplicitException represents the various implicit (aka. 'built-in') exceptions |
| 50 | + // which can be thrown implicitely by the JVM when executing bytecodes. |
| 51 | + public enum ImplicitException { |
| 52 | + // NullPointerException during field access |
| 53 | + NULL_POINTER_EXCEPTION("null_check"), |
| 54 | + // NullPointerException during invoke |
| 55 | + INVOKE_NULL_POINTER_EXCEPTION("null_check"), |
| 56 | + ARITHMETIC_EXCEPTION("div0_check"), |
| 57 | + ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION("range_check"), |
| 58 | + ARRAY_STORE_EXCEPTION("array_check"), |
| 59 | + CLASS_CAST_EXCEPTION("class_check"); |
| 60 | + private final String reason; |
| 61 | + ImplicitException(String reason) { |
| 62 | + this.reason = reason; |
| 63 | + } |
| 64 | + public String getReason() { |
| 65 | + return reason; |
| 66 | + } |
| 67 | + } |
| 68 | + // TestMode represents a specific combination of the OmitStackTraceInFastThrow command line options. |
| 69 | + // They will be set up in 'setFlags(TestMode testMode)' before a new test run starts. |
| 70 | + public enum TestMode { |
| 71 | + OMIT_STACKTRACES_IN_FASTTHROW, |
| 72 | + STACKTRACES_IN_FASTTHROW |
| 73 | + } |
| 74 | + |
| 75 | + private static final WhiteBox WB = WhiteBox.getWhiteBox(); |
| 76 | + // The number of deoptimizations after which a method will be made not-entrant |
| 77 | + private static final int PerBytecodeTrapLimit = WB.getIntxVMFlag("PerBytecodeTrapLimit").intValue(); |
| 78 | + // The number of interpreter invocations after which a decompiled method will be re-compiled. |
| 79 | + private static final int Tier0InvokeNotifyFreq = (int)Math.pow(2, WB.getIntxVMFlag("Tier0InvokeNotifyFreqLog")); |
| 80 | + // The following variables are used to track the value of the global deopt counters between the various test phases. |
| 81 | + private static int oldDeoptCount = 0; |
| 82 | + private static HashMap<String, Integer> oldDeoptCountReason = new HashMap<String, Integer>(ImplicitException.values().length); |
| 83 | + // The following two objects are declared statically to simplify the test method. |
| 84 | + private static String[] string_a = new String[1]; |
| 85 | + private static final Object o = new Object(); |
| 86 | + |
| 87 | + // This is the main test method. It will repeatedly called with the same ImplicitException 'type' to |
| 88 | + // JIT-compile it, deoptimized it, re-compile it again and do various checks on the way. |
| 89 | + // This process will be repeated then for each kind of ImplicitException 'type'. |
| 90 | + public static Object throwImplicitException(ImplicitException type, Object[] object_a) { |
| 91 | + switch (type) { |
| 92 | + case NULL_POINTER_EXCEPTION: { |
| 93 | + return object_a.length; |
| 94 | + } |
| 95 | + case INVOKE_NULL_POINTER_EXCEPTION: { |
| 96 | + return object_a.hashCode(); |
| 97 | + } |
| 98 | + case ARITHMETIC_EXCEPTION: { |
| 99 | + return ((42 / (object_a.length - 1)) > 2) ? null : object_a[0]; |
| 100 | + } |
| 101 | + case ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION: { |
| 102 | + return object_a[5]; |
| 103 | + } |
| 104 | + case ARRAY_STORE_EXCEPTION: { |
| 105 | + return (object_a[0] = o); |
| 106 | + } |
| 107 | + case CLASS_CAST_EXCEPTION: { |
| 108 | + return (ImplicitException[])object_a; |
| 109 | + } |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + // Completely unload (i.e. make "not-entrant"->"zombie"->"unload/free") a JIT-compiled |
| 115 | + // version of a method and clear the method's profiling counters. |
| 116 | + private static void unloadAndClean(Method m) { |
| 117 | + WB.deoptimizeMethod(m); // Makes the nmethod "not entrant". |
| 118 | + WB.forceNMethodSweep(); // Makes all "not entrant" nmethods "zombie". This requires |
| 119 | + WB.forceNMethodSweep(); // two sweeps, see 'nmethod::can_convert_to_zombie()' for why. |
| 120 | + WB.forceNMethodSweep(); // Need third sweep to actually unload/free all "zombie" nmethods. |
| 121 | + System.gc(); |
| 122 | + WB.clearMethodState(m); |
| 123 | + } |
| 124 | + |
| 125 | + // Set '-XX' flags according to 'TestMode' |
| 126 | + private static void setFlags(TestMode testMode) { |
| 127 | + if (testMode == TestMode.OMIT_STACKTRACES_IN_FASTTHROW) { |
| 128 | + WB.setBooleanVMFlag("OmitStackTraceInFastThrow", true); |
| 129 | + } else { |
| 130 | + WB.setBooleanVMFlag("OmitStackTraceInFastThrow", false); |
| 131 | + } |
| 132 | + |
| 133 | + System.out.println("=========================================================="); |
| 134 | + System.out.println("testMode=" + testMode + |
| 135 | + " OmitStackTraceInFastThrow=" + WB.getBooleanVMFlag("OmitStackTraceInFastThrow")); |
| 136 | + System.out.println("=========================================================="); |
| 137 | + } |
| 138 | + |
| 139 | + private static void printCounters(TestMode testMode, ImplicitException impExcp, Method throwImplicitException_m, int invocations) { |
| 140 | + System.out.println("testMode=" + testMode + " exception=" + impExcp + " invocations=" + invocations + "\n" + |
| 141 | + "decompilecount=" + WB.getMethodDecompileCount(throwImplicitException_m) + " " + |
| 142 | + "trapCount=" + WB.getMethodTrapCount(throwImplicitException_m) + " " + |
| 143 | + "trapCount(" + impExcp.getReason() + ")=" + |
| 144 | + WB.getMethodTrapCount(throwImplicitException_m, impExcp.getReason()) + " " + |
| 145 | + "globalDeoptCount=" + WB.getDeoptCount() + " " + |
| 146 | + "globalDeoptCount(" + impExcp.getReason() + ")=" + WB.getDeoptCount(impExcp.getReason(), null)); |
| 147 | + System.out.println("method compiled=" + WB.isMethodCompiled(throwImplicitException_m)); |
| 148 | + } |
| 149 | + |
| 150 | + // Checks after the test method has been JIT-compiled but before the compiled version has been invoked. |
| 151 | + private static void checkSimple(TestMode testMode, ImplicitException impExcp, Exception ex, Method throwImplicitException_m, int invocations) { |
| 152 | + |
| 153 | + printCounters(testMode, impExcp, throwImplicitException_m, invocations); |
| 154 | + // At this point, throwImplicitException() has been compiled but the compiled version has not been invoked yet. |
| 155 | + Asserts.assertEQ(WB.getMethodCompilationLevel(throwImplicitException_m), 4, "Method should be compiled at level 4."); |
| 156 | + |
| 157 | + int trapCount = WB.getMethodTrapCount(throwImplicitException_m); |
| 158 | + int trapCountSpecific = WB.getMethodTrapCount(throwImplicitException_m, impExcp.getReason()); |
| 159 | + Asserts.assertEQ(trapCount, invocations, "Trap count must much invocation count."); |
| 160 | + Asserts.assertEQ(trapCountSpecific, invocations, "Trap count must much invocation count."); |
| 161 | + Asserts.assertNotNull(ex.getMessage(), "Exceptions thrown in the interpreter should have a message."); |
| 162 | + } |
| 163 | + |
| 164 | + // Checks after the JIT-compiled test method has been invoked 'invocations' times. |
| 165 | + private static void check(TestMode testMode, ImplicitException impExcp, Exception ex, |
| 166 | + Method throwImplicitException_m, int invocations, int totalInvocations) { |
| 167 | + |
| 168 | + printCounters(testMode, impExcp, throwImplicitException_m, totalInvocations); |
| 169 | + // At this point, the compiled version of 'throwImplicitException()' has been invoked 'invocations' times. |
| 170 | + Asserts.assertEQ(WB.getMethodCompilationLevel(throwImplicitException_m), 4, "Method should be compiled at level 4."); |
| 171 | + int deoptCount = WB.getDeoptCount(); |
| 172 | + int deoptCountReason = WB.getDeoptCount(impExcp.getReason(), null/*action*/); |
| 173 | + if (testMode == TestMode.OMIT_STACKTRACES_IN_FASTTHROW) { |
| 174 | + // No deoptimizations for '-XX:+OmitStackTraceInFastThrow' |
| 175 | + Asserts.assertEQ(oldDeoptCount, deoptCount, "Wrong number of deoptimizations."); |
| 176 | + Asserts.assertEQ(oldDeoptCountReason.get(impExcp.getReason()), deoptCountReason, "Wrong number of deoptimizations."); |
| 177 | + // '-XX:+OmitStackTraceInFastThrow' never has message because it is using a global singleton exception. |
| 178 | + Asserts.assertNull(ex.getMessage(), "Optimized exceptions have no message."); |
| 179 | + } else if (testMode == TestMode.STACKTRACES_IN_FASTTHROW) { |
| 180 | + // We always deoptimize for '-XX:-OmitStackTraceInFastThrow |
| 181 | + Asserts.assertEQ(oldDeoptCount + invocations, deoptCount, "Wrong number of deoptimizations."); |
| 182 | + Asserts.assertEQ(oldDeoptCountReason.get(impExcp.getReason()) + invocations, deoptCountReason, "Wrong number of deoptimizations."); |
| 183 | + Asserts.assertNotNull(ex.getMessage(), "Exceptions thrown in the interpreter should have a message."); |
| 184 | + } else { |
| 185 | + Asserts.fail("Unknown test mode."); |
| 186 | + } |
| 187 | + oldDeoptCount = deoptCount; |
| 188 | + oldDeoptCountReason.put(impExcp.getReason(), deoptCountReason); |
| 189 | + } |
| 190 | + |
| 191 | + public static void main(String[] args) throws Exception { |
| 192 | + |
| 193 | + if (!WB.getBooleanVMFlag("ProfileTraps")) { |
| 194 | + // The fast-throw optimzation only works if we're running with -XX:+ProfileTraps |
| 195 | + return; |
| 196 | + } |
| 197 | + |
| 198 | + // Initialize global deopt counts to zero. |
| 199 | + for (ImplicitException impExcp : ImplicitException.values()) { |
| 200 | + oldDeoptCountReason.put(impExcp.getReason(), 0); |
| 201 | + } |
| 202 | + // Get a handle of the test method for usage with the WhiteBox API. |
| 203 | + Method throwImplicitException_m = OptimizeImplicitExceptions.class |
| 204 | + .getDeclaredMethod("throwImplicitException", new Class[] { ImplicitException.class, Object[].class}); |
| 205 | + |
| 206 | + for (TestMode testMode : TestMode.values()) { |
| 207 | + setFlags(testMode); |
| 208 | + for (ImplicitException impExcp : ImplicitException.values()) { |
| 209 | + int invocations = 0; |
| 210 | + Exception lastException = null; |
| 211 | + |
| 212 | + // Warmup and compile, but don't invoke compiled code. |
| 213 | + while(!WB.isMethodCompiled(throwImplicitException_m)) { |
| 214 | + invocations++; |
| 215 | + try { |
| 216 | + throwImplicitException(impExcp, impExcp.getReason().equals("null_check") ? null : string_a); |
| 217 | + } catch (Exception catchedExcp) { |
| 218 | + lastException = catchedExcp; |
| 219 | + continue; |
| 220 | + } |
| 221 | + throw new Exception("Should not happen"); |
| 222 | + } |
| 223 | + |
| 224 | + checkSimple(testMode, impExcp, lastException, throwImplicitException_m, invocations); |
| 225 | + |
| 226 | + // Invoke compiled code 'PerBytecodeTrapLimit' times. |
| 227 | + for (int i = 0; i < PerBytecodeTrapLimit; i++) { |
| 228 | + invocations++; |
| 229 | + try { |
| 230 | + throwImplicitException(impExcp, impExcp.getReason().equals("null_check") ? null : string_a); |
| 231 | + } catch (Exception catchedExcp) { |
| 232 | + lastException = catchedExcp; |
| 233 | + continue; |
| 234 | + } |
| 235 | + throw new Exception("Should not happen"); |
| 236 | + } |
| 237 | + |
| 238 | + check(testMode, impExcp, lastException, throwImplicitException_m, PerBytecodeTrapLimit, invocations); |
| 239 | + |
| 240 | + // Invoke compiled code 'Tier0InvokeNotifyFreq' times. |
| 241 | + // If the method was de-compiled before, this will re-compile it again. |
| 242 | + for (int i = 0; i < Tier0InvokeNotifyFreq; i++) { |
| 243 | + invocations++; |
| 244 | + try { |
| 245 | + throwImplicitException(impExcp, impExcp.getReason().equals("null_check") ? null : string_a); |
| 246 | + } catch (Exception catchedExcp) { |
| 247 | + lastException = catchedExcp; |
| 248 | + continue; |
| 249 | + } |
| 250 | + throw new Exception("Should not happen"); |
| 251 | + } |
| 252 | + |
| 253 | + check(testMode, impExcp, lastException, throwImplicitException_m, Tier0InvokeNotifyFreq, invocations); |
| 254 | + |
| 255 | + // Invoke compiled code 'PerBytecodeTrapLimit' times. |
| 256 | + for (int i = 0; i < PerBytecodeTrapLimit; i++) { |
| 257 | + invocations++; |
| 258 | + try { |
| 259 | + throwImplicitException(impExcp, impExcp.getReason().equals("null_check") ? null : string_a); |
| 260 | + } catch (Exception catchedExcp) { |
| 261 | + lastException = catchedExcp; |
| 262 | + continue; |
| 263 | + } |
| 264 | + throw new Exception("Should not happen"); |
| 265 | + } |
| 266 | + |
| 267 | + check(testMode, impExcp, lastException, throwImplicitException_m, PerBytecodeTrapLimit, invocations); |
| 268 | + |
| 269 | + System.out.println("------------------------------------------------------------------"); |
| 270 | + |
| 271 | + unloadAndClean(throwImplicitException_m); |
| 272 | + } |
| 273 | + } |
| 274 | + } |
| 275 | +} |
0 commit comments