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;
126 extern DisplayMode DefaultMode;
128 /* Contains the statistic implementation details */
129 //////////////////////////////////////////////////////////////////////
131 // Statistics Framework Base classes
133 //////////////////////////////////////////////////////////////////////
136 /** The name of the stat. */
138 /** The description of the stat. */
140 /** The formatting flags. */
142 /** The display precision. */
146 /** A pointer to a prerequisite Stat. */
147 const StatData *prereq;
150 : flags(none), precision(-1), prereq(0)
156 * @return true if the stat is binned.
158 virtual bool binned() const = 0;
161 * Print this stat to the given ostream.
162 * @param stream The stream to print to.
164 virtual void display(std::ostream &stream, DisplayMode mode) const = 0;
165 virtual void python(Python &py) const = 0;
166 bool dodisplay() const { return !prereq || !prereq->zero(); }
169 * Reset the corresponding stat to the default state.
171 virtual void reset() = 0;
174 * @return true if this stat has a value and satisfies its
175 * requirement as a prereq
177 virtual bool zero() const = 0;
180 * Check that this stat has been set up properly and is ready for
182 * @return true for success
184 virtual bool check() const = 0;
185 bool baseCheck() const;
188 * Checks if the first stat's name is alphabetically less than the second.
189 * This function breaks names up at periods and considers each subname
191 * @param stat1 The first stat.
192 * @param stat2 The second stat.
193 * @return stat1's name is alphabetically before stat2's
195 static bool less(StatData *stat1, StatData *stat2);
198 struct ScalarDataBase : public StatData
200 virtual result_t val() const = 0;
201 virtual result_t total() const = 0;
203 virtual void display(std::ostream &stream, DisplayMode mode) const;
204 virtual void python(Python &py) const;
208 class ScalarData : public ScalarDataBase
214 ScalarData(T &stat) : s(stat) {}
216 virtual bool binned() const { return s.binned(); }
217 virtual bool check() const { return s.check(); }
218 virtual result_t val() const { return s.val(); }
219 virtual result_t total() const { return s.total(); }
220 virtual void reset() { s.reset(); }
221 virtual bool zero() const { return s.zero(); }
224 struct VectorDataBase : public StatData
226 /** Names and descriptions of subfields. */
227 mutable std::vector<std::string> subnames;
228 mutable std::vector<std::string> subdescs;
230 virtual void display(std::ostream &stream, DisplayMode mode) const;
231 virtual void python(Python &py) const;
233 virtual size_t size() const = 0;
234 virtual const rvec_t &val() const = 0;
235 virtual result_t total() const = 0;
236 virtual void update()
238 if (!subnames.empty()) {
240 if (subnames.size() < s)
243 if (subdescs.size() < s)
250 class VectorData : public VectorDataBase
257 VectorData(T &stat) : s(stat) {}
259 virtual bool binned() const { return s.binned(); }
260 virtual bool check() const { return s.check(); }
261 virtual bool zero() const { return s.zero(); }
262 virtual void reset() { s.reset(); }
264 virtual size_t size() const { return s.size(); }
265 virtual const rvec_t &val() const
270 virtual result_t total() const { return s.total(); }
271 virtual void update()
273 VectorDataBase::update();
295 void python(Python &py, const std::string &name) const;
298 struct DistDataBase : public StatData
300 /** Local storage for the entry values, used for printing. */
303 virtual void display(std::ostream &stream, DisplayMode mode) const;
304 virtual void python(Python &py) const;
305 virtual void update() = 0;
309 class DistData : public DistDataBase
315 DistData(T &stat) : s(stat) {}
317 virtual bool binned() const { return s.binned(); }
318 virtual bool check() const { return s.check(); }
319 virtual void reset() { s.reset(); }
320 virtual bool zero() const { return s.zero(); }
321 virtual void update() { return s.update(this); }
324 struct VectorDistDataBase : public StatData
326 std::vector<DistDataData> data;
328 /** Names and descriptions of subfields. */
329 mutable std::vector<std::string> subnames;
330 mutable std::vector<std::string> subdescs;
332 /** Local storage for the entry values, used for printing. */
335 virtual size_t size() const = 0;
336 virtual void display(std::ostream &stream, DisplayMode mode) const;
337 virtual void python(Python &py) const;
338 virtual void update()
341 if (subnames.size() < s)
344 if (subdescs.size() < s)
350 class VectorDistData : public VectorDistDataBase
354 typedef typename T::bin_t bin_t;
357 VectorDistData(T &stat) : s(stat) {}
359 virtual bool binned() const { return bin_t::binned; }
360 virtual bool check() const { return s.check(); }
361 virtual void reset() { s.reset(); }
362 virtual size_t size() const { return s.size(); }
363 virtual bool zero() const { return s.zero(); }
364 virtual void update()
366 VectorDistDataBase::update();
367 return s.update(this);
371 struct Vector2dDataBase : public StatData
373 /** Names and descriptions of subfields. */
374 std::vector<std::string> subnames;
375 std::vector<std::string> subdescs;
376 std::vector<std::string> y_subnames;
378 /** Local storage for the entry values, used for printing. */
383 virtual void display(std::ostream &stream, DisplayMode mode) const;
384 virtual void python(Python &py) const;
385 virtual void update()
387 if (subnames.size() < x)
393 class Vector2dData : public Vector2dDataBase
397 typedef typename T::bin_t bin_t;
400 Vector2dData(T &stat) : s(stat) {}
402 virtual bool binned() const { return bin_t::binned; }
403 virtual bool check() const { return s.check(); }
404 virtual void reset() { s.reset(); }
405 virtual bool zero() const { return s.zero(); }
406 virtual void update()
408 Vector2dDataBase::update();
417 StatData *find() const;
418 void map(StatData *data);
420 StatData *statData();
421 const StatData *statData() const;
427 template <class Parent, class Child, template <class> class Data>
428 class Wrap : public Child
431 Parent &self() { return *reinterpret_cast<Parent *>(this); }
434 Data<Child> *statData()
436 StatData *__data = DataAccess::statData();
437 Data<Child> *ptr = dynamic_cast<Data<Child> *>(__data);
443 const Data<Child> *statData() const
445 const StatData *__data = DataAccess::statData();
446 const Data<Child> *ptr = dynamic_cast<const Data<Child> *>(__data);
454 map(new Data<Child>(*this));
458 * Set the name and marks this stat to print at the end of simulation.
459 * @param name The new name.
460 * @return A reference to this stat.
462 Parent &name(const std::string &_name)
464 Data<Child> *data = statData();
471 * Set the description and marks this stat to print at the end of
473 * @param desc The new description.
474 * @return A reference to this stat.
476 Parent &desc(const std::string &_desc)
478 statData()->desc = _desc;
483 * Set the precision and marks this stat to print at the end of simulation.
484 * @param p The new precision
485 * @return A reference to this stat.
487 Parent &precision(int _precision)
489 statData()->precision = _precision;
494 * Set the flags and marks this stat to print at the end of simulation.
495 * @param f The new flags.
496 * @return A reference to this stat.
498 Parent &flags(StatFlags _flags)
500 statData()->flags |= _flags;
505 * Set the prerequisite stat and marks this stat to print at the end of
507 * @param prereq The prerequisite stat.
508 * @return A reference to this stat.
511 Parent &prereq(const T &prereq)
513 statData()->prereq = prereq.statData();
518 template <class Parent, class Child, template <class Child> class Data>
519 class WrapVec : public Wrap<Parent, Child, Data>
522 // The following functions are specific to vectors. If you use them
523 // in a non vector context, you will get a nice compiler error!
526 * Set the subfield name for the given index, and marks this stat to print
527 * at the end of simulation.
528 * @param index The subfield index.
529 * @param name The new name of the subfield.
530 * @return A reference to this stat.
532 Parent &subname(int index, const std::string &name)
534 std::vector<std::string> &subn = statData()->subnames;
535 if (subn.size() <= index)
536 subn.resize(index + 1);
542 * Set the subfield description for the given index and marks this stat to
543 * print at the end of simulation.
544 * @param index The subfield index.
545 * @param desc The new description of the subfield
546 * @return A reference to this stat.
548 Parent &subdesc(int index, const std::string &desc)
550 std::vector<std::string> &subd = statData()->subdescs;
551 if (subd.size() <= index)
552 subd.resize(index + 1);
560 template <class Parent, class Child, template <class Child> class Data>
561 class WrapVec2d : public WrapVec<Parent, Child, Data>
565 * @warning This makes the assumption that if you're gonna subnames a 2d
566 * vector, you're subnaming across all y
568 Parent &ysubnames(const char **names)
570 Data<Child> *data = statData();
571 data->y_subnames.resize(y);
572 for (int i = 0; i < y; ++i)
573 data->y_subnames[i] = names[i];
576 Parent &ysubname(int index, const std::string subname)
578 Data<Child> *data = statData();
580 data->y_subnames.resize(y);
581 data->y_subnames[i] = subname.c_str();
586 //////////////////////////////////////////////////////////////////////
590 //////////////////////////////////////////////////////////////////////
593 * Templatized storage and interface for a simple scalar stat.
595 template <typename T>
599 /** The paramaters for this storage type, none for a scalar. */
603 /** The statistic value. */
613 * Builds this storage element and calls the base constructor of the
616 StatStor(const Params &) : data(Null()) {}
619 * The the stat to the given value.
620 * @param val The new value.
621 * @param p The paramters of this storage type.
623 void set(T val, const Params &p) { data = val; }
625 * Increment the stat by the given value.
626 * @param val The new value.
627 * @param p The paramters of this storage type.
629 void inc(T val, const Params &p) { data += val; }
631 * Decrement the stat by the given value.
632 * @param val The new value.
633 * @param p The paramters of this storage type.
635 void dec(T val, const Params &p) { data -= val; }
637 * Return the value of this stat as a result type.
638 * @param p The parameters of this storage type.
639 * @return The value of this stat.
641 result_t val(const Params &p) const { return (result_t)data; }
643 * Return the value of this stat as its base type.
644 * @param p The params of this storage type.
645 * @return The value of this stat.
647 T value(const Params &p) const { return data; }
649 * Reset stat value to default
651 void reset() { data = Null(); }
654 * @return true if zero value
656 bool zero() const { return data == Null(); }
660 * Templatized storage and interface to a per-cycle average stat. This keeps
661 * a current count and updates a total (count * cycles) when this count
662 * changes. This allows the quick calculation of a per cycle count of the item
663 * being watched. This is good for keeping track of residencies in structures
664 * among other things.
665 * @todo add lateny to the stat and fix binning.
667 template <typename T>
671 /** The paramaters for this storage type */
675 * The current count. We stash this here because the current
676 * value is not a binned value.
682 /** The total count for all cycles. */
683 mutable result_t total;
684 /** The cycle that current last changed. */
689 * Build and initializes this stat storage.
691 AvgStor(Params &p) : total(0), last(0) { p.current = T(); }
694 * Set the current count to the one provided, update the total and last
696 * @param val The new count.
697 * @param p The parameters for this storage.
699 void set(T val, Params &p) {
700 total += p.current * (curTick - last);
706 * Increment the current count by the provided value, calls set.
707 * @param val The amount to increment.
708 * @param p The parameters for this storage.
710 void inc(T val, Params &p) { set(p.current + val, p); }
713 * Deccrement the current count by the provided value, calls set.
714 * @param val The amount to decrement.
715 * @param p The parameters for this storage.
717 void dec(T val, Params &p) { set(p.current - val, p); }
720 * Return the current average.
721 * @param p The parameters for this storage.
722 * @return The current average.
724 result_t val(const Params &p) const {
725 total += p.current * (curTick - last);
727 return (result_t)(total + p.current) / (result_t)(curTick + 1);
731 * Return the current count.
732 * @param p The parameters for this storage.
733 * @return The current count.
735 T value(const Params &p) const { return p.current; }
738 * Reset stat value to default
747 * @return true if zero value
749 bool zero() const { return total == 0.0; }
753 * Implementation of a scalar stat. The type of stat is determined by the
754 * Storage template. The storage for this stat is held within the Bin class.
755 * This allows for breaking down statistics across multiple bins easily.
757 template <typename T, template <typename T> class Storage, class Bin>
758 class ScalarBase : public DataAccess
761 /** Define the type of the storage class. */
762 typedef Storage<T> storage_t;
763 /** Define the params of the storage class. */
764 typedef typename storage_t::Params params_t;
765 /** Define the bin type. */
766 typedef typename Bin::Bin<storage_t> bin_t;
769 /** The bin of this stat. */
771 /** The parameters for this stat. */
776 * Retrieve the storage from the bin.
777 * @return The storage object for this stat.
779 storage_t *data() { return bin.data(params); }
781 * Retrieve a const pointer to the storage from the bin.
782 * @return A const pointer to the storage object for this stat.
784 const storage_t *data() const
786 bin_t *_bin = const_cast<bin_t *>(&bin);
787 params_t *_params = const_cast<params_t *>(¶ms);
788 return _bin->data(*_params);
793 * Copy constructor, copies are not allowed.
795 ScalarBase(const ScalarBase &stat);
799 const ScalarBase &operator=(const ScalarBase &);
803 * Return the current value of this stat as its base type.
804 * @return The current value.
806 T value() const { return data()->value(params); }
810 * Create and initialize this stat, register it with the database.
818 // Common operators for stats
820 * Increment the stat by 1. This calls the associated storage object inc
823 void operator++() { data()->inc(1, params); }
825 * Decrement the stat by 1. This calls the associated storage object dec
828 void operator--() { data()->dec(1, params); }
830 /** Increment the stat by 1. */
831 void operator++(int) { ++*this; }
832 /** Decrement the stat by 1. */
833 void operator--(int) { --*this; }
836 * Set the data value to the given value. This calls the associated storage
837 * object set function.
838 * @param v The new value.
840 template <typename U>
841 void operator=(const U& v) { data()->set(v, params); }
844 * Increment the stat by the given value. This calls the associated
845 * storage object inc function.
846 * @param v The value to add.
848 template <typename U>
849 void operator+=(const U& v) { data()->inc(v, params); }
852 * Decrement the stat by the given value. This calls the associated
853 * storage object dec function.
854 * @param v The value to substract.
856 template <typename U>
857 void operator-=(const U& v) { data()->dec(v, params); }
860 * Return the number of elements, always 1 for a scalar.
863 size_t size() const { return 1; }
865 * Return true if stat is binned.
866 *@return True is stat is binned.
868 bool binned() const { return bin_t::binned; }
870 bool check() const { return bin.initialized(); }
873 * Reset stat value to default
875 void reset() { bin.reset(); }
877 result_t val() { return data()->val(params); }
879 result_t total() { return val(); }
881 bool zero() { return val() == 0.0; }
884 //////////////////////////////////////////////////////////////////////
888 //////////////////////////////////////////////////////////////////////
889 template <typename T, template <typename T> class Storage, class Bin>
893 * Implementation of a vector of stats. The type of stat is determined by the
894 * Storage class. @sa ScalarBase
896 template <typename T, template <typename T> class Storage, class Bin>
897 class VectorBase : public DataAccess
900 /** Define the type of the storage class. */
901 typedef Storage<T> storage_t;
902 /** Define the params of the storage class. */
903 typedef typename storage_t::Params params_t;
904 /** Define the bin type. */
905 typedef typename Bin::VectorBin<storage_t> bin_t;
908 /** The bin of this stat. */
910 /** The parameters for this stat. */
915 * Retrieve the storage from the bin for the given index.
916 * @param index The vector index to access.
917 * @return The storage object at the given index.
919 storage_t *data(int index) { return bin.data(index, params); }
921 * Retrieve a const pointer to the storage from the bin
922 * for the given index.
923 * @param index The vector index to access.
924 * @return A const pointer to the storage object at the given index.
926 const storage_t *data(int index) const
928 bin_t *_bin = const_cast<bin_t *>(&bin);
929 params_t *_params = const_cast<params_t *>(¶ms);
930 return _bin->data(index, *_params);
934 // Copying stats is not allowed
935 /** Copying stats isn't allowed. */
936 VectorBase(const VectorBase &stat);
937 /** Copying stats isn't allowed. */
938 const VectorBase &operator=(const VectorBase &);
942 * Copy the values to a local vector and return a reference to it.
943 * @return A reference to a vector of the stat values.
945 void val(rvec_t &vec) const
948 for (int i = 0; i < size(); ++i)
949 vec[i] = data(i)->val(params);
953 * @return True is stat is binned.
955 bool binned() const { return bin_t::binned; }
958 * Return a total of all entries in this vector.
959 * @return The total of all vector entries.
961 result_t total() const {
962 result_t total = 0.0;
963 for (int i = 0; i < size(); ++i)
964 total += data(i)->val(params);
969 * @return the number of elements in this vector.
971 size_t size() const { return bin.size(); }
975 for (int i = 0; i < size(); ++i)
981 bool check() const { return bin.initialized(); }
982 void reset() { bin.reset(); }
987 /** Friend this class with the associated scalar proxy. */
988 friend class ScalarProxy<T, Storage, Bin>;
991 * Return a reference (ScalarProxy) to the stat at the given index.
992 * @param index The vector index to access.
993 * @return A reference of the stat.
995 ScalarProxy<T, Storage, Bin> operator[](int index);
997 void update(StatData *data) {}
1000 const StatData * getStatData(const void *stat);
1003 * A proxy class to access the stat at a given index in a VectorBase stat.
1004 * Behaves like a ScalarBase.
1006 template <typename T, template <typename T> class Storage, class Bin>
1010 /** Define the type of the storage class. */
1011 typedef Storage<T> storage_t;
1012 /** Define the params of the storage class. */
1013 typedef typename storage_t::Params params_t;
1014 /** Define the bin type. */
1015 typedef typename Bin::VectorBin<storage_t> bin_t;
1018 /** Pointer to the bin in the parent VectorBase. */
1020 /** Pointer to the params in the parent VectorBase. */
1022 /** The index to access in the parent VectorBase. */
1024 /** Keep a pointer to the original stat so was can get data */
1029 * Retrieve the storage from the bin.
1030 * @return The storage from the bin for this stat.
1032 storage_t *data() { return bin->data(index, *params); }
1034 * Retrieve a const pointer to the storage from the bin.
1035 * @return A const pointer to the storage for this stat.
1037 const storage_t *data() const
1039 bin_t *_bin = const_cast<bin_t *>(bin);
1040 params_t *_params = const_cast<params_t *>(params);
1041 return _bin->data(index, *_params);
1046 * Return the current value of this statas a result type.
1047 * @return The current value.
1049 result_t val() const { return data()->val(*params); }
1051 * Return the current value of this stat as its base type.
1052 * @return The current value.
1054 T value() const { return data()->value(*params); }
1058 * Create and initialize this proxy, do not register it with the database.
1059 * @param b The bin to use.
1060 * @param p The params to use.
1061 * @param i The index to access.
1063 ScalarProxy(bin_t &b, params_t &p, int i, void *s)
1064 : bin(&b), params(&p), index(i), stat(s) {}
1066 * Create a copy of the provided ScalarProxy.
1067 * @param sp The proxy to copy.
1069 ScalarProxy(const ScalarProxy &sp)
1070 : bin(sp.bin), params(sp.params), index(sp.index), stat(sp.stat) {}
1072 * Set this proxy equal to the provided one.
1073 * @param sp The proxy to copy.
1074 * @return A reference to this proxy.
1076 const ScalarProxy &operator=(const ScalarProxy &sp) {
1085 // Common operators for stats
1087 * Increment the stat by 1. This calls the associated storage object inc
1090 void operator++() { data()->inc(1, *params); }
1092 * Decrement the stat by 1. This calls the associated storage object dec
1095 void operator--() { data()->dec(1, *params); }
1097 /** Increment the stat by 1. */
1098 void operator++(int) { ++*this; }
1099 /** Decrement the stat by 1. */
1100 void operator--(int) { --*this; }
1103 * Set the data value to the given value. This calls the associated storage
1104 * object set function.
1105 * @param v The new value.
1107 template <typename U>
1108 void operator=(const U& v) { data()->set(v, *params); }
1111 * Increment the stat by the given value. This calls the associated
1112 * storage object inc function.
1113 * @param v The value to add.
1115 template <typename U>
1116 void operator+=(const U& v) { data()->inc(v, *params); }
1119 * Decrement the stat by the given value. This calls the associated
1120 * storage object dec function.
1121 * @param v The value to substract.
1123 template <typename U>
1124 void operator-=(const U& v) { data()->dec(v, *params); }
1127 * Return the number of elements, always 1 for a scalar.
1130 size_t size() const { return 1; }
1133 * Return true if stat is binned.
1134 *@return false since Proxies aren't printed/binned
1136 bool binned() const { return false; }
1139 * This stat has no state. Nothing to reset
1144 const StatData *statData() const { return getStatData(stat); }
1145 std::string str() const
1147 return csprintf("%s[%d]", statData()->name, index);
1152 template <typename T, template <typename T> class Storage, class Bin>
1153 inline ScalarProxy<T, Storage, Bin>
1154 VectorBase<T, Storage, Bin>::operator[](int index)
1156 assert (index >= 0 && index < size());
1157 return ScalarProxy<T, Storage, Bin>(bin, params, index, this);
1160 template <typename T, template <typename T> class Storage, class Bin>
1163 template <typename T, template <typename T> class Storage, class Bin>
1164 class Vector2dBase : public DataAccess
1167 typedef Storage<T> storage_t;
1168 typedef typename storage_t::Params params_t;
1169 typedef typename Bin::VectorBin<storage_t> bin_t;
1178 storage_t *data(int index) { return bin.data(index, params); }
1179 const storage_t *data(int index) const
1181 bin_t *_bin = const_cast<bin_t *>(&bin);
1182 params_t *_params = const_cast<params_t *>(¶ms);
1183 return _bin->data(index, *_params);
1187 // Copying stats is not allowed
1188 Vector2dBase(const Vector2dBase &stat);
1189 const Vector2dBase &operator=(const Vector2dBase &);
1194 void update(Vector2dDataBase *data)
1196 int size = this->size();
1197 data->vec.resize(size);
1198 for (int i = 0; i < size; ++i)
1199 data->vec[i] = this->data(i)->val(params);
1202 std::string ysubname(int i) const { return (*y_subnames)[i]; }
1204 friend class VectorProxy<T, Storage, Bin>;
1205 VectorProxy<T, Storage, Bin> operator[](int index);
1207 size_t size() const { return bin.size(); }
1208 bool zero() const { return data(0)->value(params) == 0.0; }
1211 * Reset stat value to default
1213 void reset() { bin.reset(); }
1215 bool check() { return bin.initialized(); }
1218 template <typename T, template <typename T> class Storage, class Bin>
1222 typedef Storage<T> storage_t;
1223 typedef typename storage_t::Params params_t;
1224 typedef typename Bin::VectorBin<storage_t> bin_t;
1234 mutable rvec_t *vec;
1236 storage_t *data(int index) {
1237 assert(index < len);
1238 return bin->data(offset + index, *params);
1241 const storage_t *data(int index) const {
1242 bin_t *_bin = const_cast<bin_t *>(bin);
1243 params_t *_params = const_cast<params_t *>(params);
1244 return _bin->data(offset + index, *_params);
1248 const rvec_t &val() const {
1250 vec->resize(size());
1252 vec = new rvec_t(size());
1254 for (int i = 0; i < size(); ++i)
1255 (*vec)[i] = data(i)->val(*params);
1260 result_t total() const {
1261 result_t total = 0.0;
1262 for (int i = 0; i < size(); ++i)
1263 total += data(i)->val(*params);
1268 VectorProxy(bin_t &b, params_t &p, int o, int l, void *s)
1269 : bin(&b), params(&p), offset(o), len(l), stat(s), vec(NULL)
1273 VectorProxy(const VectorProxy &sp)
1274 : bin(sp.bin), params(sp.params), offset(sp.offset), len(sp.len),
1275 stat(sp.stat), vec(NULL)
1285 const VectorProxy &operator=(const VectorProxy &sp)
1298 ScalarProxy<T, Storage, Bin> operator[](int index)
1300 assert (index >= 0 && index < size());
1301 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index,
1305 size_t size() const { return len; }
1308 * Return true if stat is binned.
1309 *@return false since Proxies aren't printed/binned
1311 bool binned() const { return false; }
1314 * This stat has no state. Nothing to reset.
1319 template <typename T, template <typename T> class Storage, class Bin>
1320 inline VectorProxy<T, Storage, Bin>
1321 Vector2dBase<T, Storage, Bin>::operator[](int index)
1323 int offset = index * y;
1324 assert (index >= 0 && offset < size());
1325 return VectorProxy<T, Storage, Bin>(bin, params, offset, y, this);
1328 //////////////////////////////////////////////////////////////////////
1330 // Non formula statistics
1332 //////////////////////////////////////////////////////////////////////
1335 * Templatized storage and interface for a distrbution stat.
1337 template <typename T>
1341 /** The parameters for a distribution stat. */
1344 /** The minimum value to track. */
1346 /** The maximum value to track. */
1348 /** The number of entries in each bucket. */
1350 /** The number of buckets. Equal to (max-min)/bucket_size. */
1353 enum { fancy = false };
1356 /** The smallest value sampled. */
1358 /** The largest value sampled. */
1360 /** The number of values sampled less than min. */
1362 /** The number of values sampled more than max. */
1364 /** The current sum. */
1366 /** The sum of squares. */
1368 /** The number of samples. */
1370 /** Counter for each bucket. */
1375 * Construct this storage with the supplied params.
1376 * @param params The parameters.
1378 DistStor(const Params ¶ms)
1379 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1380 sum(T()), squares(T()), samples(0), vec(params.size)
1386 * Add a value to the distribution for the given number of times.
1387 * @param val The value to add.
1388 * @param number The number of times to add the value.
1389 * @param params The paramters of the distribution.
1391 void sample(T val, int number, const Params ¶ms)
1393 if (val < params.min)
1394 underflow += number;
1395 else if (val > params.max)
1398 int index = (val - params.min) / params.bucket_size;
1399 assert(index < size(params));
1400 vec[index] += number;
1409 T sample = val * number;
1411 squares += sample * sample;
1416 * Return the number of buckets in this distribution.
1417 * @return the number of buckets.
1418 * @todo Is it faster to return the size from the parameters?
1420 size_t size(const Params &) const { return vec.size(); }
1423 * Returns true if any calls to sample have been made.
1424 * @param params The paramters of the distribution.
1425 * @return True if any values have been sampled.
1427 bool zero(const Params ¶ms) const
1429 return samples == 0;
1432 void update(DistDataData *data, const Params ¶ms)
1434 data->min = params.min;
1435 data->max = params.max;
1436 data->bucket_size = params.bucket_size;
1437 data->size = params.size;
1439 data->min_val = (min_val == INT_MAX) ? 0 : min_val;
1440 data->max_val = (max_val == INT_MIN) ? 0 : max_val;
1441 data->underflow = underflow;
1442 data->overflow = overflow;
1443 data->vec.resize(params.size);
1444 for (int i = 0; i < params.size; ++i)
1445 data->vec[i] = vec[i];
1448 data->squares = squares;
1449 data->samples = samples;
1453 * Reset stat value to default
1462 int size = vec.size();
1463 for (int i = 0; i < size; ++i)
1473 * Templatized storage and interface for a distribution that calculates mean
1476 template <typename T>
1481 * No paramters for this storage.
1484 enum { fancy = true };
1487 /** The current sum. */
1489 /** The sum of squares. */
1491 /** The number of samples. */
1496 * Create and initialize this storage.
1498 FancyStor(const Params &) : sum(T()), squares(T()), samples(0) {}
1501 * Add a value the given number of times to this running average.
1502 * Update the running sum and sum of squares, increment the number of
1503 * values seen by the given number.
1504 * @param val The value to add.
1505 * @param number The number of times to add the value.
1506 * @param p The parameters of this stat.
1508 void sample(T val, int number, const Params &p)
1510 T value = val * number;
1512 squares += value * value;
1516 void update(DistDataData *data, const Params ¶ms)
1519 data->squares = squares;
1520 data->samples = samples;
1524 * Return the number of entries in this stat, 1
1527 size_t size(const Params &) const { return 1; }
1530 * Return true if no samples have been added.
1531 * @return True if no samples have been added.
1533 bool zero(const Params &) const { return samples == 0; }
1536 * Reset stat value to default
1547 * Templatized storage for distribution that calculates per cycle mean and
1550 template <typename T>
1554 /** No parameters for this storage. */
1556 enum { fancy = true };
1559 /** Current total. */
1561 /** Current sum of squares. */
1566 * Create and initialize this storage.
1568 AvgFancy(const Params &) : sum(T()), squares(T()) {}
1571 * Add a value to the distribution for the given number of times.
1572 * Update the running sum and sum of squares.
1573 * @param val The value to add.
1574 * @param number The number of times to add the value.
1575 * @param p The paramters of the distribution.
1577 void sample(T val, int number, const Params& p)
1579 T value = val * number;
1581 squares += value * value;
1584 void update(DistDataData *data, const Params ¶ms)
1587 data->squares = squares;
1588 data->samples = curTick;
1592 * Return the number of entries, in this case 1.
1595 size_t size(const Params ¶ms) const { return 1; }
1597 * Return true if no samples have been added.
1598 * @return True if the sum is zero.
1600 bool zero(const Params ¶ms) const { return sum == 0; }
1602 * Reset stat value to default
1612 * Implementation of a distribution stat. The type of distribution is
1613 * determined by the Storage template. @sa ScalarBase
1615 template <typename T, template <typename T> class Storage, class Bin>
1616 class DistBase : public DataAccess
1619 /** Define the type of the storage class. */
1620 typedef Storage<T> storage_t;
1621 /** Define the params of the storage class. */
1622 typedef typename storage_t::Params params_t;
1623 /** Define the bin type. */
1624 typedef typename Bin::Bin<storage_t> bin_t;
1627 /** The bin of this stat. */
1629 /** The parameters for this stat. */
1634 * Retrieve the storage from the bin.
1635 * @return The storage object for this stat.
1637 storage_t *data() { return bin.data(params); }
1639 * Retrieve a const pointer to the storage from the bin.
1640 * @return A const pointer to the storage object for this stat.
1642 const storage_t *data() const
1644 bin_t *_bin = const_cast<bin_t *>(&bin);
1645 params_t *_params = const_cast<params_t *>(¶ms);
1646 return _bin->data(*_params);
1650 // Copying stats is not allowed
1651 /** Copies are not allowed. */
1652 DistBase(const DistBase &stat);
1653 /** Copies are not allowed. */
1654 const DistBase &operator=(const DistBase &);
1660 * Add a value to the distribtion n times. Calls sample on the storage
1662 * @param v The value to add.
1663 * @param n The number of times to add it, defaults to 1.
1665 template <typename U>
1666 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1669 * Return the number of entries in this stat.
1670 * @return The number of entries.
1672 size_t size() const { return data()->size(params); }
1674 * Return true if no samples have been added.
1675 * @return True if there haven't been any samples.
1677 bool zero() const { return data()->zero(params); }
1679 void update(DistDataBase *base)
1681 base->data.fancy = storage_t::fancy;
1682 data()->update(&(base->data), params);
1685 * @return True is stat is binned.
1687 bool binned() const { return bin_t::binned; }
1689 * Reset stat value to default
1696 bool check() { return bin.initialized(); }
1699 template <typename T, template <typename T> class Storage, class Bin>
1702 template <typename T, template <typename T> class Storage, class Bin>
1703 class VectorDistBase : public DataAccess
1706 typedef Storage<T> storage_t;
1707 typedef typename storage_t::Params params_t;
1708 typedef typename Bin::VectorBin<storage_t> bin_t;
1715 storage_t *data(int index) { return bin.data(index, params); }
1716 const storage_t *data(int index) const
1718 bin_t *_bin = const_cast<bin_t *>(&bin);
1719 params_t *_params = const_cast<params_t *>(¶ms);
1720 return _bin->data(index, *_params);
1724 // Copying stats is not allowed
1725 VectorDistBase(const VectorDistBase &stat);
1726 const VectorDistBase &operator=(const VectorDistBase &);
1731 friend class DistProxy<T, Storage, Bin>;
1732 DistProxy<T, Storage, Bin> operator[](int index);
1733 const DistProxy<T, Storage, Bin> operator[](int index) const;
1735 size_t size() const { return bin.size(); }
1736 bool zero() const { return false; }
1738 * Return true if stat is binned.
1739 *@return True is stat is binned.
1741 bool binned() const { return bin_t::binned; }
1743 * Reset stat value to default
1745 void reset() { bin.reset(); }
1747 bool check() { return bin.initialized(); }
1748 void update(VectorDistDataBase *base)
1750 int size = this->size();
1751 base->data.resize(size);
1752 for (int i = 0; i < size; ++i) {
1753 base->data[i].fancy = storage_t::fancy;
1754 data(i)->update(&(base->data[i]), params);
1759 template <typename T, template <typename T> class Storage, class Bin>
1763 typedef Storage<T> storage_t;
1764 typedef typename storage_t::Params params_t;
1765 typedef typename Bin::Bin<storage_t> bin_t;
1766 typedef VectorDistBase<T, Storage, Bin> base_t;
1771 const base_t *cstat;
1776 storage_t *data() { return stat->data(index); }
1777 const storage_t *data() const { return cstat->data(index); }
1780 DistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1781 : cstat(&s), index(i) {}
1782 DistProxy(const DistProxy &sp)
1783 : cstat(sp.cstat), index(sp.index) {}
1784 const DistProxy &operator=(const DistProxy &sp) {
1785 cstat = sp.cstat; index = sp.index; return *this;
1789 template <typename U>
1790 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1792 size_t size() const { return 1; }
1793 bool zero() const { return data()->zero(cstat->params); }
1795 * Return true if stat is binned.
1796 *@return false since Proxies are not binned/printed.
1798 bool binned() const { return false; }
1800 * Proxy has no state. Nothing to reset.
1805 template <typename T, template <typename T> class Storage, class Bin>
1806 inline DistProxy<T, Storage, Bin>
1807 VectorDistBase<T, Storage, Bin>::operator[](int index)
1809 assert (index >= 0 && index < size());
1810 return DistProxy<T, Storage, Bin>(*this, index);
1813 template <typename T, template <typename T> class Storage, class Bin>
1814 inline const DistProxy<T, Storage, Bin>
1815 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1817 assert (index >= 0 && index < size());
1818 return DistProxy<T, Storage, Bin>(*this, index);
1822 template <typename T, template <typename T> class Storage, class Bin>
1824 VectorDistBase<T, Storage, Bin>::total(int index) const
1827 for (int i=0; i < x_size(); ++i) {
1828 total += data(i)->val(*params);
1833 //////////////////////////////////////////////////////////////////////
1837 //////////////////////////////////////////////////////////////////////
1840 * Base class for formula statistic node. These nodes are used to build a tree
1841 * that represents the formula.
1843 class Node : public RefCounted
1847 * Return the number of nodes in the subtree starting at this node.
1848 * @return the number of nodes in this subtree.
1850 virtual size_t size() const = 0;
1852 * Return the result vector of this subtree.
1853 * @return The result vector of this subtree.
1855 virtual const rvec_t &val() const = 0;
1857 * Return the total of the result vector.
1858 * @return The total of the result vector.
1860 virtual result_t total() const = 0;
1862 * Return true if stat is binned.
1863 *@return True is stat is binned.
1865 virtual bool binned() const = 0;
1870 virtual std::string str() const = 0;
1873 /** Reference counting pointer to a function Node. */
1874 typedef RefCountingPtr<Node> NodePtr;
1876 class ScalarStatNode : public Node
1879 const ScalarDataBase *data;
1880 mutable rvec_t result;
1883 ScalarStatNode(const ScalarDataBase *d) : data(d), result(1) {}
1884 virtual const rvec_t &val() const
1886 result[0] = data->val();
1889 virtual result_t total() const { return data->val(); };
1891 virtual size_t size() const { return 1; }
1893 * Return true if stat is binned.
1894 *@return True is stat is binned.
1896 virtual bool binned() const { return data->binned(); }
1901 virtual std::string str() const { return data->name; }
1904 template <typename T, template <typename T> class Storage, class Bin>
1905 class ScalarProxyNode : public Node
1908 const ScalarProxy<T, Storage, Bin> proxy;
1909 mutable rvec_t result;
1912 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1913 : proxy(p), result(1) { }
1914 virtual const rvec_t &val() const
1916 result[0] = proxy.val();
1919 virtual result_t total() const { return proxy.val(); };
1921 virtual size_t size() const { return 1; }
1923 * Return true if stat is binned.
1924 *@return True is stat is binned.
1926 virtual bool binned() const { return proxy.binned(); }
1931 virtual std::string str() const { return proxy.str(); }
1934 class VectorStatNode : public Node
1937 const VectorDataBase *data;
1940 VectorStatNode(const VectorDataBase *d) : data(d) { }
1941 virtual const rvec_t &val() const { return data->val(); }
1942 virtual result_t total() const { return data->total(); };
1944 virtual size_t size() const { return data->size(); }
1946 * Return true if stat is binned.
1947 *@return True is stat is binned.
1949 virtual bool binned() const { return data->binned(); }
1951 virtual std::string str() const { return data->name; }
1954 template <typename T>
1955 class ConstNode : public Node
1961 ConstNode(T s) : data(1, (result_t)s) {}
1962 const rvec_t &val() const { return data; }
1963 virtual result_t total() const { return data[0]; };
1965 virtual size_t size() const { return 1; }
1967 * Return true if stat is binned.
1968 *@return False since constants aren't binned.
1970 virtual bool binned() const { return false; }
1972 virtual std::string str() const { return to_string(data[0]); }
1975 template <typename T>
1976 class FunctorNode : public Node
1980 mutable rvec_t result;
1983 FunctorNode(T &f) : functor(f) { result.resize(1); }
1984 const rvec_t &val() const {
1985 result[0] = (result_t)functor();
1988 virtual result_t total() const { return (result_t)functor(); };
1990 virtual size_t size() const { return 1; }
1992 * Return true if stat is binned.
1993 *@return False since Functors aren't binned
1995 virtual bool binned() const { return false; }
1996 virtual std::string str() const { return to_string(functor()); }
1999 template <typename T>
2000 class ScalarNode : public Node
2004 mutable rvec_t result;
2007 ScalarNode(T &s) : scalar(s) { result.resize(1); }
2008 const rvec_t &val() const {
2009 result[0] = (result_t)scalar;
2012 virtual result_t total() const { return (result_t)scalar; };
2014 virtual size_t size() const { return 1; }
2016 * Return true if stat is binned.
2017 *@return False since Scalar's aren't binned
2019 virtual bool binned() const { return false; }
2020 virtual std::string str() const { return to_string(scalar); }
2027 struct OpString<std::plus<result_t> >
2029 static std::string str() { return "+"; }
2033 struct OpString<std::minus<result_t> >
2035 static std::string str() { return "-"; }
2039 struct OpString<std::multiplies<result_t> >
2041 static std::string str() { return "*"; }
2045 struct OpString<std::divides<result_t> >
2047 static std::string str() { return "/"; }
2051 struct OpString<std::modulus<result_t> >
2053 static std::string str() { return "%"; }
2057 struct OpString<std::negate<result_t> >
2059 static std::string str() { return "-"; }
2063 class UnaryNode : public Node
2067 mutable rvec_t result;
2070 UnaryNode(NodePtr &p) : l(p) {}
2072 const rvec_t &val() const {
2073 const rvec_t &lvec = l->val();
2074 int size = lvec.size();
2078 result.resize(size);
2080 for (int i = 0; i < size; ++i)
2081 result[i] = op(lvec[i]);
2086 result_t total() const {
2088 return op(l->total());
2091 virtual size_t size() const { return l->size(); }
2093 * Return true if child of node is binned.
2094 *@return True if child of node is binned.
2096 virtual bool binned() const { return l->binned(); }
2098 virtual std::string str() const
2100 return OpString<Op>::str() + l->str();
2105 class BinaryNode : public Node
2110 mutable rvec_t result;
2113 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2115 const rvec_t &val() const {
2117 const rvec_t &lvec = l->val();
2118 const rvec_t &rvec = r->val();
2120 assert(lvec.size() > 0 && rvec.size() > 0);
2122 if (lvec.size() == 1 && rvec.size() == 1) {
2124 result[0] = op(lvec[0], rvec[0]);
2125 } else if (lvec.size() == 1) {
2126 int size = rvec.size();
2127 result.resize(size);
2128 for (int i = 0; i < size; ++i)
2129 result[i] = op(lvec[0], rvec[i]);
2130 } else if (rvec.size() == 1) {
2131 int size = lvec.size();
2132 result.resize(size);
2133 for (int i = 0; i < size; ++i)
2134 result[i] = op(lvec[i], rvec[0]);
2135 } else if (rvec.size() == lvec.size()) {
2136 int size = rvec.size();
2137 result.resize(size);
2138 for (int i = 0; i < size; ++i)
2139 result[i] = op(lvec[i], rvec[i]);
2145 result_t total() const {
2147 return op(l->total(), r->total());
2150 virtual size_t size() const {
2158 assert(ls == rs && "Node vector sizes are not equal");
2163 * Return true if any children of node are binned
2164 *@return True if either child of node is binned.
2166 virtual bool binned() const { return (l->binned() || r->binned()); }
2168 virtual std::string str() const
2170 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2175 class SumNode : public Node
2179 mutable rvec_t result;
2182 SumNode(NodePtr &p) : l(p), result(1) {}
2184 const rvec_t &val() const {
2185 const rvec_t &lvec = l->val();
2186 int size = lvec.size();
2192 for (int i = 0; i < size; ++i)
2193 result[0] = op(result[0], lvec[i]);
2198 result_t total() const {
2199 const rvec_t &lvec = l->val();
2200 int size = lvec.size();
2203 result_t result = 0.0;
2206 for (int i = 0; i < size; ++i)
2207 result = op(result, lvec[i]);
2212 virtual size_t size() const { return 1; }
2214 * Return true if child of node is binned.
2215 *@return True if child of node is binned.
2217 virtual bool binned() const { return l->binned(); }
2219 virtual std::string str() const
2221 return csprintf("total(%s)", l->str());
2225 //////////////////////////////////////////////////////////////////////
2227 // Binning Interface
2229 //////////////////////////////////////////////////////////////////////
2233 friend class MainBin::BinBase;
2241 off_t size() const { return memsize; }
2242 char *memory(off_t off);
2245 static MainBin *&curBin()
2247 static MainBin *current = NULL;
2251 static void setCurBin(MainBin *bin) { curBin() = bin; }
2252 static MainBin *current() { assert(curBin()); return curBin(); }
2254 static off_t &offset()
2256 static off_t offset = 0;
2260 static off_t new_offset(size_t size)
2262 size_t mask = sizeof(u_int64_t) - 1;
2263 off_t off = offset();
2265 // That one is for the last trailing flags byte.
2266 offset() += (size + 1 + mask) & ~mask;
2271 MainBin(const std::string &name);
2292 BinBase() : offset(-1) {}
2293 void allocate(size_t size)
2295 offset = new_offset(size);
2299 assert(offset != -1);
2300 return current()->memory(offset);
2304 template <class Storage>
2305 class Bin : public BinBase
2308 typedef typename Storage::Params Params;
2311 enum { binned = true };
2312 Bin() { allocate(sizeof(Storage)); }
2313 bool initialized() const { return true; }
2314 void init(Params ¶ms) { }
2316 int size() const { return 1; }
2319 data(Params ¶ms)
2321 assert(initialized());
2322 char *ptr = access();
2323 char *flags = ptr + sizeof(Storage);
2324 if (!(*flags & 0x1)) {
2326 new (ptr) Storage(params);
2328 return reinterpret_cast<Storage *>(ptr);
2334 char *ptr = access();
2335 char *flags = ptr + size() * sizeof(Storage);
2336 if (!(*flags & 0x1))
2339 Storage *s = reinterpret_cast<Storage *>(ptr);
2344 template <class Storage>
2345 class VectorBin : public BinBase
2348 typedef typename Storage::Params Params;
2354 enum { binned = true };
2355 VectorBin() : _size(0) {}
2357 bool initialized() const { return _size > 0; }
2358 void init(int s, Params ¶ms)
2360 assert(!initialized());
2363 allocate(_size * sizeof(Storage));
2366 int size() const { return _size; }
2368 Storage *data(int index, Params ¶ms)
2370 assert(initialized());
2371 assert(index >= 0 && index < size());
2372 char *ptr = access();
2373 char *flags = ptr + size() * sizeof(Storage);
2374 if (!(*flags & 0x1)) {
2376 for (int i = 0; i < size(); ++i)
2377 new (ptr + i * sizeof(Storage)) Storage(params);
2379 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2383 char *ptr = access();
2384 char *flags = ptr + size() * sizeof(Storage);
2385 if (!(*flags & 0x1))
2388 for (int i = 0; i < _size; ++i) {
2389 char *p = ptr + i * sizeof(Storage);
2390 Storage *s = reinterpret_cast<Storage *>(p);
2399 template <class Storage>
2403 typedef typename Storage::Params Params;
2404 enum { binned = false };
2407 char ptr[sizeof(Storage)];
2412 reinterpret_cast<Storage *>(ptr)->~Storage();
2415 bool initialized() const { return true; }
2416 void init(Params ¶ms)
2418 new (ptr) Storage(params);
2420 int size() const{ return 1; }
2421 Storage *data(Params ¶ms)
2423 assert(initialized());
2424 return reinterpret_cast<Storage *>(ptr);
2428 Storage *s = reinterpret_cast<Storage *>(ptr);
2433 template <class Storage>
2437 typedef typename Storage::Params Params;
2438 enum { binned = false };
2445 VectorBin() : ptr(NULL) { }
2451 for (int i = 0; i < _size; ++i) {
2452 char *p = ptr + i * sizeof(Storage);
2453 reinterpret_cast<Storage *>(p)->~Storage();
2458 bool initialized() const { return ptr != NULL; }
2459 void init(int s, Params ¶ms)
2461 assert(s > 0 && "size must be positive!");
2462 assert(!initialized());
2464 ptr = new char[_size * sizeof(Storage)];
2465 for (int i = 0; i < _size; ++i)
2466 new (ptr + i * sizeof(Storage)) Storage(params);
2469 int size() const { return _size; }
2471 Storage *data(int index, Params ¶ms)
2473 assert(initialized());
2474 assert(index >= 0 && index < size());
2475 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2479 for (int i = 0; i < _size; ++i) {
2480 char *p = ptr + i * sizeof(Storage);
2481 Storage *s = reinterpret_cast<Storage *>(p);
2488 //////////////////////////////////////////////////////////////////////
2490 // Visible Statistics Types
2492 //////////////////////////////////////////////////////////////////////
2494 * @defgroup VisibleStats "Statistic Types"
2495 * These are the statistics that are used in the simulator. By default these
2496 * store counters and don't use binning, but are templatized to accept any type
2497 * and any Bin class.
2502 * This is an easy way to assign all your stats to be binned or not
2503 * binned. If the typedef is NoBin, nothing is binned. If it is
2504 * MainBin, then all stats are binned under that Bin.
2507 typedef MainBin DefaultBin;
2509 typedef NoBin DefaultBin;
2513 * This is a simple scalar statistic, like a counter.
2514 * @sa Stat, ScalarBase, StatStor
2516 template <typename T = Counter, class Bin = DefaultBin>
2518 : public Wrap<Scalar<T, Bin>,
2519 ScalarBase<T, StatStor, Bin>,
2523 /** The base implementation. */
2524 typedef ScalarBase<T, StatStor, Bin> Base;
2532 * Sets the stat equal to the given value. Calls the base implementation
2534 * @param v The new value.
2536 template <typename U>
2537 void operator=(const U& v) { Base::operator=(v); }
2541 * A stat that calculates the per cycle average of a value.
2542 * @sa Stat, ScalarBase, AvgStor
2544 template <typename T = Counter, class Bin = DefaultBin>
2546 : public Wrap<Average<T, Bin>,
2547 ScalarBase<T, AvgStor, Bin>,
2551 /** The base implementation. */
2552 typedef ScalarBase<T, AvgStor, Bin> Base;
2560 * Sets the stat equal to the given value. Calls the base implementation
2562 * @param v The new value.
2564 template <typename U>
2565 void operator=(const U& v) { Base::operator=(v); }
2569 * A vector of scalar stats.
2570 * @sa Stat, VectorBase, StatStor
2572 template <typename T = Counter, class Bin = DefaultBin>
2574 : public WrapVec<Vector<T, Bin>,
2575 VectorBase<T, StatStor, Bin>,
2579 /** The base implementation. */
2580 typedef ScalarBase<T, StatStor, Bin> Base;
2583 * Set this vector to have the given size.
2584 * @param size The new size.
2585 * @return A reference to this stat.
2587 Vector &init(size_t size) {
2588 bin.init(size, params);
2596 * A vector of Average stats.
2597 * @sa Stat, VectorBase, AvgStor
2599 template <typename T = Counter, class Bin = DefaultBin>
2601 : public WrapVec<AverageVector<T, Bin>,
2602 VectorBase<T, AvgStor, Bin>,
2607 * Set this vector to have the given size.
2608 * @param size The new size.
2609 * @return A reference to this stat.
2611 AverageVector &init(size_t size) {
2612 bin.init(size, params);
2620 * A 2-Dimensional vecto of scalar stats.
2621 * @sa Stat, Vector2dBase, StatStor
2623 template <typename T = Counter, class Bin = DefaultBin>
2625 : public WrapVec2d<Vector2d<T, Bin>,
2626 Vector2dBase<T, StatStor, Bin>,
2630 Vector2d &init(size_t _x, size_t _y) {
2631 statData()->x = x = _x;
2632 statData()->y = y = _y;
2633 bin.init(x * y, params);
2641 * A simple distribution stat.
2642 * @sa Stat, DistBase, DistStor
2644 template <typename T = Counter, class Bin = DefaultBin>
2646 : public Wrap<Distribution<T, Bin>,
2647 DistBase<T, DistStor, Bin>,
2651 /** Base implementation. */
2652 typedef DistBase<T, DistStor, Bin> Base;
2653 /** The Parameter type. */
2654 typedef typename DistStor<T>::Params Params;
2658 * Set the parameters of this distribution. @sa DistStor::Params
2659 * @param min The minimum value of the distribution.
2660 * @param max The maximum value of the distribution.
2661 * @param bkt The number of values in each bucket.
2662 * @return A reference to this distribution.
2664 Distribution &init(T min, T max, int bkt) {
2667 params.bucket_size = bkt;
2668 params.size = (max - min) / bkt + 1;
2677 * Calculates the mean and variance of all the samples.
2678 * @sa Stat, DistBase, FancyStor
2680 template <typename T = Counter, class Bin = DefaultBin>
2681 class StandardDeviation
2682 : public Wrap<StandardDeviation<T, Bin>,
2683 DistBase<T, FancyStor, Bin>,
2687 /** The base implementation */
2688 typedef DistBase<T, DistStor, Bin> Base;
2689 /** The parameter type. */
2690 typedef typename DistStor<T>::Params Params;
2694 * Construct and initialize this distribution.
2696 StandardDeviation() {
2703 * Calculates the per cycle mean and variance of the samples.
2704 * @sa Stat, DistBase, AvgFancy
2706 template <typename T = Counter, class Bin = DefaultBin>
2707 class AverageDeviation
2708 : public Wrap<AverageDeviation<T, Bin>,
2709 DistBase<T, AvgFancy, Bin>,
2713 /** The base implementation */
2714 typedef DistBase<T, DistStor, Bin> Base;
2715 /** The parameter type. */
2716 typedef typename DistStor<T>::Params Params;
2720 * Construct and initialize this distribution.
2730 * A vector of distributions.
2731 * @sa Stat, VectorDistBase, DistStor
2733 template <typename T = Counter, class Bin = DefaultBin>
2734 class VectorDistribution
2735 : public WrapVec<VectorDistribution<T, Bin>,
2736 VectorDistBase<T, DistStor, Bin>,
2740 /** The base implementation */
2741 typedef VectorDistBase<T, DistStor, Bin> Base;
2742 /** The parameter type. */
2743 typedef typename DistStor<T>::Params Params;
2747 * Initialize storage and parameters for this distribution.
2748 * @param size The size of the vector (the number of distributions).
2749 * @param min The minimum value of the distribution.
2750 * @param max The maximum value of the distribution.
2751 * @param bkt The number of values in each bucket.
2752 * @return A reference to this distribution.
2754 VectorDistribution &init(int size, T min, T max, int bkt) {
2757 params.bucket_size = bkt;
2758 params.size = (max - min) / bkt + 1;
2759 bin.init(size, params);
2767 * This is a vector of StandardDeviation stats.
2768 * @sa Stat, VectorDistBase, FancyStor
2770 template <typename T = Counter, class Bin = DefaultBin>
2771 class VectorStandardDeviation
2772 : public WrapVec<VectorStandardDeviation<T, Bin>,
2773 VectorDistBase<T, FancyStor, Bin>,
2777 /** The base implementation */
2778 typedef VectorDistBase<T, FancyStor, Bin> Base;
2779 /** The parameter type. */
2780 typedef typename DistStor<T>::Params Params;
2784 * Initialize storage for this distribution.
2785 * @param size The size of the vector.
2786 * @return A reference to this distribution.
2788 VectorStandardDeviation &init(int size) {
2789 bin.init(size, params);
2797 * This is a vector of AverageDeviation stats.
2798 * @sa Stat, VectorDistBase, AvgFancy
2800 template <typename T = Counter, class Bin = DefaultBin>
2801 class VectorAverageDeviation
2802 : public WrapVec<VectorAverageDeviation<T, Bin>,
2803 VectorDistBase<T, AvgFancy, Bin>,
2807 /** The base implementation */
2808 typedef VectorDistBase<T, AvgFancy, Bin> Base;
2809 /** The parameter type. */
2810 typedef typename DistStor<T>::Params Params;
2814 * Initialize storage for this distribution.
2815 * @param size The size of the vector.
2816 * @return A reference to this distribution.
2818 VectorAverageDeviation &init(int size) {
2819 bin.init(size, params);
2827 * A formula for statistics that is calculated when printed. A formula is
2828 * stored as a tree of Nodes that represent the equation to calculate.
2829 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2831 class FormulaBase : public DataAccess
2834 /** The root of the tree which represents the Formula */
2840 * Return the result of the Fomula in a vector. If there were no Vector
2841 * components to the Formula, then the vector is size 1. If there were,
2842 * like x/y with x being a vector of size 3, then the result returned will
2843 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2844 * @return The result vector.
2846 void val(rvec_t &vec) const;
2849 * Return the total Formula result. If there is a Vector
2850 * component to this Formula, then this is the result of the
2851 * Formula if the formula is applied after summing all the
2852 * components of the Vector. For example, if Formula is x/y where
2853 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2854 * there is no Vector component, total() returns the same value as
2855 * the first entry in the rvec_t val() returns.
2856 * @return The total of the result vector.
2858 result_t total() const;
2861 * Return the number of elements in the tree.
2863 size_t size() const;
2866 * Return true if Formula is binned. i.e. any of its children
2868 * @return True if Formula is binned.
2870 bool binned() const;
2872 bool check() const { return true; }
2875 * Formulas don't need to be reset
2887 void update(StatData *);
2889 std::string str() const;
2892 class FormulaDataBase : public VectorDataBase
2895 virtual std::string str() const = 0;
2896 virtual bool check() const { return true; }
2897 virtual void python(Python &py) const;
2901 class FormulaData : public FormulaDataBase
2908 FormulaData(T &stat) : s(stat) {}
2910 virtual bool binned() const { return s.binned(); }
2911 virtual bool zero() const { return s.zero(); }
2912 virtual void reset() { s.reset(); }
2914 virtual size_t size() const { return s.size(); }
2915 virtual const rvec_t &val() const
2920 virtual result_t total() const { return s.total(); }
2921 virtual void update()
2923 VectorDataBase::update();
2926 virtual std::string str() const { return s.str(); }
2931 : public WrapVec<Formula,
2937 * Create and initialize thie formula, and register it with the database.
2942 * Create a formula with the given root node, register it with the
2944 * @param r The root of the expression tree.
2949 * Set an unitialized Formula to the given root.
2950 * @param r The root of the expression tree.
2951 * @return a reference to this formula.
2953 const Formula &operator=(Temp r);
2956 * Add the given tree to the existing one.
2957 * @param r The root of the expression tree.
2958 * @return a reference to this formula.
2960 const Formula &operator+=(Temp r);
2963 class FormulaNode : public Node
2966 const Formula &formula;
2970 FormulaNode(const Formula &f) : formula(f) {}
2972 virtual size_t size() const { return formula.size(); }
2973 virtual const rvec_t &val() const { formula.val(vec); return vec; }
2974 virtual result_t total() const { return formula.total(); }
2975 virtual bool binned() const { return formula.binned(); }
2977 virtual std::string str() const { return formula.str(); }
2981 * Helper class to construct formula node trees.
2987 * Pointer to a Node object.
2993 * Copy the given pointer to this class.
2994 * @param n A pointer to a Node object to copy.
2996 Temp(NodePtr n) : node(n) { }
2999 * Return the node pointer.
3000 * @return the node pointer.
3002 operator NodePtr&() { return node;}
3006 * Create a new ScalarStatNode.
3007 * @param s The ScalarStat to place in a node.
3009 template <typename T, class Bin>
3010 Temp(const Scalar<T, Bin> &s)
3011 : node(new ScalarStatNode(s.statData())) { }
3014 * Create a new ScalarStatNode.
3015 * @param s The ScalarStat to place in a node.
3017 template <typename T, class Bin>
3018 Temp(const Average<T, Bin> &s)
3019 : node(new ScalarStatNode(s.statData())) { }
3022 * Create a new VectorStatNode.
3023 * @param s The VectorStat to place in a node.
3025 template <typename T, class Bin>
3026 Temp(const Vector<T, Bin> &s)
3027 : node(new VectorStatNode(s.statData())) { }
3032 Temp(const Formula &f)
3033 : node(new FormulaNode(f)) { }
3036 * Create a new ScalarProxyNode.
3037 * @param p The ScalarProxy to place in a node.
3039 template <typename T, template <typename T> class Storage, class Bin>
3040 Temp(const ScalarProxy<T, Storage, Bin> &p)
3041 : node(new ScalarProxyNode<T, Storage, Bin>(p)) { }
3044 * Create a ConstNode
3045 * @param value The value of the const node.
3047 Temp(signed char value)
3048 : node(new ConstNode<signed char>(value)) {}
3051 * Create a ConstNode
3052 * @param value The value of the const node.
3054 Temp(unsigned char value)
3055 : node(new ConstNode<unsigned char>(value)) {}
3058 * Create a ConstNode
3059 * @param value The value of the const node.
3061 Temp(signed short value)
3062 : node(new ConstNode<signed short>(value)) {}
3065 * Create a ConstNode
3066 * @param value The value of the const node.
3068 Temp(unsigned short value)
3069 : node(new ConstNode<unsigned short>(value)) {}
3072 * Create a ConstNode
3073 * @param value The value of the const node.
3075 Temp(signed int value)
3076 : node(new ConstNode<signed int>(value)) {}
3079 * Create a ConstNode
3080 * @param value The value of the const node.
3082 Temp(unsigned int value)
3083 : node(new ConstNode<unsigned int>(value)) {}
3086 * Create a ConstNode
3087 * @param value The value of the const node.
3089 Temp(signed long value)
3090 : node(new ConstNode<signed long>(value)) {}
3093 * Create a ConstNode
3094 * @param value The value of the const node.
3096 Temp(unsigned long value)
3097 : node(new ConstNode<unsigned long>(value)) {}
3100 * Create a ConstNode
3101 * @param value The value of the const node.
3103 Temp(signed long long value)
3104 : node(new ConstNode<signed long long>(value)) {}
3107 * Create a ConstNode
3108 * @param value The value of the const node.
3110 Temp(unsigned long long value)
3111 : node(new ConstNode<unsigned long long>(value)) {}
3114 * Create a ConstNode
3115 * @param value The value of the const node.
3118 : node(new ConstNode<float>(value)) {}
3121 * Create a ConstNode
3122 * @param value The value of the const node.
3125 : node(new ConstNode<double>(value)) {}
3134 void dump(std::ostream &stream, DisplayMode mode = DefaultMode);
3135 void python_start(const std::string &file);
3136 void python_dump(const std::string &name, const std::string &subname);
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__