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;
042:
043: import org.netbeans.lib.profiler.ProfilerClient;
044: import org.netbeans.lib.profiler.TargetAppRunner;
045: import org.netbeans.lib.profiler.client.ProfilingPointsProcessor;
046: import org.netbeans.lib.profiler.client.RuntimeProfilingPoint;
047: import org.netbeans.lib.profiler.global.ProfilingSessionStatus;
048: import java.lang.ref.WeakReference;
049: import java.util.ArrayList;
050: import java.util.HashSet;
051: import java.util.Iterator;
052: import java.util.List;
053: import java.util.Set;
054: import java.util.logging.Level;
055: import java.util.logging.Logger;
056:
057: /**
058: *
059: * @author Jaroslav Bachorik
060: */
061: public abstract class BaseCallGraphBuilder implements
062: ProfilingResultListener, CCTProvider {
063: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
064:
065: protected static final Logger LOGGER = Logger
066: .getLogger(BaseCallGraphBuilder.class.getName());
067:
068: //~ Instance fields ----------------------------------------------------------------------------------------------------------
069:
070: protected List /*<Runnable>*/afterBatchCommands = new ArrayList /*<Runnable>*/();
071: protected ProfilingSessionStatus status;
072: protected Set cctListeners = new HashSet();
073: protected WeakReference clientRef;
074: protected boolean batchNotEmpty = false;
075:
076: //~ Constructors -------------------------------------------------------------------------------------------------------------
077:
078: /** Creates a new instance of BaseCallGraphBuilder */
079: public BaseCallGraphBuilder() {
080: }
081:
082: //~ Methods ------------------------------------------------------------------------------------------------------------------
083:
084: public void addListener(CCTProvider.Listener listener) {
085: cctListeners.add(listener);
086: }
087:
088: public void onBatchStart() {
089: if (LOGGER.isLoggable(Level.FINEST)) {
090: LOGGER.finest("Starting batch"); // NOI18N
091: }
092:
093: afterBatchCommands.clear();
094: batchNotEmpty = false;
095: doBatchStart();
096: }
097:
098: public void onBatchStop() {
099: doBatchStop();
100:
101: if (!afterBatchCommands.isEmpty()) {
102: for (Iterator iter = afterBatchCommands.iterator(); iter
103: .hasNext();) {
104: ((Runnable) iter.next()).run();
105: }
106:
107: afterBatchCommands.clear();
108: }
109:
110: if (batchNotEmpty) {
111: fireCCTEstablished(false);
112: } else {
113: fireCCTEstablished(true);
114: }
115:
116: if (LOGGER.isLoggable(Level.FINEST)) {
117: LOGGER.finest("Finishing batch"); // NOI18N
118: }
119: }
120:
121: public void profilingPoint(final int threadId, final int ppId,
122: final long timeStamp) {
123: ProfilerClient client = getClient();
124:
125: if (client == null) {
126: return;
127: }
128:
129: final ProfilingPointsProcessor ppp = TargetAppRunner
130: .getDefault().getProfilingPointsProcessor();
131:
132: afterBatchCommands.add(new Runnable() {
133: public void run() {
134: ppp
135: .profilingPointHit(new RuntimeProfilingPoint.HitEvent(
136: ppId, timeStamp, threadId));
137: }
138: });
139: }
140:
141: public void removeAllListeners() {
142: cctListeners.clear();
143: }
144:
145: public void removeListener(CCTProvider.Listener listener) {
146: cctListeners.remove(listener);
147: }
148:
149: public void reset() {
150: if (LOGGER.isLoggable(Level.FINEST)) {
151: LOGGER.finest("Resetting CallGraphBuilder"); // NOI18N
152: }
153:
154: try {
155: doReset();
156: fireCCTReset();
157: } catch (Exception e) {
158: LOGGER.severe(e.getMessage());
159: }
160: }
161:
162: public void shutdown() {
163: status = null;
164: afterBatchCommands.clear();
165: doShutdown();
166: }
167:
168: public void startup(ProfilerClient profilerClient) {
169: status = profilerClient.getStatus();
170: clientRef = new WeakReference(profilerClient);
171: doStartup(profilerClient);
172: }
173:
174: protected abstract RuntimeCCTNode getAppRootNode();
175:
176: protected abstract void doBatchStart();
177:
178: protected abstract void doBatchStop();
179:
180: protected abstract void doReset();
181:
182: protected abstract void doShutdown();
183:
184: protected abstract void doStartup(ProfilerClient profilerClient);
185:
186: protected ProfilerClient getClient() {
187: if (clientRef == null) {
188: return null;
189: }
190:
191: return (ProfilerClient) clientRef.get();
192: }
193:
194: private void fireCCTEstablished(boolean empty) {
195: RuntimeCCTNode appNode = getAppRootNode();
196:
197: if (appNode == null) {
198: return;
199: }
200:
201: Set tmpListeners = null;
202:
203: synchronized (cctListeners) {
204: if (cctListeners.isEmpty()) {
205: return;
206: }
207:
208: tmpListeners = new HashSet(cctListeners);
209: }
210:
211: for (Iterator iter = tmpListeners.iterator(); iter.hasNext();) {
212: ((CCTProvider.Listener) iter.next()).cctEstablished(
213: appNode, empty);
214: }
215: }
216:
217: private void fireCCTReset() {
218: Set tmpListeners = null;
219:
220: synchronized (cctListeners) {
221: if (cctListeners.isEmpty()) {
222: return;
223: }
224:
225: tmpListeners = new HashSet(cctListeners);
226: }
227:
228: for (Iterator iter = tmpListeners.iterator(); iter.hasNext();) {
229: ((CCTProvider.Listener) iter.next()).cctReset();
230: }
231: }
232: }
|