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.BufferedWriter;
032: import java.io.File;
033: import java.io.FileWriter;
034: import java.io.IOException;
035: import java.io.PrintWriter;
036: import java.text.SimpleDateFormat;
037: import java.util.Date;
038:
039: import com.mentorgen.tools.profile.Controller;
040:
041: public class TextOutput {
042:
043: public static void dump() throws IOException {
044: System.err.println("TimeLine Profiler: generating output");
045:
046: // this section of code is almost identical to ProfileTextDump
047: //
048: String fileName = null;
049: File f = new File(Controller._fileName);
050: Date now = new Date();
051:
052: if (f.isDirectory()) {
053: StringBuffer b = new StringBuffer(f.getAbsolutePath());
054: b.append(File.separator);
055: b.append(new SimpleDateFormat("yyyyMMdd-HHmmss")
056: .format(now));
057: b.append(".txt");
058: fileName = b.toString();
059: } else {
060: if (Controller._fileName.endsWith(".txt")) {
061: fileName = Controller._fileName;
062: } else {
063: StringBuffer b = new StringBuffer(Controller._fileName);
064: b.append(".txt");
065: fileName = b.toString();
066: }
067: }
068:
069: FileWriter out = new FileWriter(fileName);
070:
071: BufferedWriter bufferedWriter = new BufferedWriter(out);
072: PrintWriter writer = new PrintWriter(bufferedWriter);
073:
074: for (TimeRecord rec : TimeLineProfiler._timeLine) {
075: writer.print("Time: ");
076: writer
077: .print(rec._pointInTime
078: - TimeLineProfiler._startTime);
079:
080: if (Controller._timeResoltion == Controller.TimeResolution.ns) {
081: writer.println(" ns.");
082: } else {
083: writer.println(" ms.");
084: }
085:
086: for (ActionRecord act : rec._actionRecordList) {
087: writer.print('\t');
088:
089: if (act.getAction() == Action.START) {
090: writer.print("START");
091: } else if (act.getAction() == Action.STOP) {
092: writer.print("END");
093: } else if (act.getAction() == Action.ALLOC) {
094: writer.print("ALLOC");
095: } else if (act.getAction() == Action.BEGIN_WAIT) {
096: writer.print("W:START");
097: } else if (act.getAction() == Action.END_WAIT) {
098: writer.print("W:END");
099: } else if (act.getAction() == Action.EXCEPTION) {
100: writer.print("THROWS");
101: } else {
102: writer.print("???");
103: }
104:
105: writer.print('\t');
106:
107: writer.print('[');
108: writer.print(act.getThreadId());
109: writer.print("]\t");
110:
111: // copied verbatium from Method
112: //
113: String className = act.getClassName().replace('/', '.');
114:
115: int index = className.lastIndexOf('.');
116: String shortName = null;
117: String packageName = "";
118:
119: if (index > -1) {
120: shortName = className.substring(index + 1);
121: packageName = className.substring(0, index);
122: } else {
123: shortName = className;
124: }
125:
126: StringBuffer b = new StringBuffer();
127: b.append(shortName);
128:
129: if (act.getMethodName().length() > 0) {
130: b.append(':');
131: b.append(act.getMethodName());
132: }
133:
134: b.append("\t(");
135: b.append(packageName);
136: b.append(")");
137: writer.println(b.toString());
138: }
139:
140: }
141:
142: writer.flush();
143: writer.close();
144: }
145: }
|