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