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 class ScalarProxy<T, Storage, Bin>;
723 * Return a reference (ScalarProxy) to the stat at the given index.
724 * @param index The vector index to access.
725 * @return A reference of the stat.
727 ScalarProxy<T, Storage, Bin> operator[](int index);
730 * Return the number of elements in this vector.
731 * @return The size of the vector.
733 virtual size_t size() const { return bin.size(); }
737 * A proxy class to access the stat at a given index in a VectorBase stat.
738 * Behaves like a ScalarBase.
740 template <typename T, template <typename T> class Storage, class Bin>
741 class ScalarProxy : public ScalarStat
744 /** Define the type of the storage class. */
745 typedef Storage<T> storage_t;
746 /** Define the params of the storage class. */
747 typedef typename storage_t::Params params_t;
748 /** Define the bin type. */
749 typedef typename Bin::VectorBin<storage_t> bin_t;
752 /** Pointer to the bin in the parent VectorBase. */
754 /** Pointer to the params in the parent VectorBase. */
756 /** The index to access in the parent VectorBase. */
761 * Retrieve the storage from the bin.
762 * @return The storage from the bin for this stat.
764 storage_t *data() { return bin->data(index, *params); }
766 * Retrieve a const pointer to the storage from the bin.
767 * @return A const pointer to the storage for this stat.
769 const storage_t *data() const { return bin->data(index, *params); }
773 * Return the current value of this statas a result type.
774 * @return The current value.
776 result_t val() const { return data()->val(*params); }
778 * Return the current value of this stat as its base type.
779 * @return The current value.
781 T value() const { return data()->value(*params); }
785 * Create and initialize this proxy, do not register it with the database.
786 * @param b The bin to use.
787 * @param p The params to use.
788 * @param i The index to access.
790 ScalarProxy(bin_t &b, params_t &p, int i)
791 : ScalarStat(false), bin(&b), params(&p), index(i) {}
793 * Create a copy of the provided ScalarProxy.
794 * @param sp The proxy to copy.
796 ScalarProxy(const ScalarProxy &sp)
797 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
799 * Set this proxy equal to the provided one.
800 * @param sp The proxy to copy.
801 * @return A reference to this proxy.
803 const ScalarProxy &operator=(const ScalarProxy &sp) {
811 // Common operators for stats
813 * Increment the stat by 1. This calls the associated storage object inc
816 void operator++() { data()->inc(1, *params); }
818 * Decrement the stat by 1. This calls the associated storage object dec
821 void operator--() { data()->dec(1, *params); }
823 /** Increment the stat by 1. */
824 void operator++(int) { ++*this; }
825 /** Decrement the stat by 1. */
826 void operator--(int) { --*this; }
829 * Set the data value to the given value. This calls the associated storage
830 * object set function.
831 * @param v The new value.
833 template <typename U>
834 void operator=(const U& v) { data()->set(v, *params); }
837 * Increment the stat by the given value. This calls the associated
838 * storage object inc function.
839 * @param v The value to add.
841 template <typename U>
842 void operator+=(const U& v) { data()->inc(v, *params); }
845 * Decrement the stat by the given value. This calls the associated
846 * storage object dec function.
847 * @param v The value to substract.
849 template <typename U>
850 void operator-=(const U& v) { data()->dec(v, *params); }
853 * Return the number of elements, always 1 for a scalar.
856 virtual size_t size() const { return 1; }
859 template <typename T, template <typename T> class Storage, class Bin>
860 inline ScalarProxy<T, Storage, Bin>
861 VectorBase<T, Storage, Bin>::operator[](int index)
863 assert (index >= 0 && index < size());
864 return ScalarProxy<T, Storage, Bin>(bin, params, index);
867 template <typename T, template <typename T> class Storage, class Bin>
870 template <typename T, template <typename T> class Storage, class Bin>
871 class Vector2dBase : public Stat
874 typedef Storage<T> storage_t;
875 typedef typename storage_t::Params params_t;
876 typedef typename Bin::VectorBin<storage_t> bin_t;
883 std::vector<std::string> *y_subnames;
886 storage_t *data(int index) { return bin.data(index, params); }
887 const storage_t *data(int index) const {
888 return (const_cast<bin_t *>(&bin))->data(index, params);
892 // Copying stats is not allowed
893 Vector2dBase(const Vector2dBase &stat);
894 const Vector2dBase &operator=(const Vector2dBase &);
897 Vector2dBase() : Stat(true) {}
900 Vector2dBase &init(size_t _x, size_t _y) {
903 bin.init(x * y, params);
905 y_subnames = new std::vector<std::string>(y);
911 * @warning This makes the assumption that if you're gonna subnames a 2d
912 * vector, you're subnaming across all y
914 Vector2dBase &ysubnames(const char **names)
916 for (int i=0; i < y; ++i) {
917 (*y_subnames)[i] = names[i];
921 Vector2dBase &ysubname(int index, const std::string subname)
923 (*y_subnames)[i] = subname.c_str();
926 std::string ysubname(int i) const { return (*y_subnames)[i]; }
928 friend class VectorProxy<T, Storage, Bin>;
929 VectorProxy<T, Storage, Bin> operator[](int index);
931 virtual size_t size() const { return bin.size(); }
932 virtual bool zero() const { return data(0)->value(params) == 0.0; }
935 display(std::ostream &out) const
937 bool have_subname = false;
938 for (int i = 0; i < x; ++i) {
939 if (!mysubname(i).empty())
944 result_t super_total = 0.0;
945 for (int i = 0; i < x; ++i) {
948 subname = mysubname(i);
952 subname = to_string(i);
957 result_t total = 0.0;
958 for (int j = 0; j < y; ++j) {
959 vec[j] = data(iy + j)->val(params);
960 tot_vec[j] += vec[j];
962 super_total += vec[j];
966 if (mysubdesc(i).empty()) {
972 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
973 myprecision(), myflags(), vec, total);
976 if ((myflags() & ::Statistics::total) && (x > 1)) {
977 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
978 myprecision(), myflags(), tot_vec, super_total);
984 template <typename T, template <typename T> class Storage, class Bin>
985 class VectorProxy : public VectorStat
988 typedef Storage<T> storage_t;
989 typedef typename storage_t::Params params_t;
990 typedef typename Bin::VectorBin<storage_t> bin_t;
1001 storage_t *data(int index) {
1002 assert(index < len);
1003 return bin->data(offset + index, *params);
1006 const storage_t *data(int index) const {
1007 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
1011 const rvec_t &val() const {
1013 vec->resize(size());
1015 vec = new rvec_t(size());
1017 for (int i = 0; i < size(); ++i)
1018 (*vec)[i] = data(i)->val(*params);
1023 result_t total() const {
1024 result_t total = 0.0;
1025 for (int i = 0; i < size(); ++i)
1026 total += data(i)->val(*params);
1031 VectorProxy(bin_t &b, params_t &p, int o, int l)
1032 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
1034 VectorProxy(const VectorProxy &sp)
1035 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
1036 len(sp.len), vec(NULL)
1043 const VectorProxy &operator=(const VectorProxy &sp) {
1054 virtual size_t size() const { return len; }
1056 ScalarProxy<T, Storage, Bin> operator[](int index) {
1057 assert (index >= 0 && index < size());
1058 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
1062 template <typename T, template <typename T> class Storage, class Bin>
1063 inline VectorProxy<T, Storage, Bin>
1064 Vector2dBase<T, Storage, Bin>::operator[](int index)
1066 int offset = index * y;
1067 assert (index >= 0 && offset < size());
1068 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
1071 //////////////////////////////////////////////////////////////////////
1073 // Non formula statistics
1075 //////////////////////////////////////////////////////////////////////
1077 void DistDisplay(std::ostream &stream, const std::string &name,
1078 const std::string &desc, int precision, FormatFlags flags,
1079 result_t min_val, result_t max_val,
1080 result_t underflow, result_t overflow,
1081 const rvec_t &vec, int min, int max, int bucket_size,
1084 * Templatized storage and interface for a distrbution stat.
1086 template <typename T>
1090 /** The parameters for a distribution stat. */
1093 /** The minimum value to track. */
1095 /** The maximum value to track. */
1097 /** The number of entries in each bucket. */
1099 /** The number of buckets. Equal to (max-min)/bucket_size. */
1104 /** The smallest value sampled. */
1106 /** The largest value sampled. */
1108 /** The number of values sampled less than min. */
1110 /** The number of values sampled more than max. */
1112 /** Counter for each bucket. */
1117 * Construct this storage with the supplied params.
1118 * @param params The parameters.
1120 DistStor(const Params ¶ms)
1121 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1125 * Add a value to the distribution for the given number of times.
1126 * @param val The value to add.
1127 * @param number The number of times to add the value.
1128 * @param params The paramters of the distribution.
1130 void sample(T val, int number, const Params ¶ms) {
1131 if (val < params.min)
1132 underflow += number;
1133 else if (val > params.max)
1136 int index = (val - params.min) / params.bucket_size;
1137 assert(index < size(params));
1138 vec[index] += number;
1149 * Return the number of buckets in this distribution.
1150 * @return the number of buckets.
1151 * @todo Is it faster to return the size from the parameters?
1153 size_t size(const Params &) const { return vec.size(); }
1156 * Returns true if any calls to sample have been made.
1157 * @param params The paramters of the distribution.
1158 * @return True if any values have been sampled.
1160 bool zero(const Params ¶ms) const {
1161 if (underflow != 0 || overflow != 0)
1164 int s = size(params);
1165 for (int i = 0; i < s; i++)
1173 * Print this distribution and the given print data to the given ostream.
1174 * @param stream The output stream.
1175 * @param name The name of this stat (from StatData).
1176 * @param desc The description of this stat (from StatData).
1177 * @param precision The print precision (from StatData).
1178 * @param flags The format flags (from StatData).
1179 * @param params The paramters of this distribution.
1181 void display(std::ostream &stream, const std::string &name,
1182 const std::string &desc, int precision, FormatFlags flags,
1183 const Params ¶ms) const {
1185 #ifdef STAT_DISPLAY_COMPAT
1186 result_t min = params.min;
1188 result_t min = (min_val == INT_MAX) ? params.min : min_val;
1190 result_t max = (max_val == INT_MIN) ? 0 : max_val;
1192 rvec_t rvec(params.size);
1193 for (int i = 0; i < params.size; ++i)
1196 DistDisplay(stream, name, desc, precision, flags,
1197 (result_t)min, (result_t)max,
1198 (result_t)underflow, (result_t)overflow,
1199 rvec, params.min, params.max, params.bucket_size,
1204 void FancyDisplay(std::ostream &stream, const std::string &name,
1205 const std::string &desc, int precision, FormatFlags flags,
1206 result_t mean, result_t variance);
1209 * Templatized storage and interface for a distribution that calculates mean
1212 template <typename T>
1217 * No paramters for this storage.
1222 /** The current sum. */
1224 /** The sum of squares. */
1226 /** The total number of samples. */
1231 * Create and initialize this storage.
1233 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
1236 * Add a value the given number of times to this running average.
1237 * Update the running sum and sum of squares, increment the number of
1238 * values seen by the given number.
1239 * @param val The value to add.
1240 * @param number The number of times to add the value.
1241 * @param p The parameters of this stat.
1243 void sample(T val, int number, const Params &p) {
1244 T value = val * number;
1246 squares += value * value;
1251 * Print this distribution and the given print data to the given ostream.
1252 * @param stream The output stream.
1253 * @param name The name of this stat (from StatData).
1254 * @param desc The description of this stat (from StatData).
1255 * @param precision The print precision (from StatData).
1256 * @param flags The format flags (from StatData).
1257 * @param params The paramters of this distribution.
1259 void display(std::ostream &stream, const std::string &name,
1260 const std::string &desc, int precision, FormatFlags flags,
1261 const Params ¶ms) const {
1263 result_t mean = NAN;
1264 result_t variance = NAN;
1267 result_t fsum = sum;
1268 result_t fsq = squares;
1269 result_t ftot = total;
1272 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
1275 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1279 * Return the number of entries in this stat, 1
1282 size_t size(const Params &) const { return 1; }
1284 * Return true if no samples have been added.
1285 * @return True if no samples have been added.
1287 bool zero(const Params &) const { return total == 0; }
1291 * Templatized storage for distribution that calculates per cycle mean and
1294 template <typename T>
1298 /** No parameters for this storage. */
1302 /** Current total. */
1304 /** Current sum of squares. */
1309 * Create and initialize this storage.
1311 AvgFancy(const Params &) : sum(0), squares(0) {}
1314 * Add a value to the distribution for the given number of times.
1315 * Update the running sum and sum of squares.
1316 * @param val The value to add.
1317 * @param number The number of times to add the value.
1318 * @param p The paramters of the distribution.
1320 void sample(T val, int number, const Params& p) {
1321 T value = val * number;
1323 squares += value * value;
1327 * Print this distribution and the given print data to the given ostream.
1328 * @param stream The output stream.
1329 * @param name The name of this stat (from StatData).
1330 * @param desc The description of this stat (from StatData).
1331 * @param precision The print precision (from StatData).
1332 * @param flags The format flags (from StatData).
1333 * @param params The paramters of this distribution.
1335 void display(std::ostream &stream, const std::string &name,
1336 const std::string &desc, int precision, FormatFlags flags,
1337 const Params ¶ms) const {
1338 result_t mean = sum / curTick;
1339 result_t variance = (squares - sum * sum) / curTick;
1341 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1345 * Return the number of entries, in this case 1.
1348 size_t size(const Params ¶ms) const { return 1; }
1350 * Return true if no samples have been added.
1351 * @return True if the sum is zero.
1353 bool zero(const Params ¶ms) const { return sum == 0; }
1357 * Implementation of a distribution stat. The type of distribution is
1358 * determined by the Storage template. @sa ScalarBase
1360 template <typename T, template <typename T> class Storage, class Bin>
1361 class DistBase : public Stat
1364 /** Define the type of the storage class. */
1365 typedef Storage<T> storage_t;
1366 /** Define the params of the storage class. */
1367 typedef typename storage_t::Params params_t;
1368 /** Define the bin type. */
1369 typedef typename Bin::Bin<storage_t> bin_t;
1372 /** The bin of this stat. */
1374 /** The parameters for this stat. */
1379 * Retrieve the storage from the bin.
1380 * @return The storage object for this stat.
1382 storage_t *data() { return bin.data(params); }
1384 * Retrieve a const pointer to the storage from the bin.
1385 * @return A const pointer to the storage object for this stat.
1387 const storage_t *data() const {
1388 return (const_cast<bin_t *>(&bin))->data(params);
1392 // Copying stats is not allowed
1393 /** Copies are not allowed. */
1394 DistBase(const DistBase &stat);
1395 /** Copies are not allowed. */
1396 const DistBase &operator=(const DistBase &);
1400 * Create this distrubition and register it with the database.
1402 DistBase() : Stat(true) { }
1409 * Add a value to the distribtion n times. Calls sample on the storage
1411 * @param v The value to add.
1412 * @param n The number of times to add it, defaults to 1.
1414 template <typename U>
1415 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1418 * Return the number of entries in this stat.
1419 * @return The number of entries.
1421 virtual size_t size() const { return data()->size(params); }
1423 * Return true if no samples have been added.
1424 * @return True if there haven't been any samples.
1426 virtual bool zero() const { return data()->zero(params); }
1428 * Print this distribution to the given ostream.
1429 * @param stream The output stream.
1431 virtual void display(std::ostream &stream) const {
1432 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
1437 template <typename T, template <typename T> class Storage, class Bin>
1438 class VectorDistProxy;
1440 template <typename T, template <typename T> class Storage, class Bin>
1441 class VectorDistBase : public Stat
1444 typedef Storage<T> storage_t;
1445 typedef typename storage_t::Params params_t;
1446 typedef typename Bin::VectorBin<storage_t> bin_t;
1453 storage_t *data(int index) { return bin.data(index, params); }
1454 const storage_t *data(int index) const {
1455 return (const_cast<bin_t *>(&bin))->data(index, params);
1459 // Copying stats is not allowed
1460 VectorDistBase(const VectorDistBase &stat);
1461 const VectorDistBase &operator=(const VectorDistBase &);
1464 VectorDistBase() : Stat(true) { }
1465 ~VectorDistBase() { }
1467 friend class VectorDistProxy<T, Storage, Bin>;
1468 VectorDistProxy<T, Storage, Bin> operator[](int index);
1469 const VectorDistProxy<T, Storage, Bin> operator[](int index) const;
1471 virtual size_t size() const { return bin.size(); }
1472 virtual bool zero() const { return false; }
1473 virtual void display(std::ostream &stream) const;
1476 template <typename T, template <typename T> class Storage, class Bin>
1477 class VectorDistProxy : public Stat
1480 typedef Storage<T> storage_t;
1481 typedef typename storage_t::Params params_t;
1482 typedef typename Bin::Bin<storage_t> bin_t;
1483 typedef VectorDistBase<T, Storage, Bin> base_t;
1488 const base_t *cstat;
1493 storage_t *data() { return stat->data(index); }
1494 const storage_t *data() const { return cstat->data(index); }
1497 VectorDistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1498 : Stat(false), cstat(&s), index(i) {}
1499 VectorDistProxy(const VectorDistProxy &sp)
1500 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1501 const VectorDistProxy &operator=(const VectorDistProxy &sp) {
1502 cstat = sp.cstat; index = sp.index; return *this;
1506 template <typename U>
1507 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1509 virtual size_t size() const { return 1; }
1510 virtual bool zero() const {
1511 return data()->zero(cstat->params);
1513 virtual void display(std::ostream &stream) const {
1514 std::stringstream name, desc;
1516 if (!(cstat->mysubname(index).empty())) {
1517 name << cstat->myname() << cstat->mysubname(index);
1519 name << cstat->myname() << "_" << index;
1521 if (!(cstat->mysubdesc(index).empty())) {
1522 desc << cstat->mysubdesc(index);
1524 desc << cstat->mydesc();
1527 data()->display(stream, name.str(), desc.str(),
1528 cstat->myprecision(), cstat->myflags(), cstat->params);
1532 template <typename T, template <typename T> class Storage, class Bin>
1533 inline VectorDistProxy<T, Storage, Bin>
1534 VectorDistBase<T, Storage, Bin>::operator[](int index)
1536 assert (index >= 0 && index < size());
1537 return VectorDistProxy<T, Storage, Bin>(*this, index);
1540 template <typename T, template <typename T> class Storage, class Bin>
1541 inline const VectorDistProxy<T, Storage, Bin>
1542 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1544 assert (index >= 0 && index < size());
1545 return VectorDistProxy<T, Storage, Bin>(*this, index);
1549 * @todo Need a way to print Distribution totals across the Vector
1551 template <typename T, template <typename T> class Storage, class Bin>
1553 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1555 for (int i = 0; i < size(); ++i) {
1556 VectorDistProxy<T, Storage, Bin> proxy(*this, i);
1557 proxy.display(stream);
1563 VectorDistBase<T, Storage, Bin>::total(int index) const
1566 for (int i=0; i < x_size(); ++i) {
1567 total += data(i)->val(*params);
1572 //////////////////////////////////////////////////////////////////////
1576 //////////////////////////////////////////////////////////////////////
1579 * Base class for formula statistic node. These nodes are used to build a tree
1580 * that represents the formula.
1582 class Node : public RefCounted
1586 * Return the number of nodes in the subtree starting at this node.
1587 * @return the number of nodes in this subtree.
1589 virtual size_t size() const = 0;
1591 * Return the result vector of this subtree.
1592 * @return The result vector of this subtree.
1594 virtual const rvec_t &val() const = 0;
1596 * Return the total of the result vector.
1597 * @return The total of the result vector.
1599 virtual result_t total() const = 0;
1602 /** Reference counting pointer to a function Node. */
1603 typedef RefCountingPtr<Node> NodePtr;
1605 class ScalarStatNode : public Node
1608 const ScalarStat &stat;
1609 mutable rvec_t result;
1612 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1613 const rvec_t &val() const { result[0] = stat.val(); return result; }
1614 virtual result_t total() const { return stat.val(); };
1616 virtual size_t size() const { return 1; }
1619 template <typename T, template <typename T> class Storage, class Bin>
1620 class ScalarProxyNode : public Node
1623 const ScalarProxy<T, Storage, Bin> proxy;
1624 mutable rvec_t result;
1627 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1628 : proxy(p), result(1) { }
1629 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1630 virtual result_t total() const { return proxy.val(); };
1632 virtual size_t size() const { return 1; }
1635 class VectorStatNode : public Node
1638 const VectorStat &stat;
1641 VectorStatNode(const VectorStat &s) : stat(s) {}
1642 const rvec_t &val() const { return stat.val(); }
1643 virtual result_t total() const { return stat.total(); };
1645 virtual size_t size() const { return stat.size(); }
1648 template <typename T>
1649 class ConstNode : public Node
1655 ConstNode(T s) : data(1, (result_t)s) {}
1656 const rvec_t &val() const { return data; }
1657 virtual result_t total() const { return data[0]; };
1659 virtual size_t size() const { return 1; }
1662 template <typename T>
1663 class FunctorNode : public Node
1667 mutable rvec_t result;
1670 FunctorNode(T &f) : functor(f) { result.resize(1); }
1671 const rvec_t &val() const {
1672 result[0] = (result_t)functor();
1675 virtual result_t total() const { return (result_t)functor(); };
1677 virtual size_t size() const { return 1; }
1680 template <typename T>
1681 class ScalarNode : public Node
1685 mutable rvec_t result;
1688 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1689 const rvec_t &val() const {
1690 result[0] = (result_t)scalar;
1693 virtual result_t total() const { return (result_t)scalar; };
1695 virtual size_t size() const { return 1; }
1699 class UnaryNode : public Node
1703 mutable rvec_t result;
1706 UnaryNode(NodePtr p) : l(p) {}
1708 const rvec_t &val() const {
1709 const rvec_t &lvec = l->val();
1710 int size = lvec.size();
1714 result.resize(size);
1716 for (int i = 0; i < size; ++i)
1717 result[i] = op(lvec[i]);
1722 result_t total() const {
1724 return op(l->total());
1727 virtual size_t size() const { return l->size(); }
1731 class BinaryNode : public Node
1736 mutable rvec_t result;
1739 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1741 const rvec_t &val() const {
1743 const rvec_t &lvec = l->val();
1744 const rvec_t &rvec = r->val();
1746 assert(lvec.size() > 0 && rvec.size() > 0);
1748 if (lvec.size() == 1 && rvec.size() == 1) {
1750 result[0] = op(lvec[0], rvec[0]);
1751 } else if (lvec.size() == 1) {
1752 int size = rvec.size();
1753 result.resize(size);
1754 for (int i = 0; i < size; ++i)
1755 result[i] = op(lvec[0], rvec[i]);
1756 } else if (rvec.size() == 1) {
1757 int size = lvec.size();
1758 result.resize(size);
1759 for (int i = 0; i < size; ++i)
1760 result[i] = op(lvec[i], rvec[0]);
1761 } else if (rvec.size() == lvec.size()) {
1762 int size = rvec.size();
1763 result.resize(size);
1764 for (int i = 0; i < size; ++i)
1765 result[i] = op(lvec[i], rvec[i]);
1771 result_t total() const {
1773 return op(l->total(), r->total());
1776 virtual size_t size() const {
1784 assert(ls == rs && "Node vector sizes are not equal");
1791 class SumNode : public Node
1795 mutable rvec_t result;
1798 SumNode(NodePtr p) : l(p), result(1) {}
1800 const rvec_t &val() const {
1801 const rvec_t &lvec = l->val();
1802 int size = lvec.size();
1808 for (int i = 0; i < size; ++i)
1809 result[0] = op(result[0], lvec[i]);
1814 result_t total() const {
1815 const rvec_t &lvec = l->val();
1816 int size = lvec.size();
1819 result_t result = 0.0;
1822 for (int i = 0; i < size; ++i)
1823 result = op(result, lvec[i]);
1828 virtual size_t size() const { return 1; }
1832 * Helper class to construct formula node trees.
1838 * Pointer to a Node object.
1844 * Copy the given pointer to this class.
1845 * @param n A pointer to a Node object to copy.
1847 Temp(NodePtr n) : node(n) {}
1849 * Create a new ScalarStatNode.
1850 * @param s The ScalarStat to place in a node.
1852 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1854 * Create a new ScalarProxyNode.
1855 * @param p The ScalarProxy to place in a node.
1857 template <typename T, template <typename T> class Storage, class Bin>
1858 Temp(const ScalarProxy<T, Storage, Bin> &p)
1859 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1861 * Create a new VectorStatNode.
1862 * @param s The VectorStat to place in a node.
1864 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1867 * Create a ConstNode
1868 * @param value The value of the const node.
1870 Temp(signed char value) : node(new ConstNode<signed char>(value)) {}
1872 * Create a ConstNode
1873 * @param value The value of the const node.
1875 Temp(unsigned char value) : node(new ConstNode<unsigned char>(value)) {}
1877 * Create a ConstNode
1878 * @param value The value of the const node.
1880 Temp(signed short value) : node(new ConstNode<signed short>(value)) {}
1882 * Create a ConstNode
1883 * @param value The value of the const node.
1885 Temp(unsigned short value) : node(new ConstNode<unsigned short>(value)) {}
1887 * Create a ConstNode
1888 * @param value The value of the const node.
1890 Temp(signed int value) : node(new ConstNode<signed int>(value)) {}
1892 * Create a ConstNode
1893 * @param value The value of the const node.
1895 Temp(unsigned int value) : node(new ConstNode<unsigned int>(value)) {}
1897 * Create a ConstNode
1898 * @param value The value of the const node.
1900 Temp(signed long value) : node(new ConstNode<signed long>(value)) {}
1902 * Create a ConstNode
1903 * @param value The value of the const node.
1905 Temp(unsigned long value) : node(new ConstNode<unsigned long>(value)) {}
1907 * Create a ConstNode
1908 * @param value The value of the const node.
1910 Temp(signed long long value)
1911 : node(new ConstNode<signed long long>(value)) {}
1913 * Create a ConstNode
1914 * @param value The value of the const node.
1916 Temp(unsigned long long value)
1917 : node(new ConstNode<unsigned long long>(value)) {}
1919 * Create a ConstNode
1920 * @param value The value of the const node.
1922 Temp(float value) : node(new ConstNode<float>(value)) {}
1924 * Create a ConstNode
1925 * @param value The value of the const node.
1927 Temp(double value) : node(new ConstNode<double>(value)) {}
1930 * Return the node pointer.
1931 * @return the node pointer.
1933 operator NodePtr() { return node;}
1937 //////////////////////////////////////////////////////////////////////
1939 // Binning Interface
1941 //////////////////////////////////////////////////////////////////////
1950 off_t size() const { return memsize; }
1954 BinBase(size_t size);
1958 } // namespace Detail
1960 template <class BinType>
1961 struct StatBin : public Detail::BinBase
1963 static StatBin *&curBin() {
1964 static StatBin *current = NULL;
1968 static void setCurBin(StatBin *bin) { curBin() = bin; }
1969 static StatBin *current() { assert(curBin()); return curBin(); }
1971 static off_t &offset() {
1972 static off_t offset = 0;
1976 static off_t new_offset(size_t size) {
1977 size_t mask = sizeof(u_int64_t) - 1;
1978 off_t off = offset();
1980 // That one is for the last trailing flags byte.
1981 offset() += (size + 1 + mask) & ~mask;
1986 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
1988 char *memory(off_t off) {
1989 assert(offset() <= size());
1990 return Detail::BinBase::memory() + off;
1993 static void activate(StatBin &bin) { setCurBin(&bin); }
2001 BinBase() : offset(-1) {}
2002 void allocate(size_t size) {
2003 offset = new_offset(size);
2006 assert(offset != -1);
2007 return current()->memory(offset);
2011 template <class Storage>
2012 class Bin : public BinBase
2015 typedef typename Storage::Params Params;
2018 Bin() { allocate(sizeof(Storage)); }
2019 bool initialized() const { return true; }
2020 void init(const Params ¶ms) { }
2022 int size() const { return 1; }
2024 Storage *data(const Params ¶ms) {
2025 assert(initialized());
2026 char *ptr = access();
2027 char *flags = ptr + sizeof(Storage);
2028 if (!(*flags & 0x1)) {
2030 new (ptr) Storage(params);
2032 return reinterpret_cast<Storage *>(ptr);
2036 template <class Storage>
2037 class VectorBin : public BinBase
2040 typedef typename Storage::Params Params;
2046 VectorBin() : _size(0) {}
2048 bool initialized() const { return _size > 0; }
2049 void init(int s, const Params ¶ms) {
2050 assert(!initialized());
2053 allocate(_size * sizeof(Storage));
2056 int size() const { return _size; }
2058 Storage *data(int index, const Params ¶ms) {
2059 assert(initialized());
2060 assert(index >= 0 && index < size());
2061 char *ptr = access();
2062 char *flags = ptr + size() * sizeof(Storage);
2063 if (!(*flags & 0x1)) {
2065 for (int i = 0; i < size(); ++i)
2066 new (ptr + i * sizeof(Storage)) Storage(params);
2068 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2073 class MainBinType {};
2074 typedef StatBin<MainBinType> MainBin;
2078 template <class Storage>
2082 typedef typename Storage::Params Params;
2085 char ptr[sizeof(Storage)];
2088 bool initialized() const { return true; }
2089 void init(const Params ¶ms) {
2090 new (ptr) Storage(params);
2092 int size() const{ return 1; }
2093 Storage *data(const Params ¶ms) {
2094 assert(initialized());
2095 return reinterpret_cast<Storage *>(ptr);
2099 template <class Storage>
2103 typedef typename Storage::Params Params;
2110 VectorBin() : ptr(NULL) { }
2115 bool initialized() const { return ptr != NULL; }
2116 void init(int s, const Params ¶ms) {
2117 assert(s > 0 && "size must be positive!");
2118 assert(!initialized());
2120 ptr = new char[_size * sizeof(Storage)];
2121 for (int i = 0; i < _size; ++i)
2122 new (ptr + i * sizeof(Storage)) Storage(params);
2125 int size() const { return _size; }
2127 Storage *data(int index, const Params ¶ms) {
2128 assert(initialized());
2129 assert(index >= 0 && index < size());
2130 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2135 //////////////////////////////////////////////////////////////////////
2137 // Visible Statistics Types
2139 //////////////////////////////////////////////////////////////////////
2141 * @defgroup VisibleStats "Statistic Types"
2142 * These are the statistics that are used in the simulator. By default these
2143 * store counters and don't use binning, but are templatized to accept any type
2144 * and any Bin class.
2149 * This is a simple scalar statistic, like a counter.
2150 * @sa Stat, ScalarBase, StatStor
2152 template <typename T = Counter, class Bin = NoBin>
2153 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
2156 /** The base implementation. */
2157 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
2160 * Sets the stat equal to the given value. Calls the base implementation
2162 * @param v The new value.
2164 template <typename U>
2165 void operator=(const U& v) { Base::operator=(v); }
2169 * A stat that calculates the per cycle average of a value.
2170 * @sa Stat, ScalarBase, AvgStor
2172 template <typename T = Counter, class Bin = NoBin>
2173 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
2176 /** The base implementation. */
2177 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
2180 * Sets the stat equal to the given value. Calls the base implementation
2182 * @param v The new value.
2184 template <typename U>
2185 void operator=(const U& v) { Base::operator=(v); }
2189 * A vector of scalar stats.
2190 * @sa Stat, VectorBase, StatStor
2192 template <typename T = Counter, class Bin = NoBin>
2193 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
2197 * A vector of Average stats.
2198 * @sa Stat, VectorBase, AvgStor
2200 template <typename T = Counter, class Bin = NoBin>
2201 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
2205 * A 2-Dimensional vecto of scalar stats.
2206 * @sa Stat, Vector2dBase, StatStor
2208 template <typename T = Counter, class Bin = NoBin>
2209 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
2213 * A simple distribution stat.
2214 * @sa Stat, DistBase, DistStor
2216 template <typename T = Counter, class Bin = NoBin>
2217 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
2220 /** Base implementation. */
2221 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2222 /** The Parameter type. */
2223 typedef typename Detail::DistStor<T>::Params Params;
2227 * Set the parameters of this distribution. @sa DistStor::Params
2228 * @param min The minimum value of the distribution.
2229 * @param max The maximum value of the distribution.
2230 * @param bkt The number of values in each bucket.
2231 * @return A reference to this distribution.
2233 Distribution &init(T min, T max, int bkt) {
2236 params.bucket_size = bkt;
2237 params.size = (max - min) / bkt + 1;
2246 * Calculates the mean and variance of all the samples.
2247 * @sa Stat, DistBase, FancyStor
2249 template <typename T = Counter, class Bin = NoBin>
2250 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
2253 /** The base implementation */
2254 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2255 /** The parameter type. */
2256 typedef typename Detail::DistStor<T>::Params Params;
2260 * Construct and initialize this distribution.
2262 StandardDeviation() {
2269 * Calculates the per cycle mean and variance of the samples.
2270 * @sa Stat, DistBase, AvgFancy
2272 template <typename T = Counter, class Bin = NoBin>
2273 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
2276 /** The base implementation */
2277 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2278 /** The parameter type. */
2279 typedef typename Detail::DistStor<T>::Params Params;
2283 * Construct and initialize this distribution.
2285 AverageDeviation() {
2292 * A vector of distributions.
2293 * @sa Stat, VectorDistBase, DistStor
2295 template <typename T = Counter, class Bin = NoBin>
2296 class VectorDistribution
2297 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
2300 /** The base implementation */
2301 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
2302 /** The parameter type. */
2303 typedef typename Detail::DistStor<T>::Params Params;
2307 * Initialize storage and parameters for this distribution.
2308 * @param size The size of the vector (the number of distributions).
2309 * @param min The minimum value of the distribution.
2310 * @param max The maximum value of the distribution.
2311 * @param bkt The number of values in each bucket.
2312 * @return A reference to this distribution.
2314 VectorDistribution &init(int size, T min, T max, int bkt) {
2317 params.bucket_size = bkt;
2318 params.size = (max - min) / bkt + 1;
2319 bin.init(size, params);
2327 * This is a vector of StandardDeviation stats.
2328 * @sa Stat, VectorDistBase, FancyStor
2330 template <typename T = Counter, class Bin = NoBin>
2331 class VectorStandardDeviation
2332 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
2335 /** The base implementation */
2336 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
2337 /** The parameter type. */
2338 typedef typename Detail::DistStor<T>::Params Params;
2342 * Initialize storage for this distribution.
2343 * @param size The size of the vector.
2344 * @return A reference to this distribution.
2346 VectorStandardDeviation &init(int size) {
2347 bin.init(size, params);
2355 * This is a vector of AverageDeviation stats.
2356 * @sa Stat, VectorDistBase, AvgFancy
2358 template <typename T = Counter, class Bin = NoBin>
2359 class VectorAverageDeviation
2360 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
2363 /** The base implementation */
2364 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
2365 /** The parameter type. */
2366 typedef typename Detail::DistStor<T>::Params Params;
2370 * Initialize storage for this distribution.
2371 * @param size The size of the vector.
2372 * @return A reference to this distribution.
2374 VectorAverageDeviation &init(int size) {
2375 bin.init(size, params);
2383 * A formula for statistics that is calculated when printed. A formula is
2384 * stored as a tree of Nodes that represent the equation to calculate.
2385 * @sa Stat, ScalarStat, VectorStat, Node, Detail::Temp
2387 class Formula : public Detail::VectorStat
2390 /** The root of the tree which represents the Formula */
2391 Detail::NodePtr root;
2392 friend class Statistics::Detail::Temp;
2396 * Create and initialize thie formula, and register it with the database.
2398 Formula() : VectorStat(true) { setInit(); }
2400 * Create a formula with the given root node, register it with the
2402 * @param r The root of the expression tree.
2404 Formula(Detail::Temp r) : VectorStat(true) {
2410 * Set an unitialized Formula to the given root.
2411 * @param r The root of the expression tree.
2412 * @return a reference to this formula.
2414 const Formula &operator=(Detail::Temp r) {
2415 assert(!root && "Can't change formulas");
2422 * Add the given tree to the existing one.
2423 * @param r The root of the expression tree.
2424 * @return a reference to this formula.
2426 const Formula &operator+=(Detail::Temp r) {
2427 using namespace Detail;
2429 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
2437 * Return the vector of values of this formula.
2438 * @return The result vector.
2440 const rvec_t &val() const { return root->val(); }
2442 * Return the total of the result vector.
2443 * @return The total of the result vector.
2445 result_t total() const { return root->total(); }
2448 * Return the number of elements in the tree.
2450 size_t size() const {
2454 return root->size();
2463 void dump(std::ostream &stream);
2466 operator+(Detail::Temp l, Detail::Temp r)
2468 using namespace Detail;
2469 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
2473 operator-(Detail::Temp l, Detail::Temp r)
2475 using namespace Detail;
2476 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
2480 operator*(Detail::Temp l, Detail::Temp r)
2482 using namespace Detail;
2483 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
2487 operator/(Detail::Temp l, Detail::Temp r)
2489 using namespace Detail;
2490 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
2494 operator%(Detail::Temp l, Detail::Temp r)
2496 using namespace Detail;
2497 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
2501 operator-(Detail::Temp l)
2503 using namespace Detail;
2504 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
2507 template <typename T>
2511 using namespace Detail;
2512 return NodePtr(new ConstNode<T>(val));
2515 template <typename T>
2519 using namespace Detail;
2520 return NodePtr(new FunctorNode<T>(val));
2523 template <typename T>
2527 using namespace Detail;
2528 return NodePtr(new ScalarNode<T>(val));
2532 sum(Detail::Temp val)
2534 using namespace Detail;
2535 return NodePtr(new SumNode<std::plus<result_t> >(val));
2538 extern bool PrintDescriptions;
2540 } // namespace statistics
2542 #endif // __STATISTICS_HH__