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_WORKSCHEDGRAB, "WORKSCHEDGRAB" );
34 eid2name.put( CP_EVENTID_TASKDISPATCH, "TASKDISPATCH " );
35 eid2name.put( CP_EVENTID_TASKEXECUTE, "TASKEXECUTE " );
36 eid2name.put( CP_EVENTID_TASKRETIRE, "TASKRETIRE " );
37 eid2name.put( CP_EVENTID_TASKSTALLVAR, "TASKSTALLVAR " );
38 eid2name.put( CP_EVENTID_TASKSTALLMEM, "TASKSTALLMEM " );
41 Hashtable<Integer, String> eid2name;
45 public static void main( String args[] ) {
46 if( args.length != 2 ) {
47 System.out.println( "usage: <coreprof.dat file> <trace out file>" );
50 Trace t = new Trace( args[0], args[1] );
55 // event IDs are a word, timestamps are long ints
56 public static final int WORD_SIZE = 4;
57 public static final int EVENT_SIZE = WORD_SIZE;
58 public static final int TIMESTAMP_SIZE = WORD_SIZE*2;
61 ThreadData[] threadData;
65 public Trace( String inFile, String outFile ) {
67 openInputStreams( inFile );
71 for( int i = 0; i < numThreads; i++ ) {
75 printStats( outFile );
79 public static int readInt( InputStream is ) {
86 int retval = (b4<<24)|(b3<<16)|(b2<<8)|b1;
93 } catch( Exception e ) {
99 public static long readLong( InputStream is ) {
111 (b8<<56)|(b7<<48)|(b6<<40)|(b5<<32)|
112 (b4<<24)|(b3<<16)|(b2<< 8)|b1;
119 } catch( Exception e ) {
125 protected void openInputStreams( String filename ) {
127 BufferedInputStream bis = null;
131 bis = new BufferedInputStream( new FileInputStream( filename ) );
132 offset = readHeader( bis );
134 } catch( Exception e ) {
139 for( int i = 0; i < numThreads; ++i ) {
141 // point a thread's event stream to the
142 // beginning of its data within the input file
143 threadData[i].dataStream =
144 new BufferedInputStream( new FileInputStream( filename ) );
148 skip -= threadData[i].dataStream.skip( skip );
151 offset += WORD_SIZE*threadData[i].numDataWords;
153 } catch( Exception e ) {
161 int readHeader( BufferedInputStream bis ) {
164 int version = readInt( bis );
166 throw new Error( "Unsupported Version" );
168 int offset = WORD_SIZE;
170 // read number of threads
171 numThreads = readInt( bis );
174 threadData = new ThreadData[numThreads];
176 // read number of words used for all events, per thread
177 for( int i = 0; i < numThreads; ++i ) {
178 threadData[i] = new ThreadData();
179 threadData[i].numDataWords = readInt( bis );
186 public void readThread( int tNum ) {
188 System.out.print( "Reading thread "+tNum );
190 ThreadData tdata = threadData[tNum];
191 tdata.stackDepth = 0;
194 int numProgress = 10;
196 int progressChunk = tdata.numDataWords / numProgress;
198 boolean[] progress = new boolean[numProgress];
199 for( j = 0; j < numProgress; ++j ) {
204 while( i < tdata.numDataWords ) {
206 if( !progress[j] && i > j*progressChunk ) {
207 System.out.print( "." );
209 if( j < numProgress - 1 ) {
214 int eventRaw = readInt ( tdata.dataStream );
215 timeStamp = readLong( tdata.dataStream );
218 int eventType = eventRaw & CP_EVENT_MASK;
219 int eventID = eventRaw >> CP_EVENT_BASESHIFT;
221 switch( eventType ) {
223 case CP_EVENTTYPE_BEGIN: {
224 pushEvent( tdata, eventID, timeStamp );
227 case CP_EVENTTYPE_END: {
228 popEvent( tdata, eventID, timeStamp );
234 System.out.println( "" );
236 while( tdata.stackDepth > 0 ) {
237 // worker threads currently do not exit gracefully, and therefore
238 // may not register END events, so supply them with whatever the
239 // latest known timestamp is
240 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
242 if( eventSummary == null ) {
243 // if there is no previous event it means there are no children
244 // events with a timestamp for the workaround, so just punt
248 popEvent( tdata, eventSummary.eventID, timeStamp );
255 protected void pushEvent( ThreadData tdata,
259 EventSummary eventSummary = null;
261 if( tdata.stackDepth == 0 ) {
262 // there are no parents, so look in the rootEvents
263 // for an existing EventSummary of this type
264 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
267 EventSummary es = itr.next();
268 if( es.eventID == eventID ) {
273 if( eventSummary == null ) {
274 // there is no summary for this event type yet,
276 eventSummary = new EventSummary( eventID );
277 tdata.rootEvents.add( eventSummary );
281 // look through the parent's children for an existing
282 // EventSummary of this type
283 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
284 for( Iterator<EventSummary> itr = esParent.children.iterator();
287 EventSummary es = itr.next();
288 if( es.eventID == eventID ) {
293 if( eventSummary == null ) {
294 // there is no summary for this event type yet,
295 // under this parent, so add it
296 eventSummary = new EventSummary( eventID );
297 esParent.children.add( eventSummary );
298 eventSummary.parent = esParent;
302 eventSummary.timeStampBeginLatestInstance = timeStamp;
304 eventSummary.instanceCount++;
306 if( tdata.eventStack.size() <= tdata.stackDepth ) {
307 tdata.eventStack.setSize( 2*tdata.stackDepth + 20 );
310 tdata.eventStack.set( tdata.stackDepth, eventSummary );
316 protected void popEvent( ThreadData tdata,
320 if( tdata.stackDepth < 0 ) {
321 throw new Error( "Event stack underflow\n" );
324 EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
325 assert eventSummary != null;
327 if( eventSummary.eventID != eventID ) {
328 System.out.println( "Warning: event begin("+
329 getEventName( eventSummary.eventID )+
331 getEventName( eventID )+
336 timeStamp - eventSummary.timeStampBeginLatestInstance;
338 eventSummary.totalTime_ticks += elapsedTime;
339 eventSummary.selfTime_ticks += elapsedTime;
341 if( tdata.stackDepth - 1 >= 0 ) {
342 EventSummary esParent = tdata.eventStack.get( tdata.stackDepth-1 );
343 esParent.selfTime_ticks -= elapsedTime;
349 public void printStats( String filename ) {
351 System.out.println( "Printing..." );
355 new BufferedWriter( new FileWriter( filename ) );
357 for( int i = 0; i < numThreads; ++i ) {
359 ThreadData tdata = threadData[i];
361 bw.write( "----------------------------------\n" );
362 bw.write( "Thread "+i+"\n" );
364 for( Iterator<EventSummary> itr = tdata.rootEvents.iterator();
367 EventSummary es = itr.next();
368 printEventSummary( bw, es, 0 );
376 } catch( IOException e ) {}
380 public String getEventName( int eventID ) {
382 eid2name.containsKey( eventID ) ?
383 eid2name.get ( eventID ) :
384 Integer.toString ( eventID );
388 public void printEventSummary( BufferedWriter bw,
393 String strIndent = "";
394 for( int i = 0; i < depth; ++i ) {
398 String strEventName = getEventName( es.eventID );
400 float tOfParent_perc;
401 String strPercParent = "";
402 if( es.parent != null ) {
403 float divisor = new Long( es.parent.totalTime_ticks ).floatValue();
404 if( divisor <= 0.00001f ) {
410 new Long( es.totalTime_ticks ).floatValue() /
413 strPercParent = String.format( " %%ofParent=%5.1f",
419 new Long( es.selfTime_ticks ).floatValue() /
420 new Long( es.totalTime_ticks ).floatValue();
422 String strSelfStats =
423 String.format( " total(ticks)=%12dK, %%self=%5.1f, count=%d",
424 es.totalTime_ticks/1000,
434 for( Iterator<EventSummary> itr = es.children.iterator();
437 EventSummary esChild = itr.next();
438 printEventSummary( bw, esChild, depth + 1 );