hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinterDumpHandler.java
changeset 46640 70bdce04c59b
parent 46509 b32d3928ad6a
child 46807 8b2c620d7092
--- a/hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinterDumpHandler.java	Fri Jul 07 10:37:52 2017 +0200
+++ b/hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinterDumpHandler.java	Fri Jul 07 09:40:47 2017 -0700
@@ -22,7 +22,7 @@
  */
 package org.graalvm.compiler.printer;
 
-import static org.graalvm.compiler.debug.GraalDebugConfig.asJavaMethod;
+import static org.graalvm.compiler.debug.DebugConfig.asJavaMethod;
 
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
@@ -36,19 +36,15 @@
 import java.util.Map;
 import java.util.WeakHashMap;
 
-import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
-import org.graalvm.compiler.debug.Debug;
-import org.graalvm.compiler.debug.Debug.Scope;
-import org.graalvm.compiler.debug.DebugConfig;
+import org.graalvm.compiler.debug.DebugContext;
 import org.graalvm.compiler.debug.DebugDumpHandler;
 import org.graalvm.compiler.debug.DebugDumpScope;
-import org.graalvm.compiler.debug.GraalDebugConfig;
-import org.graalvm.compiler.debug.GraalDebugConfig.Options;
+import org.graalvm.compiler.debug.DebugOptions;
 import org.graalvm.compiler.debug.GraalError;
 import org.graalvm.compiler.debug.TTY;
-import org.graalvm.compiler.debug.internal.DebugScope;
 import org.graalvm.compiler.graph.Graph;
 import org.graalvm.compiler.nodes.StructuredGraph;
+import org.graalvm.compiler.options.OptionValues;
 import org.graalvm.compiler.phases.contract.NodeCostUtil;
 
 import jdk.vm.ci.meta.JavaMethod;
