2 * Copyright (c) 2003 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Declaration of Statistics objects.
36 * Generalized N-dimensinal vector
40 * -- these both can use the same function that prints out a
41 * specific set of stats
42 * VectorStandardDeviation totals
45 #ifndef __STATISTICS_HH__
46 #define __STATISTICS_HH__
57 #include "base/refcnt.hh"
58 #include "base/str.hh"
60 #include "sim/host.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 perhaps register it with the stat database. To be
198 * printed a stat must be registered with the database.
199 * @param reg If true, 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. */
291 * Base class for all scalar stats. The class provides an interface to access
292 * the current value of the stat. This class can be used in formulas.
294 class ScalarStat : public Stat
298 * Create and perhaps register this stat with the database.
299 * @param reg If true, register this stat with the database.
301 ScalarStat(bool reg) : Stat(reg) {}
303 * Return the current value of this statistic as a result type.
304 * @return The current value of this statistic.
306 virtual result_t val() const = 0;
308 * Return true if this stat has value zero.
309 * @return True if this stat is zero.
311 virtual bool zero() const;
313 * Print this stat to the provided ostream.
314 * @param stream The output stream.
316 virtual void display(std::ostream &stream) const;
320 VectorDisplay(std::ostream &stream, const std::string &myname,
321 const std::vector<std::string> *mysubnames,
322 const std::string &mydesc,
323 const std::vector<std::string> *mysubdescs,
324 int myprecision, FormatFlags myflags, const rvec_t &vec,
328 * Base class for all vector stats. This class provides interfaces to access
329 * the current values of the stats as well as the totals. This class can be
332 class VectorStat : public Stat
336 * Create and perhaps register this stat with the database.
337 * @param reg If true, register this stat with the database.
339 VectorStat(bool reg) : Stat(reg) {}
341 * Return a vector of result typesd of all the values in the vector.
342 * @return The values of the vector.
344 virtual const rvec_t &val() const = 0;
346 * Return the total of all the entries in the vector.
347 * @return The total of the vector.
349 virtual result_t total() const = 0;
351 * Return true if this stat has value zero.
352 * @return True if this stat is zero.
354 virtual bool zero() const;
356 * Print this stat to the provided ostream.
357 * @param stream The output stream.
359 virtual void display(std::ostream &stream) const;
362 //////////////////////////////////////////////////////////////////////
366 //////////////////////////////////////////////////////////////////////
369 * Templatized storage and interface for a simple scalar stat.
371 template <typename T>
375 /** The paramaters for this storage type, none for a scalar. */
379 /** The statistic value. */
384 * Builds this storage element and calls the base constructor of the
387 StatStor(const Params &) : data(T()) {}
390 * The the stat to the given value.
391 * @param val The new value.
392 * @param p The paramters of this storage type.
394 void set(T val, const Params &p) { data = val; }
396 * Increment the stat by the given value.
397 * @param val The new value.
398 * @param p The paramters of this storage type.
400 void inc(T val, const Params &p) { data += val; }
402 * Decrement the stat by the given value.
403 * @param val The new value.
404 * @param p The paramters of this storage type.
406 void dec(T val, const Params &p) { data -= val; }
408 * Return the value of this stat as a result type.
409 * @param p The parameters of this storage type.
410 * @return The value of this stat.
412 result_t val(const Params &p) const { return (result_t)data; }
414 * Return the value of this stat as its base type.
415 * @param p The params of this storage type.
416 * @return The value of this stat.
418 T value(const Params &p) const { return data; }
422 * Templatized storage and interface to a per-cycle average stat. This keeps
423 * a current count and updates a total (count * cycles) when this count
424 * changes. This allows the quick calculation of a per cycle count of the item
425 * being watched. This is good for keeping track of residencies in structures
426 * among other things.
427 * @todo add lateny to the stat and fix binning.
429 template <typename T>
433 /** The paramaters for this storage type, none for this average. */
437 /** The current count. */
439 /** The total count for all cycles. */
440 mutable result_t total;
441 /** The cycle that current last changed. */
446 * Build and initializes this stat storage.
448 AvgStor(const Params &) : current(T()), total(0), last(0) { }
451 * Set the current count to the one provided, update the total and last
453 * @param val The new count.
454 * @param p The parameters for this storage.
456 void set(T val, const Params &p) {
457 total += current * (curTick - last);
462 * Increment the current count by the provided value, calls set.
463 * @param val The amount to increment.
464 * @param p The parameters for this storage.
466 void inc(T val, const Params &p) { set(current + val, p); }
468 * Deccrement the current count by the provided value, calls set.
469 * @param val The amount to decrement.
470 * @param p The parameters for this storage.
472 void dec(T val, const Params &p) { set(current - val, p); }
474 * Return the current average.
475 * @param p The parameters for this storage.
476 * @return The current average.
478 result_t val(const Params &p) const {
479 total += current * (curTick - last);
481 return (result_t)(total + current) / (result_t)(curTick + 1);
484 * Return the current count.
485 * @param p The parameters for this storage.
486 * @return The current count.
488 T value(const Params &p) const { return current; }
492 * Implementation of a scalar stat. The type of stat is determined by the
493 * Storage template. The storage for this stat is held within the Bin class.
494 * This allows for breaking down statistics across multiple bins easily.
496 template <typename T, template <typename T> class Storage, class Bin>
497 class ScalarBase : public ScalarStat
500 /** Define the type of the storage class. */
501 typedef Storage<T> storage_t;
502 /** Define the params of the storage class. */
503 typedef typename storage_t::Params params_t;
504 /** Define the bin type. */
505 typedef typename Bin::Bin<storage_t> bin_t;
508 /** The bin of this stat. */
510 /** The parameters for this stat. */
515 * Retrieve the storage from the bin.
516 * @return The storage object for this stat.
518 storage_t *data() { return bin.data(params); }
520 * Retrieve a const pointer to the storage from the bin.
521 * @return A const pointer to the storage object for this stat.
523 const storage_t *data() const {
524 return (const_cast<bin_t *>(&bin))->data(params);
529 * Copy constructor, copies are not allowed.
531 ScalarBase(const ScalarBase &stat);
535 const ScalarBase &operator=(const ScalarBase &);
539 * Return the current value of this stat as a result type.
540 * @return The current value.
542 result_t val() const { return data()->val(params); }
544 * Return the current value of this stat as its base type.
545 * @return The current value.
547 T value() const { return data()->value(params); }
551 * Create and initialize this stat, register it with the database.
553 ScalarBase() : ScalarStat(true) {
559 // Common operators for stats
561 * Increment the stat by 1. This calls the associated storage object inc
564 void operator++() { data()->inc(1, params); }
566 * Decrement the stat by 1. This calls the associated storage object dec
569 void operator--() { data()->dec(1, params); }
571 /** Increment the stat by 1. */
572 void operator++(int) { ++*this; }
573 /** Decrement the stat by 1. */
574 void operator--(int) { --*this; }
577 * Set the data value to the given value. This calls the associated storage
578 * object set function.
579 * @param v The new value.
581 template <typename U>
582 void operator=(const U& v) { data()->set(v, params); }
585 * Increment the stat by the given value. This calls the associated
586 * storage object inc function.
587 * @param v The value to add.
589 template <typename U>
590 void operator+=(const U& v) { data()->inc(v, params); }
593 * Decrement the stat by the given value. This calls the associated
594 * storage object dec function.
595 * @param v The value to substract.
597 template <typename U>
598 void operator-=(const U& v) { data()->dec(v, params); }
601 * Return the number of elements, always 1 for a scalar.
604 virtual size_t size() const { return 1; }
607 //////////////////////////////////////////////////////////////////////
611 //////////////////////////////////////////////////////////////////////
612 template <typename T, template <typename T> class Storage, class Bin>
616 * Implementation of a vector of stats. The type of stat is determined by the
617 * Storage class. @sa ScalarBase
619 template <typename T, template <typename T> class Storage, class Bin>
620 class VectorBase : public VectorStat
623 /** Define the type of the storage class. */
624 typedef Storage<T> storage_t;
625 /** Define the params of the storage class. */
626 typedef typename storage_t::Params params_t;
627 /** Define the bin type. */
628 typedef typename Bin::VectorBin<storage_t> bin_t;
631 /** Local storage for the entry values, used for printing. */
635 /** The bin of this stat. */
637 /** The parameters for this stat. */
642 * Retrieve the storage from the bin for the given index.
643 * @param index The vector index to access.
644 * @return The storage object at the given index.
646 storage_t *data(int index) { return bin.data(index, params); }
648 * Retrieve a const pointer to the storage from the bin
649 * for the given index.
650 * @param index The vector index to access.
651 * @return A const pointer to the storage object at the given index.
653 const storage_t *data(int index) const {
654 return (const_cast<bin_t *>(&bin))->data(index, params);
658 // Copying stats is not allowed
659 /** Copying stats isn't allowed. */
660 VectorBase(const VectorBase &stat);
661 /** Copying stats isn't allowed. */
662 const VectorBase &operator=(const VectorBase &);
666 * Copy the values to a local vector and return a reference to it.
667 * @return A reference to a vector of the stat values.
669 const rvec_t &val() const {
673 vec = new rvec_t(size());
675 for (int i = 0; i < size(); ++i)
676 (*vec)[i] = data(i)->val(params);
682 * Return a total of all entries in this vector.
683 * @return The total of all vector entries.
685 result_t total() const {
686 result_t total = 0.0;
687 for (int i = 0; i < size(); ++i)
688 total += data(i)->val(params);
694 * Create this vector and register it with the database.
696 VectorBase() : VectorStat(true), vec(NULL) {}
700 ~VectorBase() { if (vec) delete vec; }
703 * Set this vector to have the given size.
704 * @param size The new size.
705 * @return A reference to this stat.
707 VectorBase &init(size_t size) {
708 bin.init(size, params);
714 friend class ScalarProxy<T, Storage, Bin>;
717 * Return a reference (ScalarProxy) to the stat at the given index.
718 * @param index The vector index to access.
719 * @return A reference of the stat.
721 ScalarProxy<T, Storage, Bin> operator[](int index);
724 * Return the number of elements in this vector.
725 * @return The size of the vector.
727 virtual size_t size() const { return bin.size(); }
731 * A proxy class to access the stat at a given index in a VectorBase stat.
732 * Behaves like a ScalarBase.
734 template <typename T, template <typename T> class Storage, class Bin>
735 class ScalarProxy : public ScalarStat
738 /** Define the type of the storage class. */
739 typedef Storage<T> storage_t;
740 /** Define the params of the storage class. */
741 typedef typename storage_t::Params params_t;
742 /** Define the bin type. */
743 typedef typename Bin::VectorBin<storage_t> bin_t;
746 /** Pointer to the bin in the parent VectorBase. */
748 /** Pointer to the params in the parent VectorBase. */
750 /** The index to access in the parent VectorBase. */
755 * Retrieve the storage from the bin.
756 * @return The storage from the bin for this stat.
758 storage_t *data() { return bin->data(index, *params); }
760 * Retrieve a const pointer to the storage from the bin.
761 * @return A const pointer to the storage for this stat.
763 const storage_t *data() const { return bin->data(index, *params); }
767 * Return the current value of this statas a result type.
768 * @return The current value.
770 result_t val() const { return data()->val(*params); }
772 * Return the current value of this stat as its base type.
773 * @return The current value.
775 T value() const { return data()->value(*params); }
779 * Create and initialize this proxy, do not register it with the database.
780 * @param b The bin to use.
781 * @param p The params to use.
782 * @param i The index to access.
784 ScalarProxy(bin_t &b, params_t &p, int i)
785 : ScalarStat(false), bin(&b), params(&p), index(i) {}
787 * Create a copy of the provided ScalarProxy.
788 * @param sp The proxy to copy.
790 ScalarProxy(const ScalarProxy &sp)
791 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
793 * Set this proxy equal to the provided one.
794 * @param sp The proxy to copy.
795 * @return A reference to this proxy.
797 const ScalarProxy &operator=(const ScalarProxy &sp) {
805 // Common operators for stats
807 * Increment the stat by 1. This calls the associated storage object inc
810 void operator++() { data()->inc(1, *params); }
812 * Decrement the stat by 1. This calls the associated storage object dec
815 void operator--() { data()->dec(1, *params); }
817 /** Increment the stat by 1. */
818 void operator++(int) { ++*this; }
819 /** Decrement the stat by 1. */
820 void operator--(int) { --*this; }
823 * Set the data value to the given value. This calls the associated storage
824 * object set function.
825 * @param v The new value.
827 template <typename U>
828 void operator=(const U& v) { data()->set(v, *params); }
831 * Increment the stat by the given value. This calls the associated
832 * storage object inc function.
833 * @param v The value to add.
835 template <typename U>
836 void operator+=(const U& v) { data()->inc(v, *params); }
839 * Decrement the stat by the given value. This calls the associated
840 * storage object dec function.
841 * @param v The value to substract.
843 template <typename U>
844 void operator-=(const U& v) { data()->dec(v, *params); }
847 * Return the number of elements, always 1 for a scalar.
850 virtual size_t size() const { return 1; }
853 template <typename T, template <typename T> class Storage, class Bin>
854 inline ScalarProxy<T, Storage, Bin>
855 VectorBase<T, Storage, Bin>::operator[](int index)
857 assert (index >= 0 && index < size());
858 return ScalarProxy<T, Storage, Bin>(bin, params, index);
861 template <typename T, template <typename T> class Storage, class Bin>
864 template <typename T, template <typename T> class Storage, class Bin>
865 class Vector2dBase : public Stat
868 typedef Storage<T> storage_t;
869 typedef typename storage_t::Params params_t;
870 typedef typename Bin::VectorBin<storage_t> bin_t;
877 std::vector<std::string> *y_subnames;
880 storage_t *data(int index) { return bin.data(index, params); }
881 const storage_t *data(int index) const {
882 return (const_cast<bin_t *>(&bin))->data(index, params);
886 // Copying stats is not allowed
887 Vector2dBase(const Vector2dBase &stat);
888 const Vector2dBase &operator=(const Vector2dBase &);
891 Vector2dBase() : Stat(true) {}
894 Vector2dBase &init(size_t _x, size_t _y) {
897 bin.init(x * y, params);
899 y_subnames = new std::vector<std::string>(y);
905 * @warning This makes the assumption that if you're gonna subnames a 2d
906 * vector, you're subnaming across all y
908 Vector2dBase &ysubnames(const char **names)
910 for (int i=0; i < y; ++i) {
911 (*y_subnames)[i] = names[i];
915 Vector2dBase &ysubname(int index, const std::string subname)
917 (*y_subnames)[i] = subname.c_str();
920 std::string ysubname(int i) const { return (*y_subnames)[i]; }
922 friend class VectorProxy<T, Storage, Bin>;
923 VectorProxy<T, Storage, Bin> operator[](int index);
925 virtual size_t size() const { return bin.size(); }
926 virtual bool zero() const { return data(0)->value(params) == 0.0; }
929 display(std::ostream &out) const
931 bool have_subname = false;
932 for (int i = 0; i < x; ++i) {
933 if (!mysubname(i).empty())
938 result_t super_total = 0.0;
939 for (int i = 0; i < x; ++i) {
942 subname = mysubname(i);
946 subname = to_string(i);
951 result_t total = 0.0;
952 for (int j = 0; j < y; ++j) {
953 vec[j] = data(iy + j)->val(params);
954 tot_vec[j] += vec[j];
956 super_total += vec[j];
960 if (mysubdesc(i).empty()) {
966 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
967 myprecision(), myflags(), vec, total);
970 if ((myflags() & ::Statistics::total) && (x > 1)) {
971 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
972 myprecision(), myflags(), tot_vec, super_total);
978 template <typename T, template <typename T> class Storage, class Bin>
979 class VectorProxy : public VectorStat
982 typedef Storage<T> storage_t;
983 typedef typename storage_t::Params params_t;
984 typedef typename Bin::VectorBin<storage_t> bin_t;
995 storage_t *data(int index) {
997 return bin->data(offset + index, *params);
1000 const storage_t *data(int index) const {
1001 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
1005 const rvec_t &val() const {
1007 vec->resize(size());
1009 vec = new rvec_t(size());
1011 for (int i = 0; i < size(); ++i)
1012 (*vec)[i] = data(i)->val(*params);
1017 result_t total() const {
1018 result_t total = 0.0;
1019 for (int i = 0; i < size(); ++i)
1020 total += data(i)->val(*params);
1025 VectorProxy(bin_t &b, params_t &p, int o, int l)
1026 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
1028 VectorProxy(const VectorProxy &sp)
1029 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
1030 len(sp.len), vec(NULL)
1037 const VectorProxy &operator=(const VectorProxy &sp) {
1048 virtual size_t size() const { return len; }
1050 ScalarProxy<T, Storage, Bin> operator[](int index) {
1051 assert (index >= 0 && index < size());
1052 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
1056 template <typename T, template <typename T> class Storage, class Bin>
1057 inline VectorProxy<T, Storage, Bin>
1058 Vector2dBase<T, Storage, Bin>::operator[](int index)
1060 int offset = index * y;
1061 assert (index >= 0 && offset < size());
1062 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
1065 //////////////////////////////////////////////////////////////////////
1067 // Non formula statistics
1069 //////////////////////////////////////////////////////////////////////
1071 void DistDisplay(std::ostream &stream, const std::string &name,
1072 const std::string &desc, int precision, FormatFlags flags,
1073 result_t min_val, result_t max_val,
1074 result_t underflow, result_t overflow,
1075 const rvec_t &vec, int min, int max, int bucket_size,
1078 * Templatized storage and interface for a distrbution stat.
1080 template <typename T>
1084 /** The parameters for a distribution stat. */
1087 /** The minimum value to track. */
1089 /** The maximum value to track. */
1091 /** The number of entries in each bucket. */
1093 /** The number of buckets. Equal to (max-min)/bucket_size. */
1098 /** The smallest value sampled. */
1100 /** The largest value sampled. */
1102 /** The number of values sampled less than min. */
1104 /** The number of values sampled more than max. */
1106 /** Counter for each bucket. */
1111 * Construct this storage with the supplied params.
1112 * @param params The parameters.
1114 DistStor(const Params ¶ms)
1115 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1119 * Add a value to the distribution for the given number of times.
1120 * @param val The value to add.
1121 * @param number The number of times to add the value.
1122 * @param params The paramters of the distribution.
1124 void sample(T val, int number, const Params ¶ms) {
1125 if (val < params.min)
1126 underflow += number;
1127 else if (val > params.max)
1130 int index = (val - params.min) / params.bucket_size;
1131 assert(index < size(params));
1132 vec[index] += number;
1143 * Return the number of buckets in this distribution.
1144 * @return the number of buckets.
1145 * @todo Is it faster to return the size from the parameters?
1147 size_t size(const Params &) const { return vec.size(); }
1150 * Returns true if any calls to sample have been made.
1151 * @param params The paramters of the distribution.
1152 * @return True if any values have been sampled.
1154 bool zero(const Params ¶ms) const {
1155 if (underflow != 0 || overflow != 0)
1158 int s = size(params);
1159 for (int i = 0; i < s; i++)
1167 * Print this distribution and the given print data to the given ostream.
1168 * @param stream The output stream.
1169 * @param name The name of this stat (from StatData).
1170 * @param desc The description of this stat (from StatData).
1171 * @param precision The print precision (from StatData).
1172 * @param flags The format flags (from StatData).
1173 * @param params The paramters of this distribution.
1175 void display(std::ostream &stream, const std::string &name,
1176 const std::string &desc, int precision, FormatFlags flags,
1177 const Params ¶ms) const {
1179 #ifdef STAT_DISPLAY_COMPAT
1180 result_t min = params.min;
1182 result_t min = (min_val == INT_MAX) ? params.min : min_val;
1184 result_t max = (max_val == INT_MIN) ? 0 : max_val;
1186 rvec_t rvec(params.size);
1187 for (int i = 0; i < params.size; ++i)
1190 DistDisplay(stream, name, desc, precision, flags,
1191 (result_t)min, (result_t)max,
1192 (result_t)underflow, (result_t)overflow,
1193 rvec, params.min, params.max, params.bucket_size,
1198 void FancyDisplay(std::ostream &stream, const std::string &name,
1199 const std::string &desc, int precision, FormatFlags flags,
1200 result_t mean, result_t variance);
1203 * Templatized storage and interface for a distribution that calculates mean
1206 template <typename T>
1211 * No paramters for this storage.
1216 /** The current sum. */
1218 /** The sum of squares. */
1220 /** The total number of samples. */
1225 * Create and initialize this storage.
1227 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
1230 * Add a value the given number of times to this running average.
1231 * Update the running sum and sum of squares, increment the number of
1232 * values seen by the given number.
1233 * @param val The value to add.
1234 * @param number The number of times to add the value.
1235 * @param p The parameters of this stat.
1237 void sample(T val, int number, const Params &p) {
1238 T value = val * number;
1240 squares += value * value;
1245 * Print this distribution and the given print data to the given ostream.
1246 * @param stream The output stream.
1247 * @param name The name of this stat (from StatData).
1248 * @param desc The description of this stat (from StatData).
1249 * @param precision The print precision (from StatData).
1250 * @param flags The format flags (from StatData).
1251 * @param params The paramters of this distribution.
1253 void display(std::ostream &stream, const std::string &name,
1254 const std::string &desc, int precision, FormatFlags flags,
1255 const Params ¶ms) const {
1257 result_t mean = NAN;
1258 result_t variance = NAN;
1261 result_t fsum = sum;
1262 result_t fsq = squares;
1263 result_t ftot = total;
1266 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
1269 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1273 * Return the number of entries in this stat, 1
1276 size_t size(const Params &) const { return 1; }
1278 * Return true if no samples have been added.
1279 * @return True if no samples have been added.
1281 bool zero(const Params &) const { return total == 0; }
1285 * Templatized storage for distribution that calculates per cycle mean and
1288 template <typename T>
1292 /** No parameters for this storage. */
1296 /** Current total. */
1298 /** Current sum of squares. */
1303 * Create and initialize this storage.
1305 AvgFancy(const Params &) : sum(0), squares(0) {}
1308 * Add a value to the distribution for the given number of times.
1309 * Update the running sum and sum of squares.
1310 * @param val The value to add.
1311 * @param number The number of times to add the value.
1312 * @param p The paramters of the distribution.
1314 void sample(T val, int number, const Params& p) {
1315 T value = val * number;
1317 squares += value * value;
1321 * Print this distribution and the given print data to the given ostream.
1322 * @param stream The output stream.
1323 * @param name The name of this stat (from StatData).
1324 * @param desc The description of this stat (from StatData).
1325 * @param precision The print precision (from StatData).
1326 * @param flags The format flags (from StatData).
1327 * @param params The paramters of this distribution.
1329 void display(std::ostream &stream, const std::string &name,
1330 const std::string &desc, int precision, FormatFlags flags,
1331 const Params ¶ms) const {
1332 result_t mean = sum / curTick;
1333 result_t variance = (squares - sum * sum) / curTick;
1335 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1339 * Return the number of entries, in this case 1.
1342 size_t size(const Params ¶ms) const { return 1; }
1344 * Return true if no samples have been added.
1345 * @return True if the sum is zero.
1347 bool zero(const Params ¶ms) const { return sum == 0; }
1351 * Implementation of a distribution stat. The type of distribution is
1352 * determined by the Storage template. @sa ScalarBase
1354 template <typename T, template <typename T> class Storage, class Bin>
1355 class DistBase : public Stat
1358 /** Define the type of the storage class. */
1359 typedef Storage<T> storage_t;
1360 /** Define the params of the storage class. */
1361 typedef typename storage_t::Params params_t;
1362 /** Define the bin type. */
1363 typedef typename Bin::Bin<storage_t> bin_t;
1366 /** The bin of this stat. */
1368 /** The parameters for this stat. */
1373 * Retrieve the storage from the bin.
1374 * @return The storage object for this stat.
1376 storage_t *data() { return bin.data(params); }
1378 * Retrieve a const pointer to the storage from the bin.
1379 * @return A const pointer to the storage object for this stat.
1381 const storage_t *data() const {
1382 return (const_cast<bin_t *>(&bin))->data(params);
1386 // Copying stats is not allowed
1387 /** Copies are not allowed. */
1388 DistBase(const DistBase &stat);
1389 /** Copies are not allowed. */
1390 const DistBase &operator=(const DistBase &);
1394 * Create this distrubition and register it with the database.
1396 DistBase() : Stat(true) { }
1403 * Add a value to the distribtion n times. Calls sample on the storage
1405 * @param v The value to add.
1406 * @param n The number of times to add it, defaults to 1.
1408 template <typename U>
1409 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1412 * Return the number of entries in this stat.
1413 * @return The number of entries.
1415 virtual size_t size() const { return data()->size(params); }
1417 * Return true if no samples have been added.
1418 * @return True if there haven't been any samples.
1420 virtual bool zero() const { return data()->zero(params); }
1422 * Print this distribution to the given ostream.
1423 * @param stream The output stream.
1425 virtual void display(std::ostream &stream) const {
1426 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
1431 template <typename T, template <typename T> class Storage, class Bin>
1432 class VectorDistProxy;
1434 template <typename T, template <typename T> class Storage, class Bin>
1435 class VectorDistBase : public Stat
1438 typedef Storage<T> storage_t;
1439 typedef typename storage_t::Params params_t;
1440 typedef typename Bin::VectorBin<storage_t> bin_t;
1447 storage_t *data(int index) { return bin.data(index, params); }
1448 const storage_t *data(int index) const {
1449 return (const_cast<bin_t *>(&bin))->data(index, params);
1453 // Copying stats is not allowed
1454 VectorDistBase(const VectorDistBase &stat);
1455 const VectorDistBase &operator=(const VectorDistBase &);
1458 VectorDistBase() : Stat(true) { }
1459 ~VectorDistBase() { }
1461 friend class VectorDistProxy<T, Storage, Bin>;
1462 VectorDistProxy<T, Storage, Bin> operator[](int index);
1463 const VectorDistProxy<T, Storage, Bin> operator[](int index) const;
1465 virtual size_t size() const { return bin.size(); }
1466 virtual bool zero() const { return false; }
1467 virtual void display(std::ostream &stream) const;
1470 template <typename T, template <typename T> class Storage, class Bin>
1471 class VectorDistProxy : public Stat
1474 typedef Storage<T> storage_t;
1475 typedef typename storage_t::Params params_t;
1476 typedef typename Bin::Bin<storage_t> bin_t;
1477 typedef VectorDistBase<T, Storage, Bin> base_t;
1482 const base_t *cstat;
1487 storage_t *data() { return stat->data(index); }
1488 const storage_t *data() const { return cstat->data(index); }
1491 VectorDistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1492 : Stat(false), cstat(&s), index(i) {}
1493 VectorDistProxy(const VectorDistProxy &sp)
1494 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1495 const VectorDistProxy &operator=(const VectorDistProxy &sp) {
1496 cstat = sp.cstat; index = sp.index; return *this;
1500 template <typename U>
1501 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1503 virtual size_t size() const { return 1; }
1504 virtual bool zero() const {
1505 return data()->zero(cstat->params);
1507 virtual void display(std::ostream &stream) const {
1508 std::stringstream name, desc;
1510 if (!(cstat->mysubname(index).empty())) {
1511 name << cstat->myname() << cstat->mysubname(index);
1513 name << cstat->myname() << "_" << index;
1515 if (!(cstat->mysubdesc(index).empty())) {
1516 desc << cstat->mysubdesc(index);
1518 desc << cstat->mydesc();
1521 data()->display(stream, name.str(), desc.str(),
1522 cstat->myprecision(), cstat->myflags(), cstat->params);
1526 template <typename T, template <typename T> class Storage, class Bin>
1527 inline VectorDistProxy<T, Storage, Bin>
1528 VectorDistBase<T, Storage, Bin>::operator[](int index)
1530 assert (index >= 0 && index < size());
1531 return VectorDistProxy<T, Storage, Bin>(*this, index);
1534 template <typename T, template <typename T> class Storage, class Bin>
1535 inline const VectorDistProxy<T, Storage, Bin>
1536 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1538 assert (index >= 0 && index < size());
1539 return VectorDistProxy<T, Storage, Bin>(*this, index);
1543 * @todo Need a way to print Distribution totals across the Vector
1545 template <typename T, template <typename T> class Storage, class Bin>
1547 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1549 for (int i = 0; i < size(); ++i) {
1550 VectorDistProxy<T, Storage, Bin> proxy(*this, i);
1551 proxy.display(stream);
1557 VectorDistBase<T, Storage, Bin>::total(int index) const
1560 for (int i=0; i < x_size(); ++i) {
1561 total += data(i)->val(*params);
1566 //////////////////////////////////////////////////////////////////////
1570 //////////////////////////////////////////////////////////////////////
1573 * Base class for formula statistic node. These nodes are used to build a tree
1574 * that represents the formula.
1576 class Node : public RefCounted
1580 * Return the number of nodes in the subtree starting at this node.
1581 * @return the number of nodes in this subtree.
1583 virtual size_t size() const = 0;
1585 * Return the result vector of this subtree.
1586 * @return The result vector of this subtree.
1588 virtual const rvec_t &val() const = 0;
1590 * Return the total of the result vector.
1591 * @return The total of the result vector.
1593 virtual result_t total() const = 0;
1596 /** Reference counting pointer to a function Node. */
1597 typedef RefCountingPtr<Node> NodePtr;
1599 class ScalarStatNode : public Node
1602 const ScalarStat &stat;
1603 mutable rvec_t result;
1606 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1607 const rvec_t &val() const { result[0] = stat.val(); return result; }
1608 virtual result_t total() const { return stat.val(); };
1610 virtual size_t size() const { return 1; }
1613 template <typename T, template <typename T> class Storage, class Bin>
1614 class ScalarProxyNode : public Node
1617 const ScalarProxy<T, Storage, Bin> proxy;
1618 mutable rvec_t result;
1621 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1622 : proxy(p), result(1) { }
1623 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1624 virtual result_t total() const { return proxy.val(); };
1626 virtual size_t size() const { return 1; }
1629 class VectorStatNode : public Node
1632 const VectorStat &stat;
1635 VectorStatNode(const VectorStat &s) : stat(s) {}
1636 const rvec_t &val() const { return stat.val(); }
1637 virtual result_t total() const { return stat.total(); };
1639 virtual size_t size() const { return stat.size(); }
1642 template <typename T>
1643 class ConstNode : public Node
1649 ConstNode(T s) : data(1, (result_t)s) {}
1650 const rvec_t &val() const { return data; }
1651 virtual result_t total() const { return data[0]; };
1653 virtual size_t size() const { return 1; }
1656 template <typename T>
1657 class FunctorNode : public Node
1661 mutable rvec_t result;
1664 FunctorNode(T &f) : functor(f) { result.resize(1); }
1665 const rvec_t &val() const {
1666 result[0] = (result_t)functor();
1669 virtual result_t total() const { return (result_t)functor(); };
1671 virtual size_t size() const { return 1; }
1674 template <typename T>
1675 class ScalarNode : public Node
1679 mutable rvec_t result;
1682 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1683 const rvec_t &val() const {
1684 result[0] = (result_t)scalar;
1687 virtual result_t total() const { return (result_t)scalar; };
1689 virtual size_t size() const { return 1; }
1693 class UnaryNode : public Node
1697 mutable rvec_t result;
1700 UnaryNode(NodePtr p) : l(p) {}
1702 const rvec_t &val() const {
1703 const rvec_t &lvec = l->val();
1704 int size = lvec.size();
1708 result.resize(size);
1710 for (int i = 0; i < size; ++i)
1711 result[i] = op(lvec[i]);
1716 result_t total() const {
1718 return op(l->total());
1721 virtual size_t size() const { return l->size(); }
1725 class BinaryNode : public Node
1730 mutable rvec_t result;
1733 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1735 const rvec_t &val() const {
1737 const rvec_t &lvec = l->val();
1738 const rvec_t &rvec = r->val();
1740 assert(lvec.size() > 0 && rvec.size() > 0);
1742 if (lvec.size() == 1 && rvec.size() == 1) {
1744 result[0] = op(lvec[0], rvec[0]);
1745 } else if (lvec.size() == 1) {
1746 int size = rvec.size();
1747 result.resize(size);
1748 for (int i = 0; i < size; ++i)
1749 result[i] = op(lvec[0], rvec[i]);
1750 } else if (rvec.size() == 1) {
1751 int size = lvec.size();
1752 result.resize(size);
1753 for (int i = 0; i < size; ++i)
1754 result[i] = op(lvec[i], rvec[0]);
1755 } else if (rvec.size() == lvec.size()) {
1756 int size = rvec.size();
1757 result.resize(size);
1758 for (int i = 0; i < size; ++i)
1759 result[i] = op(lvec[i], rvec[i]);
1765 result_t total() const {
1767 return op(l->total(), r->total());
1770 virtual size_t size() const {
1778 assert(ls == rs && "Node vector sizes are not equal");
1785 class SumNode : public Node
1789 mutable rvec_t result;
1792 SumNode(NodePtr p) : l(p), result(1) {}
1794 const rvec_t &val() const {
1795 const rvec_t &lvec = l->val();
1796 int size = lvec.size();
1802 for (int i = 0; i < size; ++i)
1803 result[0] = op(result[0], lvec[i]);
1808 result_t total() const {
1809 const rvec_t &lvec = l->val();
1810 int size = lvec.size();
1813 result_t result = 0.0;
1816 for (int i = 0; i < size; ++i)
1817 result = op(result, lvec[i]);
1822 virtual size_t size() const { return 1; }
1826 * Helper class to construct formula node trees.
1832 * Pointer to a Node object.
1838 * Copy the given pointer to this class.
1839 * @param n A pointer to a Node object to copy.
1841 Temp(NodePtr n) : node(n) {}
1843 * Create a new ScalarStatNode.
1844 * @param s The ScalarStat to place in a node.
1846 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1848 * Create a new ScalarProxyNode.
1849 * @param p The ScalarProxy to place in a node.
1851 template <typename T, template <typename T> class Storage, class Bin>
1852 Temp(const ScalarProxy<T, Storage, Bin> &p)
1853 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1855 * Create a new VectorStatNode.
1856 * @param s The VectorStat to place in a node.
1858 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1861 * Create a ConstNode
1862 * @param value The value of the const node.
1864 Temp(signed char value) : node(new ConstNode<signed char>(value)) {}
1866 * Create a ConstNode
1867 * @param value The value of the const node.
1869 Temp(unsigned char value) : node(new ConstNode<unsigned char>(value)) {}
1871 * Create a ConstNode
1872 * @param value The value of the const node.
1874 Temp(signed short value) : node(new ConstNode<signed short>(value)) {}
1876 * Create a ConstNode
1877 * @param value The value of the const node.
1879 Temp(unsigned short value) : node(new ConstNode<unsigned short>(value)) {}
1881 * Create a ConstNode
1882 * @param value The value of the const node.
1884 Temp(signed int value) : node(new ConstNode<signed int>(value)) {}
1886 * Create a ConstNode
1887 * @param value The value of the const node.
1889 Temp(unsigned int value) : node(new ConstNode<unsigned int>(value)) {}
1891 * Create a ConstNode
1892 * @param value The value of the const node.
1894 Temp(signed long value) : node(new ConstNode<signed long>(value)) {}
1896 * Create a ConstNode
1897 * @param value The value of the const node.
1899 Temp(unsigned long value) : node(new ConstNode<unsigned long>(value)) {}
1901 * Create a ConstNode
1902 * @param value The value of the const node.
1904 Temp(signed long long value)
1905 : node(new ConstNode<signed long long>(value)) {}
1907 * Create a ConstNode
1908 * @param value The value of the const node.
1910 Temp(unsigned long long value)
1911 : node(new ConstNode<unsigned long long>(value)) {}
1913 * Create a ConstNode
1914 * @param value The value of the const node.
1916 Temp(float value) : node(new ConstNode<float>(value)) {}
1918 * Create a ConstNode
1919 * @param value The value of the const node.
1921 Temp(double value) : node(new ConstNode<double>(value)) {}
1924 * Return the node pointer.
1925 * @return the node pointer.
1927 operator NodePtr() { return node;}
1931 //////////////////////////////////////////////////////////////////////
1933 // Binning Interface
1935 //////////////////////////////////////////////////////////////////////
1944 off_t size() const { return memsize; }
1948 BinBase(size_t size);
1952 } // namespace Detail
1954 template <class BinType>
1955 struct StatBin : public Detail::BinBase
1957 static StatBin *&curBin() {
1958 static StatBin *current = NULL;
1962 static void setCurBin(StatBin *bin) { curBin() = bin; }
1963 static StatBin *current() { assert(curBin()); return curBin(); }
1965 static off_t &offset() {
1966 static off_t offset = 0;
1970 static off_t new_offset(size_t size) {
1971 size_t mask = sizeof(u_int64_t) - 1;
1972 off_t off = offset();
1974 // That one is for the last trailing flags byte.
1975 offset() += (size + 1 + mask) & ~mask;
1980 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1982 char *memory(off_t off) {
1983 assert(offset() <= size());
1984 return Detail::BinBase::memory() + off;
1987 static void activate(StatBin &bin) { setCurBin(&bin); }
1995 BinBase() : offset(-1) {}
1996 void allocate(size_t size) {
1997 offset = new_offset(size);
2000 assert(offset != -1);
2001 return current()->memory(offset);
2005 template <class Storage>
2006 class Bin : public BinBase
2009 typedef typename Storage::Params Params;
2012 Bin() { allocate(sizeof(Storage)); }
2013 bool initialized() const { return true; }
2014 void init(const Params ¶ms) { }
2016 int size() const { return 1; }
2018 Storage *data(const Params ¶ms) {
2019 assert(initialized());
2020 char *ptr = access();
2021 char *flags = ptr + sizeof(Storage);
2022 if (!(*flags & 0x1)) {
2024 new (ptr) Storage(params);
2026 return reinterpret_cast<Storage *>(ptr);
2030 template <class Storage>
2031 class VectorBin : public BinBase
2034 typedef typename Storage::Params Params;
2040 VectorBin() : _size(0) {}
2042 bool initialized() const { return _size > 0; }
2043 void init(int s, const Params ¶ms) {
2044 assert(!initialized());
2047 allocate(_size * sizeof(Storage));
2050 int size() const { return _size; }
2052 Storage *data(int index, const Params ¶ms) {
2053 assert(initialized());
2054 assert(index >= 0 && index < size());
2055 char *ptr = access();
2056 char *flags = ptr + size() * sizeof(Storage);
2057 if (!(*flags & 0x1)) {
2059 for (int i = 0; i < size(); ++i)
2060 new (ptr + i * sizeof(Storage)) Storage(params);
2062 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2067 class MainBinType {};
2068 typedef StatBin<MainBinType> MainBin;
2072 template <class Storage>
2076 typedef typename Storage::Params Params;
2079 char ptr[sizeof(Storage)];
2082 bool initialized() const { return true; }
2083 void init(const Params ¶ms) {
2084 new (ptr) Storage(params);
2086 int size() const{ return 1; }
2087 Storage *data(const Params ¶ms) {
2088 assert(initialized());
2089 return reinterpret_cast<Storage *>(ptr);
2093 template <class Storage>
2097 typedef typename Storage::Params Params;
2104 VectorBin() : ptr(NULL) { }
2109 bool initialized() const { return ptr != NULL; }
2110 void init(int s, const Params ¶ms) {
2111 assert(s > 0 && "size must be positive!");
2112 assert(!initialized());
2114 ptr = new char[_size * sizeof(Storage)];
2115 for (int i = 0; i < _size; ++i)
2116 new (ptr + i * sizeof(Storage)) Storage(params);
2119 int size() const { return _size; }
2121 Storage *data(int index, const Params ¶ms) {
2122 assert(initialized());
2123 assert(index >= 0 && index < size());
2124 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2129 //////////////////////////////////////////////////////////////////////
2131 // Visible Statistics Types
2133 //////////////////////////////////////////////////////////////////////
2135 * @defgroup VisibleStats "Statistic Types"
2136 * These are the statistics that are used in the simulator. By default these
2137 * store counters and don't use binning, but are templatized to accept any type
2138 * and any Bin class.
2143 * This is a simple scalar statistic, like a counter.
2144 * @sa Stat, ScalarBase, StatStor
2146 template <typename T = Counter, class Bin = NoBin>
2147 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
2150 /** The base implementation. */
2151 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
2154 * Sets the stat equal to the given value. Calls the base implementation
2156 * @param v The new value.
2158 template <typename U>
2159 void operator=(const U& v) { Base::operator=(v); }
2163 * A stat that calculates the per cycle average of a value.
2164 * @sa Stat, ScalarBase, AvgStor
2166 template <typename T = Counter, class Bin = NoBin>
2167 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
2170 /** The base implementation. */
2171 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
2174 * Sets the stat equal to the given value. Calls the base implementation
2176 * @param v The new value.
2178 template <typename U>
2179 void operator=(const U& v) { Base::operator=(v); }
2183 * A vector of scalar stats.
2184 * @sa Stat, VectorBase, StatStor
2186 template <typename T = Counter, class Bin = NoBin>
2187 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
2191 * A vector of Average stats.
2192 * @sa Stat, VectorBase, AvgStor
2194 template <typename T = Counter, class Bin = NoBin>
2195 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
2199 * A 2-Dimensional vecto of scalar stats.
2200 * @sa Stat, Vector2dBase, StatStor
2202 template <typename T = Counter, class Bin = NoBin>
2203 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
2207 * A simple distribution stat.
2208 * @sa Stat, DistBase, DistStor
2210 template <typename T = Counter, class Bin = NoBin>
2211 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
2214 /** Base implementation. */
2215 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2216 /** The Parameter type. */
2217 typedef typename Detail::DistStor<T>::Params Params;
2221 * Set the parameters of this distribution. @sa DistStor::Params
2222 * @param min The minimum value of the distribution.
2223 * @param max The maximum value of the distribution.
2224 * @param bkt The number of values in each bucket.
2225 * @return A reference to this distribution.
2227 Distribution &init(T min, T max, int bkt) {
2230 params.bucket_size = bkt;
2231 params.size = (max - min) / bkt + 1;
2240 * Calculates the mean and variance of all the samples.
2241 * @sa Stat, DistBase, FancyStor
2243 template <typename T = Counter, class Bin = NoBin>
2244 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
2247 /** The base implementation */
2248 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2249 /** The parameter type. */
2250 typedef typename Detail::DistStor<T>::Params Params;
2254 * Construct and initialize this distribution.
2256 StandardDeviation() {
2263 * Calculates the per cycle mean and variance of the samples.
2264 * @sa Stat, DistBase, AvgFancy
2266 template <typename T = Counter, class Bin = NoBin>
2267 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
2270 /** The base implementation */
2271 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2272 /** The parameter type. */
2273 typedef typename Detail::DistStor<T>::Params Params;
2277 * Construct and initialize this distribution.
2279 AverageDeviation() {
2286 * A vector of distributions.
2287 * @sa Stat, VectorDistBase, DistStor
2289 template <typename T = Counter, class Bin = NoBin>
2290 class VectorDistribution
2291 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
2294 /** The base implementation */
2295 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
2296 /** The parameter type. */
2297 typedef typename Detail::DistStor<T>::Params Params;
2301 * Initialize storage and parameters for this distribution.
2302 * @param size The size of the vector (the number of distributions).
2303 * @param min The minimum value of the distribution.
2304 * @param max The maximum value of the distribution.
2305 * @param bkt The number of values in each bucket.
2306 * @return A reference to this distribution.
2308 VectorDistribution &init(int size, T min, T max, int bkt) {
2311 params.bucket_size = bkt;
2312 params.size = (max - min) / bkt + 1;
2313 bin.init(size, params);
2321 * This is a vector of StandardDeviation stats.
2322 * @sa Stat, VectorDistBase, FancyStor
2324 template <typename T = Counter, class Bin = NoBin>
2325 class VectorStandardDeviation
2326 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
2329 /** The base implementation */
2330 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
2331 /** The parameter type. */
2332 typedef typename Detail::DistStor<T>::Params Params;
2336 * Initialize storage for this distribution.
2337 * @param size The size of the vector.
2338 * @return A reference to this distribution.
2340 VectorStandardDeviation &init(int size) {
2341 bin.init(size, params);
2349 * This is a vector of AverageDeviation stats.
2350 * @sa Stat, VectorDistBase, AvgFancy
2352 template <typename T = Counter, class Bin = NoBin>
2353 class VectorAverageDeviation
2354 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
2357 /** The base implementation */
2358 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
2359 /** The parameter type. */
2360 typedef typename Detail::DistStor<T>::Params Params;
2364 * Initialize storage for this distribution.
2365 * @param size The size of the vector.
2366 * @return A reference to this distribution.
2368 VectorAverageDeviation &init(int size) {
2369 bin.init(size, params);
2377 * A formula for statistics that is calculated when printed. A formula is
2378 * stored as a tree of Nodes that represent the equation to calculate.
2379 * @sa Stat, ScalarStat, VectorStat, Node, Detail::Temp
2381 class Formula : public Detail::VectorStat
2384 /** The root of the tree which represents the Formula */
2385 Detail::NodePtr root;
2386 friend class Statistics::Detail::Temp;
2390 * Create and initialize thie formula, and register it with the database.
2392 Formula() : VectorStat(true) { setInit(); }
2394 * Create a formula with the given root node, register it with the
2396 * @param r The root of the expression tree.
2398 Formula(Detail::Temp r) : VectorStat(true) {
2404 * Set an unitialized Formula to the given root.
2405 * @param r The root of the expression tree.
2406 * @return a reference to this formula.
2408 const Formula &operator=(Detail::Temp r) {
2409 assert(!root && "Can't change formulas");
2416 * Add the given tree to the existing one.
2417 * @param r The root of the expression tree.
2418 * @return a reference to this formula.
2420 const Formula &operator+=(Detail::Temp r) {
2421 using namespace Detail;
2423 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
2431 * Return the vector of values of this formula.
2432 * @return The result vector.
2434 const rvec_t &val() const { return root->val(); }
2436 * Return the total of the result vector.
2437 * @return The total of the result vector.
2439 result_t total() const { return root->total(); }
2442 * Return the number of elements in the tree.
2444 size_t size() const {
2448 return root->size();
2457 void dump(std::ostream &stream);
2460 operator+(Detail::Temp l, Detail::Temp r)
2462 using namespace Detail;
2463 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
2467 operator-(Detail::Temp l, Detail::Temp r)
2469 using namespace Detail;
2470 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
2474 operator*(Detail::Temp l, Detail::Temp r)
2476 using namespace Detail;
2477 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
2481 operator/(Detail::Temp l, Detail::Temp r)
2483 using namespace Detail;
2484 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
2488 operator%(Detail::Temp l, Detail::Temp r)
2490 using namespace Detail;
2491 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
2495 operator-(Detail::Temp l)
2497 using namespace Detail;
2498 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
2501 template <typename T>
2505 using namespace Detail;
2506 return NodePtr(new ConstNode<T>(val));
2509 template <typename T>
2513 using namespace Detail;
2514 return NodePtr(new FunctorNode<T>(val));
2517 template <typename T>
2521 using namespace Detail;
2522 return NodePtr(new ScalarNode<T>(val));
2526 sum(Detail::Temp val)
2528 using namespace Detail;
2529 return NodePtr(new SumNode<std::plus<result_t> >(val));
2532 extern bool PrintDescriptions;
2534 } // namespace statistics
2536 #endif // __STATISTICS_HH__