J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | * The contents of this file are subject to the Sun Public License |
| 29 | * Version 1.0 (the "License"); you may not use this file except in |
| 30 | * compliance with the License. A copy of the License is available at |
| 31 | * http://www.sun.com/, and in the file LICENSE.html in the |
| 32 | * doc directory. |
| 33 | * |
| 34 | * The Original Code is HAT. The Initial Developer of the |
| 35 | * Original Code is Bill Foote, with contributions from others |
| 36 | * at JavaSoft/Sun. Portions created by Bill Foote and others |
| 37 | * at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved. |
| 38 | * |
| 39 | * In addition to the formal license, I ask that you don't |
| 40 | * change the history or donations files without permission. |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | package com.sun.tools.hat.internal.model; |
| 45 | |
| 46 | import com.sun.tools.hat.internal.util.Misc; |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @author Bill Foote |
| 51 | */ |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Represents a member of the rootset, that is, one of the objects that |
| 56 | * the GC starts from when marking reachable objects. |
| 57 | */ |
| 58 | |
| 59 | public class Root { |
| 60 | |
| 61 | private long id; // ID of the JavaThing we refer to |
| 62 | private long refererId; // Thread or Class responsible for this, or 0 |
| 63 | private int index = -1; // Index in Snapshot.roots |
| 64 | private int type; |
| 65 | private String description; |
| 66 | private JavaHeapObject referer = null; |
| 67 | private StackTrace stackTrace = null; |
| 68 | |
| 69 | // Values for type. Higher values are more interesting -- see getType(). |
| 70 | // See also getTypeName() |
| 71 | public final static int INVALID_TYPE = 0; |
| 72 | public final static int UNKNOWN = 1; |
| 73 | public final static int SYSTEM_CLASS = 2; |
| 74 | |
| 75 | public final static int NATIVE_LOCAL = 3; |
| 76 | public final static int NATIVE_STATIC = 4; |
| 77 | public final static int THREAD_BLOCK = 5; |
| 78 | public final static int BUSY_MONITOR = 6; |
| 79 | public final static int JAVA_LOCAL = 7; |
| 80 | public final static int NATIVE_STACK = 8; |
| 81 | public final static int JAVA_STATIC = 9; |
| 82 | |
| 83 | |
| 84 | public Root(long id, long refererId, int type, String description) { |
| 85 | this(id, refererId, type, description, null); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | public Root(long id, long refererId, int type, String description, |
| 90 | StackTrace stackTrace) { |
| 91 | this.id = id; |
| 92 | this.refererId = refererId; |
| 93 | this.type = type; |
| 94 | this.description = description; |
| 95 | this.stackTrace = stackTrace; |
| 96 | } |
| 97 | |
| 98 | public long getId() { |
| 99 | return id; |
| 100 | } |
| 101 | |
| 102 | public String getIdString() { |
| 103 | return Misc.toHex(id); |
| 104 | } |
| 105 | |
| 106 | public String getDescription() { |
| 107 | if ("".equals(description)) { |
| 108 | return getTypeName() + " Reference"; |
| 109 | } else { |
| 110 | return description; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Return type. We guarantee that more interesting roots will have |
| 116 | * a type that is numerically higher. |
| 117 | */ |
| 118 | public int getType() { |
| 119 | return type; |
| 120 | } |
| 121 | |
| 122 | public String getTypeName() { |
| 123 | switch(type) { |
| 124 | case INVALID_TYPE: return "Invalid (?!?)"; |
| 125 | case UNKNOWN: return "Unknown"; |
| 126 | case SYSTEM_CLASS: return "System Class"; |
| 127 | case NATIVE_LOCAL: return "JNI Local"; |
| 128 | case NATIVE_STATIC: return "JNI Global"; |
| 129 | case THREAD_BLOCK: return "Thread Block"; |
| 130 | case BUSY_MONITOR: return "Busy Monitor"; |
| 131 | case JAVA_LOCAL: return "Java Local"; |
| 132 | case NATIVE_STACK: return "Native Stack (possibly Java local)"; |
| 133 | case JAVA_STATIC: return "Java Static"; |
| 134 | default: return "??"; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Given two Root instances, return the one that is most interesting. |
| 140 | */ |
| 141 | public Root mostInteresting(Root other) { |
| 142 | if (other.type > this.type) { |
| 143 | return other; |
| 144 | } else { |
| 145 | return this; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get the object that's responsible for this root, if there is one. |
| 151 | * This will be null, a Thread object, or a Class object. |
| 152 | */ |
| 153 | public JavaHeapObject getReferer() { |
| 154 | return referer; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return the stack trace responsible for this root, or null if there |
| 159 | * is none. |
| 160 | */ |
| 161 | public StackTrace getStackTrace() { |
| 162 | return stackTrace; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @return The index of this root in Snapshot.roots |
| 167 | */ |
| 168 | public int getIndex() { |
| 169 | return index; |
| 170 | } |
| 171 | |
| 172 | void resolve(Snapshot ss) { |
| 173 | if (refererId != 0) { |
| 174 | referer = ss.findThing(refererId); |
| 175 | } |
| 176 | if (stackTrace != null) { |
| 177 | stackTrace.resolve(ss); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void setIndex(int i) { |
| 182 | index = i; |
| 183 | } |
| 184 | |
| 185 | } |