2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
30 * Declaration of Statistics objects.
36 * Generalized N-dimensinal vector
40 * -- these both can use the same function that prints out a
41 * specific set of stats
42 * VectorStandardDeviation totals
45 #ifndef __BASE_STATISTICS_HH__
46 #define __BASE_STATISTICS_HH__
56 #include "base/cprintf.hh"
57 #include "base/intmath.hh"
58 #include "base/refcnt.hh"
59 #include "base/str.hh"
60 #include "base/stats/bin.hh"
61 #include "base/stats/flags.hh"
62 #include "base/stats/visit.hh"
63 #include "base/stats/types.hh"
64 #include "config/stats_binning.hh"
65 #include "sim/host.hh"
69 /** The current simulated cycle. */
72 /* A namespace for all of the Statistics */
75 /* Contains the statistic implementation details */
76 //////////////////////////////////////////////////////////////////////
78 // Statistics Framework Base classes
80 //////////////////////////////////////////////////////////////////////
83 /** The name of the stat. */
85 /** The description of the stat. */
87 /** The formatting flags. */
89 /** The display precision. */
91 /** A pointer to a prerequisite Stat. */
92 const StatData *prereq;
94 * A unique stat ID for each stat in the simulator.
95 * Can be used externally for lookups as well as for debugging.
103 * @return true if the stat is binned.
105 virtual bool binned() const = 0;
108 * Reset the corresponding stat to the default state.
110 virtual void reset() = 0;
113 * @return true if this stat has a value and satisfies its
114 * requirement as a prereq
116 virtual bool zero() const = 0;
119 * Check that this stat has been set up properly and is ready for
121 * @return true for success
123 virtual bool check() const = 0;
124 bool baseCheck() const;
127 * Visitor entry for outputing statistics data
129 virtual void visit(Visit &visitor) = 0;
132 * Checks if the first stat's name is alphabetically less than the second.
133 * This function breaks names up at periods and considers each subname
135 * @param stat1 The first stat.
136 * @param stat2 The second stat.
137 * @return stat1's name is alphabetically before stat2's
139 static bool less(StatData *stat1, StatData *stat2);
142 class ScalarData : public StatData
145 virtual Counter value() const = 0;
146 virtual Result result() const = 0;
147 virtual Result total() const = 0;
148 virtual void visit(Visit &visitor) { visitor.visit(*this); }
151 template <class Stat>
152 class ScalarStatData : public ScalarData
158 ScalarStatData(Stat &stat) : s(stat) {}
160 virtual bool binned() const { return s.binned(); }
161 virtual bool check() const { return s.check(); }
162 virtual Counter value() const { return s.value(); }
163 virtual Result result() const { return s.result(); }
164 virtual Result total() const { return s.total(); }
165 virtual void reset() { s.reset(); }
166 virtual bool zero() const { return s.zero(); }
169 struct VectorData : public StatData
171 /** Names and descriptions of subfields. */
172 mutable std::vector<std::string> subnames;
173 mutable std::vector<std::string> subdescs;
175 virtual size_t size() const = 0;
176 virtual const VCounter &value() const = 0;
177 virtual const VResult &result() const = 0;
178 virtual Result total() const = 0;
181 if (!subnames.empty()) {
183 if (subnames.size() < s)
186 if (subdescs.size() < s)
192 template <class Stat>
193 class VectorStatData : public VectorData
197 mutable VCounter cvec;
198 mutable VResult rvec;
201 VectorStatData(Stat &stat) : s(stat) {}
203 virtual bool binned() const { return s.binned(); }
204 virtual bool check() const { return s.check(); }
205 virtual bool zero() const { return s.zero(); }
206 virtual void reset() { s.reset(); }
208 virtual size_t size() const { return s.size(); }
209 virtual VCounter &value() const
214 virtual const VResult &result() const
219 virtual Result total() const { return s.total(); }
220 virtual void visit(Visit &visitor)
224 visitor.visit(*this);
246 struct DistData : public StatData
248 /** Local storage for the entry values, used for printing. */
252 template <class Stat>
253 class DistStatData : public DistData
259 DistStatData(Stat &stat) : s(stat) {}
261 virtual bool binned() const { return s.binned(); }
262 virtual bool check() const { return s.check(); }
263 virtual void reset() { s.reset(); }
264 virtual bool zero() const { return s.zero(); }
265 virtual void visit(Visit &visitor)
268 visitor.visit(*this);
272 struct VectorDistData : public StatData
274 std::vector<DistDataData> data;
276 /** Names and descriptions of subfields. */
277 mutable std::vector<std::string> subnames;
278 mutable std::vector<std::string> subdescs;
280 /** Local storage for the entry values, used for printing. */
281 mutable VResult rvec;
283 virtual size_t size() const = 0;
287 if (subnames.size() < s)
290 if (subdescs.size() < s)
295 template <class Stat>
296 class VectorDistStatData : public VectorDistData
300 typedef typename Stat::bin_t bin_t;
303 VectorDistStatData(Stat &stat) : s(stat) {}
305 virtual bool binned() const { return bin_t::binned; }
306 virtual bool check() const { return s.check(); }
307 virtual void reset() { s.reset(); }
308 virtual size_t size() const { return s.size(); }
309 virtual bool zero() const { return s.zero(); }
310 virtual void visit(Visit &visitor)
314 visitor.visit(*this);
318 struct Vector2dData : public StatData
320 /** Names and descriptions of subfields. */
321 std::vector<std::string> subnames;
322 std::vector<std::string> subdescs;
323 std::vector<std::string> y_subnames;
325 /** Local storage for the entry values, used for printing. */
326 mutable VCounter cvec;
332 if (subnames.size() < x)
337 template <class Stat>
338 class Vector2dStatData : public Vector2dData
342 typedef typename Stat::bin_t bin_t;
345 Vector2dStatData(Stat &stat) : s(stat) {}
347 virtual bool binned() const { return bin_t::binned; }
348 virtual bool check() const { return s.check(); }
349 virtual void reset() { s.reset(); }
350 virtual bool zero() const { return s.zero(); }
351 virtual void visit(Visit &visitor)
355 visitor.visit(*this);
363 StatData *find() const;
364 void map(StatData *data);
366 StatData *statData();
367 const StatData *statData() const;
373 template <class Parent, class Child, template <class> class Data>
374 class Wrap : public Child
377 Parent &self() { return *reinterpret_cast<Parent *>(this); }
380 Data<Child> *statData()
382 StatData *__data = DataAccess::statData();
383 Data<Child> *ptr = dynamic_cast<Data<Child> *>(__data);
389 const Data<Child> *statData() const
391 const StatData *__data = DataAccess::statData();
392 const Data<Child> *ptr = dynamic_cast<const Data<Child> *>(__data);
399 * Copy constructor, copies are not allowed.
401 Wrap(const Wrap &stat);
405 void operator=(const Wrap &);
410 map(new Data<Child>(*this));
414 * Set the name and marks this stat to print at the end of simulation.
415 * @param name The new name.
416 * @return A reference to this stat.
418 Parent &name(const std::string &_name)
420 Data<Child> *data = this->statData();
427 * Set the description and marks this stat to print at the end of
429 * @param desc The new description.
430 * @return A reference to this stat.
432 Parent &desc(const std::string &_desc)
434 this->statData()->desc = _desc;
439 * Set the precision and marks this stat to print at the end of simulation.
440 * @param p The new precision
441 * @return A reference to this stat.
443 Parent &precision(int _precision)
445 this->statData()->precision = _precision;
450 * Set the flags and marks this stat to print at the end of simulation.
451 * @param f The new flags.
452 * @return A reference to this stat.
454 Parent &flags(StatFlags _flags)
456 this->statData()->flags |= _flags;
461 * Set the prerequisite stat and marks this stat to print at the end of
463 * @param prereq The prerequisite stat.
464 * @return A reference to this stat.
466 template <class Stat>
467 Parent &prereq(const Stat &prereq)
469 this->statData()->prereq = prereq.statData();
474 template <class Parent, class Child, template <class Child> class Data>
475 class WrapVec : public Wrap<Parent, Child, Data>
478 // The following functions are specific to vectors. If you use them
479 // in a non vector context, you will get a nice compiler error!
482 * Set the subfield name for the given index, and marks this stat to print
483 * at the end of simulation.
484 * @param index The subfield index.
485 * @param name The new name of the subfield.
486 * @return A reference to this stat.
488 Parent &subname(int index, const std::string &name)
490 std::vector<std::string> &subn = this->statData()->subnames;
491 if (subn.size() <= index)
492 subn.resize(index + 1);
498 * Set the subfield description for the given index and marks this stat to
499 * print at the end of simulation.
500 * @param index The subfield index.
501 * @param desc The new description of the subfield
502 * @return A reference to this stat.
504 Parent &subdesc(int index, const std::string &desc)
506 std::vector<std::string> &subd = this->statData()->subdescs;
507 if (subd.size() <= index)
508 subd.resize(index + 1);
516 template <class Parent, class Child, template <class Child> class Data>
517 class WrapVec2d : public WrapVec<Parent, Child, Data>
521 * @warning This makes the assumption that if you're gonna subnames a 2d
522 * vector, you're subnaming across all y
524 Parent &ysubnames(const char **names)
526 Data<Child> *data = this->statData();
527 data->y_subnames.resize(this->y);
528 for (int i = 0; i < this->y; ++i)
529 data->y_subnames[i] = names[i];
532 Parent &ysubname(int index, const std::string subname)
534 Data<Child> *data = this->statData();
535 assert(index < this->y);
536 data->y_subnames.resize(this->y);
537 data->y_subnames[index] = subname.c_str();
542 //////////////////////////////////////////////////////////////////////
546 //////////////////////////////////////////////////////////////////////
549 * Templatized storage and interface for a simple scalar stat.
554 /** The paramaters for this storage type, none for a scalar. */
558 /** The statistic value. */
563 * Builds this storage element and calls the base constructor of the
566 StatStor(const Params &) : data(Counter()) {}
569 * The the stat to the given value.
570 * @param val The new value.
571 * @param p The paramters of this storage type.
573 void set(Counter val, const Params &p) { data = val; }
575 * Increment the stat by the given value.
576 * @param val The new value.
577 * @param p The paramters of this storage type.
579 void inc(Counter val, const Params &p) { data += val; }
581 * Decrement the stat by the given value.
582 * @param val The new value.
583 * @param p The paramters of this storage type.
585 void dec(Counter val, const Params &p) { data -= val; }
587 * Return the value of this stat as its base type.
588 * @param p The params of this storage type.
589 * @return The value of this stat.
591 Counter value(const Params &p) const { return data; }
593 * Return the value of this stat as a result type.
594 * @param p The parameters of this storage type.
595 * @return The value of this stat.
597 Result result(const Params &p) const { return (Result)data; }
599 * Reset stat value to default
601 void reset() { data = Counter(); }
604 * @return true if zero value
606 bool zero() const { return data == Counter(); }
610 * Templatized storage and interface to a per-cycle average stat. This keeps
611 * a current count and updates a total (count * cycles) when this count
612 * changes. This allows the quick calculation of a per cycle count of the item
613 * being watched. This is good for keeping track of residencies in structures
614 * among other things.
615 * @todo add lateny to the stat and fix binning.
620 /** The paramaters for this storage type */
624 * The current count. We stash this here because the current
625 * value is not a binned value.
631 /** The total count for all cycles. */
632 mutable Result total;
633 /** The cycle that current last changed. */
638 * Build and initializes this stat storage.
640 AvgStor(Params &p) : total(0), last(0) { p.current = Counter(); }
643 * Set the current count to the one provided, update the total and last
645 * @param val The new count.
646 * @param p The parameters for this storage.
648 void set(Counter val, Params &p) {
649 total += p.current * (curTick - last);
655 * Increment the current count by the provided value, calls set.
656 * @param val The amount to increment.
657 * @param p The parameters for this storage.
659 void inc(Counter val, Params &p) { set(p.current + val, p); }
662 * Deccrement the current count by the provided value, calls set.
663 * @param val The amount to decrement.
664 * @param p The parameters for this storage.
666 void dec(Counter val, Params &p) { set(p.current - val, p); }
669 * Return the current count.
670 * @param p The parameters for this storage.
671 * @return The current count.
673 Counter value(const Params &p) const { return p.current; }
676 * Return the current average.
677 * @param p The parameters for this storage.
678 * @return The current average.
680 Result result(const Params &p) const
682 total += p.current * (curTick - last);
684 return (Result)(total + p.current) / (Result)(curTick + 1);
688 * Reset stat value to default
697 * @return true if zero value
699 bool zero() const { return total == 0.0; }
703 * Implementation of a scalar stat. The type of stat is determined by the
704 * Storage template. The storage for this stat is held within the Bin class.
705 * This allows for breaking down statistics across multiple bins easily.
707 template <class Storage, class Bin>
708 class ScalarBase : public DataAccess
711 /** Define the params of the storage class. */
712 typedef typename Storage::Params params_t;
713 /** Define the bin type. */
714 typedef typename Bin::template Bin<Storage> bin_t;
717 /** The bin of this stat. */
719 /** The parameters for this stat. */
724 * Retrieve the storage from the bin.
725 * @return The storage object for this stat.
727 Storage *data() { return bin.data(params); }
729 * Retrieve a const pointer to the storage from the bin.
730 * @return A const pointer to the storage object for this stat.
732 const Storage *data() const
734 bin_t *_bin = const_cast<bin_t *>(&bin);
735 params_t *_params = const_cast<params_t *>(¶ms);
736 return _bin->data(*_params);
741 * Return the current value of this stat as its base type.
742 * @return The current value.
744 Counter value() const { return data()->value(params); }
748 * Create and initialize this stat, register it with the database.
756 // Common operators for stats
758 * Increment the stat by 1. This calls the associated storage object inc
761 void operator++() { data()->inc(1, params); }
763 * Decrement the stat by 1. This calls the associated storage object dec
766 void operator--() { data()->dec(1, params); }
768 /** Increment the stat by 1. */
769 void operator++(int) { ++*this; }
770 /** Decrement the stat by 1. */
771 void operator--(int) { --*this; }
774 * Set the data value to the given value. This calls the associated storage
775 * object set function.
776 * @param v The new value.
778 template <typename U>
779 void operator=(const U &v) { data()->set(v, params); }
782 * Increment the stat by the given value. This calls the associated
783 * storage object inc function.
784 * @param v The value to add.
786 template <typename U>
787 void operator+=(const U &v) { data()->inc(v, params); }
790 * Decrement the stat by the given value. This calls the associated
791 * storage object dec function.
792 * @param v The value to substract.
794 template <typename U>
795 void operator-=(const U &v) { data()->dec(v, params); }
798 * Return the number of elements, always 1 for a scalar.
801 size_t size() const { return 1; }
803 * Return true if stat is binned.
804 *@return True is stat is binned.
806 bool binned() const { return bin_t::binned; }
808 bool check() const { return bin.initialized(); }
811 * Reset stat value to default
813 void reset() { bin.reset(); }
815 Counter value() { return data()->value(params); }
817 Result result() { return data()->result(params); }
819 Result total() { return result(); }
821 bool zero() { return result() == 0.0; }
825 class ProxyData : public ScalarData
828 virtual void visit(Visit &visitor) { visitor.visit(*this); }
829 virtual bool binned() const { return false; }
830 virtual std::string str() const { return to_string(value()); }
831 virtual size_t size() const { return 1; }
832 virtual bool zero() const { return value() == 0; }
833 virtual bool check() const { return true; }
834 virtual void reset() { }
838 class ValueProxy : public ProxyData
844 ValueProxy(T &val) : scalar(&val) {}
845 virtual Counter value() const { return *scalar; }
846 virtual Result result() const { return *scalar; }
847 virtual Result total() const { return *scalar; }
851 class FunctorProxy : public ProxyData
857 FunctorProxy(T &func) : functor(&func) {}
858 virtual Counter value() const { return (*functor)(); }
859 virtual Result result() const { return (*functor)(); }
860 virtual Result total() const { return (*functor)(); }
863 class ValueBase : public DataAccess
869 ValueBase() : proxy(NULL) { }
870 ~ValueBase() { if (proxy) delete proxy; }
873 void scalar(T &value)
875 proxy = new ValueProxy<T>(value);
880 void functor(T &func)
882 proxy = new FunctorProxy<T>(func);
886 Counter value() { return proxy->value(); }
887 Result result() const { return proxy->result(); }
888 Result total() const { return proxy->total(); };
889 size_t size() const { return proxy->size(); }
891 bool binned() const { return proxy->binned(); }
892 std::string str() const { return proxy->str(); }
893 bool zero() const { return proxy->zero(); }
894 bool check() const { return proxy != NULL; }
898 //////////////////////////////////////////////////////////////////////
902 //////////////////////////////////////////////////////////////////////
903 template <class Storage, class Bin>
907 * Implementation of a vector of stats. The type of stat is determined by the
908 * Storage class. @sa ScalarBase
910 template <class Storage, class Bin>
911 class VectorBase : public DataAccess
914 /** Define the params of the storage class. */
915 typedef typename Storage::Params params_t;
916 /** Define the bin type. */
917 typedef typename Bin::template VectorBin<Storage> bin_t;
920 /** The bin of this stat. */
922 /** The parameters for this stat. */
927 * Retrieve the storage from the bin for the given index.
928 * @param index The vector index to access.
929 * @return The storage object at the given index.
931 Storage *data(int index) { return bin.data(index, params); }
933 * Retrieve a const pointer to the storage from the bin
934 * for the given index.
935 * @param index The vector index to access.
936 * @return A const pointer to the storage object at the given index.
938 const Storage *data(int index) const
940 bin_t *_bin = const_cast<bin_t *>(&bin);
941 params_t *_params = const_cast<params_t *>(¶ms);
942 return _bin->data(index, *_params);
946 void value(VCounter &vec) const
949 for (int i = 0; i < size(); ++i)
950 vec[i] = data(i)->value(params);
954 * Copy the values to a local vector and return a reference to it.
955 * @return A reference to a vector of the stat values.
957 void result(VResult &vec) const
960 for (int i = 0; i < size(); ++i)
961 vec[i] = data(i)->result(params);
965 * @return True is stat is binned.
967 bool binned() const { return bin_t::binned; }
970 * Return a total of all entries in this vector.
971 * @return The total of all vector entries.
973 Result total() const {
975 for (int i = 0; i < size(); ++i)
976 total += data(i)->result(params);
981 * @return the number of elements in this vector.
983 size_t size() const { return bin.size(); }
987 for (int i = 0; i < size(); ++i)
993 bool check() const { return bin.initialized(); }
994 void reset() { bin.reset(); }
999 /** Friend this class with the associated scalar proxy. */
1000 friend class ScalarProxy<Storage, Bin>;
1003 * Return a reference (ScalarProxy) to the stat at the given index.
1004 * @param index The vector index to access.
1005 * @return A reference of the stat.
1007 ScalarProxy<Storage, Bin> operator[](int index);
1009 void update(StatData *data) {}
1012 const StatData * getStatData(const void *stat);
1015 * A proxy class to access the stat at a given index in a VectorBase stat.
1016 * Behaves like a ScalarBase.
1018 template <class Storage, class Bin>
1022 /** Define the params of the storage class. */
1023 typedef typename Storage::Params params_t;
1024 /** Define the bin type. */
1025 typedef typename Bin::template VectorBin<Storage> bin_t;
1028 /** Pointer to the bin in the parent VectorBase. */
1030 /** Pointer to the params in the parent VectorBase. */
1032 /** The index to access in the parent VectorBase. */
1034 /** Keep a pointer to the original stat so was can get data */
1039 * Retrieve the storage from the bin.
1040 * @return The storage from the bin for this stat.
1042 Storage *data() { return bin->data(index, *params); }
1044 * Retrieve a const pointer to the storage from the bin.
1045 * @return A const pointer to the storage for this stat.
1047 const Storage *data() const
1049 bin_t *_bin = const_cast<bin_t *>(bin);
1050 params_t *_params = const_cast<params_t *>(params);
1051 return _bin->data(index, *_params);
1056 * Return the current value of this stat as its base type.
1057 * @return The current value.
1059 Counter value() const { return data()->value(*params); }
1062 * Return the current value of this statas a result type.
1063 * @return The current value.
1065 Result result() const { return data()->result(*params); }
1069 * Create and initialize this proxy, do not register it with the database.
1070 * @param b The bin to use.
1071 * @param p The params to use.
1072 * @param i The index to access.
1074 ScalarProxy(bin_t &b, params_t &p, int i, void *s)
1075 : bin(&b), params(&p), index(i), stat(s) {}
1077 * Create a copy of the provided ScalarProxy.
1078 * @param sp The proxy to copy.
1080 ScalarProxy(const ScalarProxy &sp)
1081 : bin(sp.bin), params(sp.params), index(sp.index), stat(sp.stat) {}
1083 * Set this proxy equal to the provided one.
1084 * @param sp The proxy to copy.
1085 * @return A reference to this proxy.
1087 const ScalarProxy &operator=(const ScalarProxy &sp) {
1096 // Common operators for stats
1098 * Increment the stat by 1. This calls the associated storage object inc
1101 void operator++() { data()->inc(1, *params); }
1103 * Decrement the stat by 1. This calls the associated storage object dec
1106 void operator--() { data()->dec(1, *params); }
1108 /** Increment the stat by 1. */
1109 void operator++(int) { ++*this; }
1110 /** Decrement the stat by 1. */
1111 void operator--(int) { --*this; }
1114 * Set the data value to the given value. This calls the associated storage
1115 * object set function.
1116 * @param v The new value.
1118 template <typename U>
1119 void operator=(const U &v) { data()->set(v, *params); }
1122 * Increment the stat by the given value. This calls the associated
1123 * storage object inc function.
1124 * @param v The value to add.
1126 template <typename U>
1127 void operator+=(const U &v) { data()->inc(v, *params); }
1130 * Decrement the stat by the given value. This calls the associated
1131 * storage object dec function.
1132 * @param v The value to substract.
1134 template <typename U>
1135 void operator-=(const U &v) { data()->dec(v, *params); }
1138 * Return the number of elements, always 1 for a scalar.
1141 size_t size() const { return 1; }
1144 * Return true if stat is binned.
1145 *@return false since Proxies aren't printed/binned
1147 bool binned() const { return false; }
1150 * This stat has no state. Nothing to reset
1155 const StatData *statData() const { return getStatData(stat); }
1156 std::string str() const
1158 return csprintf("%s[%d]", this->statData()->name, index);
1163 template <class Storage, class Bin>
1164 inline ScalarProxy<Storage, Bin>
1165 VectorBase<Storage, Bin>::operator[](int index)
1167 assert (index >= 0 && index < size());
1168 return ScalarProxy<Storage, Bin>(bin, params, index, this);
1171 template <class Storage, class Bin>
1174 template <class Storage, class Bin>
1175 class Vector2dBase : public DataAccess
1178 typedef typename Storage::Params params_t;
1179 typedef typename Bin::template VectorBin<Storage> bin_t;
1188 Storage *data(int index) { return bin.data(index, params); }
1189 const Storage *data(int index) const
1191 bin_t *_bin = const_cast<bin_t *>(&bin);
1192 params_t *_params = const_cast<params_t *>(¶ms);
1193 return _bin->data(index, *_params);
1199 void update(Vector2dData *data)
1201 int size = this->size();
1202 data->cvec.resize(size);
1203 for (int i = 0; i < size; ++i)
1204 data->cvec[i] = this->data(i)->value(params);
1207 std::string ysubname(int i) const { return (*this->y_subnames)[i]; }
1209 friend class VectorProxy<Storage, Bin>;
1210 VectorProxy<Storage, Bin> operator[](int index);
1212 size_t size() const { return bin.size(); }
1213 bool zero() const { return data(0)->value(params) == 0.0; }
1216 * Reset stat value to default
1218 void reset() { bin.reset(); }
1220 bool check() { return bin.initialized(); }
1223 template <class Storage, class Bin>
1227 typedef typename Storage::Params params_t;
1228 typedef typename Bin::template VectorBin<Storage> bin_t;
1238 mutable VResult *vec;
1240 Storage *data(int index) {
1241 assert(index < len);
1242 return bin->data(offset + index, *params);
1245 const Storage *data(int index) const {
1246 bin_t *_bin = const_cast<bin_t *>(bin);
1247 params_t *_params = const_cast<params_t *>(params);
1248 return _bin->data(offset + index, *_params);
1252 const VResult &result() const {
1254 vec->resize(size());
1256 vec = new VResult(size());
1258 for (int i = 0; i < size(); ++i)
1259 (*vec)[i] = data(i)->result(*params);
1264 Result total() const {
1266 for (int i = 0; i < size(); ++i)
1267 total += data(i)->result(*params);
1272 VectorProxy(bin_t &b, params_t &p, int o, int l, void *s)
1273 : bin(&b), params(&p), offset(o), len(l), stat(s), vec(NULL)
1277 VectorProxy(const VectorProxy &sp)
1278 : bin(sp.bin), params(sp.params), offset(sp.offset), len(sp.len),
1279 stat(sp.stat), vec(NULL)
1289 const VectorProxy &operator=(const VectorProxy &sp)
1302 ScalarProxy<Storage, Bin> operator[](int index)
1304 assert (index >= 0 && index < size());
1305 return ScalarProxy<Storage, Bin>(*bin, *params, offset + index, stat);
1308 size_t size() const { return len; }
1311 * Return true if stat is binned.
1312 *@return false since Proxies aren't printed/binned
1314 bool binned() const { return false; }
1317 * This stat has no state. Nothing to reset.
1322 template <class Storage, class Bin>
1323 inline VectorProxy<Storage, Bin>
1324 Vector2dBase<Storage, Bin>::operator[](int index)
1326 int offset = index * y;
1327 assert (index >= 0 && offset < size());
1328 return VectorProxy<Storage, Bin>(bin, params, offset, y, this);
1331 //////////////////////////////////////////////////////////////////////
1333 // Non formula statistics
1335 //////////////////////////////////////////////////////////////////////
1338 * Templatized storage and interface for a distrbution stat.
1343 /** The parameters for a distribution stat. */
1346 /** The minimum value to track. */
1348 /** The maximum value to track. */
1350 /** The number of entries in each bucket. */
1351 Counter bucket_size;
1352 /** The number of buckets. Equal to (max-min)/bucket_size. */
1355 enum { fancy = false };
1358 /** The smallest value sampled. */
1360 /** The largest value sampled. */
1362 /** The number of values sampled less than min. */
1364 /** The number of values sampled more than max. */
1366 /** The current sum. */
1368 /** The sum of squares. */
1370 /** The number of samples. */
1372 /** Counter for each bucket. */
1377 * Construct this storage with the supplied params.
1378 * @param params The parameters.
1380 DistStor(const Params ¶ms)
1381 : min_val(INT_MAX), max_val(INT_MIN), underflow(Counter()),
1382 overflow(Counter()), sum(Counter()), squares(Counter()),
1383 samples(Counter()), cvec(params.size)
1389 * Add a value to the distribution for the given number of times.
1390 * @param val The value to add.
1391 * @param number The number of times to add the value.
1392 * @param params The paramters of the distribution.
1394 void sample(Counter val, int number, const Params ¶ms)
1396 if (val < params.min)
1397 underflow += number;
1398 else if (val > params.max)
1401 int index = (int)floor((val - params.min) / params.bucket_size);
1402 assert(index < size(params));
1403 cvec[index] += number;
1412 Counter sample = val * number;
1414 squares += sample * sample;
1419 * Return the number of buckets in this distribution.
1420 * @return the number of buckets.
1421 * @todo Is it faster to return the size from the parameters?
1423 size_t size(const Params &) const { return cvec.size(); }
1426 * Returns true if any calls to sample have been made.
1427 * @param params The paramters of the distribution.
1428 * @return True if any values have been sampled.
1430 bool zero(const Params ¶ms) const
1432 return samples == Counter();
1435 void update(DistDataData *data, const Params ¶ms)
1437 data->min = params.min;
1438 data->max = params.max;
1439 data->bucket_size = params.bucket_size;
1440 data->size = params.size;
1442 data->min_val = (min_val == INT_MAX) ? 0 : min_val;
1443 data->max_val = (max_val == INT_MIN) ? 0 : max_val;
1444 data->underflow = underflow;
1445 data->overflow = overflow;
1446 data->cvec.resize(params.size);
1447 for (int i = 0; i < params.size; ++i)
1448 data->cvec[i] = cvec[i];
1451 data->squares = squares;
1452 data->samples = samples;
1456 * Reset stat value to default
1465 int size = cvec.size();
1466 for (int i = 0; i < size; ++i)
1467 cvec[i] = Counter();
1470 squares = Counter();
1471 samples = Counter();
1476 * Templatized storage and interface for a distribution that calculates mean
1483 * No paramters for this storage.
1486 enum { fancy = true };
1489 /** The current sum. */
1491 /** The sum of squares. */
1493 /** The number of samples. */
1498 * Create and initialize this storage.
1500 FancyStor(const Params &)
1501 : sum(Counter()), squares(Counter()), samples(Counter())
1505 * Add a value the given number of times to this running average.
1506 * Update the running sum and sum of squares, increment the number of
1507 * values seen by the given number.
1508 * @param val The value to add.
1509 * @param number The number of times to add the value.
1510 * @param p The parameters of this stat.
1512 void sample(Counter val, int number, const Params &p)
1514 Counter value = val * number;
1516 squares += value * value;
1520 void update(DistDataData *data, const Params ¶ms)
1523 data->squares = squares;
1524 data->samples = samples;
1528 * Return the number of entries in this stat, 1
1531 size_t size(const Params &) const { return 1; }
1534 * Return true if no samples have been added.
1535 * @return True if no samples have been added.
1537 bool zero(const Params &) const { return samples == Counter(); }
1540 * Reset stat value to default
1545 squares = Counter();
1546 samples = Counter();
1551 * Templatized storage for distribution that calculates per cycle mean and
1557 /** No parameters for this storage. */
1559 enum { fancy = true };
1562 /** Current total. */
1564 /** Current sum of squares. */
1569 * Create and initialize this storage.
1571 AvgFancy(const Params &) : sum(Counter()), squares(Counter()) {}
1574 * Add a value to the distribution for the given number of times.
1575 * Update the running sum and sum of squares.
1576 * @param val The value to add.
1577 * @param number The number of times to add the value.
1578 * @param p The paramters of the distribution.
1580 void sample(Counter val, int number, const Params &p)
1582 Counter value = val * number;
1584 squares += value * value;
1587 void update(DistDataData *data, const Params ¶ms)
1590 data->squares = squares;
1591 data->samples = curTick;
1595 * Return the number of entries, in this case 1.
1598 size_t size(const Params ¶ms) const { return 1; }
1600 * Return true if no samples have been added.
1601 * @return True if the sum is zero.
1603 bool zero(const Params ¶ms) const { return sum == Counter(); }
1605 * Reset stat value to default
1610 squares = Counter();
1615 * Implementation of a distribution stat. The type of distribution is
1616 * determined by the Storage template. @sa ScalarBase
1618 template <class Storage, class Bin>
1619 class DistBase : public DataAccess
1622 /** Define the params of the storage class. */
1623 typedef typename Storage::Params params_t;
1624 /** Define the bin type. */
1625 typedef typename Bin::template Bin<Storage> bin_t;
1628 /** The bin of this stat. */
1630 /** The parameters for this stat. */
1635 * Retrieve the storage from the bin.
1636 * @return The storage object for this stat.
1638 Storage *data() { return bin.data(params); }
1640 * Retrieve a const pointer to the storage from the bin.
1641 * @return A const pointer to the storage object for this stat.
1643 const Storage *data() const
1645 bin_t *_bin = const_cast<bin_t *>(&bin);
1646 params_t *_params = const_cast<params_t *>(¶ms);
1647 return _bin->data(*_params);
1654 * Add a value to the distribtion n times. Calls sample on the storage
1656 * @param v The value to add.
1657 * @param n The number of times to add it, defaults to 1.
1659 template <typename U>
1660 void sample(const U &v, int n = 1) { data()->sample(v, n, params); }
1663 * Return the number of entries in this stat.
1664 * @return The number of entries.
1666 size_t size() const { return data()->size(params); }
1668 * Return true if no samples have been added.
1669 * @return True if there haven't been any samples.
1671 bool zero() const { return data()->zero(params); }
1673 void update(DistData *base)
1675 base->data.fancy = Storage::fancy;
1676 data()->update(&(base->data), params);
1679 * @return True is stat is binned.
1681 bool binned() const { return bin_t::binned; }
1683 * Reset stat value to default
1690 bool check() { return bin.initialized(); }
1693 template <class Storage, class Bin>
1696 template <class Storage, class Bin>
1697 class VectorDistBase : public DataAccess
1700 typedef typename Storage::Params params_t;
1701 typedef typename Bin::template VectorBin<Storage> bin_t;
1708 Storage *data(int index) { return bin.data(index, params); }
1709 const Storage *data(int index) const
1711 bin_t *_bin = const_cast<bin_t *>(&bin);
1712 params_t *_params = const_cast<params_t *>(¶ms);
1713 return _bin->data(index, *_params);
1719 friend class DistProxy<Storage, Bin>;
1720 DistProxy<Storage, Bin> operator[](int index);
1721 const DistProxy<Storage, Bin> operator[](int index) const;
1723 size_t size() const { return bin.size(); }
1724 bool zero() const { return false; }
1726 * Return true if stat is binned.
1727 *@return True is stat is binned.
1729 bool binned() const { return bin_t::binned; }
1731 * Reset stat value to default
1733 void reset() { bin.reset(); }
1735 bool check() { return bin.initialized(); }
1736 void update(VectorDistData *base)
1738 int size = this->size();
1739 base->data.resize(size);
1740 for (int i = 0; i < size; ++i) {
1741 base->data[i].fancy = Storage::fancy;
1742 data(i)->update(&(base->data[i]), params);
1747 template <class Storage, class Bin>
1751 typedef typename Storage::Params params_t;
1752 typedef typename Bin::template Bin<Storage> bin_t;
1753 typedef VectorDistBase<Storage, Bin> base_t;
1758 const base_t *cstat;
1763 Storage *data() { return stat->data(index); }
1764 const Storage *data() const { return cstat->data(index); }
1767 DistProxy(const VectorDistBase<Storage, Bin> &s, int i)
1768 : cstat(&s), index(i) {}
1769 DistProxy(const DistProxy &sp)
1770 : cstat(sp.cstat), index(sp.index) {}
1771 const DistProxy &operator=(const DistProxy &sp) {
1772 cstat = sp.cstat; index = sp.index; return *this;
1776 template <typename U>
1777 void sample(const U &v, int n = 1) { data()->sample(v, n, cstat->params); }
1779 size_t size() const { return 1; }
1780 bool zero() const { return data()->zero(cstat->params); }
1782 * Return true if stat is binned.
1783 *@return false since Proxies are not binned/printed.
1785 bool binned() const { return false; }
1787 * Proxy has no state. Nothing to reset.
1792 template <class Storage, class Bin>
1793 inline DistProxy<Storage, Bin>
1794 VectorDistBase<Storage, Bin>::operator[](int index)
1796 assert (index >= 0 && index < size());
1797 return DistProxy<Storage, Bin>(*this, index);
1800 template <class Storage, class Bin>
1801 inline const DistProxy<Storage, Bin>
1802 VectorDistBase<Storage, Bin>::operator[](int index) const
1804 assert (index >= 0 && index < size());
1805 return DistProxy<Storage, Bin>(*this, index);
1809 template <class Storage, class Bin>
1811 VectorDistBase<Storage, Bin>::total(int index) const
1814 for (int i=0; i < x_size(); ++i) {
1815 total += data(i)->result(*params);
1820 //////////////////////////////////////////////////////////////////////
1824 //////////////////////////////////////////////////////////////////////
1827 * Base class for formula statistic node. These nodes are used to build a tree
1828 * that represents the formula.
1830 class Node : public RefCounted
1834 * Return the number of nodes in the subtree starting at this node.
1835 * @return the number of nodes in this subtree.
1837 virtual size_t size() const = 0;
1839 * Return the result vector of this subtree.
1840 * @return The result vector of this subtree.
1842 virtual const VResult &result() const = 0;
1844 * Return the total of the result vector.
1845 * @return The total of the result vector.
1847 virtual Result total() const = 0;
1849 * Return true if stat is binned.
1850 *@return True is stat is binned.
1852 virtual bool binned() const = 0;
1857 virtual std::string str() const = 0;
1860 /** Reference counting pointer to a function Node. */
1861 typedef RefCountingPtr<Node> NodePtr;
1863 class ScalarStatNode : public Node
1866 const ScalarData *data;
1867 mutable VResult vresult;
1870 ScalarStatNode(const ScalarData *d) : data(d), vresult(1) {}
1871 virtual const VResult &result() const
1873 vresult[0] = data->result();
1876 virtual Result total() const { return data->result(); };
1878 virtual size_t size() const { return 1; }
1880 * Return true if stat is binned.
1881 *@return True is stat is binned.
1883 virtual bool binned() const { return data->binned(); }
1888 virtual std::string str() const { return data->name; }
1891 template <class Storage, class Bin>
1892 class ScalarProxyNode : public Node
1895 const ScalarProxy<Storage, Bin> proxy;
1896 mutable VResult vresult;
1899 ScalarProxyNode(const ScalarProxy<Storage, Bin> &p)
1900 : proxy(p), vresult(1) { }
1901 virtual const VResult &result() const
1903 vresult[0] = proxy.result();
1906 virtual Result total() const { return proxy.result(); };
1908 virtual size_t size() const { return 1; }
1910 * Return true if stat is binned.
1911 *@return True is stat is binned.
1913 virtual bool binned() const { return proxy.binned(); }
1918 virtual std::string str() const { return proxy.str(); }
1921 class VectorStatNode : public Node
1924 const VectorData *data;
1927 VectorStatNode(const VectorData *d) : data(d) { }
1928 virtual const VResult &result() const { return data->result(); }
1929 virtual Result total() const { return data->total(); };
1931 virtual size_t size() const { return data->size(); }
1933 * Return true if stat is binned.
1934 *@return True is stat is binned.
1936 virtual bool binned() const { return data->binned(); }
1938 virtual std::string str() const { return data->name; }
1942 class ConstNode : public Node
1948 ConstNode(T s) : vresult(1, (Result)s) {}
1949 const VResult &result() const { return vresult; }
1950 virtual Result total() const { return vresult[0]; };
1951 virtual size_t size() const { return 1; }
1954 * Return true if stat is binned.
1955 *@return False since constants aren't binned.
1957 virtual bool binned() const { return false; }
1959 virtual std::string str() const { return to_string(vresult[0]); }
1966 struct OpString<std::plus<Result> >
1968 static std::string str() { return "+"; }
1972 struct OpString<std::minus<Result> >
1974 static std::string str() { return "-"; }
1978 struct OpString<std::multiplies<Result> >
1980 static std::string str() { return "*"; }
1984 struct OpString<std::divides<Result> >
1986 static std::string str() { return "/"; }
1990 struct OpString<std::modulus<Result> >
1992 static std::string str() { return "%"; }
1996 struct OpString<std::negate<Result> >
1998 static std::string str() { return "-"; }
2002 class UnaryNode : public Node
2006 mutable VResult vresult;
2009 UnaryNode(NodePtr &p) : l(p) {}
2011 const VResult &result() const
2013 const VResult &lvec = l->result();
2014 int size = lvec.size();
2018 vresult.resize(size);
2020 for (int i = 0; i < size; ++i)
2021 vresult[i] = op(lvec[i]);
2026 Result total() const {
2028 return op(l->total());
2031 virtual size_t size() const { return l->size(); }
2033 * Return true if child of node is binned.
2034 *@return True if child of node is binned.
2036 virtual bool binned() const { return l->binned(); }
2038 virtual std::string str() const
2040 return OpString<Op>::str() + l->str();
2045 class BinaryNode : public Node
2050 mutable VResult vresult;
2053 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2055 const VResult &result() const
2058 const VResult &lvec = l->result();
2059 const VResult &rvec = r->result();
2061 assert(lvec.size() > 0 && rvec.size() > 0);
2063 if (lvec.size() == 1 && rvec.size() == 1) {
2065 vresult[0] = op(lvec[0], rvec[0]);
2066 } else if (lvec.size() == 1) {
2067 int size = rvec.size();
2068 vresult.resize(size);
2069 for (int i = 0; i < size; ++i)
2070 vresult[i] = op(lvec[0], rvec[i]);
2071 } else if (rvec.size() == 1) {
2072 int size = lvec.size();
2073 vresult.resize(size);
2074 for (int i = 0; i < size; ++i)
2075 vresult[i] = op(lvec[i], rvec[0]);
2076 } else if (rvec.size() == lvec.size()) {
2077 int size = rvec.size();
2078 vresult.resize(size);
2079 for (int i = 0; i < size; ++i)
2080 vresult[i] = op(lvec[i], rvec[i]);
2086 Result total() const {
2088 return op(l->total(), r->total());
2091 virtual size_t size() const {
2099 assert(ls == rs && "Node vector sizes are not equal");
2104 * Return true if any children of node are binned
2105 *@return True if either child of node is binned.
2107 virtual bool binned() const { return (l->binned() || r->binned()); }
2109 virtual std::string str() const
2111 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2116 class SumNode : public Node
2120 mutable VResult vresult;
2123 SumNode(NodePtr &p) : l(p), vresult(1) {}
2125 const VResult &result() const
2127 const VResult &lvec = l->result();
2128 int size = lvec.size();
2134 for (int i = 0; i < size; ++i)
2135 vresult[0] = op(vresult[0], lvec[i]);
2140 Result total() const
2142 const VResult &lvec = l->result();
2143 int size = lvec.size();
2146 Result vresult = 0.0;
2149 for (int i = 0; i < size; ++i)
2150 vresult = op(vresult, lvec[i]);
2155 virtual size_t size() const { return 1; }
2157 * Return true if child of node is binned.
2158 *@return True if child of node is binned.
2160 virtual bool binned() const { return l->binned(); }
2162 virtual std::string str() const
2164 return csprintf("total(%s)", l->str());
2169 //////////////////////////////////////////////////////////////////////
2171 // Visible Statistics Types
2173 //////////////////////////////////////////////////////////////////////
2175 * @defgroup VisibleStats "Statistic Types"
2176 * These are the statistics that are used in the simulator. By default these
2177 * store counters and don't use binning, but are templatized to accept any type
2178 * and any Bin class.
2183 * This is an easy way to assign all your stats to be binned or not
2184 * binned. If the typedef is NoBin, nothing is binned. If it is
2185 * MainBin, then all stats are binned under that Bin.
2188 typedef MainBin DefaultBin;
2190 typedef NoBin DefaultBin;
2194 * This is a simple scalar statistic, like a counter.
2195 * @sa Stat, ScalarBase, StatStor
2197 template <class Bin = DefaultBin>
2199 : public Wrap<Scalar<Bin>,
2200 ScalarBase<StatStor, Bin>,
2204 /** The base implementation. */
2205 typedef ScalarBase<StatStor, Bin> Base;
2213 * Sets the stat equal to the given value. Calls the base implementation
2215 * @param v The new value.
2217 template <typename U>
2218 void operator=(const U &v) { Base::operator=(v); }
2222 : public Wrap<Value,
2227 /** The base implementation. */
2228 typedef ValueBase Base;
2231 Value &scalar(T &value)
2233 Base::scalar(value);
2238 Value &functor(T &func)
2240 Base::functor(func);
2246 * A stat that calculates the per cycle average of a value.
2247 * @sa Stat, ScalarBase, AvgStor
2249 template <class Bin = DefaultBin>
2251 : public Wrap<Average<Bin>,
2252 ScalarBase<AvgStor, Bin>,
2256 /** The base implementation. */
2257 typedef ScalarBase<AvgStor, Bin> Base;
2265 * Sets the stat equal to the given value. Calls the base implementation
2267 * @param v The new value.
2269 template <typename U>
2270 void operator=(const U &v) { Base::operator=(v); }
2274 * A vector of scalar stats.
2275 * @sa Stat, VectorBase, StatStor
2277 template <class Bin = DefaultBin>
2279 : public WrapVec<Vector<Bin>,
2280 VectorBase<StatStor, Bin>,
2284 /** The base implementation. */
2285 typedef ScalarBase<StatStor, Bin> Base;
2288 * Set this vector to have the given size.
2289 * @param size The new size.
2290 * @return A reference to this stat.
2292 Vector &init(size_t size) {
2293 this->bin.init(size, this->params);
2301 * A vector of Average stats.
2302 * @sa Stat, VectorBase, AvgStor
2304 template <class Bin = DefaultBin>
2306 : public WrapVec<AverageVector<Bin>,
2307 VectorBase<AvgStor, Bin>,
2312 * Set this vector to have the given size.
2313 * @param size The new size.
2314 * @return A reference to this stat.
2316 AverageVector &init(size_t size) {
2317 this->bin.init(size, this->params);
2325 * A 2-Dimensional vecto of scalar stats.
2326 * @sa Stat, Vector2dBase, StatStor
2328 template <class Bin = DefaultBin>
2330 : public WrapVec2d<Vector2d<Bin>,
2331 Vector2dBase<StatStor, Bin>,
2335 Vector2d &init(size_t _x, size_t _y) {
2336 this->statData()->x = this->x = _x;
2337 this->statData()->y = this->y = _y;
2338 this->bin.init(this->x * this->y, this->params);
2346 * A simple distribution stat.
2347 * @sa Stat, DistBase, DistStor
2349 template <class Bin = DefaultBin>
2351 : public Wrap<Distribution<Bin>,
2352 DistBase<DistStor, Bin>,
2356 /** Base implementation. */
2357 typedef DistBase<DistStor, Bin> Base;
2358 /** The Parameter type. */
2359 typedef typename DistStor::Params Params;
2363 * Set the parameters of this distribution. @sa DistStor::Params
2364 * @param min The minimum value of the distribution.
2365 * @param max The maximum value of the distribution.
2366 * @param bkt The number of values in each bucket.
2367 * @return A reference to this distribution.
2369 Distribution &init(Counter min, Counter max, Counter bkt) {
2370 this->params.min = min;
2371 this->params.max = max;
2372 this->params.bucket_size = bkt;
2373 this->params.size = (int)rint((max - min) / bkt + 1.0);
2374 this->bin.init(this->params);
2382 * Calculates the mean and variance of all the samples.
2383 * @sa Stat, DistBase, FancyStor
2385 template <class Bin = DefaultBin>
2386 class StandardDeviation
2387 : public Wrap<StandardDeviation<Bin>,
2388 DistBase<FancyStor, Bin>,
2392 /** The base implementation */
2393 typedef DistBase<DistStor, Bin> Base;
2394 /** The parameter type. */
2395 typedef typename DistStor::Params Params;
2399 * Construct and initialize this distribution.
2401 StandardDeviation() {
2402 this->bin.init(this->params);
2408 * Calculates the per cycle mean and variance of the samples.
2409 * @sa Stat, DistBase, AvgFancy
2411 template <class Bin = DefaultBin>
2412 class AverageDeviation
2413 : public Wrap<AverageDeviation<Bin>,
2414 DistBase<AvgFancy, Bin>,
2418 /** The base implementation */
2419 typedef DistBase<DistStor, Bin> Base;
2420 /** The parameter type. */
2421 typedef typename DistStor::Params Params;
2425 * Construct and initialize this distribution.
2429 this->bin.init(this->params);
2435 * A vector of distributions.
2436 * @sa Stat, VectorDistBase, DistStor
2438 template <class Bin = DefaultBin>
2439 class VectorDistribution
2440 : public WrapVec<VectorDistribution<Bin>,
2441 VectorDistBase<DistStor, Bin>,
2445 /** The base implementation */
2446 typedef VectorDistBase<DistStor, Bin> Base;
2447 /** The parameter type. */
2448 typedef typename DistStor::Params Params;
2452 * Initialize storage and parameters for this distribution.
2453 * @param size The size of the vector (the number of distributions).
2454 * @param min The minimum value of the distribution.
2455 * @param max The maximum value of the distribution.
2456 * @param bkt The number of values in each bucket.
2457 * @return A reference to this distribution.
2459 VectorDistribution &init(int size, Counter min, Counter max, Counter bkt) {
2460 this->params.min = min;
2461 this->params.max = max;
2462 this->params.bucket_size = bkt;
2463 this->params.size = (int)rint((max - min) / bkt + 1.0);
2464 this->bin.init(size, this->params);
2472 * This is a vector of StandardDeviation stats.
2473 * @sa Stat, VectorDistBase, FancyStor
2475 template <class Bin = DefaultBin>
2476 class VectorStandardDeviation
2477 : public WrapVec<VectorStandardDeviation<Bin>,
2478 VectorDistBase<FancyStor, Bin>,
2482 /** The base implementation */
2483 typedef VectorDistBase<FancyStor, Bin> Base;
2484 /** The parameter type. */
2485 typedef typename DistStor::Params Params;
2489 * Initialize storage for this distribution.
2490 * @param size The size of the vector.
2491 * @return A reference to this distribution.
2493 VectorStandardDeviation &init(int size) {
2494 this->bin.init(size, this->params);
2502 * This is a vector of AverageDeviation stats.
2503 * @sa Stat, VectorDistBase, AvgFancy
2505 template <class Bin = DefaultBin>
2506 class VectorAverageDeviation
2507 : public WrapVec<VectorAverageDeviation<Bin>,
2508 VectorDistBase<AvgFancy, Bin>,
2512 /** The base implementation */
2513 typedef VectorDistBase<AvgFancy, Bin> Base;
2514 /** The parameter type. */
2515 typedef typename DistStor::Params Params;
2519 * Initialize storage for this distribution.
2520 * @param size The size of the vector.
2521 * @return A reference to this distribution.
2523 VectorAverageDeviation &init(int size) {
2524 this->bin.init(size, this->params);
2532 * A formula for statistics that is calculated when printed. A formula is
2533 * stored as a tree of Nodes that represent the equation to calculate.
2534 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2536 class FormulaBase : public DataAccess
2539 /** The root of the tree which represents the Formula */
2545 * Return the result of the Fomula in a vector. If there were no Vector
2546 * components to the Formula, then the vector is size 1. If there were,
2547 * like x/y with x being a vector of size 3, then the result returned will
2548 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2549 * @return The result vector.
2551 void result(VResult &vec) const;
2554 * Return the total Formula result. If there is a Vector
2555 * component to this Formula, then this is the result of the
2556 * Formula if the formula is applied after summing all the
2557 * components of the Vector. For example, if Formula is x/y where
2558 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2559 * there is no Vector component, total() returns the same value as
2560 * the first entry in the VResult val() returns.
2561 * @return The total of the result vector.
2563 Result total() const;
2566 * Return the number of elements in the tree.
2568 size_t size() const;
2571 * Return true if Formula is binned. i.e. any of its children
2573 * @return True if Formula is binned.
2575 bool binned() const;
2577 bool check() const { return true; }
2580 * Formulas don't need to be reset
2592 void update(StatData *);
2594 std::string str() const;
2597 class FormulaData : public VectorData
2600 virtual std::string str() const = 0;
2601 virtual bool check() const { return true; }
2604 template <class Stat>
2605 class FormulaStatData : public FormulaData
2609 mutable VResult vec;
2610 mutable VCounter cvec;
2613 FormulaStatData(Stat &stat) : s(stat) {}
2615 virtual bool binned() const { return s.binned(); }
2616 virtual bool zero() const { return s.zero(); }
2617 virtual void reset() { s.reset(); }
2619 virtual size_t size() const { return s.size(); }
2620 virtual const VResult &result() const
2625 virtual Result total() const { return s.total(); }
2626 virtual VCounter &value() const { return cvec; }
2627 virtual void visit(Visit &visitor)
2631 visitor.visit(*this);
2633 virtual std::string str() const { return s.str(); }
2638 : public WrapVec<Formula,
2644 * Create and initialize thie formula, and register it with the database.
2649 * Create a formula with the given root node, register it with the
2651 * @param r The root of the expression tree.
2656 * Set an unitialized Formula to the given root.
2657 * @param r The root of the expression tree.
2658 * @return a reference to this formula.
2660 const Formula &operator=(Temp r);
2663 * Add the given tree to the existing one.
2664 * @param r The root of the expression tree.
2665 * @return a reference to this formula.
2667 const Formula &operator+=(Temp r);
2670 class FormulaNode : public Node
2673 const Formula &formula;
2674 mutable VResult vec;
2677 FormulaNode(const Formula &f) : formula(f) {}
2679 virtual size_t size() const { return formula.size(); }
2680 virtual const VResult &result() const { formula.result(vec); return vec; }
2681 virtual Result total() const { return formula.total(); }
2682 virtual bool binned() const { return formula.binned(); }
2684 virtual std::string str() const { return formula.str(); }
2688 * Helper class to construct formula node trees.
2694 * Pointer to a Node object.
2700 * Copy the given pointer to this class.
2701 * @param n A pointer to a Node object to copy.
2703 Temp(NodePtr n) : node(n) { }
2706 * Return the node pointer.
2707 * @return the node pointer.
2709 operator NodePtr&() { return node;}
2713 * Create a new ScalarStatNode.
2714 * @param s The ScalarStat to place in a node.
2716 template <class Bin>
2717 Temp(const Scalar<Bin> &s)
2718 : node(new ScalarStatNode(s.statData())) { }
2721 * Create a new ScalarStatNode.
2722 * @param s The ScalarStat to place in a node.
2724 Temp(const Value &s)
2725 : node(new ScalarStatNode(s.statData())) { }
2728 * Create a new ScalarStatNode.
2729 * @param s The ScalarStat to place in a node.
2731 template <class Bin>
2732 Temp(const Average<Bin> &s)
2733 : node(new ScalarStatNode(s.statData())) { }
2736 * Create a new VectorStatNode.
2737 * @param s The VectorStat to place in a node.
2739 template <class Bin>
2740 Temp(const Vector<Bin> &s)
2741 : node(new VectorStatNode(s.statData())) { }
2746 Temp(const Formula &f)
2747 : node(new FormulaNode(f)) { }
2750 * Create a new ScalarProxyNode.
2751 * @param p The ScalarProxy to place in a node.
2753 template <class Storage, class Bin>
2754 Temp(const ScalarProxy<Storage, Bin> &p)
2755 : node(new ScalarProxyNode<Storage, Bin>(p)) { }
2758 * Create a ConstNode
2759 * @param value The value of the const node.
2761 Temp(signed char value)
2762 : node(new ConstNode<signed char>(value)) {}
2765 * Create a ConstNode
2766 * @param value The value of the const node.
2768 Temp(unsigned char value)
2769 : node(new ConstNode<unsigned char>(value)) {}
2772 * Create a ConstNode
2773 * @param value The value of the const node.
2775 Temp(signed short value)
2776 : node(new ConstNode<signed short>(value)) {}
2779 * Create a ConstNode
2780 * @param value The value of the const node.
2782 Temp(unsigned short value)
2783 : node(new ConstNode<unsigned short>(value)) {}
2786 * Create a ConstNode
2787 * @param value The value of the const node.
2789 Temp(signed int value)
2790 : node(new ConstNode<signed int>(value)) {}
2793 * Create a ConstNode
2794 * @param value The value of the const node.
2796 Temp(unsigned int value)
2797 : node(new ConstNode<unsigned int>(value)) {}
2800 * Create a ConstNode
2801 * @param value The value of the const node.
2803 Temp(signed long value)
2804 : node(new ConstNode<signed long>(value)) {}
2807 * Create a ConstNode
2808 * @param value The value of the const node.
2810 Temp(unsigned long value)
2811 : node(new ConstNode<unsigned long>(value)) {}
2814 * Create a ConstNode
2815 * @param value The value of the const node.
2817 Temp(signed long long value)
2818 : node(new ConstNode<signed long long>(value)) {}
2821 * Create a ConstNode
2822 * @param value The value of the const node.
2824 Temp(unsigned long long value)
2825 : node(new ConstNode<unsigned long long>(value)) {}
2828 * Create a ConstNode
2829 * @param value The value of the const node.
2832 : node(new ConstNode<float>(value)) {}
2835 * Create a ConstNode
2836 * @param value The value of the const node.
2839 : node(new ConstNode<double>(value)) {}
2849 void registerResetCallback(Callback *cb);
2852 operator+(Temp l, Temp r)
2854 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
2858 operator-(Temp l, Temp r)
2860 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
2864 operator*(Temp l, Temp r)
2866 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
2870 operator/(Temp l, Temp r)
2872 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
2878 return NodePtr(new UnaryNode<std::negate<Result> >(l));
2881 template <typename T>
2885 return NodePtr(new ConstNode<T>(val));
2891 return NodePtr(new SumNode<std::plus<Result> >(val));
2894 /* namespace Stats */ }
2896 #endif // __BASE_STATISTICS_HH__