annotate agent/src/share/classes/sun/jvm/hotspot/tools/FinalizerInfo.java @ 1552:c18cbe5936b8

6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
author trims
date Thu, 27 May 2010 19:08:38 -0700
parents a61af66fc99e
children 63997f575155
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 2004, 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.tools;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import sun.jvm.hotspot.tools.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28
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.VM;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.utilities.ObjectReader;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.utilities.MarkBits;
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 import java.util.HashMap;
a61af66fc99e Initial load
duke
parents:
diff changeset
36 import java.util.ArrayList;
a61af66fc99e Initial load
duke
parents:
diff changeset
37 import java.util.Collections;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 import java.util.Comparator;
a61af66fc99e Initial load
duke
parents:
diff changeset
39
a61af66fc99e Initial load
duke
parents:
diff changeset
40 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
41 * Iterates over the queue of object pending finalization and prints a
a61af66fc99e Initial load
duke
parents:
diff changeset
42 * summary of these objects in the form of a histogram.
a61af66fc99e Initial load
duke
parents:
diff changeset
43 */
a61af66fc99e Initial load
duke
parents:
diff changeset
44 public class FinalizerInfo extends Tool {
a61af66fc99e Initial load
duke
parents:
diff changeset
45 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 FinalizerInfo finfo = new FinalizerInfo();
a61af66fc99e Initial load
duke
parents:
diff changeset
47 finfo.start(args);
a61af66fc99e Initial load
duke
parents:
diff changeset
48 finfo.stop();
a61af66fc99e Initial load
duke
parents:
diff changeset
49 }
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
53 * The implementation here has a dependency on the implementation of
a61af66fc99e Initial load
duke
parents:
diff changeset
54 * java.lang.ref.Finalizer. If the Finalizer implementation changes it's
a61af66fc99e Initial load
duke
parents:
diff changeset
55 * possible this method will require changes too. We looked into using
a61af66fc99e Initial load
duke
parents:
diff changeset
56 * ObjectReader to deserialize the objects from the target VM but as
a61af66fc99e Initial load
duke
parents:
diff changeset
57 * there aren't any public methods to traverse the queue it means using
a61af66fc99e Initial load
duke
parents:
diff changeset
58 * reflection which will also tie us to the implementation.
a61af66fc99e Initial load
duke
parents:
diff changeset
59 *
a61af66fc99e Initial load
duke
parents:
diff changeset
60 * The assumption here is that Finalizer.queue is the ReferenceQueue
a61af66fc99e Initial load
duke
parents:
diff changeset
61 * with the objects awaiting finalization. The ReferenceQueue queueLength
a61af66fc99e Initial load
duke
parents:
diff changeset
62 * is the number of objects in the queue, and 'head' is the head of the
a61af66fc99e Initial load
duke
parents:
diff changeset
63 * queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
64 */
a61af66fc99e Initial load
duke
parents:
diff changeset
65 InstanceKlass ik =
a61af66fc99e Initial load
duke
parents:
diff changeset
66 SystemDictionaryHelper.findInstanceKlass("java.lang.ref.Finalizer");
a61af66fc99e Initial load
duke
parents:
diff changeset
67 final OopField queueField[] = new OopField[1];
a61af66fc99e Initial load
duke
parents:
diff changeset
68 ik.iterateFields(new DefaultOopVisitor() {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 public void doOop(OopField field, boolean isVMField) {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 String name = field.getID().getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
71 if (name.equals("queue")) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 queueField[0] = field;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74 }
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }, false);
a61af66fc99e Initial load
duke
parents:
diff changeset
76 Oop queue = queueField[0].getValue(ik);
a61af66fc99e Initial load
duke
parents:
diff changeset
77
a61af66fc99e Initial load
duke
parents:
diff changeset
78 InstanceKlass k = (InstanceKlass) queue.getKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
79
a61af66fc99e Initial load
duke
parents:
diff changeset
80 LongField queueLengthField = (LongField) k.findField("queueLength", "J");
a61af66fc99e Initial load
duke
parents:
diff changeset
81 long queueLength = queueLengthField.getValue(queue);
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 OopField headField = (OopField) k.findField("head", "Ljava/lang/ref/Reference;");
a61af66fc99e Initial load
duke
parents:
diff changeset
84 Oop head = headField.getValue(queue);
a61af66fc99e Initial load
duke
parents:
diff changeset
85
a61af66fc99e Initial load
duke
parents:
diff changeset
86 System.out.println("Number of objects pending for finalization: " + queueLength);
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
89 * If 'head' is non-NULL then it is the head of a list of References.
a61af66fc99e Initial load
duke
parents:
diff changeset
90 * We iterate over the list (end of list is when head.next == head)
a61af66fc99e Initial load
duke
parents:
diff changeset
91 */
a61af66fc99e Initial load
duke
parents:
diff changeset
92 if (head != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
93 k = (InstanceKlass) head.getKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
94 OopField referentField =
a61af66fc99e Initial load
duke
parents:
diff changeset
95 (OopField) k.findField("referent", "Ljava/lang/Object;");
a61af66fc99e Initial load
duke
parents:
diff changeset
96 OopField nextField =
a61af66fc99e Initial load
duke
parents:
diff changeset
97 (OopField) k.findField("next", "Ljava/lang/ref/Reference;");
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 HashMap map = new HashMap();
a61af66fc99e Initial load
duke
parents:
diff changeset
100 for (;;) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 Oop referent = referentField.getValue(head);
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 Klass klass = referent.getKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
104 if (!map.containsKey(klass)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 map.put(klass, new ObjectHistogramElement(klass));
a61af66fc99e Initial load
duke
parents:
diff changeset
106 }
a61af66fc99e Initial load
duke
parents:
diff changeset
107 ((ObjectHistogramElement)map.get(klass)).updateWith(referent);
a61af66fc99e Initial load
duke
parents:
diff changeset
108
a61af66fc99e Initial load
duke
parents:
diff changeset
109 Oop next = nextField.getValue(head);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 if (next == null || next.equals(head)) break;
a61af66fc99e Initial load
duke
parents:
diff changeset
111 head = next;
a61af66fc99e Initial load
duke
parents:
diff changeset
112 }
a61af66fc99e Initial load
duke
parents:
diff changeset
113
a61af66fc99e Initial load
duke
parents:
diff changeset
114 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
115 * Sort results - decending order by total size
a61af66fc99e Initial load
duke
parents:
diff changeset
116 */
a61af66fc99e Initial load
duke
parents:
diff changeset
117 ArrayList list = new ArrayList();
a61af66fc99e Initial load
duke
parents:
diff changeset
118 list.addAll(map.values());
a61af66fc99e Initial load
duke
parents:
diff changeset
119 Collections.sort(list, new Comparator() {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 public int compare(Object o1, Object o2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 return ((ObjectHistogramElement)o1).compare((ObjectHistogramElement)o2);
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123 });
a61af66fc99e Initial load
duke
parents:
diff changeset
124
a61af66fc99e Initial load
duke
parents:
diff changeset
125 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
126 * Print summary of objects in queue
a61af66fc99e Initial load
duke
parents:
diff changeset
127 */
a61af66fc99e Initial load
duke
parents:
diff changeset
128 System.out.println("");
a61af66fc99e Initial load
duke
parents:
diff changeset
129 System.out.println("Count" + "\t" + "Class description");
a61af66fc99e Initial load
duke
parents:
diff changeset
130 System.out.println("-------------------------------------------------------");
a61af66fc99e Initial load
duke
parents:
diff changeset
131 for (int i=0; i<list.size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
132 ObjectHistogramElement e = (ObjectHistogramElement)list.get(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
133 System.out.println(e.getCount() + "\t" + e.getDescription());
a61af66fc99e Initial load
duke
parents:
diff changeset
134 }
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }
a61af66fc99e Initial load
duke
parents:
diff changeset
138 }