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.cct;
042:
043: import org.netbeans.lib.profiler.results.cpu.TimingAdjusterOld;
044: import org.netbeans.lib.profiler.results.cpu.cct.nodes.MethodCPUCCTNode;
045: import org.netbeans.lib.profiler.results.cpu.marking.Mark;
046: import org.netbeans.lib.profiler.results.cpu.marking.MarkBasedNodeVisitor;
047: import java.util.HashMap;
048: import java.util.Map;
049:
050: /**
051: *
052: * @author Jaroslav Bachorik
053: */
054: public class TimeCollector extends MarkBasedNodeVisitor {
055: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
056:
057: private static class TimingData {
058: //~ Instance fields ------------------------------------------------------------------------------------------------------
059:
060: int inCalls;
061: int outCalls;
062: long netTime0;
063: long netTime1;
064: }
065:
066: //~ Instance fields ----------------------------------------------------------------------------------------------------------
067:
068: private Map timing;
069:
070: //~ Constructors -------------------------------------------------------------------------------------------------------------
071:
072: /**
073: * Creates a new instance of MarkTimer
074: */
075: public TimeCollector() {
076: this .timing = new HashMap();
077: }
078:
079: //~ Methods ------------------------------------------------------------------------------------------------------------------
080:
081: public synchronized long getNetTime0(Mark mark) {
082: if (isReset()) {
083: return 0;
084: }
085:
086: TimingData currentTiming = (TimingData) timing.get(mark);
087: long time = (currentTiming != null) ? (long) TimingAdjusterOld
088: .getDefault().adjustTime(currentTiming.netTime0,
089: currentTiming.inCalls, currentTiming.outCalls,
090: false) : 0;
091:
092: return (time > 0) ? time : 0;
093: }
094:
095: public synchronized long getNetTime1(Mark mark) {
096: if (isReset()) {
097: return 0;
098: }
099:
100: TimingData currentTiming = (TimingData) timing.get(mark);
101: long time = (currentTiming != null) ? (long) TimingAdjusterOld
102: .getDefault().adjustTime(currentTiming.netTime1,
103: currentTiming.inCalls, currentTiming.outCalls,
104: true) : 0;
105:
106: return (time > 0) ? time : 0;
107: }
108:
109: public void beforeWalk() {
110: super .beforeWalk();
111: timing.clear();
112: }
113:
114: public void endTrans() {
115: if (isReset()) {
116: this .timing = new HashMap();
117: }
118:
119: super .endTrans();
120: }
121:
122: public void visit(final MethodCPUCCTNode node) {
123: if (isReset()) {
124: return;
125: }
126:
127: Mark mark = getCurrentMark();
128: Mark parentMark = getParentMark();
129:
130: if (mark != null) {
131: TimingData data = (TimingData) timing.get(mark);
132:
133: if (data == null) {
134: data = new TimingData();
135: timing.put(mark, data);
136: }
137:
138: data.inCalls += node.getNCalls();
139: data.netTime0 += node.getNetTime0();
140: data.netTime1 += node.getNetTime1();
141: }
142:
143: if (parentMark != null) {
144: TimingData parentData = (TimingData) timing.get(parentMark);
145:
146: if (parentData == null) {
147: parentData = new TimingData();
148: timing.put(parentMark, parentData);
149: }
150:
151: parentData.outCalls += node.getNCalls();
152: }
153: }
154: }
|