2 * Copyright (c) 2003-2005 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.
28 * Authors: Nathan Binkert
32 * Declaration of Statistics objects.
38 * Generalized N-dimensinal vector
42 * -- these both can use the same function that prints out a
43 * specific set of stats
44 * VectorStandardDeviation totals
47 #ifndef __BASE_STATISTICS_HH__
48 #define __BASE_STATISTICS_HH__
63 #include "base/stats/info.hh"
64 #include "base/stats/output.hh"
65 #include "base/stats/types.hh"
66 #include "base/cast.hh"
67 #include "base/cprintf.hh"
68 #include "base/intmath.hh"
69 #include "base/refcnt.hh"
70 #include "base/str.hh"
71 #include "base/types.hh"
75 /** The current simulated tick. */
76 extern Tick curTick();
78 /* A namespace for all of the Statistics */
81 template <class Stat, class Base>
82 class InfoProxy : public Base
88 InfoProxy(Stat &stat) : s(stat) {}
90 bool check() const { return s.check(); }
91 void prepare() { s.prepare(); }
92 void reset() { s.reset(); }
94 visit(Output &visitor)
96 visitor.visit(*static_cast<Base *>(this));
98 bool zero() const { return s.zero(); }
101 template <class Stat>
102 class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
105 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
107 Counter value() const { return this->s.value(); }
108 Result result() const { return this->s.result(); }
109 Result total() const { return this->s.total(); }
112 template <class Stat>
113 class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
116 mutable VCounter cvec;
117 mutable VResult rvec;
120 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
122 size_type size() const { return this->s.size(); }
134 this->s.result(rvec);
138 Result total() const { return this->s.total(); }
141 template <class Stat>
142 class DistInfoProxy : public InfoProxy<Stat, DistInfo>
145 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
148 template <class Stat>
149 class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
152 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
154 size_type size() const { return this->s.size(); }
157 template <class Stat>
158 class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
161 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
166 virtual ~StorageParams();
172 /** Set up an info class for this statistic */
173 void setInfo(Info *info);
174 /** Save Storage class parameters if any */
175 void setParams(const StorageParams *params);
176 /** Save Storage class parameters if any */
179 /** Grab the information class for this statistic */
181 /** Grab the information class for this statistic */
182 const Info *info() const;
186 * Reset the stat to the default state.
191 * @return true if this stat has a value and satisfies its
192 * requirement as a prereq
194 bool zero() const { return true; }
197 * Check that this stat has been set up properly and is ready for
199 * @return true for success
201 bool check() const { return true; }
204 template <class Derived, template <class> class InfoProxyType>
205 class DataWrap : public InfoAccess
208 typedef InfoProxyType<Derived> Info;
211 Derived &self() { return *static_cast<Derived *>(this); }
217 return safe_cast<Info *>(InfoAccess::info());
224 return safe_cast<const Info *>(InfoAccess::info());
229 * Copy constructor, copies are not allowed.
231 DataWrap(const DataWrap &stat);
236 void operator=(const DataWrap &);
241 this->setInfo(new Info(self()));
245 * Set the name and marks this stat to print at the end of simulation.
246 * @param name The new name.
247 * @return A reference to this stat.
250 name(const std::string &name)
252 Info *info = this->info();
254 info->flags.set(display);
257 const std::string &name() const { return this->info()->name; }
260 * Set the character(s) used between the name and vector number
261 * on vectors, dist, etc.
262 * @param _sep The new separator string
263 * @return A reference to this stat.
266 setSeparator(const std::string &_sep)
268 this->info()->setSeparator(_sep);
271 const std::string &setSeparator() const
273 return this->info()->separatorString;
277 * Set the description and marks this stat to print at the end of
279 * @param desc The new description.
280 * @return A reference to this stat.
283 desc(const std::string &_desc)
285 this->info()->desc = _desc;
290 * Set the precision and marks this stat to print at the end of simulation.
291 * @param _precision The new precision
292 * @return A reference to this stat.
295 precision(int _precision)
297 this->info()->precision = _precision;
302 * Set the flags and marks this stat to print at the end of simulation.
303 * @param f The new flags.
304 * @return A reference to this stat.
309 this->info()->flags.set(_flags);
314 * Set the prerequisite stat and marks this stat to print at the end of
316 * @param prereq The prerequisite stat.
317 * @return A reference to this stat.
319 template <class Stat>
321 prereq(const Stat &prereq)
323 this->info()->prereq = prereq.info();
328 template <class Derived, template <class> class InfoProxyType>
329 class DataWrapVec : public DataWrap<Derived, InfoProxyType>
332 typedef InfoProxyType<Derived> Info;
334 // The following functions are specific to vectors. If you use them
335 // in a non vector context, you will get a nice compiler error!
338 * Set the subfield name for the given index, and marks this stat to print
339 * at the end of simulation.
340 * @param index The subfield index.
341 * @param name The new name of the subfield.
342 * @return A reference to this stat.
345 subname(off_type index, const std::string &name)
347 Derived &self = this->self();
348 Info *info = self.info();
350 std::vector<std::string> &subn = info->subnames;
351 if (subn.size() <= index)
352 subn.resize(index + 1);
357 // The following functions are specific to 2d vectors. If you use
358 // them in a non vector context, you will get a nice compiler
359 // error because info doesn't have the right variables.
362 * Set the subfield description for the given index and marks this stat to
363 * print at the end of simulation.
364 * @param index The subfield index.
365 * @param desc The new description of the subfield
366 * @return A reference to this stat.
369 subdesc(off_type index, const std::string &desc)
371 Info *info = this->info();
373 std::vector<std::string> &subd = info->subdescs;
374 if (subd.size() <= index)
375 subd.resize(index + 1);
384 Derived &self = this->self();
385 Info *info = this->info();
387 size_t size = self.size();
388 for (off_type i = 0; i < size; ++i)
389 self.data(i)->prepare(info);
395 Derived &self = this->self();
396 Info *info = this->info();
398 size_t size = self.size();
399 for (off_type i = 0; i < size; ++i)
400 self.data(i)->reset(info);
404 template <class Derived, template <class> class InfoProxyType>
405 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
408 typedef InfoProxyType<Derived> Info;
411 * @warning This makes the assumption that if you're gonna subnames a 2d
412 * vector, you're subnaming across all y
415 ysubnames(const char **names)
417 Derived &self = this->self();
418 Info *info = this->info();
420 info->y_subnames.resize(self.y);
421 for (off_type i = 0; i < self.y; ++i)
422 info->y_subnames[i] = names[i];
427 ysubname(off_type index, const std::string &subname)
429 Derived &self = this->self();
430 Info *info = this->info();
432 assert(index < self.y);
433 info->y_subnames.resize(self.y);
434 info->y_subnames[index] = subname.c_str();
439 ysubname(off_type i) const
441 return this->info()->y_subnames[i];
446 //////////////////////////////////////////////////////////////////////
450 //////////////////////////////////////////////////////////////////////
453 * Templatized storage and interface for a simple scalar stat.
458 /** The statistic value. */
462 struct Params : public StorageParams {};
466 * Builds this storage element and calls the base constructor of the
474 * The the stat to the given value.
475 * @param val The new value.
477 void set(Counter val) { data = val; }
479 * Increment the stat by the given value.
480 * @param val The new value.
482 void inc(Counter val) { data += val; }
484 * Decrement the stat by the given value.
485 * @param val The new value.
487 void dec(Counter val) { data -= val; }
489 * Return the value of this stat as its base type.
490 * @return The value of this stat.
492 Counter value() const { return data; }
494 * Return the value of this stat as a result type.
495 * @return The value of this stat.
497 Result result() const { return (Result)data; }
499 * Prepare stat data for dumping or serialization
501 void prepare(Info *info) { }
503 * Reset stat value to default
505 void reset(Info *info) { data = Counter(); }
508 * @return true if zero value
510 bool zero() const { return data == Counter(); }
514 * Templatized storage and interface to a per-tick average stat. This keeps
515 * a current count and updates a total (count * ticks) when this count
516 * changes. This allows the quick calculation of a per tick count of the item
517 * being watched. This is good for keeping track of residencies in structures
518 * among other things.
523 /** The current count. */
525 /** The tick of the last reset */
527 /** The total count for all tick. */
528 mutable Result total;
529 /** The tick that current last changed. */
533 struct Params : public StorageParams {};
537 * Build and initializes this stat storage.
540 : current(0), lastReset(0), total(0), last(0)
544 * Set the current count to the one provided, update the total and last
546 * @param val The new count.
551 total += current * (curTick() - last);
557 * Increment the current count by the provided value, calls set.
558 * @param val The amount to increment.
560 void inc(Counter val) { set(current + val); }
563 * Deccrement the current count by the provided value, calls set.
564 * @param val The amount to decrement.
566 void dec(Counter val) { set(current - val); }
569 * Return the current count.
570 * @return The current count.
572 Counter value() const { return current; }
575 * Return the current average.
576 * @return The current average.
581 assert(last == curTick());
582 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
586 * @return true if zero value
588 bool zero() const { return total == 0.0; }
591 * Prepare stat data for dumping or serialization
596 total += current * (curTick() - last);
601 * Reset stat value to default
608 lastReset = curTick();
614 * Implementation of a scalar stat. The type of stat is determined by the
617 template <class Derived, class Stor>
618 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
621 typedef Stor Storage;
622 typedef typename Stor::Params Params;
625 /** The storage of this stat. */
626 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
630 * Retrieve the storage.
631 * @param index The vector index to access.
632 * @return The storage object at the given index.
637 return reinterpret_cast<Storage *>(storage);
641 * Retrieve a const pointer to the storage.
642 * for the given index.
643 * @param index The vector index to access.
644 * @return A const pointer to the storage object at the given index.
649 return reinterpret_cast<const Storage *>(storage);
655 new (storage) Storage(this->info());
661 * Return the current value of this stat as its base type.
662 * @return The current value.
664 Counter value() const { return data()->value(); }
673 // Common operators for stats
675 * Increment the stat by 1. This calls the associated storage object inc
678 void operator++() { data()->inc(1); }
680 * Decrement the stat by 1. This calls the associated storage object dec
683 void operator--() { data()->dec(1); }
685 /** Increment the stat by 1. */
686 void operator++(int) { ++*this; }
687 /** Decrement the stat by 1. */
688 void operator--(int) { --*this; }
691 * Set the data value to the given value. This calls the associated storage
692 * object set function.
693 * @param v The new value.
695 template <typename U>
696 void operator=(const U &v) { data()->set(v); }
699 * Increment the stat by the given value. This calls the associated
700 * storage object inc function.
701 * @param v The value to add.
703 template <typename U>
704 void operator+=(const U &v) { data()->inc(v); }
707 * Decrement the stat by the given value. This calls the associated
708 * storage object dec function.
709 * @param v The value to substract.
711 template <typename U>
712 void operator-=(const U &v) { data()->dec(v); }
715 * Return the number of elements, always 1 for a scalar.
718 size_type size() const { return 1; }
720 Counter value() { return data()->value(); }
722 Result result() { return data()->result(); }
724 Result total() { return result(); }
726 bool zero() { return result() == 0.0; }
728 void reset() { data()->reset(this->info()); }
729 void prepare() { data()->prepare(this->info()); }
732 class ProxyInfo : public ScalarInfo
735 std::string str() const { return to_string(value()); }
736 size_type size() const { return 1; }
737 bool check() const { return true; }
740 bool zero() const { return value() == 0; }
742 void visit(Output &visitor) { visitor.visit(*this); }
746 class ValueProxy : public ProxyInfo
752 ValueProxy(T &val) : scalar(&val) {}
753 Counter value() const { return *scalar; }
754 Result result() const { return *scalar; }
755 Result total() const { return *scalar; }
759 class FunctorProxy : public ProxyInfo
765 FunctorProxy(T &func) : functor(&func) {}
766 Counter value() const { return (*functor)(); }
767 Result result() const { return (*functor)(); }
768 Result total() const { return (*functor)(); }
771 template <class Derived>
772 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
778 ValueBase() : proxy(NULL) { }
779 ~ValueBase() { if (proxy) delete proxy; }
785 proxy = new ValueProxy<T>(value);
794 proxy = new FunctorProxy<T>(func);
799 Counter value() { return proxy->value(); }
800 Result result() const { return proxy->result(); }
801 Result total() const { return proxy->total(); };
802 size_type size() const { return proxy->size(); }
804 std::string str() const { return proxy->str(); }
805 bool zero() const { return proxy->zero(); }
806 bool check() const { return proxy != NULL; }
811 //////////////////////////////////////////////////////////////////////
815 //////////////////////////////////////////////////////////////////////
818 * A proxy class to access the stat at a given index in a VectorBase stat.
819 * Behaves like a ScalarBase.
821 template <class Stat>
825 /** Pointer to the parent Vector. */
828 /** The index to access in the parent VectorBase. */
833 * Return the current value of this stat as its base type.
834 * @return The current value.
836 Counter value() const { return stat.data(index)->value(); }
839 * Return the current value of this statas a result type.
840 * @return The current value.
842 Result result() const { return stat.data(index)->result(); }
846 * Create and initialize this proxy, do not register it with the database.
847 * @param i The index to access.
849 ScalarProxy(Stat &s, off_type i)
855 * Create a copy of the provided ScalarProxy.
856 * @param sp The proxy to copy.
858 ScalarProxy(const ScalarProxy &sp)
859 : stat(sp.stat), index(sp.index)
863 * Set this proxy equal to the provided one.
864 * @param sp The proxy to copy.
865 * @return A reference to this proxy.
868 operator=(const ScalarProxy &sp)
876 // Common operators for stats
878 * Increment the stat by 1. This calls the associated storage object inc
881 void operator++() { stat.data(index)->inc(1); }
883 * Decrement the stat by 1. This calls the associated storage object dec
886 void operator--() { stat.data(index)->dec(1); }
888 /** Increment the stat by 1. */
889 void operator++(int) { ++*this; }
890 /** Decrement the stat by 1. */
891 void operator--(int) { --*this; }
894 * Set the data value to the given value. This calls the associated storage
895 * object set function.
896 * @param v The new value.
898 template <typename U>
900 operator=(const U &v)
902 stat.data(index)->set(v);
906 * Increment the stat by the given value. This calls the associated
907 * storage object inc function.
908 * @param v The value to add.
910 template <typename U>
912 operator+=(const U &v)
914 stat.data(index)->inc(v);
918 * Decrement the stat by the given value. This calls the associated
919 * storage object dec function.
920 * @param v The value to substract.
922 template <typename U>
924 operator-=(const U &v)
926 stat.data(index)->dec(v);
930 * Return the number of elements, always 1 for a scalar.
933 size_type size() const { return 1; }
939 return csprintf("%s[%d]", stat.info()->name, index);
944 * Implementation of a vector of stats. The type of stat is determined by the
945 * Storage class. @sa ScalarBase
947 template <class Derived, class Stor>
948 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
951 typedef Stor Storage;
952 typedef typename Stor::Params Params;
955 typedef ScalarProxy<Derived> Proxy;
956 friend class ScalarProxy<Derived>;
957 friend class DataWrapVec<Derived, VectorInfoProxy>;
960 /** The storage of this stat. */
966 * Retrieve the storage.
967 * @param index The vector index to access.
968 * @return The storage object at the given index.
970 Storage *data(off_type index) { return &storage[index]; }
973 * Retrieve a const pointer to the storage.
974 * @param index The vector index to access.
975 * @return A const pointer to the storage object at the given index.
977 const Storage *data(off_type index) const { return &storage[index]; }
982 assert(s > 0 && "size must be positive!");
983 assert(!storage && "already initialized");
986 char *ptr = new char[_size * sizeof(Storage)];
987 storage = reinterpret_cast<Storage *>(ptr);
989 for (off_type i = 0; i < _size; ++i)
990 new (&storage[i]) Storage(this->info());
997 value(VCounter &vec) const
1000 for (off_type i = 0; i < size(); ++i)
1001 vec[i] = data(i)->value();
1005 * Copy the values to a local vector and return a reference to it.
1006 * @return A reference to a vector of the stat values.
1009 result(VResult &vec) const
1012 for (off_type i = 0; i < size(); ++i)
1013 vec[i] = data(i)->result();
1017 * Return a total of all entries in this vector.
1018 * @return The total of all vector entries.
1024 for (off_type i = 0; i < size(); ++i)
1025 total += data(i)->result();
1030 * @return the number of elements in this vector.
1032 size_type size() const { return _size; }
1037 for (off_type i = 0; i < size(); ++i)
1038 if (data(i)->zero())
1046 return storage != NULL;
1059 for (off_type i = 0; i < _size; ++i)
1060 data(i)->~Storage();
1061 delete [] reinterpret_cast<char *>(storage);
1065 * Set this vector to have the given size.
1066 * @param size The new size.
1067 * @return A reference to this stat.
1070 init(size_type size)
1072 Derived &self = this->self();
1078 * Return a reference (ScalarProxy) to the stat at the given index.
1079 * @param index The vector index to access.
1080 * @return A reference of the stat.
1083 operator[](off_type index)
1085 assert (index >= 0 && index < size());
1086 return Proxy(this->self(), index);
1090 template <class Stat>
1099 mutable VResult vec;
1101 typename Stat::Storage *
1102 data(off_type index)
1104 assert(index < len);
1105 return stat.data(offset + index);
1108 const typename Stat::Storage *
1109 data(off_type index) const
1111 assert(index < len);
1112 return stat.data(offset + index);
1121 for (off_type i = 0; i < size(); ++i)
1122 vec[i] = data(i)->result();
1131 for (off_type i = 0; i < size(); ++i)
1132 total += data(i)->result();
1137 VectorProxy(Stat &s, off_type o, size_type l)
1138 : stat(s), offset(o), len(l)
1142 VectorProxy(const VectorProxy &sp)
1143 : stat(sp.stat), offset(sp.offset), len(sp.len)
1148 operator=(const VectorProxy &sp)
1157 operator[](off_type index)
1159 assert (index >= 0 && index < size());
1160 return ScalarProxy<Stat>(stat, offset + index);
1163 size_type size() const { return len; }
1166 template <class Derived, class Stor>
1167 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1170 typedef Vector2dInfoProxy<Derived> Info;
1171 typedef Stor Storage;
1172 typedef typename Stor::Params Params;
1173 typedef VectorProxy<Derived> Proxy;
1174 friend class ScalarProxy<Derived>;
1175 friend class VectorProxy<Derived>;
1176 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1177 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1186 Storage *data(off_type index) { return &storage[index]; }
1187 const Storage *data(off_type index) const { return &storage[index]; }
1199 for (off_type i = 0; i < _size; ++i)
1200 data(i)->~Storage();
1201 delete [] reinterpret_cast<char *>(storage);
1205 init(size_type _x, size_type _y)
1207 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1208 assert(!storage && "already initialized");
1210 Derived &self = this->self();
1211 Info *info = this->info();
1219 char *ptr = new char[_size * sizeof(Storage)];
1220 storage = reinterpret_cast<Storage *>(ptr);
1222 for (off_type i = 0; i < _size; ++i)
1223 new (&storage[i]) Storage(info);
1231 operator[](off_type index)
1233 off_type offset = index * y;
1234 assert (index >= 0 && offset + y <= size());
1235 return Proxy(this->self(), offset, y);
1248 return data(0)->zero();
1250 for (off_type i = 0; i < size(); ++i)
1251 if (!data(i)->zero())
1260 Info *info = this->info();
1261 size_type size = this->size();
1263 for (off_type i = 0; i < size; ++i)
1264 data(i)->prepare(info);
1266 info->cvec.resize(size);
1267 for (off_type i = 0; i < size; ++i)
1268 info->cvec[i] = data(i)->value();
1272 * Reset stat value to default
1277 Info *info = this->info();
1278 size_type size = this->size();
1279 for (off_type i = 0; i < size; ++i)
1280 data(i)->reset(info);
1286 return storage != NULL;
1290 //////////////////////////////////////////////////////////////////////
1292 // Non formula statistics
1294 //////////////////////////////////////////////////////////////////////
1295 /** The parameters for a distribution stat. */
1296 struct DistParams : public StorageParams
1298 const DistType type;
1299 DistParams(DistType t) : type(t) {}
1303 * Templatized storage and interface for a distrbution stat.
1308 /** The parameters for a distribution stat. */
1309 struct Params : public DistParams
1311 /** The minimum value to track. */
1313 /** The maximum value to track. */
1315 /** The number of entries in each bucket. */
1316 Counter bucket_size;
1317 /** The number of buckets. Equal to (max-min)/bucket_size. */
1320 Params() : DistParams(Dist) {}
1324 /** The minimum value to track. */
1326 /** The maximum value to track. */
1328 /** The number of entries in each bucket. */
1329 Counter bucket_size;
1331 /** The smallest value sampled. */
1333 /** The largest value sampled. */
1335 /** The number of values sampled less than min. */
1337 /** The number of values sampled more than max. */
1339 /** The current sum. */
1341 /** The sum of squares. */
1343 /** The number of samples. */
1345 /** Counter for each bucket. */
1349 DistStor(Info *info)
1350 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1356 * Add a value to the distribution for the given number of times.
1357 * @param val The value to add.
1358 * @param number The number of times to add the value.
1361 sample(Counter val, int number)
1363 if (val < min_track)
1364 underflow += number;
1365 else if (val > max_track)
1369 (size_type)std::floor((val - min_track) / bucket_size);
1370 assert(index < size());
1371 cvec[index] += number;
1380 sum += val * number;
1381 squares += val * val * number;
1386 * Return the number of buckets in this distribution.
1387 * @return the number of buckets.
1389 size_type size() const { return cvec.size(); }
1392 * Returns true if any calls to sample have been made.
1393 * @return True if any values have been sampled.
1398 return samples == Counter();
1402 prepare(Info *info, DistData &data)
1404 const Params *params = safe_cast<const Params *>(info->storageParams);
1406 assert(params->type == Dist);
1407 data.type = params->type;
1408 data.min = params->min;
1409 data.max = params->max;
1410 data.bucket_size = params->bucket_size;
1412 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1413 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1414 data.underflow = underflow;
1415 data.overflow = overflow;
1417 data.cvec.resize(params->buckets);
1418 for (off_type i = 0; i < params->buckets; ++i)
1419 data.cvec[i] = cvec[i];
1422 data.squares = squares;
1423 data.samples = samples;
1427 * Reset stat value to default
1432 const Params *params = safe_cast<const Params *>(info->storageParams);
1433 min_track = params->min;
1434 max_track = params->max;
1435 bucket_size = params->bucket_size;
1437 min_val = CounterLimits::max();
1438 max_val = CounterLimits::min();
1439 underflow = Counter();
1440 overflow = Counter();
1442 size_type size = cvec.size();
1443 for (off_type i = 0; i < size; ++i)
1444 cvec[i] = Counter();
1447 squares = Counter();
1448 samples = Counter();
1453 * Templatized storage and interface for a histogram stat.
1458 /** The parameters for a distribution stat. */
1459 struct Params : public DistParams
1461 /** The number of buckets.. */
1464 Params() : DistParams(Hist) {}
1468 /** The minimum value to track. */
1470 /** The maximum value to track. */
1472 /** The number of entries in each bucket. */
1473 Counter bucket_size;
1475 /** The current sum. */
1477 /** The sum of logarithm of each sample, used to compute geometric mean. */
1479 /** The sum of squares. */
1481 /** The number of samples. */
1483 /** Counter for each bucket. */
1487 HistStor(Info *info)
1488 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1495 void grow_convert();
1498 * Add a value to the distribution for the given number of times.
1499 * @param val The value to add.
1500 * @param number The number of times to add the value.
1503 sample(Counter val, int number)
1505 assert(min_bucket < max_bucket);
1506 if (val < min_bucket) {
1507 if (min_bucket == 0)
1510 while (val < min_bucket)
1512 } else if (val >= max_bucket + bucket_size) {
1513 if (min_bucket == 0) {
1514 while (val >= max_bucket + bucket_size)
1517 while (val >= max_bucket + bucket_size)
1523 (int64_t)std::floor((val - min_bucket) / bucket_size);
1525 assert(index >= 0 && index < size());
1526 cvec[index] += number;
1528 sum += val * number;
1529 squares += val * val * number;
1530 logs += log(val) * number;
1535 * Return the number of buckets in this distribution.
1536 * @return the number of buckets.
1538 size_type size() const { return cvec.size(); }
1541 * Returns true if any calls to sample have been made.
1542 * @return True if any values have been sampled.
1547 return samples == Counter();
1551 prepare(Info *info, DistData &data)
1553 const Params *params = safe_cast<const Params *>(info->storageParams);
1555 assert(params->type == Hist);
1556 data.type = params->type;
1557 data.min = min_bucket;
1558 data.max = max_bucket + bucket_size - 1;
1559 data.bucket_size = bucket_size;
1561 data.min_val = min_bucket;
1562 data.max_val = max_bucket;
1564 int buckets = params->buckets;
1565 data.cvec.resize(buckets);
1566 for (off_type i = 0; i < buckets; ++i)
1567 data.cvec[i] = cvec[i];
1571 data.squares = squares;
1572 data.samples = samples;
1576 * Reset stat value to default
1581 const Params *params = safe_cast<const Params *>(info->storageParams);
1583 max_bucket = params->buckets - 1;
1586 size_type size = cvec.size();
1587 for (off_type i = 0; i < size; ++i)
1588 cvec[i] = Counter();
1591 squares = Counter();
1592 samples = Counter();
1598 * Templatized storage and interface for a distribution that calculates mean
1604 struct Params : public DistParams
1606 Params() : DistParams(Deviation) {}
1610 /** The current sum. */
1612 /** The sum of squares. */
1614 /** The number of samples. */
1619 * Create and initialize this storage.
1621 SampleStor(Info *info)
1622 : sum(Counter()), squares(Counter()), samples(Counter())
1626 * Add a value the given number of times to this running average.
1627 * Update the running sum and sum of squares, increment the number of
1628 * values seen by the given number.
1629 * @param val The value to add.
1630 * @param number The number of times to add the value.
1633 sample(Counter val, int number)
1635 Counter value = val * number;
1637 squares += value * value;
1642 * Return the number of entries in this stat, 1
1645 size_type size() const { return 1; }
1648 * Return true if no samples have been added.
1649 * @return True if no samples have been added.
1651 bool zero() const { return samples == Counter(); }
1654 prepare(Info *info, DistData &data)
1656 const Params *params = safe_cast<const Params *>(info->storageParams);
1658 assert(params->type == Deviation);
1659 data.type = params->type;
1661 data.squares = squares;
1662 data.samples = samples;
1666 * Reset stat value to default
1672 squares = Counter();
1673 samples = Counter();
1678 * Templatized storage for distribution that calculates per tick mean and
1684 struct Params : public DistParams
1686 Params() : DistParams(Deviation) {}
1690 /** Current total. */
1692 /** Current sum of squares. */
1697 * Create and initialize this storage.
1699 AvgSampleStor(Info *info)
1700 : sum(Counter()), squares(Counter())
1704 * Add a value to the distribution for the given number of times.
1705 * Update the running sum and sum of squares.
1706 * @param val The value to add.
1707 * @param number The number of times to add the value.
1710 sample(Counter val, int number)
1712 Counter value = val * number;
1714 squares += value * value;
1718 * Return the number of entries, in this case 1.
1721 size_type size() const { return 1; }
1724 * Return true if no samples have been added.
1725 * @return True if the sum is zero.
1727 bool zero() const { return sum == Counter(); }
1730 prepare(Info *info, DistData &data)
1732 const Params *params = safe_cast<const Params *>(info->storageParams);
1734 assert(params->type == Deviation);
1735 data.type = params->type;
1737 data.squares = squares;
1738 data.samples = curTick();
1742 * Reset stat value to default
1748 squares = Counter();
1753 * Implementation of a distribution stat. The type of distribution is
1754 * determined by the Storage template. @sa ScalarBase
1756 template <class Derived, class Stor>
1757 class DistBase : public DataWrap<Derived, DistInfoProxy>
1760 typedef DistInfoProxy<Derived> Info;
1761 typedef Stor Storage;
1762 typedef typename Stor::Params Params;
1765 /** The storage for this stat. */
1766 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1770 * Retrieve the storage.
1771 * @return The storage object for this stat.
1776 return reinterpret_cast<Storage *>(storage);
1780 * Retrieve a const pointer to the storage.
1781 * @return A const pointer to the storage object for this stat.
1786 return reinterpret_cast<const Storage *>(storage);
1792 new (storage) Storage(this->info());
1800 * Add a value to the distribtion n times. Calls sample on the storage
1802 * @param v The value to add.
1803 * @param n The number of times to add it, defaults to 1.
1805 template <typename U>
1806 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1809 * Return the number of entries in this stat.
1810 * @return The number of entries.
1812 size_type size() const { return data()->size(); }
1814 * Return true if no samples have been added.
1815 * @return True if there haven't been any samples.
1817 bool zero() const { return data()->zero(); }
1822 Info *info = this->info();
1823 data()->prepare(info, info->data);
1827 * Reset stat value to default
1832 data()->reset(this->info());
1836 template <class Stat>
1839 template <class Derived, class Stor>
1840 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1843 typedef VectorDistInfoProxy<Derived> Info;
1844 typedef Stor Storage;
1845 typedef typename Stor::Params Params;
1846 typedef DistProxy<Derived> Proxy;
1847 friend class DistProxy<Derived>;
1848 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1856 data(off_type index)
1858 return &storage[index];
1862 data(off_type index) const
1864 return &storage[index];
1870 assert(s > 0 && "size must be positive!");
1871 assert(!storage && "already initialized");
1874 char *ptr = new char[_size * sizeof(Storage)];
1875 storage = reinterpret_cast<Storage *>(ptr);
1877 Info *info = this->info();
1878 for (off_type i = 0; i < _size; ++i)
1879 new (&storage[i]) Storage(info);
1894 for (off_type i = 0; i < _size; ++i)
1895 data(i)->~Storage();
1896 delete [] reinterpret_cast<char *>(storage);
1899 Proxy operator[](off_type index)
1901 assert(index >= 0 && index < size());
1902 return Proxy(this->self(), index);
1914 for (off_type i = 0; i < size(); ++i)
1915 if (!data(i)->zero())
1923 Info *info = this->info();
1924 size_type size = this->size();
1925 info->data.resize(size);
1926 for (off_type i = 0; i < size; ++i)
1927 data(i)->prepare(info, info->data[i]);
1933 return storage != NULL;
1937 template <class Stat>
1945 typename Stat::Storage *data() { return stat.data(index); }
1946 const typename Stat::Storage *data() const { return stat.data(index); }
1949 DistProxy(Stat &s, off_type i)
1953 DistProxy(const DistProxy &sp)
1954 : stat(sp.stat), index(sp.index)
1958 operator=(const DistProxy &sp)
1966 template <typename U>
1968 sample(const U &v, int n = 1)
1970 data()->sample(v, n);
1982 return data()->zero();
1986 * Proxy has no state. Nothing to reset.
1991 //////////////////////////////////////////////////////////////////////
1995 //////////////////////////////////////////////////////////////////////
1998 * Base class for formula statistic node. These nodes are used to build a tree
1999 * that represents the formula.
2001 class Node : public RefCounted
2005 * Return the number of nodes in the subtree starting at this node.
2006 * @return the number of nodes in this subtree.
2008 virtual size_type size() const = 0;
2010 * Return the result vector of this subtree.
2011 * @return The result vector of this subtree.
2013 virtual const VResult &result() const = 0;
2015 * Return the total of the result vector.
2016 * @return The total of the result vector.
2018 virtual Result total() const = 0;
2023 virtual std::string str() const = 0;
2026 /** Reference counting pointer to a function Node. */
2027 typedef RefCountingPtr<Node> NodePtr;
2029 class ScalarStatNode : public Node
2032 const ScalarInfo *data;
2033 mutable VResult vresult;
2036 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2041 vresult[0] = data->result();
2045 Result total() const { return data->result(); };
2047 size_type size() const { return 1; }
2052 std::string str() const { return data->name; }
2055 template <class Stat>
2056 class ScalarProxyNode : public Node
2059 const ScalarProxy<Stat> proxy;
2060 mutable VResult vresult;
2063 ScalarProxyNode(const ScalarProxy<Stat> &p)
2064 : proxy(p), vresult(1)
2070 vresult[0] = proxy.result();
2077 return proxy.result();
2096 class VectorStatNode : public Node
2099 const VectorInfo *data;
2102 VectorStatNode(const VectorInfo *d) : data(d) { }
2103 const VResult &result() const { return data->result(); }
2104 Result total() const { return data->total(); };
2106 size_type size() const { return data->size(); }
2108 std::string str() const { return data->name; }
2112 class ConstNode : public Node
2118 ConstNode(T s) : vresult(1, (Result)s) {}
2119 const VResult &result() const { return vresult; }
2120 Result total() const { return vresult[0]; };
2121 size_type size() const { return 1; }
2122 std::string str() const { return to_string(vresult[0]); }
2126 class ConstVectorNode : public Node
2132 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2133 const VResult &result() const { return vresult; }
2138 size_type size = this->size();
2140 for (off_type i = 0; i < size; i++)
2145 size_type size() const { return vresult.size(); }
2149 size_type size = this->size();
2150 std::string tmp = "(";
2151 for (off_type i = 0; i < size; i++)
2152 tmp += csprintf("%s ",to_string(vresult[i]));
2162 struct OpString<std::plus<Result> >
2164 static std::string str() { return "+"; }
2168 struct OpString<std::minus<Result> >
2170 static std::string str() { return "-"; }
2174 struct OpString<std::multiplies<Result> >
2176 static std::string str() { return "*"; }
2180 struct OpString<std::divides<Result> >
2182 static std::string str() { return "/"; }
2186 struct OpString<std::modulus<Result> >
2188 static std::string str() { return "%"; }
2192 struct OpString<std::negate<Result> >
2194 static std::string str() { return "-"; }
2198 class UnaryNode : public Node
2202 mutable VResult vresult;
2205 UnaryNode(NodePtr &p) : l(p) {}
2210 const VResult &lvec = l->result();
2211 size_type size = lvec.size();
2215 vresult.resize(size);
2217 for (off_type i = 0; i < size; ++i)
2218 vresult[i] = op(lvec[i]);
2226 const VResult &vec = this->result();
2228 for (off_type i = 0; i < size(); i++)
2233 size_type size() const { return l->size(); }
2238 return OpString<Op>::str() + l->str();
2243 class BinaryNode : public Node
2248 mutable VResult vresult;
2251 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2257 const VResult &lvec = l->result();
2258 const VResult &rvec = r->result();
2260 assert(lvec.size() > 0 && rvec.size() > 0);
2262 if (lvec.size() == 1 && rvec.size() == 1) {
2264 vresult[0] = op(lvec[0], rvec[0]);
2265 } else if (lvec.size() == 1) {
2266 size_type size = rvec.size();
2267 vresult.resize(size);
2268 for (off_type i = 0; i < size; ++i)
2269 vresult[i] = op(lvec[0], rvec[i]);
2270 } else if (rvec.size() == 1) {
2271 size_type size = lvec.size();
2272 vresult.resize(size);
2273 for (off_type i = 0; i < size; ++i)
2274 vresult[i] = op(lvec[i], rvec[0]);
2275 } else if (rvec.size() == lvec.size()) {
2276 size_type size = rvec.size();
2277 vresult.resize(size);
2278 for (off_type i = 0; i < size; ++i)
2279 vresult[i] = op(lvec[i], rvec[i]);
2288 const VResult &vec = this->result();
2289 const VResult &lvec = l->result();
2290 const VResult &rvec = r->result();
2296 assert(lvec.size() > 0 && rvec.size() > 0);
2297 assert(lvec.size() == rvec.size() ||
2298 lvec.size() == 1 || rvec.size() == 1);
2300 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2301 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2302 for (off_type i = 0; i < size(); ++i) {
2306 return op(lsum, rsum);
2309 /** Otherwise divide each item by the divisor */
2310 for (off_type i = 0; i < size(); ++i) {
2320 size_type ls = l->size();
2321 size_type rs = r->size();
2324 } else if (rs == 1) {
2327 assert(ls == rs && "Node vector sizes are not equal");
2335 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2340 class SumNode : public Node
2344 mutable VResult vresult;
2347 SumNode(NodePtr &p) : l(p), vresult(1) {}
2352 const VResult &lvec = l->result();
2353 size_type size = lvec.size();
2359 for (off_type i = 0; i < size; ++i)
2360 vresult[0] = op(vresult[0], lvec[i]);
2368 const VResult &lvec = l->result();
2369 size_type size = lvec.size();
2372 Result result = 0.0;
2375 for (off_type i = 0; i < size; ++i)
2376 result = op(result, lvec[i]);
2381 size_type size() const { return 1; }
2386 return csprintf("total(%s)", l->str());
2391 //////////////////////////////////////////////////////////////////////
2393 // Visible Statistics Types
2395 //////////////////////////////////////////////////////////////////////
2397 * @defgroup VisibleStats "Statistic Types"
2398 * These are the statistics that are used in the simulator.
2403 * This is a simple scalar statistic, like a counter.
2404 * @sa Stat, ScalarBase, StatStor
2406 class Scalar : public ScalarBase<Scalar, StatStor>
2409 using ScalarBase<Scalar, StatStor>::operator=;
2413 * A stat that calculates the per tick average of a value.
2414 * @sa Stat, ScalarBase, AvgStor
2416 class Average : public ScalarBase<Average, AvgStor>
2419 using ScalarBase<Average, AvgStor>::operator=;
2422 class Value : public ValueBase<Value>
2427 * A vector of scalar stats.
2428 * @sa Stat, VectorBase, StatStor
2430 class Vector : public VectorBase<Vector, StatStor>
2435 * A vector of Average stats.
2436 * @sa Stat, VectorBase, AvgStor
2438 class AverageVector : public VectorBase<AverageVector, AvgStor>
2443 * A 2-Dimensional vecto of scalar stats.
2444 * @sa Stat, Vector2dBase, StatStor
2446 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2451 * A simple distribution stat.
2452 * @sa Stat, DistBase, DistStor
2454 class Distribution : public DistBase<Distribution, DistStor>
2458 * Set the parameters of this distribution. @sa DistStor::Params
2459 * @param min The minimum value of the distribution.
2460 * @param max The maximum value of the distribution.
2461 * @param bkt The number of values in each bucket.
2462 * @return A reference to this distribution.
2465 init(Counter min, Counter max, Counter bkt)
2467 DistStor::Params *params = new DistStor::Params;
2470 params->bucket_size = bkt;
2471 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2472 this->setParams(params);
2474 return this->self();
2479 * A simple histogram stat.
2480 * @sa Stat, DistBase, HistStor
2482 class Histogram : public DistBase<Histogram, HistStor>
2486 * Set the parameters of this histogram. @sa HistStor::Params
2487 * @param size The number of buckets in the histogram
2488 * @return A reference to this histogram.
2491 init(size_type size)
2493 HistStor::Params *params = new HistStor::Params;
2494 params->buckets = size;
2495 this->setParams(params);
2497 return this->self();
2502 * Calculates the mean and variance of all the samples.
2503 * @sa DistBase, SampleStor
2505 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2509 * Construct and initialize this distribution.
2513 SampleStor::Params *params = new SampleStor::Params;
2515 this->setParams(params);
2520 * Calculates the per tick mean and variance of the samples.
2521 * @sa DistBase, AvgSampleStor
2523 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2527 * Construct and initialize this distribution.
2531 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2533 this->setParams(params);
2538 * A vector of distributions.
2539 * @sa VectorDistBase, DistStor
2541 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2545 * Initialize storage and parameters for this distribution.
2546 * @param size The size of the vector (the number of distributions).
2547 * @param min The minimum value of the distribution.
2548 * @param max The maximum value of the distribution.
2549 * @param bkt The number of values in each bucket.
2550 * @return A reference to this distribution.
2552 VectorDistribution &
2553 init(size_type size, Counter min, Counter max, Counter bkt)
2555 DistStor::Params *params = new DistStor::Params;
2558 params->bucket_size = bkt;
2559 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2560 this->setParams(params);
2562 return this->self();
2567 * This is a vector of StandardDeviation stats.
2568 * @sa VectorDistBase, SampleStor
2570 class VectorStandardDeviation
2571 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2575 * Initialize storage for this distribution.
2576 * @param size The size of the vector.
2577 * @return A reference to this distribution.
2579 VectorStandardDeviation &
2580 init(size_type size)
2582 SampleStor::Params *params = new SampleStor::Params;
2584 this->setParams(params);
2585 return this->self();
2590 * This is a vector of AverageDeviation stats.
2591 * @sa VectorDistBase, AvgSampleStor
2593 class VectorAverageDeviation
2594 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2598 * Initialize storage for this distribution.
2599 * @param size The size of the vector.
2600 * @return A reference to this distribution.
2602 VectorAverageDeviation &
2603 init(size_type size)
2605 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2607 this->setParams(params);
2608 return this->self();
2612 template <class Stat>
2613 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2616 mutable VResult vec;
2617 mutable VCounter cvec;
2620 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2622 size_type size() const { return this->s.size(); }
2627 this->s.result(vec);
2630 Result total() const { return this->s.total(); }
2631 VCounter &value() const { return cvec; }
2633 std::string str() const { return this->s.str(); }
2636 template <class Stat>
2637 class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2640 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2644 * Implementation of a sparse histogram stat. The storage class is
2645 * determined by the Storage template.
2647 template <class Derived, class Stor>
2648 class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2651 typedef SparseHistInfoProxy<Derived> Info;
2652 typedef Stor Storage;
2653 typedef typename Stor::Params Params;
2656 /** The storage for this stat. */
2657 char storage[sizeof(Storage)];
2661 * Retrieve the storage.
2662 * @return The storage object for this stat.
2667 return reinterpret_cast<Storage *>(storage);
2671 * Retrieve a const pointer to the storage.
2672 * @return A const pointer to the storage object for this stat.
2677 return reinterpret_cast<const Storage *>(storage);
2683 new (storage) Storage(this->info());
2688 SparseHistBase() { }
2691 * Add a value to the distribtion n times. Calls sample on the storage
2693 * @param v The value to add.
2694 * @param n The number of times to add it, defaults to 1.
2696 template <typename U>
2697 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2700 * Return the number of entries in this stat.
2701 * @return The number of entries.
2703 size_type size() const { return data()->size(); }
2705 * Return true if no samples have been added.
2706 * @return True if there haven't been any samples.
2708 bool zero() const { return data()->zero(); }
2713 Info *info = this->info();
2714 data()->prepare(info, info->data);
2718 * Reset stat value to default
2723 data()->reset(this->info());
2728 * Templatized storage and interface for a sparse histogram stat.
2730 class SparseHistStor
2733 /** The parameters for a sparse histogram stat. */
2734 struct Params : public DistParams
2736 Params() : DistParams(Hist) {}
2740 /** Counter for number of samples */
2742 /** Counter for each bucket. */
2746 SparseHistStor(Info *info)
2752 * Add a value to the distribution for the given number of times.
2753 * @param val The value to add.
2754 * @param number The number of times to add the value.
2757 sample(Counter val, int number)
2759 cmap[val] += number;
2764 * Return the number of buckets in this distribution.
2765 * @return the number of buckets.
2767 size_type size() const { return cmap.size(); }
2770 * Returns true if any calls to sample have been made.
2771 * @return True if any values have been sampled.
2776 return samples == Counter();
2780 prepare(Info *info, SparseHistData &data)
2782 MCounter::iterator it;
2784 for (it = cmap.begin(); it != cmap.end(); it++) {
2785 data.cmap[(*it).first] = (*it).second;
2788 data.samples = samples;
2792 * Reset stat value to default
2802 class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2806 * Set the parameters of this histogram. @sa HistStor::Params
2807 * @param size The number of buckets in the histogram
2808 * @return A reference to this histogram.
2811 init(size_type size)
2813 SparseHistStor::Params *params = new SparseHistStor::Params;
2814 this->setParams(params);
2816 return this->self();
2822 * A formula for statistics that is calculated when printed. A formula is
2823 * stored as a tree of Nodes that represent the equation to calculate.
2824 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2826 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2829 /** The root of the tree which represents the Formula */
2835 * Create and initialize thie formula, and register it with the database.
2840 * Create a formula with the given root node, register it with the
2842 * @param r The root of the expression tree.
2847 * Set an unitialized Formula to the given root.
2848 * @param r The root of the expression tree.
2849 * @return a reference to this formula.
2851 const Formula &operator=(Temp r);
2854 * Add the given tree to the existing one.
2855 * @param r The root of the expression tree.
2856 * @return a reference to this formula.
2858 const Formula &operator+=(Temp r);
2860 * Return the result of the Fomula in a vector. If there were no Vector
2861 * components to the Formula, then the vector is size 1. If there were,
2862 * like x/y with x being a vector of size 3, then the result returned will
2863 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2864 * @return The result vector.
2866 void result(VResult &vec) const;
2869 * Return the total Formula result. If there is a Vector
2870 * component to this Formula, then this is the result of the
2871 * Formula if the formula is applied after summing all the
2872 * components of the Vector. For example, if Formula is x/y where
2873 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2874 * there is no Vector component, total() returns the same value as
2875 * the first entry in the VResult val() returns.
2876 * @return The total of the result vector.
2878 Result total() const;
2881 * Return the number of elements in the tree.
2883 size_type size() const;
2888 * Formulas don't need to be reset
2897 std::string str() const;
2900 class FormulaNode : public Node
2903 const Formula &formula;
2904 mutable VResult vec;
2907 FormulaNode(const Formula &f) : formula(f) {}
2909 size_type size() const { return formula.size(); }
2910 const VResult &result() const { formula.result(vec); return vec; }
2911 Result total() const { return formula.total(); }
2913 std::string str() const { return formula.str(); }
2917 * Helper class to construct formula node trees.
2923 * Pointer to a Node object.
2929 * Copy the given pointer to this class.
2930 * @param n A pointer to a Node object to copy.
2932 Temp(NodePtr n) : node(n) { }
2935 * Return the node pointer.
2936 * @return the node pointer.
2938 operator NodePtr&() { return node; }
2942 * Create a new ScalarStatNode.
2943 * @param s The ScalarStat to place in a node.
2945 Temp(const Scalar &s)
2946 : node(new ScalarStatNode(s.info()))
2950 * Create a new ScalarStatNode.
2951 * @param s The ScalarStat to place in a node.
2953 Temp(const Value &s)
2954 : node(new ScalarStatNode(s.info()))
2958 * Create a new ScalarStatNode.
2959 * @param s The ScalarStat to place in a node.
2961 Temp(const Average &s)
2962 : node(new ScalarStatNode(s.info()))
2966 * Create a new VectorStatNode.
2967 * @param s The VectorStat to place in a node.
2969 Temp(const Vector &s)
2970 : node(new VectorStatNode(s.info()))
2973 Temp(const AverageVector &s)
2974 : node(new VectorStatNode(s.info()))
2980 Temp(const Formula &f)
2981 : node(new FormulaNode(f))
2985 * Create a new ScalarProxyNode.
2986 * @param p The ScalarProxy to place in a node.
2988 template <class Stat>
2989 Temp(const ScalarProxy<Stat> &p)
2990 : node(new ScalarProxyNode<Stat>(p))
2994 * Create a ConstNode
2995 * @param value The value of the const node.
2997 Temp(signed char value)
2998 : node(new ConstNode<signed char>(value))
3002 * Create a ConstNode
3003 * @param value The value of the const node.
3005 Temp(unsigned char value)
3006 : node(new ConstNode<unsigned char>(value))
3010 * Create a ConstNode
3011 * @param value The value of the const node.
3013 Temp(signed short value)
3014 : node(new ConstNode<signed short>(value))
3018 * Create a ConstNode
3019 * @param value The value of the const node.
3021 Temp(unsigned short value)
3022 : node(new ConstNode<unsigned short>(value))
3026 * Create a ConstNode
3027 * @param value The value of the const node.
3029 Temp(signed int value)
3030 : node(new ConstNode<signed int>(value))
3034 * Create a ConstNode
3035 * @param value The value of the const node.
3037 Temp(unsigned int value)
3038 : node(new ConstNode<unsigned int>(value))
3042 * Create a ConstNode
3043 * @param value The value of the const node.
3045 Temp(signed long value)
3046 : node(new ConstNode<signed long>(value))
3050 * Create a ConstNode
3051 * @param value The value of the const node.
3053 Temp(unsigned long value)
3054 : node(new ConstNode<unsigned long>(value))
3058 * Create a ConstNode
3059 * @param value The value of the const node.
3061 Temp(signed long long value)
3062 : node(new ConstNode<signed long long>(value))
3066 * Create a ConstNode
3067 * @param value The value of the const node.
3069 Temp(unsigned long long value)
3070 : node(new ConstNode<unsigned long long>(value))
3074 * Create a ConstNode
3075 * @param value The value of the const node.
3078 : node(new ConstNode<float>(value))
3082 * Create a ConstNode
3083 * @param value The value of the const node.
3086 : node(new ConstNode<double>(value))
3096 operator+(Temp l, Temp r)
3098 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
3102 operator-(Temp l, Temp r)
3104 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
3108 operator*(Temp l, Temp r)
3110 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
3114 operator/(Temp l, Temp r)
3116 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
3122 return NodePtr(new UnaryNode<std::negate<Result> >(l));
3125 template <typename T>
3129 return NodePtr(new ConstNode<T>(val));
3132 template <typename T>
3134 constantVector(T val)
3136 return NodePtr(new ConstVectorNode<T>(val));
3142 return NodePtr(new SumNode<std::plus<Result> >(val));
3145 /** Dump all statistics data to the registered outputs */
3152 * Register a callback that should be called whenever statistics are
3155 void registerResetCallback(Callback *cb);
3158 * Register a callback that should be called whenever statistics are
3159 * about to be dumped
3161 void registerDumpCallback(Callback *cb);
3163 std::list<Info *> &statsList();
3165 typedef std::map<const void *, Info *> MapType;
3166 MapType &statsMap();
3168 typedef std::map<std::string, Info *> NameMapType;
3169 NameMapType &nameMap();
3171 bool validateStatName(const std::string &name);
3173 } // namespace Stats
3175 void debugDumpStats();
3177 #endif // __BASE_STATISTICS_HH__