00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012
00013
00014
00015 package edu.rice.cs.hpc.data.experiment.scope;
00016
00017
00018 import java.util.Iterator;
00019
00020 import edu.rice.cs.hpc.data.experiment.BaseExperiment;
00021 import edu.rice.cs.hpc.data.experiment.BaseExperimentWithMetrics;
00022 import edu.rice.cs.hpc.data.experiment.metric.AggregateMetric;
00023 import edu.rice.cs.hpc.data.experiment.metric.BaseMetric;
00024 import edu.rice.cs.hpc.data.experiment.metric.DerivedMetric;
00025 import edu.rice.cs.hpc.data.experiment.metric.MetricValue;
00026 import edu.rice.cs.hpc.data.experiment.scope.filters.MetricValuePropagationFilter;
00027 import edu.rice.cs.hpc.data.experiment.scope.visitors.FilterScopeVisitor;
00028 import edu.rice.cs.hpc.data.experiment.scope.visitors.IScopeVisitor;
00029 import edu.rice.cs.hpc.data.experiment.source.SourceFile;
00030
00031
00032
00034
00036
00047 public abstract class Scope extends TreeNode
00048 {
00050
00052
00053
00055 public static final int NO_LINE_NUMBER = -169;
00056
00057 static public final int SOURCE_CODE_UNKNOWN = 0;
00058 static public final int SOURCE_CODE_AVAILABLE = 1;
00059 static public final int SOURCE_CODE_NOT_AVAILABLE= 2;
00060
00062 static protected int idMax = 0;
00063
00065 protected BaseExperiment experiment;
00066
00068 protected SourceFile sourceFile;
00069
00071 protected int flat_node_index;
00072
00074 protected int firstLineNumber;
00075
00077 protected int lastLineNumber;
00078
00080 private MetricValue[] metrics;
00081 private MetricValue[] combinedMetrics;
00082
00084 protected String srcCitation;
00085
00090 private int iCounter;
00091
00092
00093
00094 protected int cpid;
00095
00096
00097 public int iSourceCodeAvailability = Scope.SOURCE_CODE_UNKNOWN;
00098
00099
00100
00102
00104
00105
00106
00107
00108
00109
00110
00111
00112 public Scope(BaseExperiment experiment, SourceFile file, int first, int last, int cct_id, int flat_id)
00113 {
00114 super(cct_id);
00115
00116
00117 this.experiment = experiment;
00118 this.sourceFile = file;
00119 this.firstLineNumber = first;
00120 this.lastLineNumber = last;
00121
00122 this.srcCitation = null;
00123 this.flat_node_index = flat_id;
00124 this.cpid = -1;
00125 this.iCounter = 0;
00126 }
00127
00128
00129 public Scope(BaseExperiment experiment, SourceFile file, int first, int last, int cct_id, int flat_id, int cpid)
00130 {
00131 this(experiment, file, first, last, cct_id, flat_id);
00132 this.cpid = cpid;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141 public Scope(BaseExperiment experiment, SourceFile file, int scopeID)
00142 {
00143 this(experiment, file, Scope.NO_LINE_NUMBER, Scope.NO_LINE_NUMBER, scopeID, scopeID);
00144 }
00145
00146
00147 public int getFlatIndex() {
00148 return this.flat_node_index;
00149 }
00150
00151 public int getCCTIndex() {
00152 return (Integer) getValue();
00153 }
00154
00156
00158
00159
00160
00161
00162
00163
00164
00165 public abstract Scope duplicate();
00166
00167
00168
00170
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 public abstract String getName();
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 public String getShortName()
00198 {
00199 return this.getName();
00200 }
00201
00203
00205
00206
00207 public void incrementCounter() {
00208 this.iCounter++;
00209 }
00210
00211 public void decrementCounter() {
00212 if (this.isCounterPositif())
00213 this.iCounter--;
00214 else {
00215 System.err.println("Scope " + this.getName() + " [" + this.getCCTIndex() + "/" + this.flat_node_index + "]" + " has non-positive counter");
00216 }
00217 }
00218
00219 public void setCounter(int counter) {
00220 this.iCounter = counter;
00221 }
00222
00223 public int getCounter() {
00224 return this.iCounter;
00225 }
00226
00227 public boolean isCounterPositif() {
00228 return this.iCounter>0;
00229 }
00230
00231 public boolean isCounterZero() {
00232 return (this.iCounter == 0);
00233 }
00234
00235
00236
00237
00238
00239 public int getCpid()
00240 {
00241 return cpid;
00242 }
00243
00244
00245
00246
00247
00248
00249 public void setCpid(int _cpid)
00250 {
00251 this.cpid = _cpid;
00252 }
00253
00254
00255
00256
00257
00258 public String getToolTip()
00259 {
00260 return this.getSourceCitation();
00261 }
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 public String toString()
00277 {
00278 return this.getName();
00279 }
00280
00281
00282 public int hashCode() {
00283 return this.flat_node_index;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 protected String getSourceCitation()
00293 {
00294 if (this.srcCitation == null) {
00295
00296 srcCitation = this.getSourceCitation(sourceFile, firstLineNumber, lastLineNumber);
00297 }
00298
00299 return srcCitation;
00300 }
00301
00302
00303 private String getSourceCitation(SourceFile sourceFile, int line1, int line2)
00304 {
00305
00306
00307
00308 if (sourceFile == null) {
00309 return this.getName();
00310 }
00311 return sourceFile.getName() + ": " + this.getLineOnlyCitation(line1, line2);
00312
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322 protected String getLineNumberCitation()
00323 {
00324 return this.getLineNumberCitation(firstLineNumber, lastLineNumber);
00325 }
00326
00327
00328 private String getLineNumberCitation(int line1, int line2)
00329 {
00330 String cite;
00331
00332
00333 int first1 = 1 + line1;
00334 int last1 = 1 + line2;
00335
00336 if(line1 == Scope.NO_LINE_NUMBER) {
00337 cite = "";
00338 } else if(line1 == line2)
00339 cite = "line" + " " + first1;
00340 else
00341 cite = "lines" + " " + first1 + "-" + last1;
00342
00343 return cite;
00344 }
00345
00346
00347 private String getLineOnlyCitation(int line1, int line2) {
00348 String cite;
00349
00350
00351 int first1 = 1 + line1;
00352 int last1 = 1 + line2;
00353
00354 if(line1 == Scope.NO_LINE_NUMBER) {
00355 cite = "";
00356 } else if(line1 == line2)
00357 cite = String.valueOf(first1);
00358 else
00359 cite = first1 + "-" + last1;
00360
00361 return cite;
00362 }
00363
00365
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 public SourceFile getSourceFile()
00381 {
00382 return this.sourceFile;
00383 }
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 public int getFirstLineNumber()
00396 {
00397 return this.firstLineNumber;
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412 public int getLastLineNumber()
00413 {
00414 return this.lastLineNumber;
00415 }
00416
00417
00418
00419
00421
00423
00424
00425
00426
00427
00428
00429
00430
00431 public Scope getParentScope()
00432 {
00433 return (Scope) this.getParent();
00434 }
00435
00436
00437
00438
00439
00440
00441 public void setParentScope(Scope parentScope)
00442 {
00443 this.setParent(parentScope);
00444 }
00445
00446
00447
00448
00449
00450
00451
00452
00453 public int getSubscopeCount()
00454 {
00455 return this.getChildCount();
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465 public Scope getSubscope(int index)
00466 {
00467 Scope child = (Scope) this.getChildAt(index);
00468 return child;
00469 }
00470
00471
00472
00473
00474
00475
00476 public void addSubscope(Scope subscope)
00477 {
00478 this.add(subscope);
00479 }
00480
00481
00482
00484
00486
00487 public boolean hasMetrics()
00488 {
00489 return (metrics != null);
00490 }
00491
00492 public boolean hasNonzeroMetrics() {
00493 if (this.hasMetrics())
00494 for (int i = 0; i< this.metrics.length; i++) {
00495 MetricValue m = this.getMetricValue(i);
00496 if (!MetricValue.isZero(m))
00497 return true;
00498 }
00499 return false;
00500 }
00501
00502
00504
00506 public BaseExperiment getExperiment() {
00507 return experiment;
00508 }
00509
00510 public void setExperiment(BaseExperiment exp) {
00511 this.experiment = exp;
00512 }
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524 public MetricValue getMetricValue(BaseMetric metric)
00525 {
00526 int index = metric.getIndex();
00527 MetricValue value = getMetricValue(index);
00528
00529
00530 Scope root = this.experiment.getRootScope();
00531 if((this != root) && (! MetricValue.isAnnotationAvailable(value)))
00532 {
00533 MetricValue total = root.getMetricValue(metric);
00534 if(MetricValue.isAvailable(total))
00535 MetricValue.setAnnotationValue(value, MetricValue.getValue(value)/MetricValue.getValue(total));
00536 }
00537
00538 return value;
00539 }
00540
00541
00542
00543
00544
00545
00546 public MetricValue getMetricValue(int index)
00547 {
00548 MetricValue value;
00549 if(this.metrics != null && index < this.metrics.length)
00550 {
00551 value = this.metrics[index];
00552 }
00553 else
00554 value = MetricValue.NONE;
00555
00556 return value;
00557 }
00558
00559
00560
00561
00562
00563 public void setMetricValue(int index, MetricValue value)
00564 {
00565 ensureMetricStorage();
00566 this.metrics[index] = value;
00567 }
00568
00569
00570
00571
00572 public void accumulateMetrics(Scope source, MetricValuePropagationFilter filter, int nMetrics) {
00573 for (int i = 0; i< nMetrics; i++) {
00574 this.accumulateMetric(source, i, i, filter);
00575 }
00576 }
00577
00578
00579
00580
00581 public void accumulateMetric(Scope source, int src_i, int targ_i, MetricValuePropagationFilter filter) {
00582 if (filter.doPropagation(source, this, src_i, targ_i)) {
00583 MetricValue m = source.getMetricValue(src_i);
00584 if (m != MetricValue.NONE && MetricValue.getValue(m) != 0.0) {
00585 this.accumulateMetricValue(targ_i, MetricValue.getValue(m));
00586 }
00587 }
00588 }
00589
00590
00591
00592
00593
00594
00595 private void accumulateMetricValue(int index, double value)
00596 {
00597 ensureMetricStorage();
00598 if (index >= this.metrics.length)
00599 return;
00600
00601 MetricValue m = this.metrics[index];
00602 if (m == MetricValue.NONE) {
00603 this.metrics[index] = new MetricValue(value);
00604 } else {
00605
00606 MetricValue.setValue(m, MetricValue.getValue(m) + value);
00607 }
00608 }
00609
00610
00611
00612
00613 public void backupMetricValues() {
00614 if (this.metrics == null)
00615 return;
00616
00617 if (!(experiment instanceof BaseExperimentWithMetrics))
00618 return;
00619
00620 this.combinedMetrics = new MetricValue[this.metrics.length];
00621
00622 for(int i=0; i<this.metrics.length; i++) {
00623 MetricValue value = this.metrics[i];
00624 BaseMetric metric = ((BaseExperimentWithMetrics)this.experiment).getMetric(i);
00625
00626
00627
00628
00629
00630 if (MetricValue.isAvailable(value)) {
00631
00632
00633
00634
00635 if (metric instanceof AggregateMetric)
00636 this.combinedMetrics[i] =
00637 new MetricValue(MetricValue.getValue(value),
00638 MetricValue.getAnnotationValue(value));
00639 else
00640 this.combinedMetrics[i] = value;
00641 } else {
00642
00643 this.combinedMetrics[i] = MetricValue.NONE;
00644 }
00645 }
00646 }
00647
00648
00649
00650
00651
00652 public MetricValue[] getMetricValues() {
00653 return this.metrics;
00654 }
00655
00656
00657
00658
00659
00660 public void setMetricValues(MetricValue values[]) {
00661 this.metrics = values;
00662 }
00663
00664
00665
00666
00667
00668
00669 public MetricValue[] getCombinedValues() {
00670
00671 assert (this.isExperimentHasMetrics());
00672
00673 final BaseExperimentWithMetrics _exp = (BaseExperimentWithMetrics) experiment;
00674 MetricValue [] values = new MetricValue[_exp.getMetricCount()];
00675
00676
00677 for (int i=0; i<values.length; i++) {
00678 BaseMetric m = _exp.getMetric(i);
00679 if (m instanceof AggregateMetric) {
00680 if (this.combinedMetrics == null) {
00681
00682
00683
00684
00685 values[i] = this.metrics[i];
00686 } else
00687 values[i] = this.combinedMetrics[i];
00688 } else if (m instanceof DerivedMetric) {
00689 values[i] = MetricValue.NONE;
00690 } else {
00691 values[i] = this.metrics[i];
00692 }
00693 }
00694 return values;
00695 }
00696
00697
00698
00699
00700
00701
00702
00703
00704 public void combine(Scope source, MetricValuePropagationFilter filter) {
00705
00706 assert (this.isExperimentHasMetrics());
00707
00708 final BaseExperimentWithMetrics _exp = (BaseExperimentWithMetrics) experiment;
00709
00710 int nMetrics = _exp.getMetricCount();
00711 for (int i=0; i<nMetrics; i++) {
00712 BaseMetric metric = _exp.getMetric(i);
00713 if (metric instanceof AggregateMetric) {
00714
00715
00716
00717 AggregateMetric aggMetric = (AggregateMetric) metric;
00718 if (filter.doPropagation(source, this, i, i)) {
00719 aggMetric.combine(source, this);
00720 }
00721 } else {
00722 this.accumulateMetric(source, i, i, filter);
00723 }
00724 }
00725 }
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737 public void safeCombine(Scope source, MetricValuePropagationFilter filter) {
00738 ensureMetricStorage();
00739 this.combine(source, filter);
00740 }
00741
00742
00743
00744
00745
00746 protected void ensureMetricStorage()
00747 {
00748
00749 assert (this.isExperimentHasMetrics());
00750
00751 final BaseExperimentWithMetrics _exp = (BaseExperimentWithMetrics) experiment;
00752
00753 if(this.metrics == null)
00754 this.metrics = this.makeMetricValueArray();
00755
00756 if(this.metrics.length < _exp.getMetricCount()) {
00757 MetricValue[] newMetrics = this.makeMetricValueArray();
00758 for(int i=0; i<this.metrics.length; i++)
00759 newMetrics[i] = metrics[i];
00760 this.metrics = newMetrics;
00761 }
00762 }
00763
00764
00765
00766
00767
00768
00769
00770
00771 private MetricValue[] makeMetricValueArray()
00772 {
00773
00774 assert (this.isExperimentHasMetrics());
00775
00776 final BaseExperimentWithMetrics _exp = (BaseExperimentWithMetrics) experiment;
00777 final int metricsNeeded= _exp.getMetricCount();
00778
00779 MetricValue[] array = new MetricValue[metricsNeeded];
00780 for(int k = 0; k < metricsNeeded; k++)
00781 array[k] = MetricValue.NONE;
00782 return array;
00783 }
00784
00785
00786
00787
00788
00789
00790
00791
00792 public void copyMetrics(Scope targetScope, int offset) {
00793
00794 if (!this.hasMetrics())
00795 return;
00796
00797 targetScope.ensureMetricStorage();
00798 for (int k=0; k<this.metrics.length && k<targetScope.metrics.length; k++) {
00799 MetricValue mine = null;
00800 MetricValue crtMetric = this.metrics[k];
00801
00802 if ( MetricValue.isAvailable(crtMetric) && MetricValue.getValue(crtMetric) != 0.0) {
00803 mine = new MetricValue();
00804 MetricValue.setValue(mine, MetricValue.getValue(crtMetric));
00805
00806 if (MetricValue.isAnnotationAvailable(crtMetric)) {
00807 MetricValue.setAnnotationValue(mine, MetricValue.getAnnotationValue(crtMetric));
00808 }
00809 } else {
00810 mine = MetricValue.NONE;
00811 }
00812 targetScope.metrics[k+offset] = mine;
00813 }
00814 }
00815
00816 protected boolean isExperimentHasMetrics()
00817 {
00818 return (this.experiment instanceof BaseExperimentWithMetrics);
00819 }
00820
00821
00823
00825
00826 public void dfsVisitScopeTree(IScopeVisitor sv) {
00827 accept(sv, ScopeVisitType.PreVisit);
00828 int nKids = getSubscopeCount();
00829 for (int i=0; i< nKids; i++) {
00830 Scope childScope = getSubscope(i);
00831 if (childScope != null)
00832 childScope.dfsVisitScopeTree(sv);
00833 }
00834 accept(sv, ScopeVisitType.PostVisit);
00835 }
00836
00837 public void accept(IScopeVisitor visitor, ScopeVisitType vt) {
00838 visitor.visit(this, vt);
00839 }
00840
00841
00842
00843
00844
00845
00846 public void dfsVisitFilterScopeTree(FilterScopeVisitor sv) {
00847 accept(sv, ScopeVisitType.PreVisit);
00848 if (sv.needToContinue())
00849 {
00850
00851
00852
00853 Iterator<TreeNode> iterator = getIterator();
00854 if (iterator != null)
00855 {
00856 for (int i=0; i<getChildCount(); i++)
00857 {
00858 Scope scope = (Scope) getChildAt(i);
00859 if (scope != null)
00860 {
00861 scope.dfsVisitFilterScopeTree(sv);
00862 }
00863 }
00864 }
00865 }
00866 accept(sv, ScopeVisitType.PostVisit);
00867 }
00868
00869 @Override
00870
00871
00872
00873
00874 public void dispose()
00875 {
00876 super.dispose();
00877 experiment = null;
00878 metrics = null;
00879 srcCitation = null;
00880 combinedMetrics = null;
00881 }
00882
00883 }