Mercurial > hg > truffle
comparison graal/GraalCompiler/src/com/sun/c1x/debug/IdealGraphPrinterObserver.java @ 2698:b179be22a3db
Allow to submit graphs to IdealGraphVisualizer remotely with new options named after their counterparts in C2
author | Peter Hofer <peter.hofer@jku.at> |
---|---|
date | Wed, 18 May 2011 15:01:43 +0200 |
parents | 79590d6b4a7c |
children | a51ef0310dad |
comparison
equal
deleted
inserted
replaced
2697:bd4c3be86fb7 | 2698:b179be22a3db |
---|---|
21 * questions. | 21 * questions. |
22 */ | 22 */ |
23 package com.sun.c1x.debug; | 23 package com.sun.c1x.debug; |
24 | 24 |
25 import java.io.*; | 25 import java.io.*; |
26 import java.net.*; | |
26 import java.util.regex.*; | 27 import java.util.regex.*; |
27 | 28 |
28 import com.oracle.graal.graph.*; | 29 import com.oracle.graal.graph.*; |
29 import com.sun.c1x.*; | 30 import com.sun.c1x.*; |
30 import com.sun.c1x.observer.*; | 31 import com.sun.c1x.observer.*; |
38 */ | 39 */ |
39 public class IdealGraphPrinterObserver implements CompilationObserver { | 40 public class IdealGraphPrinterObserver implements CompilationObserver { |
40 | 41 |
41 private static final Pattern INVALID_CHAR = Pattern.compile("[^A-Za-z0-9_.-]"); | 42 private static final Pattern INVALID_CHAR = Pattern.compile("[^A-Za-z0-9_.-]"); |
42 | 43 |
44 private final String host; | |
45 private final int port; | |
46 | |
43 private IdealGraphPrinter printer; | 47 private IdealGraphPrinter printer; |
44 private OutputStream stream; | 48 private OutputStream stream; |
49 private Socket socket; | |
50 | |
51 public IdealGraphPrinterObserver() { | |
52 this(null, -1); | |
53 } | |
54 | |
55 public IdealGraphPrinterObserver(String host, int port) { | |
56 this.host = host; | |
57 this.port = port; | |
58 } | |
45 | 59 |
46 @Override | 60 @Override |
47 public void compilationStarted(CompilationEvent event) { | 61 public void compilationStarted(CompilationEvent event) { |
48 assert (stream == null && printer == null); | 62 assert (stream == null && printer == null); |
49 | 63 |
50 if (!TTY.isSuppressed()) { | 64 if (!TTY.isSuppressed()) { |
51 String name = event.getMethod().holder().name(); | 65 String name = event.getMethod().holder().name(); |
52 name = name.substring(1, name.length() - 1).replace('/', '.'); | 66 name = name.substring(1, name.length() - 1).replace('/', '.'); |
53 name = name + "." + event.getMethod().name(); | 67 name = name + "." + event.getMethod().name(); |
54 | 68 |
55 String filename = name + ".igv.xml"; | 69 if (host != null) { |
56 filename = INVALID_CHAR.matcher(filename).replaceAll("_"); | 70 openNetworkPrinter(name); |
71 } else { | |
72 openFilePrinter(name); | |
73 } | |
74 } | |
75 } | |
57 | 76 |
58 try { | 77 private void openFilePrinter(String name) { |
59 stream = new FileOutputStream(filename); | 78 String filename = name + ".igv.xml"; |
60 printer = new IdealGraphPrinter(stream); | 79 filename = INVALID_CHAR.matcher(filename).replaceAll("_"); |
61 | 80 |
62 if (C1XOptions.OmitDOTFrameStates) { | 81 try { |
63 printer.addOmittedClass(FrameState.class); | 82 stream = new FileOutputStream(filename); |
83 printer = new IdealGraphPrinter(stream); | |
84 if (C1XOptions.OmitDOTFrameStates) { | |
85 printer.addOmittedClass(FrameState.class); | |
86 } | |
87 printer.begin(); | |
88 printer.beginGroup(name, name, -1); | |
89 } catch (IOException e) { | |
90 e.printStackTrace(); | |
91 } | |
92 } | |
93 | |
94 private void openNetworkPrinter(String name) { | |
95 try { | |
96 socket = new Socket(host, port); | |
97 if (socket.getInputStream().read() == 'y') { | |
98 stream = socket.getOutputStream(); | |
99 } else { | |
100 // server currently does not accept any input | |
101 socket.close(); | |
102 socket = null; | |
103 return; | |
104 } | |
105 | |
106 printer = new IdealGraphPrinter(stream); | |
107 if (C1XOptions.OmitDOTFrameStates) { | |
108 printer.addOmittedClass(FrameState.class); | |
109 } | |
110 printer.begin(); | |
111 printer.beginGroup(name, name, -1); | |
112 printer.flush(); | |
113 if (socket.getInputStream().read() != 'y') { | |
114 // server declines input for this method | |
115 socket.close(); | |
116 socket = null; | |
117 stream = null; | |
118 printer = null; | |
119 } | |
120 } catch (IOException e) { | |
121 e.printStackTrace(); | |
122 | |
123 if (socket != null) { | |
124 try { | |
125 socket.close(); | |
126 } catch (IOException ioe) { | |
64 } | 127 } |
65 | 128 socket = null; |
66 printer.begin(); | |
67 printer.beginGroup(name, name, -1); | |
68 } catch (IOException e) { | |
69 e.printStackTrace(); | |
70 } | 129 } |
130 stream = null; | |
131 printer = null; | |
71 } | 132 } |
72 } | 133 } |
73 | 134 |
74 @Override | 135 @Override |
75 public void compilationEvent(CompilationEvent event) { | 136 public void compilationEvent(CompilationEvent event) { |
83 public void compilationFinished(CompilationEvent event) { | 144 public void compilationFinished(CompilationEvent event) { |
84 if (printer != null) { | 145 if (printer != null) { |
85 try { | 146 try { |
86 printer.endGroup(); | 147 printer.endGroup(); |
87 printer.end(); | 148 printer.end(); |
88 stream.close(); | 149 |
150 if (socket != null) { | |
151 socket.close(); // also closes stream | |
152 } else { | |
153 stream.close(); | |
154 } | |
89 } catch (IOException e) { | 155 } catch (IOException e) { |
90 e.printStackTrace(); | 156 e.printStackTrace(); |
91 } finally { | 157 } finally { |
92 printer = null; | 158 printer = null; |
93 stream = null; | 159 stream = null; |
160 socket = null; | |
94 } | 161 } |
95 } | 162 } |
96 } | 163 } |
97 | 164 |
98 } | 165 } |