1 import java.io.FileInputStream;
2 import java.io.InputStream;
3 import java.io.BufferedInputStream;
4 import java.util.Vector;
5 import java.util.Hashtable;
6 import java.util.Iterator;
13 Hashtable<Integer, Counter> getcounter[];
15 public static final int STACKMAX=512;
17 public void printStats() {
18 for(int i=0;i<numThreads;i++) {
19 System.out.println("Thread "+i);
20 for(Iterator<Integer> evit=getcounter[i].keySet().iterator();evit.hasNext();) {
21 Integer event=evit.next();
22 Counter c=getcounter[i].get(event);
23 System.out.println("Event: "+event+" self time="+c.selftime+" total time"+c.totaltime);
25 System.out.println("-------------------------------------------------------------");
29 public static int readInt(InputStream is) {
35 int retval=(b4<<24)|(b3<<16)|(b2<<8)|b1;
39 } catch (Exception e) {
44 public static long readLong(InputStream is) {
54 long retval=(b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
55 (b4<<24)|(b3<<16)|(b2<<8)|b1;
59 } catch (Exception e) {
64 int readHeader(BufferedInputStream bir) {
67 int version=readInt(bir);
69 throw new Error("Unsupported Version");
71 //Second read number of threads
73 numThreads=readInt(bir);
75 //Third read event counts for each Thread
76 eventCounts=new int[numThreads];
77 eventstack=new Event[numThreads][STACKMAX];
78 for(int i=0;i<numThreads;i++) {
79 eventCounts[i]=readInt(bir);
85 BufferedInputStream threads[];
87 public Trace(String filename) {
88 BufferedInputStream bir=null;
91 bir=new BufferedInputStream(new FileInputStream(filename));
92 offset=readHeader(bir);
94 } catch (Exception e) {
98 threads=new BufferedInputStream[numThreads];
99 getcounter=new Hashtable[numThreads];
101 for(int i=0;i<numThreads;i++) {
102 getcounter[i]=new Hashtable<Integer,Counter>();
104 threads[i]=new BufferedInputStream(new FileInputStream(filename));
107 skip-=threads[i].skip(skip);
109 offset+=4*eventCounts[i];
110 } catch (Exception e) {
117 public void readThread(int t) {
118 BufferedInputStream bis=threads[t];
119 int numevents=eventCounts[t];
121 Event[] stack=eventstack[t];
123 Hashtable<Integer, Counter> countertab=getcounter[t];
126 int event=readInt(bis);
127 long time=readLong(bis);
128 int baseevent=event>>CP_BASE_SHIFT;
129 i+=3; //time and event
131 switch(event&CP_MASK) {
133 enqueue(stack, depth, baseevent, time, countertab);
137 Event e=stack[depth];
138 long elapsedtime=time-e.time;
140 c.totaltime+=elapsedtime;
141 c.selftime+=elapsedtime;
143 for(int j=depth;j>=0;j--) {
144 Counter cn=e.counter;
145 cn.totaltime-=elapsedtime;
155 public static void enqueue(Event[] stack, int depth, int event, long time, Hashtable<Integer, Counter> getcounter) {
156 Counter counter=getcounter.get(event);
158 Counter c=new Counter();
159 getcounter.put(event, c);
161 if (stack[depth]==null) {
162 stack[depth]=new Event(time,event);
163 stack[depth].counter=counter;
165 stack[depth].time=time;
166 stack[depth].event=event;
167 stack[depth].counter=counter;
171 public static final int CP_BEGIN=0;
172 public static final int CP_END=1;
173 public static final int CP_EVENT=2;
174 public static final int CP_MASK=3;
175 public static final int CP_BASE_SHIFT=2;
177 public static final int CP_MAIN=0;
178 public static final int CP_RUNMALLOC=1;
179 public static final int CP_RUNFREE=2;
180 public static void main(String x[]) {
181 Trace t=new Trace(x[0]);