KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > frontend > Stats


1 package polyglot.frontend;
2
3 import java.util.*;
4 import java.io.PrintStream JavaDoc;
5
6 import polyglot.util.*;
7 import polyglot.main.Report;
8
9 /**
10  * Statistics collection and reporting object.
11  * Extensions can override this to collect more stats or to change
12  * reporting.
13  */

14 public class Stats
15 {
16     protected static class Times {
17         long inclusive;
18         long exclusive;
19     }
20
21     /** Extension we're collecting stats for. */
22     protected ExtensionInfo ext;
23
24     /** Map from Objects to pair of inclusive and exclusive times. */
25     protected Map passTimes = new HashMap();
26
27     /**
28      * List of Objects used as keys to passTimes. We have an explicit
29      * list in order to report the keys in order.
30      */

31     protected List keys = new ArrayList(20);
32
33     public Stats(ExtensionInfo ext) {
34         this.ext = ext;
35     }
36
37     /** Reset the accumulated times for a pass. */
38     public void resetPassTimes(Object JavaDoc key) {
39         passTimes.remove(key);
40     }
41
42     /** Return the accumulated times for a pass. */
43     public long passTime(Object JavaDoc key, boolean inclusive) {
44         Times t = (Times) passTimes.get(key);
45         if (t == null) {
46             return 0;
47         }
48
49         return inclusive ? t.inclusive : t.exclusive;
50     }
51
52     /** Accumulate inclusive and exclusive times for a pass. */
53     public void accumPassTimes(Object JavaDoc key, long in, long ex) {
54         Times t = (Times) passTimes.get(key);
55         if (t == null) {
56             keys.add(key);
57             t = new Times();
58             passTimes.put(key, t);
59         }
60         t.inclusive += in;
61         t.exclusive += ex;
62     }
63
64     /** Report the stats. */
65     public void report() {
66         if (Report.should_report(Report.time, 1)) {
67             Report.report(1, "\nStatistics for " + ext.compilerName() +
68                           " (" + ext.getClass().getName() + ")");
69             Report.report(1, "Pass Inclusive Exclusive");
70             Report.report(1, "---- --------- ---------");
71
72             for (Iterator i = keys.iterator(); i.hasNext(); ) {
73                 Object JavaDoc key = i.next();
74                 Times t = (Times) passTimes.get(key);
75
76                 Report.report(1, key.toString() + " " +
77                                  t.inclusive + " " + t.exclusive);
78             }
79         }
80     }
81 }
82
Popular Tags