--- old/src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCIRuntime.java 2015-11-02 17:06:01.000000000 -1000 +++ new/src/jdk.vm.ci/share/classes/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCIRuntime.java 2015-11-02 17:06:01.000000000 -1000 @@ -22,54 +22,55 @@ */ package jdk.vm.ci.hotspot; -import static jdk.vm.ci.inittimer.InitTimer.*; +import static jdk.vm.ci.inittimer.InitTimer.timer; -import java.util.*; - -import jdk.vm.ci.code.*; -import jdk.vm.ci.common.*; -import jdk.vm.ci.compiler.*; -import jdk.vm.ci.compiler.Compiler; -import jdk.vm.ci.inittimer.*; -import jdk.vm.ci.meta.*; -import jdk.vm.ci.runtime.*; -import jdk.vm.ci.service.*; +import java.io.IOException; +import java.io.OutputStream; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.TreeMap; + +import jdk.vm.ci.code.Architecture; +import jdk.vm.ci.code.CompilationResult; +import jdk.vm.ci.code.InstalledCode; +import jdk.vm.ci.common.JVMCIError; +import jdk.vm.ci.inittimer.InitTimer; +import jdk.vm.ci.meta.JVMCIMetaAccessContext; +import jdk.vm.ci.meta.JavaKind; +import jdk.vm.ci.meta.JavaType; +import jdk.vm.ci.meta.ResolvedJavaType; +import jdk.vm.ci.runtime.JVMCI; +import jdk.vm.ci.runtime.JVMCIBackend; +import jdk.vm.ci.runtime.JVMCICompiler; +import jdk.vm.ci.service.Services; //JaCoCo Exclude +/** + * HotSpot implementation of a JVMCI runtime. + * + * The initialization of this class is very fragile since it's initialized both through + * {@link JVMCI#initialize()} or through calling {@link HotSpotJVMCIRuntime#runtime()} and + * {@link HotSpotJVMCIRuntime#runtime()} is also called by {@link JVMCI#initialize()}. So this class + * can't have a static initializer and any required initialization must be done as part of + * {@link #runtime()}. This allows the initialization to funnel back through + * {@link JVMCI#initialize()} without deadlocking. + */ public final class HotSpotJVMCIRuntime implements HotSpotJVMCIRuntimeProvider, HotSpotProxified { - /** - * The proper initialization of this class is complex because it's tangled up with the - * initialization of the JVMCI and really should only ever be triggered through - * {@link JVMCI#getRuntime}. However since {@link #runtime} can also be called directly it - * should also trigger proper initialization. To ensure proper ordering, the static initializer - * of this class initializes {@link JVMCI} and then access to {@link DelayedInit#instance} - * triggers the final initialization of the {@link HotSpotJVMCIRuntime}. - */ - static { - JVMCI.initialize(); - } - @SuppressWarnings("try") static class DelayedInit { private static final HotSpotJVMCIRuntime instance; static { - try (InitTimer t0 = timer("HotSpotJVMCIRuntime.")) { - try (InitTimer t = timer("StartupEventListener.beforeJVMCIStartup")) { - for (StartupEventListener l : Services.load(StartupEventListener.class)) { - l.beforeJVMCIStartup(); - } - } - - try (InitTimer t = timer("HotSpotJVMCIRuntime.")) { - instance = new HotSpotJVMCIRuntime(); - } - - try (InitTimer t = timer("HotSpotJVMCIRuntime.completeInitialization")) { - instance.completeInitialization(); - } + try (InitTimer t = timer("HotSpotJVMCIRuntime.")) { + instance = new HotSpotJVMCIRuntime(); } } } @@ -78,20 +79,10 @@ * Gets the singleton {@link HotSpotJVMCIRuntime} object. */ public static HotSpotJVMCIRuntime runtime() { - assert DelayedInit.instance != null; + JVMCI.initialize(); return DelayedInit.instance; } - /** - * Do deferred initialization. - */ - public void completeInitialization() { - compiler = HotSpotJVMCICompilerConfig.getCompilerFactory().createCompiler(this); - for (HotSpotVMEventListener vmEventListener : vmEventListeners) { - vmEventListener.completeInitialization(this); - } - } - public static HotSpotJVMCIBackendFactory findFactory(String architecture) { for (HotSpotJVMCIBackendFactory factory : Services.load(HotSpotJVMCIBackendFactory.class)) { if (factory.getArchitecture().equalsIgnoreCase(architecture)) { @@ -106,7 +97,7 @@ * Gets the kind of a word value on the {@linkplain #getHostJVMCIBackend() host} backend. */ public static JavaKind getHostWordKind() { - return runtime().getHostJVMCIBackend().getCodeCache().getTarget().wordKind; + return runtime().getHostJVMCIBackend().getCodeCache().getTarget().wordJavaKind; } protected final CompilerToVM compilerToVm; @@ -114,16 +105,19 @@ protected final HotSpotVMConfig config; private final JVMCIBackend hostBackend; - private Compiler compiler; + private volatile JVMCICompiler compiler; protected final JVMCIMetaAccessContext metaAccessContext; private final Map, JVMCIBackend> backends = new HashMap<>(); private final Iterable vmEventListeners; + @SuppressWarnings("unused") private final String[] trivialPrefixes; + @SuppressWarnings("try") private HotSpotJVMCIRuntime() { compilerToVm = new CompilerToVM(); + try (InitTimer t = timer("HotSpotVMConfig")) { config = new HotSpotVMConfig(compilerToVm); } @@ -135,10 +129,8 @@ factory = findFactory(hostArchitecture); } - CompilerFactory compilerFactory = HotSpotJVMCICompilerConfig.getCompilerFactory(); - try (InitTimer t = timer("create JVMCI backend:", hostArchitecture)) { - hostBackend = registerBackend(factory.createJVMCIBackend(this, compilerFactory, null)); + hostBackend = registerBackend(factory.createJVMCIBackend(this, null)); } vmEventListeners = Services.load(HotSpotVMEventListener.class); @@ -154,6 +146,12 @@ context = new HotSpotJVMCIMetaAccessContext(); } metaAccessContext = context; + + if (Boolean.valueOf(System.getProperty("jvmci.printconfig"))) { + printConfig(config, compilerToVm); + } + + trivialPrefixes = HotSpotJVMCICompilerConfig.getCompilerFactory().getTrivialPrefixes(); } private JVMCIBackend registerBackend(JVMCIBackend backend) { @@ -179,7 +177,14 @@ return metaAccessContext; } - public Compiler getCompiler() { + public JVMCICompiler getCompiler() { + if (compiler == null) { + synchronized (this) { + if (compiler == null) { + compiler = HotSpotJVMCICompilerConfig.getCompilerFactory().createCompiler(this); + } + } + } return compiler; } @@ -211,7 +216,7 @@ return backends.get(arch); } - public Map, JVMCIBackend> getBackends() { + public Map, JVMCIBackend> getJVMCIBackends() { return Collections.unmodifiableMap(backends); } @@ -220,7 +225,7 @@ */ @SuppressWarnings({"unused"}) private void compileMethod(HotSpotResolvedJavaMethod method, int entryBCI, long jvmciEnv, int id) { - compiler.compileMethod(method, entryBCI, jvmciEnv, id); + getCompiler().compileMethod(new HotSpotCompilationRequest(method, entryBCI, jvmciEnv, id)); } /** @@ -247,4 +252,105 @@ vmEventListener.notifyInstall(hotSpotCodeCacheProvider, installedCode, compResult); } } + + private static void printConfig(HotSpotVMConfig config, CompilerToVM vm) { + Field[] fields = config.getClass().getDeclaredFields(); + Map sortedFields = new TreeMap<>(); + for (Field f : fields) { + if (!f.isSynthetic() && !Modifier.isStatic(f.getModifiers())) { + f.setAccessible(true); + sortedFields.put(f.getName(), f); + } + } + for (Field f : sortedFields.values()) { + try { + String line = String.format("%9s %-40s = %s%n", f.getType().getSimpleName(), f.getName(), pretty(f.get(config))); + byte[] lineBytes = line.getBytes(); + vm.writeDebugOutput(lineBytes, 0, lineBytes.length); + vm.flushDebugOutput(); + } catch (Exception e) { + } + } + } + + private static String pretty(Object value) { + if (value == null) { + return "null"; + } + + Class klass = value.getClass(); + if (value instanceof String) { + return "\"" + value + "\""; + } else if (value instanceof Method) { + return "method \"" + ((Method) value).getName() + "\""; + } else if (value instanceof Class) { + return "class \"" + ((Class) value).getSimpleName() + "\""; + } else if (value instanceof Integer) { + if ((Integer) value < 10) { + return value.toString(); + } + return value + " (0x" + Integer.toHexString((Integer) value) + ")"; + } else if (value instanceof Long) { + if ((Long) value < 10 && (Long) value > -10) { + return value + "l"; + } + return value + "l (0x" + Long.toHexString((Long) value) + "l)"; + } else if (klass.isArray()) { + StringBuilder str = new StringBuilder(); + int dimensions = 0; + while (klass.isArray()) { + dimensions++; + klass = klass.getComponentType(); + } + int length = Array.getLength(value); + str.append(klass.getSimpleName()).append('[').append(length).append(']'); + for (int i = 1; i < dimensions; i++) { + str.append("[]"); + } + str.append(" {"); + for (int i = 0; i < length; i++) { + str.append(pretty(Array.get(value, i))); + if (i < length - 1) { + str.append(", "); + } + } + str.append('}'); + return str.toString(); + } + return value.toString(); + } + + public OutputStream getLogStream() { + return new OutputStream() { + + @Override + public void write(byte[] b, int off, int len) throws IOException { + if (b == null) { + throw new NullPointerException(); + } else if (off < 0 || off > b.length || len < 0 || (off + len) > b.length || (off + len) < 0) { + throw new IndexOutOfBoundsException(); + } else if (len == 0) { + return; + } + compilerToVm.writeDebugOutput(b, off, len); + } + + @Override + public void write(int b) throws IOException { + write(new byte[]{(byte) b}, 0, 1); + } + + @Override + public void flush() throws IOException { + compilerToVm.flushDebugOutput(); + } + }; + } + + /** + * Collects the current values of all JVMCI benchmark counters, summed up over all threads. + */ + public long[] collectCounters() { + return compilerToVm.collectCounters(); + } }