PrintFlatViewScopeVisitor.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.data.experiment.scope.visitors;
00002 
00003 import java.io.PrintStream;
00004 
00005 import edu.rice.cs.hpc.data.experiment.Experiment;
00006 import edu.rice.cs.hpc.data.experiment.metric.BaseMetric;
00007 import edu.rice.cs.hpc.data.experiment.metric.MetricValue;
00008 import edu.rice.cs.hpc.data.experiment.scope.AlienScope;
00009 import edu.rice.cs.hpc.data.experiment.scope.CallSiteScope;
00010 import edu.rice.cs.hpc.data.experiment.scope.FileScope;
00011 import edu.rice.cs.hpc.data.experiment.scope.GroupScope;
00012 import edu.rice.cs.hpc.data.experiment.scope.LineScope;
00013 import edu.rice.cs.hpc.data.experiment.scope.LoadModuleScope;
00014 import edu.rice.cs.hpc.data.experiment.scope.LoopScope;
00015 import edu.rice.cs.hpc.data.experiment.scope.ProcedureScope;
00016 import edu.rice.cs.hpc.data.experiment.scope.RootScope;
00017 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00018 import edu.rice.cs.hpc.data.experiment.scope.ScopeVisitType;
00019 import edu.rice.cs.hpc.data.experiment.scope.StatementRangeScope;
00020 import edu.rice.cs.hpc.data.experiment.source.SourceFile;
00021 import edu.rice.cs.hpc.data.experiment.xml.PrintFileXML;
00022 
00023 
00024 /****************************************************************************************
00025  * 
00026  * @author laksonoadhianto
00027  *
00028  ****************************************************************************************/
00029 public class PrintFlatViewScopeVisitor implements IScopeVisitor {
00030     static private StringBuffer indent;
00031 
00032     private Experiment objExperiment;
00033     private PrintStream objOutputStream;
00034 
00035     
00036     public PrintFlatViewScopeVisitor(Experiment experiment, PrintStream stream) {
00037         this.objExperiment = experiment;
00038         this.objOutputStream = stream;
00039         indent = new StringBuffer();
00040     }
00041     
00042     //----------------------------------------------------
00043     // visitor pattern instantiations for each Scope type
00044     //----------------------------------------------------
00045 
00046     public void visit(Scope scope, ScopeVisitType vt) { print(scope, "u", vt, false, false); }
00047     public void visit(RootScope scope, ScopeVisitType vt) { 
00048         if (vt == ScopeVisitType.PreVisit) printMetrics(scope); 
00049     }
00050     public void visit(LoadModuleScope scope, ScopeVisitType vt) { print(scope, "LM", vt, true, false); }
00051     public void visit(FileScope scope, ScopeVisitType vt) { print(scope, "F", vt, true, false); }
00052     public void visit(ProcedureScope scope, ScopeVisitType vt) { print(scope, "P", vt, true, true); }
00053     public void visit(AlienScope scope, ScopeVisitType vt) { print(scope, "A", vt, true, true); }
00054     public void visit(LoopScope scope, ScopeVisitType vt) { print(scope, "L", vt, false, true); }
00055     public void visit(LineScope scope, ScopeVisitType vt) { print(scope, "S", vt, false, true); }
00056     public void visit(StatementRangeScope scope, ScopeVisitType vt) { print(scope, "S", vt, false, true); }
00057     public void visit(CallSiteScope scope, ScopeVisitType vt) { printCallSite(scope, vt);   }
00058     public void visit(GroupScope scope, ScopeVisitType vt) { print(scope, "G", vt, false, true); }
00059 
00068     private void print(Scope scope, String initial, ScopeVisitType vt, 
00069             boolean name, boolean line) {
00070         if (vt == ScopeVisitType.PreVisit) {
00071             
00072             //--------------------------------------------------
00073             // print the scope tag, attributes and values
00074             //--------------------------------------------------
00075             Scope objScopeToPrint = scope;
00076             if ( (scope instanceof CallSiteScope) && initial.equals("PF") ) 
00077                 objScopeToPrint = ((CallSiteScope) scope).getProcedureScope();
00078             
00079             this.printScopeTag(objScopeToPrint, initial, name, line);
00080 
00081             //--------------------------------------------------
00082             // print the metric values of this scope, except callsite
00083             //--------------------------------------------------
00084             if (initial.charAt(0) != 'C')
00085                 this.printMetrics(scope);
00086             
00087             //--------------------------------------------------
00088             // increment the indentation for future usage
00089             //--------------------------------------------------
00090             indent.append(' ');
00091             
00092         } else {
00093             
00094             indent.deleteCharAt(0);
00095             this.objOutputStream.println(indent + "</" + initial + ">" );
00096             
00097         }
00098     }
00099     
00100     
00101     private void printCallSite( CallSiteScope scope, ScopeVisitType vt) {
00102         if (vt == ScopeVisitType.PreVisit)  {
00103             print(scope, "C", vt, false, true);
00104             //-------------------------------------------------------------------
00105             // a call site contains information of line scope AND procedure scope.
00106             // we need to generate both
00107             //-------------------------------------------------------------------
00108             print(scope, "PF", vt, true, true); 
00109 
00110         } else {
00111             print(scope, "PF", vt, true, false); 
00112             print(scope, "C", vt, true, false);
00113             
00114         }
00115     }
00116     
00124     private void printScopeTag(Scope objScopeToPrint, String initial, boolean name, boolean line) {
00125         this.objOutputStream.print(indent + "<" + initial);
00126         PrintFileXML.printAttribute(objOutputStream, "i", objScopeToPrint.hashCode());
00127         
00128         if (name) {
00129             final String scopeName;
00130             // if the scope is a file, we have to print the original path from hpcprof
00131             if (objScopeToPrint instanceof FileScope) 
00132             {
00133                 SourceFile srcFile = ((FileScope)objScopeToPrint).getSourceFile();
00134                 if (srcFile != null && srcFile.getFilename()!= null)
00135                     scopeName = srcFile.getFilename().toString();
00136                 else
00137                     scopeName = objScopeToPrint.getName();
00138             } else {
00139                 scopeName = objScopeToPrint.getName();
00140             }
00141             // 2010.12.17 Ashay addition:
00142             // Escape the "name" so that we conform to a valid XML syntax
00143             String sName = scopeName.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
00144             PrintFileXML.printAttribute(objOutputStream, "n", sName);
00145         }
00146         
00147         if (line) {
00148             Scope linescope = objScopeToPrint;
00149             if ( (objScopeToPrint instanceof CallSiteScope) && initial.equals("C")) {
00150                 // for call site, we just need to get the line number from its line scope
00151                 linescope = ((CallSiteScope) objScopeToPrint).getLineScope();
00152             }
00153             // the original data decrement the line number by 1,
00154             //  in return we need to increment again
00155             int line1 = linescope.getFirstLineNumber() + 1;
00156             int line2 = linescope.getLastLineNumber() + 1;
00157             if (line1 == line2)
00158                 PrintFileXML.printAttribute(objOutputStream, "l", line1);
00159             else 
00160                 PrintFileXML.printAttribute(objOutputStream, "l", line1 + "-" + line2);
00161         }
00162         
00163         if (objScopeToPrint instanceof AlienScope) {
00164             SourceFile objFile = objScopeToPrint.getSourceFile();
00165             if (objFile != null)
00166                 PrintFileXML.printAttribute(objOutputStream, "f", objFile.getFilename());
00167         }
00168         this.objOutputStream.println(">" );
00169     }
00170     
00171     /***-------------------------------------------------------------------------------**
00172      * 
00173      * @param scope
00174      ***-------------------------------------------------------------------------------**/
00175     private void printMetrics(Scope scope) {
00176 
00177         int nbMetrics = objExperiment.getMetricCount();
00178         for (int i=0; i<nbMetrics; i++) {
00179             MetricValue value = scope.getMetricValue(i);
00180             if (MetricValue.isAvailable(value)) {
00181                 BaseMetric m = objExperiment.getMetric(i);
00182                 this.objOutputStream.print(indent + "<M");
00183                 PrintFileXML.printAttribute(this.objOutputStream, "n", m.getIndex());
00184                 PrintFileXML.printAttribute(this.objOutputStream, "v", MetricValue.getValue(value));
00185                 this.objOutputStream.print("/>");
00186             }
00187         }
00188         this.objOutputStream.println();
00189     }
00190 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1