PrintFileXML.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.data.experiment.xml;
00002 
00003 import java.io.File;
00004 import java.io.FileInputStream;
00005 import java.io.FileNotFoundException;
00006 import java.io.IOException;
00007 import java.io.InputStream;
00008 import java.io.PrintStream;
00009 
00010 import edu.rice.cs.hpc.data.experiment.Experiment;
00011 import edu.rice.cs.hpc.data.experiment.metric.BaseMetric;
00012 import edu.rice.cs.hpc.data.experiment.metric.MetricType;
00013 import edu.rice.cs.hpc.data.experiment.scope.RootScope;
00014 import edu.rice.cs.hpc.data.experiment.scope.visitors.PrintFlatViewScopeVisitor;
00015 
00016 
00017 /***************************************************************************************
00018  * Class to print the content of an experiment database into an output stream
00019  * @author laksonoadhianto
00020  *
00021  ***************************************************************************************/
00022 public class PrintFileXML {
00023     //-----------------------------------------------------------------------
00024     // Constants
00025     //-----------------------------------------------------------------------
00026     final private String DTD_FILE_NAME = "experiment.dtd";
00027     final private int MAX_BUFFER = 1024;
00028 
00029     
00035     public void print(PrintStream objStream, Experiment experiment) {
00036         Object []rootChildren = experiment.getRootScopeChildren();
00037         if (rootChildren != null) {
00038 
00039             int nbChildren = rootChildren.length;
00040             
00041             // flat root must be the last one
00042             RootScope flatRoot = (RootScope) rootChildren[nbChildren-1];
00043             
00044             //---------------------------------------------------------------------------------
00045             // print the DTD
00046             //---------------------------------------------------------------------------------
00047             this.printDTD(objStream);
00048             
00049             //---------------------------------------------------------------------------------
00050             // print the header
00051             //---------------------------------------------------------------------------------
00052             this.printHeader(objStream, experiment);
00053             
00054             //---------------------------------------------------------------------------------
00055             // print the content
00056             //---------------------------------------------------------------------------------
00057             objStream.println("<SecFlatProfileData>");
00058             PrintFlatViewScopeVisitor objPrintFlat = new PrintFlatViewScopeVisitor(experiment, objStream);
00059             flatRoot.dfsVisitScopeTree(objPrintFlat);
00060             
00061             //---------------------------------------------------------------------------------
00062             // print the footer
00063             //---------------------------------------------------------------------------------
00064             objStream.println("</SecFlatProfileData>\n</SecFlatProfile>\n</HPCToolkitExperiment>");
00065         } else {
00066             System.err.println("The database contains no information");
00067         }
00068 
00069     }
00070     
00071     
00078     static public void printAttribute(PrintStream objPrint, String attribute, Object value) {
00079         objPrint.print(" "+ attribute + "=\"" + value + "\"");
00080     }
00081 
00082     
00088     private void printMetricTable(PrintStream objPrint, Experiment experiment) {
00089         objPrint.println(" <MetricTable>");
00090         BaseMetric metrics[] = experiment.getMetrics();
00091         for(int i=0; i<metrics.length; i++) {
00092             BaseMetric m = metrics[i];
00093             objPrint.print("    <Metric"); 
00094             {
00095                 printAttribute(objPrint, "i", m.getIndex());                
00096                 printAttribute(objPrint, "n", m.getDisplayName().trim());               
00097                 printAttribute(objPrint, "v", "final");             
00098                 printAttribute(objPrint, "t", getMetricType(m) );               
00099                 printAttribute(objPrint, "show", booleanToInt(m.getDisplayed()));
00100                 printAnnotationType(objPrint, m);
00101             }
00102             objPrint.print(">");
00103 
00104             objPrint.println(" </Metric>");
00105         }
00106         objPrint.println(" </MetricTable>");
00107     }
00108     
00109     
00115     private String getMetricType (BaseMetric m) {
00116         if (m.getMetricType() == MetricType.EXCLUSIVE )
00117             return "exclusive";
00118         else if (m.getMetricType() == MetricType.INCLUSIVE )
00119             return "inclusive";
00120         return "nil";
00121     }
00122     
00123     
00129     private void printAnnotationType (PrintStream objPrint, BaseMetric m) {
00130         
00131         switch (m.getAnnotationType()) {
00132         case PERCENT:
00133             printAttribute(objPrint, "show-percent", "1" );
00134             break;
00135         case PROCESS:
00136             printAttribute(objPrint, "show-process", "1" );
00137             break;
00138         }
00139     }
00140     
00141     
00147     private int booleanToInt(boolean b) {
00148         if (b)
00149             return 1;
00150         else
00151             return 0;
00152     }
00153     
00154     
00160     private void printHeader(PrintStream objPrint, Experiment experiment) {
00161         objPrint.println("<HPCToolkitExperiment version=\"" + experiment.getMajorVersion() + "\">");
00162 
00163         objPrint.print("<Header");
00164         PrintFileXML.printAttribute( objPrint, "n", experiment.getName() );
00165         objPrint.println(">\n  <Info/>\n</Header>");
00166         
00167         objPrint.print("<SecFlatProfile ");
00168         PrintFileXML.printAttribute( objPrint, "i", "0");
00169         PrintFileXML.printAttribute( objPrint, "n", experiment.getName() );
00170         objPrint.println(">\n<SecHeader>");
00171         
00172         this.printMetricTable(objPrint, experiment);
00173         
00174         objPrint.println("</SecHeader>");
00175     }
00176 
00177     
00184     private void printDTD(PrintStream objPrint) {
00185 
00186         //---------------------------------------------------------
00187         // finding and opeing DTD file (quite painful for jar file
00188         //---------------------------------------------------------
00189 
00190         String hpc_dir = System.getenv("HPCVIEWER_DIR_PATH");
00191         if (hpc_dir == null) return;
00192         
00193         InputStream objFile = null;
00194         File file = new File(hpc_dir + DTD_FILE_NAME);
00195         try {
00196             objFile = new FileInputStream(file);
00197         } catch (FileNotFoundException e) {
00198             // we don't need DTD. let's exit silently
00199             return;
00200         }
00201 
00202         byte[] buf=new byte[MAX_BUFFER];
00203         
00204         //---------------------------------------------------------
00205         // iteratively read DTD file and print partially to the stream
00206         //---------------------------------------------------------
00207         while (true) {
00208             int numRead = 0;
00209             try {
00210                 numRead = objFile.read(buf, 0, buf.length);
00211             } catch (IOException e) {
00212                 e.printStackTrace();
00213             }
00214             if (numRead <= 0) {
00215                 break;
00216             } else {
00217                 String dtd = new String(buf, 0, numRead);
00218                 objPrint.print(dtd);
00219             }
00220 
00221         }
00222         //---------------------------------------------------------
00223         // DTD has been printed, we need a new line to make nice format 
00224         //---------------------------------------------------------
00225         objPrint.println();
00226 
00227     }
00228     
00229 
00230 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1