annotate graal/GraalCompiler/src/com/sun/c1x/debug/GraphvizPrinterObserver.java @ 2691:4149feada801

GraphvizPrinterObserver: close output stream also in case of exception
author Peter Hofer <peter.hofer@jku.at>
date Wed, 18 May 2011 11:27:10 +0200
parents 6ab73784566a
children 79590d6b4a7c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
1 /*
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
4 *
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
7 * published by the Free Software Foundation.
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
8 *
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
13 * accompanied this code).
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
14 *
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
18 *
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
21 * questions.
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
22 */
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
23 package com.sun.c1x.debug;
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
24
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
25 import java.io.*;
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
26
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
27 import com.oracle.graal.graph.*;
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
28 import com.oracle.graal.graph.vis.*;
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
29 import com.sun.c1x.*;
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
30 import com.sun.c1x.observer.*;
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
31 import com.sun.c1x.value.*;
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
32
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
33 /**
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
34 * Observes compilation events and uses {@link GraphvizPrinter} to produce a control flow graph in the DOT language
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
35 * which can be visualized with Graphviz.
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
36 *
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
37 * @author Peter Hofer
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
38 */
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
39 public class GraphvizPrinterObserver implements CompilationObserver {
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
40
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
41 private final boolean pdf;
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
42 private int n;
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
43
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
44 public GraphvizPrinterObserver(boolean pdf) {
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
45 this.pdf = pdf;
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
46 }
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
47
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
48 public void compilationStarted(CompilationEvent event) {
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
49 n = 0;
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
50 }
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
51
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
52 public void compilationFinished(CompilationEvent event) {
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
53 }
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
54
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
55 public void compilationEvent(CompilationEvent event) {
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
56 if (event.getStartBlock() != null && !TTY.isSuppressed()) {
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
57 Graph graph = event.getStartBlock().graph();
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
58
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
59 String name = event.getMethod().holder().name();
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
60 name = name.substring(1, name.length() - 1).replace('/', '.');
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
61 name = name + "." + event.getMethod().name();
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
62 String filename = name + "_" + (n++) + "_" + event.getLabel();
2691
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
63
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
64 OutputStream out = null;
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
65 try {
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
66 if (pdf) {
2691
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
67 ByteArrayOutputStream buffer = new ByteArrayOutputStream();
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
68 GraphvizPrinter printer = new GraphvizPrinter(buffer);
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
69 if (C1XOptions.OmitDOTFrameStates) {
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
70 printer.addOmittedClass(FrameState.class);
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
71 }
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
72 printer.begin(name);
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
73 printer.print(graph, true);
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
74 printer.end();
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
75
2691
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
76 out = new FileOutputStream(filename + ".pdf");
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
77 GraphvizRunner.process(GraphvizRunner.DOT_LAYOUT, new ByteArrayInputStream(buffer.toByteArray()), out, "pdf");
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
78 } else {
2691
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
79 out = new FileOutputStream(filename + ".gv");
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
80
2691
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
81 GraphvizPrinter printer = new GraphvizPrinter(out);
2674
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
82 if (C1XOptions.OmitDOTFrameStates) {
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
83 printer.addOmittedClass(FrameState.class);
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
84 }
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
85 printer.begin(name);
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
86 printer.print(graph, true);
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
87 printer.end();
6ab73784566a * BlockBegin.predecessors changed to List<BlockEnd>
Lukas Stadler <lukas.stadler@jku.at>
parents: 2648
diff changeset
88 }
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
89 } catch (IOException e) {
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
90 e.printStackTrace();
2691
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
91 } finally {
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
92 if (out != null) {
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
93 try {
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
94 out.close();
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
95 } catch (IOException e) {
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
96 }
4149feada801 GraphvizPrinterObserver: close output stream also in case of exception
Peter Hofer <peter.hofer@jku.at>
parents: 2674
diff changeset
97 }
2648
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
98 }
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
99 }
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
100 }
d456b679b6de Add option PrintDOTGraphToFile to produce Graphviz graphs in the DOT language for compiled methods that match PrintFilter
Peter Hofer <peter.hofer@jku.at>
parents:
diff changeset
101 }