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 ) {
73 System.out.println( "usage: <coreprof.dat file> <trace out file> [-2txt]\n"+
74 "\nThe -2txt option will take the raw binary events and spit\n"+
75 "out every event as text in events.txt, useful for debugging\n"+
76 "event mis-matches." );
79 String inputfile=null;
80 String outputfile=null;
82 HashSet<Integer> eventset=new HashSet<Integer>();
83 boolean hasinput=false;
88 for(int i=0; i<args.length; i++) {
89 if (args[i].equals("-2txt")) {
91 } else if (args[i].equals("-event")) {
92 eventset.add(Integer.parseInt(args[i+1]));
94 } else if (args[i].equals("-maxtime")) {
95 maxtime=Long.parseLong(args[i+1]);
97 } else if (args[i].equals("-mintime")) {
98 mintime=Long.parseLong(args[i+1]);
100 } else if (args[i].equals("-scale")) {
101 scale=Long.parseLong(args[i+1]);
113 Trace t = new Trace(events, inputfile, outputfile, eventset, mintime, maxtime,scale);
118 // event IDs are a word, timestamps are long ints
119 public static final int WORD_SIZE = 4;
120 public static final int EVENT_SIZE = WORD_SIZE;
121 public static final int TIMESTAMP_SIZE = WORD_SIZE*2;
124 ThreadData[] threadData;
125 HashSet<Integer> eventset;
128 BufferedWriter txtStream;
130 boolean convert2plot;
131 BufferedWriter bwPlot;
137 public Trace( boolean c2txt, String inFile, String outFile, HashSet<Integer> eventset, long mintime, long maxtime, long scale) {
138 this.eventset=eventset;
139 this.maxtime=maxtime;
140 this.mintime=mintime;
145 openInputStreams( inFile );
153 for( int i = 0; i < numThreads; i++ ) {
157 printStats( outFile );
161 bwPlot.write("plot [0:"+(globendtime/scale)+"] [-1:"+(numThreads+1)+"] -3\n");
163 } catch (IOException e) {
170 public static int readInt( InputStream is ) {
177 int retval = (b4<<24)|(b3<<16)|(b2<<8)|b1;
184 } catch( Exception e ) {
190 public static long readLong( InputStream is ) {
202 (b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
203 (b4<<24)|(b3<<16)|(b2<< 8)|b1;
210 } catch( Exception e ) {
216 protected void openInputStreams( String filename ) {
218 BufferedInputStream bis = null;
222 bis = new BufferedInputStream( new FileInputStream( filename ) );
223 offset = readHeader( bis );
226 } catch( Exception e ) {
231 for( int i = 0; i < numThreads; ++i ) {
233 // point a thread's event stream to the
234 // beginning of its data within the input file
235 threadData[i].dataStream =
236 new BufferedInputStream( new FileInputStream( filename ) );
240 skip -= threadData[i].dataStream.skip( skip );
242 threadData[i].dataStream.mark(20);
243 int eventRaw = readInt ( threadData[i].dataStream );
244 long Stamp = readLong( threadData[i].dataStream );
245 threadData[i].dataStream.reset();
247 if (globalstarttime==-1||globalstarttime>Stamp)
248 globalstarttime=Stamp;
251 offset += WORD_SIZE*threadData[i].numDataWords;
253 } catch( Exception e ) {
261 int readHeader( BufferedInputStream bis ) {
264 int version = readInt( bis );
266 throw new Error( "Unsupported Version" );
268 int offset = WORD_SIZE;
270 // read number of threads
271 numThreads = readInt( bis );
274 threadData = new ThreadData[numThreads];
276 // read number of words used for all events, per thread
277 for( int i = 0; i < numThreads; ++i ) {
278 threadData[i] = new ThreadData();
279 threadData[i].numDataWords = readInt( bis );
286 public void readThread( int tNum ) {
288 System.out.print( "Reading thread "+tNum );
292 txtStream = new BufferedWriter( new FileWriter( "events.txt" ) );
294 txtStream.write( "\n\n\n\n" );
295 txtStream.write( "*************************************************\n" );
296 txtStream.write( "** Thread "+tNum+"\n" );
297 txtStream.write( "*************************************************\n" );
298 } catch( IOException e ) {
305 ThreadData tdata = threadData[tNum];
306 tdata.stackDepth = 0;
309 int numProgress = 10;
311 int progressChunk = tdata.numDataWords / numProgress;
313 boolean[] progress = new boolean[numProgress];
314 for( j = 0; j < numProgress; ++j ) {
320 while( i < tdata.numDataWords ) {
322 if( !progress[j] && i > j*progressChunk ) {
323 System.out.print( "." );
325 if( j < numProgress - 1 ) {
330 int eventRaw = readInt ( tdata.dataStream );
331 timeStamp = readLong( tdata.dataStream );
334 int eventType = eventRaw & CP_EVENT_MASK;
335 int eventID = eventRaw >> CP_EVENT_BASESHIFT;
337 if (eventset.isEmpty()||eventset.contains(eventID))
338 switch( eventType ) {
340 case CP_EVENTTYPE_BEGIN: {
341 if( eventID == CP_EVENTID_MAIN ) {
342 tSysZero = timeStamp;
344 if( tdata.stackDepth>0 && convert2plot ) {
345 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
346 long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
347 addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
350 pushEvent( tdata, eventID, timeStamp );
353 case CP_EVENTTYPE_END: {
355 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
356 long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
358 addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
361 popEvent( tdata, eventID, timeStamp );
366 System.out.println( "" );
368 while( tdata.stackDepth > 0 ) {
369 // worker threads currently do not exit gracefully, and therefore
370 // may not register END events, so supply them with whatever the
371 // latest known timestamp is
372 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth);
374 if( eventSummary == null ) {
375 // if there is no previous event it means there are no children
376 // events with a timestamp for the workaround, so just punt
380 popEvent( tdata, eventSummary.eventID, timeStamp );
388 protected void pushEvent( ThreadData tdata,
392 EventSummary eventSummary = null;
394 if( tdata.stackDepth == 0 ) {
395 // there are no parents, so look in the rootEvents
396 // for an existing EventSummary of this type
397 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
400 EventSummary es = itr.next();
401 if( es.eventID == eventID ) {
406 if( eventSummary == null ) {
407 // there is no summary for this event type yet,
409 eventSummary = new EventSummary( eventID );
410 tdata.rootEvents.add( eventSummary );
414 // look through the parent's children for an existing
415 // EventSummary of this type
416 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
417 for( Iterator<EventSummary> itr = esParent.children.iterator();
420 EventSummary es = itr.next();
421 if( es.eventID == eventID ) {
426 if( eventSummary == null ) {
427 // there is no summary for this event type yet,
428 // under this parent, so add it
429 eventSummary = new EventSummary( eventID );
430 esParent.children.add( eventSummary );
431 eventSummary.parent = esParent;
435 eventSummary.timeStampBeginLatestInstance = timeStamp;
437 eventSummary.instanceCount++;
439 if( tdata.eventStack.size() <= tdata.stackDepth ) {
440 tdata.eventStack.setSize( 2*tdata.stackDepth + 20 );
443 tdata.eventStack.set( tdata.stackDepth, eventSummary );
446 // print to the event text file (if needed) before
447 // updating the stack depth to get tabs right
450 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
451 txtStream.write( " " );
453 txtStream.write( "begin "+getEventName( eventID )+"@"+timeStamp+"\n" );
454 } catch( IOException e ) {
465 protected void popEvent( ThreadData tdata,
471 // print to the event text file (if needed) after
472 // updating the stack depth to get tabs right
475 for( int tabs = 0; tabs < tdata.stackDepth; ++tabs ) {
476 txtStream.write( " " );
478 txtStream.write( "end "+getEventName( eventID )+"@"+timeStamp+"\n" );
479 } catch( IOException e ) {
486 if( tdata.stackDepth < 0 ) {
487 throw new Error( "Event stack underflow\n" );
490 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
491 assert eventSummary != null;
493 if( eventSummary.eventID != eventID ) {
494 System.out.println( "Warning: event begin("+
495 getEventName( eventSummary.eventID )+
497 getEventName( eventID )+
502 timeStamp - eventSummary.timeStampBeginLatestInstance;
504 eventSummary.totalTime_ticks += elapsedTime;
505 eventSummary.selfTime_ticks += elapsedTime;
507 if( tdata.stackDepth - 1 >= 0 ) {
508 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth-1 );
509 esParent.selfTime_ticks -= elapsedTime;
513 long globalstarttime=-1;
516 public void addPointToPlot( int thread, int eventID, long starttime, long endtime) {
518 long nstart=starttime-globalstarttime-mintime;
519 long nend=endtime-globalstarttime-mintime;
520 if ((maxtime==-1 || (endtime-globalstarttime)<maxtime)&&(nend>0)) {
521 if (nend>globendtime)
525 bwPlot.write( "set arrow from "+(nstart/scale)+","+thread+" to "+(nend/scale)+","+thread+" lt "+eventID+" nohead\n");
527 } catch( IOException e ) {
535 public void printStats( String filename ) {
537 System.out.println( "Printing..." );
541 new BufferedWriter( new FileWriter( filename ) );
543 for( int i = 0; i < numThreads; ++i ) {
545 ThreadData tdata = threadData[i];
547 bw.write( "----------------------------------\n" );
548 bw.write( "Thread "+i+"\n" );
550 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
553 EventSummary es = itr.next();
554 printEventSummary( bw, es, 0 );
561 } catch( IOException e ) {
568 public String getEventName( int eventID ) {
570 eid2name.containsKey( eventID ) ?
571 eid2name.get ( eventID ) :
572 Integer.toString ( eventID );
576 public void printEventSummary( BufferedWriter bw,
581 String strIndent = "";
582 for( int i = 0; i < depth; ++i ) {
586 String strEventName = getEventName( es.eventID );
588 float tOfParent_perc;
589 String strPercParent = "";
590 if( es.parent != null ) {
591 float divisor = new Long( es.parent.totalTime_ticks ).floatValue();
592 if( divisor <= 0.00001f ) {
598 new Long( es.totalTime_ticks ).floatValue() /
601 strPercParent = String.format( " %%ofParent=%5.1f",
607 new Long( es.selfTime_ticks ).floatValue() /
608 new Long( es.totalTime_ticks ).floatValue();
610 String strSelfStats =
611 String.format( " total(ticks)=%12dK, %%self=%5.1f, count=%d, avgTicks=%d",
612 es.totalTime_ticks/1000,
615 (int)((float)es.totalTime_ticks/(float)es.instanceCount) );
623 for( Iterator<EventSummary> itr = es.children.iterator();
626 EventSummary esChild = itr.next();
627 printEventSummary( bw, esChild, depth + 1 );
632 public void printPlotCmd() {
635 new BufferedWriter( new FileWriter( "plot.cmd" ) );
636 } catch( IOException e ) {