annotate agent/src/share/classes/sun/jvm/hotspot/tools/FinalizerInfo.java @ 17524:89152779163c

Merge with jdk8-b132
author Gilles Duboscq <duboscq@ssw.jku.at>
date Wed, 15 Oct 2014 11:59:32 +0200
parents 4ca6dc0799b6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
17524
89152779163c Merge with jdk8-b132
Gilles Duboscq <duboscq@ssw.jku.at>
parents: 14909
diff changeset
2 * Copyright (c) 2004, 2013, 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
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
27 import sun.jvm.hotspot.debugger.JVMDebugger;
0
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import sun.jvm.hotspot.tools.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.runtime.VM;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.utilities.ObjectReader;
a61af66fc99e Initial load
duke
parents:
diff changeset
34 import sun.jvm.hotspot.utilities.MarkBits;
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 import java.util.HashMap;
a61af66fc99e Initial load
duke
parents:
diff changeset
37 import java.util.ArrayList;
a61af66fc99e Initial load
duke
parents:
diff changeset
38 import java.util.Collections;
a61af66fc99e Initial load
duke
parents:
diff changeset
39 import java.util.Comparator;
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
42 * Iterates over the queue of object pending finalization and prints a
a61af66fc99e Initial load
duke
parents:
diff changeset
43 * summary of these objects in the form of a histogram.
a61af66fc99e Initial load
duke
parents:
diff changeset
44 */
a61af66fc99e Initial load
duke
parents:
diff changeset
45 public class FinalizerInfo extends Tool {
11054
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
46
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
47 public FinalizerInfo() {
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
48 super();
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
49 }
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
50
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
51 public FinalizerInfo(JVMDebugger d) {
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
52 super(d);
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
53 }
38ea2efa32a7 8010278: SA: provide mechanism for using an alternative SA debugger back-end.
kevinw
parents: 2411
diff changeset
54
0
a61af66fc99e Initial load
duke
parents:
diff changeset
55 public static void main(String[] args) {
a61af66fc99e Initial load
duke
parents:
diff changeset
56 FinalizerInfo finfo = new FinalizerInfo();
12932
7fe6ef09d242 8025638: jmap returns 0 instead of 1 when it fails.
farvidsson
parents: 11054
diff changeset
57 finfo.execute(args);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
58 }
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 public void run() {
a61af66fc99e Initial load
duke
parents:
diff changeset
61 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
62 * The implementation here has a dependency on the implementation of
a61af66fc99e Initial load
duke
parents:
diff changeset
63 * java.lang.ref.Finalizer. If the Finalizer implementation changes it's
a61af66fc99e Initial load
duke
parents:
diff changeset
64 * possible this method will require changes too. We looked into using
a61af66fc99e Initial load
duke
parents:
diff changeset
65 * ObjectReader to deserialize the objects from the target VM but as
a61af66fc99e Initial load
duke
parents:
diff changeset
66 * there aren't any public methods to traverse the queue it means using
a61af66fc99e Initial load
duke
parents:
diff changeset
67 * reflection which will also tie us to the implementation.
a61af66fc99e Initial load
duke
parents:
diff changeset
68 *
a61af66fc99e Initial load
duke
parents:
diff changeset
69 * The assumption here is that Finalizer.queue is the ReferenceQueue
a61af66fc99e Initial load
duke
parents:
diff changeset
70 * with the objects awaiting finalization. The ReferenceQueue queueLength
a61af66fc99e Initial load
duke
parents:
diff changeset
71 * is the number of objects in the queue, and 'head' is the head of the
a61af66fc99e Initial load
duke
parents:
diff changeset
72 * queue.
a61af66fc99e Initial load
duke
parents:
diff changeset
73 */
a61af66fc99e Initial load
duke
parents:
diff changeset
74 InstanceKlass ik =
a61af66fc99e Initial load
duke
parents:
diff changeset
75 SystemDictionaryHelper.findInstanceKlass("java.lang.ref.Finalizer");
2411
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
76 final Oop[] queueref = new Oop[1];
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
77 ik.iterateStaticFields(new DefaultOopVisitor() {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
78 public void doOop(OopField field, boolean isVMField) {
2411
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
79 String name = field.getID().getName();
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
80 if (name.equals("queue")) {
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
81 queueref[0] = field.getValue(getObj());
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
82 }
0
a61af66fc99e Initial load
duke
parents:
diff changeset
83 }
2411
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
84 });
63997f575155 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 1552
diff changeset
85 Oop queue = queueref[0];
0
a61af66fc99e Initial load
duke
parents:
diff changeset
86
a61af66fc99e Initial load
duke
parents:
diff changeset
87 InstanceKlass k = (InstanceKlass) queue.getKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
88
a61af66fc99e Initial load
duke
parents:
diff changeset
89 LongField queueLengthField = (LongField) k.findField("queueLength", "J");
a61af66fc99e Initial load
duke
parents:
diff changeset
90 long queueLength = queueLengthField.getValue(queue);
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 OopField headField = (OopField) k.findField("head", "Ljava/lang/ref/Reference;");
a61af66fc99e Initial load
duke
parents:
diff changeset
93 Oop head = headField.getValue(queue);
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 System.out.println("Number of objects pending for finalization: " + queueLength);
a61af66fc99e Initial load
duke
parents:
diff changeset
96
a61af66fc99e Initial load
duke
parents:
diff changeset
97 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
98 * If 'head' is non-NULL then it is the head of a list of References.
a61af66fc99e Initial load
duke
parents:
diff changeset
99 * We iterate over the list (end of list is when head.next == head)
a61af66fc99e Initial load
duke
parents:
diff changeset
100 */
a61af66fc99e Initial load
duke
parents:
diff changeset
101 if (head != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
102 k = (InstanceKlass) head.getKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
103 OopField referentField =
a61af66fc99e Initial load
duke
parents:
diff changeset
104 (OopField) k.findField("referent", "Ljava/lang/Object;");
a61af66fc99e Initial load
duke
parents:
diff changeset
105 OopField nextField =
a61af66fc99e Initial load
duke
parents:
diff changeset
106 (OopField) k.findField("next", "Ljava/lang/ref/Reference;");
a61af66fc99e Initial load
duke
parents:
diff changeset
107
a61af66fc99e Initial load
duke
parents:
diff changeset
108 HashMap map = new HashMap();
a61af66fc99e Initial load
duke
parents:
diff changeset
109 for (;;) {
a61af66fc99e Initial load
duke
parents:
diff changeset
110 Oop referent = referentField.getValue(head);
a61af66fc99e Initial load
duke
parents:
diff changeset
111
a61af66fc99e Initial load
duke
parents:
diff changeset
112 Klass klass = referent.getKlass();
a61af66fc99e Initial load
duke
parents:
diff changeset
113 if (!map.containsKey(klass)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 map.put(klass, new ObjectHistogramElement(klass));
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116 ((ObjectHistogramElement)map.get(klass)).updateWith(referent);
a61af66fc99e Initial load
duke
parents:
diff changeset
117
a61af66fc99e Initial load
duke
parents:
diff changeset
118 Oop next = nextField.getValue(head);
a61af66fc99e Initial load
duke
parents:
diff changeset
119 if (next == null || next.equals(head)) break;
a61af66fc99e Initial load
duke
parents:
diff changeset
120 head = next;
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
124 * Sort results - decending order by total size
a61af66fc99e Initial load
duke
parents:
diff changeset
125 */
a61af66fc99e Initial load
duke
parents:
diff changeset
126 ArrayList list = new ArrayList();
a61af66fc99e Initial load
duke
parents:
diff changeset
127 list.addAll(map.values());
a61af66fc99e Initial load
duke
parents:
diff changeset
128 Collections.sort(list, new Comparator() {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 public int compare(Object o1, Object o2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 return ((ObjectHistogramElement)o1).compare((ObjectHistogramElement)o2);
a61af66fc99e Initial load
duke
parents:
diff changeset
131 }
a61af66fc99e Initial load
duke
parents:
diff changeset
132 });
a61af66fc99e Initial load
duke
parents:
diff changeset
133
a61af66fc99e Initial load
duke
parents:
diff changeset
134 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
135 * Print summary of objects in queue
a61af66fc99e Initial load
duke
parents:
diff changeset
136 */
a61af66fc99e Initial load
duke
parents:
diff changeset
137 System.out.println("");
a61af66fc99e Initial load
duke
parents:
diff changeset
138 System.out.println("Count" + "\t" + "Class description");
a61af66fc99e Initial load
duke
parents:
diff changeset
139 System.out.println("-------------------------------------------------------");
a61af66fc99e Initial load
duke
parents:
diff changeset
140 for (int i=0; i<list.size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
141 ObjectHistogramElement e = (ObjectHistogramElement)list.get(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
142 System.out.println(e.getCount() + "\t" + e.getDescription());
a61af66fc99e Initial load
duke
parents:
diff changeset
143 }
a61af66fc99e Initial load
duke
parents:
diff changeset
144 }
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146 }
a61af66fc99e Initial load
duke
parents:
diff changeset
147 }