20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.printer;
26
27 import static org.graalvm.compiler.debug.DebugConfig.asJavaMethod;
28
29 import java.io.IOException;
30 import java.nio.channels.ClosedByInterruptException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.WeakHashMap;
39
40 import org.graalvm.compiler.debug.DebugContext;
41 import org.graalvm.compiler.debug.DebugDumpHandler;
42 import org.graalvm.compiler.debug.DebugDumpScope;
43 import org.graalvm.compiler.debug.DebugOptions;
44 import org.graalvm.compiler.debug.GraalError;
45 import org.graalvm.compiler.debug.TTY;
46 import org.graalvm.compiler.debug.DebugOptions.PrintGraphTarget;
47 import org.graalvm.compiler.graph.Graph;
48 import org.graalvm.compiler.nodes.StructuredGraph;
49 import org.graalvm.compiler.options.OptionValues;
50 import org.graalvm.compiler.phases.contract.NodeCostUtil;
51 import org.graalvm.compiler.serviceprovider.GraalServices;
52
53 import jdk.vm.ci.meta.JavaMethod;
54 import jdk.vm.ci.meta.ResolvedJavaMethod;
55 import jdk.vm.ci.services.Services;
56
57 //JaCoCo Exclude
58
59 /**
60 * Observes compilation events and uses {@link BinaryGraphPrinter} to generate a graph
61 * representation that can be inspected with the Graph Visualizer.
62 */
63 public class GraphPrinterDumpHandler implements DebugDumpHandler {
64
65 private static final int FAILURE_LIMIT = 8;
66 private final GraphPrinterSupplier printerSupplier;
67 protected GraphPrinter printer;
68 private List<String> previousInlineContext;
69 private int[] dumpIds = {};
70 private int failuresCount;
71 private Map<Graph, List<String>> inlineContextMap;
72 private final String jvmArguments;
73 private final String sunJavaCommand;
74
75 @FunctionalInterface
76 public interface GraphPrinterSupplier {
77 GraphPrinter get(DebugContext ctx, Graph graph) throws IOException;
78 }
79
80 /**
81 * Creates a new {@link GraphPrinterDumpHandler}.
82 *
83 * @param printerSupplier Supplier used to create the GraphPrinter. Should supply an optional or
84 * null in case of failure.
85 */
86 public GraphPrinterDumpHandler(GraphPrinterSupplier printerSupplier) {
87 this.printerSupplier = printerSupplier;
88 /* Add the JVM and Java arguments to the graph properties to help identify it. */
118 int depth = previousInlineContext.size();
119 if (dumpIds.length < depth) {
120 dumpIds = Arrays.copyOf(dumpIds, depth);
121 }
122 return dumpIds[depth - 1]++;
123 }
124
125 @Override
126 @SuppressWarnings("try")
127 public void dump(DebugContext debug, Object object, final String format, Object... arguments) {
128 OptionValues options = debug.getOptions();
129 if (object instanceof Graph && DebugOptions.PrintGraph.getValue(options) != PrintGraphTarget.Disable) {
130 final Graph graph = (Graph) object;
131 ensureInitialized(debug, graph);
132 if (printer == null) {
133 return;
134 }
135
136 // Get all current JavaMethod instances in the context.
137 List<String> inlineContext = getInlineContext(graph);
138
139 if (!inlineContext.equals(previousInlineContext)) {
140 Map<Object, Object> properties = new HashMap<>();
141 properties.put("graph", graph.toString());
142 addCompilationId(properties, graph);
143 // Check for method scopes that must be closed since the previous dump.
144 for (int i = 0; i < previousInlineContext.size(); ++i) {
145 if (i >= inlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
146 for (int inlineDepth = previousInlineContext.size() - 1; inlineDepth >= i; --inlineDepth) {
147 closeScope(debug, inlineDepth);
148 }
149 break;
150 }
151 }
152 // Check for method scopes that must be opened since the previous dump.
153 for (int i = 0; i < inlineContext.size(); ++i) {
154 if (i >= previousInlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
155 for (int inlineDepth = i; inlineDepth < inlineContext.size(); ++inlineDepth) {
156 openScope(debug, inlineContext.get(inlineDepth), inlineDepth, inlineDepth == inlineContext.size() - 1 ? properties : null);
157 }
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 package org.graalvm.compiler.printer;
26
27 import static org.graalvm.compiler.debug.DebugConfig.asJavaMethod;
28
29 import java.io.IOException;
30 import java.nio.channels.ClosedByInterruptException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.Date;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.WeakHashMap;
39
40 import org.graalvm.compiler.core.common.CompilationIdentifier;
41 import org.graalvm.compiler.debug.DebugContext;
42 import org.graalvm.compiler.debug.DebugDumpHandler;
43 import org.graalvm.compiler.debug.DebugDumpScope;
44 import org.graalvm.compiler.debug.DebugOptions;
45 import org.graalvm.compiler.debug.DebugOptions.PrintGraphTarget;
46 import org.graalvm.compiler.debug.GraalError;
47 import org.graalvm.compiler.debug.TTY;
48 import org.graalvm.compiler.graph.Graph;
49 import org.graalvm.compiler.nodes.StructuredGraph;
50 import org.graalvm.compiler.options.OptionValues;
51 import org.graalvm.compiler.phases.contract.NodeCostUtil;
52 import org.graalvm.compiler.serviceprovider.GraalServices;
53
54 import jdk.vm.ci.meta.JavaMethod;
55 import jdk.vm.ci.meta.ResolvedJavaMethod;
56 import jdk.vm.ci.services.Services;
57
58 //JaCoCo Exclude
59
60 /**
61 * Observes compilation events and uses {@link BinaryGraphPrinter} to generate a graph
62 * representation that can be inspected with the Graph Visualizer.
63 */
64 public class GraphPrinterDumpHandler implements DebugDumpHandler {
65
66 private static final int FAILURE_LIMIT = 8;
67 private final GraphPrinterSupplier printerSupplier;
68 protected GraphPrinter printer;
69 private List<String> previousInlineContext;
70 private CompilationIdentifier previousCompilationID = CompilationIdentifier.INVALID_COMPILATION_ID;
71 private int[] dumpIds = {};
72 private int failuresCount;
73 private Map<Graph, List<String>> inlineContextMap;
74 private final String jvmArguments;
75 private final String sunJavaCommand;
76
77 @FunctionalInterface
78 public interface GraphPrinterSupplier {
79 GraphPrinter get(DebugContext ctx, Graph graph) throws IOException;
80 }
81
82 /**
83 * Creates a new {@link GraphPrinterDumpHandler}.
84 *
85 * @param printerSupplier Supplier used to create the GraphPrinter. Should supply an optional or
86 * null in case of failure.
87 */
88 public GraphPrinterDumpHandler(GraphPrinterSupplier printerSupplier) {
89 this.printerSupplier = printerSupplier;
90 /* Add the JVM and Java arguments to the graph properties to help identify it. */
120 int depth = previousInlineContext.size();
121 if (dumpIds.length < depth) {
122 dumpIds = Arrays.copyOf(dumpIds, depth);
123 }
124 return dumpIds[depth - 1]++;
125 }
126
127 @Override
128 @SuppressWarnings("try")
129 public void dump(DebugContext debug, Object object, final String format, Object... arguments) {
130 OptionValues options = debug.getOptions();
131 if (object instanceof Graph && DebugOptions.PrintGraph.getValue(options) != PrintGraphTarget.Disable) {
132 final Graph graph = (Graph) object;
133 ensureInitialized(debug, graph);
134 if (printer == null) {
135 return;
136 }
137
138 // Get all current JavaMethod instances in the context.
139 List<String> inlineContext = getInlineContext(graph);
140
141 if (graph instanceof StructuredGraph) {
142 CompilationIdentifier compilationID = ((StructuredGraph) graph).compilationId();
143 // If the graph to be dumped is with an invalid compilation id, it is likely derived
144 // from inlining.
145 if (compilationID != CompilationIdentifier.INVALID_COMPILATION_ID) {
146 if (previousCompilationID != CompilationIdentifier.INVALID_COMPILATION_ID && !compilationID.equals(previousCompilationID)) {
147 // Compilation ID does not match, close existing scopes.
148 for (int inlineDepth = previousInlineContext.size() - 1; inlineDepth >= 0; --inlineDepth) {
149 closeScope(debug, inlineDepth);
150 }
151 previousInlineContext = new ArrayList<>();
152 }
153 previousCompilationID = compilationID;
154 }
155 }
156
157 if (!inlineContext.equals(previousInlineContext)) {
158 Map<Object, Object> properties = new HashMap<>();
159 properties.put("graph", graph.toString());
160 addCompilationId(properties, graph);
161 // Check for method scopes that must be closed since the previous dump.
162 for (int i = 0; i < previousInlineContext.size(); ++i) {
163 if (i >= inlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
164 for (int inlineDepth = previousInlineContext.size() - 1; inlineDepth >= i; --inlineDepth) {
165 closeScope(debug, inlineDepth);
166 }
167 break;
168 }
169 }
170 // Check for method scopes that must be opened since the previous dump.
171 for (int i = 0; i < inlineContext.size(); ++i) {
172 if (i >= previousInlineContext.size() || !inlineContext.get(i).equals(previousInlineContext.get(i))) {
173 for (int inlineDepth = i; inlineDepth < inlineContext.size(); ++inlineDepth) {
174 openScope(debug, inlineContext.get(inlineDepth), inlineDepth, inlineDepth == inlineContext.size() - 1 ? properties : null);
175 }
|