001: /*
002: Copyright (c) 2007, Andrew Wilcox
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: + Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010: + Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: + Neither the name Andrew Wilcox nor the names of its contributors may be
014: used to endorse or promote products derived from this software without
015: specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
021: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
022: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
023: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
024: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
025: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
026: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
027: POSSIBILITY OF SUCH DAMAGE.
028: */
029: package net.sourceforge.jiprof.timeline;
030:
031: import java.io.IOException;
032:
033: import com.mentorgen.tools.profile.Controller;
034:
035: public class TimeLineProfiler implements Runnable {
036: public static TimeLine _timeLine;
037: static long _startTime;
038:
039: public static void initProfiler() {
040: System.err.println("TimeLineProfiler: starting");
041: Runtime.getRuntime().addShutdownHook(
042: new Thread(new TimeLineProfiler()));
043: _timeLine = new TimeLine();
044:
045: if (Controller._timeResoltion == Controller.TimeResolution.ns) {
046: _startTime = System.nanoTime();
047: } else {
048: _startTime = System.currentTimeMillis();
049: }
050: }
051:
052: public static void start(String className, String methodName) {
053: createActionRecord(className, methodName, Action.START);
054: }
055:
056: public static void end(String className, String methodName) {
057: createActionRecord(className, methodName, Action.STOP);
058: }
059:
060: private static void createActionRecord(String className,
061: String methodName, Action type) {
062: long threadId = Thread.currentThread().getId();
063: ActionRecord rec = new ActionRecord(className, methodName,
064: type, threadId);
065: long time;
066:
067: if (Controller._timeResoltion == Controller.TimeResolution.ns) {
068: time = System.nanoTime();
069: } else {
070: time = System.currentTimeMillis();
071: }
072:
073: synchronized (_timeLine) {
074: TimeRecord tr = null;
075:
076: if (_timeLine.size() > 0) {
077: tr = _timeLine.getLast();
078: }
079:
080: if (tr == null || tr._pointInTime != time) {
081: tr = new TimeRecord(time);
082: _timeLine.add(tr);
083: }
084:
085: tr._actionRecordList.add(rec);
086: }
087: }
088:
089: public static void alloc(String className) {
090: createActionRecord(className, "", Action.ALLOC);
091: }
092:
093: public static void beginWait(String className, String methodName) {
094: createActionRecord(className, methodName, Action.BEGIN_WAIT);
095: }
096:
097: public static void endWait(String className, String methodName) {
098: createActionRecord(className, methodName, Action.END_WAIT);
099: }
100:
101: public static void unwind(String className, String methodName,
102: String exception) {
103: createActionRecord(className, methodName, Action.EXCEPTION);
104: }
105:
106: /**
107: * ShutdownHook: This will dump the profiling info when the VM shutsdown.
108: */
109: public void run() {
110:
111: try {
112: TextOutput.dump();
113: } catch (IOException e) {
114: e.printStackTrace();
115: }
116: }
117:
118: }
|