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
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__