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 weirdo-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;
230 virtual bool binned() const = 0;
233 * Set the name and marks this stat to print at the end of simulation.
234 * @param name The new name.
235 * @return A reference to this stat.
237 Stat &name(const std::string &name);
239 * Set the description and marks this stat to print at the end of
241 * @param desc The new description.
242 * @return A reference to this stat.
244 Stat &desc(const std::string &desc);
246 * Set the precision and marks this stat to print at the end of simulation.
247 * @param p The new precision
248 * @return A reference to this stat.
250 Stat &precision(int p);
252 * Set the flags and marks this stat to print at the end of simulation.
253 * @param f The new flags.
254 * @return A reference to this stat.
256 Stat &flags(FormatFlags f);
258 * Set the prerequisite stat and marks this stat to print at the end of
260 * @param prereq The prerequisite stat.
261 * @return A reference to this stat.
263 Stat &prereq(const Stat &prereq);
265 * Set the subfield name for the given index, and marks this stat to print
266 * at the end of simulation.
267 * @param index The subfield index.
268 * @param name The new name of the subfield.
269 * @return A reference to this stat.
271 Stat &subname(int index, const std::string &name);
273 * Set the subfield description for the given index and marks this stat to
274 * print at the end of simulation.
275 * @param index The subfield index.
276 * @param desc The new description of the subfield
277 * @return A reference to this stat.
279 Stat &subdesc(int index, const std::string &desc);
283 * Checks if the first stat's name is alphabetically less than the second.
284 * This function breaks names up at periods and considers each subname
286 * @param stat1 The first stat.
287 * @param stat2 The second stat.
288 * @return stat1's name is alphabetically before stat2's
290 static bool less(Stat *stat1, Stat *stat2);
293 /** A unique ID used for debugging. */
299 * Base class for all scalar stats. The class provides an interface to access
300 * the current value of the stat. This class can be used in formulas.
302 class ScalarStat : public Stat
306 * Create and perhaps register this stat with the database.
307 * @param reg If true, register this stat with the database.
309 ScalarStat(bool reg) : Stat(reg) {}
311 * Return the current value of this statistic as a result type.
312 * @return The current value of this statistic.
314 virtual result_t val() const = 0;
316 * Return true if this stat has value zero.
317 * @return True if this stat is zero.
319 virtual bool zero() const;
321 * Print this stat to the provided ostream.
322 * @param stream The output stream.
324 virtual void display(std::ostream &stream) const;
327 virtual bool binned() const = 0;
331 VectorDisplay(std::ostream &stream, const std::string &myname,
332 const std::vector<std::string> *mysubnames,
333 const std::string &mydesc,
334 const std::vector<std::string> *mysubdescs,
335 int myprecision, FormatFlags myflags, const rvec_t &vec,
339 * Base class for all vector stats. This class provides interfaces to access
340 * the current values of the stats as well as the totals. This class can be
343 class VectorStat : public Stat
347 * Create and perhaps register this stat with the database.
348 * @param reg If true, register this stat with the database.
350 VectorStat(bool reg) : Stat(reg) {}
352 * Return a vector of result typesd of all the values in the vector.
353 * @return The values of the vector.
355 virtual const rvec_t &val() const = 0;
357 * Return the total of all the entries in the vector.
358 * @return The total of the vector.
360 virtual result_t total() const = 0;
362 * Return true if this stat has value zero.
363 * @return True if this stat is zero.
365 virtual bool zero() const;
367 * Print this stat to the provided ostream.
368 * @param stream The output stream.
370 virtual void display(std::ostream &stream) const;
373 virtual bool binned() const = 0;
376 //////////////////////////////////////////////////////////////////////
380 //////////////////////////////////////////////////////////////////////
383 * Templatized storage and interface for a simple scalar stat.
385 template <typename T>
389 /** The paramaters for this storage type, none for a scalar. */
393 /** The statistic value. */
398 * Builds this storage element and calls the base constructor of the
401 StatStor(const Params &) : data(T()) {}
404 * The the stat to the given value.
405 * @param val The new value.
406 * @param p The paramters of this storage type.
408 void set(T val, const Params &p) { data = val; }
410 * Increment the stat by the given value.
411 * @param val The new value.
412 * @param p The paramters of this storage type.
414 void inc(T val, const Params &p) { data += val; }
416 * Decrement the stat by the given value.
417 * @param val The new value.
418 * @param p The paramters of this storage type.
420 void dec(T val, const Params &p) { data -= val; }
422 * Return the value of this stat as a result type.
423 * @param p The parameters of this storage type.
424 * @return The value of this stat.
426 result_t val(const Params &p) const { return (result_t)data; }
428 * Return the value of this stat as its base type.
429 * @param p The params of this storage type.
430 * @return The value of this stat.
432 T value(const Params &p) const { return data; }
436 * Templatized storage and interface to a per-cycle average stat. This keeps
437 * a current count and updates a total (count * cycles) when this count
438 * changes. This allows the quick calculation of a per cycle count of the item
439 * being watched. This is good for keeping track of residencies in structures
440 * among other things.
441 * @todo add lateny to the stat and fix binning.
443 template <typename T>
447 /** The paramaters for this storage type, none for this average. */
451 /** The current count. */
453 /** The total count for all cycles. */
454 mutable result_t total;
455 /** The cycle that current last changed. */
460 * Build and initializes this stat storage.
462 AvgStor(const Params &) : current(T()), total(0), last(0) { }
465 * Set the current count to the one provided, update the total and last
467 * @param val The new count.
468 * @param p The parameters for this storage.
470 void set(T val, const Params &p) {
471 total += current * (curTick - last);
476 * Increment the current count by the provided value, calls set.
477 * @param val The amount to increment.
478 * @param p The parameters for this storage.
480 void inc(T val, const Params &p) { set(current + val, p); }
482 * Deccrement the current count by the provided value, calls set.
483 * @param val The amount to decrement.
484 * @param p The parameters for this storage.
486 void dec(T val, const Params &p) { set(current - val, p); }
488 * Return the current average.
489 * @param p The parameters for this storage.
490 * @return The current average.
492 result_t val(const Params &p) const {
493 total += current * (curTick - last);
495 return (result_t)(total + current) / (result_t)(curTick + 1);
498 * Return the current count.
499 * @param p The parameters for this storage.
500 * @return The current count.
502 T value(const Params &p) const { return current; }
506 * Implementation of a scalar stat. The type of stat is determined by the
507 * Storage template. The storage for this stat is held within the Bin class.
508 * This allows for breaking down statistics across multiple bins easily.
510 template <typename T, template <typename T> class Storage, class Bin>
511 class ScalarBase : public ScalarStat
514 /** Define the type of the storage class. */
515 typedef Storage<T> storage_t;
516 /** Define the params of the storage class. */
517 typedef typename storage_t::Params params_t;
518 /** Define the bin type. */
519 typedef typename Bin::Bin<storage_t> bin_t;
522 /** The bin of this stat. */
524 /** The parameters for this stat. */
529 * Retrieve the storage from the bin.
530 * @return The storage object for this stat.
532 storage_t *data() { return bin.data(params); }
534 * Retrieve a const pointer to the storage from the bin.
535 * @return A const pointer to the storage object for this stat.
537 const storage_t *data() const {
538 return (const_cast<bin_t *>(&bin))->data(params);
543 * Copy constructor, copies are not allowed.
545 ScalarBase(const ScalarBase &stat);
549 const ScalarBase &operator=(const ScalarBase &);
553 * Return the current value of this stat as a result type.
554 * @return The current value.
556 result_t val() const { return data()->val(params); }
558 * Return the current value of this stat as its base type.
559 * @return The current value.
561 T value() const { return data()->value(params); }
565 * Create and initialize this stat, register it with the database.
567 ScalarBase() : ScalarStat(true) {
573 // Common operators for stats
575 * Increment the stat by 1. This calls the associated storage object inc
578 void operator++() { data()->inc(1, params); }
580 * Decrement the stat by 1. This calls the associated storage object dec
583 void operator--() { data()->dec(1, params); }
585 /** Increment the stat by 1. */
586 void operator++(int) { ++*this; }
587 /** Decrement the stat by 1. */
588 void operator--(int) { --*this; }
591 * Set the data value to the given value. This calls the associated storage
592 * object set function.
593 * @param v The new value.
595 template <typename U>
596 void operator=(const U& v) { data()->set(v, params); }
599 * Increment the stat by the given value. This calls the associated
600 * storage object inc function.
601 * @param v The value to add.
603 template <typename U>
604 void operator+=(const U& v) { data()->inc(v, params); }
607 * Decrement the stat by the given value. This calls the associated
608 * storage object dec function.
609 * @param v The value to substract.
611 template <typename U>
612 void operator-=(const U& v) { data()->dec(v, params); }
615 * Return the number of elements, always 1 for a scalar.
618 virtual size_t size() const { return 1; }
620 virtual bool binned() const { return bin_t::binned; }
623 //////////////////////////////////////////////////////////////////////
627 //////////////////////////////////////////////////////////////////////
628 template <typename T, template <typename T> class Storage, class Bin>
632 * Implementation of a vector of stats. The type of stat is determined by the
633 * Storage class. @sa ScalarBase
635 template <typename T, template <typename T> class Storage, class Bin>
636 class VectorBase : public VectorStat
639 /** Define the type of the storage class. */
640 typedef Storage<T> storage_t;
641 /** Define the params of the storage class. */
642 typedef typename storage_t::Params params_t;
643 /** Define the bin type. */
644 typedef typename Bin::VectorBin<storage_t> bin_t;
647 /** Local storage for the entry values, used for printing. */
651 /** The bin of this stat. */
653 /** The parameters for this stat. */
658 * Retrieve the storage from the bin for the given index.
659 * @param index The vector index to access.
660 * @return The storage object at the given index.
662 storage_t *data(int index) { return bin.data(index, params); }
664 * Retrieve a const pointer to the storage from the bin
665 * for the given index.
666 * @param index The vector index to access.
667 * @return A const pointer to the storage object at the given index.
669 const storage_t *data(int index) const {
670 return (const_cast<bin_t *>(&bin))->data(index, params);
674 // Copying stats is not allowed
675 /** Copying stats isn't allowed. */
676 VectorBase(const VectorBase &stat);
677 /** Copying stats isn't allowed. */
678 const VectorBase &operator=(const VectorBase &);
682 * Copy the values to a local vector and return a reference to it.
683 * @return A reference to a vector of the stat values.
685 const rvec_t &val() const {
689 vec = new rvec_t(size());
691 for (int i = 0; i < size(); ++i)
692 (*vec)[i] = data(i)->val(params);
698 * Return a total of all entries in this vector.
699 * @return The total of all vector entries.
701 result_t total() const {
702 result_t total = 0.0;
703 for (int i = 0; i < size(); ++i)
704 total += data(i)->val(params);
710 * Create this vector and register it with the database.
712 VectorBase() : VectorStat(true), vec(NULL) {}
716 ~VectorBase() { if (vec) delete vec; }
719 * Set this vector to have the given size.
720 * @param size The new size.
721 * @return A reference to this stat.
723 VectorBase &init(size_t size) {
724 bin.init(size, params);
730 /** Friend this class with the associated scalar proxy. */
731 friend class ScalarProxy<T, Storage, Bin>;
734 * Return a reference (ScalarProxy) to the stat at the given index.
735 * @param index The vector index to access.
736 * @return A reference of the stat.
738 ScalarProxy<T, Storage, Bin> operator[](int index);
741 * Return the number of elements in this vector.
742 * @return The size of the vector.
744 virtual size_t size() const { return bin.size(); }
746 virtual bool binned() const { return bin_t::binned; }
750 * A proxy class to access the stat at a given index in a VectorBase stat.
751 * Behaves like a ScalarBase.
753 template <typename T, template <typename T> class Storage, class Bin>
754 class ScalarProxy : public ScalarStat
757 /** Define the type of the storage class. */
758 typedef Storage<T> storage_t;
759 /** Define the params of the storage class. */
760 typedef typename storage_t::Params params_t;
761 /** Define the bin type. */
762 typedef typename Bin::VectorBin<storage_t> bin_t;
765 /** Pointer to the bin in the parent VectorBase. */
767 /** Pointer to the params in the parent VectorBase. */
769 /** The index to access in the parent VectorBase. */
774 * Retrieve the storage from the bin.
775 * @return The storage from the bin for this stat.
777 storage_t *data() { return bin->data(index, *params); }
779 * Retrieve a const pointer to the storage from the bin.
780 * @return A const pointer to the storage for this stat.
782 const storage_t *data() const { return bin->data(index, *params); }
786 * Return the current value of this statas a result type.
787 * @return The current value.
789 result_t val() const { return data()->val(*params); }
791 * Return the current value of this stat as its base type.
792 * @return The current value.
794 T value() const { return data()->value(*params); }
798 * Create and initialize this proxy, do not register it with the database.
799 * @param b The bin to use.
800 * @param p The params to use.
801 * @param i The index to access.
803 ScalarProxy(bin_t &b, params_t &p, int i)
804 : ScalarStat(false), bin(&b), params(&p), index(i) {}
806 * Create a copy of the provided ScalarProxy.
807 * @param sp The proxy to copy.
809 ScalarProxy(const ScalarProxy &sp)
810 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
812 * Set this proxy equal to the provided one.
813 * @param sp The proxy to copy.
814 * @return A reference to this proxy.
816 const ScalarProxy &operator=(const ScalarProxy &sp) {
824 // Common operators for stats
826 * Increment the stat by 1. This calls the associated storage object inc
829 void operator++() { data()->inc(1, *params); }
831 * Decrement the stat by 1. This calls the associated storage object dec
834 void operator--() { data()->dec(1, *params); }
836 /** Increment the stat by 1. */
837 void operator++(int) { ++*this; }
838 /** Decrement the stat by 1. */
839 void operator--(int) { --*this; }
842 * Set the data value to the given value. This calls the associated storage
843 * object set function.
844 * @param v The new value.
846 template <typename U>
847 void operator=(const U& v) { data()->set(v, *params); }
850 * Increment the stat by the given value. This calls the associated
851 * storage object inc function.
852 * @param v The value to add.
854 template <typename U>
855 void operator+=(const U& v) { data()->inc(v, *params); }
858 * Decrement the stat by the given value. This calls the associated
859 * storage object dec function.
860 * @param v The value to substract.
862 template <typename U>
863 void operator-=(const U& v) { data()->dec(v, *params); }
866 * Return the number of elements, always 1 for a scalar.
869 virtual size_t size() const { return 1; }
871 virtual bool binned() const { return false; }
874 template <typename T, template <typename T> class Storage, class Bin>
875 inline ScalarProxy<T, Storage, Bin>
876 VectorBase<T, Storage, Bin>::operator[](int index)
878 assert (index >= 0 && index < size());
879 return ScalarProxy<T, Storage, Bin>(bin, params, index);
882 template <typename T, template <typename T> class Storage, class Bin>
885 template <typename T, template <typename T> class Storage, class Bin>
886 class Vector2dBase : public Stat
889 typedef Storage<T> storage_t;
890 typedef typename storage_t::Params params_t;
891 typedef typename Bin::VectorBin<storage_t> bin_t;
898 std::vector<std::string> *y_subnames;
901 storage_t *data(int index) { return bin.data(index, params); }
902 const storage_t *data(int index) const {
903 return (const_cast<bin_t *>(&bin))->data(index, params);
907 // Copying stats is not allowed
908 Vector2dBase(const Vector2dBase &stat);
909 const Vector2dBase &operator=(const Vector2dBase &);
912 Vector2dBase() : Stat(true) {}
915 Vector2dBase &init(size_t _x, size_t _y) {
918 bin.init(x * y, params);
920 y_subnames = new std::vector<std::string>(y);
926 * @warning This makes the assumption that if you're gonna subnames a 2d
927 * vector, you're subnaming across all y
929 Vector2dBase &ysubnames(const char **names)
931 for (int i=0; i < y; ++i) {
932 (*y_subnames)[i] = names[i];
936 Vector2dBase &ysubname(int index, const std::string subname)
938 (*y_subnames)[i] = subname.c_str();
941 std::string ysubname(int i) const { return (*y_subnames)[i]; }
943 friend class VectorProxy<T, Storage, Bin>;
944 VectorProxy<T, Storage, Bin> operator[](int index);
946 virtual size_t size() const { return bin.size(); }
947 virtual bool zero() const { return data(0)->value(params) == 0.0; }
948 virtual bool binned() const { return bin_t::binned; }
951 display(std::ostream &out) const
953 bool have_subname = false;
954 for (int i = 0; i < x; ++i) {
955 if (!mysubname(i).empty())
960 result_t super_total = 0.0;
961 for (int i = 0; i < x; ++i) {
964 subname = mysubname(i);
968 subname = to_string(i);
973 result_t total = 0.0;
974 for (int j = 0; j < y; ++j) {
975 vec[j] = data(iy + j)->val(params);
976 tot_vec[j] += vec[j];
978 super_total += vec[j];
982 if (mysubdesc(i).empty()) {
988 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
989 myprecision(), myflags(), vec, total);
992 if ((myflags() & ::Statistics::total) && (x > 1)) {
993 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
994 myprecision(), myflags(), tot_vec, super_total);
1000 template <typename T, template <typename T> class Storage, class Bin>
1001 class VectorProxy : public VectorStat
1004 typedef Storage<T> storage_t;
1005 typedef typename storage_t::Params params_t;
1006 typedef typename Bin::VectorBin<storage_t> bin_t;
1015 mutable rvec_t *vec;
1017 storage_t *data(int index) {
1018 assert(index < len);
1019 return bin->data(offset + index, *params);
1022 const storage_t *data(int index) const {
1023 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
1027 const rvec_t &val() const {
1029 vec->resize(size());
1031 vec = new rvec_t(size());
1033 for (int i = 0; i < size(); ++i)
1034 (*vec)[i] = data(i)->val(*params);
1039 result_t total() const {
1040 result_t total = 0.0;
1041 for (int i = 0; i < size(); ++i)
1042 total += data(i)->val(*params);
1047 VectorProxy(bin_t &b, params_t &p, int o, int l)
1048 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
1050 VectorProxy(const VectorProxy &sp)
1051 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
1052 len(sp.len), vec(NULL)
1059 const VectorProxy &operator=(const VectorProxy &sp) {
1070 virtual size_t size() const { return len; }
1072 ScalarProxy<T, Storage, Bin> operator[](int index) {
1073 assert (index >= 0 && index < size());
1074 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
1077 virtual bool binned() const { return false; }
1080 template <typename T, template <typename T> class Storage, class Bin>
1081 inline VectorProxy<T, Storage, Bin>
1082 Vector2dBase<T, Storage, Bin>::operator[](int index)
1084 int offset = index * y;
1085 assert (index >= 0 && offset < size());
1086 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
1089 //////////////////////////////////////////////////////////////////////
1091 // Non formula statistics
1093 //////////////////////////////////////////////////////////////////////
1095 void DistDisplay(std::ostream &stream, const std::string &name,
1096 const std::string &desc, int precision, FormatFlags flags,
1097 result_t min_val, result_t max_val,
1098 result_t underflow, result_t overflow,
1099 const rvec_t &vec, int min, int max, int bucket_size,
1102 * Templatized storage and interface for a distrbution stat.
1104 template <typename T>
1108 /** The parameters for a distribution stat. */
1111 /** The minimum value to track. */
1113 /** The maximum value to track. */
1115 /** The number of entries in each bucket. */
1117 /** The number of buckets. Equal to (max-min)/bucket_size. */
1122 /** The smallest value sampled. */
1124 /** The largest value sampled. */
1126 /** The number of values sampled less than min. */
1128 /** The number of values sampled more than max. */
1130 /** Counter for each bucket. */
1135 * Construct this storage with the supplied params.
1136 * @param params The parameters.
1138 DistStor(const Params ¶ms)
1139 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1143 * Add a value to the distribution for the given number of times.
1144 * @param val The value to add.
1145 * @param number The number of times to add the value.
1146 * @param params The paramters of the distribution.
1148 void sample(T val, int number, const Params ¶ms) {
1149 if (val < params.min)
1150 underflow += number;
1151 else if (val > params.max)
1154 int index = (val - params.min) / params.bucket_size;
1155 assert(index < size(params));
1156 vec[index] += number;
1167 * Return the number of buckets in this distribution.
1168 * @return the number of buckets.
1169 * @todo Is it faster to return the size from the parameters?
1171 size_t size(const Params &) const { return vec.size(); }
1174 * Returns true if any calls to sample have been made.
1175 * @param params The paramters of the distribution.
1176 * @return True if any values have been sampled.
1178 bool zero(const Params ¶ms) const {
1179 if (underflow != 0 || overflow != 0)
1182 int s = size(params);
1183 for (int i = 0; i < s; i++)
1191 * Print this distribution and the given print data to the given ostream.
1192 * @param stream The output stream.
1193 * @param name The name of this stat (from StatData).
1194 * @param desc The description of this stat (from StatData).
1195 * @param precision The print precision (from StatData).
1196 * @param flags The format flags (from StatData).
1197 * @param params The paramters of this distribution.
1199 void display(std::ostream &stream, const std::string &name,
1200 const std::string &desc, int precision, FormatFlags flags,
1201 const Params ¶ms) const {
1203 #ifdef STAT_DISPLAY_COMPAT
1204 result_t min = params.min;
1206 result_t min = (min_val == INT_MAX) ? params.min : min_val;
1208 result_t max = (max_val == INT_MIN) ? 0 : max_val;
1210 rvec_t rvec(params.size);
1211 for (int i = 0; i < params.size; ++i)
1214 DistDisplay(stream, name, desc, precision, flags,
1215 (result_t)min, (result_t)max,
1216 (result_t)underflow, (result_t)overflow,
1217 rvec, params.min, params.max, params.bucket_size,
1222 void FancyDisplay(std::ostream &stream, const std::string &name,
1223 const std::string &desc, int precision, FormatFlags flags,
1224 result_t mean, result_t variance);
1227 * Templatized storage and interface for a distribution that calculates mean
1230 template <typename T>
1235 * No paramters for this storage.
1240 /** The current sum. */
1242 /** The sum of squares. */
1244 /** The total number of samples. */
1249 * Create and initialize this storage.
1251 FancyStor(const Params &) : sum(0), squares(0), total(0) {}
1254 * Add a value the given number of times to this running average.
1255 * Update the running sum and sum of squares, increment the number of
1256 * values seen by the given number.
1257 * @param val The value to add.
1258 * @param number The number of times to add the value.
1259 * @param p The parameters of this stat.
1261 void sample(T val, int number, const Params &p) {
1262 T value = val * number;
1264 squares += value * value;
1269 * Print this distribution and the given print data to the given ostream.
1270 * @param stream The output stream.
1271 * @param name The name of this stat (from StatData).
1272 * @param desc The description of this stat (from StatData).
1273 * @param precision The print precision (from StatData).
1274 * @param flags The format flags (from StatData).
1275 * @param params The paramters of this distribution.
1277 void display(std::ostream &stream, const std::string &name,
1278 const std::string &desc, int precision, FormatFlags flags,
1279 const Params ¶ms) const {
1281 result_t mean = NAN;
1282 result_t variance = NAN;
1285 result_t fsum = sum;
1286 result_t fsq = squares;
1287 result_t ftot = total;
1290 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
1293 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1297 * Return the number of entries in this stat, 1
1300 size_t size(const Params &) const { return 1; }
1302 * Return true if no samples have been added.
1303 * @return True if no samples have been added.
1305 bool zero(const Params &) const { return total == 0; }
1309 * Templatized storage for distribution that calculates per cycle mean and
1312 template <typename T>
1316 /** No parameters for this storage. */
1320 /** Current total. */
1322 /** Current sum of squares. */
1327 * Create and initialize this storage.
1329 AvgFancy(const Params &) : sum(0), squares(0) {}
1332 * Add a value to the distribution for the given number of times.
1333 * Update the running sum and sum of squares.
1334 * @param val The value to add.
1335 * @param number The number of times to add the value.
1336 * @param p The paramters of the distribution.
1338 void sample(T val, int number, const Params& p) {
1339 T value = val * number;
1341 squares += value * value;
1345 * Print this distribution and the given print data to the given ostream.
1346 * @param stream The output stream.
1347 * @param name The name of this stat (from StatData).
1348 * @param desc The description of this stat (from StatData).
1349 * @param precision The print precision (from StatData).
1350 * @param flags The format flags (from StatData).
1351 * @param params The paramters of this distribution.
1353 void display(std::ostream &stream, const std::string &name,
1354 const std::string &desc, int precision, FormatFlags flags,
1355 const Params ¶ms) const {
1356 result_t mean = sum / curTick;
1357 result_t variance = (squares - sum * sum) / curTick;
1359 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1363 * Return the number of entries, in this case 1.
1366 size_t size(const Params ¶ms) const { return 1; }
1368 * Return true if no samples have been added.
1369 * @return True if the sum is zero.
1371 bool zero(const Params ¶ms) const { return sum == 0; }
1375 * Implementation of a distribution stat. The type of distribution is
1376 * determined by the Storage template. @sa ScalarBase
1378 template <typename T, template <typename T> class Storage, class Bin>
1379 class DistBase : public Stat
1382 /** Define the type of the storage class. */
1383 typedef Storage<T> storage_t;
1384 /** Define the params of the storage class. */
1385 typedef typename storage_t::Params params_t;
1386 /** Define the bin type. */
1387 typedef typename Bin::Bin<storage_t> bin_t;
1390 /** The bin of this stat. */
1392 /** The parameters for this stat. */
1397 * Retrieve the storage from the bin.
1398 * @return The storage object for this stat.
1400 storage_t *data() { return bin.data(params); }
1402 * Retrieve a const pointer to the storage from the bin.
1403 * @return A const pointer to the storage object for this stat.
1405 const storage_t *data() const {
1406 return (const_cast<bin_t *>(&bin))->data(params);
1410 // Copying stats is not allowed
1411 /** Copies are not allowed. */
1412 DistBase(const DistBase &stat);
1413 /** Copies are not allowed. */
1414 const DistBase &operator=(const DistBase &);
1418 * Create this distrubition and register it with the database.
1420 DistBase() : Stat(true) { }
1427 * Add a value to the distribtion n times. Calls sample on the storage
1429 * @param v The value to add.
1430 * @param n The number of times to add it, defaults to 1.
1432 template <typename U>
1433 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1436 * Return the number of entries in this stat.
1437 * @return The number of entries.
1439 virtual size_t size() const { return data()->size(params); }
1441 * Return true if no samples have been added.
1442 * @return True if there haven't been any samples.
1444 virtual bool zero() const { return data()->zero(params); }
1446 * Print this distribution to the given ostream.
1447 * @param stream The output stream.
1449 virtual void display(std::ostream &stream) const {
1450 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
1454 virtual bool binned() const { return bin_t::binned; }
1457 template <typename T, template <typename T> class Storage, class Bin>
1458 class VectorDistProxy;
1460 template <typename T, template <typename T> class Storage, class Bin>
1461 class VectorDistBase : public Stat
1464 typedef Storage<T> storage_t;
1465 typedef typename storage_t::Params params_t;
1466 typedef typename Bin::VectorBin<storage_t> bin_t;
1473 storage_t *data(int index) { return bin.data(index, params); }
1474 const storage_t *data(int index) const {
1475 return (const_cast<bin_t *>(&bin))->data(index, params);
1479 // Copying stats is not allowed
1480 VectorDistBase(const VectorDistBase &stat);
1481 const VectorDistBase &operator=(const VectorDistBase &);
1484 VectorDistBase() : Stat(true) { }
1485 ~VectorDistBase() { }
1487 friend class VectorDistProxy<T, Storage, Bin>;
1488 VectorDistProxy<T, Storage, Bin> operator[](int index);
1489 const VectorDistProxy<T, Storage, Bin> operator[](int index) const;
1491 virtual size_t size() const { return bin.size(); }
1492 virtual bool zero() const { return false; }
1493 virtual void display(std::ostream &stream) const;
1494 virtual bool binned() const { return bin_t::binned; }
1497 template <typename T, template <typename T> class Storage, class Bin>
1498 class VectorDistProxy : public Stat
1501 typedef Storage<T> storage_t;
1502 typedef typename storage_t::Params params_t;
1503 typedef typename Bin::Bin<storage_t> bin_t;
1504 typedef VectorDistBase<T, Storage, Bin> base_t;
1509 const base_t *cstat;
1514 storage_t *data() { return stat->data(index); }
1515 const storage_t *data() const { return cstat->data(index); }
1518 VectorDistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1519 : Stat(false), cstat(&s), index(i) {}
1520 VectorDistProxy(const VectorDistProxy &sp)
1521 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1522 const VectorDistProxy &operator=(const VectorDistProxy &sp) {
1523 cstat = sp.cstat; index = sp.index; return *this;
1527 template <typename U>
1528 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1530 virtual size_t size() const { return 1; }
1531 virtual bool zero() const {
1532 return data()->zero(cstat->params);
1534 virtual void display(std::ostream &stream) const {
1535 std::stringstream name, desc;
1537 if (!(cstat->mysubname(index).empty())) {
1538 name << cstat->myname() << cstat->mysubname(index);
1540 name << cstat->myname() << "_" << index;
1542 if (!(cstat->mysubdesc(index).empty())) {
1543 desc << cstat->mysubdesc(index);
1545 desc << cstat->mydesc();
1548 data()->display(stream, name.str(), desc.str(),
1549 cstat->myprecision(), cstat->myflags(), cstat->params);
1552 virtual bool binned() const { return false; }
1555 template <typename T, template <typename T> class Storage, class Bin>
1556 inline VectorDistProxy<T, Storage, Bin>
1557 VectorDistBase<T, Storage, Bin>::operator[](int index)
1559 assert (index >= 0 && index < size());
1560 return VectorDistProxy<T, Storage, Bin>(*this, index);
1563 template <typename T, template <typename T> class Storage, class Bin>
1564 inline const VectorDistProxy<T, Storage, Bin>
1565 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1567 assert (index >= 0 && index < size());
1568 return VectorDistProxy<T, Storage, Bin>(*this, index);
1572 * @todo Need a way to print Distribution totals across the Vector
1574 template <typename T, template <typename T> class Storage, class Bin>
1576 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1578 for (int i = 0; i < size(); ++i) {
1579 VectorDistProxy<T, Storage, Bin> proxy(*this, i);
1580 proxy.display(stream);
1586 VectorDistBase<T, Storage, Bin>::total(int index) const
1589 for (int i=0; i < x_size(); ++i) {
1590 total += data(i)->val(*params);
1595 //////////////////////////////////////////////////////////////////////
1599 //////////////////////////////////////////////////////////////////////
1602 * Base class for formula statistic node. These nodes are used to build a tree
1603 * that represents the formula.
1605 class Node : public RefCounted
1609 * Return the number of nodes in the subtree starting at this node.
1610 * @return the number of nodes in this subtree.
1612 virtual size_t size() const = 0;
1614 * Return the result vector of this subtree.
1615 * @return The result vector of this subtree.
1617 virtual const rvec_t &val() const = 0;
1619 * Return the total of the result vector.
1620 * @return The total of the result vector.
1622 virtual result_t total() const = 0;
1624 virtual bool binned() const = 0;
1627 /** Reference counting pointer to a function Node. */
1628 typedef RefCountingPtr<Node> NodePtr;
1630 class ScalarStatNode : public Node
1633 const ScalarStat &stat;
1634 mutable rvec_t result;
1637 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1638 const rvec_t &val() const { result[0] = stat.val(); return result; }
1639 virtual result_t total() const { return stat.val(); };
1641 virtual size_t size() const { return 1; }
1643 virtual bool binned() const { return stat.binned(); }
1646 template <typename T, template <typename T> class Storage, class Bin>
1647 class ScalarProxyNode : public Node
1650 const ScalarProxy<T, Storage, Bin> proxy;
1651 mutable rvec_t result;
1654 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1655 : proxy(p), result(1) { }
1656 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1657 virtual result_t total() const { return proxy.val(); };
1659 virtual size_t size() const { return 1; }
1661 virtual bool binned() const { return proxy.binned(); }
1664 class VectorStatNode : public Node
1667 const VectorStat &stat;
1670 VectorStatNode(const VectorStat &s) : stat(s) {}
1671 const rvec_t &val() const { return stat.val(); }
1672 virtual result_t total() const { return stat.total(); };
1674 virtual size_t size() const { return stat.size(); }
1676 virtual bool binned() const { return stat.binned(); }
1679 template <typename T>
1680 class ConstNode : public Node
1686 ConstNode(T s) : data(1, (result_t)s) {}
1687 const rvec_t &val() const { return data; }
1688 virtual result_t total() const { return data[0]; };
1690 virtual size_t size() const { return 1; }
1691 virtual bool binned() const { return false; }
1694 template <typename T>
1695 class FunctorNode : public Node
1699 mutable rvec_t result;
1702 FunctorNode(T &f) : functor(f) { result.resize(1); }
1703 const rvec_t &val() const {
1704 result[0] = (result_t)functor();
1707 virtual result_t total() const { return (result_t)functor(); };
1709 virtual size_t size() const { return 1; }
1710 virtual bool binned() const { return false; }
1713 template <typename T>
1714 class ScalarNode : public Node
1718 mutable rvec_t result;
1721 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1722 const rvec_t &val() const {
1723 result[0] = (result_t)scalar;
1726 virtual result_t total() const { return (result_t)scalar; };
1728 virtual size_t size() const { return 1; }
1729 virtual bool binned() const { return false; }
1733 class UnaryNode : public Node
1737 mutable rvec_t result;
1740 UnaryNode(NodePtr p) : l(p) {}
1742 const rvec_t &val() const {
1743 const rvec_t &lvec = l->val();
1744 int size = lvec.size();
1748 result.resize(size);
1750 for (int i = 0; i < size; ++i)
1751 result[i] = op(lvec[i]);
1756 result_t total() const {
1758 return op(l->total());
1761 virtual size_t size() const { return l->size(); }
1762 virtual bool binned() const { return l->binned(); }
1766 class BinaryNode : public Node
1771 mutable rvec_t result;
1774 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1776 const rvec_t &val() const {
1778 const rvec_t &lvec = l->val();
1779 const rvec_t &rvec = r->val();
1781 assert(lvec.size() > 0 && rvec.size() > 0);
1783 if (lvec.size() == 1 && rvec.size() == 1) {
1785 result[0] = op(lvec[0], rvec[0]);
1786 } else if (lvec.size() == 1) {
1787 int size = rvec.size();
1788 result.resize(size);
1789 for (int i = 0; i < size; ++i)
1790 result[i] = op(lvec[0], rvec[i]);
1791 } else if (rvec.size() == 1) {
1792 int size = lvec.size();
1793 result.resize(size);
1794 for (int i = 0; i < size; ++i)
1795 result[i] = op(lvec[i], rvec[0]);
1796 } else if (rvec.size() == lvec.size()) {
1797 int size = rvec.size();
1798 result.resize(size);
1799 for (int i = 0; i < size; ++i)
1800 result[i] = op(lvec[i], rvec[i]);
1806 result_t total() const {
1808 return op(l->total(), r->total());
1811 virtual size_t size() const {
1819 assert(ls == rs && "Node vector sizes are not equal");
1824 virtual bool binned() const { return (l->binned() || r->binned()); }
1828 class SumNode : public Node
1832 mutable rvec_t result;
1835 SumNode(NodePtr p) : l(p), result(1) {}
1837 const rvec_t &val() const {
1838 const rvec_t &lvec = l->val();
1839 int size = lvec.size();
1845 for (int i = 0; i < size; ++i)
1846 result[0] = op(result[0], lvec[i]);
1851 result_t total() const {
1852 const rvec_t &lvec = l->val();
1853 int size = lvec.size();
1856 result_t result = 0.0;
1859 for (int i = 0; i < size; ++i)
1860 result = op(result, lvec[i]);
1865 virtual size_t size() const { return 1; }
1866 virtual bool binned() const { return l->binned(); }
1870 * Helper class to construct formula node trees.
1876 * Pointer to a Node object.
1882 * Copy the given pointer to this class.
1883 * @param n A pointer to a Node object to copy.
1885 Temp(NodePtr n) : node(n) {}
1887 * Create a new ScalarStatNode.
1888 * @param s The ScalarStat to place in a node.
1890 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1892 * Create a new ScalarProxyNode.
1893 * @param p The ScalarProxy to place in a node.
1895 template <typename T, template <typename T> class Storage, class Bin>
1896 Temp(const ScalarProxy<T, Storage, Bin> &p)
1897 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1899 * Create a new VectorStatNode.
1900 * @param s The VectorStat to place in a node.
1902 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1905 * Create a ConstNode
1906 * @param value The value of the const node.
1908 Temp(signed char value) : node(new ConstNode<signed char>(value)) {}
1910 * Create a ConstNode
1911 * @param value The value of the const node.
1913 Temp(unsigned char value) : node(new ConstNode<unsigned char>(value)) {}
1915 * Create a ConstNode
1916 * @param value The value of the const node.
1918 Temp(signed short value) : node(new ConstNode<signed short>(value)) {}
1920 * Create a ConstNode
1921 * @param value The value of the const node.
1923 Temp(unsigned short value) : node(new ConstNode<unsigned short>(value)) {}
1925 * Create a ConstNode
1926 * @param value The value of the const node.
1928 Temp(signed int value) : node(new ConstNode<signed int>(value)) {}
1930 * Create a ConstNode
1931 * @param value The value of the const node.
1933 Temp(unsigned int value) : node(new ConstNode<unsigned int>(value)) {}
1935 * Create a ConstNode
1936 * @param value The value of the const node.
1938 Temp(signed long value) : node(new ConstNode<signed long>(value)) {}
1940 * Create a ConstNode
1941 * @param value The value of the const node.
1943 Temp(unsigned long value) : node(new ConstNode<unsigned long>(value)) {}
1945 * Create a ConstNode
1946 * @param value The value of the const node.
1948 Temp(signed long long value)
1949 : node(new ConstNode<signed long long>(value)) {}
1951 * Create a ConstNode
1952 * @param value The value of the const node.
1954 Temp(unsigned long long value)
1955 : node(new ConstNode<unsigned long long>(value)) {}
1957 * Create a ConstNode
1958 * @param value The value of the const node.
1960 Temp(float value) : node(new ConstNode<float>(value)) {}
1962 * Create a ConstNode
1963 * @param value The value of the const node.
1965 Temp(double value) : node(new ConstNode<double>(value)) {}
1968 * Return the node pointer.
1969 * @return the node pointer.
1971 operator NodePtr() { return node;}
1975 //////////////////////////////////////////////////////////////////////
1977 // Binning Interface
1979 //////////////////////////////////////////////////////////////////////
1988 off_t size() const { return memsize; }
1992 BinBase(size_t size);
1994 virtual void activate() = 0;
1995 void regBin(BinBase *bin, std::string name);
1998 } // namespace Detail
2000 template <class BinType>
2001 struct StatBin : public Detail::BinBase
2007 std::string name() const { return _name;}
2009 static StatBin *&curBin() {
2010 static StatBin *current = NULL;
2014 static void setCurBin(StatBin *bin) { curBin() = bin; }
2015 static StatBin *current() { assert(curBin()); return curBin(); }
2017 static off_t &offset() {
2018 static off_t offset = 0;
2022 static off_t new_offset(size_t size) {
2023 size_t mask = sizeof(u_int64_t) - 1;
2024 off_t off = offset();
2026 // That one is for the last trailing flags byte.
2027 offset() += (size + 1 + mask) & ~mask;
2032 explicit StatBin(std::string name, size_t size = 1024) : Detail::BinBase(size) { _name = name; this->regBin(this, name); }
2034 char *memory(off_t off) {
2035 assert(offset() <= size());
2036 return Detail::BinBase::memory() + off;
2039 virtual void activate() { setCurBin(this); }
2040 static void activate(StatBin &bin) { setCurBin(&bin); }
2048 BinBase() : offset(-1) {}
2049 void allocate(size_t size) {
2050 offset = new_offset(size);
2053 assert(offset != -1);
2054 return current()->memory(offset);
2058 template <class Storage>
2059 class Bin : public BinBase
2062 typedef typename Storage::Params Params;
2065 enum { binned = true };
2066 Bin() { allocate(sizeof(Storage)); }
2067 bool initialized() const { return true; }
2068 void init(const Params ¶ms) { }
2070 int size() const { return 1; }
2072 Storage *data(const Params ¶ms) {
2073 assert(initialized());
2074 char *ptr = access();
2075 char *flags = ptr + sizeof(Storage);
2076 if (!(*flags & 0x1)) {
2078 new (ptr) Storage(params);
2080 return reinterpret_cast<Storage *>(ptr);
2084 template <class Storage>
2085 class VectorBin : public BinBase
2088 typedef typename Storage::Params Params;
2094 VectorBin() : _size(0) {}
2096 bool initialized() const { return _size > 0; }
2097 void init(int s, const Params ¶ms) {
2098 assert(!initialized());
2101 allocate(_size * sizeof(Storage));
2104 int size() const { return _size; }
2106 Storage *data(int index, const Params ¶ms) {
2107 assert(initialized());
2108 assert(index >= 0 && index < size());
2109 char *ptr = access();
2110 char *flags = ptr + size() * sizeof(Storage);
2111 if (!(*flags & 0x1)) {
2113 for (int i = 0; i < size(); ++i)
2114 new (ptr + i * sizeof(Storage)) Storage(params);
2116 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2121 class MainBinType {};
2122 typedef StatBin<MainBinType> MainBin;
2126 template <class Storage>
2130 typedef typename Storage::Params Params;
2131 enum { binned = false };
2134 char ptr[sizeof(Storage)];
2137 bool initialized() const { return true; }
2138 void init(const Params ¶ms) {
2139 new (ptr) Storage(params);
2141 int size() const{ return 1; }
2142 Storage *data(const Params ¶ms) {
2143 assert(initialized());
2144 return reinterpret_cast<Storage *>(ptr);
2148 template <class Storage>
2152 typedef typename Storage::Params Params;
2153 enum { binned = false };
2160 VectorBin() : ptr(NULL) { }
2165 bool initialized() const { return ptr != NULL; }
2166 void init(int s, const Params ¶ms) {
2167 assert(s > 0 && "size must be positive!");
2168 assert(!initialized());
2170 ptr = new char[_size * sizeof(Storage)];
2171 for (int i = 0; i < _size; ++i)
2172 new (ptr + i * sizeof(Storage)) Storage(params);
2175 int size() const { return _size; }
2177 Storage *data(int index, const Params ¶ms) {
2178 assert(initialized());
2179 assert(index >= 0 && index < size());
2180 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2185 //////////////////////////////////////////////////////////////////////
2187 // Visible Statistics Types
2189 //////////////////////////////////////////////////////////////////////
2191 * @defgroup VisibleStats "Statistic Types"
2192 * These are the statistics that are used in the simulator. By default these
2193 * store counters and don't use binning, but are templatized to accept any type
2194 * and any Bin class.
2199 * This is a simple scalar statistic, like a counter.
2200 * @sa Stat, ScalarBase, StatStor
2202 template <typename T = Counter, class Bin = NoBin>
2203 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
2206 /** The base implementation. */
2207 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
2210 * Sets the stat equal to the given value. Calls the base implementation
2212 * @param v The new value.
2214 template <typename U>
2215 void operator=(const U& v) { Base::operator=(v); }
2219 * A stat that calculates the per cycle average of a value.
2220 * @sa Stat, ScalarBase, AvgStor
2222 template <typename T = Counter, class Bin = NoBin>
2223 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
2226 /** The base implementation. */
2227 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
2230 * Sets the stat equal to the given value. Calls the base implementation
2232 * @param v The new value.
2234 template <typename U>
2235 void operator=(const U& v) { Base::operator=(v); }
2239 * A vector of scalar stats.
2240 * @sa Stat, VectorBase, StatStor
2242 template <typename T = Counter, class Bin = NoBin>
2243 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
2247 * A vector of Average stats.
2248 * @sa Stat, VectorBase, AvgStor
2250 template <typename T = Counter, class Bin = NoBin>
2251 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
2255 * A 2-Dimensional vecto of scalar stats.
2256 * @sa Stat, Vector2dBase, StatStor
2258 template <typename T = Counter, class Bin = NoBin>
2259 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
2263 * A simple distribution stat.
2264 * @sa Stat, DistBase, DistStor
2266 template <typename T = Counter, class Bin = NoBin>
2267 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
2270 /** Base implementation. */
2271 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2272 /** The Parameter type. */
2273 typedef typename Detail::DistStor<T>::Params Params;
2277 * Set the parameters of this distribution. @sa DistStor::Params
2278 * @param min The minimum value of the distribution.
2279 * @param max The maximum value of the distribution.
2280 * @param bkt The number of values in each bucket.
2281 * @return A reference to this distribution.
2283 Distribution &init(T min, T max, int bkt) {
2286 params.bucket_size = bkt;
2287 params.size = (max - min) / bkt + 1;
2296 * Calculates the mean and variance of all the samples.
2297 * @sa Stat, DistBase, FancyStor
2299 template <typename T = Counter, class Bin = NoBin>
2300 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
2303 /** The base implementation */
2304 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2305 /** The parameter type. */
2306 typedef typename Detail::DistStor<T>::Params Params;
2310 * Construct and initialize this distribution.
2312 StandardDeviation() {
2319 * Calculates the per cycle mean and variance of the samples.
2320 * @sa Stat, DistBase, AvgFancy
2322 template <typename T = Counter, class Bin = NoBin>
2323 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
2326 /** The base implementation */
2327 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2328 /** The parameter type. */
2329 typedef typename Detail::DistStor<T>::Params Params;
2333 * Construct and initialize this distribution.
2335 AverageDeviation() {
2342 * A vector of distributions.
2343 * @sa Stat, VectorDistBase, DistStor
2345 template <typename T = Counter, class Bin = NoBin>
2346 class VectorDistribution
2347 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
2350 /** The base implementation */
2351 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
2352 /** The parameter type. */
2353 typedef typename Detail::DistStor<T>::Params Params;
2357 * Initialize storage and parameters for this distribution.
2358 * @param size The size of the vector (the number of distributions).
2359 * @param min The minimum value of the distribution.
2360 * @param max The maximum value of the distribution.
2361 * @param bkt The number of values in each bucket.
2362 * @return A reference to this distribution.
2364 VectorDistribution &init(int size, T min, T max, int bkt) {
2367 params.bucket_size = bkt;
2368 params.size = (max - min) / bkt + 1;
2369 bin.init(size, params);
2377 * This is a vector of StandardDeviation stats.
2378 * @sa Stat, VectorDistBase, FancyStor
2380 template <typename T = Counter, class Bin = NoBin>
2381 class VectorStandardDeviation
2382 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
2385 /** The base implementation */
2386 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
2387 /** The parameter type. */
2388 typedef typename Detail::DistStor<T>::Params Params;
2392 * Initialize storage for this distribution.
2393 * @param size The size of the vector.
2394 * @return A reference to this distribution.
2396 VectorStandardDeviation &init(int size) {
2397 bin.init(size, params);
2405 * This is a vector of AverageDeviation stats.
2406 * @sa Stat, VectorDistBase, AvgFancy
2408 template <typename T = Counter, class Bin = NoBin>
2409 class VectorAverageDeviation
2410 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
2413 /** The base implementation */
2414 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
2415 /** The parameter type. */
2416 typedef typename Detail::DistStor<T>::Params Params;
2420 * Initialize storage for this distribution.
2421 * @param size The size of the vector.
2422 * @return A reference to this distribution.
2424 VectorAverageDeviation &init(int size) {
2425 bin.init(size, params);
2433 * A formula for statistics that is calculated when printed. A formula is
2434 * stored as a tree of Nodes that represent the equation to calculate.
2435 * @sa Stat, ScalarStat, VectorStat, Node, Detail::Temp
2437 class Formula : public Detail::VectorStat
2440 /** The root of the tree which represents the Formula */
2441 Detail::NodePtr root;
2442 friend class Statistics::Detail::Temp;
2446 * Create and initialize thie formula, and register it with the database.
2448 Formula() : VectorStat(true) { setInit(); }
2450 * Create a formula with the given root node, register it with the
2452 * @param r The root of the expression tree.
2454 Formula(Detail::Temp r) : VectorStat(true) {
2460 * Set an unitialized Formula to the given root.
2461 * @param r The root of the expression tree.
2462 * @return a reference to this formula.
2464 const Formula &operator=(Detail::Temp r) {
2465 assert(!root && "Can't change formulas");
2472 * Add the given tree to the existing one.
2473 * @param r The root of the expression tree.
2474 * @return a reference to this formula.
2476 const Formula &operator+=(Detail::Temp r) {
2477 using namespace Detail;
2479 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
2487 * Return the vector of values of this formula.
2488 * @return The result vector.
2490 const rvec_t &val() const { return root->val(); }
2492 * Return the total of the result vector.
2493 * @return The total of the result vector.
2495 result_t total() const { return root->total(); }
2498 * Return the number of elements in the tree.
2500 size_t size() const {
2504 return root->size();
2507 virtual bool binned() const { return root->binned(); }
2515 void dump(std::ostream &stream);
2518 operator+(Detail::Temp l, Detail::Temp r)
2520 using namespace Detail;
2521 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
2525 operator-(Detail::Temp l, Detail::Temp r)
2527 using namespace Detail;
2528 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
2532 operator*(Detail::Temp l, Detail::Temp r)
2534 using namespace Detail;
2535 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
2539 operator/(Detail::Temp l, Detail::Temp r)
2541 using namespace Detail;
2542 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
2546 operator%(Detail::Temp l, Detail::Temp r)
2548 using namespace Detail;
2549 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
2553 operator-(Detail::Temp l)
2555 using namespace Detail;
2556 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
2559 template <typename T>
2563 using namespace Detail;
2564 return NodePtr(new ConstNode<T>(val));
2567 template <typename T>
2571 using namespace Detail;
2572 return NodePtr(new FunctorNode<T>(val));
2575 template <typename T>
2579 using namespace Detail;
2580 return NodePtr(new ScalarNode<T>(val));
2584 sum(Detail::Temp val)
2586 using namespace Detail;
2587 return NodePtr(new SumNode<std::plus<result_t> >(val));
2590 extern bool PrintDescriptions;
2592 } // namespace statistics
2594 #endif // __STATISTICS_HH__