changeset 18201:ae3b7695f0fb

Truffle: refactor TruffleCallTargetProfiling into a separate class.
author Christian Humer <christian.humer@gmail.com>
date Mon, 27 Oct 2014 15:18:14 +0100
parents 62de94d5cf73
children b3f6b210f723
files graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTargetLog.java graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/debug/PrintCallTargetProfiling.java
diffstat 3 files changed, 77 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java	Mon Oct 27 15:18:14 2014 +0100
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/GraalTruffleRuntime.java	Mon Oct 27 15:18:14 2014 +0100
@@ -60,6 +60,7 @@
         TraceCompilationFailureListener.install(this);
         TraceCompilationListener.install(this);
         TraceCompilationPolymorphismListener.install(this);
+        PrintCallTargetProfiling.install(this);
     }
 
     protected void lookupCallMethods(MetaAccessProvider metaAccess) {
--- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTargetLog.java	Mon Oct 27 15:18:14 2014 +0100
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/OptimizedCallTargetLog.java	Mon Oct 27 15:18:14 2014 +0100
@@ -26,30 +26,15 @@
 
 import java.io.*;
 import java.util.*;
-import java.util.function.*;
-import java.util.stream.*;
 
 import com.oracle.graal.debug.*;
 import com.oracle.graal.truffle.TruffleInlining.CallTreeNodeVisitor;
-import com.oracle.truffle.api.*;
-import com.oracle.truffle.api.nodes.Node;
+import com.oracle.truffle.api.nodes.*;
 
 public final class OptimizedCallTargetLog {
 
     protected static final PrintStream OUT = TTY.out().out();
 
-    static {
-        if (TruffleCallTargetProfiling.getValue()) {
-            Runtime.getRuntime().addShutdownHook(new Thread() {
-
-                @Override
-                public void run() {
-                    printProfiling();
-                }
-            });
-        }
-    }
-
     private OptimizedCallTargetLog() {
     }
 
@@ -188,63 +173,4 @@
         }
         OUT.println(sb.toString());
     }