@@ -58,15 +54,13 @@
 
 /**
  * Observes compilation events and uses {@link IdealGraphPrinter} to generate a graph representation
- * that can be inspected with the <a href="http://kenai.com/projects/igv">Ideal Graph Visualizer</a>
- * .
+ * that can be inspected with the Graph Visualizer.
  */
 public class GraphPrinterDumpHandler implements DebugDumpHandler {
 
     private static final int FAILURE_LIMIT = 8;
     private final GraphPrinterSupplier printerSupplier;
     protected GraphPrinter printer;
-    private SnippetReflectionProvider snippetReflection;
     private List<String> previousInlineContext;
     private int[] dumpIds = {};
     private int failuresCount;
@@ -76,7 +70,7 @@
 
     @FunctionalInterface
     public interface GraphPrinterSupplier {
-        GraphPrinter get() throws IOException;
+        GraphPrinter get(Graph graph) throws IOException;
     }
 
     /**
@@ -92,20 +86,18 @@
         this.sunJavaCommand = System.getProperty("sun.java.command");
     }
 
-    private void ensureInitialized() {
+    private void ensureInitialized(Graph graph) {
         if (printer == null) {
             if (failuresCount >= FAILURE_LIMIT) {
                 return;
             }
             previousInlineContext = new ArrayList<>();
             inlineContextMap = new WeakHashMap<>();
+            DebugContext debug = graph.getDebug();
             try {
-                printer = printerSupplier.get();
-                if (snippetReflection != null) {
-                    printer.setSnippetReflectionProvider(snippetReflection);
-                }
+                printer = printerSupplier.get(graph);
             } catch (IOException e) {
-                handleException(e);
+                handleException(debug, e);
             }
         }
     }
@@ -120,13 +112,14 @@
 
     @Override
     @SuppressWarnings("try")
-    public void dump(Object object, final String format, Object... arguments) {
-        if (object instanceof Graph && Options.PrintGraph.getValue(DebugScope.getConfig().getOptions())) {
-            ensureInitialized();
+    public void dump(DebugContext debug, Object object, final String format, Object... arguments) {
+        OptionValues options = debug.getOptions();
+        if (object instanceof Graph && DebugOptions.PrintGraph.getValue(options)) {
+            final Graph graph = (Graph) object;
+            ensureInitialized(graph);
             if (printer == null) {
                 return;
             }
-            final Graph graph = (Graph) object;
 
             // Get all current JavaMethod instances in the context.
             List<String> inlineContext = getInlineContext(graph);
@@ -135,21 +128,20 @@
                 Map<Object, Object> properties = new HashMap<>();
                 properties.put("graph", graph.toString());
                 addCompilationId(properties, graph);
-                addCFGFileName(properties);
                 if (inlineContext.equals(previousInlineContext)) {
                     /*
                      * two different graphs have the same inline context, so make sure they appear
                      * in different folders by closing and reopening the top scope.
                      */
                     int inlineDepth = previousInlineContext.size() - 1;
-                    closeScope(inlineDepth);
-                    openScope(inlineContext.get(inlineDepth), inlineDepth, properties);
+                    closeScope(debug, inlineDepth);
+                    openScope(debug, inlineContext.get(inlineDepth), inlineDepth, properties);
                 } else {
                     // Check for method scopes that must be closed since the previous dump.
                     for (int i = 0; i < previousInlineContext.size(); ++i) {
                         if (i >= inlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
                             for (int inlineDepth = previousInlineContext.size() - 1; inlineDepth >= i; --inlineDepth) {
-                                closeScope(inlineDepth);
+                                closeScope(debug, inlineDepth);
                             }
                             break;
                         }
@@ -158,7 +150,7 @@
                     for (int i = 0; i < inlineContext.size(); ++i) {
                         if (i >= previousInlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
                             for (int inlineDepth = i; inlineDepth < inlineContext.size(); ++inlineDepth) {
-                                openScope(inlineContext.get(inlineDepth), inlineDepth, inlineDepth == inlineContext.size() - 1 ? properties : null);
+                                openScope(debug, inlineContext.get(inlineDepth), inlineDepth, inlineDepth == inlineContext.size() - 1 ? properties : null);
                             }
                             break;
                         }
@@ -169,11 +161,11 @@
             // Save inline context for next dump.
             previousInlineContext = inlineContext;
 
-            try (Scope s = Debug.sandbox("PrintingGraph", null)) {
+            try (DebugContext.Scope s = debug.sandbox("PrintingGraph", null)) {
                 // Finally, output the graph.
                 Map<Object, Object> properties = new HashMap<>();
                 properties.put("graph", graph.toString());
-                properties.put("scope", Debug.currentScope());
+                properties.put("scope", debug.getCurrentScopeName());
                 if (graph instanceof StructuredGraph) {
                     properties.put("compilationIdentifier", ((StructuredGraph) graph).compilationId());
                     try {
@@ -183,18 +175,17 @@
                         properties.put("node-cost-exception", t.getMessage());
                     }
                 }
-                addCFGFileName(properties);
-                printer.print(graph, properties, nextDumpId(), format, arguments);
+                printer.print(debug, graph, properties, nextDumpId(), format, arguments);
             } catch (IOException e) {
-                handleException(e);
+                handleException(debug, e);
             } catch (Throwable e) {
-                throw Debug.handle(e);
+                throw debug.handle(e);
             }
         }
     }
 
-    void handleException(IOException e) {
-        if (GraalDebugConfig.Options.DumpingErrorsAreFatal.getValue(DebugScope.getConfig().getOptions())) {
+    void handleException(DebugContext debug, IOException e) {
+        if (debug != null && DebugOptions.DumpingErrorsAreFatal.getValue(debug.getOptions())) {
             throw new GraalError(e);
         }
         if (e instanceof ClosedByInterruptException) {
@@ -206,11 +197,9 @@
             failuresCount++;
         }
         printer = null;
+        e.printStackTrace(TTY.out);
         if (failuresCount > FAILURE_LIMIT) {
-            e.printStackTrace(TTY.out);
-            TTY.println("Too many failures with dumping.  Disabling dump in thread " + Thread.currentThread());
-        } else {
-            TTY.println(e.getMessage());
+            TTY.println("Too many failures with dumping. Disabling dump in thread " + Thread.currentThread());
         }
     }
 
@@ -220,29 +209,14 @@
         }
     }
 
-    private static void addCFGFileName(Map<Object, Object> properties) {
-        DebugConfig config = DebugScope.getConfig();
-        if (config != null) {
-            for (DebugDumpHandler dumpHandler : config.dumpHandlers()) {
-                if (dumpHandler instanceof CFGPrinterObserver) {
-                    CFGPrinterObserver cfg = (CFGPrinterObserver) dumpHandler;
-                    String path = cfg.getDumpPath();
-                    if (path != null) {
-                        properties.put("PrintCFGFileName", path);
-                    }
-                    return;
-                }
-            }
-        }
-    }
-
     private List<String> getInlineContext(Graph graph) {
         List<String> result = inlineContextMap.get(graph);
         if (result == null) {
             result = new ArrayList<>();
             Object lastMethodOrGraph = null;
             boolean graphSeen = false;
-            for (Object o : Debug.context()) {
+            DebugContext debug = graph.getDebug();
+            for (Object o : debug.context()) {
                 if (o == graph) {
                     graphSeen = true;
                 }
@@ -307,7 +281,7 @@
         }
     }
 
-    private void openScope(String name, int inlineDepth, Map<Object, Object> properties) {
+    private void openScope(DebugContext debug, String name, int inlineDepth, Map<Object, Object> properties) {
         try {
             Map<Object, Object> props = properties;
             if (inlineDepth == 0) {
@@ -321,18 +295,20 @@
                 }
                 props.put("date", new Date().toString());
             }
-            printer.beginGroup(name, name, Debug.contextLookup(ResolvedJavaMethod.class), -1, props);
+            printer.beginGroup(debug, name, name, debug.contextLookup(ResolvedJavaMethod.class), -1, props);
         } catch (IOException e) {
-            handleException(e);
+            handleException(debug, e);
         }
     }
 
-    private void closeScope(int inlineDepth) {
+    private void closeScope(DebugContext debug, int inlineDepth) {
         dumpIds[inlineDepth] = 0;
         try {
-            printer.endGroup();
+            if (printer != null) {
+                printer.endGroup();
+            }
         } catch (IOException e) {
-            handleException(e);
+            handleException(debug, e);
         }
     }
 
@@ -340,7 +316,7 @@
     public void close() {
         if (previousInlineContext != null) {
             for (int inlineDepth = 0; inlineDepth < previousInlineContext.size(); inlineDepth++) {
-                closeScope(inlineDepth);
+                closeScope(null, inlineDepth);
             }
         }
         if (printer != null) {
@@ -348,14 +324,4 @@
             printer = null;
         }
     }
-
-    @Override
-    public void addCapability(Object capability) {
-        if (capability instanceof SnippetReflectionProvider) {
-            snippetReflection = (SnippetReflectionProvider) capability;
-            if (printer != null && printer.getSnippetReflectionProvider() == null) {
-                printer.setSnippetReflectionProvider(snippetReflection);
-            }
-        }
-    }
 }