annotate agent/src/share/classes/sun/jvm/hotspot/utilities/HeapGXLWriter.java @ 6725:da91efe96a93

6964458: Reimplement class meta-data storage to use native memory Summary: Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland Contributed-by: jmasa <jon.masamitsu@oracle.com>, stefank <stefan.karlsson@oracle.com>, mgerdin <mikael.gerdin@oracle.com>, never <tom.rodriguez@oracle.com>
author coleenp
date Sat, 01 Sep 2012 13:25:18 -0400
parents 63997f575155
children 6bd680e9ea35
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 2411
diff changeset
2 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.utilities;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 /**
a61af66fc99e Initial load
duke
parents:
diff changeset
33 * <p>This class writes Java heap in Graph eXchange Language (GXL)
a61af66fc99e Initial load
duke
parents:
diff changeset
34 * format. GXL is an open standard for serializing arbitrary graphs in
a61af66fc99e Initial load
duke
parents:
diff changeset
35 * XML syntax.</p>
a61af66fc99e Initial load
duke
parents:
diff changeset
36 *
a61af66fc99e Initial load
duke
parents:
diff changeset
37 * <p>A GXL document contains one or more graphs. A graph contains
a61af66fc99e Initial load
duke
parents:
diff changeset
38 * nodes and edges. Both nodes and edges can have attributes. graphs,
a61af66fc99e Initial load
duke
parents:
diff changeset
39 * nodes, edges and attributes are represented by XML elements graph,
a61af66fc99e Initial load
duke
parents:
diff changeset
40 * node, edge and attr respectively. Attributes can be typed. GXL
a61af66fc99e Initial load
duke
parents:
diff changeset
41 * supports locator, bool, int, float, bool, string, enum as well as
a61af66fc99e Initial load
duke
parents:
diff changeset
42 * set, seq, bag, tup types. Nodes must have a XML attribute 'id' that
a61af66fc99e Initial load
duke
parents:
diff changeset
43 * is unique id of the node in the GXL document. Edges must have
a61af66fc99e Initial load
duke
parents:
diff changeset
44 * 'from' and 'to' XML attributes that are ids of from and to nodes.</p>
a61af66fc99e Initial load
duke
parents:
diff changeset
45 *
a61af66fc99e Initial load
duke
parents:
diff changeset
46 * <p>Java heap to GXL document mapping:</p>
a61af66fc99e Initial load
duke
parents:
diff changeset
47 * <ul>
a61af66fc99e Initial load
duke
parents:
diff changeset
48 * <li>Java object - GXL node.
a61af66fc99e Initial load
duke
parents:
diff changeset
49 * <li>Java primitive field - GXL attribute (type mapping below).
a61af66fc99e Initial load
duke
parents:
diff changeset
50 * <li>Java reference field - GXL edge from referee to referent node.
a61af66fc99e Initial load
duke
parents:
diff changeset
51 * <li>Java primitive array - GXL node with seq type attribute.
a61af66fc99e Initial load
duke
parents:
diff changeset
52 * <li>Java char array - GXL node with one attribute of string type.
a61af66fc99e Initial load
duke
parents:
diff changeset
53 * <li>Java object array - GXL node and 'length' edges.
a61af66fc99e Initial load
duke
parents:
diff changeset
54 * </ul>
a61af66fc99e Initial load
duke
parents:
diff changeset
55 *
a61af66fc99e Initial load
duke
parents:
diff changeset
56 * <p>Java primitive to GXL type mapping:</p>
a61af66fc99e Initial load
duke
parents:
diff changeset
57 * <ul>
a61af66fc99e Initial load
duke
parents:
diff changeset
58 * <li>Java byte, int, short, long - GXL int attribute
a61af66fc99e Initial load
duke
parents:
diff changeset
59 * <li>Java float, double - GXL float attribute
a61af66fc99e Initial load
duke
parents:
diff changeset
60 * <li>Java boolean - GXL bool atttribute
a61af66fc99e Initial load
duke
parents:
diff changeset
61 * <li>Java char - GXL string attribute
a61af66fc99e Initial load
duke
parents:
diff changeset
62 * </ul>
a61af66fc99e Initial load
duke
parents:
diff changeset
63 *
a61af66fc99e Initial load
duke
parents:
diff changeset
64 * Exact Java primitive type code is written in 'kind' attribute of
a61af66fc99e Initial load
duke
parents:
diff changeset
65 * 'attr' element. Type code is specified in JVM spec. second edition
a61af66fc99e Initial load
duke
parents:
diff changeset
66 * section 4.3.2 (Field Descriptor).
a61af66fc99e Initial load
duke
parents:
diff changeset
67 *
a61af66fc99e Initial load
duke
parents:
diff changeset
68 * @see <a href="http://www.gupro.de/GXL/">GXL</a>
a61af66fc99e Initial load
duke
parents:
diff changeset
69 * @see <a href="http://www.gupro.de/GXL/dtd/dtd.html">GXL DTD</a>
a61af66fc99e Initial load
duke
parents:
diff changeset
70 */
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72 public class HeapGXLWriter extends AbstractHeapGraphWriter {
a61af66fc99e Initial load
duke
parents:
diff changeset
73 public void write(String fileName) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
a61af66fc99e Initial load
duke
parents:
diff changeset
75 super.write();
a61af66fc99e Initial load
duke
parents:
diff changeset
76 if (out.checkError()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
77 throw new IOException();
a61af66fc99e Initial load
duke
parents:
diff changeset
78 }
a61af66fc99e Initial load
duke
parents:
diff changeset
79 out.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 protected void writeHeapHeader() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // XML processing instruction
a61af66fc99e Initial load
duke
parents:
diff changeset
84 out.print("<?xml version='1.0' encoding='");
a61af66fc99e Initial load
duke
parents:
diff changeset
85 out.print(ENCODING);
a61af66fc99e Initial load
duke
parents:
diff changeset
86 out.println("'?>");
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 out.println("<gxl>");
a61af66fc99e Initial load
duke
parents:
diff changeset
89 out.println("<graph id='JavaHeap'>");
a61af66fc99e Initial load
duke
parents:
diff changeset
90
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // document properties
a61af66fc99e Initial load
duke
parents:
diff changeset
92 writeAttribute("creation-date", "string", new Date().toString());
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 // write VM info
a61af66fc99e Initial load
duke
parents:
diff changeset
95 writeVMInfo();
a61af66fc99e Initial load
duke
parents:
diff changeset
96
a61af66fc99e Initial load
duke
parents:
diff changeset
97 // emit a node for null
a61af66fc99e Initial load
duke
parents:
diff changeset
98 out.print("<node id='");
a61af66fc99e Initial load
duke
parents:
diff changeset
99 out.print(getID(null));
a61af66fc99e Initial load
duke
parents:
diff changeset
100 out.println("'/>");
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 protected void writeObjectHeader(Oop oop) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 refFields = new ArrayList();
a61af66fc99e Initial load
duke
parents:
diff changeset
105 isArray = oop.isArray();
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 // generate an edge for instanceof relation
a61af66fc99e Initial load
duke
parents:
diff changeset
108 // between object node and it's class node.
a61af66fc99e Initial load
duke
parents:
diff changeset
109 writeEdge(oop, oop.getKlass().getJavaMirror(), "instanceof");
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 out.print("<node id='");
a61af66fc99e Initial load
duke
parents:
diff changeset
112 out.print(getID(oop));
a61af66fc99e Initial load
duke
parents:
diff changeset
113 out.println("'>");
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 protected void writeObjectFooter(Oop oop) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 out.println("</node>");
a61af66fc99e Initial load
duke
parents:
diff changeset
118
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // write the reference fields as edges
a61af66fc99e Initial load
duke
parents:
diff changeset
120 for (Iterator itr = refFields.iterator(); itr.hasNext();) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 OopField field = (OopField) itr.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
122 Oop ref = field.getValue(oop);
a61af66fc99e Initial load
duke
parents:
diff changeset
123
a61af66fc99e Initial load
duke
parents:
diff changeset
124 String name = field.getID().getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
125 if (isArray) {
a61af66fc99e Initial load
duke
parents:
diff changeset
126 // for arrays elements we use element<index> pattern
a61af66fc99e Initial load
duke
parents:
diff changeset
127 name = "element" + name;
a61af66fc99e Initial load
duke
parents:
diff changeset
128 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 name = identifierToXMLName(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
130 }
a61af66fc99e Initial load
duke
parents:
diff changeset
131 writeEdge(oop, ref, name);
a61af66fc99e Initial load
duke
parents:
diff changeset
132 }
a61af66fc99e Initial load
duke
parents:
diff changeset
133 refFields = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
134 }
a61af66fc99e Initial load
duke
parents:
diff changeset
135
a61af66fc99e Initial load
duke
parents:
diff changeset
136 protected void writeObjectArray(ObjArray array) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 writeObjectHeader(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
138 writeArrayLength(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
139 writeObjectFields(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
140 writeObjectFooter(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
141 }
a61af66fc99e Initial load
duke
parents:
diff changeset
142
a61af66fc99e Initial load
duke
parents:
diff changeset
143 protected void writePrimitiveArray(TypeArray array)
a61af66fc99e Initial load
duke
parents:
diff changeset
144 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 writeObjectHeader(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 // write array length
a61af66fc99e Initial load
duke
parents:
diff changeset
147 writeArrayLength(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
148 // write array elements
a61af66fc99e Initial load
duke
parents:
diff changeset
149 out.println("\t<attr name='elements'>");
a61af66fc99e Initial load
duke
parents:
diff changeset
150 TypeArrayKlass klass = (TypeArrayKlass) array.getKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
151 if (klass.getElementType() == TypeArrayKlass.T_CHAR) {
a61af66fc99e Initial load
duke
parents:
diff changeset
152 // char[] special treatment -- write it as string
a61af66fc99e Initial load
duke
parents:
diff changeset
153 out.print("\t<string>");
a61af66fc99e Initial load
duke
parents:
diff changeset
154 out.print(escapeXMLChars(OopUtilities.charArrayToString(array)));
a61af66fc99e Initial load
duke
parents:
diff changeset
155 out.println("</string>");
a61af66fc99e Initial load
duke
parents:
diff changeset
156 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 out.println("\t<seq>");
a61af66fc99e Initial load
duke
parents:
diff changeset
158 writeObjectFields(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
159 out.println("\t</seq>");
a61af66fc99e Initial load
duke
parents:
diff changeset
160 }
a61af66fc99e Initial load
duke
parents:
diff changeset
161 out.println("\t</attr>");
a61af66fc99e Initial load
duke
parents:
diff changeset
162 writeObjectFooter(array);
a61af66fc99e Initial load
duke
parents:
diff changeset
163 }
a61af66fc99e Initial load
duke
parents:
diff changeset
164
a61af66fc99e Initial load
duke
parents:
diff changeset
165 protected void writeClass(Instance instance) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
166 writeObjectHeader(instance);
2411
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
167 Klass reflectedType = java_lang_Class.asKlass(instance);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
168 boolean isInstanceKlass = (reflectedType instanceof InstanceKlass);
a61af66fc99e Initial load
duke
parents:
diff changeset
169 // reflectedType is null for primitive types (int.class etc).
a61af66fc99e Initial load
duke
parents:
diff changeset
170 if (reflectedType != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 Symbol name = reflectedType.getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
172 if (name != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 // write class name as an attribute
a61af66fc99e Initial load
duke
parents:
diff changeset
174 writeAttribute("class-name", "string", name.asString());
a61af66fc99e Initial load
duke
parents:
diff changeset
175 }
a61af66fc99e Initial load
duke
parents:
diff changeset
176 if (isInstanceKlass) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 // write object-size as an attribute
a61af66fc99e Initial load
duke
parents:
diff changeset
178 long sizeInBytes = reflectedType.getLayoutHelper();
a61af66fc99e Initial load
duke
parents:
diff changeset
179 writeAttribute("object-size", "int",
a61af66fc99e Initial load
duke
parents:
diff changeset
180 Long.toString(sizeInBytes));
a61af66fc99e Initial load
duke
parents:
diff changeset
181 // write static fields of this class.
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 2411
diff changeset
182 writeObjectFields((InstanceKlass)reflectedType);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
183 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185 out.println("</node>");
a61af66fc99e Initial load
duke
parents:
diff changeset
186
a61af66fc99e Initial load
duke
parents:
diff changeset
187 // write edges for super class and direct interfaces
a61af66fc99e Initial load
duke
parents:
diff changeset
188 if (reflectedType != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
189 Klass superType = reflectedType.getSuper();
a61af66fc99e Initial load
duke
parents:
diff changeset
190 Oop superMirror = (superType == null)?
a61af66fc99e Initial load
duke
parents:
diff changeset
191 null : superType.getJavaMirror();
a61af66fc99e Initial load
duke
parents:
diff changeset
192 writeEdge(instance, superMirror, "extends");
a61af66fc99e Initial load
duke
parents:
diff changeset
193 if (isInstanceKlass) {
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // write edges for directly implemented interfaces
a61af66fc99e Initial load
duke
parents:
diff changeset
195 InstanceKlass ik = (InstanceKlass) reflectedType;
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 2411
diff changeset
196 KlassArray interfaces = ik.getLocalInterfaces();
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 2411
diff changeset
197 final int len = interfaces.length();
0
a61af66fc99e Initial load
duke
parents:
diff changeset
198 for (int i = 0; i < len; i++) {
6725
da91efe96a93 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 2411
diff changeset
199 Klass k = interfaces.getAt(i);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
200 writeEdge(instance, k.getJavaMirror(), "implements");
a61af66fc99e Initial load
duke
parents:
diff changeset
201 }
a61af66fc99e Initial load
duke
parents:
diff changeset
202
a61af66fc99e Initial load
duke
parents:
diff changeset
203 // write loader
a61af66fc99e Initial load
duke
parents:
diff changeset
204 Oop loader = ik.getClassLoader();
a61af66fc99e Initial load
duke
parents:
diff changeset
205 writeEdge(instance, loader, "loaded-by");
a61af66fc99e Initial load
duke
parents:
diff changeset
206
a61af66fc99e Initial load
duke
parents:
diff changeset
207 // write signers
a61af66fc99e Initial load
duke
parents:
diff changeset
208 Oop signers = ik.getSigners();
a61af66fc99e Initial load
duke
parents:
diff changeset
209 writeEdge(instance, signers, "signed-by");
a61af66fc99e Initial load
duke
parents:
diff changeset
210
a61af66fc99e Initial load
duke
parents:
diff changeset
211 // write protection domain
a61af66fc99e Initial load
duke
parents:
diff changeset
212 Oop protectionDomain = ik.getProtectionDomain();
a61af66fc99e Initial load
duke
parents:
diff changeset
213 writeEdge(instance, protectionDomain, "protection-domain");
a61af66fc99e Initial load
duke
parents:
diff changeset
214
a61af66fc99e Initial load
duke
parents:
diff changeset
215 // write edges for static reference fields from this class
a61af66fc99e Initial load
duke
parents:
diff changeset
216 for (Iterator itr = refFields.iterator(); itr.hasNext();) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 OopField field = (OopField) itr.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
218 Oop ref = field.getValue(reflectedType);
a61af66fc99e Initial load
duke
parents:
diff changeset
219 String name = field.getID().getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
220 writeEdge(instance, ref, identifierToXMLName(name));
a61af66fc99e Initial load
duke
parents:
diff changeset
221 }
a61af66fc99e Initial load
duke
parents:
diff changeset
222 }
a61af66fc99e Initial load
duke
parents:
diff changeset
223 }
a61af66fc99e Initial load
duke
parents:
diff changeset
224 refFields = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
225 }
a61af66fc99e Initial load
duke
parents:
diff changeset
226
a61af66fc99e Initial load
duke
parents:
diff changeset
227 protected void writeReferenceField(Oop oop, OopField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
228 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
229 refFields.add(field);
a61af66fc99e Initial load
duke
parents:
diff changeset
230 }
a61af66fc99e Initial load
duke
parents:
diff changeset
231
a61af66fc99e Initial load
duke
parents:
diff changeset
232 protected void writeByteField(Oop oop, ByteField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
233 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
234 writeField(field, "int", "B", Byte.toString(field.getValue(oop)));
a61af66fc99e Initial load
duke
parents:
diff changeset
235 }
a61af66fc99e Initial load
duke
parents:
diff changeset
236
a61af66fc99e Initial load
duke
parents:
diff changeset
237 protected void writeCharField(Oop oop, CharField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
238 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 writeField(field, "string", "C",
a61af66fc99e Initial load
duke
parents:
diff changeset
240 escapeXMLChars(Character.toString(field.getValue(oop))));
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 protected void writeBooleanField(Oop oop, BooleanField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
244 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 writeField(field, "bool", "Z", Boolean.toString(field.getValue(oop)));
a61af66fc99e Initial load
duke
parents:
diff changeset
246 }
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248 protected void writeShortField(Oop oop, ShortField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
249 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
250 writeField(field, "int", "S", Short.toString(field.getValue(oop)));
a61af66fc99e Initial load
duke
parents:
diff changeset
251 }
a61af66fc99e Initial load
duke
parents:
diff changeset
252
a61af66fc99e Initial load
duke
parents:
diff changeset
253 protected void writeIntField(Oop oop, IntField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
254 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 writeField(field, "int", "I", Integer.toString(field.getValue(oop)));
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257
a61af66fc99e Initial load
duke
parents:
diff changeset
258 protected void writeLongField(Oop oop, LongField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
259 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
260 writeField(field, "int", "J", Long.toString(field.getValue(oop)));
a61af66fc99e Initial load
duke
parents:
diff changeset
261 }
a61af66fc99e Initial load
duke
parents:
diff changeset
262
a61af66fc99e Initial load
duke
parents:
diff changeset
263 protected void writeFloatField(Oop oop, FloatField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
264 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
265 writeField(field, "float", "F", Float.toString(field.getValue(oop)));
a61af66fc99e Initial load
duke
parents:
diff changeset
266 }
a61af66fc99e Initial load
duke
parents:
diff changeset
267
a61af66fc99e Initial load
duke
parents:
diff changeset
268 protected void writeDoubleField(Oop oop, DoubleField field)
a61af66fc99e Initial load
duke
parents:
diff changeset
269 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
270 writeField(field, "float", "D", Double.toString(field.getValue(oop)));
a61af66fc99e Initial load
duke
parents:
diff changeset
271 }
a61af66fc99e Initial load
duke
parents:
diff changeset
272
a61af66fc99e Initial load
duke
parents:
diff changeset
273 protected void writeHeapFooter() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
274 out.println("</graph>");
a61af66fc99e Initial load
duke
parents:
diff changeset
275 out.println("</gxl>");
a61af66fc99e Initial load
duke
parents:
diff changeset
276 }
a61af66fc99e Initial load
duke
parents:
diff changeset
277
a61af66fc99e Initial load
duke
parents:
diff changeset
278 //-- Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
279
a61af66fc99e Initial load
duke
parents:
diff changeset
280 // Java identifier to XML NMTOKEN type string
a61af66fc99e Initial load
duke
parents:
diff changeset
281 private static String identifierToXMLName(String name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
282 // for now, just replace '$' with '_'
a61af66fc99e Initial load
duke
parents:
diff changeset
283 return name.replace('$', '_');
a61af66fc99e Initial load
duke
parents:
diff changeset
284 }
a61af66fc99e Initial load
duke
parents:
diff changeset
285
a61af66fc99e Initial load
duke
parents:
diff changeset
286 // escapes XML meta-characters and illegal characters
a61af66fc99e Initial load
duke
parents:
diff changeset
287 private static String escapeXMLChars(String s) {
a61af66fc99e Initial load
duke
parents:
diff changeset
288 // FIXME: is there a better way or API?
a61af66fc99e Initial load
duke
parents:
diff changeset
289 StringBuffer result = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
290 for(int i = 0, max = s.length(), delta = 0; i < max; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
291 char c = s.charAt(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
292 String replacement = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if (c == '&') {
a61af66fc99e Initial load
duke
parents:
diff changeset
294 replacement = "&amp;";
a61af66fc99e Initial load
duke
parents:
diff changeset
295 } else if (c == '<') {
a61af66fc99e Initial load
duke
parents:
diff changeset
296 replacement = "&lt;";
a61af66fc99e Initial load
duke
parents:
diff changeset
297 } else if (c == '>') {
a61af66fc99e Initial load
duke
parents:
diff changeset
298 replacement = "&gt;";
a61af66fc99e Initial load
duke
parents:
diff changeset
299 } else if (c == '"') {
a61af66fc99e Initial load
duke
parents:
diff changeset
300 replacement = "&quot;";
a61af66fc99e Initial load
duke
parents:
diff changeset
301 } else if (c == '\'') {
a61af66fc99e Initial load
duke
parents:
diff changeset
302 replacement = "&apos;";
a61af66fc99e Initial load
duke
parents:
diff changeset
303 } else if (c < '\u0020' || (c > '\ud7ff' && c < '\ue000') ||
a61af66fc99e Initial load
duke
parents:
diff changeset
304 c == '\ufffe' || c == '\uffff') {
a61af66fc99e Initial load
duke
parents:
diff changeset
305 // These are illegal in XML -- put these in a CDATA section.
a61af66fc99e Initial load
duke
parents:
diff changeset
306 // Refer to section 2.2 Characters in XML specification at
a61af66fc99e Initial load
duke
parents:
diff changeset
307 // http://www.w3.org/TR/2004/REC-xml-20040204/
a61af66fc99e Initial load
duke
parents:
diff changeset
308 replacement = "<![CDATA[&#x" +
a61af66fc99e Initial load
duke
parents:
diff changeset
309 Integer.toHexString((int)c) + ";]]>";
a61af66fc99e Initial load
duke
parents:
diff changeset
310 }
a61af66fc99e Initial load
duke
parents:
diff changeset
311
a61af66fc99e Initial load
duke
parents:
diff changeset
312 if (replacement != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
313 if (result == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
314 result = new StringBuffer(s);
a61af66fc99e Initial load
duke
parents:
diff changeset
315 }
a61af66fc99e Initial load
duke
parents:
diff changeset
316 result.replace(i + delta, i + delta + 1, replacement);
a61af66fc99e Initial load
duke
parents:
diff changeset
317 delta += (replacement.length() - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
318 }
a61af66fc99e Initial load
duke
parents:
diff changeset
319 }
a61af66fc99e Initial load
duke
parents:
diff changeset
320 if (result == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
321 return s;
a61af66fc99e Initial load
duke
parents:
diff changeset
322 }
a61af66fc99e Initial load
duke
parents:
diff changeset
323 return result.toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
324 }
a61af66fc99e Initial load
duke
parents:
diff changeset
325
a61af66fc99e Initial load
duke
parents:
diff changeset
326 private static String getID(Oop oop) {
a61af66fc99e Initial load
duke
parents:
diff changeset
327 // address as unique id for node -- prefixed by "ID_".
a61af66fc99e Initial load
duke
parents:
diff changeset
328 if (oop == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 return "ID_NULL";
a61af66fc99e Initial load
duke
parents:
diff changeset
330 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
331 return "ID_" + oop.getHandle().toString();
a61af66fc99e Initial load
duke
parents:
diff changeset
332 }
a61af66fc99e Initial load
duke
parents:
diff changeset
333 }
a61af66fc99e Initial load
duke
parents:
diff changeset
334
a61af66fc99e Initial load
duke
parents:
diff changeset
335 private void writeArrayLength(Array array) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
336 writeAttribute("length", "int",
a61af66fc99e Initial load
duke
parents:
diff changeset
337 Integer.toString((int) array.getLength()));
a61af66fc99e Initial load
duke
parents:
diff changeset
338 }
a61af66fc99e Initial load
duke
parents:
diff changeset
339
a61af66fc99e Initial load
duke
parents:
diff changeset
340 private void writeAttribute(String name, String type, String value) {
a61af66fc99e Initial load
duke
parents:
diff changeset
341 out.print("\t<attr name='");
a61af66fc99e Initial load
duke
parents:
diff changeset
342 out.print(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
343 out.print("'><");
a61af66fc99e Initial load
duke
parents:
diff changeset
344 out.print(type);
a61af66fc99e Initial load
duke
parents:
diff changeset
345 out.print('>');
a61af66fc99e Initial load
duke
parents:
diff changeset
346 out.print(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
347 out.print("</");
a61af66fc99e Initial load
duke
parents:
diff changeset
348 out.print(type);
a61af66fc99e Initial load
duke
parents:
diff changeset
349 out.println("></attr>");
a61af66fc99e Initial load
duke
parents:
diff changeset
350 }
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 private void writeEdge(Oop from, Oop to, String name) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
353 out.print("<edge from='");
a61af66fc99e Initial load
duke
parents:
diff changeset
354 out.print(getID(from));
a61af66fc99e Initial load
duke
parents:
diff changeset
355 out.print("' to='");
a61af66fc99e Initial load
duke
parents:
diff changeset
356 out.print(getID(to));
a61af66fc99e Initial load
duke
parents:
diff changeset
357 out.println("'>");
a61af66fc99e Initial load
duke
parents:
diff changeset
358 writeAttribute("name", "string", name);
a61af66fc99e Initial load
duke
parents:
diff changeset
359 out.println("</edge>");
a61af66fc99e Initial load
duke
parents:
diff changeset
360 }
a61af66fc99e Initial load
duke
parents:
diff changeset
361
a61af66fc99e Initial load
duke
parents:
diff changeset
362 private void writeField(Field field, String type, String kind,
a61af66fc99e Initial load
duke
parents:
diff changeset
363 String value) throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
364 // 'type' is GXL type of the attribute
a61af66fc99e Initial load
duke
parents:
diff changeset
365 // 'kind' is Java type code ("B", "C", "Z", "S", "I", "J", "F", "D")
a61af66fc99e Initial load
duke
parents:
diff changeset
366 if (isArray) {
a61af66fc99e Initial load
duke
parents:
diff changeset
367 out.print('\t');
a61af66fc99e Initial load
duke
parents:
diff changeset
368 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
369 out.print("\t<attr name='");
a61af66fc99e Initial load
duke
parents:
diff changeset
370 String name = field.getID().getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
371 out.print(identifierToXMLName(name));
a61af66fc99e Initial load
duke
parents:
diff changeset
372 out.print("' kind='");
a61af66fc99e Initial load
duke
parents:
diff changeset
373 out.print(kind);
a61af66fc99e Initial load
duke
parents:
diff changeset
374 out.print("'>");
a61af66fc99e Initial load
duke
parents:
diff changeset
375 }
a61af66fc99e Initial load
duke
parents:
diff changeset
376 out.print('<');
a61af66fc99e Initial load
duke
parents:
diff changeset
377 out.print(type);
a61af66fc99e Initial load
duke
parents:
diff changeset
378 out.print('>');
a61af66fc99e Initial load
duke
parents:
diff changeset
379 out.print(value);
a61af66fc99e Initial load
duke
parents:
diff changeset
380 out.print("</");
a61af66fc99e Initial load
duke
parents:
diff changeset
381 out.print(type);
a61af66fc99e Initial load
duke
parents:
diff changeset
382 out.print('>');
a61af66fc99e Initial load
duke
parents:
diff changeset
383 if (isArray) {
a61af66fc99e Initial load
duke
parents:
diff changeset
384 out.println();
a61af66fc99e Initial load
duke
parents:
diff changeset
385 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
386 out.println("</attr>");
a61af66fc99e Initial load
duke
parents:
diff changeset
387 }
a61af66fc99e Initial load
duke
parents:
diff changeset
388 }
a61af66fc99e Initial load
duke
parents:
diff changeset
389
a61af66fc99e Initial load
duke
parents:
diff changeset
390 private void writeVMInfo() throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
391 VM vm = VM.getVM();
a61af66fc99e Initial load
duke
parents:
diff changeset
392 writeAttribute("vm-version", "string", vm.getVMRelease());
a61af66fc99e Initial load
duke
parents:
diff changeset
393 writeAttribute("vm-type", "string",
a61af66fc99e Initial load
duke
parents:
diff changeset
394 (vm.isClientCompiler())? "client" :
a61af66fc99e Initial load
duke
parents:
diff changeset
395 ((vm.isServerCompiler())? "server" : "core"));
a61af66fc99e Initial load
duke
parents:
diff changeset
396 writeAttribute("os", "string", vm.getOS());
a61af66fc99e Initial load
duke
parents:
diff changeset
397 writeAttribute("cpu", "string", vm.getCPU());
a61af66fc99e Initial load
duke
parents:
diff changeset
398 writeAttribute("pointer-size", "string",
a61af66fc99e Initial load
duke
parents:
diff changeset
399 Integer.toString((int)vm.getOopSize() * 8));
a61af66fc99e Initial load
duke
parents:
diff changeset
400 }
a61af66fc99e Initial load
duke
parents:
diff changeset
401
a61af66fc99e Initial load
duke
parents:
diff changeset
402 // XML encoding that we'll use
a61af66fc99e Initial load
duke
parents:
diff changeset
403 private static final String ENCODING = "UTF-8";
a61af66fc99e Initial load
duke
parents:
diff changeset
404
a61af66fc99e Initial load
duke
parents:
diff changeset
405 // reference fields of currently visited object
a61af66fc99e Initial load
duke
parents:
diff changeset
406 private List/*<OopField>*/ refFields;
a61af66fc99e Initial load
duke
parents:
diff changeset
407 // are we writing an array now?
a61af66fc99e Initial load
duke
parents:
diff changeset
408 private boolean isArray;
a61af66fc99e Initial load
duke
parents:
diff changeset
409 private PrintWriter out;
a61af66fc99e Initial load
duke
parents:
diff changeset
410 }