statistics.hh (9857:8301f882a916) statistics.hh (9863:9483739f83ee)
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/** @file
32 * Declaration of Statistics objects.
33 */
34
35/**
36* @todo
37*
38* Generalized N-dimensinal vector
39* documentation
40* key stats
41* interval stats
42* -- these both can use the same function that prints out a
43* specific set of stats
44* VectorStandardDeviation totals
45* Document Namespaces
46*/
47#ifndef __BASE_STATISTICS_HH__
48#define __BASE_STATISTICS_HH__
49
50#include <algorithm>
51#include <cassert>
52#ifdef __SUNPRO_CC
53#include <math.h>
54#endif
55#include <cmath>
56#include <functional>
57#include <iosfwd>
58#include <list>
59#include <map>
60#include <string>
61#include <vector>
62
63#include "base/stats/info.hh"
64#include "base/stats/output.hh"
65#include "base/stats/types.hh"
66#include "base/cast.hh"
67#include "base/cprintf.hh"
68#include "base/intmath.hh"
69#include "base/refcnt.hh"
70#include "base/str.hh"
71#include "base/types.hh"
72
73class Callback;
74
75/** The current simulated tick. */
76extern Tick curTick();
77
78/* A namespace for all of the Statistics */
79namespace Stats {
80
81template <class Stat, class Base>
82class InfoProxy : public Base
83{
84 protected:
85 Stat &s;
86
87 public:
88 InfoProxy(Stat &stat) : s(stat) {}
89
90 bool check() const { return s.check(); }
91 void prepare() { s.prepare(); }
92 void reset() { s.reset(); }
93 void
94 visit(Output &visitor)
95 {
96 visitor.visit(*static_cast<Base *>(this));
97 }
98 bool zero() const { return s.zero(); }
99};
100
101template <class Stat>
102class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
103{
104 public:
105 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
106
107 Counter value() const { return this->s.value(); }
108 Result result() const { return this->s.result(); }
109 Result total() const { return this->s.total(); }
110};
111
112template <class Stat>
113class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
114{
115 protected:
116 mutable VCounter cvec;
117 mutable VResult rvec;
118
119 public:
120 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
121
122 size_type size() const { return this->s.size(); }
123
124 VCounter &
125 value() const
126 {
127 this->s.value(cvec);
128 return cvec;
129 }
130
131 const VResult &
132 result() const
133 {
134 this->s.result(rvec);
135 return rvec;
136 }
137
138 Result total() const { return this->s.total(); }
139};
140
141template <class Stat>
142class DistInfoProxy : public InfoProxy<Stat, DistInfo>
143{
144 public:
145 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
146};
147
148template <class Stat>
149class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
150{
151 public:
152 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
153
154 size_type size() const { return this->s.size(); }
155};
156
157template <class Stat>
158class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
159{
160 public:
161 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
162};
163
164struct StorageParams
165{
166 virtual ~StorageParams();
167};
168
169class InfoAccess
170{
171 protected:
172 /** Set up an info class for this statistic */
173 void setInfo(Info *info);
174 /** Save Storage class parameters if any */
175 void setParams(const StorageParams *params);
176 /** Save Storage class parameters if any */
177 void setInit();
178
179 /** Grab the information class for this statistic */
180 Info *info();
181 /** Grab the information class for this statistic */
182 const Info *info() const;
183
184 public:
185 /**
186 * Reset the stat to the default state.
187 */
188 void reset() { }
189
190 /**
191 * @return true if this stat has a value and satisfies its
192 * requirement as a prereq
193 */
194 bool zero() const { return true; }
195
196 /**
197 * Check that this stat has been set up properly and is ready for
198 * use
199 * @return true for success
200 */
201 bool check() const { return true; }
202};
203
204template <class Derived, template <class> class InfoProxyType>
205class DataWrap : public InfoAccess
206{
207 public:
208 typedef InfoProxyType<Derived> Info;
209
210 protected:
211 Derived &self() { return *static_cast<Derived *>(this); }
212
213 protected:
214 Info *
215 info()
216 {
217 return safe_cast<Info *>(InfoAccess::info());
218 }
219
220 public:
221 const Info *
222 info() const
223 {
224 return safe_cast<const Info *>(InfoAccess::info());
225 }
226
227 protected:
228 /**
229 * Copy constructor, copies are not allowed.
230 */
231 DataWrap(const DataWrap &stat);
232
233 /**
234 * Can't copy stats.
235 */
236 void operator=(const DataWrap &);
237
238 public:
239 DataWrap()
240 {
241 this->setInfo(new Info(self()));
242 }
243
244 /**
245 * Set the name and marks this stat to print at the end of simulation.
246 * @param name The new name.
247 * @return A reference to this stat.
248 */
249 Derived &
250 name(const std::string &name)
251 {
252 Info *info = this->info();
253 info->setName(name);
254 info->flags.set(display);
255 return this->self();
256 }
257 const std::string &name() const { return this->info()->name; }
258
259 /**
260 * Set the character(s) used between the name and vector number
261 * on vectors, dist, etc.
262 * @param _sep The new separator string
263 * @return A reference to this stat.
264 */
265 Derived &
266 setSeparator(const std::string &_sep)
267 {
268 this->info()->setSeparator(_sep);
269 return this->self();
270 }
271 const std::string &setSeparator() const
272 {
273 return this->info()->separatorString;
274 }
275
276 /**
277 * Set the description and marks this stat to print at the end of
278 * simulation.
279 * @param desc The new description.
280 * @return A reference to this stat.
281 */
282 Derived &
283 desc(const std::string &_desc)
284 {
285 this->info()->desc = _desc;
286 return this->self();
287 }
288
289 /**
290 * Set the precision and marks this stat to print at the end of simulation.
291 * @param _precision The new precision
292 * @return A reference to this stat.
293 */
294 Derived &
295 precision(int _precision)
296 {
297 this->info()->precision = _precision;
298 return this->self();
299 }
300
301 /**
302 * Set the flags and marks this stat to print at the end of simulation.
303 * @param f The new flags.
304 * @return A reference to this stat.
305 */
306 Derived &
307 flags(Flags _flags)
308 {
309 this->info()->flags.set(_flags);
310 return this->self();
311 }
312
313 /**
314 * Set the prerequisite stat and marks this stat to print at the end of
315 * simulation.
316 * @param prereq The prerequisite stat.
317 * @return A reference to this stat.
318 */
319 template <class Stat>
320 Derived &
321 prereq(const Stat &prereq)
322 {
323 this->info()->prereq = prereq.info();
324 return this->self();
325 }
326};
327
328template <class Derived, template <class> class InfoProxyType>
329class DataWrapVec : public DataWrap<Derived, InfoProxyType>
330{
331 public:
332 typedef InfoProxyType<Derived> Info;
333
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/** @file
32 * Declaration of Statistics objects.
33 */
34
35/**
36* @todo
37*
38* Generalized N-dimensinal vector
39* documentation
40* key stats
41* interval stats
42* -- these both can use the same function that prints out a
43* specific set of stats
44* VectorStandardDeviation totals
45* Document Namespaces
46*/
47#ifndef __BASE_STATISTICS_HH__
48#define __BASE_STATISTICS_HH__
49
50#include <algorithm>
51#include <cassert>
52#ifdef __SUNPRO_CC
53#include <math.h>
54#endif
55#include <cmath>
56#include <functional>
57#include <iosfwd>
58#include <list>
59#include <map>
60#include <string>
61#include <vector>
62
63#include "base/stats/info.hh"
64#include "base/stats/output.hh"
65#include "base/stats/types.hh"
66#include "base/cast.hh"
67#include "base/cprintf.hh"
68#include "base/intmath.hh"
69#include "base/refcnt.hh"
70#include "base/str.hh"
71#include "base/types.hh"
72
73class Callback;
74
75/** The current simulated tick. */
76extern Tick curTick();
77
78/* A namespace for all of the Statistics */
79namespace Stats {
80
81template <class Stat, class Base>
82class InfoProxy : public Base
83{
84 protected:
85 Stat &s;
86
87 public:
88 InfoProxy(Stat &stat) : s(stat) {}
89
90 bool check() const { return s.check(); }
91 void prepare() { s.prepare(); }
92 void reset() { s.reset(); }
93 void
94 visit(Output &visitor)
95 {
96 visitor.visit(*static_cast<Base *>(this));
97 }
98 bool zero() const { return s.zero(); }
99};
100
101template <class Stat>
102class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
103{
104 public:
105 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
106
107 Counter value() const { return this->s.value(); }
108 Result result() const { return this->s.result(); }
109 Result total() const { return this->s.total(); }
110};
111
112template <class Stat>
113class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
114{
115 protected:
116 mutable VCounter cvec;
117 mutable VResult rvec;
118
119 public:
120 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
121
122 size_type size() const { return this->s.size(); }
123
124 VCounter &
125 value() const
126 {
127 this->s.value(cvec);
128 return cvec;
129 }
130
131 const VResult &
132 result() const
133 {
134 this->s.result(rvec);
135 return rvec;
136 }
137
138 Result total() const { return this->s.total(); }
139};
140
141template <class Stat>
142class DistInfoProxy : public InfoProxy<Stat, DistInfo>
143{
144 public:
145 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
146};
147
148template <class Stat>
149class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
150{
151 public:
152 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
153
154 size_type size() const { return this->s.size(); }
155};
156
157template <class Stat>
158class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
159{
160 public:
161 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
162};
163
164struct StorageParams
165{
166 virtual ~StorageParams();
167};
168
169class InfoAccess
170{
171 protected:
172 /** Set up an info class for this statistic */
173 void setInfo(Info *info);
174 /** Save Storage class parameters if any */
175 void setParams(const StorageParams *params);
176 /** Save Storage class parameters if any */
177 void setInit();
178
179 /** Grab the information class for this statistic */
180 Info *info();
181 /** Grab the information class for this statistic */
182 const Info *info() const;
183
184 public:
185 /**
186 * Reset the stat to the default state.
187 */
188 void reset() { }
189
190 /**
191 * @return true if this stat has a value and satisfies its
192 * requirement as a prereq
193 */
194 bool zero() const { return true; }
195
196 /**
197 * Check that this stat has been set up properly and is ready for
198 * use
199 * @return true for success
200 */
201 bool check() const { return true; }
202};
203
204template <class Derived, template <class> class InfoProxyType>
205class DataWrap : public InfoAccess
206{
207 public:
208 typedef InfoProxyType<Derived> Info;
209
210 protected:
211 Derived &self() { return *static_cast<Derived *>(this); }
212
213 protected:
214 Info *
215 info()
216 {
217 return safe_cast<Info *>(InfoAccess::info());
218 }
219
220 public:
221 const Info *
222 info() const
223 {
224 return safe_cast<const Info *>(InfoAccess::info());
225 }
226
227 protected:
228 /**
229 * Copy constructor, copies are not allowed.
230 */
231 DataWrap(const DataWrap &stat);
232
233 /**
234 * Can't copy stats.
235 */
236 void operator=(const DataWrap &);
237
238 public:
239 DataWrap()
240 {
241 this->setInfo(new Info(self()));
242 }
243
244 /**
245 * Set the name and marks this stat to print at the end of simulation.
246 * @param name The new name.
247 * @return A reference to this stat.
248 */
249 Derived &
250 name(const std::string &name)
251 {
252 Info *info = this->info();
253 info->setName(name);
254 info->flags.set(display);
255 return this->self();
256 }
257 const std::string &name() const { return this->info()->name; }
258
259 /**
260 * Set the character(s) used between the name and vector number
261 * on vectors, dist, etc.
262 * @param _sep The new separator string
263 * @return A reference to this stat.
264 */
265 Derived &
266 setSeparator(const std::string &_sep)
267 {
268 this->info()->setSeparator(_sep);
269 return this->self();
270 }
271 const std::string &setSeparator() const
272 {
273 return this->info()->separatorString;
274 }
275
276 /**
277 * Set the description and marks this stat to print at the end of
278 * simulation.
279 * @param desc The new description.
280 * @return A reference to this stat.
281 */
282 Derived &
283 desc(const std::string &_desc)
284 {
285 this->info()->desc = _desc;
286 return this->self();
287 }
288
289 /**
290 * Set the precision and marks this stat to print at the end of simulation.
291 * @param _precision The new precision
292 * @return A reference to this stat.
293 */
294 Derived &
295 precision(int _precision)
296 {
297 this->info()->precision = _precision;
298 return this->self();
299 }
300
301 /**
302 * Set the flags and marks this stat to print at the end of simulation.
303 * @param f The new flags.
304 * @return A reference to this stat.
305 */
306 Derived &
307 flags(Flags _flags)
308 {
309 this->info()->flags.set(_flags);
310 return this->self();
311 }
312
313 /**
314 * Set the prerequisite stat and marks this stat to print at the end of
315 * simulation.
316 * @param prereq The prerequisite stat.
317 * @return A reference to this stat.
318 */
319 template <class Stat>
320 Derived &
321 prereq(const Stat &prereq)
322 {
323 this->info()->prereq = prereq.info();
324 return this->self();
325 }
326};
327
328template <class Derived, template <class> class InfoProxyType>
329class DataWrapVec : public DataWrap<Derived, InfoProxyType>
330{
331 public:
332 typedef InfoProxyType<Derived> Info;
333
334 DataWrapVec()
335 {}
336
337 DataWrapVec(const DataWrapVec &ref)
338 {}
339
334 // The following functions are specific to vectors. If you use them
335 // in a non vector context, you will get a nice compiler error!
336
337 /**
338 * Set the subfield name for the given index, and marks this stat to print
339 * at the end of simulation.
340 * @param index The subfield index.
341 * @param name The new name of the subfield.
342 * @return A reference to this stat.
343 */
344 Derived &
345 subname(off_type index, const std::string &name)
346 {
347 Derived &self = this->self();
348 Info *info = self.info();
349
350 std::vector<std::string> &subn = info->subnames;
351 if (subn.size() <= index)
352 subn.resize(index + 1);
353 subn[index] = name;
354 return self;
355 }
356
357 // The following functions are specific to 2d vectors. If you use
358 // them in a non vector context, you will get a nice compiler
359 // error because info doesn't have the right variables.
360
361 /**
362 * Set the subfield description for the given index and marks this stat to
363 * print at the end of simulation.
364 * @param index The subfield index.
365 * @param desc The new description of the subfield
366 * @return A reference to this stat.
367 */
368 Derived &
369 subdesc(off_type index, const std::string &desc)
370 {
371 Info *info = this->info();
372
373 std::vector<std::string> &subd = info->subdescs;
374 if (subd.size() <= index)
375 subd.resize(index + 1);
376 subd[index] = desc;
377
378 return this->self();
379 }
380
381 void
382 prepare()
383 {
384 Derived &self = this->self();
385 Info *info = this->info();
386
387 size_t size = self.size();
388 for (off_type i = 0; i < size; ++i)
389 self.data(i)->prepare(info);
390 }
391
392 void
393 reset()
394 {
395 Derived &self = this->self();
396 Info *info = this->info();
397
398 size_t size = self.size();
399 for (off_type i = 0; i < size; ++i)
400 self.data(i)->reset(info);
401 }
402};
403
404template <class Derived, template <class> class InfoProxyType>
405class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
406{
407 public:
408 typedef InfoProxyType<Derived> Info;
409
410 /**
411 * @warning This makes the assumption that if you're gonna subnames a 2d
412 * vector, you're subnaming across all y
413 */
414 Derived &
415 ysubnames(const char **names)
416 {
417 Derived &self = this->self();
418 Info *info = this->info();
419
420 info->y_subnames.resize(self.y);
421 for (off_type i = 0; i < self.y; ++i)
422 info->y_subnames[i] = names[i];
423 return self;
424 }
425
426 Derived &
427 ysubname(off_type index, const std::string &subname)
428 {
429 Derived &self = this->self();
430 Info *info = this->info();
431
432 assert(index < self.y);
433 info->y_subnames.resize(self.y);
434 info->y_subnames[index] = subname.c_str();
435 return self;
436 }
437
438 std::string
439 ysubname(off_type i) const
440 {
441 return this->info()->y_subnames[i];
442 }
443
444};
445
446//////////////////////////////////////////////////////////////////////
447//
448// Simple Statistics
449//
450//////////////////////////////////////////////////////////////////////
451
452/**
453 * Templatized storage and interface for a simple scalar stat.
454 */
455class StatStor
456{
457 private:
458 /** The statistic value. */
459 Counter data;
460
461 public:
462 struct Params : public StorageParams {};
463
464 public:
465 /**
466 * Builds this storage element and calls the base constructor of the
467 * datatype.
468 */
469 StatStor(Info *info)
470 : data(Counter())
471 { }
472
473 /**
474 * The the stat to the given value.
475 * @param val The new value.
476 */
477 void set(Counter val) { data = val; }
478 /**
479 * Increment the stat by the given value.
480 * @param val The new value.
481 */
482 void inc(Counter val) { data += val; }
483 /**
484 * Decrement the stat by the given value.
485 * @param val The new value.
486 */
487 void dec(Counter val) { data -= val; }
488 /**
489 * Return the value of this stat as its base type.
490 * @return The value of this stat.
491 */
492 Counter value() const { return data; }
493 /**
494 * Return the value of this stat as a result type.
495 * @return The value of this stat.
496 */
497 Result result() const { return (Result)data; }
498 /**
499 * Prepare stat data for dumping or serialization
500 */
501 void prepare(Info *info) { }
502 /**
503 * Reset stat value to default
504 */
505 void reset(Info *info) { data = Counter(); }
506
507 /**
508 * @return true if zero value
509 */
510 bool zero() const { return data == Counter(); }
511};
512
513/**
514 * Templatized storage and interface to a per-tick average stat. This keeps
515 * a current count and updates a total (count * ticks) when this count
516 * changes. This allows the quick calculation of a per tick count of the item
517 * being watched. This is good for keeping track of residencies in structures
518 * among other things.
519 */
520class AvgStor
521{
522 private:
523 /** The current count. */
524 Counter current;
525 /** The tick of the last reset */
526 Tick lastReset;
527 /** The total count for all tick. */
528 mutable Result total;
529 /** The tick that current last changed. */
530 mutable Tick last;
531
532 public:
533 struct Params : public StorageParams {};
534
535 public:
536 /**
537 * Build and initializes this stat storage.
538 */
539 AvgStor(Info *info)
540 : current(0), lastReset(0), total(0), last(0)
541 { }
542
543 /**
544 * Set the current count to the one provided, update the total and last
545 * set values.
546 * @param val The new count.
547 */
548 void
549 set(Counter val)
550 {
551 total += current * (curTick() - last);
552 last = curTick();
553 current = val;
554 }
555
556 /**
557 * Increment the current count by the provided value, calls set.
558 * @param val The amount to increment.
559 */
560 void inc(Counter val) { set(current + val); }
561
562 /**
563 * Deccrement the current count by the provided value, calls set.
564 * @param val The amount to decrement.
565 */
566 void dec(Counter val) { set(current - val); }
567
568 /**
569 * Return the current count.
570 * @return The current count.
571 */
572 Counter value() const { return current; }
573
574 /**
575 * Return the current average.
576 * @return The current average.
577 */
578 Result
579 result() const
580 {
581 assert(last == curTick());
582 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
583 }
584
585 /**
586 * @return true if zero value
587 */
588 bool zero() const { return total == 0.0; }
589
590 /**
591 * Prepare stat data for dumping or serialization
592 */
593 void
594 prepare(Info *info)
595 {
596 total += current * (curTick() - last);
597 last = curTick();
598 }
599
600 /**
601 * Reset stat value to default
602 */
603 void
604 reset(Info *info)
605 {
606 total = 0.0;
607 last = curTick();
608 lastReset = curTick();
609 }
610
611};
612
613/**
614 * Implementation of a scalar stat. The type of stat is determined by the
615 * Storage template.
616 */
617template <class Derived, class Stor>
618class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
619{
620 public:
621 typedef Stor Storage;
622 typedef typename Stor::Params Params;
623
624 protected:
625 /** The storage of this stat. */
626 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
627
628 protected:
629 /**
630 * Retrieve the storage.
631 * @param index The vector index to access.
632 * @return The storage object at the given index.
633 */
634 Storage *
635 data()
636 {
637 return reinterpret_cast<Storage *>(storage);
638 }
639
640 /**
641 * Retrieve a const pointer to the storage.
642 * for the given index.
643 * @param index The vector index to access.
644 * @return A const pointer to the storage object at the given index.
645 */
646 const Storage *
647 data() const
648 {
649 return reinterpret_cast<const Storage *>(storage);
650 }
651
652 void
653 doInit()
654 {
655 new (storage) Storage(this->info());
656 this->setInit();
657 }
658
659 public:
660 /**
661 * Return the current value of this stat as its base type.
662 * @return The current value.
663 */
664 Counter value() const { return data()->value(); }
665
666 public:
667 ScalarBase()
668 {
669 this->doInit();
670 }
671
672 public:
673 // Common operators for stats
674 /**
675 * Increment the stat by 1. This calls the associated storage object inc
676 * function.
677 */
678 void operator++() { data()->inc(1); }
679 /**
680 * Decrement the stat by 1. This calls the associated storage object dec
681 * function.
682 */
683 void operator--() { data()->dec(1); }
684
685 /** Increment the stat by 1. */
686 void operator++(int) { ++*this; }
687 /** Decrement the stat by 1. */
688 void operator--(int) { --*this; }
689
690 /**
691 * Set the data value to the given value. This calls the associated storage
692 * object set function.
693 * @param v The new value.
694 */
695 template <typename U>
696 void operator=(const U &v) { data()->set(v); }
697
698 /**
699 * Increment the stat by the given value. This calls the associated
700 * storage object inc function.
701 * @param v The value to add.
702 */
703 template <typename U>
704 void operator+=(const U &v) { data()->inc(v); }
705
706 /**
707 * Decrement the stat by the given value. This calls the associated
708 * storage object dec function.
709 * @param v The value to substract.
710 */
711 template <typename U>
712 void operator-=(const U &v) { data()->dec(v); }
713
714 /**
715 * Return the number of elements, always 1 for a scalar.
716 * @return 1.
717 */
718 size_type size() const { return 1; }
719
720 Counter value() { return data()->value(); }
721
722 Result result() { return data()->result(); }
723
724 Result total() { return result(); }
725
726 bool zero() { return result() == 0.0; }
727
728 void reset() { data()->reset(this->info()); }
729 void prepare() { data()->prepare(this->info()); }
730};
731
732class ProxyInfo : public ScalarInfo
733{
734 public:
735 std::string str() const { return to_string(value()); }
736 size_type size() const { return 1; }
737 bool check() const { return true; }
738 void prepare() { }
739 void reset() { }
740 bool zero() const { return value() == 0; }
741
742 void visit(Output &visitor) { visitor.visit(*this); }
743};
744
745template <class T>
746class ValueProxy : public ProxyInfo
747{
748 private:
749 T *scalar;
750
751 public:
752 ValueProxy(T &val) : scalar(&val) {}
753 Counter value() const { return *scalar; }
754 Result result() const { return *scalar; }
755 Result total() const { return *scalar; }
756};
757
758template <class T>
759class FunctorProxy : public ProxyInfo
760{
761 private:
762 T *functor;
763
764 public:
765 FunctorProxy(T &func) : functor(&func) {}
766 Counter value() const { return (*functor)(); }
767 Result result() const { return (*functor)(); }
768 Result total() const { return (*functor)(); }
769};
770
771template <class Derived>
772class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
773{
774 private:
775 ProxyInfo *proxy;
776
777 public:
778 ValueBase() : proxy(NULL) { }
779 ~ValueBase() { if (proxy) delete proxy; }
780
781 template <class T>
782 Derived &
783 scalar(T &value)
784 {
785 proxy = new ValueProxy<T>(value);
786 this->setInit();
787 return this->self();
788 }
789
790 template <class T>
791 Derived &
792 functor(T &func)
793 {
794 proxy = new FunctorProxy<T>(func);
795 this->setInit();
796 return this->self();
797 }
798
799 Counter value() { return proxy->value(); }
800 Result result() const { return proxy->result(); }
801 Result total() const { return proxy->total(); };
802 size_type size() const { return proxy->size(); }
803
804 std::string str() const { return proxy->str(); }
805 bool zero() const { return proxy->zero(); }
806 bool check() const { return proxy != NULL; }
807 void prepare() { }
808 void reset() { }
809};
810
811//////////////////////////////////////////////////////////////////////
812//
813// Vector Statistics
814//
815//////////////////////////////////////////////////////////////////////
816
817/**
818 * A proxy class to access the stat at a given index in a VectorBase stat.
819 * Behaves like a ScalarBase.
820 */
821template <class Stat>
822class ScalarProxy
823{
824 private:
825 /** Pointer to the parent Vector. */
826 Stat &stat;
827
828 /** The index to access in the parent VectorBase. */
829 off_type index;
830
831 public:
832 /**
833 * Return the current value of this stat as its base type.
834 * @return The current value.
835 */
836 Counter value() const { return stat.data(index)->value(); }
837
838 /**
839 * Return the current value of this statas a result type.
840 * @return The current value.
841 */
842 Result result() const { return stat.data(index)->result(); }
843
844 public:
845 /**
846 * Create and initialize this proxy, do not register it with the database.
847 * @param i The index to access.
848 */
849 ScalarProxy(Stat &s, off_type i)
850 : stat(s), index(i)
851 {
852 }
853
854 /**
855 * Create a copy of the provided ScalarProxy.
856 * @param sp The proxy to copy.
857 */
858 ScalarProxy(const ScalarProxy &sp)
859 : stat(sp.stat), index(sp.index)
860 {}
861
862 /**
863 * Set this proxy equal to the provided one.
864 * @param sp The proxy to copy.
865 * @return A reference to this proxy.
866 */
867 const ScalarProxy &
868 operator=(const ScalarProxy &sp)
869 {
870 stat = sp.stat;
871 index = sp.index;
872 return *this;
873 }
874
875 public:
876 // Common operators for stats
877 /**
878 * Increment the stat by 1. This calls the associated storage object inc
879 * function.
880 */
881 void operator++() { stat.data(index)->inc(1); }
882 /**
883 * Decrement the stat by 1. This calls the associated storage object dec
884 * function.
885 */
886 void operator--() { stat.data(index)->dec(1); }
887
888 /** Increment the stat by 1. */
889 void operator++(int) { ++*this; }
890 /** Decrement the stat by 1. */
891 void operator--(int) { --*this; }
892
893 /**
894 * Set the data value to the given value. This calls the associated storage
895 * object set function.
896 * @param v The new value.
897 */
898 template <typename U>
899 void
900 operator=(const U &v)
901 {
902 stat.data(index)->set(v);
903 }
904
905 /**
906 * Increment the stat by the given value. This calls the associated
907 * storage object inc function.
908 * @param v The value to add.
909 */
910 template <typename U>
911 void
912 operator+=(const U &v)
913 {
914 stat.data(index)->inc(v);
915 }
916
917 /**
918 * Decrement the stat by the given value. This calls the associated
919 * storage object dec function.
920 * @param v The value to substract.
921 */
922 template <typename U>
923 void
924 operator-=(const U &v)
925 {
926 stat.data(index)->dec(v);
927 }
928
929 /**
930 * Return the number of elements, always 1 for a scalar.
931 * @return 1.
932 */
933 size_type size() const { return 1; }
934
935 public:
936 std::string
937 str() const
938 {
939 return csprintf("%s[%d]", stat.info()->name, index);
940 }
941};
942
943/**
944 * Implementation of a vector of stats. The type of stat is determined by the
945 * Storage class. @sa ScalarBase
946 */
947template <class Derived, class Stor>
948class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
949{
950 public:
951 typedef Stor Storage;
952 typedef typename Stor::Params Params;
953
954 /** Proxy type */
955 typedef ScalarProxy<Derived> Proxy;
956 friend class ScalarProxy<Derived>;
957 friend class DataWrapVec<Derived, VectorInfoProxy>;
958
959 protected:
960 /** The storage of this stat. */
961 Storage *storage;
962 size_type _size;
963
964 protected:
965 /**
966 * Retrieve the storage.
967 * @param index The vector index to access.
968 * @return The storage object at the given index.
969 */
970 Storage *data(off_type index) { return &storage[index]; }
971
972 /**
973 * Retrieve a const pointer to the storage.
974 * @param index The vector index to access.
975 * @return A const pointer to the storage object at the given index.
976 */
977 const Storage *data(off_type index) const { return &storage[index]; }
978
979 void
980 doInit(size_type s)
981 {
982 assert(s > 0 && "size must be positive!");
983 assert(!storage && "already initialized");
984 _size = s;
985
986 char *ptr = new char[_size * sizeof(Storage)];
987 storage = reinterpret_cast<Storage *>(ptr);
988
989 for (off_type i = 0; i < _size; ++i)
990 new (&storage[i]) Storage(this->info());
991
992 this->setInit();
993 }
994
995 public:
996 void
997 value(VCounter &vec) const
998 {
999 vec.resize(size());
1000 for (off_type i = 0; i < size(); ++i)
1001 vec[i] = data(i)->value();
1002 }
1003
1004 /**
1005 * Copy the values to a local vector and return a reference to it.
1006 * @return A reference to a vector of the stat values.
1007 */
1008 void
1009 result(VResult &vec) const
1010 {
1011 vec.resize(size());
1012 for (off_type i = 0; i < size(); ++i)
1013 vec[i] = data(i)->result();
1014 }
1015
1016 /**
1017 * Return a total of all entries in this vector.
1018 * @return The total of all vector entries.
1019 */
1020 Result
1021 total() const
1022 {
1023 Result total = 0.0;
1024 for (off_type i = 0; i < size(); ++i)
1025 total += data(i)->result();
1026 return total;
1027 }
1028
1029 /**
1030 * @return the number of elements in this vector.
1031 */
1032 size_type size() const { return _size; }
1033
1034 bool
1035 zero() const
1036 {
1037 for (off_type i = 0; i < size(); ++i)
1038 if (data(i)->zero())
1039 return false;
1040 return true;
1041 }
1042
1043 bool
1044 check() const
1045 {
1046 return storage != NULL;
1047 }
1048
1049 public:
1050 VectorBase()
1051 : storage(NULL)
1052 {}
1053
1054 ~VectorBase()
1055 {
1056 if (!storage)
1057 return;
1058
1059 for (off_type i = 0; i < _size; ++i)
1060 data(i)->~Storage();
1061 delete [] reinterpret_cast<char *>(storage);
1062 }
1063
1064 /**
1065 * Set this vector to have the given size.
1066 * @param size The new size.
1067 * @return A reference to this stat.
1068 */
1069 Derived &
1070 init(size_type size)
1071 {
1072 Derived &self = this->self();
1073 self.doInit(size);
1074 return self;
1075 }
1076
1077 /**
1078 * Return a reference (ScalarProxy) to the stat at the given index.
1079 * @param index The vector index to access.
1080 * @return A reference of the stat.
1081 */
1082 Proxy
1083 operator[](off_type index)
1084 {
1085 assert (index >= 0 && index < size());
1086 return Proxy(this->self(), index);
1087 }
1088};
1089
1090template <class Stat>
1091class VectorProxy
1092{
1093 private:
1094 Stat &stat;
1095 off_type offset;
1096 size_type len;
1097
1098 private:
1099 mutable VResult vec;
1100
1101 typename Stat::Storage *
1102 data(off_type index)
1103 {
1104 assert(index < len);
1105 return stat.data(offset + index);
1106 }
1107
1108 const typename Stat::Storage *
1109 data(off_type index) const
1110 {
1111 assert(index < len);
1112 return stat.data(offset + index);
1113 }
1114
1115 public:
1116 const VResult &
1117 result() const
1118 {
1119 vec.resize(size());
1120
1121 for (off_type i = 0; i < size(); ++i)
1122 vec[i] = data(i)->result();
1123
1124 return vec;
1125 }
1126
1127 Result
1128 total() const
1129 {
1130 Result total = 0.0;
1131 for (off_type i = 0; i < size(); ++i)
1132 total += data(i)->result();
1133 return total;
1134 }
1135
1136 public:
1137 VectorProxy(Stat &s, off_type o, size_type l)
1138 : stat(s), offset(o), len(l)
1139 {
1140 }
1141
1142 VectorProxy(const VectorProxy &sp)
1143 : stat(sp.stat), offset(sp.offset), len(sp.len)
1144 {
1145 }
1146
1147 const VectorProxy &
1148 operator=(const VectorProxy &sp)
1149 {
1150 stat = sp.stat;
1151 offset = sp.offset;
1152 len = sp.len;
1153 return *this;
1154 }
1155
1156 ScalarProxy<Stat>
1157 operator[](off_type index)
1158 {
1159 assert (index >= 0 && index < size());
1160 return ScalarProxy<Stat>(stat, offset + index);
1161 }
1162
1163 size_type size() const { return len; }
1164};
1165
1166template <class Derived, class Stor>
1167class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1168{
1169 public:
1170 typedef Vector2dInfoProxy<Derived> Info;
1171 typedef Stor Storage;
1172 typedef typename Stor::Params Params;
1173 typedef VectorProxy<Derived> Proxy;
1174 friend class ScalarProxy<Derived>;
1175 friend class VectorProxy<Derived>;
1176 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1177 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1178
1179 protected:
1180 size_type x;
1181 size_type y;
1182 size_type _size;
1183 Storage *storage;
1184
1185 protected:
1186 Storage *data(off_type index) { return &storage[index]; }
1187 const Storage *data(off_type index) const { return &storage[index]; }
1188
1189 public:
1190 Vector2dBase()
1191 : storage(NULL)
1192 {}
1193
1194 ~Vector2dBase()
1195 {
1196 if (!storage)
1197 return;
1198
1199 for (off_type i = 0; i < _size; ++i)
1200 data(i)->~Storage();
1201 delete [] reinterpret_cast<char *>(storage);
1202 }
1203
1204 Derived &
1205 init(size_type _x, size_type _y)
1206 {
1207 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1208 assert(!storage && "already initialized");
1209
1210 Derived &self = this->self();
1211 Info *info = this->info();
1212
1213 x = _x;
1214 y = _y;
1215 info->x = _x;
1216 info->y = _y;
1217 _size = x * y;
1218
1219 char *ptr = new char[_size * sizeof(Storage)];
1220 storage = reinterpret_cast<Storage *>(ptr);
1221
1222 for (off_type i = 0; i < _size; ++i)
1223 new (&storage[i]) Storage(info);
1224
1225 this->setInit();
1226
1227 return self;
1228 }
1229
1230 Proxy
1231 operator[](off_type index)
1232 {
1233 off_type offset = index * y;
1234 assert (index >= 0 && offset + y <= size());
1235 return Proxy(this->self(), offset, y);
1236 }
1237
1238
1239 size_type
1240 size() const
1241 {
1242 return _size;
1243 }
1244
1245 bool
1246 zero() const
1247 {
1248 return data(0)->zero();
1249#if 0
1250 for (off_type i = 0; i < size(); ++i)
1251 if (!data(i)->zero())
1252 return false;
1253 return true;
1254#endif
1255 }
1256
1257 void
1258 prepare()
1259 {
1260 Info *info = this->info();
1261 size_type size = this->size();
1262
1263 for (off_type i = 0; i < size; ++i)
1264 data(i)->prepare(info);
1265
1266 info->cvec.resize(size);
1267 for (off_type i = 0; i < size; ++i)
1268 info->cvec[i] = data(i)->value();
1269 }
1270
1271 /**
1272 * Reset stat value to default
1273 */
1274 void
1275 reset()
1276 {
1277 Info *info = this->info();
1278 size_type size = this->size();
1279 for (off_type i = 0; i < size; ++i)
1280 data(i)->reset(info);
1281 }
1282
1283 bool
1284 check() const
1285 {
1286 return storage != NULL;
1287 }
1288};
1289
1290//////////////////////////////////////////////////////////////////////
1291//
1292// Non formula statistics
1293//
1294//////////////////////////////////////////////////////////////////////
1295/** The parameters for a distribution stat. */
1296struct DistParams : public StorageParams
1297{
1298 const DistType type;
1299 DistParams(DistType t) : type(t) {}
1300};
1301
1302/**
1303 * Templatized storage and interface for a distrbution stat.
1304 */
1305class DistStor
1306{
1307 public:
1308 /** The parameters for a distribution stat. */
1309 struct Params : public DistParams
1310 {
1311 /** The minimum value to track. */
1312 Counter min;
1313 /** The maximum value to track. */
1314 Counter max;
1315 /** The number of entries in each bucket. */
1316 Counter bucket_size;
1317 /** The number of buckets. Equal to (max-min)/bucket_size. */
1318 size_type buckets;
1319
1320 Params() : DistParams(Dist) {}
1321 };
1322
1323 private:
1324 /** The minimum value to track. */
1325 Counter min_track;
1326 /** The maximum value to track. */
1327 Counter max_track;
1328 /** The number of entries in each bucket. */
1329 Counter bucket_size;
1330
1331 /** The smallest value sampled. */
1332 Counter min_val;
1333 /** The largest value sampled. */
1334 Counter max_val;
1335 /** The number of values sampled less than min. */
1336 Counter underflow;
1337 /** The number of values sampled more than max. */
1338 Counter overflow;
1339 /** The current sum. */
1340 Counter sum;
1341 /** The sum of squares. */
1342 Counter squares;
1343 /** The number of samples. */
1344 Counter samples;
1345 /** Counter for each bucket. */
1346 VCounter cvec;
1347
1348 public:
1349 DistStor(Info *info)
1350 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1351 {
1352 reset(info);
1353 }
1354
1355 /**
1356 * Add a value to the distribution for the given number of times.
1357 * @param val The value to add.
1358 * @param number The number of times to add the value.
1359 */
1360 void
1361 sample(Counter val, int number)
1362 {
1363 if (val < min_track)
1364 underflow += number;
1365 else if (val > max_track)
1366 overflow += number;
1367 else {
1368 size_type index =
1369 (size_type)std::floor((val - min_track) / bucket_size);
1370 assert(index < size());
1371 cvec[index] += number;
1372 }
1373
1374 if (val < min_val)
1375 min_val = val;
1376
1377 if (val > max_val)
1378 max_val = val;
1379
1380 sum += val * number;
1381 squares += val * val * number;
1382 samples += number;
1383 }
1384
1385 /**
1386 * Return the number of buckets in this distribution.
1387 * @return the number of buckets.
1388 */
1389 size_type size() const { return cvec.size(); }
1390
1391 /**
1392 * Returns true if any calls to sample have been made.
1393 * @return True if any values have been sampled.
1394 */
1395 bool
1396 zero() const
1397 {
1398 return samples == Counter();
1399 }
1400
1401 void
1402 prepare(Info *info, DistData &data)
1403 {
1404 const Params *params = safe_cast<const Params *>(info->storageParams);
1405
1406 assert(params->type == Dist);
1407 data.type = params->type;
1408 data.min = params->min;
1409 data.max = params->max;
1410 data.bucket_size = params->bucket_size;
1411
1412 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1413 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1414 data.underflow = underflow;
1415 data.overflow = overflow;
1416
1417 data.cvec.resize(params->buckets);
1418 for (off_type i = 0; i < params->buckets; ++i)
1419 data.cvec[i] = cvec[i];
1420
1421 data.sum = sum;
1422 data.squares = squares;
1423 data.samples = samples;
1424 }
1425
1426 /**
1427 * Reset stat value to default
1428 */
1429 void
1430 reset(Info *info)
1431 {
1432 const Params *params = safe_cast<const Params *>(info->storageParams);
1433 min_track = params->min;
1434 max_track = params->max;
1435 bucket_size = params->bucket_size;
1436
1437 min_val = CounterLimits::max();
1438 max_val = CounterLimits::min();
1439 underflow = Counter();
1440 overflow = Counter();
1441
1442 size_type size = cvec.size();
1443 for (off_type i = 0; i < size; ++i)
1444 cvec[i] = Counter();
1445
1446 sum = Counter();
1447 squares = Counter();
1448 samples = Counter();
1449 }
1450};
1451
1452/**
1453 * Templatized storage and interface for a histogram stat.
1454 */
1455class HistStor
1456{
1457 public:
1458 /** The parameters for a distribution stat. */
1459 struct Params : public DistParams
1460 {
1461 /** The number of buckets.. */
1462 size_type buckets;
1463
1464 Params() : DistParams(Hist) {}
1465 };
1466
1467 private:
1468 /** The minimum value to track. */
1469 Counter min_bucket;
1470 /** The maximum value to track. */
1471 Counter max_bucket;
1472 /** The number of entries in each bucket. */
1473 Counter bucket_size;
1474
1475 /** The current sum. */
1476 Counter sum;
1477 /** The sum of logarithm of each sample, used to compute geometric mean. */
1478 Counter logs;
1479 /** The sum of squares. */
1480 Counter squares;
1481 /** The number of samples. */
1482 Counter samples;
1483 /** Counter for each bucket. */
1484 VCounter cvec;
1485
1486 public:
1487 HistStor(Info *info)
1488 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1489 {
1490 reset(info);
1491 }
1492
1493 void grow_up();
1494 void grow_out();
1495 void grow_convert();
1496
1497 /**
1498 * Add a value to the distribution for the given number of times.
1499 * @param val The value to add.
1500 * @param number The number of times to add the value.
1501 */
1502 void
1503 sample(Counter val, int number)
1504 {
1505 assert(min_bucket < max_bucket);
1506 if (val < min_bucket) {
1507 if (min_bucket == 0)
1508 grow_convert();
1509
1510 while (val < min_bucket)
1511 grow_out();
1512 } else if (val >= max_bucket + bucket_size) {
1513 if (min_bucket == 0) {
1514 while (val >= max_bucket + bucket_size)
1515 grow_up();
1516 } else {
1517 while (val >= max_bucket + bucket_size)
1518 grow_out();
1519 }
1520 }
1521
1522 size_type index =
1523 (int64_t)std::floor((val - min_bucket) / bucket_size);
1524
1525 assert(index >= 0 && index < size());
1526 cvec[index] += number;
1527
1528 sum += val * number;
1529 squares += val * val * number;
1530 logs += log(val) * number;
1531 samples += number;
1532 }
1533
1534 /**
1535 * Return the number of buckets in this distribution.
1536 * @return the number of buckets.
1537 */
1538 size_type size() const { return cvec.size(); }
1539
1540 /**
1541 * Returns true if any calls to sample have been made.
1542 * @return True if any values have been sampled.
1543 */
1544 bool
1545 zero() const
1546 {
1547 return samples == Counter();
1548 }
1549
1550 void
1551 prepare(Info *info, DistData &data)
1552 {
1553 const Params *params = safe_cast<const Params *>(info->storageParams);
1554
1555 assert(params->type == Hist);
1556 data.type = params->type;
1557 data.min = min_bucket;
1558 data.max = max_bucket + bucket_size - 1;
1559 data.bucket_size = bucket_size;
1560
1561 data.min_val = min_bucket;
1562 data.max_val = max_bucket;
1563
1564 int buckets = params->buckets;
1565 data.cvec.resize(buckets);
1566 for (off_type i = 0; i < buckets; ++i)
1567 data.cvec[i] = cvec[i];
1568
1569 data.sum = sum;
1570 data.logs = logs;
1571 data.squares = squares;
1572 data.samples = samples;
1573 }
1574
1575 /**
1576 * Reset stat value to default
1577 */
1578 void
1579 reset(Info *info)
1580 {
1581 const Params *params = safe_cast<const Params *>(info->storageParams);
1582 min_bucket = 0;
1583 max_bucket = params->buckets - 1;
1584 bucket_size = 1;
1585
1586 size_type size = cvec.size();
1587 for (off_type i = 0; i < size; ++i)
1588 cvec[i] = Counter();
1589
1590 sum = Counter();
1591 squares = Counter();
1592 samples = Counter();
1593 logs = Counter();
1594 }
1595};
1596
1597/**
1598 * Templatized storage and interface for a distribution that calculates mean
1599 * and variance.
1600 */
1601class SampleStor
1602{
1603 public:
1604 struct Params : public DistParams
1605 {
1606 Params() : DistParams(Deviation) {}
1607 };
1608
1609 private:
1610 /** The current sum. */
1611 Counter sum;
1612 /** The sum of squares. */
1613 Counter squares;
1614 /** The number of samples. */
1615 Counter samples;
1616
1617 public:
1618 /**
1619 * Create and initialize this storage.
1620 */
1621 SampleStor(Info *info)
1622 : sum(Counter()), squares(Counter()), samples(Counter())
1623 { }
1624
1625 /**
1626 * Add a value the given number of times to this running average.
1627 * Update the running sum and sum of squares, increment the number of
1628 * values seen by the given number.
1629 * @param val The value to add.
1630 * @param number The number of times to add the value.
1631 */
1632 void
1633 sample(Counter val, int number)
1634 {
1635 Counter value = val * number;
1636 sum += value;
1637 squares += value * value;
1638 samples += number;
1639 }
1640
1641 /**
1642 * Return the number of entries in this stat, 1
1643 * @return 1.
1644 */
1645 size_type size() const { return 1; }
1646
1647 /**
1648 * Return true if no samples have been added.
1649 * @return True if no samples have been added.
1650 */
1651 bool zero() const { return samples == Counter(); }
1652
1653 void
1654 prepare(Info *info, DistData &data)
1655 {
1656 const Params *params = safe_cast<const Params *>(info->storageParams);
1657
1658 assert(params->type == Deviation);
1659 data.type = params->type;
1660 data.sum = sum;
1661 data.squares = squares;
1662 data.samples = samples;
1663 }
1664
1665 /**
1666 * Reset stat value to default
1667 */
1668 void
1669 reset(Info *info)
1670 {
1671 sum = Counter();
1672 squares = Counter();
1673 samples = Counter();
1674 }
1675};
1676
1677/**
1678 * Templatized storage for distribution that calculates per tick mean and
1679 * variance.
1680 */
1681class AvgSampleStor
1682{
1683 public:
1684 struct Params : public DistParams
1685 {
1686 Params() : DistParams(Deviation) {}
1687 };
1688
1689 private:
1690 /** Current total. */
1691 Counter sum;
1692 /** Current sum of squares. */
1693 Counter squares;
1694
1695 public:
1696 /**
1697 * Create and initialize this storage.
1698 */
1699 AvgSampleStor(Info *info)
1700 : sum(Counter()), squares(Counter())
1701 {}
1702
1703 /**
1704 * Add a value to the distribution for the given number of times.
1705 * Update the running sum and sum of squares.
1706 * @param val The value to add.
1707 * @param number The number of times to add the value.
1708 */
1709 void
1710 sample(Counter val, int number)
1711 {
1712 Counter value = val * number;
1713 sum += value;
1714 squares += value * value;
1715 }
1716
1717 /**
1718 * Return the number of entries, in this case 1.
1719 * @return 1.
1720 */
1721 size_type size() const { return 1; }
1722
1723 /**
1724 * Return true if no samples have been added.
1725 * @return True if the sum is zero.
1726 */
1727 bool zero() const { return sum == Counter(); }
1728
1729 void
1730 prepare(Info *info, DistData &data)
1731 {
1732 const Params *params = safe_cast<const Params *>(info->storageParams);
1733
1734 assert(params->type == Deviation);
1735 data.type = params->type;
1736 data.sum = sum;
1737 data.squares = squares;
1738 data.samples = curTick();
1739 }
1740
1741 /**
1742 * Reset stat value to default
1743 */
1744 void
1745 reset(Info *info)
1746 {
1747 sum = Counter();
1748 squares = Counter();
1749 }
1750};
1751
1752/**
1753 * Implementation of a distribution stat. The type of distribution is
1754 * determined by the Storage template. @sa ScalarBase
1755 */
1756template <class Derived, class Stor>
1757class DistBase : public DataWrap<Derived, DistInfoProxy>
1758{
1759 public:
1760 typedef DistInfoProxy<Derived> Info;
1761 typedef Stor Storage;
1762 typedef typename Stor::Params Params;
1763
1764 protected:
1765 /** The storage for this stat. */
1766 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1767
1768 protected:
1769 /**
1770 * Retrieve the storage.
1771 * @return The storage object for this stat.
1772 */
1773 Storage *
1774 data()
1775 {
1776 return reinterpret_cast<Storage *>(storage);
1777 }
1778
1779 /**
1780 * Retrieve a const pointer to the storage.
1781 * @return A const pointer to the storage object for this stat.
1782 */
1783 const Storage *
1784 data() const
1785 {
1786 return reinterpret_cast<const Storage *>(storage);
1787 }
1788
1789 void
1790 doInit()
1791 {
1792 new (storage) Storage(this->info());
1793 this->setInit();
1794 }
1795
1796 public:
1797 DistBase() { }
1798
1799 /**
1800 * Add a value to the distribtion n times. Calls sample on the storage
1801 * class.
1802 * @param v The value to add.
1803 * @param n The number of times to add it, defaults to 1.
1804 */
1805 template <typename U>
1806 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1807
1808 /**
1809 * Return the number of entries in this stat.
1810 * @return The number of entries.
1811 */
1812 size_type size() const { return data()->size(); }
1813 /**
1814 * Return true if no samples have been added.
1815 * @return True if there haven't been any samples.
1816 */
1817 bool zero() const { return data()->zero(); }
1818
1819 void
1820 prepare()
1821 {
1822 Info *info = this->info();
1823 data()->prepare(info, info->data);
1824 }
1825
1826 /**
1827 * Reset stat value to default
1828 */
1829 void
1830 reset()
1831 {
1832 data()->reset(this->info());
1833 }
1834};
1835
1836template <class Stat>
1837class DistProxy;
1838
1839template <class Derived, class Stor>
1840class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1841{
1842 public:
1843 typedef VectorDistInfoProxy<Derived> Info;
1844 typedef Stor Storage;
1845 typedef typename Stor::Params Params;
1846 typedef DistProxy<Derived> Proxy;
1847 friend class DistProxy<Derived>;
1848 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1849
1850 protected:
1851 Storage *storage;
1852 size_type _size;
1853
1854 protected:
1855 Storage *
1856 data(off_type index)
1857 {
1858 return &storage[index];
1859 }
1860
1861 const Storage *
1862 data(off_type index) const
1863 {
1864 return &storage[index];
1865 }
1866
1867 void
1868 doInit(size_type s)
1869 {
1870 assert(s > 0 && "size must be positive!");
1871 assert(!storage && "already initialized");
1872 _size = s;
1873
1874 char *ptr = new char[_size * sizeof(Storage)];
1875 storage = reinterpret_cast<Storage *>(ptr);
1876
1877 Info *info = this->info();
1878 for (off_type i = 0; i < _size; ++i)
1879 new (&storage[i]) Storage(info);
1880
1881 this->setInit();
1882 }
1883
1884 public:
1885 VectorDistBase()
1886 : storage(NULL)
1887 {}
1888
1889 ~VectorDistBase()
1890 {
1891 if (!storage)
1892 return ;
1893
1894 for (off_type i = 0; i < _size; ++i)
1895 data(i)->~Storage();
1896 delete [] reinterpret_cast<char *>(storage);
1897 }
1898
1899 Proxy operator[](off_type index)
1900 {
1901 assert(index >= 0 && index < size());
1902 return Proxy(this->self(), index);
1903 }
1904
1905 size_type
1906 size() const
1907 {
1908 return _size;
1909 }
1910
1911 bool
1912 zero() const
1913 {
1914 for (off_type i = 0; i < size(); ++i)
1915 if (!data(i)->zero())
1916 return false;
1917 return true;
1918 }
1919
1920 void
1921 prepare()
1922 {
1923 Info *info = this->info();
1924 size_type size = this->size();
1925 info->data.resize(size);
1926 for (off_type i = 0; i < size; ++i)
1927 data(i)->prepare(info, info->data[i]);
1928 }
1929
1930 bool
1931 check() const
1932 {
1933 return storage != NULL;
1934 }
1935};
1936
1937template <class Stat>
1938class DistProxy
1939{
1940 private:
1941 Stat &stat;
1942 off_type index;
1943
1944 protected:
1945 typename Stat::Storage *data() { return stat.data(index); }
1946 const typename Stat::Storage *data() const { return stat.data(index); }
1947
1948 public:
1949 DistProxy(Stat &s, off_type i)
1950 : stat(s), index(i)
1951 {}
1952
1953 DistProxy(const DistProxy &sp)
1954 : stat(sp.stat), index(sp.index)
1955 {}
1956
1957 const DistProxy &
1958 operator=(const DistProxy &sp)
1959 {
1960 stat = sp.stat;
1961 index = sp.index;
1962 return *this;
1963 }
1964
1965 public:
1966 template <typename U>
1967 void
1968 sample(const U &v, int n = 1)
1969 {
1970 data()->sample(v, n);
1971 }
1972
1973 size_type
1974 size() const
1975 {
1976 return 1;
1977 }
1978
1979 bool
1980 zero() const
1981 {
1982 return data()->zero();
1983 }
1984
1985 /**
1986 * Proxy has no state. Nothing to reset.
1987 */
1988 void reset() { }
1989};
1990
1991//////////////////////////////////////////////////////////////////////
1992//
1993// Formula Details
1994//
1995//////////////////////////////////////////////////////////////////////
1996
1997/**
1998 * Base class for formula statistic node. These nodes are used to build a tree
1999 * that represents the formula.
2000 */
2001class Node : public RefCounted
2002{
2003 public:
2004 /**
2005 * Return the number of nodes in the subtree starting at this node.
2006 * @return the number of nodes in this subtree.
2007 */
2008 virtual size_type size() const = 0;
2009 /**
2010 * Return the result vector of this subtree.
2011 * @return The result vector of this subtree.
2012 */
2013 virtual const VResult &result() const = 0;
2014 /**
2015 * Return the total of the result vector.
2016 * @return The total of the result vector.
2017 */
2018 virtual Result total() const = 0;
2019
2020 /**
2021 *
2022 */
2023 virtual std::string str() const = 0;
2024};
2025
2026/** Reference counting pointer to a function Node. */
2027typedef RefCountingPtr<Node> NodePtr;
2028
2029class ScalarStatNode : public Node
2030{
2031 private:
2032 const ScalarInfo *data;
2033 mutable VResult vresult;
2034
2035 public:
2036 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2037
2038 const VResult &
2039 result() const
2040 {
2041 vresult[0] = data->result();
2042 return vresult;
2043 }
2044
2045 Result total() const { return data->result(); };
2046
2047 size_type size() const { return 1; }
2048
2049 /**
2050 *
2051 */
2052 std::string str() const { return data->name; }
2053};
2054
2055template <class Stat>
2056class ScalarProxyNode : public Node
2057{
2058 private:
2059 const ScalarProxy<Stat> proxy;
2060 mutable VResult vresult;
2061
2062 public:
2063 ScalarProxyNode(const ScalarProxy<Stat> &p)
2064 : proxy(p), vresult(1)
2065 { }
2066
2067 const VResult &
2068 result() const
2069 {
2070 vresult[0] = proxy.result();
2071 return vresult;
2072 }
2073
2074 Result
2075 total() const
2076 {
2077 return proxy.result();
2078 }
2079
2080 size_type
2081 size() const
2082 {
2083 return 1;
2084 }
2085
2086 /**
2087 *
2088 */
2089 std::string
2090 str() const
2091 {
2092 return proxy.str();
2093 }
2094};
2095
2096class VectorStatNode : public Node
2097{
2098 private:
2099 const VectorInfo *data;
2100
2101 public:
2102 VectorStatNode(const VectorInfo *d) : data(d) { }
2103 const VResult &result() const { return data->result(); }
2104 Result total() const { return data->total(); };
2105
2106 size_type size() const { return data->size(); }
2107
2108 std::string str() const { return data->name; }
2109};
2110
2111template <class T>
2112class ConstNode : public Node
2113{
2114 private:
2115 VResult vresult;
2116
2117 public:
2118 ConstNode(T s) : vresult(1, (Result)s) {}
2119 const VResult &result() const { return vresult; }
2120 Result total() const { return vresult[0]; };
2121 size_type size() const { return 1; }
2122 std::string str() const { return to_string(vresult[0]); }
2123};
2124
2125template <class T>
2126class ConstVectorNode : public Node
2127{
2128 private:
2129 VResult vresult;
2130
2131 public:
2132 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2133 const VResult &result() const { return vresult; }
2134
2135 Result
2136 total() const
2137 {
2138 size_type size = this->size();
2139 Result tmp = 0;
2140 for (off_type i = 0; i < size; i++)
2141 tmp += vresult[i];
2142 return tmp;
2143 }
2144
2145 size_type size() const { return vresult.size(); }
2146 std::string
2147 str() const
2148 {
2149 size_type size = this->size();
2150 std::string tmp = "(";
2151 for (off_type i = 0; i < size; i++)
2152 tmp += csprintf("%s ",to_string(vresult[i]));
2153 tmp += ")";
2154 return tmp;
2155 }
2156};
2157
2158template <class Op>
2159struct OpString;
2160
2161template<>
2162struct OpString<std::plus<Result> >
2163{
2164 static std::string str() { return "+"; }
2165};
2166
2167template<>
2168struct OpString<std::minus<Result> >
2169{
2170 static std::string str() { return "-"; }
2171};
2172
2173template<>
2174struct OpString<std::multiplies<Result> >
2175{
2176 static std::string str() { return "*"; }
2177};
2178
2179template<>
2180struct OpString<std::divides<Result> >
2181{
2182 static std::string str() { return "/"; }
2183};
2184
2185template<>
2186struct OpString<std::modulus<Result> >
2187{
2188 static std::string str() { return "%"; }
2189};
2190
2191template<>
2192struct OpString<std::negate<Result> >
2193{
2194 static std::string str() { return "-"; }
2195};
2196
2197template <class Op>
2198class UnaryNode : public Node
2199{
2200 public:
2201 NodePtr l;
2202 mutable VResult vresult;
2203
2204 public:
2205 UnaryNode(NodePtr &p) : l(p) {}
2206
2207 const VResult &
2208 result() const
2209 {
2210 const VResult &lvec = l->result();
2211 size_type size = lvec.size();
2212
2213 assert(size > 0);
2214
2215 vresult.resize(size);
2216 Op op;
2217 for (off_type i = 0; i < size; ++i)
2218 vresult[i] = op(lvec[i]);
2219
2220 return vresult;
2221 }
2222
2223 Result
2224 total() const
2225 {
2226 const VResult &vec = this->result();
2227 Result total = 0.0;
2228 for (off_type i = 0; i < size(); i++)
2229 total += vec[i];
2230 return total;
2231 }
2232
2233 size_type size() const { return l->size(); }
2234
2235 std::string
2236 str() const
2237 {
2238 return OpString<Op>::str() + l->str();
2239 }
2240};
2241
2242template <class Op>
2243class BinaryNode : public Node
2244{
2245 public:
2246 NodePtr l;
2247 NodePtr r;
2248 mutable VResult vresult;
2249
2250 public:
2251 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2252
2253 const VResult &
2254 result() const
2255 {
2256 Op op;
2257 const VResult &lvec = l->result();
2258 const VResult &rvec = r->result();
2259
2260 assert(lvec.size() > 0 && rvec.size() > 0);
2261
2262 if (lvec.size() == 1 && rvec.size() == 1) {
2263 vresult.resize(1);
2264 vresult[0] = op(lvec[0], rvec[0]);
2265 } else if (lvec.size() == 1) {
2266 size_type size = rvec.size();
2267 vresult.resize(size);
2268 for (off_type i = 0; i < size; ++i)
2269 vresult[i] = op(lvec[0], rvec[i]);
2270 } else if (rvec.size() == 1) {
2271 size_type size = lvec.size();
2272 vresult.resize(size);
2273 for (off_type i = 0; i < size; ++i)
2274 vresult[i] = op(lvec[i], rvec[0]);
2275 } else if (rvec.size() == lvec.size()) {
2276 size_type size = rvec.size();
2277 vresult.resize(size);
2278 for (off_type i = 0; i < size; ++i)
2279 vresult[i] = op(lvec[i], rvec[i]);
2280 }
2281
2282 return vresult;
2283 }
2284
2285 Result
2286 total() const
2287 {
2288 const VResult &vec = this->result();
2289 const VResult &lvec = l->result();
2290 const VResult &rvec = r->result();
2291 Result total = 0.0;
2292 Result lsum = 0.0;
2293 Result rsum = 0.0;
2294 Op op;
2295
2296 assert(lvec.size() > 0 && rvec.size() > 0);
2297 assert(lvec.size() == rvec.size() ||
2298 lvec.size() == 1 || rvec.size() == 1);
2299
2300 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2301 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2302 for (off_type i = 0; i < size(); ++i) {
2303 lsum += lvec[i];
2304 rsum += rvec[i];
2305 }
2306 return op(lsum, rsum);
2307 }
2308
2309 /** Otherwise divide each item by the divisor */
2310 for (off_type i = 0; i < size(); ++i) {
2311 total += vec[i];
2312 }
2313
2314 return total;
2315 }
2316
2317 size_type
2318 size() const
2319 {
2320 size_type ls = l->size();
2321 size_type rs = r->size();
2322 if (ls == 1) {
2323 return rs;
2324 } else if (rs == 1) {
2325 return ls;
2326 } else {
2327 assert(ls == rs && "Node vector sizes are not equal");
2328 return ls;
2329 }
2330 }
2331
2332 std::string
2333 str() const
2334 {
2335 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2336 }
2337};
2338
2339template <class Op>
2340class SumNode : public Node
2341{
2342 public:
2343 NodePtr l;
2344 mutable VResult vresult;
2345
2346 public:
2347 SumNode(NodePtr &p) : l(p), vresult(1) {}
2348
2349 const VResult &
2350 result() const
2351 {
2352 const VResult &lvec = l->result();
2353 size_type size = lvec.size();
2354 assert(size > 0);
2355
2356 vresult[0] = 0.0;
2357
2358 Op op;
2359 for (off_type i = 0; i < size; ++i)
2360 vresult[0] = op(vresult[0], lvec[i]);
2361
2362 return vresult;
2363 }
2364
2365 Result
2366 total() const
2367 {
2368 const VResult &lvec = l->result();
2369 size_type size = lvec.size();
2370 assert(size > 0);
2371
2372 Result result = 0.0;
2373
2374 Op op;
2375 for (off_type i = 0; i < size; ++i)
2376 result = op(result, lvec[i]);
2377
2378 return result;
2379 }
2380
2381 size_type size() const { return 1; }
2382
2383 std::string
2384 str() const
2385 {
2386 return csprintf("total(%s)", l->str());
2387 }
2388};
2389
2390
2391//////////////////////////////////////////////////////////////////////
2392//
2393// Visible Statistics Types
2394//
2395//////////////////////////////////////////////////////////////////////
2396/**
2397 * @defgroup VisibleStats "Statistic Types"
2398 * These are the statistics that are used in the simulator.
2399 * @{
2400 */
2401
2402/**
2403 * This is a simple scalar statistic, like a counter.
2404 * @sa Stat, ScalarBase, StatStor
2405 */
2406class Scalar : public ScalarBase<Scalar, StatStor>
2407{
2408 public:
2409 using ScalarBase<Scalar, StatStor>::operator=;
2410};
2411
2412/**
2413 * A stat that calculates the per tick average of a value.
2414 * @sa Stat, ScalarBase, AvgStor
2415 */
2416class Average : public ScalarBase<Average, AvgStor>
2417{
2418 public:
2419 using ScalarBase<Average, AvgStor>::operator=;
2420};
2421
2422class Value : public ValueBase<Value>
2423{
2424};
2425
2426/**
2427 * A vector of scalar stats.
2428 * @sa Stat, VectorBase, StatStor
2429 */
2430class Vector : public VectorBase<Vector, StatStor>
2431{
2432};
2433
2434/**
2435 * A vector of Average stats.
2436 * @sa Stat, VectorBase, AvgStor
2437 */
2438class AverageVector : public VectorBase<AverageVector, AvgStor>
2439{
2440};
2441
2442/**
2443 * A 2-Dimensional vecto of scalar stats.
2444 * @sa Stat, Vector2dBase, StatStor
2445 */
2446class Vector2d : public Vector2dBase<Vector2d, StatStor>
2447{
2448};
2449
2450/**
2451 * A simple distribution stat.
2452 * @sa Stat, DistBase, DistStor
2453 */
2454class Distribution : public DistBase<Distribution, DistStor>
2455{
2456 public:
2457 /**
2458 * Set the parameters of this distribution. @sa DistStor::Params
2459 * @param min The minimum value of the distribution.
2460 * @param max The maximum value of the distribution.
2461 * @param bkt The number of values in each bucket.
2462 * @return A reference to this distribution.
2463 */
2464 Distribution &
2465 init(Counter min, Counter max, Counter bkt)
2466 {
2467 DistStor::Params *params = new DistStor::Params;
2468 params->min = min;
2469 params->max = max;
2470 params->bucket_size = bkt;
2471 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2472 this->setParams(params);
2473 this->doInit();
2474 return this->self();
2475 }
2476};
2477
2478/**
2479 * A simple histogram stat.
2480 * @sa Stat, DistBase, HistStor
2481 */
2482class Histogram : public DistBase<Histogram, HistStor>
2483{
2484 public:
2485 /**
2486 * Set the parameters of this histogram. @sa HistStor::Params
2487 * @param size The number of buckets in the histogram
2488 * @return A reference to this histogram.
2489 */
2490 Histogram &
2491 init(size_type size)
2492 {
2493 HistStor::Params *params = new HistStor::Params;
2494 params->buckets = size;
2495 this->setParams(params);
2496 this->doInit();
2497 return this->self();
2498 }
2499};
2500
2501/**
2502 * Calculates the mean and variance of all the samples.
2503 * @sa DistBase, SampleStor
2504 */
2505class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2506{
2507 public:
2508 /**
2509 * Construct and initialize this distribution.
2510 */
2511 StandardDeviation()
2512 {
2513 SampleStor::Params *params = new SampleStor::Params;
2514 this->doInit();
2515 this->setParams(params);
2516 }
2517};
2518
2519/**
2520 * Calculates the per tick mean and variance of the samples.
2521 * @sa DistBase, AvgSampleStor
2522 */
2523class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2524{
2525 public:
2526 /**
2527 * Construct and initialize this distribution.
2528 */
2529 AverageDeviation()
2530 {
2531 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2532 this->doInit();
2533 this->setParams(params);
2534 }
2535};
2536
2537/**
2538 * A vector of distributions.
2539 * @sa VectorDistBase, DistStor
2540 */
2541class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2542{
2543 public:
2544 /**
2545 * Initialize storage and parameters for this distribution.
2546 * @param size The size of the vector (the number of distributions).
2547 * @param min The minimum value of the distribution.
2548 * @param max The maximum value of the distribution.
2549 * @param bkt The number of values in each bucket.
2550 * @return A reference to this distribution.
2551 */
2552 VectorDistribution &
2553 init(size_type size, Counter min, Counter max, Counter bkt)
2554 {
2555 DistStor::Params *params = new DistStor::Params;
2556 params->min = min;
2557 params->max = max;
2558 params->bucket_size = bkt;
2559 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2560 this->setParams(params);
2561 this->doInit(size);
2562 return this->self();
2563 }
2564};
2565
2566/**
2567 * This is a vector of StandardDeviation stats.
2568 * @sa VectorDistBase, SampleStor
2569 */
2570class VectorStandardDeviation
2571 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2572{
2573 public:
2574 /**
2575 * Initialize storage for this distribution.
2576 * @param size The size of the vector.
2577 * @return A reference to this distribution.
2578 */
2579 VectorStandardDeviation &
2580 init(size_type size)
2581 {
2582 SampleStor::Params *params = new SampleStor::Params;
2583 this->doInit(size);
2584 this->setParams(params);
2585 return this->self();
2586 }
2587};
2588
2589/**
2590 * This is a vector of AverageDeviation stats.
2591 * @sa VectorDistBase, AvgSampleStor
2592 */
2593class VectorAverageDeviation
2594 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2595{
2596 public:
2597 /**
2598 * Initialize storage for this distribution.
2599 * @param size The size of the vector.
2600 * @return A reference to this distribution.
2601 */
2602 VectorAverageDeviation &
2603 init(size_type size)
2604 {
2605 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2606 this->doInit(size);
2607 this->setParams(params);
2608 return this->self();
2609 }
2610};
2611
2612template <class Stat>
2613class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2614{
2615 protected:
2616 mutable VResult vec;
2617 mutable VCounter cvec;
2618
2619 public:
2620 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2621
2622 size_type size() const { return this->s.size(); }
2623
2624 const VResult &
2625 result() const
2626 {
2627 this->s.result(vec);
2628 return vec;
2629 }
2630 Result total() const { return this->s.total(); }
2631 VCounter &value() const { return cvec; }
2632
2633 std::string str() const { return this->s.str(); }
2634};
2635
2636template <class Stat>
2637class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2638{
2639 public:
2640 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2641};
2642
2643/**
2644 * Implementation of a sparse histogram stat. The storage class is
2645 * determined by the Storage template.
2646 */
2647template <class Derived, class Stor>
2648class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2649{
2650 public:
2651 typedef SparseHistInfoProxy<Derived> Info;
2652 typedef Stor Storage;
2653 typedef typename Stor::Params Params;
2654
2655 protected:
2656 /** The storage for this stat. */
2657 char storage[sizeof(Storage)];
2658
2659 protected:
2660 /**
2661 * Retrieve the storage.
2662 * @return The storage object for this stat.
2663 */
2664 Storage *
2665 data()
2666 {
2667 return reinterpret_cast<Storage *>(storage);
2668 }
2669
2670 /**
2671 * Retrieve a const pointer to the storage.
2672 * @return A const pointer to the storage object for this stat.
2673 */
2674 const Storage *
2675 data() const
2676 {
2677 return reinterpret_cast<const Storage *>(storage);
2678 }
2679
2680 void
2681 doInit()
2682 {
2683 new (storage) Storage(this->info());
2684 this->setInit();
2685 }
2686
2687 public:
2688 SparseHistBase() { }
2689
2690 /**
2691 * Add a value to the distribtion n times. Calls sample on the storage
2692 * class.
2693 * @param v The value to add.
2694 * @param n The number of times to add it, defaults to 1.
2695 */
2696 template <typename U>
2697 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2698
2699 /**
2700 * Return the number of entries in this stat.
2701 * @return The number of entries.
2702 */
2703 size_type size() const { return data()->size(); }
2704 /**
2705 * Return true if no samples have been added.
2706 * @return True if there haven't been any samples.
2707 */
2708 bool zero() const { return data()->zero(); }
2709
2710 void
2711 prepare()
2712 {
2713 Info *info = this->info();
2714 data()->prepare(info, info->data);
2715 }
2716
2717 /**
2718 * Reset stat value to default
2719 */
2720 void
2721 reset()
2722 {
2723 data()->reset(this->info());
2724 }
2725};
2726
2727/**
2728 * Templatized storage and interface for a sparse histogram stat.
2729 */
2730class SparseHistStor
2731{
2732 public:
2733 /** The parameters for a sparse histogram stat. */
2734 struct Params : public DistParams
2735 {
2736 Params() : DistParams(Hist) {}
2737 };
2738
2739 private:
2740 /** Counter for number of samples */
2741 Counter samples;
2742 /** Counter for each bucket. */
2743 MCounter cmap;
2744
2745 public:
2746 SparseHistStor(Info *info)
2747 {
2748 reset(info);
2749 }
2750
2751 /**
2752 * Add a value to the distribution for the given number of times.
2753 * @param val The value to add.
2754 * @param number The number of times to add the value.
2755 */
2756 void
2757 sample(Counter val, int number)
2758 {
2759 cmap[val] += number;
2760 samples += number;
2761 }
2762
2763 /**
2764 * Return the number of buckets in this distribution.
2765 * @return the number of buckets.
2766 */
2767 size_type size() const { return cmap.size(); }
2768
2769 /**
2770 * Returns true if any calls to sample have been made.
2771 * @return True if any values have been sampled.
2772 */
2773 bool
2774 zero() const
2775 {
2776 return samples == Counter();
2777 }
2778
2779 void
2780 prepare(Info *info, SparseHistData &data)
2781 {
2782 MCounter::iterator it;
2783 data.cmap.clear();
2784 for (it = cmap.begin(); it != cmap.end(); it++) {
2785 data.cmap[(*it).first] = (*it).second;
2786 }
2787
2788 data.samples = samples;
2789 }
2790
2791 /**
2792 * Reset stat value to default
2793 */
2794 void
2795 reset(Info *info)
2796 {
2797 cmap.clear();
2798 samples = 0;
2799 }
2800};
2801
2802class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2803{
2804 public:
2805 /**
2806 * Set the parameters of this histogram. @sa HistStor::Params
2807 * @param size The number of buckets in the histogram
2808 * @return A reference to this histogram.
2809 */
2810 SparseHistogram &
2811 init(size_type size)
2812 {
2813 SparseHistStor::Params *params = new SparseHistStor::Params;
2814 this->setParams(params);
2815 this->doInit();
2816 return this->self();
2817 }
2818};
2819
2820class Temp;
2821/**
2822 * A formula for statistics that is calculated when printed. A formula is
2823 * stored as a tree of Nodes that represent the equation to calculate.
2824 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2825 */
2826class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2827{
2828 protected:
2829 /** The root of the tree which represents the Formula */
2830 NodePtr root;
2831 friend class Temp;
2832
2833 public:
2834 /**
2835 * Create and initialize thie formula, and register it with the database.
2836 */
2837 Formula();
2838
2839 /**
2840 * Create a formula with the given root node, register it with the
2841 * database.
2842 * @param r The root of the expression tree.
2843 */
2844 Formula(Temp r);
2845
2846 /**
2847 * Set an unitialized Formula to the given root.
2848 * @param r The root of the expression tree.
2849 * @return a reference to this formula.
2850 */
2851 const Formula &operator=(Temp r);
2852
2853 /**
2854 * Add the given tree to the existing one.
2855 * @param r The root of the expression tree.
2856 * @return a reference to this formula.
2857 */
2858 const Formula &operator+=(Temp r);
2859
2860 /**
2861 * Divide the existing tree by the given one.
2862 * @param r The root of the expression tree.
2863 * @return a reference to this formula.
2864 */
2865 const Formula &operator/=(Temp r);
2866
2867 /**
2868 * Return the result of the Fomula in a vector. If there were no Vector
2869 * components to the Formula, then the vector is size 1. If there were,
2870 * like x/y with x being a vector of size 3, then the result returned will
2871 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2872 * @return The result vector.
2873 */
2874 void result(VResult &vec) const;
2875
2876 /**
2877 * Return the total Formula result. If there is a Vector
2878 * component to this Formula, then this is the result of the
2879 * Formula if the formula is applied after summing all the
2880 * components of the Vector. For example, if Formula is x/y where
2881 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2882 * there is no Vector component, total() returns the same value as
2883 * the first entry in the VResult val() returns.
2884 * @return The total of the result vector.
2885 */
2886 Result total() const;
2887
2888 /**
2889 * Return the number of elements in the tree.
2890 */
2891 size_type size() const;
2892
2893 void prepare() { }
2894
2895 /**
2896 * Formulas don't need to be reset
2897 */
2898 void reset();
2899
2900 /**
2901 *
2902 */
2903 bool zero() const;
2904
2905 std::string str() const;
2906};
2907
2908class FormulaNode : public Node
2909{
2910 private:
2911 const Formula &formula;
2912 mutable VResult vec;
2913
2914 public:
2915 FormulaNode(const Formula &f) : formula(f) {}
2916
2917 size_type size() const { return formula.size(); }
2918 const VResult &result() const { formula.result(vec); return vec; }
2919 Result total() const { return formula.total(); }
2920
2921 std::string str() const { return formula.str(); }
2922};
2923
2924/**
2925 * Helper class to construct formula node trees.
2926 */
2927class Temp
2928{
2929 protected:
2930 /**
2931 * Pointer to a Node object.
2932 */
2933 NodePtr node;
2934
2935 public:
2936 /**
2937 * Copy the given pointer to this class.
2938 * @param n A pointer to a Node object to copy.
2939 */
2940 Temp(NodePtr n) : node(n) { }
2941
2942 /**
2943 * Return the node pointer.
2944 * @return the node pointer.
2945 */
2946 operator NodePtr&() { return node; }
2947
2948 public:
2949 /**
2950 * Create a new ScalarStatNode.
2951 * @param s The ScalarStat to place in a node.
2952 */
2953 Temp(const Scalar &s)
2954 : node(new ScalarStatNode(s.info()))
2955 { }
2956
2957 /**
2958 * Create a new ScalarStatNode.
2959 * @param s The ScalarStat to place in a node.
2960 */
2961 Temp(const Value &s)
2962 : node(new ScalarStatNode(s.info()))
2963 { }
2964
2965 /**
2966 * Create a new ScalarStatNode.
2967 * @param s The ScalarStat to place in a node.
2968 */
2969 Temp(const Average &s)
2970 : node(new ScalarStatNode(s.info()))
2971 { }
2972
2973 /**
2974 * Create a new VectorStatNode.
2975 * @param s The VectorStat to place in a node.
2976 */
2977 Temp(const Vector &s)
2978 : node(new VectorStatNode(s.info()))
2979 { }
2980
2981 Temp(const AverageVector &s)
2982 : node(new VectorStatNode(s.info()))
2983 { }
2984
2985 /**
2986 *
2987 */
2988 Temp(const Formula &f)
2989 : node(new FormulaNode(f))
2990 { }
2991
2992 /**
2993 * Create a new ScalarProxyNode.
2994 * @param p The ScalarProxy to place in a node.
2995 */
2996 template <class Stat>
2997 Temp(const ScalarProxy<Stat> &p)
2998 : node(new ScalarProxyNode<Stat>(p))
2999 { }
3000
3001 /**
3002 * Create a ConstNode
3003 * @param value The value of the const node.
3004 */
3005 Temp(signed char value)
3006 : node(new ConstNode<signed char>(value))
3007 { }
3008
3009 /**
3010 * Create a ConstNode
3011 * @param value The value of the const node.
3012 */
3013 Temp(unsigned char value)
3014 : node(new ConstNode<unsigned char>(value))
3015 { }
3016
3017 /**
3018 * Create a ConstNode
3019 * @param value The value of the const node.
3020 */
3021 Temp(signed short value)
3022 : node(new ConstNode<signed short>(value))
3023 { }
3024
3025 /**
3026 * Create a ConstNode
3027 * @param value The value of the const node.
3028 */
3029 Temp(unsigned short value)
3030 : node(new ConstNode<unsigned short>(value))
3031 { }
3032
3033 /**
3034 * Create a ConstNode
3035 * @param value The value of the const node.
3036 */
3037 Temp(signed int value)
3038 : node(new ConstNode<signed int>(value))
3039 { }
3040
3041 /**
3042 * Create a ConstNode
3043 * @param value The value of the const node.
3044 */
3045 Temp(unsigned int value)
3046 : node(new ConstNode<unsigned int>(value))
3047 { }
3048
3049 /**
3050 * Create a ConstNode
3051 * @param value The value of the const node.
3052 */
3053 Temp(signed long value)
3054 : node(new ConstNode<signed long>(value))
3055 { }
3056
3057 /**
3058 * Create a ConstNode
3059 * @param value The value of the const node.
3060 */
3061 Temp(unsigned long value)
3062 : node(new ConstNode<unsigned long>(value))
3063 { }
3064
3065 /**
3066 * Create a ConstNode
3067 * @param value The value of the const node.
3068 */
3069 Temp(signed long long value)
3070 : node(new ConstNode<signed long long>(value))
3071 { }
3072
3073 /**
3074 * Create a ConstNode
3075 * @param value The value of the const node.
3076 */
3077 Temp(unsigned long long value)
3078 : node(new ConstNode<unsigned long long>(value))
3079 { }
3080
3081 /**
3082 * Create a ConstNode
3083 * @param value The value of the const node.
3084 */
3085 Temp(float value)
3086 : node(new ConstNode<float>(value))
3087 { }
3088
3089 /**
3090 * Create a ConstNode
3091 * @param value The value of the const node.
3092 */
3093 Temp(double value)
3094 : node(new ConstNode<double>(value))
3095 { }
3096};
3097
3098
3099/**
3100 * @}
3101 */
3102
3103inline Temp
3104operator+(Temp l, Temp r)
3105{
3106 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
3107}
3108
3109inline Temp
3110operator-(Temp l, Temp r)
3111{
3112 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
3113}
3114
3115inline Temp
3116operator*(Temp l, Temp r)
3117{
3118 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
3119}
3120
3121inline Temp
3122operator/(Temp l, Temp r)
3123{
3124 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
3125}
3126
3127inline Temp
3128operator-(Temp l)
3129{
3130 return NodePtr(new UnaryNode<std::negate<Result> >(l));
3131}
3132
3133template <typename T>
3134inline Temp
3135constant(T val)
3136{
3137 return NodePtr(new ConstNode<T>(val));
3138}
3139
3140template <typename T>
3141inline Temp
3142constantVector(T val)
3143{
3144 return NodePtr(new ConstVectorNode<T>(val));
3145}
3146
3147inline Temp
3148sum(Temp val)
3149{
3150 return NodePtr(new SumNode<std::plus<Result> >(val));
3151}
3152
3153/** Dump all statistics data to the registered outputs */
3154void dump();
3155void reset();
3156void enable();
3157bool enabled();
3158
3159/**
3160 * Register a callback that should be called whenever statistics are
3161 * reset
3162 */
3163void registerResetCallback(Callback *cb);
3164
3165/**
3166 * Register a callback that should be called whenever statistics are
3167 * about to be dumped
3168 */
3169void registerDumpCallback(Callback *cb);
3170
3171std::list<Info *> &statsList();
3172
3173typedef std::map<const void *, Info *> MapType;
3174MapType &statsMap();
3175
3176typedef std::map<std::string, Info *> NameMapType;
3177NameMapType &nameMap();
3178
3179bool validateStatName(const std::string &name);
3180
3181} // namespace Stats
3182
3183void debugDumpStats();
3184
3185#endif // __BASE_STATISTICS_HH__
340 // The following functions are specific to vectors. If you use them
341 // in a non vector context, you will get a nice compiler error!
342
343 /**
344 * Set the subfield name for the given index, and marks this stat to print
345 * at the end of simulation.
346 * @param index The subfield index.
347 * @param name The new name of the subfield.
348 * @return A reference to this stat.
349 */
350 Derived &
351 subname(off_type index, const std::string &name)
352 {
353 Derived &self = this->self();
354 Info *info = self.info();
355
356 std::vector<std::string> &subn = info->subnames;
357 if (subn.size() <= index)
358 subn.resize(index + 1);
359 subn[index] = name;
360 return self;
361 }
362
363 // The following functions are specific to 2d vectors. If you use
364 // them in a non vector context, you will get a nice compiler
365 // error because info doesn't have the right variables.
366
367 /**
368 * Set the subfield description for the given index and marks this stat to
369 * print at the end of simulation.
370 * @param index The subfield index.
371 * @param desc The new description of the subfield
372 * @return A reference to this stat.
373 */
374 Derived &
375 subdesc(off_type index, const std::string &desc)
376 {
377 Info *info = this->info();
378
379 std::vector<std::string> &subd = info->subdescs;
380 if (subd.size() <= index)
381 subd.resize(index + 1);
382 subd[index] = desc;
383
384 return this->self();
385 }
386
387 void
388 prepare()
389 {
390 Derived &self = this->self();
391 Info *info = this->info();
392
393 size_t size = self.size();
394 for (off_type i = 0; i < size; ++i)
395 self.data(i)->prepare(info);
396 }
397
398 void
399 reset()
400 {
401 Derived &self = this->self();
402 Info *info = this->info();
403
404 size_t size = self.size();
405 for (off_type i = 0; i < size; ++i)
406 self.data(i)->reset(info);
407 }
408};
409
410template <class Derived, template <class> class InfoProxyType>
411class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
412{
413 public:
414 typedef InfoProxyType<Derived> Info;
415
416 /**
417 * @warning This makes the assumption that if you're gonna subnames a 2d
418 * vector, you're subnaming across all y
419 */
420 Derived &
421 ysubnames(const char **names)
422 {
423 Derived &self = this->self();
424 Info *info = this->info();
425
426 info->y_subnames.resize(self.y);
427 for (off_type i = 0; i < self.y; ++i)
428 info->y_subnames[i] = names[i];
429 return self;
430 }
431
432 Derived &
433 ysubname(off_type index, const std::string &subname)
434 {
435 Derived &self = this->self();
436 Info *info = this->info();
437
438 assert(index < self.y);
439 info->y_subnames.resize(self.y);
440 info->y_subnames[index] = subname.c_str();
441 return self;
442 }
443
444 std::string
445 ysubname(off_type i) const
446 {
447 return this->info()->y_subnames[i];
448 }
449
450};
451
452//////////////////////////////////////////////////////////////////////
453//
454// Simple Statistics
455//
456//////////////////////////////////////////////////////////////////////
457
458/**
459 * Templatized storage and interface for a simple scalar stat.
460 */
461class StatStor
462{
463 private:
464 /** The statistic value. */
465 Counter data;
466
467 public:
468 struct Params : public StorageParams {};
469
470 public:
471 /**
472 * Builds this storage element and calls the base constructor of the
473 * datatype.
474 */
475 StatStor(Info *info)
476 : data(Counter())
477 { }
478
479 /**
480 * The the stat to the given value.
481 * @param val The new value.
482 */
483 void set(Counter val) { data = val; }
484 /**
485 * Increment the stat by the given value.
486 * @param val The new value.
487 */
488 void inc(Counter val) { data += val; }
489 /**
490 * Decrement the stat by the given value.
491 * @param val The new value.
492 */
493 void dec(Counter val) { data -= val; }
494 /**
495 * Return the value of this stat as its base type.
496 * @return The value of this stat.
497 */
498 Counter value() const { return data; }
499 /**
500 * Return the value of this stat as a result type.
501 * @return The value of this stat.
502 */
503 Result result() const { return (Result)data; }
504 /**
505 * Prepare stat data for dumping or serialization
506 */
507 void prepare(Info *info) { }
508 /**
509 * Reset stat value to default
510 */
511 void reset(Info *info) { data = Counter(); }
512
513 /**
514 * @return true if zero value
515 */
516 bool zero() const { return data == Counter(); }
517};
518
519/**
520 * Templatized storage and interface to a per-tick average stat. This keeps
521 * a current count and updates a total (count * ticks) when this count
522 * changes. This allows the quick calculation of a per tick count of the item
523 * being watched. This is good for keeping track of residencies in structures
524 * among other things.
525 */
526class AvgStor
527{
528 private:
529 /** The current count. */
530 Counter current;
531 /** The tick of the last reset */
532 Tick lastReset;
533 /** The total count for all tick. */
534 mutable Result total;
535 /** The tick that current last changed. */
536 mutable Tick last;
537
538 public:
539 struct Params : public StorageParams {};
540
541 public:
542 /**
543 * Build and initializes this stat storage.
544 */
545 AvgStor(Info *info)
546 : current(0), lastReset(0), total(0), last(0)
547 { }
548
549 /**
550 * Set the current count to the one provided, update the total and last
551 * set values.
552 * @param val The new count.
553 */
554 void
555 set(Counter val)
556 {
557 total += current * (curTick() - last);
558 last = curTick();
559 current = val;
560 }
561
562 /**
563 * Increment the current count by the provided value, calls set.
564 * @param val The amount to increment.
565 */
566 void inc(Counter val) { set(current + val); }
567
568 /**
569 * Deccrement the current count by the provided value, calls set.
570 * @param val The amount to decrement.
571 */
572 void dec(Counter val) { set(current - val); }
573
574 /**
575 * Return the current count.
576 * @return The current count.
577 */
578 Counter value() const { return current; }
579
580 /**
581 * Return the current average.
582 * @return The current average.
583 */
584 Result
585 result() const
586 {
587 assert(last == curTick());
588 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
589 }
590
591 /**
592 * @return true if zero value
593 */
594 bool zero() const { return total == 0.0; }
595
596 /**
597 * Prepare stat data for dumping or serialization
598 */
599 void
600 prepare(Info *info)
601 {
602 total += current * (curTick() - last);
603 last = curTick();
604 }
605
606 /**
607 * Reset stat value to default
608 */
609 void
610 reset(Info *info)
611 {
612 total = 0.0;
613 last = curTick();
614 lastReset = curTick();
615 }
616
617};
618
619/**
620 * Implementation of a scalar stat. The type of stat is determined by the
621 * Storage template.
622 */
623template <class Derived, class Stor>
624class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
625{
626 public:
627 typedef Stor Storage;
628 typedef typename Stor::Params Params;
629
630 protected:
631 /** The storage of this stat. */
632 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
633
634 protected:
635 /**
636 * Retrieve the storage.
637 * @param index The vector index to access.
638 * @return The storage object at the given index.
639 */
640 Storage *
641 data()
642 {
643 return reinterpret_cast<Storage *>(storage);
644 }
645
646 /**
647 * Retrieve a const pointer to the storage.
648 * for the given index.
649 * @param index The vector index to access.
650 * @return A const pointer to the storage object at the given index.
651 */
652 const Storage *
653 data() const
654 {
655 return reinterpret_cast<const Storage *>(storage);
656 }
657
658 void
659 doInit()
660 {
661 new (storage) Storage(this->info());
662 this->setInit();
663 }
664
665 public:
666 /**
667 * Return the current value of this stat as its base type.
668 * @return The current value.
669 */
670 Counter value() const { return data()->value(); }
671
672 public:
673 ScalarBase()
674 {
675 this->doInit();
676 }
677
678 public:
679 // Common operators for stats
680 /**
681 * Increment the stat by 1. This calls the associated storage object inc
682 * function.
683 */
684 void operator++() { data()->inc(1); }
685 /**
686 * Decrement the stat by 1. This calls the associated storage object dec
687 * function.
688 */
689 void operator--() { data()->dec(1); }
690
691 /** Increment the stat by 1. */
692 void operator++(int) { ++*this; }
693 /** Decrement the stat by 1. */
694 void operator--(int) { --*this; }
695
696 /**
697 * Set the data value to the given value. This calls the associated storage
698 * object set function.
699 * @param v The new value.
700 */
701 template <typename U>
702 void operator=(const U &v) { data()->set(v); }
703
704 /**
705 * Increment the stat by the given value. This calls the associated
706 * storage object inc function.
707 * @param v The value to add.
708 */
709 template <typename U>
710 void operator+=(const U &v) { data()->inc(v); }
711
712 /**
713 * Decrement the stat by the given value. This calls the associated
714 * storage object dec function.
715 * @param v The value to substract.
716 */
717 template <typename U>
718 void operator-=(const U &v) { data()->dec(v); }
719
720 /**
721 * Return the number of elements, always 1 for a scalar.
722 * @return 1.
723 */
724 size_type size() const { return 1; }
725
726 Counter value() { return data()->value(); }
727
728 Result result() { return data()->result(); }
729
730 Result total() { return result(); }
731
732 bool zero() { return result() == 0.0; }
733
734 void reset() { data()->reset(this->info()); }
735 void prepare() { data()->prepare(this->info()); }
736};
737
738class ProxyInfo : public ScalarInfo
739{
740 public:
741 std::string str() const { return to_string(value()); }
742 size_type size() const { return 1; }
743 bool check() const { return true; }
744 void prepare() { }
745 void reset() { }
746 bool zero() const { return value() == 0; }
747
748 void visit(Output &visitor) { visitor.visit(*this); }
749};
750
751template <class T>
752class ValueProxy : public ProxyInfo
753{
754 private:
755 T *scalar;
756
757 public:
758 ValueProxy(T &val) : scalar(&val) {}
759 Counter value() const { return *scalar; }
760 Result result() const { return *scalar; }
761 Result total() const { return *scalar; }
762};
763
764template <class T>
765class FunctorProxy : public ProxyInfo
766{
767 private:
768 T *functor;
769
770 public:
771 FunctorProxy(T &func) : functor(&func) {}
772 Counter value() const { return (*functor)(); }
773 Result result() const { return (*functor)(); }
774 Result total() const { return (*functor)(); }
775};
776
777template <class Derived>
778class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
779{
780 private:
781 ProxyInfo *proxy;
782
783 public:
784 ValueBase() : proxy(NULL) { }
785 ~ValueBase() { if (proxy) delete proxy; }
786
787 template <class T>
788 Derived &
789 scalar(T &value)
790 {
791 proxy = new ValueProxy<T>(value);
792 this->setInit();
793 return this->self();
794 }
795
796 template <class T>
797 Derived &
798 functor(T &func)
799 {
800 proxy = new FunctorProxy<T>(func);
801 this->setInit();
802 return this->self();
803 }
804
805 Counter value() { return proxy->value(); }
806 Result result() const { return proxy->result(); }
807 Result total() const { return proxy->total(); };
808 size_type size() const { return proxy->size(); }
809
810 std::string str() const { return proxy->str(); }
811 bool zero() const { return proxy->zero(); }
812 bool check() const { return proxy != NULL; }
813 void prepare() { }
814 void reset() { }
815};
816
817//////////////////////////////////////////////////////////////////////
818//
819// Vector Statistics
820//
821//////////////////////////////////////////////////////////////////////
822
823/**
824 * A proxy class to access the stat at a given index in a VectorBase stat.
825 * Behaves like a ScalarBase.
826 */
827template <class Stat>
828class ScalarProxy
829{
830 private:
831 /** Pointer to the parent Vector. */
832 Stat &stat;
833
834 /** The index to access in the parent VectorBase. */
835 off_type index;
836
837 public:
838 /**
839 * Return the current value of this stat as its base type.
840 * @return The current value.
841 */
842 Counter value() const { return stat.data(index)->value(); }
843
844 /**
845 * Return the current value of this statas a result type.
846 * @return The current value.
847 */
848 Result result() const { return stat.data(index)->result(); }
849
850 public:
851 /**
852 * Create and initialize this proxy, do not register it with the database.
853 * @param i The index to access.
854 */
855 ScalarProxy(Stat &s, off_type i)
856 : stat(s), index(i)
857 {
858 }
859
860 /**
861 * Create a copy of the provided ScalarProxy.
862 * @param sp The proxy to copy.
863 */
864 ScalarProxy(const ScalarProxy &sp)
865 : stat(sp.stat), index(sp.index)
866 {}
867
868 /**
869 * Set this proxy equal to the provided one.
870 * @param sp The proxy to copy.
871 * @return A reference to this proxy.
872 */
873 const ScalarProxy &
874 operator=(const ScalarProxy &sp)
875 {
876 stat = sp.stat;
877 index = sp.index;
878 return *this;
879 }
880
881 public:
882 // Common operators for stats
883 /**
884 * Increment the stat by 1. This calls the associated storage object inc
885 * function.
886 */
887 void operator++() { stat.data(index)->inc(1); }
888 /**
889 * Decrement the stat by 1. This calls the associated storage object dec
890 * function.
891 */
892 void operator--() { stat.data(index)->dec(1); }
893
894 /** Increment the stat by 1. */
895 void operator++(int) { ++*this; }
896 /** Decrement the stat by 1. */
897 void operator--(int) { --*this; }
898
899 /**
900 * Set the data value to the given value. This calls the associated storage
901 * object set function.
902 * @param v The new value.
903 */
904 template <typename U>
905 void
906 operator=(const U &v)
907 {
908 stat.data(index)->set(v);
909 }
910
911 /**
912 * Increment the stat by the given value. This calls the associated
913 * storage object inc function.
914 * @param v The value to add.
915 */
916 template <typename U>
917 void
918 operator+=(const U &v)
919 {
920 stat.data(index)->inc(v);
921 }
922
923 /**
924 * Decrement the stat by the given value. This calls the associated
925 * storage object dec function.
926 * @param v The value to substract.
927 */
928 template <typename U>
929 void
930 operator-=(const U &v)
931 {
932 stat.data(index)->dec(v);
933 }
934
935 /**
936 * Return the number of elements, always 1 for a scalar.
937 * @return 1.
938 */
939 size_type size() const { return 1; }
940
941 public:
942 std::string
943 str() const
944 {
945 return csprintf("%s[%d]", stat.info()->name, index);
946 }
947};
948
949/**
950 * Implementation of a vector of stats. The type of stat is determined by the
951 * Storage class. @sa ScalarBase
952 */
953template <class Derived, class Stor>
954class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
955{
956 public:
957 typedef Stor Storage;
958 typedef typename Stor::Params Params;
959
960 /** Proxy type */
961 typedef ScalarProxy<Derived> Proxy;
962 friend class ScalarProxy<Derived>;
963 friend class DataWrapVec<Derived, VectorInfoProxy>;
964
965 protected:
966 /** The storage of this stat. */
967 Storage *storage;
968 size_type _size;
969
970 protected:
971 /**
972 * Retrieve the storage.
973 * @param index The vector index to access.
974 * @return The storage object at the given index.
975 */
976 Storage *data(off_type index) { return &storage[index]; }
977
978 /**
979 * Retrieve a const pointer to the storage.
980 * @param index The vector index to access.
981 * @return A const pointer to the storage object at the given index.
982 */
983 const Storage *data(off_type index) const { return &storage[index]; }
984
985 void
986 doInit(size_type s)
987 {
988 assert(s > 0 && "size must be positive!");
989 assert(!storage && "already initialized");
990 _size = s;
991
992 char *ptr = new char[_size * sizeof(Storage)];
993 storage = reinterpret_cast<Storage *>(ptr);
994
995 for (off_type i = 0; i < _size; ++i)
996 new (&storage[i]) Storage(this->info());
997
998 this->setInit();
999 }
1000
1001 public:
1002 void
1003 value(VCounter &vec) const
1004 {
1005 vec.resize(size());
1006 for (off_type i = 0; i < size(); ++i)
1007 vec[i] = data(i)->value();
1008 }
1009
1010 /**
1011 * Copy the values to a local vector and return a reference to it.
1012 * @return A reference to a vector of the stat values.
1013 */
1014 void
1015 result(VResult &vec) const
1016 {
1017 vec.resize(size());
1018 for (off_type i = 0; i < size(); ++i)
1019 vec[i] = data(i)->result();
1020 }
1021
1022 /**
1023 * Return a total of all entries in this vector.
1024 * @return The total of all vector entries.
1025 */
1026 Result
1027 total() const
1028 {
1029 Result total = 0.0;
1030 for (off_type i = 0; i < size(); ++i)
1031 total += data(i)->result();
1032 return total;
1033 }
1034
1035 /**
1036 * @return the number of elements in this vector.
1037 */
1038 size_type size() const { return _size; }
1039
1040 bool
1041 zero() const
1042 {
1043 for (off_type i = 0; i < size(); ++i)
1044 if (data(i)->zero())
1045 return false;
1046 return true;
1047 }
1048
1049 bool
1050 check() const
1051 {
1052 return storage != NULL;
1053 }
1054
1055 public:
1056 VectorBase()
1057 : storage(NULL)
1058 {}
1059
1060 ~VectorBase()
1061 {
1062 if (!storage)
1063 return;
1064
1065 for (off_type i = 0; i < _size; ++i)
1066 data(i)->~Storage();
1067 delete [] reinterpret_cast<char *>(storage);
1068 }
1069
1070 /**
1071 * Set this vector to have the given size.
1072 * @param size The new size.
1073 * @return A reference to this stat.
1074 */
1075 Derived &
1076 init(size_type size)
1077 {
1078 Derived &self = this->self();
1079 self.doInit(size);
1080 return self;
1081 }
1082
1083 /**
1084 * Return a reference (ScalarProxy) to the stat at the given index.
1085 * @param index The vector index to access.
1086 * @return A reference of the stat.
1087 */
1088 Proxy
1089 operator[](off_type index)
1090 {
1091 assert (index >= 0 && index < size());
1092 return Proxy(this->self(), index);
1093 }
1094};
1095
1096template <class Stat>
1097class VectorProxy
1098{
1099 private:
1100 Stat &stat;
1101 off_type offset;
1102 size_type len;
1103
1104 private:
1105 mutable VResult vec;
1106
1107 typename Stat::Storage *
1108 data(off_type index)
1109 {
1110 assert(index < len);
1111 return stat.data(offset + index);
1112 }
1113
1114 const typename Stat::Storage *
1115 data(off_type index) const
1116 {
1117 assert(index < len);
1118 return stat.data(offset + index);
1119 }
1120
1121 public:
1122 const VResult &
1123 result() const
1124 {
1125 vec.resize(size());
1126
1127 for (off_type i = 0; i < size(); ++i)
1128 vec[i] = data(i)->result();
1129
1130 return vec;
1131 }
1132
1133 Result
1134 total() const
1135 {
1136 Result total = 0.0;
1137 for (off_type i = 0; i < size(); ++i)
1138 total += data(i)->result();
1139 return total;
1140 }
1141
1142 public:
1143 VectorProxy(Stat &s, off_type o, size_type l)
1144 : stat(s), offset(o), len(l)
1145 {
1146 }
1147
1148 VectorProxy(const VectorProxy &sp)
1149 : stat(sp.stat), offset(sp.offset), len(sp.len)
1150 {
1151 }
1152
1153 const VectorProxy &
1154 operator=(const VectorProxy &sp)
1155 {
1156 stat = sp.stat;
1157 offset = sp.offset;
1158 len = sp.len;
1159 return *this;
1160 }
1161
1162 ScalarProxy<Stat>
1163 operator[](off_type index)
1164 {
1165 assert (index >= 0 && index < size());
1166 return ScalarProxy<Stat>(stat, offset + index);
1167 }
1168
1169 size_type size() const { return len; }
1170};
1171
1172template <class Derived, class Stor>
1173class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1174{
1175 public:
1176 typedef Vector2dInfoProxy<Derived> Info;
1177 typedef Stor Storage;
1178 typedef typename Stor::Params Params;
1179 typedef VectorProxy<Derived> Proxy;
1180 friend class ScalarProxy<Derived>;
1181 friend class VectorProxy<Derived>;
1182 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1183 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1184
1185 protected:
1186 size_type x;
1187 size_type y;
1188 size_type _size;
1189 Storage *storage;
1190
1191 protected:
1192 Storage *data(off_type index) { return &storage[index]; }
1193 const Storage *data(off_type index) const { return &storage[index]; }
1194
1195 public:
1196 Vector2dBase()
1197 : storage(NULL)
1198 {}
1199
1200 ~Vector2dBase()
1201 {
1202 if (!storage)
1203 return;
1204
1205 for (off_type i = 0; i < _size; ++i)
1206 data(i)->~Storage();
1207 delete [] reinterpret_cast<char *>(storage);
1208 }
1209
1210 Derived &
1211 init(size_type _x, size_type _y)
1212 {
1213 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1214 assert(!storage && "already initialized");
1215
1216 Derived &self = this->self();
1217 Info *info = this->info();
1218
1219 x = _x;
1220 y = _y;
1221 info->x = _x;
1222 info->y = _y;
1223 _size = x * y;
1224
1225 char *ptr = new char[_size * sizeof(Storage)];
1226 storage = reinterpret_cast<Storage *>(ptr);
1227
1228 for (off_type i = 0; i < _size; ++i)
1229 new (&storage[i]) Storage(info);
1230
1231 this->setInit();
1232
1233 return self;
1234 }
1235
1236 Proxy
1237 operator[](off_type index)
1238 {
1239 off_type offset = index * y;
1240 assert (index >= 0 && offset + y <= size());
1241 return Proxy(this->self(), offset, y);
1242 }
1243
1244
1245 size_type
1246 size() const
1247 {
1248 return _size;
1249 }
1250
1251 bool
1252 zero() const
1253 {
1254 return data(0)->zero();
1255#if 0
1256 for (off_type i = 0; i < size(); ++i)
1257 if (!data(i)->zero())
1258 return false;
1259 return true;
1260#endif
1261 }
1262
1263 void
1264 prepare()
1265 {
1266 Info *info = this->info();
1267 size_type size = this->size();
1268
1269 for (off_type i = 0; i < size; ++i)
1270 data(i)->prepare(info);
1271
1272 info->cvec.resize(size);
1273 for (off_type i = 0; i < size; ++i)
1274 info->cvec[i] = data(i)->value();
1275 }
1276
1277 /**
1278 * Reset stat value to default
1279 */
1280 void
1281 reset()
1282 {
1283 Info *info = this->info();
1284 size_type size = this->size();
1285 for (off_type i = 0; i < size; ++i)
1286 data(i)->reset(info);
1287 }
1288
1289 bool
1290 check() const
1291 {
1292 return storage != NULL;
1293 }
1294};
1295
1296//////////////////////////////////////////////////////////////////////
1297//
1298// Non formula statistics
1299//
1300//////////////////////////////////////////////////////////////////////
1301/** The parameters for a distribution stat. */
1302struct DistParams : public StorageParams
1303{
1304 const DistType type;
1305 DistParams(DistType t) : type(t) {}
1306};
1307
1308/**
1309 * Templatized storage and interface for a distrbution stat.
1310 */
1311class DistStor
1312{
1313 public:
1314 /** The parameters for a distribution stat. */
1315 struct Params : public DistParams
1316 {
1317 /** The minimum value to track. */
1318 Counter min;
1319 /** The maximum value to track. */
1320 Counter max;
1321 /** The number of entries in each bucket. */
1322 Counter bucket_size;
1323 /** The number of buckets. Equal to (max-min)/bucket_size. */
1324 size_type buckets;
1325
1326 Params() : DistParams(Dist) {}
1327 };
1328
1329 private:
1330 /** The minimum value to track. */
1331 Counter min_track;
1332 /** The maximum value to track. */
1333 Counter max_track;
1334 /** The number of entries in each bucket. */
1335 Counter bucket_size;
1336
1337 /** The smallest value sampled. */
1338 Counter min_val;
1339 /** The largest value sampled. */
1340 Counter max_val;
1341 /** The number of values sampled less than min. */
1342 Counter underflow;
1343 /** The number of values sampled more than max. */
1344 Counter overflow;
1345 /** The current sum. */
1346 Counter sum;
1347 /** The sum of squares. */
1348 Counter squares;
1349 /** The number of samples. */
1350 Counter samples;
1351 /** Counter for each bucket. */
1352 VCounter cvec;
1353
1354 public:
1355 DistStor(Info *info)
1356 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1357 {
1358 reset(info);
1359 }
1360
1361 /**
1362 * Add a value to the distribution for the given number of times.
1363 * @param val The value to add.
1364 * @param number The number of times to add the value.
1365 */
1366 void
1367 sample(Counter val, int number)
1368 {
1369 if (val < min_track)
1370 underflow += number;
1371 else if (val > max_track)
1372 overflow += number;
1373 else {
1374 size_type index =
1375 (size_type)std::floor((val - min_track) / bucket_size);
1376 assert(index < size());
1377 cvec[index] += number;
1378 }
1379
1380 if (val < min_val)
1381 min_val = val;
1382
1383 if (val > max_val)
1384 max_val = val;
1385
1386 sum += val * number;
1387 squares += val * val * number;
1388 samples += number;
1389 }
1390
1391 /**
1392 * Return the number of buckets in this distribution.
1393 * @return the number of buckets.
1394 */
1395 size_type size() const { return cvec.size(); }
1396
1397 /**
1398 * Returns true if any calls to sample have been made.
1399 * @return True if any values have been sampled.
1400 */
1401 bool
1402 zero() const
1403 {
1404 return samples == Counter();
1405 }
1406
1407 void
1408 prepare(Info *info, DistData &data)
1409 {
1410 const Params *params = safe_cast<const Params *>(info->storageParams);
1411
1412 assert(params->type == Dist);
1413 data.type = params->type;
1414 data.min = params->min;
1415 data.max = params->max;
1416 data.bucket_size = params->bucket_size;
1417
1418 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1419 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1420 data.underflow = underflow;
1421 data.overflow = overflow;
1422
1423 data.cvec.resize(params->buckets);
1424 for (off_type i = 0; i < params->buckets; ++i)
1425 data.cvec[i] = cvec[i];
1426
1427 data.sum = sum;
1428 data.squares = squares;
1429 data.samples = samples;
1430 }
1431
1432 /**
1433 * Reset stat value to default
1434 */
1435 void
1436 reset(Info *info)
1437 {
1438 const Params *params = safe_cast<const Params *>(info->storageParams);
1439 min_track = params->min;
1440 max_track = params->max;
1441 bucket_size = params->bucket_size;
1442
1443 min_val = CounterLimits::max();
1444 max_val = CounterLimits::min();
1445 underflow = Counter();
1446 overflow = Counter();
1447
1448 size_type size = cvec.size();
1449 for (off_type i = 0; i < size; ++i)
1450 cvec[i] = Counter();
1451
1452 sum = Counter();
1453 squares = Counter();
1454 samples = Counter();
1455 }
1456};
1457
1458/**
1459 * Templatized storage and interface for a histogram stat.
1460 */
1461class HistStor
1462{
1463 public:
1464 /** The parameters for a distribution stat. */
1465 struct Params : public DistParams
1466 {
1467 /** The number of buckets.. */
1468 size_type buckets;
1469
1470 Params() : DistParams(Hist) {}
1471 };
1472
1473 private:
1474 /** The minimum value to track. */
1475 Counter min_bucket;
1476 /** The maximum value to track. */
1477 Counter max_bucket;
1478 /** The number of entries in each bucket. */
1479 Counter bucket_size;
1480
1481 /** The current sum. */
1482 Counter sum;
1483 /** The sum of logarithm of each sample, used to compute geometric mean. */
1484 Counter logs;
1485 /** The sum of squares. */
1486 Counter squares;
1487 /** The number of samples. */
1488 Counter samples;
1489 /** Counter for each bucket. */
1490 VCounter cvec;
1491
1492 public:
1493 HistStor(Info *info)
1494 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1495 {
1496 reset(info);
1497 }
1498
1499 void grow_up();
1500 void grow_out();
1501 void grow_convert();
1502
1503 /**
1504 * Add a value to the distribution for the given number of times.
1505 * @param val The value to add.
1506 * @param number The number of times to add the value.
1507 */
1508 void
1509 sample(Counter val, int number)
1510 {
1511 assert(min_bucket < max_bucket);
1512 if (val < min_bucket) {
1513 if (min_bucket == 0)
1514 grow_convert();
1515
1516 while (val < min_bucket)
1517 grow_out();
1518 } else if (val >= max_bucket + bucket_size) {
1519 if (min_bucket == 0) {
1520 while (val >= max_bucket + bucket_size)
1521 grow_up();
1522 } else {
1523 while (val >= max_bucket + bucket_size)
1524 grow_out();
1525 }
1526 }
1527
1528 size_type index =
1529 (int64_t)std::floor((val - min_bucket) / bucket_size);
1530
1531 assert(index >= 0 && index < size());
1532 cvec[index] += number;
1533
1534 sum += val * number;
1535 squares += val * val * number;
1536 logs += log(val) * number;
1537 samples += number;
1538 }
1539
1540 /**
1541 * Return the number of buckets in this distribution.
1542 * @return the number of buckets.
1543 */
1544 size_type size() const { return cvec.size(); }
1545
1546 /**
1547 * Returns true if any calls to sample have been made.
1548 * @return True if any values have been sampled.
1549 */
1550 bool
1551 zero() const
1552 {
1553 return samples == Counter();
1554 }
1555
1556 void
1557 prepare(Info *info, DistData &data)
1558 {
1559 const Params *params = safe_cast<const Params *>(info->storageParams);
1560
1561 assert(params->type == Hist);
1562 data.type = params->type;
1563 data.min = min_bucket;
1564 data.max = max_bucket + bucket_size - 1;
1565 data.bucket_size = bucket_size;
1566
1567 data.min_val = min_bucket;
1568 data.max_val = max_bucket;
1569
1570 int buckets = params->buckets;
1571 data.cvec.resize(buckets);
1572 for (off_type i = 0; i < buckets; ++i)
1573 data.cvec[i] = cvec[i];
1574
1575 data.sum = sum;
1576 data.logs = logs;
1577 data.squares = squares;
1578 data.samples = samples;
1579 }
1580
1581 /**
1582 * Reset stat value to default
1583 */
1584 void
1585 reset(Info *info)
1586 {
1587 const Params *params = safe_cast<const Params *>(info->storageParams);
1588 min_bucket = 0;
1589 max_bucket = params->buckets - 1;
1590 bucket_size = 1;
1591
1592 size_type size = cvec.size();
1593 for (off_type i = 0; i < size; ++i)
1594 cvec[i] = Counter();
1595
1596 sum = Counter();
1597 squares = Counter();
1598 samples = Counter();
1599 logs = Counter();
1600 }
1601};
1602
1603/**
1604 * Templatized storage and interface for a distribution that calculates mean
1605 * and variance.
1606 */
1607class SampleStor
1608{
1609 public:
1610 struct Params : public DistParams
1611 {
1612 Params() : DistParams(Deviation) {}
1613 };
1614
1615 private:
1616 /** The current sum. */
1617 Counter sum;
1618 /** The sum of squares. */
1619 Counter squares;
1620 /** The number of samples. */
1621 Counter samples;
1622
1623 public:
1624 /**
1625 * Create and initialize this storage.
1626 */
1627 SampleStor(Info *info)
1628 : sum(Counter()), squares(Counter()), samples(Counter())
1629 { }
1630
1631 /**
1632 * Add a value the given number of times to this running average.
1633 * Update the running sum and sum of squares, increment the number of
1634 * values seen by the given number.
1635 * @param val The value to add.
1636 * @param number The number of times to add the value.
1637 */
1638 void
1639 sample(Counter val, int number)
1640 {
1641 Counter value = val * number;
1642 sum += value;
1643 squares += value * value;
1644 samples += number;
1645 }
1646
1647 /**
1648 * Return the number of entries in this stat, 1
1649 * @return 1.
1650 */
1651 size_type size() const { return 1; }
1652
1653 /**
1654 * Return true if no samples have been added.
1655 * @return True if no samples have been added.
1656 */
1657 bool zero() const { return samples == Counter(); }
1658
1659 void
1660 prepare(Info *info, DistData &data)
1661 {
1662 const Params *params = safe_cast<const Params *>(info->storageParams);
1663
1664 assert(params->type == Deviation);
1665 data.type = params->type;
1666 data.sum = sum;
1667 data.squares = squares;
1668 data.samples = samples;
1669 }
1670
1671 /**
1672 * Reset stat value to default
1673 */
1674 void
1675 reset(Info *info)
1676 {
1677 sum = Counter();
1678 squares = Counter();
1679 samples = Counter();
1680 }
1681};
1682
1683/**
1684 * Templatized storage for distribution that calculates per tick mean and
1685 * variance.
1686 */
1687class AvgSampleStor
1688{
1689 public:
1690 struct Params : public DistParams
1691 {
1692 Params() : DistParams(Deviation) {}
1693 };
1694
1695 private:
1696 /** Current total. */
1697 Counter sum;
1698 /** Current sum of squares. */
1699 Counter squares;
1700
1701 public:
1702 /**
1703 * Create and initialize this storage.
1704 */
1705 AvgSampleStor(Info *info)
1706 : sum(Counter()), squares(Counter())
1707 {}
1708
1709 /**
1710 * Add a value to the distribution for the given number of times.
1711 * Update the running sum and sum of squares.
1712 * @param val The value to add.
1713 * @param number The number of times to add the value.
1714 */
1715 void
1716 sample(Counter val, int number)
1717 {
1718 Counter value = val * number;
1719 sum += value;
1720 squares += value * value;
1721 }
1722
1723 /**
1724 * Return the number of entries, in this case 1.
1725 * @return 1.
1726 */
1727 size_type size() const { return 1; }
1728
1729 /**
1730 * Return true if no samples have been added.
1731 * @return True if the sum is zero.
1732 */
1733 bool zero() const { return sum == Counter(); }
1734
1735 void
1736 prepare(Info *info, DistData &data)
1737 {
1738 const Params *params = safe_cast<const Params *>(info->storageParams);
1739
1740 assert(params->type == Deviation);
1741 data.type = params->type;
1742 data.sum = sum;
1743 data.squares = squares;
1744 data.samples = curTick();
1745 }
1746
1747 /**
1748 * Reset stat value to default
1749 */
1750 void
1751 reset(Info *info)
1752 {
1753 sum = Counter();
1754 squares = Counter();
1755 }
1756};
1757
1758/**
1759 * Implementation of a distribution stat. The type of distribution is
1760 * determined by the Storage template. @sa ScalarBase
1761 */
1762template <class Derived, class Stor>
1763class DistBase : public DataWrap<Derived, DistInfoProxy>
1764{
1765 public:
1766 typedef DistInfoProxy<Derived> Info;
1767 typedef Stor Storage;
1768 typedef typename Stor::Params Params;
1769
1770 protected:
1771 /** The storage for this stat. */
1772 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1773
1774 protected:
1775 /**
1776 * Retrieve the storage.
1777 * @return The storage object for this stat.
1778 */
1779 Storage *
1780 data()
1781 {
1782 return reinterpret_cast<Storage *>(storage);
1783 }
1784
1785 /**
1786 * Retrieve a const pointer to the storage.
1787 * @return A const pointer to the storage object for this stat.
1788 */
1789 const Storage *
1790 data() const
1791 {
1792 return reinterpret_cast<const Storage *>(storage);
1793 }
1794
1795 void
1796 doInit()
1797 {
1798 new (storage) Storage(this->info());
1799 this->setInit();
1800 }
1801
1802 public:
1803 DistBase() { }
1804
1805 /**
1806 * Add a value to the distribtion n times. Calls sample on the storage
1807 * class.
1808 * @param v The value to add.
1809 * @param n The number of times to add it, defaults to 1.
1810 */
1811 template <typename U>
1812 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1813
1814 /**
1815 * Return the number of entries in this stat.
1816 * @return The number of entries.
1817 */
1818 size_type size() const { return data()->size(); }
1819 /**
1820 * Return true if no samples have been added.
1821 * @return True if there haven't been any samples.
1822 */
1823 bool zero() const { return data()->zero(); }
1824
1825 void
1826 prepare()
1827 {
1828 Info *info = this->info();
1829 data()->prepare(info, info->data);
1830 }
1831
1832 /**
1833 * Reset stat value to default
1834 */
1835 void
1836 reset()
1837 {
1838 data()->reset(this->info());
1839 }
1840};
1841
1842template <class Stat>
1843class DistProxy;
1844
1845template <class Derived, class Stor>
1846class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1847{
1848 public:
1849 typedef VectorDistInfoProxy<Derived> Info;
1850 typedef Stor Storage;
1851 typedef typename Stor::Params Params;
1852 typedef DistProxy<Derived> Proxy;
1853 friend class DistProxy<Derived>;
1854 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1855
1856 protected:
1857 Storage *storage;
1858 size_type _size;
1859
1860 protected:
1861 Storage *
1862 data(off_type index)
1863 {
1864 return &storage[index];
1865 }
1866
1867 const Storage *
1868 data(off_type index) const
1869 {
1870 return &storage[index];
1871 }
1872
1873 void
1874 doInit(size_type s)
1875 {
1876 assert(s > 0 && "size must be positive!");
1877 assert(!storage && "already initialized");
1878 _size = s;
1879
1880 char *ptr = new char[_size * sizeof(Storage)];
1881 storage = reinterpret_cast<Storage *>(ptr);
1882
1883 Info *info = this->info();
1884 for (off_type i = 0; i < _size; ++i)
1885 new (&storage[i]) Storage(info);
1886
1887 this->setInit();
1888 }
1889
1890 public:
1891 VectorDistBase()
1892 : storage(NULL)
1893 {}
1894
1895 ~VectorDistBase()
1896 {
1897 if (!storage)
1898 return ;
1899
1900 for (off_type i = 0; i < _size; ++i)
1901 data(i)->~Storage();
1902 delete [] reinterpret_cast<char *>(storage);
1903 }
1904
1905 Proxy operator[](off_type index)
1906 {
1907 assert(index >= 0 && index < size());
1908 return Proxy(this->self(), index);
1909 }
1910
1911 size_type
1912 size() const
1913 {
1914 return _size;
1915 }
1916
1917 bool
1918 zero() const
1919 {
1920 for (off_type i = 0; i < size(); ++i)
1921 if (!data(i)->zero())
1922 return false;
1923 return true;
1924 }
1925
1926 void
1927 prepare()
1928 {
1929 Info *info = this->info();
1930 size_type size = this->size();
1931 info->data.resize(size);
1932 for (off_type i = 0; i < size; ++i)
1933 data(i)->prepare(info, info->data[i]);
1934 }
1935
1936 bool
1937 check() const
1938 {
1939 return storage != NULL;
1940 }
1941};
1942
1943template <class Stat>
1944class DistProxy
1945{
1946 private:
1947 Stat &stat;
1948 off_type index;
1949
1950 protected:
1951 typename Stat::Storage *data() { return stat.data(index); }
1952 const typename Stat::Storage *data() const { return stat.data(index); }
1953
1954 public:
1955 DistProxy(Stat &s, off_type i)
1956 : stat(s), index(i)
1957 {}
1958
1959 DistProxy(const DistProxy &sp)
1960 : stat(sp.stat), index(sp.index)
1961 {}
1962
1963 const DistProxy &
1964 operator=(const DistProxy &sp)
1965 {
1966 stat = sp.stat;
1967 index = sp.index;
1968 return *this;
1969 }
1970
1971 public:
1972 template <typename U>
1973 void
1974 sample(const U &v, int n = 1)
1975 {
1976 data()->sample(v, n);
1977 }
1978
1979 size_type
1980 size() const
1981 {
1982 return 1;
1983 }
1984
1985 bool
1986 zero() const
1987 {
1988 return data()->zero();
1989 }
1990
1991 /**
1992 * Proxy has no state. Nothing to reset.
1993 */
1994 void reset() { }
1995};
1996
1997//////////////////////////////////////////////////////////////////////
1998//
1999// Formula Details
2000//
2001//////////////////////////////////////////////////////////////////////
2002
2003/**
2004 * Base class for formula statistic node. These nodes are used to build a tree
2005 * that represents the formula.
2006 */
2007class Node : public RefCounted
2008{
2009 public:
2010 /**
2011 * Return the number of nodes in the subtree starting at this node.
2012 * @return the number of nodes in this subtree.
2013 */
2014 virtual size_type size() const = 0;
2015 /**
2016 * Return the result vector of this subtree.
2017 * @return The result vector of this subtree.
2018 */
2019 virtual const VResult &result() const = 0;
2020 /**
2021 * Return the total of the result vector.
2022 * @return The total of the result vector.
2023 */
2024 virtual Result total() const = 0;
2025
2026 /**
2027 *
2028 */
2029 virtual std::string str() const = 0;
2030};
2031
2032/** Reference counting pointer to a function Node. */
2033typedef RefCountingPtr<Node> NodePtr;
2034
2035class ScalarStatNode : public Node
2036{
2037 private:
2038 const ScalarInfo *data;
2039 mutable VResult vresult;
2040
2041 public:
2042 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2043
2044 const VResult &
2045 result() const
2046 {
2047 vresult[0] = data->result();
2048 return vresult;
2049 }
2050
2051 Result total() const { return data->result(); };
2052
2053 size_type size() const { return 1; }
2054
2055 /**
2056 *
2057 */
2058 std::string str() const { return data->name; }
2059};
2060
2061template <class Stat>
2062class ScalarProxyNode : public Node
2063{
2064 private:
2065 const ScalarProxy<Stat> proxy;
2066 mutable VResult vresult;
2067
2068 public:
2069 ScalarProxyNode(const ScalarProxy<Stat> &p)
2070 : proxy(p), vresult(1)
2071 { }
2072
2073 const VResult &
2074 result() const
2075 {
2076 vresult[0] = proxy.result();
2077 return vresult;
2078 }
2079
2080 Result
2081 total() const
2082 {
2083 return proxy.result();
2084 }
2085
2086 size_type
2087 size() const
2088 {
2089 return 1;
2090 }
2091
2092 /**
2093 *
2094 */
2095 std::string
2096 str() const
2097 {
2098 return proxy.str();
2099 }
2100};
2101
2102class VectorStatNode : public Node
2103{
2104 private:
2105 const VectorInfo *data;
2106
2107 public:
2108 VectorStatNode(const VectorInfo *d) : data(d) { }
2109 const VResult &result() const { return data->result(); }
2110 Result total() const { return data->total(); };
2111
2112 size_type size() const { return data->size(); }
2113
2114 std::string str() const { return data->name; }
2115};
2116
2117template <class T>
2118class ConstNode : public Node
2119{
2120 private:
2121 VResult vresult;
2122
2123 public:
2124 ConstNode(T s) : vresult(1, (Result)s) {}
2125 const VResult &result() const { return vresult; }
2126 Result total() const { return vresult[0]; };
2127 size_type size() const { return 1; }
2128 std::string str() const { return to_string(vresult[0]); }
2129};
2130
2131template <class T>
2132class ConstVectorNode : public Node
2133{
2134 private:
2135 VResult vresult;
2136
2137 public:
2138 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2139 const VResult &result() const { return vresult; }
2140
2141 Result
2142 total() const
2143 {
2144 size_type size = this->size();
2145 Result tmp = 0;
2146 for (off_type i = 0; i < size; i++)
2147 tmp += vresult[i];
2148 return tmp;
2149 }
2150
2151 size_type size() const { return vresult.size(); }
2152 std::string
2153 str() const
2154 {
2155 size_type size = this->size();
2156 std::string tmp = "(";
2157 for (off_type i = 0; i < size; i++)
2158 tmp += csprintf("%s ",to_string(vresult[i]));
2159 tmp += ")";
2160 return tmp;
2161 }
2162};
2163
2164template <class Op>
2165struct OpString;
2166
2167template<>
2168struct OpString<std::plus<Result> >
2169{
2170 static std::string str() { return "+"; }
2171};
2172
2173template<>
2174struct OpString<std::minus<Result> >
2175{
2176 static std::string str() { return "-"; }
2177};
2178
2179template<>
2180struct OpString<std::multiplies<Result> >
2181{
2182 static std::string str() { return "*"; }
2183};
2184
2185template<>
2186struct OpString<std::divides<Result> >
2187{
2188 static std::string str() { return "/"; }
2189};
2190
2191template<>
2192struct OpString<std::modulus<Result> >
2193{
2194 static std::string str() { return "%"; }
2195};
2196
2197template<>
2198struct OpString<std::negate<Result> >
2199{
2200 static std::string str() { return "-"; }
2201};
2202
2203template <class Op>
2204class UnaryNode : public Node
2205{
2206 public:
2207 NodePtr l;
2208 mutable VResult vresult;
2209
2210 public:
2211 UnaryNode(NodePtr &p) : l(p) {}
2212
2213 const VResult &
2214 result() const
2215 {
2216 const VResult &lvec = l->result();
2217 size_type size = lvec.size();
2218
2219 assert(size > 0);
2220
2221 vresult.resize(size);
2222 Op op;
2223 for (off_type i = 0; i < size; ++i)
2224 vresult[i] = op(lvec[i]);
2225
2226 return vresult;
2227 }
2228
2229 Result
2230 total() const
2231 {
2232 const VResult &vec = this->result();
2233 Result total = 0.0;
2234 for (off_type i = 0; i < size(); i++)
2235 total += vec[i];
2236 return total;
2237 }
2238
2239 size_type size() const { return l->size(); }
2240
2241 std::string
2242 str() const
2243 {
2244 return OpString<Op>::str() + l->str();
2245 }
2246};
2247
2248template <class Op>
2249class BinaryNode : public Node
2250{
2251 public:
2252 NodePtr l;
2253 NodePtr r;
2254 mutable VResult vresult;
2255
2256 public:
2257 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2258
2259 const VResult &
2260 result() const
2261 {
2262 Op op;
2263 const VResult &lvec = l->result();
2264 const VResult &rvec = r->result();
2265
2266 assert(lvec.size() > 0 && rvec.size() > 0);
2267
2268 if (lvec.size() == 1 && rvec.size() == 1) {
2269 vresult.resize(1);
2270 vresult[0] = op(lvec[0], rvec[0]);
2271 } else if (lvec.size() == 1) {
2272 size_type size = rvec.size();
2273 vresult.resize(size);
2274 for (off_type i = 0; i < size; ++i)
2275 vresult[i] = op(lvec[0], rvec[i]);
2276 } else if (rvec.size() == 1) {
2277 size_type size = lvec.size();
2278 vresult.resize(size);
2279 for (off_type i = 0; i < size; ++i)
2280 vresult[i] = op(lvec[i], rvec[0]);
2281 } else if (rvec.size() == lvec.size()) {
2282 size_type size = rvec.size();
2283 vresult.resize(size);
2284 for (off_type i = 0; i < size; ++i)
2285 vresult[i] = op(lvec[i], rvec[i]);
2286 }
2287
2288 return vresult;
2289 }
2290
2291 Result
2292 total() const
2293 {
2294 const VResult &vec = this->result();
2295 const VResult &lvec = l->result();
2296 const VResult &rvec = r->result();
2297 Result total = 0.0;
2298 Result lsum = 0.0;
2299 Result rsum = 0.0;
2300 Op op;
2301
2302 assert(lvec.size() > 0 && rvec.size() > 0);
2303 assert(lvec.size() == rvec.size() ||
2304 lvec.size() == 1 || rvec.size() == 1);
2305
2306 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2307 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2308 for (off_type i = 0; i < size(); ++i) {
2309 lsum += lvec[i];
2310 rsum += rvec[i];
2311 }
2312 return op(lsum, rsum);
2313 }
2314
2315 /** Otherwise divide each item by the divisor */
2316 for (off_type i = 0; i < size(); ++i) {
2317 total += vec[i];
2318 }
2319
2320 return total;
2321 }
2322
2323 size_type
2324 size() const
2325 {
2326 size_type ls = l->size();
2327 size_type rs = r->size();
2328 if (ls == 1) {
2329 return rs;
2330 } else if (rs == 1) {
2331 return ls;
2332 } else {
2333 assert(ls == rs && "Node vector sizes are not equal");
2334 return ls;
2335 }
2336 }
2337
2338 std::string
2339 str() const
2340 {
2341 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2342 }
2343};
2344
2345template <class Op>
2346class SumNode : public Node
2347{
2348 public:
2349 NodePtr l;
2350 mutable VResult vresult;
2351
2352 public:
2353 SumNode(NodePtr &p) : l(p), vresult(1) {}
2354
2355 const VResult &
2356 result() const
2357 {
2358 const VResult &lvec = l->result();
2359 size_type size = lvec.size();
2360 assert(size > 0);
2361
2362 vresult[0] = 0.0;
2363
2364 Op op;
2365 for (off_type i = 0; i < size; ++i)
2366 vresult[0] = op(vresult[0], lvec[i]);
2367
2368 return vresult;
2369 }
2370
2371 Result
2372 total() const
2373 {
2374 const VResult &lvec = l->result();
2375 size_type size = lvec.size();
2376 assert(size > 0);
2377
2378 Result result = 0.0;
2379
2380 Op op;
2381 for (off_type i = 0; i < size; ++i)
2382 result = op(result, lvec[i]);
2383
2384 return result;
2385 }
2386
2387 size_type size() const { return 1; }
2388
2389 std::string
2390 str() const
2391 {
2392 return csprintf("total(%s)", l->str());
2393 }
2394};
2395
2396
2397//////////////////////////////////////////////////////////////////////
2398//
2399// Visible Statistics Types
2400//
2401//////////////////////////////////////////////////////////////////////
2402/**
2403 * @defgroup VisibleStats "Statistic Types"
2404 * These are the statistics that are used in the simulator.
2405 * @{
2406 */
2407
2408/**
2409 * This is a simple scalar statistic, like a counter.
2410 * @sa Stat, ScalarBase, StatStor
2411 */
2412class Scalar : public ScalarBase<Scalar, StatStor>
2413{
2414 public:
2415 using ScalarBase<Scalar, StatStor>::operator=;
2416};
2417
2418/**
2419 * A stat that calculates the per tick average of a value.
2420 * @sa Stat, ScalarBase, AvgStor
2421 */
2422class Average : public ScalarBase<Average, AvgStor>
2423{
2424 public:
2425 using ScalarBase<Average, AvgStor>::operator=;
2426};
2427
2428class Value : public ValueBase<Value>
2429{
2430};
2431
2432/**
2433 * A vector of scalar stats.
2434 * @sa Stat, VectorBase, StatStor
2435 */
2436class Vector : public VectorBase<Vector, StatStor>
2437{
2438};
2439
2440/**
2441 * A vector of Average stats.
2442 * @sa Stat, VectorBase, AvgStor
2443 */
2444class AverageVector : public VectorBase<AverageVector, AvgStor>
2445{
2446};
2447
2448/**
2449 * A 2-Dimensional vecto of scalar stats.
2450 * @sa Stat, Vector2dBase, StatStor
2451 */
2452class Vector2d : public Vector2dBase<Vector2d, StatStor>
2453{
2454};
2455
2456/**
2457 * A simple distribution stat.
2458 * @sa Stat, DistBase, DistStor
2459 */
2460class Distribution : public DistBase<Distribution, DistStor>
2461{
2462 public:
2463 /**
2464 * Set the parameters of this distribution. @sa DistStor::Params
2465 * @param min The minimum value of the distribution.
2466 * @param max The maximum value of the distribution.
2467 * @param bkt The number of values in each bucket.
2468 * @return A reference to this distribution.
2469 */
2470 Distribution &
2471 init(Counter min, Counter max, Counter bkt)
2472 {
2473 DistStor::Params *params = new DistStor::Params;
2474 params->min = min;
2475 params->max = max;
2476 params->bucket_size = bkt;
2477 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2478 this->setParams(params);
2479 this->doInit();
2480 return this->self();
2481 }
2482};
2483
2484/**
2485 * A simple histogram stat.
2486 * @sa Stat, DistBase, HistStor
2487 */
2488class Histogram : public DistBase<Histogram, HistStor>
2489{
2490 public:
2491 /**
2492 * Set the parameters of this histogram. @sa HistStor::Params
2493 * @param size The number of buckets in the histogram
2494 * @return A reference to this histogram.
2495 */
2496 Histogram &
2497 init(size_type size)
2498 {
2499 HistStor::Params *params = new HistStor::Params;
2500 params->buckets = size;
2501 this->setParams(params);
2502 this->doInit();
2503 return this->self();
2504 }
2505};
2506
2507/**
2508 * Calculates the mean and variance of all the samples.
2509 * @sa DistBase, SampleStor
2510 */
2511class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2512{
2513 public:
2514 /**
2515 * Construct and initialize this distribution.
2516 */
2517 StandardDeviation()
2518 {
2519 SampleStor::Params *params = new SampleStor::Params;
2520 this->doInit();
2521 this->setParams(params);
2522 }
2523};
2524
2525/**
2526 * Calculates the per tick mean and variance of the samples.
2527 * @sa DistBase, AvgSampleStor
2528 */
2529class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2530{
2531 public:
2532 /**
2533 * Construct and initialize this distribution.
2534 */
2535 AverageDeviation()
2536 {
2537 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2538 this->doInit();
2539 this->setParams(params);
2540 }
2541};
2542
2543/**
2544 * A vector of distributions.
2545 * @sa VectorDistBase, DistStor
2546 */
2547class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2548{
2549 public:
2550 /**
2551 * Initialize storage and parameters for this distribution.
2552 * @param size The size of the vector (the number of distributions).
2553 * @param min The minimum value of the distribution.
2554 * @param max The maximum value of the distribution.
2555 * @param bkt The number of values in each bucket.
2556 * @return A reference to this distribution.
2557 */
2558 VectorDistribution &
2559 init(size_type size, Counter min, Counter max, Counter bkt)
2560 {
2561 DistStor::Params *params = new DistStor::Params;
2562 params->min = min;
2563 params->max = max;
2564 params->bucket_size = bkt;
2565 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2566 this->setParams(params);
2567 this->doInit(size);
2568 return this->self();
2569 }
2570};
2571
2572/**
2573 * This is a vector of StandardDeviation stats.
2574 * @sa VectorDistBase, SampleStor
2575 */
2576class VectorStandardDeviation
2577 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2578{
2579 public:
2580 /**
2581 * Initialize storage for this distribution.
2582 * @param size The size of the vector.
2583 * @return A reference to this distribution.
2584 */
2585 VectorStandardDeviation &
2586 init(size_type size)
2587 {
2588 SampleStor::Params *params = new SampleStor::Params;
2589 this->doInit(size);
2590 this->setParams(params);
2591 return this->self();
2592 }
2593};
2594
2595/**
2596 * This is a vector of AverageDeviation stats.
2597 * @sa VectorDistBase, AvgSampleStor
2598 */
2599class VectorAverageDeviation
2600 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2601{
2602 public:
2603 /**
2604 * Initialize storage for this distribution.
2605 * @param size The size of the vector.
2606 * @return A reference to this distribution.
2607 */
2608 VectorAverageDeviation &
2609 init(size_type size)
2610 {
2611 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2612 this->doInit(size);
2613 this->setParams(params);
2614 return this->self();
2615 }
2616};
2617
2618template <class Stat>
2619class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2620{
2621 protected:
2622 mutable VResult vec;
2623 mutable VCounter cvec;
2624
2625 public:
2626 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2627
2628 size_type size() const { return this->s.size(); }
2629
2630 const VResult &
2631 result() const
2632 {
2633 this->s.result(vec);
2634 return vec;
2635 }
2636 Result total() const { return this->s.total(); }
2637 VCounter &value() const { return cvec; }
2638
2639 std::string str() const { return this->s.str(); }
2640};
2641
2642template <class Stat>
2643class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2644{
2645 public:
2646 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2647};
2648
2649/**
2650 * Implementation of a sparse histogram stat. The storage class is
2651 * determined by the Storage template.
2652 */
2653template <class Derived, class Stor>
2654class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2655{
2656 public:
2657 typedef SparseHistInfoProxy<Derived> Info;
2658 typedef Stor Storage;
2659 typedef typename Stor::Params Params;
2660
2661 protected:
2662 /** The storage for this stat. */
2663 char storage[sizeof(Storage)];
2664
2665 protected:
2666 /**
2667 * Retrieve the storage.
2668 * @return The storage object for this stat.
2669 */
2670 Storage *
2671 data()
2672 {
2673 return reinterpret_cast<Storage *>(storage);
2674 }
2675
2676 /**
2677 * Retrieve a const pointer to the storage.
2678 * @return A const pointer to the storage object for this stat.
2679 */
2680 const Storage *
2681 data() const
2682 {
2683 return reinterpret_cast<const Storage *>(storage);
2684 }
2685
2686 void
2687 doInit()
2688 {
2689 new (storage) Storage(this->info());
2690 this->setInit();
2691 }
2692
2693 public:
2694 SparseHistBase() { }
2695
2696 /**
2697 * Add a value to the distribtion n times. Calls sample on the storage
2698 * class.
2699 * @param v The value to add.
2700 * @param n The number of times to add it, defaults to 1.
2701 */
2702 template <typename U>
2703 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2704
2705 /**
2706 * Return the number of entries in this stat.
2707 * @return The number of entries.
2708 */
2709 size_type size() const { return data()->size(); }
2710 /**
2711 * Return true if no samples have been added.
2712 * @return True if there haven't been any samples.
2713 */
2714 bool zero() const { return data()->zero(); }
2715
2716 void
2717 prepare()
2718 {
2719 Info *info = this->info();
2720 data()->prepare(info, info->data);
2721 }
2722
2723 /**
2724 * Reset stat value to default
2725 */
2726 void
2727 reset()
2728 {
2729 data()->reset(this->info());
2730 }
2731};
2732
2733/**
2734 * Templatized storage and interface for a sparse histogram stat.
2735 */
2736class SparseHistStor
2737{
2738 public:
2739 /** The parameters for a sparse histogram stat. */
2740 struct Params : public DistParams
2741 {
2742 Params() : DistParams(Hist) {}
2743 };
2744
2745 private:
2746 /** Counter for number of samples */
2747 Counter samples;
2748 /** Counter for each bucket. */
2749 MCounter cmap;
2750
2751 public:
2752 SparseHistStor(Info *info)
2753 {
2754 reset(info);
2755 }
2756
2757 /**
2758 * Add a value to the distribution for the given number of times.
2759 * @param val The value to add.
2760 * @param number The number of times to add the value.
2761 */
2762 void
2763 sample(Counter val, int number)
2764 {
2765 cmap[val] += number;
2766 samples += number;
2767 }
2768
2769 /**
2770 * Return the number of buckets in this distribution.
2771 * @return the number of buckets.
2772 */
2773 size_type size() const { return cmap.size(); }
2774
2775 /**
2776 * Returns true if any calls to sample have been made.
2777 * @return True if any values have been sampled.
2778 */
2779 bool
2780 zero() const
2781 {
2782 return samples == Counter();
2783 }
2784
2785 void
2786 prepare(Info *info, SparseHistData &data)
2787 {
2788 MCounter::iterator it;
2789 data.cmap.clear();
2790 for (it = cmap.begin(); it != cmap.end(); it++) {
2791 data.cmap[(*it).first] = (*it).second;
2792 }
2793
2794 data.samples = samples;
2795 }
2796
2797 /**
2798 * Reset stat value to default
2799 */
2800 void
2801 reset(Info *info)
2802 {
2803 cmap.clear();
2804 samples = 0;
2805 }
2806};
2807
2808class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2809{
2810 public:
2811 /**
2812 * Set the parameters of this histogram. @sa HistStor::Params
2813 * @param size The number of buckets in the histogram
2814 * @return A reference to this histogram.
2815 */
2816 SparseHistogram &
2817 init(size_type size)
2818 {
2819 SparseHistStor::Params *params = new SparseHistStor::Params;
2820 this->setParams(params);
2821 this->doInit();
2822 return this->self();
2823 }
2824};
2825
2826class Temp;
2827/**
2828 * A formula for statistics that is calculated when printed. A formula is
2829 * stored as a tree of Nodes that represent the equation to calculate.
2830 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2831 */
2832class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2833{
2834 protected:
2835 /** The root of the tree which represents the Formula */
2836 NodePtr root;
2837 friend class Temp;
2838
2839 public:
2840 /**
2841 * Create and initialize thie formula, and register it with the database.
2842 */
2843 Formula();
2844
2845 /**
2846 * Create a formula with the given root node, register it with the
2847 * database.
2848 * @param r The root of the expression tree.
2849 */
2850 Formula(Temp r);
2851
2852 /**
2853 * Set an unitialized Formula to the given root.
2854 * @param r The root of the expression tree.
2855 * @return a reference to this formula.
2856 */
2857 const Formula &operator=(Temp r);
2858
2859 /**
2860 * Add the given tree to the existing one.
2861 * @param r The root of the expression tree.
2862 * @return a reference to this formula.
2863 */
2864 const Formula &operator+=(Temp r);
2865
2866 /**
2867 * Divide the existing tree by the given one.
2868 * @param r The root of the expression tree.
2869 * @return a reference to this formula.
2870 */
2871 const Formula &operator/=(Temp r);
2872
2873 /**
2874 * Return the result of the Fomula in a vector. If there were no Vector
2875 * components to the Formula, then the vector is size 1. If there were,
2876 * like x/y with x being a vector of size 3, then the result returned will
2877 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2878 * @return The result vector.
2879 */
2880 void result(VResult &vec) const;
2881
2882 /**
2883 * Return the total Formula result. If there is a Vector
2884 * component to this Formula, then this is the result of the
2885 * Formula if the formula is applied after summing all the
2886 * components of the Vector. For example, if Formula is x/y where
2887 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2888 * there is no Vector component, total() returns the same value as
2889 * the first entry in the VResult val() returns.
2890 * @return The total of the result vector.
2891 */
2892 Result total() const;
2893
2894 /**
2895 * Return the number of elements in the tree.
2896 */
2897 size_type size() const;
2898
2899 void prepare() { }
2900
2901 /**
2902 * Formulas don't need to be reset
2903 */
2904 void reset();
2905
2906 /**
2907 *
2908 */
2909 bool zero() const;
2910
2911 std::string str() const;
2912};
2913
2914class FormulaNode : public Node
2915{
2916 private:
2917 const Formula &formula;
2918 mutable VResult vec;
2919
2920 public:
2921 FormulaNode(const Formula &f) : formula(f) {}
2922
2923 size_type size() const { return formula.size(); }
2924 const VResult &result() const { formula.result(vec); return vec; }
2925 Result total() const { return formula.total(); }
2926
2927 std::string str() const { return formula.str(); }
2928};
2929
2930/**
2931 * Helper class to construct formula node trees.
2932 */
2933class Temp
2934{
2935 protected:
2936 /**
2937 * Pointer to a Node object.
2938 */
2939 NodePtr node;
2940
2941 public:
2942 /**
2943 * Copy the given pointer to this class.
2944 * @param n A pointer to a Node object to copy.
2945 */
2946 Temp(NodePtr n) : node(n) { }
2947
2948 /**
2949 * Return the node pointer.
2950 * @return the node pointer.
2951 */
2952 operator NodePtr&() { return node; }
2953
2954 public:
2955 /**
2956 * Create a new ScalarStatNode.
2957 * @param s The ScalarStat to place in a node.
2958 */
2959 Temp(const Scalar &s)
2960 : node(new ScalarStatNode(s.info()))
2961 { }
2962
2963 /**
2964 * Create a new ScalarStatNode.
2965 * @param s The ScalarStat to place in a node.
2966 */
2967 Temp(const Value &s)
2968 : node(new ScalarStatNode(s.info()))
2969 { }
2970
2971 /**
2972 * Create a new ScalarStatNode.
2973 * @param s The ScalarStat to place in a node.
2974 */
2975 Temp(const Average &s)
2976 : node(new ScalarStatNode(s.info()))
2977 { }
2978
2979 /**
2980 * Create a new VectorStatNode.
2981 * @param s The VectorStat to place in a node.
2982 */
2983 Temp(const Vector &s)
2984 : node(new VectorStatNode(s.info()))
2985 { }
2986
2987 Temp(const AverageVector &s)
2988 : node(new VectorStatNode(s.info()))
2989 { }
2990
2991 /**
2992 *
2993 */
2994 Temp(const Formula &f)
2995 : node(new FormulaNode(f))
2996 { }
2997
2998 /**
2999 * Create a new ScalarProxyNode.
3000 * @param p The ScalarProxy to place in a node.
3001 */
3002 template <class Stat>
3003 Temp(const ScalarProxy<Stat> &p)
3004 : node(new ScalarProxyNode<Stat>(p))
3005 { }
3006
3007 /**
3008 * Create a ConstNode
3009 * @param value The value of the const node.
3010 */
3011 Temp(signed char value)
3012 : node(new ConstNode<signed char>(value))
3013 { }
3014
3015 /**
3016 * Create a ConstNode
3017 * @param value The value of the const node.
3018 */
3019 Temp(unsigned char value)
3020 : node(new ConstNode<unsigned char>(value))
3021 { }
3022
3023 /**
3024 * Create a ConstNode
3025 * @param value The value of the const node.
3026 */
3027 Temp(signed short value)
3028 : node(new ConstNode<signed short>(value))
3029 { }
3030
3031 /**
3032 * Create a ConstNode
3033 * @param value The value of the const node.
3034 */
3035 Temp(unsigned short value)
3036 : node(new ConstNode<unsigned short>(value))
3037 { }
3038
3039 /**
3040 * Create a ConstNode
3041 * @param value The value of the const node.
3042 */
3043 Temp(signed int value)
3044 : node(new ConstNode<signed int>(value))
3045 { }
3046
3047 /**
3048 * Create a ConstNode
3049 * @param value The value of the const node.
3050 */
3051 Temp(unsigned int value)
3052 : node(new ConstNode<unsigned int>(value))
3053 { }
3054
3055 /**
3056 * Create a ConstNode
3057 * @param value The value of the const node.
3058 */
3059 Temp(signed long value)
3060 : node(new ConstNode<signed long>(value))
3061 { }
3062
3063 /**
3064 * Create a ConstNode
3065 * @param value The value of the const node.
3066 */
3067 Temp(unsigned long value)
3068 : node(new ConstNode<unsigned long>(value))
3069 { }
3070
3071 /**
3072 * Create a ConstNode
3073 * @param value The value of the const node.
3074 */
3075 Temp(signed long long value)
3076 : node(new ConstNode<signed long long>(value))
3077 { }
3078
3079 /**
3080 * Create a ConstNode
3081 * @param value The value of the const node.
3082 */
3083 Temp(unsigned long long value)
3084 : node(new ConstNode<unsigned long long>(value))
3085 { }
3086
3087 /**
3088 * Create a ConstNode
3089 * @param value The value of the const node.
3090 */
3091 Temp(float value)
3092 : node(new ConstNode<float>(value))
3093 { }
3094
3095 /**
3096 * Create a ConstNode
3097 * @param value The value of the const node.
3098 */
3099 Temp(double value)
3100 : node(new ConstNode<double>(value))
3101 { }
3102};
3103
3104
3105/**
3106 * @}
3107 */
3108
3109inline Temp
3110operator+(Temp l, Temp r)
3111{
3112 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
3113}
3114
3115inline Temp
3116operator-(Temp l, Temp r)
3117{
3118 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
3119}
3120
3121inline Temp
3122operator*(Temp l, Temp r)
3123{
3124 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
3125}
3126
3127inline Temp
3128operator/(Temp l, Temp r)
3129{
3130 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
3131}
3132
3133inline Temp
3134operator-(Temp l)
3135{
3136 return NodePtr(new UnaryNode<std::negate<Result> >(l));
3137}
3138
3139template <typename T>
3140inline Temp
3141constant(T val)
3142{
3143 return NodePtr(new ConstNode<T>(val));
3144}
3145
3146template <typename T>
3147inline Temp
3148constantVector(T val)
3149{
3150 return NodePtr(new ConstVectorNode<T>(val));
3151}
3152
3153inline Temp
3154sum(Temp val)
3155{
3156 return NodePtr(new SumNode<std::plus<Result> >(val));
3157}
3158
3159/** Dump all statistics data to the registered outputs */
3160void dump();
3161void reset();
3162void enable();
3163bool enabled();
3164
3165/**
3166 * Register a callback that should be called whenever statistics are
3167 * reset
3168 */
3169void registerResetCallback(Callback *cb);
3170
3171/**
3172 * Register a callback that should be called whenever statistics are
3173 * about to be dumped
3174 */
3175void registerDumpCallback(Callback *cb);
3176
3177std::list<Info *> &statsList();
3178
3179typedef std::map<const void *, Info *> MapType;
3180MapType &statsMap();
3181
3182typedef std::map<std::string, Info *> NameMapType;
3183NameMapType &nameMap();
3184
3185bool validateStatName(const std::string &name);
3186
3187} // namespace Stats
3188
3189void debugDumpStats();
3190
3191#endif // __BASE_STATISTICS_HH__