00001
00002
00003
00004
00005
00006
00007
00008
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
00030
00038 public class Experiment extends BaseExperimentWithMetrics
00039 {
00040
00041 private MetricRaw[] metrics_raw;
00042 private boolean need_caller_tree;
00043
00044
00046
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
00062 @Override
00063
00064
00065
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
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
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
00109
00110
00111
00112
00113
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
00125
00126
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
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 callersViewRootScope.accumulateMetrics(callingContextViewRootScope, filter, this.getMetricCount());
00149
00150 AbstractFinalizeMetricVisitor diVisitor = new FinalizeMetricVisitorWithBackup(this.getMetrics());
00151 this.finalizeAggregateMetrics(callersViewRootScope, diVisitor);
00152
00153
00154 addPercents(callersViewRootScope, callersViewRootScope);
00155
00156 return callersViewRootScope;
00157 }
00158
00159
00160
00161
00162
00163
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
00197 if (metric instanceof Metric) {
00198 if (metric.getMetricType() == sourceType) {
00199
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
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
00228
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
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
00265 Scope callingContextViewRootScope = firstSubTree;
00266
00267 EmptyMetricValuePropagationFilter emptyFilter = new EmptyMetricValuePropagationFilter();
00268 InclusiveOnlyMetricPropagationFilter rootInclProp = new InclusiveOnlyMetricPropagationFilter(this);
00269
00270
00271
00272
00273 if (this.inclusiveNeeded()) {
00274
00275 addInclusiveMetrics(callingContextViewRootScope, rootInclProp);
00276 this.computeExclusiveMetrics(callingContextViewRootScope);
00277 }
00278
00279 copyMetricsToPartner(callingContextViewRootScope, MetricType.INCLUSIVE, emptyFilter);
00280
00281
00282
00283
00284 if (callerView) {
00285 prepareCallersView(callingContextViewRootScope);
00286 }
00287
00288
00289
00290
00291 Scope flatViewRootScope = null;
00292
00293
00294 flatViewRootScope = createFlatView(callingContextViewRootScope);
00295
00296
00297
00298
00299 AbstractFinalizeMetricVisitor diVisitor = new FinalizeMetricVisitor(this.getMetrics());
00300
00301 this.finalizeAggregateMetrics(flatViewRootScope, diVisitor);
00302
00303 diVisitor = new FinalizeMetricVisitorWithBackup(this.getMetrics());
00304
00305 this.finalizeAggregateMetrics(callingContextViewRootScope, diVisitor);
00306
00307
00308
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
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
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) );
00363 }
00364 return isNeeded;
00365 }
00367
00369
00376 public DerivedMetric addDerivedMetric(DerivedMetric objMetric) {
00377
00378 this.metrics.add(objMetric);
00379
00380 int iInclusive = this.getMetricCount() - 1;
00381 int iExclusive = -1;
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
00427 Scope root = getRootScope();
00428 root.remove(1);
00429 root.remove(1);
00430
00431
00432
00433
00434
00435
00436
00437 prepareCallersView(rootCCT);
00438
00439
00440
00441
00442 Scope flatViewRootScope = null;
00443
00444
00445 flatViewRootScope = createFlatView(rootCCT);
00446
00447
00448
00449
00450 AbstractFinalizeMetricVisitor diVisitor = new FinalizeMetricVisitor(this.getMetrics());
00451
00452 this.finalizeAggregateMetrics(flatViewRootScope, diVisitor);
00453 addPercents(flatViewRootScope, (RootScope) rootCCT);
00454 }
00455
00456 @Override
00457 protected void open_finalize() {
00458 postprocess(need_caller_tree);
00459 }
00460 }