6 // everything defined here should match coreprof.h
8 public static final int CP_EVENT_MASK = 3;
9 public static final int CP_EVENT_BASESHIFT = 8;
11 public static final int CP_EVENTTYPE_BEGIN = 1;
12 public static final int CP_EVENTTYPE_END = 2;
13 public static final int CP_EVENTTYPE_ONEOFF = 3;
15 public static final int CP_EVENTID_MAIN = 0x04;
16 public static final int CP_EVENTID_RUNMALLOC = 0x05;
17 public static final int CP_EVENTID_RUNFREE = 0x06;
18 public static final int CP_EVENTID_TASKDISPATCH = 0x07;
19 public static final int CP_EVENTID_TASKRETIRE = 0x08;
20 public static final int CP_EVENTID_TASKSTALLVAR = 0x09;
21 public static final int CP_EVENTID_TASKSTALLMEM = 0x0a;
25 eid2name = new Hashtable<Integer, String>();
26 eid2name.put( CP_EVENTID_MAIN, "MAIN " );
27 eid2name.put( CP_EVENTID_RUNMALLOC, "RUNMALLOC " );
28 eid2name.put( CP_EVENTID_RUNFREE, "RUNFREE " );
29 eid2name.put( CP_EVENTID_TASKDISPATCH, "TASKDISPATCH" );
30 eid2name.put( CP_EVENTID_TASKRETIRE, "TASKRETIRE " );
31 eid2name.put( CP_EVENTID_TASKSTALLVAR, "TASKSTALLVAR" );
32 eid2name.put( CP_EVENTID_TASKSTALLMEM, "TASKSTALLMEM" );
35 Hashtable<Integer, String> eid2name;
39 public static void main( String args[] ) {
40 if( args.length != 2 ) {
41 System.out.println( "usage: <coreprof.dat file> <trace out file>" );
44 Trace t = new Trace( args[0], args[1] );
49 // event IDs are a word, timestamps are long ints
50 public static final int WORD_SIZE = 4;
51 public static final int EVENT_SIZE = WORD_SIZE;
52 public static final int TIMESTAMP_SIZE = WORD_SIZE*2;
55 ThreadData[] threadData;
59 public Trace( String inFile, String outFile ) {
61 openInputStreams( inFile );
65 for( int i = 0; i < numThreads; i++ ) {
69 printStats( outFile );
73 public static int readInt( InputStream is ) {
80 int retval = (b4<<24)|(b3<<16)|(b2<<8)|b1;
87 } catch( Exception e ) {
93 public static long readLong( InputStream is ) {
105 (b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
106 (b4<<24)|(b3<<16)|(b2<< 8)|b1;
113 } catch( Exception e ) {
119 protected void openInputStreams( String filename ) {
121 BufferedInputStream bis = null;
125 bis = new BufferedInputStream( new FileInputStream( filename ) );
126 offset = readHeader( bis );
128 } catch( Exception e ) {
133 for( int i = 0; i < numThreads; ++i ) {
135 // point a thread's event stream to the
136 // beginning of its data within the input file
137 threadData[i].dataStream =
138 new BufferedInputStream( new FileInputStream( filename ) );
142 skip -= threadData[i].dataStream.skip( skip );
145 offset += WORD_SIZE*threadData[i].numDataWords;
147 } catch( Exception e ) {
155 int readHeader( BufferedInputStream bis ) {
158 int version = readInt( bis );
160 throw new Error( "Unsupported Version" );
162 int offset = WORD_SIZE;
164 // read number of threads
165 numThreads = readInt( bis );
168 threadData = new ThreadData[numThreads];
170 // read number of words used for all events, per thread
171 for( int i = 0; i < numThreads; ++i ) {
172 threadData[i] = new ThreadData();
173 threadData[i].numDataWords = readInt( bis );
180 public void readThread( int tNum ) {
182 System.out.print( "Reading thread "+tNum );
184 ThreadData tdata = threadData[tNum];
185 tdata.stackDepth = 0;
188 int numProgress = 10;
190 int progressChunk = tdata.numDataWords / numProgress;
192 boolean[] progress = new boolean[numProgress];
193 for( j = 0; j < numProgress; ++j ) {
198 while( i < tdata.numDataWords ) {
200 if( !progress[j] && i > j*progressChunk ) {
201 System.out.print( "." );
203 if( j < numProgress - 1 ) {
208 int eventRaw = readInt ( tdata.dataStream );
209 timeStamp = readLong( tdata.dataStream );
212 int eventType = eventRaw & CP_EVENT_MASK;
213 int eventID = eventRaw >> CP_EVENT_BASESHIFT;
215 switch( eventType ) {
217 case CP_EVENTTYPE_BEGIN: {
218 pushEvent( tdata, eventID, timeStamp );
221 case CP_EVENTTYPE_END: {
222 popEvent( tdata, eventID, timeStamp );
228 System.out.println( "" );
230 if( tdata.stackDepth != 0 ) {
231 // worker threads currently do not exit gracefully, and therefore
232 // never register their MAIN END event, so if the mismatch is with
233 // MAIN BEGIN then treat it as fine, otherwise warn.
234 if( tdata.stackDepth == 1 ) {
235 // the value of timestamp will be equal to whatever the last
236 // properly registered event for this thread was
237 popEvent( tdata, CP_EVENTID_MAIN, timeStamp );
239 System.out.println( "Warning: unmatched event begin/end\n" );
245 protected void pushEvent( ThreadData tdata,
249 EventSummary eventSummary = null;
251 if( tdata.stackDepth == 0 ) {
252 // there are no parents, so look in the rootEvents
253 // for an existing EventSummary of this type
254 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
257 EventSummary es = itr.next();
258 if( es.eventID == eventID ) {
263 if( eventSummary == null ) {
264 // there is no summary for this event type yet,
266 eventSummary = new EventSummary( eventID );
267 tdata.rootEvents.add( eventSummary );
271 // look through the parent's children for an existing
272 // EventSummary of this type
273 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
274 for( Iterator<EventSummary> itr = esParent.children.iterator();
277 EventSummary es = itr.next();
278 if( es.eventID == eventID ) {
283 if( eventSummary == null ) {
284 // there is no summary for this event type yet,
285 // under this parent, so add it
286 eventSummary = new EventSummary( eventID );
287 esParent.children.add( eventSummary );
288 eventSummary.parent = esParent;
292 eventSummary.timeStampBeginLatestInstance = timeStamp;
294 eventSummary.instanceCount++;
296 if( tdata.eventStack.size() <= tdata.stackDepth ) {
297 tdata.eventStack.setSize( 2*tdata.stackDepth + 20 );
299 tdata.eventStack.set( tdata.stackDepth, eventSummary );
305 protected void popEvent( ThreadData tdata,
309 if( tdata.stackDepth < 0 ) {
310 throw new Error( "Event stack underflow\n" );
313 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
314 assert eventSummary != null;
317 timeStamp - eventSummary.timeStampBeginLatestInstance;
319 eventSummary.totalTime_ticks += elapsedTime;
320 eventSummary.selfTime_ticks += elapsedTime;
322 if( tdata.stackDepth - 1 >= 0 ) {
323 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth-1 );
324 esParent.selfTime_ticks -= elapsedTime;
330 public void printStats( String filename ) {
332 System.out.println( "Printing..." );
336 new BufferedWriter( new FileWriter( filename ) );
338 for( int i = 0; i < numThreads; ++i ) {
340 ThreadData tdata = threadData[i];
342 bw.write( "----------------------------------\n" );
343 bw.write( "Thread "+i+"\n" );
345 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
348 EventSummary es = itr.next();
349 printEventSummary( bw, es, 0 );
357 } catch( IOException e ) {}
361 public void printEventSummary( BufferedWriter bw,
366 String strIndent = "";
367 for( int i = 0; i < depth; ++i ) {
371 String strEventName =
372 eid2name.containsKey( es.eventID ) ?
373 eid2name.get( es.eventID ) :
374 Integer.toString( es.eventID );
376 float tOfParent_perc;
377 String strPercParent = "";
378 if( es.parent != null ) {
381 new Long( es.totalTime_ticks ).floatValue() /
382 new Long( es.parent.totalTime_ticks ).floatValue();
384 strPercParent = String.format( " %%ofParent=%5.1f",
390 new Long( es.selfTime_ticks ).floatValue() /
391 new Long( es.totalTime_ticks ).floatValue();
393 String strSelfStats =
394 String.format( " total(ticks)=%12dK, %%self=%5.1f, count=%d",
395 es.totalTime_ticks/1000,
405 for( Iterator<EventSummary> itr = es.children.iterator();
408 EventSummary esChild = itr.next();
409 printEventSummary( bw, esChild, depth + 1 );