First steps toward getting full system to work with
[gem5.git] / base / hybrid_pred.cc
1 /*
2 * Copyright (c) 2003-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
29 #include <string>
30 #include <sstream>
31
32 #include "base/hybrid_pred.hh"
33 #include "base/statistics.hh"
34 #include "sim/stats.hh"
35
36 using namespace std;
37
38 HybridPredictor::HybridPredictor(const char *_p_name, const char *_z_name,
39 const char *_o_name,
40 unsigned _index_bits, unsigned _counter_bits,
41 unsigned _zero_change, unsigned _one_change,
42 unsigned _thresh,
43 unsigned _global_bits,
44 unsigned _global_thresh,
45 bool _reg_individual_stats)
46 {
47 stringstream local_name, global_name;
48
49 pred_name = _p_name;
50 one_name = _o_name;
51 zero_name = _z_name;
52 reg_individual_stats = _reg_individual_stats;
53
54 local_name << pred_name.c_str() << ":L";
55 local = new SaturatingCounterPred(local_name.str(), zero_name, one_name,
56 _index_bits, _counter_bits,
57 _zero_change, _one_change, _thresh);
58
59 global_name << pred_name.c_str() << ":G";
60 global = new SaturatingCounterPred(global_name.str(), zero_name, one_name,
61 0, _global_bits, 1, 1, _global_thresh);
62 }
63
64 void HybridPredictor::regStats()
65 {
66 using namespace Stats;
67
68 string p_name;
69 stringstream description;
70
71 if (reg_individual_stats)
72 p_name = pred_name + ":A";
73 else
74 p_name = pred_name;
75
76
77 //
78 // Number of predictions
79 //
80 stringstream num_zero_preds;
81 num_zero_preds << p_name << ":" << zero_name << ":preds";
82 description << "number of predictions of " << zero_name;
83 pred_zero
84 .name(num_zero_preds.str())
85 .desc(description.str());
86 description.str("");
87
88 stringstream num_one_preds;
89 num_one_preds << p_name << ":" << one_name << ":preds";
90 description << "number of predictions of " << one_name;
91 pred_one
92 .name(num_one_preds.str())
93 .desc(description.str())
94 ;
95 description.str("");
96
97 //
98 // Count the number of correct predictions
99 //
100 stringstream num_zero_correct;
101 num_zero_correct << p_name << ":" << zero_name << ":corr_preds";
102 description << "number of correct " << zero_name << " preds" ;
103 correct_pred_zero
104 .name(num_zero_correct.str())
105 .desc(description.str())
106 ;
107 description.str("");
108
109 stringstream num_one_correct;
110 num_one_correct << p_name << ":" << one_name << ":corr_preds";
111 description << "number of correct " << one_name << " preds" ;
112 correct_pred_one
113 .name(num_one_correct.str())
114 .desc(description.str())
115 ;
116 description.str("");
117
118
119 //
120 // Number of predictor updates
121 //
122 stringstream num_zero_updates;
123 num_zero_updates << p_name << ":" << zero_name << ":updates" ;
124 description << "number of actual " << zero_name << "s" ;
125 record_zero
126 .name(num_zero_updates.str())
127 .desc(description.str())
128 ;
129 description.str("");
130
131 stringstream num_one_updates;
132 num_one_updates << p_name << ":" << one_name << ":updates" ;
133 description << "number of actual " << one_name << "s" ;
134 record_one
135 .name(num_one_updates.str())
136 .desc(description.str())
137 ;
138 description.str("");
139
140 //
141 // Local & Global predictor stats
142 //
143 if (reg_individual_stats) {
144 local->regStats();
145 global->regStats();
146 }
147 }
148
149 void HybridPredictor::regFormulas()
150 {
151 using namespace Stats;
152
153 string p_name;
154 stringstream description;
155 stringstream name;
156
157 if (reg_individual_stats)
158 p_name = pred_name + ":A";
159 else
160 p_name = pred_name;
161
162 //
163 // Number of predictions
164 //
165 name << p_name << ":predictions" ;
166 total_preds
167 .name(name.str())
168 .desc("total number of predictions made")
169 ;
170 total_preds = pred_one + pred_zero;
171 name.str("");
172
173 //
174 // Fraction of all predictions that are one or zero
175 //
176 name << p_name << ":" << zero_name << ":pred_frac";
177 description << "fraction of all preds that were " << zero_name ;
178 frac_preds_zero
179 .name(name.str())
180 .desc(description.str())
181 ;
182 frac_preds_zero = 100 * record_zero / total_preds;
183 description.str("");
184 name.str("");
185
186 name << p_name << ":" << one_name << ":pred_frac";
187 description << "fraction of all preds that were " << one_name ;
188 frac_preds_one
189 .name(name.str())
190 .desc(description.str())
191 ;
192 frac_preds_one = 100 * record_one / total_preds;
193 description.str("");
194 name.str("");
195
196 //
197 // Count the number of correct predictions
198 //
199 name << p_name << ":correct_preds" ;
200 total_correct
201 .name(name.str())
202 .desc("total number of correct predictions made")
203 ;
204 total_correct = correct_pred_one + correct_pred_zero;
205 name.str("");
206
207
208 //
209 // Prediction accuracy rates
210 //
211 name << p_name << ":pred_rate";
212 total_accuracy
213 .name(name.str())
214 .desc("fraction of all preds that were correct")
215 ;
216 total_accuracy = 100 * total_correct / total_preds;
217 name.str("");
218
219 name << p_name << ":" << zero_name << ":pred_rate" ;
220 description << "fraction of "<< zero_name <<" preds that were correct";
221 zero_accuracy
222 .name(name.str())
223 .desc(description.str())
224 ;
225 zero_accuracy = 100 * correct_pred_zero / pred_zero;
226 description.str("");
227 name.str("");
228
229 name << p_name << ":" << one_name << ":pred_rate" ;
230 description << "fraction of "<< one_name <<" preds that were correct";
231 one_accuracy
232 .name(name.str())
233 .desc(description.str())
234 ;
235 one_accuracy = 100 * correct_pred_one / pred_one;
236 description.str("");
237 name.str("");
238
239 //
240 // Coverage
241 //
242 name << p_name << ":" << zero_name << ":coverage";
243 description << "fraction of " << zero_name
244 << "s that were predicted correctly";
245 zero_coverage
246 .name(name.str())
247 .desc(description.str())
248 ;
249 zero_coverage = 100 * correct_pred_zero / record_zero;
250 description.str("");
251 name.str("");
252
253 name << p_name << ":" << one_name << ":coverage";
254 description << "fraction of " << one_name
255 << "s that were predicted correctly";
256 one_coverage
257 .name(name.str())
258 .desc(description.str())
259 ;
260 one_coverage = 100 * correct_pred_one / record_one;
261 description.str("");
262 name.str("");
263
264 //
265 // Local & Global predictor stats
266 //
267 if (reg_individual_stats) {
268 local->regFormulas();
269 global->regFormulas();
270 }
271
272 }
273