package jdk.tools.jaotc;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.core.GraalCompilerOptions;
import org.graalvm.compiler.debug.DebugContext;
import org.graalvm.compiler.debug.DebugContext.Activation;
import org.graalvm.compiler.debug.TTY;
import org.graalvm.compiler.options.OptionValues;
import org.graalvm.compiler.printer.GraalDebugHandlersFactory;
import org.graalvm.compiler.serviceprovider.GraalServices;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.runtime.JVMCICompiler;
final class AOTCompilationTask implements Runnable, Comparable<Object> {
private static final AtomicInteger ids = new AtomicInteger();
private final Main main;
private OptionValues graalOptions;
private final int id;
private final AOTCompiledClass holder;
private final ResolvedJavaMethod method;
private final AOTBackend aotBackend;
private CompiledMethodInfo result;
AOTCompilationTask(Main main, OptionValues graalOptions, AOTCompiledClass holder, ResolvedJavaMethod method, AOTBackend aotBackend) {
this.main = main;
this.graalOptions = graalOptions;
this.id = ids.incrementAndGet();
this.holder = holder;
this.method = method;
this.aotBackend = aotBackend;
}
@Override
@SuppressWarnings("try")
public void run() {
HotSpotJVMCIRuntime.runtime();
AOTCompiler.logCompilation(JavaMethodInfo.uniqueMethodName(method), "Compiling");
final long threadId = Thread.currentThread().getId();
final boolean printCompilation = GraalCompilerOptions.PrintCompilation.getValue(graalOptions) && !TTY.isSuppressed() && GraalServices.isThreadAllocatedMemorySupported();
if (printCompilation) {
TTY.println(getMethodDescription() + "...");
}
final long start;
final long allocatedBytesBefore;
if (printCompilation) {
start = System.currentTimeMillis();
allocatedBytesBefore = GraalServices.getThreadAllocatedBytes(threadId);
} else {
start = 0L;
allocatedBytesBefore = 0L;
}
CompilationResult compResult = null;
final long startTime = System.currentTimeMillis();
SnippetReflectionProvider snippetReflection = aotBackend.getProviders().getSnippetReflection();
try (DebugContext debug = DebugContext.create(graalOptions, new GraalDebugHandlersFactory(snippetReflection)); Activation a = debug.activate()) {
compResult = aotBackend.compileMethod(method, debug);
}
final long endTime = System.currentTimeMillis();
if (printCompilation) {
final long stop = System.currentTimeMillis();
final int targetCodeSize = compResult != null ? compResult.getTargetCodeSize() : -1;
final long allocatedBytesAfter = GraalServices.getThreadAllocatedBytes(threadId);
final long allocatedBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024;
TTY.println(getMethodDescription() + String.format(" | %4dms %5dB %5dkB", stop - start, targetCodeSize, allocatedBytes));
}
if (compResult == null) {
result = null;
return;
}
LogPrinter.writeLog(" Compile Time: " + TimeUnit.MILLISECONDS.toSeconds(endTime - startTime) + "secs");
if (main.options.debug) {
aotBackend.printCompiledMethod((HotSpotResolvedJavaMethod) method, compResult);
}
result = new CompiledMethodInfo(compResult, new AOTHotSpotResolvedJavaMethod((HotSpotResolvedJavaMethod) method, aotBackend.getBackend(), graalOptions));
}
private String getMethodDescription() {
return String.format("%-6d aot %s %s", getId(), JavaMethodInfo.uniqueMethodName(method),
getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI ? "" : "(OSR@" + getEntryBCI() + ") ");
}
private int getId() {
return id;
}
private static int getEntryBCI() {
return JVMCICompiler.INVOCATION_ENTRY_BCI;
}
ResolvedJavaMethod getMethod() {
return method;
}
AOTCompiledClass getHolder() {
return holder;
}
CompiledMethodInfo getResult() {
return result;
}
@Override
public int compareTo(Object obj) {
AOTCompilationTask other = (AOTCompilationTask) obj;
return this.id - other.id;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
AOTCompilationTask other = (AOTCompilationTask) obj;
return (this.id == other.id);
}
@Override
public int hashCode() {
return 31 + id;
}
}