package org.graalvm.compiler.printer;
import static org.graalvm.compiler.debug.GraalDebugConfig.asJavaMethod;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.Debug.Scope;
import org.graalvm.compiler.debug.DebugDumpHandler;
import org.graalvm.compiler.debug.DebugDumpScope;
import org.graalvm.compiler.debug.GraalDebugConfig.Options;
import org.graalvm.compiler.debug.TTY;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.nodes.StructuredGraph;
import jdk.vm.ci.meta.JavaMethod;
import jdk.vm.ci.meta.ResolvedJavaMethod;
public class GraphPrinterDumpHandler implements DebugDumpHandler {
private final GraphPrinterSupplier printerSupplier;
protected GraphPrinter printer;
private List<String> previousInlineContext;
private int[] dumpIds = {};
private int failuresCount;
private Map<Graph, List<String>> inlineContextMap;
private final String jvmArguments;
private final String sunJavaCommand;
@FunctionalInterface
public interface GraphPrinterSupplier {
GraphPrinter get() throws IOException;
}
public GraphPrinterDumpHandler(GraphPrinterSupplier printerSupplier) {
this.printerSupplier = printerSupplier;
this.jvmArguments = String.join(" ", ManagementFactory.getRuntimeMXBean().getInputArguments());
this.sunJavaCommand = System.getProperty("sun.java.command");
}
private void ensureInitialized() {
if (printer == null) {
if (failuresCount > 8) {
return;
}
previousInlineContext = new ArrayList<>();
inlineContextMap = new WeakHashMap<>();
try {
printer = printerSupplier.get();
} catch (IOException e) {
TTY.println(e.getMessage());
failuresCount++;
}
}
}
private int nextDumpId() {
int depth = previousInlineContext.size();
if (dumpIds.length < depth) {
dumpIds = Arrays.copyOf(dumpIds, depth);
}
return dumpIds[depth - 1]++;
}
@Override
@SuppressWarnings("try")
public void dump(Object object, final String message) {
if (object instanceof Graph && Options.PrintIdealGraph.getValue()) {
ensureInitialized();
if (printer == null) {
return;
}
final Graph graph = (Graph) object;
List<String> inlineContext = getInlineContext(graph);
if (inlineContext != previousInlineContext) {
Map<Object, Object> properties = new HashMap<>();
properties.put("graph", graph.toString());
addCompilationId(properties, graph);
addCFGFileName(properties);
if (inlineContext.equals(previousInlineContext)) {
int inlineDepth = previousInlineContext.size() - 1;
closeScope(inlineDepth);
openScope(inlineContext.get(inlineDepth), inlineDepth, properties);
} else {
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);
}
break;
}
}
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);
}
break;
}
}
}
}
previousInlineContext = inlineContext;
try (Scope s = Debug.sandbox("PrintingGraph", null)) {
Map<Object, Object> properties = new HashMap<>();
properties.put("graph", graph.toString());
properties.put("scope", Debug.currentScope());
addCFGFileName(properties);
printer.print(graph, nextDumpId() + ":" + message, properties);
} catch (IOException e) {
failuresCount++;
printer = null;
} catch (Throwable e) {
throw Debug.handle(e);
}
}
}
private static void addCompilationId(Map<Object, Object> properties, final Graph graph) {
if (graph instanceof StructuredGraph) {
properties.put("compilationId", ((StructuredGraph) graph).compilationId());
}
}
private static void addCFGFileName(Map<Object, Object> properties) {
if (Options.PrintCFG.getValue() || Options.PrintBackendCFG.getValue()) {
properties.put("PrintCFGFileName", CFGPrinterObserver.getCFGPath().toAbsolutePath().toString());
}
}
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()) {
if (o == graph) {
graphSeen = true;
}
if (o instanceof DebugDumpScope) {
DebugDumpScope debugDumpScope = (DebugDumpScope) o;
if (debugDumpScope.decorator && !result.isEmpty()) {
result.set(result.size() - 1, debugDumpScope.name + ":" + result.get(result.size() - 1));
} else {
result.add(debugDumpScope.name);
}
} else {
addMethodContext(result, o, lastMethodOrGraph);
}
if (o instanceof JavaMethod || o instanceof Graph) {
lastMethodOrGraph = o;
}
}
if (result.isEmpty()) {
result.add(graph.toString());
graphSeen = true;
}
Collections.reverse(result);
if (!graphSeen) {
if (asJavaMethod(graph) != null) {
addMethodContext(result, graph, lastMethodOrGraph);
} else {
result.add(graph.toString());
}
}
inlineContextMap.put(graph, result);
}
return result;
}
private static void addMethodContext(List<String> result, Object o, Object lastMethodOrGraph) {
JavaMethod method = asJavaMethod(o);
if (method != null) {
if (lastMethodOrGraph == null || asJavaMethod(lastMethodOrGraph) == null || !asJavaMethod(lastMethodOrGraph).equals(method) ||
(lastMethodOrGraph != o && lastMethodOrGraph instanceof Graph && o instanceof Graph)) {
result.add(method.format("%H::%n(%p)"));
} else {
}
}
}
private void openScope(String name, int inlineDepth, Map<Object, Object> properties) {
String prefix = inlineDepth == 0 ? Thread.currentThread().getName() + ":" : "";
try {
Map<Object, Object> props = properties;
if (inlineDepth == 0) {
if (props == null) {
props = new HashMap<>();
}
props.put("jvmArguments", jvmArguments);
if (sunJavaCommand != null) {
props.put("sun.java.command", sunJavaCommand);
}
props.put("date", new Date().toString());
}
printer.beginGroup(prefix + name, name, Debug.contextLookup(ResolvedJavaMethod.class), -1, props);
} catch (IOException e) {
failuresCount++;
printer = null;
}
}
private void closeScope(int inlineDepth) {
dumpIds[inlineDepth] = 0;
try {
printer.endGroup();
} catch (IOException e) {
failuresCount++;
printer = null;
}
}
@Override
public void close() {
if (previousInlineContext != null) {
for (int inlineDepth = 0; inlineDepth < previousInlineContext.size(); inlineDepth++) {
closeScope(inlineDepth);
}
}
if (printer != null) {
printer.close();
printer = null;
}
}
}