changeset 11722:ff05c78a7f64

use time passed to decide what methods to compile or inline
author Christian Wirth <christian.wirth@oracle.com>
date Thu, 19 Sep 2013 10:36:56 +0200
parents cb81a0f3407c
children 9b8e3b2986c5
files graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/CompilationPolicy.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTarget.java
diffstat 2 files changed, 114 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/CompilationPolicy.java	Thu Sep 19 10:36:56 2013 +0200
@@ -0,0 +1,105 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.truffle;
+
+import static com.oracle.graal.truffle.TruffleCompilerOptions.*;
+
+public class CompilationPolicy {
+
+    private int invokeCounter;
+    private int originalInvokeCounter;
+    private int loopAndInvokeCounter;
+    private long prevTimestamp;
+
+    private final int initialInvokeCounter;
+    private final int compilationThreshold;
+
+    public CompilationPolicy(final int compilationThreshold, final int initialInvokeCounter) {
+        this.invokeCounter = initialInvokeCounter;
+        this.loopAndInvokeCounter = compilationThreshold;
+        this.originalInvokeCounter = compilationThreshold;
+        this.prevTimestamp = System.currentTimeMillis();
+
+        this.compilationThreshold = compilationThreshold;
+        this.initialInvokeCounter = initialInvokeCounter;
+    }
+
+    public int getInvokeCounter() {
+        return invokeCounter;
+    }
+
+    public int getOriginalInvokeCounter() {
+        return originalInvokeCounter;
+    }
+
+    public int getLoopAndInvokeCounter() {
+        return loopAndInvokeCounter;
+    }
+
+    public void compilationInvalidated() {
+        int invalidationReprofileCount = TruffleInvalidationReprofileCount.getValue();
+        invokeCounter = invalidationReprofileCount;
+        if (TruffleFunctionInlining.getValue()) {
+            originalInvokeCounter += invalidationReprofileCount;
+        }
+    }
+
+    public void countInterpreterCall() {
+        invokeCounter--;
+        loopAndInvokeCounter--;
+    }
+
+    public void inlined(int minInvokesAfterInlining) {
+        invokeCounter = minInvokesAfterInlining;
+        int inliningReprofileCount = TruffleInliningReprofileCount.getValue();
+        loopAndInvokeCounter = inliningReprofileCount;
+        originalInvokeCounter = inliningReprofileCount;
+    }
+
+    public void reportLoopCount(int count) {
+        loopAndInvokeCounter = Math.max(0, loopAndInvokeCounter - count);
+    }
+
+    public void nodeReplaced() {
+        // delay compilation until tree is deemed stable enough
+        int replaceBackoff = TruffleReplaceReprofileCount.getValue();
+        if (loopAndInvokeCounter < replaceBackoff) {
+            loopAndInvokeCounter = replaceBackoff;
+        }
+    }
+
+    public boolean compileOrInline() {
+        if (invokeCounter <= 0 && loopAndInvokeCounter <= 0) {
+            long timestamp = System.currentTimeMillis();
+            if ((timestamp - prevTimestamp) < 100) {
+                return true;
+            }
+            this.invokeCounter = initialInvokeCounter;
+            this.loopAndInvokeCounter = compilationThreshold;
+            this.originalInvokeCounter = compilationThreshold;
+            this.prevTimestamp = timestamp;
+        }
+        return false;
+    }
+
+}
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTarget.java	Thu Sep 19 05:00:12 2013 +0200
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTarget.java	Thu Sep 19 10:36:56 2013 +0200
@@ -46,9 +46,7 @@
     protected OptimizedCallTarget(RootNode rootNode, FrameDescriptor descriptor, TruffleCompiler compiler, int invokeCounter, int compilationThreshold) {
         super(rootNode, descriptor);
         this.compiler = compiler;
-        this.invokeCounter = invokeCounter;
-        this.loopAndInvokeCounter = compilationThreshold;
-        this.originalInvokeCounter = compilationThreshold;
+        this.compilationPolicy = new CompilationPolicy(compilationThreshold, invokeCounter);
         this.rootNode.setCallTarget(this);
 
         if (TruffleCallTargetProfiling.getValue()) {
@@ -58,10 +56,8 @@
 
     private HotSpotNmethod compiledMethod;
     private final TruffleCompiler compiler;
+    private final CompilationPolicy compilationPolicy;
 
-    private int invokeCounter;
-    private int originalInvokeCounter;
-    private int loopAndInvokeCounter;
     private boolean disableCompilation;
 
     private int callCount;
@@ -97,12 +93,8 @@
     private Object compiledCodeInvalidated(PackedFrame caller, Arguments args) {
         CompilerAsserts.neverPartOfCompilation();
         compiledMethod = null;
-        int invalidationReprofileCount = TruffleInvalidationReprofileCount.getValue();
-        invokeCounter = invalidationReprofileCount;
         invalidationCount++;
-        if (TruffleFunctionInlining.getValue()) {
-            originalInvokeCounter += invalidationReprofileCount;
-        }
+        compilationPolicy.compilationInvalidated();
         if (TraceTruffleCompilation.getValue()) {
             OUT.printf("[truffle] invalidated %-48s |Alive %5.0fms |Inv# %d                                     |Replace# %d\n", rootNode, (System.nanoTime() - timeCompilationFinished) / 1e6,
                             invalidationCount, replaceCount);
@@ -112,9 +104,8 @@
 
     private Object interpreterCall(PackedFrame caller, Arguments args) {
         CompilerAsserts.neverPartOfCompilation();
-        invokeCounter--;
-        loopAndInvokeCounter--;
-        if (disableCompilation || loopAndInvokeCounter > 0 || invokeCounter > 0) {
+        compilationPolicy.countInterpreterCall();
+        if (disableCompilation || !compilationPolicy.compileOrInline()) {
             return executeHelper(caller, args);
         } else {
             compileOrInline();
@@ -124,10 +115,7 @@
 
     private void compileOrInline() {
         if (TruffleFunctionInlining.getValue() && inline()) {
-            invokeCounter = MIN_INVOKES_AFTER_INLINING;
-            int inliningReprofileCount = TruffleInliningReprofileCount.getValue();
-            loopAndInvokeCounter = inliningReprofileCount;
-            originalInvokeCounter = inliningReprofileCount;
+            compilationPolicy.inlined(MIN_INVOKES_AFTER_INLINING);
         } else {
             compile();
         }
@@ -189,18 +177,13 @@
 
     @Override
     public void reportLoopCount(int count) {
-        loopAndInvokeCounter = Math.max(0, loopAndInvokeCounter - count);
+        compilationPolicy.reportLoopCount(count);
     }
 
     @Override
     public void nodeReplaced() {
         replaceCount++;
-
-        // delay compilation until tree is deemed stable enough
-        int replaceBackoff = TruffleReplaceReprofileCount.getValue();
-        if (loopAndInvokeCounter < replaceBackoff) {
-            loopAndInvokeCounter = replaceBackoff;
-        }
+        compilationPolicy.nodeReplaced();
     }
 
     private static class InliningHelper {
@@ -280,7 +263,7 @@
 
             public InliningPolicy(OptimizedCallTarget caller) {
                 this.callerNodeCount = NodeUtil.countNodes(caller.getRootNode());
-                this.callerInvocationCount = caller.originalInvokeCounter;
+                this.callerInvocationCount = caller.compilationPolicy.getOriginalInvokeCounter();
             }
 
             public boolean continueInlining() {