Experiment.java

Go to the documentation of this file.
00001 
00002 //                                                                      //
00003 //  Experiment.java                                                     //
00004 //                                                                      //
00005 //  experiment.Experiment -- an open HPCView experiment                 //
00006 //  Last edited: January 15, 2002 at 12:37 am                           //
00007 //                                                                      //
00008 //  (c) Copyright 2002 Rice University. All rights reserved.            //
00009 //                                                                      //
00011 
00012 
00013 
00014 
00015 package edu.rice.cs.hpc.data.experiment;
00016 
00017 
00018 import edu.rice.cs.hpc.data.experiment.metric.*;
00019 import edu.rice.cs.hpc.data.experiment.scope.*;
00020 import edu.rice.cs.hpc.data.experiment.scope.filters.*;
00021 import edu.rice.cs.hpc.data.experiment.scope.visitors.*;
00022 import edu.rice.cs.hpc.data.filter.IFilterData;
00023 import edu.rice.cs.hpc.data.util.IUserData;
00024 
00025 import java.io.File;
00026 
00028 //  CLASS EXPERIMENT                                                    //
00030 
00038 public class Experiment extends BaseExperimentWithMetrics
00039 {
00040     // thread level database
00041     private MetricRaw[] metrics_raw;
00042     private boolean need_caller_tree;
00043 
00044 
00046     //  PERSISTENCE                                                         //
00048 
00049 
00050 
00051     public int getMajorVersion()
00052     {
00053         if (this.version == null)
00054             return 1;
00055         int ip = this.version.indexOf('.');
00056         return Integer.parseInt(this.version.substring(0, ip));
00057     }
00058 
00060     // File opening                                                         //
00062     @Override
00063     /*
00064      * (non-Javadoc)
00065      * @see edu.rice.cs.hpc.data.experiment.BaseExperiment#open(java.io.File, edu.rice.cs.hpc.data.util.IUserData, boolean)
00066      */
00067     public void open(File fileExperiment, IUserData<String, String> userData, boolean need_caller_tree)
00068             throws  Exception
00069     {
00070         this.need_caller_tree = need_caller_tree;
00071         super.open(fileExperiment, userData, true);
00072     }
00073 
00075     // Postprocessing                                                       //
00077     protected void accumulateMetricsFromKids(Scope target, Scope source, MetricValuePropagationFilter filter) {
00078         int nkids = source.getSubscopeCount();
00079         for (int i = 0; i < nkids; i++) {
00080             Scope child = source.getSubscope(i);
00081             if (child instanceof LoopScope) {
00082                 accumulateMetricsFromKids(target, child, filter);
00083             }
00084             target.accumulateMetrics(child, filter, this.getMetricCount());
00085         }
00086     }
00087 
00088     protected void copyMetric(Scope target, Scope source, int src_i, int targ_i, MetricValuePropagationFilter filter) {
00089         if (filter.doPropagation(source, target, src_i, targ_i)) {
00090             MetricValue mv = source.getMetricValue(src_i);
00091             if (mv != MetricValue.NONE && MetricValue.getValue(mv) != 0.0) {
00092                 target.setMetricValue(targ_i, mv);
00093             }
00094         }
00095     }
00096 
00097     /*************************************************************************
00098      *  Adds a new scope subtree to the scope tree (& scope list)
00099      ************************************************************************/
00100     public void beginScope(Scope scope)
00101     {
00102         Scope top = this.getRootScope();
00103         top.addSubscope(scope);
00104         scope.setParentScope(top);
00105     }
00106 
00107     /***
00108      * Preparing the tree for caller view. Since we will create the tree dynamically,
00109      *  we need to create at least the root. All the children will be created by
00110      *  createCallersView() method.
00111      * 
00112      * @param callingContextViewRootScope
00113      * @return
00114      */
00115     protected RootScope prepareCallersView(Scope callingContextViewRootScope)
00116     {
00117         RootScope callersViewRootScope = new RootScope(this, "Callers View", RootScopeType.CallerTree);
00118         beginScope(callersViewRootScope);
00119         
00120         return createCallersView(callingContextViewRootScope, callersViewRootScope);
00121     }
00122     
00123     /***
00124      * create callers view
00125      * @param callingContextViewRootScope
00126      * @return
00127      */
00128     public RootScope createCallersView(Scope callingContextViewRootScope, RootScope callersViewRootScope)
00129     {
00130         EmptyMetricValuePropagationFilter filter = new EmptyMetricValuePropagationFilter();
00131 
00132         CallersViewScopeVisitor csv = new CallersViewScopeVisitor(this, callersViewRootScope, 
00133                 this.getMetricCount(), false, filter);
00134         callingContextViewRootScope.dfsVisitScopeTree(csv);
00135 
00136         // --------------------------------
00137         // compute the aggregate metrics
00138         // --------------------------------
00139 
00140         // first, reset the values for the root in callers tree. 
00141         // Callers tree is special since we can have the root, but its children are created dynamically.
00142         // For the case of derived metric, the root may already have the value, so we need to reset it again
00143         //  before we "accumulate" from the CCT
00144         //callersViewRootScope.resetMetricValues();
00145         
00146         // bug fix 2008.10.21 : we don't need to recompute the aggregate metrics here. Just copy it from the CCT
00147         //  This will solve the problem where there is only nested loops in the programs
00148         callersViewRootScope.accumulateMetrics(callingContextViewRootScope, filter, this.getMetricCount());
00149         
00150         AbstractFinalizeMetricVisitor diVisitor = new FinalizeMetricVisitorWithBackup(this.getMetrics());
00151         this.finalizeAggregateMetrics(callersViewRootScope, diVisitor);
00152         
00153         // bug fix 2010.06.17: move the percent after finalization
00154         addPercents(callersViewRootScope, callersViewRootScope);
00155 
00156         return callersViewRootScope;
00157     }
00158 
00159     /***
00160      * Create a flat tree
00161      * 
00162      * @param callingContextViewRootScope : the original CCT 
00163      * @return the root scope of flat tree
00164      */
00165     private Scope createFlatView(Scope callingContextViewRootScope)
00166     {
00167         Scope flatViewRootScope = new RootScope(this, "Flat View", RootScopeType.Flat);
00168         beginScope(flatViewRootScope);
00169 
00170         FlatViewScopeVisitor fv = new FlatViewScopeVisitor(this, (RootScope) flatViewRootScope);
00171 
00172         callingContextViewRootScope.dfsVisitScopeTree(fv);
00173 
00174         EmptyMetricValuePropagationFilter filter = new EmptyMetricValuePropagationFilter();
00175         flatViewRootScope.accumulateMetrics(callingContextViewRootScope, filter , getMetricCount());
00176 
00177         return flatViewRootScope;
00178     }
00179 
00180 
00181 
00182     protected void addInclusiveMetrics(Scope scope, MetricValuePropagationFilter filter)
00183     {
00184         InclusiveMetricsScopeVisitor isv = new InclusiveMetricsScopeVisitor(this, filter);
00185         scope.dfsVisitScopeTree(isv);
00186     }
00187 
00188     private void computeExclusiveMetrics(Scope scope) {
00189         ExclusiveCallingContextVisitor visitor = new ExclusiveCallingContextVisitor(this);
00190         scope.dfsVisitScopeTree(visitor);
00191     }
00192 
00193     protected void copyMetricsToPartner(Scope scope, MetricType sourceType, MetricValuePropagationFilter filter) {
00194         for (int i = 0; i< this.getMetricCount(); i++) {
00195             BaseMetric metric = this.getMetric(i);
00196             // Laksono 2009.12.11: aggregate metrc doesn't have partner
00197             if (metric instanceof Metric) {
00198                 if (metric.getMetricType() == sourceType) {
00199                     // laksono hack bug fix: the partner is "always" the next metric
00200                     int partner = i+1; 
00201                     copyMetric(scope, scope, i, partner, filter);
00202                 }
00203             } else if (metric instanceof AggregateMetric) {
00204                 if (metric.getMetricType() == MetricType.EXCLUSIVE ) {
00205                     int partner = ((AggregateMetric)metric).getPartner();
00206                     String partner_id = String.valueOf(partner);
00207                     BaseMetric partner_metric = this.getMetric( partner_id );
00208                     // case for old database: no partner information
00209                     if (partner_metric != null) {
00210                         MetricValue partner_value = scope.getMetricValue( partner_metric );
00211                         scope.setMetricValue( i, partner_value);
00212                     }
00213                 }
00214             }
00215         }
00216     }
00217 
00218     protected void addPercents(Scope scope, RootScope totalScope)
00219     {   
00220         PercentScopeVisitor psv = new PercentScopeVisitor(this.getMetricCount(), totalScope);
00221         scope.dfsVisitScopeTree(psv);
00222     }
00223 
00224 
00225 
00226     /*****
00227      * return a tree root
00228      * @return
00229      */
00230     public RootScope getCallerTreeRoot() {
00231 
00232         for (Object node: getRootScope().getChildren()) {
00233             Scope scope = (Scope) node;
00234             if ( (scope instanceof RootScope) && 
00235                     ((RootScope)scope).getType()==RootScopeType.CallerTree )
00236                 return (RootScope) scope;
00237         }
00238 
00239         return null;
00240     }
00241 
00256     private void postprocess(boolean callerView) {
00257         if (this.rootScope.getSubscopeCount() <= 0) return;
00258         // Get first scope subtree: CCT or Flat
00259         Scope firstSubTree = this.rootScope.getSubscope(0);
00260         if (!(firstSubTree instanceof RootScope)) return;
00261         RootScopeType firstRootType = ((RootScope)firstSubTree).getType();
00262 
00263         if (firstRootType.equals(RootScopeType.CallingContextTree)) {
00264             // accumulate, create views, percents, etc
00265             Scope callingContextViewRootScope = firstSubTree;
00266 
00267             EmptyMetricValuePropagationFilter emptyFilter = new EmptyMetricValuePropagationFilter();
00268             InclusiveOnlyMetricPropagationFilter rootInclProp = new InclusiveOnlyMetricPropagationFilter(this);
00269 
00270             //----------------------------------------------------------------------------------------------
00271             // Inclusive metrics
00272             //----------------------------------------------------------------------------------------------
00273             if (this.inclusiveNeeded()) {
00274                 // TODO: if the metric is a derived metric then DO NOT do this process !
00275                 addInclusiveMetrics(callingContextViewRootScope, rootInclProp);
00276                 this.computeExclusiveMetrics(callingContextViewRootScope);
00277             }
00278 
00279             copyMetricsToPartner(callingContextViewRootScope, MetricType.INCLUSIVE, emptyFilter);
00280 
00281             //----------------------------------------------------------------------------------------------
00282             // Callers View
00283             //----------------------------------------------------------------------------------------------
00284             if (callerView) {
00285                 prepareCallersView(callingContextViewRootScope);
00286             }
00287 
00288             //----------------------------------------------------------------------------------------------
00289             // Flat View
00290             //----------------------------------------------------------------------------------------------
00291             Scope flatViewRootScope = null;
00292             // While creating the flat tree, we attribute the cost for procedure scopes
00293             // One the tree has been created, we compute the inclusive cost for other scopes
00294             flatViewRootScope = createFlatView(callingContextViewRootScope);
00295 
00296             //----------------------------------------------------------------------------------------------
00297             // FINALIZATION
00298             //----------------------------------------------------------------------------------------------
00299             AbstractFinalizeMetricVisitor diVisitor = new FinalizeMetricVisitor(this.getMetrics());
00300 
00301             this.finalizeAggregateMetrics(flatViewRootScope, diVisitor);    // flat view
00302 
00303             diVisitor = new FinalizeMetricVisitorWithBackup(this.getMetrics());
00304 
00305             this.finalizeAggregateMetrics(callingContextViewRootScope, diVisitor);      // cct
00306 
00307             //----------------------------------------------------------------------------------------------
00308             // Laks 2008.06.16: adjusting the percent based on the aggregate value in the calling context
00309             //----------------------------------------------------------------------------------------------
00310             addPercents(callingContextViewRootScope, (RootScope) callingContextViewRootScope);
00311             addPercents(flatViewRootScope, (RootScope) callingContextViewRootScope);
00312 
00313         } else if (firstRootType.equals(RootScopeType.Flat)) {
00314             addPercents(firstSubTree, (RootScope) firstSubTree);
00315         } else {
00316             // ignore; do no nothing
00317         }
00318     }
00319 
00325     private boolean checkExistenceOfDerivedIncr() {
00326         boolean isAggregate = false;
00327         for (int i=0; i<this.getMetricCount(); i++) {
00328             BaseMetric metric = this.getMetric(i);
00329             boolean is_aggregate = (metric instanceof AggregateMetric); 
00330             if (is_aggregate) {
00331                 isAggregate |= is_aggregate;
00332                 AggregateMetric aggMetric = (AggregateMetric) metric;
00333                 // TODO hack: initialize the metric here
00334                 aggMetric.init(this);
00335             }
00336         }
00337         return isAggregate;
00338     }
00339 
00340 
00346     private void finalizeAggregateMetrics(Scope root, AbstractFinalizeMetricVisitor diVisitor) {
00347         if (! checkExistenceOfDerivedIncr())
00348             return;
00349         root.dfsVisitScopeTree(diVisitor);
00350     }
00351 
00352 
00358     private boolean inclusiveNeeded() {
00359         boolean isNeeded = false;
00360         for (int i=0; !isNeeded && i<this.getMetricCount(); i++) {
00361             BaseMetric m = this.getMetric(i);
00362             isNeeded = !( (m instanceof FinalMetric) || (m instanceof AggregateMetric) );//.getMetricType() != MetricType.PREAGGREGATE;
00363         }
00364         return isNeeded;
00365     }
00367     //Compute Derived Metrics                                               //
00369 
00376     public DerivedMetric addDerivedMetric(DerivedMetric objMetric) {
00377 
00378         this.metrics.add(objMetric);
00379 
00380         int iInclusive = this.getMetricCount() - 1;
00381         int iExclusive = -1;        // at the moment we do not support exclusive/inclusive derived metric
00382 
00383         InclusiveOnlyMetricPropagationFilter rootInclProp = new InclusiveOnlyMetricPropagationFilter(this);
00384 
00385         for (int i=0; i<this.rootScope.getSubscopeCount(); i++) {
00386             RootScope rootScope = (RootScope) this.rootScope.getSubscope(i);
00387 
00388             DerivedMetricVisitor csv = new DerivedMetricVisitor(this, rootInclProp, iInclusive, iExclusive );
00389             rootScope.dfsVisitScopeTree(csv);
00390 
00391         }
00392 
00393         return objMetric;
00394     }
00395 
00396 
00397 
00398     public Experiment duplicate() {
00399 
00400         Experiment copy     = new Experiment();
00401         copy.configuration  = configuration;
00402         copy.databaseRepresentation = databaseRepresentation;
00403         
00404         return copy;
00405     }
00406 
00407 
00408     public void setXMLExperimentFile(File file) {
00409         databaseRepresentation.getXMLFile().setFile(file);
00410     }
00411 
00412     public void setMetricRaw(MetricRaw []metrics) {
00413         this.metrics_raw = metrics;
00414     }
00415 
00416 
00417     public MetricRaw[] getMetricRaw() {
00418         return this.metrics_raw;
00419     }
00420 
00421 
00422 
00423     @Override
00424     protected void filter_finalize(RootScope rootCCT, IFilterData filter) 
00425     {
00426         // removing the original root for caller tree and flat tree
00427         Scope root = getRootScope();
00428         root.remove(1);
00429         root.remove(1);
00430         
00431         //------------------------------------------------------------------------------------------
00432         // filtering callers tree (bottom-up):
00433         // check if the callers tree has been created or not. If it's been created,
00434         // we need to remove it and create a new filter.
00435         // otherwise, we do nothing, and let the viewer to create dynamically
00436         //------------------------------------------------------------------------------------------
00437         prepareCallersView(rootCCT);
00438         
00439         //------------------------------------------------------------------------------------------
00440         // creating the flat tree from the filtered cct tree
00441         //------------------------------------------------------------------------------------------
00442         Scope flatViewRootScope = null;
00443         // While creating the flat tree, we attribute the cost for procedure scopes
00444         // One the tree has been created, we compute the inclusive cost for other scopes
00445         flatViewRootScope = createFlatView(rootCCT);
00446 
00447         //------------------------------------------------------------------------------------------
00448         // FINALIZATION
00449         //------------------------------------------------------------------------------------------
00450         AbstractFinalizeMetricVisitor diVisitor = new FinalizeMetricVisitor(this.getMetrics());
00451 
00452         this.finalizeAggregateMetrics(flatViewRootScope, diVisitor);    // flat view
00453         addPercents(flatViewRootScope, (RootScope) rootCCT);
00454     }
00455 
00456     @Override
00457     protected void open_finalize() {
00458         postprocess(need_caller_tree);      
00459     }
00460 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1