jdk/src/share/classes/com/sun/tools/hat/internal/model/Root.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 468 642c8c0be52e
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 468
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * The Original Code is HAT. The Initial Developer of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Original Code is Bill Foote, with contributions from others
468
642c8c0be52e 6695553: Cleanup GPLv2+SPL legal notices in hat sources
ohair
parents: 2
diff changeset
    30
 * at JavaSoft/Sun.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package com.sun.tools.hat.internal.model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.hat.internal.util.Misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author      Bill Foote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Represents a member of the rootset, that is, one of the objects that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the GC starts from when marking reachable objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class Root {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private long id;            // ID of the JavaThing we refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private long refererId;     // Thread or Class responsible for this, or 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int index = -1;             // Index in Snapshot.roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private String description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private JavaHeapObject referer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private StackTrace stackTrace = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Values for type.  Higher values are more interesting -- see getType().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // See also getTypeName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public final static int INVALID_TYPE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public final static int UNKNOWN = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public final static int SYSTEM_CLASS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public final static int NATIVE_LOCAL = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public final static int NATIVE_STATIC = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public final static int THREAD_BLOCK = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public final static int BUSY_MONITOR = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public final static int JAVA_LOCAL = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public final static int NATIVE_STACK = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public final static int JAVA_STATIC = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public Root(long id, long refererId, int type, String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this(id, refererId, type, description, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public Root(long id, long refererId, int type, String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                StackTrace stackTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.id = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.refererId = refererId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.description = description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.stackTrace = stackTrace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public long getId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public String getIdString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return Misc.toHex(id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public String getDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if ("".equals(description)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return getTypeName() + " Reference";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return description;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Return type.  We guarantee that more interesting roots will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * a type that is numerically higher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public String getTypeName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        switch(type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            case INVALID_TYPE:          return "Invalid (?!?)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            case UNKNOWN:               return "Unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            case SYSTEM_CLASS:          return "System Class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            case NATIVE_LOCAL:          return "JNI Local";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            case NATIVE_STATIC:         return "JNI Global";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            case THREAD_BLOCK:          return "Thread Block";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            case BUSY_MONITOR:          return "Busy Monitor";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            case JAVA_LOCAL:            return "Java Local";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            case NATIVE_STACK:          return "Native Stack (possibly Java local)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            case JAVA_STATIC:           return "Java Static";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            default:                    return "??";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Given two Root instances, return the one that is most interesting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Root mostInteresting(Root other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (other.type > this.type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Get the object that's responsible for this root, if there is one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * This will be null, a Thread object, or a Class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public JavaHeapObject getReferer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return referer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the stack trace responsible for this root, or null if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public StackTrace getStackTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return stackTrace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return The index of this root in Snapshot.roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    void resolve(Snapshot ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (refererId != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            referer = ss.findThing(refererId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (stackTrace != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            stackTrace.resolve(ss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    void setIndex(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}