001: /*
002: * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.tools.profiler;
030:
031: import com.caucho.util.Alarm;
032:
033: import java.util.ArrayList;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036:
037: final class ThreadProfiler implements Profiler {
038: private static final Logger log = Logger
039: .getLogger(ThreadProfiler.class.getName());
040:
041: private static ThreadLocal<ThreadProfiler> _current = new ThreadLocal<ThreadProfiler>();
042:
043: private final ArrayList<ProfilerPoint> _nodeStack = new ArrayList<ProfilerPoint>();
044: private long[] _cumulativeTimeStack = new long[16];
045: private boolean[] _unwindStack = new boolean[16];
046: private long[] _startTimeStack = new long[16];
047:
048: static ThreadProfiler current() {
049: ThreadProfiler current = _current.get();
050:
051: if (current == null) {
052: current = new ThreadProfiler();
053: _current.set(current);
054: }
055:
056: return current;
057: }
058:
059: private long currentTimeNanoseconds() {
060: return Alarm.getExactTimeNanoseconds();
061: }
062:
063: void start(ProfilerPoint profilerPoint) {
064: start(profilerPoint, false);
065: }
066:
067: void start(ProfilerPoint guaranteedParent,
068: ProfilerPoint profilerPoint) {
069: int stackLen = _nodeStack.size();
070:
071: boolean isParentFound = false;
072:
073: for (int i = 0; i < stackLen; i++) {
074: if (_nodeStack.get(i) == guaranteedParent) {
075: isParentFound = true;
076: break;
077: }
078: }
079:
080: if (!isParentFound)
081: start(guaranteedParent, true);
082:
083: start(profilerPoint, false);
084: }
085:
086: private void start(ProfilerPoint node, boolean isUnwind) {
087: int stackLen = _nodeStack.size();
088: int topOfStack = stackLen - 1;
089:
090: ProfilerPoint parentNode;
091:
092: /** XXX:>>
093: for (int i = 0; i < stackLen; i++)
094: System.out.print(" ");
095: System.out.println(">>start " + profilerPoint);
096: (new Exception()).printStackTrace(System.out);
097: */
098:
099: if (stackLen == 0) {
100: parentNode = null;
101: } else {
102: // if there is parent, update it's cumulativeTimeStack so that
103: // when the stack unwinds past it in finish() the time is added to the total
104: parentNode = _nodeStack.get(topOfStack);
105:
106: long parentStartTime = _startTimeStack[topOfStack];
107:
108: long parentTime = currentTimeNanoseconds()
109: - parentStartTime;
110:
111: _cumulativeTimeStack[topOfStack] += parentTime;
112: }
113:
114: // ensure capacity
115:
116: int stackCapacity = _startTimeStack.length;
117: int newStackCapacity = stackLen + 2;
118:
119: if (newStackCapacity > stackCapacity) {
120: long[] newStartTimeStack = new long[stackCapacity * 3 / 2 + 1];
121: System.arraycopy(_startTimeStack, 0, newStartTimeStack, 0,
122: stackCapacity);
123: _startTimeStack = newStartTimeStack;
124:
125: long[] newCumulativeTimeStack = new long[stackCapacity * 3 / 2 + 1];
126: System.arraycopy(_cumulativeTimeStack, 0,
127: newCumulativeTimeStack, 0, stackCapacity);
128: _cumulativeTimeStack = newCumulativeTimeStack;
129:
130: boolean[] newUnwindStack = new boolean[stackCapacity * 3 / 2 + 1];
131: System.arraycopy(_unwindStack, 0, newUnwindStack, 0,
132: stackCapacity);
133: _unwindStack = newUnwindStack;
134: }
135:
136: // push a new node onto the stack
137:
138: long currentTime = currentTimeNanoseconds();
139:
140: _nodeStack.add(node);
141:
142: _unwindStack[stackLen] = isUnwind;
143: _startTimeStack[stackLen] = currentTime;
144: _cumulativeTimeStack[stackLen] = 0;
145:
146: /** XXX:>>
147: for (int i = 0; i < stackLen; i++)
148: System.out.print(" ");
149: System.out.println(">>startNode " + node + " currentTime " + currentTime);
150: */
151:
152: if (log.isLoggable(Level.FINEST)) {
153: log.finest("[" + stackLen + "] start " + node
154: + " isUnwind=" + isUnwind);
155: log.log(Level.FINEST, "", new Exception());
156: }
157: }
158:
159: public void finish() {
160: int removeIndex = _nodeStack.size() - 1;
161:
162: ProfilerPoint node = _nodeStack.remove(removeIndex);
163: long startTime = _startTimeStack[removeIndex];
164:
165: long currentTime = currentTimeNanoseconds();
166:
167: long time = currentTime - startTime;
168:
169: long totalTime = _cumulativeTimeStack[removeIndex] + time;
170:
171: node.update(totalTime);
172:
173: /** XXX:>>
174: for (int i = 0; i < removeIndex; i++)
175: System.out.print(" ");
176: System.out.println(">>finishNode " + node +
177: " startTime " + startTime +
178: " currentTime " + currentTime +
179: " totalTime" + time +
180: " time" + totalTime);
181: */
182:
183: int parentIndex = removeIndex - 1;
184:
185: if (parentIndex >= 0) {
186: _startTimeStack[parentIndex] = currentTimeNanoseconds();
187:
188: boolean isUnwind = _unwindStack[parentIndex];
189:
190: if (log.isLoggable(Level.FINEST)) {
191: log.finest("[" + removeIndex + "] finish " + node
192: + " isUnwind=" + isUnwind);
193: }
194:
195: if (isUnwind)
196: finish();
197: } else {
198: if (log.isLoggable(Level.FINEST)) {
199: log.finest("[" + removeIndex + "] finish " + node);
200: }
201: }
202:
203: /** XXX:>>
204: for (int i = 0; i < removeIndex; i++)
205: System.out.print(" ");
206: System.out.println(">>finish " + node + " " + totalTime);
207: */
208: }
209:
210: public String toString() {
211: return "Profiler[" + Thread.currentThread().getName() + "]";
212: }
213: }
|