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__
58 #include "base/refcnt.hh"
59 #include "base/str.hh"
61 #include "sim/host.hh"
65 /** Define Not a number. */
67 /** Need to define __nan() */
71 /** Print stats out in SS format. */
72 #define STAT_DISPLAY_COMPAT
74 /** The current simulated cycle. */
77 /* A namespace for all of the Statistics */
78 namespace Statistics {
79 /** All results are doubles. */
80 typedef double result_t;
81 /** A vector to hold results. */
82 typedef std::vector<result_t> rvec_t;
85 * Define the storage for format flags.
86 * @todo Can probably shrink this.
88 typedef u_int32_t FormatFlags;
89 /** Nothing extra to print. */
90 const FormatFlags none = 0x0000;
91 /** Print the total. */
92 const FormatFlags total = 0x0001;
93 /** Print the percent of the total that this entry represents. */
94 const FormatFlags pdf = 0x0002;
95 /** Don't print if this is zero. */
96 const FormatFlags nozero = 0x0004;
97 /** Don't print if this is NAN */
98 const FormatFlags nonan = 0x0008;
99 /** Print the cumulative percentage of total upto this entry. */
100 const FormatFlags cdf = 0x0010;
101 /** Print the distribution. */
102 const FormatFlags dist = 0x0020;
103 /** Used for SS compatability. */
104 const FormatFlags __substat = 0x8000;
105 /** Mask of flags that can't be set directly */
106 const FormatFlags __reserved = __substat;
108 /* Contains the statistic implementation details */
110 //////////////////////////////////////////////////////////////////////
112 // Statistics Framework Base classes
114 //////////////////////////////////////////////////////////////////////
119 * Common base class for all statistics, used to maintain a list and print.
120 * This class holds no data itself but is used to find the associated
121 * StatData in the stat database @sa Statistics::Database.
126 /** Mark this statistics as initialized. */
129 * Finds and returns the associated StatData from the database.
130 * @return The formatting and output data of this statistic.
134 * Finds and returns a const pointer to the associated StatData.
135 * @return The formatting and output data of this statistic.
137 const StatData *mydata() const;
139 * Mark this stat for output at the end of simulation.
140 * @return The formatting and output data of this statistic.
144 * Finds and returns the SubData at the given index.
145 * @param index The index of the SubData to find.
146 * @return The name and description of the given index.
148 const SubData *mysubdata(int index) const;
150 * Create and return a new SubData field for the given index.
151 * @param index The index to create a SubData for.
152 * @return A pointer to the created SubData.
154 SubData *mysubdata_create(int index);
158 * Return the name of this stat.
159 * @return the name of the stat.
161 virtual std::string myname() const;
163 * Return the name of the sub field at the given index.
164 * @param index the subfield index.
165 * @return the name of the subfield.
167 virtual std::string mysubname(int index) const;
169 * Return the description of this stat.
170 * @return the description of this stat.
172 virtual std::string mydesc() const;
174 * Return the description of the subfield at the given index.
175 * @param index The subfield index.
176 * @return the description of the subfield.
178 virtual std::string mysubdesc(int index) const;
180 * Return the format flags of this stat.
181 * @return the format flags.
183 virtual FormatFlags myflags() const;
185 * Return true if this stat's prereqs have been satisfied (they are non
187 * @return true if the prerequisite stats aren't zero.
189 virtual bool dodisplay() const;
191 * Return the display percision.
192 * @return The display precision.
194 virtual int myprecision() const;
198 * Create this stat and register it if reg is true.
199 * @param reg Register this stat in the database?
208 * Print this stat to the given ostream.
209 * @param stream The stream to print to.
211 virtual void display(std::ostream &stream) const = 0;
213 * Return the number of entries in this stat.
214 * @return The number of entries.
216 virtual size_t size() const = 0;
218 * Return true if the stat has value zero.
219 * @return True if the stat is zero.
221 virtual bool zero() const = 0;
225 * Set the name and marks this stat to print at the end of simulation.
226 * @param name The new name.
227 * @return A reference to this stat.
229 Stat &name(const std::string &name);
231 * Set the description and marks this stat to print at the end of
233 * @param desc The new description.
234 * @return A reference to this stat.
236 Stat &desc(const std::string &desc);
238 * Set the precision and marks this stat to print at the end of simulation.
239 * @param p The new precision
240 * @return A reference to this stat.
242 Stat &precision(int p);
244 * Set the flags and marks this stat to print at the end of simulation.
245 * @param f The new flags.
246 * @return A reference to this stat.
248 Stat &flags(FormatFlags f);
250 * Set the prerequisite stat and marks this stat to print at the end of
252 * @param prereq The prerequisite stat.
253 * @return A reference to this stat.
255 Stat &prereq(const Stat &prereq);
257 * Set the subfield name for the given index, and marks this stat to print
258 * at the end of simulation.
259 * @param index The subfield index.
260 * @param name The new name of the subfield.
261 * @return A reference to this stat.
263 Stat &subname(int index, const std::string &name);
265 * Set the subfield description for the given index and marks this stat to
266 * print at the end of simulation.
267 * @param index The subfield index.
268 * @param desc The new description of the subfield
269 * @return A reference to this stat.
271 Stat &subdesc(int index, const std::string &desc);
275 * Checks if the first stat's name is alphabetically less than the second.
276 * This function breaks names up at periods and considers each subname
278 * @param stat1 The first stat.
279 * @param stat2 The second stat.
280 * @return stat1's name is alphabetically before stat2's
282 static bool less(Stat *stat1, Stat *stat2);
285 /** A unique ID used for debugging. */
290 // Scalar stats involved in formulas
291 class ScalarStat : public Stat
294 ScalarStat(bool reg) : Stat(reg) {}
295 virtual result_t val() const = 0;
296 virtual bool zero() const;
297 virtual void display(std::ostream &stream) const;
301 VectorDisplay(std::ostream &stream, const std::string &myname,
302 const std::vector<std::string> *mysubnames,
303 const std::string &mydesc,
304 const std::vector<std::string> *mysubdescs,
305 int myprecision, FormatFlags myflags, const rvec_t &vec,
308 // Vector stats involved in formulas
309 class VectorStat : public Stat
312 VectorStat(bool reg) : Stat(reg) {}
313 virtual const rvec_t &val() const = 0;
314 virtual result_t total() const = 0;
315 virtual bool zero() const;
316 virtual void display(std::ostream &stream) const;
319 //////////////////////////////////////////////////////////////////////
323 //////////////////////////////////////////////////////////////////////
324 template <typename T>
334 StatStor(const Params &) : data(T()) {}
336 void set(T val, const Params &p) { data = val; }
337 void inc(T val, const Params &p) { data += val; }
338 void dec(T val, const Params &p) { data -= val; }
339 result_t val(const Params &p) const { return (result_t)data; }
340 T value(const Params &p) const { return data; }
343 template <typename T>
351 mutable result_t total;
355 AvgStor(const Params &) : current(T()), total(0), last(0) { }
357 void set(T val, const Params &p) {
358 total += current * (curTick - last);
362 void inc(T val, const Params &p) { set(current + val, p); }
363 void dec(T val, const Params &p) { set(current - val, p); }
364 result_t val(const Params &p) const {
365 total += current * (curTick - last);
367 return (result_t)(total + current) / (result_t)(curTick + 1);
369 T value(const Params &p) const { return current; }
372 template <typename T, template <typename T> class Storage, class Bin>
373 class ScalarBase : public ScalarStat
376 typedef Storage<T> storage_t;
377 typedef typename storage_t::Params params_t;
378 typedef typename Bin::Bin<storage_t> bin_t;
385 storage_t *data() { return bin.data(params); }
386 const storage_t *data() const {
387 return (const_cast<bin_t *>(&bin))->data(params);
391 // Copying stats is not allowed
392 ScalarBase(const ScalarBase &stat);
393 const ScalarBase &operator=(const ScalarBase &);
396 result_t val() const { return data()->val(params); }
397 T value() const { return data()->value(params); }
400 ScalarBase() : ScalarStat(true) {
406 // Common operators for stats
407 void operator++() { data()->inc(1, params); }
408 void operator--() { data()->dec(1, params); }
410 void operator++(int) { ++*this; }
411 void operator--(int) { --*this; }
413 template <typename U>
414 void operator=(const U& v) { data()->set(v, params); }
416 template <typename U>
417 void operator+=(const U& v) { data()->inc(v, params); }
419 template <typename U>
420 void operator-=(const U& v) { data()->dec(v, params); }
422 virtual size_t size() const { return 1; }
425 //////////////////////////////////////////////////////////////////////
429 //////////////////////////////////////////////////////////////////////
430 template <typename T, template <typename T> class Storage, class Bin>
433 template <typename T, template <typename T> class Storage, class Bin>
434 class VectorBase : public VectorStat
437 typedef Storage<T> storage_t;
438 typedef typename storage_t::Params params_t;
439 typedef typename Bin::VectorBin<storage_t> bin_t;
449 storage_t *data(int index) { return bin.data(index, params); }
450 const storage_t *data(int index) const {
451 return (const_cast<bin_t *>(&bin))->data(index, params);
455 // Copying stats is not allowed
456 VectorBase(const VectorBase &stat);
457 const VectorBase &operator=(const VectorBase &);
460 const rvec_t &val() const {
464 vec = new rvec_t(size());
466 for (int i = 0; i < size(); ++i)
467 (*vec)[i] = data(i)->val(params);
472 result_t total() const {
473 result_t total = 0.0;
474 for (int i = 0; i < size(); ++i)
475 total += data(i)->val(params);
480 VectorBase() : VectorStat(true), vec(NULL) {}
481 ~VectorBase() { if (vec) delete vec; }
483 VectorBase &init(size_t size) {
484 bin.init(size, params);
490 friend class ScalarProxy<T, Storage, Bin>;
491 ScalarProxy<T, Storage, Bin> operator[](int index);
493 virtual size_t size() const { return bin.size(); }
496 template <typename T, template <typename T> class Storage, class Bin>
497 class ScalarProxy : public ScalarStat
500 typedef Storage<T> storage_t;
501 typedef typename storage_t::Params params_t;
502 typedef typename Bin::VectorBin<storage_t> bin_t;
510 storage_t *data() { return bin->data(index, *params); }
511 const storage_t *data() const { return bin->data(index, *params); }
514 result_t val() const { return data()->val(*params); }
515 T value() const { return data()->value(*params); }
518 ScalarProxy(bin_t &b, params_t &p, int i)
519 : ScalarStat(false), bin(&b), params(&p), index(i) {}
520 ScalarProxy(const ScalarProxy &sp)
521 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
522 const ScalarProxy &operator=(const ScalarProxy &sp) {
530 // Common operators for stats
531 void operator++() { data()->inc(1, *params); }
532 void operator--() { data()->dec(1, *params); }
534 void operator++(int) { ++*this; }
535 void operator--(int) { --*this; }
537 template <typename U>
538 void operator=(const U& v) { data()->set(v, *params); }
540 template <typename U>
541 void operator+=(const U& v) { data()->inc(v, *params); }
543 template <typename U>
544 void operator-=(const U& v) { data()->dec(v, *params); }
546 virtual size_t size() const { return 1; }
549 template <typename T, template <typename T> class Storage, class Bin>
550 inline ScalarProxy<T, Storage, Bin>
551 VectorBase<T, Storage, Bin>::operator[](int index)
553 assert (index >= 0 && index < size());
554 return ScalarProxy<T, Storage, Bin>(bin, params, index);
557 template <typename T, template <typename T> class Storage, class Bin>
560 template <typename T, template <typename T> class Storage, class Bin>
561 class Vector2dBase : public Stat
564 typedef Storage<T> storage_t;
565 typedef typename storage_t::Params params_t;
566 typedef typename Bin::VectorBin<storage_t> bin_t;
573 std::vector<std::string> *y_subnames;
576 storage_t *data(int index) { return bin.data(index, params); }
577 const storage_t *data(int index) const {
578 return (const_cast<bin_t *>(&bin))->data(index, params);
582 // Copying stats is not allowed
583 Vector2dBase(const Vector2dBase &stat);
584 const Vector2dBase &operator=(const Vector2dBase &);
587 Vector2dBase() : Stat(true) {}
590 Vector2dBase &init(size_t _x, size_t _y) {
593 bin.init(x * y, params);
595 y_subnames = new std::vector<std::string>(y);
601 * This makes the assumption that if you're gonna subnames a 2d vector,
602 * you're subnaming across all y
604 Vector2dBase &ysubnames(const char **names)
606 for (int i=0; i < y; ++i) {
607 (*y_subnames)[i] = names[i];
611 Vector2dBase &ysubname(int index, const std::string subname)
613 (*y_subnames)[i] = subname.c_str();
616 std::string ysubname(int i) const { return (*y_subnames)[i]; }
618 friend class VectorProxy<T, Storage, Bin>;
619 VectorProxy<T, Storage, Bin> operator[](int index);
621 virtual size_t size() const { return bin.size(); }
622 virtual bool zero() const { return data(0)->value(params) == 0.0; }
625 display(std::ostream &out) const
627 bool have_subname = false;
628 for (int i = 0; i < x; ++i) {
629 if (!mysubname(i).empty())
634 result_t super_total = 0.0;
635 for (int i = 0; i < x; ++i) {
638 subname = mysubname(i);
642 subname = to_string(i);
647 result_t total = 0.0;
648 for (int j = 0; j < y; ++j) {
649 vec[j] = data(iy + j)->val(params);
650 tot_vec[j] += vec[j];
652 super_total += vec[j];
656 if (mysubdesc(i).empty()) {
662 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
663 myprecision(), myflags(), vec, total);
666 if ((myflags() & ::Statistics::total) && (x > 1)) {
667 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
668 myprecision(), myflags(), tot_vec, super_total);
674 template <typename T, template <typename T> class Storage, class Bin>
675 class VectorProxy : public VectorStat
678 typedef Storage<T> storage_t;
679 typedef typename storage_t::Params params_t;
680 typedef typename Bin::VectorBin<storage_t> bin_t;
691 storage_t *data(int index) {
693 return bin->data(offset + index, *params);
696 const storage_t *data(int index) const {
697 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
701 const rvec_t &val() const {
705 vec = new rvec_t(size());
707 for (int i = 0; i < size(); ++i)
708 (*vec)[i] = data(i)->val(*params);
713 result_t total() const {
714 result_t total = 0.0;
715 for (int i = 0; i < size(); ++i)
716 total += data(i)->val(*params);
721 VectorProxy(bin_t &b, params_t &p, int o, int l)
722 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
724 VectorProxy(const VectorProxy &sp)
725 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
726 len(sp.len), vec(NULL)
733 const VectorProxy &operator=(const VectorProxy &sp) {
744 virtual size_t size() const { return len; }
746 ScalarProxy<T, Storage, Bin> operator[](int index) {
747 assert (index >= 0 && index < size());
748 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
752 template <typename T, template <typename T> class Storage, class Bin>
753 inline VectorProxy<T, Storage, Bin>
754 Vector2dBase<T, Storage, Bin>::operator[](int index)
756 int offset = index * y;
757 assert (index >= 0 && offset < size());
758 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
761 //////////////////////////////////////////////////////////////////////
763 // Non formula statistics
765 //////////////////////////////////////////////////////////////////////
767 void DistDisplay(std::ostream &stream, const std::string &name,
768 const std::string &desc, int precision, FormatFlags flags,
769 result_t min_val, result_t max_val,
770 result_t underflow, result_t overflow,
771 const rvec_t &vec, int min, int max, int bucket_size,
774 template <typename T>
794 DistStor(const Params ¶ms)
795 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
798 void sample(T val, int number, const Params ¶ms) {
799 if (val < params.min)
801 else if (val > params.max)
804 int index = (val - params.min) / params.bucket_size;
805 assert(index < size(params));
806 vec[index] += number;
816 size_t size(const Params &) const { return vec.size(); }
818 bool zero(const Params ¶ms) const {
819 if (underflow != 0 || overflow != 0)
822 int s = size(params);
823 for (int i = 0; i < s; i++)
830 void display(std::ostream &stream, const std::string &name,
831 const std::string &desc, int precision, FormatFlags flags,
832 const Params ¶ms) const {
834 #ifdef STAT_DISPLAY_COMPAT
835 result_t min = params.min;
837 result_t min = (min_val == INT_MAX) ? params.min : min_val;
839 result_t max = (max_val == INT_MIN) ? 0 : max_val;
841 rvec_t rvec(params.size);
842 for (int i = 0; i < params.size; ++i)
845 DistDisplay(stream, name, desc, precision, flags,
846 (result_t)min, (result_t)max,
847 (result_t)underflow, (result_t)overflow,
848 rvec, params.min, params.max, params.bucket_size,
853 void FancyDisplay(std::ostream &stream, const std::string &name,
854 const std::string &desc, int precision, FormatFlags flags,
855 result_t mean, result_t variance);
856 template <typename T>
868 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
870 void sample(T val, int number, const Params &) {
871 T value = val * number;
873 squares += value * value;
876 void display(std::ostream &stream, const std::string &name,
877 const std::string &desc, int precision, FormatFlags flags,
878 const Params &) const {
881 result_t variance = NAN;
885 result_t fsq = squares;
886 result_t ftot = total;
889 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
892 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
895 size_t size(const Params &) const { return 1; }
896 bool zero(const Params &) const { return total == 0; }
899 template <typename T>
910 AvgFancy(const Params &) : sum(0), squares(0) {}
912 void sample(T val, int number, const Params& p) {
913 T value = val * number;
915 squares += value * value;
917 void display(std::ostream &stream, const std::string &name,
918 const std::string &desc, int precision, FormatFlags flags,
919 const Params ¶ms) const {
920 result_t mean = sum / curTick;
921 result_t variance = (squares - sum * sum) / curTick;
923 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
926 size_t size(const Params ¶ms) const { return 1; }
927 bool zero(const Params ¶ms) const { return sum == 0; }
930 template <typename T, template <typename T> class Storage, class Bin>
931 class DistBase : public Stat
934 typedef Storage<T> storage_t;
935 typedef typename storage_t::Params params_t;
936 typedef typename Bin::Bin<storage_t> bin_t;
943 storage_t *data() { return bin.data(params); }
944 const storage_t *data() const {
945 return (const_cast<bin_t *>(&bin))->data(params);
949 // Copying stats is not allowed
950 DistBase(const DistBase &stat);
951 const DistBase &operator=(const DistBase &);
954 DistBase() : Stat(true) { }
957 template <typename U>
958 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
960 virtual size_t size() const { return data()->size(params); }
961 virtual bool zero() const { return data()->zero(params); }
962 virtual void display(std::ostream &stream) const {
963 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
968 template <typename T, template <typename T> class Storage, class Bin>
969 class VectorDistProxy;
971 template <typename T, template <typename T> class Storage, class Bin>
972 class VectorDistBase : public Stat
975 typedef Storage<T> storage_t;
976 typedef typename storage_t::Params params_t;
977 typedef typename Bin::VectorBin<storage_t> bin_t;
984 storage_t *data(int index) { return bin.data(index, params); }
985 const storage_t *data(int index) const {
986 return (const_cast<bin_t *>(&bin))->data(index, params);
990 // Copying stats is not allowed
991 VectorDistBase(const VectorDistBase &stat);
992 const VectorDistBase &operator=(const VectorDistBase &);
995 VectorDistBase() : Stat(true) { }
996 ~VectorDistBase() { }
998 friend class VectorDistProxy<T, Storage, Bin>;
999 VectorDistProxy<T, Storage, Bin> operator[](int index);
1000 const VectorDistProxy<T, Storage, Bin> operator[](int index) const;
1002 virtual size_t size() const { return bin.size(); }
1003 virtual bool zero() const { return false; }
1004 virtual void display(std::ostream &stream) const;
1007 template <typename T, template <typename T> class Storage, class Bin>
1008 class VectorDistProxy : public Stat
1011 typedef Storage<T> storage_t;
1012 typedef typename storage_t::Params params_t;
1013 typedef typename Bin::Bin<storage_t> bin_t;
1014 typedef VectorDistBase<T, Storage, Bin> base_t;
1019 const base_t *cstat;
1024 storage_t *data() { return stat->data(index); }
1025 const storage_t *data() const { return cstat->data(index); }
1028 VectorDistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1029 : Stat(false), cstat(&s), index(i) {}
1030 VectorDistProxy(const VectorDistProxy &sp)
1031 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1032 const VectorDistProxy &operator=(const VectorDistProxy &sp) {
1033 cstat = sp.cstat; index = sp.index; return *this;
1037 template <typename U>
1038 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1040 virtual size_t size() const { return 1; }
1041 virtual bool zero() const {
1042 return data()->zero(cstat->params);
1044 virtual void display(std::ostream &stream) const {
1045 std::stringstream name, desc;
1047 if (!(cstat->mysubname(index).empty())) {
1048 name << cstat->myname() << cstat->mysubname(index);
1050 name << cstat->myname() << "_" << index;
1052 if (!(cstat->mysubdesc(index).empty())) {
1053 desc << cstat->mysubdesc(index);
1055 desc << cstat->mydesc();
1058 data()->display(stream, name.str(), desc.str(),
1059 cstat->myprecision(), cstat->myflags(), cstat->params);
1063 template <typename T, template <typename T> class Storage, class Bin>
1064 inline VectorDistProxy<T, Storage, Bin>
1065 VectorDistBase<T, Storage, Bin>::operator[](int index)
1067 assert (index >= 0 && index < size());
1068 return VectorDistProxy<T, Storage, Bin>(*this, index);
1071 template <typename T, template <typename T> class Storage, class Bin>
1072 inline const VectorDistProxy<T, Storage, Bin>
1073 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1075 assert (index >= 0 && index < size());
1076 return VectorDistProxy<T, Storage, Bin>(*this, index);
1080 * @todo Need a way to print Distribution totals across the Vector
1082 template <typename T, template <typename T> class Storage, class Bin>
1084 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1086 for (int i = 0; i < size(); ++i) {
1087 VectorDistProxy<T, Storage, Bin> proxy(*this, i);
1088 proxy.display(stream);
1094 VectorDistBase<T, Storage, Bin>::total(int index) const
1097 for (int i=0; i < x_size(); ++i) {
1098 total += data(i)->val(*params);
1103 //////////////////////////////////////////////////////////////////////
1107 //////////////////////////////////////////////////////////////////////
1108 class Node : public RefCounted
1111 virtual size_t size() const = 0;
1112 virtual const rvec_t &val() const = 0;
1113 virtual result_t total() const = 0;
1116 typedef RefCountingPtr<Node> NodePtr;
1118 class ScalarStatNode : public Node
1121 const ScalarStat &stat;
1122 mutable rvec_t result;
1125 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1126 const rvec_t &val() const { result[0] = stat.val(); return result; }
1127 virtual result_t total() const { return stat.val(); };
1129 virtual size_t size() const { return 1; }
1132 template <typename T, template <typename T> class Storage, class Bin>
1133 class ScalarProxyNode : public Node
1136 const ScalarProxy<T, Storage, Bin> proxy;
1137 mutable rvec_t result;
1140 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1141 : proxy(p), result(1) { }
1142 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1143 virtual result_t total() const { return proxy.val(); };
1145 virtual size_t size() const { return 1; }
1148 class VectorStatNode : public Node
1151 const VectorStat &stat;
1154 VectorStatNode(const VectorStat &s) : stat(s) {}
1155 const rvec_t &val() const { return stat.val(); }
1156 virtual result_t total() const { return stat.total(); };
1158 virtual size_t size() const { return stat.size(); }
1161 template <typename T>
1162 class ConstNode : public Node
1168 ConstNode(T s) : data(1, (result_t)s) {}
1169 const rvec_t &val() const { return data; }
1170 virtual result_t total() const { return data[0]; };
1172 virtual size_t size() const { return 1; }
1175 template <typename T>
1176 class FunctorNode : public Node
1180 mutable rvec_t result;
1183 FunctorNode(T &f) : functor(f) { result.resize(1); }
1184 const rvec_t &val() const {
1185 result[0] = (result_t)functor();
1188 virtual result_t total() const { return (result_t)functor(); };
1190 virtual size_t size() const { return 1; }
1193 template <typename T>
1194 class ScalarNode : public Node
1198 mutable rvec_t result;
1201 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1202 const rvec_t &val() const {
1203 result[0] = (result_t)scalar;
1206 virtual result_t total() const { return (result_t)scalar; };
1208 virtual size_t size() const { return 1; }
1212 class UnaryNode : public Node
1216 mutable rvec_t result;
1219 UnaryNode(NodePtr p) : l(p) {}
1221 const rvec_t &val() const {
1222 const rvec_t &lvec = l->val();
1223 int size = lvec.size();
1227 result.resize(size);
1229 for (int i = 0; i < size; ++i)
1230 result[i] = op(lvec[i]);
1235 result_t total() const {
1237 return op(l->total());
1240 virtual size_t size() const { return l->size(); }
1244 class BinaryNode : public Node
1249 mutable rvec_t result;
1252 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1254 const rvec_t &val() const {
1256 const rvec_t &lvec = l->val();
1257 const rvec_t &rvec = r->val();
1259 assert(lvec.size() > 0 && rvec.size() > 0);
1261 if (lvec.size() == 1 && rvec.size() == 1) {
1263 result[0] = op(lvec[0], rvec[0]);
1264 } else if (lvec.size() == 1) {
1265 int size = rvec.size();
1266 result.resize(size);
1267 for (int i = 0; i < size; ++i)
1268 result[i] = op(lvec[0], rvec[i]);
1269 } else if (rvec.size() == 1) {
1270 int size = lvec.size();
1271 result.resize(size);
1272 for (int i = 0; i < size; ++i)
1273 result[i] = op(lvec[i], rvec[0]);
1274 } else if (rvec.size() == lvec.size()) {
1275 int size = rvec.size();
1276 result.resize(size);
1277 for (int i = 0; i < size; ++i)
1278 result[i] = op(lvec[i], rvec[i]);
1284 result_t total() const {
1286 return op(l->total(), r->total());
1289 virtual size_t size() const {
1297 assert(ls == rs && "Node vector sizes are not equal");
1304 class SumNode : public Node
1308 mutable rvec_t result;
1311 SumNode(NodePtr p) : l(p), result(1) {}
1313 const rvec_t &val() const {
1314 const rvec_t &lvec = l->val();
1315 int size = lvec.size();
1321 for (int i = 0; i < size; ++i)
1322 result[0] = op(result[0], lvec[i]);
1327 result_t total() const {
1328 const rvec_t &lvec = l->val();
1329 int size = lvec.size();
1332 result_t result = 0.0;
1335 for (int i = 0; i < size; ++i)
1336 result = op(result, lvec[i]);
1341 virtual size_t size() const { return 1; }
1350 Temp(NodePtr n) : node(n) {}
1351 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1352 template <typename T, template <typename T> class Storage, class Bin>
1353 Temp(const ScalarProxy<T, Storage, Bin> &p)
1354 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1355 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1357 #define TempSCALAR(T) \
1358 Temp(T value) : node(new ConstNode<T>(value)) {}
1360 TempSCALAR( signed char);
1361 TempSCALAR(unsigned char);
1362 TempSCALAR( signed short);
1363 TempSCALAR(unsigned short);
1364 TempSCALAR( signed int);
1365 TempSCALAR(unsigned int);
1366 TempSCALAR( signed long);
1367 TempSCALAR(unsigned long);
1368 TempSCALAR( signed long long);
1369 TempSCALAR(unsigned long long);
1374 operator NodePtr() { return node;}
1378 //////////////////////////////////////////////////////////////////////
1380 // Binning Interface
1382 //////////////////////////////////////////////////////////////////////
1391 off_t size() const { return memsize; }
1395 BinBase(size_t size);
1399 } // namespace Detail
1401 template <class BinType>
1402 struct StatBin : public Detail::BinBase
1404 static StatBin *&curBin() {
1405 static StatBin *current = NULL;
1409 static void setCurBin(StatBin *bin) { curBin() = bin; }
1410 static StatBin *current() { assert(curBin()); return curBin(); }
1412 static off_t &offset() {
1413 static off_t offset = 0;
1417 static off_t new_offset(size_t size) {
1418 size_t mask = sizeof(u_int64_t) - 1;
1419 off_t off = offset();
1421 // That one is for the last trailing flags byte.
1422 offset() += (size + 1 + mask) & ~mask;
1427 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1429 char *memory(off_t off) {
1430 assert(offset() <= size());
1431 return Detail::BinBase::memory() + off;
1434 static void activate(StatBin &bin) { setCurBin(&bin); }
1442 BinBase() : offset(-1) {}
1443 void allocate(size_t size) {
1444 offset = new_offset(size);
1447 assert(offset != -1);
1448 return current()->memory(offset);
1452 template <class Storage>
1453 class Bin : public BinBase
1456 typedef typename Storage::Params Params;
1459 Bin() { allocate(sizeof(Storage)); }
1460 bool initialized() const { return true; }
1461 void init(const Params ¶ms) { }
1463 int size() const { return 1; }
1465 Storage *data(const Params ¶ms) {
1466 assert(initialized());
1467 char *ptr = access();
1468 char *flags = ptr + sizeof(Storage);
1469 if (!(*flags & 0x1)) {
1471 new (ptr) Storage(params);
1473 return reinterpret_cast<Storage *>(ptr);
1477 template <class Storage>
1478 class VectorBin : public BinBase
1481 typedef typename Storage::Params Params;
1487 VectorBin() : _size(0) {}
1489 bool initialized() const { return _size > 0; }
1490 void init(int s, const Params ¶ms) {
1491 assert(!initialized());
1494 allocate(_size * sizeof(Storage));
1497 int size() const { return _size; }
1499 Storage *data(int index, const Params ¶ms) {
1500 assert(initialized());
1501 assert(index >= 0 && index < size());
1502 char *ptr = access();
1503 char *flags = ptr + size() * sizeof(Storage);
1504 if (!(*flags & 0x1)) {
1506 for (int i = 0; i < size(); ++i)
1507 new (ptr + i * sizeof(Storage)) Storage(params);
1509 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1514 class MainBinType {};
1515 typedef StatBin<MainBinType> MainBin;
1519 template <class Storage>
1523 typedef typename Storage::Params Params;
1526 char ptr[sizeof(Storage)];
1529 bool initialized() const { return true; }
1530 void init(const Params ¶ms) {
1531 new (ptr) Storage(params);
1533 int size() const{ return 1; }
1534 Storage *data(const Params ¶ms) {
1535 assert(initialized());
1536 return reinterpret_cast<Storage *>(ptr);
1540 template <class Storage>
1544 typedef typename Storage::Params Params;
1551 VectorBin() : ptr(NULL) { }
1556 bool initialized() const { return ptr != NULL; }
1557 void init(int s, const Params ¶ms) {
1558 assert(s > 0 && "size must be positive!");
1559 assert(!initialized());
1561 ptr = new char[_size * sizeof(Storage)];
1562 for (int i = 0; i < _size; ++i)
1563 new (ptr + i * sizeof(Storage)) Storage(params);
1566 int size() const { return _size; }
1568 Storage *data(int index, const Params ¶ms) {
1569 assert(initialized());
1570 assert(index >= 0 && index < size());
1571 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1576 //////////////////////////////////////////////////////////////////////
1578 // Visible Statistics Types
1580 //////////////////////////////////////////////////////////////////////
1581 /**@defgroup VStats VisibleStatTypes
1585 *This is the simplest counting stat. Default type is Counter, but can be
1586 *anything (like double, int, etc). To bin, just designate the name of the bin
1587 * when declaring. It can be used like a regular Counter.
1588 *Example: Stat<> foo;
1591 template <typename T = Counter, class Bin = NoBin>
1592 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
1595 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
1597 /** sets Stat equal to value of type U */
1598 template <typename U>
1599 void operator=(const U& v) { Base::operator=(v); }
1603 *This calculates averages over number of cycles. Additionally, the update per
1604 *cycle is implicit if there is no change. In other words, if you want to know
1605 *the average number of instructions in the IQ per cycle, then you can use this
1606 * stat and not have to update it on cycles where there is no change.
1608 template <typename T = Counter, class Bin = NoBin>
1609 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
1612 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
1614 /** sets Average equalt to value of type U*/
1615 template <typename U>
1616 void operator=(const U& v) { Base::operator=(v); }
1620 *This is a vector of type T, ideally suited to track stats across something like
1623 template <typename T = Counter, class Bin = NoBin>
1624 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
1628 *This is a vector of Averages of type T
1630 template <typename T = Counter, class Bin = NoBin>
1631 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
1635 *This is a 2-dimensional vector. Intended usage is for something like tracking a
1636 * Vector stat across another Vector like SMT threads.
1638 template <typename T = Counter, class Bin = NoBin>
1639 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
1643 * This is essentially a Vector, but with minor differences. Where a
1644 * Vector's index maps directly to what it's tracking, a Distribution's index can
1645 * map to an arbitrary bucket type. For example, you could map 1-8 to bucket 0
1646 * of a Distribution, and if ever there are 1-8 instructions within an IQ, increment
1649 template <typename T = Counter, class Bin = NoBin>
1650 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
1653 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1654 typedef typename Detail::DistStor<T>::Params Params;
1658 *This must be called to set some data members of the distribution
1659 *as well as to allocate the appropriate storage size.
1660 *@param min The minimum value of the Distribution
1661 *@param max The maximum value of the Distribution (NOT the size!)
1662 *@param bkt The size of the buckets to indicate mapping. I.e. if you have
1663 *min=0, max=15, bkt=8, you will have two buckets, and anything from 0-7
1664 *will go into bucket 0, and anything from 8-15 be in bucket 1.
1665 *@return the Distribution itself.
1667 Distribution &init(T min, T max, int bkt) {
1670 params.bucket_size = bkt;
1671 params.size = (max - min) / bkt + 1;
1680 *This has the functionality of a standard deviation built into it. Update it
1681 *every cycle, and at the end you will have the standard deviation.
1683 template <typename T = Counter, class Bin = NoBin>
1684 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
1687 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1688 typedef typename Detail::DistStor<T>::Params Params;
1691 StandardDeviation() {
1698 *This also calculates standard deviations, but there is no need to
1699 *update every cycle if there is no change, the stat will update for you.
1701 template <typename T = Counter, class Bin = NoBin>
1702 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
1705 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1706 typedef typename Detail::DistStor<T>::Params Params;
1709 AverageDeviation() {
1716 *This is a vector of Distributions. (The complexity increases!). Intended usage
1717 * is for something like tracking a distribution across a vector like SMT threads.
1719 template <typename T = Counter, class Bin = NoBin>
1720 class VectorDistribution
1721 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
1724 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
1725 typedef typename Detail::DistStor<T>::Params Params;
1729 *This must be called to set some data members and allocate storage space.
1730 *@param size The size of the Vector
1731 *@param min The minumum value of the Distribution
1732 *@param max The maximum value of the Distribution (NOT the size)
1733 *@param bkt The range of the bucket. I.e if min=0, max=15, and bkt=8,
1734 *then 0-7 will be bucket 0, and 8-15 will be bucket 1.
1735 *@return return the VectorDistribution itself.
1737 VectorDistribution &init(int size, T min, T max, int bkt) {
1740 params.bucket_size = bkt;
1741 params.size = (max - min) / bkt + 1;
1742 bin.init(size, params);
1750 *This is a vector of Standard Deviations. Intended usage is for tracking
1751 *Standard Deviations across a vector like SMT threads.
1753 template <typename T = Counter, class Bin = NoBin>
1754 class VectorStandardDeviation
1755 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
1758 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
1759 typedef typename Detail::DistStor<T>::Params Params;
1762 /** This must be called to initialize some data members and allocate
1763 * approprate storage space for the stat.
1764 *@param size The size of the Vector
1765 * @return the VectorStandardDeviation itself.
1767 VectorStandardDeviation &init(int size) {
1768 bin.init(size, params);
1776 * This is a vector of Average Deviations. Intended usage is for tracking
1777 *Average Deviations across a vector like SMT threads.
1779 template <typename T = Counter, class Bin = NoBin>
1780 class VectorAverageDeviation
1781 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
1784 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
1785 typedef typename Detail::DistStor<T>::Params Params;
1788 /** This must be called to initialize some data members and allocate
1789 * approprate storage space for the stat.
1790 *@param size The size of the Vector
1791 * @return The VectorAverageDeviation itself.
1793 VectorAverageDeviation &init(int size) {
1794 bin.init(size, params);
1802 *This is a formula type. When defining it, you can just say:
1803 *Formula foo = manchu + 3 / bar;
1804 *The calculations for Formulas are all done at the end of the simulation, this
1805 *really is just a definition of how to calculate at the end.
1807 class Formula : public Detail::VectorStat
1810 /** The root of the tree which represents the Formula */
1811 Detail::NodePtr root;
1812 friend class Detail::Temp;
1815 Formula() : VectorStat(true) { setInit(); }
1816 Formula(Detail::Temp r) : VectorStat(true) {
1821 const Formula &operator=(Detail::Temp r) {
1822 assert(!root && "Can't change formulas");
1828 const Formula &operator+=(Detail::Temp r) {
1829 using namespace Detail;
1831 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
1838 const rvec_t &val() const { return root->val(); }
1839 result_t total() const { return root->total(); }
1841 size_t size() const {
1845 return root->size();
1850 void dump(std::ostream &stream);
1853 operator+(Detail::Temp l, Detail::Temp r)
1855 using namespace Detail;
1856 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
1860 operator-(Detail::Temp l, Detail::Temp r)
1862 using namespace Detail;
1863 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
1867 operator*(Detail::Temp l, Detail::Temp r)
1869 using namespace Detail;
1870 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
1874 operator/(Detail::Temp l, Detail::Temp r)
1876 using namespace Detail;
1877 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
1881 operator%(Detail::Temp l, Detail::Temp r)
1883 using namespace Detail;
1884 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
1888 operator-(Detail::Temp l)
1890 using namespace Detail;
1891 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
1894 template <typename T>
1898 using namespace Detail;
1899 return NodePtr(new ConstNode<T>(val));
1902 template <typename T>
1906 using namespace Detail;
1907 return NodePtr(new FunctorNode<T>(val));
1910 template <typename T>
1914 using namespace Detail;
1915 return NodePtr(new ScalarNode<T>(val));
1919 sum(Detail::Temp val)
1921 using namespace Detail;
1922 return NodePtr(new SumNode<std::plus<result_t> >(val));
1925 extern bool PrintDescriptions;
1927 } // namespace statistics
1929 #endif // __STATISTICS_HH__