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
41 * -- these both can use the same function that prints out a
42 * specific set of stats
43 * VectorStandardDeviation totals
46 #ifndef __STATISTICS_HH__
47 #define __STATISTICS_HH__
64 /** Define Not a number. */
66 /** Need to define __nan() */
70 /** Print stats out in SS format. */
71 #define STAT_DISPLAY_COMPAT
73 /** The current simulated cycle. */
76 /* A namespace for all of the Statistics */
77 namespace Statistics {
78 /** All results are doubles. */
79 typedef double result_t;
80 /** A vector to hold results. */
81 typedef std::vector<result_t> rvec_t;
84 * Define the storage for format flags.
85 * @todo Can probably shrink this.
87 typedef u_int32_t FormatFlags;
88 /** Nothing extra to print. */
89 const FormatFlags none = 0x0000;
90 /** Print the total. */
91 const FormatFlags total = 0x0001;
92 /** Print the percent of the total that this entry represents. */
93 const FormatFlags pdf = 0x0002;
94 /** Don't print if this is zero. */
95 const FormatFlags nozero = 0x0004;
96 /** Don't print if this is NAN */
97 const FormatFlags nonan = 0x0008;
98 /** Print the cumulative percentage of total upto this entry. */
99 const FormatFlags cdf = 0x0010;
100 /** Print the distribution. */
101 const FormatFlags dist = 0x0020;
102 /** Used for SS compatability. */
103 const FormatFlags __substat = 0x8000;
104 /** Mask of flags that can't be set directly */
105 const FormatFlags __reserved = __substat;
107 /* Contains the statistic implementation details */
109 //////////////////////////////////////////////////////////////////////
111 // Statistics Framework Base classes
113 //////////////////////////////////////////////////////////////////////
118 * Common base class for all statistics, used to maintain a list and print.
119 * This class holds no data itself but is used to find the associated
120 * StatData in the stat database @sa Statistics::Database.
125 /** Mark this statistics as initialized. */
128 * Finds and returns the associated StatData from the database.
129 * @return The formatting and output data of this statistic.
133 * Finds and returns a const pointer to the associated StatData.
134 * @return The formatting and output data of this statistic.
136 const StatData *mydata() const;
138 * Mark this stat for output at the end of simulation.
139 * @return The formatting and output data of this statistic.
143 * Finds and returns the SubData at the given index.
144 * @param index The index of the SubData to find.
145 * @return The name and description of the given index.
147 const SubData *mysubdata(int index) const;
149 * Create and return a new SubData field for the given index.
150 * @param index The index to create a SubData for.
151 * @return A pointer to the created SubData.
153 SubData *mysubdata_create(int index);
157 * Return the name of this stat.
158 * @return the name of the stat.
160 virtual std::string myname() const;
162 * Return the name of the sub field at the given index.
163 * @param index the subfield index.
164 * @return the name of the subfield.
166 virtual std::string mysubname(int index) const;
168 * Return the description of this stat.
169 * @return the description of this stat.
171 virtual std::string mydesc() const;
173 * Return the description of the subfield at the given index.
174 * @param index The subfield index.
175 * @return the description of the subfield.
177 virtual std::string mysubdesc(int index) const;
179 * Return the format flags of this stat.
180 * @return the format flags.
182 virtual FormatFlags myflags() const;
184 * Return true if this stat's prereqs have been satisfied (they are non
186 * @return true if the prerequisite stats aren't zero.
188 virtual bool dodisplay() const;
190 * Return the display percision.
191 * @return The display precision.
193 virtual int myprecision() const;
197 * Create this stat and register it if reg is true.
198 * @param reg Register this stat in the database?
207 * Print this stat to the given ostream.
208 * @param stream The stream to print to.
210 virtual void display(std::ostream &stream) const = 0;
212 * Return the number of entries in this stat.
213 * @return The number of entries.
215 virtual size_t size() const = 0;
217 * Return true if the stat has value zero.
218 * @return True if the stat is zero.
220 virtual bool zero() const = 0;
224 * Set the name and marks this stat to print at the end of simulation.
225 * @param name The new name.
226 * @return A reference to this stat.
228 Stat &name(const std::string &name);
230 * Set the description and marks this stat to print at the end of
232 * @param desc The new description.
233 * @return A reference to this stat.
235 Stat &desc(const std::string &desc);
237 * Set the precision and marks this stat to print at the end of simulation.
238 * @param p The new precision
239 * @return A reference to this stat.
241 Stat &precision(int p);
243 * Set the flags and marks this stat to print at the end of simulation.
244 * @param f The new flags.
245 * @return A reference to this stat.
247 Stat &flags(FormatFlags f);
249 * Set the prerequisite stat and marks this stat to print at the end of
251 * @param prereq The prerequisite stat.
252 * @return A reference to this stat.
254 Stat &prereq(const Stat &prereq);
256 * Set the subfield name for the given index, and marks this stat to print
257 * at the end of simulation.
258 * @param index The subfield index.
259 * @param name The new name of the subfield.
260 * @return A reference to this stat.
262 Stat &subname(int index, const std::string &name);
264 * Set the subfield description for the given index and marks this stat to
265 * print at the end of simulation.
266 * @param index The subfield index.
267 * @param desc The new description of the subfield
268 * @return A reference to this stat.
270 Stat &subdesc(int index, const std::string &desc);
274 * Checks if the first stat's name is alphabetically less than the second.
275 * This function breaks names up at periods and considers each subname
277 * @param stat1 The first stat.
278 * @param stat2 The second stat.
279 * @return stat1's name is alphabetically before stat2's
281 static bool less(Stat *stat1, Stat *stat2);
284 /** A unique ID used for debugging. */
289 // Scalar stats involved in formulas
290 class ScalarStat : public Stat
293 ScalarStat(bool reg) : Stat(reg) {}
294 virtual result_t val() const = 0;
295 virtual bool zero() const;
296 virtual void display(std::ostream &stream) const;
300 VectorDisplay(std::ostream &stream, const std::string &myname,
301 const std::vector<std::string> *mysubnames,
302 const std::string &mydesc,
303 const std::vector<std::string> *mysubdescs,
304 int myprecision, FormatFlags myflags, const rvec_t &vec,
307 // Vector stats involved in formulas
308 class VectorStat : public Stat
311 VectorStat(bool reg) : Stat(reg) {}
312 virtual const rvec_t &val() const = 0;
313 virtual result_t total() const = 0;
314 virtual bool zero() const;
315 virtual void display(std::ostream &stream) const;
318 //////////////////////////////////////////////////////////////////////
322 //////////////////////////////////////////////////////////////////////
323 template <typename T>
333 StatStor(const Params &) : data(T()) {}
335 void set(T val, const Params &p) { data = val; }
336 void inc(T val, const Params &p) { data += val; }
337 void dec(T val, const Params &p) { data -= val; }
338 result_t val(const Params &p) const { return (result_t)data; }
339 T value(const Params &p) const { return data; }
342 template <typename T>
350 mutable result_t total;
354 AvgStor(const Params &) : current(T()), total(0), last(0) { }
356 void set(T val, const Params &p) {
357 total += current * (curTick - last);
361 void inc(T val, const Params &p) { set(current + val, p); }
362 void dec(T val, const Params &p) { set(current - val, p); }
363 result_t val(const Params &p) const {
364 total += current * (curTick - last);
366 return (result_t)(total + current) / (result_t)(curTick + 1);
368 T value(const Params &p) const { return current; }
371 template <typename T, template <typename T> class Storage, class Bin>
372 class ScalarBase : public ScalarStat
375 typedef Storage<T> storage_t;
376 typedef typename storage_t::Params params_t;
377 typedef typename Bin::Bin<storage_t> bin_t;
384 storage_t *data() { return bin.data(params); }
385 const storage_t *data() const {
386 return (const_cast<bin_t *>(&bin))->data(params);
390 // Copying stats is not allowed
391 ScalarBase(const ScalarBase &stat);
392 const ScalarBase &operator=(const ScalarBase &);
395 result_t val() const { return data()->val(params); }
396 T value() const { return data()->value(params); }
399 ScalarBase() : ScalarStat(true) {
405 // Common operators for stats
406 void operator++() { data()->inc(1, params); }
407 void operator--() { data()->dec(1, params); }
409 void operator++(int) { ++*this; }
410 void operator--(int) { --*this; }
412 template <typename U>
413 void operator=(const U& v) { data()->set(v, params); }
415 template <typename U>
416 void operator+=(const U& v) { data()->inc(v, params); }
418 template <typename U>
419 void operator-=(const U& v) { data()->dec(v, params); }
421 virtual size_t size() const { return 1; }
424 //////////////////////////////////////////////////////////////////////
428 //////////////////////////////////////////////////////////////////////
429 template <typename T, template <typename T> class Storage, class Bin>
432 template <typename T, template <typename T> class Storage, class Bin>
433 class VectorBase : public VectorStat
436 typedef Storage<T> storage_t;
437 typedef typename storage_t::Params params_t;
438 typedef typename Bin::VectorBin<storage_t> bin_t;
448 storage_t *data(int index) { return bin.data(index, params); }
449 const storage_t *data(int index) const {
450 return (const_cast<bin_t *>(&bin))->data(index, params);
454 // Copying stats is not allowed
455 VectorBase(const VectorBase &stat);
456 const VectorBase &operator=(const VectorBase &);
459 const rvec_t &val() const {
463 vec = new rvec_t(size());
465 for (int i = 0; i < size(); ++i)
466 (*vec)[i] = data(i)->val(params);
471 result_t total() const {
472 result_t total = 0.0;
473 for (int i = 0; i < size(); ++i)
474 total += data(i)->val(params);
479 VectorBase() : VectorStat(true), vec(NULL) {}
480 ~VectorBase() { if (vec) delete vec; }
482 VectorBase &init(size_t size) {
483 bin.init(size, params);
489 friend class ScalarProxy<T, Storage, Bin>;
490 ScalarProxy<T, Storage, Bin> operator[](int index);
492 virtual size_t size() const { return bin.size(); }
495 template <typename T, template <typename T> class Storage, class Bin>
496 class ScalarProxy : public ScalarStat
499 typedef Storage<T> storage_t;
500 typedef typename storage_t::Params params_t;
501 typedef typename Bin::VectorBin<storage_t> bin_t;
509 storage_t *data() { return bin->data(index, *params); }
510 const storage_t *data() const { return bin->data(index, *params); }
513 result_t val() const { return data()->val(*params); }
514 T value() const { return data()->value(*params); }
517 ScalarProxy(bin_t &b, params_t &p, int i)
518 : ScalarStat(false), bin(&b), params(&p), index(i) {}
519 ScalarProxy(const ScalarProxy &sp)
520 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
521 const ScalarProxy &operator=(const ScalarProxy &sp) {
529 // Common operators for stats
530 void operator++() { data()->inc(1, *params); }
531 void operator--() { data()->dec(1, *params); }
533 void operator++(int) { ++*this; }
534 void operator--(int) { --*this; }
536 template <typename U>
537 void operator=(const U& v) { data()->set(v, *params); }
539 template <typename U>
540 void operator+=(const U& v) { data()->inc(v, *params); }
542 template <typename U>
543 void operator-=(const U& v) { data()->dec(v, *params); }
545 virtual size_t size() const { return 1; }
548 template <typename T, template <typename T> class Storage, class Bin>
549 inline ScalarProxy<T, Storage, Bin>
550 VectorBase<T, Storage, Bin>::operator[](int index)
552 assert (index >= 0 && index < size());
553 return ScalarProxy<T, Storage, Bin>(bin, params, index);
556 template <typename T, template <typename T> class Storage, class Bin>
559 template <typename T, template <typename T> class Storage, class Bin>
560 class Vector2dBase : public Stat
563 typedef Storage<T> storage_t;
564 typedef typename storage_t::Params params_t;
565 typedef typename Bin::VectorBin<storage_t> bin_t;
572 std::vector<std::string> *y_subnames;
575 storage_t *data(int index) { return bin.data(index, params); }
576 const storage_t *data(int index) const {
577 return (const_cast<bin_t *>(&bin))->data(index, params);
581 // Copying stats is not allowed
582 Vector2dBase(const Vector2dBase &stat);
583 const Vector2dBase &operator=(const Vector2dBase &);
586 Vector2dBase() : Stat(true) {}
589 Vector2dBase &init(size_t _x, size_t _y) {
592 bin.init(x * y, params);
594 y_subnames = new std::vector<std::string>(y);
600 * This makes the assumption that if you're gonna subnames a 2d vector,
601 * you're subnaming across all y
603 Vector2dBase &ysubnames(const char **names)
605 for (int i=0; i < y; ++i) {
606 (*y_subnames)[i] = names[i];
610 Vector2dBase &ysubname(int index, const std::string subname)
612 (*y_subnames)[i] = subname.c_str();
615 std::string ysubname(int i) const { return (*y_subnames)[i]; }
617 friend class VectorProxy<T, Storage, Bin>;
618 VectorProxy<T, Storage, Bin> operator[](int index);
620 virtual size_t size() const { return bin.size(); }
621 virtual bool zero() const { return data(0)->value(params) == 0.0; }
624 display(std::ostream &out) const
626 bool have_subname = false;
627 for (int i = 0; i < x; ++i) {
628 if (!mysubname(i).empty())
633 result_t super_total = 0.0;
634 for (int i = 0; i < x; ++i) {
637 subname = mysubname(i);
641 subname = to_string(i);
646 result_t total = 0.0;
647 for (int j = 0; j < y; ++j) {
648 vec[j] = data(iy + j)->val(params);
649 tot_vec[j] += vec[j];
651 super_total += vec[j];
655 if (mysubdesc(i).empty()) {
661 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
662 myprecision(), myflags(), vec, total);
665 if ((myflags() & ::Statistics::total) && (x > 1)) {
666 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
667 myprecision(), myflags(), tot_vec, super_total);
673 template <typename T, template <typename T> class Storage, class Bin>
674 class VectorProxy : public VectorStat
677 typedef Storage<T> storage_t;
678 typedef typename storage_t::Params params_t;
679 typedef typename Bin::VectorBin<storage_t> bin_t;
690 storage_t *data(int index) {
692 return bin->data(offset + index, *params);
695 const storage_t *data(int index) const {
696 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
700 const rvec_t &val() const {
704 vec = new rvec_t(size());
706 for (int i = 0; i < size(); ++i)
707 (*vec)[i] = data(i)->val(*params);
712 result_t total() const {
713 result_t total = 0.0;
714 for (int i = 0; i < size(); ++i)
715 total += data(i)->val(*params);
720 VectorProxy(bin_t &b, params_t &p, int o, int l)
721 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
723 VectorProxy(const VectorProxy &sp)
724 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
725 len(sp.len), vec(NULL)
732 const VectorProxy &operator=(const VectorProxy &sp) {
743 virtual size_t size() const { return len; }
745 ScalarProxy<T, Storage, Bin> operator[](int index) {
746 assert (index >= 0 && index < size());
747 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
751 template <typename T, template <typename T> class Storage, class Bin>
752 inline VectorProxy<T, Storage, Bin>
753 Vector2dBase<T, Storage, Bin>::operator[](int index)
755 int offset = index * y;
756 assert (index >= 0 && offset < size());
757 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
760 //////////////////////////////////////////////////////////////////////
762 // Non formula statistics
764 //////////////////////////////////////////////////////////////////////
766 void DistDisplay(std::ostream &stream, const std::string &name,
767 const std::string &desc, int precision, FormatFlags flags,
768 result_t min_val, result_t max_val,
769 result_t underflow, result_t overflow,
770 const rvec_t &vec, int min, int max, int bucket_size,
773 template <typename T>
793 DistStor(const Params ¶ms)
794 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
797 void sample(T val, int number, const Params ¶ms) {
798 if (val < params.min)
800 else if (val > params.max)
803 int index = (val - params.min) / params.bucket_size;
804 assert(index < size(params));
805 vec[index] += number;
815 size_t size(const Params &) const { return vec.size(); }
817 bool zero(const Params ¶ms) const {
818 if (underflow != 0 || overflow != 0)
821 int s = size(params);
822 for (int i = 0; i < s; i++)
829 void display(std::ostream &stream, const std::string &name,
830 const std::string &desc, int precision, FormatFlags flags,
831 const Params ¶ms) const {
833 #ifdef STAT_DISPLAY_COMPAT
834 result_t min = params.min;
836 result_t min = (min_val == INT_MAX) ? params.min : min_val;
838 result_t max = (max_val == INT_MIN) ? 0 : max_val;
840 rvec_t rvec(params.size);
841 for (int i = 0; i < params.size; ++i)
844 DistDisplay(stream, name, desc, precision, flags,
845 (result_t)min, (result_t)max,
846 (result_t)underflow, (result_t)overflow,
847 rvec, params.min, params.max, params.bucket_size,
852 void FancyDisplay(std::ostream &stream, const std::string &name,
853 const std::string &desc, int precision, FormatFlags flags,
854 result_t mean, result_t variance);
855 template <typename T>
867 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
869 void sample(T val, int number, const Params &) {
870 T value = val * number;
872 squares += value * value;
875 void display(std::ostream &stream, const std::string &name,
876 const std::string &desc, int precision, FormatFlags flags,
877 const Params &) const {
880 result_t variance = NAN;
884 result_t fsq = squares;
885 result_t ftot = total;
888 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
891 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
894 size_t size(const Params &) const { return 1; }
895 bool zero(const Params &) const { return total == 0; }
898 template <typename T>
909 AvgFancy(const Params &) : sum(0), squares(0) {}
911 void sample(T val, int number, const Params& p) {
912 T value = val * number;
914 squares += value * value;
916 void display(std::ostream &stream, const std::string &name,
917 const std::string &desc, int precision, FormatFlags flags,
918 const Params ¶ms) const {
919 result_t mean = sum / curTick;
920 result_t variance = (squares - sum * sum) / curTick;
922 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
925 size_t size(const Params ¶ms) const { return 1; }
926 bool zero(const Params ¶ms) const { return sum == 0; }
929 template <typename T, template <typename T> class Storage, class Bin>
930 class DistBase : public Stat
933 typedef Storage<T> storage_t;
934 typedef typename storage_t::Params params_t;
935 typedef typename Bin::Bin<storage_t> bin_t;
942 storage_t *data() { return bin.data(params); }
943 const storage_t *data() const {
944 return (const_cast<bin_t *>(&bin))->data(params);
948 // Copying stats is not allowed
949 DistBase(const DistBase &stat);
950 const DistBase &operator=(const DistBase &);
953 DistBase() : Stat(true) { }
956 template <typename U>
957 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
959 virtual size_t size() const { return data()->size(params); }
960 virtual bool zero() const { return data()->zero(params); }
961 virtual void display(std::ostream &stream) const {
962 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
967 template <typename T, template <typename T> class Storage, class Bin>
968 class VectorDistProxy;
970 template <typename T, template <typename T> class Storage, class Bin>
971 class VectorDistBase : public Stat
974 typedef Storage<T> storage_t;
975 typedef typename storage_t::Params params_t;
976 typedef typename Bin::VectorBin<storage_t> bin_t;
983 storage_t *data(int index) { return bin.data(index, params); }
984 const storage_t *data(int index) const {
985 return (const_cast<bin_t *>(&bin))->data(index, params);
989 // Copying stats is not allowed
990 VectorDistBase(const VectorDistBase &stat);
991 const VectorDistBase &operator=(const VectorDistBase &);
994 VectorDistBase() : Stat(true) { }
995 ~VectorDistBase() { }
997 friend class VectorDistProxy<T, Storage, Bin>;
998 VectorDistProxy<T, Storage, Bin> operator[](int index);
999 const VectorDistProxy<T, Storage, Bin> operator[](int index) const;
1001 virtual size_t size() const { return bin.size(); }
1002 virtual bool zero() const { return false; }
1003 virtual void display(std::ostream &stream) const;
1006 template <typename T, template <typename T> class Storage, class Bin>
1007 class VectorDistProxy : public Stat
1010 typedef Storage<T> storage_t;
1011 typedef typename storage_t::Params params_t;
1012 typedef typename Bin::Bin<storage_t> bin_t;
1013 typedef VectorDistBase<T, Storage, Bin> base_t;
1018 const base_t *cstat;
1023 storage_t *data() { return stat->data(index); }
1024 const storage_t *data() const { return cstat->data(index); }
1027 VectorDistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1028 : Stat(false), cstat(&s), index(i) {}
1029 VectorDistProxy(const VectorDistProxy &sp)
1030 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1031 const VectorDistProxy &operator=(const VectorDistProxy &sp) {
1032 cstat = sp.cstat; index = sp.index; return *this;
1036 template <typename U>
1037 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1039 virtual size_t size() const { return 1; }
1040 virtual bool zero() const {
1041 return data()->zero(cstat->params);
1043 virtual void display(std::ostream &stream) const {
1044 std::stringstream name, desc;
1046 if (!(cstat->mysubname(index).empty())) {
1047 name << cstat->myname() << cstat->mysubname(index);
1049 name << cstat->myname() << "_" << index;
1051 if (!(cstat->mysubdesc(index).empty())) {
1052 desc << cstat->mysubdesc(index);
1054 desc << cstat->mydesc();
1057 data()->display(stream, name.str(), desc.str(),
1058 cstat->myprecision(), cstat->myflags(), cstat->params);
1062 template <typename T, template <typename T> class Storage, class Bin>
1063 inline VectorDistProxy<T, Storage, Bin>
1064 VectorDistBase<T, Storage, Bin>::operator[](int index)
1066 assert (index >= 0 && index < size());
1067 return VectorDistProxy<T, Storage, Bin>(*this, index);
1070 template <typename T, template <typename T> class Storage, class Bin>
1071 inline const VectorDistProxy<T, Storage, Bin>
1072 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1074 assert (index >= 0 && index < size());
1075 return VectorDistProxy<T, Storage, Bin>(*this, index);
1079 * @todo Need a way to print Distribution totals across the Vector
1081 template <typename T, template <typename T> class Storage, class Bin>
1083 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1085 for (int i = 0; i < size(); ++i) {
1086 VectorDistProxy<T, Storage, Bin> proxy(*this, i);
1087 proxy.display(stream);
1093 VectorDistBase<T, Storage, Bin>::total(int index) const
1096 for (int i=0; i < x_size(); ++i) {
1097 total += data(i)->val(*params);
1102 //////////////////////////////////////////////////////////////////////
1106 //////////////////////////////////////////////////////////////////////
1107 class Node : public RefCounted
1110 virtual size_t size() const = 0;
1111 virtual const rvec_t &val() const = 0;
1112 virtual result_t total() const = 0;
1115 typedef RefCountingPtr<Node> NodePtr;
1117 class ScalarStatNode : public Node
1120 const ScalarStat &stat;
1121 mutable rvec_t result;
1124 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1125 const rvec_t &val() const { result[0] = stat.val(); return result; }
1126 virtual result_t total() const { return stat.val(); };
1128 virtual size_t size() const { return 1; }
1131 template <typename T, template <typename T> class Storage, class Bin>
1132 class ScalarProxyNode : public Node
1135 const ScalarProxy<T, Storage, Bin> proxy;
1136 mutable rvec_t result;
1139 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1140 : proxy(p), result(1) { }
1141 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1142 virtual result_t total() const { return proxy.val(); };
1144 virtual size_t size() const { return 1; }
1147 class VectorStatNode : public Node
1150 const VectorStat &stat;
1153 VectorStatNode(const VectorStat &s) : stat(s) {}
1154 const rvec_t &val() const { return stat.val(); }
1155 virtual result_t total() const { return stat.total(); };
1157 virtual size_t size() const { return stat.size(); }
1160 template <typename T>
1161 class ConstNode : public Node
1167 ConstNode(T s) : data(1, (result_t)s) {}
1168 const rvec_t &val() const { return data; }
1169 virtual result_t total() const { return data[0]; };
1171 virtual size_t size() const { return 1; }
1174 template <typename T>
1175 class FunctorNode : public Node
1179 mutable rvec_t result;
1182 FunctorNode(T &f) : functor(f) { result.resize(1); }
1183 const rvec_t &val() const {
1184 result[0] = (result_t)functor();
1187 virtual result_t total() const { return (result_t)functor(); };
1189 virtual size_t size() const { return 1; }
1192 template <typename T>
1193 class ScalarNode : public Node
1197 mutable rvec_t result;
1200 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1201 const rvec_t &val() const {
1202 result[0] = (result_t)scalar;
1205 virtual result_t total() const { return (result_t)scalar; };
1207 virtual size_t size() const { return 1; }
1211 class UnaryNode : public Node
1215 mutable rvec_t result;
1218 UnaryNode(NodePtr p) : l(p) {}
1220 const rvec_t &val() const {
1221 const rvec_t &lvec = l->val();
1222 int size = lvec.size();
1226 result.resize(size);
1228 for (int i = 0; i < size; ++i)
1229 result[i] = op(lvec[i]);
1234 result_t total() const {
1236 return op(l->total());
1239 virtual size_t size() const { return l->size(); }
1243 class BinaryNode : public Node
1248 mutable rvec_t result;
1251 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1253 const rvec_t &val() const {
1255 const rvec_t &lvec = l->val();
1256 const rvec_t &rvec = r->val();
1258 assert(lvec.size() > 0 && rvec.size() > 0);
1260 if (lvec.size() == 1 && rvec.size() == 1) {
1262 result[0] = op(lvec[0], rvec[0]);
1263 } else if (lvec.size() == 1) {
1264 int size = rvec.size();
1265 result.resize(size);
1266 for (int i = 0; i < size; ++i)
1267 result[i] = op(lvec[0], rvec[i]);
1268 } else if (rvec.size() == 1) {
1269 int size = lvec.size();
1270 result.resize(size);
1271 for (int i = 0; i < size; ++i)
1272 result[i] = op(lvec[i], rvec[0]);
1273 } else if (rvec.size() == lvec.size()) {
1274 int size = rvec.size();
1275 result.resize(size);
1276 for (int i = 0; i < size; ++i)
1277 result[i] = op(lvec[i], rvec[i]);
1283 result_t total() const {
1285 return op(l->total(), r->total());
1288 virtual size_t size() const {
1296 assert(ls == rs && "Node vector sizes are not equal");
1303 class SumNode : public Node
1307 mutable rvec_t result;
1310 SumNode(NodePtr p) : l(p), result(1) {}
1312 const rvec_t &val() const {
1313 const rvec_t &lvec = l->val();
1314 int size = lvec.size();
1320 for (int i = 0; i < size; ++i)
1321 result[0] = op(result[0], lvec[i]);
1326 result_t total() const {
1327 const rvec_t &lvec = l->val();
1328 int size = lvec.size();
1331 result_t result = 0.0;
1334 for (int i = 0; i < size; ++i)
1335 result = op(result, lvec[i]);
1340 virtual size_t size() const { return 1; }
1349 Temp(NodePtr n) : node(n) {}
1350 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1351 template <typename T, template <typename T> class Storage, class Bin>
1352 Temp(const ScalarProxy<T, Storage, Bin> &p)
1353 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1354 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1356 #define TempSCALAR(T) \
1357 Temp(T value) : node(new ConstNode<T>(value)) {}
1359 TempSCALAR( signed char);
1360 TempSCALAR(unsigned char);
1361 TempSCALAR( signed short);
1362 TempSCALAR(unsigned short);
1363 TempSCALAR( signed int);
1364 TempSCALAR(unsigned int);
1365 TempSCALAR( signed long);
1366 TempSCALAR(unsigned long);
1367 TempSCALAR( signed long long);
1368 TempSCALAR(unsigned long long);
1373 operator NodePtr() { return node;}
1377 //////////////////////////////////////////////////////////////////////
1379 // Binning Interface
1381 //////////////////////////////////////////////////////////////////////
1390 off_t size() const { return memsize; }
1394 BinBase(size_t size);
1398 } // namespace Detail
1400 template <class BinType>
1401 struct StatBin : public Detail::BinBase
1403 static StatBin *&curBin() {
1404 static StatBin *current = NULL;
1408 static void setCurBin(StatBin *bin) { curBin() = bin; }
1409 static StatBin *current() { assert(curBin()); return curBin(); }
1411 static off_t &offset() {
1412 static off_t offset = 0;
1416 static off_t new_offset(size_t size) {
1417 size_t mask = sizeof(u_int64_t) - 1;
1418 off_t off = offset();
1420 // That one is for the last trailing flags byte.
1421 offset() += (size + 1 + mask) & ~mask;
1426 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1428 char *memory(off_t off) {
1429 assert(offset() <= size());
1430 return Detail::BinBase::memory() + off;
1433 static void activate(StatBin &bin) { setCurBin(&bin); }
1441 BinBase() : offset(-1) {}
1442 void allocate(size_t size) {
1443 offset = new_offset(size);
1446 assert(offset != -1);
1447 return current()->memory(offset);
1451 template <class Storage>
1452 class Bin : public BinBase
1455 typedef typename Storage::Params Params;
1458 Bin() { allocate(sizeof(Storage)); }
1459 bool initialized() const { return true; }
1460 void init(const Params ¶ms) { }
1462 int size() const { return 1; }
1464 Storage *data(const Params ¶ms) {
1465 assert(initialized());
1466 char *ptr = access();
1467 char *flags = ptr + sizeof(Storage);
1468 if (!(*flags & 0x1)) {
1470 new (ptr) Storage(params);
1472 return reinterpret_cast<Storage *>(ptr);
1476 template <class Storage>
1477 class VectorBin : public BinBase
1480 typedef typename Storage::Params Params;
1486 VectorBin() : _size(0) {}
1488 bool initialized() const { return _size > 0; }
1489 void init(int s, const Params ¶ms) {
1490 assert(!initialized());
1493 allocate(_size * sizeof(Storage));
1496 int size() const { return _size; }
1498 Storage *data(int index, const Params ¶ms) {
1499 assert(initialized());
1500 assert(index >= 0 && index < size());
1501 char *ptr = access();
1502 char *flags = ptr + size() * sizeof(Storage);
1503 if (!(*flags & 0x1)) {
1505 for (int i = 0; i < size(); ++i)
1506 new (ptr + i * sizeof(Storage)) Storage(params);
1508 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1513 class MainBinType {};
1514 typedef StatBin<MainBinType> MainBin;
1518 template <class Storage>
1522 typedef typename Storage::Params Params;
1525 char ptr[sizeof(Storage)];
1528 bool initialized() const { return true; }
1529 void init(const Params ¶ms) {
1530 new (ptr) Storage(params);
1532 int size() const{ return 1; }
1533 Storage *data(const Params ¶ms) {
1534 assert(initialized());
1535 return reinterpret_cast<Storage *>(ptr);
1539 template <class Storage>
1543 typedef typename Storage::Params Params;
1550 VectorBin() : ptr(NULL) { }
1555 bool initialized() const { return ptr != NULL; }
1556 void init(int s, const Params ¶ms) {
1557 assert(s > 0 && "size must be positive!");
1558 assert(!initialized());
1560 ptr = new char[_size * sizeof(Storage)];
1561 for (int i = 0; i < _size; ++i)
1562 new (ptr + i * sizeof(Storage)) Storage(params);
1565 int size() const { return _size; }
1567 Storage *data(int index, const Params ¶ms) {
1568 assert(initialized());
1569 assert(index >= 0 && index < size());
1570 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1575 //////////////////////////////////////////////////////////////////////
1577 // Visible Statistics Types
1579 //////////////////////////////////////////////////////////////////////
1580 /**@defgroup VStats VisibleStatTypes
1584 *This is the simplest counting stat. Default type is Counter, but can be
1585 *anything (like double, int, etc). To bin, just designate the name of the bin
1586 * when declaring. It can be used like a regular Counter.
1587 *Example: Stat<> foo;
1590 template <typename T = Counter, class Bin = NoBin>
1591 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
1594 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
1596 /** sets Stat equal to value of type U */
1597 template <typename U>
1598 void operator=(const U& v) { Base::operator=(v); }
1602 *This calculates averages over number of cycles. Additionally, the update per
1603 *cycle is implicit if there is no change. In other words, if you want to know
1604 *the average number of instructions in the IQ per cycle, then you can use this
1605 * stat and not have to update it on cycles where there is no change.
1607 template <typename T = Counter, class Bin = NoBin>
1608 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
1611 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
1613 /** sets Average equalt to value of type U*/
1614 template <typename U>
1615 void operator=(const U& v) { Base::operator=(v); }
1619 *This is a vector of type T, ideally suited to track stats across something like
1622 template <typename T = Counter, class Bin = NoBin>
1623 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
1627 *This is a vector of Averages of type T
1629 template <typename T = Counter, class Bin = NoBin>
1630 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
1634 *This is a 2-dimensional vector. Intended usage is for something like tracking a
1635 * Vector stat across another Vector like SMT threads.
1637 template <typename T = Counter, class Bin = NoBin>
1638 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
1642 * This is essentially a Vector, but with minor differences. Where a
1643 * Vector's index maps directly to what it's tracking, a Distribution's index can
1644 * map to an arbitrary bucket type. For example, you could map 1-8 to bucket 0
1645 * of a Distribution, and if ever there are 1-8 instructions within an IQ, increment
1648 template <typename T = Counter, class Bin = NoBin>
1649 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
1652 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1653 typedef typename Detail::DistStor<T>::Params Params;
1657 *This must be called to set some data members of the distribution
1658 *as well as to allocate the appropriate storage size.
1659 *@param min The minimum value of the Distribution
1660 *@param max The maximum value of the Distribution (NOT the size!)
1661 *@param bkt The size of the buckets to indicate mapping. I.e. if you have
1662 *min=0, max=15, bkt=8, you will have two buckets, and anything from 0-7
1663 *will go into bucket 0, and anything from 8-15 be in bucket 1.
1664 *@return the Distribution itself.
1666 Distribution &init(T min, T max, int bkt) {
1669 params.bucket_size = bkt;
1670 params.size = (max - min) / bkt + 1;
1679 *This has the functionality of a standard deviation built into it. Update it
1680 *every cycle, and at the end you will have the standard deviation.
1682 template <typename T = Counter, class Bin = NoBin>
1683 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
1686 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1687 typedef typename Detail::DistStor<T>::Params Params;
1690 StandardDeviation() {
1697 *This also calculates standard deviations, but there is no need to
1698 *update every cycle if there is no change, the stat will update for you.
1700 template <typename T = Counter, class Bin = NoBin>
1701 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
1704 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1705 typedef typename Detail::DistStor<T>::Params Params;
1708 AverageDeviation() {
1715 *This is a vector of Distributions. (The complexity increases!). Intended usage
1716 * is for something like tracking a distribution across a vector like SMT threads.
1718 template <typename T = Counter, class Bin = NoBin>
1719 class VectorDistribution
1720 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
1723 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
1724 typedef typename Detail::DistStor<T>::Params Params;
1728 *This must be called to set some data members and allocate storage space.
1729 *@param size The size of the Vector
1730 *@param min The minumum value of the Distribution
1731 *@param max The maximum value of the Distribution (NOT the size)
1732 *@param bkt The range of the bucket. I.e if min=0, max=15, and bkt=8,
1733 *then 0-7 will be bucket 0, and 8-15 will be bucket 1.
1734 *@return return the VectorDistribution itself.
1736 VectorDistribution &init(int size, T min, T max, int bkt) {
1739 params.bucket_size = bkt;
1740 params.size = (max - min) / bkt + 1;
1741 bin.init(size, params);
1749 *This is a vector of Standard Deviations. Intended usage is for tracking
1750 *Standard Deviations across a vector like SMT threads.
1752 template <typename T = Counter, class Bin = NoBin>
1753 class VectorStandardDeviation
1754 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
1757 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
1758 typedef typename Detail::DistStor<T>::Params Params;
1761 /** This must be called to initialize some data members and allocate
1762 * approprate storage space for the stat.
1763 *@param size The size of the Vector
1764 * @return the VectorStandardDeviation itself.
1766 VectorStandardDeviation &init(int size) {
1767 bin.init(size, params);
1775 * This is a vector of Average Deviations. Intended usage is for tracking
1776 *Average Deviations across a vector like SMT threads.
1778 template <typename T = Counter, class Bin = NoBin>
1779 class VectorAverageDeviation
1780 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
1783 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
1784 typedef typename Detail::DistStor<T>::Params Params;
1787 /** This must be called to initialize some data members and allocate
1788 * approprate storage space for the stat.
1789 *@param size The size of the Vector
1790 * @return The VectorAverageDeviation itself.
1792 VectorAverageDeviation &init(int size) {
1793 bin.init(size, params);
1801 *This is a formula type. When defining it, you can just say:
1802 *Formula foo = manchu + 3 / bar;
1803 *The calculations for Formulas are all done at the end of the simulation, this
1804 *really is just a definition of how to calculate at the end.
1806 class Formula : public Detail::VectorStat
1809 /** The root of the tree which represents the Formula */
1810 Detail::NodePtr root;
1811 friend class Detail::Temp;
1814 Formula() : VectorStat(true) { setInit(); }
1815 Formula(Detail::Temp r) : VectorStat(true) {
1820 const Formula &operator=(Detail::Temp r) {
1821 assert(!root && "Can't change formulas");
1827 const Formula &operator+=(Detail::Temp r) {
1828 using namespace Detail;
1830 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
1837 const rvec_t &val() const { return root->val(); }
1838 result_t total() const { return root->total(); }
1840 size_t size() const {
1844 return root->size();
1849 void dump(std::ostream &stream);
1852 operator+(Detail::Temp l, Detail::Temp r)
1854 using namespace Detail;
1855 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
1859 operator-(Detail::Temp l, Detail::Temp r)
1861 using namespace Detail;
1862 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
1866 operator*(Detail::Temp l, Detail::Temp r)
1868 using namespace Detail;
1869 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
1873 operator/(Detail::Temp l, Detail::Temp r)
1875 using namespace Detail;
1876 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
1880 operator%(Detail::Temp l, Detail::Temp r)
1882 using namespace Detail;
1883 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
1887 operator-(Detail::Temp l)
1889 using namespace Detail;
1890 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
1893 template <typename T>
1897 using namespace Detail;
1898 return NodePtr(new ConstNode<T>(val));
1901 template <typename T>
1905 using namespace Detail;
1906 return NodePtr(new FunctorNode<T>(val));
1909 template <typename T>
1913 using namespace Detail;
1914 return NodePtr(new ScalarNode<T>(val));
1918 sum(Detail::Temp val)
1920 using namespace Detail;
1921 return NodePtr(new SumNode<std::plus<result_t> >(val));
1924 extern bool PrintDescriptions;
1926 } // namespace statistics
1928 #endif // __STATISTICS_HH__