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.cpu;
042:
043: import org.netbeans.lib.profiler.results.CCTNode;
044:
045: /**
046: * Presentation-Time CPU Profiling Calling Context Tree (CCT) Node backed by the flattened tree data array in
047: * CPUCCTContainer. These nodes are constructed on demand, i.e. only when the user opens some node in the CCT on screen.
048: * They contain minimum amount of data in the node instance itself. As a result, a tree constructed of such nodes has
049: * a very small overhead on top of the flattened data that already exists (and has relatively low space consumption and
050: * construction time). The drawback is that it's difficult to add elements to a tree represented in the flattened form.
051: *
052: * @author Misha Dmitriev
053: */
054: public class PrestimeCPUCCTNodeBacked extends PrestimeCPUCCTNode {
055: //~ Instance fields ----------------------------------------------------------------------------------------------------------
056:
057: protected int compactDataOfs;
058: protected int nChildren;
059:
060: //~ Constructors -------------------------------------------------------------------------------------------------------------
061:
062: /**
063: * Constructor for creating normal nodes representing methods
064: */
065: public PrestimeCPUCCTNodeBacked(CPUCCTContainer container,
066: PrestimeCPUCCTNode parent, int compactDataOfs) {
067: super (container, parent);
068: this .compactDataOfs = compactDataOfs;
069: this .container = container;
070: nChildren = container.getNChildrenForNodeOfs(compactDataOfs);
071: }
072:
073: /**
074: * Constructor for creating a node that represent a whole thread
075: */
076: protected PrestimeCPUCCTNodeBacked(CPUCCTContainer container,
077: PrestimeCPUCCTNode[] children) {
078: super (container, null);
079: setThreadNode();
080: this .children = children;
081: nChildren = children.length;
082:
083: for (int i = 0; i < nChildren; i++) {
084: children[i].parent = this ;
085: }
086: }
087:
088: protected PrestimeCPUCCTNodeBacked() {
089: }
090:
091: //~ Methods ------------------------------------------------------------------------------------------------------------------
092:
093: public CCTNode getChild(int index) {
094: getChildren();
095:
096: if (index < children.length) {
097: return children[index];
098: } else {
099: return null;
100: }
101: }
102:
103: public CCTNode[] getChildren() {
104: if (nChildren == 0) {
105: return null;
106: } else if (children != null) {
107: return children;
108: }
109:
110: // Otherwise need to construct children first
111: int addChild = isThreadNode() ? 0 : 1; // There will be an additional "self time" node
112: children = new PrestimeCPUCCTNode[nChildren + addChild];
113:
114: for (int i = 0; i < nChildren; i++) {
115: int dataOfs = container.getChildOfsForNodeOfs(
116: compactDataOfs, i);
117: children[i + addChild] = new PrestimeCPUCCTNodeBacked(
118: container, this , dataOfs);
119: }
120:
121: if (addChild > 0) {
122: children[0] = createSelfTimeNodeForThisNode();
123: nChildren++;
124: }
125:
126: // Now that children are created, sort them in the order previously used
127: sortChildren(container.getCPUResSnapshot().getSortBy(),
128: container.getCPUResSnapshot().getSortOrder());
129:
130: return children;
131: }
132:
133: public int getMethodId() {
134: return container.getMethodIdForNodeOfs(compactDataOfs);
135: }
136:
137: public int getNCalls() {
138: return container.getNCallsForNodeOfs(compactDataOfs);
139: }
140:
141: public int getNChildren() {
142: if (children == null) { // Actual array not yet initialized
143:
144: int addChild = (nChildren > 0) ? (isThreadNode() ? 0 : 1)
145: : 0; // There will be an additional "self time" node
146:
147: return nChildren + addChild;
148: } else {
149: return nChildren;
150: }
151: }
152:
153: public long getSleepTime0() {
154: return container.getSleepTime0ForNodeOfs(compactDataOfs);
155:
156: // TODO: [wait] self time node?
157: }
158:
159: public int getThreadId() {
160: return container.getThreadId();
161: }
162:
163: public long getTotalTime0() {
164: if (!isSelfTimeNode()) {
165: return container.getTotalTime0ForNodeOfs(compactDataOfs);
166: } else {
167: return container.getSelfTime0ForNodeOfs(compactDataOfs);
168: }
169: }
170:
171: public float getTotalTime0InPerCent() {
172: float result = (float) ((container.getWholeGraphNetTime0() > 0) ? ((double) getTotalTime0()
173: / (double) container.getWholeGraphNetTime0() * 100.0)
174: : 0);
175:
176: return (result < 100) ? result : 100;
177: }
178:
179: public long getTotalTime1() {
180: if (!isSelfTimeNode()) {
181: return container.getTotalTime1ForNodeOfs(compactDataOfs);
182: } else {
183: return container.getSelfTime1ForNodeOfs(compactDataOfs);
184: }
185: }
186:
187: public float getTotalTime1InPerCent() {
188: return (float) ((container.getWholeGraphNetTime1() > 0) ? ((double) getTotalTime1()
189: / (double) container.getWholeGraphNetTime1() * 100.0)
190: : 0);
191: }
192:
193: public long getWaitTime0() {
194: return container.getWaitTime0ForNodeOfs(compactDataOfs);
195:
196: // TODO: [wait] self time node?
197: }
198:
199: public void sortChildren(int sortBy, boolean sortOrder) {
200: container.getCPUResSnapshot().saveSortParams(sortBy, sortOrder);
201:
202: // We don't eagerly initialize children for sorting
203: if ((nChildren == 0) || (children == null)) {
204: return;
205: }
206:
207: doSortChildren(sortBy, sortOrder);
208: }
209:
210: protected PrestimeCPUCCTNode createSelfTimeNodeForThisNode() {
211: PrestimeCPUCCTNodeBacked selfTimeChild;
212:
213: selfTimeChild = new PrestimeCPUCCTNodeBacked();
214: selfTimeChild.setSelfTimeNode();
215:
216: selfTimeChild.compactDataOfs = compactDataOfs;
217: selfTimeChild.container = container;
218: selfTimeChild.parent = this;
219:
220: return selfTimeChild;
221: }
222: }
|