annotate agent/src/share/classes/sun/jvm/hotspot/tools/JMap.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
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 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
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.tools;
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 sun.jvm.hotspot.utilities.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 public class JMap extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
31 public JMap(int m) {
a61af66fc99e Initial load
duke
parents:
diff changeset
32 mode = m;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 }
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 public JMap() {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 this(MODE_PMAP);
a61af66fc99e Initial load
duke
parents:
diff changeset
37 }
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 protected boolean needsJavaPrefix() {
a61af66fc99e Initial load
duke
parents:
diff changeset
40 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
41 }
a61af66fc99e Initial load
duke
parents:
diff changeset
42
a61af66fc99e Initial load
duke
parents:
diff changeset
43 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
44 return "jmap";
a61af66fc99e Initial load
duke
parents:
diff changeset
45 }
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 protected String getCommandFlags() {
a61af66fc99e Initial load
duke
parents:
diff changeset
48 return "-heap|-heap:format=b|-histo|-permstat|-finalizerinfo";
a61af66fc99e Initial load
duke
parents:
diff changeset
49 }
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 protected void printFlagsUsage() {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 System.out.println(" <no option>\tto print same info as Solaris pmap");
a61af66fc99e Initial load
duke
parents:
diff changeset
53 System.out.println(" -heap\tto print java heap summary");
a61af66fc99e Initial load
duke
parents:
diff changeset
54 System.out.println(" -heap:format=b\tto dump java heap in hprof binary format");
a61af66fc99e Initial load
duke
parents:
diff changeset
55 System.out.println(" -histo\tto print histogram of java object heap");
a61af66fc99e Initial load
duke
parents:
diff changeset
56 System.out.println(" -permstat\tto print permanent generation statistics");
a61af66fc99e Initial load
duke
parents:
diff changeset
57 System.out.println(" -finalizerinfo\tto print information on objects awaiting finalization");
a61af66fc99e Initial load
duke
parents:
diff changeset
58 super.printFlagsUsage();
a61af66fc99e Initial load
duke
parents:
diff changeset
59 }
a61af66fc99e Initial load
duke
parents:
diff changeset
60
a61af66fc99e Initial load
duke
parents:
diff changeset
61 public static final int MODE_HEAP_SUMMARY = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
62 public static final int MODE_HISTOGRAM = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
63 public static final int MODE_PERMSTAT = 2;
a61af66fc99e Initial load
duke
parents:
diff changeset
64 public static final int MODE_PMAP = 3;
a61af66fc99e Initial load
duke
parents:
diff changeset
65 public static final int MODE_HEAP_GRAPH_HPROF_BIN = 4;
a61af66fc99e Initial load
duke
parents:
diff changeset
66 public static final int MODE_HEAP_GRAPH_GXL = 5;
a61af66fc99e Initial load
duke
parents:
diff changeset
67 public static final int MODE_FINALIZERINFO = 6;
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 Tool tool = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 switch (mode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72
a61af66fc99e Initial load
duke
parents:
diff changeset
73 case MODE_HEAP_SUMMARY:
a61af66fc99e Initial load
duke
parents:
diff changeset
74 tool = new HeapSummary();
a61af66fc99e Initial load
duke
parents:
diff changeset
75 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 case MODE_HISTOGRAM:
a61af66fc99e Initial load
duke
parents:
diff changeset
78 tool = new ObjectHistogram();
a61af66fc99e Initial load
duke
parents:
diff changeset
79 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
80
a61af66fc99e Initial load
duke
parents:
diff changeset
81 case MODE_PERMSTAT:
a61af66fc99e Initial load
duke
parents:
diff changeset
82 tool = new PermStat();
a61af66fc99e Initial load
duke
parents:
diff changeset
83 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
84
a61af66fc99e Initial load
duke
parents:
diff changeset
85 case MODE_PMAP:
a61af66fc99e Initial load
duke
parents:
diff changeset
86 tool = new PMap();
a61af66fc99e Initial load
duke
parents:
diff changeset
87 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 case MODE_HEAP_GRAPH_HPROF_BIN:
a61af66fc99e Initial load
duke
parents:
diff changeset
90 writeHeapHprofBin();
a61af66fc99e Initial load
duke
parents:
diff changeset
91 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 case MODE_HEAP_GRAPH_GXL:
a61af66fc99e Initial load
duke
parents:
diff changeset
94 writeHeapGXL();
a61af66fc99e Initial load
duke
parents:
diff changeset
95 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
96
a61af66fc99e Initial load
duke
parents:
diff changeset
97 case MODE_FINALIZERINFO:
a61af66fc99e Initial load
duke
parents:
diff changeset
98 tool = new FinalizerInfo();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
100
a61af66fc99e Initial load
duke
parents:
diff changeset
101 default:
a61af66fc99e Initial load
duke
parents:
diff changeset
102 usage();
a61af66fc99e Initial load
duke
parents:
diff changeset
103 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
104 }
a61af66fc99e Initial load
duke
parents:
diff changeset
105
a61af66fc99e Initial load
duke
parents:
diff changeset
106 tool.setAgent(getAgent());
a61af66fc99e Initial load
duke
parents:
diff changeset
107 tool.setDebugeeType(getDebugeeType());
a61af66fc99e Initial load
duke
parents:
diff changeset
108 tool.run();
a61af66fc99e Initial load
duke
parents:
diff changeset
109 }
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
112 int mode = MODE_PMAP;
a61af66fc99e Initial load
duke
parents:
diff changeset
113 if (args.length > 1 ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 String modeFlag = args[0];
a61af66fc99e Initial load
duke
parents:
diff changeset
115 boolean copyArgs = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
116 if (modeFlag.equals("-heap")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
117 mode = MODE_HEAP_SUMMARY;
a61af66fc99e Initial load
duke
parents:
diff changeset
118 } else if (modeFlag.equals("-histo")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 mode = MODE_HISTOGRAM;
a61af66fc99e Initial load
duke
parents:
diff changeset
120 } else if (modeFlag.equals("-permstat")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 mode = MODE_PERMSTAT;
a61af66fc99e Initial load
duke
parents:
diff changeset
122 } else if (modeFlag.equals("-finalizerinfo")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
123 mode = MODE_FINALIZERINFO;
a61af66fc99e Initial load
duke
parents:
diff changeset
124 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
125 int index = modeFlag.indexOf("-heap:format=");
a61af66fc99e Initial load
duke
parents:
diff changeset
126 if (index != -1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
127 String format = modeFlag.substring(1 + modeFlag.indexOf('='));
a61af66fc99e Initial load
duke
parents:
diff changeset
128 if (format.equals("b")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 mode = MODE_HEAP_GRAPH_HPROF_BIN;
a61af66fc99e Initial load
duke
parents:
diff changeset
130 } else if (format.equals("x")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
131 mode = MODE_HEAP_GRAPH_GXL;
a61af66fc99e Initial load
duke
parents:
diff changeset
132 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
133 System.err.println("unknown heap format:" + format);
a61af66fc99e Initial load
duke
parents:
diff changeset
134 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 copyArgs = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 if (copyArgs) {
a61af66fc99e Initial load
duke
parents:
diff changeset
142 String[] newArgs = new String[args.length - 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
143 for (int i = 0; i < newArgs.length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 newArgs[i] = args[i + 1];
a61af66fc99e Initial load
duke
parents:
diff changeset
145 }
a61af66fc99e Initial load
duke
parents:
diff changeset
146 args = newArgs;
a61af66fc99e Initial load
duke
parents:
diff changeset
147 }
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 JMap jmap = new JMap(mode);
a61af66fc99e Initial load
duke
parents:
diff changeset
151 jmap.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
152 jmap.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 public boolean writeHeapHprofBin(String fileName) {
a61af66fc99e Initial load
duke
parents:
diff changeset
156 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 HeapGraphWriter hgw = new HeapHprofBinWriter();
a61af66fc99e Initial load
duke
parents:
diff changeset
158 hgw.write(fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
159 System.out.println("heap written to " + fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
160 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
161 } catch (IOException exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 System.err.println(exp.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
163 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }
a61af66fc99e Initial load
duke
parents:
diff changeset
165 }
a61af66fc99e Initial load
duke
parents:
diff changeset
166
a61af66fc99e Initial load
duke
parents:
diff changeset
167 public boolean writeHeapHprofBin() {
a61af66fc99e Initial load
duke
parents:
diff changeset
168 return writeHeapHprofBin("heap.bin");
a61af66fc99e Initial load
duke
parents:
diff changeset
169 }
a61af66fc99e Initial load
duke
parents:
diff changeset
170
a61af66fc99e Initial load
duke
parents:
diff changeset
171 private boolean writeHeapGXL(String fileName) {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
173 HeapGraphWriter hgw = new HeapGXLWriter();
a61af66fc99e Initial load
duke
parents:
diff changeset
174 hgw.write(fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
175 System.out.println("heap written to " + fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
176 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
177 } catch (IOException exp) {
a61af66fc99e Initial load
duke
parents:
diff changeset
178 System.err.println(exp.getMessage());
a61af66fc99e Initial load
duke
parents:
diff changeset
179 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181 }
a61af66fc99e Initial load
duke
parents:
diff changeset
182
a61af66fc99e Initial load
duke
parents:
diff changeset
183 public boolean writeHeapGXL() {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 return writeHeapGXL("heap.xml");
a61af66fc99e Initial load
duke
parents:
diff changeset
185 }
a61af66fc99e Initial load
duke
parents:
diff changeset
186
a61af66fc99e Initial load
duke
parents:
diff changeset
187 private int mode;
a61af66fc99e Initial load
duke
parents:
diff changeset
188 }