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.
41 * Authors: Nathan Binkert
46 * Declaration of Statistics objects.
52 * Generalized N-dimensinal vector
56 * -- these both can use the same function that prints out a
57 * specific set of stats
58 * VectorStandardDeviation totals
61 #ifndef __BASE_STATISTICS_HH__
62 #define __BASE_STATISTICS_HH__
78 #include "base/stats/group.hh"
79 #include "base/stats/info.hh"
80 #include "base/stats/output.hh"
81 #include "base/stats/types.hh"
82 #include "base/cast.hh"
83 #include "base/cprintf.hh"
84 #include "base/intmath.hh"
85 #include "base/str.hh"
86 #include "base/types.hh"
90 /** The current simulated tick. */
91 extern Tick curTick();
93 /* A namespace for all of the Statistics */
96 template <class Stat, class Base>
97 class InfoProxy : public Base
103 InfoProxy(Stat &stat) : s(stat) {}
105 bool check() const { return s.check(); }
106 void prepare() { s.prepare(); }
107 void reset() { s.reset(); }
109 visit(Output &visitor)
111 visitor.visit(*static_cast<Base *>(this));
113 bool zero() const { return s.zero(); }
116 template <class Stat>
117 class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
120 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
122 Counter value() const { return this->s.value(); }
123 Result result() const { return this->s.result(); }
124 Result total() const { return this->s.total(); }
127 template <class Stat>
128 class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
131 mutable VCounter cvec;
132 mutable VResult rvec;
135 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
137 size_type size() const { return this->s.size(); }
149 this->s.result(rvec);
153 Result total() const { return this->s.total(); }
156 template <class Stat>
157 class DistInfoProxy : public InfoProxy<Stat, DistInfo>
160 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
163 template <class Stat>
164 class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
167 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
169 size_type size() const { return this->s.size(); }
172 template <class Stat>
173 class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
176 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
178 Result total() const { return this->s.total(); }
183 virtual ~StorageParams();
192 /** Set up an info class for this statistic */
193 void setInfo(Group *parent, Info *info);
194 /** Save Storage class parameters if any */
195 void setParams(const StorageParams *params);
196 /** Save Storage class parameters if any */
199 /** Grab the information class for this statistic */
201 /** Grab the information class for this statistic */
202 const Info *info() const;
209 * Reset the stat to the default state.
214 * @return true if this stat has a value and satisfies its
215 * requirement as a prereq
217 bool zero() const { return true; }
220 * Check that this stat has been set up properly and is ready for
222 * @return true for success
224 bool check() const { return true; }
227 template <class Derived, template <class> class InfoProxyType>
228 class DataWrap : public InfoAccess
231 typedef InfoProxyType<Derived> Info;
234 Derived &self() { return *static_cast<Derived *>(this); }
240 return safe_cast<Info *>(InfoAccess::info());
247 return safe_cast<const Info *>(InfoAccess::info());
252 DataWrap(const DataWrap &) = delete;
253 DataWrap &operator=(const DataWrap &) = delete;
256 DataWrap(Group *parent, const char *name, const char *desc)
258 auto info = new Info(self());
259 this->setInfo(parent, info);
262 parent->addStat(info);
265 info->setName(parent, name);
266 info->flags.set(display);
274 * Set the name and marks this stat to print at the end of simulation.
275 * @param name The new name.
276 * @return A reference to this stat.
279 name(const std::string &name)
281 Info *info = this->info();
283 info->flags.set(display);
286 const std::string &name() const { return this->info()->name; }
289 * Set the character(s) used between the name and vector number
290 * on vectors, dist, etc.
291 * @param _sep The new separator string
292 * @return A reference to this stat.
295 setSeparator(const std::string &_sep)
297 this->info()->setSeparator(_sep);
300 const std::string &setSeparator() const
302 return this->info()->separatorString;
306 * Set the description and marks this stat to print at the end of
308 * @param desc The new description.
309 * @return A reference to this stat.
312 desc(const std::string &_desc)
314 this->info()->desc = _desc;
319 * Set the precision and marks this stat to print at the end of simulation.
320 * @param _precision The new precision
321 * @return A reference to this stat.
324 precision(int _precision)
326 this->info()->precision = _precision;
331 * Set the flags and marks this stat to print at the end of simulation.
332 * @param f The new flags.
333 * @return A reference to this stat.
338 this->info()->flags.set(_flags);
343 * Set the prerequisite stat and marks this stat to print at the end of
345 * @param prereq The prerequisite stat.
346 * @return A reference to this stat.
348 template <class Stat>
350 prereq(const Stat &prereq)
352 this->info()->prereq = prereq.info();
357 template <class Derived, template <class> class InfoProxyType>
358 class DataWrapVec : public DataWrap<Derived, InfoProxyType>
361 typedef InfoProxyType<Derived> Info;
363 DataWrapVec(Group *parent = nullptr, const char *name = nullptr,
364 const char *desc = nullptr)
365 : DataWrap<Derived, InfoProxyType>(parent, name, desc)
368 // The following functions are specific to vectors. If you use them
369 // in a non vector context, you will get a nice compiler error!
372 * Set the subfield name for the given index, and marks this stat to print
373 * at the end of simulation.
374 * @param index The subfield index.
375 * @param name The new name of the subfield.
376 * @return A reference to this stat.
379 subname(off_type index, const std::string &name)
381 Derived &self = this->self();
382 Info *info = self.info();
384 std::vector<std::string> &subn = info->subnames;
385 if (subn.size() <= index)
386 subn.resize(index + 1);
391 // The following functions are specific to 2d vectors. If you use
392 // them in a non vector context, you will get a nice compiler
393 // error because info doesn't have the right variables.
396 * Set the subfield description for the given index and marks this stat to
397 * print at the end of simulation.
398 * @param index The subfield index.
399 * @param desc The new description of the subfield
400 * @return A reference to this stat.
403 subdesc(off_type index, const std::string &desc)
405 Info *info = this->info();
407 std::vector<std::string> &subd = info->subdescs;
408 if (subd.size() <= index)
409 subd.resize(index + 1);
418 Derived &self = this->self();
419 Info *info = this->info();
421 size_t size = self.size();
422 for (off_type i = 0; i < size; ++i)
423 self.data(i)->prepare(info);
429 Derived &self = this->self();
430 Info *info = this->info();
432 size_t size = self.size();
433 for (off_type i = 0; i < size; ++i)
434 self.data(i)->reset(info);
438 template <class Derived, template <class> class InfoProxyType>
439 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
442 typedef InfoProxyType<Derived> Info;
444 DataWrapVec2d(Group *parent, const char *name, const char *desc)
445 : DataWrapVec<Derived, InfoProxyType>(parent, name, desc)
450 * @warning This makes the assumption that if you're gonna subnames a 2d
451 * vector, you're subnaming across all y
454 ysubnames(const char **names)
456 Derived &self = this->self();
457 Info *info = this->info();
459 info->y_subnames.resize(self.y);
460 for (off_type i = 0; i < self.y; ++i)
461 info->y_subnames[i] = names[i];
466 ysubname(off_type index, const std::string &subname)
468 Derived &self = this->self();
469 Info *info = this->info();
471 assert(index < self.y);
472 info->y_subnames.resize(self.y);
473 info->y_subnames[index] = subname.c_str();
478 ysubname(off_type i) const
480 return this->info()->y_subnames[i];
485 //////////////////////////////////////////////////////////////////////
489 //////////////////////////////////////////////////////////////////////
492 * Templatized storage and interface for a simple scalar stat.
497 /** The statistic value. */
501 struct Params : public StorageParams {};
505 * Builds this storage element and calls the base constructor of the
513 * The the stat to the given value.
514 * @param val The new value.
516 void set(Counter val) { data = val; }
518 * Increment the stat by the given value.
519 * @param val The new value.
521 void inc(Counter val) { data += val; }
523 * Decrement the stat by the given value.
524 * @param val The new value.
526 void dec(Counter val) { data -= val; }
528 * Return the value of this stat as its base type.
529 * @return The value of this stat.
531 Counter value() const { return data; }
533 * Return the value of this stat as a result type.
534 * @return The value of this stat.
536 Result result() const { return (Result)data; }
538 * Prepare stat data for dumping or serialization
540 void prepare(Info *info) { }
542 * Reset stat value to default
544 void reset(Info *info) { data = Counter(); }
547 * @return true if zero value
549 bool zero() const { return data == Counter(); }
553 * Templatized storage and interface to a per-tick average stat. This keeps
554 * a current count and updates a total (count * ticks) when this count
555 * changes. This allows the quick calculation of a per tick count of the item
556 * being watched. This is good for keeping track of residencies in structures
557 * among other things.
562 /** The current count. */
564 /** The tick of the last reset */
566 /** The total count for all tick. */
567 mutable Result total;
568 /** The tick that current last changed. */
572 struct Params : public StorageParams {};
576 * Build and initializes this stat storage.
579 : current(0), lastReset(0), total(0), last(0)
583 * Set the current count to the one provided, update the total and last
585 * @param val The new count.
590 total += current * (curTick() - last);
596 * Increment the current count by the provided value, calls set.
597 * @param val The amount to increment.
599 void inc(Counter val) { set(current + val); }
602 * Deccrement the current count by the provided value, calls set.
603 * @param val The amount to decrement.
605 void dec(Counter val) { set(current - val); }
608 * Return the current count.
609 * @return The current count.
611 Counter value() const { return current; }
614 * Return the current average.
615 * @return The current average.
620 assert(last == curTick());
621 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
625 * @return true if zero value
627 bool zero() const { return total == 0.0; }
630 * Prepare stat data for dumping or serialization
635 total += current * (curTick() - last);
640 * Reset stat value to default
647 lastReset = curTick();
653 * Implementation of a scalar stat. The type of stat is determined by the
656 template <class Derived, class Stor>
657 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
660 typedef Stor Storage;
661 typedef typename Stor::Params Params;
664 /** The storage of this stat. */
665 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
669 * Retrieve the storage.
670 * @param index The vector index to access.
671 * @return The storage object at the given index.
676 return reinterpret_cast<Storage *>(storage);
680 * Retrieve a const pointer to the storage.
681 * for the given index.
682 * @param index The vector index to access.
683 * @return A const pointer to the storage object at the given index.
688 return reinterpret_cast<const Storage *>(storage);
694 new (storage) Storage(this->info());
700 * Return the current value of this stat as its base type.
701 * @return The current value.
703 Counter value() const { return data()->value(); }
706 ScalarBase(Group *parent = nullptr, const char *name = nullptr,
707 const char *desc = nullptr)
708 : DataWrap<Derived, ScalarInfoProxy>(parent, name, desc)
714 // Common operators for stats
716 * Increment the stat by 1. This calls the associated storage object inc
719 void operator++() { data()->inc(1); }
721 * Decrement the stat by 1. This calls the associated storage object dec
724 void operator--() { data()->dec(1); }
726 /** Increment the stat by 1. */
727 void operator++(int) { ++*this; }
728 /** Decrement the stat by 1. */
729 void operator--(int) { --*this; }
732 * Set the data value to the given value. This calls the associated storage
733 * object set function.
734 * @param v The new value.
736 template <typename U>
737 void operator=(const U &v) { data()->set(v); }
740 * Increment the stat by the given value. This calls the associated
741 * storage object inc function.
742 * @param v The value to add.
744 template <typename U>
745 void operator+=(const U &v) { data()->inc(v); }
748 * Decrement the stat by the given value. This calls the associated
749 * storage object dec function.
750 * @param v The value to substract.
752 template <typename U>
753 void operator-=(const U &v) { data()->dec(v); }
756 * Return the number of elements, always 1 for a scalar.
759 size_type size() const { return 1; }
761 Counter value() { return data()->value(); }
763 Result result() { return data()->result(); }
765 Result total() { return result(); }
767 bool zero() { return result() == 0.0; }
769 void reset() { data()->reset(this->info()); }
770 void prepare() { data()->prepare(this->info()); }
773 class ProxyInfo : public ScalarInfo
776 std::string str() const { return std::to_string(value()); }
777 size_type size() const { return 1; }
778 bool check() const { return true; }
781 bool zero() const { return value() == 0; }
783 void visit(Output &visitor) { visitor.visit(*this); }
787 class ValueProxy : public ProxyInfo
793 ValueProxy(T &val) : scalar(&val) {}
794 Counter value() const { return *scalar; }
795 Result result() const { return *scalar; }
796 Result total() const { return *scalar; }
800 class FunctorProxy : public ProxyInfo
806 FunctorProxy(T &func) : functor(&func) {}
807 Counter value() const { return (*functor)(); }
808 Result result() const { return (*functor)(); }
809 Result total() const { return (*functor)(); }
813 * A proxy similar to the FunctorProxy, but allows calling a method of a bound
814 * object, instead of a global free-standing function.
816 template <class T, class V>
817 class MethodProxy : public ProxyInfo
821 typedef V (T::*MethodPointer) () const;
822 MethodPointer method;
825 MethodProxy(T *obj, MethodPointer meth) : object(obj), method(meth) {}
826 Counter value() const { return (object->*method)(); }
827 Result result() const { return (object->*method)(); }
828 Result total() const { return (object->*method)(); }
831 template <class Derived>
832 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
838 ValueBase(Group *parent, const char *name, const char *desc)
839 : DataWrap<Derived, ScalarInfoProxy>(parent, name, desc),
844 ~ValueBase() { if (proxy) delete proxy; }
850 proxy = new ValueProxy<T>(value);
859 proxy = new FunctorProxy<T>(func);
865 * Extended functor that calls the specified method of the provided object.
867 * @param obj Pointer to the object whose method should be called.
868 * @param method Pointer of the function / method of the object.
869 * @return Updated stats item.
871 template <class T, class V>
873 method(T *obj, V (T::*method)() const)
875 proxy = new MethodProxy<T,V>(obj, method);
880 Counter value() { return proxy->value(); }
881 Result result() const { return proxy->result(); }
882 Result total() const { return proxy->total(); };
883 size_type size() const { return proxy->size(); }
885 std::string str() const { return proxy->str(); }
886 bool zero() const { return proxy->zero(); }
887 bool check() const { return proxy != NULL; }
892 //////////////////////////////////////////////////////////////////////
896 //////////////////////////////////////////////////////////////////////
899 * A proxy class to access the stat at a given index in a VectorBase stat.
900 * Behaves like a ScalarBase.
902 template <class Stat>
906 /** Pointer to the parent Vector. */
909 /** The index to access in the parent VectorBase. */
914 * Return the current value of this stat as its base type.
915 * @return The current value.
917 Counter value() const { return stat.data(index)->value(); }
920 * Return the current value of this statas a result type.
921 * @return The current value.
923 Result result() const { return stat.data(index)->result(); }
927 * Create and initialize this proxy, do not register it with the database.
928 * @param i The index to access.
930 ScalarProxy(Stat &s, off_type i)
936 * Create a copy of the provided ScalarProxy.
937 * @param sp The proxy to copy.
939 ScalarProxy(const ScalarProxy &sp)
940 : stat(sp.stat), index(sp.index)
944 * Set this proxy equal to the provided one.
945 * @param sp The proxy to copy.
946 * @return A reference to this proxy.
949 operator=(const ScalarProxy &sp)
957 // Common operators for stats
959 * Increment the stat by 1. This calls the associated storage object inc
962 void operator++() { stat.data(index)->inc(1); }
964 * Decrement the stat by 1. This calls the associated storage object dec
967 void operator--() { stat.data(index)->dec(1); }
969 /** Increment the stat by 1. */
970 void operator++(int) { ++*this; }
971 /** Decrement the stat by 1. */
972 void operator--(int) { --*this; }
975 * Set the data value to the given value. This calls the associated storage
976 * object set function.
977 * @param v The new value.
979 template <typename U>
981 operator=(const U &v)
983 stat.data(index)->set(v);
987 * Increment the stat by the given value. This calls the associated
988 * storage object inc function.
989 * @param v The value to add.
991 template <typename U>
993 operator+=(const U &v)
995 stat.data(index)->inc(v);
999 * Decrement the stat by the given value. This calls the associated
1000 * storage object dec function.
1001 * @param v The value to substract.
1003 template <typename U>
1005 operator-=(const U &v)
1007 stat.data(index)->dec(v);
1011 * Return the number of elements, always 1 for a scalar.
1014 size_type size() const { return 1; }
1020 return csprintf("%s[%d]", stat.info()->name, index);
1025 * Implementation of a vector of stats. The type of stat is determined by the
1026 * Storage class. @sa ScalarBase
1028 template <class Derived, class Stor>
1029 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
1032 typedef Stor Storage;
1033 typedef typename Stor::Params Params;
1036 typedef ScalarProxy<Derived> Proxy;
1037 friend class ScalarProxy<Derived>;
1038 friend class DataWrapVec<Derived, VectorInfoProxy>;
1041 /** The storage of this stat. */
1047 * Retrieve the storage.
1048 * @param index The vector index to access.
1049 * @return The storage object at the given index.
1051 Storage *data(off_type index) { return &storage[index]; }
1054 * Retrieve a const pointer to the storage.
1055 * @param index The vector index to access.
1056 * @return A const pointer to the storage object at the given index.
1058 const Storage *data(off_type index) const { return &storage[index]; }
1063 assert(s > 0 && "size must be positive!");
1064 assert(!storage && "already initialized");
1067 char *ptr = new char[_size * sizeof(Storage)];
1068 storage = reinterpret_cast<Storage *>(ptr);
1070 for (off_type i = 0; i < _size; ++i)
1071 new (&storage[i]) Storage(this->info());
1078 value(VCounter &vec) const
1081 for (off_type i = 0; i < size(); ++i)
1082 vec[i] = data(i)->value();
1086 * Copy the values to a local vector and return a reference to it.
1087 * @return A reference to a vector of the stat values.
1090 result(VResult &vec) const
1093 for (off_type i = 0; i < size(); ++i)
1094 vec[i] = data(i)->result();
1098 * Return a total of all entries in this vector.
1099 * @return The total of all vector entries.
1105 for (off_type i = 0; i < size(); ++i)
1106 total += data(i)->result();
1111 * @return the number of elements in this vector.
1113 size_type size() const { return _size; }
1118 for (off_type i = 0; i < size(); ++i)
1119 if (data(i)->zero())
1127 return storage != NULL;
1131 VectorBase(Group *parent, const char *name, const char *desc)
1132 : DataWrapVec<Derived, VectorInfoProxy>(parent, name, desc),
1133 storage(nullptr), _size(0)
1141 for (off_type i = 0; i < _size; ++i)
1142 data(i)->~Storage();
1143 delete [] reinterpret_cast<char *>(storage);
1147 * Set this vector to have the given size.
1148 * @param size The new size.
1149 * @return A reference to this stat.
1152 init(size_type size)
1154 Derived &self = this->self();
1160 * Return a reference (ScalarProxy) to the stat at the given index.
1161 * @param index The vector index to access.
1162 * @return A reference of the stat.
1165 operator[](off_type index)
1167 assert (index >= 0 && index < size());
1168 return Proxy(this->self(), index);
1172 template <class Stat>
1181 mutable VResult vec;
1183 typename Stat::Storage *
1184 data(off_type index)
1186 assert(index < len);
1187 return stat.data(offset + index);
1190 const typename Stat::Storage *
1191 data(off_type index) const
1193 assert(index < len);
1194 return stat.data(offset + index);
1203 for (off_type i = 0; i < size(); ++i)
1204 vec[i] = data(i)->result();
1213 for (off_type i = 0; i < size(); ++i)
1214 total += data(i)->result();
1219 VectorProxy(Stat &s, off_type o, size_type l)
1220 : stat(s), offset(o), len(l)
1224 VectorProxy(const VectorProxy &sp)
1225 : stat(sp.stat), offset(sp.offset), len(sp.len)
1230 operator=(const VectorProxy &sp)
1239 operator[](off_type index)
1241 assert (index >= 0 && index < size());
1242 return ScalarProxy<Stat>(stat, offset + index);
1245 size_type size() const { return len; }
1248 template <class Derived, class Stor>
1249 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1252 typedef Vector2dInfoProxy<Derived> Info;
1253 typedef Stor Storage;
1254 typedef typename Stor::Params Params;
1255 typedef VectorProxy<Derived> Proxy;
1256 friend class ScalarProxy<Derived>;
1257 friend class VectorProxy<Derived>;
1258 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1259 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1268 Storage *data(off_type index) { return &storage[index]; }
1269 const Storage *data(off_type index) const { return &storage[index]; }
1272 Vector2dBase(Group *parent, const char *name, const char *desc)
1273 : DataWrapVec2d<Derived, Vector2dInfoProxy>(parent, name, desc),
1274 x(0), y(0), _size(0), storage(nullptr)
1282 for (off_type i = 0; i < _size; ++i)
1283 data(i)->~Storage();
1284 delete [] reinterpret_cast<char *>(storage);
1288 init(size_type _x, size_type _y)
1290 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1291 assert(!storage && "already initialized");
1293 Derived &self = this->self();
1294 Info *info = this->info();
1302 char *ptr = new char[_size * sizeof(Storage)];
1303 storage = reinterpret_cast<Storage *>(ptr);
1305 for (off_type i = 0; i < _size; ++i)
1306 new (&storage[i]) Storage(info);
1314 operator[](off_type index)
1316 off_type offset = index * y;
1317 assert (index >= 0 && offset + y <= size());
1318 return Proxy(this->self(), offset, y);
1331 return data(0)->zero();
1335 * Return a total of all entries in this vector.
1336 * @return The total of all vector entries.
1342 for (off_type i = 0; i < size(); ++i)
1343 total += data(i)->result();
1350 Info *info = this->info();
1351 size_type size = this->size();
1353 for (off_type i = 0; i < size; ++i)
1354 data(i)->prepare(info);
1356 info->cvec.resize(size);
1357 for (off_type i = 0; i < size; ++i)
1358 info->cvec[i] = data(i)->value();
1362 * Reset stat value to default
1367 Info *info = this->info();
1368 size_type size = this->size();
1369 for (off_type i = 0; i < size; ++i)
1370 data(i)->reset(info);
1376 return storage != NULL;
1380 //////////////////////////////////////////////////////////////////////
1382 // Non formula statistics
1384 //////////////////////////////////////////////////////////////////////
1385 /** The parameters for a distribution stat. */
1386 struct DistParams : public StorageParams
1388 const DistType type;
1389 DistParams(DistType t) : type(t) {}
1393 * Templatized storage and interface for a distribution stat.
1398 /** The parameters for a distribution stat. */
1399 struct Params : public DistParams
1401 /** The minimum value to track. */
1403 /** The maximum value to track. */
1405 /** The number of entries in each bucket. */
1406 Counter bucket_size;
1407 /** The number of buckets. Equal to (max-min)/bucket_size. */
1410 Params() : DistParams(Dist), min(0), max(0), bucket_size(0),
1415 /** The minimum value to track. */
1417 /** The maximum value to track. */
1419 /** The number of entries in each bucket. */
1420 Counter bucket_size;
1422 /** The smallest value sampled. */
1424 /** The largest value sampled. */
1426 /** The number of values sampled less than min. */
1428 /** The number of values sampled more than max. */
1430 /** The current sum. */
1432 /** The sum of squares. */
1434 /** The number of samples. */
1436 /** Counter for each bucket. */
1440 DistStor(Info *info)
1441 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1447 * Add a value to the distribution for the given number of times.
1448 * @param val The value to add.
1449 * @param number The number of times to add the value.
1452 sample(Counter val, int number)
1454 if (val < min_track)
1455 underflow += number;
1456 else if (val > max_track)
1460 (size_type)std::floor((val - min_track) / bucket_size);
1461 assert(index < size());
1462 cvec[index] += number;
1471 sum += val * number;
1472 squares += val * val * number;
1477 * Return the number of buckets in this distribution.
1478 * @return the number of buckets.
1480 size_type size() const { return cvec.size(); }
1483 * Returns true if any calls to sample have been made.
1484 * @return True if any values have been sampled.
1489 return samples == Counter();
1493 prepare(Info *info, DistData &data)
1495 const Params *params = safe_cast<const Params *>(info->storageParams);
1497 assert(params->type == Dist);
1498 data.type = params->type;
1499 data.min = params->min;
1500 data.max = params->max;
1501 data.bucket_size = params->bucket_size;
1503 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1504 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1505 data.underflow = underflow;
1506 data.overflow = overflow;
1508 data.cvec.resize(params->buckets);
1509 for (off_type i = 0; i < params->buckets; ++i)
1510 data.cvec[i] = cvec[i];
1513 data.squares = squares;
1514 data.samples = samples;
1518 * Reset stat value to default
1523 const Params *params = safe_cast<const Params *>(info->storageParams);
1524 min_track = params->min;
1525 max_track = params->max;
1526 bucket_size = params->bucket_size;
1528 min_val = CounterLimits::max();
1529 max_val = CounterLimits::min();
1530 underflow = Counter();
1531 overflow = Counter();
1533 size_type size = cvec.size();
1534 for (off_type i = 0; i < size; ++i)
1535 cvec[i] = Counter();
1538 squares = Counter();
1539 samples = Counter();
1544 * Templatized storage and interface for a histogram stat.
1549 /** The parameters for a distribution stat. */
1550 struct Params : public DistParams
1552 /** The number of buckets.. */
1555 Params() : DistParams(Hist), buckets(0) {}
1559 /** The minimum value to track. */
1561 /** The maximum value to track. */
1563 /** The number of entries in each bucket. */
1564 Counter bucket_size;
1566 /** The current sum. */
1568 /** The sum of logarithm of each sample, used to compute geometric mean. */
1570 /** The sum of squares. */
1572 /** The number of samples. */
1574 /** Counter for each bucket. */
1578 HistStor(Info *info)
1579 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1586 void grow_convert();
1587 void add(HistStor *);
1590 * Add a value to the distribution for the given number of times.
1591 * @param val The value to add.
1592 * @param number The number of times to add the value.
1595 sample(Counter val, int number)
1597 assert(min_bucket < max_bucket);
1598 if (val < min_bucket) {
1599 if (min_bucket == 0)
1602 while (val < min_bucket)
1604 } else if (val >= max_bucket + bucket_size) {
1605 if (min_bucket == 0) {
1606 while (val >= max_bucket + bucket_size)
1609 while (val >= max_bucket + bucket_size)
1615 (int64_t)std::floor((val - min_bucket) / bucket_size);
1617 assert(index < size());
1618 cvec[index] += number;
1620 sum += val * number;
1621 squares += val * val * number;
1622 logs += log(val) * number;
1627 * Return the number of buckets in this distribution.
1628 * @return the number of buckets.
1630 size_type size() const { return cvec.size(); }
1633 * Returns true if any calls to sample have been made.
1634 * @return True if any values have been sampled.
1639 return samples == Counter();
1643 prepare(Info *info, DistData &data)
1645 const Params *params = safe_cast<const Params *>(info->storageParams);
1647 assert(params->type == Hist);
1648 data.type = params->type;
1649 data.min = min_bucket;
1650 data.max = max_bucket + bucket_size - 1;
1651 data.bucket_size = bucket_size;
1653 data.min_val = min_bucket;
1654 data.max_val = max_bucket;
1656 int buckets = params->buckets;
1657 data.cvec.resize(buckets);
1658 for (off_type i = 0; i < buckets; ++i)
1659 data.cvec[i] = cvec[i];
1663 data.squares = squares;
1664 data.samples = samples;
1668 * Reset stat value to default
1673 const Params *params = safe_cast<const Params *>(info->storageParams);
1675 max_bucket = params->buckets - 1;
1678 size_type size = cvec.size();
1679 for (off_type i = 0; i < size; ++i)
1680 cvec[i] = Counter();
1683 squares = Counter();
1684 samples = Counter();
1690 * Templatized storage and interface for a distribution that calculates mean
1696 struct Params : public DistParams
1698 Params() : DistParams(Deviation) {}
1702 /** The current sum. */
1704 /** The sum of squares. */
1706 /** The number of samples. */
1711 * Create and initialize this storage.
1713 SampleStor(Info *info)
1714 : sum(Counter()), squares(Counter()), samples(Counter())
1718 * Add a value the given number of times to this running average.
1719 * Update the running sum and sum of squares, increment the number of
1720 * values seen by the given number.
1721 * @param val The value to add.
1722 * @param number The number of times to add the value.
1725 sample(Counter val, int number)
1727 sum += val * number;
1728 squares += val * val * number;
1733 * Return the number of entries in this stat, 1
1736 size_type size() const { return 1; }
1739 * Return true if no samples have been added.
1740 * @return True if no samples have been added.
1742 bool zero() const { return samples == Counter(); }
1745 prepare(Info *info, DistData &data)
1747 const Params *params = safe_cast<const Params *>(info->storageParams);
1749 assert(params->type == Deviation);
1750 data.type = params->type;
1752 data.squares = squares;
1753 data.samples = samples;
1757 * Reset stat value to default
1763 squares = Counter();
1764 samples = Counter();
1769 * Templatized storage for distribution that calculates per tick mean and
1775 struct Params : public DistParams
1777 Params() : DistParams(Deviation) {}
1781 /** Current total. */
1783 /** Current sum of squares. */
1788 * Create and initialize this storage.
1790 AvgSampleStor(Info *info)
1791 : sum(Counter()), squares(Counter())
1795 * Add a value to the distribution for the given number of times.
1796 * Update the running sum and sum of squares.
1797 * @param val The value to add.
1798 * @param number The number of times to add the value.
1801 sample(Counter val, int number)
1803 sum += val * number;
1804 squares += val * val * number;
1808 * Return the number of entries, in this case 1.
1811 size_type size() const { return 1; }
1814 * Return true if no samples have been added.
1815 * @return True if the sum is zero.
1817 bool zero() const { return sum == Counter(); }
1820 prepare(Info *info, DistData &data)
1822 const Params *params = safe_cast<const Params *>(info->storageParams);
1824 assert(params->type == Deviation);
1825 data.type = params->type;
1827 data.squares = squares;
1828 data.samples = curTick();
1832 * Reset stat value to default
1838 squares = Counter();
1843 * Implementation of a distribution stat. The type of distribution is
1844 * determined by the Storage template. @sa ScalarBase
1846 template <class Derived, class Stor>
1847 class DistBase : public DataWrap<Derived, DistInfoProxy>
1850 typedef DistInfoProxy<Derived> Info;
1851 typedef Stor Storage;
1852 typedef typename Stor::Params Params;
1855 /** The storage for this stat. */
1856 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1860 * Retrieve the storage.
1861 * @return The storage object for this stat.
1866 return reinterpret_cast<Storage *>(storage);
1870 * Retrieve a const pointer to the storage.
1871 * @return A const pointer to the storage object for this stat.
1876 return reinterpret_cast<const Storage *>(storage);
1882 new (storage) Storage(this->info());
1887 DistBase(Group *parent, const char *name, const char *desc)
1888 : DataWrap<Derived, DistInfoProxy>(parent, name, desc)
1893 * Add a value to the distribtion n times. Calls sample on the storage
1895 * @param v The value to add.
1896 * @param n The number of times to add it, defaults to 1.
1898 template <typename U>
1899 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1902 * Return the number of entries in this stat.
1903 * @return The number of entries.
1905 size_type size() const { return data()->size(); }
1907 * Return true if no samples have been added.
1908 * @return True if there haven't been any samples.
1910 bool zero() const { return data()->zero(); }
1915 Info *info = this->info();
1916 data()->prepare(info, info->data);
1920 * Reset stat value to default
1925 data()->reset(this->info());
1929 * Add the argument distribution to the this distribution.
1931 void add(DistBase &d) { data()->add(d.data()); }
1935 template <class Stat>
1938 template <class Derived, class Stor>
1939 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1942 typedef VectorDistInfoProxy<Derived> Info;
1943 typedef Stor Storage;
1944 typedef typename Stor::Params Params;
1945 typedef DistProxy<Derived> Proxy;
1946 friend class DistProxy<Derived>;
1947 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1955 data(off_type index)
1957 return &storage[index];
1961 data(off_type index) const
1963 return &storage[index];
1969 assert(s > 0 && "size must be positive!");
1970 assert(!storage && "already initialized");
1973 char *ptr = new char[_size * sizeof(Storage)];
1974 storage = reinterpret_cast<Storage *>(ptr);
1976 Info *info = this->info();
1977 for (off_type i = 0; i < _size; ++i)
1978 new (&storage[i]) Storage(info);
1984 VectorDistBase(Group *parent, const char *name, const char *desc)
1985 : DataWrapVec<Derived, VectorDistInfoProxy>(parent, name, desc),
1994 for (off_type i = 0; i < _size; ++i)
1995 data(i)->~Storage();
1996 delete [] reinterpret_cast<char *>(storage);
1999 Proxy operator[](off_type index)
2001 assert(index >= 0 && index < size());
2002 return Proxy(this->self(), index);
2014 for (off_type i = 0; i < size(); ++i)
2015 if (!data(i)->zero())
2023 Info *info = this->info();
2024 size_type size = this->size();
2025 info->data.resize(size);
2026 for (off_type i = 0; i < size; ++i)
2027 data(i)->prepare(info, info->data[i]);
2033 return storage != NULL;
2037 template <class Stat>
2045 typename Stat::Storage *data() { return stat.data(index); }
2046 const typename Stat::Storage *data() const { return stat.data(index); }
2049 DistProxy(Stat &s, off_type i)
2053 DistProxy(const DistProxy &sp)
2054 : stat(sp.stat), index(sp.index)
2058 operator=(const DistProxy &sp)
2066 template <typename U>
2068 sample(const U &v, int n = 1)
2070 data()->sample(v, n);
2082 return data()->zero();
2086 * Proxy has no state. Nothing to reset.
2091 //////////////////////////////////////////////////////////////////////
2095 //////////////////////////////////////////////////////////////////////
2098 * Base class for formula statistic node. These nodes are used to build a tree
2099 * that represents the formula.
2105 * Return the number of nodes in the subtree starting at this node.
2106 * @return the number of nodes in this subtree.
2108 virtual size_type size() const = 0;
2110 * Return the result vector of this subtree.
2111 * @return The result vector of this subtree.
2113 virtual const VResult &result() const = 0;
2115 * Return the total of the result vector.
2116 * @return The total of the result vector.
2118 virtual Result total() const = 0;
2123 virtual std::string str() const = 0;
2128 /** Shared pointer to a function Node. */
2129 typedef std::shared_ptr<Node> NodePtr;
2131 class ScalarStatNode : public Node
2134 const ScalarInfo *data;
2135 mutable VResult vresult;
2138 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2143 vresult[0] = data->result();
2147 Result total() const { return data->result(); };
2149 size_type size() const { return 1; }
2154 std::string str() const { return data->name; }
2157 template <class Stat>
2158 class ScalarProxyNode : public Node
2161 const ScalarProxy<Stat> proxy;
2162 mutable VResult vresult;
2165 ScalarProxyNode(const ScalarProxy<Stat> &p)
2166 : proxy(p), vresult(1)
2172 vresult[0] = proxy.result();
2179 return proxy.result();
2198 class VectorStatNode : public Node
2201 const VectorInfo *data;
2204 VectorStatNode(const VectorInfo *d) : data(d) { }
2205 const VResult &result() const { return data->result(); }
2206 Result total() const { return data->total(); };
2208 size_type size() const { return data->size(); }
2210 std::string str() const { return data->name; }
2214 class ConstNode : public Node
2220 ConstNode(T s) : vresult(1, (Result)s) {}
2221 const VResult &result() const { return vresult; }
2222 Result total() const { return vresult[0]; };
2223 size_type size() const { return 1; }
2224 std::string str() const { return std::to_string(vresult[0]); }
2228 class ConstVectorNode : public Node
2234 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2235 const VResult &result() const { return vresult; }
2240 size_type size = this->size();
2242 for (off_type i = 0; i < size; i++)
2247 size_type size() const { return vresult.size(); }
2251 size_type size = this->size();
2252 std::string tmp = "(";
2253 for (off_type i = 0; i < size; i++)
2254 tmp += csprintf("%s ", std::to_string(vresult[i]));
2264 struct OpString<std::plus<Result> >
2266 static std::string str() { return "+"; }
2270 struct OpString<std::minus<Result> >
2272 static std::string str() { return "-"; }
2276 struct OpString<std::multiplies<Result> >
2278 static std::string str() { return "*"; }
2282 struct OpString<std::divides<Result> >
2284 static std::string str() { return "/"; }
2288 struct OpString<std::modulus<Result> >
2290 static std::string str() { return "%"; }
2294 struct OpString<std::negate<Result> >
2296 static std::string str() { return "-"; }
2300 class UnaryNode : public Node
2304 mutable VResult vresult;
2307 UnaryNode(NodePtr &p) : l(p) {}
2312 const VResult &lvec = l->result();
2313 size_type size = lvec.size();
2317 vresult.resize(size);
2319 for (off_type i = 0; i < size; ++i)
2320 vresult[i] = op(lvec[i]);
2328 const VResult &vec = this->result();
2330 for (off_type i = 0; i < size(); i++)
2335 size_type size() const { return l->size(); }
2340 return OpString<Op>::str() + l->str();
2345 class BinaryNode : public Node
2350 mutable VResult vresult;
2353 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2356 result() const override
2359 const VResult &lvec = l->result();
2360 const VResult &rvec = r->result();
2362 assert(lvec.size() > 0 && rvec.size() > 0);
2364 if (lvec.size() == 1 && rvec.size() == 1) {
2366 vresult[0] = op(lvec[0], rvec[0]);
2367 } else if (lvec.size() == 1) {
2368 size_type size = rvec.size();
2369 vresult.resize(size);
2370 for (off_type i = 0; i < size; ++i)
2371 vresult[i] = op(lvec[0], rvec[i]);
2372 } else if (rvec.size() == 1) {
2373 size_type size = lvec.size();
2374 vresult.resize(size);
2375 for (off_type i = 0; i < size; ++i)
2376 vresult[i] = op(lvec[i], rvec[0]);
2377 } else if (rvec.size() == lvec.size()) {
2378 size_type size = rvec.size();
2379 vresult.resize(size);
2380 for (off_type i = 0; i < size; ++i)
2381 vresult[i] = op(lvec[i], rvec[i]);
2388 total() const override
2390 const VResult &vec = this->result();
2391 const VResult &lvec = l->result();
2392 const VResult &rvec = r->result();
2398 assert(lvec.size() > 0 && rvec.size() > 0);
2399 assert(lvec.size() == rvec.size() ||
2400 lvec.size() == 1 || rvec.size() == 1);
2402 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2403 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2404 for (off_type i = 0; i < size(); ++i) {
2408 return op(lsum, rsum);
2411 /** Otherwise divide each item by the divisor */
2412 for (off_type i = 0; i < size(); ++i) {
2420 size() const override
2422 size_type ls = l->size();
2423 size_type rs = r->size();
2426 } else if (rs == 1) {
2429 assert(ls == rs && "Node vector sizes are not equal");
2435 str() const override
2437 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2442 class SumNode : public Node
2446 mutable VResult vresult;
2449 SumNode(NodePtr &p) : l(p), vresult(1) {}
2454 const VResult &lvec = l->result();
2455 size_type size = lvec.size();
2461 for (off_type i = 0; i < size; ++i)
2462 vresult[0] = op(vresult[0], lvec[i]);
2470 const VResult &lvec = l->result();
2471 size_type size = lvec.size();
2474 Result result = 0.0;
2477 for (off_type i = 0; i < size; ++i)
2478 result = op(result, lvec[i]);
2483 size_type size() const { return 1; }
2488 return csprintf("total(%s)", l->str());
2493 //////////////////////////////////////////////////////////////////////
2495 // Visible Statistics Types
2497 //////////////////////////////////////////////////////////////////////
2499 * @defgroup VisibleStats "Statistic Types"
2500 * These are the statistics that are used in the simulator.
2505 * This is a simple scalar statistic, like a counter.
2506 * @sa Stat, ScalarBase, StatStor
2508 class Scalar : public ScalarBase<Scalar, StatStor>
2511 using ScalarBase<Scalar, StatStor>::operator=;
2513 Scalar(Group *parent = nullptr, const char *name = nullptr,
2514 const char *desc = nullptr)
2515 : ScalarBase<Scalar, StatStor>(parent, name, desc)
2521 * A stat that calculates the per tick average of a value.
2522 * @sa Stat, ScalarBase, AvgStor
2524 class Average : public ScalarBase<Average, AvgStor>
2527 using ScalarBase<Average, AvgStor>::operator=;
2529 Average(Group *parent = nullptr, const char *name = nullptr,
2530 const char *desc = nullptr)
2531 : ScalarBase<Average, AvgStor>(parent, name, desc)
2536 class Value : public ValueBase<Value>
2539 Value(Group *parent = nullptr, const char *name = nullptr,
2540 const char *desc = nullptr)
2541 : ValueBase<Value>(parent, name, desc)
2547 * A vector of scalar stats.
2548 * @sa Stat, VectorBase, StatStor
2550 class Vector : public VectorBase<Vector, StatStor>
2553 Vector(Group *parent = nullptr, const char *name = nullptr,
2554 const char *desc = nullptr)
2555 : VectorBase<Vector, StatStor>(parent, name, desc)
2561 * A vector of Average stats.
2562 * @sa Stat, VectorBase, AvgStor
2564 class AverageVector : public VectorBase<AverageVector, AvgStor>
2567 AverageVector(Group *parent = nullptr, const char *name = nullptr,
2568 const char *desc = nullptr)
2569 : VectorBase<AverageVector, AvgStor>(parent, name, desc)
2575 * A 2-Dimensional vecto of scalar stats.
2576 * @sa Stat, Vector2dBase, StatStor
2578 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2581 Vector2d(Group *parent = nullptr, const char *name = nullptr,
2582 const char *desc = nullptr)
2583 : Vector2dBase<Vector2d, StatStor>(parent, name, desc)
2589 * A simple distribution stat.
2590 * @sa Stat, DistBase, DistStor
2592 class Distribution : public DistBase<Distribution, DistStor>
2595 Distribution(Group *parent = nullptr, const char *name = nullptr,
2596 const char *desc = nullptr)
2597 : DistBase<Distribution, DistStor>(parent, name, desc)
2602 * Set the parameters of this distribution. @sa DistStor::Params
2603 * @param min The minimum value of the distribution.
2604 * @param max The maximum value of the distribution.
2605 * @param bkt The number of values in each bucket.
2606 * @return A reference to this distribution.
2609 init(Counter min, Counter max, Counter bkt)
2611 DistStor::Params *params = new DistStor::Params;
2614 params->bucket_size = bkt;
2615 // Division by zero is especially serious in an Aarch64 host,
2616 // where it gets rounded to allocate 32GiB RAM.
2618 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2619 this->setParams(params);
2621 return this->self();
2626 * A simple histogram stat.
2627 * @sa Stat, DistBase, HistStor
2629 class Histogram : public DistBase<Histogram, HistStor>
2632 Histogram(Group *parent = nullptr, const char *name = nullptr,
2633 const char *desc = nullptr)
2634 : DistBase<Histogram, HistStor>(parent, name, desc)
2639 * Set the parameters of this histogram. @sa HistStor::Params
2640 * @param size The number of buckets in the histogram
2641 * @return A reference to this histogram.
2644 init(size_type size)
2646 HistStor::Params *params = new HistStor::Params;
2647 params->buckets = size;
2648 this->setParams(params);
2650 return this->self();
2655 * Calculates the mean and variance of all the samples.
2656 * @sa DistBase, SampleStor
2658 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2662 * Construct and initialize this distribution.
2664 StandardDeviation(Group *parent = nullptr, const char *name = nullptr,
2665 const char *desc = nullptr)
2666 : DistBase<StandardDeviation, SampleStor>(parent, name, desc)
2668 SampleStor::Params *params = new SampleStor::Params;
2670 this->setParams(params);
2675 * Calculates the per tick mean and variance of the samples.
2676 * @sa DistBase, AvgSampleStor
2678 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2682 * Construct and initialize this distribution.
2684 AverageDeviation(Group *parent = nullptr, const char *name = nullptr,
2685 const char *desc = nullptr)
2686 : DistBase<AverageDeviation, AvgSampleStor>(parent, name, desc)
2688 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2690 this->setParams(params);
2695 * A vector of distributions.
2696 * @sa VectorDistBase, DistStor
2698 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2701 VectorDistribution(Group *parent = nullptr, const char *name = nullptr,
2702 const char *desc = nullptr)
2703 : VectorDistBase<VectorDistribution, DistStor>(parent, name, desc)
2708 * Initialize storage and parameters for this distribution.
2709 * @param size The size of the vector (the number of distributions).
2710 * @param min The minimum value of the distribution.
2711 * @param max The maximum value of the distribution.
2712 * @param bkt The number of values in each bucket.
2713 * @return A reference to this distribution.
2715 VectorDistribution &
2716 init(size_type size, Counter min, Counter max, Counter bkt)
2718 DistStor::Params *params = new DistStor::Params;
2721 params->bucket_size = bkt;
2722 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2723 this->setParams(params);
2725 return this->self();
2730 * This is a vector of StandardDeviation stats.
2731 * @sa VectorDistBase, SampleStor
2733 class VectorStandardDeviation
2734 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2737 VectorStandardDeviation(Group *parent = nullptr, const char *name = nullptr,
2738 const char *desc = nullptr)
2739 : VectorDistBase<VectorStandardDeviation, SampleStor>(parent, name,
2745 * Initialize storage for this distribution.
2746 * @param size The size of the vector.
2747 * @return A reference to this distribution.
2749 VectorStandardDeviation &
2750 init(size_type size)
2752 SampleStor::Params *params = new SampleStor::Params;
2754 this->setParams(params);
2755 return this->self();
2760 * This is a vector of AverageDeviation stats.
2761 * @sa VectorDistBase, AvgSampleStor
2763 class VectorAverageDeviation
2764 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2767 VectorAverageDeviation(Group *parent = nullptr, const char *name = nullptr,
2768 const char *desc = nullptr)
2769 : VectorDistBase<VectorAverageDeviation, AvgSampleStor>(parent, name,
2775 * Initialize storage for this distribution.
2776 * @param size The size of the vector.
2777 * @return A reference to this distribution.
2779 VectorAverageDeviation &
2780 init(size_type size)
2782 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2784 this->setParams(params);
2785 return this->self();
2789 template <class Stat>
2790 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2793 mutable VResult vec;
2794 mutable VCounter cvec;
2797 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2799 size_type size() const { return this->s.size(); }
2804 this->s.result(vec);
2807 Result total() const { return this->s.total(); }
2808 VCounter &value() const { return cvec; }
2810 std::string str() const { return this->s.str(); }
2813 template <class Stat>
2814 class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2817 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2821 * Implementation of a sparse histogram stat. The storage class is
2822 * determined by the Storage template.
2824 template <class Derived, class Stor>
2825 class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2828 typedef SparseHistInfoProxy<Derived> Info;
2829 typedef Stor Storage;
2830 typedef typename Stor::Params Params;
2833 /** The storage for this stat. */
2834 char storage[sizeof(Storage)];
2838 * Retrieve the storage.
2839 * @return The storage object for this stat.
2844 return reinterpret_cast<Storage *>(storage);
2848 * Retrieve a const pointer to the storage.
2849 * @return A const pointer to the storage object for this stat.
2854 return reinterpret_cast<const Storage *>(storage);
2860 new (storage) Storage(this->info());
2865 SparseHistBase(Group *parent, const char *name, const char *desc)
2866 : DataWrap<Derived, SparseHistInfoProxy>(parent, name, desc)
2871 * Add a value to the distribtion n times. Calls sample on the storage
2873 * @param v The value to add.
2874 * @param n The number of times to add it, defaults to 1.
2876 template <typename U>
2877 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2880 * Return the number of entries in this stat.
2881 * @return The number of entries.
2883 size_type size() const { return data()->size(); }
2885 * Return true if no samples have been added.
2886 * @return True if there haven't been any samples.
2888 bool zero() const { return data()->zero(); }
2893 Info *info = this->info();
2894 data()->prepare(info, info->data);
2898 * Reset stat value to default
2903 data()->reset(this->info());
2908 * Templatized storage and interface for a sparse histogram stat.
2910 class SparseHistStor
2913 /** The parameters for a sparse histogram stat. */
2914 struct Params : public DistParams
2916 Params() : DistParams(Hist) {}
2920 /** Counter for number of samples */
2922 /** Counter for each bucket. */
2926 SparseHistStor(Info *info)
2932 * Add a value to the distribution for the given number of times.
2933 * @param val The value to add.
2934 * @param number The number of times to add the value.
2937 sample(Counter val, int number)
2939 cmap[val] += number;
2944 * Return the number of buckets in this distribution.
2945 * @return the number of buckets.
2947 size_type size() const { return cmap.size(); }
2950 * Returns true if any calls to sample have been made.
2951 * @return True if any values have been sampled.
2956 return samples == Counter();
2960 prepare(Info *info, SparseHistData &data)
2962 MCounter::iterator it;
2964 for (it = cmap.begin(); it != cmap.end(); it++) {
2965 data.cmap[(*it).first] = (*it).second;
2968 data.samples = samples;
2972 * Reset stat value to default
2982 class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2985 SparseHistogram(Group *parent = nullptr, const char *name = nullptr,
2986 const char *desc = nullptr)
2987 : SparseHistBase<SparseHistogram, SparseHistStor>(parent, name, desc)
2992 * Set the parameters of this histogram. @sa HistStor::Params
2993 * @param size The number of buckets in the histogram
2994 * @return A reference to this histogram.
2997 init(size_type size)
2999 SparseHistStor::Params *params = new SparseHistStor::Params;
3000 this->setParams(params);
3002 return this->self();
3008 * A formula for statistics that is calculated when printed. A formula is
3009 * stored as a tree of Nodes that represent the equation to calculate.
3010 * @sa Stat, ScalarStat, VectorStat, Node, Temp
3012 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
3015 /** The root of the tree which represents the Formula */
3021 * Create and initialize thie formula, and register it with the database.
3023 Formula(Group *parent = nullptr, const char *name = nullptr,
3024 const char *desc = nullptr);
3026 Formula(Group *parent, const char *name, const char *desc,
3030 * Set an unitialized Formula to the given root.
3031 * @param r The root of the expression tree.
3032 * @return a reference to this formula.
3034 const Formula &operator=(const Temp &r);
3036 template<typename T>
3037 const Formula &operator=(const T &v)
3044 * Add the given tree to the existing one.
3045 * @param r The root of the expression tree.
3046 * @return a reference to this formula.
3048 const Formula &operator+=(Temp r);
3051 * Divide the existing tree by the given one.
3052 * @param r The root of the expression tree.
3053 * @return a reference to this formula.
3055 const Formula &operator/=(Temp r);
3058 * Return the result of the Fomula in a vector. If there were no Vector
3059 * components to the Formula, then the vector is size 1. If there were,
3060 * like x/y with x being a vector of size 3, then the result returned will
3061 * be x[0]/y, x[1]/y, x[2]/y, respectively.
3062 * @return The result vector.
3064 void result(VResult &vec) const;
3067 * Return the total Formula result. If there is a Vector
3068 * component to this Formula, then this is the result of the
3069 * Formula if the formula is applied after summing all the
3070 * components of the Vector. For example, if Formula is x/y where
3071 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
3072 * there is no Vector component, total() returns the same value as
3073 * the first entry in the VResult val() returns.
3074 * @return The total of the result vector.
3076 Result total() const;
3079 * Return the number of elements in the tree.
3081 size_type size() const;
3086 * Formulas don't need to be reset
3095 std::string str() const;
3098 class FormulaNode : public Node
3101 const Formula &formula;
3102 mutable VResult vec;
3105 FormulaNode(const Formula &f) : formula(f) {}
3107 size_type size() const { return formula.size(); }
3108 const VResult &result() const { formula.result(vec); return vec; }
3109 Result total() const { return formula.total(); }
3111 std::string str() const { return formula.str(); }
3115 * Helper class to construct formula node trees.
3121 * Pointer to a Node object.
3127 * Copy the given pointer to this class.
3128 * @param n A pointer to a Node object to copy.
3130 Temp(const NodePtr &n) : node(n) { }
3132 Temp(NodePtr &&n) : node(std::move(n)) { }
3135 * Return the node pointer.
3136 * @return the node pointer.
3138 operator NodePtr&() { return node; }
3141 * Makde gcc < 4.6.3 happy and explicitly get the underlying node.
3143 NodePtr getNodePtr() const { return node; }
3147 * Create a new ScalarStatNode.
3148 * @param s The ScalarStat to place in a node.
3150 Temp(const Scalar &s)
3151 : node(new ScalarStatNode(s.info()))
3155 * Create a new ScalarStatNode.
3156 * @param s The ScalarStat to place in a node.
3158 Temp(const Value &s)
3159 : node(new ScalarStatNode(s.info()))
3163 * Create a new ScalarStatNode.
3164 * @param s The ScalarStat to place in a node.
3166 Temp(const Average &s)
3167 : node(new ScalarStatNode(s.info()))
3171 * Create a new VectorStatNode.
3172 * @param s The VectorStat to place in a node.
3174 Temp(const Vector &s)
3175 : node(new VectorStatNode(s.info()))
3178 Temp(const AverageVector &s)
3179 : node(new VectorStatNode(s.info()))
3185 Temp(const Formula &f)
3186 : node(new FormulaNode(f))
3190 * Create a new ScalarProxyNode.
3191 * @param p The ScalarProxy to place in a node.
3193 template <class Stat>
3194 Temp(const ScalarProxy<Stat> &p)
3195 : node(new ScalarProxyNode<Stat>(p))
3199 * Create a ConstNode
3200 * @param value The value of the const node.
3202 Temp(signed char value)
3203 : node(new ConstNode<signed char>(value))
3207 * Create a ConstNode
3208 * @param value The value of the const node.
3210 Temp(unsigned char value)
3211 : node(new ConstNode<unsigned char>(value))
3215 * Create a ConstNode
3216 * @param value The value of the const node.
3218 Temp(signed short value)
3219 : node(new ConstNode<signed short>(value))
3223 * Create a ConstNode
3224 * @param value The value of the const node.
3226 Temp(unsigned short value)
3227 : node(new ConstNode<unsigned short>(value))
3231 * Create a ConstNode
3232 * @param value The value of the const node.
3234 Temp(signed int value)
3235 : node(new ConstNode<signed int>(value))
3239 * Create a ConstNode
3240 * @param value The value of the const node.
3242 Temp(unsigned int value)
3243 : node(new ConstNode<unsigned int>(value))
3247 * Create a ConstNode
3248 * @param value The value of the const node.
3250 Temp(signed long value)
3251 : node(new ConstNode<signed long>(value))
3255 * Create a ConstNode
3256 * @param value The value of the const node.
3258 Temp(unsigned long value)
3259 : node(new ConstNode<unsigned long>(value))
3263 * Create a ConstNode
3264 * @param value The value of the const node.
3266 Temp(signed long long value)
3267 : node(new ConstNode<signed long long>(value))
3271 * Create a ConstNode
3272 * @param value The value of the const node.
3274 Temp(unsigned long long value)
3275 : node(new ConstNode<unsigned long long>(value))
3279 * Create a ConstNode
3280 * @param value The value of the const node.
3283 : node(new ConstNode<float>(value))
3287 * Create a ConstNode
3288 * @param value The value of the const node.
3291 : node(new ConstNode<double>(value))
3301 operator+(Temp l, Temp r)
3303 return Temp(std::make_shared<BinaryNode<std::plus<Result> > >(l, r));
3307 operator-(Temp l, Temp r)
3309 return Temp(std::make_shared<BinaryNode<std::minus<Result> > >(l, r));
3313 operator*(Temp l, Temp r)
3315 return Temp(std::make_shared<BinaryNode<std::multiplies<Result> > >(l, r));
3319 operator/(Temp l, Temp r)
3321 return Temp(std::make_shared<BinaryNode<std::divides<Result> > >(l, r));
3327 return Temp(std::make_shared<UnaryNode<std::negate<Result> > >(l));
3330 template <typename T>
3334 return Temp(std::make_shared<ConstNode<T> >(val));
3337 template <typename T>
3339 constantVector(T val)
3341 return Temp(std::make_shared<ConstVectorNode<T> >(val));
3347 return Temp(std::make_shared<SumNode<std::plus<Result> > >(val));
3350 /** Dump all statistics data to the registered outputs */
3357 * Register reset and dump handlers. These are the functions which
3358 * will actually perform the whole statistics reset/dump actions
3359 * including processing the reset/dump callbacks
3361 typedef void (*Handler)();
3363 void registerHandlers(Handler reset_handler, Handler dump_handler);
3366 * Register a callback that should be called whenever statistics are
3369 void registerResetCallback(Callback *cb);
3372 * Register a callback that should be called whenever statistics are
3373 * about to be dumped
3375 void registerDumpCallback(Callback *cb);
3378 * Process all the callbacks in the reset callbacks queue
3380 void processResetQueue();
3383 * Process all the callbacks in the dump callbacks queue
3385 void processDumpQueue();
3387 std::list<Info *> &statsList();
3389 typedef std::map<const void *, Info *> MapType;
3390 MapType &statsMap();
3392 typedef std::map<std::string, Info *> NameMapType;
3393 NameMapType &nameMap();
3395 bool validateStatName(const std::string &name);
3397 } // namespace Stats
3399 void debugDumpStats();
3401 #endif // __BASE_STATISTICS_HH__