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