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_COUNT_POOLALLOC = 0x15;
19 public static final int CP_EVENTID_COUNT_POOLREUSE = 0x16;
20 public static final int CP_EVENTID_WORKSCHEDGRAB = 0x20;
21 public static final int CP_EVENTID_TASKDISPATCH = 0x30;
22 public static final int CP_EVENTID_TASKEXECUTE = 0x31;
23 public static final int CP_EVENTID_TASKRETIRE = 0x32;
24 public static final int CP_EVENTID_TASKSTALLVAR = 0x40;
25 public static final int CP_EVENTID_TASKSTALLMEM = 0x41;
29 eid2name = new Hashtable<Integer, String>();
30 eid2name.put( CP_EVENTID_MAIN, "MAIN " );
31 eid2name.put( CP_EVENTID_RUNMALLOC, "RUNMALLOC " );
32 eid2name.put( CP_EVENTID_RUNFREE, "RUNFREE " );
33 eid2name.put( CP_EVENTID_RUNFREE, "POOLALLOC " );
34 eid2name.put( CP_EVENTID_RUNFREE, "POOLREUSE " );
35 eid2name.put( CP_EVENTID_WORKSCHEDGRAB, "WORKSCHEDGRAB" );
36 eid2name.put( CP_EVENTID_TASKDISPATCH, "TASKDISPATCH " );
37 eid2name.put( CP_EVENTID_TASKEXECUTE, "TASKEXECUTE " );
38 eid2name.put( CP_EVENTID_TASKRETIRE, "TASKRETIRE " );
39 eid2name.put( CP_EVENTID_TASKSTALLVAR, "TASKSTALLVAR " );
40 eid2name.put( CP_EVENTID_TASKSTALLMEM, "TASKSTALLMEM " );
43 Hashtable<Integer, String> eid2name;
47 public static void main( String args[] ) {
48 if( args.length != 2 ) {
49 System.out.println( "usage: <coreprof.dat file> <trace out file>" );
52 Trace t = new Trace( args[0], args[1] );
57 // event IDs are a word, timestamps are long ints
58 public static final int WORD_SIZE = 4;
59 public static final int EVENT_SIZE = WORD_SIZE;
60 public static final int TIMESTAMP_SIZE = WORD_SIZE*2;
63 ThreadData[] threadData;
67 public Trace( String inFile, String outFile ) {
69 openInputStreams( inFile );
73 for( int i = 0; i < numThreads; i++ ) {
77 printStats( outFile );
81 public static int readInt( InputStream is ) {
88 int retval = (b4<<24)|(b3<<16)|(b2<<8)|b1;
95 } catch( Exception e ) {
101 public static long readLong( InputStream is ) {
113 (b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
114 (b4<<24)|(b3<<16)|(b2<< 8)|b1;
121 } catch( Exception e ) {
127 protected void openInputStreams( String filename ) {
129 BufferedInputStream bis = null;
133 bis = new BufferedInputStream( new FileInputStream( filename ) );
134 offset = readHeader( bis );
136 } catch( Exception e ) {
141 for( int i = 0; i < numThreads; ++i ) {
143 // point a thread's event stream to the
144 // beginning of its data within the input file
145 threadData[i].dataStream =
146 new BufferedInputStream( new FileInputStream( filename ) );
150 skip -= threadData[i].dataStream.skip( skip );
153 offset += WORD_SIZE*threadData[i].numDataWords;
155 } catch( Exception e ) {
163 int readHeader( BufferedInputStream bis ) {
166 int version = readInt( bis );
168 throw new Error( "Unsupported Version" );
170 int offset = WORD_SIZE;
172 // read number of threads
173 numThreads = readInt( bis );
176 threadData = new ThreadData[numThreads];
178 // read number of words used for all events, per thread
179 for( int i = 0; i < numThreads; ++i ) {
180 threadData[i] = new ThreadData();
181 threadData[i].numDataWords = readInt( bis );
188 public void readThread( int tNum ) {
190 System.out.print( "Reading thread "+tNum );
192 ThreadData tdata = threadData[tNum];
193 tdata.stackDepth = 0;
196 int numProgress = 10;
198 int progressChunk = tdata.numDataWords / numProgress;
200 boolean[] progress = new boolean[numProgress];
201 for( j = 0; j < numProgress; ++j ) {
206 while( i < tdata.numDataWords ) {
208 if( !progress[j] && i > j*progressChunk ) {
209 System.out.print( "." );
211 if( j < numProgress - 1 ) {
216 int eventRaw = readInt ( tdata.dataStream );
217 timeStamp = readLong( tdata.dataStream );
220 int eventType = eventRaw & CP_EVENT_MASK;
221 int eventID = eventRaw >> CP_EVENT_BASESHIFT;
223 switch( eventType ) {
225 case CP_EVENTTYPE_BEGIN: {
226 pushEvent( tdata, eventID, timeStamp );
229 case CP_EVENTTYPE_END: {
230 popEvent( tdata, eventID, timeStamp );
236 System.out.println( "" );
238 while( tdata.stackDepth > 0 ) {
239 // worker threads currently do not exit gracefully, and therefore
240 // may not register END events, so supply them with whatever the
241 // latest known timestamp is
242 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
244 if( eventSummary == null ) {
245 // if there is no previous event it means there are no children
246 // events with a timestamp for the workaround, so just punt
250 popEvent( tdata, eventSummary.eventID, timeStamp );
257 protected void pushEvent( ThreadData tdata,
261 EventSummary eventSummary = null;
263 if( tdata.stackDepth == 0 ) {
264 // there are no parents, so look in the rootEvents
265 // for an existing EventSummary of this type
266 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
269 EventSummary es = itr.next();
270 if( es.eventID == eventID ) {
275 if( eventSummary == null ) {
276 // there is no summary for this event type yet,
278 eventSummary = new EventSummary( eventID );
279 tdata.rootEvents.add( eventSummary );
283 // look through the parent's children for an existing
284 // EventSummary of this type
285 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
286 for( Iterator<EventSummary> itr = esParent.children.iterator();
289 EventSummary es = itr.next();
290 if( es.eventID == eventID ) {
295 if( eventSummary == null ) {
296 // there is no summary for this event type yet,
297 // under this parent, so add it
298 eventSummary = new EventSummary( eventID );
299 esParent.children.add( eventSummary );
300 eventSummary.parent = esParent;
304 eventSummary.timeStampBeginLatestInstance = timeStamp;
306 eventSummary.instanceCount++;
308 if( tdata.eventStack.size() <= tdata.stackDepth ) {
309 tdata.eventStack.setSize( 2*tdata.stackDepth + 20 );
312 tdata.eventStack.set( tdata.stackDepth, eventSummary );
318 protected void popEvent( ThreadData tdata,
322 if( tdata.stackDepth < 0 ) {
323 throw new Error( "Event stack underflow\n" );
326 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
327 assert eventSummary != null;
329 if( eventSummary.eventID != eventID ) {
330 System.out.println( "Warning: event begin("+
331 getEventName( eventSummary.eventID )+
333 getEventName( eventID )+
338 timeStamp - eventSummary.timeStampBeginLatestInstance;
340 eventSummary.totalTime_ticks += elapsedTime;
341 eventSummary.selfTime_ticks += elapsedTime;
343 if( tdata.stackDepth - 1 >= 0 ) {
344 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth-1 );
345 esParent.selfTime_ticks -= elapsedTime;
351 public void printStats( String filename ) {
353 System.out.println( "Printing..." );
357 new BufferedWriter( new FileWriter( filename ) );
359 for( int i = 0; i < numThreads; ++i ) {
361 ThreadData tdata = threadData[i];
363 bw.write( "----------------------------------\n" );
364 bw.write( "Thread "+i+"\n" );
366 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
369 EventSummary es = itr.next();
370 printEventSummary( bw, es, 0 );
378 } catch( IOException e ) {}
382 public String getEventName( int eventID ) {
384 eid2name.containsKey( eventID ) ?
385 eid2name.get ( eventID ) :
386 Integer.toString ( eventID );
390 public void printEventSummary( BufferedWriter bw,
395 String strIndent = "";
396 for( int i = 0; i < depth; ++i ) {
400 String strEventName = getEventName( es.eventID );
402 float tOfParent_perc;
403 String strPercParent = "";
404 if( es.parent != null ) {
405 float divisor = new Long( es.parent.totalTime_ticks ).floatValue();
406 if( divisor <= 0.00001f ) {
412 new Long( es.totalTime_ticks ).floatValue() /
415 strPercParent = String.format( " %%ofParent=%5.1f",
421 new Long( es.selfTime_ticks ).floatValue() /
422 new Long( es.totalTime_ticks ).floatValue();
424 String strSelfStats =
425 String.format( " total(ticks)=%12dK, %%self=%5.1f, count=%d",
426 es.totalTime_ticks/1000,
436 for( Iterator<EventSummary> itr = es.children.iterator();
439 EventSummary esChild = itr.next();
440 printEventSummary( bw, esChild, depth + 1 );