KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
33  * Gathers statistics for a {@link ProfilerPoint} with a particular parentage.
34  */

35 final public class ProfilerNode {
36   private final ProfilerNode _parent;
37   private final ProfilerPoint _profilerPoint;
38   private long _longHashCode;
39
40   private long _time;
41   private long _invocationCount;
42
43   private long _minTime = Long.MAX_VALUE;
44   private long _maxTime = Long.MIN_VALUE;
45
46   ProfilerNode(ProfilerNode parent, ProfilerPoint profilerPoint)
47   {
48     _parent = parent;
49     _profilerPoint = profilerPoint;
50   }
51
52   public ProfilerNode getParent()
53   {
54     return _parent;
55   }
56
57   public ProfilerPoint getProfilerPoint()
58   {
59     return _profilerPoint;
60   }
61
62   public String JavaDoc getName()
63   {
64     return _profilerPoint.getName();
65   }
66
67   /**
68    * Increment the invocation count and add time.
69    *
70    * @param totalTime
71    */

72   void update(long totalTime)
73   {
74     synchronized (this) {
75       _invocationCount++;
76
77       if (_invocationCount > 0) {
78         _time += totalTime;
79       }
80
81       if (totalTime < _minTime)
82     _minTime = totalTime;
83
84       if (_maxTime < totalTime)
85     _maxTime = totalTime;
86     }
87   }
88
89   /**
90    * Time for this node in nanoseconds, does not include the time for child
91    * nodes.
92    */

93   public long getTime()
94   {
95     return _time;
96   }
97
98   /**
99    * Minimum time for this node in nanoseconds, does not include
100    * the time for child nodes.
101    */

102   public long getMinTime()
103   {
104     return _minTime;
105   }
106
107   /**
108    * Minimum time for this node in nanoseconds, does not include
109    * the time for child nodes.
110    */

111   public long getMaxTime()
112   {
113     return _maxTime;
114   }
115
116   void incrementInvocationCount()
117   {
118     synchronized (this) {
119       _invocationCount++;
120     }
121   }
122
123   public long getInvocationCount()
124   {
125     return _invocationCount;
126   }
127
128   public long longHashCode()
129   {
130     if (_longHashCode == 0) {
131       long longHashCode = _parent == null ? 7 : _parent.longHashCode();
132
133       longHashCode = longHashCode * 33 ^ _profilerPoint.longHashCode();
134
135       _longHashCode = longHashCode;
136     }
137
138     return _longHashCode;
139   }
140
141   public int hashCode()
142   {
143     return (int) longHashCode();
144   }
145
146   public boolean equals(Object JavaDoc o)
147   {
148     if (o == this)
149       return true;
150
151     // if (!(o instanceof ProfilerNode)) return false;
152

153     ProfilerNode other = (ProfilerNode) o;
154
155     if (other._parent != _parent)
156       return false;
157
158     if (other._profilerPoint != _profilerPoint)
159       return false;
160
161     return true;
162   }
163
164   public String JavaDoc toString()
165   {
166     StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
167
168     ProfilerNode node = this;
169
170     do {
171       if (builder.length() != 0)
172         builder.insert(0, " --> ");
173
174       builder.insert(0, node._profilerPoint.getName());
175
176       node = node.getParent();
177
178     } while (node != null);
179
180     builder.insert(0, "ProfilerNode[");
181     builder.append(']');
182
183     return builder.toString();
184   }
185 }
186
Popular Tags