changeset 22756:c2ce8dd9be05

Lazy initialization of HotSpotTruffleRuntime.
author Roland Schatz <roland.schatz@oracle.com>
date Mon, 05 Oct 2015 13:26:59 +0200
parents a48f9b3e01f5
children 110ea233237d
files graal/com.oracle.graal.api.runtime/src/com/oracle/graal/api/runtime/Graal.java graal/com.oracle.graal.truffle.hotspot/src/com/oracle/graal/truffle/hotspot/HotSpotTruffleRuntime.java mx.graal/suite.py
diffstat 3 files changed, 80 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- 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());
--- 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<OptimizedCallTarget, Future<?>> compilations = Collections.synchronizedMap(new IdentityHashMap<>());
-    private final ExecutorService compileQueue;
-    private StackIntrospection stackIntrospection;
+    static class Lazy {
+        private Map<OptimizedCallTarget, Future<?>> compilations = Collections.synchronizedMap(new IdentityHashMap<>());
+        private final ExecutorService compileQueue;
+        private StackIntrospection stackIntrospection;
+        private final Map<RootCallTarget, Void> callTargets = Collections.synchronizedMap(new WeakHashMap<RootCallTarget, Void>());
+
+        public Lazy(HotSpotTruffleRuntime runtime) {
+            setDontInlineCallBoundaryMethod();
+
+            runtime.installDefaultListeners();
 
-    private final Map<RootCallTarget, Void> callTargets = Collections.synchronizedMap(new WeakHashMap<RootCallTarget, Void>());
+            // 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<OptimizedCallTarget> 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<RootCallTarget> getCallTargets() {
-        return Collections.unmodifiableSet(callTargets.keySet());
+        return Collections.unmodifiableSet(lazy().callTargets.keySet());
     }
 
     @Override
--- 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"},