1 /* 2 * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved. 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 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 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; 120 ik.iterateStaticFields( 121 new DefaultOopVisitor() { 122 public void doOop(OopField field, boolean isVMField) { 123 Oop next = field.getValue(getObj()); 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 } 145 }); 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 284 public void visitCompOopAddress(Address addr) { 285 Oop next = heap.newOop(addr.getCompOopHandleAt(0)); 286 LivenessPathElement lp = new LivenessPathElement(null, 287 new NamedFieldIdentifier(baseRootDescription + 288 " @ " + addr)); 289 rp.put(lp, next); 290 markAndTraverse(next); 291 } 292 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 }