001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.results.memory;
042:
043: import org.netbeans.lib.profiler.results.RuntimeCCTNode;
044: import java.io.DataInputStream;
045: import java.io.DataOutputStream;
046: import java.io.IOException;
047: import java.util.Set;
048:
049: /**
050: * A node of the run time Memory Profiling Calling Context Tree (CCT). Unlike the presentation-time CCT, this one
051: * contains information in the form that is quickly updateable at run time, but needs further processing for
052: * proper presentation. Instances of class RuntimeMemoryCCTNode are used only as non-terminal nodes, and contain
053: * minimum information to save space. The information such as the total number of calls, size of allocated objects,
054: * etc., which can be calculated for intermediate nodes if known for terminal nodes, is contained, in runtime CCT,
055: * only in specialized terminal nodes (instances of classes RuntimeObjAllocTermCCTNode and
056: * RuntimeObjLivenessTermCCTNode).
057: *
058: * @author Misha Dmitriev
059: * @author Ian Formanek
060: */
061: public class RuntimeMemoryCCTNode implements Cloneable, RuntimeCCTNode {
062: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
063:
064: protected static final int TYPE_RuntimeMemoryCCTNode = 1;
065: protected static final int TYPE_RuntimeObjAllocTermCCTNode = 2;
066: protected static final int TYPE_RuntimeObjLivenessTermCCTNode = 3;
067:
068: //~ Instance fields ----------------------------------------------------------------------------------------------------------
069:
070: /** Children nodes in the forward stack trace tree. This fiels can have three different values depending on the
071: * number of children:
072: * null if there are no children
073: * instance of RuntimeMemoryCCTNode if there is exactly one child
074: * instance of RuntimeMemoryCCTNode[] if there are multiple children
075: * This is purely a memory consumption optimization, which typically saves about 80% of memory, since most allocation
076: * stack traces are a sequence of single-child nodes, and in such case we remove the need to create a one-item array
077: */
078: Object children;
079:
080: /** unique Id of method - jMethodId from JVM (see MemoryCallGraphBuilder.getNamesForJMethodIds) */
081: int methodId;
082:
083: //~ Constructors -------------------------------------------------------------------------------------------------------------
084:
085: /** For I/O only */
086: protected RuntimeMemoryCCTNode() {
087: }
088:
089: RuntimeMemoryCCTNode(int methodId) {
090: this .methodId = methodId;
091: }
092:
093: //~ Methods ------------------------------------------------------------------------------------------------------------------
094:
095: public int getType() {
096: return TYPE_RuntimeMemoryCCTNode;
097: }
098:
099: public static RuntimeMemoryCCTNode create(int type) {
100: switch (type) {
101: case TYPE_RuntimeMemoryCCTNode:
102: return new RuntimeMemoryCCTNode();
103: case TYPE_RuntimeObjAllocTermCCTNode:
104: return new RuntimeObjAllocTermCCTNode();
105: case TYPE_RuntimeObjLivenessTermCCTNode:
106: return new RuntimeObjLivenessTermCCTNode();
107: }
108:
109: throw new IllegalArgumentException("Illegal type: " + type); // NOI18N
110: }
111:
112: public void addAllJMethodIds(Set set) {
113: if (methodId != 0) {
114: set.add(new Integer(methodId));
115: }
116:
117: if (children != null) {
118: if (children instanceof RuntimeMemoryCCTNode) {
119: ((RuntimeMemoryCCTNode) children).addAllJMethodIds(set);
120: } else {
121: RuntimeMemoryCCTNode[] ar = (RuntimeMemoryCCTNode[]) children;
122:
123: for (int i = 0; i < ar.length; i++) {
124: ar[i].addAllJMethodIds(set);
125: }
126: }
127: }
128: }
129:
130: public RuntimeMemoryCCTNode addNewChild(int methodId) {
131: if (children == null) {
132: children = new RuntimeMemoryCCTNode(methodId);
133:
134: return (RuntimeMemoryCCTNode) children;
135: } else {
136: RuntimeMemoryCCTNode[] ar = addChildEntry();
137:
138: return (ar[ar.length - 1] = new RuntimeMemoryCCTNode(
139: methodId));
140: }
141: }
142:
143: public void attachNodeAsChild(RuntimeMemoryCCTNode node) {
144: if (children == null) {
145: children = node;
146: } else {
147: RuntimeMemoryCCTNode[] ar = addChildEntry();
148: ar[ar.length - 1] = node;
149: }
150: }
151:
152: public Object clone() {
153: try {
154: RuntimeMemoryCCTNode ret = (RuntimeMemoryCCTNode) super
155: .clone();
156:
157: if (children != null) {
158: if (children instanceof RuntimeMemoryCCTNode) {
159: ret.children = ((RuntimeMemoryCCTNode) children)
160: .clone();
161: } else {
162: RuntimeMemoryCCTNode[] ar = (RuntimeMemoryCCTNode[]) children;
163: ret.children = new RuntimeMemoryCCTNode[ar.length];
164:
165: for (int i = 0; i < ar.length; i++) {
166: ((RuntimeMemoryCCTNode[]) ret.children)[i] = (RuntimeMemoryCCTNode) ar[i]
167: .clone();
168: }
169: }
170: }
171:
172: return ret;
173: } catch (CloneNotSupportedException e) {
174: throw new InternalError("Clone should never fail"); // NOI18N
175: }
176: }
177:
178: public void readFromStream(DataInputStream in) throws IOException {
179: methodId = in.readInt();
180:
181: int len = in.readInt();
182:
183: if (len == 0) {
184: children = null;
185: } else if (len == 1) {
186: int type = in.readInt();
187: children = RuntimeMemoryCCTNode.create(type);
188: ((RuntimeMemoryCCTNode) children).readFromStream(in);
189: } else {
190: RuntimeMemoryCCTNode[] ar = new RuntimeMemoryCCTNode[len];
191:
192: for (int i = 0; i < len; i++) {
193: int type = in.readInt();
194: ar[i] = RuntimeMemoryCCTNode.create(type);
195: ar[i].readFromStream(in);
196: }
197:
198: children = ar;
199: }
200: }
201:
202: public void writeToStream(DataOutputStream out) throws IOException {
203: out.writeInt(methodId);
204:
205: if (children == null) {
206: out.writeInt(0);
207: } else if (children instanceof RuntimeMemoryCCTNode) {
208: out.writeInt(1);
209: out.writeInt(((RuntimeMemoryCCTNode) children).getType());
210: ((RuntimeMemoryCCTNode) children).writeToStream(out);
211: } else {
212: RuntimeMemoryCCTNode[] ar = (RuntimeMemoryCCTNode[]) children;
213: out.writeInt(ar.length);
214:
215: for (int i = 0; i < ar.length; i++) {
216: out.writeInt(ar[i].getType());
217: ar[i].writeToStream(out);
218: }
219: }
220: }
221:
222: private RuntimeMemoryCCTNode[] addChildEntry() {
223: assert (children != null);
224:
225: if (children instanceof RuntimeMemoryCCTNode) {
226: // currently just single child
227: RuntimeMemoryCCTNode[] ret = new RuntimeMemoryCCTNode[2];
228: ret[0] = (RuntimeMemoryCCTNode) children;
229: children = ret;
230:
231: return ret;
232: } else {
233: RuntimeMemoryCCTNode[] ar = (RuntimeMemoryCCTNode[]) children;
234: RuntimeMemoryCCTNode[] newchildren = new RuntimeMemoryCCTNode[ar.length + 1];
235: System.arraycopy(ar, 0, newchildren, 0, ar.length);
236: children = newchildren;
237:
238: return newchildren;
239: }
240: }
241: }
|