changeset 13863:88026f1d51e4

Merge.
author Christian Humer <christian.humer@gmail.com>
date Mon, 03 Feb 2014 21:01:26 +0100
parents f9b934e1e172 (current diff) 1e01e2644a5d (diff)
children 5365f8d35b06
files
diffstat 3 files changed, 63 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationTask.java	Mon Feb 03 21:01:11 2014 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompilationTask.java	Mon Feb 03 21:01:26 2014 +0100
@@ -49,7 +49,7 @@
 import com.oracle.graal.phases.*;
 import com.oracle.graal.phases.tiers.*;
 
-public class CompilationTask implements Runnable {
+public class CompilationTask implements Runnable, Comparable {
 
     public static final ThreadLocal<Boolean> withinEnqueue = new ThreadLocal<Boolean>() {
 
@@ -60,7 +60,7 @@
     };
 
     private enum CompilationStatus {
-        Queued, Running
+        Queued, Running, Finished
     }
 
     private final HotSpotBackend backend;
@@ -71,12 +71,22 @@
 
     private StructuredGraph graph;
 
-    public CompilationTask(HotSpotBackend backend, HotSpotResolvedJavaMethod method, int entryBCI, int id) {
-        assert id >= 0;
+    private static final AtomicLong uniqueTaskIds = new AtomicLong();
+
+    /**
+     * A long representing the sequence number of this task. Used for sorting the compile queue.
+     */
+    private long taskId;
+
+    private boolean blocking;
+
+    public CompilationTask(HotSpotBackend backend, HotSpotResolvedJavaMethod method, int entryBCI, boolean blocking) {
         this.backend = backend;
         this.method = method;
         this.entryBCI = entryBCI;
-        this.id = id;
+        this.id = backend.getRuntime().getCompilerToVM().allocateCompileId(method, entryBCI);
+        this.blocking = blocking;
+        this.taskId = uniqueTaskIds.incrementAndGet();
         this.status = new AtomicReference<>(CompilationStatus.Queued);
     }
 
@@ -101,10 +111,43 @@
                 method.setCurrentTask(null);
             }
             withinEnqueue.set(Boolean.TRUE);
+            status.set(CompilationStatus.Finished);
+            synchronized (this) {
+                notifyAll();
+            }
         }
     }
 
     /**
+     * Block waiting till the compilation completes.
+     */
+    public synchronized void block() {
+        while (status.get() != CompilationStatus.Finished) {
+            try {
+                wait();
+            } catch (InterruptedException e) {
+                // Ignore and retry
+            }
+        }
+    }
+
+    /**
+     * Sort blocking tasks before non-blocking ones and then by lowest taskId within the group.
+     */
+    public int compareTo(Object o) {
+        if (!(o instanceof CompilationTask)) {
+            return 1;
+        }
+        CompilationTask task2 = (CompilationTask) o;
+        if (this.blocking != task2.blocking) {
+            // Blocking CompilationTasks are always higher than CompilationTasks
+            return task2.blocking ? 1 : -1;
+        }
+        // Within the two groups sort by sequence id, so they are processed in insertion order.
+        return this.taskId > task2.taskId ? 1 : -1;
+    }
+
+    /**
      * Time spent in compilation.
      */
     public static final DebugTimer CompilationTime = Debug.timer("CompilationTime");
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompileTheWorld.java	Mon Feb 03 21:01:11 2014 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/CompileTheWorld.java	Mon Feb 03 21:01:26 2014 +0100
@@ -39,9 +39,7 @@
 import com.oracle.graal.debug.*;
 import com.oracle.graal.graph.*;
 import com.oracle.graal.hotspot.HotSpotOptions.OptionConsumer;
-import com.oracle.graal.hotspot.bridge.*;
 import com.oracle.graal.hotspot.meta.*;
-import com.oracle.graal.nodes.*;
 import com.oracle.graal.options.*;
 import com.oracle.graal.options.OptionValue.OverrideScope;
 import com.oracle.graal.phases.tiers.*;
@@ -147,7 +145,6 @@
 
     // Some runtime instances we need.
     private final HotSpotGraalRuntime runtime = runtime();
