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_WORKSCHEDSUBMIT = 0x21;
23 public static final int CP_EVENTID_TASKDISPATCH = 0x30;
24 public static final int CP_EVENTID_PREPAREMEMQ = 0x31;
25 public static final int CP_EVENTID_TASKEXECUTE = 0x40;
26 public static final int CP_EVENTID_TASKRETIRE = 0x50;
27 public static final int CP_EVENTID_TASKSTALLVAR = 0x60;
28 public static final int CP_EVENTID_TASKSTALLMEM = 0x61;
29 public static final int CP_EVENTID_RCR_TRAVERSE = 0x80;
30 public static final int CP_EVENTID_DEBUG_A = 0x180;
31 public static final int CP_EVENTID_DEBUG_B = 0x181;
32 public static final int CP_EVENTID_DEBUG_C = 0x182;
33 public static final int CP_EVENTID_DEBUG_D = 0x183;
34 public static final int CP_EVENTID_DEBUG_E = 0x184;
35 public static final int CP_EVENTID_DEBUG_F = 0x185;
36 public static final int CP_EVENTID_DEBUG_G = 0x186;
37 public static final int CP_EVENTID_DEBUG_H = 0x187;
38 public static final int CP_EVENTID_DEBUG_I = 0x188;
39 public static final int CP_EVENTID_DEBUG_J = 0x189;
43 eid2name = new Hashtable<Integer, String>();
44 eid2name.put( CP_EVENTID_MAIN, "MAIN " );
45 eid2name.put( CP_EVENTID_RUNMALLOC, "RUNMALLOC " );
46 eid2name.put( CP_EVENTID_RUNFREE, "RUNFREE " );
47 eid2name.put( CP_EVENTID_POOLALLOC, "POOLALLOC " );
48 eid2name.put( CP_EVENTID_COUNT_POOLALLOC, "POOLALLOC CNT " );
49 eid2name.put( CP_EVENTID_COUNT_POOLREUSE, "POOLREUSE CNT " );
50 eid2name.put( CP_EVENTID_WORKSCHEDGRAB, "WORKSCHEDGRAB " );
51 eid2name.put( CP_EVENTID_WORKSCHEDSUBMIT, "WORKSCHEDSUBMIT" );
52 eid2name.put( CP_EVENTID_TASKDISPATCH, "TASKDISPATCH " );
53 eid2name.put( CP_EVENTID_PREPAREMEMQ, "PREPAREMEMQ " );
54 eid2name.put( CP_EVENTID_TASKEXECUTE, "TASKEXECUTE " );
55 eid2name.put( CP_EVENTID_TASKRETIRE, "TASKRETIRE " );
56 eid2name.put( CP_EVENTID_TASKSTALLVAR, "TASKSTALLVAR " );
57 eid2name.put( CP_EVENTID_TASKSTALLMEM, "TASKSTALLMEM " );
58 eid2name.put( CP_EVENTID_RCR_TRAVERSE, "RCR TRAVERSE " );
59 eid2name.put( CP_EVENTID_DEBUG_A, "DEBUG A " );
60 eid2name.put( CP_EVENTID_DEBUG_B, "DEBUG B " );
61 eid2name.put( CP_EVENTID_DEBUG_C, "DEBUG C " );
62 eid2name.put( CP_EVENTID_DEBUG_D, "DEBUG D " );
63 eid2name.put( CP_EVENTID_DEBUG_E, "DEBUG E " );
64 eid2name.put( CP_EVENTID_DEBUG_F, "DEBUG F " );
65 eid2name.put( CP_EVENTID_DEBUG_G, "DEBUG G " );
66 eid2name.put( CP_EVENTID_DEBUG_H, "DEBUG H " );
67 eid2name.put( CP_EVENTID_DEBUG_I, "DEBUG I " );
68 eid2name.put( CP_EVENTID_DEBUG_J, "DEBUG J " );
71 Hashtable<Integer, String> eid2name;
75 public static void main( String args[] ) {
76 if( args.length < 2 ) {
77 System.out.println( "usage: <coreprof.dat file> <trace out file> [-2txt]\n"+
78 "\nThe -2txt option will take the raw binary events and spit\n"+
79 "out every event as text in events.txt, useful for debugging\n"+
80 "event mis-matches." );
83 String inputfile=null;
84 String outputfile=null;
86 HashSet<Integer> eventset=new HashSet<Integer>();
87 boolean hasinput=false;
92 for(int i=0; i<args.length; i++) {
93 if (args[i].equals("-2txt")) {
95 } else if (args[i].equals("-event")) {
96 eventset.add(Integer.parseInt(args[i+1]));
98 } else if (args[i].equals("-maxtime")) {
99 maxtime=Long.parseLong(args[i+1]);
101 } else if (args[i].equals("-mintime")) {
102 mintime=Long.parseLong(args[i+1]);
104 } else if (args[i].equals("-scale")) {
105 scale=Long.parseLong(args[i+1]);
117 Trace t = new Trace(events, inputfile, outputfile, eventset, mintime, maxtime,scale);
122 // event IDs are a word, timestamps are long ints
123 public static final int WORD_SIZE = 4;
124 public static final int EVENT_SIZE = WORD_SIZE;
125 public static final int TIMESTAMP_SIZE = WORD_SIZE*2;
128 ThreadData[] threadData;
129 HashSet<Integer> eventset;
132 BufferedWriter txtStream;
134 boolean convert2plot;
135 BufferedWriter bwPlot;
141 public Trace( boolean c2txt, String inFile, String outFile, HashSet<Integer> eventset, long mintime, long maxtime, long scale) {
142 this.eventset=eventset;
143 this.maxtime=maxtime;
144 this.mintime=mintime;
149 openInputStreams( inFile );
157 for( int i = 0; i < numThreads; i++ ) {
161 printStats( outFile );
165 bwPlot.write("plot [0:"+(globendtime/scale)+"] [-1:"+(numThreads+1)+"] -3\n");
167 } catch (IOException e) {
174 public static int readInt( InputStream is ) {
181 int retval = (b4<<24)|(b3<<16)|(b2<<8)|b1;
188 } catch( Exception e ) {
194 public static long readLong( InputStream is ) {
206 (b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
207 (b4<<24)|(b3<<16)|(b2<< 8)|b1;
214 } catch( Exception e ) {
220 protected void openInputStreams( String filename ) {
222 BufferedInputStream bis = null;
226 bis = new BufferedInputStream( new FileInputStream( filename ) );
227 offset = readHeader( bis );
230 } catch( Exception e ) {
235 for( int i = 0; i < numThreads; ++i ) {
237 // point a thread's event stream to the
238 // beginning of its data within the input file
239 threadData[i].dataStream =
240 new BufferedInputStream( new FileInputStream( filename ) );
244 skip -= threadData[i].dataStream.skip( skip );
246 threadData[i].dataStream.mark(20);
247 int eventRaw = readInt ( threadData[i].dataStream );
248 long Stamp = readLong( threadData[i].dataStream );
249 threadData[i].dataStream.reset();
251 if (globalstarttime==-1||globalstarttime>Stamp)
252 globalstarttime=Stamp;
255 offset += WORD_SIZE*threadData[i].numDataWords;
257 } catch( Exception e ) {
265 int readHeader( BufferedInputStream bis ) {
268 int version = readInt( bis );
270 throw new Error( "Unsupported Version" );
272 int offset = WORD_SIZE;
274 // read number of threads
275 numThreads = readInt( bis );
278 threadData = new ThreadData[numThreads];
280 // read number of words used for all events, per thread
281 for( int i = 0; i < numThreads; ++i ) {
282 threadData[i] = new ThreadData();
283 threadData[i].numDataWords = readInt( bis );
290 public void readThread( int tNum ) {
292 System.out.print( "Reading thread "+tNum );
296 txtStream = new BufferedWriter( new FileWriter( "events.txt" ) );
298 txtStream.write( "\n\n\n\n" );
299 txtStream.write( "*************************************************\n" );
300 txtStream.write( "** Thread "+tNum+"\n" );
301 txtStream.write( "*************************************************\n" );
302 } catch( IOException e ) {
309 ThreadData tdata = threadData[tNum];
310 tdata.stackDepth = 0;
313 int numProgress = 10;
315 int progressChunk = tdata.numDataWords / numProgress;
317 boolean[] progress = new boolean[numProgress];
318 for( j = 0; j < numProgress; ++j ) {
324 while( i < tdata.numDataWords ) {
326 if( !progress[j] && i > j*progressChunk ) {
327 System.out.print( "." );
329 if( j < numProgress - 1 ) {
334 int eventRaw = readInt ( tdata.dataStream );
335 timeStamp = readLong( tdata.dataStream );
338 int eventType = eventRaw & CP_EVENT_MASK;
339 int eventID = eventRaw >> CP_EVENT_BASESHIFT;
341 if (eventset.isEmpty()||eventset.contains(eventID))
342 switch( eventType ) {
344 case CP_EVENTTYPE_BEGIN: {
345 if( eventID == CP_EVENTID_MAIN ) {
346 tSysZero = timeStamp;
348 if( tdata.stackDepth>0 && convert2plot ) {
349 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
350 long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
351 addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
354 pushEvent( tdata, eventID, timeStamp );
357 case CP_EVENTTYPE_END: {
359 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
360 long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
362 addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
365 popEvent( tdata, eventID, timeStamp );
370 System.out.println( "" );
372 while( tdata.stackDepth > 0 ) {
373 // worker threads currently do not exit gracefully, and therefore
374 // may not register END events, so supply them with whatever the
375 // latest known timestamp is
376 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth);
378 if( eventSummary == null ) {
379 // if there is no previous event it means there are no children
380 // events with a timestamp for the workaround, so just punt
384 popEvent( tdata, eventSummary.eventID, timeStamp );
392 protected void pushEvent( ThreadData tdata,
396 EventSummary eventSummary = null;
398 if( tdata.stackDepth == 0 ) {
399 // there are no parents, so look in the rootEvents
400 // for an existing EventSummary of this type
401 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
404 EventSummary es = itr.next();
405 if( es.eventID == eventID ) {
410 if( eventSummary == null ) {
411 // there is no summary for this event type yet,
413 eventSummary = new EventSummary( eventID );
414 tdata.rootEvents.add( eventSummary );
418 // look through the parent's children for an existing
419 // EventSummary of this type
420 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
421 for( Iterator<EventSummary> itr = esParent.children.iterator();
424 EventSummary es = itr.next();
425 if( es.eventID == eventID ) {
430 if( eventSummary == null ) {
431 // there is no summary for this event type yet,
432 // under this parent, so add it
433 eventSummary = new EventSummary( eventID );
434 esParent.children.add( eventSummary );
435 eventSummary.parent = esParent;
439 eventSummary.timeStampBeginLatestInstance = timeStamp;
441 eventSummary.instanceCount++;
443 if( tdata.eventStack.size() <= tdata.stackDepth ) {
444 tdata.eventStack.setSize( 2*tdata.stackDepth + 20 );
447 tdata.eventStack.set( tdata.stackDepth, eventSummary );
450 // print to the event text file (if needed) before
451 // updating the stack depth to get tabs right
454 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
455 txtStream.write( " " );
457 txtStream.write( "begin "+getEventName( eventID )+"@"+timeStamp+"\n" );
458 } catch( IOException e ) {
469 protected void popEvent( ThreadData tdata,
475 // print to the event text file (if needed) after
476 // updating the stack depth to get tabs right
479 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
480 txtStream.write( " " );
482 txtStream.write( "end "+getEventName( eventID )+"@"+timeStamp+"\n" );
483 } catch( IOException e ) {
490 if( tdata.stackDepth < 0 ) {
491 throw new Error( "Event stack underflow\n" );
494 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
495 assert eventSummary != null;
497 if( eventSummary.eventID != eventID ) {
498 System.out.println( "Warning: event begin("+
499 getEventName( eventSummary.eventID )+
501 getEventName( eventID )+
506 timeStamp - eventSummary.timeStampBeginLatestInstance;
508 eventSummary.totalTime_ticks += elapsedTime;
509 eventSummary.selfTime_ticks += elapsedTime;
511 if( tdata.stackDepth - 1 >= 0 ) {
512 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth-1 );
513 esParent.selfTime_ticks -= elapsedTime;
517 long globalstarttime=-1;
520 public void addPointToPlot( int thread, int eventID, long starttime, long endtime) {
522 long nstart=starttime-globalstarttime-mintime;
523 long nend=endtime-globalstarttime-mintime;
524 if ((maxtime==-1 || (endtime-globalstarttime)<maxtime)&&(nend>0)) {
525 if (nend>globendtime)
529 bwPlot.write( "set arrow from "+(nstart/scale)+","+thread+
530 " to "+(nend/scale)+","+thread+" lt palette cb "+eventID+" nohead\n");
532 } catch( IOException e ) {
540 public void printStats( String filename ) {
542 System.out.println( "Printing..." );
546 new BufferedWriter( new FileWriter( filename ) );
548 for( int i = 0; i < numThreads; ++i ) {
550 ThreadData tdata = threadData[i];
552 bw.write( "----------------------------------\n" );
553 bw.write( "Thread "+i+"\n" );
555 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
558 EventSummary es = itr.next();
559 printEventSummary( bw, es, 0 );
566 } catch( IOException e ) {
573 public String getEventName( int eventID ) {
575 eid2name.containsKey( eventID ) ?
576 eid2name.get ( eventID ) :
577 Integer.toString ( eventID );
581 public void printEventSummary( BufferedWriter bw,
586 String strIndent = "";
587 for( int i = 0; i < depth; ++i ) {
591 String strEventName = getEventName( es.eventID );
593 float tOfParent_perc;
594 String strPercParent = "";
595 if( es.parent != null ) {
596 float divisor = new Long( es.parent.totalTime_ticks ).floatValue();
597 if( divisor <= 0.00001f ) {
603 new Long( es.totalTime_ticks ).floatValue() /
606 strPercParent = String.format( " %%ofParent=%5.1f",
612 new Long( es.selfTime_ticks ).floatValue() /
613 new Long( es.totalTime_ticks ).floatValue();
615 String strSelfStats =
616 String.format( " total(ticks)=%12dK, %%self=%5.1f, count=%d, avgTicks=%d",
617 es.totalTime_ticks/1000,
620 (int)((float)es.totalTime_ticks/(float)es.instanceCount) );
628 for( Iterator<EventSummary> itr = es.children.iterator();
631 EventSummary esChild = itr.next();
632 printEventSummary( bw, esChild, depth + 1 );
637 public void printPlotCmd() {
640 new BufferedWriter( new FileWriter( "plot.cmd" ) );
641 } catch( IOException e ) {