< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.printer/src/org/graalvm/compiler/printer/GraphPrinterDumpHandler.java

Print this page




  35 import java.util.Map;
  36 import java.util.WeakHashMap;
  37 
  38 import org.graalvm.compiler.debug.DebugContext;
  39 import org.graalvm.compiler.debug.DebugDumpHandler;
  40 import org.graalvm.compiler.debug.DebugDumpScope;
  41 import org.graalvm.compiler.debug.DebugOptions;
  42 import org.graalvm.compiler.debug.GraalError;
  43 import org.graalvm.compiler.debug.TTY;
  44 import org.graalvm.compiler.graph.Graph;
  45 import org.graalvm.compiler.nodes.StructuredGraph;
  46 import org.graalvm.compiler.options.OptionValues;
  47 import org.graalvm.compiler.phases.contract.NodeCostUtil;
  48 
  49 import jdk.vm.ci.meta.JavaMethod;
  50 import jdk.vm.ci.meta.ResolvedJavaMethod;
  51 
  52 //JaCoCo Exclude
  53 
  54 /**
  55  * Observes compilation events and uses {@link IdealGraphPrinter} to generate a graph representation
  56  * that can be inspected with the Graph Visualizer.
  57  */
  58 public class GraphPrinterDumpHandler implements DebugDumpHandler {
  59 
  60     private static final int FAILURE_LIMIT = 8;
  61     private final GraphPrinterSupplier printerSupplier;
  62     protected GraphPrinter printer;
  63     private List<String> previousInlineContext;
  64     private int[] dumpIds = {};
  65     private int failuresCount;
  66     private Map<Graph, List<String>> inlineContextMap;
  67     private final String jvmArguments;
  68     private final String sunJavaCommand;
  69 
  70     @FunctionalInterface
  71     public interface GraphPrinterSupplier {
  72         GraphPrinter get(Graph graph) throws IOException;
  73     }
  74 
  75     /**
  76      * Creates a new {@link GraphPrinterDumpHandler}.
  77      *
  78      * @param printerSupplier Supplier used to create the GraphPrinter. Should supply an optional or
  79      *            null in case of failure.
  80      */
  81     public GraphPrinterDumpHandler(GraphPrinterSupplier printerSupplier) {
  82         this.printerSupplier = printerSupplier;
  83         /* Add the JVM and Java arguments to the graph properties to help identify it. */
  84         this.jvmArguments = jvmArguments();
  85         this.sunJavaCommand = System.getProperty("sun.java.command");
  86     }
  87 
  88     private static String jvmArguments() {
  89         try {
  90             return String.join(" ", java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments());
  91         } catch (LinkageError err) {
  92             return "unknown";
  93         }
  94     }
  95 
  96     private void ensureInitialized(Graph graph) {
  97         if (printer == null) {
  98             if (failuresCount >= FAILURE_LIMIT) {
  99                 return;
 100             }
 101             previousInlineContext = new ArrayList<>();
 102             inlineContextMap = new WeakHashMap<>();
 103             DebugContext debug = graph.getDebug();
 104             try {
 105                 printer = printerSupplier.get(graph);
 106             } catch (IOException e) {
 107                 handleException(debug, e);
 108             }
 109         }
 110     }
 111 
 112     private int nextDumpId() {
 113         int depth = previousInlineContext.size();
 114         if (dumpIds.length < depth) {
 115             dumpIds = Arrays.copyOf(dumpIds, depth);
 116         }
 117         return dumpIds[depth - 1]++;
 118     }
 119 
 120     @Override
 121     @SuppressWarnings("try")
 122     public void dump(DebugContext debug, Object object, final String format, Object... arguments) {
 123         OptionValues options = debug.getOptions();
 124         if (object instanceof Graph && DebugOptions.PrintGraph.getValue(options)) {
 125             final Graph graph = (Graph) object;
 126             ensureInitialized(graph);
 127             if (printer == null) {
 128                 return;
 129             }
 130 
 131             // Get all current JavaMethod instances in the context.
 132             List<String> inlineContext = getInlineContext(graph);
 133 
 134             if (inlineContext != previousInlineContext) {
 135                 Map<Object, Object> properties = new HashMap<>();
 136                 properties.put("graph", graph.toString());
 137                 addCompilationId(properties, graph);
 138                 if (inlineContext.equals(previousInlineContext)) {
 139                     /*
 140                      * two different graphs have the same inline context, so make sure they appear
 141                      * in different folders by closing and reopening the top scope.
 142                      */
 143                     int inlineDepth = previousInlineContext.size() - 1;
 144                     closeScope(debug, inlineDepth);
 145                     openScope(debug, inlineContext.get(inlineDepth), inlineDepth, properties);
 146                 } else {


 151                                 closeScope(debug, inlineDepth);
 152                             }
 153                             break;
 154                         }
 155                     }
 156                     // Check for method scopes that must be opened since the previous dump.
 157                     for (int i = 0; i < inlineContext.size(); ++i) {
 158                         if (i >= previousInlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
 159                             for (int inlineDepth = i; inlineDepth < inlineContext.size(); ++inlineDepth) {
 160                                 openScope(debug, inlineContext.get(inlineDepth), inlineDepth, inlineDepth == inlineContext.size() - 1 ? properties : null);
 161                             }
 162                             break;
 163                         }
 164                     }
 165                 }
 166             }
 167 
 168             // Save inline context for next dump.
 169             previousInlineContext = inlineContext;
 170 


 171             try (DebugContext.Scope s = debug.sandbox("PrintingGraph", null)) {
 172                 // Finally, output the graph.
 173                 Map<Object, Object> properties = new HashMap<>();
 174                 properties.put("graph", graph.toString());
 175                 properties.put("scope", debug.getCurrentScopeName());
 176                 if (graph instanceof StructuredGraph) {
 177                     properties.put("compilationIdentifier", ((StructuredGraph) graph).compilationId());
 178                     try {
 179                         int size = NodeCostUtil.computeGraphSize((StructuredGraph) graph);
 180                         properties.put("node-cost graph size", size);
 181                     } catch (Throwable t) {
 182                         properties.put("node-cost-exception", t.getMessage());
 183                     }
 184                 }
 185                 printer.print(debug, graph, properties, nextDumpId(), format, arguments);
 186             } catch (IOException e) {
 187                 handleException(debug, e);
 188             } catch (Throwable e) {
 189                 throw debug.handle(e);
 190             }
 191         }
 192     }
 193 
 194     void handleException(DebugContext debug, IOException e) {
 195         if (debug != null && DebugOptions.DumpingErrorsAreFatal.getValue(debug.getOptions())) {




  35 import java.util.Map;
  36 import java.util.WeakHashMap;
  37 
  38 import org.graalvm.compiler.debug.DebugContext;
  39 import org.graalvm.compiler.debug.DebugDumpHandler;
  40 import org.graalvm.compiler.debug.DebugDumpScope;
  41 import org.graalvm.compiler.debug.DebugOptions;
  42 import org.graalvm.compiler.debug.GraalError;
  43 import org.graalvm.compiler.debug.TTY;
  44 import org.graalvm.compiler.graph.Graph;
  45 import org.graalvm.compiler.nodes.StructuredGraph;
  46 import org.graalvm.compiler.options.OptionValues;
  47 import org.graalvm.compiler.phases.contract.NodeCostUtil;
  48 
  49 import jdk.vm.ci.meta.JavaMethod;
  50 import jdk.vm.ci.meta.ResolvedJavaMethod;
  51 
  52 //JaCoCo Exclude
  53 
  54 /**
  55  * Observes compilation events and uses {@link BinaryGraphPrinter} to generate a graph
  56  * representation that can be inspected with the Graph Visualizer.
  57  */
  58 public class GraphPrinterDumpHandler implements DebugDumpHandler {
  59 
  60     private static final int FAILURE_LIMIT = 8;
  61     private final GraphPrinterSupplier printerSupplier;
  62     protected GraphPrinter printer;
  63     private List<String> previousInlineContext;
  64     private int[] dumpIds = {};
  65     private int failuresCount;
  66     private Map<Graph, List<String>> inlineContextMap;
  67     private final String jvmArguments;
  68     private final String sunJavaCommand;
  69 
  70     @FunctionalInterface
  71     public interface GraphPrinterSupplier {
  72         GraphPrinter get(DebugContext ctx, Graph graph) throws IOException;
  73     }
  74 
  75     /**
  76      * Creates a new {@link GraphPrinterDumpHandler}.
  77      *
  78      * @param printerSupplier Supplier used to create the GraphPrinter. Should supply an optional or
  79      *            null in case of failure.
  80      */
  81     public GraphPrinterDumpHandler(GraphPrinterSupplier printerSupplier) {
  82         this.printerSupplier = printerSupplier;
  83         /* Add the JVM and Java arguments to the graph properties to help identify it. */
  84         this.jvmArguments = jvmArguments();
  85         this.sunJavaCommand = System.getProperty("sun.java.command");
  86     }
  87 
  88     private static String jvmArguments() {
  89         try {
  90             return String.join(" ", java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments());
  91         } catch (LinkageError err) {
  92             return "unknown";
  93         }
  94     }
  95 
  96     private void ensureInitialized(DebugContext ctx, Graph graph) {
  97         if (printer == null) {
  98             if (failuresCount >= FAILURE_LIMIT) {
  99                 return;
 100             }
 101             previousInlineContext = new ArrayList<>();
 102             inlineContextMap = new WeakHashMap<>();
 103             DebugContext debug = graph.getDebug();
 104             try {
 105                 printer = printerSupplier.get(ctx, graph);
 106             } catch (IOException e) {
 107                 handleException(debug, e);
 108             }
 109         }
 110     }
 111 
 112     private int nextDumpId() {
 113         int depth = previousInlineContext.size();
 114         if (dumpIds.length < depth) {
 115             dumpIds = Arrays.copyOf(dumpIds, depth);
 116         }
 117         return dumpIds[depth - 1]++;
 118     }
 119 
 120     @Override
 121     @SuppressWarnings("try")
 122     public void dump(DebugContext debug, Object object, final String format, Object... arguments) {
 123         OptionValues options = debug.getOptions();
 124         if (object instanceof Graph && DebugOptions.PrintGraph.getValue(options)) {
 125             final Graph graph = (Graph) object;
 126             ensureInitialized(debug, graph);
 127             if (printer == null) {
 128                 return;
 129             }
 130 
 131             // Get all current JavaMethod instances in the context.
 132             List<String> inlineContext = getInlineContext(graph);
 133 
 134             if (inlineContext != previousInlineContext) {
 135                 Map<Object, Object> properties = new HashMap<>();
 136                 properties.put("graph", graph.toString());
 137                 addCompilationId(properties, graph);
 138                 if (inlineContext.equals(previousInlineContext)) {
 139                     /*
 140                      * two different graphs have the same inline context, so make sure they appear
 141                      * in different folders by closing and reopening the top scope.
 142                      */
 143                     int inlineDepth = previousInlineContext.size() - 1;
 144                     closeScope(debug, inlineDepth);
 145                     openScope(debug, inlineContext.get(inlineDepth), inlineDepth, properties);
 146                 } else {


 151                                 closeScope(debug, inlineDepth);
 152                             }
 153                             break;
 154                         }
 155                     }
 156                     // Check for method scopes that must be opened since the previous dump.
 157                     for (int i = 0; i < inlineContext.size(); ++i) {
 158                         if (i >= previousInlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
 159                             for (int inlineDepth = i; inlineDepth < inlineContext.size(); ++inlineDepth) {
 160                                 openScope(debug, inlineContext.get(inlineDepth), inlineDepth, inlineDepth == inlineContext.size() - 1 ? properties : null);
 161                             }
 162                             break;
 163                         }
 164                     }
 165                 }
 166             }
 167 
 168             // Save inline context for next dump.
 169             previousInlineContext = inlineContext;
 170 
 171             // Capture before creating the sandbox
 172             String currentScopeName = debug.getCurrentScopeName();
 173             try (DebugContext.Scope s = debug.sandbox("PrintingGraph", null)) {
 174                 // Finally, output the graph.
 175                 Map<Object, Object> properties = new HashMap<>();
 176                 properties.put("graph", graph.toString());
 177                 properties.put("scope", currentScopeName);
 178                 if (graph instanceof StructuredGraph) {
 179                     properties.put("compilationIdentifier", ((StructuredGraph) graph).compilationId());
 180                     try {
 181                         int size = NodeCostUtil.computeGraphSize((StructuredGraph) graph);
 182                         properties.put("node-cost graph size", size);
 183                     } catch (Throwable t) {
 184                         properties.put("node-cost-exception", t.getMessage());
 185                     }
 186                 }
 187                 printer.print(debug, graph, properties, nextDumpId(), format, arguments);
 188             } catch (IOException e) {
 189                 handleException(debug, e);
 190             } catch (Throwable e) {
 191                 throw debug.handle(e);
 192             }
 193         }
 194     }
 195 
 196     void handleException(DebugContext debug, IOException e) {
 197         if (debug != null && DebugOptions.DumpingErrorsAreFatal.getValue(debug.getOptions())) {


< prev index next >