1 /* 2 * Copyright (c) 2002, 2018, 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.classfile.*; 31 import sun.jvm.hotspot.gc.shared.*; 32 import sun.jvm.hotspot.memory.*; 33 import sun.jvm.hotspot.oops.*; 34 import sun.jvm.hotspot.runtime.*; 35 import sun.jvm.hotspot.utilities.*; 36 37 /** For a set of known roots, descends recursively into the object 38 graph, for each object recording those objects (and their fields) 39 which point to it. NOTE: currently only a subset of the roots 40 known to the VM is exposed to the SA: objects on the stack, static 41 fields in classes, and JNI handles. These should be most of the 42 user-level roots keeping objects alive. */ 43 44 public class ReversePtrsAnalysis { 45 // Used for debugging this code 46 private static final boolean DEBUG = false; 47 48 public ReversePtrsAnalysis() { 49 } 50 51 /** Sets an optional progress thunk */ 52 public void setHeapProgressThunk(HeapProgressThunk thunk) { 53 progressThunk = thunk; 54 } 55 56 57 /** Runs the analysis algorithm */ 58 public void run() { 59 if (VM.getVM().getRevPtrs() != null) { 60 return; // Assume already done 61 } 62 63 VM vm = VM.getVM(); 64 rp = new ReversePtrs(); 65 vm.setRevPtrs(rp); 66 Universe universe = vm.getUniverse(); 67 CollectedHeap collHeap = universe.heap(); 68 usedSize = collHeap.used(); 69 visitedSize = 0; 70 71 // Note that an experiment to iterate the heap linearly rather 72 // than in recursive-descent order has been done. It turns out 73 // that the recursive-descent algorithm is nearly twice as fast 74 // due to the fact that it scans only live objects and (currently) 75 // only a fraction of the perm gen, namely the static fields 76 // contained in instanceKlasses. (Iterating the heap linearly 77 // would also change the semantics of the result so that 78 // ReversePtrs.get() would return a non-null value even for dead 79 // objects.) Nonetheless, the reverse pointer computation is still 80 // quite slow and optimization in field iteration of objects 81 // should be done. 82 83 if (progressThunk != null) { 84 // Get it started 85 progressThunk.heapIterationFractionUpdate(0); 86 } 87 88 // Allocate mark bits for heap 89 markBits = new MarkBits(collHeap); 90 91 // Get a hold of the object heap 92 heap = vm.getObjectHeap(); 93 94 // Do each thread's roots 95 VM.getVM().getThreads().doJavaThreads((thread) -> { 96 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 97 thread.printThreadIDOn(new PrintStream(bos)); 98 String threadDesc = 99 " in thread \"" + thread.getThreadName() + 100 "\" (id " + bos.toString() + ")"; 101 doStack(thread, 102 new RootVisitor("Stack root" + threadDesc)); 103 doJNIHandleBlock(thread.activeHandles(), 104 new RootVisitor("JNI handle root" + threadDesc)); 105 }); 106 107 // Do global JNI handles 108 JNIHandles handles = VM.getVM().getJNIHandles(); 109 doOopStorage(handles.globalHandles(), 110 new RootVisitor("Global JNI handle root")); 111 doOopStorage(handles.weakGlobalHandles(), 112 new RootVisitor("Weak global JNI handle root")); 113 114 // Do Java-level static fields 115 ClassLoaderDataGraph cldg = VM.getVM().getClassLoaderDataGraph(); 116 cldg.classesDo(new ClassLoaderDataGraph.ClassVisitor() { 117 118 public void visit(Klass k) { 119 if (k instanceof InstanceKlass) { 120 final InstanceKlass ik = (InstanceKlass)k; 121 ik.iterateStaticFields( 122 new DefaultOopVisitor() { 123 public void doOop(OopField field, boolean isVMField) { 124 Oop next = field.getValue(getObj()); 125 NamedFieldIdentifier nfi = new NamedFieldIdentifier("Static field \"" + 126 field.getID().getName() + 127 "\" in class \"" + 128 ik.getName().asString() + "\""); 129 LivenessPathElement lp = new LivenessPathElement(null, nfi); 130 rp.put(lp, next); 131 try { 132 markAndTraverse(next); 133 } catch (AddressException e) { 134 System.err.print("RevPtrs analysis: WARNING: AddressException at 0x" + 135 Long.toHexString(e.getAddress()) + 136 " while traversing static fields of InstanceKlass "); 137 ik.printValueOn(System.err); 138 System.err.println(); 139 } catch (UnknownOopException e) { 140 System.err.println("RevPtrs analysis: WARNING: UnknownOopException while " + 141 "traversing static fields of InstanceKlass "); 142 ik.printValueOn(System.err); 143 System.err.println(); 144 } 145 } 146 }); 147 } 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 308 // Traverse jobjects in global JNIHandles 309 private void doOopStorage(OopStorage oopSet, AddressVisitor oopVisitor) { 310 oopSet.oopsDo(oopVisitor); 311 } 312 }