-    private final VMToCompilerImpl vmToCompiler = (VMToCompilerImpl) runtime.getVMToCompiler();
 
     /** List of Zip/Jar files to compile (see {@link #CompileTheWorldClasspath}. */
     private final String files;
@@ -320,8 +317,8 @@
 
     class CTWCompilationTask extends CompilationTask {
 
-        CTWCompilationTask(HotSpotBackend backend, HotSpotResolvedJavaMethod method, int id) {
-            super(backend, method, INVOCATION_ENTRY_BCI, id);
+        CTWCompilationTask(HotSpotBackend backend, HotSpotResolvedJavaMethod method) {
+            super(backend, method, INVOCATION_ENTRY_BCI, false);
         }
 
         /**
@@ -351,9 +348,8 @@
         try {
             long start = System.currentTimeMillis();
 
-            int id = vmToCompiler.allocateCompileTaskId(method, StructuredGraph.INVOCATION_ENTRY_BCI);
             HotSpotBackend backend = runtime.getHostBackend();
-            CompilationTask task = new CTWCompilationTask(backend, method, id);
+            CompilationTask task = new CTWCompilationTask(backend, method);
             task.runCompilation(false);
 
             compileTime += (System.currentTimeMillis() - start);
--- a/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/VMToCompilerImpl.java	Mon Feb 03 21:01:11 2014 +0100
+++ b/graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/bridge/VMToCompilerImpl.java	Mon Feb 03 21:01:26 2014 +0100
@@ -33,6 +33,7 @@
 
 import com.oracle.graal.api.meta.*;
 import com.oracle.graal.compiler.*;
+import com.oracle.graal.compiler.CompilerThreadFactory.CompilerThread;
 import com.oracle.graal.compiler.CompilerThreadFactory.DebugConfigAccess;
 import com.oracle.graal.debug.*;
 import com.oracle.graal.debug.internal.*;
@@ -95,10 +96,6 @@
         this.runtime = runtime;
     }
 
-    public int allocateCompileTaskId(HotSpotResolvedJavaMethod method, int entryBCI) {
-        return runtime.getCompilerToVM().allocateCompileId(method, entryBCI);
-    }
-
     public void startCompiler(boolean bootstrapEnabled) throws Throwable {
 
         FastNodeClassRegistry.initialize();
@@ -157,7 +154,7 @@
                 return Debug.isEnabled() ? DebugEnvironment.initialize(log) : null;
             }
         });
-        compileQueue = new ThreadPoolExecutor(Threads.getValue(), Threads.getValue(), 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), factory);
+        compileQueue = new ThreadPoolExecutor(Threads.getValue(), Threads.getValue(), 0L, TimeUnit.MILLISECONDS, new PriorityBlockingQueue<Runnable>(), factory);
 
         // Create queue status printing thread.
         if (PrintQueue.getValue()) {
@@ -548,24 +545,24 @@
             return;
         }
 
+        // Don't allow blocking compiles from CompilerThreads
+        boolean block = blocking && !(Thread.currentThread() instanceof CompilerThread);
         CompilationTask.withinEnqueue.set(Boolean.TRUE);
         try {
             if (method.tryToQueueForCompilation()) {
                 assert method.isQueuedForCompilation();
 
-                int id = allocateCompileTaskId(method, entryBCI);
                 HotSpotBackend backend = runtime.getHostBackend();
-                CompilationTask task = new CompilationTask(backend, method, entryBCI, id);
+                CompilationTask task = new CompilationTask(backend, method, entryBCI, block);
 
-                if (blocking) {
-                    task.runCompilation(true);
-                } else {
-                    try {
-                        method.setCurrentTask(task);
-                        compileQueue.execute(task);
-                    } catch (RejectedExecutionException e) {
-                        // The compile queue was already shut down.
+                try {
+                    method.setCurrentTask(task);
+                    compileQueue.execute(task);
+                    if (block) {
+                        task.block();
                     }
+                } catch (RejectedExecutionException e) {
+                    // The compile queue was already shut down.
                 }
             }
         } finally {