2 * Copyright (c) 2003 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 __STATISTICS_HH__
46 #define __STATISTICS_HH__
57 #include "base/cprintf.hh"
58 #include "base/intmath.hh"
59 #include "base/refcnt.hh"
60 #include "base/str.hh"
61 #include "sim/host.hh"
64 // Un-comment this to enable weirdo-stat debugging
71 /** Define Not a number. */
73 /** Need to define __nan() */
80 /** The current simulated cycle. */
83 /* A namespace for all of the Statistics */
84 namespace Statistics {
85 /** All results are doubles. */
86 typedef double result_t;
87 /** A vector to hold results. */
88 typedef std::vector<result_t> rvec_t;
91 * Define the storage for format flags.
92 * @todo Can probably shrink this.
94 typedef u_int32_t StatFlags;
96 /** Nothing extra to print. */
97 const StatFlags none = 0x00000000;
98 /** This Stat is Initialized */
99 const StatFlags init = 0x00000001;
100 /** Print this stat. */
101 const StatFlags print = 0x00000002;
102 /** Print the total. */
103 const StatFlags total = 0x00000010;
104 /** Print the percent of the total that this entry represents. */
105 const StatFlags pdf = 0x00000020;
106 /** Print the cumulative percentage of total upto this entry. */
107 const StatFlags cdf = 0x00000040;
108 /** Print the distribution. */
109 const StatFlags dist = 0x00000080;
110 /** Don't print if this is zero. */
111 const StatFlags nozero = 0x00000100;
112 /** Don't print if this is NAN */
113 const StatFlags nonan = 0x00000200;
114 /** Used for SS compatability. */
115 const StatFlags __substat = 0x80000000;
117 /** Mask of flags that can't be set directly */
118 const StatFlags __reserved = init | print | __substat;
127 extern DisplayMode DefaultMode;
129 /* Contains the statistic implementation details */
130 //////////////////////////////////////////////////////////////////////
132 // Statistics Framework Base classes
134 //////////////////////////////////////////////////////////////////////
137 /** The name of the stat. */
139 /** The description of the stat. */
141 /** The formatting flags. */
143 /** The display precision. */
147 /** A pointer to a prerequisite Stat. */
148 const StatData *prereq;
151 : flags(none), precision(-1), prereq(0)
157 * @return true if the stat is binned.
159 virtual bool binned() const = 0;
162 * Print this stat to the given ostream.
163 * @param stream The stream to print to.
165 virtual void display(std::ostream &stream, DisplayMode mode) const = 0;
166 virtual void python(Python &py) const = 0;
167 bool dodisplay() const { return !prereq || !prereq->zero(); }
170 * Reset the corresponding stat to the default state.
172 virtual void reset() = 0;
175 * @return true if this stat has a value and satisfies its
176 * requirement as a prereq
178 virtual bool zero() const = 0;
181 * Check that this stat has been set up properly and is ready for
183 * @return true for success
185 virtual bool check() const = 0;
186 bool baseCheck() const;
189 * Checks if the first stat's name is alphabetically less than the second.
190 * This function breaks names up at periods and considers each subname
192 * @param stat1 The first stat.
193 * @param stat2 The second stat.
194 * @return stat1's name is alphabetically before stat2's
196 static bool less(StatData *stat1, StatData *stat2);
199 struct ScalarDataBase : public StatData
201 virtual result_t val() const = 0;
202 virtual result_t total() const = 0;
204 virtual void display(std::ostream &stream, DisplayMode mode) const;
205 virtual void python(Python &py) const;
209 class ScalarData : public ScalarDataBase
215 ScalarData(T &stat) : s(stat) {}
217 virtual bool binned() const { return s.binned(); }
218 virtual bool check() const { return s.check(); }
219 virtual result_t val() const { return s.val(); }
220 virtual result_t total() const { return s.total(); }
221 virtual void reset() { s.reset(); }
222 virtual bool zero() const { return s.zero(); }
225 struct VectorDataBase : public StatData
227 /** Names and descriptions of subfields. */
228 mutable std::vector<std::string> subnames;
229 mutable std::vector<std::string> subdescs;
231 virtual void display(std::ostream &stream, DisplayMode mode) const;
232 virtual void python(Python &py) const;
234 virtual size_t size() const = 0;
235 virtual const rvec_t &val() const = 0;
236 virtual result_t total() const = 0;
237 virtual void update()
239 if (!subnames.empty()) {
241 if (subnames.size() < s)
244 if (subdescs.size() < s)
251 class VectorData : public VectorDataBase
258 VectorData(T &stat) : s(stat) {}
260 virtual bool binned() const { return s.binned(); }
261 virtual bool check() const { return s.check(); }
262 virtual bool zero() const { return s.zero(); }
263 virtual void reset() { s.reset(); }
265 virtual size_t size() const { return s.size(); }
266 virtual const rvec_t &val() const
271 virtual result_t total() const { return s.total(); }
272 virtual void update()
274 VectorDataBase::update();
296 void python(Python &py, const std::string &name) const;
299 struct DistDataBase : public StatData
301 /** Local storage for the entry values, used for printing. */
304 virtual void display(std::ostream &stream, DisplayMode mode) const;
305 virtual void python(Python &py) const;
306 virtual void update() = 0;
310 class DistData : public DistDataBase
316 DistData(T &stat) : s(stat) {}
318 virtual bool binned() const { return s.binned(); }
319 virtual bool check() const { return s.check(); }
320 virtual void reset() { s.reset(); }
321 virtual bool zero() const { return s.zero(); }
322 virtual void update() { return s.update(this); }
325 struct VectorDistDataBase : public StatData
327 std::vector<DistDataData> data;
329 /** Names and descriptions of subfields. */
330 mutable std::vector<std::string> subnames;
331 mutable std::vector<std::string> subdescs;
333 /** Local storage for the entry values, used for printing. */
336 virtual size_t size() const = 0;
337 virtual void display(std::ostream &stream, DisplayMode mode) const;
338 virtual void python(Python &py) const;
339 virtual void update()
342 if (subnames.size() < s)
345 if (subdescs.size() < s)
351 class VectorDistData : public VectorDistDataBase
355 typedef typename T::bin_t bin_t;
358 VectorDistData(T &stat) : s(stat) {}
360 virtual bool binned() const { return bin_t::binned; }
361 virtual bool check() const { return s.check(); }
362 virtual void reset() { s.reset(); }
363 virtual size_t size() const { return s.size(); }
364 virtual bool zero() const { return s.zero(); }
365 virtual void update()
367 VectorDistDataBase::update();
368 return s.update(this);
372 struct Vector2dDataBase : public StatData
374 /** Names and descriptions of subfields. */
375 std::vector<std::string> subnames;
376 std::vector<std::string> subdescs;
377 std::vector<std::string> y_subnames;
379 /** Local storage for the entry values, used for printing. */
384 virtual void display(std::ostream &stream, DisplayMode mode) const;
385 virtual void python(Python &py) const;
386 virtual void update()
388 if (subnames.size() < x)
394 class Vector2dData : public Vector2dDataBase
398 typedef typename T::bin_t bin_t;
401 Vector2dData(T &stat) : s(stat) {}
403 virtual bool binned() const { return bin_t::binned; }
404 virtual bool check() const { return s.check(); }
405 virtual void reset() { s.reset(); }
406 virtual bool zero() const { return s.zero(); }
407 virtual void update()
409 Vector2dDataBase::update();
418 StatData *find() const;
419 void map(StatData *data);
421 StatData *statData();
422 const StatData *statData() const;
428 template <class Parent, class Child, template <class> class Data>
429 class Wrap : public Child
432 Parent &self() { return *reinterpret_cast<Parent *>(this); }
435 Data<Child> *statData()
437 StatData *__data = DataAccess::statData();
438 Data<Child> *ptr = dynamic_cast<Data<Child> *>(__data);
444 const Data<Child> *statData() const
446 const StatData *__data = DataAccess::statData();
447 const Data<Child> *ptr = dynamic_cast<const Data<Child> *>(__data);
455 map(new Data<Child>(*this));
459 * Set the name and marks this stat to print at the end of simulation.
460 * @param name The new name.
461 * @return A reference to this stat.
463 Parent &name(const std::string &_name)
465 Data<Child> *data = statData();
472 * Set the description and marks this stat to print at the end of
474 * @param desc The new description.
475 * @return A reference to this stat.
477 Parent &desc(const std::string &_desc)
479 statData()->desc = _desc;
484 * Set the precision and marks this stat to print at the end of simulation.
485 * @param p The new precision
486 * @return A reference to this stat.
488 Parent &precision(int _precision)
490 statData()->precision = _precision;
495 * Set the flags and marks this stat to print at the end of simulation.
496 * @param f The new flags.
497 * @return A reference to this stat.
499 Parent &flags(StatFlags _flags)
501 statData()->flags |= _flags;
506 * Set the prerequisite stat and marks this stat to print at the end of
508 * @param prereq The prerequisite stat.
509 * @return A reference to this stat.
512 Parent &prereq(const T &prereq)
514 statData()->prereq = prereq.statData();
519 template <class Parent, class Child, template <class Child> class Data>
520 class WrapVec : public Wrap<Parent, Child, Data>
523 // The following functions are specific to vectors. If you use them
524 // in a non vector context, you will get a nice compiler error!
527 * Set the subfield name for the given index, and marks this stat to print
528 * at the end of simulation.
529 * @param index The subfield index.
530 * @param name The new name of the subfield.
531 * @return A reference to this stat.
533 Parent &subname(int index, const std::string &name)
535 std::vector<std::string> &subn = statData()->subnames;
536 if (subn.size() <= index)
537 subn.resize(index + 1);
543 * Set the subfield description for the given index and marks this stat to
544 * print at the end of simulation.
545 * @param index The subfield index.
546 * @param desc The new description of the subfield
547 * @return A reference to this stat.
549 Parent &subdesc(int index, const std::string &desc)
551 std::vector<std::string> &subd = statData()->subdescs;
552 if (subd.size() <= index)
553 subd.resize(index + 1);
561 template <class Parent, class Child, template <class Child> class Data>
562 class WrapVec2d : public WrapVec<Parent, Child, Data>
566 * @warning This makes the assumption that if you're gonna subnames a 2d
567 * vector, you're subnaming across all y
569 Parent &ysubnames(const char **names)
571 Data<Child> *data = statData();
572 data->y_subnames.resize(y);
573 for (int i = 0; i < y; ++i)
574 data->y_subnames[i] = names[i];
577 Parent &ysubname(int index, const std::string subname)
579 Data<Child> *data = statData();
581 data->y_subnames.resize(y);
582 data->y_subnames[i] = subname.c_str();
587 //////////////////////////////////////////////////////////////////////
591 //////////////////////////////////////////////////////////////////////
594 * Templatized storage and interface for a simple scalar stat.
596 template <typename T>
600 /** The paramaters for this storage type, none for a scalar. */
604 /** The statistic value. */
614 * Builds this storage element and calls the base constructor of the
617 StatStor(const Params &) : data(Null()) {}
620 * The the stat to the given value.
621 * @param val The new value.
622 * @param p The paramters of this storage type.
624 void set(T val, const Params &p) { data = val; }
626 * Increment the stat by the given value.
627 * @param val The new value.
628 * @param p The paramters of this storage type.
630 void inc(T val, const Params &p) { data += val; }
632 * Decrement the stat by the given value.
633 * @param val The new value.
634 * @param p The paramters of this storage type.
636 void dec(T val, const Params &p) { data -= val; }
638 * Return the value of this stat as a result type.
639 * @param p The parameters of this storage type.
640 * @return The value of this stat.
642 result_t val(const Params &p) const { return (result_t)data; }
644 * Return the value of this stat as its base type.
645 * @param p The params of this storage type.
646 * @return The value of this stat.
648 T value(const Params &p) const { return data; }
650 * Reset stat value to default
652 void reset() { data = Null(); }
655 * @return true if zero value
657 bool zero() const { return data == Null(); }
661 * Templatized storage and interface to a per-cycle average stat. This keeps
662 * a current count and updates a total (count * cycles) when this count
663 * changes. This allows the quick calculation of a per cycle count of the item
664 * being watched. This is good for keeping track of residencies in structures
665 * among other things.
666 * @todo add lateny to the stat and fix binning.
668 template <typename T>
672 /** The paramaters for this storage type */
676 * The current count. We stash this here because the current
677 * value is not a binned value.
683 /** The total count for all cycles. */
684 mutable result_t total;
685 /** The cycle that current last changed. */
690 * Build and initializes this stat storage.
692 AvgStor(Params &p) : total(0), last(0) { p.current = T(); }
695 * Set the current count to the one provided, update the total and last
697 * @param val The new count.
698 * @param p The parameters for this storage.
700 void set(T val, Params &p) {
701 total += p.current * (curTick - last);
707 * Increment the current count by the provided value, calls set.
708 * @param val The amount to increment.
709 * @param p The parameters for this storage.
711 void inc(T val, Params &p) { set(p.current + val, p); }
714 * Deccrement the current count by the provided value, calls set.
715 * @param val The amount to decrement.
716 * @param p The parameters for this storage.
718 void dec(T val, Params &p) { set(p.current - val, p); }
721 * Return the current average.
722 * @param p The parameters for this storage.
723 * @return The current average.
725 result_t val(const Params &p) const {
726 total += p.current * (curTick - last);
728 return (result_t)(total + p.current) / (result_t)(curTick + 1);
732 * Return the current count.
733 * @param p The parameters for this storage.
734 * @return The current count.
736 T value(const Params &p) const { return p.current; }
739 * Reset stat value to default
748 * @return true if zero value
750 bool zero() const { return total == 0.0; }
754 * Implementation of a scalar stat. The type of stat is determined by the
755 * Storage template. The storage for this stat is held within the Bin class.
756 * This allows for breaking down statistics across multiple bins easily.
758 template <typename T, template <typename T> class Storage, class Bin>
759 class ScalarBase : public DataAccess
762 /** Define the type of the storage class. */
763 typedef Storage<T> storage_t;
764 /** Define the params of the storage class. */
765 typedef typename storage_t::Params params_t;
766 /** Define the bin type. */
767 typedef typename Bin::Bin<storage_t> bin_t;
770 /** The bin of this stat. */
772 /** The parameters for this stat. */
777 * Retrieve the storage from the bin.
778 * @return The storage object for this stat.
780 storage_t *data() { return bin.data(params); }
782 * Retrieve a const pointer to the storage from the bin.
783 * @return A const pointer to the storage object for this stat.
785 const storage_t *data() const
787 bin_t *_bin = const_cast<bin_t *>(&bin);
788 params_t *_params = const_cast<params_t *>(¶ms);
789 return _bin->data(*_params);
794 * Copy constructor, copies are not allowed.
796 ScalarBase(const ScalarBase &stat);
800 const ScalarBase &operator=(const ScalarBase &);
804 * Return the current value of this stat as its base type.
805 * @return The current value.
807 T value() const { return data()->value(params); }
811 * Create and initialize this stat, register it with the database.
819 // Common operators for stats
821 * Increment the stat by 1. This calls the associated storage object inc
824 void operator++() { data()->inc(1, params); }
826 * Decrement the stat by 1. This calls the associated storage object dec
829 void operator--() { data()->dec(1, params); }
831 /** Increment the stat by 1. */
832 void operator++(int) { ++*this; }
833 /** Decrement the stat by 1. */
834 void operator--(int) { --*this; }
837 * Set the data value to the given value. This calls the associated storage
838 * object set function.
839 * @param v The new value.
841 template <typename U>
842 void operator=(const U& v) { data()->set(v, params); }
845 * Increment the stat by the given value. This calls the associated
846 * storage object inc function.
847 * @param v The value to add.
849 template <typename U>
850 void operator+=(const U& v) { data()->inc(v, params); }
853 * Decrement the stat by the given value. This calls the associated
854 * storage object dec function.
855 * @param v The value to substract.
857 template <typename U>
858 void operator-=(const U& v) { data()->dec(v, params); }
861 * Return the number of elements, always 1 for a scalar.
864 size_t size() const { return 1; }
866 * Return true if stat is binned.
867 *@return True is stat is binned.
869 bool binned() const { return bin_t::binned; }
871 bool check() const { return bin.initialized(); }
874 * Reset stat value to default
876 void reset() { bin.reset(); }
878 result_t val() { return data()->val(params); }
880 result_t total() { return val(); }
882 bool zero() { return val() == 0.0; }
885 //////////////////////////////////////////////////////////////////////
889 //////////////////////////////////////////////////////////////////////
890 template <typename T, template <typename T> class Storage, class Bin>
894 * Implementation of a vector of stats. The type of stat is determined by the
895 * Storage class. @sa ScalarBase
897 template <typename T, template <typename T> class Storage, class Bin>
898 class VectorBase : public DataAccess
901 /** Define the type of the storage class. */
902 typedef Storage<T> storage_t;
903 /** Define the params of the storage class. */
904 typedef typename storage_t::Params params_t;
905 /** Define the bin type. */
906 typedef typename Bin::VectorBin<storage_t> bin_t;
909 /** The bin of this stat. */
911 /** The parameters for this stat. */
916 * Retrieve the storage from the bin for the given index.
917 * @param index The vector index to access.
918 * @return The storage object at the given index.
920 storage_t *data(int index) { return bin.data(index, params); }
922 * Retrieve a const pointer to the storage from the bin
923 * for the given index.
924 * @param index The vector index to access.
925 * @return A const pointer to the storage object at the given index.
927 const storage_t *data(int index) const
929 bin_t *_bin = const_cast<bin_t *>(&bin);
930 params_t *_params = const_cast<params_t *>(¶ms);
931 return _bin->data(index, *_params);
935 // Copying stats is not allowed
936 /** Copying stats isn't allowed. */
937 VectorBase(const VectorBase &stat);
938 /** Copying stats isn't allowed. */
939 const VectorBase &operator=(const VectorBase &);
943 * Copy the values to a local vector and return a reference to it.
944 * @return A reference to a vector of the stat values.
946 void val(rvec_t &vec) const
949 for (int i = 0; i < size(); ++i)
950 vec[i] = data(i)->val(params);
954 * @return True is stat is binned.
956 bool binned() const { return bin_t::binned; }
959 * Return a total of all entries in this vector.
960 * @return The total of all vector entries.
962 result_t total() const {
963 result_t total = 0.0;
964 for (int i = 0; i < size(); ++i)
965 total += data(i)->val(params);
970 * @return the number of elements in this vector.
972 size_t size() const { return bin.size(); }
976 for (int i = 0; i < size(); ++i)
982 bool check() const { return bin.initialized(); }
983 void reset() { bin.reset(); }
988 /** Friend this class with the associated scalar proxy. */
989 friend class ScalarProxy<T, Storage, Bin>;
992 * Return a reference (ScalarProxy) to the stat at the given index.
993 * @param index The vector index to access.
994 * @return A reference of the stat.
996 ScalarProxy<T, Storage, Bin> operator[](int index);
998 void update(StatData *data) {}
1001 const StatData * getStatData(const void *stat);
1004 * A proxy class to access the stat at a given index in a VectorBase stat.
1005 * Behaves like a ScalarBase.
1007 template <typename T, template <typename T> class Storage, class Bin>
1011 /** Define the type of the storage class. */
1012 typedef Storage<T> storage_t;
1013 /** Define the params of the storage class. */
1014 typedef typename storage_t::Params params_t;
1015 /** Define the bin type. */
1016 typedef typename Bin::VectorBin<storage_t> bin_t;
1019 /** Pointer to the bin in the parent VectorBase. */
1021 /** Pointer to the params in the parent VectorBase. */
1023 /** The index to access in the parent VectorBase. */
1025 /** Keep a pointer to the original stat so was can get data */
1030 * Retrieve the storage from the bin.
1031 * @return The storage from the bin for this stat.
1033 storage_t *data() { return bin->data(index, *params); }
1035 * Retrieve a const pointer to the storage from the bin.
1036 * @return A const pointer to the storage for this stat.
1038 const storage_t *data() const
1040 bin_t *_bin = const_cast<bin_t *>(bin);
1041 params_t *_params = const_cast<params_t *>(params);
1042 return _bin->data(index, *_params);
1047 * Return the current value of this statas a result type.
1048 * @return The current value.
1050 result_t val() const { return data()->val(*params); }
1052 * Return the current value of this stat as its base type.
1053 * @return The current value.
1055 T value() const { return data()->value(*params); }
1059 * Create and initialize this proxy, do not register it with the database.
1060 * @param b The bin to use.
1061 * @param p The params to use.
1062 * @param i The index to access.
1064 ScalarProxy(bin_t &b, params_t &p, int i, void *s)
1065 : bin(&b), params(&p), index(i), stat(s) {}
1067 * Create a copy of the provided ScalarProxy.
1068 * @param sp The proxy to copy.
1070 ScalarProxy(const ScalarProxy &sp)
1071 : bin(sp.bin), params(sp.params), index(sp.index), stat(sp.stat) {}
1073 * Set this proxy equal to the provided one.
1074 * @param sp The proxy to copy.
1075 * @return A reference to this proxy.
1077 const ScalarProxy &operator=(const ScalarProxy &sp) {
1086 // Common operators for stats
1088 * Increment the stat by 1. This calls the associated storage object inc
1091 void operator++() { data()->inc(1, *params); }
1093 * Decrement the stat by 1. This calls the associated storage object dec
1096 void operator--() { data()->dec(1, *params); }
1098 /** Increment the stat by 1. */
1099 void operator++(int) { ++*this; }
1100 /** Decrement the stat by 1. */
1101 void operator--(int) { --*this; }
1104 * Set the data value to the given value. This calls the associated storage
1105 * object set function.
1106 * @param v The new value.
1108 template <typename U>
1109 void operator=(const U& v) { data()->set(v, *params); }
1112 * Increment the stat by the given value. This calls the associated
1113 * storage object inc function.
1114 * @param v The value to add.
1116 template <typename U>
1117 void operator+=(const U& v) { data()->inc(v, *params); }
1120 * Decrement the stat by the given value. This calls the associated
1121 * storage object dec function.
1122 * @param v The value to substract.
1124 template <typename U>
1125 void operator-=(const U& v) { data()->dec(v, *params); }
1128 * Return the number of elements, always 1 for a scalar.
1131 size_t size() const { return 1; }
1134 * Return true if stat is binned.
1135 *@return false since Proxies aren't printed/binned
1137 bool binned() const { return false; }
1140 * This stat has no state. Nothing to reset
1145 const StatData *statData() const { return getStatData(stat); }
1146 std::string str() const
1148 return csprintf("%s[%d]", statData()->name, index);
1153 template <typename T, template <typename T> class Storage, class Bin>
1154 inline ScalarProxy<T, Storage, Bin>
1155 VectorBase<T, Storage, Bin>::operator[](int index)
1157 assert (index >= 0 && index < size());
1158 return ScalarProxy<T, Storage, Bin>(bin, params, index, this);
1161 template <typename T, template <typename T> class Storage, class Bin>
1164 template <typename T, template <typename T> class Storage, class Bin>
1165 class Vector2dBase : public DataAccess
1168 typedef Storage<T> storage_t;
1169 typedef typename storage_t::Params params_t;
1170 typedef typename Bin::VectorBin<storage_t> bin_t;
1179 storage_t *data(int index) { return bin.data(index, params); }
1180 const storage_t *data(int index) const
1182 bin_t *_bin = const_cast<bin_t *>(&bin);
1183 params_t *_params = const_cast<params_t *>(¶ms);
1184 return _bin->data(index, *_params);
1188 // Copying stats is not allowed
1189 Vector2dBase(const Vector2dBase &stat);
1190 const Vector2dBase &operator=(const Vector2dBase &);
1195 void update(Vector2dDataBase *data)
1197 int size = this->size();
1198 data->vec.resize(size);
1199 for (int i = 0; i < size; ++i)
1200 data->vec[i] = this->data(i)->val(params);
1203 std::string ysubname(int i) const { return (*y_subnames)[i]; }
1205 friend class VectorProxy<T, Storage, Bin>;
1206 VectorProxy<T, Storage, Bin> operator[](int index);
1208 size_t size() const { return bin.size(); }
1209 bool zero() const { return data(0)->value(params) == 0.0; }
1212 * Reset stat value to default
1214 void reset() { bin.reset(); }
1216 bool check() { return bin.initialized(); }
1219 template <typename T, template <typename T> class Storage, class Bin>
1223 typedef Storage<T> storage_t;
1224 typedef typename storage_t::Params params_t;
1225 typedef typename Bin::VectorBin<storage_t> bin_t;
1235 mutable rvec_t *vec;
1237 storage_t *data(int index) {
1238 assert(index < len);
1239 return bin->data(offset + index, *params);
1242 const storage_t *data(int index) const {
1243 bin_t *_bin = const_cast<bin_t *>(bin);
1244 params_t *_params = const_cast<params_t *>(params);
1245 return _bin->data(offset + index, *_params);
1249 const rvec_t &val() const {
1251 vec->resize(size());
1253 vec = new rvec_t(size());
1255 for (int i = 0; i < size(); ++i)
1256 (*vec)[i] = data(i)->val(*params);
1261 result_t total() const {
1262 result_t total = 0.0;
1263 for (int i = 0; i < size(); ++i)
1264 total += data(i)->val(*params);
1269 VectorProxy(bin_t &b, params_t &p, int o, int l, void *s)
1270 : bin(&b), params(&p), offset(o), len(l), stat(s), vec(NULL)
1274 VectorProxy(const VectorProxy &sp)
1275 : bin(sp.bin), params(sp.params), offset(sp.offset), len(sp.len),
1276 stat(sp.stat), vec(NULL)
1286 const VectorProxy &operator=(const VectorProxy &sp)
1299 ScalarProxy<T, Storage, Bin> operator[](int index)
1301 assert (index >= 0 && index < size());
1302 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index,
1306 size_t size() const { return len; }
1309 * Return true if stat is binned.
1310 *@return false since Proxies aren't printed/binned
1312 bool binned() const { return false; }
1315 * This stat has no state. Nothing to reset.
1320 template <typename T, template <typename T> class Storage, class Bin>
1321 inline VectorProxy<T, Storage, Bin>
1322 Vector2dBase<T, Storage, Bin>::operator[](int index)
1324 int offset = index * y;
1325 assert (index >= 0 && offset < size());
1326 return VectorProxy<T, Storage, Bin>(bin, params, offset, y, this);
1329 //////////////////////////////////////////////////////////////////////
1331 // Non formula statistics
1333 //////////////////////////////////////////////////////////////////////
1336 * Templatized storage and interface for a distrbution stat.
1338 template <typename T>
1342 /** The parameters for a distribution stat. */
1345 /** The minimum value to track. */
1347 /** The maximum value to track. */
1349 /** The number of entries in each bucket. */
1351 /** The number of buckets. Equal to (max-min)/bucket_size. */
1354 enum { fancy = false };
1357 /** The smallest value sampled. */
1359 /** The largest value sampled. */
1361 /** The number of values sampled less than min. */
1363 /** The number of values sampled more than max. */
1365 /** The current sum. */
1367 /** The sum of squares. */
1369 /** The number of samples. */
1371 /** Counter for each bucket. */
1376 * Construct this storage with the supplied params.
1377 * @param params The parameters.
1379 DistStor(const Params ¶ms)
1380 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1381 sum(T()), squares(T()), samples(0), vec(params.size)
1387 * Add a value to the distribution for the given number of times.
1388 * @param val The value to add.
1389 * @param number The number of times to add the value.
1390 * @param params The paramters of the distribution.
1392 void sample(T val, int number, const Params ¶ms)
1394 if (val < params.min)
1395 underflow += number;
1396 else if (val > params.max)
1399 int index = (val - params.min) / params.bucket_size;
1400 assert(index < size(params));
1401 vec[index] += number;
1410 T sample = val * number;
1412 squares += sample * sample;
1417 * Return the number of buckets in this distribution.
1418 * @return the number of buckets.
1419 * @todo Is it faster to return the size from the parameters?
1421 size_t size(const Params &) const { return vec.size(); }
1424 * Returns true if any calls to sample have been made.
1425 * @param params The paramters of the distribution.
1426 * @return True if any values have been sampled.
1428 bool zero(const Params ¶ms) const
1430 return samples == 0;
1433 void update(DistDataData *data, const Params ¶ms)
1435 data->min = params.min;
1436 data->max = params.max;
1437 data->bucket_size = params.bucket_size;
1438 data->size = params.size;
1440 data->min_val = (min_val == INT_MAX) ? 0 : min_val;
1441 data->max_val = (max_val == INT_MIN) ? 0 : max_val;
1442 data->underflow = underflow;
1443 data->overflow = overflow;
1444 data->vec.resize(params.size);
1445 for (int i = 0; i < params.size; ++i)
1446 data->vec[i] = vec[i];
1449 data->squares = squares;
1450 data->samples = samples;
1454 * Reset stat value to default
1463 int size = vec.size();
1464 for (int i = 0; i < size; ++i)
1474 * Templatized storage and interface for a distribution that calculates mean
1477 template <typename T>
1482 * No paramters for this storage.
1485 enum { fancy = true };
1488 /** The current sum. */
1490 /** The sum of squares. */
1492 /** The number of samples. */
1497 * Create and initialize this storage.
1499 FancyStor(const Params &) : sum(T()), squares(T()), samples(0) {}
1502 * Add a value the given number of times to this running average.
1503 * Update the running sum and sum of squares, increment the number of
1504 * values seen by the given number.
1505 * @param val The value to add.
1506 * @param number The number of times to add the value.
1507 * @param p The parameters of this stat.
1509 void sample(T val, int number, const Params &p)
1511 T value = val * number;
1513 squares += value * value;
1517 void update(DistDataData *data, const Params ¶ms)
1520 data->squares = squares;
1521 data->samples = samples;
1525 * Return the number of entries in this stat, 1
1528 size_t size(const Params &) const { return 1; }
1531 * Return true if no samples have been added.
1532 * @return True if no samples have been added.
1534 bool zero(const Params &) const { return samples == 0; }
1537 * Reset stat value to default
1548 * Templatized storage for distribution that calculates per cycle mean and
1551 template <typename T>
1555 /** No parameters for this storage. */
1557 enum { fancy = true };
1560 /** Current total. */
1562 /** Current sum of squares. */
1567 * Create and initialize this storage.
1569 AvgFancy(const Params &) : sum(T()), squares(T()) {}
1572 * Add a value to the distribution for the given number of times.
1573 * Update the running sum and sum of squares.
1574 * @param val The value to add.
1575 * @param number The number of times to add the value.
1576 * @param p The paramters of the distribution.
1578 void sample(T val, int number, const Params& p)
1580 T value = val * number;
1582 squares += value * value;
1585 void update(DistDataData *data, const Params ¶ms)
1588 data->squares = squares;
1589 data->samples = curTick;
1593 * Return the number of entries, in this case 1.
1596 size_t size(const Params ¶ms) const { return 1; }
1598 * Return true if no samples have been added.
1599 * @return True if the sum is zero.
1601 bool zero(const Params ¶ms) const { return sum == 0; }
1603 * Reset stat value to default
1613 * Implementation of a distribution stat. The type of distribution is
1614 * determined by the Storage template. @sa ScalarBase
1616 template <typename T, template <typename T> class Storage, class Bin>
1617 class DistBase : public DataAccess
1620 /** Define the type of the storage class. */
1621 typedef Storage<T> storage_t;
1622 /** Define the params of the storage class. */
1623 typedef typename storage_t::Params params_t;
1624 /** Define the bin type. */
1625 typedef typename Bin::Bin<storage_t> 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_t *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_t *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);
1651 // Copying stats is not allowed
1652 /** Copies are not allowed. */
1653 DistBase(const DistBase &stat);
1654 /** Copies are not allowed. */
1655 const DistBase &operator=(const DistBase &);
1661 * Add a value to the distribtion n times. Calls sample on the storage
1663 * @param v The value to add.
1664 * @param n The number of times to add it, defaults to 1.
1666 template <typename U>
1667 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1670 * Return the number of entries in this stat.
1671 * @return The number of entries.
1673 size_t size() const { return data()->size(params); }
1675 * Return true if no samples have been added.
1676 * @return True if there haven't been any samples.
1678 bool zero() const { return data()->zero(params); }
1680 void update(DistDataBase *base)
1682 base->data.fancy = storage_t::fancy;
1683 data()->update(&(base->data), params);
1686 * @return True is stat is binned.
1688 bool binned() const { return bin_t::binned; }
1690 * Reset stat value to default
1697 bool check() { return bin.initialized(); }
1700 template <typename T, template <typename T> class Storage, class Bin>
1703 template <typename T, template <typename T> class Storage, class Bin>
1704 class VectorDistBase : public DataAccess
1707 typedef Storage<T> storage_t;
1708 typedef typename storage_t::Params params_t;
1709 typedef typename Bin::VectorBin<storage_t> bin_t;
1716 storage_t *data(int index) { return bin.data(index, params); }
1717 const storage_t *data(int index) const
1719 bin_t *_bin = const_cast<bin_t *>(&bin);
1720 params_t *_params = const_cast<params_t *>(¶ms);
1721 return _bin->data(index, *_params);
1725 // Copying stats is not allowed
1726 VectorDistBase(const VectorDistBase &stat);
1727 const VectorDistBase &operator=(const VectorDistBase &);
1732 friend class DistProxy<T, Storage, Bin>;
1733 DistProxy<T, Storage, Bin> operator[](int index);
1734 const DistProxy<T, Storage, Bin> operator[](int index) const;
1736 size_t size() const { return bin.size(); }
1737 bool zero() const { return false; }
1739 * Return true if stat is binned.
1740 *@return True is stat is binned.
1742 bool binned() const { return bin_t::binned; }
1744 * Reset stat value to default
1746 void reset() { bin.reset(); }
1748 bool check() { return bin.initialized(); }
1749 void update(VectorDistDataBase *base)
1751 int size = this->size();
1752 base->data.resize(size);
1753 for (int i = 0; i < size; ++i) {
1754 base->data[i].fancy = storage_t::fancy;
1755 data(i)->update(&(base->data[i]), params);
1760 template <typename T, template <typename T> class Storage, class Bin>
1764 typedef Storage<T> storage_t;
1765 typedef typename storage_t::Params params_t;
1766 typedef typename Bin::Bin<storage_t> bin_t;
1767 typedef VectorDistBase<T, Storage, Bin> base_t;
1772 const base_t *cstat;
1777 storage_t *data() { return stat->data(index); }
1778 const storage_t *data() const { return cstat->data(index); }
1781 DistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1782 : cstat(&s), index(i) {}
1783 DistProxy(const DistProxy &sp)
1784 : cstat(sp.cstat), index(sp.index) {}
1785 const DistProxy &operator=(const DistProxy &sp) {
1786 cstat = sp.cstat; index = sp.index; return *this;
1790 template <typename U>
1791 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1793 size_t size() const { return 1; }
1794 bool zero() const { return data()->zero(cstat->params); }
1796 * Return true if stat is binned.
1797 *@return false since Proxies are not binned/printed.
1799 bool binned() const { return false; }
1801 * Proxy has no state. Nothing to reset.
1806 template <typename T, template <typename T> class Storage, class Bin>
1807 inline DistProxy<T, Storage, Bin>
1808 VectorDistBase<T, Storage, Bin>::operator[](int index)
1810 assert (index >= 0 && index < size());
1811 return DistProxy<T, Storage, Bin>(*this, index);
1814 template <typename T, template <typename T> class Storage, class Bin>
1815 inline const DistProxy<T, Storage, Bin>
1816 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1818 assert (index >= 0 && index < size());
1819 return DistProxy<T, Storage, Bin>(*this, index);
1823 template <typename T, template <typename T> class Storage, class Bin>
1825 VectorDistBase<T, Storage, Bin>::total(int index) const
1828 for (int i=0; i < x_size(); ++i) {
1829 total += data(i)->val(*params);
1834 //////////////////////////////////////////////////////////////////////
1838 //////////////////////////////////////////////////////////////////////
1841 * Base class for formula statistic node. These nodes are used to build a tree
1842 * that represents the formula.
1844 class Node : public RefCounted
1848 * Return the number of nodes in the subtree starting at this node.
1849 * @return the number of nodes in this subtree.
1851 virtual size_t size() const = 0;
1853 * Return the result vector of this subtree.
1854 * @return The result vector of this subtree.
1856 virtual const rvec_t &val() const = 0;
1858 * Return the total of the result vector.
1859 * @return The total of the result vector.
1861 virtual result_t total() const = 0;
1863 * Return true if stat is binned.
1864 *@return True is stat is binned.
1866 virtual bool binned() const = 0;
1871 virtual std::string str() const = 0;
1874 /** Reference counting pointer to a function Node. */
1875 typedef RefCountingPtr<Node> NodePtr;
1877 class ScalarStatNode : public Node
1880 const ScalarDataBase *data;
1881 mutable rvec_t result;
1884 ScalarStatNode(const ScalarDataBase *d) : data(d), result(1) {}
1885 virtual const rvec_t &val() const
1887 result[0] = data->val();
1890 virtual result_t total() const { return data->val(); };
1892 virtual size_t size() const { return 1; }
1894 * Return true if stat is binned.
1895 *@return True is stat is binned.
1897 virtual bool binned() const { return data->binned(); }
1902 virtual std::string str() const { return data->name; }
1905 template <typename T, template <typename T> class Storage, class Bin>
1906 class ScalarProxyNode : public Node
1909 const ScalarProxy<T, Storage, Bin> proxy;
1910 mutable rvec_t result;
1913 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1914 : proxy(p), result(1) { }
1915 virtual const rvec_t &val() const
1917 result[0] = proxy.val();
1920 virtual result_t total() const { return proxy.val(); };
1922 virtual size_t size() const { return 1; }
1924 * Return true if stat is binned.
1925 *@return True is stat is binned.
1927 virtual bool binned() const { return proxy.binned(); }
1932 virtual std::string str() const { return proxy.str(); }
1935 class VectorStatNode : public Node
1938 const VectorDataBase *data;
1941 VectorStatNode(const VectorDataBase *d) : data(d) { }
1942 virtual const rvec_t &val() const { return data->val(); }
1943 virtual result_t total() const { return data->total(); };
1945 virtual size_t size() const { return data->size(); }
1947 * Return true if stat is binned.
1948 *@return True is stat is binned.
1950 virtual bool binned() const { return data->binned(); }
1952 virtual std::string str() const { return data->name; }
1955 template <typename T>
1956 class ConstNode : public Node
1962 ConstNode(T s) : data(1, (result_t)s) {}
1963 const rvec_t &val() const { return data; }
1964 virtual result_t total() const { return data[0]; };
1966 virtual size_t size() const { return 1; }
1968 * Return true if stat is binned.
1969 *@return False since constants aren't binned.
1971 virtual bool binned() const { return false; }
1973 virtual std::string str() const { return to_string(data[0]); }
1976 template <typename T>
1977 class FunctorNode : public Node
1981 mutable rvec_t result;
1984 FunctorNode(T &f) : functor(f) { result.resize(1); }
1985 const rvec_t &val() const {
1986 result[0] = (result_t)functor();
1989 virtual result_t total() const { return (result_t)functor(); };
1991 virtual size_t size() const { return 1; }
1993 * Return true if stat is binned.
1994 *@return False since Functors aren't binned
1996 virtual bool binned() const { return false; }
1997 virtual std::string str() const { return to_string(functor()); }
2000 template <typename T>
2001 class ScalarNode : public Node
2005 mutable rvec_t result;
2008 ScalarNode(T &s) : scalar(s) { result.resize(1); }
2009 const rvec_t &val() const {
2010 result[0] = (result_t)scalar;
2013 virtual result_t total() const { return (result_t)scalar; };
2015 virtual size_t size() const { return 1; }
2017 * Return true if stat is binned.
2018 *@return False since Scalar's aren't binned
2020 virtual bool binned() const { return false; }
2021 virtual std::string str() const { return to_string(scalar); }
2028 struct OpString<std::plus<result_t> >
2030 static std::string str() { return "+"; }
2034 struct OpString<std::minus<result_t> >
2036 static std::string str() { return "-"; }
2040 struct OpString<std::multiplies<result_t> >
2042 static std::string str() { return "*"; }
2046 struct OpString<std::divides<result_t> >
2048 static std::string str() { return "/"; }
2052 struct OpString<std::modulus<result_t> >
2054 static std::string str() { return "%"; }
2058 struct OpString<std::negate<result_t> >
2060 static std::string str() { return "-"; }
2064 class UnaryNode : public Node
2068 mutable rvec_t result;
2071 UnaryNode(NodePtr p) : l(p) {}
2073 const rvec_t &val() const {
2074 const rvec_t &lvec = l->val();
2075 int size = lvec.size();
2079 result.resize(size);
2081 for (int i = 0; i < size; ++i)
2082 result[i] = op(lvec[i]);
2087 result_t total() const {
2089 return op(l->total());
2092 virtual size_t size() const { return l->size(); }
2094 * Return true if child of node is binned.
2095 *@return True if child of node is binned.
2097 virtual bool binned() const { return l->binned(); }
2099 virtual std::string str() const
2101 return OpString<Op>::str() + l->str();
2106 class BinaryNode : public Node
2111 mutable rvec_t result;
2114 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
2116 const rvec_t &val() const {
2118 const rvec_t &lvec = l->val();
2119 const rvec_t &rvec = r->val();
2121 assert(lvec.size() > 0 && rvec.size() > 0);
2123 if (lvec.size() == 1 && rvec.size() == 1) {
2125 result[0] = op(lvec[0], rvec[0]);
2126 } else if (lvec.size() == 1) {
2127 int size = rvec.size();
2128 result.resize(size);
2129 for (int i = 0; i < size; ++i)
2130 result[i] = op(lvec[0], rvec[i]);
2131 } else if (rvec.size() == 1) {
2132 int size = lvec.size();
2133 result.resize(size);
2134 for (int i = 0; i < size; ++i)
2135 result[i] = op(lvec[i], rvec[0]);
2136 } else if (rvec.size() == lvec.size()) {
2137 int size = rvec.size();
2138 result.resize(size);
2139 for (int i = 0; i < size; ++i)
2140 result[i] = op(lvec[i], rvec[i]);
2146 result_t total() const {
2148 return op(l->total(), r->total());
2151 virtual size_t size() const {
2159 assert(ls == rs && "Node vector sizes are not equal");
2164 * Return true if any children of node are binned
2165 *@return True if either child of node is binned.
2167 virtual bool binned() const { return (l->binned() || r->binned()); }
2169 virtual std::string str() const
2171 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2176 class SumNode : public Node
2180 mutable rvec_t result;
2183 SumNode(NodePtr p) : l(p), result(1) {}
2185 const rvec_t &val() const {
2186 const rvec_t &lvec = l->val();
2187 int size = lvec.size();
2193 for (int i = 0; i < size; ++i)
2194 result[0] = op(result[0], lvec[i]);
2199 result_t total() const {
2200 const rvec_t &lvec = l->val();
2201 int size = lvec.size();
2204 result_t result = 0.0;
2207 for (int i = 0; i < size; ++i)
2208 result = op(result, lvec[i]);
2213 virtual size_t size() const { return 1; }
2215 * Return true if child of node is binned.
2216 *@return True if child of node is binned.
2218 virtual bool binned() const { return l->binned(); }
2220 virtual std::string str() const
2222 return csprintf("total(%s)", l->str());
2226 //////////////////////////////////////////////////////////////////////
2228 // Binning Interface
2230 //////////////////////////////////////////////////////////////////////
2234 friend class MainBin::BinBase;
2242 off_t size() const { return memsize; }
2243 char *memory(off_t off);
2246 static MainBin *&curBin()
2248 static MainBin *current = NULL;
2252 static void setCurBin(MainBin *bin) { curBin() = bin; }
2253 static MainBin *current() { assert(curBin()); return curBin(); }
2255 static off_t &offset()
2257 static off_t offset = 0;
2261 static off_t new_offset(size_t size)
2263 size_t mask = sizeof(u_int64_t) - 1;
2264 off_t off = offset();
2266 // That one is for the last trailing flags byte.
2267 offset() += (size + 1 + mask) & ~mask;
2272 MainBin(const std::string &name);
2293 BinBase() : offset(-1) {}
2294 void allocate(size_t size)
2296 offset = new_offset(size);
2300 assert(offset != -1);
2301 return current()->memory(offset);
2305 template <class Storage>
2306 class Bin : public BinBase
2309 typedef typename Storage::Params Params;
2312 enum { binned = true };
2313 Bin() { allocate(sizeof(Storage)); }
2314 bool initialized() const { return true; }
2315 void init(Params ¶ms) { }
2317 int size() const { return 1; }
2320 data(Params ¶ms)
2322 assert(initialized());
2323 char *ptr = access();
2324 char *flags = ptr + sizeof(Storage);
2325 if (!(*flags & 0x1)) {
2327 new (ptr) Storage(params);
2329 return reinterpret_cast<Storage *>(ptr);
2335 char *ptr = access();
2336 char *flags = ptr + size() * sizeof(Storage);
2337 if (!(*flags & 0x1))
2340 Storage *s = reinterpret_cast<Storage *>(ptr);
2345 template <class Storage>
2346 class VectorBin : public BinBase
2349 typedef typename Storage::Params Params;
2355 enum { binned = true };
2356 VectorBin() : _size(0) {}
2358 bool initialized() const { return _size > 0; }
2359 void init(int s, Params ¶ms)
2361 assert(!initialized());
2364 allocate(_size * sizeof(Storage));
2367 int size() const { return _size; }
2369 Storage *data(int index, Params ¶ms)
2371 assert(initialized());
2372 assert(index >= 0 && index < size());
2373 char *ptr = access();
2374 char *flags = ptr + size() * sizeof(Storage);
2375 if (!(*flags & 0x1)) {
2377 for (int i = 0; i < size(); ++i)
2378 new (ptr + i * sizeof(Storage)) Storage(params);
2380 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2384 char *ptr = access();
2385 char *flags = ptr + size() * sizeof(Storage);
2386 if (!(*flags & 0x1))
2389 for (int i = 0; i < _size; ++i) {
2390 char *p = ptr + i * sizeof(Storage);
2391 Storage *s = reinterpret_cast<Storage *>(p);
2400 template <class Storage>
2404 typedef typename Storage::Params Params;
2405 enum { binned = false };
2408 char ptr[sizeof(Storage)];
2413 reinterpret_cast<Storage *>(ptr)->~Storage();
2416 bool initialized() const { return true; }
2417 void init(Params ¶ms)
2419 new (ptr) Storage(params);
2421 int size() const{ return 1; }
2422 Storage *data(Params ¶ms)
2424 assert(initialized());
2425 return reinterpret_cast<Storage *>(ptr);
2429 Storage *s = reinterpret_cast<Storage *>(ptr);
2434 template <class Storage>
2438 typedef typename Storage::Params Params;
2439 enum { binned = false };
2446 VectorBin() : ptr(NULL) { }
2452 for (int i = 0; i < _size; ++i) {
2453 char *p = ptr + i * sizeof(Storage);
2454 reinterpret_cast<Storage *>(p)->~Storage();
2459 bool initialized() const { return ptr != NULL; }
2460 void init(int s, Params ¶ms)
2462 assert(s > 0 && "size must be positive!");
2463 assert(!initialized());
2465 ptr = new char[_size * sizeof(Storage)];
2466 for (int i = 0; i < _size; ++i)
2467 new (ptr + i * sizeof(Storage)) Storage(params);
2470 int size() const { return _size; }
2472 Storage *data(int index, Params ¶ms)
2474 assert(initialized());
2475 assert(index >= 0 && index < size());
2476 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2480 for (int i = 0; i < _size; ++i) {
2481 char *p = ptr + i * sizeof(Storage);
2482 Storage *s = reinterpret_cast<Storage *>(p);
2489 //////////////////////////////////////////////////////////////////////
2491 // Visible Statistics Types
2493 //////////////////////////////////////////////////////////////////////
2495 * @defgroup VisibleStats "Statistic Types"
2496 * These are the statistics that are used in the simulator. By default these
2497 * store counters and don't use binning, but are templatized to accept any type
2498 * and any Bin class.
2503 * This is an easy way to assign all your stats to be binned or not
2504 * binned. If the typedef is NoBin, nothing is binned. If it is
2505 * MainBin, then all stats are binned under that Bin.
2508 typedef MainBin DefaultBin;
2510 typedef NoBin DefaultBin;
2514 * This is a simple scalar statistic, like a counter.
2515 * @sa Stat, ScalarBase, StatStor
2517 template <typename T = Counter, class Bin = DefaultBin>
2519 : public Wrap<Scalar<T, Bin>,
2520 ScalarBase<T, StatStor, Bin>,
2524 /** The base implementation. */
2525 typedef ScalarBase<T, StatStor, Bin> Base;
2533 * Sets the stat equal to the given value. Calls the base implementation
2535 * @param v The new value.
2537 template <typename U>
2538 void operator=(const U& v) { Base::operator=(v); }
2542 * A stat that calculates the per cycle average of a value.
2543 * @sa Stat, ScalarBase, AvgStor
2545 template <typename T = Counter, class Bin = DefaultBin>
2547 : public Wrap<Average<T, Bin>,
2548 ScalarBase<T, AvgStor, Bin>,
2552 /** The base implementation. */
2553 typedef ScalarBase<T, AvgStor, Bin> Base;
2561 * Sets the stat equal to the given value. Calls the base implementation
2563 * @param v The new value.
2565 template <typename U>
2566 void operator=(const U& v) { Base::operator=(v); }
2570 * A vector of scalar stats.
2571 * @sa Stat, VectorBase, StatStor
2573 template <typename T = Counter, class Bin = DefaultBin>
2575 : public WrapVec<Vector<T, Bin>,
2576 VectorBase<T, StatStor, Bin>,
2580 /** The base implementation. */
2581 typedef ScalarBase<T, StatStor, Bin> Base;
2584 * Set this vector to have the given size.
2585 * @param size The new size.
2586 * @return A reference to this stat.
2588 Vector &init(size_t size) {
2589 bin.init(size, params);
2597 * A vector of Average stats.
2598 * @sa Stat, VectorBase, AvgStor
2600 template <typename T = Counter, class Bin = DefaultBin>
2602 : public WrapVec<AverageVector<T, Bin>,
2603 VectorBase<T, AvgStor, Bin>,
2608 * Set this vector to have the given size.
2609 * @param size The new size.
2610 * @return A reference to this stat.
2612 AverageVector &init(size_t size) {
2613 bin.init(size, params);
2621 * A 2-Dimensional vecto of scalar stats.
2622 * @sa Stat, Vector2dBase, StatStor
2624 template <typename T = Counter, class Bin = DefaultBin>
2626 : public WrapVec2d<Vector2d<T, Bin>,
2627 Vector2dBase<T, StatStor, Bin>,
2631 Vector2d &init(size_t _x, size_t _y) {
2632 statData()->x = x = _x;
2633 statData()->y = y = _y;
2634 bin.init(x * y, params);
2642 * A simple distribution stat.
2643 * @sa Stat, DistBase, DistStor
2645 template <typename T = Counter, class Bin = DefaultBin>
2647 : public Wrap<Distribution<T, Bin>,
2648 DistBase<T, DistStor, Bin>,
2652 /** Base implementation. */
2653 typedef DistBase<T, DistStor, Bin> Base;
2654 /** The Parameter type. */
2655 typedef typename DistStor<T>::Params Params;
2659 * Set the parameters of this distribution. @sa DistStor::Params
2660 * @param min The minimum value of the distribution.
2661 * @param max The maximum value of the distribution.
2662 * @param bkt The number of values in each bucket.
2663 * @return A reference to this distribution.
2665 Distribution &init(T min, T max, int bkt) {
2668 params.bucket_size = bkt;
2669 params.size = (max - min) / bkt + 1;
2678 * Calculates the mean and variance of all the samples.
2679 * @sa Stat, DistBase, FancyStor
2681 template <typename T = Counter, class Bin = DefaultBin>
2682 class StandardDeviation
2683 : public Wrap<StandardDeviation<T, Bin>,
2684 DistBase<T, FancyStor, Bin>,
2688 /** The base implementation */
2689 typedef DistBase<T, DistStor, Bin> Base;
2690 /** The parameter type. */
2691 typedef typename DistStor<T>::Params Params;
2695 * Construct and initialize this distribution.
2697 StandardDeviation() {
2704 * Calculates the per cycle mean and variance of the samples.
2705 * @sa Stat, DistBase, AvgFancy
2707 template <typename T = Counter, class Bin = DefaultBin>
2708 class AverageDeviation
2709 : public Wrap<AverageDeviation<T, Bin>,
2710 DistBase<T, AvgFancy, Bin>,
2714 /** The base implementation */
2715 typedef DistBase<T, DistStor, Bin> Base;
2716 /** The parameter type. */
2717 typedef typename DistStor<T>::Params Params;
2721 * Construct and initialize this distribution.
2731 * A vector of distributions.
2732 * @sa Stat, VectorDistBase, DistStor
2734 template <typename T = Counter, class Bin = DefaultBin>
2735 class VectorDistribution
2736 : public WrapVec<VectorDistribution<T, Bin>,
2737 VectorDistBase<T, DistStor, Bin>,
2741 /** The base implementation */
2742 typedef VectorDistBase<T, DistStor, Bin> Base;
2743 /** The parameter type. */
2744 typedef typename DistStor<T>::Params Params;
2748 * Initialize storage and parameters for this distribution.
2749 * @param size The size of the vector (the number of distributions).
2750 * @param min The minimum value of the distribution.
2751 * @param max The maximum value of the distribution.
2752 * @param bkt The number of values in each bucket.
2753 * @return A reference to this distribution.
2755 VectorDistribution &init(int size, T min, T max, int bkt) {
2758 params.bucket_size = bkt;
2759 params.size = (max - min) / bkt + 1;
2760 bin.init(size, params);
2768 * This is a vector of StandardDeviation stats.
2769 * @sa Stat, VectorDistBase, FancyStor
2771 template <typename T = Counter, class Bin = DefaultBin>
2772 class VectorStandardDeviation
2773 : public WrapVec<VectorStandardDeviation<T, Bin>,
2774 VectorDistBase<T, FancyStor, Bin>,
2778 /** The base implementation */
2779 typedef VectorDistBase<T, FancyStor, Bin> Base;
2780 /** The parameter type. */
2781 typedef typename DistStor<T>::Params Params;
2785 * Initialize storage for this distribution.
2786 * @param size The size of the vector.
2787 * @return A reference to this distribution.
2789 VectorStandardDeviation &init(int size) {
2790 bin.init(size, params);
2798 * This is a vector of AverageDeviation stats.
2799 * @sa Stat, VectorDistBase, AvgFancy
2801 template <typename T = Counter, class Bin = DefaultBin>
2802 class VectorAverageDeviation
2803 : public WrapVec<VectorAverageDeviation<T, Bin>,
2804 VectorDistBase<T, AvgFancy, Bin>,
2808 /** The base implementation */
2809 typedef VectorDistBase<T, AvgFancy, Bin> Base;
2810 /** The parameter type. */
2811 typedef typename DistStor<T>::Params Params;
2815 * Initialize storage for this distribution.
2816 * @param size The size of the vector.
2817 * @return A reference to this distribution.
2819 VectorAverageDeviation &init(int size) {
2820 bin.init(size, params);
2828 * A formula for statistics that is calculated when printed. A formula is
2829 * stored as a tree of Nodes that represent the equation to calculate.
2830 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2832 class FormulaBase : public DataAccess
2835 /** The root of the tree which represents the Formula */
2841 * Return the result of the Fomula in a vector. If there were no Vector
2842 * components to the Formula, then the vector is size 1. If there were,
2843 * like x/y with x being a vector of size 3, then the result returned will
2844 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2845 * @return The result vector.
2847 void val(rvec_t &vec) const;
2850 * Return the total Formula result. If there is a Vector
2851 * component to this Formula, then this is the result of the
2852 * Formula if the formula is applied after summing all the
2853 * components of the Vector. For example, if Formula is x/y where
2854 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2855 * there is no Vector component, total() returns the same value as
2856 * the first entry in the rvec_t val() returns.
2857 * @return The total of the result vector.
2859 result_t total() const;
2862 * Return the number of elements in the tree.
2864 size_t size() const;
2867 * Return true if Formula is binned. i.e. any of its children
2869 * @return True if Formula is binned.
2871 bool binned() const;
2873 bool check() const { return true; }
2876 * Formulas don't need to be reset
2888 void update(StatData *);
2890 std::string str() const;
2893 class FormulaDataBase : public VectorDataBase
2896 virtual std::string str() const = 0;
2897 virtual bool check() const { return true; }
2898 virtual void python(Python &py) const;
2902 class FormulaData : public FormulaDataBase
2909 FormulaData(T &stat) : s(stat) {}
2911 virtual bool binned() const { return s.binned(); }
2912 virtual bool zero() const { return s.zero(); }
2913 virtual void reset() { s.reset(); }
2915 virtual size_t size() const { return s.size(); }
2916 virtual const rvec_t &val() const
2921 virtual result_t total() const { return s.total(); }
2922 virtual void update()
2924 VectorDataBase::update();
2927 virtual std::string str() const { return s.str(); }
2932 : public WrapVec<Formula,
2938 * Create and initialize thie formula, and register it with the database.
2943 * Create a formula with the given root node, register it with the
2945 * @param r The root of the expression tree.
2950 * Set an unitialized Formula to the given root.
2951 * @param r The root of the expression tree.
2952 * @return a reference to this formula.
2954 const Formula &operator=(Temp r);
2957 * Add the given tree to the existing one.
2958 * @param r The root of the expression tree.
2959 * @return a reference to this formula.
2961 const Formula &operator+=(Temp r);
2964 class FormulaNode : public Node
2967 const Formula &formula;
2971 FormulaNode(const Formula &f) : formula(f) {}
2973 virtual size_t size() const { return formula.size(); }
2974 virtual const rvec_t &val() const { formula.val(vec); return vec; }
2975 virtual result_t total() const { return formula.total(); }
2976 virtual bool binned() const { return formula.binned(); }
2978 virtual std::string str() const { return formula.str(); }
2982 * Helper class to construct formula node trees.
2988 * Pointer to a Node object.
2994 * Copy the given pointer to this class.
2995 * @param n A pointer to a Node object to copy.
2997 Temp(NodePtr n) : node(n) { }
3000 * Return the node pointer.
3001 * @return the node pointer.
3003 operator NodePtr() { return node;}
3007 * Create a new ScalarStatNode.
3008 * @param s The ScalarStat to place in a node.
3010 template <typename T, class Bin>
3011 Temp(const Scalar<T, Bin> &s)
3012 : node(new ScalarStatNode(s.statData())) { }
3015 * Create a new ScalarStatNode.
3016 * @param s The ScalarStat to place in a node.
3018 template <typename T, class Bin>
3019 Temp(const Average<T, Bin> &s)
3020 : node(new ScalarStatNode(s.statData())) { }
3023 * Create a new VectorStatNode.
3024 * @param s The VectorStat to place in a node.
3026 template <typename T, class Bin>
3027 Temp(const Vector<T, Bin> &s)
3028 : node(new VectorStatNode(s.statData())) { }
3033 Temp(const Formula &f)
3034 : node(new FormulaNode(f)) { }
3037 * Create a new ScalarProxyNode.
3038 * @param p The ScalarProxy to place in a node.
3040 template <typename T, template <typename T> class Storage, class Bin>
3041 Temp(const ScalarProxy<T, Storage, Bin> &p)
3042 : node(new ScalarProxyNode<T, Storage, Bin>(p)) { }
3045 * Create a ConstNode
3046 * @param value The value of the const node.
3048 Temp(signed char value)
3049 : node(new ConstNode<signed char>(value)) {}
3052 * Create a ConstNode
3053 * @param value The value of the const node.
3055 Temp(unsigned char value)
3056 : node(new ConstNode<unsigned char>(value)) {}
3059 * Create a ConstNode
3060 * @param value The value of the const node.
3062 Temp(signed short value)
3063 : node(new ConstNode<signed short>(value)) {}
3066 * Create a ConstNode
3067 * @param value The value of the const node.
3069 Temp(unsigned short value)
3070 : node(new ConstNode<unsigned short>(value)) {}
3073 * Create a ConstNode
3074 * @param value The value of the const node.
3076 Temp(signed int value)
3077 : node(new ConstNode<signed int>(value)) {}
3080 * Create a ConstNode
3081 * @param value The value of the const node.
3083 Temp(unsigned int value)
3084 : node(new ConstNode<unsigned int>(value)) {}
3087 * Create a ConstNode
3088 * @param value The value of the const node.
3090 Temp(signed long value)
3091 : node(new ConstNode<signed long>(value)) {}
3094 * Create a ConstNode
3095 * @param value The value of the const node.
3097 Temp(unsigned long value)
3098 : node(new ConstNode<unsigned long>(value)) {}
3101 * Create a ConstNode
3102 * @param value The value of the const node.
3104 Temp(signed long long value)
3105 : node(new ConstNode<signed long long>(value)) {}
3108 * Create a ConstNode
3109 * @param value The value of the const node.
3111 Temp(unsigned long long value)
3112 : node(new ConstNode<unsigned long long>(value)) {}
3115 * Create a ConstNode
3116 * @param value The value of the const node.
3119 : node(new ConstNode<float>(value)) {}
3122 * Create a ConstNode
3123 * @param value The value of the const node.
3126 : node(new ConstNode<double>(value)) {}
3135 void dump(std::ostream &stream, const std::string &name = "",
3136 DisplayMode mode = DefaultMode);
3138 void registerResetCallback(Callback *cb);
3141 operator+(Temp l, Temp r)
3143 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
3147 operator-(Temp l, Temp r)
3149 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
3153 operator*(Temp l, Temp r)
3155 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
3159 operator/(Temp l, Temp r)
3161 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
3165 operator%(Temp l, Temp r)
3167 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
3173 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
3176 template <typename T>
3180 return NodePtr(new ConstNode<T>(val));
3183 template <typename T>
3187 return NodePtr(new FunctorNode<T>(val));
3190 template <typename T>
3194 return NodePtr(new ScalarNode<T>(val));
3200 return NodePtr(new SumNode<std::plus<result_t> >(val));
3202 extern bool PrintDescriptions;
3204 } // namespace statistics
3206 #endif // __STATISTICS_HH__