CallersViewScopeVisitor.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.data.experiment.scope.visitors;
00002
00003 import java.util.ArrayList;
00004 import java.util.Hashtable;
00005 import java.util.Iterator;
00006 import java.util.LinkedList;
00007 import java.util.Stack;
00008
00009 import edu.rice.cs.hpc.data.experiment.Experiment;
00010 import edu.rice.cs.hpc.data.experiment.metric.*;
00011 import edu.rice.cs.hpc.data.experiment.scope.*;
00012 import edu.rice.cs.hpc.data.experiment.scope.filters.ExclusiveOnlyMetricPropagationFilter;
00013 import edu.rice.cs.hpc.data.experiment.scope.filters.InclusiveOnlyMetricPropagationFilter;
00014 import edu.rice.cs.hpc.data.experiment.scope.filters.MetricValuePropagationFilter;
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 public class CallersViewScopeVisitor extends CallerScopeBuilder implements IScopeVisitor {
00025
00026
00027
00028
00031 static private final String HiddenProcedures = "Partial Call Paths";
00032
00033
00034
00035
00036 private final CombineCallerScopeMetric combinedMetrics;
00037
00038 private final ExclusiveOnlyMetricPropagationFilter exclusiveOnly;
00039 private final InclusiveOnlyMetricPropagationFilter inclusiveOnly;
00040
00041 final private ListCombinedScopes listCombinedScopes;
00042
00043 private Hashtable<Integer, Scope> calleeht = new Hashtable<Integer, Scope>();
00044
00045 private Scope callersViewRootScope;
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 public CallersViewScopeVisitor(Experiment experiment, Scope cvrs,
00056 int nMetrics, boolean dodebug, MetricValuePropagationFilter filter) {
00057 this.callersViewRootScope = cvrs;
00058 exclusiveOnly = new ExclusiveOnlyMetricPropagationFilter(experiment);
00059 inclusiveOnly = new InclusiveOnlyMetricPropagationFilter(experiment);
00060 combinedMetrics = new CombineCallerScopeMetric();
00061
00062 listCombinedScopes = new ListCombinedScopes();
00063
00064 }
00065
00066
00067
00068
00069
00070 public void visit(CallSiteScope scope, ScopeVisitType vt) {
00071
00072
00073
00074
00075 if (!scope.hasNonzeroMetrics()) {
00076 return;
00077 }
00078
00079 if (vt == ScopeVisitType.PreVisit) {
00080 this.listCombinedScopes.push();
00081
00082
00083 ProcedureScope callee = this.createProcedureIfNecessary(scope);
00084
00085 prepareCallChain(scope, callee);
00086
00087 } else if (vt == ScopeVisitType.PostVisit) {
00088
00089 this.decrementCounter();
00090 }
00091 }
00092
00093
00094 public void visit(Scope scope, ScopeVisitType vt) { }
00095 public void visit(RootScope scope, ScopeVisitType vt) { }
00096 public void visit(LoadModuleScope scope, ScopeVisitType vt) { }
00097 public void visit(FileScope scope, ScopeVisitType vt) { }
00098
00099 public void visit(ProcedureScope scope, ScopeVisitType vt) {
00100
00101
00102
00103
00104 if (!scope.hasNonzeroMetrics() || scope.getName().equals(HiddenProcedures)) {
00105 return;
00106 }
00107
00108 if (vt == ScopeVisitType.PreVisit) {
00109 this.listCombinedScopes.push();
00110
00111 ProcedureScope callee = this.createProcedureIfNecessary(scope);
00112 if (scope.isAlien()) {
00113 prepareCallChain(scope, callee);
00114 }
00115
00116 } else if (vt == ScopeVisitType.PostVisit){
00117
00118 this.decrementCounter();
00119 }
00120
00121 }
00122
00123 public void visit(AlienScope scope, ScopeVisitType vt) { }
00124 public void visit(LoopScope scope, ScopeVisitType vt) { }
00125 public void visit(StatementRangeScope scope, ScopeVisitType vt) { }
00126 public void visit(LineScope scope, ScopeVisitType vt) { }
00127 public void visit(GroupScope scope, ScopeVisitType vt) { }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 private void prepareCallChain(Scope scope, ProcedureScope callee) {
00142 LinkedList<CallSiteScopeCallerView> callPathList = createCallChain(scope, scope,
00143 combinedMetrics, this.inclusiveOnly, this.exclusiveOnly);
00144
00145
00146
00147
00148 mergeCallerPath(IMergedScope.MergingStatus.INIT, 0, callee, callPathList,
00149 combinedMetrics, this.inclusiveOnly, this.exclusiveOnly);
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 private ProcedureScope createProcedureIfNecessary( Scope cct_s ) {
00159 ProcedureScope cct_proc_s;
00160
00161 if (cct_s instanceof ProcedureScope)
00162 cct_proc_s = (ProcedureScope) cct_s;
00163 else
00164 cct_proc_s = ( (CallSiteScope)cct_s).getProcedureScope();
00165
00166 Integer objCode = Integer.valueOf(cct_proc_s.hashCode());
00167
00168 ProcedureScope caller_proc = (ProcedureScope) calleeht.get(objCode);
00169
00170 if (caller_proc == null) {
00171
00172 caller_proc = (ProcedureScope) cct_proc_s.duplicate();
00173
00174
00175 callersViewRootScope.addSubscope(caller_proc);
00176 caller_proc.setParentScope(this.callersViewRootScope);
00177
00178
00179 calleeht.put(objCode, caller_proc);
00180 }
00181
00182
00183 this.combinedMetrics.combine(caller_proc, cct_s, inclusiveOnly, exclusiveOnly);
00184 return caller_proc;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 private void decrementCounter() {
00194
00195
00196
00197
00198
00199
00200 ArrayList<Scope> list = this.listCombinedScopes.pop();
00201 if (list != null) {
00202 Iterator<Scope> iter = list.iterator();
00203 while (iter.hasNext()) {
00204 Scope combinedScope = iter.next();
00205 combinedScope.decrementCounter();
00206 }
00207 }
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 private class ListCombinedScopes {
00223 private Stack<ArrayList<Scope>> combinedScopes;
00224
00225 public ArrayList<Scope> push() {
00226 if (this.combinedScopes == null) {
00227 this.combinedScopes = new Stack< ArrayList<Scope>>();
00228 }
00229 ArrayList<Scope> list = new ArrayList<Scope>();
00230 this.combinedScopes.push(list);
00231 return list;
00232 }
00233
00234 public ArrayList<Scope> pop() {
00235 return this.combinedScopes.pop();
00236 }
00237
00238 public void addList(Scope combined) {
00239 ArrayList<Scope> list = this.combinedScopes.peek();
00240 list.add(combined);
00241 }
00242
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 private class CombineCallerScopeMetric extends AbstractCombineMetric {
00252
00253 public void combine(Scope target, Scope source,
00254 MetricValuePropagationFilter inclusiveOnly,
00255 MetricValuePropagationFilter exclusiveOnly) {
00256
00257 if (target.isCounterZero() && inclusiveOnly != null) {
00258 target.safeCombine(source, inclusiveOnly);
00259 }
00260 if (exclusiveOnly != null)
00261 target.combine(source, exclusiveOnly);
00262
00263 target.incrementCounter();
00264
00265 listCombinedScopes.addList(target);
00266
00267 }
00268 }
00269
00270
00271 }