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"
65 /** Define Not a number. */
67 /** Need to define __nan() */
74 /** The current simulated cycle. */
77 /* A namespace for all of the Statistics */
78 namespace Statistics {
79 /** All results are doubles. */
80 typedef double result_t;
81 /** A vector to hold results. */
82 typedef std::vector<result_t> rvec_t;
85 * Define the storage for format flags.
86 * @todo Can probably shrink this.
88 typedef u_int32_t StatFlags;
90 /** Nothing extra to print. */
91 const StatFlags none = 0x00000000;
92 /** This Stat is Initialized */
93 const StatFlags init = 0x00000001;
94 /** Print this stat. */
95 const StatFlags print = 0x00000002;
96 /** Print the total. */
97 const StatFlags total = 0x00000010;
98 /** Print the percent of the total that this entry represents. */
99 const StatFlags pdf = 0x00000020;
100 /** Print the cumulative percentage of total upto this entry. */
101 const StatFlags cdf = 0x00000040;
102 /** Print the distribution. */
103 const StatFlags dist = 0x00000080;
104 /** Don't print if this is zero. */
105 const StatFlags nozero = 0x00000100;
106 /** Don't print if this is NAN */
107 const StatFlags nonan = 0x00000200;
108 /** Used for SS compatability. */
109 const StatFlags __substat = 0x80000000;
111 /** Mask of flags that can't be set directly */
112 const StatFlags __reserved = init | print | __substat;
120 extern DisplayMode DefaultMode;
122 /* Contains the statistic implementation details */
123 //////////////////////////////////////////////////////////////////////
125 // Statistics Framework Base classes
127 //////////////////////////////////////////////////////////////////////
130 /** The name of the stat. */
132 /** The description of the stat. */
134 /** The formatting flags. */
136 /** The display precision. */
140 /** A pointer to a prerequisite Stat. */
141 const StatData *prereq;
147 * @return true if the stat is binned.
149 virtual bool binned() const = 0;
152 * Print this stat to the given ostream.
153 * @param stream The stream to print to.
155 virtual void display(std::ostream &stream, DisplayMode mode) const = 0;
156 virtual void python(Python &py) const = 0;
157 bool dodisplay() const { return !prereq || !prereq->zero(); }
160 * Reset the corresponding stat to the default state.
162 virtual void reset() = 0;
165 * @return true if this stat has a value and satisfies its
166 * requirement as a prereq
168 virtual bool zero() const = 0;
171 * Check that this stat has been set up properly and is ready for
173 * @return true for success
175 virtual bool check() const = 0;
176 bool baseCheck() const;
179 * Checks if the first stat's name is alphabetically less than the second.
180 * This function breaks names up at periods and considers each subname
182 * @param stat1 The first stat.
183 * @param stat2 The second stat.
184 * @return stat1's name is alphabetically before stat2's
186 static bool less(StatData *stat1, StatData *stat2);
193 struct ScalarDataBase : public StatData
195 virtual result_t val() const = 0;
196 virtual result_t total() const = 0;
198 virtual void display(std::ostream &stream, DisplayMode mode) const;
199 virtual void python(Python &py) const;
203 class ScalarData : public ScalarDataBase
209 ScalarData(T &stat) : s(stat) {}
211 virtual bool binned() const { return s.binned(); }
212 virtual bool check() const { return s.check(); }
213 virtual result_t val() const { return s.val(); }
214 virtual result_t total() const { return s.total(); }
215 virtual void reset() { s.reset(); }
216 virtual bool zero() const { return s.zero(); }
219 struct VectorDataBase : public StatData
221 /** Names and descriptions of subfields. */
222 mutable std::vector<std::string> subnames;
223 mutable std::vector<std::string> subdescs;
225 virtual void display(std::ostream &stream, DisplayMode mode) const;
226 virtual void python(Python &py) const;
228 virtual size_t size() const = 0;
229 virtual const rvec_t &val() const = 0;
230 virtual result_t total() const = 0;
231 virtual void update()
233 if (!subnames.empty()) {
235 if (subnames.size() < s)
238 if (subdescs.size() < s)
245 class VectorData : public VectorDataBase
252 VectorData(T &stat) : s(stat) {}
254 virtual bool binned() const { return s.binned(); }
255 virtual bool check() const { return s.check(); }
256 virtual bool zero() const { return s.zero(); }
257 virtual void reset() { s.reset(); }
259 virtual size_t size() const { return s.size(); }
260 virtual const rvec_t &val() const
265 virtual result_t total() const { return s.total(); }
266 virtual void update()
268 VectorDataBase::update();
290 void python(Python &py, const std::string &name) const;
293 struct DistDataBase : public StatData
295 /** Local storage for the entry values, used for printing. */
298 virtual void display(std::ostream &stream, DisplayMode mode) const;
299 virtual void python(Python &py) const;
300 virtual void update() = 0;
304 class DistData : public DistDataBase
310 DistData(T &stat) : s(stat) {}
312 virtual bool binned() const { return s.binned(); }
313 virtual bool check() const { return s.check(); }
314 virtual void reset() { s.reset(); }
315 virtual bool zero() const { return s.zero(); }
316 virtual void update() { return s.update(this); }
319 struct VectorDistDataBase : public StatData
321 std::vector<DistDataData> data;
323 /** Names and descriptions of subfields. */
324 mutable std::vector<std::string> subnames;
325 mutable std::vector<std::string> subdescs;
327 /** Local storage for the entry values, used for printing. */
330 virtual size_t size() const = 0;
331 virtual void display(std::ostream &stream, DisplayMode mode) const;
332 virtual void python(Python &py) const;
333 virtual void update()
336 if (subnames.size() < s)
339 if (subdescs.size() < s)
345 class VectorDistData : public VectorDistDataBase
349 typedef typename T::bin_t bin_t;
352 VectorDistData(T &stat) : s(stat) {}
354 virtual bool binned() const { return bin_t::binned; }
355 virtual bool check() const { return s.check(); }
356 virtual void reset() { s.reset(); }
357 virtual size_t size() const { return s.size(); }
358 virtual bool zero() const { return s.zero(); }
359 virtual void update()
361 VectorDistDataBase::update();
362 return s.update(this);
366 struct Vector2dDataBase : public StatData
368 /** Names and descriptions of subfields. */
369 std::vector<std::string> subnames;
370 std::vector<std::string> subdescs;
371 std::vector<std::string> y_subnames;
373 /** Local storage for the entry values, used for printing. */
378 virtual void display(std::ostream &stream, DisplayMode mode) const;
379 virtual void python(Python &py) const;
380 virtual void update()
382 if (subnames.size() < x)
388 class Vector2dData : public Vector2dDataBase
392 typedef typename T::bin_t bin_t;
395 Vector2dData(T &stat) : s(stat) {}
397 virtual bool binned() const { return bin_t::binned; }
398 virtual bool check() const { return s.check(); }
399 virtual void reset() { s.reset(); }
400 virtual bool zero() const { return s.zero(); }
401 virtual void update()
403 Vector2dDataBase::update();
412 StatData *find() const;
413 void map(StatData *data);
415 StatData *statData();
416 const StatData *statData() const;
422 template <class Parent, class Child, template <class> class Data>
423 class Wrap : public Child
426 Parent &self() { return *reinterpret_cast<Parent *>(this); }
429 Data<Child> *statData()
431 StatData *__data = DataAccess::statData();
432 Data<Child> *ptr = dynamic_cast<Data<Child> *>(__data);
438 const Data<Child> *statData() const
440 const StatData *__data = DataAccess::statData();
441 const Data<Child> *ptr = dynamic_cast<const Data<Child> *>(__data);
449 map(new Data<Child>(*this));
453 * Set the name and marks this stat to print at the end of simulation.
454 * @param name The new name.
455 * @return A reference to this stat.
457 Parent &name(const std::string &_name)
459 Data<Child> *data = statData();
466 * Set the description and marks this stat to print at the end of
468 * @param desc The new description.
469 * @return A reference to this stat.
471 Parent &desc(const std::string &_desc)
473 statData()->desc = _desc;
478 * Set the precision and marks this stat to print at the end of simulation.
479 * @param p The new precision
480 * @return A reference to this stat.
482 Parent &precision(int _precision)
484 statData()->precision = _precision;
489 * Set the flags and marks this stat to print at the end of simulation.
490 * @param f The new flags.
491 * @return A reference to this stat.
493 Parent &flags(StatFlags _flags)
495 statData()->flags |= _flags;
500 * Set the prerequisite stat and marks this stat to print at the end of
502 * @param prereq The prerequisite stat.
503 * @return A reference to this stat.
506 Parent &prereq(const T &prereq)
508 statData()->prereq = prereq.statData();
513 template <class Parent, class Child, template <class Child> class Data>
514 class WrapVec : public Wrap<Parent, Child, Data>
517 // The following functions are specific to vectors. If you use them
518 // in a non vector context, you will get a nice compiler error!
521 * Set the subfield name for the given index, and marks this stat to print
522 * at the end of simulation.
523 * @param index The subfield index.
524 * @param name The new name of the subfield.
525 * @return A reference to this stat.
527 Parent &subname(int index, const std::string &name)
529 std::vector<std::string> &subn = statData()->subnames;
530 if (subn.size() <= index)
531 subn.resize(index + 1);
537 * Set the subfield description for the given index and marks this stat to
538 * print at the end of simulation.
539 * @param index The subfield index.
540 * @param desc The new description of the subfield
541 * @return A reference to this stat.
543 Parent &subdesc(int index, const std::string &desc)
545 std::vector<std::string> &subd = statData()->subdescs;
546 if (subd.size() <= index)
547 subd.resize(index + 1);
555 template <class Parent, class Child, template <class Child> class Data>
556 class WrapVec2d : public WrapVec<Parent, Child, Data>
560 * @warning This makes the assumption that if you're gonna subnames a 2d
561 * vector, you're subnaming across all y
563 Parent &ysubnames(const char **names)
565 Data<Child> *data = statData();
566 data->y_subnames.resize(y);
567 for (int i = 0; i < y; ++i)
568 data->y_subnames[i] = names[i];
571 Parent &ysubname(int index, const std::string subname)
573 Data<Child> *data = statData();
575 data->y_subnames.resize(y);
576 data->y_subnames[i] = subname.c_str();
581 //////////////////////////////////////////////////////////////////////
585 //////////////////////////////////////////////////////////////////////
588 * Templatized storage and interface for a simple scalar stat.
590 template <typename T>
594 /** The paramaters for this storage type, none for a scalar. */
598 /** The statistic value. */
608 * Builds this storage element and calls the base constructor of the
611 StatStor(const Params &) : data(Null()) {}
614 * The the stat to the given value.
615 * @param val The new value.
616 * @param p The paramters of this storage type.
618 void set(T val, const Params &p) { data = val; }
620 * Increment the stat by the given value.
621 * @param val The new value.
622 * @param p The paramters of this storage type.
624 void inc(T val, const Params &p) { data += val; }
626 * Decrement the stat by the given value.
627 * @param val The new value.
628 * @param p The paramters of this storage type.
630 void dec(T val, const Params &p) { data -= val; }
632 * Return the value of this stat as a result type.
633 * @param p The parameters of this storage type.
634 * @return The value of this stat.
636 result_t val(const Params &p) const { return (result_t)data; }
638 * Return the value of this stat as its base type.
639 * @param p The params of this storage type.
640 * @return The value of this stat.
642 T value(const Params &p) const { return data; }
644 * Reset stat value to default
646 void reset() { data = Null(); }
649 * @return true if zero value
651 bool zero() const { return data == Null(); }
655 * Templatized storage and interface to a per-cycle average stat. This keeps
656 * a current count and updates a total (count * cycles) when this count
657 * changes. This allows the quick calculation of a per cycle count of the item
658 * being watched. This is good for keeping track of residencies in structures
659 * among other things.
660 * @todo add lateny to the stat and fix binning.
662 template <typename T>
666 /** The paramaters for this storage type */
670 * The current count. We stash this here because the current
671 * value is not a binned value.
677 /** The total count for all cycles. */
678 mutable result_t total;
679 /** The cycle that current last changed. */
684 * Build and initializes this stat storage.
686 AvgStor(Params &p) : total(0), last(0) { p.current = T(); }
689 * Set the current count to the one provided, update the total and last
691 * @param val The new count.
692 * @param p The parameters for this storage.
694 void set(T val, Params &p) {
695 total += p.current * (curTick - last);
701 * Increment the current count by the provided value, calls set.
702 * @param val The amount to increment.
703 * @param p The parameters for this storage.
705 void inc(T val, Params &p) { set(p.current + val, p); }
708 * Deccrement the current count by the provided value, calls set.
709 * @param val The amount to decrement.
710 * @param p The parameters for this storage.
712 void dec(T val, Params &p) { set(p.current - val, p); }
715 * Return the current average.
716 * @param p The parameters for this storage.
717 * @return The current average.
719 result_t val(const Params &p) const {
720 total += p.current * (curTick - last);
722 return (result_t)(total + p.current) / (result_t)(curTick + 1);
726 * Return the current count.
727 * @param p The parameters for this storage.
728 * @return The current count.
730 T value(const Params &p) const { return p.current; }
733 * Reset stat value to default
742 * @return true if zero value
744 bool zero() const { return total == 0.0; }
748 * Implementation of a scalar stat. The type of stat is determined by the
749 * Storage template. The storage for this stat is held within the Bin class.
750 * This allows for breaking down statistics across multiple bins easily.
752 template <typename T, template <typename T> class Storage, class Bin>
753 class ScalarBase : public DataAccess
756 /** Define the type of the storage class. */
757 typedef Storage<T> storage_t;
758 /** Define the params of the storage class. */
759 typedef typename storage_t::Params params_t;
760 /** Define the bin type. */
761 typedef typename Bin::Bin<storage_t> bin_t;
764 /** The bin of this stat. */
766 /** The parameters for this stat. */
771 * Retrieve the storage from the bin.
772 * @return The storage object for this stat.
774 storage_t *data() { return bin.data(params); }
776 * Retrieve a const pointer to the storage from the bin.
777 * @return A const pointer to the storage object for this stat.
779 const storage_t *data() const
781 bin_t *_bin = const_cast<bin_t *>(&bin);
782 params_t *_params = const_cast<params_t *>(¶ms);
783 return _bin->data(*_params);
788 * Copy constructor, copies are not allowed.
790 ScalarBase(const ScalarBase &stat);
794 const ScalarBase &operator=(const ScalarBase &);
798 * Return the current value of this stat as its base type.
799 * @return The current value.
801 T value() const { return data()->value(params); }
805 * Create and initialize this stat, register it with the database.
813 // Common operators for stats
815 * Increment the stat by 1. This calls the associated storage object inc
818 void operator++() { data()->inc(1, params); }
820 * Decrement the stat by 1. This calls the associated storage object dec
823 void operator--() { data()->dec(1, params); }
825 /** Increment the stat by 1. */
826 void operator++(int) { ++*this; }
827 /** Decrement the stat by 1. */
828 void operator--(int) { --*this; }
831 * Set the data value to the given value. This calls the associated storage
832 * object set function.
833 * @param v The new value.
835 template <typename U>
836 void operator=(const U &v) { data()->set(v, params); }
839 * Increment the stat by the given value. This calls the associated
840 * storage object inc function.
841 * @param v The value to add.
843 template <typename U>
844 void operator+=(const U &v) { data()->inc(v, params); }
847 * Decrement the stat by the given value. This calls the associated
848 * storage object dec function.
849 * @param v The value to substract.
851 template <typename U>
852 void operator-=(const U &v) { data()->dec(v, params); }
855 * Return the number of elements, always 1 for a scalar.
858 size_t size() const { return 1; }
860 * Return true if stat is binned.
861 *@return True is stat is binned.
863 bool binned() const { return bin_t::binned; }
865 bool check() const { return bin.initialized(); }
868 * Reset stat value to default
870 void reset() { bin.reset(); }
872 result_t val() { return data()->val(params); }
874 result_t total() { return val(); }
876 bool zero() { return val() == 0.0; }
879 //////////////////////////////////////////////////////////////////////
883 //////////////////////////////////////////////////////////////////////
884 template <typename T, template <typename T> class Storage, class Bin>
888 * Implementation of a vector of stats. The type of stat is determined by the
889 * Storage class. @sa ScalarBase
891 template <typename T, template <typename T> class Storage, class Bin>
892 class VectorBase : public DataAccess
895 /** Define the type of the storage class. */
896 typedef Storage<T> storage_t;
897 /** Define the params of the storage class. */
898 typedef typename storage_t::Params params_t;
899 /** Define the bin type. */
900 typedef typename Bin::VectorBin<storage_t> bin_t;
903 /** The bin of this stat. */
905 /** The parameters for this stat. */
910 * Retrieve the storage from the bin for the given index.
911 * @param index The vector index to access.
912 * @return The storage object at the given index.
914 storage_t *data(int index) { return bin.data(index, params); }
916 * Retrieve a const pointer to the storage from the bin
917 * for the given index.
918 * @param index The vector index to access.
919 * @return A const pointer to the storage object at the given index.
921 const storage_t *data(int index) const
923 bin_t *_bin = const_cast<bin_t *>(&bin);
924 params_t *_params = const_cast<params_t *>(¶ms);
925 return _bin->data(index, *_params);
929 // Copying stats is not allowed
930 /** Copying stats isn't allowed. */
931 VectorBase(const VectorBase &stat);
932 /** Copying stats isn't allowed. */
933 const VectorBase &operator=(const VectorBase &);
937 * Copy the values to a local vector and return a reference to it.
938 * @return A reference to a vector of the stat values.
940 void val(rvec_t &vec) const
943 for (int i = 0; i < size(); ++i)
944 vec[i] = data(i)->val(params);
948 * @return True is stat is binned.
950 bool binned() const { return bin_t::binned; }
953 * Return a total of all entries in this vector.
954 * @return The total of all vector entries.
956 result_t total() const {
957 result_t total = 0.0;
958 for (int i = 0; i < size(); ++i)
959 total += data(i)->val(params);
964 * @return the number of elements in this vector.
966 size_t size() const { return bin.size(); }
970 for (int i = 0; i < size(); ++i)
976 bool check() const { return bin.initialized(); }
977 void reset() { bin.reset(); }
982 /** Friend this class with the associated scalar proxy. */
983 friend class ScalarProxy<T, Storage, Bin>;
986 * Return a reference (ScalarProxy) to the stat at the given index.
987 * @param index The vector index to access.
988 * @return A reference of the stat.
990 ScalarProxy<T, Storage, Bin> operator[](int index);
992 void update(StatData *data) {}
995 const StatData * getStatData(const void *stat);
998 * A proxy class to access the stat at a given index in a VectorBase stat.
999 * Behaves like a ScalarBase.
1001 template <typename T, template <typename T> class Storage, class Bin>
1005 /** Define the type of the storage class. */
1006 typedef Storage<T> storage_t;
1007 /** Define the params of the storage class. */
1008 typedef typename storage_t::Params params_t;
1009 /** Define the bin type. */
1010 typedef typename Bin::VectorBin<storage_t> bin_t;
1013 /** Pointer to the bin in the parent VectorBase. */
1015 /** Pointer to the params in the parent VectorBase. */
1017 /** The index to access in the parent VectorBase. */
1019 /** Keep a pointer to the original stat so was can get data */
1024 * Retrieve the storage from the bin.
1025 * @return The storage from the bin for this stat.
1027 storage_t *data() { return bin->data(index, *params); }
1029 * Retrieve a const pointer to the storage from the bin.
1030 * @return A const pointer to the storage for this stat.
1032 const storage_t *data() const
1034 bin_t *_bin = const_cast<bin_t *>(bin);
1035 params_t *_params = const_cast<params_t *>(params);
1036 return _bin->data(index, *_params);
1041 * Return the current value of this statas a result type.
1042 * @return The current value.
1044 result_t val() const { return data()->val(*params); }
1046 * Return the current value of this stat as its base type.
1047 * @return The current value.
1049 T value() const { return data()->value(*params); }
1053 * Create and initialize this proxy, do not register it with the database.
1054 * @param b The bin to use.
1055 * @param p The params to use.
1056 * @param i The index to access.
1058 ScalarProxy(bin_t &b, params_t &p, int i, void *s)
1059 : bin(&b), params(&p), index(i), stat(s) {}
1061 * Create a copy of the provided ScalarProxy.
1062 * @param sp The proxy to copy.
1064 ScalarProxy(const ScalarProxy &sp)
1065 : bin(sp.bin), params(sp.params), index(sp.index), stat(sp.stat) {}
1067 * Set this proxy equal to the provided one.
1068 * @param sp The proxy to copy.
1069 * @return A reference to this proxy.
1071 const ScalarProxy &operator=(const ScalarProxy &sp) {
1080 // Common operators for stats
1082 * Increment the stat by 1. This calls the associated storage object inc
1085 void operator++() { data()->inc(1, *params); }
1087 * Decrement the stat by 1. This calls the associated storage object dec
1090 void operator--() { data()->dec(1, *params); }
1092 /** Increment the stat by 1. */
1093 void operator++(int) { ++*this; }
1094 /** Decrement the stat by 1. */
1095 void operator--(int) { --*this; }
1098 * Set the data value to the given value. This calls the associated storage
1099 * object set function.
1100 * @param v The new value.
1102 template <typename U>
1103 void operator=(const U &v) { data()->set(v, *params); }
1106 * Increment the stat by the given value. This calls the associated
1107 * storage object inc function.
1108 * @param v The value to add.
1110 template <typename U>
1111 void operator+=(const U &v) { data()->inc(v, *params); }
1114 * Decrement the stat by the given value. This calls the associated
1115 * storage object dec function.
1116 * @param v The value to substract.
1118 template <typename U>
1119 void operator-=(const U &v) { data()->dec(v, *params); }
1122 * Return the number of elements, always 1 for a scalar.
1125 size_t size() const { return 1; }
1128 * Return true if stat is binned.
1129 *@return false since Proxies aren't printed/binned
1131 bool binned() const { return false; }
1134 * This stat has no state. Nothing to reset
1139 const StatData *statData() const { return getStatData(stat); }
1140 std::string str() const
1142 return csprintf("%s[%d]", statData()->name, index);
1147 template <typename T, template <typename T> class Storage, class Bin>
1148 inline ScalarProxy<T, Storage, Bin>
1149 VectorBase<T, Storage, Bin>::operator[](int index)
1151 assert (index >= 0 && index < size());
1152 return ScalarProxy<T, Storage, Bin>(bin, params, index, this);
1155 template <typename T, template <typename T> class Storage, class Bin>
1158 template <typename T, template <typename T> class Storage, class Bin>
1159 class Vector2dBase : public DataAccess
1162 typedef Storage<T> storage_t;
1163 typedef typename storage_t::Params params_t;
1164 typedef typename Bin::VectorBin<storage_t> bin_t;
1173 storage_t *data(int index) { return bin.data(index, params); }
1174 const storage_t *data(int index) const
1176 bin_t *_bin = const_cast<bin_t *>(&bin);
1177 params_t *_params = const_cast<params_t *>(¶ms);
1178 return _bin->data(index, *_params);
1182 // Copying stats is not allowed
1183 Vector2dBase(const Vector2dBase &stat);
1184 const Vector2dBase &operator=(const Vector2dBase &);
1189 void update(Vector2dDataBase *data)
1191 int size = this->size();
1192 data->vec.resize(size);
1193 for (int i = 0; i < size; ++i)
1194 data->vec[i] = this->data(i)->val(params);
1197 std::string ysubname(int i) const { return (*y_subnames)[i]; }
1199 friend class VectorProxy<T, Storage, Bin>;
1200 VectorProxy<T, Storage, Bin> operator[](int index);
1202 size_t size() const { return bin.size(); }
1203 bool zero() const { return data(0)->value(params) == 0.0; }
1206 * Reset stat value to default
1208 void reset() { bin.reset(); }
1210 bool check() { return bin.initialized(); }
1213 template <typename T, template <typename T> class Storage, class Bin>
1217 typedef Storage<T> storage_t;
1218 typedef typename storage_t::Params params_t;
1219 typedef typename Bin::VectorBin<storage_t> bin_t;
1229 mutable rvec_t *vec;
1231 storage_t *data(int index) {
1232 assert(index < len);
1233 return bin->data(offset + index, *params);
1236 const storage_t *data(int index) const {
1237 bin_t *_bin = const_cast<bin_t *>(bin);
1238 params_t *_params = const_cast<params_t *>(params);
1239 return _bin->data(offset + index, *_params);
1243 const rvec_t &val() const {
1245 vec->resize(size());
1247 vec = new rvec_t(size());
1249 for (int i = 0; i < size(); ++i)
1250 (*vec)[i] = data(i)->val(*params);
1255 result_t total() const {
1256 result_t total = 0.0;
1257 for (int i = 0; i < size(); ++i)
1258 total += data(i)->val(*params);
1263 VectorProxy(bin_t &b, params_t &p, int o, int l, void *s)
1264 : bin(&b), params(&p), offset(o), len(l), stat(s), vec(NULL)
1268 VectorProxy(const VectorProxy &sp)
1269 : bin(sp.bin), params(sp.params), offset(sp.offset), len(sp.len),
1270 stat(sp.stat), vec(NULL)
1280 const VectorProxy &operator=(const VectorProxy &sp)
1293 ScalarProxy<T, Storage, Bin> operator[](int index)
1295 assert (index >= 0 && index < size());
1296 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index,
1300 size_t size() const { return len; }
1303 * Return true if stat is binned.
1304 *@return false since Proxies aren't printed/binned
1306 bool binned() const { return false; }
1309 * This stat has no state. Nothing to reset.
1314 template <typename T, template <typename T> class Storage, class Bin>
1315 inline VectorProxy<T, Storage, Bin>
1316 Vector2dBase<T, Storage, Bin>::operator[](int index)
1318 int offset = index * y;
1319 assert (index >= 0 && offset < size());
1320 return VectorProxy<T, Storage, Bin>(bin, params, offset, y, this);
1323 //////////////////////////////////////////////////////////////////////
1325 // Non formula statistics
1327 //////////////////////////////////////////////////////////////////////
1330 * Templatized storage and interface for a distrbution stat.
1332 template <typename T>
1336 /** The parameters for a distribution stat. */
1339 /** The minimum value to track. */
1341 /** The maximum value to track. */
1343 /** The number of entries in each bucket. */
1345 /** The number of buckets. Equal to (max-min)/bucket_size. */
1348 enum { fancy = false };
1351 /** The smallest value sampled. */
1353 /** The largest value sampled. */
1355 /** The number of values sampled less than min. */
1357 /** The number of values sampled more than max. */
1359 /** The current sum. */
1361 /** The sum of squares. */
1363 /** The number of samples. */
1365 /** Counter for each bucket. */
1370 * Construct this storage with the supplied params.
1371 * @param params The parameters.
1373 DistStor(const Params ¶ms)
1374 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1375 sum(T()), squares(T()), samples(0), vec(params.size)
1381 * Add a value to the distribution for the given number of times.
1382 * @param val The value to add.
1383 * @param number The number of times to add the value.
1384 * @param params The paramters of the distribution.
1386 void sample(T val, int number, const Params ¶ms)
1388 if (val < params.min)
1389 underflow += number;
1390 else if (val > params.max)
1393 int index = (val - params.min) / params.bucket_size;
1394 assert(index < size(params));
1395 vec[index] += number;
1404 T sample = val * number;
1406 squares += sample * sample;
1411 * Return the number of buckets in this distribution.
1412 * @return the number of buckets.
1413 * @todo Is it faster to return the size from the parameters?
1415 size_t size(const Params &) const { return vec.size(); }
1418 * Returns true if any calls to sample have been made.
1419 * @param params The paramters of the distribution.
1420 * @return True if any values have been sampled.
1422 bool zero(const Params ¶ms) const
1424 return samples == 0;
1427 void update(DistDataData *data, const Params ¶ms)
1429 data->min = params.min;
1430 data->max = params.max;
1431 data->bucket_size = params.bucket_size;
1432 data->size = params.size;
1434 data->min_val = (min_val == INT_MAX) ? 0 : min_val;
1435 data->max_val = (max_val == INT_MIN) ? 0 : max_val;
1436 data->underflow = underflow;
1437 data->overflow = overflow;
1438 data->vec.resize(params.size);
1439 for (int i = 0; i < params.size; ++i)
1440 data->vec[i] = vec[i];
1443 data->squares = squares;
1444 data->samples = samples;
1448 * Reset stat value to default
1457 int size = vec.size();
1458 for (int i = 0; i < size; ++i)
1468 * Templatized storage and interface for a distribution that calculates mean
1471 template <typename T>
1476 * No paramters for this storage.
1479 enum { fancy = true };
1482 /** The current sum. */
1484 /** The sum of squares. */
1486 /** The number of samples. */
1491 * Create and initialize this storage.
1493 FancyStor(const Params &) : sum(T()), squares(T()), samples(0) {}
1496 * Add a value the given number of times to this running average.
1497 * Update the running sum and sum of squares, increment the number of
1498 * values seen by the given number.
1499 * @param val The value to add.
1500 * @param number The number of times to add the value.
1501 * @param p The parameters of this stat.
1503 void sample(T val, int number, const Params &p)
1505 T value = val * number;
1507 squares += value * value;
1511 void update(DistDataData *data, const Params ¶ms)
1514 data->squares = squares;
1515 data->samples = samples;
1519 * Return the number of entries in this stat, 1
1522 size_t size(const Params &) const { return 1; }
1525 * Return true if no samples have been added.
1526 * @return True if no samples have been added.
1528 bool zero(const Params &) const { return samples == 0; }
1531 * Reset stat value to default
1542 * Templatized storage for distribution that calculates per cycle mean and
1545 template <typename T>
1549 /** No parameters for this storage. */
1551 enum { fancy = true };
1554 /** Current total. */
1556 /** Current sum of squares. */
1561 * Create and initialize this storage.
1563 AvgFancy(const Params &) : sum(T()), squares(T()) {}
1566 * Add a value to the distribution for the given number of times.
1567 * Update the running sum and sum of squares.
1568 * @param val The value to add.
1569 * @param number The number of times to add the value.
1570 * @param p The paramters of the distribution.
1572 void sample(T val, int number, const Params &p)
1574 T value = val * number;
1576 squares += value * value;
1579 void update(DistDataData *data, const Params ¶ms)
1582 data->squares = squares;
1583 data->samples = curTick;
1587 * Return the number of entries, in this case 1.
1590 size_t size(const Params ¶ms) const { return 1; }
1592 * Return true if no samples have been added.
1593 * @return True if the sum is zero.
1595 bool zero(const Params ¶ms) const { return sum == 0; }
1597 * Reset stat value to default
1607 * Implementation of a distribution stat. The type of distribution is
1608 * determined by the Storage template. @sa ScalarBase
1610 template <typename T, template <typename T> class Storage, class Bin>
1611 class DistBase : public DataAccess
1614 /** Define the type of the storage class. */
1615 typedef Storage<T> storage_t;
1616 /** Define the params of the storage class. */
1617 typedef typename storage_t::Params params_t;
1618 /** Define the bin type. */
1619 typedef typename Bin::Bin<storage_t> bin_t;
1622 /** The bin of this stat. */
1624 /** The parameters for this stat. */
1629 * Retrieve the storage from the bin.
1630 * @return The storage object for this stat.
1632 storage_t *data() { return bin.data(params); }
1634 * Retrieve a const pointer to the storage from the bin.
1635 * @return A const pointer to the storage object for this stat.
1637 const storage_t *data() const
1639 bin_t *_bin = const_cast<bin_t *>(&bin);
1640 params_t *_params = const_cast<params_t *>(¶ms);
1641 return _bin->data(*_params);
1645 // Copying stats is not allowed
1646 /** Copies are not allowed. */
1647 DistBase(const DistBase &stat);
1648 /** Copies are not allowed. */
1649 const DistBase &operator=(const DistBase &);
1655 * Add a value to the distribtion n times. Calls sample on the storage
1657 * @param v The value to add.
1658 * @param n The number of times to add it, defaults to 1.
1660 template <typename U>
1661 void sample(const U &v, int n = 1) { data()->sample(v, n, params); }
1664 * Return the number of entries in this stat.
1665 * @return The number of entries.
1667 size_t size() const { return data()->size(params); }
1669 * Return true if no samples have been added.
1670 * @return True if there haven't been any samples.
1672 bool zero() const { return data()->zero(params); }
1674 void update(DistDataBase *base)
1676 base->data.fancy = storage_t::fancy;
1677 data()->update(&(base->data), params);
1680 * @return True is stat is binned.
1682 bool binned() const { return bin_t::binned; }
1684 * Reset stat value to default
1691 bool check() { return bin.initialized(); }
1694 template <typename T, template <typename T> class Storage, class Bin>
1697 template <typename T, template <typename T> class Storage, class Bin>
1698 class VectorDistBase : public DataAccess
1701 typedef Storage<T> storage_t;
1702 typedef typename storage_t::Params params_t;
1703 typedef typename Bin::VectorBin<storage_t> bin_t;
1710 storage_t *data(int index) { return bin.data(index, params); }
1711 const storage_t *data(int index) const
1713 bin_t *_bin = const_cast<bin_t *>(&bin);
1714 params_t *_params = const_cast<params_t *>(¶ms);
1715 return _bin->data(index, *_params);
1719 // Copying stats is not allowed
1720 VectorDistBase(const VectorDistBase &stat);
1721 const VectorDistBase &operator=(const VectorDistBase &);
1726 friend class DistProxy<T, Storage, Bin>;
1727 DistProxy<T, Storage, Bin> operator[](int index);
1728 const DistProxy<T, Storage, Bin> operator[](int index) const;
1730 size_t size() const { return bin.size(); }
1731 bool zero() const { return false; }
1733 * Return true if stat is binned.
1734 *@return True is stat is binned.
1736 bool binned() const { return bin_t::binned; }
1738 * Reset stat value to default
1740 void reset() { bin.reset(); }
1742 bool check() { return bin.initialized(); }
1743 void update(VectorDistDataBase *base)
1745 int size = this->size();
1746 base->data.resize(size);
1747 for (int i = 0; i < size; ++i) {
1748 base->data[i].fancy = storage_t::fancy;
1749 data(i)->update(&(base->data[i]), params);
1754 template <typename T, template <typename T> class Storage, class Bin>
1758 typedef Storage<T> storage_t;
1759 typedef typename storage_t::Params params_t;
1760 typedef typename Bin::Bin<storage_t> bin_t;
1761 typedef VectorDistBase<T, Storage, Bin> base_t;
1766 const base_t *cstat;
1771 storage_t *data() { return stat->data(index); }
1772 const storage_t *data() const { return cstat->data(index); }
1775 DistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1776 : cstat(&s), index(i) {}
1777 DistProxy(const DistProxy &sp)
1778 : cstat(sp.cstat), index(sp.index) {}
1779 const DistProxy &operator=(const DistProxy &sp) {
1780 cstat = sp.cstat; index = sp.index; return *this;
1784 template <typename U>
1785 void sample(const U &v, int n = 1) { data()->sample(v, n, cstat->params); }
1787 size_t size() const { return 1; }
1788 bool zero() const { return data()->zero(cstat->params); }
1790 * Return true if stat is binned.
1791 *@return false since Proxies are not binned/printed.
1793 bool binned() const { return false; }
1795 * Proxy has no state. Nothing to reset.
1800 template <typename T, template <typename T> class Storage, class Bin>
1801 inline DistProxy<T, Storage, Bin>
1802 VectorDistBase<T, Storage, Bin>::operator[](int index)
1804 assert (index >= 0 && index < size());
1805 return DistProxy<T, Storage, Bin>(*this, index);
1808 template <typename T, template <typename T> class Storage, class Bin>
1809 inline const DistProxy<T, Storage, Bin>
1810 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1812 assert (index >= 0 && index < size());
1813 return DistProxy<T, Storage, Bin>(*this, index);
1817 template <typename T, template <typename T> class Storage, class Bin>
1819 VectorDistBase<T, Storage, Bin>::total(int index) const
1822 for (int i=0; i < x_size(); ++i) {
1823 total += data(i)->val(*params);
1828 //////////////////////////////////////////////////////////////////////
1832 //////////////////////////////////////////////////////////////////////
1835 * Base class for formula statistic node. These nodes are used to build a tree
1836 * that represents the formula.
1838 class Node : public RefCounted
1842 * Return the number of nodes in the subtree starting at this node.
1843 * @return the number of nodes in this subtree.
1845 virtual size_t size() const = 0;
1847 * Return the result vector of this subtree.
1848 * @return The result vector of this subtree.
1850 virtual const rvec_t &val() const = 0;
1852 * Return the total of the result vector.
1853 * @return The total of the result vector.
1855 virtual result_t total() const = 0;
1857 * Return true if stat is binned.
1858 *@return True is stat is binned.
1860 virtual bool binned() const = 0;
1865 virtual std::string str() const = 0;
1868 /** Reference counting pointer to a function Node. */
1869 typedef RefCountingPtr<Node> NodePtr;
1871 class ScalarStatNode : public Node
1874 const ScalarDataBase *data;
1875 mutable rvec_t result;
1878 ScalarStatNode(const ScalarDataBase *d) : data(d), result(1) {}
1879 virtual const rvec_t &val() const
1881 result[0] = data->val();
1884 virtual result_t total() const { return data->val(); };
1886 virtual size_t size() const { return 1; }
1888 * Return true if stat is binned.
1889 *@return True is stat is binned.
1891 virtual bool binned() const { return data->binned(); }
1896 virtual std::string str() const { return data->name; }
1899 template <typename T, template <typename T> class Storage, class Bin>
1900 class ScalarProxyNode : public Node
1903 const ScalarProxy<T, Storage, Bin> proxy;
1904 mutable rvec_t result;
1907 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1908 : proxy(p), result(1) { }
1909 virtual const rvec_t &val() const
1911 result[0] = proxy.val();
1914 virtual result_t total() const { return proxy.val(); };
1916 virtual size_t size() const { return 1; }
1918 * Return true if stat is binned.
1919 *@return True is stat is binned.
1921 virtual bool binned() const { return proxy.binned(); }
1926 virtual std::string str() const { return proxy.str(); }
1929 class VectorStatNode : public Node
1932 const VectorDataBase *data;
1935 VectorStatNode(const VectorDataBase *d) : data(d) { }
1936 virtual const rvec_t &val() const { return data->val(); }
1937 virtual result_t total() const { return data->total(); };
1939 virtual size_t size() const { return data->size(); }
1941 * Return true if stat is binned.
1942 *@return True is stat is binned.
1944 virtual bool binned() const { return data->binned(); }
1946 virtual std::string str() const { return data->name; }
1949 template <typename T>
1950 class ConstNode : public Node
1956 ConstNode(T s) : data(1, (result_t)s) {}
1957 const rvec_t &val() const { return data; }
1958 virtual result_t total() const { return data[0]; };
1960 virtual size_t size() const { return 1; }
1962 * Return true if stat is binned.
1963 *@return False since constants aren't binned.
1965 virtual bool binned() const { return false; }
1967 virtual std::string str() const { return to_string(data[0]); }
1970 template <typename T>
1971 class FunctorNode : public Node
1975 mutable rvec_t result;
1978 FunctorNode(T &f) : functor(f) { result.resize(1); }
1979 const rvec_t &val() const {
1980 result[0] = (result_t)functor();
1983 virtual result_t total() const { return (result_t)functor(); };
1985 virtual size_t size() const { return 1; }
1987 * Return true if stat is binned.
1988 *@return False since Functors aren't binned
1990 virtual bool binned() const { return false; }
1991 virtual std::string str() const { return to_string(functor()); }
1994 template <typename T>
1995 class ScalarNode : public Node
1999 mutable rvec_t result;
2002 ScalarNode(T &s) : scalar(s) { result.resize(1); }
2003 const rvec_t &val() const {
2004 result[0] = (result_t)scalar;
2007 virtual result_t total() const { return (result_t)scalar; };
2009 virtual size_t size() const { return 1; }
2011 * Return true if stat is binned.
2012 *@return False since Scalar's aren't binned
2014 virtual bool binned() const { return false; }
2015 virtual std::string str() const { return to_string(scalar); }
2022 struct OpString<std::plus<result_t> >
2024 static std::string str() { return "+"; }
2028 struct OpString<std::minus<result_t> >
2030 static std::string str() { return "-"; }
2034 struct OpString<std::multiplies<result_t> >
2036 static std::string str() { return "*"; }
2040 struct OpString<std::divides<result_t> >
2042 static std::string str() { return "/"; }
2046 struct OpString<std::modulus<result_t> >
2048 static std::string str() { return "%"; }
2052 struct OpString<std::negate<result_t> >
2054 static std::string str() { return "-"; }
2058 class UnaryNode : public Node
2062 mutable rvec_t result;
2065 UnaryNode(NodePtr &p) : l(p) {}
2067 const rvec_t &val() const {
2068 const rvec_t &lvec = l->val();
2069 int size = lvec.size();
2073 result.resize(size);
2075 for (int i = 0; i < size; ++i)
2076 result[i] = op(lvec[i]);
2081 result_t total() const {
2083 return op(l->total());
2086 virtual size_t size() const { return l->size(); }
2088 * Return true if child of node is binned.
2089 *@return True if child of node is binned.
2091 virtual bool binned() const { return l->binned(); }
2093 virtual std::string str() const
2095 return OpString<Op>::str() + l->str();
2100 class BinaryNode : public Node
2105 mutable rvec_t result;
2108 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2110 const rvec_t &val() const {
2112 const rvec_t &lvec = l->val();
2113 const rvec_t &rvec = r->val();
2115 assert(lvec.size() > 0 && rvec.size() > 0);
2117 if (lvec.size() == 1 && rvec.size() == 1) {
2119 result[0] = op(lvec[0], rvec[0]);
2120 } else if (lvec.size() == 1) {
2121 int size = rvec.size();
2122 result.resize(size);
2123 for (int i = 0; i < size; ++i)
2124 result[i] = op(lvec[0], rvec[i]);
2125 } else if (rvec.size() == 1) {
2126 int size = lvec.size();
2127 result.resize(size);
2128 for (int i = 0; i < size; ++i)
2129 result[i] = op(lvec[i], rvec[0]);
2130 } else if (rvec.size() == lvec.size()) {
2131 int size = rvec.size();
2132 result.resize(size);
2133 for (int i = 0; i < size; ++i)
2134 result[i] = op(lvec[i], rvec[i]);
2140 result_t total() const {
2142 return op(l->total(), r->total());
2145 virtual size_t size() const {
2153 assert(ls == rs && "Node vector sizes are not equal");
2158 * Return true if any children of node are binned
2159 *@return True if either child of node is binned.
2161 virtual bool binned() const { return (l->binned() || r->binned()); }
2163 virtual std::string str() const
2165 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2170 class SumNode : public Node
2174 mutable rvec_t result;
2177 SumNode(NodePtr &p) : l(p), result(1) {}
2179 const rvec_t &val() const {
2180 const rvec_t &lvec = l->val();
2181 int size = lvec.size();
2187 for (int i = 0; i < size; ++i)
2188 result[0] = op(result[0], lvec[i]);
2193 result_t total() const {
2194 const rvec_t &lvec = l->val();
2195 int size = lvec.size();
2198 result_t result = 0.0;
2201 for (int i = 0; i < size; ++i)
2202 result = op(result, lvec[i]);
2207 virtual size_t size() const { return 1; }
2209 * Return true if child of node is binned.
2210 *@return True if child of node is binned.
2212 virtual bool binned() const { return l->binned(); }
2214 virtual std::string str() const
2216 return csprintf("total(%s)", l->str());
2220 //////////////////////////////////////////////////////////////////////
2222 // Binning Interface
2224 //////////////////////////////////////////////////////////////////////
2228 friend class MainBin::BinBase;
2236 off_t size() const { return memsize; }
2237 char *memory(off_t off);
2240 static MainBin *&curBin()
2242 static MainBin *current = NULL;
2246 static void setCurBin(MainBin *bin) { curBin() = bin; }
2247 static MainBin *current() { assert(curBin()); return curBin(); }
2249 static off_t &offset()
2251 static off_t offset = 0;
2255 static off_t new_offset(size_t size)
2257 size_t mask = sizeof(u_int64_t) - 1;
2258 off_t off = offset();
2260 // That one is for the last trailing flags byte.
2261 offset() += (size + 1 + mask) & ~mask;
2266 MainBin(const std::string &name);
2287 BinBase() : offset(-1) {}
2288 void allocate(size_t size)
2290 offset = new_offset(size);
2294 assert(offset != -1);
2295 return current()->memory(offset);
2299 template <class Storage>
2300 class Bin : public BinBase
2303 typedef typename Storage::Params Params;
2306 enum { binned = true };
2307 Bin() { allocate(sizeof(Storage)); }
2308 bool initialized() const { return true; }
2309 void init(Params ¶ms) { }
2311 int size() const { return 1; }
2314 data(Params ¶ms)
2316 assert(initialized());
2317 char *ptr = access();
2318 char *flags = ptr + sizeof(Storage);
2319 if (!(*flags & 0x1)) {
2321 new (ptr) Storage(params);
2323 return reinterpret_cast<Storage *>(ptr);
2329 char *ptr = access();
2330 char *flags = ptr + size() * sizeof(Storage);
2331 if (!(*flags & 0x1))
2334 Storage *s = reinterpret_cast<Storage *>(ptr);
2339 template <class Storage>
2340 class VectorBin : public BinBase
2343 typedef typename Storage::Params Params;
2349 enum { binned = true };
2350 VectorBin() : _size(0) {}
2352 bool initialized() const { return _size > 0; }
2353 void init(int s, Params ¶ms)
2355 assert(!initialized());
2358 allocate(_size * sizeof(Storage));
2361 int size() const { return _size; }
2363 Storage *data(int index, Params ¶ms)
2365 assert(initialized());
2366 assert(index >= 0 && index < size());
2367 char *ptr = access();
2368 char *flags = ptr + size() * sizeof(Storage);
2369 if (!(*flags & 0x1)) {
2371 for (int i = 0; i < size(); ++i)
2372 new (ptr + i * sizeof(Storage)) Storage(params);
2374 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2378 char *ptr = access();
2379 char *flags = ptr + size() * sizeof(Storage);
2380 if (!(*flags & 0x1))
2383 for (int i = 0; i < _size; ++i) {
2384 char *p = ptr + i * sizeof(Storage);
2385 Storage *s = reinterpret_cast<Storage *>(p);
2394 template <class Storage>
2398 typedef typename Storage::Params Params;
2399 enum { binned = false };
2402 char ptr[sizeof(Storage)];
2407 reinterpret_cast<Storage *>(ptr)->~Storage();
2410 bool initialized() const { return true; }
2411 void init(Params ¶ms)
2413 new (ptr) Storage(params);
2415 int size() const{ return 1; }
2416 Storage *data(Params ¶ms)
2418 assert(initialized());
2419 return reinterpret_cast<Storage *>(ptr);
2423 Storage *s = reinterpret_cast<Storage *>(ptr);
2428 template <class Storage>
2432 typedef typename Storage::Params Params;
2433 enum { binned = false };
2440 VectorBin() : ptr(NULL) { }
2446 for (int i = 0; i < _size; ++i) {
2447 char *p = ptr + i * sizeof(Storage);
2448 reinterpret_cast<Storage *>(p)->~Storage();
2453 bool initialized() const { return ptr != NULL; }
2454 void init(int s, Params ¶ms)
2456 assert(s > 0 && "size must be positive!");
2457 assert(!initialized());
2459 ptr = new char[_size * sizeof(Storage)];
2460 for (int i = 0; i < _size; ++i)
2461 new (ptr + i * sizeof(Storage)) Storage(params);
2464 int size() const { return _size; }
2466 Storage *data(int index, Params ¶ms)
2468 assert(initialized());
2469 assert(index >= 0 && index < size());
2470 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2474 for (int i = 0; i < _size; ++i) {
2475 char *p = ptr + i * sizeof(Storage);
2476 Storage *s = reinterpret_cast<Storage *>(p);
2483 //////////////////////////////////////////////////////////////////////
2485 // Visible Statistics Types
2487 //////////////////////////////////////////////////////////////////////
2489 * @defgroup VisibleStats "Statistic Types"
2490 * These are the statistics that are used in the simulator. By default these
2491 * store counters and don't use binning, but are templatized to accept any type
2492 * and any Bin class.
2497 * This is an easy way to assign all your stats to be binned or not
2498 * binned. If the typedef is NoBin, nothing is binned. If it is
2499 * MainBin, then all stats are binned under that Bin.
2502 typedef MainBin DefaultBin;
2504 typedef NoBin DefaultBin;
2508 * This is a simple scalar statistic, like a counter.
2509 * @sa Stat, ScalarBase, StatStor
2511 template <typename T = Counter, class Bin = DefaultBin>
2513 : public Wrap<Scalar<T, Bin>,
2514 ScalarBase<T, StatStor, Bin>,
2518 /** The base implementation. */
2519 typedef ScalarBase<T, StatStor, Bin> Base;
2527 * Sets the stat equal to the given value. Calls the base implementation
2529 * @param v The new value.
2531 template <typename U>
2532 void operator=(const U &v) { Base::operator=(v); }
2536 * A stat that calculates the per cycle average of a value.
2537 * @sa Stat, ScalarBase, AvgStor
2539 template <typename T = Counter, class Bin = DefaultBin>
2541 : public Wrap<Average<T, Bin>,
2542 ScalarBase<T, AvgStor, Bin>,
2546 /** The base implementation. */
2547 typedef ScalarBase<T, AvgStor, Bin> Base;
2555 * Sets the stat equal to the given value. Calls the base implementation
2557 * @param v The new value.
2559 template <typename U>
2560 void operator=(const U &v) { Base::operator=(v); }
2564 * A vector of scalar stats.
2565 * @sa Stat, VectorBase, StatStor
2567 template <typename T = Counter, class Bin = DefaultBin>
2569 : public WrapVec<Vector<T, Bin>,
2570 VectorBase<T, StatStor, Bin>,
2574 /** The base implementation. */
2575 typedef ScalarBase<T, StatStor, Bin> Base;
2578 * Set this vector to have the given size.
2579 * @param size The new size.
2580 * @return A reference to this stat.
2582 Vector &init(size_t size) {
2583 bin.init(size, params);
2591 * A vector of Average stats.
2592 * @sa Stat, VectorBase, AvgStor
2594 template <typename T = Counter, class Bin = DefaultBin>
2596 : public WrapVec<AverageVector<T, Bin>,
2597 VectorBase<T, AvgStor, Bin>,
2602 * Set this vector to have the given size.
2603 * @param size The new size.
2604 * @return A reference to this stat.
2606 AverageVector &init(size_t size) {
2607 bin.init(size, params);
2615 * A 2-Dimensional vecto of scalar stats.
2616 * @sa Stat, Vector2dBase, StatStor
2618 template <typename T = Counter, class Bin = DefaultBin>
2620 : public WrapVec2d<Vector2d<T, Bin>,
2621 Vector2dBase<T, StatStor, Bin>,
2625 Vector2d &init(size_t _x, size_t _y) {
2626 statData()->x = x = _x;
2627 statData()->y = y = _y;
2628 bin.init(x * y, params);
2636 * A simple distribution stat.
2637 * @sa Stat, DistBase, DistStor
2639 template <typename T = Counter, class Bin = DefaultBin>
2641 : public Wrap<Distribution<T, Bin>,
2642 DistBase<T, DistStor, Bin>,
2646 /** Base implementation. */
2647 typedef DistBase<T, DistStor, Bin> Base;
2648 /** The Parameter type. */
2649 typedef typename DistStor<T>::Params Params;
2653 * Set the parameters of this distribution. @sa DistStor::Params
2654 * @param min The minimum value of the distribution.
2655 * @param max The maximum value of the distribution.
2656 * @param bkt The number of values in each bucket.
2657 * @return A reference to this distribution.
2659 Distribution &init(T min, T max, int bkt) {
2662 params.bucket_size = bkt;
2663 params.size = (max - min) / bkt + 1;
2672 * Calculates the mean and variance of all the samples.
2673 * @sa Stat, DistBase, FancyStor
2675 template <typename T = Counter, class Bin = DefaultBin>
2676 class StandardDeviation
2677 : public Wrap<StandardDeviation<T, Bin>,
2678 DistBase<T, FancyStor, Bin>,
2682 /** The base implementation */
2683 typedef DistBase<T, DistStor, Bin> Base;
2684 /** The parameter type. */
2685 typedef typename DistStor<T>::Params Params;
2689 * Construct and initialize this distribution.
2691 StandardDeviation() {
2698 * Calculates the per cycle mean and variance of the samples.
2699 * @sa Stat, DistBase, AvgFancy
2701 template <typename T = Counter, class Bin = DefaultBin>
2702 class AverageDeviation
2703 : public Wrap<AverageDeviation<T, Bin>,
2704 DistBase<T, AvgFancy, Bin>,
2708 /** The base implementation */
2709 typedef DistBase<T, DistStor, Bin> Base;
2710 /** The parameter type. */
2711 typedef typename DistStor<T>::Params Params;
2715 * Construct and initialize this distribution.
2725 * A vector of distributions.
2726 * @sa Stat, VectorDistBase, DistStor
2728 template <typename T = Counter, class Bin = DefaultBin>
2729 class VectorDistribution
2730 : public WrapVec<VectorDistribution<T, Bin>,
2731 VectorDistBase<T, DistStor, Bin>,
2735 /** The base implementation */
2736 typedef VectorDistBase<T, DistStor, Bin> Base;
2737 /** The parameter type. */
2738 typedef typename DistStor<T>::Params Params;
2742 * Initialize storage and parameters for this distribution.
2743 * @param size The size of the vector (the number of distributions).
2744 * @param min The minimum value of the distribution.
2745 * @param max The maximum value of the distribution.
2746 * @param bkt The number of values in each bucket.
2747 * @return A reference to this distribution.
2749 VectorDistribution &init(int size, T min, T max, int bkt) {
2752 params.bucket_size = bkt;
2753 params.size = (max - min) / bkt + 1;
2754 bin.init(size, params);
2762 * This is a vector of StandardDeviation stats.
2763 * @sa Stat, VectorDistBase, FancyStor
2765 template <typename T = Counter, class Bin = DefaultBin>
2766 class VectorStandardDeviation
2767 : public WrapVec<VectorStandardDeviation<T, Bin>,
2768 VectorDistBase<T, FancyStor, Bin>,
2772 /** The base implementation */
2773 typedef VectorDistBase<T, FancyStor, Bin> Base;
2774 /** The parameter type. */
2775 typedef typename DistStor<T>::Params Params;
2779 * Initialize storage for this distribution.
2780 * @param size The size of the vector.
2781 * @return A reference to this distribution.
2783 VectorStandardDeviation &init(int size) {
2784 bin.init(size, params);
2792 * This is a vector of AverageDeviation stats.
2793 * @sa Stat, VectorDistBase, AvgFancy
2795 template <typename T = Counter, class Bin = DefaultBin>
2796 class VectorAverageDeviation
2797 : public WrapVec<VectorAverageDeviation<T, Bin>,
2798 VectorDistBase<T, AvgFancy, Bin>,
2802 /** The base implementation */
2803 typedef VectorDistBase<T, AvgFancy, Bin> Base;
2804 /** The parameter type. */
2805 typedef typename DistStor<T>::Params Params;
2809 * Initialize storage for this distribution.
2810 * @param size The size of the vector.
2811 * @return A reference to this distribution.
2813 VectorAverageDeviation &init(int size) {
2814 bin.init(size, params);
2822 * A formula for statistics that is calculated when printed. A formula is
2823 * stored as a tree of Nodes that represent the equation to calculate.
2824 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2826 class FormulaBase : public DataAccess
2829 /** The root of the tree which represents the Formula */
2835 * Return the result of the Fomula in a vector. If there were no Vector
2836 * components to the Formula, then the vector is size 1. If there were,
2837 * like x/y with x being a vector of size 3, then the result returned will
2838 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2839 * @return The result vector.
2841 void val(rvec_t &vec) const;
2844 * Return the total Formula result. If there is a Vector
2845 * component to this Formula, then this is the result of the
2846 * Formula if the formula is applied after summing all the
2847 * components of the Vector. For example, if Formula is x/y where
2848 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2849 * there is no Vector component, total() returns the same value as
2850 * the first entry in the rvec_t val() returns.
2851 * @return The total of the result vector.
2853 result_t total() const;
2856 * Return the number of elements in the tree.
2858 size_t size() const;
2861 * Return true if Formula is binned. i.e. any of its children
2863 * @return True if Formula is binned.
2865 bool binned() const;
2867 bool check() const { return true; }
2870 * Formulas don't need to be reset
2882 void update(StatData *);
2884 std::string str() const;
2887 class FormulaDataBase : public VectorDataBase
2890 virtual std::string str() const = 0;
2891 virtual bool check() const { return true; }
2892 virtual void python(Python &py) const;
2896 class FormulaData : public FormulaDataBase
2903 FormulaData(T &stat) : s(stat) {}
2905 virtual bool binned() const { return s.binned(); }
2906 virtual bool zero() const { return s.zero(); }
2907 virtual void reset() { s.reset(); }
2909 virtual size_t size() const { return s.size(); }
2910 virtual const rvec_t &val() const
2915 virtual result_t total() const { return s.total(); }
2916 virtual void update()
2918 VectorDataBase::update();
2921 virtual std::string str() const { return s.str(); }
2926 : public WrapVec<Formula,
2932 * Create and initialize thie formula, and register it with the database.
2937 * Create a formula with the given root node, register it with the
2939 * @param r The root of the expression tree.
2944 * Set an unitialized Formula to the given root.
2945 * @param r The root of the expression tree.
2946 * @return a reference to this formula.
2948 const Formula &operator=(Temp r);
2951 * Add the given tree to the existing one.
2952 * @param r The root of the expression tree.
2953 * @return a reference to this formula.
2955 const Formula &operator+=(Temp r);
2958 class FormulaNode : public Node
2961 const Formula &formula;
2965 FormulaNode(const Formula &f) : formula(f) {}
2967 virtual size_t size() const { return formula.size(); }
2968 virtual const rvec_t &val() const { formula.val(vec); return vec; }
2969 virtual result_t total() const { return formula.total(); }
2970 virtual bool binned() const { return formula.binned(); }
2972 virtual std::string str() const { return formula.str(); }
2976 * Helper class to construct formula node trees.
2982 * Pointer to a Node object.
2988 * Copy the given pointer to this class.
2989 * @param n A pointer to a Node object to copy.
2991 Temp(NodePtr n) : node(n) { }
2994 * Return the node pointer.
2995 * @return the node pointer.
2997 operator NodePtr&() { return node;}
3001 * Create a new ScalarStatNode.
3002 * @param s The ScalarStat to place in a node.
3004 template <typename T, class Bin>
3005 Temp(const Scalar<T, Bin> &s)
3006 : node(new ScalarStatNode(s.statData())) { }
3009 * Create a new ScalarStatNode.
3010 * @param s The ScalarStat to place in a node.
3012 template <typename T, class Bin>
3013 Temp(const Average<T, Bin> &s)
3014 : node(new ScalarStatNode(s.statData())) { }
3017 * Create a new VectorStatNode.
3018 * @param s The VectorStat to place in a node.
3020 template <typename T, class Bin>
3021 Temp(const Vector<T, Bin> &s)
3022 : node(new VectorStatNode(s.statData())) { }
3027 Temp(const Formula &f)
3028 : node(new FormulaNode(f)) { }
3031 * Create a new ScalarProxyNode.
3032 * @param p The ScalarProxy to place in a node.
3034 template <typename T, template <typename T> class Storage, class Bin>
3035 Temp(const ScalarProxy<T, Storage, Bin> &p)
3036 : node(new ScalarProxyNode<T, Storage, Bin>(p)) { }
3039 * Create a ConstNode
3040 * @param value The value of the const node.
3042 Temp(signed char value)
3043 : node(new ConstNode<signed char>(value)) {}
3046 * Create a ConstNode
3047 * @param value The value of the const node.
3049 Temp(unsigned char value)
3050 : node(new ConstNode<unsigned char>(value)) {}
3053 * Create a ConstNode
3054 * @param value The value of the const node.
3056 Temp(signed short value)
3057 : node(new ConstNode<signed short>(value)) {}
3060 * Create a ConstNode
3061 * @param value The value of the const node.
3063 Temp(unsigned short value)
3064 : node(new ConstNode<unsigned short>(value)) {}
3067 * Create a ConstNode
3068 * @param value The value of the const node.
3070 Temp(signed int value)
3071 : node(new ConstNode<signed int>(value)) {}
3074 * Create a ConstNode
3075 * @param value The value of the const node.
3077 Temp(unsigned int value)
3078 : node(new ConstNode<unsigned int>(value)) {}
3081 * Create a ConstNode
3082 * @param value The value of the const node.
3084 Temp(signed long value)
3085 : node(new ConstNode<signed long>(value)) {}
3088 * Create a ConstNode
3089 * @param value The value of the const node.
3091 Temp(unsigned long value)
3092 : node(new ConstNode<unsigned long>(value)) {}
3095 * Create a ConstNode
3096 * @param value The value of the const node.
3098 Temp(signed long long value)
3099 : node(new ConstNode<signed long long>(value)) {}
3102 * Create a ConstNode
3103 * @param value The value of the const node.
3105 Temp(unsigned long long value)
3106 : node(new ConstNode<unsigned long long>(value)) {}
3109 * Create a ConstNode
3110 * @param value The value of the const node.
3113 : node(new ConstNode<float>(value)) {}
3116 * Create a ConstNode
3117 * @param value The value of the const node.
3120 : node(new ConstNode<double>(value)) {}
3129 void dump(std::ostream &stream, DisplayMode mode = DefaultMode);
3130 void python_start(const std::string &file);
3131 void python_dump(const std::string &name, const std::string &subname);
3133 void registerResetCallback(Callback *cb);
3136 operator+(Temp l, Temp r)
3138 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
3142 operator-(Temp l, Temp r)
3144 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
3148 operator*(Temp l, Temp r)
3150 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
3154 operator/(Temp l, Temp r)
3156 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
3160 operator%(Temp l, Temp r)
3162 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
3168 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
3171 template <typename T>
3175 return NodePtr(new ConstNode<T>(val));
3178 template <typename T>
3182 return NodePtr(new FunctorNode<T>(val));
3185 template <typename T>
3189 return NodePtr(new ScalarNode<T>(val));
3195 return NodePtr(new SumNode<std::plus<result_t> >(val));
3197 extern bool PrintDescriptions;
3199 } // namespace statistics
3201 #endif // __STATISTICS_HH__