package org.jruby.compiler;
import org.jruby.Ruby;
import org.jruby.RubyEncoding;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubyModule;
import org.jruby.internal.runtime.methods.CompiledIRMethod;
import org.jruby.internal.runtime.methods.MixedModeIRMethod;
import org.jruby.ir.IRScope;
import org.jruby.ir.interpreter.InterpreterContext;
import org.jruby.ir.targets.JVMVisitor;
import org.jruby.ir.targets.JVMVisitorMethodContext;
import org.jruby.runtime.MethodIndex;
import org.jruby.runtime.MixedModeIRBlockBody;
import org.jruby.runtime.ThreadContext;
import org.jruby.threading.DaemonThreadFactory;
import org.jruby.util.ClassDefiningClassLoader;
import org.jruby.util.ClassDefiningJRubyClassLoader;
import org.jruby.util.OneShotClassLoader;
import org.jruby.util.cli.Options;
import org.jruby.util.collections.WeakValuedMap;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;
import java.lang.invoke.MethodHandles;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.AbstractMap;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class JITCompiler implements JITCompilerMBean {
private static final Logger LOG = LoggerFactory.getLogger(JITCompiler.class);
public static final String RUBY_JIT_PREFIX = "rubyjit";
final JITCounts counts = new JITCounts();
private final ExecutorService executor;
final Ruby runtime;
final RubyInstanceConfig config;
final Map<String, ClassDefiningClassLoader> loaderMap;
public JITCompiler(Ruby runtime) {
this.runtime = runtime;
this.config = runtime.getInstanceConfig();
this.executor = new ThreadPoolExecutor(
0,
2,
60,
TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(),
new DaemonThreadFactory("Ruby-" + runtime.getRuntimeNumber() + "-JIT", Thread.MIN_PRIORITY));
switch (Options.JIT_LOADER_MODE.load()) {
case SHARED:
this.loaderMap = new SharedClassLoaderMap(new ClassDefiningJRubyClassLoader(runtime.getJRubyClassLoader()));
break;
case SHARED_SOURCE:
this.loaderMap = new WeakValuedMap<>();
break;
case UNIQUE:
default:
this.loaderMap = null;
break;
}
}
public long getSuccessCount() {
return counts.successCount.get();
}
public long getCompileCount() {
return counts.compiledCount.get();
}
public long getFailCount() {
return counts.failCount.get();
}
public long getCompileTime() {
return counts.compileTime.get() / 1000;
}
public long getAbandonCount() {
return counts.abandonCount.get();
}
public long getCodeSize() {
return counts.codeSize.get();
}
public long getCodeAverageSize() {
return counts.codeAverageSize.get();
}
public long getCompileTimeAverage() {
return counts.compileTimeAverage.get() / 1000;
}
public long getCodeLargestSize() {
return counts.codeLargestSize.get();
}
public long getIRSize() {
return counts.irSize.get();
}
public long getIRAverageSize() {
return counts.irAverageSize.get();
}
public long getIRLargestSize() {
return counts.irLargestSize.get();
}
public String[] getFrameAwareMethods() {
String[] frameAwareMethods = MethodIndex.FRAME_AWARE_METHODS.toArray(new String[0]);
Arrays.sort(frameAwareMethods);
return frameAwareMethods;
}
public String[] getScopeAwareMethods() {
String[] scopeAwareMethods = MethodIndex.SCOPE_AWARE_METHODS.toArray(new String[0]);
Arrays.sort(scopeAwareMethods);
return scopeAwareMethods;
}
public void tearDown() {
shutdown();
}
public void shutdown() {
try {
executor.shutdown();
} catch (SecurityException se) {
}
}
public boolean isShutdown() {
return executor.isShutdown();
}
public Runnable getTaskFor(ThreadContext context, Compilable method) {
if (method instanceof MixedModeIRMethod) {
return new MethodJITTask(this, (MixedModeIRMethod) method, method.getOwnerName());
} else if (method instanceof MixedModeIRBlockBody) {
return new BlockJITTask(this, (MixedModeIRBlockBody) method, method.getOwnerName());
} else if (method instanceof CompiledIRMethod) {
return new MethodCompiledJITTask(this, (CompiledIRMethod) method, method.getOwnerName());
}
return new FullBuildTask(this, method);
}
public void buildThresholdReached(ThreadContext context, final Compilable method) {
final RubyInstanceConfig config = context.runtime.getInstanceConfig();
final Runnable task = getTaskFor(context, method);
if (task instanceof Task && config.getJitMax() >= 0 && config.getJitMax() < getSuccessCount()) {
if (config.isJitLogging()) {
JITCompiler.log(method, method.getName(), "skipping: jit.max threshold reached");
}
return;
}
try {
if (config.getJitBackground() && config.getJitThreshold() > 0) {
try {
executor.submit(task);
} catch (RejectedExecutionException ree) {
task.run();
}
} else {
method.ensureInstrsReady();
task.run();
}
} catch (Exception e) {
throw new NotCompilableException(e);
}
}
static final MethodHandles.Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup().in(Ruby.class);
public static String getHashForString(String str) {
return getHashForBytes(RubyEncoding.encodeUTF8(str));
}
public static String getHashForBytes(byte[] bytes) {
try {
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
sha1.update(bytes);
StringBuilder builder = new StringBuilder();
for (byte aByte : sha1.digest()) {
builder.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));
}
return builder.toString().toUpperCase(Locale.ENGLISH);
} catch (NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae);
}
}
ClassDefiningClassLoader getLoaderFor(final String source) {
if (source != null && loaderMap != null) {
ClassDefiningClassLoader loader = loaderMap.get(source);
if (loader == null) {
synchronized (loaderMap) {
loader = loaderMap.get(source);
if (loader == null) {
loaderMap.put(source, loader = new ClassDefiningJRubyClassLoader(runtime.getJRubyClassLoader()));
}
}
}
return loader;
}
return new OneShotClassLoader(runtime.getJRubyClassLoader());
}
static void log(Compilable<?> target, String name, String message, Object... reason) {
String className = target.getImplementationClass().getName();
StringBuilder builder = new StringBuilder(32);
builder.append(message).append(": ").append(className);
if (name != null) builder.append(' ').append(name);
builder.append(" at ").append(target.getFile()).append(':').append(target.getLine());
if (reason.length > 0) {
builder.append(" because of: \"");
for (Object aReason : reason) builder.append(aReason);
builder.append('"');
}
LOG.info(builder.toString());
}
static abstract class Task implements Runnable {
protected final JITCompiler jitCompiler;
public Task(JITCompiler jitCompiler) {
this.jitCompiler = jitCompiler;
}
public void run() {
synchronized (jitCompiler) {
try {
exec();
} catch (Throwable ex) {
jitFailed(ex);
}
}
}
protected abstract void exec() throws Exception ;
protected String getSourceFile() {
return null;
}
protected void jitFailed(final Throwable ex) {
if (jitCompiler.config.isJitLogging()) {
logFailed(ex);
if (jitCompiler.config.isJitLoggingVerbose()) {
ex.printStackTrace();
}
}
jitCompiler.counts.failCount.incrementAndGet();
}
protected Class<?> defineClass(final JITClassGenerator generator, final JVMVisitor visitor,
final IRScope scope, final InterpreterContext interpreterContext) {
Class sourceClass = visitor.defineFromBytecode(scope, generator.bytecode(), getCodeLoader());
if (sourceClass == null) {
jitCompiler.counts.failCount.incrementAndGet();
return null;
}
generator.updateCounters(jitCompiler.counts, interpreterContext);
long methodCount = jitCompiler.counts.successCount.incrementAndGet();
if (jitCompiler.config.isJitLogging()) logJitted();
if (jitCompiler.config.getJitLogEvery() > 0) {
if (methodCount % jitCompiler.config.getJitLogEvery() == 0) {
logImpl("live compiled count: " + methodCount);
}
}
return sourceClass;
}
ClassDefiningClassLoader getCodeLoader() {
return jitCompiler.getLoaderFor(getSourceFile());
}
protected void logJitted() {
logImpl("done jitting");
}
protected void logFailed(Throwable ex) {
logImpl("could not compile", ex);
}
protected abstract void logImpl(String msg, Object... cause) ;
}
private static class SharedClassLoaderMap extends AbstractMap<String, ClassDefiningClassLoader> {
final ClassDefiningClassLoader value;
SharedClassLoaderMap(ClassDefiningClassLoader value) {
this.value = value;
}
@Override
public ClassDefiningClassLoader get(final Object key) {
return value;
}
@Override
public ClassDefiningClassLoader put(String key, ClassDefiningClassLoader value) {
return this.value;
}
@Override
public int size() {
return -1;
}
@Override
public void clear() {
}
@Override
public Set<Entry<String, ClassDefiningClassLoader>> entrySet() {
throw new UnsupportedOperationException();
}
}
}