2 * Copyright (c) 2019 Arm Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2003-2005 The Regents of The University of Michigan
15 * Copyright (c) 2017, Centre National de la Recherche Scientifique
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * Declaration of Statistics objects.
49 * Generalized N-dimensinal vector
53 * -- these both can use the same function that prints out a
54 * specific set of stats
55 * VectorStandardDeviation totals
58 #ifndef __BASE_STATISTICS_HH__
59 #define __BASE_STATISTICS_HH__
75 #include "base/stats/group.hh"
76 #include "base/stats/info.hh"
77 #include "base/stats/output.hh"
78 #include "base/stats/types.hh"
79 #include "base/cast.hh"
80 #include "base/cprintf.hh"
81 #include "base/intmath.hh"
82 #include "base/str.hh"
83 #include "base/types.hh"
87 /** The current simulated tick. */
88 extern Tick curTick();
90 /* A namespace for all of the Statistics */
93 template <class Stat, class Base>
94 class InfoProxy : public Base
100 InfoProxy(Stat &stat) : s(stat) {}
102 bool check() const { return s.check(); }
103 void prepare() { s.prepare(); }
104 void reset() { s.reset(); }
106 visit(Output &visitor)
108 visitor.visit(*static_cast<Base *>(this));
110 bool zero() const { return s.zero(); }
113 template <class Stat>
114 class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
117 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
119 Counter value() const { return this->s.value(); }
120 Result result() const { return this->s.result(); }
121 Result total() const { return this->s.total(); }
124 template <class Stat>
125 class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
128 mutable VCounter cvec;
129 mutable VResult rvec;
132 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
134 size_type size() const { return this->s.size(); }
146 this->s.result(rvec);
150 Result total() const { return this->s.total(); }
153 template <class Stat>
154 class DistInfoProxy : public InfoProxy<Stat, DistInfo>
157 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
160 template <class Stat>
161 class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
164 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
166 size_type size() const { return this->s.size(); }
169 template <class Stat>
170 class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
173 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
175 Result total() const { return this->s.total(); }
180 virtual ~StorageParams();
189 /** Set up an info class for this statistic */
190 void setInfo(Group *parent, Info *info);
191 /** Save Storage class parameters if any */
192 void setParams(const StorageParams *params);
193 /** Save Storage class parameters if any */
196 /** Grab the information class for this statistic */
198 /** Grab the information class for this statistic */
199 const Info *info() const;
206 * Reset the stat to the default state.
211 * @return true if this stat has a value and satisfies its
212 * requirement as a prereq
214 bool zero() const { return true; }
217 * Check that this stat has been set up properly and is ready for
219 * @return true for success
221 bool check() const { return true; }
224 template <class Derived, template <class> class InfoProxyType>
225 class DataWrap : public InfoAccess
228 typedef InfoProxyType<Derived> Info;
231 Derived &self() { return *static_cast<Derived *>(this); }
237 return safe_cast<Info *>(InfoAccess::info());
244 return safe_cast<const Info *>(InfoAccess::info());
249 DataWrap(const DataWrap &) = delete;
250 DataWrap &operator=(const DataWrap &) = delete;
253 DataWrap(Group *parent, const char *name, const char *desc)
255 auto info = new Info(self());
256 this->setInfo(parent, info);
259 parent->addStat(info);
262 info->setName(parent, name);
263 info->flags.set(display);
271 * Set the name and marks this stat to print at the end of simulation.
272 * @param name The new name.
273 * @return A reference to this stat.
276 name(const std::string &name)
278 Info *info = this->info();
280 info->flags.set(display);
283 const std::string &name() const { return this->info()->name; }
286 * Set the character(s) used between the name and vector number
287 * on vectors, dist, etc.
288 * @param _sep The new separator string
289 * @return A reference to this stat.
292 setSeparator(const std::string &_sep)
294 this->info()->setSeparator(_sep);
297 const std::string &setSeparator() const
299 return this->info()->separatorString;
303 * Set the description and marks this stat to print at the end of
305 * @param desc The new description.
306 * @return A reference to this stat.
309 desc(const std::string &_desc)
311 this->info()->desc = _desc;
316 * Set the precision and marks this stat to print at the end of simulation.
317 * @param _precision The new precision
318 * @return A reference to this stat.
321 precision(int _precision)
323 this->info()->precision = _precision;
328 * Set the flags and marks this stat to print at the end of simulation.
329 * @param f The new flags.
330 * @return A reference to this stat.
335 this->info()->flags.set(_flags);
340 * Set the prerequisite stat and marks this stat to print at the end of
342 * @param prereq The prerequisite stat.
343 * @return A reference to this stat.
345 template <class Stat>
347 prereq(const Stat &prereq)
349 this->info()->prereq = prereq.info();
354 template <class Derived, template <class> class InfoProxyType>
355 class DataWrapVec : public DataWrap<Derived, InfoProxyType>
358 typedef InfoProxyType<Derived> Info;
360 DataWrapVec(Group *parent = nullptr, const char *name = nullptr,
361 const char *desc = nullptr)
362 : DataWrap<Derived, InfoProxyType>(parent, name, desc)
365 // The following functions are specific to vectors. If you use them
366 // in a non vector context, you will get a nice compiler error!
369 * Set the subfield name for the given index, and marks this stat to print
370 * at the end of simulation.
371 * @param index The subfield index.
372 * @param name The new name of the subfield.
373 * @return A reference to this stat.
376 subname(off_type index, const std::string &name)
378 Derived &self = this->self();
379 Info *info = self.info();
381 std::vector<std::string> &subn = info->subnames;
382 if (subn.size() <= index)
383 subn.resize(index + 1);
388 // The following functions are specific to 2d vectors. If you use
389 // them in a non vector context, you will get a nice compiler
390 // error because info doesn't have the right variables.
393 * Set the subfield description for the given index and marks this stat to
394 * print at the end of simulation.
395 * @param index The subfield index.
396 * @param desc The new description of the subfield
397 * @return A reference to this stat.
400 subdesc(off_type index, const std::string &desc)
402 Info *info = this->info();
404 std::vector<std::string> &subd = info->subdescs;
405 if (subd.size() <= index)
406 subd.resize(index + 1);
415 Derived &self = this->self();
416 Info *info = this->info();
418 size_t size = self.size();
419 for (off_type i = 0; i < size; ++i)
420 self.data(i)->prepare(info);
426 Derived &self = this->self();
427 Info *info = this->info();
429 size_t size = self.size();
430 for (off_type i = 0; i < size; ++i)
431 self.data(i)->reset(info);
435 template <class Derived, template <class> class InfoProxyType>
436 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
439 typedef InfoProxyType<Derived> Info;
441 DataWrapVec2d(Group *parent, const char *name, const char *desc)
442 : DataWrapVec<Derived, InfoProxyType>(parent, name, desc)
447 * @warning This makes the assumption that if you're gonna subnames a 2d
448 * vector, you're subnaming across all y
451 ysubnames(const char **names)
453 Derived &self = this->self();
454 Info *info = this->info();
456 info->y_subnames.resize(self.y);
457 for (off_type i = 0; i < self.y; ++i)
458 info->y_subnames[i] = names[i];
463 ysubname(off_type index, const std::string &subname)
465 Derived &self = this->self();
466 Info *info = this->info();
468 assert(index < self.y);
469 info->y_subnames.resize(self.y);
470 info->y_subnames[index] = subname.c_str();
475 ysubname(off_type i) const
477 return this->info()->y_subnames[i];
482 //////////////////////////////////////////////////////////////////////
486 //////////////////////////////////////////////////////////////////////
489 * Templatized storage and interface for a simple scalar stat.
494 /** The statistic value. */
498 struct Params : public StorageParams {};
502 * Builds this storage element and calls the base constructor of the
510 * The the stat to the given value.
511 * @param val The new value.
513 void set(Counter val) { data = val; }
515 * Increment the stat by the given value.
516 * @param val The new value.
518 void inc(Counter val) { data += val; }
520 * Decrement the stat by the given value.
521 * @param val The new value.
523 void dec(Counter val) { data -= val; }
525 * Return the value of this stat as its base type.
526 * @return The value of this stat.
528 Counter value() const { return data; }
530 * Return the value of this stat as a result type.
531 * @return The value of this stat.
533 Result result() const { return (Result)data; }
535 * Prepare stat data for dumping or serialization
537 void prepare(Info *info) { }
539 * Reset stat value to default
541 void reset(Info *info) { data = Counter(); }
544 * @return true if zero value
546 bool zero() const { return data == Counter(); }
550 * Templatized storage and interface to a per-tick average stat. This keeps
551 * a current count and updates a total (count * ticks) when this count
552 * changes. This allows the quick calculation of a per tick count of the item
553 * being watched. This is good for keeping track of residencies in structures
554 * among other things.
559 /** The current count. */
561 /** The tick of the last reset */
563 /** The total count for all tick. */
564 mutable Result total;
565 /** The tick that current last changed. */
569 struct Params : public StorageParams {};
573 * Build and initializes this stat storage.
576 : current(0), lastReset(0), total(0), last(0)
580 * Set the current count to the one provided, update the total and last
582 * @param val The new count.
587 total += current * (curTick() - last);
593 * Increment the current count by the provided value, calls set.
594 * @param val The amount to increment.
596 void inc(Counter val) { set(current + val); }
599 * Deccrement the current count by the provided value, calls set.
600 * @param val The amount to decrement.
602 void dec(Counter val) { set(current - val); }
605 * Return the current count.
606 * @return The current count.
608 Counter value() const { return current; }
611 * Return the current average.
612 * @return The current average.
617 assert(last == curTick());
618 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
622 * @return true if zero value
624 bool zero() const { return total == 0.0; }
627 * Prepare stat data for dumping or serialization
632 total += current * (curTick() - last);
637 * Reset stat value to default
644 lastReset = curTick();
650 * Implementation of a scalar stat. The type of stat is determined by the
653 template <class Derived, class Stor>
654 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
657 typedef Stor Storage;
658 typedef typename Stor::Params Params;
661 /** The storage of this stat. */
662 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
666 * Retrieve the storage.
667 * @param index The vector index to access.
668 * @return The storage object at the given index.
673 return reinterpret_cast<Storage *>(storage);
677 * Retrieve a const pointer to the storage.
678 * for the given index.
679 * @param index The vector index to access.
680 * @return A const pointer to the storage object at the given index.
685 return reinterpret_cast<const Storage *>(storage);
691 new (storage) Storage(this->info());
697 * Return the current value of this stat as its base type.
698 * @return The current value.
700 Counter value() const { return data()->value(); }
703 ScalarBase(Group *parent = nullptr, const char *name = nullptr,
704 const char *desc = nullptr)
705 : DataWrap<Derived, ScalarInfoProxy>(parent, name, desc)
711 // Common operators for stats
713 * Increment the stat by 1. This calls the associated storage object inc
716 void operator++() { data()->inc(1); }
718 * Decrement the stat by 1. This calls the associated storage object dec
721 void operator--() { data()->dec(1); }
723 /** Increment the stat by 1. */
724 void operator++(int) { ++*this; }
725 /** Decrement the stat by 1. */
726 void operator--(int) { --*this; }
729 * Set the data value to the given value. This calls the associated storage
730 * object set function.
731 * @param v The new value.
733 template <typename U>
734 void operator=(const U &v) { data()->set(v); }
737 * Increment the stat by the given value. This calls the associated
738 * storage object inc function.
739 * @param v The value to add.
741 template <typename U>
742 void operator+=(const U &v) { data()->inc(v); }
745 * Decrement the stat by the given value. This calls the associated
746 * storage object dec function.
747 * @param v The value to substract.
749 template <typename U>
750 void operator-=(const U &v) { data()->dec(v); }
753 * Return the number of elements, always 1 for a scalar.
756 size_type size() const { return 1; }
758 Counter value() { return data()->value(); }
760 Result result() { return data()->result(); }
762 Result total() { return result(); }
764 bool zero() { return result() == 0.0; }
766 void reset() { data()->reset(this->info()); }
767 void prepare() { data()->prepare(this->info()); }
770 class ProxyInfo : public ScalarInfo
773 std::string str() const { return std::to_string(value()); }
774 size_type size() const { return 1; }
775 bool check() const { return true; }
778 bool zero() const { return value() == 0; }
780 void visit(Output &visitor) { visitor.visit(*this); }
784 class ValueProxy : public ProxyInfo
790 ValueProxy(T &val) : scalar(&val) {}
791 Counter value() const { return *scalar; }
792 Result result() const { return *scalar; }
793 Result total() const { return *scalar; }
797 class FunctorProxy : public ProxyInfo
803 FunctorProxy(T &func) : functor(&func) {}
804 Counter value() const { return (*functor)(); }
805 Result result() const { return (*functor)(); }
806 Result total() const { return (*functor)(); }
810 * A proxy similar to the FunctorProxy, but allows calling a method of a bound
811 * object, instead of a global free-standing function.
813 template <class T, class V>
814 class MethodProxy : public ProxyInfo
818 typedef V (T::*MethodPointer) () const;
819 MethodPointer method;
822 MethodProxy(T *obj, MethodPointer meth) : object(obj), method(meth) {}
823 Counter value() const { return (object->*method)(); }
824 Result result() const { return (object->*method)(); }
825 Result total() const { return (object->*method)(); }
828 template <class Derived>
829 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
835 ValueBase(Group *parent, const char *name, const char *desc)
836 : DataWrap<Derived, ScalarInfoProxy>(parent, name, desc),
841 ~ValueBase() { if (proxy) delete proxy; }
847 proxy = new ValueProxy<T>(value);
856 proxy = new FunctorProxy<T>(func);
862 * Extended functor that calls the specified method of the provided object.
864 * @param obj Pointer to the object whose method should be called.
865 * @param method Pointer of the function / method of the object.
866 * @return Updated stats item.
868 template <class T, class V>
870 method(T *obj, V (T::*method)() const)
872 proxy = new MethodProxy<T,V>(obj, method);
877 Counter value() { return proxy->value(); }
878 Result result() const { return proxy->result(); }
879 Result total() const { return proxy->total(); };
880 size_type size() const { return proxy->size(); }
882 std::string str() const { return proxy->str(); }
883 bool zero() const { return proxy->zero(); }
884 bool check() const { return proxy != NULL; }
889 //////////////////////////////////////////////////////////////////////
893 //////////////////////////////////////////////////////////////////////
896 * A proxy class to access the stat at a given index in a VectorBase stat.
897 * Behaves like a ScalarBase.
899 template <class Stat>
903 /** Pointer to the parent Vector. */
906 /** The index to access in the parent VectorBase. */
911 * Return the current value of this stat as its base type.
912 * @return The current value.
914 Counter value() const { return stat.data(index)->value(); }
917 * Return the current value of this statas a result type.
918 * @return The current value.
920 Result result() const { return stat.data(index)->result(); }
924 * Create and initialize this proxy, do not register it with the database.
925 * @param i The index to access.
927 ScalarProxy(Stat &s, off_type i)
933 * Create a copy of the provided ScalarProxy.
934 * @param sp The proxy to copy.
936 ScalarProxy(const ScalarProxy &sp)
937 : stat(sp.stat), index(sp.index)
941 * Set this proxy equal to the provided one.
942 * @param sp The proxy to copy.
943 * @return A reference to this proxy.
946 operator=(const ScalarProxy &sp)
954 // Common operators for stats
956 * Increment the stat by 1. This calls the associated storage object inc
959 void operator++() { stat.data(index)->inc(1); }
961 * Decrement the stat by 1. This calls the associated storage object dec
964 void operator--() { stat.data(index)->dec(1); }
966 /** Increment the stat by 1. */
967 void operator++(int) { ++*this; }
968 /** Decrement the stat by 1. */
969 void operator--(int) { --*this; }
972 * Set the data value to the given value. This calls the associated storage
973 * object set function.
974 * @param v The new value.
976 template <typename U>
978 operator=(const U &v)
980 stat.data(index)->set(v);
984 * Increment the stat by the given value. This calls the associated
985 * storage object inc function.
986 * @param v The value to add.
988 template <typename U>
990 operator+=(const U &v)
992 stat.data(index)->inc(v);
996 * Decrement the stat by the given value. This calls the associated
997 * storage object dec function.
998 * @param v The value to substract.
1000 template <typename U>
1002 operator-=(const U &v)
1004 stat.data(index)->dec(v);
1008 * Return the number of elements, always 1 for a scalar.
1011 size_type size() const { return 1; }
1017 return csprintf("%s[%d]", stat.info()->name, index);
1022 * Implementation of a vector of stats. The type of stat is determined by the
1023 * Storage class. @sa ScalarBase
1025 template <class Derived, class Stor>
1026 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
1029 typedef Stor Storage;
1030 typedef typename Stor::Params Params;
1033 typedef ScalarProxy<Derived> Proxy;
1034 friend class ScalarProxy<Derived>;
1035 friend class DataWrapVec<Derived, VectorInfoProxy>;
1038 /** The storage of this stat. */
1044 * Retrieve the storage.
1045 * @param index The vector index to access.
1046 * @return The storage object at the given index.
1048 Storage *data(off_type index) { return &storage[index]; }
1051 * Retrieve a const pointer to the storage.
1052 * @param index The vector index to access.
1053 * @return A const pointer to the storage object at the given index.
1055 const Storage *data(off_type index) const { return &storage[index]; }
1060 assert(s > 0 && "size must be positive!");
1061 assert(!storage && "already initialized");
1064 char *ptr = new char[_size * sizeof(Storage)];
1065 storage = reinterpret_cast<Storage *>(ptr);
1067 for (off_type i = 0; i < _size; ++i)
1068 new (&storage[i]) Storage(this->info());
1075 value(VCounter &vec) const
1078 for (off_type i = 0; i < size(); ++i)
1079 vec[i] = data(i)->value();
1083 * Copy the values to a local vector and return a reference to it.
1084 * @return A reference to a vector of the stat values.
1087 result(VResult &vec) const
1090 for (off_type i = 0; i < size(); ++i)
1091 vec[i] = data(i)->result();
1095 * Return a total of all entries in this vector.
1096 * @return The total of all vector entries.
1102 for (off_type i = 0; i < size(); ++i)
1103 total += data(i)->result();
1108 * @return the number of elements in this vector.
1110 size_type size() const { return _size; }
1115 for (off_type i = 0; i < size(); ++i)
1116 if (data(i)->zero())
1124 return storage != NULL;
1128 VectorBase(Group *parent, const char *name, const char *desc)
1129 : DataWrapVec<Derived, VectorInfoProxy>(parent, name, desc),
1130 storage(nullptr), _size(0)
1138 for (off_type i = 0; i < _size; ++i)
1139 data(i)->~Storage();
1140 delete [] reinterpret_cast<char *>(storage);
1144 * Set this vector to have the given size.
1145 * @param size The new size.
1146 * @return A reference to this stat.
1149 init(size_type size)
1151 Derived &self = this->self();
1157 * Return a reference (ScalarProxy) to the stat at the given index.
1158 * @param index The vector index to access.
1159 * @return A reference of the stat.
1162 operator[](off_type index)
1164 assert (index >= 0 && index < size());
1165 return Proxy(this->self(), index);
1169 template <class Stat>
1178 mutable VResult vec;
1180 typename Stat::Storage *
1181 data(off_type index)
1183 assert(index < len);
1184 return stat.data(offset + index);
1187 const typename Stat::Storage *
1188 data(off_type index) const
1190 assert(index < len);
1191 return stat.data(offset + index);
1200 for (off_type i = 0; i < size(); ++i)
1201 vec[i] = data(i)->result();
1210 for (off_type i = 0; i < size(); ++i)
1211 total += data(i)->result();
1216 VectorProxy(Stat &s, off_type o, size_type l)
1217 : stat(s), offset(o), len(l)
1221 VectorProxy(const VectorProxy &sp)
1222 : stat(sp.stat), offset(sp.offset), len(sp.len)
1227 operator=(const VectorProxy &sp)
1236 operator[](off_type index)
1238 assert (index >= 0 && index < size());
1239 return ScalarProxy<Stat>(stat, offset + index);
1242 size_type size() const { return len; }
1245 template <class Derived, class Stor>
1246 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1249 typedef Vector2dInfoProxy<Derived> Info;
1250 typedef Stor Storage;
1251 typedef typename Stor::Params Params;
1252 typedef VectorProxy<Derived> Proxy;
1253 friend class ScalarProxy<Derived>;
1254 friend class VectorProxy<Derived>;
1255 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1256 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1265 Storage *data(off_type index) { return &storage[index]; }
1266 const Storage *data(off_type index) const { return &storage[index]; }
1269 Vector2dBase(Group *parent, const char *name, const char *desc)
1270 : DataWrapVec2d<Derived, Vector2dInfoProxy>(parent, name, desc),
1271 x(0), y(0), _size(0), storage(nullptr)
1279 for (off_type i = 0; i < _size; ++i)
1280 data(i)->~Storage();
1281 delete [] reinterpret_cast<char *>(storage);
1285 init(size_type _x, size_type _y)
1287 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1288 assert(!storage && "already initialized");
1290 Derived &self = this->self();
1291 Info *info = this->info();
1299 char *ptr = new char[_size * sizeof(Storage)];
1300 storage = reinterpret_cast<Storage *>(ptr);
1302 for (off_type i = 0; i < _size; ++i)
1303 new (&storage[i]) Storage(info);
1311 operator[](off_type index)
1313 off_type offset = index * y;
1314 assert (index >= 0 && offset + y <= size());
1315 return Proxy(this->self(), offset, y);
1328 return data(0)->zero();
1332 * Return a total of all entries in this vector.
1333 * @return The total of all vector entries.
1339 for (off_type i = 0; i < size(); ++i)
1340 total += data(i)->result();
1347 Info *info = this->info();
1348 size_type size = this->size();
1350 for (off_type i = 0; i < size; ++i)
1351 data(i)->prepare(info);
1353 info->cvec.resize(size);
1354 for (off_type i = 0; i < size; ++i)
1355 info->cvec[i] = data(i)->value();
1359 * Reset stat value to default
1364 Info *info = this->info();
1365 size_type size = this->size();
1366 for (off_type i = 0; i < size; ++i)
1367 data(i)->reset(info);
1373 return storage != NULL;
1377 //////////////////////////////////////////////////////////////////////
1379 // Non formula statistics
1381 //////////////////////////////////////////////////////////////////////
1382 /** The parameters for a distribution stat. */
1383 struct DistParams : public StorageParams
1385 const DistType type;
1386 DistParams(DistType t) : type(t) {}
1390 * Templatized storage and interface for a distribution stat.
1395 /** The parameters for a distribution stat. */
1396 struct Params : public DistParams
1398 /** The minimum value to track. */
1400 /** The maximum value to track. */
1402 /** The number of entries in each bucket. */
1403 Counter bucket_size;
1404 /** The number of buckets. Equal to (max-min)/bucket_size. */
1407 Params() : DistParams(Dist), min(0), max(0), bucket_size(0),
1412 /** The minimum value to track. */
1414 /** The maximum value to track. */
1416 /** The number of entries in each bucket. */
1417 Counter bucket_size;
1419 /** The smallest value sampled. */
1421 /** The largest value sampled. */
1423 /** The number of values sampled less than min. */
1425 /** The number of values sampled more than max. */
1427 /** The current sum. */
1429 /** The sum of squares. */
1431 /** The number of samples. */
1433 /** Counter for each bucket. */
1437 DistStor(Info *info)
1438 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1444 * Add a value to the distribution for the given number of times.
1445 * @param val The value to add.
1446 * @param number The number of times to add the value.
1449 sample(Counter val, int number)
1451 if (val < min_track)
1452 underflow += number;
1453 else if (val > max_track)
1457 (size_type)std::floor((val - min_track) / bucket_size);
1458 assert(index < size());
1459 cvec[index] += number;
1468 sum += val * number;
1469 squares += val * val * number;
1474 * Return the number of buckets in this distribution.
1475 * @return the number of buckets.
1477 size_type size() const { return cvec.size(); }
1480 * Returns true if any calls to sample have been made.
1481 * @return True if any values have been sampled.
1486 return samples == Counter();
1490 prepare(Info *info, DistData &data)
1492 const Params *params = safe_cast<const Params *>(info->storageParams);
1494 assert(params->type == Dist);
1495 data.type = params->type;
1496 data.min = params->min;
1497 data.max = params->max;
1498 data.bucket_size = params->bucket_size;
1500 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1501 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1502 data.underflow = underflow;
1503 data.overflow = overflow;
1505 data.cvec.resize(params->buckets);
1506 for (off_type i = 0; i < params->buckets; ++i)
1507 data.cvec[i] = cvec[i];
1510 data.squares = squares;
1511 data.samples = samples;
1515 * Reset stat value to default
1520 const Params *params = safe_cast<const Params *>(info->storageParams);
1521 min_track = params->min;
1522 max_track = params->max;
1523 bucket_size = params->bucket_size;
1525 min_val = CounterLimits::max();
1526 max_val = CounterLimits::min();
1527 underflow = Counter();
1528 overflow = Counter();
1530 size_type size = cvec.size();
1531 for (off_type i = 0; i < size; ++i)
1532 cvec[i] = Counter();
1535 squares = Counter();
1536 samples = Counter();
1541 * Templatized storage and interface for a histogram stat.
1546 /** The parameters for a distribution stat. */
1547 struct Params : public DistParams
1549 /** The number of buckets.. */
1552 Params() : DistParams(Hist), buckets(0) {}
1556 /** The minimum value to track. */
1558 /** The maximum value to track. */
1560 /** The number of entries in each bucket. */
1561 Counter bucket_size;
1563 /** The current sum. */
1565 /** The sum of logarithm of each sample, used to compute geometric mean. */
1567 /** The sum of squares. */
1569 /** The number of samples. */
1571 /** Counter for each bucket. */
1575 HistStor(Info *info)
1576 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1583 void grow_convert();
1584 void add(HistStor *);
1587 * Add a value to the distribution for the given number of times.
1588 * @param val The value to add.
1589 * @param number The number of times to add the value.
1592 sample(Counter val, int number)
1594 assert(min_bucket < max_bucket);
1595 if (val < min_bucket) {
1596 if (min_bucket == 0)
1599 while (val < min_bucket)
1601 } else if (val >= max_bucket + bucket_size) {
1602 if (min_bucket == 0) {
1603 while (val >= max_bucket + bucket_size)
1606 while (val >= max_bucket + bucket_size)
1612 (int64_t)std::floor((val - min_bucket) / bucket_size);
1614 assert(index < size());
1615 cvec[index] += number;
1617 sum += val * number;
1618 squares += val * val * number;
1619 logs += log(val) * number;
1624 * Return the number of buckets in this distribution.
1625 * @return the number of buckets.
1627 size_type size() const { return cvec.size(); }
1630 * Returns true if any calls to sample have been made.
1631 * @return True if any values have been sampled.
1636 return samples == Counter();
1640 prepare(Info *info, DistData &data)
1642 const Params *params = safe_cast<const Params *>(info->storageParams);
1644 assert(params->type == Hist);
1645 data.type = params->type;
1646 data.min = min_bucket;
1647 data.max = max_bucket + bucket_size - 1;
1648 data.bucket_size = bucket_size;
1650 data.min_val = min_bucket;
1651 data.max_val = max_bucket;
1653 int buckets = params->buckets;
1654 data.cvec.resize(buckets);
1655 for (off_type i = 0; i < buckets; ++i)
1656 data.cvec[i] = cvec[i];
1660 data.squares = squares;
1661 data.samples = samples;
1665 * Reset stat value to default
1670 const Params *params = safe_cast<const Params *>(info->storageParams);
1672 max_bucket = params->buckets - 1;
1675 size_type size = cvec.size();
1676 for (off_type i = 0; i < size; ++i)
1677 cvec[i] = Counter();
1680 squares = Counter();
1681 samples = Counter();
1687 * Templatized storage and interface for a distribution that calculates mean
1693 struct Params : public DistParams
1695 Params() : DistParams(Deviation) {}
1699 /** The current sum. */
1701 /** The sum of squares. */
1703 /** The number of samples. */
1708 * Create and initialize this storage.
1710 SampleStor(Info *info)
1711 : sum(Counter()), squares(Counter()), samples(Counter())
1715 * Add a value the given number of times to this running average.
1716 * Update the running sum and sum of squares, increment the number of
1717 * values seen by the given number.
1718 * @param val The value to add.
1719 * @param number The number of times to add the value.
1722 sample(Counter val, int number)
1724 sum += val * number;
1725 squares += val * val * number;
1730 * Return the number of entries in this stat, 1
1733 size_type size() const { return 1; }
1736 * Return true if no samples have been added.
1737 * @return True if no samples have been added.
1739 bool zero() const { return samples == Counter(); }
1742 prepare(Info *info, DistData &data)
1744 const Params *params = safe_cast<const Params *>(info->storageParams);
1746 assert(params->type == Deviation);
1747 data.type = params->type;
1749 data.squares = squares;
1750 data.samples = samples;
1754 * Reset stat value to default
1760 squares = Counter();
1761 samples = Counter();
1766 * Templatized storage for distribution that calculates per tick mean and
1772 struct Params : public DistParams
1774 Params() : DistParams(Deviation) {}
1778 /** Current total. */
1780 /** Current sum of squares. */
1785 * Create and initialize this storage.
1787 AvgSampleStor(Info *info)
1788 : sum(Counter()), squares(Counter())
1792 * Add a value to the distribution for the given number of times.
1793 * Update the running sum and sum of squares.
1794 * @param val The value to add.
1795 * @param number The number of times to add the value.
1798 sample(Counter val, int number)
1800 sum += val * number;
1801 squares += val * val * number;
1805 * Return the number of entries, in this case 1.
1808 size_type size() const { return 1; }
1811 * Return true if no samples have been added.
1812 * @return True if the sum is zero.
1814 bool zero() const { return sum == Counter(); }
1817 prepare(Info *info, DistData &data)
1819 const Params *params = safe_cast<const Params *>(info->storageParams);
1821 assert(params->type == Deviation);
1822 data.type = params->type;
1824 data.squares = squares;
1825 data.samples = curTick();
1829 * Reset stat value to default
1835 squares = Counter();
1840 * Implementation of a distribution stat. The type of distribution is
1841 * determined by the Storage template. @sa ScalarBase
1843 template <class Derived, class Stor>
1844 class DistBase : public DataWrap<Derived, DistInfoProxy>
1847 typedef DistInfoProxy<Derived> Info;
1848 typedef Stor Storage;
1849 typedef typename Stor::Params Params;
1852 /** The storage for this stat. */
1853 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1857 * Retrieve the storage.
1858 * @return The storage object for this stat.
1863 return reinterpret_cast<Storage *>(storage);
1867 * Retrieve a const pointer to the storage.
1868 * @return A const pointer to the storage object for this stat.
1873 return reinterpret_cast<const Storage *>(storage);
1879 new (storage) Storage(this->info());
1884 DistBase(Group *parent, const char *name, const char *desc)
1885 : DataWrap<Derived, DistInfoProxy>(parent, name, desc)
1890 * Add a value to the distribtion n times. Calls sample on the storage
1892 * @param v The value to add.
1893 * @param n The number of times to add it, defaults to 1.
1895 template <typename U>
1896 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1899 * Return the number of entries in this stat.
1900 * @return The number of entries.
1902 size_type size() const { return data()->size(); }
1904 * Return true if no samples have been added.
1905 * @return True if there haven't been any samples.
1907 bool zero() const { return data()->zero(); }
1912 Info *info = this->info();
1913 data()->prepare(info, info->data);
1917 * Reset stat value to default
1922 data()->reset(this->info());
1926 * Add the argument distribution to the this distribution.
1928 void add(DistBase &d) { data()->add(d.data()); }
1932 template <class Stat>
1935 template <class Derived, class Stor>
1936 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1939 typedef VectorDistInfoProxy<Derived> Info;
1940 typedef Stor Storage;
1941 typedef typename Stor::Params Params;
1942 typedef DistProxy<Derived> Proxy;
1943 friend class DistProxy<Derived>;
1944 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1952 data(off_type index)
1954 return &storage[index];
1958 data(off_type index) const
1960 return &storage[index];
1966 assert(s > 0 && "size must be positive!");
1967 assert(!storage && "already initialized");
1970 char *ptr = new char[_size * sizeof(Storage)];
1971 storage = reinterpret_cast<Storage *>(ptr);
1973 Info *info = this->info();
1974 for (off_type i = 0; i < _size; ++i)
1975 new (&storage[i]) Storage(info);
1981 VectorDistBase(Group *parent, const char *name, const char *desc)
1982 : DataWrapVec<Derived, VectorDistInfoProxy>(parent, name, desc),
1991 for (off_type i = 0; i < _size; ++i)
1992 data(i)->~Storage();
1993 delete [] reinterpret_cast<char *>(storage);
1996 Proxy operator[](off_type index)
1998 assert(index >= 0 && index < size());
1999 return Proxy(this->self(), index);
2011 for (off_type i = 0; i < size(); ++i)
2012 if (!data(i)->zero())
2020 Info *info = this->info();
2021 size_type size = this->size();
2022 info->data.resize(size);
2023 for (off_type i = 0; i < size; ++i)
2024 data(i)->prepare(info, info->data[i]);
2030 return storage != NULL;
2034 template <class Stat>
2042 typename Stat::Storage *data() { return stat.data(index); }
2043 const typename Stat::Storage *data() const { return stat.data(index); }
2046 DistProxy(Stat &s, off_type i)
2050 DistProxy(const DistProxy &sp)
2051 : stat(sp.stat), index(sp.index)
2055 operator=(const DistProxy &sp)
2063 template <typename U>
2065 sample(const U &v, int n = 1)
2067 data()->sample(v, n);
2079 return data()->zero();
2083 * Proxy has no state. Nothing to reset.
2088 //////////////////////////////////////////////////////////////////////
2092 //////////////////////////////////////////////////////////////////////
2095 * Base class for formula statistic node. These nodes are used to build a tree
2096 * that represents the formula.
2102 * Return the number of nodes in the subtree starting at this node.
2103 * @return the number of nodes in this subtree.
2105 virtual size_type size() const = 0;
2107 * Return the result vector of this subtree.
2108 * @return The result vector of this subtree.
2110 virtual const VResult &result() const = 0;
2112 * Return the total of the result vector.
2113 * @return The total of the result vector.
2115 virtual Result total() const = 0;
2120 virtual std::string str() const = 0;
2125 /** Shared pointer to a function Node. */
2126 typedef std::shared_ptr<Node> NodePtr;
2128 class ScalarStatNode : public Node
2131 const ScalarInfo *data;
2132 mutable VResult vresult;
2135 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2140 vresult[0] = data->result();
2144 Result total() const { return data->result(); };
2146 size_type size() const { return 1; }
2151 std::string str() const { return data->name; }
2154 template <class Stat>
2155 class ScalarProxyNode : public Node
2158 const ScalarProxy<Stat> proxy;
2159 mutable VResult vresult;
2162 ScalarProxyNode(const ScalarProxy<Stat> &p)
2163 : proxy(p), vresult(1)
2169 vresult[0] = proxy.result();
2176 return proxy.result();
2195 class VectorStatNode : public Node
2198 const VectorInfo *data;
2201 VectorStatNode(const VectorInfo *d) : data(d) { }
2202 const VResult &result() const { return data->result(); }
2203 Result total() const { return data->total(); };
2205 size_type size() const { return data->size(); }
2207 std::string str() const { return data->name; }
2211 class ConstNode : public Node
2217 ConstNode(T s) : vresult(1, (Result)s) {}
2218 const VResult &result() const { return vresult; }
2219 Result total() const { return vresult[0]; };
2220 size_type size() const { return 1; }
2221 std::string str() const { return std::to_string(vresult[0]); }
2225 class ConstVectorNode : public Node
2231 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2232 const VResult &result() const { return vresult; }
2237 size_type size = this->size();
2239 for (off_type i = 0; i < size; i++)
2244 size_type size() const { return vresult.size(); }
2248 size_type size = this->size();
2249 std::string tmp = "(";
2250 for (off_type i = 0; i < size; i++)
2251 tmp += csprintf("%s ", std::to_string(vresult[i]));
2261 struct OpString<std::plus<Result> >
2263 static std::string str() { return "+"; }
2267 struct OpString<std::minus<Result> >
2269 static std::string str() { return "-"; }
2273 struct OpString<std::multiplies<Result> >
2275 static std::string str() { return "*"; }
2279 struct OpString<std::divides<Result> >
2281 static std::string str() { return "/"; }
2285 struct OpString<std::modulus<Result> >
2287 static std::string str() { return "%"; }
2291 struct OpString<std::negate<Result> >
2293 static std::string str() { return "-"; }
2297 class UnaryNode : public Node
2301 mutable VResult vresult;
2304 UnaryNode(NodePtr &p) : l(p) {}
2309 const VResult &lvec = l->result();
2310 size_type size = lvec.size();
2314 vresult.resize(size);
2316 for (off_type i = 0; i < size; ++i)
2317 vresult[i] = op(lvec[i]);
2325 const VResult &vec = this->result();
2327 for (off_type i = 0; i < size(); i++)
2332 size_type size() const { return l->size(); }
2337 return OpString<Op>::str() + l->str();
2342 class BinaryNode : public Node
2347 mutable VResult vresult;
2350 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2353 result() const override
2356 const VResult &lvec = l->result();
2357 const VResult &rvec = r->result();
2359 assert(lvec.size() > 0 && rvec.size() > 0);
2361 if (lvec.size() == 1 && rvec.size() == 1) {
2363 vresult[0] = op(lvec[0], rvec[0]);
2364 } else if (lvec.size() == 1) {
2365 size_type size = rvec.size();
2366 vresult.resize(size);
2367 for (off_type i = 0; i < size; ++i)
2368 vresult[i] = op(lvec[0], rvec[i]);
2369 } else if (rvec.size() == 1) {
2370 size_type size = lvec.size();
2371 vresult.resize(size);
2372 for (off_type i = 0; i < size; ++i)
2373 vresult[i] = op(lvec[i], rvec[0]);
2374 } else if (rvec.size() == lvec.size()) {
2375 size_type size = rvec.size();
2376 vresult.resize(size);
2377 for (off_type i = 0; i < size; ++i)
2378 vresult[i] = op(lvec[i], rvec[i]);
2385 total() const override
2387 const VResult &vec = this->result();
2388 const VResult &lvec = l->result();
2389 const VResult &rvec = r->result();
2395 assert(lvec.size() > 0 && rvec.size() > 0);
2396 assert(lvec.size() == rvec.size() ||
2397 lvec.size() == 1 || rvec.size() == 1);
2399 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2400 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2401 for (off_type i = 0; i < size(); ++i) {
2405 return op(lsum, rsum);
2408 /** Otherwise divide each item by the divisor */
2409 for (off_type i = 0; i < size(); ++i) {
2417 size() const override
2419 size_type ls = l->size();
2420 size_type rs = r->size();
2423 } else if (rs == 1) {
2426 assert(ls == rs && "Node vector sizes are not equal");
2432 str() const override
2434 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2439 class SumNode : public Node
2443 mutable VResult vresult;
2446 SumNode(NodePtr &p) : l(p), vresult(1) {}
2451 const VResult &lvec = l->result();
2452 size_type size = lvec.size();
2458 for (off_type i = 0; i < size; ++i)
2459 vresult[0] = op(vresult[0], lvec[i]);
2467 const VResult &lvec = l->result();
2468 size_type size = lvec.size();
2471 Result result = 0.0;
2474 for (off_type i = 0; i < size; ++i)
2475 result = op(result, lvec[i]);
2480 size_type size() const { return 1; }
2485 return csprintf("total(%s)", l->str());
2490 //////////////////////////////////////////////////////////////////////
2492 // Visible Statistics Types
2494 //////////////////////////////////////////////////////////////////////
2496 * @defgroup VisibleStats "Statistic Types"
2497 * These are the statistics that are used in the simulator.
2502 * This is a simple scalar statistic, like a counter.
2503 * @sa Stat, ScalarBase, StatStor
2505 class Scalar : public ScalarBase<Scalar, StatStor>
2508 using ScalarBase<Scalar, StatStor>::operator=;
2510 Scalar(Group *parent = nullptr, const char *name = nullptr,
2511 const char *desc = nullptr)
2512 : ScalarBase<Scalar, StatStor>(parent, name, desc)
2518 * A stat that calculates the per tick average of a value.
2519 * @sa Stat, ScalarBase, AvgStor
2521 class Average : public ScalarBase<Average, AvgStor>
2524 using ScalarBase<Average, AvgStor>::operator=;
2526 Average(Group *parent = nullptr, const char *name = nullptr,
2527 const char *desc = nullptr)
2528 : ScalarBase<Average, AvgStor>(parent, name, desc)
2533 class Value : public ValueBase<Value>
2536 Value(Group *parent = nullptr, const char *name = nullptr,
2537 const char *desc = nullptr)
2538 : ValueBase<Value>(parent, name, desc)
2544 * A vector of scalar stats.
2545 * @sa Stat, VectorBase, StatStor
2547 class Vector : public VectorBase<Vector, StatStor>
2550 Vector(Group *parent = nullptr, const char *name = nullptr,
2551 const char *desc = nullptr)
2552 : VectorBase<Vector, StatStor>(parent, name, desc)
2558 * A vector of Average stats.
2559 * @sa Stat, VectorBase, AvgStor
2561 class AverageVector : public VectorBase<AverageVector, AvgStor>
2564 AverageVector(Group *parent = nullptr, const char *name = nullptr,
2565 const char *desc = nullptr)
2566 : VectorBase<AverageVector, AvgStor>(parent, name, desc)
2572 * A 2-Dimensional vecto of scalar stats.
2573 * @sa Stat, Vector2dBase, StatStor
2575 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2578 Vector2d(Group *parent = nullptr, const char *name = nullptr,
2579 const char *desc = nullptr)
2580 : Vector2dBase<Vector2d, StatStor>(parent, name, desc)
2586 * A simple distribution stat.
2587 * @sa Stat, DistBase, DistStor
2589 class Distribution : public DistBase<Distribution, DistStor>
2592 Distribution(Group *parent = nullptr, const char *name = nullptr,
2593 const char *desc = nullptr)
2594 : DistBase<Distribution, DistStor>(parent, name, desc)
2599 * Set the parameters of this distribution. @sa DistStor::Params
2600 * @param min The minimum value of the distribution.
2601 * @param max The maximum value of the distribution.
2602 * @param bkt The number of values in each bucket.
2603 * @return A reference to this distribution.
2606 init(Counter min, Counter max, Counter bkt)
2608 DistStor::Params *params = new DistStor::Params;
2611 params->bucket_size = bkt;
2612 // Division by zero is especially serious in an Aarch64 host,
2613 // where it gets rounded to allocate 32GiB RAM.
2615 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2616 this->setParams(params);
2618 return this->self();
2623 * A simple histogram stat.
2624 * @sa Stat, DistBase, HistStor
2626 class Histogram : public DistBase<Histogram, HistStor>
2629 Histogram(Group *parent = nullptr, const char *name = nullptr,
2630 const char *desc = nullptr)
2631 : DistBase<Histogram, HistStor>(parent, name, desc)
2636 * Set the parameters of this histogram. @sa HistStor::Params
2637 * @param size The number of buckets in the histogram
2638 * @return A reference to this histogram.
2641 init(size_type size)
2643 HistStor::Params *params = new HistStor::Params;
2644 params->buckets = size;
2645 this->setParams(params);
2647 return this->self();
2652 * Calculates the mean and variance of all the samples.
2653 * @sa DistBase, SampleStor
2655 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2659 * Construct and initialize this distribution.
2661 StandardDeviation(Group *parent = nullptr, const char *name = nullptr,
2662 const char *desc = nullptr)
2663 : DistBase<StandardDeviation, SampleStor>(parent, name, desc)
2665 SampleStor::Params *params = new SampleStor::Params;
2667 this->setParams(params);
2672 * Calculates the per tick mean and variance of the samples.
2673 * @sa DistBase, AvgSampleStor
2675 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2679 * Construct and initialize this distribution.
2681 AverageDeviation(Group *parent = nullptr, const char *name = nullptr,
2682 const char *desc = nullptr)
2683 : DistBase<AverageDeviation, AvgSampleStor>(parent, name, desc)
2685 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2687 this->setParams(params);
2692 * A vector of distributions.
2693 * @sa VectorDistBase, DistStor
2695 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2698 VectorDistribution(Group *parent = nullptr, const char *name = nullptr,
2699 const char *desc = nullptr)
2700 : VectorDistBase<VectorDistribution, DistStor>(parent, name, desc)
2705 * Initialize storage and parameters for this distribution.
2706 * @param size The size of the vector (the number of distributions).
2707 * @param min The minimum value of the distribution.
2708 * @param max The maximum value of the distribution.
2709 * @param bkt The number of values in each bucket.
2710 * @return A reference to this distribution.
2712 VectorDistribution &
2713 init(size_type size, Counter min, Counter max, Counter bkt)
2715 DistStor::Params *params = new DistStor::Params;
2718 params->bucket_size = bkt;
2719 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2720 this->setParams(params);
2722 return this->self();
2727 * This is a vector of StandardDeviation stats.
2728 * @sa VectorDistBase, SampleStor
2730 class VectorStandardDeviation
2731 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2734 VectorStandardDeviation(Group *parent = nullptr, const char *name = nullptr,
2735 const char *desc = nullptr)
2736 : VectorDistBase<VectorStandardDeviation, SampleStor>(parent, name,
2742 * Initialize storage for this distribution.
2743 * @param size The size of the vector.
2744 * @return A reference to this distribution.
2746 VectorStandardDeviation &
2747 init(size_type size)
2749 SampleStor::Params *params = new SampleStor::Params;
2751 this->setParams(params);
2752 return this->self();
2757 * This is a vector of AverageDeviation stats.
2758 * @sa VectorDistBase, AvgSampleStor
2760 class VectorAverageDeviation
2761 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2764 VectorAverageDeviation(Group *parent = nullptr, const char *name = nullptr,
2765 const char *desc = nullptr)
2766 : VectorDistBase<VectorAverageDeviation, AvgSampleStor>(parent, name,
2772 * Initialize storage for this distribution.
2773 * @param size The size of the vector.
2774 * @return A reference to this distribution.
2776 VectorAverageDeviation &
2777 init(size_type size)
2779 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2781 this->setParams(params);
2782 return this->self();
2786 template <class Stat>
2787 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2790 mutable VResult vec;
2791 mutable VCounter cvec;
2794 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2796 size_type size() const { return this->s.size(); }
2801 this->s.result(vec);
2804 Result total() const { return this->s.total(); }
2805 VCounter &value() const { return cvec; }
2807 std::string str() const { return this->s.str(); }
2810 template <class Stat>
2811 class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2814 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2818 * Implementation of a sparse histogram stat. The storage class is
2819 * determined by the Storage template.
2821 template <class Derived, class Stor>
2822 class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2825 typedef SparseHistInfoProxy<Derived> Info;
2826 typedef Stor Storage;
2827 typedef typename Stor::Params Params;
2830 /** The storage for this stat. */
2831 char storage[sizeof(Storage)];
2835 * Retrieve the storage.
2836 * @return The storage object for this stat.
2841 return reinterpret_cast<Storage *>(storage);
2845 * Retrieve a const pointer to the storage.
2846 * @return A const pointer to the storage object for this stat.
2851 return reinterpret_cast<const Storage *>(storage);
2857 new (storage) Storage(this->info());
2862 SparseHistBase(Group *parent, const char *name, const char *desc)
2863 : DataWrap<Derived, SparseHistInfoProxy>(parent, name, desc)
2868 * Add a value to the distribtion n times. Calls sample on the storage
2870 * @param v The value to add.
2871 * @param n The number of times to add it, defaults to 1.
2873 template <typename U>
2874 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2877 * Return the number of entries in this stat.
2878 * @return The number of entries.
2880 size_type size() const { return data()->size(); }
2882 * Return true if no samples have been added.
2883 * @return True if there haven't been any samples.
2885 bool zero() const { return data()->zero(); }
2890 Info *info = this->info();
2891 data()->prepare(info, info->data);
2895 * Reset stat value to default
2900 data()->reset(this->info());
2905 * Templatized storage and interface for a sparse histogram stat.
2907 class SparseHistStor
2910 /** The parameters for a sparse histogram stat. */
2911 struct Params : public DistParams
2913 Params() : DistParams(Hist) {}
2917 /** Counter for number of samples */
2919 /** Counter for each bucket. */
2923 SparseHistStor(Info *info)
2929 * Add a value to the distribution for the given number of times.
2930 * @param val The value to add.
2931 * @param number The number of times to add the value.
2934 sample(Counter val, int number)
2936 cmap[val] += number;
2941 * Return the number of buckets in this distribution.
2942 * @return the number of buckets.
2944 size_type size() const { return cmap.size(); }
2947 * Returns true if any calls to sample have been made.
2948 * @return True if any values have been sampled.
2953 return samples == Counter();
2957 prepare(Info *info, SparseHistData &data)
2959 MCounter::iterator it;
2961 for (it = cmap.begin(); it != cmap.end(); it++) {
2962 data.cmap[(*it).first] = (*it).second;
2965 data.samples = samples;
2969 * Reset stat value to default
2979 class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2982 SparseHistogram(Group *parent = nullptr, const char *name = nullptr,
2983 const char *desc = nullptr)
2984 : SparseHistBase<SparseHistogram, SparseHistStor>(parent, name, desc)
2989 * Set the parameters of this histogram. @sa HistStor::Params
2990 * @param size The number of buckets in the histogram
2991 * @return A reference to this histogram.
2994 init(size_type size)
2996 SparseHistStor::Params *params = new SparseHistStor::Params;
2997 this->setParams(params);
2999 return this->self();
3005 * A formula for statistics that is calculated when printed. A formula is
3006 * stored as a tree of Nodes that represent the equation to calculate.
3007 * @sa Stat, ScalarStat, VectorStat, Node, Temp
3009 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
3012 /** The root of the tree which represents the Formula */
3018 * Create and initialize thie formula, and register it with the database.
3020 Formula(Group *parent = nullptr, const char *name = nullptr,
3021 const char *desc = nullptr);
3023 Formula(Group *parent, const char *name, const char *desc,
3027 * Set an unitialized Formula to the given root.
3028 * @param r The root of the expression tree.
3029 * @return a reference to this formula.
3031 const Formula &operator=(const Temp &r);
3033 template<typename T>
3034 const Formula &operator=(const T &v)
3041 * Add the given tree to the existing one.
3042 * @param r The root of the expression tree.
3043 * @return a reference to this formula.
3045 const Formula &operator+=(Temp r);
3048 * Divide the existing tree by the given one.
3049 * @param r The root of the expression tree.
3050 * @return a reference to this formula.
3052 const Formula &operator/=(Temp r);
3055 * Return the result of the Fomula in a vector. If there were no Vector
3056 * components to the Formula, then the vector is size 1. If there were,
3057 * like x/y with x being a vector of size 3, then the result returned will
3058 * be x[0]/y, x[1]/y, x[2]/y, respectively.
3059 * @return The result vector.
3061 void result(VResult &vec) const;
3064 * Return the total Formula result. If there is a Vector
3065 * component to this Formula, then this is the result of the
3066 * Formula if the formula is applied after summing all the
3067 * components of the Vector. For example, if Formula is x/y where
3068 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
3069 * there is no Vector component, total() returns the same value as
3070 * the first entry in the VResult val() returns.
3071 * @return The total of the result vector.
3073 Result total() const;
3076 * Return the number of elements in the tree.
3078 size_type size() const;
3083 * Formulas don't need to be reset
3092 std::string str() const;
3095 class FormulaNode : public Node
3098 const Formula &formula;
3099 mutable VResult vec;
3102 FormulaNode(const Formula &f) : formula(f) {}
3104 size_type size() const { return formula.size(); }
3105 const VResult &result() const { formula.result(vec); return vec; }
3106 Result total() const { return formula.total(); }
3108 std::string str() const { return formula.str(); }
3112 * Helper class to construct formula node trees.
3118 * Pointer to a Node object.
3124 * Copy the given pointer to this class.
3125 * @param n A pointer to a Node object to copy.
3127 Temp(const NodePtr &n) : node(n) { }
3129 Temp(NodePtr &&n) : node(std::move(n)) { }
3132 * Return the node pointer.
3133 * @return the node pointer.
3135 operator NodePtr&() { return node; }
3138 * Makde gcc < 4.6.3 happy and explicitly get the underlying node.
3140 NodePtr getNodePtr() const { return node; }
3144 * Create a new ScalarStatNode.
3145 * @param s The ScalarStat to place in a node.
3147 Temp(const Scalar &s)
3148 : node(new ScalarStatNode(s.info()))
3152 * Create a new ScalarStatNode.
3153 * @param s The ScalarStat to place in a node.
3155 Temp(const Value &s)
3156 : node(new ScalarStatNode(s.info()))
3160 * Create a new ScalarStatNode.
3161 * @param s The ScalarStat to place in a node.
3163 Temp(const Average &s)
3164 : node(new ScalarStatNode(s.info()))
3168 * Create a new VectorStatNode.
3169 * @param s The VectorStat to place in a node.
3171 Temp(const Vector &s)
3172 : node(new VectorStatNode(s.info()))
3175 Temp(const AverageVector &s)
3176 : node(new VectorStatNode(s.info()))
3182 Temp(const Formula &f)
3183 : node(new FormulaNode(f))
3187 * Create a new ScalarProxyNode.
3188 * @param p The ScalarProxy to place in a node.
3190 template <class Stat>
3191 Temp(const ScalarProxy<Stat> &p)
3192 : node(new ScalarProxyNode<Stat>(p))
3196 * Create a ConstNode
3197 * @param value The value of the const node.
3199 Temp(signed char value)
3200 : node(new ConstNode<signed char>(value))
3204 * Create a ConstNode
3205 * @param value The value of the const node.
3207 Temp(unsigned char value)
3208 : node(new ConstNode<unsigned char>(value))
3212 * Create a ConstNode
3213 * @param value The value of the const node.
3215 Temp(signed short value)
3216 : node(new ConstNode<signed short>(value))
3220 * Create a ConstNode
3221 * @param value The value of the const node.
3223 Temp(unsigned short value)
3224 : node(new ConstNode<unsigned short>(value))
3228 * Create a ConstNode
3229 * @param value The value of the const node.
3231 Temp(signed int value)
3232 : node(new ConstNode<signed int>(value))
3236 * Create a ConstNode
3237 * @param value The value of the const node.
3239 Temp(unsigned int value)
3240 : node(new ConstNode<unsigned int>(value))
3244 * Create a ConstNode
3245 * @param value The value of the const node.
3247 Temp(signed long value)
3248 : node(new ConstNode<signed long>(value))
3252 * Create a ConstNode
3253 * @param value The value of the const node.
3255 Temp(unsigned long value)
3256 : node(new ConstNode<unsigned long>(value))
3260 * Create a ConstNode
3261 * @param value The value of the const node.
3263 Temp(signed long long value)
3264 : node(new ConstNode<signed long long>(value))
3268 * Create a ConstNode
3269 * @param value The value of the const node.
3271 Temp(unsigned long long value)
3272 : node(new ConstNode<unsigned long long>(value))
3276 * Create a ConstNode
3277 * @param value The value of the const node.
3280 : node(new ConstNode<float>(value))
3284 * Create a ConstNode
3285 * @param value The value of the const node.
3288 : node(new ConstNode<double>(value))
3298 operator+(Temp l, Temp r)
3300 return Temp(std::make_shared<BinaryNode<std::plus<Result> > >(l, r));
3304 operator-(Temp l, Temp r)
3306 return Temp(std::make_shared<BinaryNode<std::minus<Result> > >(l, r));
3310 operator*(Temp l, Temp r)
3312 return Temp(std::make_shared<BinaryNode<std::multiplies<Result> > >(l, r));
3316 operator/(Temp l, Temp r)
3318 return Temp(std::make_shared<BinaryNode<std::divides<Result> > >(l, r));
3324 return Temp(std::make_shared<UnaryNode<std::negate<Result> > >(l));
3327 template <typename T>
3331 return Temp(std::make_shared<ConstNode<T> >(val));
3334 template <typename T>
3336 constantVector(T val)
3338 return Temp(std::make_shared<ConstVectorNode<T> >(val));
3344 return Temp(std::make_shared<SumNode<std::plus<Result> > >(val));
3347 /** Dump all statistics data to the registered outputs */
3354 * Register reset and dump handlers. These are the functions which
3355 * will actually perform the whole statistics reset/dump actions
3356 * including processing the reset/dump callbacks
3358 typedef void (*Handler)();
3360 void registerHandlers(Handler reset_handler, Handler dump_handler);
3363 * Register a callback that should be called whenever statistics are
3366 void registerResetCallback(Callback *cb);
3369 * Register a callback that should be called whenever statistics are
3370 * about to be dumped
3372 void registerDumpCallback(Callback *cb);
3375 * Process all the callbacks in the reset callbacks queue
3377 void processResetQueue();
3380 * Process all the callbacks in the dump callbacks queue
3382 void processDumpQueue();
3384 std::list<Info *> &statsList();
3386 typedef std::map<const void *, Info *> MapType;
3387 MapType &statsMap();
3389 typedef std::map<std::string, Info *> NameMapType;
3390 NameMapType &nameMap();
3392 bool validateStatName(const std::string &name);
3394 } // namespace Stats
3396 void debugDumpStats();
3398 #endif // __BASE_STATISTICS_HH__