Mercurial > hg > graal-compiler
annotate agent/src/share/classes/sun/jvm/hotspot/utilities/ReversePtrsAnalysis.java @ 2426:1d1603768966
7010070: Update all 2010 Oracle-changed OpenJDK files to have the proper copyright dates - second pass
Summary: Update the copyright to be 2010 on all changed files in OpenJDK
Reviewed-by: ohair
author | trims |
---|---|
date | Tue, 05 Apr 2011 14:12:31 -0700 |
parents | 63997f575155 |
children | da91efe96a93 |
rev | line source |
---|---|
0 | 1 /* |
2411
63997f575155
7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents:
1552
diff
changeset
|
2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved. |
0 | 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
196
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:
196
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
25 package sun.jvm.hotspot.utilities; | |
26 | |
27 import java.io.*; | |
28 import java.util.*; | |
29 import sun.jvm.hotspot.debugger.*; | |
30 import sun.jvm.hotspot.gc_interface.*; | |
31 import sun.jvm.hotspot.memory.*; | |
32 import sun.jvm.hotspot.oops.*; | |
33 import sun.jvm.hotspot.runtime.*; | |
34 import sun.jvm.hotspot.utilities.*; | |
35 | |
36 /** For a set of known roots, descends recursively into the object | |
37 graph, for each object recording those objects (and their fields) | |
38 which point to it. NOTE: currently only a subset of the roots | |
39 known to the VM is exposed to the SA: objects on the stack, static | |
40 fields in classes, and JNI handles. These should be most of the | |
41 user-level roots keeping objects alive. */ | |
42 | |
43 public class ReversePtrsAnalysis { | |
44 // Used for debugging this code | |
45 private static final boolean DEBUG = false; | |
46 | |
47 public ReversePtrsAnalysis() { | |
48 } | |
49 | |
50 /** Sets an optional progress thunk */ | |
51 public void setHeapProgressThunk(HeapProgressThunk thunk) { | |
52 progressThunk = thunk; | |
53 } | |
54 | |
55 | |
56 /** Runs the analysis algorithm */ | |
57 public void run() { | |
58 if (VM.getVM().getRevPtrs() != null) { | |
59 return; // Assume already done | |
60 } | |
61 | |
62 VM vm = VM.getVM(); | |
63 rp = new ReversePtrs(); | |
64 vm.setRevPtrs(rp); | |
65 Universe universe = vm.getUniverse(); | |
66 CollectedHeap collHeap = universe.heap(); | |
67 usedSize = collHeap.used(); | |
68 visitedSize = 0; | |
69 | |
70 // Note that an experiment to iterate the heap linearly rather | |
71 // than in recursive-descent order has been done. It turns out | |
72 // that the recursive-descent algorithm is nearly twice as fast | |
73 // due to the fact that it scans only live objects and (currently) | |
74 // only a fraction of the perm gen, namely the static fields | |
75 // contained in instanceKlasses. (Iterating the heap linearly | |
76 // would also change the semantics of the result so that | |
77 // ReversePtrs.get() would return a non-null value even for dead | |
78 // objects.) Nonetheless, the reverse pointer computation is still | |
79 // quite slow and optimization in field iteration of objects | |
80 // should be done. | |
81 | |
82 if (progressThunk != null) { | |
83 // Get it started | |
84 progressThunk.heapIterationFractionUpdate(0); | |
85 } | |
86 | |
87 // Allocate mark bits for heap | |
88 markBits = new MarkBits(collHeap); | |
89 | |
90 // Get a hold of the object heap | |
91 heap = vm.getObjectHeap(); | |
92 | |
93 // Do each thread's roots | |
94 for (JavaThread thread = VM.getVM().getThreads().first(); | |
95 thread != null; | |
96 thread = thread.next()) { | |
97 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
98 thread.printThreadIDOn(new PrintStream(bos)); | |
99 String threadDesc = | |
100 " in thread \"" + thread.getThreadName() + | |
101 "\" (id " + bos.toString() + ")"; | |
102 doStack(thread, | |
103 new RootVisitor("Stack root" + threadDesc)); | |
104 doJNIHandleBlock(thread.activeHandles(), | |
105 new RootVisitor("JNI handle root" + threadDesc)); | |
106 } | |
107 | |
108 // Do global JNI handles | |
109 JNIHandles handles = VM.getVM().getJNIHandles(); | |
110 doJNIHandleBlock(handles.globalHandles(), | |
111 new RootVisitor("Global JNI handle root")); | |
112 doJNIHandleBlock(handles.weakGlobalHandles(), | |
113 new RootVisitor("Weak global JNI handle root")); | |
114 | |
115 // Do Java-level static fields in perm gen | |
116 heap.iteratePerm(new DefaultHeapVisitor() { | |
117 public boolean doObj(Oop obj) { | |
118 if (obj instanceof InstanceKlass) { | |
119 final InstanceKlass ik = (InstanceKlass) obj; | |
2411
63997f575155
7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents:
1552
diff
changeset
|
120 ik.iterateStaticFields( |
0 | 121 new DefaultOopVisitor() { |
122 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
|
123 Oop next = field.getValue(getObj()); |
0 | 124 LivenessPathElement lp = new LivenessPathElement(null, |
125 new NamedFieldIdentifier("Static field \"" + | |
126 field.getID().getName() + | |
127 "\" in class \"" + | |
128 ik.getName().asString() + "\"")); | |
129 rp.put(lp, next); | |
130 try { | |
131 markAndTraverse(next); | |
132 } catch (AddressException e) { | |
133 System.err.print("RevPtrs analysis: WARNING: AddressException at 0x" + | |
134 Long.toHexString(e.getAddress()) + | |
135 " while traversing static fields of InstanceKlass "); | |
136 ik.printValueOn(System.err); | |
137 System.err.println(); | |
138 } catch (UnknownOopException e) { | |
139 System.err.println("RevPtrs analysis: WARNING: UnknownOopException while " + | |
140 "traversing static fields of InstanceKlass "); | |
141 ik.printValueOn(System.err); | |
142 System.err.println(); | |
143 } | |
144 } | |
2411
63997f575155
7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents:
1552
diff
changeset
|
145 }); |
0 | 146 } |
147 return false; | |
148 } | |
149 }); | |
150 | |
151 if (progressThunk != null) { | |
152 progressThunk.heapIterationComplete(); | |
153 } | |
154 | |
155 // Clear out markBits | |
156 markBits = null; | |
157 } | |
158 | |
159 | |
160 //--------------------------------------------------------------------------- | |
161 // Internals only below this point | |
162 // | |
163 private HeapProgressThunk progressThunk; | |
164 private long usedSize; | |
165 private long visitedSize; | |
166 private double lastNotificationFraction; | |
167 private static final double MINIMUM_NOTIFICATION_FRACTION = 0.01; | |
168 private ObjectHeap heap; | |
169 private MarkBits markBits; | |
170 private int depth; // Debugging only | |
171 private ReversePtrs rp; | |
172 | |
173 private void markAndTraverse(OopHandle handle) { | |
174 try { | |
175 markAndTraverse(heap.newOop(handle)); | |
176 } catch (AddressException e) { | |
177 System.err.println("RevPtrs analysis: WARNING: AddressException at 0x" + | |
178 Long.toHexString(e.getAddress()) + | |
179 " while traversing oop at " + handle); | |
180 } catch (UnknownOopException e) { | |
181 System.err.println("RevPtrs analysis: WARNING: UnknownOopException for " + | |
182 "oop at " + handle); | |
183 } | |
184 } | |
185 | |
186 private void printHeader() { | |
187 for (int i = 0; i < depth; i++) { | |
188 System.err.print(" "); | |
189 } | |
190 } | |
191 | |
192 private void markAndTraverse(final Oop obj) { | |
193 | |
194 // End of path | |
195 if (obj == null) { | |
196 return; | |
197 } | |
198 | |
199 // Visited object | |
200 if (!markBits.mark(obj)) { | |
201 return; | |
202 } | |
203 | |
204 // Root of work list for objects to be visited. A simple | |
205 // stack for saving new objects to be analyzed. | |
206 | |
207 final Stack workList = new Stack(); | |
208 | |
209 // Next object to be visited. | |
210 Oop next = obj; | |
211 | |
212 try { | |
213 // Node in the list currently being visited. | |
214 | |
215 while (true) { | |
216 final Oop currObj = next; | |
217 | |
218 // For the progress meter | |
219 if (progressThunk != null) { | |
220 visitedSize += currObj.getObjectSize(); | |
221 double curFrac = (double) visitedSize / (double) usedSize; | |
222 if (curFrac > | |
223 lastNotificationFraction + MINIMUM_NOTIFICATION_FRACTION) { | |
224 progressThunk.heapIterationFractionUpdate(curFrac); | |
225 lastNotificationFraction = curFrac; | |
226 } | |
227 } | |
228 | |
229 if (DEBUG) { | |
230 ++depth; | |
231 printHeader(); | |
232 System.err.println("ReversePtrs.markAndTraverse(" + | |
233 currObj.getHandle() + ")"); | |
234 } | |
235 | |
236 // Iterate over the references in the object. Do the | |
237 // reverse pointer analysis for each reference. | |
238 // Add the reference to the work-list so that its | |
239 // references will be visited. | |
240 currObj.iterate(new DefaultOopVisitor() { | |
241 public void doOop(OopField field, boolean isVMField) { | |
242 // "field" refers to a reference in currObj | |
243 Oop next = field.getValue(currObj); | |
244 rp.put(new LivenessPathElement(currObj, field.getID()), next); | |
245 if ((next != null) && markBits.mark(next)) { | |
246 workList.push(next); | |
247 } | |
248 } | |
249 }, false); | |
250 | |
251 if (DEBUG) { | |
252 --depth; | |
253 } | |
254 | |
255 // Get the next object to visit. | |
256 next = (Oop) workList.pop(); | |
257 } | |
258 } catch (EmptyStackException e) { | |
259 // Done | |
260 } catch (NullPointerException e) { | |
261 System.err.println("ReversePtrs: WARNING: " + e + | |
262 " during traversal"); | |
263 } catch (Exception e) { | |
264 System.err.println("ReversePtrs: WARNING: " + e + | |
265 " during traversal"); | |
266 } | |
267 } | |
268 | |
269 | |
270 class RootVisitor implements AddressVisitor { | |
271 RootVisitor(String baseRootDescription) { | |
272 this.baseRootDescription = baseRootDescription; | |
273 } | |
274 | |
275 public void visitAddress(Address addr) { | |
276 Oop next = heap.newOop(addr.getOopHandleAt(0)); | |
277 LivenessPathElement lp = new LivenessPathElement(null, | |
278 new NamedFieldIdentifier(baseRootDescription + | |
279 " @ " + addr)); | |
280 rp.put(lp, next); | |
281 markAndTraverse(next); | |
282 } | |
283 | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
284 public void visitCompOopAddress(Address addr) { |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
285 Oop next = heap.newOop(addr.getCompOopHandleAt(0)); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
286 LivenessPathElement lp = new LivenessPathElement(null, |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
287 new NamedFieldIdentifier(baseRootDescription + |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
288 " @ " + addr)); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
289 rp.put(lp, next); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
290 markAndTraverse(next); |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
291 } |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
292 |
0 | 293 private String baseRootDescription; |
294 } | |
295 | |
296 // Traverse the roots on a given thread's stack | |
297 private void doStack(JavaThread thread, AddressVisitor oopVisitor) { | |
298 for (StackFrameStream fst = new StackFrameStream(thread); !fst.isDone(); fst.next()) { | |
299 fst.getCurrent().oopsDo(oopVisitor, fst.getRegisterMap()); | |
300 } | |
301 } | |
302 | |
303 // Traverse a JNIHandleBlock | |
304 private void doJNIHandleBlock(JNIHandleBlock handles, AddressVisitor oopVisitor) { | |
305 handles.oopsDo(oopVisitor); | |
306 } | |
307 } |