statistics.cc (8248:d69720504203) statistics.cc (8296:be7f03723412)
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
47using namespace std;
48
49namespace Stats {
50
51std::string Info::separatorString = "::";
52typedef map<const void *, Info *> MapType;
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
117typedef map<std::string, Info *> NameMapType;
118NameMapType &
119nameMap()
120{
121 static NameMapType the_map;
122 return the_map;
123}
124
125int Info::id_count = 0;
126
127int debug_break_id = -1;
128
129Info::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
137Info::~Info()
138{
139}
140
141bool
142validateStatName(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
172void
173Info::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
193bool
194Info::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
219bool
220Info::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
238void
239Info::enable()
240{
241}
242
243void
244VectorInfo::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
253void
254VectorDistInfo::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
263void
264Vector2dInfo::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
274void
275HistStor::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
313void
314HistStor::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
335void
336HistStor::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
357Formula::Formula()
358{
359}
360
361Formula::Formula(Temp r)
362{
363 root = r;
364 setInit();
365 assert(size());
366}
367
368const Formula &
369Formula::operator=(Temp r)
370{
371 assert(!root && "Can't change formulas");
372 root = r;
373 setInit();
374 assert(size());
375 return *this;
376}
377
378const Formula &
379Formula::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
392void
393Formula::result(VResult &vec) const
394{
395 if (root)
396 vec = root->result();
397}
398
399Result
400Formula::total() const
401{
402 return root ? root->total() : 0.0;
403}
404
405size_type
406Formula::size() const
407{
408 if (!root)
409 return 0;
410 else
411 return root->size();
412}
413
414void
415Formula::reset()
416{
417}
418
419bool
420Formula::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
430string
431Formula::str() const
432{
433 return root ? root->str() : "";
434}
435
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
47using namespace std;
48
49namespace Stats {
50
51std::string Info::separatorString = "::";
52typedef map<const void *, Info *> MapType;
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
117typedef map<std::string, Info *> NameMapType;
118NameMapType &
119nameMap()
120{
121 static NameMapType the_map;
122 return the_map;
123}
124
125int Info::id_count = 0;
126
127int debug_break_id = -1;
128
129Info::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
137Info::~Info()
138{
139}
140
141bool
142validateStatName(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
172void
173Info::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
193bool
194Info::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
219bool
220Info::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
238void
239Info::enable()
240{
241}
242
243void
244VectorInfo::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
253void
254VectorDistInfo::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
263void
264Vector2dInfo::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
274void
275HistStor::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
313void
314HistStor::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
335void
336HistStor::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
357Formula::Formula()
358{
359}
360
361Formula::Formula(Temp r)
362{
363 root = r;
364 setInit();
365 assert(size());
366}
367
368const Formula &
369Formula::operator=(Temp r)
370{
371 assert(!root && "Can't change formulas");
372 root = r;
373 setInit();
374 assert(size());
375 return *this;
376}
377
378const Formula &
379Formula::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
392void
393Formula::result(VResult &vec) const
394{
395 if (root)
396 vec = root->result();
397}
398
399Result
400Formula::total() const
401{
402 return root ? root->total() : 0.0;
403}
404
405size_type
406Formula::size() const
407{
408 if (!root)
409 return 0;
410 else
411 return root->size();
412}
413
414void
415Formula::reset()
416{
417}
418
419bool
420Formula::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
430string
431Formula::str() const
432{
433 return root ? root->str() : "";
434}
435
436void
437enable()
438{
439 typedef list<Info *>::iterator iter_t;
440
441 iter_t i, end = statsList().end();
442 for (i = statsList().begin(); i != end; ++i) {
443 Info *info = *i;
444 assert(info);
445 if (!info->check() || !info->baseCheck())
446 panic("stat check failed for '%s' %d\n", info->name, info->id);
447 }
448
449 off_t j = 0;
450 for (i = statsList().begin(); i != end; ++i) {
451 Info *info = *i;
452 if (!(info->flags & display))
453 info->name = "__Stat" + to_string(j++);
454 }
455
456 statsList().sort(Info::less);
457
458 for (i = statsList().begin(); i != end; ++i) {
459 Info *info = *i;
460 info->enable();
461 }
462}
463
464void
465prepare()
466{
467 list<Info *>::iterator i = statsList().begin();
468 list<Info *>::iterator end = statsList().end();
469 while (i != end) {
470 Info *info = *i;
471 info->prepare();
472 ++i;
473 }
474}
475
476CallbackQueue resetQueue;
477
478void
436CallbackQueue resetQueue;
437
438void
479reset()
480{
481 list<Info *>::iterator i = statsList().begin();
482 list<Info *>::iterator end = statsList().end();
483 while (i != end) {
484 Info *info = *i;
485 info->reset();
486 ++i;
487 }
488
489 resetQueue.process();
490}
491
492void
493registerResetCallback(Callback *cb)
494{
495 resetQueue.add(cb);
496}
497
498} // namespace Stats
439registerResetCallback(Callback *cb)
440{
441 resetQueue.add(cb);
442}
443
444} // namespace Stats
445
446void
447debugDumpStats()
448{
449 Stats::dump();
450}