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
81 /** The current simulated cycle. */
84 /* A namespace for all of the Statistics */
85 namespace Statistics {
86 /** All results are doubles. */
87 typedef double result_t;
88 /** A vector to hold results. */
89 typedef std::vector<result_t> rvec_t;
92 * Define the storage for format flags.
93 * @todo Can probably shrink this.
95 typedef u_int32_t FormatFlags;
96 /** Nothing extra to print. */
97 const FormatFlags none = 0x0000;
98 /** Print the total. */
99 const FormatFlags total = 0x0001;
100 /** Print the percent of the total that this entry represents. */
101 const FormatFlags pdf = 0x0002;
102 /** Don't print if this is zero. */
103 const FormatFlags nozero = 0x0004;
104 /** Don't print if this is NAN */
105 const FormatFlags nonan = 0x0008;
106 /** Print the cumulative percentage of total upto this entry. */
107 const FormatFlags cdf = 0x0010;
108 /** Print the distribution. */
109 const FormatFlags dist = 0x0020;
110 /** Used for SS compatability. */
111 const FormatFlags __substat = 0x8000;
112 /** Mask of flags that can't be set directly */
113 const FormatFlags __reserved = __substat;
115 /* Contains the statistic implementation details */
117 //////////////////////////////////////////////////////////////////////
119 // Statistics Framework Base classes
121 //////////////////////////////////////////////////////////////////////
126 * Common base class for all statistics, used to maintain a list and print.
127 * This class holds no data itself but is used to find the associated
128 * StatData in the stat database @sa Statistics::Database.
133 /** Mark this statistics as initialized. */
136 * Finds and returns the associated StatData from the database.
137 * @return The formatting and output data of this statistic.
141 * Finds and returns a const pointer to the associated StatData.
142 * @return The formatting and output data of this statistic.
144 const StatData *mydata() const;
146 * Mark this stat for output at the end of simulation.
147 * @return The formatting and output data of this statistic.
151 * Finds and returns the SubData at the given index.
152 * @param index The index of the SubData to find.
153 * @return The name and description of the given index.
155 const SubData *mysubdata(int index) const;
157 * Create and return a new SubData field for the given index.
158 * @param index The index to create a SubData for.
159 * @return A pointer to the created SubData.
161 SubData *mysubdata_create(int index);
165 * Return the name of this stat.
166 * @return the name of the stat.
168 virtual std::string myname() const;
170 * Return the name of the sub field at the given index.
171 * @param index the subfield index.
172 * @return the name of the subfield.
174 virtual std::string mysubname(int index) const;
176 * Return the description of this stat.
177 * @return the description of this stat.
179 virtual std::string mydesc() const;
181 * Return the description of the subfield at the given index.
182 * @param index The subfield index.
183 * @return the description of the subfield.
185 virtual std::string mysubdesc(int index) const;
187 * Return the format flags of this stat.
188 * @return the format flags.
190 virtual FormatFlags myflags() const;
192 * Return true if this stat's prereqs have been satisfied (they are non
194 * @return true if the prerequisite stats aren't zero.
196 virtual bool dodisplay() const;
198 * Return the display percision.
199 * @return The display precision.
201 virtual int myprecision() const;
205 * Create this stat and perhaps register it with the stat database. To be
206 * printed a stat must be registered with the database.
207 * @param reg If true, register this stat in the database.
216 * Print this stat to the given ostream.
217 * @param stream The stream to print to.
219 virtual void display(std::ostream &stream) const = 0;
221 * Reset this stat to the default state.
223 virtual void reset() = 0;
225 * Return the number of entries in this stat.
226 * @return The number of entries.
228 virtual size_t size() const = 0;
230 * Return true if the stat has value zero.
231 * @return True if the stat is zero.
233 virtual bool zero() const = 0;
237 * Set the name and marks this stat to print at the end of simulation.
238 * @param name The new name.
239 * @return A reference to this stat.
241 Stat &name(const std::string &name);
243 * Set the description and marks this stat to print at the end of
245 * @param desc The new description.
246 * @return A reference to this stat.
248 Stat &desc(const std::string &desc);
250 * Set the precision and marks this stat to print at the end of simulation.
251 * @param p The new precision
252 * @return A reference to this stat.
254 Stat &precision(int p);
256 * Set the flags and marks this stat to print at the end of simulation.
257 * @param f The new flags.
258 * @return A reference to this stat.
260 Stat &flags(FormatFlags f);
262 * Set the prerequisite stat and marks this stat to print at the end of
264 * @param prereq The prerequisite stat.
265 * @return A reference to this stat.
267 Stat &prereq(const Stat &prereq);
269 * Set the subfield name for the given index, and marks this stat to print
270 * at the end of simulation.
271 * @param index The subfield index.
272 * @param name The new name of the subfield.
273 * @return A reference to this stat.
275 Stat &subname(int index, const std::string &name);
277 * Set the subfield description for the given index and marks this stat to
278 * print at the end of simulation.
279 * @param index The subfield index.
280 * @param desc The new description of the subfield
281 * @return A reference to this stat.
283 Stat &subdesc(int index, const std::string &desc);
287 * Checks if the first stat's name is alphabetically less than the second.
288 * This function breaks names up at periods and considers each subname
290 * @param stat1 The first stat.
291 * @param stat2 The second stat.
292 * @return stat1's name is alphabetically before stat2's
294 static bool less(Stat *stat1, Stat *stat2);
297 /** A unique ID used for debugging. */
303 * Base class for all scalar stats. The class provides an interface to access
304 * the current value of the stat. This class can be used in formulas.
306 class ScalarStat : public Stat
310 * Create and perhaps register this stat with the database.
311 * @param reg If true, register this stat with the database.
313 ScalarStat(bool reg) : Stat(reg) {}
315 * Return the current value of this statistic as a result type.
316 * @return The current value of this statistic.
318 virtual result_t val() const = 0;
320 * Return true if this stat has value zero.
321 * @return True if this stat is zero.
323 virtual bool zero() const;
325 * Print this stat to the provided ostream.
326 * @param stream The output stream.
328 virtual void display(std::ostream &stream) const;
332 VectorDisplay(std::ostream &stream, const std::string &myname,
333 const std::vector<std::string> *mysubnames,
334 const std::string &mydesc,
335 const std::vector<std::string> *mysubdescs,
336 int myprecision, FormatFlags myflags, const rvec_t &vec,
340 * Base class for all vector stats. This class provides interfaces to access
341 * the current values of the stats as well as the totals. This class can be
344 class VectorStat : public Stat
348 * Create and perhaps register this stat with the database.
349 * @param reg If true, register this stat with the database.
351 VectorStat(bool reg) : Stat(reg) {}
353 * Return a vector of result typesd of all the values in the vector.
354 * @return The values of the vector.
356 virtual const rvec_t &val() const = 0;
358 * Return the total of all the entries in the vector.
359 * @return The total of the vector.
361 virtual result_t total() const = 0;
363 * Return true if this stat has value zero.
364 * @return True if this stat is zero.
366 virtual bool zero() const;
368 * Print this stat to the provided ostream.
369 * @param stream The output stream.
371 virtual void display(std::ostream &stream) const;
374 //////////////////////////////////////////////////////////////////////
378 //////////////////////////////////////////////////////////////////////
381 * Templatized storage and interface for a simple scalar stat.
383 template <typename T>
387 /** The paramaters for this storage type, none for a scalar. */
391 /** The statistic value. */
396 * Builds this storage element and calls the base constructor of the
399 StatStor(const Params &) : data(T()) {}
402 * The the stat to the given value.
403 * @param val The new value.
404 * @param p The paramters of this storage type.
406 void set(T val, const Params &p) { data = val; }
408 * Increment the stat by the given value.
409 * @param val The new value.
410 * @param p The paramters of this storage type.
412 void inc(T val, const Params &p) { data += val; }
414 * Decrement the stat by the given value.
415 * @param val The new value.
416 * @param p The paramters of this storage type.
418 void dec(T val, const Params &p) { data -= val; }
420 * Return the value of this stat as a result type.
421 * @param p The parameters of this storage type.
422 * @return The value of this stat.
424 result_t val(const Params &p) const { return (result_t)data; }
426 * Return the value of this stat as its base type.
427 * @param p The params of this storage type.
428 * @return The value of this stat.
430 T value(const Params &p) const { return data; }
432 * Reset stat value to default
434 void reset() { data = T(); }
438 * Templatized storage and interface to a per-cycle average stat. This keeps
439 * a current count and updates a total (count * cycles) when this count
440 * changes. This allows the quick calculation of a per cycle count of the item
441 * being watched. This is good for keeping track of residencies in structures
442 * among other things.
443 * @todo add lateny to the stat and fix binning.
445 template <typename T>
449 /** The paramaters for this storage type, none for this average. */
453 /** The current count. */
455 /** The total count for all cycles. */
456 mutable result_t total;
457 /** The cycle that current last changed. */
462 * Build and initializes this stat storage.
464 AvgStor(const Params &) : current(T()), total(0), last(0) { }
467 * Set the current count to the one provided, update the total and last
469 * @param val The new count.
470 * @param p The parameters for this storage.
472 void set(T val, const Params &p) {
473 total += current * (curTick - last);
478 * Increment the current count by the provided value, calls set.
479 * @param val The amount to increment.
480 * @param p The parameters for this storage.
482 void inc(T val, const Params &p) { set(current + val, p); }
484 * Deccrement the current count by the provided value, calls set.
485 * @param val The amount to decrement.
486 * @param p The parameters for this storage.
488 void dec(T val, const Params &p) { set(current - val, p); }
490 * Return the current average.
491 * @param p The parameters for this storage.
492 * @return The current average.
494 result_t val(const Params &p) const {
495 total += current * (curTick - last);
497 return (result_t)(total + current) / (result_t)(curTick + 1);
500 * Return the current count.
501 * @param p The parameters for this storage.
502 * @return The current count.
504 T value(const Params &p) const { return current; }
506 * Reset stat value to default
517 * Implementation of a scalar stat. The type of stat is determined by the
518 * Storage template. The storage for this stat is held within the Bin class.
519 * This allows for breaking down statistics across multiple bins easily.
521 template <typename T, template <typename T> class Storage, class Bin>
522 class ScalarBase : public ScalarStat
525 /** Define the type of the storage class. */
526 typedef Storage<T> storage_t;
527 /** Define the params of the storage class. */
528 typedef typename storage_t::Params params_t;
529 /** Define the bin type. */
530 typedef typename Bin::Bin<storage_t> bin_t;
533 /** The bin of this stat. */
535 /** The parameters for this stat. */
540 * Retrieve the storage from the bin.
541 * @return The storage object for this stat.
543 storage_t *data() { return bin.data(params); }
545 * Retrieve a const pointer to the storage from the bin.
546 * @return A const pointer to the storage object for this stat.
548 const storage_t *data() const {
549 return (const_cast<bin_t *>(&bin))->data(params);
554 * Copy constructor, copies are not allowed.
556 ScalarBase(const ScalarBase &stat);
560 const ScalarBase &operator=(const ScalarBase &);
564 * Return the current value of this stat as a result type.
565 * @return The current value.
567 result_t val() const { return data()->val(params); }
569 * Return the current value of this stat as its base type.
570 * @return The current value.
572 T value() const { return data()->value(params); }
576 * Create and initialize this stat, register it with the database.
578 ScalarBase() : ScalarStat(true) {
584 // Common operators for stats
586 * Increment the stat by 1. This calls the associated storage object inc
589 void operator++() { data()->inc(1, params); }
591 * Decrement the stat by 1. This calls the associated storage object dec
594 void operator--() { data()->dec(1, params); }
596 /** Increment the stat by 1. */
597 void operator++(int) { ++*this; }
598 /** Decrement the stat by 1. */
599 void operator--(int) { --*this; }
602 * Set the data value to the given value. This calls the associated storage
603 * object set function.
604 * @param v The new value.
606 template <typename U>
607 void operator=(const U& v) { data()->set(v, params); }
610 * Increment the stat by the given value. This calls the associated
611 * storage object inc function.
612 * @param v The value to add.
614 template <typename U>
615 void operator+=(const U& v) { data()->inc(v, params); }
618 * Decrement the stat by the given value. This calls the associated
619 * storage object dec function.
620 * @param v The value to substract.
622 template <typename U>
623 void operator-=(const U& v) { data()->dec(v, params); }
626 * Return the number of elements, always 1 for a scalar.
629 virtual size_t size() const { return 1; }
632 * Reset stat value to default
634 void reset() { bin.reset(); }
637 //////////////////////////////////////////////////////////////////////
641 //////////////////////////////////////////////////////////////////////
642 template <typename T, template <typename T> class Storage, class Bin>
646 * Implementation of a vector of stats. The type of stat is determined by the
647 * Storage class. @sa ScalarBase
649 template <typename T, template <typename T> class Storage, class Bin>
650 class VectorBase : public VectorStat
653 /** Define the type of the storage class. */
654 typedef Storage<T> storage_t;
655 /** Define the params of the storage class. */
656 typedef typename storage_t::Params params_t;
657 /** Define the bin type. */
658 typedef typename Bin::VectorBin<storage_t> bin_t;
661 /** Local storage for the entry values, used for printing. */
665 /** The bin of this stat. */
667 /** The parameters for this stat. */
672 * Retrieve the storage from the bin for the given index.
673 * @param index The vector index to access.
674 * @return The storage object at the given index.
676 storage_t *data(int index) { return bin.data(index, params); }
678 * Retrieve a const pointer to the storage from the bin
679 * for the given index.
680 * @param index The vector index to access.
681 * @return A const pointer to the storage object at the given index.
683 const storage_t *data(int index) const {
684 return (const_cast<bin_t *>(&bin))->data(index, params);
688 // Copying stats is not allowed
689 /** Copying stats isn't allowed. */
690 VectorBase(const VectorBase &stat);
691 /** Copying stats isn't allowed. */
692 const VectorBase &operator=(const VectorBase &);
696 * Copy the values to a local vector and return a reference to it.
697 * @return A reference to a vector of the stat values.
699 const rvec_t &val() const {
703 vec = new rvec_t(size());
705 for (int i = 0; i < size(); ++i)
706 (*vec)[i] = data(i)->val(params);
712 * Return a total of all entries in this vector.
713 * @return The total of all vector entries.
715 result_t total() const {
716 result_t total = 0.0;
717 for (int i = 0; i < size(); ++i)
718 total += data(i)->val(params);
724 * Create this vector and register it with the database.
726 VectorBase() : VectorStat(true), vec(NULL) {}
730 ~VectorBase() { if (vec) delete vec; }
733 * Set this vector to have the given size.
734 * @param size The new size.
735 * @return A reference to this stat.
737 VectorBase &init(size_t size) {
738 bin.init(size, params);
744 /** Friend this class with the associated scalar proxy. */
745 friend class ScalarProxy<T, Storage, Bin>;
748 * Return a reference (ScalarProxy) to the stat at the given index.
749 * @param index The vector index to access.
750 * @return A reference of the stat.
752 ScalarProxy<T, Storage, Bin> operator[](int index);
755 * Return the number of elements in this vector.
756 * @return The size of the vector.
758 virtual size_t size() const { return bin.size(); }
760 * Reset stat value to default
762 virtual void reset() { bin.reset(); }
766 * A proxy class to access the stat at a given index in a VectorBase stat.
767 * Behaves like a ScalarBase.
769 template <typename T, template <typename T> class Storage, class Bin>
770 class ScalarProxy : public ScalarStat
773 /** Define the type of the storage class. */
774 typedef Storage<T> storage_t;
775 /** Define the params of the storage class. */
776 typedef typename storage_t::Params params_t;
777 /** Define the bin type. */
778 typedef typename Bin::VectorBin<storage_t> bin_t;
781 /** Pointer to the bin in the parent VectorBase. */
783 /** Pointer to the params in the parent VectorBase. */
785 /** The index to access in the parent VectorBase. */
790 * Retrieve the storage from the bin.
791 * @return The storage from the bin for this stat.
793 storage_t *data() { return bin->data(index, *params); }
795 * Retrieve a const pointer to the storage from the bin.
796 * @return A const pointer to the storage for this stat.
798 const storage_t *data() const { return bin->data(index, *params); }
802 * Return the current value of this statas a result type.
803 * @return The current value.
805 result_t val() const { return data()->val(*params); }
807 * Return the current value of this stat as its base type.
808 * @return The current value.
810 T value() const { return data()->value(*params); }
814 * Create and initialize this proxy, do not register it with the database.
815 * @param b The bin to use.
816 * @param p The params to use.
817 * @param i The index to access.
819 ScalarProxy(bin_t &b, params_t &p, int i)
820 : ScalarStat(false), bin(&b), params(&p), index(i) {}
822 * Create a copy of the provided ScalarProxy.
823 * @param sp The proxy to copy.
825 ScalarProxy(const ScalarProxy &sp)
826 : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {}
828 * Set this proxy equal to the provided one.
829 * @param sp The proxy to copy.
830 * @return A reference to this proxy.
832 const ScalarProxy &operator=(const ScalarProxy &sp) {
840 // Common operators for stats
842 * Increment the stat by 1. This calls the associated storage object inc
845 void operator++() { data()->inc(1, *params); }
847 * Decrement the stat by 1. This calls the associated storage object dec
850 void operator--() { data()->dec(1, *params); }
852 /** Increment the stat by 1. */
853 void operator++(int) { ++*this; }
854 /** Decrement the stat by 1. */
855 void operator--(int) { --*this; }
858 * Set the data value to the given value. This calls the associated storage
859 * object set function.
860 * @param v The new value.
862 template <typename U>
863 void operator=(const U& v) { data()->set(v, *params); }
866 * Increment the stat by the given value. This calls the associated
867 * storage object inc function.
868 * @param v The value to add.
870 template <typename U>
871 void operator+=(const U& v) { data()->inc(v, *params); }
874 * Decrement the stat by the given value. This calls the associated
875 * storage object dec function.
876 * @param v The value to substract.
878 template <typename U>
879 void operator-=(const U& v) { data()->dec(v, *params); }
882 * Return the number of elements, always 1 for a scalar.
885 virtual size_t size() const { return 1; }
887 * This stat has no state. Nothing to reset
889 virtual void reset() { }
892 template <typename T, template <typename T> class Storage, class Bin>
893 inline ScalarProxy<T, Storage, Bin>
894 VectorBase<T, Storage, Bin>::operator[](int index)
896 assert (index >= 0 && index < size());
897 return ScalarProxy<T, Storage, Bin>(bin, params, index);
900 template <typename T, template <typename T> class Storage, class Bin>
903 template <typename T, template <typename T> class Storage, class Bin>
904 class Vector2dBase : public Stat
907 typedef Storage<T> storage_t;
908 typedef typename storage_t::Params params_t;
909 typedef typename Bin::VectorBin<storage_t> bin_t;
916 std::vector<std::string> *y_subnames;
919 storage_t *data(int index) { return bin.data(index, params); }
920 const storage_t *data(int index) const {
921 return (const_cast<bin_t *>(&bin))->data(index, params);
925 // Copying stats is not allowed
926 Vector2dBase(const Vector2dBase &stat);
927 const Vector2dBase &operator=(const Vector2dBase &);
930 Vector2dBase() : Stat(true) {}
933 Vector2dBase &init(size_t _x, size_t _y) {
936 bin.init(x * y, params);
938 y_subnames = new std::vector<std::string>(y);
944 * @warning This makes the assumption that if you're gonna subnames a 2d
945 * vector, you're subnaming across all y
947 Vector2dBase &ysubnames(const char **names)
949 for (int i=0; i < y; ++i) {
950 (*y_subnames)[i] = names[i];
954 Vector2dBase &ysubname(int index, const std::string subname)
956 (*y_subnames)[i] = subname.c_str();
959 std::string ysubname(int i) const { return (*y_subnames)[i]; }
961 friend class VectorProxy<T, Storage, Bin>;
962 VectorProxy<T, Storage, Bin> operator[](int index);
964 virtual size_t size() const { return bin.size(); }
965 virtual bool zero() const { return data(0)->value(params) == 0.0; }
968 display(std::ostream &out) const
970 bool have_subname = false;
971 for (int i = 0; i < x; ++i) {
972 if (!mysubname(i).empty())
977 result_t super_total = 0.0;
978 for (int i = 0; i < x; ++i) {
981 subname = mysubname(i);
985 subname = to_string(i);
990 result_t total = 0.0;
991 for (int j = 0; j < y; ++j) {
992 vec[j] = data(iy + j)->val(params);
993 tot_vec[j] += vec[j];
995 super_total += vec[j];
999 if (mysubdesc(i).empty()) {
1002 desc = mysubdesc(i);
1005 VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0,
1006 myprecision(), myflags(), vec, total);
1009 if ((myflags() & ::Statistics::total) && (x > 1)) {
1010 VectorDisplay(out, myname(), y_subnames, mydesc(), 0,
1011 myprecision(), myflags(), tot_vec, super_total);
1016 * Reset stat value to default
1018 virtual void reset() { bin.reset(); }
1021 template <typename T, template <typename T> class Storage, class Bin>
1022 class VectorProxy : public VectorStat
1025 typedef Storage<T> storage_t;
1026 typedef typename storage_t::Params params_t;
1027 typedef typename Bin::VectorBin<storage_t> bin_t;
1036 mutable rvec_t *vec;
1038 storage_t *data(int index) {
1039 assert(index < len);
1040 return bin->data(offset + index, *params);
1043 const storage_t *data(int index) const {
1044 return (const_cast<bin_t *>(bin))->data(offset + index, *params);
1048 const rvec_t &val() const {
1050 vec->resize(size());
1052 vec = new rvec_t(size());
1054 for (int i = 0; i < size(); ++i)
1055 (*vec)[i] = data(i)->val(*params);
1060 result_t total() const {
1061 result_t total = 0.0;
1062 for (int i = 0; i < size(); ++i)
1063 total += data(i)->val(*params);
1068 VectorProxy(bin_t &b, params_t &p, int o, int l)
1069 : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL)
1071 VectorProxy(const VectorProxy &sp)
1072 : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset),
1073 len(sp.len), vec(NULL)
1080 const VectorProxy &operator=(const VectorProxy &sp) {
1091 virtual size_t size() const { return len; }
1093 ScalarProxy<T, Storage, Bin> operator[](int index) {
1094 assert (index >= 0 && index < size());
1095 return ScalarProxy<T, Storage, Bin>(*bin, *params, offset + index);
1099 * This stat has no state. Nothing to reset.
1101 virtual void reset() { }
1104 template <typename T, template <typename T> class Storage, class Bin>
1105 inline VectorProxy<T, Storage, Bin>
1106 Vector2dBase<T, Storage, Bin>::operator[](int index)
1108 int offset = index * y;
1109 assert (index >= 0 && offset < size());
1110 return VectorProxy<T, Storage, Bin>(bin, params, offset, y);
1113 //////////////////////////////////////////////////////////////////////
1115 // Non formula statistics
1117 //////////////////////////////////////////////////////////////////////
1119 void DistDisplay(std::ostream &stream, const std::string &name,
1120 const std::string &desc, int precision, FormatFlags flags,
1121 result_t min_val, result_t max_val,
1122 result_t underflow, result_t overflow,
1123 const rvec_t &vec, int min, int max, int bucket_size,
1126 * Templatized storage and interface for a distrbution stat.
1128 template <typename T>
1132 /** The parameters for a distribution stat. */
1135 /** The minimum value to track. */
1137 /** The maximum value to track. */
1139 /** The number of entries in each bucket. */
1141 /** The number of buckets. Equal to (max-min)/bucket_size. */
1146 /** The smallest value sampled. */
1148 /** The largest value sampled. */
1150 /** The number of values sampled less than min. */
1152 /** The number of values sampled more than max. */
1154 /** Counter for each bucket. */
1159 * Construct this storage with the supplied params.
1160 * @param params The parameters.
1162 DistStor(const Params ¶ms)
1163 : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0),
1170 * Add a value to the distribution for the given number of times.
1171 * @param val The value to add.
1172 * @param number The number of times to add the value.
1173 * @param params The paramters of the distribution.
1175 void sample(T val, int number, const Params ¶ms) {
1176 if (val < params.min)
1177 underflow += number;
1178 else if (val > params.max)
1181 int index = (val - params.min) / params.bucket_size;
1182 assert(index < size(params));
1183 vec[index] += number;
1194 * Return the number of buckets in this distribution.
1195 * @return the number of buckets.
1196 * @todo Is it faster to return the size from the parameters?
1198 size_t size(const Params &) const { return vec.size(); }
1201 * Returns true if any calls to sample have been made.
1202 * @param params The paramters of the distribution.
1203 * @return True if any values have been sampled.
1205 bool zero(const Params ¶ms) const {
1206 if (underflow != 0 || overflow != 0)
1209 int s = size(params);
1210 for (int i = 0; i < s; i++)
1218 * Print this distribution and the given print data to the given ostream.
1219 * @param stream The output stream.
1220 * @param name The name of this stat (from StatData).
1221 * @param desc The description of this stat (from StatData).
1222 * @param precision The print precision (from StatData).
1223 * @param flags The format flags (from StatData).
1224 * @param params The paramters of this distribution.
1226 void display(std::ostream &stream, const std::string &name,
1227 const std::string &desc, int precision, FormatFlags flags,
1228 const Params ¶ms) const {
1230 #ifdef STAT_DISPLAY_COMPAT
1231 result_t min = params.min;
1233 result_t min = (min_val == INT_MAX) ? params.min : min_val;
1235 result_t max = (max_val == INT_MIN) ? 0 : max_val;
1237 rvec_t rvec(params.size);
1238 for (int i = 0; i < params.size; ++i)
1241 DistDisplay(stream, name, desc, precision, flags,
1242 (result_t)min, (result_t)max,
1243 (result_t)underflow, (result_t)overflow,
1244 rvec, params.min, params.max, params.bucket_size,
1248 * Reset stat value to default
1257 int size = vec.size();
1258 for (int i = 0; i < size; ++i)
1264 void FancyDisplay(std::ostream &stream, const std::string &name,
1265 const std::string &desc, int precision, FormatFlags flags,
1266 result_t mean, result_t variance);
1269 * Templatized storage and interface for a distribution that calculates mean
1272 template <typename T>
1277 * No paramters for this storage.
1282 /** The current sum. */
1284 /** The sum of squares. */
1286 /** The total number of samples. */
1291 * Create and initialize this storage.
1293 FancyStor(const Params &) : sum(T()), squares(T()), total(0) {}
1296 * Add a value the given number of times to this running average.
1297 * Update the running sum and sum of squares, increment the number of
1298 * values seen by the given number.
1299 * @param val The value to add.
1300 * @param number The number of times to add the value.
1301 * @param p The parameters of this stat.
1303 void sample(T val, int number, const Params &p) {
1304 T value = val * number;
1306 squares += value * value;
1311 * Print this distribution and the given print data to the given ostream.
1312 * @param stream The output stream.
1313 * @param name The name of this stat (from StatData).
1314 * @param desc The description of this stat (from StatData).
1315 * @param precision The print precision (from StatData).
1316 * @param flags The format flags (from StatData).
1317 * @param params The paramters of this distribution.
1319 void display(std::ostream &stream, const std::string &name,
1320 const std::string &desc, int precision, FormatFlags flags,
1321 const Params ¶ms) const {
1323 result_t mean = NAN;
1324 result_t variance = NAN;
1327 result_t fsum = sum;
1328 result_t fsq = squares;
1329 result_t ftot = total;
1332 variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0));
1335 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1339 * Return the number of entries in this stat, 1
1342 size_t size(const Params &) const { return 1; }
1344 * Return true if no samples have been added.
1345 * @return True if no samples have been added.
1347 bool zero(const Params &) const { return total == 0; }
1349 * Reset stat value to default
1351 virtual void reset()
1360 * Templatized storage for distribution that calculates per cycle mean and
1363 template <typename T>
1367 /** No parameters for this storage. */
1371 /** Current total. */
1373 /** Current sum of squares. */
1378 * Create and initialize this storage.
1380 AvgFancy(const Params &) : sum(T()), squares(T()) {}
1383 * Add a value to the distribution for the given number of times.
1384 * Update the running sum and sum of squares.
1385 * @param val The value to add.
1386 * @param number The number of times to add the value.
1387 * @param p The paramters of the distribution.
1389 void sample(T val, int number, const Params& p) {
1390 T value = val * number;
1392 squares += value * value;
1396 * Print this distribution and the given print data to the given ostream.
1397 * @param stream The output stream.
1398 * @param name The name of this stat (from StatData).
1399 * @param desc The description of this stat (from StatData).
1400 * @param precision The print precision (from StatData).
1401 * @param flags The format flags (from StatData).
1402 * @param params The paramters of this distribution.
1404 void display(std::ostream &stream, const std::string &name,
1405 const std::string &desc, int precision, FormatFlags flags,
1406 const Params ¶ms) const {
1407 result_t mean = sum / curTick;
1408 result_t variance = (squares - sum * sum) / curTick;
1410 FancyDisplay(stream, name, desc, precision, flags, mean, variance);
1414 * Return the number of entries, in this case 1.
1417 size_t size(const Params ¶ms) const { return 1; }
1419 * Return true if no samples have been added.
1420 * @return True if the sum is zero.
1422 bool zero(const Params ¶ms) const { return sum == 0; }
1424 * Reset stat value to default
1426 virtual void reset()
1434 * Implementation of a distribution stat. The type of distribution is
1435 * determined by the Storage template. @sa ScalarBase
1437 template <typename T, template <typename T> class Storage, class Bin>
1438 class DistBase : public Stat
1441 /** Define the type of the storage class. */
1442 typedef Storage<T> storage_t;
1443 /** Define the params of the storage class. */
1444 typedef typename storage_t::Params params_t;
1445 /** Define the bin type. */
1446 typedef typename Bin::Bin<storage_t> bin_t;
1449 /** The bin of this stat. */
1451 /** The parameters for this stat. */
1456 * Retrieve the storage from the bin.
1457 * @return The storage object for this stat.
1459 storage_t *data() { return bin.data(params); }
1461 * Retrieve a const pointer to the storage from the bin.
1462 * @return A const pointer to the storage object for this stat.
1464 const storage_t *data() const {
1465 return (const_cast<bin_t *>(&bin))->data(params);
1469 // Copying stats is not allowed
1470 /** Copies are not allowed. */
1471 DistBase(const DistBase &stat);
1472 /** Copies are not allowed. */
1473 const DistBase &operator=(const DistBase &);
1477 * Create this distrubition and register it with the database.
1479 DistBase() : Stat(true) { }
1486 * Add a value to the distribtion n times. Calls sample on the storage
1488 * @param v The value to add.
1489 * @param n The number of times to add it, defaults to 1.
1491 template <typename U>
1492 void sample(const U& v, int n = 1) { data()->sample(v, n, params); }
1495 * Return the number of entries in this stat.
1496 * @return The number of entries.
1498 virtual size_t size() const { return data()->size(params); }
1500 * Return true if no samples have been added.
1501 * @return True if there haven't been any samples.
1503 virtual bool zero() const { return data()->zero(params); }
1505 * Print this distribution to the given ostream.
1506 * @param stream The output stream.
1508 virtual void display(std::ostream &stream) const {
1509 data()->display(stream, myname(), mydesc(), myprecision(), myflags(),
1513 * Reset stat value to default
1515 virtual void reset()
1521 template <typename T, template <typename T> class Storage, class Bin>
1524 template <typename T, template <typename T> class Storage, class Bin>
1525 class VectorDistBase : public Stat
1528 typedef Storage<T> storage_t;
1529 typedef typename storage_t::Params params_t;
1530 typedef typename Bin::VectorBin<storage_t> bin_t;
1537 storage_t *data(int index) { return bin.data(index, params); }
1538 const storage_t *data(int index) const {
1539 return (const_cast<bin_t *>(&bin))->data(index, params);
1543 // Copying stats is not allowed
1544 VectorDistBase(const VectorDistBase &stat);
1545 const VectorDistBase &operator=(const VectorDistBase &);
1548 VectorDistBase() : Stat(true) { }
1549 ~VectorDistBase() { }
1551 friend class DistProxy<T, Storage, Bin>;
1552 DistProxy<T, Storage, Bin> operator[](int index);
1553 const DistProxy<T, Storage, Bin> operator[](int index) const;
1555 virtual size_t size() const { return bin.size(); }
1556 virtual bool zero() const { return false; }
1557 virtual void display(std::ostream &stream) const;
1559 * Reset stat value to default
1561 virtual void reset()
1567 template <typename T, template <typename T> class Storage, class Bin>
1568 class DistProxy : public Stat
1571 typedef Storage<T> storage_t;
1572 typedef typename storage_t::Params params_t;
1573 typedef typename Bin::Bin<storage_t> bin_t;
1574 typedef VectorDistBase<T, Storage, Bin> base_t;
1579 const base_t *cstat;
1584 storage_t *data() { return stat->data(index); }
1585 const storage_t *data() const { return cstat->data(index); }
1588 DistProxy(const VectorDistBase<T, Storage, Bin> &s, int i)
1589 : Stat(false), cstat(&s), index(i) {}
1590 DistProxy(const DistProxy &sp)
1591 : Stat(false), cstat(sp.cstat), index(sp.index) {}
1592 const DistProxy &operator=(const DistProxy &sp) {
1593 cstat = sp.cstat; index = sp.index; return *this;
1597 template <typename U>
1598 void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); }
1600 virtual size_t size() const { return 1; }
1601 virtual bool zero() const {
1602 return data()->zero(cstat->params);
1604 virtual void display(std::ostream &stream) const {
1605 std::stringstream name, desc;
1607 if (!(cstat->mysubname(index).empty())) {
1608 name << cstat->myname() << cstat->mysubname(index);
1610 name << cstat->myname() << "_" << index;
1612 if (!(cstat->mysubdesc(index).empty())) {
1613 desc << cstat->mysubdesc(index);
1615 desc << cstat->mydesc();
1618 data()->display(stream, name.str(), desc.str(),
1619 cstat->myprecision(), cstat->myflags(), cstat->params);
1622 * Proxy has no state. Nothing to reset.
1624 virtual void reset() { }
1627 template <typename T, template <typename T> class Storage, class Bin>
1628 inline DistProxy<T, Storage, Bin>
1629 VectorDistBase<T, Storage, Bin>::operator[](int index)
1631 assert (index >= 0 && index < size());
1632 return DistProxy<T, Storage, Bin>(*this, index);
1635 template <typename T, template <typename T> class Storage, class Bin>
1636 inline const DistProxy<T, Storage, Bin>
1637 VectorDistBase<T, Storage, Bin>::operator[](int index) const
1639 assert (index >= 0 && index < size());
1640 return DistProxy<T, Storage, Bin>(*this, index);
1644 * @todo Need a way to print Distribution totals across the Vector
1646 template <typename T, template <typename T> class Storage, class Bin>
1648 VectorDistBase<T, Storage, Bin>::display(std::ostream &stream) const
1650 for (int i = 0; i < size(); ++i) {
1651 DistProxy<T, Storage, Bin> proxy(*this, i);
1652 proxy.display(stream);
1658 VectorDistBase<T, Storage, Bin>::total(int index) const
1661 for (int i=0; i < x_size(); ++i) {
1662 total += data(i)->val(*params);
1667 //////////////////////////////////////////////////////////////////////
1671 //////////////////////////////////////////////////////////////////////
1674 * Base class for formula statistic node. These nodes are used to build a tree
1675 * that represents the formula.
1677 class Node : public RefCounted
1681 * Return the number of nodes in the subtree starting at this node.
1682 * @return the number of nodes in this subtree.
1684 virtual size_t size() const = 0;
1686 * Return the result vector of this subtree.
1687 * @return The result vector of this subtree.
1689 virtual const rvec_t &val() const = 0;
1691 * Return the total of the result vector.
1692 * @return The total of the result vector.
1694 virtual result_t total() const = 0;
1697 /** Reference counting pointer to a function Node. */
1698 typedef RefCountingPtr<Node> NodePtr;
1700 class ScalarStatNode : public Node
1703 const ScalarStat &stat;
1704 mutable rvec_t result;
1707 ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {}
1708 const rvec_t &val() const { result[0] = stat.val(); return result; }
1709 virtual result_t total() const { return stat.val(); };
1711 virtual size_t size() const { return 1; }
1714 template <typename T, template <typename T> class Storage, class Bin>
1715 class ScalarProxyNode : public Node
1718 const ScalarProxy<T, Storage, Bin> proxy;
1719 mutable rvec_t result;
1722 ScalarProxyNode(const ScalarProxy<T, Storage, Bin> &p)
1723 : proxy(p), result(1) { }
1724 const rvec_t &val() const { result[0] = proxy.val(); return result; }
1725 virtual result_t total() const { return proxy.val(); };
1727 virtual size_t size() const { return 1; }
1730 class VectorStatNode : public Node
1733 const VectorStat &stat;
1736 VectorStatNode(const VectorStat &s) : stat(s) {}
1737 const rvec_t &val() const { return stat.val(); }
1738 virtual result_t total() const { return stat.total(); };
1740 virtual size_t size() const { return stat.size(); }
1743 template <typename T>
1744 class ConstNode : public Node
1750 ConstNode(T s) : data(1, (result_t)s) {}
1751 const rvec_t &val() const { return data; }
1752 virtual result_t total() const { return data[0]; };
1754 virtual size_t size() const { return 1; }
1757 template <typename T>
1758 class FunctorNode : public Node
1762 mutable rvec_t result;
1765 FunctorNode(T &f) : functor(f) { result.resize(1); }
1766 const rvec_t &val() const {
1767 result[0] = (result_t)functor();
1770 virtual result_t total() const { return (result_t)functor(); };
1772 virtual size_t size() const { return 1; }
1775 template <typename T>
1776 class ScalarNode : public Node
1780 mutable rvec_t result;
1783 ScalarNode(T &s) : scalar(s) { result.resize(1); }
1784 const rvec_t &val() const {
1785 result[0] = (result_t)scalar;
1788 virtual result_t total() const { return (result_t)scalar; };
1790 virtual size_t size() const { return 1; }
1794 class UnaryNode : public Node
1798 mutable rvec_t result;
1801 UnaryNode(NodePtr p) : l(p) {}
1803 const rvec_t &val() const {
1804 const rvec_t &lvec = l->val();
1805 int size = lvec.size();
1809 result.resize(size);
1811 for (int i = 0; i < size; ++i)
1812 result[i] = op(lvec[i]);
1817 result_t total() const {
1819 return op(l->total());
1822 virtual size_t size() const { return l->size(); }
1826 class BinaryNode : public Node
1831 mutable rvec_t result;
1834 BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {}
1836 const rvec_t &val() const {
1838 const rvec_t &lvec = l->val();
1839 const rvec_t &rvec = r->val();
1841 assert(lvec.size() > 0 && rvec.size() > 0);
1843 if (lvec.size() == 1 && rvec.size() == 1) {
1845 result[0] = op(lvec[0], rvec[0]);
1846 } else if (lvec.size() == 1) {
1847 int size = rvec.size();
1848 result.resize(size);
1849 for (int i = 0; i < size; ++i)
1850 result[i] = op(lvec[0], rvec[i]);
1851 } else if (rvec.size() == 1) {
1852 int size = lvec.size();
1853 result.resize(size);
1854 for (int i = 0; i < size; ++i)
1855 result[i] = op(lvec[i], rvec[0]);
1856 } else if (rvec.size() == lvec.size()) {
1857 int size = rvec.size();
1858 result.resize(size);
1859 for (int i = 0; i < size; ++i)
1860 result[i] = op(lvec[i], rvec[i]);
1866 result_t total() const {
1868 return op(l->total(), r->total());
1871 virtual size_t size() const {
1879 assert(ls == rs && "Node vector sizes are not equal");
1886 class SumNode : public Node
1890 mutable rvec_t result;
1893 SumNode(NodePtr p) : l(p), result(1) {}
1895 const rvec_t &val() const {
1896 const rvec_t &lvec = l->val();
1897 int size = lvec.size();
1903 for (int i = 0; i < size; ++i)
1904 result[0] = op(result[0], lvec[i]);
1909 result_t total() const {
1910 const rvec_t &lvec = l->val();
1911 int size = lvec.size();
1914 result_t result = 0.0;
1917 for (int i = 0; i < size; ++i)
1918 result = op(result, lvec[i]);
1923 virtual size_t size() const { return 1; }
1927 * Helper class to construct formula node trees.
1933 * Pointer to a Node object.
1939 * Copy the given pointer to this class.
1940 * @param n A pointer to a Node object to copy.
1942 Temp(NodePtr n) : node(n) {}
1944 * Create a new ScalarStatNode.
1945 * @param s The ScalarStat to place in a node.
1947 Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {}
1949 * Create a new ScalarProxyNode.
1950 * @param p The ScalarProxy to place in a node.
1952 template <typename T, template <typename T> class Storage, class Bin>
1953 Temp(const ScalarProxy<T, Storage, Bin> &p)
1954 : node(new ScalarProxyNode<T, Storage, Bin>(p)) {}
1956 * Create a new VectorStatNode.
1957 * @param s The VectorStat to place in a node.
1959 Temp(const VectorStat &s) : node(new VectorStatNode(s)) {}
1962 * Create a ConstNode
1963 * @param value The value of the const node.
1965 Temp(signed char value) : node(new ConstNode<signed char>(value)) {}
1967 * Create a ConstNode
1968 * @param value The value of the const node.
1970 Temp(unsigned char value) : node(new ConstNode<unsigned char>(value)) {}
1972 * Create a ConstNode
1973 * @param value The value of the const node.
1975 Temp(signed short value) : node(new ConstNode<signed short>(value)) {}
1977 * Create a ConstNode
1978 * @param value The value of the const node.
1980 Temp(unsigned short value) : node(new ConstNode<unsigned short>(value)) {}
1982 * Create a ConstNode
1983 * @param value The value of the const node.
1985 Temp(signed int value) : node(new ConstNode<signed int>(value)) {}
1987 * Create a ConstNode
1988 * @param value The value of the const node.
1990 Temp(unsigned int value) : node(new ConstNode<unsigned int>(value)) {}
1992 * Create a ConstNode
1993 * @param value The value of the const node.
1995 Temp(signed long value) : node(new ConstNode<signed long>(value)) {}
1997 * Create a ConstNode
1998 * @param value The value of the const node.
2000 Temp(unsigned long value) : node(new ConstNode<unsigned long>(value)) {}
2002 * Create a ConstNode
2003 * @param value The value of the const node.
2005 Temp(signed long long value)
2006 : node(new ConstNode<signed long long>(value)) {}
2008 * Create a ConstNode
2009 * @param value The value of the const node.
2011 Temp(unsigned long long value)
2012 : node(new ConstNode<unsigned long long>(value)) {}
2014 * Create a ConstNode
2015 * @param value The value of the const node.
2017 Temp(float value) : node(new ConstNode<float>(value)) {}
2019 * Create a ConstNode
2020 * @param value The value of the const node.
2022 Temp(double value) : node(new ConstNode<double>(value)) {}
2025 * Return the node pointer.
2026 * @return the node pointer.
2028 operator NodePtr() { return node;}
2032 //////////////////////////////////////////////////////////////////////
2034 // Binning Interface
2036 //////////////////////////////////////////////////////////////////////
2045 off_t size() const { return memsize; }
2049 BinBase(size_t size);
2053 } // namespace Detail
2055 template <class BinType>
2056 struct StatBin : public Detail::BinBase
2058 static StatBin *&curBin() {
2059 static StatBin *current = NULL;
2063 static void setCurBin(StatBin *bin) { curBin() = bin; }
2064 static StatBin *current() { assert(curBin()); return curBin(); }
2066 static off_t &offset() {
2067 static off_t offset = 0;
2071 static off_t new_offset(size_t size) {
2072 size_t mask = sizeof(u_int64_t) - 1;
2073 off_t off = offset();
2075 // That one is for the last trailing flags byte.
2076 offset() += (size + 1 + mask) & ~mask;
2081 explicit StatBin(size_t size = 1024) : Detail::BinBase(size) {}
2083 char *memory(off_t off) {
2084 assert(offset() <= size());
2085 return Detail::BinBase::memory() + off;
2088 static void activate(StatBin &bin) { setCurBin(&bin); }
2096 BinBase() : offset(-1) {}
2097 void allocate(size_t size) {
2098 offset = new_offset(size);
2101 assert(offset != -1);
2102 return current()->memory(offset);
2106 template <class Storage>
2107 class Bin : public BinBase
2110 typedef typename Storage::Params Params;
2113 Bin() { allocate(sizeof(Storage)); }
2114 bool initialized() const { return true; }
2115 void init(const Params ¶ms) { }
2117 int size() const { return 1; }
2119 Storage *data(const Params ¶ms) {
2120 assert(initialized());
2121 char *ptr = access();
2122 char *flags = ptr + sizeof(Storage);
2123 if (!(*flags & 0x1)) {
2125 new (ptr) Storage(params);
2127 return reinterpret_cast<Storage *>(ptr);
2131 char *ptr = access();
2132 char *flags = ptr + size() * sizeof(Storage);
2133 if (!(*flags & 0x1))
2136 Storage *s = reinterpret_cast<Storage *>(ptr);
2141 template <class Storage>
2142 class VectorBin : public BinBase
2145 typedef typename Storage::Params Params;
2151 VectorBin() : _size(0) {}
2153 bool initialized() const { return _size > 0; }
2154 void init(int s, const Params ¶ms) {
2155 assert(!initialized());
2158 allocate(_size * sizeof(Storage));
2161 int size() const { return _size; }
2163 Storage *data(int index, const Params ¶ms) {
2164 assert(initialized());
2165 assert(index >= 0 && index < size());
2166 char *ptr = access();
2167 char *flags = ptr + size() * sizeof(Storage);
2168 if (!(*flags & 0x1)) {
2170 for (int i = 0; i < size(); ++i)
2171 new (ptr + i * sizeof(Storage)) Storage(params);
2173 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2177 char *ptr = access();
2178 char *flags = ptr + size() * sizeof(Storage);
2179 if (!(*flags & 0x1))
2182 for (int i = 0; i < _size; ++i) {
2183 char *p = ptr + i * sizeof(Storage);
2184 Storage *s = reinterpret_cast<Storage *>(p);
2191 class MainBinType {};
2192 typedef StatBin<MainBinType> MainBin;
2196 template <class Storage>
2200 typedef typename Storage::Params Params;
2203 char ptr[sizeof(Storage)];
2208 reinterpret_cast<Storage *>(ptr)->~Storage();
2211 bool initialized() const { return true; }
2212 void init(const Params ¶ms) {
2213 new (ptr) Storage(params);
2215 int size() const{ return 1; }
2216 Storage *data(const Params ¶ms) {
2217 assert(initialized());
2218 return reinterpret_cast<Storage *>(ptr);
2222 Storage *s = reinterpret_cast<Storage *>(ptr);
2227 template <class Storage>
2231 typedef typename Storage::Params Params;
2238 VectorBin() : ptr(NULL) { }
2244 for (int i = 0; i < _size; ++i) {
2245 char *p = ptr + i * sizeof(Storage);
2246 reinterpret_cast<Storage *>(p)->~Storage();
2251 bool initialized() const { return ptr != NULL; }
2252 void init(int s, const Params ¶ms) {
2253 assert(s > 0 && "size must be positive!");
2254 assert(!initialized());
2256 ptr = new char[_size * sizeof(Storage)];
2257 for (int i = 0; i < _size; ++i)
2258 new (ptr + i * sizeof(Storage)) Storage(params);
2261 int size() const { return _size; }
2263 Storage *data(int index, const Params ¶ms) {
2264 assert(initialized());
2265 assert(index >= 0 && index < size());
2266 return reinterpret_cast<Storage *>(ptr + index * sizeof(Storage));
2270 for (int i = 0; i < _size; ++i) {
2271 char *p = ptr + i * sizeof(Storage);
2272 Storage *s = reinterpret_cast<Storage *>(p);
2279 //////////////////////////////////////////////////////////////////////
2281 // Visible Statistics Types
2283 //////////////////////////////////////////////////////////////////////
2285 * @defgroup VisibleStats "Statistic Types"
2286 * These are the statistics that are used in the simulator. By default these
2287 * store counters and don't use binning, but are templatized to accept any type
2288 * and any Bin class.
2293 * This is a simple scalar statistic, like a counter.
2294 * @sa Stat, ScalarBase, StatStor
2296 template <typename T = Counter, class Bin = NoBin>
2297 class Scalar : public Detail::ScalarBase<T, Detail::StatStor, Bin>
2300 /** The base implementation. */
2301 typedef Detail::ScalarBase<T, Detail::StatStor, Bin> Base;
2304 * Sets the stat equal to the given value. Calls the base implementation
2306 * @param v The new value.
2308 template <typename U>
2309 void operator=(const U& v) { Base::operator=(v); }
2313 * A stat that calculates the per cycle average of a value.
2314 * @sa Stat, ScalarBase, AvgStor
2316 template <typename T = Counter, class Bin = NoBin>
2317 class Average : public Detail::ScalarBase<T, Detail::AvgStor, Bin>
2320 /** The base implementation. */
2321 typedef Detail::ScalarBase<T, Detail::AvgStor, Bin> Base;
2324 * Sets the stat equal to the given value. Calls the base implementation
2326 * @param v The new value.
2328 template <typename U>
2329 void operator=(const U& v) { Base::operator=(v); }
2333 * A vector of scalar stats.
2334 * @sa Stat, VectorBase, StatStor
2336 template <typename T = Counter, class Bin = NoBin>
2337 class Vector : public Detail::VectorBase<T, Detail::StatStor, Bin>
2341 * A vector of Average stats.
2342 * @sa Stat, VectorBase, AvgStor
2344 template <typename T = Counter, class Bin = NoBin>
2345 class AverageVector : public Detail::VectorBase<T, Detail::AvgStor, Bin>
2349 * A 2-Dimensional vecto of scalar stats.
2350 * @sa Stat, Vector2dBase, StatStor
2352 template <typename T = Counter, class Bin = NoBin>
2353 class Vector2d : public Detail::Vector2dBase<T, Detail::StatStor, Bin>
2357 * A simple distribution stat.
2358 * @sa Stat, DistBase, DistStor
2360 template <typename T = Counter, class Bin = NoBin>
2361 class Distribution : public Detail::DistBase<T, Detail::DistStor, Bin>
2364 /** Base implementation. */
2365 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2366 /** The Parameter type. */
2367 typedef typename Detail::DistStor<T>::Params Params;
2371 * Set the parameters of this distribution. @sa DistStor::Params
2372 * @param min The minimum value of the distribution.
2373 * @param max The maximum value of the distribution.
2374 * @param bkt The number of values in each bucket.
2375 * @return A reference to this distribution.
2377 Distribution &init(T min, T max, int bkt) {
2380 params.bucket_size = bkt;
2381 params.size = (max - min) / bkt + 1;
2390 * Calculates the mean and variance of all the samples.
2391 * @sa Stat, DistBase, FancyStor
2393 template <typename T = Counter, class Bin = NoBin>
2394 class StandardDeviation : public Detail::DistBase<T, Detail::FancyStor, Bin>
2397 /** The base implementation */
2398 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2399 /** The parameter type. */
2400 typedef typename Detail::DistStor<T>::Params Params;
2404 * Construct and initialize this distribution.
2406 StandardDeviation() {
2413 * Calculates the per cycle mean and variance of the samples.
2414 * @sa Stat, DistBase, AvgFancy
2416 template <typename T = Counter, class Bin = NoBin>
2417 class AverageDeviation : public Detail::DistBase<T, Detail::AvgFancy, Bin>
2420 /** The base implementation */
2421 typedef Detail::DistBase<T, Detail::DistStor, Bin> Base;
2422 /** The parameter type. */
2423 typedef typename Detail::DistStor<T>::Params Params;
2427 * Construct and initialize this distribution.
2429 AverageDeviation() {
2436 * A vector of distributions.
2437 * @sa Stat, VectorDistBase, DistStor
2439 template <typename T = Counter, class Bin = NoBin>
2440 class VectorDistribution
2441 : public Detail::VectorDistBase<T, Detail::DistStor, Bin>
2444 /** The base implementation */
2445 typedef Detail::VectorDistBase<T, Detail::DistStor, Bin> Base;
2446 /** The parameter type. */
2447 typedef typename Detail::DistStor<T>::Params Params;
2451 * Initialize storage and parameters for this distribution.
2452 * @param size The size of the vector (the number of distributions).
2453 * @param min The minimum value of the distribution.
2454 * @param max The maximum value of the distribution.
2455 * @param bkt The number of values in each bucket.
2456 * @return A reference to this distribution.
2458 VectorDistribution &init(int size, T min, T max, int bkt) {
2461 params.bucket_size = bkt;
2462 params.size = (max - min) / bkt + 1;
2463 bin.init(size, params);
2471 * This is a vector of StandardDeviation stats.
2472 * @sa Stat, VectorDistBase, FancyStor
2474 template <typename T = Counter, class Bin = NoBin>
2475 class VectorStandardDeviation
2476 : public Detail::VectorDistBase<T, Detail::FancyStor, Bin>
2479 /** The base implementation */
2480 typedef Detail::VectorDistBase<T, Detail::FancyStor, Bin> Base;
2481 /** The parameter type. */
2482 typedef typename Detail::DistStor<T>::Params Params;
2486 * Initialize storage for this distribution.
2487 * @param size The size of the vector.
2488 * @return A reference to this distribution.
2490 VectorStandardDeviation &init(int size) {
2491 bin.init(size, params);
2499 * This is a vector of AverageDeviation stats.
2500 * @sa Stat, VectorDistBase, AvgFancy
2502 template <typename T = Counter, class Bin = NoBin>
2503 class VectorAverageDeviation
2504 : public Detail::VectorDistBase<T, Detail::AvgFancy, Bin>
2507 /** The base implementation */
2508 typedef Detail::VectorDistBase<T, Detail::AvgFancy, Bin> Base;
2509 /** The parameter type. */
2510 typedef typename Detail::DistStor<T>::Params Params;
2514 * Initialize storage for this distribution.
2515 * @param size The size of the vector.
2516 * @return A reference to this distribution.
2518 VectorAverageDeviation &init(int size) {
2519 bin.init(size, params);
2527 * A formula for statistics that is calculated when printed. A formula is
2528 * stored as a tree of Nodes that represent the equation to calculate.
2529 * @sa Stat, ScalarStat, VectorStat, Node, Detail::Temp
2531 class Formula : public Detail::VectorStat
2534 /** The root of the tree which represents the Formula */
2535 Detail::NodePtr root;
2536 friend class Statistics::Detail::Temp;
2540 * Create and initialize thie formula, and register it with the database.
2542 Formula() : VectorStat(true) { setInit(); }
2544 * Create a formula with the given root node, register it with the
2546 * @param r The root of the expression tree.
2548 Formula(Detail::Temp r) : VectorStat(true) {
2554 * Set an unitialized Formula to the given root.
2555 * @param r The root of the expression tree.
2556 * @return a reference to this formula.
2558 const Formula &operator=(Detail::Temp r) {
2559 assert(!root && "Can't change formulas");
2566 * Add the given tree to the existing one.
2567 * @param r The root of the expression tree.
2568 * @return a reference to this formula.
2570 const Formula &operator+=(Detail::Temp r) {
2571 using namespace Detail;
2573 root = NodePtr(new BinaryNode<std::plus<result_t> >(root, r));
2581 * Return the result of the Fomula in a vector. If there were no Vector
2582 * components to the Formula, then the vector is size 1. If there were,
2583 * like x/y with x being a vector of size 3, then the result returned will
2584 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2585 * @return The result vector.
2587 const rvec_t &val() const { return root->val(); }
2589 * Return the total Formula result. If there is a Vector component to this
2590 * Formula, then this is the result of the Formula if the formula is applied
2591 * after summing all the components of the Vector. For example, if Formula
2592 * is x/y where x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If there is no
2593 * Vector component, total() returns the same value as the first entry in the rvec_t
2595 * @return The total of the result vector.
2597 result_t total() const { return root->total(); }
2600 * Return the number of elements in the tree.
2602 size_t size() const {
2606 return root->size();
2610 * Formulas don't need to be reset
2612 virtual void reset() {}
2620 void dump(std::ostream &stream);
2622 void regReset(Callback *cb);
2625 operator+(Detail::Temp l, Detail::Temp r)
2627 using namespace Detail;
2628 return NodePtr(new BinaryNode<std::plus<result_t> >(l, r));
2632 operator-(Detail::Temp l, Detail::Temp r)
2634 using namespace Detail;
2635 return NodePtr(new BinaryNode<std::minus<result_t> >(l, r));
2639 operator*(Detail::Temp l, Detail::Temp r)
2641 using namespace Detail;
2642 return NodePtr(new BinaryNode<std::multiplies<result_t> >(l, r));
2646 operator/(Detail::Temp l, Detail::Temp r)
2648 using namespace Detail;
2649 return NodePtr(new BinaryNode<std::divides<result_t> >(l, r));
2653 operator%(Detail::Temp l, Detail::Temp r)
2655 using namespace Detail;
2656 return NodePtr(new BinaryNode<std::modulus<result_t> >(l, r));
2660 operator-(Detail::Temp l)
2662 using namespace Detail;
2663 return NodePtr(new UnaryNode<std::negate<result_t> >(l));
2666 template <typename T>
2670 using namespace Detail;
2671 return NodePtr(new ConstNode<T>(val));
2674 template <typename T>
2678 using namespace Detail;
2679 return NodePtr(new FunctorNode<T>(val));
2682 template <typename T>
2686 using namespace Detail;
2687 return NodePtr(new ScalarNode<T>(val));
2691 sum(Detail::Temp val)
2693 using namespace Detail;
2694 return NodePtr(new SumNode<std::plus<result_t> >(val));
2697 extern bool PrintDescriptions;
2699 } // namespace statistics
2701 #endif // __STATISTICS_HH__