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.
28 * Authors: Nathan Binkert
33 * Declaration of Statistics objects.
39 * Generalized N-dimensinal vector
43 * -- these both can use the same function that prints out a
44 * specific set of stats
45 * VectorStandardDeviation totals
48 #ifndef __BASE_STATISTICS_HH__
49 #define __BASE_STATISTICS_HH__
59 #include "base/cprintf.hh"
60 #include "base/intmath.hh"
61 #include "base/refcnt.hh"
62 #include "base/str.hh"
63 #include "base/stats/bin.hh"
64 #include "base/stats/flags.hh"
65 #include "base/stats/visit.hh"
66 #include "base/stats/types.hh"
67 #include "config/stats_binning.hh"
68 #include "sim/host.hh"
72 /** The current simulated cycle. */
75 /* A namespace for all of the Statistics */
78 /* Contains the statistic implementation details */
79 //////////////////////////////////////////////////////////////////////
81 // Statistics Framework Base classes
83 //////////////////////////////////////////////////////////////////////
86 /** The name of the stat. */
88 /** The description of the stat. */
90 /** The formatting flags. */
92 /** The display precision. */
94 /** A pointer to a prerequisite Stat. */
95 const StatData *prereq;
97 * A unique stat ID for each stat in the simulator.
98 * Can be used externally for lookups as well as for debugging.
106 * @return true if the stat is binned.
108 virtual bool binned() const = 0;
111 * Reset the corresponding stat to the default state.
113 virtual void reset() = 0;
116 * @return true if this stat has a value and satisfies its
117 * requirement as a prereq
119 virtual bool zero() const = 0;
122 * Check that this stat has been set up properly and is ready for
124 * @return true for success
126 virtual bool check() const = 0;
127 bool baseCheck() const;
130 * Visitor entry for outputing statistics data
132 virtual void visit(Visit &visitor) = 0;
135 * Checks if the first stat's name is alphabetically less than the second.
136 * This function breaks names up at periods and considers each subname
138 * @param stat1 The first stat.
139 * @param stat2 The second stat.
140 * @return stat1's name is alphabetically before stat2's
142 static bool less(StatData *stat1, StatData *stat2);
145 class ScalarData : public StatData
148 virtual Counter value() const = 0;
149 virtual Result result() const = 0;
150 virtual Result total() const = 0;
151 virtual void visit(Visit &visitor) { visitor.visit(*this); }
154 template <class Stat>
155 class ScalarStatData : public ScalarData
161 ScalarStatData(Stat &stat) : s(stat) {}
163 virtual bool binned() const { return s.binned(); }
164 virtual bool check() const { return s.check(); }
165 virtual Counter value() const { return s.value(); }
166 virtual Result result() const { return s.result(); }
167 virtual Result total() const { return s.total(); }
168 virtual void reset() { s.reset(); }
169 virtual bool zero() const { return s.zero(); }
172 struct VectorData : public StatData
174 /** Names and descriptions of subfields. */
175 mutable std::vector<std::string> subnames;
176 mutable std::vector<std::string> subdescs;
178 virtual size_t size() const = 0;
179 virtual const VCounter &value() const = 0;
180 virtual const VResult &result() const = 0;
181 virtual Result total() const = 0;
184 if (!subnames.empty()) {
186 if (subnames.size() < s)
189 if (subdescs.size() < s)
195 template <class Stat>
196 class VectorStatData : public VectorData
200 mutable VCounter cvec;
201 mutable VResult rvec;
204 VectorStatData(Stat &stat) : s(stat) {}
206 virtual bool binned() const { return s.binned(); }
207 virtual bool check() const { return s.check(); }
208 virtual bool zero() const { return s.zero(); }
209 virtual void reset() { s.reset(); }
211 virtual size_t size() const { return s.size(); }
212 virtual VCounter &value() const
217 virtual const VResult &result() const
222 virtual Result total() const { return s.total(); }
223 virtual void visit(Visit &visitor)
227 visitor.visit(*this);
249 struct DistData : public StatData
251 /** Local storage for the entry values, used for printing. */
255 template <class Stat>
256 class DistStatData : public DistData
262 DistStatData(Stat &stat) : s(stat) {}
264 virtual bool binned() const { return s.binned(); }
265 virtual bool check() const { return s.check(); }
266 virtual void reset() { s.reset(); }
267 virtual bool zero() const { return s.zero(); }
268 virtual void visit(Visit &visitor)
271 visitor.visit(*this);
275 struct VectorDistData : public StatData
277 std::vector<DistDataData> data;
279 /** Names and descriptions of subfields. */
280 mutable std::vector<std::string> subnames;
281 mutable std::vector<std::string> subdescs;
283 /** Local storage for the entry values, used for printing. */
284 mutable VResult rvec;
286 virtual size_t size() const = 0;
290 if (subnames.size() < s)
293 if (subdescs.size() < s)
298 template <class Stat>
299 class VectorDistStatData : public VectorDistData
303 typedef typename Stat::bin_t bin_t;
306 VectorDistStatData(Stat &stat) : s(stat) {}
308 virtual bool binned() const { return bin_t::binned; }
309 virtual bool check() const { return s.check(); }
310 virtual void reset() { s.reset(); }
311 virtual size_t size() const { return s.size(); }
312 virtual bool zero() const { return s.zero(); }
313 virtual void visit(Visit &visitor)
317 visitor.visit(*this);
321 struct Vector2dData : public StatData
323 /** Names and descriptions of subfields. */
324 std::vector<std::string> subnames;
325 std::vector<std::string> subdescs;
326 std::vector<std::string> y_subnames;
328 /** Local storage for the entry values, used for printing. */
329 mutable VCounter cvec;
335 if (subnames.size() < x)
340 template <class Stat>
341 class Vector2dStatData : public Vector2dData
345 typedef typename Stat::bin_t bin_t;
348 Vector2dStatData(Stat &stat) : s(stat) {}
350 virtual bool binned() const { return bin_t::binned; }
351 virtual bool check() const { return s.check(); }
352 virtual void reset() { s.reset(); }
353 virtual bool zero() const { return s.zero(); }
354 virtual void visit(Visit &visitor)
358 visitor.visit(*this);
366 StatData *find() const;
367 void map(StatData *data);
369 StatData *statData();
370 const StatData *statData() const;
376 template <class Parent, class Child, template <class> class Data>
377 class Wrap : public Child
380 Parent &self() { return *reinterpret_cast<Parent *>(this); }
383 Data<Child> *statData()
385 StatData *__data = DataAccess::statData();
386 Data<Child> *ptr = dynamic_cast<Data<Child> *>(__data);
392 const Data<Child> *statData() const
394 const StatData *__data = DataAccess::statData();
395 const Data<Child> *ptr = dynamic_cast<const Data<Child> *>(__data);
402 * Copy constructor, copies are not allowed.
404 Wrap(const Wrap &stat);
408 void operator=(const Wrap &);
413 map(new Data<Child>(*this));
417 * Set the name and marks this stat to print at the end of simulation.
418 * @param name The new name.
419 * @return A reference to this stat.
421 Parent &name(const std::string &_name)
423 Data<Child> *data = this->statData();
430 * Set the description and marks this stat to print at the end of
432 * @param desc The new description.
433 * @return A reference to this stat.
435 Parent &desc(const std::string &_desc)
437 this->statData()->desc = _desc;
442 * Set the precision and marks this stat to print at the end of simulation.
443 * @param p The new precision
444 * @return A reference to this stat.
446 Parent &precision(int _precision)
448 this->statData()->precision = _precision;
453 * Set the flags and marks this stat to print at the end of simulation.
454 * @param f The new flags.
455 * @return A reference to this stat.
457 Parent &flags(StatFlags _flags)
459 this->statData()->flags |= _flags;
464 * Set the prerequisite stat and marks this stat to print at the end of
466 * @param prereq The prerequisite stat.
467 * @return A reference to this stat.
469 template <class Stat>
470 Parent &prereq(const Stat &prereq)
472 this->statData()->prereq = prereq.statData();
477 template <class Parent, class Child, template <class Child> class Data>
478 class WrapVec : public Wrap<Parent, Child, Data>
481 // The following functions are specific to vectors. If you use them
482 // in a non vector context, you will get a nice compiler error!
485 * Set the subfield name for the given index, and marks this stat to print
486 * at the end of simulation.
487 * @param index The subfield index.
488 * @param name The new name of the subfield.
489 * @return A reference to this stat.
491 Parent &subname(int index, const std::string &name)
493 std::vector<std::string> &subn = this->statData()->subnames;
494 if (subn.size() <= index)
495 subn.resize(index + 1);
501 * Set the subfield description for the given index and marks this stat to
502 * print at the end of simulation.
503 * @param index The subfield index.
504 * @param desc The new description of the subfield
505 * @return A reference to this stat.
507 Parent &subdesc(int index, const std::string &desc)
509 std::vector<std::string> &subd = this->statData()->subdescs;
510 if (subd.size() <= index)
511 subd.resize(index + 1);
519 template <class Parent, class Child, template <class Child> class Data>
520 class WrapVec2d : public WrapVec<Parent, Child, Data>
524 * @warning This makes the assumption that if you're gonna subnames a 2d
525 * vector, you're subnaming across all y
527 Parent &ysubnames(const char **names)
529 Data<Child> *data = this->statData();
530 data->y_subnames.resize(this->y);
531 for (int i = 0; i < this->y; ++i)
532 data->y_subnames[i] = names[i];
535 Parent &ysubname(int index, const std::string subname)
537 Data<Child> *data = this->statData();
538 assert(index < this->y);
539 data->y_subnames.resize(this->y);
540 data->y_subnames[index] = subname.c_str();
545 //////////////////////////////////////////////////////////////////////
549 //////////////////////////////////////////////////////////////////////
552 * Templatized storage and interface for a simple scalar stat.
557 /** The paramaters for this storage type, none for a scalar. */
561 /** The statistic value. */
566 * Builds this storage element and calls the base constructor of the
569 StatStor(const Params &) : data(Counter()) {}
572 * The the stat to the given value.
573 * @param val The new value.
574 * @param p The paramters of this storage type.
576 void set(Counter val, const Params &p) { data = val; }
578 * Increment the stat by the given value.
579 * @param val The new value.
580 * @param p The paramters of this storage type.
582 void inc(Counter val, const Params &p) { data += val; }
584 * Decrement the stat by the given value.
585 * @param val The new value.
586 * @param p The paramters of this storage type.
588 void dec(Counter val, const Params &p) { data -= val; }
590 * Return the value of this stat as its base type.
591 * @param p The params of this storage type.
592 * @return The value of this stat.
594 Counter value(const Params &p) const { return data; }
596 * Return the value of this stat as a result type.
597 * @param p The parameters of this storage type.
598 * @return The value of this stat.
600 Result result(const Params &p) const { return (Result)data; }
602 * Reset stat value to default
604 void reset() { data = Counter(); }
607 * @return true if zero value
609 bool zero() const { return data == Counter(); }
613 * Templatized storage and interface to a per-cycle average stat. This keeps
614 * a current count and updates a total (count * cycles) when this count
615 * changes. This allows the quick calculation of a per cycle count of the item
616 * being watched. This is good for keeping track of residencies in structures
617 * among other things.
618 * @todo add lateny to the stat and fix binning.
623 /** The paramaters for this storage type */
627 * The current count. We stash this here because the current
628 * value is not a binned value.
634 /** The total count for all cycles. */
635 mutable Result total;
636 /** The cycle that current last changed. */
641 * Build and initializes this stat storage.
643 AvgStor(Params &p) : total(0), last(0) { p.current = Counter(); }
646 * Set the current count to the one provided, update the total and last
648 * @param val The new count.
649 * @param p The parameters for this storage.
651 void set(Counter val, Params &p) {
652 total += p.current * (curTick - last);
658 * Increment the current count by the provided value, calls set.
659 * @param val The amount to increment.
660 * @param p The parameters for this storage.
662 void inc(Counter val, Params &p) { set(p.current + val, p); }
665 * Deccrement the current count by the provided value, calls set.
666 * @param val The amount to decrement.
667 * @param p The parameters for this storage.
669 void dec(Counter val, Params &p) { set(p.current - val, p); }
672 * Return the current count.
673 * @param p The parameters for this storage.
674 * @return The current count.
676 Counter value(const Params &p) const { return p.current; }
679 * Return the current average.
680 * @param p The parameters for this storage.
681 * @return The current average.
683 Result result(const Params &p) const
685 total += p.current * (curTick - last);
687 return (Result)(total + p.current) / (Result)(curTick + 1);
691 * Reset stat value to default
700 * @return true if zero value
702 bool zero() const { return total == 0.0; }
706 * Implementation of a scalar stat. The type of stat is determined by the
707 * Storage template. The storage for this stat is held within the Bin class.
708 * This allows for breaking down statistics across multiple bins easily.
710 template <class Storage, class Bin>
711 class ScalarBase : public DataAccess
714 /** Define the params of the storage class. */
715 typedef typename Storage::Params params_t;
716 /** Define the bin type. */
717 typedef typename Bin::template Bin<Storage> bin_t;
720 /** The bin of this stat. */
722 /** The parameters for this stat. */
727 * Retrieve the storage from the bin.
728 * @return The storage object for this stat.
730 Storage *data() { return bin.data(params); }
732 * Retrieve a const pointer to the storage from the bin.
733 * @return A const pointer to the storage object for this stat.
735 const Storage *data() const
737 bin_t *_bin = const_cast<bin_t *>(&bin);
738 params_t *_params = const_cast<params_t *>(¶ms);
739 return _bin->data(*_params);
744 * Return the current value of this stat as its base type.
745 * @return The current value.
747 Counter value() const { return data()->value(params); }
751 * Create and initialize this stat, register it with the database.
759 // Common operators for stats
761 * Increment the stat by 1. This calls the associated storage object inc
764 void operator++() { data()->inc(1, params); }
766 * Decrement the stat by 1. This calls the associated storage object dec
769 void operator--() { data()->dec(1, params); }
771 /** Increment the stat by 1. */
772 void operator++(int) { ++*this; }
773 /** Decrement the stat by 1. */
774 void operator--(int) { --*this; }
777 * Set the data value to the given value. This calls the associated storage
778 * object set function.
779 * @param v The new value.
781 template <typename U>
782 void operator=(const U &v) { data()->set(v, params); }
785 * Increment the stat by the given value. This calls the associated
786 * storage object inc function.
787 * @param v The value to add.
789 template <typename U>
790 void operator+=(const U &v) { data()->inc(v, params); }
793 * Decrement the stat by the given value. This calls the associated
794 * storage object dec function.
795 * @param v The value to substract.
797 template <typename U>
798 void operator-=(const U &v) { data()->dec(v, params); }
801 * Return the number of elements, always 1 for a scalar.
804 size_t size() const { return 1; }
806 * Return true if stat is binned.
807 *@return True is stat is binned.
809 bool binned() const { return bin_t::binned; }
811 bool check() const { return bin.initialized(); }
814 * Reset stat value to default
816 void reset() { bin.reset(); }
818 Counter value() { return data()->value(params); }
820 Result result() { return data()->result(params); }
822 Result total() { return result(); }
824 bool zero() { return result() == 0.0; }
828 class ProxyData : public ScalarData
831 virtual void visit(Visit &visitor) { visitor.visit(*this); }
832 virtual bool binned() const { return false; }
833 virtual std::string str() const { return to_string(value()); }
834 virtual size_t size() const { return 1; }
835 virtual bool zero() const { return value() == 0; }
836 virtual bool check() const { return true; }
837 virtual void reset() { }
841 class ValueProxy : public ProxyData
847 ValueProxy(T &val) : scalar(&val) {}
848 virtual Counter value() const { return *scalar; }
849 virtual Result result() const { return *scalar; }
850 virtual Result total() const { return *scalar; }
854 class FunctorProxy : public ProxyData
860 FunctorProxy(T &func) : functor(&func) {}
861 virtual Counter value() const { return (*functor)(); }
862 virtual Result result() const { return (*functor)(); }
863 virtual Result total() const { return (*functor)(); }
866 class ValueBase : public DataAccess
872 ValueBase() : proxy(NULL) { }
873 ~ValueBase() { if (proxy) delete proxy; }
876 void scalar(T &value)
878 proxy = new ValueProxy<T>(value);
883 void functor(T &func)
885 proxy = new FunctorProxy<T>(func);
889 Counter value() { return proxy->value(); }
890 Result result() const { return proxy->result(); }
891 Result total() const { return proxy->total(); };
892 size_t size() const { return proxy->size(); }
894 bool binned() const { return proxy->binned(); }
895 std::string str() const { return proxy->str(); }
896 bool zero() const { return proxy->zero(); }
897 bool check() const { return proxy != NULL; }
901 //////////////////////////////////////////////////////////////////////
905 //////////////////////////////////////////////////////////////////////
906 template <class Storage, class Bin>
910 * Implementation of a vector of stats. The type of stat is determined by the
911 * Storage class. @sa ScalarBase
913 template <class Storage, class Bin>
914 class VectorBase : public DataAccess
917 /** Define the params of the storage class. */
918 typedef typename Storage::Params params_t;
919 /** Define the bin type. */
920 typedef typename Bin::template VectorBin<Storage> bin_t;
923 /** The bin of this stat. */
925 /** The parameters for this stat. */
930 * Retrieve the storage from the bin for the given index.
931 * @param index The vector index to access.
932 * @return The storage object at the given index.
934 Storage *data(int index) { return bin.data(index, params); }
936 * Retrieve a const pointer to the storage from the bin
937 * for the given index.
938 * @param index The vector index to access.
939 * @return A const pointer to the storage object at the given index.
941 const Storage *data(int index) const
943 bin_t *_bin = const_cast<bin_t *>(&bin);
944 params_t *_params = const_cast<params_t *>(¶ms);
945 return _bin->data(index, *_params);
949 void value(VCounter &vec) const
952 for (int i = 0; i < size(); ++i)
953 vec[i] = data(i)->value(params);
957 * Copy the values to a local vector and return a reference to it.
958 * @return A reference to a vector of the stat values.
960 void result(VResult &vec) const
963 for (int i = 0; i < size(); ++i)
964 vec[i] = data(i)->result(params);
968 * @return True is stat is binned.
970 bool binned() const { return bin_t::binned; }
973 * Return a total of all entries in this vector.
974 * @return The total of all vector entries.
976 Result total() const {
978 for (int i = 0; i < size(); ++i)
979 total += data(i)->result(params);
984 * @return the number of elements in this vector.
986 size_t size() const { return bin.size(); }
990 for (int i = 0; i < size(); ++i)
996 bool check() const { return bin.initialized(); }
997 void reset() { bin.reset(); }
1002 /** Friend this class with the associated scalar proxy. */
1003 friend class ScalarProxy<Storage, Bin>;
1006 * Return a reference (ScalarProxy) to the stat at the given index.
1007 * @param index The vector index to access.
1008 * @return A reference of the stat.
1010 ScalarProxy<Storage, Bin> operator[](int index);
1012 void update(StatData *data) {}
1015 const StatData * getStatData(const void *stat);
1018 * A proxy class to access the stat at a given index in a VectorBase stat.
1019 * Behaves like a ScalarBase.
1021 template <class Storage, class Bin>
1025 /** Define the params of the storage class. */
1026 typedef typename Storage::Params params_t;
1027 /** Define the bin type. */
1028 typedef typename Bin::template VectorBin<Storage> bin_t;
1031 /** Pointer to the bin in the parent VectorBase. */
1033 /** Pointer to the params in the parent VectorBase. */
1035 /** The index to access in the parent VectorBase. */
1037 /** Keep a pointer to the original stat so was can get data */
1042 * Retrieve the storage from the bin.
1043 * @return The storage from the bin for this stat.
1045 Storage *data() { return bin->data(index, *params); }
1047 * Retrieve a const pointer to the storage from the bin.
1048 * @return A const pointer to the storage for this stat.
1050 const Storage *data() const
1052 bin_t *_bin = const_cast<bin_t *>(bin);
1053 params_t *_params = const_cast<params_t *>(params);
1054 return _bin->data(index, *_params);
1059 * Return the current value of this stat as its base type.
1060 * @return The current value.
1062 Counter value() const { return data()->value(*params); }
1065 * Return the current value of this statas a result type.
1066 * @return The current value.
1068 Result result() const { return data()->result(*params); }
1072 * Create and initialize this proxy, do not register it with the database.
1073 * @param b The bin to use.
1074 * @param p The params to use.
1075 * @param i The index to access.
1077 ScalarProxy(bin_t &b, params_t &p, int i, void *s)
1078 : bin(&b), params(&p), index(i), stat(s) {}
1080 * Create a copy of the provided ScalarProxy.
1081 * @param sp The proxy to copy.
1083 ScalarProxy(const ScalarProxy &sp)
1084 : bin(sp.bin), params(sp.params), index(sp.index), stat(sp.stat) {}
1086 * Set this proxy equal to the provided one.
1087 * @param sp The proxy to copy.
1088 * @return A reference to this proxy.
1090 const ScalarProxy &operator=(const ScalarProxy &sp) {
1099 // Common operators for stats
1101 * Increment the stat by 1. This calls the associated storage object inc
1104 void operator++() { data()->inc(1, *params); }
1106 * Decrement the stat by 1. This calls the associated storage object dec
1109 void operator--() { data()->dec(1, *params); }
1111 /** Increment the stat by 1. */
1112 void operator++(int) { ++*this; }
1113 /** Decrement the stat by 1. */
1114 void operator--(int) { --*this; }
1117 * Set the data value to the given value. This calls the associated storage
1118 * object set function.
1119 * @param v The new value.
1121 template <typename U>
1122 void operator=(const U &v) { data()->set(v, *params); }
1125 * Increment the stat by the given value. This calls the associated
1126 * storage object inc function.
1127 * @param v The value to add.
1129 template <typename U>
1130 void operator+=(const U &v) { data()->inc(v, *params); }
1133 * Decrement the stat by the given value. This calls the associated
1134 * storage object dec function.
1135 * @param v The value to substract.
1137 template <typename U>
1138 void operator-=(const U &v) { data()->dec(v, *params); }
1141 * Return the number of elements, always 1 for a scalar.
1144 size_t size() const { return 1; }
1147 * Return true if stat is binned.
1148 *@return false since Proxies aren't printed/binned
1150 bool binned() const { return false; }
1153 * This stat has no state. Nothing to reset
1158 const StatData *statData() const { return getStatData(stat); }
1159 std::string str() const
1161 return csprintf("%s[%d]", this->statData()->name, index);
1166 template <class Storage, class Bin>
1167 inline ScalarProxy<Storage, Bin>
1168 VectorBase<Storage, Bin>::operator[](int index)
1170 assert (index >= 0 && index < size());
1171 return ScalarProxy<Storage, Bin>(bin, params, index, this);
1174 template <class Storage, class Bin>
1177 template <class Storage, class Bin>
1178 class Vector2dBase : public DataAccess
1181 typedef typename Storage::Params params_t;
1182 typedef typename Bin::template VectorBin<Storage> bin_t;
1191 Storage *data(int index) { return bin.data(index, params); }
1192 const Storage *data(int index) const
1194 bin_t *_bin = const_cast<bin_t *>(&bin);
1195 params_t *_params = const_cast<params_t *>(¶ms);
1196 return _bin->data(index, *_params);
1202 void update(Vector2dData *data)
1204 int size = this->size();
1205 data->cvec.resize(size);
1206 for (int i = 0; i < size; ++i)
1207 data->cvec[i] = this->data(i)->value(params);
1210 std::string ysubname(int i) const { return (*this->y_subnames)[i]; }
1212 friend class VectorProxy<Storage, Bin>;
1213 VectorProxy<Storage, Bin> operator[](int index);
1215 size_t size() const { return bin.size(); }
1216 bool zero() const { return data(0)->value(params) == 0.0; }
1219 * Reset stat value to default
1221 void reset() { bin.reset(); }
1223 bool check() { return bin.initialized(); }
1226 template <class Storage, class Bin>
1230 typedef typename Storage::Params params_t;
1231 typedef typename Bin::template VectorBin<Storage> bin_t;
1241 mutable VResult *vec;
1243 Storage *data(int index) {
1244 assert(index < len);
1245 return bin->data(offset + index, *params);
1248 const Storage *data(int index) const {
1249 bin_t *_bin = const_cast<bin_t *>(bin);
1250 params_t *_params = const_cast<params_t *>(params);
1251 return _bin->data(offset + index, *_params);
1255 const VResult &result() const {
1257 vec->resize(size());
1259 vec = new VResult(size());
1261 for (int i = 0; i < size(); ++i)
1262 (*vec)[i] = data(i)->result(*params);
1267 Result total() const {
1269 for (int i = 0; i < size(); ++i)
1270 total += data(i)->result(*params);
1275 VectorProxy(bin_t &b, params_t &p, int o, int l, void *s)
1276 : bin(&b), params(&p), offset(o), len(l), stat(s), vec(NULL)
1280 VectorProxy(const VectorProxy &sp)
1281 : bin(sp.bin), params(sp.params), offset(sp.offset), len(sp.len),
1282 stat(sp.stat), vec(NULL)
1292 const VectorProxy &operator=(const VectorProxy &sp)
1305 ScalarProxy<Storage, Bin> operator[](int index)
1307 assert (index >= 0 && index < size());
1308 return ScalarProxy<Storage, Bin>(*bin, *params, offset + index, stat);
1311 size_t size() const { return len; }
1314 * Return true if stat is binned.
1315 *@return false since Proxies aren't printed/binned
1317 bool binned() const { return false; }
1320 * This stat has no state. Nothing to reset.
1325 template <class Storage, class Bin>
1326 inline VectorProxy<Storage, Bin>
1327 Vector2dBase<Storage, Bin>::operator[](int index)
1329 int offset = index * y;
1330 assert (index >= 0 && offset < size());
1331 return VectorProxy<Storage, Bin>(bin, params, offset, y, this);
1334 //////////////////////////////////////////////////////////////////////
1336 // Non formula statistics
1338 //////////////////////////////////////////////////////////////////////
1341 * Templatized storage and interface for a distrbution stat.
1346 /** The parameters for a distribution stat. */
1349 /** The minimum value to track. */
1351 /** The maximum value to track. */
1353 /** The number of entries in each bucket. */
1354 Counter bucket_size;
1355 /** The number of buckets. Equal to (max-min)/bucket_size. */
1358 enum { fancy = false };
1361 /** The smallest value sampled. */
1363 /** The largest value sampled. */
1365 /** The number of values sampled less than min. */
1367 /** The number of values sampled more than max. */
1369 /** The current sum. */
1371 /** The sum of squares. */
1373 /** The number of samples. */
1375 /** Counter for each bucket. */
1380 * Construct this storage with the supplied params.
1381 * @param params The parameters.
1383 DistStor(const Params ¶ms)
1384 : min_val(INT_MAX), max_val(INT_MIN), underflow(Counter()),
1385 overflow(Counter()), sum(Counter()), squares(Counter()),
1386 samples(Counter()), cvec(params.size)
1392 * Add a value to the distribution for the given number of times.
1393 * @param val The value to add.
1394 * @param number The number of times to add the value.
1395 * @param params The paramters of the distribution.
1397 void sample(Counter val, int number, const Params ¶ms)
1399 if (val < params.min)
1400 underflow += number;
1401 else if (val > params.max)
1404 int index = (int)floor((val - params.min) / params.bucket_size);
1405 assert(index < size(params));
1406 cvec[index] += number;
1415 Counter sample = val * number;
1417 squares += sample * sample;
1422 * Return the number of buckets in this distribution.
1423 * @return the number of buckets.
1424 * @todo Is it faster to return the size from the parameters?
1426 size_t size(const Params &) const { return cvec.size(); }
1429 * Returns true if any calls to sample have been made.
1430 * @param params The paramters of the distribution.
1431 * @return True if any values have been sampled.
1433 bool zero(const Params ¶ms) const
1435 return samples == Counter();
1438 void update(DistDataData *data, const Params ¶ms)
1440 data->min = params.min;
1441 data->max = params.max;
1442 data->bucket_size = params.bucket_size;
1443 data->size = params.size;
1445 data->min_val = (min_val == INT_MAX) ? 0 : min_val;
1446 data->max_val = (max_val == INT_MIN) ? 0 : max_val;
1447 data->underflow = underflow;
1448 data->overflow = overflow;
1449 data->cvec.resize(params.size);
1450 for (int i = 0; i < params.size; ++i)
1451 data->cvec[i] = cvec[i];
1454 data->squares = squares;
1455 data->samples = samples;
1459 * Reset stat value to default
1468 int size = cvec.size();
1469 for (int i = 0; i < size; ++i)
1470 cvec[i] = Counter();
1473 squares = Counter();
1474 samples = Counter();
1479 * Templatized storage and interface for a distribution that calculates mean
1486 * No paramters for this storage.
1489 enum { fancy = true };
1492 /** The current sum. */
1494 /** The sum of squares. */
1496 /** The number of samples. */
1501 * Create and initialize this storage.
1503 FancyStor(const Params &)
1504 : sum(Counter()), squares(Counter()), samples(Counter())
1508 * Add a value the given number of times to this running average.
1509 * Update the running sum and sum of squares, increment the number of
1510 * values seen by the given number.
1511 * @param val The value to add.
1512 * @param number The number of times to add the value.
1513 * @param p The parameters of this stat.
1515 void sample(Counter val, int number, const Params &p)
1517 Counter value = val * number;
1519 squares += value * value;
1523 void update(DistDataData *data, const Params ¶ms)
1526 data->squares = squares;
1527 data->samples = samples;
1531 * Return the number of entries in this stat, 1
1534 size_t size(const Params &) const { return 1; }
1537 * Return true if no samples have been added.
1538 * @return True if no samples have been added.
1540 bool zero(const Params &) const { return samples == Counter(); }
1543 * Reset stat value to default
1548 squares = Counter();
1549 samples = Counter();
1554 * Templatized storage for distribution that calculates per cycle mean and
1560 /** No parameters for this storage. */
1562 enum { fancy = true };
1565 /** Current total. */
1567 /** Current sum of squares. */
1572 * Create and initialize this storage.
1574 AvgFancy(const Params &) : sum(Counter()), squares(Counter()) {}
1577 * Add a value to the distribution for the given number of times.
1578 * Update the running sum and sum of squares.
1579 * @param val The value to add.
1580 * @param number The number of times to add the value.
1581 * @param p The paramters of the distribution.
1583 void sample(Counter val, int number, const Params &p)
1585 Counter value = val * number;
1587 squares += value * value;
1590 void update(DistDataData *data, const Params ¶ms)
1593 data->squares = squares;
1594 data->samples = curTick;
1598 * Return the number of entries, in this case 1.
1601 size_t size(const Params ¶ms) const { return 1; }
1603 * Return true if no samples have been added.
1604 * @return True if the sum is zero.
1606 bool zero(const Params ¶ms) const { return sum == Counter(); }
1608 * Reset stat value to default
1613 squares = Counter();
1618 * Implementation of a distribution stat. The type of distribution is
1619 * determined by the Storage template. @sa ScalarBase
1621 template <class Storage, class Bin>
1622 class DistBase : public DataAccess
1625 /** Define the params of the storage class. */
1626 typedef typename Storage::Params params_t;
1627 /** Define the bin type. */
1628 typedef typename Bin::template Bin<Storage> bin_t;
1631 /** The bin of this stat. */
1633 /** The parameters for this stat. */
1638 * Retrieve the storage from the bin.
1639 * @return The storage object for this stat.
1641 Storage *data() { return bin.data(params); }
1643 * Retrieve a const pointer to the storage from the bin.
1644 * @return A const pointer to the storage object for this stat.
1646 const Storage *data() const
1648 bin_t *_bin = const_cast<bin_t *>(&bin);
1649 params_t *_params = const_cast<params_t *>(¶ms);
1650 return _bin->data(*_params);
1657 * Add a value to the distribtion n times. Calls sample on the storage
1659 * @param v The value to add.
1660 * @param n The number of times to add it, defaults to 1.
1662 template <typename U>
1663 void sample(const U &v, int n = 1) { data()->sample(v, n, params); }
1666 * Return the number of entries in this stat.
1667 * @return The number of entries.
1669 size_t size() const { return data()->size(params); }
1671 * Return true if no samples have been added.
1672 * @return True if there haven't been any samples.
1674 bool zero() const { return data()->zero(params); }
1676 void update(DistData *base)
1678 base->data.fancy = Storage::fancy;
1679 data()->update(&(base->data), params);
1682 * @return True is stat is binned.
1684 bool binned() const { return bin_t::binned; }
1686 * Reset stat value to default
1693 bool check() { return bin.initialized(); }
1696 template <class Storage, class Bin>
1699 template <class Storage, class Bin>
1700 class VectorDistBase : public DataAccess
1703 typedef typename Storage::Params params_t;
1704 typedef typename Bin::template VectorBin<Storage> bin_t;
1711 Storage *data(int index) { return bin.data(index, params); }
1712 const Storage *data(int index) const
1714 bin_t *_bin = const_cast<bin_t *>(&bin);
1715 params_t *_params = const_cast<params_t *>(¶ms);
1716 return _bin->data(index, *_params);
1722 friend class DistProxy<Storage, Bin>;
1723 DistProxy<Storage, Bin> operator[](int index);
1724 const DistProxy<Storage, Bin> operator[](int index) const;
1726 size_t size() const { return bin.size(); }
1727 bool zero() const { return false; }
1729 * Return true if stat is binned.
1730 *@return True is stat is binned.
1732 bool binned() const { return bin_t::binned; }
1734 * Reset stat value to default
1736 void reset() { bin.reset(); }
1738 bool check() { return bin.initialized(); }
1739 void update(VectorDistData *base)
1741 int size = this->size();
1742 base->data.resize(size);
1743 for (int i = 0; i < size; ++i) {
1744 base->data[i].fancy = Storage::fancy;
1745 data(i)->update(&(base->data[i]), params);
1750 template <class Storage, class Bin>
1754 typedef typename Storage::Params params_t;
1755 typedef typename Bin::template Bin<Storage> bin_t;
1756 typedef VectorDistBase<Storage, Bin> base_t;
1761 const base_t *cstat;
1766 Storage *data() { return stat->data(index); }
1767 const Storage *data() const { return cstat->data(index); }
1770 DistProxy(const VectorDistBase<Storage, Bin> &s, int i)
1771 : cstat(&s), index(i) {}
1772 DistProxy(const DistProxy &sp)
1773 : cstat(sp.cstat), index(sp.index) {}
1774 const DistProxy &operator=(const DistProxy &sp) {
1775 cstat = sp.cstat; index = sp.index; return *this;
1779 template <typename U>
1780 void sample(const U &v, int n = 1) { data()->sample(v, n, cstat->params); }
1782 size_t size() const { return 1; }
1783 bool zero() const { return data()->zero(cstat->params); }
1785 * Return true if stat is binned.
1786 *@return false since Proxies are not binned/printed.
1788 bool binned() const { return false; }
1790 * Proxy has no state. Nothing to reset.
1795 template <class Storage, class Bin>
1796 inline DistProxy<Storage, Bin>
1797 VectorDistBase<Storage, Bin>::operator[](int index)
1799 assert (index >= 0 && index < size());
1800 return DistProxy<Storage, Bin>(*this, index);
1803 template <class Storage, class Bin>
1804 inline const DistProxy<Storage, Bin>
1805 VectorDistBase<Storage, Bin>::operator[](int index) const
1807 assert (index >= 0 && index < size());
1808 return DistProxy<Storage, Bin>(*this, index);
1812 template <class Storage, class Bin>
1814 VectorDistBase<Storage, Bin>::total(int index) const
1817 for (int i=0; i < x_size(); ++i) {
1818 total += data(i)->result(*params);
1823 //////////////////////////////////////////////////////////////////////
1827 //////////////////////////////////////////////////////////////////////
1830 * Base class for formula statistic node. These nodes are used to build a tree
1831 * that represents the formula.
1833 class Node : public RefCounted
1837 * Return the number of nodes in the subtree starting at this node.
1838 * @return the number of nodes in this subtree.
1840 virtual size_t size() const = 0;
1842 * Return the result vector of this subtree.
1843 * @return The result vector of this subtree.
1845 virtual const VResult &result() const = 0;
1847 * Return the total of the result vector.
1848 * @return The total of the result vector.
1850 virtual Result total() const = 0;
1852 * Return true if stat is binned.
1853 *@return True is stat is binned.
1855 virtual bool binned() const = 0;
1860 virtual std::string str() const = 0;
1863 /** Reference counting pointer to a function Node. */
1864 typedef RefCountingPtr<Node> NodePtr;
1866 class ScalarStatNode : public Node
1869 const ScalarData *data;
1870 mutable VResult vresult;
1873 ScalarStatNode(const ScalarData *d) : data(d), vresult(1) {}
1874 virtual const VResult &result() const
1876 vresult[0] = data->result();
1879 virtual Result total() const { return data->result(); };
1881 virtual size_t size() const { return 1; }
1883 * Return true if stat is binned.
1884 *@return True is stat is binned.
1886 virtual bool binned() const { return data->binned(); }
1891 virtual std::string str() const { return data->name; }
1894 template <class Storage, class Bin>
1895 class ScalarProxyNode : public Node
1898 const ScalarProxy<Storage, Bin> proxy;
1899 mutable VResult vresult;
1902 ScalarProxyNode(const ScalarProxy<Storage, Bin> &p)
1903 : proxy(p), vresult(1) { }
1904 virtual const VResult &result() const
1906 vresult[0] = proxy.result();
1909 virtual Result total() const { return proxy.result(); };
1911 virtual size_t size() const { return 1; }
1913 * Return true if stat is binned.
1914 *@return True is stat is binned.
1916 virtual bool binned() const { return proxy.binned(); }
1921 virtual std::string str() const { return proxy.str(); }
1924 class VectorStatNode : public Node
1927 const VectorData *data;
1930 VectorStatNode(const VectorData *d) : data(d) { }
1931 virtual const VResult &result() const { return data->result(); }
1932 virtual Result total() const { return data->total(); };
1934 virtual size_t size() const { return data->size(); }
1936 * Return true if stat is binned.
1937 *@return True is stat is binned.
1939 virtual bool binned() const { return data->binned(); }
1941 virtual std::string str() const { return data->name; }
1945 class ConstNode : public Node
1951 ConstNode(T s) : vresult(1, (Result)s) {}
1952 const VResult &result() const { return vresult; }
1953 virtual Result total() const { return vresult[0]; };
1954 virtual size_t size() const { return 1; }
1957 * Return true if stat is binned.
1958 *@return False since constants aren't binned.
1960 virtual bool binned() const { return false; }
1962 virtual std::string str() const { return to_string(vresult[0]); }
1969 struct OpString<std::plus<Result> >
1971 static std::string str() { return "+"; }
1975 struct OpString<std::minus<Result> >
1977 static std::string str() { return "-"; }
1981 struct OpString<std::multiplies<Result> >
1983 static std::string str() { return "*"; }
1987 struct OpString<std::divides<Result> >
1989 static std::string str() { return "/"; }
1993 struct OpString<std::modulus<Result> >
1995 static std::string str() { return "%"; }
1999 struct OpString<std::negate<Result> >
2001 static std::string str() { return "-"; }
2005 class UnaryNode : public Node
2009 mutable VResult vresult;
2012 UnaryNode(NodePtr &p) : l(p) {}
2014 const VResult &result() const
2016 const VResult &lvec = l->result();
2017 int size = lvec.size();
2021 vresult.resize(size);
2023 for (int i = 0; i < size; ++i)
2024 vresult[i] = op(lvec[i]);
2029 Result total() const {
2031 return op(l->total());
2034 virtual size_t size() const { return l->size(); }
2036 * Return true if child of node is binned.
2037 *@return True if child of node is binned.
2039 virtual bool binned() const { return l->binned(); }
2041 virtual std::string str() const
2043 return OpString<Op>::str() + l->str();
2048 class BinaryNode : public Node
2053 mutable VResult vresult;
2056 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2058 const VResult &result() const
2061 const VResult &lvec = l->result();
2062 const VResult &rvec = r->result();
2064 assert(lvec.size() > 0 && rvec.size() > 0);
2066 if (lvec.size() == 1 && rvec.size() == 1) {
2068 vresult[0] = op(lvec[0], rvec[0]);
2069 } else if (lvec.size() == 1) {
2070 int size = rvec.size();
2071 vresult.resize(size);
2072 for (int i = 0; i < size; ++i)
2073 vresult[i] = op(lvec[0], rvec[i]);
2074 } else if (rvec.size() == 1) {
2075 int size = lvec.size();
2076 vresult.resize(size);
2077 for (int i = 0; i < size; ++i)
2078 vresult[i] = op(lvec[i], rvec[0]);
2079 } else if (rvec.size() == lvec.size()) {
2080 int size = rvec.size();
2081 vresult.resize(size);
2082 for (int i = 0; i < size; ++i)
2083 vresult[i] = op(lvec[i], rvec[i]);
2089 Result total() const {
2091 return op(l->total(), r->total());
2094 virtual size_t size() const {
2102 assert(ls == rs && "Node vector sizes are not equal");
2107 * Return true if any children of node are binned
2108 *@return True if either child of node is binned.
2110 virtual bool binned() const { return (l->binned() || r->binned()); }
2112 virtual std::string str() const
2114 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2119 class SumNode : public Node
2123 mutable VResult vresult;
2126 SumNode(NodePtr &p) : l(p), vresult(1) {}
2128 const VResult &result() const
2130 const VResult &lvec = l->result();
2131 int size = lvec.size();
2137 for (int i = 0; i < size; ++i)
2138 vresult[0] = op(vresult[0], lvec[i]);
2143 Result total() const
2145 const VResult &lvec = l->result();
2146 int size = lvec.size();
2149 Result vresult = 0.0;
2152 for (int i = 0; i < size; ++i)
2153 vresult = op(vresult, lvec[i]);
2158 virtual size_t size() const { return 1; }
2160 * Return true if child of node is binned.
2161 *@return True if child of node is binned.
2163 virtual bool binned() const { return l->binned(); }
2165 virtual std::string str() const
2167 return csprintf("total(%s)", l->str());
2172 //////////////////////////////////////////////////////////////////////
2174 // Visible Statistics Types
2176 //////////////////////////////////////////////////////////////////////
2178 * @defgroup VisibleStats "Statistic Types"
2179 * These are the statistics that are used in the simulator. By default these
2180 * store counters and don't use binning, but are templatized to accept any type
2181 * and any Bin class.
2186 * This is an easy way to assign all your stats to be binned or not
2187 * binned. If the typedef is NoBin, nothing is binned. If it is
2188 * MainBin, then all stats are binned under that Bin.
2191 typedef MainBin DefaultBin;
2193 typedef NoBin DefaultBin;
2197 * This is a simple scalar statistic, like a counter.
2198 * @sa Stat, ScalarBase, StatStor
2200 template <class Bin = DefaultBin>
2202 : public Wrap<Scalar<Bin>,
2203 ScalarBase<StatStor, Bin>,
2207 /** The base implementation. */
2208 typedef ScalarBase<StatStor, Bin> Base;
2216 * Sets the stat equal to the given value. Calls the base implementation
2218 * @param v The new value.
2220 template <typename U>
2221 void operator=(const U &v) { Base::operator=(v); }
2225 : public Wrap<Value,
2230 /** The base implementation. */
2231 typedef ValueBase Base;
2234 Value &scalar(T &value)
2236 Base::scalar(value);
2241 Value &functor(T &func)
2243 Base::functor(func);
2249 * A stat that calculates the per cycle average of a value.
2250 * @sa Stat, ScalarBase, AvgStor
2252 template <class Bin = DefaultBin>
2254 : public Wrap<Average<Bin>,
2255 ScalarBase<AvgStor, Bin>,
2259 /** The base implementation. */
2260 typedef ScalarBase<AvgStor, Bin> Base;
2268 * Sets the stat equal to the given value. Calls the base implementation
2270 * @param v The new value.
2272 template <typename U>
2273 void operator=(const U &v) { Base::operator=(v); }
2277 * A vector of scalar stats.
2278 * @sa Stat, VectorBase, StatStor
2280 template <class Bin = DefaultBin>
2282 : public WrapVec<Vector<Bin>,
2283 VectorBase<StatStor, Bin>,
2287 /** The base implementation. */
2288 typedef ScalarBase<StatStor, Bin> Base;
2291 * Set this vector to have the given size.
2292 * @param size The new size.
2293 * @return A reference to this stat.
2295 Vector &init(size_t size) {
2296 this->bin.init(size, this->params);
2304 * A vector of Average stats.
2305 * @sa Stat, VectorBase, AvgStor
2307 template <class Bin = DefaultBin>
2309 : public WrapVec<AverageVector<Bin>,
2310 VectorBase<AvgStor, Bin>,
2315 * Set this vector to have the given size.
2316 * @param size The new size.
2317 * @return A reference to this stat.
2319 AverageVector &init(size_t size) {
2320 this->bin.init(size, this->params);
2328 * A 2-Dimensional vecto of scalar stats.
2329 * @sa Stat, Vector2dBase, StatStor
2331 template <class Bin = DefaultBin>
2333 : public WrapVec2d<Vector2d<Bin>,
2334 Vector2dBase<StatStor, Bin>,
2338 Vector2d &init(size_t _x, size_t _y) {
2339 this->statData()->x = this->x = _x;
2340 this->statData()->y = this->y = _y;
2341 this->bin.init(this->x * this->y, this->params);
2349 * A simple distribution stat.
2350 * @sa Stat, DistBase, DistStor
2352 template <class Bin = DefaultBin>
2354 : public Wrap<Distribution<Bin>,
2355 DistBase<DistStor, Bin>,
2359 /** Base implementation. */
2360 typedef DistBase<DistStor, Bin> Base;
2361 /** The Parameter type. */
2362 typedef typename DistStor::Params Params;
2366 * Set the parameters of this distribution. @sa DistStor::Params
2367 * @param min The minimum value of the distribution.
2368 * @param max The maximum value of the distribution.
2369 * @param bkt The number of values in each bucket.
2370 * @return A reference to this distribution.
2372 Distribution &init(Counter min, Counter max, Counter bkt) {
2373 this->params.min = min;
2374 this->params.max = max;
2375 this->params.bucket_size = bkt;
2376 this->params.size = (int)rint((max - min) / bkt + 1.0);
2377 this->bin.init(this->params);
2385 * Calculates the mean and variance of all the samples.
2386 * @sa Stat, DistBase, FancyStor
2388 template <class Bin = DefaultBin>
2389 class StandardDeviation
2390 : public Wrap<StandardDeviation<Bin>,
2391 DistBase<FancyStor, Bin>,
2395 /** The base implementation */
2396 typedef DistBase<DistStor, Bin> Base;
2397 /** The parameter type. */
2398 typedef typename DistStor::Params Params;
2402 * Construct and initialize this distribution.
2404 StandardDeviation() {
2405 this->bin.init(this->params);
2411 * Calculates the per cycle mean and variance of the samples.
2412 * @sa Stat, DistBase, AvgFancy
2414 template <class Bin = DefaultBin>
2415 class AverageDeviation
2416 : public Wrap<AverageDeviation<Bin>,
2417 DistBase<AvgFancy, Bin>,
2421 /** The base implementation */
2422 typedef DistBase<DistStor, Bin> Base;
2423 /** The parameter type. */
2424 typedef typename DistStor::Params Params;
2428 * Construct and initialize this distribution.
2432 this->bin.init(this->params);
2438 * A vector of distributions.
2439 * @sa Stat, VectorDistBase, DistStor
2441 template <class Bin = DefaultBin>
2442 class VectorDistribution
2443 : public WrapVec<VectorDistribution<Bin>,
2444 VectorDistBase<DistStor, Bin>,
2448 /** The base implementation */
2449 typedef VectorDistBase<DistStor, Bin> Base;
2450 /** The parameter type. */
2451 typedef typename DistStor::Params Params;
2455 * Initialize storage and parameters for this distribution.
2456 * @param size The size of the vector (the number of distributions).
2457 * @param min The minimum value of the distribution.
2458 * @param max The maximum value of the distribution.
2459 * @param bkt The number of values in each bucket.
2460 * @return A reference to this distribution.
2462 VectorDistribution &init(int size, Counter min, Counter max, Counter bkt) {
2463 this->params.min = min;
2464 this->params.max = max;
2465 this->params.bucket_size = bkt;
2466 this->params.size = (int)rint((max - min) / bkt + 1.0);
2467 this->bin.init(size, this->params);
2475 * This is a vector of StandardDeviation stats.
2476 * @sa Stat, VectorDistBase, FancyStor
2478 template <class Bin = DefaultBin>
2479 class VectorStandardDeviation
2480 : public WrapVec<VectorStandardDeviation<Bin>,
2481 VectorDistBase<FancyStor, Bin>,
2485 /** The base implementation */
2486 typedef VectorDistBase<FancyStor, Bin> Base;
2487 /** The parameter type. */
2488 typedef typename DistStor::Params Params;
2492 * Initialize storage for this distribution.
2493 * @param size The size of the vector.
2494 * @return A reference to this distribution.
2496 VectorStandardDeviation &init(int size) {
2497 this->bin.init(size, this->params);
2505 * This is a vector of AverageDeviation stats.
2506 * @sa Stat, VectorDistBase, AvgFancy
2508 template <class Bin = DefaultBin>
2509 class VectorAverageDeviation
2510 : public WrapVec<VectorAverageDeviation<Bin>,
2511 VectorDistBase<AvgFancy, Bin>,
2515 /** The base implementation */
2516 typedef VectorDistBase<AvgFancy, Bin> Base;
2517 /** The parameter type. */
2518 typedef typename DistStor::Params Params;
2522 * Initialize storage for this distribution.
2523 * @param size The size of the vector.
2524 * @return A reference to this distribution.
2526 VectorAverageDeviation &init(int size) {
2527 this->bin.init(size, this->params);
2535 * A formula for statistics that is calculated when printed. A formula is
2536 * stored as a tree of Nodes that represent the equation to calculate.
2537 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2539 class FormulaBase : public DataAccess
2542 /** The root of the tree which represents the Formula */
2548 * Return the result of the Fomula in a vector. If there were no Vector
2549 * components to the Formula, then the vector is size 1. If there were,
2550 * like x/y with x being a vector of size 3, then the result returned will
2551 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2552 * @return The result vector.
2554 void result(VResult &vec) const;
2557 * Return the total Formula result. If there is a Vector
2558 * component to this Formula, then this is the result of the
2559 * Formula if the formula is applied after summing all the
2560 * components of the Vector. For example, if Formula is x/y where
2561 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2562 * there is no Vector component, total() returns the same value as
2563 * the first entry in the VResult val() returns.
2564 * @return The total of the result vector.
2566 Result total() const;
2569 * Return the number of elements in the tree.
2571 size_t size() const;
2574 * Return true if Formula is binned. i.e. any of its children
2576 * @return True if Formula is binned.
2578 bool binned() const;
2580 bool check() const { return true; }
2583 * Formulas don't need to be reset
2595 void update(StatData *);
2597 std::string str() const;
2600 class FormulaData : public VectorData
2603 virtual std::string str() const = 0;
2604 virtual bool check() const { return true; }
2607 template <class Stat>
2608 class FormulaStatData : public FormulaData
2612 mutable VResult vec;
2613 mutable VCounter cvec;
2616 FormulaStatData(Stat &stat) : s(stat) {}
2618 virtual bool binned() const { return s.binned(); }
2619 virtual bool zero() const { return s.zero(); }
2620 virtual void reset() { s.reset(); }
2622 virtual size_t size() const { return s.size(); }
2623 virtual const VResult &result() const
2628 virtual Result total() const { return s.total(); }
2629 virtual VCounter &value() const { return cvec; }
2630 virtual void visit(Visit &visitor)
2634 visitor.visit(*this);
2636 virtual std::string str() const { return s.str(); }
2641 : public WrapVec<Formula,
2647 * Create and initialize thie formula, and register it with the database.
2652 * Create a formula with the given root node, register it with the
2654 * @param r The root of the expression tree.
2659 * Set an unitialized Formula to the given root.
2660 * @param r The root of the expression tree.
2661 * @return a reference to this formula.
2663 const Formula &operator=(Temp r);
2666 * Add the given tree to the existing one.
2667 * @param r The root of the expression tree.
2668 * @return a reference to this formula.
2670 const Formula &operator+=(Temp r);
2673 class FormulaNode : public Node
2676 const Formula &formula;
2677 mutable VResult vec;
2680 FormulaNode(const Formula &f) : formula(f) {}
2682 virtual size_t size() const { return formula.size(); }
2683 virtual const VResult &result() const { formula.result(vec); return vec; }
2684 virtual Result total() const { return formula.total(); }
2685 virtual bool binned() const { return formula.binned(); }
2687 virtual std::string str() const { return formula.str(); }
2691 * Helper class to construct formula node trees.
2697 * Pointer to a Node object.
2703 * Copy the given pointer to this class.
2704 * @param n A pointer to a Node object to copy.
2706 Temp(NodePtr n) : node(n) { }
2709 * Return the node pointer.
2710 * @return the node pointer.
2712 operator NodePtr&() { return node;}
2716 * Create a new ScalarStatNode.
2717 * @param s The ScalarStat to place in a node.
2719 template <class Bin>
2720 Temp(const Scalar<Bin> &s)
2721 : node(new ScalarStatNode(s.statData())) { }
2724 * Create a new ScalarStatNode.
2725 * @param s The ScalarStat to place in a node.
2727 Temp(const Value &s)
2728 : node(new ScalarStatNode(s.statData())) { }
2731 * Create a new ScalarStatNode.
2732 * @param s The ScalarStat to place in a node.
2734 template <class Bin>
2735 Temp(const Average<Bin> &s)
2736 : node(new ScalarStatNode(s.statData())) { }
2739 * Create a new VectorStatNode.
2740 * @param s The VectorStat to place in a node.
2742 template <class Bin>
2743 Temp(const Vector<Bin> &s)
2744 : node(new VectorStatNode(s.statData())) { }
2749 Temp(const Formula &f)
2750 : node(new FormulaNode(f)) { }
2753 * Create a new ScalarProxyNode.
2754 * @param p The ScalarProxy to place in a node.
2756 template <class Storage, class Bin>
2757 Temp(const ScalarProxy<Storage, Bin> &p)
2758 : node(new ScalarProxyNode<Storage, Bin>(p)) { }
2761 * Create a ConstNode
2762 * @param value The value of the const node.
2764 Temp(signed char value)
2765 : node(new ConstNode<signed char>(value)) {}
2768 * Create a ConstNode
2769 * @param value The value of the const node.
2771 Temp(unsigned char value)
2772 : node(new ConstNode<unsigned char>(value)) {}
2775 * Create a ConstNode
2776 * @param value The value of the const node.
2778 Temp(signed short value)
2779 : node(new ConstNode<signed short>(value)) {}
2782 * Create a ConstNode
2783 * @param value The value of the const node.
2785 Temp(unsigned short value)
2786 : node(new ConstNode<unsigned short>(value)) {}
2789 * Create a ConstNode
2790 * @param value The value of the const node.
2792 Temp(signed int value)
2793 : node(new ConstNode<signed int>(value)) {}
2796 * Create a ConstNode
2797 * @param value The value of the const node.
2799 Temp(unsigned int value)
2800 : node(new ConstNode<unsigned int>(value)) {}
2803 * Create a ConstNode
2804 * @param value The value of the const node.
2806 Temp(signed long value)
2807 : node(new ConstNode<signed long>(value)) {}
2810 * Create a ConstNode
2811 * @param value The value of the const node.
2813 Temp(unsigned long value)
2814 : node(new ConstNode<unsigned long>(value)) {}
2817 * Create a ConstNode
2818 * @param value The value of the const node.
2820 Temp(signed long long value)
2821 : node(new ConstNode<signed long long>(value)) {}
2824 * Create a ConstNode
2825 * @param value The value of the const node.
2827 Temp(unsigned long long value)
2828 : node(new ConstNode<unsigned long long>(value)) {}
2831 * Create a ConstNode
2832 * @param value The value of the const node.
2835 : node(new ConstNode<float>(value)) {}
2838 * Create a ConstNode
2839 * @param value The value of the const node.
2842 : node(new ConstNode<double>(value)) {}
2852 void registerResetCallback(Callback *cb);
2855 operator+(Temp l, Temp r)
2857 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
2861 operator-(Temp l, Temp r)
2863 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
2867 operator*(Temp l, Temp r)
2869 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
2873 operator/(Temp l, Temp r)
2875 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
2881 return NodePtr(new UnaryNode<std::negate<Result> >(l));
2884 template <typename T>
2888 return NodePtr(new ConstNode<T>(val));
2894 return NodePtr(new SumNode<std::plus<Result> >(val));
2897 /* namespace Stats */ }
2899 #endif // __BASE_STATISTICS_HH__