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 long minObservedTime = Long.MAX_VALUE;
142 long maxObservedTime = Long.MIN_VALUE;
145 public Trace( boolean c2txt, String inFile, String outFile, HashSet<Integer> eventset, long mintime, long maxtime, long scale) {
146 this.eventset=eventset;
147 this.maxtime=maxtime;
148 this.mintime=mintime;
153 openInputStreams( inFile );
161 for( int i = 0; i < numThreads; i++ ) {
165 System.out.println( "Minimum observed time stamp: "+minObservedTime );
166 System.out.println( "Maximum observed time stamp: "+maxObservedTime );
168 System.out.println( "\nelapsed time: "+(maxObservedTime - minObservedTime) );
171 printStats( outFile );
175 bwPlot.write("plot [0:"+(globendtime/scale)+"] [-1:"+(numThreads+1)+"] -3\n");
177 } catch (IOException e) {
184 public static int readInt( InputStream is ) {
191 int retval = (b4<<24)|(b3<<16)|(b2<<8)|b1;
198 } catch( Exception e ) {
204 public static long readLong( InputStream is ) {
216 (b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
217 (b4<<24)|(b3<<16)|(b2<< 8)|b1;
224 } catch( Exception e ) {
230 protected void openInputStreams( String filename ) {
232 BufferedInputStream bis = null;
236 bis = new BufferedInputStream( new FileInputStream( filename ) );
237 offset = readHeader( bis );
240 } catch( Exception e ) {
245 for( int i = 0; i < numThreads; ++i ) {
247 // point a thread's event stream to the
248 // beginning of its data within the input file
249 threadData[i].dataStream =
250 new BufferedInputStream( new FileInputStream( filename ) );
254 skip -= threadData[i].dataStream.skip( skip );
256 threadData[i].dataStream.mark(20);
257 int eventRaw = readInt ( threadData[i].dataStream );
258 long Stamp = readLong( threadData[i].dataStream );
259 threadData[i].dataStream.reset();
261 if (globalstarttime==-1||globalstarttime>Stamp)
262 globalstarttime=Stamp;
265 offset += WORD_SIZE*threadData[i].numDataWords;
267 } catch( Exception e ) {
275 int readHeader( BufferedInputStream bis ) {
278 int version = readInt( bis );
280 throw new Error( "Unsupported Version" );
282 int offset = WORD_SIZE;
284 // read number of threads
285 numThreads = readInt( bis );
288 threadData = new ThreadData[numThreads];
290 // read number of words used for all events, per thread
291 for( int i = 0; i < numThreads; ++i ) {
292 threadData[i] = new ThreadData();
293 threadData[i].numDataWords = readInt( bis );
300 public void readThread( int tNum ) {
302 System.out.print( "Reading thread "+tNum );
306 txtStream = new BufferedWriter( new FileWriter( "events.txt" ) );
308 txtStream.write( "\n\n\n\n" );
309 txtStream.write( "*************************************************\n" );
310 txtStream.write( "** Thread "+tNum+"\n" );
311 txtStream.write( "*************************************************\n" );
312 } catch( IOException e ) {
319 ThreadData tdata = threadData[tNum];
320 tdata.stackDepth = 0;
323 int numProgress = 10;
325 int progressChunk = tdata.numDataWords / numProgress;
327 boolean[] progress = new boolean[numProgress];
328 for( j = 0; j < numProgress; ++j ) {
334 while( i < tdata.numDataWords ) {
336 if( !progress[j] && i > j*progressChunk ) {
337 System.out.print( "." );
339 if( j < numProgress - 1 ) {
344 int eventRaw = readInt ( tdata.dataStream );
345 timeStamp = readLong( tdata.dataStream );
349 if( timeStamp < minObservedTime ) {
350 minObservedTime = timeStamp;
353 if( timeStamp > maxObservedTime ) {
354 maxObservedTime = timeStamp;
358 int eventType = eventRaw & CP_EVENT_MASK;
359 int eventID = eventRaw >> CP_EVENT_BASESHIFT;
361 if (eventset.isEmpty()||eventset.contains(eventID))
362 switch( eventType ) {
364 case CP_EVENTTYPE_BEGIN: {
365 if( eventID == CP_EVENTID_MAIN ) {
366 tSysZero = timeStamp;
368 if( tdata.stackDepth>0 && convert2plot ) {
369 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
370 long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
371 addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
374 pushEvent( tdata, eventID, timeStamp );
377 case CP_EVENTTYPE_END: {
379 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
380 long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
382 addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
385 popEvent( tdata, eventID, timeStamp );
390 System.out.println( "" );
392 while( tdata.stackDepth > 0 ) {
393 // worker threads currently do not exit gracefully, and therefore
394 // may not register END events, so supply them with whatever the
395 // latest known timestamp is
396 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth);
398 if( eventSummary == null ) {
399 // if there is no previous event it means there are no children
400 // events with a timestamp for the workaround, so just punt
404 popEvent( tdata, eventSummary.eventID, timeStamp );
412 protected void pushEvent( ThreadData tdata,
416 EventSummary eventSummary = null;
418 if( tdata.stackDepth == 0 ) {
419 // there are no parents, so look in the rootEvents
420 // for an existing EventSummary of this type
421 for( Iterator<EventSummary> itr = tdata.rootEvents.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,
433 eventSummary = new EventSummary( eventID );
434 tdata.rootEvents.add( eventSummary );
438 // look through the parent's children for an existing
439 // EventSummary of this type
440 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
441 for( Iterator<EventSummary> itr = esParent.children.iterator();
444 EventSummary es = itr.next();
445 if( es.eventID == eventID ) {
450 if( eventSummary == null ) {
451 // there is no summary for this event type yet,
452 // under this parent, so add it
453 eventSummary = new EventSummary( eventID );
454 esParent.children.add( eventSummary );
455 eventSummary.parent = esParent;
459 eventSummary.timeStampBeginLatestInstance = timeStamp;
461 eventSummary.instanceCount++;
463 if( tdata.eventStack.size() <= tdata.stackDepth ) {
464 tdata.eventStack.setSize( 2*tdata.stackDepth + 20 );
467 tdata.eventStack.set( tdata.stackDepth, eventSummary );
470 // print to the event text file (if needed) before
471 // updating the stack depth to get tabs right
474 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
475 txtStream.write( " " );
477 txtStream.write( "begin "+getEventName( eventID )+"@"+timeStamp+"\n" );
478 } catch( IOException e ) {
489 protected void popEvent( ThreadData tdata,
495 // print to the event text file (if needed) after
496 // updating the stack depth to get tabs right
499 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
500 txtStream.write( " " );
502 txtStream.write( "end "+getEventName( eventID )+"@"+timeStamp+"\n" );
503 } catch( IOException e ) {
510 if( tdata.stackDepth < 0 ) {
511 throw new Error( "Event stack underflow\n" );
514 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
515 assert eventSummary != null;
517 if( eventSummary.eventID != eventID ) {
518 System.out.println( "Warning: event begin("+
519 getEventName( eventSummary.eventID )+
521 getEventName( eventID )+
526 timeStamp - eventSummary.timeStampBeginLatestInstance;
528 eventSummary.totalTime_ticks += elapsedTime;
529 eventSummary.selfTime_ticks += elapsedTime;
531 if( tdata.stackDepth - 1 >= 0 ) {
532 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth-1 );
533 esParent.selfTime_ticks -= elapsedTime;
537 long globalstarttime=-1;
540 public void addPointToPlot( int thread, int eventID, long starttime, long endtime) {
542 long nstart=starttime-globalstarttime-mintime;
543 long nend=endtime-globalstarttime-mintime;
544 if ((maxtime==-1 || (endtime-globalstarttime)<maxtime)&&(nend>0)) {
545 if (nend>globendtime)
549 bwPlot.write( "set arrow from "+(nstart/scale)+","+thread+
550 " to "+(nend/scale)+","+thread+" lt "+eventID+"\n");
552 } catch( IOException e ) {
560 public void printStats( String filename ) {
562 System.out.println( "Printing..." );
566 new BufferedWriter( new FileWriter( filename ) );
568 for( int i = 0; i < numThreads; ++i ) {
570 ThreadData tdata = threadData[i];
572 bw.write( "----------------------------------\n" );
573 bw.write( "Thread "+i+"\n" );
575 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
578 EventSummary es = itr.next();
579 printEventSummary( bw, es, 0 );
586 } catch( IOException e ) {
593 public String getEventName( int eventID ) {
595 eid2name.containsKey( eventID ) ?
596 eid2name.get ( eventID ) :
597 Integer.toString ( eventID );
601 public void printEventSummary( BufferedWriter bw,
606 String strIndent = "";
607 for( int i = 0; i < depth; ++i ) {
611 String strEventName = getEventName( es.eventID );
613 float tOfParent_perc;
614 String strPercParent = "";
615 if( es.parent != null ) {
616 float divisor = new Long( es.parent.totalTime_ticks ).floatValue();
617 if( divisor <= 0.00001f ) {
623 new Long( es.totalTime_ticks ).floatValue() /
626 strPercParent = String.format( " %%ofParent=%5.1f",
632 new Long( es.selfTime_ticks ).floatValue() /
633 new Long( es.totalTime_ticks ).floatValue();
635 String strSelfStats =
636 String.format( " total(ticks)=%12dK, %%self=%5.1f, count=%d, avgTicks=%d",
637 es.totalTime_ticks/1000,
640 (int)((float)es.totalTime_ticks/(float)es.instanceCount) );
648 for( Iterator<EventSummary> itr = es.children.iterator();
651 EventSummary esChild = itr.next();
652 printEventSummary( bw, esChild, depth + 1 );
657 public void printPlotCmd() {
660 new BufferedWriter( new FileWriter( "plot.cmd" ) );
661 } catch( IOException e ) {