stats: forgot an include for the mysql stuff
[gem5.git] / src / base / hybrid_pred.hh
1 /*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Steve Raasch
29 * Steve Reinhardt
30 */
31
32 //==========================================================================
33 //
34 // This predictor takes the AND of a "local" and a "global" predictor
35 // in order to determine its prediction.
36 //
37 //
38 //
39 //
40
41 #ifndef __HYBRID_PRED_HH__
42 #define __HYBRID_PRED_HH__
43
44 #include <string>
45
46 #include "base/sat_counter.hh"
47 #include "base/statistics.hh"
48
49 class HybridPredictor : public GenericPredictor
50 {
51 private:
52 std::string pred_name;
53 std::string one_name;
54 std::string zero_name;
55 bool reg_individual_stats;
56
57 SaturatingCounterPred *local;
58 SaturatingCounterPred *global;
59
60 unsigned long max_index;
61
62 //
63 // Stats
64 //
65 Stats::Scalar pred_one; //num_one_preds
66 Stats::Scalar pred_zero; //num_zero_preds
67 Stats::Scalar correct_pred_one; //num_one_correct
68 Stats::Scalar correct_pred_zero; //num_zero_correct
69 Stats::Scalar record_one; //num_one_updates
70 Stats::Scalar record_zero; //num_zero_updates
71
72 Stats::Formula total_preds;
73 Stats::Formula frac_preds_zero;
74 Stats::Formula frac_preds_one;
75 Stats::Formula total_correct;
76 Stats::Formula total_accuracy;
77 Stats::Formula zero_accuracy;
78 Stats::Formula one_accuracy;
79 Stats::Formula zero_coverage;
80 Stats::Formula one_coverage;
81
82 public:
83 HybridPredictor(const char *_p_name, const char *_z_name,
84 const char *_o_name,
85 unsigned _index_bits, unsigned _counter_bits,
86 unsigned _zero_change, unsigned _one_change,
87 unsigned _thresh,
88 unsigned _global_bits, unsigned _global_thresh,
89 bool _reg_individual_stats = false);
90
91 void clear() {
92 global->clear();
93 local->clear();
94 }
95
96 unsigned peek(unsigned long _index) {
97 unsigned l = local->peek(_index);
98 unsigned g = global->peek(_index);
99
100 if (l && g)
101 return 1;
102
103 return 0;
104 }
105
106 unsigned value(unsigned long _index) {
107 unsigned l = local->peek(_index);
108 unsigned g = global->peek(_index);
109
110 l = l & 0xFFFF;
111 g = g & 0xFFFF;
112
113 return (l << 16) | g;
114
115 }
116
117 unsigned predict(unsigned long _index) {
118 unsigned l = local->predict(_index);
119 unsigned g = global->predict(_index);
120
121 if (l && g) {
122 ++pred_one;
123 return 1;
124 }
125
126 ++pred_zero;
127 return 0;
128 }
129
130
131 //
132 // This version need only be used if local/global statistics
133 // will be maintained
134 //
135 unsigned predict(unsigned long _index, unsigned &_pdata) {
136 unsigned l = local->predict(_index);
137 unsigned g = global->predict(_index);
138
139 //
140 // bit 0 => local predictor result
141 // bit 1 => global predictor result
142 //
143 _pdata = 0;
144 if (l)
145 _pdata |= 1;
146 if (g)
147 _pdata |= 2;
148 if (l && g) {
149 ++pred_one;
150 return 1;
151 }
152
153 ++pred_zero;
154 return 0;
155 }
156
157 void record(unsigned long _index, unsigned _val, unsigned _predicted) {
158
159 if (_val) {
160 local->record(_index, _val, 0);
161 global->record(_index, _val, 0);
162 ++record_one;
163
164 if (_val == _predicted) {
165 ++correct_pred_one;
166 }
167 } else {
168 local->record(_index, _val, 0);
169 global->record(_index, _val, 0);
170 ++record_zero;
171
172 if (_val == _predicted)
173 ++correct_pred_zero;
174 }
175 }
176
177 void record(unsigned long _index, unsigned _val, unsigned _predicted,
178 unsigned _pdata)
179 {
180
181 local->record(_index, _val, (_pdata & 1));
182 global->record(_index, _val, ((_pdata & 2) ? 1 : 0));
183
184
185 if (_val) {
186 ++record_one;
187
188 if (_val == _predicted)
189 ++correct_pred_one;
190 } else {
191 ++record_zero;
192
193 if (_val == _predicted)
194 ++correct_pred_zero;
195 }
196 }
197
198 void regStats();
199 void regFormulas();
200 };
201
202
203 #endif // _HYBRID_PRED_HH__
204