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