1 /*
2 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
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
56 //JaCoCo Exclude
57
58 /**
59 * Observes compilation events and uses {@link BinaryGraphPrinter} to generate a graph
60 * representation that can be inspected with the Graph Visualizer.
61 */
62 public class GraphPrinterDumpHandler implements DebugDumpHandler {
63
64 private static final int FAILURE_LIMIT = 8;
65 private final GraphPrinterSupplier printerSupplier;
66 protected GraphPrinter printer;
67 private List<String> previousInlineContext;
68 private int[] dumpIds = {};
69 private int failuresCount;
70 private Map<Graph, List<String>> inlineContextMap;
71 private final String jvmArguments;
72 private final String sunJavaCommand;
73
74 @FunctionalInterface
75 public interface GraphPrinterSupplier {
76 GraphPrinter get(DebugContext ctx, Graph graph) throws IOException;
77 }
78
79 /**
80 * Creates a new {@link GraphPrinterDumpHandler}.
81 *
82 * @param printerSupplier Supplier used to create the GraphPrinter. Should supply an optional or
83 * null in case of failure.
84 */
85 public GraphPrinterDumpHandler(GraphPrinterSupplier printerSupplier) {
86 this.printerSupplier = printerSupplier;
87 /* Add the JVM and Java arguments to the graph properties to help identify it. */
88 this.jvmArguments = jvmArguments();
89 this.sunJavaCommand = System.getProperty("sun.java.command");
90 }
91
92 private static String jvmArguments() {
93 List<String> inputArguments = GraalServices.getInputArguments();
94 if (inputArguments != null) {
95 return String.join(" ", inputArguments);
96 }
97 return "unknown";
98 }
99
100 private void ensureInitialized(DebugContext ctx, Graph graph) {
101 if (printer == null) {
102 if (failuresCount >= FAILURE_LIMIT) {
103 return;
104 }
105 previousInlineContext = new ArrayList<>();
106 inlineContextMap = new WeakHashMap<>();
107 DebugContext debug = graph.getDebug();
108 try {
109 printer = printerSupplier.get(ctx, graph);
|
1 /*
2 * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
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. */
89 this.jvmArguments = jvmArguments();
90 this.sunJavaCommand = Services.getSavedProperties().get("sun.java.command");
91 }
92
93 private static String jvmArguments() {
94 List<String> inputArguments = GraalServices.getInputArguments();
95 if (inputArguments != null) {
96 return String.join(" ", inputArguments);
97 }
98 return "unknown";
99 }
100
101 private void ensureInitialized(DebugContext ctx, Graph graph) {
102 if (printer == null) {
103 if (failuresCount >= FAILURE_LIMIT) {
104 return;
105 }
106 previousInlineContext = new ArrayList<>();
107 inlineContextMap = new WeakHashMap<>();
108 DebugContext debug = graph.getDebug();
109 try {
110 printer = printerSupplier.get(ctx, graph);
|