package jdk.tools.jaotc;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.core.GraalCompilerOptions;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.DebugEnvironment;
import org.graalvm.compiler.debug.Management;
import org.graalvm.compiler.debug.TTY;
import org.graalvm.compiler.debug.internal.DebugScope;
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.runtime.JVMCICompiler;
public class AOTCompilationTask implements Runnable, Comparable<Object> {
private static final AtomicInteger ids = new AtomicInteger();
private static final com.sun.management.ThreadMXBean threadMXBean = (com.sun.management.ThreadMXBean) Management.getThreadMXBean();
private final Main main;
private final int id;
private final AOTCompiledClass holder;
private final ResolvedJavaMethod method;
private final AOTBackend aotBackend;
private CompiledMethodInfo result;
public AOTCompilationTask(Main main, AOTCompiledClass holder, ResolvedJavaMethod method, AOTBackend aotBackend) {
this.main = main;
this.id = ids.getAndIncrement();
this.holder = holder;
this.method = method;
this.aotBackend = aotBackend;
}
public void run() {
HotSpotJVMCIRuntime.runtime();
if (Debug.isEnabled() && DebugScope.getConfig() == null) {
DebugEnvironment.initialize(TTY.out);
}
AOTCompiler.logCompilation(MiscUtils.uniqueMethodName(method), "Compiling");
final long threadId = Thread.currentThread().getId();
final boolean printCompilation = GraalCompilerOptions.PrintCompilation.getValue() && !TTY.isSuppressed();
final boolean printAfterCompilation = GraalCompilerOptions.PrintAfterCompilation.getValue() && !TTY.isSuppressed();
if (printCompilation) {
TTY.println(getMethodDescription() + "...");
}
final long start;
final long allocatedBytesBefore;
if (printAfterCompilation || printCompilation) {
start = System.currentTimeMillis();
allocatedBytesBefore = printAfterCompilation || printCompilation ? threadMXBean.getThreadAllocatedBytes(threadId) : 0L;
} else {
start = 0L;
allocatedBytesBefore = 0L;
}
final long startTime = System.currentTimeMillis();
CompilationResult compResult = aotBackend.compileMethod(method);
final long endTime = System.currentTimeMillis();
if (printAfterCompilation || printCompilation) {
final long stop = System.currentTimeMillis();
final int targetCodeSize = compResult != null ? compResult.getTargetCodeSize() : -1;
final long allocatedBytesAfter = threadMXBean.getThreadAllocatedBytes(threadId);
final long allocatedBytes = (allocatedBytesAfter - allocatedBytesBefore) / 1024;
if (printAfterCompilation) {
TTY.println(getMethodDescription() + String.format(" | %4dms %5dB %5dkB", stop - start, targetCodeSize, allocatedBytes));
} else if (printCompilation) {
TTY.println(String.format("%-6d JVMCI %-70s %-45s %-50s | %4dms %5dB %5dkB", getId(), "", "", "", stop - start, targetCodeSize, allocatedBytes));
}
}
if (compResult == null) {
result = null;
return;
}
Main.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));
}
private String getMethodDescription() {
return String.format("%-6d JVMCI %-70s %-45s %-50s %s", getId(), method.getDeclaringClass().getName(), method.getName(), method.getSignature().toMethodDescriptor(),
getEntryBCI() == JVMCICompiler.INVOCATION_ENTRY_BCI ? "" : "(OSR@" + getEntryBCI() + ") ");
}
private int getId() {
return id;
}
public int getEntryBCI() {
return JVMCICompiler.INVOCATION_ENTRY_BCI;
}
public ResolvedJavaMethod getMethod() {
return method;
}
public AOTCompiledClass getHolder() {
return holder;
}
public 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;
}
}