update trace to dump out gnuplot plots
authorbdemsky <bdemsky>
Tue, 2 Nov 2010 14:47:20 +0000 (14:47 +0000)
committerbdemsky <bdemsky>
Tue, 2 Nov 2010 14:47:20 +0000 (14:47 +0000)
Robust/CoreProf/Trace.java

index 6665d0b1991c046520f1893396c423fc7ef7878f..d85b15a25cf3472cc3ba556d8c9f9e43ddaa3a7f 100644 (file)
@@ -69,21 +69,48 @@ public class Trace {
 
 
   public static void main( String args[] ) {
 
 
   public static void main( String args[] ) {
-    if( args.length < 2 ||
-        args.length > 3 ) {
+    if( args.length < 2 ) {
       System.out.println( "usage: <coreprof.dat file> <trace out file> [-2txt]\n"+
                           "\nThe -2txt option will take the raw binary events and spit\n"+
                           "out every event as text in events.txt, useful for debugging\n"+
                           "event mis-matches." );
       System.exit( 0 );
     }
       System.out.println( "usage: <coreprof.dat file> <trace out file> [-2txt]\n"+
                           "\nThe -2txt option will take the raw binary events and spit\n"+
                           "out every event as text in events.txt, useful for debugging\n"+
                           "event mis-matches." );
       System.exit( 0 );
     }
-
-    Trace t;
-    if( args[0].equals( "-2txt" ) ) {
-      t = new Trace( true,  args[1], args[2] );
-    } else {
-      t = new Trace( false, args[0], args[1] );
+    String inputfile=null;
+    String outputfile=null;
+    boolean events=false;
+    HashSet<Integer> eventset=new HashSet<Integer>();
+    boolean hasinput=false;
+    long maxtime=-1;
+    long mintime=0;
+    long scale=1;
+
+    for(int i=0; i<args.length; i++) {
+      if (args[i].equals("-2txt")) {
+       events=true;
+      } else if (args[i].equals("-event")) {
+       eventset.add(Integer.parseInt(args[i+1]));
+       i++;
+      } else if (args[i].equals("-maxtime")) {
+       maxtime=Long.parseLong(args[i+1]);
+       i++;
+      } else if (args[i].equals("-mintime")) {
+       mintime=Long.parseLong(args[i+1]);
+       i++;
+      } else if (args[i].equals("-scale")) {
+       scale=Long.parseLong(args[i+1]);
+       i++;
+      } else {
+       if (hasinput) {
+         outputfile=args[i];
+       } else {
+         hasinput=true;
+         inputfile=args[i];
+       }
+      }
     }
     }
+
+    Trace t = new Trace(events, inputfile, outputfile, eventset, mintime, maxtime,scale);
   }
 
 
   }
 
 
@@ -95,6 +122,7 @@ public class Trace {
 
   int          numThreads;
   ThreadData[] threadData;
 
   int          numThreads;
   ThreadData[] threadData;
+  HashSet<Integer> eventset;
 
   boolean        convert2txt;
   BufferedWriter txtStream;
 
   boolean        convert2txt;
   BufferedWriter txtStream;
@@ -102,12 +130,15 @@ public class Trace {
   boolean             convert2plot;
   BufferedWriter      bwPlot;
   long                tSysZero;
   boolean             convert2plot;
   BufferedWriter      bwPlot;
   long                tSysZero;
-  Hashtable<Integer, Integer> plottedEvents;
-
-
-
-  public Trace( boolean c2txt, String inFile, String outFile ) {
-
+  long maxtime;
+  long mintime;
+  long scale;
+
+  public Trace( boolean c2txt, String inFile, String outFile,  HashSet<Integer> eventset, long mintime, long maxtime, long scale) {
+    this.eventset=eventset;
+    this.maxtime=maxtime;
+    this.mintime=mintime;
+    this.scale=scale;
     convert2txt  = c2txt;
     convert2plot = true;
 
     convert2txt  = c2txt;
     convert2plot = true;
 
@@ -115,6 +146,10 @@ public class Trace {
 
     initNames();
 
 
     initNames();
 
+    if (convert2plot) {
+      printPlotCmd();
+    }
+
     for( int i = 0; i < numThreads; i++ ) {
       readThread( i );
     }
     for( int i = 0; i < numThreads; i++ ) {
       readThread( i );
     }
@@ -122,7 +157,12 @@ public class Trace {
     printStats( outFile );
 
     if( convert2plot ) {
     printStats( outFile );
 
     if( convert2plot ) {
-      printPlotCmd();
+      try {
+       bwPlot.write("plot [0:"+(globendtime/scale)+"] [-1:"+(numThreads+1)+"] -3\n");
+       bwPlot.close();
+      } catch (IOException e) {
+       e.printStackTrace();
+      }
     }
   }
 
     }
   }
 
@@ -199,6 +239,14 @@ public class Trace {
        while( skip > 0 ) {
          skip -= threadData[i].dataStream.skip( skip );
        }
        while( skip > 0 ) {
          skip -= threadData[i].dataStream.skip( skip );
        }
+       threadData[i].dataStream.mark(20);
+       int eventRaw = readInt ( threadData[i].dataStream );
+       long Stamp    = readLong( threadData[i].dataStream );
+       threadData[i].dataStream.reset();
+       
+       if (globalstarttime==-1||globalstarttime>Stamp)
+         globalstarttime=Stamp;
+
 
        offset += WORD_SIZE*threadData[i].numDataWords;
 
 
        offset += WORD_SIZE*threadData[i].numDataWords;
 
@@ -253,17 +301,6 @@ public class Trace {
       }
     }
 
       }
     }
 
-    if( convert2plot ) {
-      try {
-        bwPlot = new BufferedWriter( new FileWriter( "plot-t"+tNum+".dat" ) );
-      } catch( IOException e ) {
-        e.printStackTrace();
-        System.exit( -1 );
-      }
-
-      plottedEvents = new Hashtable<Integer, Integer>();
-    }
-
 
     ThreadData tdata = threadData[tNum];
     tdata.stackDepth = 0;
 
     ThreadData tdata = threadData[tNum];
     tdata.stackDepth = 0;
@@ -279,6 +316,7 @@ public class Trace {
     }
     j = 0;
     
     }
     j = 0;
     
+    long lasttime=-1;
     while( i < tdata.numDataWords ) {
       
       if( !progress[j] && i > j*progressChunk ) {
     while( i < tdata.numDataWords ) {
       
       if( !progress[j] && i > j*progressChunk ) {
@@ -296,25 +334,33 @@ public class Trace {
       int eventType = eventRaw &  CP_EVENT_MASK;
       int eventID   = eventRaw >> CP_EVENT_BASESHIFT;
 
       int eventType = eventRaw &  CP_EVENT_MASK;
       int eventID   = eventRaw >> CP_EVENT_BASESHIFT;
 
-      switch( eventType ) {
+      if (eventset.isEmpty()||eventset.contains(eventID))
+       switch( eventType ) {
 
         case CP_EVENTTYPE_BEGIN: {
           if( eventID == CP_EVENTID_MAIN ) {
             tSysZero = timeStamp;
           }
 
         case CP_EVENTTYPE_BEGIN: {
           if( eventID == CP_EVENTID_MAIN ) {
             tSysZero = timeStamp;
           }
+         if( tdata.stackDepth>0 && convert2plot ) {
+           EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
+           long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
+           addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
+         }
 
           pushEvent( tdata, eventID, timeStamp );          
         } break;
 
         case CP_EVENTTYPE_END: {
 
           pushEvent( tdata, eventID, timeStamp );          
         } break;
 
         case CP_EVENTTYPE_END: {
+         if( convert2plot ) {
+           EventSummary esParent = tdata.eventStack.get( tdata.stackDepth - 1 );
+           long starttime=lasttime>esParent.timeStampBeginLatestInstance?lasttime:esParent.timeStampBeginLatestInstance;
+
+           addPointToPlot( tNum, esParent.eventID, starttime, timeStamp);
+           lasttime=timeStamp;
+         }
           popEvent( tdata, eventID, timeStamp );
         } break;    
           popEvent( tdata, eventID, timeStamp );
         } break;    
-    
-      }
-
-      if( convert2plot ) {
-        addPointToPlot( timeStamp, eventID );
-      }
+       }
     }
 
     System.out.println( "" );
     }
 
     System.out.println( "" );
@@ -323,7 +369,7 @@ public class Trace {
       // worker threads currently do not exit gracefully, and therefore
       // may not register END events, so supply them with whatever the
       // latest known timestamp is
       // worker threads currently do not exit gracefully, and therefore
       // may not register END events, so supply them with whatever the
       // latest known timestamp is
-      EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth );
+      EventSummary eventSummary = tdata.eventStack.get( tdata.stackDepth);
 
       if( eventSummary == null ) {
         // if there is no previous event it means there are no children
 
       if( eventSummary == null ) {
         // if there is no previous event it means there are no children
@@ -336,15 +382,6 @@ public class Trace {
       --tdata.stackDepth;
     }
 
       --tdata.stackDepth;
     }
 
-
-    if( convert2plot ) {
-      try {
-        bwPlot.close();
-      } catch( IOException e ) {
-        e.printStackTrace();
-        System.exit( -1 );
-      }
-    }
   }
 
 
   }
 
 
@@ -473,18 +510,20 @@ public class Trace {
     }
   }
 
     }
   }
 
+  long globalstarttime=-1;
+  long globendtime=-1;
 
 
-  public void addPointToPlot( long timeStamp, int eventID ) {
-
-    if( !plottedEvents.containsKey( eventID ) ) {
-      plottedEvents.put( eventID, plottedEvents.size() );
-    }
-
+  public void addPointToPlot( int thread, int eventID, long starttime, long endtime) {
     try {
     try {
-      bwPlot.write( (timeStamp - tSysZero)+" "+
-                    plottedEvents.get( eventID )+" "+
-                    getEventName( eventID )+"\n"
-                    );
+      long nstart=starttime-globalstarttime-mintime;
+      long nend=endtime-globalstarttime-mintime;
+      if ((maxtime==-1 || (endtime-globalstarttime)<maxtime)&&(nend>0)) {
+       if (nend>globendtime)
+         globendtime=nend;
+       if (nstart<0)
+         nstart=0;
+       bwPlot.write( "set arrow from "+(nstart/scale)+","+thread+" to "+(nend/scale)+","+thread+" lt "+eventID+" nohead\n");
+      }
     } catch( IOException e ) {
       e.printStackTrace();
       System.exit( -1 );
     } catch( IOException e ) {
       e.printStackTrace();
       System.exit( -1 );
@@ -592,22 +631,8 @@ public class Trace {
 
   public void printPlotCmd() {
     try {
 
   public void printPlotCmd() {
     try {
-      BufferedWriter bw = 
+      bwPlot = 
         new BufferedWriter( new FileWriter( "plot.cmd" ) );
         new BufferedWriter( new FileWriter( "plot.cmd" ) );
-
-      bw.write( "set ytics\n" );
-      bw.write( "plot \\\n" );
-
-      for( int i = 0; i < numThreads; ++i ) {
-        bw.write( "\"plot-t"+i+".dat\" using 1:2:yticlabels(3) "+
-                  "title 't"+i+"' with linespoints" );
-        if( i != numThreads - 1 ) {
-          bw.write( ", \\" );
-        }
-        bw.write( "\n" );
-      }      
-
-      bw.close();
     } catch( IOException e ) {
       e.printStackTrace();
       System.exit( -1 );
     } catch( IOException e ) {
       e.printStackTrace();
       System.exit( -1 );