hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinterDumpHandler.java
changeset 46344 694c102fd8ed
parent 43972 1ade39b8381b
child 46371 0337d0617e7b
--- a/hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinterDumpHandler.java	Mon Dec 12 16:16:27 2016 +0300
+++ b/hotspot/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinterDumpHandler.java	Wed Mar 22 13:42:45 2017 -0700
@@ -26,6 +26,7 @@
 
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
+import java.nio.channels.ClosedByInterruptException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -35,12 +36,17 @@
 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.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.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;
 
@@ -56,8 +62,10 @@
  */
 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;
@@ -85,16 +93,18 @@
 
     private void ensureInitialized() {
         if (printer == null) {
-            if (failuresCount > 8) {
+            if (failuresCount >= FAILURE_LIMIT) {
                 return;
             }
             previousInlineContext = new ArrayList<>();
             inlineContextMap = new WeakHashMap<>();
             try {
                 printer = printerSupplier.get();
+                if (snippetReflection != null) {
+                    printer.setSnippetReflectionProvider(snippetReflection);
+                }
             } catch (IOException e) {
-                TTY.println(e.getMessage());
-                failuresCount++;
+                handleException(e);
             }
         }
     }
@@ -110,7 +120,7 @@
     @Override
     @SuppressWarnings("try")
     public void dump(Object object, final String message) {
-        if (object instanceof Graph && Options.PrintIdealGraph.getValue()) {
+        if (object instanceof Graph && Options.PrintGraph.getValue(DebugScope.getConfig().getOptions())) {
             ensureInitialized();
             if (printer == null) {
                 return;
@@ -166,14 +176,34 @@
                 addCFGFileName(properties);
                 printer.print(graph, nextDumpId() + ":" + message, properties);
             } catch (IOException e) {
-                failuresCount++;
-                printer = null;
+                handleException(e);
             } catch (Throwable e) {
                 throw Debug.handle(e);
             }
         }
     }
 
+    void handleException(IOException e) {
+        if (GraalDebugConfig.Options.DumpingErrorsAreFatal.getValue(DebugScope.getConfig().getOptions())) {
+            throw new GraalError(e);
+        }
+        if (e instanceof ClosedByInterruptException) {
+            /*
+             * The current dumping was aborted by an interrupt so treat this as a transient failure.
+             */
+            failuresCount = 0;
+        } else {
+            failuresCount++;
+        }
+        printer = null;
+        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());
+        }
+    }
+
     private static void addCompilationId(Map<Object, Object> properties, final Graph graph) {
         if (graph instanceof StructuredGraph) {
             properties.put("compilationId", ((StructuredGraph) graph).compilationId());
@@ -181,8 +211,18 @@
     }
 
     private static void addCFGFileName(Map<Object, Object> properties) {
-        if (Options.PrintCFG.getValue() || Options.PrintBackendCFG.getValue()) {
-            properties.put("PrintCFGFileName", CFGPrinterObserver.getCFGPath().toAbsolutePath().toString());
+        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;
+                }
+            }
         }
     }
 
@@ -274,8 +314,7 @@
             }
             printer.beginGroup(prefix + name, name, Debug.contextLookup(ResolvedJavaMethod.class), -1, props);
         } catch (IOException e) {
-            failuresCount++;
-            printer = null;
+            handleException(e);
         }
     }
 
@@ -284,8 +323,7 @@
         try {
             printer.endGroup();
         } catch (IOException e) {
-            failuresCount++;
-            printer = null;
+            handleException(e);
         }
     }
 
@@ -301,4 +339,14 @@
             printer = null;
         }
     }
+
+    @Override
+    public void addCapability(Object capability) {
+        if (capability instanceof SnippetReflectionProvider) {
+            snippetReflection = (SnippetReflectionProvider) capability;
+            if (printer != null && printer.getSnippetReflectionProvider() == null) {
+                printer.setSnippetReflectionProvider(snippetReflection);
+            }
+        }
+    }
 }