1 /* 2 * Copyright (c) 2012, 2016, Oracle and/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 package org.graalvm.compiler.hotspot; 24 25 import static org.graalvm.compiler.core.GraalCompilerOptions.ExitVMOnBailout; 26 import static org.graalvm.compiler.core.GraalCompilerOptions.ExitVMOnException; 27 import static org.graalvm.compiler.core.GraalCompilerOptions.PrintAfterCompilation; 28 import static org.graalvm.compiler.core.GraalCompilerOptions.PrintBailout; 29 import static org.graalvm.compiler.core.GraalCompilerOptions.PrintCompilation; 30 import static org.graalvm.compiler.core.GraalCompilerOptions.PrintFilter; 31 import static org.graalvm.compiler.core.GraalCompilerOptions.PrintStackTraceOnException; 32 import static org.graalvm.compiler.core.phases.HighTier.Options.Inline; 33 import static org.graalvm.compiler.java.BytecodeParserOptions.InlineDuringParsing; 34 35 import java.util.List; 36 37 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; 38 import org.graalvm.compiler.code.CompilationResult; 39 import org.graalvm.compiler.core.common.CompilationIdentifier; 40 import org.graalvm.compiler.debug.CounterKey; 41 import org.graalvm.compiler.debug.DebugCloseable; 42 import org.graalvm.compiler.debug.DebugContext; 43 import org.graalvm.compiler.debug.DebugDumpScope; 44 import org.graalvm.compiler.debug.GraalError; 45 import org.graalvm.compiler.debug.Management; 46 import org.graalvm.compiler.debug.TTY; 47 import org.graalvm.compiler.debug.TimeSource; 48 import org.graalvm.compiler.debug.TimerKey; 49 import org.graalvm.compiler.options.OptionKey; 50 import org.graalvm.compiler.options.OptionValues; 51 import org.graalvm.compiler.printer.GraalDebugHandlersFactory; 52 import org.graalvm.util.EconomicMap; 53 54 import jdk.vm.ci.code.BailoutException; 55 import jdk.vm.ci.code.CodeCacheProvider; 56 import jdk.vm.ci.hotspot.EventProvider; 57 import jdk.vm.ci.hotspot.HotSpotCompilationRequest; 58 import jdk.vm.ci.hotspot.HotSpotCompilationRequestResult; 59 import jdk.vm.ci.hotspot.HotSpotInstalledCode; 60 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider; 61 import jdk.vm.ci.hotspot.HotSpotNmethod; 62 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 63 import jdk.vm.ci.runtime.JVMCICompiler; 64 import jdk.vm.ci.services.JVMCIServiceLocator; 65 66 public class CompilationTask { 67 68 private static final CounterKey BAILOUTS = DebugContext.counter("Bailouts"); 69 70 private static final EventProvider eventProvider; 71 72 static { 73 List<EventProvider> providers = JVMCIServiceLocator.getProviders(EventProvider.class); 74 if (providers.size() > 1) { 75 throw new GraalError("Multiple %s providers found: %s", EventProvider.class.getName(), providers); 76 } else if (providers.isEmpty()) { 77 eventProvider = EventProvider.createEmptyEventProvider(); 78 } else { 79 eventProvider = providers.get(0); 80 } 81 } 82 83 private final HotSpotJVMCIRuntimeProvider jvmciRuntime; 84 85 private final HotSpotGraalCompiler compiler; 86 private final HotSpotCompilationIdentifier compilationId; 87 88 private HotSpotInstalledCode installedCode; 89 90 /** 91 * Specifies whether the compilation result is installed as the 92 * {@linkplain HotSpotNmethod#isDefault() default} nmethod for the compiled method. 93 */ 94 private final boolean installAsDefault; 95 96 private final boolean useProfilingInfo; 97 private final OptionValues options; 98 99 final class RetryableCompilation extends HotSpotRetryableCompilation<HotSpotCompilationRequestResult> { 100 private final EventProvider.CompilationEvent compilationEvent; 101 CompilationResult result; 102 103 RetryableCompilation(EventProvider.CompilationEvent compilationEvent) { 104 super(compiler.getGraalRuntime()); 105 this.compilationEvent = compilationEvent; 106 } 107 108 @Override 109 public String toString() { 110 return getMethod().format("%H.%n"); 111 } 112 113 @SuppressWarnings("try") 114 @Override 115 protected HotSpotCompilationRequestResult run(DebugContext debug, Throwable retryCause) { 116 HotSpotResolvedJavaMethod method = getMethod(); 117 int entryBCI = getEntryBCI(); 118 final boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI; 119 CompilationStatistics stats = CompilationStatistics.create(options, method, isOSR); 120 final boolean printCompilation = PrintCompilation.getValue(options) && !TTY.isSuppressed(); 121 final boolean printAfterCompilation = PrintAfterCompilation.getValue(options) && !TTY.isSuppressed(); 122 if (printCompilation) { 123 TTY.println(getMethodDescription() + "..."); 124 } 125 126 TTY.Filter filter = new TTY.Filter(PrintFilter.getValue(options), method); 127 final long start; 128 final long allocatedBytesBefore; 129 if (printAfterCompilation || printCompilation) { 130 final long threadId = Thread.currentThread().getId(); 131 start = TimeSource.getTimeNS(); 132 allocatedBytesBefore = printAfterCompilation || printCompilation ? Lazy.threadMXBean.getThreadAllocatedBytes(threadId) : 0L; 133 } else { 134 start = 0L; 135 allocatedBytesBefore = 0L; 136 } 137 138 try (DebugContext.Scope s = debug.scope("Compiling", new DebugDumpScope(getIdString(), true))) { 139 // Begin the compilation event. 140 compilationEvent.begin(); 141 result = compiler.compile(method, entryBCI, useProfilingInfo, compilationId, options, debug); 142 } catch (Throwable e) { 143 if (retryCause != null) { 144 log("Exception during retry", e); 145 } 146 throw debug.handle(e); 147 } finally { 148 // End the compilation event. 149 compilationEvent.end(); 150 151 filter.remove(); 152 153 if (printAfterCompilation || printCompilation) { 154 final long threadId = Thread.currentThread().getId(); 155 final long stop = TimeSource.getTimeNS(); 156 final long duration = (stop - start) / 1000000; 157 final int targetCodeSize = result != null ? result.getTargetCodeSize() : -1; 158 final int bytecodeSize = result != null ? result.getBytecodeSize() : 0; 159 final long allocatedBytesAfter = Lazy.threadMXBean.getThreadAllocatedBytes(threadId); 160 final long allocatedKBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024; 161 162 if (printAfterCompilation) { 163 TTY.println(getMethodDescription() + String.format(" | %4dms %5dB %5dB %5dkB", duration, bytecodeSize, targetCodeSize, allocatedKBytes)); 164 } else if (printCompilation) { 165 TTY.println(String.format("%-6d JVMCI %-70s %-45s %-50s | %4dms %5dB %5dB %5dkB", getId(), "", "", "", duration, bytecodeSize, targetCodeSize, allocatedKBytes)); 166 } 167 } 168 } 169 170 if (result != null) { 171 try (DebugCloseable b = CodeInstallationTime.start(debug)) { 172 installMethod(debug, result); 173 } 174 } 175 stats.finish(method, installedCode); 176 if (result != null) { 177 return HotSpotCompilationRequestResult.success(result.getBytecodeSize() - method.getCodeSize()); 178 } 179 return null; 180 } 181 } 182 183 static class Lazy { 184 /** 185 * A {@link com.sun.management.ThreadMXBean} to be able to query some information about the 186 * current compiler thread, e.g. total allocated bytes. 187 */ 188 static final com.sun.management.ThreadMXBean threadMXBean = (com.sun.management.ThreadMXBean) Management.getThreadMXBean(); 189 } 190 191 public CompilationTask(HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotGraalCompiler compiler, HotSpotCompilationRequest request, boolean useProfilingInfo, boolean installAsDefault, 192 OptionValues options) { 193 this.jvmciRuntime = jvmciRuntime; 194 this.compiler = compiler; 195 this.compilationId = new HotSpotCompilationIdentifier(request); 196 this.useProfilingInfo = useProfilingInfo; 197 this.installAsDefault = installAsDefault; 198 199 /* 200 * Disable inlining if HotSpot has it disabled unless it's been explicitly set in Graal. 201 */ 202 HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime(); 203 GraalHotSpotVMConfig config = graalRuntime.getVMConfig(); 204 OptionValues newOptions = options; 205 if (!config.inline) { 206 EconomicMap<OptionKey<?>, Object> m = OptionValues.newOptionMap(); 207 if (Inline.getValue(options) && !Inline.hasBeenSet(options)) { 208 m.put(Inline, false); 209 } 210 if (InlineDuringParsing.getValue(options) && !InlineDuringParsing.hasBeenSet(options)) { 211 m.put(InlineDuringParsing, false); 212 } 213 if (!m.isEmpty()) { 214 newOptions = new OptionValues(options, m); 215 } 216 } 217 this.options = newOptions; 218 } 219 220 public HotSpotResolvedJavaMethod getMethod() { 221 return getRequest().getMethod(); 222 } 223 224 CompilationIdentifier getCompilationIdentifier() { 225 return compilationId; 226 } 227 228 /** 229 * Returns the HostSpot id of this compilation. 230 * 231 * @return HotSpot compile id 232 */ 233 public int getId() { 234 return getRequest().getId(); 235 } 236 237 public int getEntryBCI() { 238 return getRequest().getEntryBCI(); 239 } 240 241 /** 242 * @return the compilation id plus a trailing '%' is the compilation is an OSR to match 243 * PrintCompilation style output 244 */ 245 public String getIdString() { 246 if (getEntryBCI() != JVMCICompiler.INVOCATION_ENTRY_BCI) { 247 return getId() + "%"; 248 } else { 249 return Integer.toString(getId()); 250 } 251 } 252 253 public HotSpotInstalledCode getInstalledCode() { 254 return installedCode; 255 } 256 257 /** 258 * Time spent in compilation. 259 */ 260 private static final TimerKey CompilationTime = DebugContext.timer("CompilationTime").doc("Time spent in compilation and code installation."); 261 262 /** 263 * Counts the number of compiled {@linkplain CompilationResult#getBytecodeSize() bytecodes}. 264 */ 265 private static final CounterKey CompiledBytecodes = DebugContext.counter("CompiledBytecodes"); 266 267 /** 268 * Counts the number of compiled {@linkplain CompilationResult#getBytecodeSize() bytecodes} for 269 * which {@linkplain CompilationResult#getTargetCode()} code was installed. 270 */ 271 private static final CounterKey CompiledAndInstalledBytecodes = DebugContext.counter("CompiledAndInstalledBytecodes"); 272 273 /** 274 * Counts the number of installed {@linkplain CompilationResult#getTargetCodeSize()} bytes. 275 */ 276 private static final CounterKey InstalledCodeSize = DebugContext.counter("InstalledCodeSize"); 277 278 /** 279 * Time spent in code installation. 280 */ 281 public static final TimerKey CodeInstallationTime = DebugContext.timer("CodeInstallation"); 282 283 public HotSpotCompilationRequestResult runCompilation() { 284 SnippetReflectionProvider snippetReflection = compiler.getGraalRuntime().getHostProviders().getSnippetReflection(); 285 try (DebugContext debug = DebugContext.create(options, new GraalDebugHandlersFactory(snippetReflection))) { 286 return runCompilation(debug); 287 } 288 } 289 290 @SuppressWarnings("try") 291 public HotSpotCompilationRequestResult runCompilation(DebugContext debug) { 292 HotSpotGraalRuntimeProvider graalRuntime = compiler.getGraalRuntime(); 293 GraalHotSpotVMConfig config = graalRuntime.getVMConfig(); 294 int entryBCI = getEntryBCI(); 295 boolean isOSR = entryBCI != JVMCICompiler.INVOCATION_ENTRY_BCI; 296 HotSpotResolvedJavaMethod method = getMethod(); 297 298 // Log a compilation event. 299 EventProvider.CompilationEvent compilationEvent = eventProvider.newCompilationEvent(); 300 301 if (installAsDefault) { 302 // If there is already compiled code for this method on our level we simply return. 303 // JVMCI compiles are always at the highest compile level, even in non-tiered mode so we 304 // only need to check for that value. 305 if (method.hasCodeAtLevel(entryBCI, config.compilationLevelFullOptimization)) { 306 return HotSpotCompilationRequestResult.failure("Already compiled", false); 307 } 308 } 309 310 RetryableCompilation compilation = new RetryableCompilation(compilationEvent); 311 try (DebugCloseable a = CompilationTime.start(debug)) { 312 return compilation.runWithRetry(debug); 313 } catch (BailoutException bailout) { 314 BAILOUTS.increment(debug); 315 if (ExitVMOnBailout.getValue(options)) { 316 TTY.out.println(method.format("Bailout in %H.%n(%p)")); 317 bailout.printStackTrace(TTY.out); 318 System.exit(-1); 319 } else if (PrintBailout.getValue(options)) { 320 TTY.out.println(method.format("Bailout in %H.%n(%p)")); 321 bailout.printStackTrace(TTY.out); 322 } 323 /* 324 * Handling of permanent bailouts: Permanent bailouts that can happen for example due to 325 * unsupported unstructured control flow in the bytecodes of a method must not be 326 * retried. Hotspot compile broker will ensure that no recompilation at the given tier 327 * will happen if retry is false. 328 */ 329 final boolean permanentBailout = bailout.isPermanent(); 330 if (permanentBailout && PrintBailout.getValue(options)) { 331 TTY.println("Permanent bailout %s compiling method %s %s.", bailout.getMessage(), HotSpotGraalCompiler.str(method), (isOSR ? "OSR" : "")); 332 } 333 return HotSpotCompilationRequestResult.failure(bailout.getMessage(), !permanentBailout); 334 } catch (Throwable t) { 335 // Log a failure event. 336 EventProvider.CompilerFailureEvent event = eventProvider.newCompilerFailureEvent(); 337 if (event.shouldWrite()) { 338 event.setCompileId(getId()); 339 event.setMessage(t.getMessage()); 340 event.commit(); 341 } 342 343 handleException(t); 344 /* 345 * Treat random exceptions from the compiler as indicating a problem compiling this 346 * method. Report the result of toString instead of getMessage to ensure that the 347 * exception type is included in the output in case there's no detail mesage. 348 */ 349 return HotSpotCompilationRequestResult.failure(t.toString(), false); 350 } finally { 351 try { 352 int compiledBytecodes = 0; 353 int codeSize = 0; 354 355 if (compilation.result != null) { 356 compiledBytecodes = compilation.result.getBytecodeSize(); 357 CompiledBytecodes.add(debug, compiledBytecodes); 358 if (installedCode != null) { 359 codeSize = installedCode.getSize(); 360 CompiledAndInstalledBytecodes.add(debug, compiledBytecodes); 361 InstalledCodeSize.add(debug, codeSize); 362 } 363 } 364 365 // Log a compilation event. 366 if (compilationEvent.shouldWrite()) { 367 compilationEvent.setMethod(method.format("%H.%n(%p)")); 368 compilationEvent.setCompileId(getId()); 369 compilationEvent.setCompileLevel(config.compilationLevelFullOptimization); 370 compilationEvent.setSucceeded(compilation.result != null && installedCode != null); 371 compilationEvent.setIsOsr(isOSR); 372 compilationEvent.setCodeSize(codeSize); 373 compilationEvent.setInlinedBytes(compiledBytecodes); 374 compilationEvent.commit(); 375 } 376 } catch (Throwable t) { 377 handleException(t); 378 } 379 } 380 } 381 382 protected void handleException(Throwable t) { 383 /* 384 * Automatically enable ExitVMOnException during bootstrap or when asserts are enabled but 385 * respect ExitVMOnException if it's been explicitly set. 386 */ 387 boolean exitVMOnException = ExitVMOnException.getValue(options); 388 if (!ExitVMOnException.hasBeenSet(options)) { 389 assert (exitVMOnException = true) == true; 390 if (!exitVMOnException) { 391 HotSpotGraalRuntimeProvider runtime = compiler.getGraalRuntime(); 392 if (runtime.isBootstrapping()) { 393 exitVMOnException = true; 394 } 395 } 396 } 397 398 if (PrintStackTraceOnException.getValue(options) || exitVMOnException) { 399 try { 400 t.printStackTrace(TTY.out); 401 } catch (Throwable throwable) { 402 // Don't let an exception here change the other control flow 403 } 404 } 405 406 if (exitVMOnException) { 407 System.exit(-1); 408 } 409 } 410 411 private String getMethodDescription() { 412 HotSpotResolvedJavaMethod method = getMethod(); 413 return String.format("%-6d JVMCI %-70s %-45s %-50s %s", getId(), method.getDeclaringClass().getName(), method.getName(), method.getSignature().toMethodDescriptor(), 414 getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI ? "" : "(OSR@" + getEntryBCI() + ") "); 415 } 416 417 @SuppressWarnings("try") 418 private void installMethod(DebugContext debug, final CompilationResult compResult) { 419 final CodeCacheProvider codeCache = jvmciRuntime.getHostJVMCIBackend().getCodeCache(); 420 HotSpotBackend backend = compiler.getGraalRuntime().getHostBackend(); 421 installedCode = null; 422 Object[] context = {new DebugDumpScope(getIdString(), true), codeCache, getMethod(), compResult}; 423 try (DebugContext.Scope s = debug.scope("CodeInstall", context)) { 424 installedCode = (HotSpotInstalledCode) backend.createInstalledCode(debug, getRequest().getMethod(), getRequest(), compResult, 425 getRequest().getMethod().getSpeculationLog(), null, installAsDefault, context); 426 } catch (Throwable e) { 427 throw debug.handle(e); 428 } 429 } 430 431 @Override 432 public String toString() { 433 return "Compilation[id=" + getId() + ", " + getMethod().format("%H.%n(%p)") + (getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI ? "" : "@" + getEntryBCI()) + "]"; 434 } 435 436 private HotSpotCompilationRequest getRequest() { 437 return compilationId.getRequest(); 438 } 439 }