comparison visualizer/Graph/src/com/sun/hotspot/igv/graph/Diagram.java @ 4512:015fb895586b

Moved visualizer to new directory.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Tue, 07 Feb 2012 22:41:09 +0100
parents
children
comparison
equal deleted inserted replaced
4511:6cb549627941 4512:015fb895586b
1 /*
2 * Copyright (c) 2008, 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 */
24 package com.sun.hotspot.igv.graph;
25
26 import com.sun.hotspot.igv.data.InputEdge;
27 import com.sun.hotspot.igv.data.InputGraph;
28 import com.sun.hotspot.igv.data.InputNode;
29 import com.sun.hotspot.igv.data.Properties;
30 import com.sun.hotspot.igv.data.Properties.StringPropertyMatcher;
31 import java.awt.Font;
32 import java.util.*;
33
34 /**
35 *
36 * @author Thomas Wuerthinger
37 */
38 public class Diagram {
39
40 private List<Figure> figures;
41 private InputGraph graph;
42 private int curId;
43 private String nodeText;
44 private Font font;
45 private Font slotFont;
46
47 public Font getFont() {
48 return font;
49 }
50
51 public Font getSlotFont() {
52 return slotFont;
53 }
54
55 private Diagram() {
56 figures = new ArrayList<>();
57 this.nodeText = "";
58 this.font = new Font("Arial", Font.PLAIN, 13);
59 this.slotFont = new Font("Arial", Font.PLAIN, 10);
60 }
61
62 public String getNodeText() {
63 return nodeText;
64 }
65
66 public Diagram getNext() {
67 return Diagram.createDiagram(graph.getNext(), nodeText);
68 }
69
70 public Diagram getPrev() {
71 return Diagram.createDiagram(graph.getPrev(), nodeText);
72 }
73
74 public List<Figure> getFigures() {
75 return Collections.unmodifiableList(figures);
76 }
77
78 public Figure createFigure() {
79 Figure f = new Figure(this, curId);
80 curId++;
81 this.figures.add(f);
82 return f;
83 }
84
85 public Connection createConnection(InputSlot inputSlot, OutputSlot outputSlot, String label) {
86 assert inputSlot.getFigure().getDiagram() == this;
87 assert outputSlot.getFigure().getDiagram() == this;
88 return new Connection(inputSlot, outputSlot, label);
89 }
90
91 public Map<InputNode, Set<Figure>> calcSourceToFigureRelation() {
92 Map<InputNode, Set<Figure>> map = new HashMap<>();
93
94 for(InputNode node : this.getGraph().getNodes()) {
95 map.put(node, new HashSet<Figure>());
96 }
97
98 for(Figure f : this.getFigures()) {
99 for(InputNode node : f.getSource().getSourceNodes()) {
100 map.get(node).add(f);
101 }
102 }
103
104 return map;
105 }
106
107 public static Diagram createDiagram(InputGraph graph, String nodeText) {
108 if (graph == null) {
109 return null;
110 }
111
112 Diagram d = new Diagram();
113 d.graph = graph;
114 d.nodeText = nodeText;
115
116 Collection<InputNode> nodes = graph.getNodes();
117 Hashtable<Integer, Figure> figureHash = new Hashtable<>();
118 for (InputNode n : nodes) {
119 Figure f = d.createFigure();
120 f.getSource().addSourceNode(n);
121 f.getProperties().add(n.getProperties());
122 figureHash.put(n.getId(), f);
123 }
124
125 for (InputEdge e : graph.getEdges()) {
126
127 int from = e.getFrom();
128 int to = e.getTo();
129 Figure fromFigure = figureHash.get(from);
130 Figure toFigure = figureHash.get(to);
131 assert fromFigure != null && toFigure != null;
132
133 if(fromFigure == null || toFigure == null) continue;
134
135 int fromIndex = e.getFromIndex();
136 while (fromFigure.getOutputSlots().size() <= fromIndex) {
137 fromFigure.createOutputSlot();
138 }
139 OutputSlot outputSlot = fromFigure.getOutputSlots().get(fromIndex);
140
141 int toIndex = e.getToIndex();
142 while (toFigure.getInputSlots().size() <= toIndex) {
143 toFigure.createInputSlot();
144 }
145 InputSlot inputSlot = toFigure.getInputSlots().get(toIndex);
146
147 Connection c = d.createConnection(inputSlot, outputSlot, e.getLabel());
148
149 if (e.getState() == InputEdge.State.NEW) {
150 c.setStyle(Connection.ConnectionStyle.BOLD);
151 } else if (e.getState() == InputEdge.State.DELETED) {
152 c.setStyle(Connection.ConnectionStyle.DASHED);
153 }
154 }
155
156
157 return d;
158 }
159
160 public void removeAllFigures(Set<Figure> figuresToRemove) {
161 for (Figure f : figuresToRemove) {
162 freeFigure(f);
163 }
164
165 ArrayList<Figure> newFigures = new ArrayList<>();
166 for (Figure f : this.figures) {
167 if (!figuresToRemove.contains(f)) {
168 newFigures.add(f);
169 }
170 }
171 figures = newFigures;
172 }
173
174 private void freeFigure(Figure succ) {
175
176 List<InputSlot> inputSlots = new ArrayList<>(succ.getInputSlots());
177 for (InputSlot s : inputSlots) {
178 succ.removeInputSlot(s);
179 }
180
181 List<OutputSlot> outputSlots = new ArrayList<>(succ.getOutputSlots());
182 for (OutputSlot s : outputSlots) {
183 succ.removeOutputSlot(s);
184 }
185
186 assert succ.getInputSlots().size() == 0;
187 assert succ.getOutputSlots().size() == 0;
188 assert succ.getPredecessors().size() == 0;
189 assert succ.getSuccessors().size() == 0;
190
191 }
192
193 public void removeFigure(Figure succ) {
194
195 assert this.figures.contains(succ);
196 freeFigure(succ);
197 this.figures.remove(succ);
198 }
199
200 public String getName() {
201 return graph.getName();
202 }
203
204 public InputGraph getGraph() {
205 return graph;
206 }
207
208 public Set<Connection> getConnections() {
209
210 Set<Connection> connections = new HashSet<>();
211 for (Figure f : figures) {
212
213 for (InputSlot s : f.getInputSlots()) {
214 connections.addAll(s.getConnections());
215 }
216 }
217
218 return connections;
219 }
220
221 public Figure getRootFigure() {
222 Properties.PropertySelector<Figure> selector = new Properties.PropertySelector<>(figures);
223 Figure root = selector.selectSingle(new StringPropertyMatcher("name", "Root"));
224 if (root == null) {
225 root = selector.selectSingle(new StringPropertyMatcher("name", "Start"));
226 }
227 if (root == null) {
228 List<Figure> rootFigures = getRootFigures();
229 if (rootFigures.size() > 0) {
230 root = rootFigures.get(0);
231 } else if (figures.size() > 0) {
232 root = figures.get(0);
233 }
234 }
235
236 return root;
237 }
238
239 public void printStatistics() {
240 System.out.println("=============================================================");
241 System.out.println("Diagram statistics");
242
243 List<Figure> tmpFigures = getFigures();
244 Set<Connection> connections = getConnections();
245
246 System.out.println("Number of figures: " + tmpFigures.size());
247 System.out.println("Number of connections: " + connections.size());
248
249 List<Figure> figuresSorted = new ArrayList<>(tmpFigures);
250 Collections.sort(figuresSorted, new Comparator<Figure>() {
251
252 @Override
253 public int compare(Figure a, Figure b) {
254 return b.getPredecessors().size() + b.getSuccessors().size() - a.getPredecessors().size() - a.getSuccessors().size();
255 }
256 });
257
258 final int COUNT = 10;
259 int z = 0;
260 for (Figure f : figuresSorted) {
261
262 z++;
263 int sum = f.getPredecessors().size() + f.getSuccessors().size();
264 System.out.println("#" + z + ": " + f + ", predCount=" + f.getPredecessors().size() + " succCount=" + f.getSuccessors().size());
265 if (sum < COUNT) {
266 break;
267 }
268
269 }
270
271 System.out.println("=============================================================");
272 }
273
274 public List<Figure> getRootFigures() {
275 ArrayList<Figure> rootFigures = new ArrayList<>();
276 for (Figure f : figures) {
277 if (f.getPredecessors().size() == 0) {
278 rootFigures.add(f);
279 }
280 }
281 return rootFigures;
282 }
283 }