# HG changeset patch # User Roland Schatz # Date 1444044419 -7200 # Node ID c2ce8dd9be055442fc17cc8e5a9aeb2a30f2ec40 # Parent a48f9b3e01f5e0ee9a174c3811588938e06a5407 Lazy initialization of HotSpotTruffleRuntime. diff -r a48f9b3e01f5 -r c2ce8dd9be05 graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Graal.java --- a/graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Graal.java Sat Oct 03 17:25:59 2015 -0700 +++ b/graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Graal.java Mon Oct 05 13:26:59 2015 +0200 @@ -31,23 +31,25 @@ */ public class Graal { - private static final GraalRuntime runtime = initializeRuntime(); + private static final class Lazy { + private static final GraalRuntime runtime = initializeRuntime(); - private static GraalRuntime initializeRuntime() { - GraalRuntimeAccess access = Services.loadSingle(GraalRuntimeAccess.class, false); - if (access != null) { - GraalRuntime rt = access.getRuntime(); - assert rt != null; - return rt; + private static GraalRuntime initializeRuntime() { + GraalRuntimeAccess access = Services.loadSingle(GraalRuntimeAccess.class, false); + if (access != null) { + GraalRuntime rt = access.getRuntime(); + assert rt != null; + return rt; + } + return new InvalidGraalRuntime(); } - return new InvalidGraalRuntime(); } /** * Gets the singleton {@link GraalRuntime} instance available to the application. */ public static GraalRuntime getRuntime() { - return runtime; + return Lazy.runtime; } /** @@ -61,7 +63,7 @@ String javaHome = System.getProperty("java.home"); String vmName = System.getProperty("java.vm.name"); Formatter errorMessage = new Formatter(); - if (runtime.getClass() == InvalidGraalRuntime.class) { + if (getRuntime().getClass() == InvalidGraalRuntime.class) { errorMessage.format("The VM does not support the Graal API.%n"); } else { errorMessage.format("The VM does not expose required Graal capability %s.%n", clazz.getName()); diff -r a48f9b3e01f5 -r c2ce8dd9be05 graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java --- a/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java Sat Oct 03 17:25:59 2015 -0700 +++ b/graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java Mon Oct 05 13:26:59 2015 +0200 @@ -63,6 +63,7 @@ import jdk.internal.jvmci.meta.MetaAccessProvider; import jdk.internal.jvmci.meta.ResolvedJavaMethod; import jdk.internal.jvmci.meta.ResolvedJavaType; +import jdk.internal.jvmci.runtime.JVMCI; import jdk.internal.jvmci.service.Services; import com.oracle.graal.api.runtime.Graal; @@ -119,41 +120,45 @@ return new HotSpotTruffleRuntime(); } - private Map> compilations = Collections.synchronizedMap(new IdentityHashMap<>()); - private final ExecutorService compileQueue; - private StackIntrospection stackIntrospection; + static class Lazy { + private Map> compilations = Collections.synchronizedMap(new IdentityHashMap<>()); + private final ExecutorService compileQueue; + private StackIntrospection stackIntrospection; + private final Map callTargets = Collections.synchronizedMap(new WeakHashMap()); + + public Lazy(HotSpotTruffleRuntime runtime) { + setDontInlineCallBoundaryMethod(); + + runtime.installDefaultListeners(); - private final Map callTargets = Collections.synchronizedMap(new WeakHashMap()); + // Create compilation queue. + CompilerThreadFactory factory = new CompilerThreadFactory("TruffleCompilerThread", new CompilerThreadFactory.DebugConfigAccess() { + public GraalDebugConfig getDebugConfig() { + if (Debug.isEnabled()) { + GraalDebugConfig debugConfig = DebugEnvironment.initialize(TTY.out().out()); + debugConfig.dumpHandlers().add(new TruffleTreeDumpHandler()); + return debugConfig; + } else { + return null; + } + } + }); + int selectedProcessors = TruffleCompilerOptions.TruffleCompilerThreads.getValue(); + if (selectedProcessors == 0) { + // No manual selection made, check how many processors are available. + int availableProcessors = Runtime.getRuntime().availableProcessors(); + if (availableProcessors >= 12) { + selectedProcessors = 4; + } else if (availableProcessors >= 4) { + selectedProcessors = 2; + } + } + selectedProcessors = Math.max(1, selectedProcessors); + compileQueue = Executors.newFixedThreadPool(selectedProcessors, factory); + } + } private HotSpotTruffleRuntime() { - setDontInlineCallBoundaryMethod(); - - installDefaultListeners(); - - // Create compilation queue. - CompilerThreadFactory factory = new CompilerThreadFactory("TruffleCompilerThread", new CompilerThreadFactory.DebugConfigAccess() { - public GraalDebugConfig getDebugConfig() { - if (Debug.isEnabled()) { - GraalDebugConfig debugConfig = DebugEnvironment.initialize(TTY.out().out()); - debugConfig.dumpHandlers().add(new TruffleTreeDumpHandler()); - return debugConfig; - } else { - return null; - } - } - }); - int selectedProcessors = TruffleCompilerOptions.TruffleCompilerThreads.getValue(); - if (selectedProcessors == 0) { - // No manual selection made, check how many processors are available. - int availableProcessors = Runtime.getRuntime().availableProcessors(); - if (availableProcessors >= 12) { - selectedProcessors = 4; - } else if (availableProcessors >= 4) { - selectedProcessors = 2; - } - } - selectedProcessors = Math.max(1, selectedProcessors); - compileQueue = Executors.newFixedThreadPool(selectedProcessors, factory); } @Override @@ -161,12 +166,26 @@ return "Graal Truffle Runtime"; } + private volatile Lazy lazy; + + private Lazy lazy() { + if (lazy == null) { + synchronized (this) { + if (lazy == null) { + lazy = new Lazy(this); + } + } + } + return lazy; + } + @Override protected StackIntrospection getStackIntrospection() { - if (stackIntrospection == null) { - stackIntrospection = HotSpotJVMCIRuntime.runtime().getHostJVMCIBackend().getStackIntrospection(); + Lazy l = lazy(); + if (l.stackIntrospection == null) { + l.stackIntrospection = HotSpotJVMCIRuntime.runtime().getHostJVMCIBackend().getStackIntrospection(); } - return stackIntrospection; + return l.stackIntrospection; } @Override @@ -191,7 +210,7 @@ } OptimizedCallTarget target = new OptimizedCallTarget(source, rootNode, this, compilationPolicy, new HotSpotSpeculationLog()); rootNode.setCallTarget(target); - callTargets.put(target, null); + lazy().callTargets.put(target, null); return target; } @@ -202,8 +221,7 @@ } public static void setDontInlineCallBoundaryMethod() { - Providers providers = getHotSpotProviders(); - MetaAccessProvider metaAccess = providers.getMetaAccess(); + MetaAccessProvider metaAccess = JVMCI.getRuntime().getHostJVMCIBackend().getMetaAccess(); ResolvedJavaType type = metaAccess.lookupJavaType(OptimizedCallTarget.class); for (ResolvedJavaMethod method : type.getDeclaredMethods()) { if (method.getAnnotation(TruffleCallBoundary.class) != null) { @@ -298,8 +316,9 @@ doCompile(optimizedCallTarget); } }; - Future future = compileQueue.submit(r); - this.compilations.put(optimizedCallTarget, future); + Lazy l = lazy(); + Future future = l.compileQueue.submit(r); + l.compilations.put(optimizedCallTarget, future); getCompilationNotify().notifyCompilationQueued(optimizedCallTarget); if (!mayBeAsynchronous) { @@ -319,9 +338,10 @@ @Override public boolean cancelInstalledTask(OptimizedCallTarget optimizedCallTarget, Object source, CharSequence reason) { - Future codeTask = this.compilations.get(optimizedCallTarget); + Lazy l = lazy(); + Future codeTask = l.compilations.get(optimizedCallTarget); if (codeTask != null && isCompiling(optimizedCallTarget)) { - this.compilations.remove(optimizedCallTarget); + l.compilations.remove(optimizedCallTarget); boolean result = codeTask.cancel(true); if (result) { optimizedCallTarget.notifyCompilationFinished(false); @@ -334,7 +354,7 @@ @Override public void waitForCompilation(OptimizedCallTarget optimizedCallTarget, long timeout) throws ExecutionException, TimeoutException { - Future codeTask = this.compilations.get(optimizedCallTarget); + Future codeTask = lazy().compilations.get(optimizedCallTarget); if (codeTask != null && isCompiling(optimizedCallTarget)) { try { codeTask.get(timeout, TimeUnit.MILLISECONDS); @@ -346,15 +366,15 @@ @Override public Collection getQueuedCallTargets() { - return compilations.keySet().stream().filter(e -> !compilations.get(e).isDone()).collect(Collectors.toList()); + return lazy().compilations.keySet().stream().filter(e -> !lazy().compilations.get(e).isDone()).collect(Collectors.toList()); } @Override public boolean isCompiling(OptimizedCallTarget optimizedCallTarget) { - Future codeTask = this.compilations.get(optimizedCallTarget); + Future codeTask = lazy().compilations.get(optimizedCallTarget); if (codeTask != null) { if (codeTask.isCancelled() || codeTask.isDone()) { - this.compilations.remove(optimizedCallTarget); + lazy().compilations.remove(optimizedCallTarget); return false; } return true; @@ -393,7 +413,7 @@ @Override public Collection getCallTargets() { - return Collections.unmodifiableSet(callTargets.keySet()); + return Collections.unmodifiableSet(lazy().callTargets.keySet()); } @Override diff -r a48f9b3e01f5 -r c2ce8dd9be05 mx.graal/suite.py --- a/mx.graal/suite.py Sat Oct 03 17:25:59 2015 -0700 +++ b/mx.graal/suite.py Mon Oct 05 13:26:59 2015 +0200 @@ -6,7 +6,7 @@ "suites": [ { "name" : "jvmci", - "version" : "8ed4037e828628c8a957ed1ec9cdfdd694c64d91", + "version" : "9203f93ffeb0957968a092c7752ef5c755f91a2a", "urls" : [ {"url" : "http://lafo.ssw.uni-linz.ac.at/hg/graal-jvmci-8", "kind" : "hg"}, {"url" : "https://curio.ssw.jku.at/nexus/content/repositories/snapshots", "kind" : "binary"},