-
-    private static int sumCalls(List<OptimizedCallTarget> targets, Function<TraceCompilationProfile, Integer> function) {
-        return targets.stream().collect(Collectors.summingInt(target -> function.apply((TraceCompilationProfile) target.getCompilationProfile())));
-    }
-
-    private static void printProfiling() {
-        // @formatter:off
-        Map<OptimizedCallTarget, List<OptimizedCallTarget>> groupedTargets = Truffle.getRuntime().getCallTargets().stream().
-        map(target -> (OptimizedCallTarget) target).
-        collect(Collectors.groupingBy(target -> {
-            if (target.getSourceCallTarget() != null) {
-                return target.getSourceCallTarget();
-            }
-            return target;
-        }));
-        // @formatter:on
-
-        List<OptimizedCallTarget> uniqueSortedTargets = groupedTargets.keySet().stream().sorted(
-                        (target1, target2) -> sumCalls(groupedTargets.get(target2), p -> p.getTotalCallCount()) - sumCalls(groupedTargets.get(target1), p -> p.getTotalCallCount())).collect(
-                        Collectors.toList());
-        // @formatter:on
-
-        int totalDirectCallCount = 0;
-        int totalInlinedCallCount = 0;
-        int totalIndirectCallCount = 0;
-        int totalTotalCallCount = 0;
-        int totalInterpretedCallCount = 0;
-        int totalInvalidationCount = 0;
-
-        OUT.println();
-        OUT.printf(" %-50s  | %-15s || %-15s | %-15s || %-15s | %-15s | %-15s || %3s \n", "Call Target", "Total Calls", "Interp. Calls", "Opt. Calls", "Direct Calls", "Inlined Calls",
-                        "Indirect Calls", "Invalidations");
-        for (OptimizedCallTarget uniqueCallTarget : uniqueSortedTargets) {
-            List<OptimizedCallTarget> allCallTargets = groupedTargets.get(uniqueCallTarget);
-            int directCallCount = sumCalls(allCallTargets, p -> p.getDirectCallCount());
-            int indirectCallCount = sumCalls(allCallTargets, p -> p.getIndirectCallCount());
-            int inlinedCallCount = sumCalls(allCallTargets, p -> p.getInlinedCallCount());
-            int interpreterCallCount = sumCalls(allCallTargets, p -> p.getInterpreterCallCount());
-            int totalCallCount = sumCalls(allCallTargets, p -> p.getTotalCallCount());
-            int invalidationCount = allCallTargets.stream().collect(Collectors.summingInt(target -> target.getCompilationProfile().getInvalidationCount()));
-
-            totalDirectCallCount += directCallCount;
-            totalInlinedCallCount += inlinedCallCount;
-            totalIndirectCallCount += indirectCallCount;
-            totalInvalidationCount += invalidationCount;
-            totalInterpretedCallCount += interpreterCallCount;
-            totalTotalCallCount += totalCallCount;
-
-            if (totalCallCount > 0) {
-                OUT.printf("  %-50s | %15d || %15d | %15d || %15d | %15d | %15d || %3d\n", uniqueCallTarget, totalCallCount, interpreterCallCount, totalCallCount - interpreterCallCount,
-                                directCallCount, inlinedCallCount, indirectCallCount, invalidationCount);
-            }
-
-        }
-
-        OUT.printf(" %-50s  | %15d || %15d | %15d || %15d | %15d | %15d || %3d\n", "Total", totalTotalCallCount, totalInterpretedCallCount, totalTotalCallCount - totalInterpretedCallCount,
-                        totalDirectCallCount, totalInlinedCallCount, totalIndirectCallCount, totalInvalidationCount);
-
-    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/debug/PrintCallTargetProfiling.java	Mon Oct 27 15:18:14 2014 +0100
@@ -0,0 +1,75 @@
+package com.oracle.graal.truffle.debug;
+
+import static com.oracle.graal.truffle.TruffleCompilerOptions.*;
+
+import java.util.*;
+import java.util.function.*;
+import java.util.stream.*;
+
+import com.oracle.graal.truffle.*;
+import com.oracle.truffle.api.*;
+
+public class PrintCallTargetProfiling extends AbstractDebugCompilationListener {
+
+    public static void install(GraalTruffleRuntime runtime) {
+        if (TruffleCallTargetProfiling.getValue()) {
+            runtime.addCompilationListener(new PrintCallTargetProfiling());
+        }
+    }
+
+    @Override
+    public void notifyShutdown(TruffleRuntime runtime) {
+        Map<OptimizedCallTarget, List<OptimizedCallTarget>> groupedTargets = Truffle.getRuntime().getCallTargets().stream().map(target -> (OptimizedCallTarget) target).collect(
+                        Collectors.groupingBy(target -> {
+                            if (target.getSourceCallTarget() != null) {
+                                return target.getSourceCallTarget();
+                            }
+                            return target;
+                        }));
+
+        List<OptimizedCallTarget> uniqueSortedTargets = groupedTargets.keySet().stream().sorted(
+                        (target1, target2) -> sumCalls(groupedTargets.get(target2), p -> p.getTotalCallCount()) - sumCalls(groupedTargets.get(target1), p -> p.getTotalCallCount())).collect(
+                        Collectors.toList());
+
+        int totalDirectCallCount = 0;
+        int totalInlinedCallCount = 0;
+        int totalIndirectCallCount = 0;
+        int totalTotalCallCount = 0;
+        int totalInterpretedCallCount = 0;
+        int totalInvalidationCount = 0;
+
+        OUT.println();
+        OUT.printf(" %-50s  | %-15s || %-15s | %-15s || %-15s | %-15s | %-15s || %3s \n", "Call Target", "Total Calls", "Interp. Calls", "Opt. Calls", "Direct Calls", "Inlined Calls",
+                        "Indirect Calls", "Invalidations");
+        for (OptimizedCallTarget uniqueCallTarget : uniqueSortedTargets) {
+            List<OptimizedCallTarget> allCallTargets = groupedTargets.get(uniqueCallTarget);
+            int directCallCount = sumCalls(allCallTargets, p -> p.getDirectCallCount());
+            int indirectCallCount = sumCalls(allCallTargets, p -> p.getIndirectCallCount());
+            int inlinedCallCount = sumCalls(allCallTargets, p -> p.getInlinedCallCount());
+            int interpreterCallCount = sumCalls(allCallTargets, p -> p.getInterpreterCallCount());
+            int totalCallCount = sumCalls(allCallTargets, p -> p.getTotalCallCount());
+            int invalidationCount = allCallTargets.stream().collect(Collectors.summingInt(target -> target.getCompilationProfile().getInvalidationCount()));
+
+            totalDirectCallCount += directCallCount;
+            totalInlinedCallCount += inlinedCallCount;
+            totalIndirectCallCount += indirectCallCount;
+            totalInvalidationCount += invalidationCount;
+            totalInterpretedCallCount += interpreterCallCount;
+            totalTotalCallCount += totalCallCount;
+
+            if (totalCallCount > 0) {
+                OUT.printf("  %-50s | %15d || %15d | %15d || %15d | %15d | %15d || %3d\n", uniqueCallTarget, totalCallCount, interpreterCallCount, totalCallCount - interpreterCallCount,
+                                directCallCount, inlinedCallCount, indirectCallCount, invalidationCount);
+            }
+
+        }
+
+        OUT.printf(" %-50s  | %15d || %15d | %15d || %15d | %15d | %15d || %3d\n", "Total", totalTotalCallCount, totalInterpretedCallCount, totalTotalCallCount - totalInterpretedCallCount,
+                        totalDirectCallCount, totalInlinedCallCount, totalIndirectCallCount, totalInvalidationCount);
+
+    }
+
+    private static int sumCalls(List<OptimizedCallTarget> targets, Function<TraceCompilationProfile, Integer> function) {
+        return targets.stream().collect(Collectors.summingInt(target -> function.apply((TraceCompilationProfile) target.getCompilationProfile())));
+    }
+}