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.
34 * Generalized N-dimensinal vector
39 * -- these both can use the same function that prints out a
40 * specific set of stats
41 * VectorStandardDeviation totals
44 #ifndef __STATISTICS_HH__
45 #define __STATISTICS_HH__
56 #include "base/refcnt.hh"
57 #include "base/str.hh"
59 #include "sim/host.hh"
67 #define STAT_DISPLAY_COMPAT
71 namespace Statistics {
72 typedef double result_t;
73 typedef std::vector<result_t> rvec_t;
75 typedef u_int32_t FormatFlags;
76 const FormatFlags none = 0x0000;
77 const FormatFlags total = 0x0001;
78 const FormatFlags pdf = 0x0002;
79 const FormatFlags nozero = 0x0004;
80 const FormatFlags nonan = 0x0008;
81 const FormatFlags cdf = 0x0010;
82 const FormatFlags dist = 0x0020;
83 const FormatFlags __substat = 0x8000;
84 const FormatFlags __reserved = __substat;
87 //////////////////////////////////////////////////////////////////////
89 // Statistics Framework Base classes
91 //////////////////////////////////////////////////////////////////////
96 *The base class of all Stats. This does NOT actually hold all the data, but
97 *it does provide the means for accessing all the Stats data.
104 const StatData *mydata() const;
106 const SubData *mysubdata(int index) const;
107 SubData *mysubdata_create(int index);
110 virtual std::string myname() const;
111 virtual std::string mysubname(int index) const;
112 virtual std::string mydesc() const;
113 virtual std::string mysubdesc(int index) const;
114 virtual FormatFlags myflags() const;
115 virtual bool dodisplay() const;
116 virtual int myprecision() const;
122 virtual void display(std::ostream &stream) const = 0;
123 virtual size_t size() const = 0;
124 virtual bool zero() const = 0;
126 Stat &name(const std::string &name);
127 Stat &desc(const std::string &desc);
128 Stat &precision(int p);
129 Stat &flags(FormatFlags f);
130 Stat &prereq(const Stat &prereq);
131 Stat &subname(int index, const std::string &name);
132 Stat &subdesc(int index, const std::string &name);
135 static bool less(Stat *stat1, Stat *stat2);
142 // Scalar stats involved in formulas
143 class ScalarStat : public Stat
146 ScalarStat(bool reg) : Stat(reg) {}
147 virtual result_t val() const = 0;
148 virtual bool zero() const;
149 virtual void display(std::ostream &stream) const;
153 VectorDisplay(std::ostream &stream, const std::string &myname,
154 const std::vector<std::string> *mysubnames,
155 const std::string &mydesc,
156 const std::vector<std::string> *mysubdescs,
157 int myprecision, FormatFlags myflags, const rvec_t &vec,
160 // Vector stats involved in formulas
161 class VectorStat : public Stat
164 VectorStat(bool reg) : Stat(reg) {}
165 virtual const rvec_t &val() const = 0;
166 virtual result_t total() const = 0;
167 virtual bool zero() const;
168 virtual void display(std::ostream &stream) const;
171 //////////////////////////////////////////////////////////////////////
175 //////////////////////////////////////////////////////////////////////
176 template <typename T>
186 StatStor(const Params &) : data(T()) {}
188 void set(T val, const Params &p) { data = val; }
189 void inc(T val, const Params &p) { data += val; }
190 void dec(T val, const Params &p) { data -= val; }
191 result_t val(const Params &p) const { return (result_t)data; }
192 T value(const Params &p) const { return data; }
195 template <typename T>
203 mutable result_t total;
207 AvgStor(const Params &) : current(T()), total(0), last(0) { }
209 void set(T val, const Params &p) {
210 total += current * (curTick - last);
214 void inc(T val, const Params &p) { set(current + val, p); }
215 void dec(T val, const Params &p) { set(current - val, p); }
216 result_t val(const Params &p) const {
217 total += current * (curTick - last);
219 return (result_t)(total + current) / (result_t)(curTick + 1);
221 T value(const Params &p) const { return current; }
224 template <typename T, template <typename T> class Storage, class Bin>
225 class ScalarBase : public ScalarStat
228 typedef Storage<T> storage_t;
229 typedef typename storage_t::Params params_t;
230 typedef typename Bin::Bin<storage_t> bin_t;
237 storage_t *data() { return bin.data(params); }
238 const storage_t *data() const {
239 return (const_cast<bin_t *>(&bin))->data(params);
243 // Copying stats is not allowed
244 ScalarBase(const ScalarBase &stat);
245 const ScalarBase &operator=(const ScalarBase &);
248 result_t val() const { return data()->val(params); }
249 T value() const { return data()->value(params); }
252 ScalarBase() : ScalarStat(true) {
258 // Common operators for stats
259 void operator++() { data()->inc(1, params); }
260 void operator--() { data()->dec(1, params); }
262 void operator++(int) { ++*this; }
263 void operator--(int) { --*this; }
265 template <typename U>
266 void operator=(const U& v) { data()->set(v, params); }
268 template <typename U>
269 void operator+=(const U& v) { data()->inc(v, params); }
271 template <typename U>
272 void operator-=(const U& v) { data()->dec(v, params); }
274 virtual size_t size() const { return 1; }
277 //////////////////////////////////////////////////////////////////////
281 //////////////////////////////////////////////////////////////////////
282 template <typename T, template <typename T> class Storage, class Bin>
285 template <typename T, template <typename T> class Storage, class Bin>
286 class VectorBase : public VectorStat
289 typedef Storage<T> storage_t;
290 typedef typename storage_t::Params params_t;
291 typedef typename Bin::VectorBin<storage_t> bin_t;
301 storage_t *data(int index) { return bin.data(index, params); }
302 const storage_t *data(int index) const {
303 return (const_cast<bin_t *>(&bin))->data(index, params);
307 // Copying stats is not allowed
308 VectorBase(const VectorBase &stat);
309 const VectorBase &operator=(const VectorBase &);
312 const rvec_t &val() const {
316 vec = new rvec_t(size());
318 for (int i = 0; i < size(); ++i)
319 (*vec)[i] = data(i)->val(params);
324 result_t total() const {
325 result_t total = 0.0;
326 for (int i = 0; i < size(); ++i)
327 total += data(i)->val(params);
332 VectorBase() : VectorStat(true), vec(NULL) {}
333 ~VectorBase() { if (vec) delete vec; }
335 VectorBase &init(size_t size) {
336 bin.init(size, params);
342 friend class ScalarProxy<T, Storage, Bin>;
343 ScalarProxy<T, Storage, Bin> operator[](int index);
345 virtual size_t size() const { return bin.size(); }
348 template <typename T, template <typename T> class Storage, class Bin>
349 class ScalarProxy : public ScalarStat
352 typedef Storage<T> storage_t;
353 typedef typename storage_t::Params params_t;
354 typedef typename Bin::VectorBin<storage_t> bin_t;
362 storage_t *data() { return bin->data(index, *params); }
363 const storage_t *data() const { return bin->data(index, *params); }
366 result_t val() const { return data()->val(*params); }
367 T value() const { return data()->value(*params); }
370 ScalarProxy(bin_t &b, params_t &p, int i)
371 : ScalarStat(false), bin(&b), params(&p), index(i) {}
372 ScalarProxy(const ScalarProxy &sp)
373 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
374 const ScalarProxy &operator=(const ScalarProxy &sp) {
382 // Common operators for stats
383 void operator++() { data()->inc(1, *params); }
384 void operator--() { data()->dec(1, *params); }
386 void operator++(int) { ++*this; }
387 void operator--(int) { --*this; }
389 template <typename U>
390 void operator=(const U& v) { data()->set(v, *params); }
392 template <typename U>
393 void operator+=(const U& v) { data()->inc(v, *params); }
395 template <typename U>
396 void operator-=(const U& v) { data()->dec(v, *params); }
398 virtual size_t size() const { return 1; }
401 template <typename T, template <typename T> class Storage, class Bin>
402 inline ScalarProxy<T, Storage, Bin>
403 VectorBase<T, Storage, Bin>::operator[](int index)
405 assert (index >= 0 && index < size());
406 return ScalarProxy<T, Storage, Bin>(bin, params, index);
409 template <typename T, template <typename T> class Storage, class Bin>
412 template <typename T, template <typename T> class Storage, class Bin>
413 class Vector2dBase : public Stat
416 typedef Storage<T> storage_t;
417 typedef typename storage_t::Params params_t;
418 typedef typename Bin::VectorBin<storage_t> bin_t;
425 std::vector<std::string> *y_subnames;
428 storage_t *data(int index) { return bin.data(index, params); }
429 const storage_t *data(int index) const {
430 return (const_cast<bin_t *>(&bin))->data(index, params);
434 // Copying stats is not allowed
435 Vector2dBase(const Vector2dBase &stat);
436 const Vector2dBase &operator=(const Vector2dBase &);
439 Vector2dBase() : Stat(true) {}
442 Vector2dBase &init(size_t _x, size_t _y) {
445 bin.init(x * y, params);
447 y_subnames = new std::vector<std::string>(y);
453 * This makes the assumption that if you're gonna subnames a 2d vector,
454 * you're subnaming across all y
456 Vector2dBase &ysubnames(const char **names)
458 for (int i=0; i < y; ++i) {
459 (*y_subnames)[i] = names[i];
463 Vector2dBase &ysubname(int index, const std::string subname)
465 (*y_subnames)[i] = subname.c_str();
468 std::string ysubname(int i) const { return (*y_subnames)[i]; }
470 friend class VectorProxy<T, Storage, Bin>;
471 VectorProxy<T, Storage, Bin> operator[](int index);
473 virtual size_t size() const { return bin.size(); }
474 virtual bool zero() const { return data(0)->value(params) == 0.0; }
477 display(std::ostream &out) const
479 bool have_subname = false;
480 for (int i = 0; i < x; ++i) {
481 if (!mysubname(i).empty())
486 result_t super_total = 0.0;
487 for (int i = 0; i < x; ++i) {
490 subname = mysubname(i);
494 subname = to_string(i);
499 result_t total = 0.0;
500 for (int j = 0; j < y; ++j) {
501 vec[j] = data(iy + j)->val(params);
502 tot_vec[j] += vec[j];
504 super_total += vec[j];
508 if (mysubdesc(i).empty()) {
514 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
515 myprecision(), myflags(), vec, total);
518 if ((myflags() & ::Statistics::total) && (x > 1)) {
519 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
520 myprecision(), myflags(), tot_vec, super_total);
526 template <typename T, template <typename T> class Storage, class Bin>
527 class VectorProxy : public VectorStat
530 typedef Storage<T> storage_t;
531 typedef typename storage_t::Params params_t;
532 typedef typename Bin::VectorBin<storage_t> bin_t;
543 storage_t *data(int index) {
545 return bin->data(offset + index, *params);
548 const storage_t *data(int index) const {
549 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
553 const rvec_t &val() const {
557 vec = new rvec_t(size());
559 for (int i = 0; i < size(); ++i)
560 (*vec)[i] = data(i)->val(*params);
565 result_t total() const {
566 result_t total = 0.0;
567 for (int i = 0; i < size(); ++i)
568 total += data(i)->val(*params);
573 VectorProxy(bin_t &b, params_t &p, int o, int l)
574 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
576 VectorProxy(const VectorProxy &sp)
577 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
578 len(sp.len), vec(NULL)
585 const VectorProxy &operator=(const VectorProxy &sp) {
596 virtual size_t size() const { return len; }
598 ScalarProxy<T, Storage, Bin> operator[](int index) {
599 assert (index >= 0 && index < size());
600 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
604 template <typename T, template <typename T> class Storage, class Bin>
605 inline VectorProxy<T, Storage, Bin>
606 Vector2dBase<T, Storage, Bin>::operator[](int index)
608 int offset = index * y;
609 assert (index >= 0 && offset < size());
610 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
613 //////////////////////////////////////////////////////////////////////
615 // Non formula statistics
617 //////////////////////////////////////////////////////////////////////
619 void DistDisplay(std::ostream &stream, const std::string &name,
620 const std::string &desc, int precision, FormatFlags flags,
621 result_t min_val, result_t max_val,
622 result_t underflow, result_t overflow,
623 const rvec_t &vec, int min, int max, int bucket_size,
626 template <typename T>
646 DistStor(const Params ¶ms)
647 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
650 void sample(T val, int number, const Params ¶ms) {
651 if (val < params.min)
653 else if (val > params.max)
656 int index = (val - params.min) / params.bucket_size;
657 assert(index < size(params));
658 vec[index] += number;
668 size_t size(const Params &) const { return vec.size(); }
670 bool zero(const Params ¶ms) const {
671 if (underflow != 0 || overflow != 0)
674 int s = size(params);
675 for (int i = 0; i < s; i++)
682 void display(std::ostream &stream, const std::string &name,
683 const std::string &desc, int precision, FormatFlags flags,
684 const Params ¶ms) const {
686 #ifdef STAT_DISPLAY_COMPAT
687 result_t min = params.min;
689 result_t min = (min_val == INT_MAX) ? params.min : min_val;
691 result_t max = (max_val == INT_MIN) ? 0 : max_val;
693 rvec_t rvec(params.size);
694 for (int i = 0; i < params.size; ++i)
697 DistDisplay(stream, name, desc, precision, flags,
698 (result_t)min, (result_t)max,
699 (result_t)underflow, (result_t)overflow,
700 rvec, params.min, params.max, params.bucket_size,
705 void FancyDisplay(std::ostream &stream, const std::string &name,
706 const std::string &desc, int precision, FormatFlags flags,
707 result_t mean, result_t variance);
708 template <typename T>
720 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
722 void sample(T val, int number, const Params &) {
723 T value = val * number;
725 squares += value * value;
728 void display(std::ostream &stream, const std::string &name,
729 const std::string &desc, int precision, FormatFlags flags,
730 const Params &) const {
733 result_t variance = NAN;
737 result_t fsq = squares;
738 result_t ftot = total;
741 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
744 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
747 size_t size(const Params &) const { return 1; }
748 bool zero(const Params &) const { return total == 0; }
751 template <typename T>
762 AvgFancy(const Params &) : sum(0), squares(0) {}
764 void sample(T val, int number, const Params& p) {
765 T value = val * number;
767 squares += value * value;
769 void display(std::ostream &stream, const std::string &name,
770 const std::string &desc, int precision, FormatFlags flags,
771 const Params ¶ms) const {
772 result_t mean = sum / curTick;
773 result_t variance = (squares - sum * sum) / curTick;
775 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
778 size_t size(const Params ¶ms) const { return 1; }
779 bool zero(const Params ¶ms) const { return sum == 0; }
782 template <typename T, template <typename T> class Storage, class Bin>
783 class DistBase : public Stat
786 typedef Storage<T> storage_t;
787 typedef typename storage_t::Params params_t;
788 typedef typename Bin::Bin<storage_t> bin_t;
795 storage_t *data() { return bin.data(params); }
796 const storage_t *data() const {
797 return (const_cast<bin_t *>(&bin))->data(params);
801 // Copying stats is not allowed
802 DistBase(const DistBase &stat);
803 const DistBase &operator=(const DistBase &);
806 DistBase() : Stat(true) { }
809 template <typename U>
810 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
812 virtual size_t size() const { return data()->size(params); }
813 virtual bool zero() const { return data()->zero(params); }
814 virtual void display(std::ostream &stream) const {
815 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
820 template <typename T, template <typename T> class Storage, class Bin>
821 class VectorDistProxy;
823 template <typename T, template <typename T> class Storage, class Bin>
824 class VectorDistBase : public Stat
827 typedef Storage<T> storage_t;
828 typedef typename storage_t::Params params_t;
829 typedef typename Bin::VectorBin<storage_t> bin_t;
836 storage_t *data(int index) { return bin.data(index, params); }
837 const storage_t *data(int index) const {
838 return (const_cast<bin_t *>(&bin))->data(index, params);
842 // Copying stats is not allowed
843 VectorDistBase(const VectorDistBase &stat);
844 const VectorDistBase &operator=(const VectorDistBase &);
847 VectorDistBase() : Stat(true) { }
848 ~VectorDistBase() { }
850 friend class VectorDistProxy<T, Storage, Bin>;
851 VectorDistProxy<T, Storage, Bin> operator[](int index);
852 const VectorDistProxy<T, Storage, Bin> operator[](int index) const;
854 virtual size_t size() const { return bin.size(); }
855 virtual bool zero() const { return false; }
856 virtual void display(std::ostream &stream) const;
859 template <typename T, template <typename T> class Storage, class Bin>
860 class VectorDistProxy : public Stat
863 typedef Storage<T> storage_t;
864 typedef typename storage_t::Params params_t;
865 typedef typename Bin::Bin<storage_t> bin_t;
866 typedef VectorDistBase<T, Storage, Bin> base_t;
876 storage_t *data() { return stat->data(index); }
877 const storage_t *data() const { return cstat->data(index); }
880 VectorDistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
881 : Stat(false), cstat(&s), index(i) {}
882 VectorDistProxy(const VectorDistProxy &sp)
883 : Stat(false), cstat(sp.cstat), index(sp.index) {}
884 const VectorDistProxy &operator=(const VectorDistProxy &sp) {
885 cstat = sp.cstat; index = sp.index; return *this;
889 template <typename U>
890 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
892 virtual size_t size() const { return 1; }
893 virtual bool zero() const {
894 return data()->zero(cstat->params);
896 virtual void display(std::ostream &stream) const {
897 std::stringstream name, desc;
899 if (!(cstat->mysubname(index).empty())) {
900 name << cstat->myname() << cstat->mysubname(index);
902 name << cstat->myname() << "_" << index;
904 if (!(cstat->mysubdesc(index).empty())) {
905 desc << cstat->mysubdesc(index);
907 desc << cstat->mydesc();
910 data()->display(stream, name.str(), desc.str(),
911 cstat->myprecision(), cstat->myflags(), cstat->params);
915 template <typename T, template <typename T> class Storage, class Bin>
916 inline VectorDistProxy<T, Storage, Bin>
917 VectorDistBase<T, Storage, Bin>::operator[](int index)
919 assert (index >= 0 && index < size());
920 return VectorDistProxy<T, Storage, Bin>(*this, index);
923 template <typename T, template <typename T> class Storage, class Bin>
924 inline const VectorDistProxy<T, Storage, Bin>
925 VectorDistBase<T, Storage, Bin>::operator[](int index) const
927 assert (index >= 0 && index < size());
928 return VectorDistProxy<T, Storage, Bin>(*this, index);
932 * @todo Need a way to print Distribution totals across the Vector
934 template <typename T, template <typename T> class Storage, class Bin>
936 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
938 for (int i = 0; i < size(); ++i) {
939 VectorDistProxy<T, Storage, Bin> proxy(*this, i);
940 proxy.display(stream);
946 VectorDistBase<T, Storage, Bin>::total(int index) const
949 for (int i=0; i < x_size(); ++i) {
950 total += data(i)->val(*params);
955 //////////////////////////////////////////////////////////////////////
959 //////////////////////////////////////////////////////////////////////
960 class Node : public RefCounted
963 virtual size_t size() const = 0;
964 virtual const rvec_t &val() const = 0;
965 virtual result_t total() const = 0;
968 typedef RefCountingPtr<Node> NodePtr;
970 class ScalarStatNode : public Node
973 const ScalarStat &stat;
974 mutable rvec_t result;
977 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
978 const rvec_t &val() const { result[0] = stat.val(); return result; }
979 virtual result_t total() const { return stat.val(); };
981 virtual size_t size() const { return 1; }
984 template <typename T, template <typename T> class Storage, class Bin>
985 class ScalarProxyNode : public Node
988 const ScalarProxy<T, Storage, Bin> proxy;
989 mutable rvec_t result;
992 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
993 : proxy(p), result(1) { }
994 const rvec_t &val() const { result[0] = proxy.val(); return result; }
995 virtual result_t total() const { return proxy.val(); };
997 virtual size_t size() const { return 1; }
1000 class VectorStatNode : public Node
1003 const VectorStat &stat;
1006 VectorStatNode(const VectorStat &s) : stat(s) {}
1007 const rvec_t &val() const { return stat.val(); }
1008 virtual result_t total() const { return stat.total(); };
1010 virtual size_t size() const { return stat.size(); }
1013 template <typename T>
1014 class ConstNode : public Node
1020 ConstNode(T s) : data(1, (result_t)s) {}
1021 const rvec_t &val() const { return data; }
1022 virtual result_t total() const { return data[0]; };
1024 virtual size_t size() const { return 1; }
1027 template <typename T>
1028 class FunctorNode : public Node
1032 mutable rvec_t result;
1035 FunctorNode(T &f) : functor(f) { result.resize(1); }
1036 const rvec_t &val() const {
1037 result[0] = (result_t)functor();
1040 virtual result_t total() const { return (result_t)functor(); };
1042 virtual size_t size() const { return 1; }
1045 template <typename T>
1046 class ScalarNode : public Node
1050 mutable rvec_t result;
1053 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1054 const rvec_t &val() const {
1055 result[0] = (result_t)scalar;
1058 virtual result_t total() const { return (result_t)scalar; };
1060 virtual size_t size() const { return 1; }
1064 class UnaryNode : public Node
1068 mutable rvec_t result;
1071 UnaryNode(NodePtr p) : l(p) {}
1073 const rvec_t &val() const {
1074 const rvec_t &lvec = l->val();
1075 int size = lvec.size();
1079 result.resize(size);
1081 for (int i = 0; i < size; ++i)
1082 result[i] = op(lvec[i]);
1087 result_t total() const {
1089 return op(l->total());
1092 virtual size_t size() const { return l->size(); }
1096 class BinaryNode : public Node
1101 mutable rvec_t result;
1104 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1106 const rvec_t &val() const {
1108 const rvec_t &lvec = l->val();
1109 const rvec_t &rvec = r->val();
1111 assert(lvec.size() > 0 && rvec.size() > 0);
1113 if (lvec.size() == 1 && rvec.size() == 1) {
1115 result[0] = op(lvec[0], rvec[0]);
1116 } else if (lvec.size() == 1) {
1117 int size = rvec.size();
1118 result.resize(size);
1119 for (int i = 0; i < size; ++i)
1120 result[i] = op(lvec[0], rvec[i]);
1121 } else if (rvec.size() == 1) {
1122 int size = lvec.size();
1123 result.resize(size);
1124 for (int i = 0; i < size; ++i)
1125 result[i] = op(lvec[i], rvec[0]);
1126 } else if (rvec.size() == lvec.size()) {
1127 int size = rvec.size();
1128 result.resize(size);
1129 for (int i = 0; i < size; ++i)
1130 result[i] = op(lvec[i], rvec[i]);
1136 result_t total() const {
1138 return op(l->total(), r->total());
1141 virtual size_t size() const {
1149 assert(ls == rs && "Node vector sizes are not equal");
1156 class SumNode : public Node
1160 mutable rvec_t result;
1163 SumNode(NodePtr p) : l(p), result(1) {}
1165 const rvec_t &val() const {
1166 const rvec_t &lvec = l->val();
1167 int size = lvec.size();
1173 for (int i = 0; i < size; ++i)
1174 result[0] = op(result[0], lvec[i]);
1179 result_t total() const {
1180 const rvec_t &lvec = l->val();
1181 int size = lvec.size();
1184 result_t result = 0.0;
1187 for (int i = 0; i < size; ++i)
1188 result = op(result, lvec[i]);
1193 virtual size_t size() const { return 1; }
1202 Temp(NodePtr n) : node(n) {}
1203 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1204 template <typename T, template <typename T> class Storage, class Bin>
1205 Temp(const ScalarProxy<T, Storage, Bin> &p)
1206 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1207 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1209 #define TempSCALAR(T) \
1210 Temp(T value) : node(new ConstNode<T>(value)) {}
1212 TempSCALAR( signed char);
1213 TempSCALAR(unsigned char);
1214 TempSCALAR( signed short);
1215 TempSCALAR(unsigned short);
1216 TempSCALAR( signed int);
1217 TempSCALAR(unsigned int);
1218 TempSCALAR( signed long);
1219 TempSCALAR(unsigned long);
1220 TempSCALAR( signed long long);
1221 TempSCALAR(unsigned long long);
1226 operator NodePtr() { return node;}
1230 //////////////////////////////////////////////////////////////////////
1232 // Binning Interface
1234 //////////////////////////////////////////////////////////////////////
1243 off_t size() const { return memsize; }
1247 BinBase(size_t size);
1251 } // namespace Detail
1253 template <class BinType>
1254 struct StatBin : public Detail::BinBase
1256 static StatBin *&curBin() {
1257 static StatBin *current = NULL;
1261 static void setCurBin(StatBin *bin) { curBin() = bin; }
1262 static StatBin *current() { assert(curBin()); return curBin(); }
1264 static off_t &offset() {
1265 static off_t offset = 0;
1269 static off_t new_offset(size_t size) {
1270 size_t mask = sizeof(u_int64_t) - 1;
1271 off_t off = offset();
1273 // That one is for the last trailing flags byte.
1274 offset() += (size + 1 + mask) & ~mask;
1279 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1281 char *memory(off_t off) {
1282 assert(offset() <= size());
1283 return Detail::BinBase::memory() + off;
1286 static void activate(StatBin &bin) { setCurBin(&bin); }
1294 BinBase() : offset(-1) {}
1295 void allocate(size_t size) {
1296 offset = new_offset(size);
1299 assert(offset != -1);
1300 return current()->memory(offset);
1304 template <class Storage>
1305 class Bin : public BinBase
1308 typedef typename Storage::Params Params;
1311 Bin() { allocate(sizeof(Storage)); }
1312 bool initialized() const { return true; }
1313 void init(const Params ¶ms) { }
1315 int size() const { return 1; }
1317 Storage *data(const Params ¶ms) {
1318 assert(initialized());
1319 char *ptr = access();
1320 char *flags = ptr + sizeof(Storage);
1321 if (!(*flags & 0x1)) {
1323 new (ptr) Storage(params);
1325 return reinterpret_cast<Storage *>(ptr);
1329 template <class Storage>
1330 class VectorBin : public BinBase
1333 typedef typename Storage::Params Params;
1339 VectorBin() : _size(0) {}
1341 bool initialized() const { return _size > 0; }
1342 void init(int s, const Params ¶ms) {
1343 assert(!initialized());
1346 allocate(_size * sizeof(Storage));
1349 int size() const { return _size; }
1351 Storage *data(int index, const Params ¶ms) {
1352 assert(initialized());
1353 assert(index >= 0 && index < size());
1354 char *ptr = access();
1355 char *flags = ptr + size() * sizeof(Storage);
1356 if (!(*flags & 0x1)) {
1358 for (int i = 0; i < size(); ++i)
1359 new (ptr + i * sizeof(Storage)) Storage(params);
1361 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1366 class MainBinType {};
1367 typedef StatBin<MainBinType> MainBin;
1371 template <class Storage>
1375 typedef typename Storage::Params Params;
1378 char ptr[sizeof(Storage)];
1381 bool initialized() const { return true; }
1382 void init(const Params ¶ms) {
1383 new (ptr) Storage(params);
1385 int size() const{ return 1; }
1386 Storage *data(const Params ¶ms) {
1387 assert(initialized());
1388 return reinterpret_cast<Storage *>(ptr);
1392 template <class Storage>
1396 typedef typename Storage::Params Params;
1403 VectorBin() : ptr(NULL) { }
1408 bool initialized() const { return ptr != NULL; }
1409 void init(int s, const Params ¶ms) {
1410 assert(s > 0 && "size must be positive!");
1411 assert(!initialized());
1413 ptr = new char[_size * sizeof(Storage)];
1414 for (int i = 0; i < _size; ++i)
1415 new (ptr + i * sizeof(Storage)) Storage(params);
1418 int size() const { return _size; }
1420 Storage *data(int index, const Params ¶ms) {
1421 assert(initialized());
1422 assert(index >= 0 && index < size());
1423 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
1428 //////////////////////////////////////////////////////////////////////
1430 // Visible Statistics Types
1432 //////////////////////////////////////////////////////////////////////
1433 /**@defgroup VStats VisibleStatTypes
1437 *This is the simplest counting stat. Default type is Counter, but can be
1438 *anything (like double, int, etc). To bin, just designate the name of the bin
1439 * when declaring. It can be used like a regular Counter.
1440 *Example: Stat<> foo;
1443 template <typename T = Counter, class Bin = NoBin>
1444 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
1447 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
1449 /** sets Stat equal to value of type U */
1450 template <typename U>
1451 void operator=(const U& v) { Base::operator=(v); }
1455 *This calculates averages over number of cycles. Additionally, the update per
1456 *cycle is implicit if there is no change. In other words, if you want to know
1457 *the average number of instructions in the IQ per cycle, then you can use this
1458 * stat and not have to update it on cycles where there is no change.
1460 template <typename T = Counter, class Bin = NoBin>
1461 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
1464 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
1466 /** sets Average equalt to value of type U*/
1467 template <typename U>
1468 void operator=(const U& v) { Base::operator=(v); }
1472 *This is a vector of type T, ideally suited to track stats across something like
1475 template <typename T = Counter, class Bin = NoBin>
1476 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
1480 *This is a vector of Averages of type T
1482 template <typename T = Counter, class Bin = NoBin>
1483 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
1487 *This is a 2-dimensional vector. Intended usage is for something like tracking a
1488 * Vector stat across another Vector like SMT threads.
1490 template <typename T = Counter, class Bin = NoBin>
1491 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
1495 * This is essentially a Vector, but with minor differences. Where a
1496 * Vector's index maps directly to what it's tracking, a Distribution's index can
1497 * map to an arbitrary bucket type. For example, you could map 1-8 to bucket 0
1498 * of a Distribution, and if ever there are 1-8 instructions within an IQ, increment
1501 template <typename T = Counter, class Bin = NoBin>
1502 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
1505 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1506 typedef typename Detail::DistStor<T>::Params Params;
1510 *This must be called to set some data members of the distribution
1511 *as well as to allocate the appropriate storage size.
1512 *@param min The minimum value of the Distribution
1513 *@param max The maximum value of the Distribution (NOT the size!)
1514 *@param bkt The size of the buckets to indicate mapping. I.e. if you have
1515 *min=0, max=15, bkt=8, you will have two buckets, and anything from 0-7
1516 *will go into bucket 0, and anything from 8-15 be in bucket 1.
1517 *@return the Distribution itself.
1519 Distribution &init(T min, T max, int bkt) {
1522 params.bucket_size = bkt;
1523 params.size = (max - min) / bkt + 1;
1532 *This has the functionality of a standard deviation built into it. Update it
1533 *every cycle, and at the end you will have the standard deviation.
1535 template <typename T = Counter, class Bin = NoBin>
1536 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
1539 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1540 typedef typename Detail::DistStor<T>::Params Params;
1543 StandardDeviation() {
1550 *This also calculates standard deviations, but there is no need to
1551 *update every cycle if there is no change, the stat will update for you.
1553 template <typename T = Counter, class Bin = NoBin>
1554 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
1557 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
1558 typedef typename Detail::DistStor<T>::Params Params;
1561 AverageDeviation() {
1568 *This is a vector of Distributions. (The complexity increases!). Intended usage
1569 * is for something like tracking a distribution across a vector like SMT threads.
1571 template <typename T = Counter, class Bin = NoBin>
1572 class VectorDistribution
1573 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
1576 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
1577 typedef typename Detail::DistStor<T>::Params Params;
1581 *This must be called to set some data members and allocate storage space.
1582 *@param size The size of the Vector
1583 *@param min The minumum value of the Distribution
1584 *@param max The maximum value of the Distribution (NOT the size)
1585 *@param bkt The range of the bucket. I.e if min=0, max=15, and bkt=8,
1586 *then 0-7 will be bucket 0, and 8-15 will be bucket 1.
1587 *@return return the VectorDistribution itself.
1589 VectorDistribution &init(int size, T min, T max, int bkt) {
1592 params.bucket_size = bkt;
1593 params.size = (max - min) / bkt + 1;
1594 bin.init(size, params);
1602 *This is a vector of Standard Deviations. Intended usage is for tracking
1603 *Standard Deviations across a vector like SMT threads.
1605 template <typename T = Counter, class Bin = NoBin>
1606 class VectorStandardDeviation
1607 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
1610 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
1611 typedef typename Detail::DistStor<T>::Params Params;
1614 /** This must be called to initialize some data members and allocate
1615 * approprate storage space for the stat.
1616 *@param size The size of the Vector
1617 * @return the VectorStandardDeviation itself.
1619 VectorStandardDeviation &init(int size) {
1620 bin.init(size, params);
1628 * This is a vector of Average Deviations. Intended usage is for tracking
1629 *Average Deviations across a vector like SMT threads.
1631 template <typename T = Counter, class Bin = NoBin>
1632 class VectorAverageDeviation
1633 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
1636 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
1637 typedef typename Detail::DistStor<T>::Params Params;
1640 /** This must be called to initialize some data members and allocate
1641 * approprate storage space for the stat.
1642 *@param size The size of the Vector
1643 * @return The VectorAverageDeviation itself.
1645 VectorAverageDeviation &init(int size) {
1646 bin.init(size, params);
1654 *This is a formula type. When defining it, you can just say:
1655 *Formula foo = manchu + 3 / bar;
1656 *The calculations for Formulas are all done at the end of the simulation, this
1657 *really is just a definition of how to calculate at the end.
1659 class Formula : public Detail::VectorStat
1662 /** The root of the tree which represents the Formula */
1663 Detail::NodePtr root;
1664 friend class Detail::Temp;
1667 Formula() : VectorStat(true) { setInit(); }
1668 Formula(Detail::Temp r) : VectorStat(true) {
1673 const Formula &operator=(Detail::Temp r) {
1674 assert(!root && "Can't change formulas");
1680 const Formula &operator+=(Detail::Temp r) {
1681 using namespace Detail;
1683 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
1690 const rvec_t &val() const { return root->val(); }
1691 result_t total() const { return root->total(); }
1693 size_t size() const {
1697 return root->size();
1702 void dump(std::ostream &stream);
1705 operator+(Detail::Temp l, Detail::Temp r)
1707 using namespace Detail;
1708 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
1712 operator-(Detail::Temp l, Detail::Temp r)
1714 using namespace Detail;
1715 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
1719 operator*(Detail::Temp l, Detail::Temp r)
1721 using namespace Detail;
1722 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
1726 operator/(Detail::Temp l, Detail::Temp r)
1728 using namespace Detail;
1729 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
1733 operator%(Detail::Temp l, Detail::Temp r)
1735 using namespace Detail;
1736 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
1740 operator-(Detail::Temp l)
1742 using namespace Detail;
1743 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
1746 template <typename T>
1750 using namespace Detail;
1751 return NodePtr(new ConstNode<T>(val));
1754 template <typename T>
1758 using namespace Detail;
1759 return NodePtr(new FunctorNode<T>(val));
1762 template <typename T>
1766 using namespace Detail;
1767 return NodePtr(new ScalarNode<T>(val));
1771 sum(Detail::Temp val)
1773 using namespace Detail;
1774 return NodePtr(new SumNode<std::plus<result_t> >(val));
1777 extern bool PrintDescriptions;
1779 } // namespace statistics
1781 #endif // __STATISTICS_HH__