DPRINTF: Improve some dprintf messages.
[gem5.git] / src / base / statistics.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: Nathan Binkert
29 */
30
31 #include <fstream>
32 #include <iomanip>
33 #include <list>
34 #include <map>
35 #include <string>
36
37 #include "base/callback.hh"
38 #include "base/cprintf.hh"
39 #include "base/debug.hh"
40 #include "base/hostinfo.hh"
41 #include "base/misc.hh"
42 #include "base/statistics.hh"
43 #include "base/str.hh"
44 #include "base/time.hh"
45 #include "base/trace.hh"
46
47 using namespace std;
48
49 namespace Stats {
50
51 std::string Info::separatorString = "::";
52 typedef map<const void *, Info *> MapType;
53
54 // We wrap these in a function to make sure they're built in time.
55 list<Info *> &
56 statsList()
57 {
58 static list<Info *> the_list;
59 return the_list;
60 }
61
62 MapType &
63 statsMap()
64 {
65 static MapType the_map;
66 return the_map;
67 }
68
69 void
70 InfoAccess::setInfo(Info *info)
71 {
72 if (statsMap().find(this) != statsMap().end())
73 panic("shouldn't register stat twice!");
74
75 statsList().push_back(info);
76
77 #ifndef NDEBUG
78 pair<MapType::iterator, bool> result =
79 #endif
80 statsMap().insert(make_pair(this, info));
81 assert(result.second && "this should never fail");
82 assert(statsMap().find(this) != statsMap().end());
83 }
84
85 void
86 InfoAccess::setParams(const StorageParams *params)
87 {
88 info()->storageParams = params;
89 }
90
91 void
92 InfoAccess::setInit()
93 {
94 info()->flags.set(init);
95 }
96
97 Info *
98 InfoAccess::info()
99 {
100 MapType::const_iterator i = statsMap().find(this);
101 assert(i != statsMap().end());
102 return (*i).second;
103 }
104
105 const Info *
106 InfoAccess::info() const
107 {
108 MapType::const_iterator i = statsMap().find(this);
109 assert(i != statsMap().end());
110 return (*i).second;
111 }
112
113 StorageParams::~StorageParams()
114 {
115 }
116
117 typedef map<std::string, Info *> NameMapType;
118 NameMapType &
119 nameMap()
120 {
121 static NameMapType the_map;
122 return the_map;
123 }
124
125 int Info::id_count = 0;
126
127 int debug_break_id = -1;
128
129 Info::Info()
130 : flags(none), precision(-1), prereq(0), storageParams(NULL)
131 {
132 id = id_count++;
133 if (debug_break_id >= 0 and debug_break_id == id)
134 Debug::breakpoint();
135 }
136
137 Info::~Info()
138 {
139 }
140
141 bool
142 validateStatName(const string &name)
143 {
144 if (name.empty())
145 return false;
146
147 vector<string> vec;
148 tokenize(vec, name, '.');
149 vector<string>::const_iterator item = vec.begin();
150 while (item != vec.end()) {
151 if (item->empty())
152 return false;
153
154 string::const_iterator c = item->begin();
155
156 // The first character is different
157 if (!isalpha(*c) && *c != '_')
158 return false;
159
160 // The rest of the characters have different rules.
161 while (++c != item->end()) {
162 if (!isalnum(*c) && *c != '_')
163 return false;
164 }
165
166 ++item;
167 }
168
169 return true;
170 }
171
172 void
173 Info::setName(const string &name)
174 {
175 if (!validateStatName(name))
176 panic("invalid stat name '%s'", name);
177
178 pair<NameMapType::iterator, bool> p =
179 nameMap().insert(make_pair(name, this));
180
181 Info *other = p.first->second;
182 bool result = p.second;
183
184 if (!result) {
185 // using other->name instead of just name to avoid a compiler
186 // warning. They should be the same.
187 panic("same statistic name used twice! name=%s\n", other->name);
188 }
189
190 this->name = name;
191 }
192
193 bool
194 Info::less(Info *stat1, Info *stat2)
195 {
196 const string &name1 = stat1->name;
197 const string &name2 = stat2->name;
198
199 vector<string> v1;
200 vector<string> v2;
201
202 tokenize(v1, name1, '.');
203 tokenize(v2, name2, '.');
204
205 size_type last = min(v1.size(), v2.size()) - 1;
206 for (off_type i = 0; i < last; ++i)
207 if (v1[i] != v2[i])
208 return v1[i] < v2[i];
209
210 // Special compare for last element.
211 if (v1[last] == v2[last])
212 return v1.size() < v2.size();
213 else
214 return v1[last] < v2[last];
215
216 return false;
217 }
218
219 bool
220 Info::baseCheck() const
221 {
222 if (!(flags & Stats::init)) {
223 #ifdef DEBUG
224 cprintf("this is stat number %d\n", id);
225 #endif
226 panic("Not all stats have been initialized");
227 return false;
228 }
229
230 if ((flags & display) && name.empty()) {
231 panic("all printable stats must be named");
232 return false;
233 }
234
235 return true;
236 }
237
238 void
239 Info::enable()
240 {
241 }
242
243 void
244 VectorInfo::enable()
245 {
246 size_type s = size();
247 if (subnames.size() < s)
248 subnames.resize(s);
249 if (subdescs.size() < s)
250 subdescs.resize(s);
251 }
252
253 void
254 VectorDistInfo::enable()
255 {
256 size_type s = size();
257 if (subnames.size() < s)
258 subnames.resize(s);
259 if (subdescs.size() < s)
260 subdescs.resize(s);
261 }
262
263 void
264 Vector2dInfo::enable()
265 {
266 if (subnames.size() < x)
267 subnames.resize(x);
268 if (subdescs.size() < x)
269 subdescs.resize(x);
270 if (y_subnames.size() < y)
271 y_subnames.resize(y);
272 }
273
274 void
275 HistStor::grow_out()
276 {
277 int size = cvec.size();
278 int zero = size / 2; // round down!
279 int top_half = zero + (size - zero + 1) / 2; // round up!
280 int bottom_half = (size - zero) / 2; // round down!
281
282 // grow down
283 int low_pair = zero - 1;
284 for (int i = zero - 1; i >= bottom_half; i--) {
285 cvec[i] = cvec[low_pair];
286 if (low_pair - 1 >= 0)
287 cvec[i] += cvec[low_pair - 1];
288 low_pair -= 2;
289 }
290 assert(low_pair == 0 || low_pair == -1 || low_pair == -2);
291
292 for (int i = bottom_half - 1; i >= 0; i--)
293 cvec[i] = Counter();
294
295 // grow up
296 int high_pair = zero;
297 for (int i = zero; i < top_half; i++) {
298 cvec[i] = cvec[high_pair];
299 if (high_pair + 1 < size)
300 cvec[i] += cvec[high_pair + 1];
301 high_pair += 2;
302 }
303 assert(high_pair == size || high_pair == size + 1);
304
305 for (int i = top_half; i < size; i++)
306 cvec[i] = Counter();
307
308 max_bucket *= 2;
309 min_bucket *= 2;
310 bucket_size *= 2;
311 }
312
313 void
314 HistStor::grow_convert()
315 {
316 int size = cvec.size();
317 int half = (size + 1) / 2; // round up!
318 //bool even = (size & 1) == 0;
319
320 int pair = size - 1;
321 for (int i = size - 1; i >= half; --i) {
322 cvec[i] = cvec[pair];
323 if (pair - 1 >= 0)
324 cvec[i] += cvec[pair - 1];
325 pair -= 2;
326 }
327
328 for (int i = half - 1; i >= 0; i--)
329 cvec[i] = Counter();
330
331 min_bucket = -max_bucket;// - (even ? bucket_size : 0);
332 bucket_size *= 2;
333 }
334
335 void
336 HistStor::grow_up()
337 {
338 int size = cvec.size();
339 int half = (size + 1) / 2; // round up!
340
341 int pair = 0;
342 for (int i = 0; i < half; i++) {
343 cvec[i] = cvec[pair];
344 if (pair + 1 < size)
345 cvec[i] += cvec[pair + 1];
346 pair += 2;
347 }
348 assert(pair == size || pair == size + 1);
349
350 for (int i = half; i < size; i++)
351 cvec[i] = Counter();
352
353 max_bucket *= 2;
354 bucket_size *= 2;
355 }
356
357 Formula::Formula()
358 {
359 }
360
361 Formula::Formula(Temp r)
362 {
363 root = r;
364 setInit();
365 assert(size());
366 }
367
368 const Formula &
369 Formula::operator=(Temp r)
370 {
371 assert(!root && "Can't change formulas");
372 root = r;
373 setInit();
374 assert(size());
375 return *this;
376 }
377
378 const Formula &
379 Formula::operator+=(Temp r)
380 {
381 if (root)
382 root = NodePtr(new BinaryNode<std::plus<Result> >(root, r));
383 else {
384 root = r;
385 setInit();
386 }
387
388 assert(size());
389 return *this;
390 }
391
392 void
393 Formula::result(VResult &vec) const
394 {
395 if (root)
396 vec = root->result();
397 }
398
399 Result
400 Formula::total() const
401 {
402 return root ? root->total() : 0.0;
403 }
404
405 size_type
406 Formula::size() const
407 {
408 if (!root)
409 return 0;
410 else
411 return root->size();
412 }
413
414 void
415 Formula::reset()
416 {
417 }
418
419 bool
420 Formula::zero() const
421 {
422 VResult vec;
423 result(vec);
424 for (VResult::size_type i = 0; i < vec.size(); ++i)
425 if (vec[i] != 0.0)
426 return false;
427 return true;
428 }
429
430 string
431 Formula::str() const
432 {
433 return root ? root->str() : "";
434 }
435
436 CallbackQueue resetQueue;
437
438 void
439 registerResetCallback(Callback *cb)
440 {
441 resetQueue.add(cb);
442 }
443
444 } // namespace Stats
445
446 void
447 debugDumpStats()
448 {
449 Stats::dump();
450 }