annotate agent/src/share/classes/sun/jvm/hotspot/utilities/LivenessAnalysis.java @ 2042:0a8e0d4345b3 hs20-b05 jdk7-b124

7010068: Update all 2010 Oracle-changed OpenJDK files to have the proper copyright dates - first pass Summary: Update the copyright to be 2010 on all changed files in OpenJDK Reviewed-by: jcoomes
author trims
date Mon, 03 Jan 2011 15:30:05 -0800
parents c18cbe5936b8
children
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) 2001, 2006, 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.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.gc_interface.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
31 import sun.jvm.hotspot.memory.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
32 import sun.jvm.hotspot.oops.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
33 import sun.jvm.hotspot.runtime.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 /** Finds all paths from roots to the specified set of objects. NOTE:
a61af66fc99e Initial load
duke
parents:
diff changeset
36 currently only a subset of the roots known to the VM is exposed to
a61af66fc99e Initial load
duke
parents:
diff changeset
37 the SA: objects on the stack, static fields in classes, and JNI
a61af66fc99e Initial load
duke
parents:
diff changeset
38 handles. These should be most of the user-level roots keeping
a61af66fc99e Initial load
duke
parents:
diff changeset
39 objects alive. */
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41 public class LivenessAnalysis {
a61af66fc99e Initial load
duke
parents:
diff changeset
42 // Used for debugging this code
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private static final boolean DEBUG = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 private LivenessAnalysis() {}
a61af66fc99e Initial load
duke
parents:
diff changeset
46
a61af66fc99e Initial load
duke
parents:
diff changeset
47 public static LivenessPathList computeAllLivenessPaths(Oop target) {
a61af66fc99e Initial load
duke
parents:
diff changeset
48 LivenessPathList list = computeAllLivenessPaths(target, true);
a61af66fc99e Initial load
duke
parents:
diff changeset
49 if ((list == null) || (list.size() == 0)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
50 // Dead object
a61af66fc99e Initial load
duke
parents:
diff changeset
51 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53 return list;
a61af66fc99e Initial load
duke
parents:
diff changeset
54 }
a61af66fc99e Initial load
duke
parents:
diff changeset
55
a61af66fc99e Initial load
duke
parents:
diff changeset
56 //---------------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
57 // Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
58 //
a61af66fc99e Initial load
duke
parents:
diff changeset
59
a61af66fc99e Initial load
duke
parents:
diff changeset
60 // Returns true if a new path was completed, otherwise false
a61af66fc99e Initial load
duke
parents:
diff changeset
61 // indicating there were no more paths to complete.
a61af66fc99e Initial load
duke
parents:
diff changeset
62 //
a61af66fc99e Initial load
duke
parents:
diff changeset
63 // The trimPathsThroughPopularObjects flag alters the behavior of
a61af66fc99e Initial load
duke
parents:
diff changeset
64 // the returned results. If true, then if multiple paths to
a61af66fc99e Initial load
duke
parents:
diff changeset
65 // different roots all go through a particular popular object, those
a61af66fc99e Initial load
duke
parents:
diff changeset
66 // paths will be truncated and only one (arbitrary one) will be be
a61af66fc99e Initial load
duke
parents:
diff changeset
67 // returned. On the other hand, if the target object itself is
a61af66fc99e Initial load
duke
parents:
diff changeset
68 // popular and there are multiple distinct paths to it (indicating
a61af66fc99e Initial load
duke
parents:
diff changeset
69 // that there are multiple objects pointing directly to it) then all
a61af66fc99e Initial load
duke
parents:
diff changeset
70 // of those paths will be reported.
a61af66fc99e Initial load
duke
parents:
diff changeset
71 private static LivenessPathList computeAllLivenessPaths(Oop target, boolean trimPathsThroughPopularObjects) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 ReversePtrs rev = VM.getVM().getRevPtrs();
a61af66fc99e Initial load
duke
parents:
diff changeset
73 if (rev == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
74 throw new RuntimeException("LivenessAnalysis requires ReversePtrs to have been computed");
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // Currently the reverse pointer analysis returns non-null results
a61af66fc99e Initial load
duke
parents:
diff changeset
78 // only for live objects
a61af66fc99e Initial load
duke
parents:
diff changeset
79 if (rev.get(target) == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
80 // Object is dead
a61af66fc99e Initial load
duke
parents:
diff changeset
81 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
82 }
a61af66fc99e Initial load
duke
parents:
diff changeset
83
a61af66fc99e Initial load
duke
parents:
diff changeset
84 // HashSet of Oops acting as a bit mask indicating which ones have
a61af66fc99e Initial load
duke
parents:
diff changeset
85 // already been traversed
a61af66fc99e Initial load
duke
parents:
diff changeset
86 Set/*<Oop>*/ visitedOops = new HashSet/*<Oop>*/();
a61af66fc99e Initial load
duke
parents:
diff changeset
87
a61af66fc99e Initial load
duke
parents:
diff changeset
88 // IdentityHashMap of LivenessElements acting as a bit mask
a61af66fc99e Initial load
duke
parents:
diff changeset
89 // indicating which roots have already been traversed
a61af66fc99e Initial load
duke
parents:
diff changeset
90 Map/*<LivenessElement, LivenessElement>*/ visitedRoots =
a61af66fc99e Initial load
duke
parents:
diff changeset
91 new IdentityHashMap/*<LivenessElement, LivenessElement>*/();
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 visitedOops.add(target);
a61af66fc99e Initial load
duke
parents:
diff changeset
94
a61af66fc99e Initial load
duke
parents:
diff changeset
95 // Construct the initial LivenessPath
a61af66fc99e Initial load
duke
parents:
diff changeset
96 LivenessPathList list = new LivenessPathList();
a61af66fc99e Initial load
duke
parents:
diff changeset
97 {
a61af66fc99e Initial load
duke
parents:
diff changeset
98 LivenessPath path = new LivenessPath();
a61af66fc99e Initial load
duke
parents:
diff changeset
99 path.push(new LivenessPathElement(target, null));
a61af66fc99e Initial load
duke
parents:
diff changeset
100 list.add(path);
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // Iterate until done
a61af66fc99e Initial load
duke
parents:
diff changeset
104 while (true) {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 // See if there are any incomplete paths left
a61af66fc99e Initial load
duke
parents:
diff changeset
106 LivenessPath path = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
107
a61af66fc99e Initial load
duke
parents:
diff changeset
108 for (int i = list.size() - 1; i >= 0; i--) {
a61af66fc99e Initial load
duke
parents:
diff changeset
109 LivenessPath tmp = list.get(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 if (!tmp.isComplete()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 path = tmp;
a61af66fc99e Initial load
duke
parents:
diff changeset
112 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
113 }
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 // If no incomplete paths left, return
a61af66fc99e Initial load
duke
parents:
diff changeset
117 if (path == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
118 return list;
a61af66fc99e Initial load
duke
parents:
diff changeset
119 }
a61af66fc99e Initial load
duke
parents:
diff changeset
120
a61af66fc99e Initial load
duke
parents:
diff changeset
121 // Try to complete this path
a61af66fc99e Initial load
duke
parents:
diff changeset
122
a61af66fc99e Initial load
duke
parents:
diff changeset
123 // Remove the path from the list of reported ones in
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // anticipation of completing it
a61af66fc99e Initial load
duke
parents:
diff changeset
125 list.remove(path);
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
128 // Fetch next set of reverse pointers for the last object on
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // the list
a61af66fc99e Initial load
duke
parents:
diff changeset
130 ArrayList/*<LivenessPathElement>*/ nextPtrs =
a61af66fc99e Initial load
duke
parents:
diff changeset
131 rev.get(path.peek().getObj());
a61af66fc99e Initial load
duke
parents:
diff changeset
132
a61af66fc99e Initial load
duke
parents:
diff changeset
133 // Depending on exactly what the reverse pointers analysis
a61af66fc99e Initial load
duke
parents:
diff changeset
134 // yields, these results may be null, although currently they
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // won't be
a61af66fc99e Initial load
duke
parents:
diff changeset
136 if (nextPtrs != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 // Iterate these
a61af66fc99e Initial load
duke
parents:
diff changeset
138 for (Iterator iter = nextPtrs.iterator(); iter.hasNext(); ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 LivenessPathElement nextElement = (LivenessPathElement) iter.next();
a61af66fc99e Initial load
duke
parents:
diff changeset
140 // See whether we've visited this element yet
a61af66fc99e Initial load
duke
parents:
diff changeset
141 if ((nextElement.isRoot() && (visitedRoots.get(nextElement) == null)) ||
a61af66fc99e Initial load
duke
parents:
diff changeset
142 (!nextElement.isRoot() && !visitedOops.contains(nextElement.getObj()))) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 // Show we've visited this one
a61af66fc99e Initial load
duke
parents:
diff changeset
144 if (nextElement.isRoot()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 visitedRoots.put(nextElement, nextElement);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 visitedOops.add(nextElement.getObj());
a61af66fc99e Initial load
duke
parents:
diff changeset
148 }
a61af66fc99e Initial load
duke
parents:
diff changeset
149
a61af66fc99e Initial load
duke
parents:
diff changeset
150 // Create a new LivenessPath for each one
a61af66fc99e Initial load
duke
parents:
diff changeset
151 LivenessPath nextPath = path.copy();
a61af66fc99e Initial load
duke
parents:
diff changeset
152 nextPath.push(nextElement);
a61af66fc99e Initial load
duke
parents:
diff changeset
153
a61af66fc99e Initial load
duke
parents:
diff changeset
154 // Regardless of whether we found a root, put the
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // original path back on the list because we're going to
a61af66fc99e Initial load
duke
parents:
diff changeset
156 // do depth-first searching rather than breadth-first
a61af66fc99e Initial load
duke
parents:
diff changeset
157 list.add(path);
a61af66fc99e Initial load
duke
parents:
diff changeset
158 list.add(nextPath);
a61af66fc99e Initial load
duke
parents:
diff changeset
159
a61af66fc99e Initial load
duke
parents:
diff changeset
160 // See whether this path is complete (i.e., it
a61af66fc99e Initial load
duke
parents:
diff changeset
161 // terminates in a root)
a61af66fc99e Initial load
duke
parents:
diff changeset
162 if (trimPathsThroughPopularObjects && nextElement.isRoot()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 // Go back through the path list and remove all
a61af66fc99e Initial load
duke
parents:
diff changeset
164 // incomplete paths ending in any of the intermediate
a61af66fc99e Initial load
duke
parents:
diff changeset
165 // (non-root and non-terminal) nodes in this path.
a61af66fc99e Initial load
duke
parents:
diff changeset
166 // This has the effect of removing multiple paths
a61af66fc99e Initial load
duke
parents:
diff changeset
167 // going through popular objects.
a61af66fc99e Initial load
duke
parents:
diff changeset
168 for (int i = 1; i < nextPath.size() - 1; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 LivenessPathElement el = nextPath.get(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
170 int j = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
171 while (j < list.size()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
172 LivenessPath curPath = list.get(j);
a61af66fc99e Initial load
duke
parents:
diff changeset
173 // We can use an object identity since these
a61af66fc99e Initial load
duke
parents:
diff changeset
174 // intermediate nodes are canonicalized via the
a61af66fc99e Initial load
duke
parents:
diff changeset
175 // ReversePtrsAnalysis
a61af66fc99e Initial load
duke
parents:
diff changeset
176 if (curPath.peek() == el) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 list.remove(curPath);
a61af66fc99e Initial load
duke
parents:
diff changeset
178 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
179 j++;
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 }
a61af66fc99e Initial load
duke
parents:
diff changeset
184
a61af66fc99e Initial load
duke
parents:
diff changeset
185 // Do depth-first searching, not breadth-first
a61af66fc99e Initial load
duke
parents:
diff changeset
186 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
187 }
a61af66fc99e Initial load
duke
parents:
diff changeset
188 }
a61af66fc99e Initial load
duke
parents:
diff changeset
189 }
a61af66fc99e Initial load
duke
parents:
diff changeset
190 } catch (Exception e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 System.err.println("LivenessAnalysis: WARNING: " + e +
a61af66fc99e Initial load
duke
parents:
diff changeset
192 " during traversal");
a61af66fc99e Initial load
duke
parents:
diff changeset
193 }
a61af66fc99e Initial load
duke
parents:
diff changeset
194 }
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }