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 "base/statistics.hh"
32
33#include <fstream>
34#include <iomanip>
35#include <list>
36#include <map>
37#include <string>
38
39#include "base/callback.hh"
40#include "base/cprintf.hh"
41#include "base/debug.hh"
42#include "base/hostinfo.hh"
43#include "base/misc.hh"
43#include "base/logging.hh"
44#include "base/str.hh"
45#include "base/time.hh"
46#include "base/trace.hh"
47
48using namespace std;
49
50namespace Stats {
51
52std::string Info::separatorString = "::";
53
54// We wrap these in a function to make sure they're built in time.
55list<Info *> &
56statsList()
57{
58 static list<Info *> the_list;
59 return the_list;
60}
61
62MapType &
63statsMap()
64{
65 static MapType the_map;
66 return the_map;
67}
68
69void
70InfoAccess::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
85void
86InfoAccess::setParams(const StorageParams *params)
87{
88 info()->storageParams = params;
89}
90
91void
92InfoAccess::setInit()
93{
94 info()->flags.set(init);
95}
96
97Info *
98InfoAccess::info()
99{
100 MapType::const_iterator i = statsMap().find(this);
101 assert(i != statsMap().end());
102 return (*i).second;
103}
104
105const Info *
106InfoAccess::info() const
107{
108 MapType::const_iterator i = statsMap().find(this);
109 assert(i != statsMap().end());
110 return (*i).second;
111}
112
113StorageParams::~StorageParams()
114{
115}
116
117NameMapType &
118nameMap()
119{
120 static NameMapType the_map;
121 return the_map;
122}
123
124int Info::id_count = 0;
125
126int debug_break_id = -1;
127
128Info::Info()
129 : flags(none), precision(-1), prereq(0), storageParams(NULL)
130{
131 id = id_count++;
132 if (debug_break_id >= 0 and debug_break_id == id)
133 Debug::breakpoint();
134}
135
136Info::~Info()
137{
138}
139
140bool
141validateStatName(const string &name)
142{
143 if (name.empty())
144 return false;
145
146 vector<string> vec;
147 tokenize(vec, name, '.');
148 vector<string>::const_iterator item = vec.begin();
149 while (item != vec.end()) {
150 if (item->empty())
151 return false;
152
153 string::const_iterator c = item->begin();
154
155 // The first character is different
156 if (!isalpha(*c) && *c != '_')
157 return false;
158
159 // The rest of the characters have different rules.
160 while (++c != item->end()) {
161 if (!isalnum(*c) && *c != '_')
162 return false;
163 }
164
165 ++item;
166 }
167
168 return true;
169}
170
171void
172Info::setName(const string &name)
173{
174 if (!validateStatName(name))
175 panic("invalid stat name '%s'", name);
176
177 pair<NameMapType::iterator, bool> p =
178 nameMap().insert(make_pair(name, this));
179
180 Info *other = p.first->second;
181 bool result = p.second;
182
183 if (!result) {
184 // using other->name instead of just name to avoid a compiler
185 // warning. They should be the same.
186 panic("same statistic name used twice! name=%s\n", other->name);
187 }
188
189 this->name = name;
190}
191
192bool
193Info::less(Info *stat1, Info *stat2)
194{
195 const string &name1 = stat1->name;
196 const string &name2 = stat2->name;
197
198 vector<string> v1;
199 vector<string> v2;
200
201 tokenize(v1, name1, '.');
202 tokenize(v2, name2, '.');
203
204 size_type last = min(v1.size(), v2.size()) - 1;
205 for (off_type i = 0; i < last; ++i)
206 if (v1[i] != v2[i])
207 return v1[i] < v2[i];
208
209 // Special compare for last element.
210 if (v1[last] == v2[last])
211 return v1.size() < v2.size();
212 else
213 return v1[last] < v2[last];
214
215 return false;
216}
217
218bool
219Info::baseCheck() const
220{
221 if (!(flags & Stats::init)) {
222#ifdef DEBUG
223 cprintf("this is stat number %d\n", id);
224#endif
225 panic("Not all stats have been initialized.\n"
226 "You may need to add <ParentClass>::regStats() to a"
227 " new SimObject's regStats() function.");
228 return false;
229 }
230
231 if ((flags & display) && name.empty()) {
232 panic("all printable stats must be named");
233 return false;
234 }
235
236 return true;
237}
238
239void
240Info::enable()
241{
242}
243
244void
245VectorInfo::enable()
246{
247 size_type s = size();
248 if (subnames.size() < s)
249 subnames.resize(s);
250 if (subdescs.size() < s)
251 subdescs.resize(s);
252}
253
254void
255VectorDistInfo::enable()
256{
257 size_type s = size();
258 if (subnames.size() < s)
259 subnames.resize(s);
260 if (subdescs.size() < s)
261 subdescs.resize(s);
262}
263
264void
265Vector2dInfo::enable()
266{
267 if (subnames.size() < x)
268 subnames.resize(x);
269 if (subdescs.size() < x)
270 subdescs.resize(x);
271 if (y_subnames.size() < y)
272 y_subnames.resize(y);
273}
274
275void
276HistStor::grow_out()
277{
278 int size = cvec.size();
279 int zero = size / 2; // round down!
280 int top_half = zero + (size - zero + 1) / 2; // round up!
281 int bottom_half = (size - zero) / 2; // round down!
282
283 // grow down
284 int low_pair = zero - 1;
285 for (int i = zero - 1; i >= bottom_half; i--) {
286 cvec[i] = cvec[low_pair];
287 if (low_pair - 1 >= 0)
288 cvec[i] += cvec[low_pair - 1];
289 low_pair -= 2;
290 }
291 assert(low_pair == 0 || low_pair == -1 || low_pair == -2);
292
293 for (int i = bottom_half - 1; i >= 0; i--)
294 cvec[i] = Counter();
295
296 // grow up
297 int high_pair = zero;
298 for (int i = zero; i < top_half; i++) {
299 cvec[i] = cvec[high_pair];
300 if (high_pair + 1 < size)
301 cvec[i] += cvec[high_pair + 1];
302 high_pair += 2;
303 }
304 assert(high_pair == size || high_pair == size + 1);
305
306 for (int i = top_half; i < size; i++)
307 cvec[i] = Counter();
308
309 max_bucket *= 2;
310 min_bucket *= 2;
311 bucket_size *= 2;
312}
313
314void
315HistStor::grow_convert()
316{
317 int size = cvec.size();
318 int half = (size + 1) / 2; // round up!
319 //bool even = (size & 1) == 0;
320
321 int pair = size - 1;
322 for (int i = size - 1; i >= half; --i) {
323 cvec[i] = cvec[pair];
324 if (pair - 1 >= 0)
325 cvec[i] += cvec[pair - 1];
326 pair -= 2;
327 }
328
329 for (int i = half - 1; i >= 0; i--)
330 cvec[i] = Counter();
331
332 min_bucket = -max_bucket;// - (even ? bucket_size : 0);
333 bucket_size *= 2;
334}
335
336void
337HistStor::grow_up()
338{
339 int size = cvec.size();
340 int half = (size + 1) / 2; // round up!
341
342 int pair = 0;
343 for (int i = 0; i < half; i++) {
344 cvec[i] = cvec[pair];
345 if (pair + 1 < size)
346 cvec[i] += cvec[pair + 1];
347 pair += 2;
348 }
349 assert(pair == size || pair == size + 1);
350
351 for (int i = half; i < size; i++)
352 cvec[i] = Counter();
353
354 max_bucket *= 2;
355 bucket_size *= 2;
356}
357
358void
359HistStor::add(HistStor *hs)
360{
361 int b_size = hs->size();
362 assert(size() == b_size);
363 assert(min_bucket == hs->min_bucket);
364
365 sum += hs->sum;
366 logs += hs->logs;
367 squares += hs->squares;
368 samples += hs->samples;
369
370 while (bucket_size > hs->bucket_size)
371 hs->grow_up();
372 while (bucket_size < hs->bucket_size)
373 grow_up();
374
375 for (uint32_t i = 0; i < b_size; i++)
376 cvec[i] += hs->cvec[i];
377}
378
379Formula::Formula()
380{
381}
382
383Formula::Formula(Temp r)
384{
385 root = r.getNodePtr();
386 setInit();
387 assert(size());
388}
389
390const Formula &
391Formula::operator=(Temp r)
392{
393 assert(!root && "Can't change formulas");
394 root = r.getNodePtr();
395 setInit();
396 assert(size());
397 return *this;
398}
399
400const Formula &
401Formula::operator+=(Temp r)
402{
403 if (root)
404 root = NodePtr(new BinaryNode<std::plus<Result> >(root, r));
405 else {
406 root = r.getNodePtr();
407 setInit();
408 }
409
410 assert(size());
411 return *this;
412}
413
414const Formula &
415Formula::operator/=(Temp r)
416{
417 assert (root);
418 root = NodePtr(new BinaryNode<std::divides<Result> >(root, r));
419
420 assert(size());
421 return *this;
422}
423
424void
425Formula::result(VResult &vec) const
426{
427 if (root)
428 vec = root->result();
429}
430
431Result
432Formula::total() const
433{
434 return root ? root->total() : 0.0;
435}
436
437size_type
438Formula::size() const
439{
440 if (!root)
441 return 0;
442 else
443 return root->size();
444}
445
446void
447Formula::reset()
448{
449}
450
451bool
452Formula::zero() const
453{
454 VResult vec;
455 result(vec);
456 for (VResult::size_type i = 0; i < vec.size(); ++i)
457 if (vec[i] != 0.0)
458 return false;
459 return true;
460}
461
462string
463Formula::str() const
464{
465 return root ? root->str() : "";
466}
467
468Handler resetHandler = NULL;
469Handler dumpHandler = NULL;
470
471void
472registerHandlers(Handler reset_handler, Handler dump_handler)
473{
474 resetHandler = reset_handler;
475 dumpHandler = dump_handler;
476}
477
478CallbackQueue dumpQueue;
479CallbackQueue resetQueue;
480
481void
482processResetQueue()
483{
484 resetQueue.process();
485}
486
487void
488processDumpQueue()
489{
490 dumpQueue.process();
491}
492
493void
494registerResetCallback(Callback *cb)
495{
496 resetQueue.add(cb);
497}
498
499bool _enabled = false;
500
501bool
502enabled()
503{
504 return _enabled;
505}
506
507void
508enable()
509{
510 if (_enabled)
511 fatal("Stats are already enabled");
512
513 _enabled = true;
514}
515
516void
517dump()
518{
519 if (dumpHandler)
520 dumpHandler();
521 else
522 fatal("No registered Stats::dump handler");
523}
524
525void
526reset()
527{
528 if (resetHandler)
529 resetHandler();
530 else
531 fatal("No registered Stats::reset handler");
532}
533
534void
535registerDumpCallback(Callback *cb)
536{
537 dumpQueue.add(cb);
538}
539
540} // namespace Stats
541
542void
543debugDumpStats()
544{
545 Stats::dump();
546}