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"
63 // Un-comment this to enable wierdo-stat debugging
70 /** Define Not a number. */
72 /** Need to define __nan() */
76 /** Print stats out in SS format. */
77 #define STAT_DISPLAY_COMPAT
79 /** The current simulated cycle. */
82 /* A namespace for all of the Statistics */
83 namespace Statistics {
84 /** All results are doubles. */
85 typedef double result_t;
86 /** A vector to hold results. */
87 typedef std::vector<result_t> rvec_t;
90 * Define the storage for format flags.
91 * @todo Can probably shrink this.
93 typedef u_int32_t FormatFlags;
94 /** Nothing extra to print. */
95 const FormatFlags none = 0x0000;
96 /** Print the total. */
97 const FormatFlags total = 0x0001;
98 /** Print the percent of the total that this entry represents. */
99 const FormatFlags pdf = 0x0002;
100 /** Don't print if this is zero. */
101 const FormatFlags nozero = 0x0004;
102 /** Don't print if this is NAN */
103 const FormatFlags nonan = 0x0008;
104 /** Print the cumulative percentage of total upto this entry. */
105 const FormatFlags cdf = 0x0010;
106 /** Print the distribution. */
107 const FormatFlags dist = 0x0020;
108 /** Used for SS compatability. */
109 const FormatFlags __substat = 0x8000;
110 /** Mask of flags that can't be set directly */
111 const FormatFlags __reserved = __substat;
113 /* Contains the statistic implementation details */
115 //////////////////////////////////////////////////////////////////////
117 // Statistics Framework Base classes
119 //////////////////////////////////////////////////////////////////////
124 * Common base class for all statistics, used to maintain a list and print.
125 * This class holds no data itself but is used to find the associated
126 * StatData in the stat database @sa Statistics::Database.
131 /** Mark this statistics as initialized. */
134 * Finds and returns the associated StatData from the database.
135 * @return The formatting and output data of this statistic.
139 * Finds and returns a const pointer to the associated StatData.
140 * @return The formatting and output data of this statistic.
142 const StatData *mydata() const;
144 * Mark this stat for output at the end of simulation.
145 * @return The formatting and output data of this statistic.
149 * Finds and returns the SubData at the given index.
150 * @param index The index of the SubData to find.
151 * @return The name and description of the given index.
153 const SubData *mysubdata(int index) const;
155 * Create and return a new SubData field for the given index.
156 * @param index The index to create a SubData for.
157 * @return A pointer to the created SubData.
159 SubData *mysubdata_create(int index);
163 * Return the name of this stat.
164 * @return the name of the stat.
166 virtual std::string myname() const;
168 * Return the name of the sub field at the given index.
169 * @param index the subfield index.
170 * @return the name of the subfield.
172 virtual std::string mysubname(int index) const;
174 * Return the description of this stat.
175 * @return the description of this stat.
177 virtual std::string mydesc() const;
179 * Return the description of the subfield at the given index.
180 * @param index The subfield index.
181 * @return the description of the subfield.
183 virtual std::string mysubdesc(int index) const;
185 * Return the format flags of this stat.
186 * @return the format flags.
188 virtual FormatFlags myflags() const;
190 * Return true if this stat's prereqs have been satisfied (they are non
192 * @return true if the prerequisite stats aren't zero.
194 virtual bool dodisplay() const;
196 * Return the display percision.
197 * @return The display precision.
199 virtual int myprecision() const;
203 * Create this stat and perhaps register it with the stat database. To be
204 * printed a stat must be registered with the database.
205 * @param reg If true, register this stat in the database.
214 * Print this stat to the given ostream.
215 * @param stream The stream to print to.
217 virtual void display(std::ostream &stream) const = 0;
219 * Return the number of entries in this stat.
220 * @return The number of entries.
222 virtual size_t size() const = 0;
224 * Return true if the stat has value zero.
225 * @return True if the stat is zero.
227 virtual bool zero() const = 0;
231 * Set the name and marks this stat to print at the end of simulation.
232 * @param name The new name.
233 * @return A reference to this stat.
235 Stat &name(const std::string &name);
237 * Set the description and marks this stat to print at the end of
239 * @param desc The new description.
240 * @return A reference to this stat.
242 Stat &desc(const std::string &desc);
244 * Set the precision and marks this stat to print at the end of simulation.
245 * @param p The new precision
246 * @return A reference to this stat.
248 Stat &precision(int p);
250 * Set the flags and marks this stat to print at the end of simulation.
251 * @param f The new flags.
252 * @return A reference to this stat.
254 Stat &flags(FormatFlags f);
256 * Set the prerequisite stat and marks this stat to print at the end of
258 * @param prereq The prerequisite stat.
259 * @return A reference to this stat.
261 Stat &prereq(const Stat &prereq);
263 * Set the subfield name for the given index, and marks this stat to print
264 * at the end of simulation.
265 * @param index The subfield index.
266 * @param name The new name of the subfield.
267 * @return A reference to this stat.
269 Stat &subname(int index, const std::string &name);
271 * Set the subfield description for the given index and marks this stat to
272 * print at the end of simulation.
273 * @param index The subfield index.
274 * @param desc The new description of the subfield
275 * @return A reference to this stat.
277 Stat &subdesc(int index, const std::string &desc);
281 * Checks if the first stat's name is alphabetically less than the second.
282 * This function breaks names up at periods and considers each subname
284 * @param stat1 The first stat.
285 * @param stat2 The second stat.
286 * @return stat1's name is alphabetically before stat2's
288 static bool less(Stat *stat1, Stat *stat2);
291 /** A unique ID used for debugging. */
297 * Base class for all scalar stats. The class provides an interface to access
298 * the current value of the stat. This class can be used in formulas.
300 class ScalarStat : public Stat
304 * Create and perhaps register this stat with the database.
305 * @param reg If true, register this stat with the database.
307 ScalarStat(bool reg) : Stat(reg) {}
309 * Return the current value of this statistic as a result type.
310 * @return The current value of this statistic.
312 virtual result_t val() const = 0;
314 * Return true if this stat has value zero.
315 * @return True if this stat is zero.
317 virtual bool zero() const;
319 * Print this stat to the provided ostream.
320 * @param stream The output stream.
322 virtual void display(std::ostream &stream) const;
326 VectorDisplay(std::ostream &stream, const std::string &myname,
327 const std::vector<std::string> *mysubnames,
328 const std::string &mydesc,
329 const std::vector<std::string> *mysubdescs,
330 int myprecision, FormatFlags myflags, const rvec_t &vec,
334 * Base class for all vector stats. This class provides interfaces to access
335 * the current values of the stats as well as the totals. This class can be
338 class VectorStat : public Stat
342 * Create and perhaps register this stat with the database.
343 * @param reg If true, register this stat with the database.
345 VectorStat(bool reg) : Stat(reg) {}
347 * Return a vector of result typesd of all the values in the vector.
348 * @return The values of the vector.
350 virtual const rvec_t &val() const = 0;
352 * Return the total of all the entries in the vector.
353 * @return The total of the vector.
355 virtual result_t total() const = 0;
357 * Return true if this stat has value zero.
358 * @return True if this stat is zero.
360 virtual bool zero() const;
362 * Print this stat to the provided ostream.
363 * @param stream The output stream.
365 virtual void display(std::ostream &stream) const;
368 //////////////////////////////////////////////////////////////////////
372 //////////////////////////////////////////////////////////////////////
375 * Templatized storage and interface for a simple scalar stat.
377 template <typename T>
381 /** The paramaters for this storage type, none for a scalar. */
385 /** The statistic value. */
390 * Builds this storage element and calls the base constructor of the
393 StatStor(const Params &) : data(T()) {}
396 * The the stat to the given value.
397 * @param val The new value.
398 * @param p The paramters of this storage type.
400 void set(T val, const Params &p) { data = val; }
402 * Increment the stat by the given value.
403 * @param val The new value.
404 * @param p The paramters of this storage type.
406 void inc(T val, const Params &p) { data += val; }
408 * Decrement the stat by the given value.
409 * @param val The new value.
410 * @param p The paramters of this storage type.
412 void dec(T val, const Params &p) { data -= val; }
414 * Return the value of this stat as a result type.
415 * @param p The parameters of this storage type.
416 * @return The value of this stat.
418 result_t val(const Params &p) const { return (result_t)data; }
420 * Return the value of this stat as its base type.
421 * @param p The params of this storage type.
422 * @return The value of this stat.
424 T value(const Params &p) const { return data; }
428 * Templatized storage and interface to a per-cycle average stat. This keeps
429 * a current count and updates a total (count * cycles) when this count
430 * changes. This allows the quick calculation of a per cycle count of the item
431 * being watched. This is good for keeping track of residencies in structures
432 * among other things.
433 * @todo add lateny to the stat and fix binning.
435 template <typename T>
439 /** The paramaters for this storage type, none for this average. */
443 /** The current count. */
445 /** The total count for all cycles. */
446 mutable result_t total;
447 /** The cycle that current last changed. */
452 * Build and initializes this stat storage.
454 AvgStor(const Params &) : current(T()), total(0), last(0) { }
457 * Set the current count to the one provided, update the total and last
459 * @param val The new count.
460 * @param p The parameters for this storage.
462 void set(T val, const Params &p) {
463 total += current * (curTick - last);
468 * Increment the current count by the provided value, calls set.
469 * @param val The amount to increment.
470 * @param p The parameters for this storage.
472 void inc(T val, const Params &p) { set(current + val, p); }
474 * Deccrement the current count by the provided value, calls set.
475 * @param val The amount to decrement.
476 * @param p The parameters for this storage.
478 void dec(T val, const Params &p) { set(current - val, p); }
480 * Return the current average.
481 * @param p The parameters for this storage.
482 * @return The current average.
484 result_t val(const Params &p) const {
485 total += current * (curTick - last);
487 return (result_t)(total + current) / (result_t)(curTick + 1);
490 * Return the current count.
491 * @param p The parameters for this storage.
492 * @return The current count.
494 T value(const Params &p) const { return current; }
498 * Implementation of a scalar stat. The type of stat is determined by the
499 * Storage template. The storage for this stat is held within the Bin class.
500 * This allows for breaking down statistics across multiple bins easily.
502 template <typename T, template <typename T> class Storage, class Bin>
503 class ScalarBase : public ScalarStat
506 /** Define the type of the storage class. */
507 typedef Storage<T> storage_t;
508 /** Define the params of the storage class. */
509 typedef typename storage_t::Params params_t;
510 /** Define the bin type. */
511 typedef typename Bin::Bin<storage_t> bin_t;
514 /** The bin of this stat. */
516 /** The parameters for this stat. */
521 * Retrieve the storage from the bin.
522 * @return The storage object for this stat.
524 storage_t *data() { return bin.data(params); }
526 * Retrieve a const pointer to the storage from the bin.
527 * @return A const pointer to the storage object for this stat.
529 const storage_t *data() const {
530 return (const_cast<bin_t *>(&bin))->data(params);
535 * Copy constructor, copies are not allowed.
537 ScalarBase(const ScalarBase &stat);
541 const ScalarBase &operator=(const ScalarBase &);
545 * Return the current value of this stat as a result type.
546 * @return The current value.
548 result_t val() const { return data()->val(params); }
550 * Return the current value of this stat as its base type.
551 * @return The current value.
553 T value() const { return data()->value(params); }
557 * Create and initialize this stat, register it with the database.
559 ScalarBase() : ScalarStat(true) {
565 // Common operators for stats
567 * Increment the stat by 1. This calls the associated storage object inc
570 void operator++() { data()->inc(1, params); }
572 * Decrement the stat by 1. This calls the associated storage object dec
575 void operator--() { data()->dec(1, params); }
577 /** Increment the stat by 1. */
578 void operator++(int) { ++*this; }
579 /** Decrement the stat by 1. */
580 void operator--(int) { --*this; }
583 * Set the data value to the given value. This calls the associated storage
584 * object set function.
585 * @param v The new value.
587 template <typename U>
588 void operator=(const U& v) { data()->set(v, params); }
591 * Increment the stat by the given value. This calls the associated
592 * storage object inc function.
593 * @param v The value to add.
595 template <typename U>
596 void operator+=(const U& v) { data()->inc(v, params); }
599 * Decrement the stat by the given value. This calls the associated
600 * storage object dec function.
601 * @param v The value to substract.
603 template <typename U>
604 void operator-=(const U& v) { data()->dec(v, params); }
607 * Return the number of elements, always 1 for a scalar.
610 virtual size_t size() const { return 1; }
613 //////////////////////////////////////////////////////////////////////
617 //////////////////////////////////////////////////////////////////////
618 template <typename T, template <typename T> class Storage, class Bin>
622 * Implementation of a vector of stats. The type of stat is determined by the
623 * Storage class. @sa ScalarBase
625 template <typename T, template <typename T> class Storage, class Bin>
626 class VectorBase : public VectorStat
629 /** Define the type of the storage class. */
630 typedef Storage<T> storage_t;
631 /** Define the params of the storage class. */
632 typedef typename storage_t::Params params_t;
633 /** Define the bin type. */
634 typedef typename Bin::VectorBin<storage_t> bin_t;
637 /** Local storage for the entry values, used for printing. */
641 /** The bin of this stat. */
643 /** The parameters for this stat. */
648 * Retrieve the storage from the bin for the given index.
649 * @param index The vector index to access.
650 * @return The storage object at the given index.
652 storage_t *data(int index) { return bin.data(index, params); }
654 * Retrieve a const pointer to the storage from the bin
655 * for the given index.
656 * @param index The vector index to access.
657 * @return A const pointer to the storage object at the given index.
659 const storage_t *data(int index) const {
660 return (const_cast<bin_t *>(&bin))->data(index, params);
664 // Copying stats is not allowed
665 /** Copying stats isn't allowed. */
666 VectorBase(const VectorBase &stat);
667 /** Copying stats isn't allowed. */
668 const VectorBase &operator=(const VectorBase &);
672 * Copy the values to a local vector and return a reference to it.
673 * @return A reference to a vector of the stat values.
675 const rvec_t &val() const {
679 vec = new rvec_t(size());
681 for (int i = 0; i < size(); ++i)
682 (*vec)[i] = data(i)->val(params);
688 * Return a total of all entries in this vector.
689 * @return The total of all vector entries.
691 result_t total() const {
692 result_t total = 0.0;
693 for (int i = 0; i < size(); ++i)
694 total += data(i)->val(params);
700 * Create this vector and register it with the database.
702 VectorBase() : VectorStat(true), vec(NULL) {}
706 ~VectorBase() { if (vec) delete vec; }
709 * Set this vector to have the given size.
710 * @param size The new size.
711 * @return A reference to this stat.
713 VectorBase &init(size_t size) {
714 bin.init(size, params);
720 /** Friend this class with the associated scalar proxy. */
721 friend class ScalarProxy<T, Storage, Bin>;
724 * Return a reference (ScalarProxy) to the stat at the given index.
725 * @param index The vector index to access.
726 * @return A reference of the stat.
728 ScalarProxy<T, Storage, Bin> operator[](int index);
731 * Return the number of elements in this vector.
732 * @return The size of the vector.
734 virtual size_t size() const { return bin.size(); }
738 * A proxy class to access the stat at a given index in a VectorBase stat.
739 * Behaves like a ScalarBase.
741 template <typename T, template <typename T> class Storage, class Bin>
742 class ScalarProxy : public ScalarStat
745 /** Define the type of the storage class. */
746 typedef Storage<T> storage_t;
747 /** Define the params of the storage class. */
748 typedef typename storage_t::Params params_t;
749 /** Define the bin type. */
750 typedef typename Bin::VectorBin<storage_t> bin_t;
753 /** Pointer to the bin in the parent VectorBase. */
755 /** Pointer to the params in the parent VectorBase. */
757 /** The index to access in the parent VectorBase. */
762 * Retrieve the storage from the bin.
763 * @return The storage from the bin for this stat.
765 storage_t *data() { return bin->data(index, *params); }
767 * Retrieve a const pointer to the storage from the bin.
768 * @return A const pointer to the storage for this stat.
770 const storage_t *data() const { return bin->data(index, *params); }
774 * Return the current value of this statas a result type.
775 * @return The current value.
777 result_t val() const { return data()->val(*params); }
779 * Return the current value of this stat as its base type.
780 * @return The current value.
782 T value() const { return data()->value(*params); }
786 * Create and initialize this proxy, do not register it with the database.
787 * @param b The bin to use.
788 * @param p The params to use.
789 * @param i The index to access.
791 ScalarProxy(bin_t &b, params_t &p, int i)
792 : ScalarStat(false), bin(&b), params(&p), index(i) {}
794 * Create a copy of the provided ScalarProxy.
795 * @param sp The proxy to copy.
797 ScalarProxy(const ScalarProxy &sp)
798 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
800 * Set this proxy equal to the provided one.
801 * @param sp The proxy to copy.
802 * @return A reference to this proxy.
804 const ScalarProxy &operator=(const ScalarProxy &sp) {
812 // Common operators for stats
814 * Increment the stat by 1. This calls the associated storage object inc
817 void operator++() { data()->inc(1, *params); }
819 * Decrement the stat by 1. This calls the associated storage object dec
822 void operator--() { data()->dec(1, *params); }
824 /** Increment the stat by 1. */
825 void operator++(int) { ++*this; }
826 /** Decrement the stat by 1. */
827 void operator--(int) { --*this; }
830 * Set the data value to the given value. This calls the associated storage
831 * object set function.
832 * @param v The new value.
834 template <typename U>
835 void operator=(const U& v) { data()->set(v, *params); }
838 * Increment the stat by the given value. This calls the associated
839 * storage object inc function.
840 * @param v The value to add.
842 template <typename U>
843 void operator+=(const U& v) { data()->inc(v, *params); }
846 * Decrement the stat by the given value. This calls the associated
847 * storage object dec function.
848 * @param v The value to substract.
850 template <typename U>
851 void operator-=(const U& v) { data()->dec(v, *params); }
854 * Return the number of elements, always 1 for a scalar.
857 virtual size_t size() const { return 1; }
860 template <typename T, template <typename T> class Storage, class Bin>
861 inline ScalarProxy<T, Storage, Bin>
862 VectorBase<T, Storage, Bin>::operator[](int index)
864 assert (index >= 0 && index < size());
865 return ScalarProxy<T, Storage, Bin>(bin, params, index);
868 template <typename T, template <typename T> class Storage, class Bin>
871 template <typename T, template <typename T> class Storage, class Bin>
872 class Vector2dBase : public Stat
875 typedef Storage<T> storage_t;
876 typedef typename storage_t::Params params_t;
877 typedef typename Bin::VectorBin<storage_t> bin_t;
884 std::vector<std::string> *y_subnames;
887 storage_t *data(int index) { return bin.data(index, params); }
888 const storage_t *data(int index) const {
889 return (const_cast<bin_t *>(&bin))->data(index, params);
893 // Copying stats is not allowed
894 Vector2dBase(const Vector2dBase &stat);
895 const Vector2dBase &operator=(const Vector2dBase &);
898 Vector2dBase() : Stat(true) {}
901 Vector2dBase &init(size_t _x, size_t _y) {
904 bin.init(x * y, params);
906 y_subnames = new std::vector<std::string>(y);
912 * @warning This makes the assumption that if you're gonna subnames a 2d
913 * vector, you're subnaming across all y
915 Vector2dBase &ysubnames(const char **names)
917 for (int i=0; i < y; ++i) {
918 (*y_subnames)[i] = names[i];
922 Vector2dBase &ysubname(int index, const std::string subname)
924 (*y_subnames)[i] = subname.c_str();
927 std::string ysubname(int i) const { return (*y_subnames)[i]; }
929 friend class VectorProxy<T, Storage, Bin>;
930 VectorProxy<T, Storage, Bin> operator[](int index);
932 virtual size_t size() const { return bin.size(); }
933 virtual bool zero() const { return data(0)->value(params) == 0.0; }
936 display(std::ostream &out) const
938 bool have_subname = false;
939 for (int i = 0; i < x; ++i) {
940 if (!mysubname(i).empty())
945 result_t super_total = 0.0;
946 for (int i = 0; i < x; ++i) {
949 subname = mysubname(i);
953 subname = to_string(i);
958 result_t total = 0.0;
959 for (int j = 0; j < y; ++j) {
960 vec[j] = data(iy + j)->val(params);
961 tot_vec[j] += vec[j];
963 super_total += vec[j];
967 if (mysubdesc(i).empty()) {
973 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
974 myprecision(), myflags(), vec, total);
977 if ((myflags() & ::Statistics::total) && (x > 1)) {
978 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
979 myprecision(), myflags(), tot_vec, super_total);
985 template <typename T, template <typename T> class Storage, class Bin>
986 class VectorProxy : public VectorStat
989 typedef Storage<T> storage_t;
990 typedef typename storage_t::Params params_t;
991 typedef typename Bin::VectorBin<storage_t> bin_t;
1000 mutable rvec_t *vec;
1002 storage_t *data(int index) {
1003 assert(index < len);
1004 return bin->data(offset + index, *params);
1007 const storage_t *data(int index) const {
1008 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
1012 const rvec_t &val() const {
1014 vec->resize(size());
1016 vec = new rvec_t(size());
1018 for (int i = 0; i < size(); ++i)
1019 (*vec)[i] = data(i)->val(*params);
1024 result_t total() const {
1025 result_t total = 0.0;
1026 for (int i = 0; i < size(); ++i)
1027 total += data(i)->val(*params);
1032 VectorProxy(bin_t &b, params_t &p, int o, int l)
1033 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
1035 VectorProxy(const VectorProxy &sp)
1036 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
1037 len(sp.len), vec(NULL)
1044 const VectorProxy &operator=(const VectorProxy &sp) {
1055 virtual size_t size() const { return len; }
1057 ScalarProxy<T, Storage, Bin> operator[](int index) {
1058 assert (index >= 0 && index < size());
1059 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
1063 template <typename T, template <typename T> class Storage, class Bin>
1064 inline VectorProxy<T, Storage, Bin>
1065 Vector2dBase<T, Storage, Bin>::operator[](int index)
1067 int offset = index * y;
1068 assert (index >= 0 && offset < size());
1069 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
1072 //////////////////////////////////////////////////////////////////////
1074 // Non formula statistics
1076 //////////////////////////////////////////////////////////////////////
1078 void DistDisplay(std::ostream &stream, const std::string &name,
1079 const std::string &desc, int precision, FormatFlags flags,
1080 result_t min_val, result_t max_val,
1081 result_t underflow, result_t overflow,
1082 const rvec_t &vec, int min, int max, int bucket_size,
1085 * Templatized storage and interface for a distrbution stat.
1087 template <typename T>
1091 /** The parameters for a distribution stat. */
1094 /** The minimum value to track. */
1096 /** The maximum value to track. */
1098 /** The number of entries in each bucket. */
1100 /** The number of buckets. Equal to (max-min)/bucket_size. */
1105 /** The smallest value sampled. */
1107 /** The largest value sampled. */
1109 /** The number of values sampled less than min. */
1111 /** The number of values sampled more than max. */
1113 /** Counter for each bucket. */
1118 * Construct this storage with the supplied params.
1119 * @param params The parameters.
1121 DistStor(const Params ¶ms)
1122 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1126 * Add a value to the distribution for the given number of times.
1127 * @param val The value to add.
1128 * @param number The number of times to add the value.
1129 * @param params The paramters of the distribution.
1131 void sample(T val, int number, const Params ¶ms) {
1132 if (val < params.min)
1133 underflow += number;
1134 else if (val > params.max)
1137 int index = (val - params.min) / params.bucket_size;
1138 assert(index < size(params));
1139 vec[index] += number;
1150 * Return the number of buckets in this distribution.
1151 * @return the number of buckets.
1152 * @todo Is it faster to return the size from the parameters?
1154 size_t size(const Params &) const { return vec.size(); }
1157 * Returns true if any calls to sample have been made.
1158 * @param params The paramters of the distribution.
1159 * @return True if any values have been sampled.
1161 bool zero(const Params ¶ms) const {
1162 if (underflow != 0 || overflow != 0)
1165 int s = size(params);
1166 for (int i = 0; i < s; i++)
1174 * Print this distribution and the given print data to the given ostream.
1175 * @param stream The output stream.
1176 * @param name The name of this stat (from StatData).
1177 * @param desc The description of this stat (from StatData).
1178 * @param precision The print precision (from StatData).
1179 * @param flags The format flags (from StatData).
1180 * @param params The paramters of this distribution.
1182 void display(std::ostream &stream, const std::string &name,
1183 const std::string &desc, int precision, FormatFlags flags,
1184 const Params ¶ms) const {
1186 #ifdef STAT_DISPLAY_COMPAT
1187 result_t min = params.min;
1189 result_t min = (min_val == INT_MAX) ? params.min : min_val;
1191 result_t max = (max_val == INT_MIN) ? 0 : max_val;
1193 rvec_t rvec(params.size);
1194 for (int i = 0; i < params.size; ++i)
1197 DistDisplay(stream, name, desc, precision, flags,
1198 (result_t)min, (result_t)max,
1199 (result_t)underflow, (result_t)overflow,
1200 rvec, params.min, params.max, params.bucket_size,
1205 void FancyDisplay(std::ostream &stream, const std::string &name,
1206 const std::string &desc, int precision, FormatFlags flags,
1207 result_t mean, result_t variance);
1210 * Templatized storage and interface for a distribution that calculates mean
1213 template <typename T>
1218 * No paramters for this storage.
1223 /** The current sum. */
1225 /** The sum of squares. */
1227 /** The total number of samples. */
1232 * Create and initialize this storage.
1234 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
1237 * Add a value the given number of times to this running average.
1238 * Update the running sum and sum of squares, increment the number of
1239 * values seen by the given number.
1240 * @param val The value to add.
1241 * @param number The number of times to add the value.
1242 * @param p The parameters of this stat.
1244 void sample(T val, int number, const Params &p) {
1245 T value = val * number;
1247 squares += value * value;
1252 * Print this distribution and the given print data to the given ostream.
1253 * @param stream The output stream.
1254 * @param name The name of this stat (from StatData).
1255 * @param desc The description of this stat (from StatData).
1256 * @param precision The print precision (from StatData).
1257 * @param flags The format flags (from StatData).
1258 * @param params The paramters of this distribution.
1260 void display(std::ostream &stream, const std::string &name,
1261 const std::string &desc, int precision, FormatFlags flags,
1262 const Params ¶ms) const {
1264 result_t mean = NAN;
1265 result_t variance = NAN;
1268 result_t fsum = sum;
1269 result_t fsq = squares;
1270 result_t ftot = total;
1273 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
1276 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1280 * Return the number of entries in this stat, 1
1283 size_t size(const Params &) const { return 1; }
1285 * Return true if no samples have been added.
1286 * @return True if no samples have been added.
1288 bool zero(const Params &) const { return total == 0; }
1292 * Templatized storage for distribution that calculates per cycle mean and
1295 template <typename T>
1299 /** No parameters for this storage. */
1303 /** Current total. */
1305 /** Current sum of squares. */
1310 * Create and initialize this storage.
1312 AvgFancy(const Params &) : sum(0), squares(0) {}
1315 * Add a value to the distribution for the given number of times.
1316 * Update the running sum and sum of squares.
1317 * @param val The value to add.
1318 * @param number The number of times to add the value.
1319 * @param p The paramters of the distribution.
1321 void sample(T val, int number, const Params& p) {
1322 T value = val * number;
1324 squares += value * value;
1328 * Print this distribution and the given print data to the given ostream.
1329 * @param stream The output stream.
1330 * @param name The name of this stat (from StatData).
1331 * @param desc The description of this stat (from StatData).
1332 * @param precision The print precision (from StatData).
1333 * @param flags The format flags (from StatData).
1334 * @param params The paramters of this distribution.
1336 void display(std::ostream &stream, const std::string &name,
1337 const std::string &desc, int precision, FormatFlags flags,
1338 const Params ¶ms) const {
1339 result_t mean = sum / curTick;
1340 result_t variance = (squares - sum * sum) / curTick;
1342 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1346 * Return the number of entries, in this case 1.
1349 size_t size(const Params ¶ms) const { return 1; }
1351 * Return true if no samples have been added.
1352 * @return True if the sum is zero.
1354 bool zero(const Params ¶ms) const { return sum == 0; }
1358 * Implementation of a distribution stat. The type of distribution is
1359 * determined by the Storage template. @sa ScalarBase
1361 template <typename T, template <typename T> class Storage, class Bin>
1362 class DistBase : public Stat
1365 /** Define the type of the storage class. */
1366 typedef Storage<T> storage_t;
1367 /** Define the params of the storage class. */
1368 typedef typename storage_t::Params params_t;
1369 /** Define the bin type. */
1370 typedef typename Bin::Bin<storage_t> bin_t;
1373 /** The bin of this stat. */
1375 /** The parameters for this stat. */
1380 * Retrieve the storage from the bin.
1381 * @return The storage object for this stat.
1383 storage_t *data() { return bin.data(params); }
1385 * Retrieve a const pointer to the storage from the bin.
1386 * @return A const pointer to the storage object for this stat.
1388 const storage_t *data() const {
1389 return (const_cast<bin_t *>(&bin))->data(params);
1393 // Copying stats is not allowed
1394 /** Copies are not allowed. */
1395 DistBase(const DistBase &stat);
1396 /** Copies are not allowed. */
1397 const DistBase &operator=(const DistBase &);
1401 * Create this distrubition and register it with the database.
1403 DistBase() : Stat(true) { }
1410 * Add a value to the distribtion n times. Calls sample on the storage
1412 * @param v The value to add.
1413 * @param n The number of times to add it, defaults to 1.
1415 template <typename U>
1416 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1419 * Return the number of entries in this stat.
1420 * @return The number of entries.
1422 virtual size_t size() const { return data()->size(params); }
1424 * Return true if no samples have been added.
1425 * @return True if there haven't been any samples.
1427 virtual bool zero() const { return data()->zero(params); }
1429 * Print this distribution to the given ostream.
1430 * @param stream The output stream.
1432 virtual void display(std::ostream &stream) const {
1433 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
1438 template <typename T, template <typename T> class Storage, class Bin>
1441 template <typename T, template <typename T> class Storage, class Bin>
1442 class VectorDistBase : public Stat
1445 typedef Storage<T> storage_t;
1446 typedef typename storage_t::Params params_t;
1447 typedef typename Bin::VectorBin<storage_t> bin_t;
1454 storage_t *data(int index) { return bin.data(index, params); }
1455 const storage_t *data(int index) const {
1456 return (const_cast<bin_t *>(&bin))->data(index, params);
1460 // Copying stats is not allowed
1461 VectorDistBase(const VectorDistBase &stat);
1462 const VectorDistBase &operator=(const VectorDistBase &);
1465 VectorDistBase() : Stat(true) { }
1466 ~VectorDistBase() { }
1468 friend class DistProxy<T, Storage, Bin>;
1469 DistProxy<T, Storage, Bin> operator[](int index);
1470 const DistProxy<T, Storage, Bin> operator[](int index) const;
1472 virtual size_t size() const { return bin.size(); }
1473 virtual bool zero() const { return false; }
1474 virtual void display(std::ostream &stream) const;
1477 template <typename T, template <typename T> class Storage, class Bin>
1478 class DistProxy : public Stat
1481 typedef Storage<T> storage_t;
1482 typedef typename storage_t::Params params_t;
1483 typedef typename Bin::Bin<storage_t> bin_t;
1484 typedef VectorDistBase<T, Storage, Bin> base_t;
1489 const base_t *cstat;
1494 storage_t *data() { return stat->data(index); }
1495 const storage_t *data() const { return cstat->data(index); }
1498 DistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1499 : Stat(false), cstat(&s), index(i) {}
1500 DistProxy(const DistProxy &sp)
1501 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1502 const DistProxy &operator=(const DistProxy &sp) {
1503 cstat = sp.cstat; index = sp.index; return *this;
1507 template <typename U>
1508 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1510 virtual size_t size() const { return 1; }
1511 virtual bool zero() const {
1512 return data()->zero(cstat->params);
1514 virtual void display(std::ostream &stream) const {
1515 std::stringstream name, desc;
1517 if (!(cstat->mysubname(index).empty())) {
1518 name << cstat->myname() << cstat->mysubname(index);
1520 name << cstat->myname() << "_" << index;
1522 if (!(cstat->mysubdesc(index).empty())) {
1523 desc << cstat->mysubdesc(index);
1525 desc << cstat->mydesc();
1528 data()->display(stream, name.str(), desc.str(),
1529 cstat->myprecision(), cstat->myflags(), cstat->params);
1533 template <typename T, template <typename T> class Storage, class Bin>
1534 inline DistProxy<T, Storage, Bin>
1535 VectorDistBase<T, Storage, Bin>::operator[](int index)
1537 assert (index >= 0 && index < size());
1538 return DistProxy<T, Storage, Bin>(*this, index);
1541 template <typename T, template <typename T> class Storage, class Bin>
1542 inline const DistProxy<T, Storage, Bin>
1543 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1545 assert (index >= 0 && index < size());
1546 return DistProxy<T, Storage, Bin>(*this, index);
1550 * @todo Need a way to print Distribution totals across the Vector
1552 template <typename T, template <typename T> class Storage, class Bin>
1554 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1556 for (int i = 0; i < size(); ++i) {
1557 DistProxy<T, Storage, Bin> proxy(*this, i);
1558 proxy.display(stream);
1564 VectorDistBase<T, Storage, Bin>::total(int index) const
1567 for (int i=0; i < x_size(); ++i) {
1568 total += data(i)->val(*params);
1573 //////////////////////////////////////////////////////////////////////
1577 //////////////////////////////////////////////////////////////////////
1580 * Base class for formula statistic node. These nodes are used to build a tree
1581 * that represents the formula.
1583 class Node : public RefCounted
1587 * Return the number of nodes in the subtree starting at this node.
1588 * @return the number of nodes in this subtree.
1590 virtual size_t size() const = 0;
1592 * Return the result vector of this subtree.
1593 * @return The result vector of this subtree.
1595 virtual const rvec_t &val() const = 0;
1597 * Return the total of the result vector.
1598 * @return The total of the result vector.
1600 virtual result_t total() const = 0;
1603 /** Reference counting pointer to a function Node. */
1604 typedef RefCountingPtr<Node> NodePtr;
1606 class ScalarStatNode : public Node
1609 const ScalarStat &stat;
1610 mutable rvec_t result;
1613 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1614 const rvec_t &val() const { result[0] = stat.val(); return result; }
1615 virtual result_t total() const { return stat.val(); };
1617 virtual size_t size() const { return 1; }
1620 template <typename T, template <typename T> class Storage, class Bin>
1621 class ScalarProxyNode : public Node
1624 const ScalarProxy<T, Storage, Bin> proxy;
1625 mutable rvec_t result;
1628 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1629 : proxy(p), result(1) { }
1630 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1631 virtual result_t total() const { return proxy.val(); };
1633 virtual size_t size() const { return 1; }
1636 class VectorStatNode : public Node
1639 const VectorStat &stat;
1642 VectorStatNode(const VectorStat &s) : stat(s) {}
1643 const rvec_t &val() const { return stat.val(); }
1644 virtual result_t total() const { return stat.total(); };
1646 virtual size_t size() const { return stat.size(); }
1649 template <typename T>
1650 class ConstNode : public Node
1656 ConstNode(T s) : data(1, (result_t)s) {}
1657 const rvec_t &val() const { return data; }
1658 virtual result_t total() const { return data[0]; };
1660 virtual size_t size() const { return 1; }
1663 template <typename T>
1664 class FunctorNode : public Node
1668 mutable rvec_t result;
1671 FunctorNode(T &f) : functor(f) { result.resize(1); }
1672 const rvec_t &val() const {
1673 result[0] = (result_t)functor();
1676 virtual result_t total() const { return (result_t)functor(); };
1678 virtual size_t size() const { return 1; }
1681 template <typename T>
1682 class ScalarNode : public Node
1686 mutable rvec_t result;
1689 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1690 const rvec_t &val() const {
1691 result[0] = (result_t)scalar;
1694 virtual result_t total() const { return (result_t)scalar; };
1696 virtual size_t size() const { return 1; }
1700 class UnaryNode : public Node
1704 mutable rvec_t result;
1707 UnaryNode(NodePtr p) : l(p) {}
1709 const rvec_t &val() const {
1710 const rvec_t &lvec = l->val();
1711 int size = lvec.size();
1715 result.resize(size);
1717 for (int i = 0; i < size; ++i)
1718 result[i] = op(lvec[i]);
1723 result_t total() const {
1725 return op(l->total());
1728 virtual size_t size() const { return l->size(); }
1732 class BinaryNode : public Node
1737 mutable rvec_t result;
1740 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1742 const rvec_t &val() const {
1744 const rvec_t &lvec = l->val();
1745 const rvec_t &rvec = r->val();
1747 assert(lvec.size() > 0 && rvec.size() > 0);
1749 if (lvec.size() == 1 && rvec.size() == 1) {
1751 result[0] = op(lvec[0], rvec[0]);
1752 } else if (lvec.size() == 1) {
1753 int size = rvec.size();
1754 result.resize(size);
1755 for (int i = 0; i < size; ++i)
1756 result[i] = op(lvec[0], rvec[i]);
1757 } else if (rvec.size() == 1) {
1758 int size = lvec.size();
1759 result.resize(size);
1760 for (int i = 0; i < size; ++i)
1761 result[i] = op(lvec[i], rvec[0]);
1762 } else if (rvec.size() == lvec.size()) {
1763 int size = rvec.size();
1764 result.resize(size);
1765 for (int i = 0; i < size; ++i)
1766 result[i] = op(lvec[i], rvec[i]);
1772 result_t total() const {
1774 return op(l->total(), r->total());
1777 virtual size_t size() const {
1785 assert(ls == rs && "Node vector sizes are not equal");
1792 class SumNode : public Node
1796 mutable rvec_t result;
1799 SumNode(NodePtr p) : l(p), result(1) {}
1801 const rvec_t &val() const {
1802 const rvec_t &lvec = l->val();
1803 int size = lvec.size();
1809 for (int i = 0; i < size; ++i)
1810 result[0] = op(result[0], lvec[i]);
1815 result_t total() const {
1816 const rvec_t &lvec = l->val();
1817 int size = lvec.size();
1820 result_t result = 0.0;
1823 for (int i = 0; i < size; ++i)
1824 result = op(result, lvec[i]);
1829 virtual size_t size() const { return 1; }
1833 * Helper class to construct formula node trees.
1839 * Pointer to a Node object.
1845 * Copy the given pointer to this class.
1846 * @param n A pointer to a Node object to copy.
1848 Temp(NodePtr n) : node(n) {}
1850 * Create a new ScalarStatNode.
1851 * @param s The ScalarStat to place in a node.
1853 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1855 * Create a new ScalarProxyNode.
1856 * @param p The ScalarProxy to place in a node.
1858 template <typename T, template <typename T> class Storage, class Bin>
1859 Temp(const ScalarProxy<T, Storage, Bin> &p)
1860 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1862 * Create a new VectorStatNode.
1863 * @param s The VectorStat to place in a node.
1865 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1868 * Create a ConstNode
1869 * @param value The value of the const node.
1871 Temp(signed char value) : node(new ConstNode<signed char>(value)) {}
1873 * Create a ConstNode
1874 * @param value The value of the const node.
1876 Temp(unsigned char value) : node(new ConstNode<unsigned char>(value)) {}
1878 * Create a ConstNode
1879 * @param value The value of the const node.
1881 Temp(signed short value) : node(new ConstNode<signed short>(value)) {}
1883 * Create a ConstNode
1884 * @param value The value of the const node.
1886 Temp(unsigned short value) : node(new ConstNode<unsigned short>(value)) {}
1888 * Create a ConstNode
1889 * @param value The value of the const node.
1891 Temp(signed int value) : node(new ConstNode<signed int>(value)) {}
1893 * Create a ConstNode
1894 * @param value The value of the const node.
1896 Temp(unsigned int value) : node(new ConstNode<unsigned int>(value)) {}
1898 * Create a ConstNode
1899 * @param value The value of the const node.
1901 Temp(signed long value) : node(new ConstNode<signed long>(value)) {}
1903 * Create a ConstNode
1904 * @param value The value of the const node.
1906 Temp(unsigned long value) : node(new ConstNode<unsigned long>(value)) {}
1908 * Create a ConstNode
1909 * @param value The value of the const node.
1911 Temp(signed long long value)
1912 : node(new ConstNode<signed long long>(value)) {}
1914 * Create a ConstNode
1915 * @param value The value of the const node.
1917 Temp(unsigned long long value)
1918 : node(new ConstNode<unsigned long long>(value)) {}
1920 * Create a ConstNode
1921 * @param value The value of the const node.
1923 Temp(float value) : node(new ConstNode<float>(value)) {}
1925 * Create a ConstNode
1926 * @param value The value of the const node.
1928 Temp(double value) : node(new ConstNode<double>(value)) {}
1931 * Return the node pointer.
1932 * @return the node pointer.
1934 operator NodePtr() { return node;}
1938 //////////////////////////////////////////////////////////////////////
1940 // Binning Interface
1942 //////////////////////////////////////////////////////////////////////
1951 off_t size() const { return memsize; }
1955 BinBase(size_t size);
1959 } // namespace Detail
1961 template <class BinType>
1962 struct StatBin : public Detail::BinBase
1964 static StatBin *&curBin() {
1965 static StatBin *current = NULL;
1969 static void setCurBin(StatBin *bin) { curBin() = bin; }
1970 static StatBin *current() { assert(curBin()); return curBin(); }
1972 static off_t &offset() {
1973 static off_t offset = 0;
1977 static off_t new_offset(size_t size) {
1978 size_t mask = sizeof(u_int64_t) - 1;
1979 off_t off = offset();
1981 // That one is for the last trailing flags byte.
1982 offset() += (size + 1 + mask) & ~mask;
1987 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1989 char *memory(off_t off) {
1990 assert(offset() <= size());
1991 return Detail::BinBase::memory() + off;
1994 static void activate(StatBin &bin) { setCurBin(&bin); }
2002 BinBase() : offset(-1) {}
2003 void allocate(size_t size) {
2004 offset = new_offset(size);
2007 assert(offset != -1);
2008 return current()->memory(offset);
2012 template <class Storage>
2013 class Bin : public BinBase
2016 typedef typename Storage::Params Params;
2019 Bin() { allocate(sizeof(Storage)); }
2020 bool initialized() const { return true; }
2021 void init(const Params ¶ms) { }
2023 int size() const { return 1; }
2025 Storage *data(const Params ¶ms) {
2026 assert(initialized());
2027 char *ptr = access();
2028 char *flags = ptr + sizeof(Storage);
2029 if (!(*flags & 0x1)) {
2031 new (ptr) Storage(params);
2033 return reinterpret_cast<Storage *>(ptr);
2037 template <class Storage>
2038 class VectorBin : public BinBase
2041 typedef typename Storage::Params Params;
2047 VectorBin() : _size(0) {}
2049 bool initialized() const { return _size > 0; }
2050 void init(int s, const Params ¶ms) {
2051 assert(!initialized());
2054 allocate(_size * sizeof(Storage));
2057 int size() const { return _size; }
2059 Storage *data(int index, const Params ¶ms) {
2060 assert(initialized());
2061 assert(index >= 0 && index < size());
2062 char *ptr = access();
2063 char *flags = ptr + size() * sizeof(Storage);
2064 if (!(*flags & 0x1)) {
2066 for (int i = 0; i < size(); ++i)
2067 new (ptr + i * sizeof(Storage)) Storage(params);
2069 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2074 class MainBinType {};
2075 typedef StatBin<MainBinType> MainBin;
2079 template <class Storage>
2083 typedef typename Storage::Params Params;
2086 char ptr[sizeof(Storage)];
2089 bool initialized() const { return true; }
2090 void init(const Params ¶ms) {
2091 new (ptr) Storage(params);
2093 int size() const{ return 1; }
2094 Storage *data(const Params ¶ms) {
2095 assert(initialized());
2096 return reinterpret_cast<Storage *>(ptr);
2100 template <class Storage>
2104 typedef typename Storage::Params Params;
2111 VectorBin() : ptr(NULL) { }
2116 bool initialized() const { return ptr != NULL; }
2117 void init(int s, const Params ¶ms) {
2118 assert(s > 0 && "size must be positive!");
2119 assert(!initialized());
2121 ptr = new char[_size * sizeof(Storage)];
2122 for (int i = 0; i < _size; ++i)
2123 new (ptr + i * sizeof(Storage)) Storage(params);
2126 int size() const { return _size; }
2128 Storage *data(int index, const Params ¶ms) {
2129 assert(initialized());
2130 assert(index >= 0 && index < size());
2131 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2136 //////////////////////////////////////////////////////////////////////
2138 // Visible Statistics Types
2140 //////////////////////////////////////////////////////////////////////
2142 * @defgroup VisibleStats "Statistic Types"
2143 * These are the statistics that are used in the simulator. By default these
2144 * store counters and don't use binning, but are templatized to accept any type
2145 * and any Bin class.
2150 * This is a simple scalar statistic, like a counter.
2151 * @sa Stat, ScalarBase, StatStor
2153 template <typename T = Counter, class Bin = NoBin>
2154 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
2157 /** The base implementation. */
2158 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
2161 * Sets the stat equal to the given value. Calls the base implementation
2163 * @param v The new value.
2165 template <typename U>
2166 void operator=(const U& v) { Base::operator=(v); }
2170 * A stat that calculates the per cycle average of a value.
2171 * @sa Stat, ScalarBase, AvgStor
2173 template <typename T = Counter, class Bin = NoBin>
2174 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
2177 /** The base implementation. */
2178 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
2181 * Sets the stat equal to the given value. Calls the base implementation
2183 * @param v The new value.
2185 template <typename U>
2186 void operator=(const U& v) { Base::operator=(v); }
2190 * A vector of scalar stats.
2191 * @sa Stat, VectorBase, StatStor
2193 template <typename T = Counter, class Bin = NoBin>
2194 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
2198 * A vector of Average stats.
2199 * @sa Stat, VectorBase, AvgStor
2201 template <typename T = Counter, class Bin = NoBin>
2202 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
2206 * A 2-Dimensional vecto of scalar stats.
2207 * @sa Stat, Vector2dBase, StatStor
2209 template <typename T = Counter, class Bin = NoBin>
2210 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
2214 * A simple distribution stat.
2215 * @sa Stat, DistBase, DistStor
2217 template <typename T = Counter, class Bin = NoBin>
2218 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
2221 /** Base implementation. */
2222 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2223 /** The Parameter type. */
2224 typedef typename Detail::DistStor<T>::Params Params;
2228 * Set the parameters of this distribution. @sa DistStor::Params
2229 * @param min The minimum value of the distribution.
2230 * @param max The maximum value of the distribution.
2231 * @param bkt The number of values in each bucket.
2232 * @return A reference to this distribution.
2234 Distribution &init(T min, T max, int bkt) {
2237 params.bucket_size = bkt;
2238 params.size = (max - min) / bkt + 1;
2247 * Calculates the mean and variance of all the samples.
2248 * @sa Stat, DistBase, FancyStor
2250 template <typename T = Counter, class Bin = NoBin>
2251 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
2254 /** The base implementation */
2255 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2256 /** The parameter type. */
2257 typedef typename Detail::DistStor<T>::Params Params;
2261 * Construct and initialize this distribution.
2263 StandardDeviation() {
2270 * Calculates the per cycle mean and variance of the samples.
2271 * @sa Stat, DistBase, AvgFancy
2273 template <typename T = Counter, class Bin = NoBin>
2274 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
2277 /** The base implementation */
2278 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2279 /** The parameter type. */
2280 typedef typename Detail::DistStor<T>::Params Params;
2284 * Construct and initialize this distribution.
2286 AverageDeviation() {
2293 * A vector of distributions.
2294 * @sa Stat, VectorDistBase, DistStor
2296 template <typename T = Counter, class Bin = NoBin>
2297 class VectorDistribution
2298 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
2301 /** The base implementation */
2302 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
2303 /** The parameter type. */
2304 typedef typename Detail::DistStor<T>::Params Params;
2308 * Initialize storage and parameters for this distribution.
2309 * @param size The size of the vector (the number of distributions).
2310 * @param min The minimum value of the distribution.
2311 * @param max The maximum value of the distribution.
2312 * @param bkt The number of values in each bucket.
2313 * @return A reference to this distribution.
2315 VectorDistribution &init(int size, T min, T max, int bkt) {
2318 params.bucket_size = bkt;
2319 params.size = (max - min) / bkt + 1;
2320 bin.init(size, params);
2328 * This is a vector of StandardDeviation stats.
2329 * @sa Stat, VectorDistBase, FancyStor
2331 template <typename T = Counter, class Bin = NoBin>
2332 class VectorStandardDeviation
2333 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
2336 /** The base implementation */
2337 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
2338 /** The parameter type. */
2339 typedef typename Detail::DistStor<T>::Params Params;
2343 * Initialize storage for this distribution.
2344 * @param size The size of the vector.
2345 * @return A reference to this distribution.
2347 VectorStandardDeviation &init(int size) {
2348 bin.init(size, params);
2356 * This is a vector of AverageDeviation stats.
2357 * @sa Stat, VectorDistBase, AvgFancy
2359 template <typename T = Counter, class Bin = NoBin>
2360 class VectorAverageDeviation
2361 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
2364 /** The base implementation */
2365 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
2366 /** The parameter type. */
2367 typedef typename Detail::DistStor<T>::Params Params;
2371 * Initialize storage for this distribution.
2372 * @param size The size of the vector.
2373 * @return A reference to this distribution.
2375 VectorAverageDeviation &init(int size) {
2376 bin.init(size, params);
2384 * A formula for statistics that is calculated when printed. A formula is
2385 * stored as a tree of Nodes that represent the equation to calculate.
2386 * @sa Stat, ScalarStat, VectorStat, Node, Detail::Temp
2388 class Formula : public Detail::VectorStat
2391 /** The root of the tree which represents the Formula */
2392 Detail::NodePtr root;
2393 friend class Statistics::Detail::Temp;
2397 * Create and initialize thie formula, and register it with the database.
2399 Formula() : VectorStat(true) { setInit(); }
2401 * Create a formula with the given root node, register it with the
2403 * @param r The root of the expression tree.
2405 Formula(Detail::Temp r) : VectorStat(true) {
2411 * Set an unitialized Formula to the given root.
2412 * @param r The root of the expression tree.
2413 * @return a reference to this formula.
2415 const Formula &operator=(Detail::Temp r) {
2416 assert(!root && "Can't change formulas");
2423 * Add the given tree to the existing one.
2424 * @param r The root of the expression tree.
2425 * @return a reference to this formula.
2427 const Formula &operator+=(Detail::Temp r) {
2428 using namespace Detail;
2430 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
2438 * Return the vector of values of this formula.
2439 * @return The result vector.
2441 const rvec_t &val() const { return root->val(); }
2443 * Return the total of the result vector.
2444 * @return The total of the result vector.
2446 result_t total() const { return root->total(); }
2449 * Return the number of elements in the tree.
2451 size_t size() const {
2455 return root->size();
2464 void dump(std::ostream &stream);
2467 operator+(Detail::Temp l, Detail::Temp r)
2469 using namespace Detail;
2470 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
2474 operator-(Detail::Temp l, Detail::Temp r)
2476 using namespace Detail;
2477 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
2481 operator*(Detail::Temp l, Detail::Temp r)
2483 using namespace Detail;
2484 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
2488 operator/(Detail::Temp l, Detail::Temp r)
2490 using namespace Detail;
2491 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
2495 operator%(Detail::Temp l, Detail::Temp r)
2497 using namespace Detail;
2498 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
2502 operator-(Detail::Temp l)
2504 using namespace Detail;
2505 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
2508 template <typename T>
2512 using namespace Detail;
2513 return NodePtr(new ConstNode<T>(val));
2516 template <typename T>
2520 using namespace Detail;
2521 return NodePtr(new FunctorNode<T>(val));
2524 template <typename T>
2528 using namespace Detail;
2529 return NodePtr(new ScalarNode<T>(val));
2533 sum(Detail::Temp val)
2535 using namespace Detail;
2536 return NodePtr(new SumNode<std::plus<result_t> >(val));
2539 extern bool PrintDescriptions;
2541 } // namespace statistics
2543 #endif // __STATISTICS_HH__