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__
64 #include "base/stats/info.hh"
65 #include "base/stats/output.hh"
66 #include "base/stats/types.hh"
67 #include "base/cast.hh"
68 #include "base/cprintf.hh"
69 #include "base/intmath.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;
337 DataWrapVec(const DataWrapVec &ref)
340 void operator=(const DataWrapVec &)
343 // The following functions are specific to vectors. If you use them
344 // in a non vector context, you will get a nice compiler error!
347 * Set the subfield name for the given index, and marks this stat to print
348 * at the end of simulation.
349 * @param index The subfield index.
350 * @param name The new name of the subfield.
351 * @return A reference to this stat.
354 subname(off_type index, const std::string &name)
356 Derived &self = this->self();
357 Info *info = self.info();
359 std::vector<std::string> &subn = info->subnames;
360 if (subn.size() <= index)
361 subn.resize(index + 1);
366 // The following functions are specific to 2d vectors. If you use
367 // them in a non vector context, you will get a nice compiler
368 // error because info doesn't have the right variables.
371 * Set the subfield description for the given index and marks this stat to
372 * print at the end of simulation.
373 * @param index The subfield index.
374 * @param desc The new description of the subfield
375 * @return A reference to this stat.
378 subdesc(off_type index, const std::string &desc)
380 Info *info = this->info();
382 std::vector<std::string> &subd = info->subdescs;
383 if (subd.size() <= index)
384 subd.resize(index + 1);
393 Derived &self = this->self();
394 Info *info = this->info();
396 size_t size = self.size();
397 for (off_type i = 0; i < size; ++i)
398 self.data(i)->prepare(info);
404 Derived &self = this->self();
405 Info *info = this->info();
407 size_t size = self.size();
408 for (off_type i = 0; i < size; ++i)
409 self.data(i)->reset(info);
413 template <class Derived, template <class> class InfoProxyType>
414 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
417 typedef InfoProxyType<Derived> Info;
420 * @warning This makes the assumption that if you're gonna subnames a 2d
421 * vector, you're subnaming across all y
424 ysubnames(const char **names)
426 Derived &self = this->self();
427 Info *info = this->info();
429 info->y_subnames.resize(self.y);
430 for (off_type i = 0; i < self.y; ++i)
431 info->y_subnames[i] = names[i];
436 ysubname(off_type index, const std::string &subname)
438 Derived &self = this->self();
439 Info *info = this->info();
441 assert(index < self.y);
442 info->y_subnames.resize(self.y);
443 info->y_subnames[index] = subname.c_str();
448 ysubname(off_type i) const
450 return this->info()->y_subnames[i];
455 //////////////////////////////////////////////////////////////////////
459 //////////////////////////////////////////////////////////////////////
462 * Templatized storage and interface for a simple scalar stat.
467 /** The statistic value. */
471 struct Params : public StorageParams {};
475 * Builds this storage element and calls the base constructor of the
483 * The the stat to the given value.
484 * @param val The new value.
486 void set(Counter val) { data = val; }
488 * Increment the stat by the given value.
489 * @param val The new value.
491 void inc(Counter val) { data += val; }
493 * Decrement the stat by the given value.
494 * @param val The new value.
496 void dec(Counter val) { data -= val; }
498 * Return the value of this stat as its base type.
499 * @return The value of this stat.
501 Counter value() const { return data; }
503 * Return the value of this stat as a result type.
504 * @return The value of this stat.
506 Result result() const { return (Result)data; }
508 * Prepare stat data for dumping or serialization
510 void prepare(Info *info) { }
512 * Reset stat value to default
514 void reset(Info *info) { data = Counter(); }
517 * @return true if zero value
519 bool zero() const { return data == Counter(); }
523 * Templatized storage and interface to a per-tick average stat. This keeps
524 * a current count and updates a total (count * ticks) when this count
525 * changes. This allows the quick calculation of a per tick count of the item
526 * being watched. This is good for keeping track of residencies in structures
527 * among other things.
532 /** The current count. */
534 /** The tick of the last reset */
536 /** The total count for all tick. */
537 mutable Result total;
538 /** The tick that current last changed. */
542 struct Params : public StorageParams {};
546 * Build and initializes this stat storage.
549 : current(0), lastReset(0), total(0), last(0)
553 * Set the current count to the one provided, update the total and last
555 * @param val The new count.
560 total += current * (curTick() - last);
566 * Increment the current count by the provided value, calls set.
567 * @param val The amount to increment.
569 void inc(Counter val) { set(current + val); }
572 * Deccrement the current count by the provided value, calls set.
573 * @param val The amount to decrement.
575 void dec(Counter val) { set(current - val); }
578 * Return the current count.
579 * @return The current count.
581 Counter value() const { return current; }
584 * Return the current average.
585 * @return The current average.
590 assert(last == curTick());
591 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
595 * @return true if zero value
597 bool zero() const { return total == 0.0; }
600 * Prepare stat data for dumping or serialization
605 total += current * (curTick() - last);
610 * Reset stat value to default
617 lastReset = curTick();
623 * Implementation of a scalar stat. The type of stat is determined by the
626 template <class Derived, class Stor>
627 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
630 typedef Stor Storage;
631 typedef typename Stor::Params Params;
634 /** The storage of this stat. */
635 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
639 * Retrieve the storage.
640 * @param index The vector index to access.
641 * @return The storage object at the given index.
646 return reinterpret_cast<Storage *>(storage);
650 * Retrieve a const pointer to the storage.
651 * for the given index.
652 * @param index The vector index to access.
653 * @return A const pointer to the storage object at the given index.
658 return reinterpret_cast<const Storage *>(storage);
664 new (storage) Storage(this->info());
670 * Return the current value of this stat as its base type.
671 * @return The current value.
673 Counter value() const { return data()->value(); }
682 // Common operators for stats
684 * Increment the stat by 1. This calls the associated storage object inc
687 void operator++() { data()->inc(1); }
689 * Decrement the stat by 1. This calls the associated storage object dec
692 void operator--() { data()->dec(1); }
694 /** Increment the stat by 1. */
695 void operator++(int) { ++*this; }
696 /** Decrement the stat by 1. */
697 void operator--(int) { --*this; }
700 * Set the data value to the given value. This calls the associated storage
701 * object set function.
702 * @param v The new value.
704 template <typename U>
705 void operator=(const U &v) { data()->set(v); }
708 * Increment the stat by the given value. This calls the associated
709 * storage object inc function.
710 * @param v The value to add.
712 template <typename U>
713 void operator+=(const U &v) { data()->inc(v); }
716 * Decrement the stat by the given value. This calls the associated
717 * storage object dec function.
718 * @param v The value to substract.
720 template <typename U>
721 void operator-=(const U &v) { data()->dec(v); }
724 * Return the number of elements, always 1 for a scalar.
727 size_type size() const { return 1; }
729 Counter value() { return data()->value(); }
731 Result result() { return data()->result(); }
733 Result total() { return result(); }
735 bool zero() { return result() == 0.0; }
737 void reset() { data()->reset(this->info()); }
738 void prepare() { data()->prepare(this->info()); }
741 class ProxyInfo : public ScalarInfo
744 std::string str() const { return std::to_string(value()); }
745 size_type size() const { return 1; }
746 bool check() const { return true; }
749 bool zero() const { return value() == 0; }
751 void visit(Output &visitor) { visitor.visit(*this); }
755 class ValueProxy : public ProxyInfo
761 ValueProxy(T &val) : scalar(&val) {}
762 Counter value() const { return *scalar; }
763 Result result() const { return *scalar; }
764 Result total() const { return *scalar; }
768 class FunctorProxy : public ProxyInfo
774 FunctorProxy(T &func) : functor(&func) {}
775 Counter value() const { return (*functor)(); }
776 Result result() const { return (*functor)(); }
777 Result total() const { return (*functor)(); }
781 * A proxy similar to the FunctorProxy, but allows calling a method of a bound
782 * object, instead of a global free-standing function.
784 template <class T, class V>
785 class MethodProxy : public ProxyInfo
789 typedef V (T::*MethodPointer) () const;
790 MethodPointer method;
793 MethodProxy(T *obj, MethodPointer meth) : object(obj), method(meth) {}
794 Counter value() const { return (object->*method)(); }
795 Result result() const { return (object->*method)(); }
796 Result total() const { return (object->*method)(); }
799 template <class Derived>
800 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
806 ValueBase() : proxy(NULL) { }
807 ~ValueBase() { if (proxy) delete proxy; }
813 proxy = new ValueProxy<T>(value);
822 proxy = new FunctorProxy<T>(func);
828 * Extended functor that calls the specified method of the provided object.
830 * @param obj Pointer to the object whose method should be called.
831 * @param method Pointer of the function / method of the object.
832 * @return Updated stats item.
834 template <class T, class V>
836 method(T *obj, V (T::*method)() const)
838 proxy = new MethodProxy<T,V>(obj, method);
843 Counter value() { return proxy->value(); }
844 Result result() const { return proxy->result(); }
845 Result total() const { return proxy->total(); };
846 size_type size() const { return proxy->size(); }
848 std::string str() const { return proxy->str(); }
849 bool zero() const { return proxy->zero(); }
850 bool check() const { return proxy != NULL; }
855 //////////////////////////////////////////////////////////////////////
859 //////////////////////////////////////////////////////////////////////
862 * A proxy class to access the stat at a given index in a VectorBase stat.
863 * Behaves like a ScalarBase.
865 template <class Stat>
869 /** Pointer to the parent Vector. */
872 /** The index to access in the parent VectorBase. */
877 * Return the current value of this stat as its base type.
878 * @return The current value.
880 Counter value() const { return stat.data(index)->value(); }
883 * Return the current value of this statas a result type.
884 * @return The current value.
886 Result result() const { return stat.data(index)->result(); }
890 * Create and initialize this proxy, do not register it with the database.
891 * @param i The index to access.
893 ScalarProxy(Stat &s, off_type i)
899 * Create a copy of the provided ScalarProxy.
900 * @param sp The proxy to copy.
902 ScalarProxy(const ScalarProxy &sp)
903 : stat(sp.stat), index(sp.index)
907 * Set this proxy equal to the provided one.
908 * @param sp The proxy to copy.
909 * @return A reference to this proxy.
912 operator=(const ScalarProxy &sp)
920 // Common operators for stats
922 * Increment the stat by 1. This calls the associated storage object inc
925 void operator++() { stat.data(index)->inc(1); }
927 * Decrement the stat by 1. This calls the associated storage object dec
930 void operator--() { stat.data(index)->dec(1); }
932 /** Increment the stat by 1. */
933 void operator++(int) { ++*this; }
934 /** Decrement the stat by 1. */
935 void operator--(int) { --*this; }
938 * Set the data value to the given value. This calls the associated storage
939 * object set function.
940 * @param v The new value.
942 template <typename U>
944 operator=(const U &v)
946 stat.data(index)->set(v);
950 * Increment the stat by the given value. This calls the associated
951 * storage object inc function.
952 * @param v The value to add.
954 template <typename U>
956 operator+=(const U &v)
958 stat.data(index)->inc(v);
962 * Decrement the stat by the given value. This calls the associated
963 * storage object dec function.
964 * @param v The value to substract.
966 template <typename U>
968 operator-=(const U &v)
970 stat.data(index)->dec(v);
974 * Return the number of elements, always 1 for a scalar.
977 size_type size() const { return 1; }
983 return csprintf("%s[%d]", stat.info()->name, index);
988 * Implementation of a vector of stats. The type of stat is determined by the
989 * Storage class. @sa ScalarBase
991 template <class Derived, class Stor>
992 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
995 typedef Stor Storage;
996 typedef typename Stor::Params Params;
999 typedef ScalarProxy<Derived> Proxy;
1000 friend class ScalarProxy<Derived>;
1001 friend class DataWrapVec<Derived, VectorInfoProxy>;
1004 /** The storage of this stat. */
1010 * Retrieve the storage.
1011 * @param index The vector index to access.
1012 * @return The storage object at the given index.
1014 Storage *data(off_type index) { return &storage[index]; }
1017 * Retrieve a const pointer to the storage.
1018 * @param index The vector index to access.
1019 * @return A const pointer to the storage object at the given index.
1021 const Storage *data(off_type index) const { return &storage[index]; }
1026 assert(s > 0 && "size must be positive!");
1027 assert(!storage && "already initialized");
1030 char *ptr = new char[_size * sizeof(Storage)];
1031 storage = reinterpret_cast<Storage *>(ptr);
1033 for (off_type i = 0; i < _size; ++i)
1034 new (&storage[i]) Storage(this->info());
1041 value(VCounter &vec) const
1044 for (off_type i = 0; i < size(); ++i)
1045 vec[i] = data(i)->value();
1049 * Copy the values to a local vector and return a reference to it.
1050 * @return A reference to a vector of the stat values.
1053 result(VResult &vec) const
1056 for (off_type i = 0; i < size(); ++i)
1057 vec[i] = data(i)->result();
1061 * Return a total of all entries in this vector.
1062 * @return The total of all vector entries.
1068 for (off_type i = 0; i < size(); ++i)
1069 total += data(i)->result();
1074 * @return the number of elements in this vector.
1076 size_type size() const { return _size; }
1081 for (off_type i = 0; i < size(); ++i)
1082 if (data(i)->zero())
1090 return storage != NULL;
1095 : storage(nullptr), _size(0)
1103 for (off_type i = 0; i < _size; ++i)
1104 data(i)->~Storage();
1105 delete [] reinterpret_cast<char *>(storage);
1109 * Set this vector to have the given size.
1110 * @param size The new size.
1111 * @return A reference to this stat.
1114 init(size_type size)
1116 Derived &self = this->self();
1122 * Return a reference (ScalarProxy) to the stat at the given index.
1123 * @param index The vector index to access.
1124 * @return A reference of the stat.
1127 operator[](off_type index)
1129 assert (index >= 0 && index < size());
1130 return Proxy(this->self(), index);
1134 template <class Stat>
1143 mutable VResult vec;
1145 typename Stat::Storage *
1146 data(off_type index)
1148 assert(index < len);
1149 return stat.data(offset + index);
1152 const typename Stat::Storage *
1153 data(off_type index) const
1155 assert(index < len);
1156 return stat.data(offset + index);
1165 for (off_type i = 0; i < size(); ++i)
1166 vec[i] = data(i)->result();
1175 for (off_type i = 0; i < size(); ++i)
1176 total += data(i)->result();
1181 VectorProxy(Stat &s, off_type o, size_type l)
1182 : stat(s), offset(o), len(l)
1186 VectorProxy(const VectorProxy &sp)
1187 : stat(sp.stat), offset(sp.offset), len(sp.len)
1192 operator=(const VectorProxy &sp)
1201 operator[](off_type index)
1203 assert (index >= 0 && index < size());
1204 return ScalarProxy<Stat>(stat, offset + index);
1207 size_type size() const { return len; }
1210 template <class Derived, class Stor>
1211 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1214 typedef Vector2dInfoProxy<Derived> Info;
1215 typedef Stor Storage;
1216 typedef typename Stor::Params Params;
1217 typedef VectorProxy<Derived> Proxy;
1218 friend class ScalarProxy<Derived>;
1219 friend class VectorProxy<Derived>;
1220 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1221 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1230 Storage *data(off_type index) { return &storage[index]; }
1231 const Storage *data(off_type index) const { return &storage[index]; }
1235 : x(0), y(0), _size(0), storage(nullptr)
1243 for (off_type i = 0; i < _size; ++i)
1244 data(i)->~Storage();
1245 delete [] reinterpret_cast<char *>(storage);
1249 init(size_type _x, size_type _y)
1251 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1252 assert(!storage && "already initialized");
1254 Derived &self = this->self();
1255 Info *info = this->info();
1263 char *ptr = new char[_size * sizeof(Storage)];
1264 storage = reinterpret_cast<Storage *>(ptr);
1266 for (off_type i = 0; i < _size; ++i)
1267 new (&storage[i]) Storage(info);
1275 operator[](off_type index)
1277 off_type offset = index * y;
1278 assert (index >= 0 && offset + y <= size());
1279 return Proxy(this->self(), offset, y);
1292 return data(0)->zero();
1294 for (off_type i = 0; i < size(); ++i)
1295 if (!data(i)->zero())
1304 Info *info = this->info();
1305 size_type size = this->size();
1307 for (off_type i = 0; i < size; ++i)
1308 data(i)->prepare(info);
1310 info->cvec.resize(size);
1311 for (off_type i = 0; i < size; ++i)
1312 info->cvec[i] = data(i)->value();
1316 * Reset stat value to default
1321 Info *info = this->info();
1322 size_type size = this->size();
1323 for (off_type i = 0; i < size; ++i)
1324 data(i)->reset(info);
1330 return storage != NULL;
1334 //////////////////////////////////////////////////////////////////////
1336 // Non formula statistics
1338 //////////////////////////////////////////////////////////////////////
1339 /** The parameters for a distribution stat. */
1340 struct DistParams : public StorageParams
1342 const DistType type;
1343 DistParams(DistType t) : type(t) {}
1347 * Templatized storage and interface for a distrbution stat.
1352 /** The parameters for a distribution stat. */
1353 struct Params : public DistParams
1355 /** The minimum value to track. */
1357 /** The maximum value to track. */
1359 /** The number of entries in each bucket. */
1360 Counter bucket_size;
1361 /** The number of buckets. Equal to (max-min)/bucket_size. */
1364 Params() : DistParams(Dist), min(0), max(0), bucket_size(0),
1369 /** The minimum value to track. */
1371 /** The maximum value to track. */
1373 /** The number of entries in each bucket. */
1374 Counter bucket_size;
1376 /** The smallest value sampled. */
1378 /** The largest value sampled. */
1380 /** The number of values sampled less than min. */
1382 /** The number of values sampled more than max. */
1384 /** The current sum. */
1386 /** The sum of squares. */
1388 /** The number of samples. */
1390 /** Counter for each bucket. */
1394 DistStor(Info *info)
1395 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1401 * Add a value to the distribution for the given number of times.
1402 * @param val The value to add.
1403 * @param number The number of times to add the value.
1406 sample(Counter val, int number)
1408 if (val < min_track)
1409 underflow += number;
1410 else if (val > max_track)
1414 (size_type)std::floor((val - min_track) / bucket_size);
1415 assert(index < size());
1416 cvec[index] += number;
1425 sum += val * number;
1426 squares += val * val * number;
1431 * Return the number of buckets in this distribution.
1432 * @return the number of buckets.
1434 size_type size() const { return cvec.size(); }
1437 * Returns true if any calls to sample have been made.
1438 * @return True if any values have been sampled.
1443 return samples == Counter();
1447 prepare(Info *info, DistData &data)
1449 const Params *params = safe_cast<const Params *>(info->storageParams);
1451 assert(params->type == Dist);
1452 data.type = params->type;
1453 data.min = params->min;
1454 data.max = params->max;
1455 data.bucket_size = params->bucket_size;
1457 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1458 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1459 data.underflow = underflow;
1460 data.overflow = overflow;
1462 data.cvec.resize(params->buckets);
1463 for (off_type i = 0; i < params->buckets; ++i)
1464 data.cvec[i] = cvec[i];
1467 data.squares = squares;
1468 data.samples = samples;
1472 * Reset stat value to default
1477 const Params *params = safe_cast<const Params *>(info->storageParams);
1478 min_track = params->min;
1479 max_track = params->max;
1480 bucket_size = params->bucket_size;
1482 min_val = CounterLimits::max();
1483 max_val = CounterLimits::min();
1484 underflow = Counter();
1485 overflow = Counter();
1487 size_type size = cvec.size();
1488 for (off_type i = 0; i < size; ++i)
1489 cvec[i] = Counter();
1492 squares = Counter();
1493 samples = Counter();
1498 * Templatized storage and interface for a histogram stat.
1503 /** The parameters for a distribution stat. */
1504 struct Params : public DistParams
1506 /** The number of buckets.. */
1509 Params() : DistParams(Hist), buckets(0) {}
1513 /** The minimum value to track. */
1515 /** The maximum value to track. */
1517 /** The number of entries in each bucket. */
1518 Counter bucket_size;
1520 /** The current sum. */
1522 /** The sum of logarithm of each sample, used to compute geometric mean. */
1524 /** The sum of squares. */
1526 /** The number of samples. */
1528 /** Counter for each bucket. */
1532 HistStor(Info *info)
1533 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1540 void grow_convert();
1541 void add(HistStor *);
1544 * Add a value to the distribution for the given number of times.
1545 * @param val The value to add.
1546 * @param number The number of times to add the value.
1549 sample(Counter val, int number)
1551 assert(min_bucket < max_bucket);
1552 if (val < min_bucket) {
1553 if (min_bucket == 0)
1556 while (val < min_bucket)
1558 } else if (val >= max_bucket + bucket_size) {
1559 if (min_bucket == 0) {
1560 while (val >= max_bucket + bucket_size)
1563 while (val >= max_bucket + bucket_size)
1569 (int64_t)std::floor((val - min_bucket) / bucket_size);
1571 assert(index < size());
1572 cvec[index] += number;
1574 sum += val * number;
1575 squares += val * val * number;
1576 logs += log(val) * number;
1581 * Return the number of buckets in this distribution.
1582 * @return the number of buckets.
1584 size_type size() const { return cvec.size(); }
1587 * Returns true if any calls to sample have been made.
1588 * @return True if any values have been sampled.
1593 return samples == Counter();
1597 prepare(Info *info, DistData &data)
1599 const Params *params = safe_cast<const Params *>(info->storageParams);
1601 assert(params->type == Hist);
1602 data.type = params->type;
1603 data.min = min_bucket;
1604 data.max = max_bucket + bucket_size - 1;
1605 data.bucket_size = bucket_size;
1607 data.min_val = min_bucket;
1608 data.max_val = max_bucket;
1610 int buckets = params->buckets;
1611 data.cvec.resize(buckets);
1612 for (off_type i = 0; i < buckets; ++i)
1613 data.cvec[i] = cvec[i];
1617 data.squares = squares;
1618 data.samples = samples;
1622 * Reset stat value to default
1627 const Params *params = safe_cast<const Params *>(info->storageParams);
1629 max_bucket = params->buckets - 1;
1632 size_type size = cvec.size();
1633 for (off_type i = 0; i < size; ++i)
1634 cvec[i] = Counter();
1637 squares = Counter();
1638 samples = Counter();
1644 * Templatized storage and interface for a distribution that calculates mean
1650 struct Params : public DistParams
1652 Params() : DistParams(Deviation) {}
1656 /** The current sum. */
1658 /** The sum of squares. */
1660 /** The number of samples. */
1665 * Create and initialize this storage.
1667 SampleStor(Info *info)
1668 : sum(Counter()), squares(Counter()), samples(Counter())
1672 * Add a value the given number of times to this running average.
1673 * Update the running sum and sum of squares, increment the number of
1674 * values seen by the given number.
1675 * @param val The value to add.
1676 * @param number The number of times to add the value.
1679 sample(Counter val, int number)
1681 Counter value = val * number;
1683 squares += value * value;
1688 * Return the number of entries in this stat, 1
1691 size_type size() const { return 1; }
1694 * Return true if no samples have been added.
1695 * @return True if no samples have been added.
1697 bool zero() const { return samples == Counter(); }
1700 prepare(Info *info, DistData &data)
1702 const Params *params = safe_cast<const Params *>(info->storageParams);
1704 assert(params->type == Deviation);
1705 data.type = params->type;
1707 data.squares = squares;
1708 data.samples = samples;
1712 * Reset stat value to default
1718 squares = Counter();
1719 samples = Counter();
1724 * Templatized storage for distribution that calculates per tick mean and
1730 struct Params : public DistParams
1732 Params() : DistParams(Deviation) {}
1736 /** Current total. */
1738 /** Current sum of squares. */
1743 * Create and initialize this storage.
1745 AvgSampleStor(Info *info)
1746 : sum(Counter()), squares(Counter())
1750 * Add a value to the distribution for the given number of times.
1751 * Update the running sum and sum of squares.
1752 * @param val The value to add.
1753 * @param number The number of times to add the value.
1756 sample(Counter val, int number)
1758 Counter value = val * number;
1760 squares += value * value;
1764 * Return the number of entries, in this case 1.
1767 size_type size() const { return 1; }
1770 * Return true if no samples have been added.
1771 * @return True if the sum is zero.
1773 bool zero() const { return sum == Counter(); }
1776 prepare(Info *info, DistData &data)
1778 const Params *params = safe_cast<const Params *>(info->storageParams);
1780 assert(params->type == Deviation);
1781 data.type = params->type;
1783 data.squares = squares;
1784 data.samples = curTick();
1788 * Reset stat value to default
1794 squares = Counter();
1799 * Implementation of a distribution stat. The type of distribution is
1800 * determined by the Storage template. @sa ScalarBase
1802 template <class Derived, class Stor>
1803 class DistBase : public DataWrap<Derived, DistInfoProxy>
1806 typedef DistInfoProxy<Derived> Info;
1807 typedef Stor Storage;
1808 typedef typename Stor::Params Params;
1811 /** The storage for this stat. */
1812 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1816 * Retrieve the storage.
1817 * @return The storage object for this stat.
1822 return reinterpret_cast<Storage *>(storage);
1826 * Retrieve a const pointer to the storage.
1827 * @return A const pointer to the storage object for this stat.
1832 return reinterpret_cast<const Storage *>(storage);
1838 new (storage) Storage(this->info());
1846 * Add a value to the distribtion n times. Calls sample on the storage
1848 * @param v The value to add.
1849 * @param n The number of times to add it, defaults to 1.
1851 template <typename U>
1852 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1855 * Return the number of entries in this stat.
1856 * @return The number of entries.
1858 size_type size() const { return data()->size(); }
1860 * Return true if no samples have been added.
1861 * @return True if there haven't been any samples.
1863 bool zero() const { return data()->zero(); }
1868 Info *info = this->info();
1869 data()->prepare(info, info->data);
1873 * Reset stat value to default
1878 data()->reset(this->info());
1882 * Add the argument distribution to the this distibution.
1884 void add(DistBase &d) { data()->add(d.data()); }
1888 template <class Stat>
1891 template <class Derived, class Stor>
1892 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1895 typedef VectorDistInfoProxy<Derived> Info;
1896 typedef Stor Storage;
1897 typedef typename Stor::Params Params;
1898 typedef DistProxy<Derived> Proxy;
1899 friend class DistProxy<Derived>;
1900 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1908 data(off_type index)
1910 return &storage[index];
1914 data(off_type index) const
1916 return &storage[index];
1922 assert(s > 0 && "size must be positive!");
1923 assert(!storage && "already initialized");
1926 char *ptr = new char[_size * sizeof(Storage)];
1927 storage = reinterpret_cast<Storage *>(ptr);
1929 Info *info = this->info();
1930 for (off_type i = 0; i < _size; ++i)
1931 new (&storage[i]) Storage(info);
1946 for (off_type i = 0; i < _size; ++i)
1947 data(i)->~Storage();
1948 delete [] reinterpret_cast<char *>(storage);
1951 Proxy operator[](off_type index)
1953 assert(index >= 0 && index < size());
1954 return Proxy(this->self(), index);
1966 for (off_type i = 0; i < size(); ++i)
1967 if (!data(i)->zero())
1975 Info *info = this->info();
1976 size_type size = this->size();
1977 info->data.resize(size);
1978 for (off_type i = 0; i < size; ++i)
1979 data(i)->prepare(info, info->data[i]);
1985 return storage != NULL;
1989 template <class Stat>
1997 typename Stat::Storage *data() { return stat.data(index); }
1998 const typename Stat::Storage *data() const { return stat.data(index); }
2001 DistProxy(Stat &s, off_type i)
2005 DistProxy(const DistProxy &sp)
2006 : stat(sp.stat), index(sp.index)
2010 operator=(const DistProxy &sp)
2018 template <typename U>
2020 sample(const U &v, int n = 1)
2022 data()->sample(v, n);
2034 return data()->zero();
2038 * Proxy has no state. Nothing to reset.
2043 //////////////////////////////////////////////////////////////////////
2047 //////////////////////////////////////////////////////////////////////
2050 * Base class for formula statistic node. These nodes are used to build a tree
2051 * that represents the formula.
2057 * Return the number of nodes in the subtree starting at this node.
2058 * @return the number of nodes in this subtree.
2060 virtual size_type size() const = 0;
2062 * Return the result vector of this subtree.
2063 * @return The result vector of this subtree.
2065 virtual const VResult &result() const = 0;
2067 * Return the total of the result vector.
2068 * @return The total of the result vector.
2070 virtual Result total() const = 0;
2075 virtual std::string str() const = 0;
2078 /** Shared pointer to a function Node. */
2079 typedef std::shared_ptr<Node> NodePtr;
2081 class ScalarStatNode : public Node
2084 const ScalarInfo *data;
2085 mutable VResult vresult;
2088 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2093 vresult[0] = data->result();
2097 Result total() const { return data->result(); };
2099 size_type size() const { return 1; }
2104 std::string str() const { return data->name; }
2107 template <class Stat>
2108 class ScalarProxyNode : public Node
2111 const ScalarProxy<Stat> proxy;
2112 mutable VResult vresult;
2115 ScalarProxyNode(const ScalarProxy<Stat> &p)
2116 : proxy(p), vresult(1)
2122 vresult[0] = proxy.result();
2129 return proxy.result();
2148 class VectorStatNode : public Node
2151 const VectorInfo *data;
2154 VectorStatNode(const VectorInfo *d) : data(d) { }
2155 const VResult &result() const { return data->result(); }
2156 Result total() const { return data->total(); };
2158 size_type size() const { return data->size(); }
2160 std::string str() const { return data->name; }
2164 class ConstNode : public Node
2170 ConstNode(T s) : vresult(1, (Result)s) {}
2171 const VResult &result() const { return vresult; }
2172 Result total() const { return vresult[0]; };
2173 size_type size() const { return 1; }
2174 std::string str() const { return std::to_string(vresult[0]); }
2178 class ConstVectorNode : public Node
2184 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2185 const VResult &result() const { return vresult; }
2190 size_type size = this->size();
2192 for (off_type i = 0; i < size; i++)
2197 size_type size() const { return vresult.size(); }
2201 size_type size = this->size();
2202 std::string tmp = "(";
2203 for (off_type i = 0; i < size; i++)
2204 tmp += csprintf("%s ", std::to_string(vresult[i]));
2214 struct OpString<std::plus<Result> >
2216 static std::string str() { return "+"; }
2220 struct OpString<std::minus<Result> >
2222 static std::string str() { return "-"; }
2226 struct OpString<std::multiplies<Result> >
2228 static std::string str() { return "*"; }
2232 struct OpString<std::divides<Result> >
2234 static std::string str() { return "/"; }
2238 struct OpString<std::modulus<Result> >
2240 static std::string str() { return "%"; }
2244 struct OpString<std::negate<Result> >
2246 static std::string str() { return "-"; }
2250 class UnaryNode : public Node
2254 mutable VResult vresult;
2257 UnaryNode(NodePtr &p) : l(p) {}
2262 const VResult &lvec = l->result();
2263 size_type size = lvec.size();
2267 vresult.resize(size);
2269 for (off_type i = 0; i < size; ++i)
2270 vresult[i] = op(lvec[i]);
2278 const VResult &vec = this->result();
2280 for (off_type i = 0; i < size(); i++)
2285 size_type size() const { return l->size(); }
2290 return OpString<Op>::str() + l->str();
2295 class BinaryNode : public Node
2300 mutable VResult vresult;
2303 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2309 const VResult &lvec = l->result();
2310 const VResult &rvec = r->result();
2312 assert(lvec.size() > 0 && rvec.size() > 0);
2314 if (lvec.size() == 1 && rvec.size() == 1) {
2316 vresult[0] = op(lvec[0], rvec[0]);
2317 } else if (lvec.size() == 1) {
2318 size_type size = rvec.size();
2319 vresult.resize(size);
2320 for (off_type i = 0; i < size; ++i)
2321 vresult[i] = op(lvec[0], rvec[i]);
2322 } else if (rvec.size() == 1) {
2323 size_type size = lvec.size();
2324 vresult.resize(size);
2325 for (off_type i = 0; i < size; ++i)
2326 vresult[i] = op(lvec[i], rvec[0]);
2327 } else if (rvec.size() == lvec.size()) {
2328 size_type size = rvec.size();
2329 vresult.resize(size);
2330 for (off_type i = 0; i < size; ++i)
2331 vresult[i] = op(lvec[i], rvec[i]);
2340 const VResult &vec = this->result();
2341 const VResult &lvec = l->result();
2342 const VResult &rvec = r->result();
2348 assert(lvec.size() > 0 && rvec.size() > 0);
2349 assert(lvec.size() == rvec.size() ||
2350 lvec.size() == 1 || rvec.size() == 1);
2352 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2353 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2354 for (off_type i = 0; i < size(); ++i) {
2358 return op(lsum, rsum);
2361 /** Otherwise divide each item by the divisor */
2362 for (off_type i = 0; i < size(); ++i) {
2372 size_type ls = l->size();
2373 size_type rs = r->size();
2376 } else if (rs == 1) {
2379 assert(ls == rs && "Node vector sizes are not equal");
2387 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2392 class SumNode : public Node
2396 mutable VResult vresult;
2399 SumNode(NodePtr &p) : l(p), vresult(1) {}
2404 const VResult &lvec = l->result();
2405 size_type size = lvec.size();
2411 for (off_type i = 0; i < size; ++i)
2412 vresult[0] = op(vresult[0], lvec[i]);
2420 const VResult &lvec = l->result();
2421 size_type size = lvec.size();
2424 Result result = 0.0;
2427 for (off_type i = 0; i < size; ++i)
2428 result = op(result, lvec[i]);
2433 size_type size() const { return 1; }
2438 return csprintf("total(%s)", l->str());
2443 //////////////////////////////////////////////////////////////////////
2445 // Visible Statistics Types
2447 //////////////////////////////////////////////////////////////////////
2449 * @defgroup VisibleStats "Statistic Types"
2450 * These are the statistics that are used in the simulator.
2455 * This is a simple scalar statistic, like a counter.
2456 * @sa Stat, ScalarBase, StatStor
2458 class Scalar : public ScalarBase<Scalar, StatStor>
2461 using ScalarBase<Scalar, StatStor>::operator=;
2465 * A stat that calculates the per tick average of a value.
2466 * @sa Stat, ScalarBase, AvgStor
2468 class Average : public ScalarBase<Average, AvgStor>
2471 using ScalarBase<Average, AvgStor>::operator=;
2474 class Value : public ValueBase<Value>
2479 * A vector of scalar stats.
2480 * @sa Stat, VectorBase, StatStor
2482 class Vector : public VectorBase<Vector, StatStor>
2487 * A vector of Average stats.
2488 * @sa Stat, VectorBase, AvgStor
2490 class AverageVector : public VectorBase<AverageVector, AvgStor>
2495 * A 2-Dimensional vecto of scalar stats.
2496 * @sa Stat, Vector2dBase, StatStor
2498 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2503 * A simple distribution stat.
2504 * @sa Stat, DistBase, DistStor
2506 class Distribution : public DistBase<Distribution, DistStor>
2510 * Set the parameters of this distribution. @sa DistStor::Params
2511 * @param min The minimum value of the distribution.
2512 * @param max The maximum value of the distribution.
2513 * @param bkt The number of values in each bucket.
2514 * @return A reference to this distribution.
2517 init(Counter min, Counter max, Counter bkt)
2519 DistStor::Params *params = new DistStor::Params;
2522 params->bucket_size = bkt;
2523 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2524 this->setParams(params);
2526 return this->self();
2531 * A simple histogram stat.
2532 * @sa Stat, DistBase, HistStor
2534 class Histogram : public DistBase<Histogram, HistStor>
2538 * Set the parameters of this histogram. @sa HistStor::Params
2539 * @param size The number of buckets in the histogram
2540 * @return A reference to this histogram.
2543 init(size_type size)
2545 HistStor::Params *params = new HistStor::Params;
2546 params->buckets = size;
2547 this->setParams(params);
2549 return this->self();
2554 * Calculates the mean and variance of all the samples.
2555 * @sa DistBase, SampleStor
2557 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2561 * Construct and initialize this distribution.
2565 SampleStor::Params *params = new SampleStor::Params;
2567 this->setParams(params);
2572 * Calculates the per tick mean and variance of the samples.
2573 * @sa DistBase, AvgSampleStor
2575 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2579 * Construct and initialize this distribution.
2583 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2585 this->setParams(params);
2590 * A vector of distributions.
2591 * @sa VectorDistBase, DistStor
2593 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2597 * Initialize storage and parameters for this distribution.
2598 * @param size The size of the vector (the number of distributions).
2599 * @param min The minimum value of the distribution.
2600 * @param max The maximum value of the distribution.
2601 * @param bkt The number of values in each bucket.
2602 * @return A reference to this distribution.
2604 VectorDistribution &
2605 init(size_type size, Counter min, Counter max, Counter bkt)
2607 DistStor::Params *params = new DistStor::Params;
2610 params->bucket_size = bkt;
2611 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2612 this->setParams(params);
2614 return this->self();
2619 * This is a vector of StandardDeviation stats.
2620 * @sa VectorDistBase, SampleStor
2622 class VectorStandardDeviation
2623 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2627 * Initialize storage for this distribution.
2628 * @param size The size of the vector.
2629 * @return A reference to this distribution.
2631 VectorStandardDeviation &
2632 init(size_type size)
2634 SampleStor::Params *params = new SampleStor::Params;
2636 this->setParams(params);
2637 return this->self();
2642 * This is a vector of AverageDeviation stats.
2643 * @sa VectorDistBase, AvgSampleStor
2645 class VectorAverageDeviation
2646 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2650 * Initialize storage for this distribution.
2651 * @param size The size of the vector.
2652 * @return A reference to this distribution.
2654 VectorAverageDeviation &
2655 init(size_type size)
2657 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2659 this->setParams(params);
2660 return this->self();
2664 template <class Stat>
2665 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2668 mutable VResult vec;
2669 mutable VCounter cvec;
2672 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2674 size_type size() const { return this->s.size(); }
2679 this->s.result(vec);
2682 Result total() const { return this->s.total(); }
2683 VCounter &value() const { return cvec; }
2685 std::string str() const { return this->s.str(); }
2688 template <class Stat>
2689 class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2692 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2696 * Implementation of a sparse histogram stat. The storage class is
2697 * determined by the Storage template.
2699 template <class Derived, class Stor>
2700 class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2703 typedef SparseHistInfoProxy<Derived> Info;
2704 typedef Stor Storage;
2705 typedef typename Stor::Params Params;
2708 /** The storage for this stat. */
2709 char storage[sizeof(Storage)];
2713 * Retrieve the storage.
2714 * @return The storage object for this stat.
2719 return reinterpret_cast<Storage *>(storage);
2723 * Retrieve a const pointer to the storage.
2724 * @return A const pointer to the storage object for this stat.
2729 return reinterpret_cast<const Storage *>(storage);
2735 new (storage) Storage(this->info());
2740 SparseHistBase() { }
2743 * Add a value to the distribtion n times. Calls sample on the storage
2745 * @param v The value to add.
2746 * @param n The number of times to add it, defaults to 1.
2748 template <typename U>
2749 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2752 * Return the number of entries in this stat.
2753 * @return The number of entries.
2755 size_type size() const { return data()->size(); }
2757 * Return true if no samples have been added.
2758 * @return True if there haven't been any samples.
2760 bool zero() const { return data()->zero(); }
2765 Info *info = this->info();
2766 data()->prepare(info, info->data);
2770 * Reset stat value to default
2775 data()->reset(this->info());
2780 * Templatized storage and interface for a sparse histogram stat.
2782 class SparseHistStor
2785 /** The parameters for a sparse histogram stat. */
2786 struct Params : public DistParams
2788 Params() : DistParams(Hist) {}
2792 /** Counter for number of samples */
2794 /** Counter for each bucket. */
2798 SparseHistStor(Info *info)
2804 * Add a value to the distribution for the given number of times.
2805 * @param val The value to add.
2806 * @param number The number of times to add the value.
2809 sample(Counter val, int number)
2811 cmap[val] += number;
2816 * Return the number of buckets in this distribution.
2817 * @return the number of buckets.
2819 size_type size() const { return cmap.size(); }
2822 * Returns true if any calls to sample have been made.
2823 * @return True if any values have been sampled.
2828 return samples == Counter();
2832 prepare(Info *info, SparseHistData &data)
2834 MCounter::iterator it;
2836 for (it = cmap.begin(); it != cmap.end(); it++) {
2837 data.cmap[(*it).first] = (*it).second;
2840 data.samples = samples;
2844 * Reset stat value to default
2854 class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2858 * Set the parameters of this histogram. @sa HistStor::Params
2859 * @param size The number of buckets in the histogram
2860 * @return A reference to this histogram.
2863 init(size_type size)
2865 SparseHistStor::Params *params = new SparseHistStor::Params;
2866 this->setParams(params);
2868 return this->self();
2874 * A formula for statistics that is calculated when printed. A formula is
2875 * stored as a tree of Nodes that represent the equation to calculate.
2876 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2878 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2881 /** The root of the tree which represents the Formula */
2887 * Create and initialize thie formula, and register it with the database.
2892 * Create a formula with the given root node, register it with the
2894 * @param r The root of the expression tree.
2899 * Set an unitialized Formula to the given root.
2900 * @param r The root of the expression tree.
2901 * @return a reference to this formula.
2903 const Formula &operator=(Temp r);
2906 * Add the given tree to the existing one.
2907 * @param r The root of the expression tree.
2908 * @return a reference to this formula.
2910 const Formula &operator+=(Temp r);
2913 * Divide the existing tree by the given one.
2914 * @param r The root of the expression tree.
2915 * @return a reference to this formula.
2917 const Formula &operator/=(Temp r);
2920 * Return the result of the Fomula in a vector. If there were no Vector
2921 * components to the Formula, then the vector is size 1. If there were,
2922 * like x/y with x being a vector of size 3, then the result returned will
2923 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2924 * @return The result vector.
2926 void result(VResult &vec) const;
2929 * Return the total Formula result. If there is a Vector
2930 * component to this Formula, then this is the result of the
2931 * Formula if the formula is applied after summing all the
2932 * components of the Vector. For example, if Formula is x/y where
2933 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2934 * there is no Vector component, total() returns the same value as
2935 * the first entry in the VResult val() returns.
2936 * @return The total of the result vector.
2938 Result total() const;
2941 * Return the number of elements in the tree.
2943 size_type size() const;
2948 * Formulas don't need to be reset
2957 std::string str() const;
2960 class FormulaNode : public Node
2963 const Formula &formula;
2964 mutable VResult vec;
2967 FormulaNode(const Formula &f) : formula(f) {}
2969 size_type size() const { return formula.size(); }
2970 const VResult &result() const { formula.result(vec); return vec; }
2971 Result total() const { return formula.total(); }
2973 std::string str() const { return formula.str(); }
2977 * Helper class to construct formula node trees.
2983 * Pointer to a Node object.
2989 * Copy the given pointer to this class.
2990 * @param n A pointer to a Node object to copy.
2992 Temp(const NodePtr &n) : node(n) { }
2994 Temp(NodePtr &&n) : node(std::move(n)) { }
2997 * Return the node pointer.
2998 * @return the node pointer.
3000 operator NodePtr&() { return node; }
3003 * Makde gcc < 4.6.3 happy and explicitly get the underlying node.
3005 NodePtr getNodePtr() const { return node; }
3009 * Create a new ScalarStatNode.
3010 * @param s The ScalarStat to place in a node.
3012 Temp(const Scalar &s)
3013 : node(new ScalarStatNode(s.info()))
3017 * Create a new ScalarStatNode.
3018 * @param s The ScalarStat to place in a node.
3020 Temp(const Value &s)
3021 : node(new ScalarStatNode(s.info()))
3025 * Create a new ScalarStatNode.
3026 * @param s The ScalarStat to place in a node.
3028 Temp(const Average &s)
3029 : node(new ScalarStatNode(s.info()))
3033 * Create a new VectorStatNode.
3034 * @param s The VectorStat to place in a node.
3036 Temp(const Vector &s)
3037 : node(new VectorStatNode(s.info()))
3040 Temp(const AverageVector &s)
3041 : node(new VectorStatNode(s.info()))
3047 Temp(const Formula &f)
3048 : node(new FormulaNode(f))
3052 * Create a new ScalarProxyNode.
3053 * @param p The ScalarProxy to place in a node.
3055 template <class Stat>
3056 Temp(const ScalarProxy<Stat> &p)
3057 : node(new ScalarProxyNode<Stat>(p))
3061 * Create a ConstNode
3062 * @param value The value of the const node.
3064 Temp(signed char value)
3065 : node(new ConstNode<signed char>(value))
3069 * Create a ConstNode
3070 * @param value The value of the const node.
3072 Temp(unsigned char value)
3073 : node(new ConstNode<unsigned char>(value))
3077 * Create a ConstNode
3078 * @param value The value of the const node.
3080 Temp(signed short value)
3081 : node(new ConstNode<signed short>(value))
3085 * Create a ConstNode
3086 * @param value The value of the const node.
3088 Temp(unsigned short value)
3089 : node(new ConstNode<unsigned short>(value))
3093 * Create a ConstNode
3094 * @param value The value of the const node.
3096 Temp(signed int value)
3097 : node(new ConstNode<signed int>(value))
3101 * Create a ConstNode
3102 * @param value The value of the const node.
3104 Temp(unsigned int value)
3105 : node(new ConstNode<unsigned int>(value))
3109 * Create a ConstNode
3110 * @param value The value of the const node.
3112 Temp(signed long value)
3113 : node(new ConstNode<signed long>(value))
3117 * Create a ConstNode
3118 * @param value The value of the const node.
3120 Temp(unsigned long value)
3121 : node(new ConstNode<unsigned long>(value))
3125 * Create a ConstNode
3126 * @param value The value of the const node.
3128 Temp(signed long long value)
3129 : node(new ConstNode<signed long long>(value))
3133 * Create a ConstNode
3134 * @param value The value of the const node.
3136 Temp(unsigned long long value)
3137 : node(new ConstNode<unsigned long long>(value))
3141 * Create a ConstNode
3142 * @param value The value of the const node.
3145 : node(new ConstNode<float>(value))
3149 * Create a ConstNode
3150 * @param value The value of the const node.
3153 : node(new ConstNode<double>(value))
3163 operator+(Temp l, Temp r)
3165 return Temp(std::make_shared<BinaryNode<std::plus<Result> > >(l, r));
3169 operator-(Temp l, Temp r)
3171 return Temp(std::make_shared<BinaryNode<std::minus<Result> > >(l, r));
3175 operator*(Temp l, Temp r)
3177 return Temp(std::make_shared<BinaryNode<std::multiplies<Result> > >(l, r));
3181 operator/(Temp l, Temp r)
3183 return Temp(std::make_shared<BinaryNode<std::divides<Result> > >(l, r));
3189 return Temp(std::make_shared<UnaryNode<std::negate<Result> > >(l));
3192 template <typename T>
3196 return Temp(std::make_shared<ConstNode<T> >(val));
3199 template <typename T>
3201 constantVector(T val)
3203 return Temp(std::make_shared<ConstVectorNode<T> >(val));
3209 return Temp(std::make_shared<SumNode<std::plus<Result> > >(val));
3212 /** Dump all statistics data to the registered outputs */
3219 * Register reset and dump handlers. These are the functions which
3220 * will actually perform the whole statistics reset/dump actions
3221 * including processing the reset/dump callbacks
3223 typedef void (*Handler)();
3225 void registerHandlers(Handler reset_handler, Handler dump_handler);
3228 * Register a callback that should be called whenever statistics are
3231 void registerResetCallback(Callback *cb);
3234 * Register a callback that should be called whenever statistics are
3235 * about to be dumped
3237 void registerDumpCallback(Callback *cb);
3240 * Process all the callbacks in the reset callbacks queue
3242 void processResetQueue();
3245 * Process all the callbacks in the dump callbacks queue
3247 void processDumpQueue();
3249 std::list<Info *> &statsList();
3251 typedef std::map<const void *, Info *> MapType;
3252 MapType &statsMap();
3254 typedef std::map<std::string, Info *> NameMapType;
3255 NameMapType &nameMap();
3257 bool validateStatName(const std::string &name);
3259 } // namespace Stats
3261 void debugDumpStats();
3263 #endif // __BASE_STATISTICS_HH__