KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > tools > profiler > ProfilerManager


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.tools.profiler;
31
32 import com.caucho.loader.EnvironmentLocal;
33 import com.caucho.util.LruCache;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Comparator JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.LinkedList JavaDoc;
39 import java.util.Set JavaDoc;
40 import java.util.TreeSet JavaDoc;
41
42 /**
43  * The main entry point for profiling. This class is used to obtain instances
44  * of {@link ProfilerPoint}, which are then used during execution of code to
45  * demarcate the code to be profiled.
46  * <p/>
47  * A {@link ProfilerManager} for the current {@link ClassLoader} is obtained
48  * with {@link #getLocal()}.
49  */

50 public class ProfilerManager {
51   private static final EnvironmentLocal<ProfilerManager> _local = new EnvironmentLocal<ProfilerManager>();
52
53   private final LruCache<String JavaDoc, ProfilerPoint> _profilerPointMap
54     = new LruCache<String JavaDoc, ProfilerPoint>(1024);
55
56   private boolean _isEnabled = false;
57
58   public static ProfilerManager getLocal()
59   {
60     synchronized (_local) {
61       ProfilerManager local = _local.get();
62
63       if (local == null) {
64         local = new ProfilerManager();
65         _local.set(local);
66       }
67
68       return local;
69     }
70   }
71
72   private ProfilerManager()
73   {
74     new ProfilerAdmin(this);
75   }
76
77   /**
78    * Set to true to enable profiling, default false.
79    */

80   public void setEnabled(boolean isEnabled)
81   {
82     _isEnabled = isEnabled;
83   }
84
85   public boolean isEnabled()
86   {
87     return _isEnabled;
88   }
89
90   public void enable()
91   {
92     if (!_isEnabled) {
93       reset();
94       _isEnabled = true;
95     }
96   }
97
98   public void disable()
99   {
100     _isEnabled = false;
101   }
102
103   public ProfilerPoint getProfilerPoint(String JavaDoc name)
104   {
105     synchronized (_profilerPointMap) {
106       ProfilerPoint profilerPoint = _profilerPointMap.get(name);
107
108       if (profilerPoint == null) {
109         profilerPoint = new ProfilerPoint(this, name);
110         _profilerPointMap.put(name, profilerPoint);
111       }
112
113       return profilerPoint;
114     }
115   }
116
117   public CategorizingProfilerPoint getCategorizingProfilerPoint(String JavaDoc name)
118   {
119     synchronized (_profilerPointMap) {
120       ProfilerPoint profilerPoint = _profilerPointMap.get(name);
121
122       if (profilerPoint == null) {
123         profilerPoint = new CategorizingProfilerPoint(this, name);
124         _profilerPointMap.put(name, profilerPoint);
125       }
126
127       return (CategorizingProfilerPoint) profilerPoint;
128     }
129   }
130
131   public Set JavaDoc<ProfilerPoint> getAllProfilerPoints()
132   {
133     synchronized (_profilerPointMap) {
134       TreeSet JavaDoc<ProfilerPoint> allProfilerPoints = new TreeSet JavaDoc<ProfilerPoint>();
135
136       Iterator JavaDoc<ProfilerPoint> existingValues = _profilerPointMap.values();
137
138       while (existingValues.hasNext())
139         allProfilerPoints.add(existingValues.next());
140
141       return allProfilerPoints;
142     }
143   }
144
145   /**
146    * Returns a copy of all of the ProfilerNodes.
147    */

148   public Collection JavaDoc<ProfilerNode> getAllProfilerNodes()
149   {
150     LinkedList JavaDoc<ProfilerNode> profilerNodes = new LinkedList JavaDoc<ProfilerNode>();
151
152     fillAllProfilerNodes(profilerNodes);
153
154     return profilerNodes;
155   }
156
157   /**
158    * Returns a copy of all of the ProfilerNodes, sorted with the given {@link
159    * Comparator}.
160    */

161   public Collection JavaDoc<ProfilerNode> getAllProfilerNodes(Comparator JavaDoc<ProfilerNode> comparator)
162   {
163     TreeSet JavaDoc<ProfilerNode> profilerNodes = new TreeSet JavaDoc<ProfilerNode>(comparator);
164
165     fillAllProfilerNodes(profilerNodes);
166
167     return profilerNodes;
168   }
169
170   /**
171    * Returns a copy of all of the ProfilerNodes that are children of the
172    * parent.
173    *
174    * @param parent the parent, null to retrieve top-lvel nodes
175    */

176   public Collection JavaDoc<ProfilerNode> getChildProfilerNodes(ProfilerNode parent)
177   {
178     LinkedList JavaDoc<ProfilerNode> profilerNodes = new LinkedList JavaDoc<ProfilerNode>();
179
180     fillChildProfilerNodes(parent, profilerNodes);
181
182     return profilerNodes;
183   }
184
185   /**
186    * Returns a copy of all of the ProfilerNodes that are children of the parent,
187    * sorted with the given {@link Comparator}..
188    *
189    * @param parent the parent, null to retrieve top-lvel nodes
190    */

191   public Collection JavaDoc<ProfilerNode> getChildProfilerNodes(ProfilerNode parent,
192                                                         Comparator JavaDoc<ProfilerNode> comparator)
193   {
194     TreeSet JavaDoc<ProfilerNode> profilerNodes = new TreeSet JavaDoc<ProfilerNode>(comparator);
195
196     fillChildProfilerNodes(parent, profilerNodes);
197
198     return profilerNodes;
199   }
200
201   private void fillAllProfilerNodes(Collection JavaDoc<ProfilerNode> profilerNodes)
202   {
203     synchronized (_profilerPointMap) {
204       Iterator JavaDoc<ProfilerPoint> iter = _profilerPointMap.values();
205
206       while (iter.hasNext()) {
207         ProfilerPoint profilerPoint = iter.next();
208
209         synchronized (profilerPoint) {
210           profilerNodes.addAll(profilerPoint.getProfilerNodes());
211         }
212       }
213     }
214   }
215
216   private void fillChildProfilerNodes(ProfilerNode parent,
217                                       Collection JavaDoc<ProfilerNode> profilerNodes)
218   {
219     synchronized (_profilerPointMap) {
220       Iterator JavaDoc<ProfilerPoint> iter = _profilerPointMap.values();
221
222       while (iter.hasNext()) {
223         ProfilerPoint profilerPoint = iter.next();
224
225         synchronized (profilerPoint) {
226           for (ProfilerNode profilerNode : profilerPoint.getProfilerNodes()) {
227             if (profilerNode.getParent() == parent)
228               profilerNodes.add(profilerNode);
229           }
230         }
231       }
232     }
233   }
234
235   /**
236    * Clear all profiling information.
237    */

238   public void reset()
239   {
240     synchronized (_profilerPointMap) {
241       Iterator JavaDoc<ProfilerPoint> iter = _profilerPointMap.values();
242
243       while (iter.hasNext()) {
244         ProfilerPoint profilerPoint = iter.next();
245
246         profilerPoint.reset();
247       }
248     }
249   }
250
251   public String JavaDoc toString()
252   {
253     return "ProfilerManager[" + getClass().getClassLoader() + "]";
254   }
255
256 }
257
Popular Tags