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 = 0x10;
17 public static final int CP_EVENTID_RUNFREE = 0x11;
18 public static final int CP_EVENTID_POOLALLOC = 0x14;
19 public static final int CP_EVENTID_COUNT_POOLALLOC = 0x15;
20 public static final int CP_EVENTID_COUNT_POOLREUSE = 0x16;
21 public static final int CP_EVENTID_WORKSCHEDGRAB = 0x20;
22 public static final int CP_EVENTID_TASKDISPATCH = 0x30;
23 public static final int CP_EVENTID_PREPAREMEMQ = 0x31;
24 public static final int CP_EVENTID_TASKEXECUTE = 0x40;
25 public static final int CP_EVENTID_TASKRETIRE = 0x50;
26 public static final int CP_EVENTID_TASKSTALLVAR = 0x60;
27 public static final int CP_EVENTID_TASKSTALLMEM = 0x61;
28 public static final int CP_EVENTID_DEBUG_A = 0x180;
29 public static final int CP_EVENTID_DEBUG_B = 0x181;
30 public static final int CP_EVENTID_DEBUG_C = 0x182;
31 public static final int CP_EVENTID_DEBUG_D = 0x183;
32 public static final int CP_EVENTID_DEBUG_E = 0x184;
33 public static final int CP_EVENTID_DEBUG_F = 0x185;
34 public static final int CP_EVENTID_DEBUG_G = 0x186;
35 public static final int CP_EVENTID_DEBUG_H = 0x187;
36 public static final int CP_EVENTID_DEBUG_I = 0x188;
37 public static final int CP_EVENTID_DEBUG_J = 0x189;
41 eid2name = new Hashtable<Integer, String>();
42 eid2name.put( CP_EVENTID_MAIN, "MAIN " );
43 eid2name.put( CP_EVENTID_RUNMALLOC, "RUNMALLOC " );
44 eid2name.put( CP_EVENTID_RUNFREE, "RUNFREE " );
45 eid2name.put( CP_EVENTID_POOLALLOC, "POOLALLOC " );
46 eid2name.put( CP_EVENTID_COUNT_POOLALLOC, "POOLALLOC CNT" );
47 eid2name.put( CP_EVENTID_COUNT_POOLREUSE, "POOLREUSE CNT" );
48 eid2name.put( CP_EVENTID_WORKSCHEDGRAB, "WORKSCHEDGRAB" );
49 eid2name.put( CP_EVENTID_TASKDISPATCH, "TASKDISPATCH " );
50 eid2name.put( CP_EVENTID_PREPAREMEMQ, "PREPAREMEMQ " );
51 eid2name.put( CP_EVENTID_TASKEXECUTE, "TASKEXECUTE " );
52 eid2name.put( CP_EVENTID_TASKRETIRE, "TASKRETIRE " );
53 eid2name.put( CP_EVENTID_TASKSTALLVAR, "TASKSTALLVAR " );
54 eid2name.put( CP_EVENTID_TASKSTALLMEM, "TASKSTALLMEM " );
55 eid2name.put( CP_EVENTID_DEBUG_A, "DEBUG A " );
56 eid2name.put( CP_EVENTID_DEBUG_B, "DEBUG B " );
57 eid2name.put( CP_EVENTID_DEBUG_C, "DEBUG C " );
58 eid2name.put( CP_EVENTID_DEBUG_D, "DEBUG D " );
59 eid2name.put( CP_EVENTID_DEBUG_E, "DEBUG E " );
60 eid2name.put( CP_EVENTID_DEBUG_F, "DEBUG F " );
61 eid2name.put( CP_EVENTID_DEBUG_G, "DEBUG G " );
62 eid2name.put( CP_EVENTID_DEBUG_H, "DEBUG H " );
63 eid2name.put( CP_EVENTID_DEBUG_I, "DEBUG I " );
64 eid2name.put( CP_EVENTID_DEBUG_J, "DEBUG J " );
67 Hashtable<Integer, String> eid2name;
71 public static void main( String args[] ) {
72 if( args.length < 2 ||
74 System.out.println( "usage: [-2txt] <coreprof.dat file> <trace out file>" );
75 System.out.println( "The -2txt option will take the raw binary events and spit\n"+
76 "out every event as text in events.txt, useful for debugging\n"+
77 "event mis-matches." );
82 if( args[0].equals( "-2txt" ) ) {
83 t = new Trace( true, args[1], args[2] );
85 t = new Trace( false, args[0], args[1] );
91 // event IDs are a word, timestamps are long ints
92 public static final int WORD_SIZE = 4;
93 public static final int EVENT_SIZE = WORD_SIZE;
94 public static final int TIMESTAMP_SIZE = WORD_SIZE*2;
97 ThreadData[] threadData;
100 BufferedWriter txtStream;
104 public Trace( boolean c2txt, String inFile, String outFile ) {
108 openInputStreams( inFile );
112 for( int i = 0; i < numThreads; i++ ) {
116 printStats( outFile );
120 public static int readInt( InputStream is ) {
127 int retval = (b4<<24)|(b3<<16)|(b2<<8)|b1;
134 } catch( Exception e ) {
140 public static long readLong( InputStream is ) {
152 (b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
153 (b4<<24)|(b3<<16)|(b2<< 8)|b1;
160 } catch( Exception e ) {
166 protected void openInputStreams( String filename ) {
168 BufferedInputStream bis = null;
172 bis = new BufferedInputStream( new FileInputStream( filename ) );
173 offset = readHeader( bis );
177 txtStream = new BufferedWriter( new FileWriter( "events.txt" ) );
180 } catch( Exception e ) {
185 for( int i = 0; i < numThreads; ++i ) {
187 // point a thread's event stream to the
188 // beginning of its data within the input file
189 threadData[i].dataStream =
190 new BufferedInputStream( new FileInputStream( filename ) );
194 skip -= threadData[i].dataStream.skip( skip );
197 offset += WORD_SIZE*threadData[i].numDataWords;
199 } catch( Exception e ) {
207 int readHeader( BufferedInputStream bis ) {
210 int version = readInt( bis );
212 throw new Error( "Unsupported Version" );
214 int offset = WORD_SIZE;
216 // read number of threads
217 numThreads = readInt( bis );
220 threadData = new ThreadData[numThreads];
222 // read number of words used for all events, per thread
223 for( int i = 0; i < numThreads; ++i ) {
224 threadData[i] = new ThreadData();
225 threadData[i].numDataWords = readInt( bis );
232 public void readThread( int tNum ) {
234 System.out.print( "Reading thread "+tNum );
238 txtStream.write( "\n\n\n\n" );
239 txtStream.write( "*************************************************\n" );
240 txtStream.write( "** Thread "+tNum+"\n" );
241 txtStream.write( "*************************************************\n" );
242 } catch( IOException e ) {
248 ThreadData tdata = threadData[tNum];
249 tdata.stackDepth = 0;
252 int numProgress = 10;
254 int progressChunk = tdata.numDataWords / numProgress;
256 boolean[] progress = new boolean[numProgress];
257 for( j = 0; j < numProgress; ++j ) {
262 while( i < tdata.numDataWords ) {
264 if( !progress[j] && i > j*progressChunk ) {
265 System.out.print( "." );
267 if( j < numProgress - 1 ) {
272 int eventRaw = readInt ( tdata.dataStream );
273 timeStamp = readLong( tdata.dataStream );
276 int eventType = eventRaw & CP_EVENT_MASK;
277 int eventID = eventRaw >> CP_EVENT_BASESHIFT;
279 switch( eventType ) {
281 case CP_EVENTTYPE_BEGIN: {
282 pushEvent( tdata, eventID, timeStamp );
285 case CP_EVENTTYPE_END: {
286 popEvent( tdata, eventID, timeStamp );
292 System.out.println( "" );
294 while( tdata.stackDepth > 0 ) {
295 // worker threads currently do not exit gracefully, and therefore
296 // may not register END events, so supply them with whatever the
297 // latest known timestamp is
298 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
300 if( eventSummary == null ) {
301 // if there is no previous event it means there are no children
302 // events with a timestamp for the workaround, so just punt
306 popEvent( tdata, eventSummary.eventID, timeStamp );
313 protected void pushEvent( ThreadData tdata,
317 EventSummary eventSummary = null;
319 if( tdata.stackDepth == 0 ) {
320 // there are no parents, so look in the rootEvents
321 // for an existing EventSummary of this type
322 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
325 EventSummary es = itr.next();
326 if( es.eventID == eventID ) {
331 if( eventSummary == null ) {
332 // there is no summary for this event type yet,
334 eventSummary = new EventSummary( eventID );
335 tdata.rootEvents.add( eventSummary );
339 // look through the parent's children for an existing
340 // EventSummary of this type
341 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
342 for( Iterator<EventSummary> itr = esParent.children.iterator();
345 EventSummary es = itr.next();
346 if( es.eventID == eventID ) {
351 if( eventSummary == null ) {
352 // there is no summary for this event type yet,
353 // under this parent, so add it
354 eventSummary = new EventSummary( eventID );
355 esParent.children.add( eventSummary );
356 eventSummary.parent = esParent;
360 eventSummary.timeStampBeginLatestInstance = timeStamp;
362 eventSummary.instanceCount++;
364 if( tdata.eventStack.size() <= tdata.stackDepth ) {
365 tdata.eventStack.setSize( 2*tdata.stackDepth + 20 );
368 tdata.eventStack.set( tdata.stackDepth, eventSummary );
371 // print to the event text file (if needed) before
372 // updating the stack depth to get tabs right
375 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
376 txtStream.write( " " );
378 txtStream.write( "begin "+getEventName( eventID )+"@"+timeStamp+"\n" );
379 } catch( IOException e ) {
390 protected void popEvent( ThreadData tdata,
396 // print to the event text file (if needed) after
397 // updating the stack depth to get tabs right
400 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
401 txtStream.write( " " );
403 txtStream.write( "end "+getEventName( eventID )+"@"+timeStamp+"\n" );
404 } catch( IOException e ) {
411 if( tdata.stackDepth < 0 ) {
412 throw new Error( "Event stack underflow\n" );
415 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
416 assert eventSummary != null;
418 if( eventSummary.eventID != eventID ) {
419 System.out.println( "Warning: event begin("+
420 getEventName( eventSummary.eventID )+
422 getEventName( eventID )+
427 timeStamp - eventSummary.timeStampBeginLatestInstance;
429 eventSummary.totalTime_ticks += elapsedTime;
430 eventSummary.selfTime_ticks += elapsedTime;
432 if( tdata.stackDepth - 1 >= 0 ) {
433 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth-1 );
434 esParent.selfTime_ticks -= elapsedTime;
440 public void printStats( String filename ) {
442 System.out.println( "Printing..." );
446 new BufferedWriter( new FileWriter( filename ) );
448 for( int i = 0; i < numThreads; ++i ) {
450 ThreadData tdata = threadData[i];
452 bw.write( "----------------------------------\n" );
453 bw.write( "Thread "+i+"\n" );
455 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
458 EventSummary es = itr.next();
459 printEventSummary( bw, es, 0 );
466 } catch( IOException e ) {
473 public String getEventName( int eventID ) {
475 eid2name.containsKey( eventID ) ?
476 eid2name.get ( eventID ) :
477 Integer.toString ( eventID );
481 public void printEventSummary( BufferedWriter bw,
486 String strIndent = "";
487 for( int i = 0; i < depth; ++i ) {
491 String strEventName = getEventName( es.eventID );
493 float tOfParent_perc;
494 String strPercParent = "";
495 if( es.parent != null ) {
496 float divisor = new Long( es.parent.totalTime_ticks ).floatValue();
497 if( divisor <= 0.00001f ) {
503 new Long( es.totalTime_ticks ).floatValue() /
506 strPercParent = String.format( " %%ofParent=%5.1f",
512 new Long( es.selfTime_ticks ).floatValue() /
513 new Long( es.totalTime_ticks ).floatValue();
515 String strSelfStats =
516 String.format( " total(ticks)=%12dK, %%self=%5.1f, count=%d, avgTicks=%d",
517 es.totalTime_ticks/1000,
520 (int)((float)es.totalTime_ticks/(float)es.instanceCount) );
528 for( Iterator<EventSummary> itr = es.children.iterator();
531 EventSummary esChild = itr.next();
532 printEventSummary( bw, esChild, depth + 1 );