Mercurial > hg > truffle
comparison graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java @ 2708:4272b7af2d17
merge
author | Lukas Stadler <lukas.stadler@jku.at> |
---|---|
date | Wed, 18 May 2011 18:40:58 +0200 |
parents | a51ef0310dad |
children | b003ea36fa12 |
comparison
equal
deleted
inserted
replaced
2707:7ed72769d51a | 2708:4272b7af2d17 |
---|---|
1 /* | |
2 * Copyright (c) 2011, 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 */ | |
23 package com.sun.c1x.debug; | |
24 | |
25 import java.io.*; | |
26 import java.net.*; | |
27 import java.util.regex.*; | |
28 | |
29 import com.oracle.graal.graph.*; | |
30 import com.sun.c1x.*; | |
31 import com.sun.c1x.observer.*; | |
32 import com.sun.c1x.value.*; | |
33 | |
34 /** | |
35 * Observes compilation events and uses {@link IdealGraphPrinter} to generate a graph representation that can be | |
36 * inspected with the <a href="http://kenai.com/projects/igv">Ideal Graph Visualizer</a>. | |
37 * | |
38 * @author Peter Hofer | |
39 */ | |
40 public class IdealGraphPrinterObserver implements CompilationObserver { | |
41 | |
42 private static final Pattern INVALID_CHAR = Pattern.compile("[^A-Za-z0-9_.-]"); | |
43 | |
44 private final String host; | |
45 private final int port; | |
46 | |
47 private IdealGraphPrinter printer; | |
48 private OutputStream stream; | |
49 private Socket socket; | |
50 | |
51 /** | |
52 * Creates a new {@link IdealGraphPrinterObserver} that writes output to a file named after the compiled method. | |
53 */ | |
54 public IdealGraphPrinterObserver() { | |
55 this(null, -1); | |
56 } | |
57 | |
58 /** | |
59 * Creates a new {@link IdealGraphPrinterObserver} that sends output to a remove IdealGraphVisualizer instance. | |
60 */ | |
61 public IdealGraphPrinterObserver(String host, int port) { | |
62 this.host = host; | |
63 this.port = port; | |
64 } | |
65 | |
66 @Override | |
67 public void compilationStarted(CompilationEvent event) { | |
68 assert (stream == null && printer == null); | |
69 | |
70 if (!TTY.isSuppressed()) { | |
71 String name = event.getMethod().holder().name(); | |
72 name = name.substring(1, name.length() - 1).replace('/', '.'); | |
73 name = name + "." + event.getMethod().name(); | |
74 | |
75 if (host != null) { | |
76 openNetworkPrinter(name); | |
77 } else { | |
78 openFilePrinter(name); | |
79 } | |
80 } | |
81 } | |
82 | |
83 private void openFilePrinter(String name) { | |
84 String filename = name + ".igv.xml"; | |
85 filename = INVALID_CHAR.matcher(filename).replaceAll("_"); | |
86 | |
87 try { | |
88 stream = new FileOutputStream(filename); | |
89 printer = new IdealGraphPrinter(stream); | |
90 if (C1XOptions.OmitDOTFrameStates) { | |
91 printer.addOmittedClass(FrameState.class); | |
92 } | |
93 printer.begin(); | |
94 printer.beginGroup(name, name, -1); | |
95 } catch (IOException e) { | |
96 e.printStackTrace(); | |
97 } | |
98 } | |
99 | |
100 private void openNetworkPrinter(String name) { | |
101 try { | |
102 socket = new Socket(host, port); | |
103 if (socket.getInputStream().read() == 'y') { | |
104 stream = socket.getOutputStream(); | |
105 } else { | |
106 // server currently does not accept any input | |
107 socket.close(); | |
108 socket = null; | |
109 return; | |
110 } | |
111 | |
112 printer = new IdealGraphPrinter(stream); | |
113 if (C1XOptions.OmitDOTFrameStates) { | |
114 printer.addOmittedClass(FrameState.class); | |
115 } | |
116 printer.begin(); | |
117 printer.beginGroup(name, name, -1); | |
118 printer.flush(); | |
119 if (socket.getInputStream().read() != 'y') { | |
120 // server declines input for this method | |
121 socket.close(); | |
122 socket = null; | |
123 stream = null; | |
124 printer = null; | |
125 } | |
126 } catch (IOException e) { | |
127 e.printStackTrace(); | |
128 | |
129 if (socket != null) { | |
130 try { | |
131 socket.close(); | |
132 } catch (IOException ioe) { | |
133 } | |
134 socket = null; | |
135 } | |
136 stream = null; | |
137 printer = null; | |
138 } | |
139 } | |
140 | |
141 @Override | |
142 public void compilationEvent(CompilationEvent event) { | |
143 if (printer != null && event.getStartBlock() != null) { | |
144 Graph graph = event.getStartBlock().graph(); | |
145 printer.print(graph, event.getLabel(), true); | |
146 } | |
147 } | |
148 | |
149 @Override | |
150 public void compilationFinished(CompilationEvent event) { | |
151 if (printer != null) { | |
152 try { | |
153 printer.endGroup(); | |
154 printer.end(); | |
155 | |
156 if (socket != null) { | |
157 socket.close(); // also closes stream | |
158 } else { | |
159 stream.close(); | |
160 } | |
161 } catch (IOException e) { | |
162 e.printStackTrace(); | |
163 } finally { | |
164 printer = null; | |
165 stream = null; | |
166 socket = null; | |
167 } | |
168 } | |
169 } | |
170 | |
171 } |