2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
32 * Declaration of Statistics objects.
38 * Generalized N-dimensinal vector
42 * -- these both can use the same function that prints out a
43 * specific set of stats
44 * VectorStandardDeviation totals
47 #ifndef __BASE_STATISTICS_HH__
48 #define __BASE_STATISTICS_HH__
62 #include "base/cast.hh"
63 #include "base/cprintf.hh"
64 #include "base/intmath.hh"
65 #include "base/refcnt.hh"
66 #include "base/stats/info.hh"
67 #include "base/stats/types.hh"
68 #include "base/stats/visit.hh"
69 #include "base/str.hh"
70 #include "base/types.hh"
74 /** The current simulated tick. */
77 /* A namespace for all of the Statistics */
80 template <class Stat, class Base>
81 class InfoProxy : public Base
87 InfoProxy(Stat &stat) : s(stat) {}
89 bool check() const { return s.check(); }
90 void prepare() { s.prepare(); }
91 void reset() { s.reset(); }
95 visitor.visit(*static_cast<Base *>(this));
97 bool zero() const { return s.zero(); }
100 template <class Stat>
101 class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
104 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
106 Counter value() const { return this->s.value(); }
107 Result result() const { return this->s.result(); }
108 Result total() const { return this->s.total(); }
111 template <class Stat>
112 class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
115 mutable VCounter cvec;
116 mutable VResult rvec;
119 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
121 size_type size() const { return this->s.size(); }
133 this->s.result(rvec);
137 Result total() const { return this->s.total(); }
140 template <class Stat>
141 class DistInfoProxy : public InfoProxy<Stat, DistInfo>
144 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
147 template <class Stat>
148 class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
151 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
153 size_type size() const { return this->s.size(); }
156 template <class Stat>
157 class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
160 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
165 virtual ~StorageParams();
171 /** Set up an info class for this statistic */
172 void setInfo(Info *info);
173 /** Save Storage class parameters if any */
174 void setParams(const StorageParams *params);
175 /** Save Storage class parameters if any */
178 /** Grab the information class for this statistic */
180 /** Grab the information class for this statistic */
181 const Info *info() const;
185 * Reset the stat to the default state.
190 * @return true if this stat has a value and satisfies its
191 * requirement as a prereq
193 bool zero() const { return true; }
196 * Check that this stat has been set up properly and is ready for
198 * @return true for success
200 bool check() const { return true; }
203 template <class Derived, template <class> class InfoProxyType>
204 class DataWrap : public InfoAccess
207 typedef InfoProxyType<Derived> Info;
210 Derived &self() { return *static_cast<Derived *>(this); }
216 return safe_cast<Info *>(InfoAccess::info());
223 return safe_cast<const Info *>(InfoAccess::info());
228 * Copy constructor, copies are not allowed.
230 DataWrap(const DataWrap &stat);
235 void operator=(const DataWrap &);
240 this->setInfo(new Info(self()));
244 * Set the name and marks this stat to print at the end of simulation.
245 * @param name The new name.
246 * @return A reference to this stat.
249 name(const std::string &name)
251 Info *info = this->info();
253 info->flags.set(display);
256 const std::string &name() const { return this->info()->name; }
259 * Set the description and marks this stat to print at the end of
261 * @param desc The new description.
262 * @return A reference to this stat.
265 desc(const std::string &_desc)
267 this->info()->desc = _desc;
272 * Set the precision and marks this stat to print at the end of simulation.
273 * @param _precision The new precision
274 * @return A reference to this stat.
277 precision(int _precision)
279 this->info()->precision = _precision;
284 * Set the flags and marks this stat to print at the end of simulation.
285 * @param f The new flags.
286 * @return A reference to this stat.
291 this->info()->flags.set(_flags);
296 * Set the prerequisite stat and marks this stat to print at the end of
298 * @param prereq The prerequisite stat.
299 * @return A reference to this stat.
301 template <class Stat>
303 prereq(const Stat &prereq)
305 this->info()->prereq = prereq.info();
310 template <class Derived, template <class> class InfoProxyType>
311 class DataWrapVec : public DataWrap<Derived, InfoProxyType>
314 typedef InfoProxyType<Derived> Info;
316 // The following functions are specific to vectors. If you use them
317 // in a non vector context, you will get a nice compiler error!
320 * Set the subfield name for the given index, and marks this stat to print
321 * at the end of simulation.
322 * @param index The subfield index.
323 * @param name The new name of the subfield.
324 * @return A reference to this stat.
327 subname(off_type index, const std::string &name)
329 Derived &self = this->self();
330 Info *info = self.info();
332 std::vector<std::string> &subn = info->subnames;
333 if (subn.size() <= index)
334 subn.resize(index + 1);
339 // The following functions are specific to 2d vectors. If you use
340 // them in a non vector context, you will get a nice compiler
341 // error because info doesn't have the right variables.
344 * Set the subfield description for the given index and marks this stat to
345 * print at the end of simulation.
346 * @param index The subfield index.
347 * @param desc The new description of the subfield
348 * @return A reference to this stat.
351 subdesc(off_type index, const std::string &desc)
353 Info *info = this->info();
355 std::vector<std::string> &subd = info->subdescs;
356 if (subd.size() <= index)
357 subd.resize(index + 1);
366 Derived &self = this->self();
367 Info *info = this->info();
369 size_t size = self.size();
370 for (off_type i = 0; i < size; ++i)
371 self.data(i)->prepare(info);
377 Derived &self = this->self();
378 Info *info = this->info();
380 size_t size = self.size();
381 for (off_type i = 0; i < size; ++i)
382 self.data(i)->reset(info);
386 template <class Derived, template <class> class InfoProxyType>
387 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
390 typedef InfoProxyType<Derived> Info;
393 * @warning This makes the assumption that if you're gonna subnames a 2d
394 * vector, you're subnaming across all y
397 ysubnames(const char **names)
399 Derived &self = this->self();
400 Info *info = this->info();
402 info->y_subnames.resize(self.y);
403 for (off_type i = 0; i < self.y; ++i)
404 info->y_subnames[i] = names[i];
409 ysubname(off_type index, const std::string &subname)
411 Derived &self = this->self();
412 Info *info = this->info();
414 assert(index < self.y);
415 info->y_subnames.resize(self.y);
416 info->y_subnames[index] = subname.c_str();
421 ysubname(off_type i) const
423 return this->info()->y_subnames[i];
428 //////////////////////////////////////////////////////////////////////
432 //////////////////////////////////////////////////////////////////////
435 * Templatized storage and interface for a simple scalar stat.
440 /** The statistic value. */
444 struct Params : public StorageParams {};
448 * Builds this storage element and calls the base constructor of the
456 * The the stat to the given value.
457 * @param val The new value.
459 void set(Counter val) { data = val; }
461 * Increment the stat by the given value.
462 * @param val The new value.
464 void inc(Counter val) { data += val; }
466 * Decrement the stat by the given value.
467 * @param val The new value.
469 void dec(Counter val) { data -= val; }
471 * Return the value of this stat as its base type.
472 * @return The value of this stat.
474 Counter value() const { return data; }
476 * Return the value of this stat as a result type.
477 * @return The value of this stat.
479 Result result() const { return (Result)data; }
481 * Prepare stat data for dumping or serialization
483 void prepare(Info *info) { }
485 * Reset stat value to default
487 void reset(Info *info) { data = Counter(); }
490 * @return true if zero value
492 bool zero() const { return data == Counter(); }
496 * Templatized storage and interface to a per-tick average stat. This keeps
497 * a current count and updates a total (count * ticks) when this count
498 * changes. This allows the quick calculation of a per tick count of the item
499 * being watched. This is good for keeping track of residencies in structures
500 * among other things.
505 /** The current count. */
507 /** The tick of the last reset */
509 /** The total count for all tick. */
510 mutable Result total;
511 /** The tick that current last changed. */
515 struct Params : public StorageParams {};
519 * Build and initializes this stat storage.
522 : current(0), lastReset(0), total(0), last(0)
526 * Set the current count to the one provided, update the total and last
528 * @param val The new count.
533 total += current * (curTick - last);
539 * Increment the current count by the provided value, calls set.
540 * @param val The amount to increment.
542 void inc(Counter val) { set(current + val); }
545 * Deccrement the current count by the provided value, calls set.
546 * @param val The amount to decrement.
548 void dec(Counter val) { set(current - val); }
551 * Return the current count.
552 * @return The current count.
554 Counter value() const { return current; }
557 * Return the current average.
558 * @return The current average.
563 assert(last == curTick);
564 return (Result)(total + current) / (Result)(curTick - lastReset + 1);
568 * @return true if zero value
570 bool zero() const { return total == 0.0; }
573 * Prepare stat data for dumping or serialization
578 total += current * (curTick - last);
583 * Reset stat value to default
596 * Implementation of a scalar stat. The type of stat is determined by the
599 template <class Derived, class Stor>
600 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
603 typedef Stor Storage;
604 typedef typename Stor::Params Params;
607 /** The storage of this stat. */
608 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
612 * Retrieve the storage.
613 * @param index The vector index to access.
614 * @return The storage object at the given index.
619 return reinterpret_cast<Storage *>(storage);
623 * Retrieve a const pointer to the storage.
624 * for the given index.
625 * @param index The vector index to access.
626 * @return A const pointer to the storage object at the given index.
631 return reinterpret_cast<const Storage *>(storage);
637 new (storage) Storage(this->info());
643 * Return the current value of this stat as its base type.
644 * @return The current value.
646 Counter value() const { return data()->value(); }
655 // Common operators for stats
657 * Increment the stat by 1. This calls the associated storage object inc
660 void operator++() { data()->inc(1); }
662 * Decrement the stat by 1. This calls the associated storage object dec
665 void operator--() { data()->dec(1); }
667 /** Increment the stat by 1. */
668 void operator++(int) { ++*this; }
669 /** Decrement the stat by 1. */
670 void operator--(int) { --*this; }
673 * Set the data value to the given value. This calls the associated storage
674 * object set function.
675 * @param v The new value.
677 template <typename U>
678 void operator=(const U &v) { data()->set(v); }
681 * Increment the stat by the given value. This calls the associated
682 * storage object inc function.
683 * @param v The value to add.
685 template <typename U>
686 void operator+=(const U &v) { data()->inc(v); }
689 * Decrement the stat by the given value. This calls the associated
690 * storage object dec function.
691 * @param v The value to substract.
693 template <typename U>
694 void operator-=(const U &v) { data()->dec(v); }
697 * Return the number of elements, always 1 for a scalar.
700 size_type size() const { return 1; }
702 Counter value() { return data()->value(); }
704 Result result() { return data()->result(); }
706 Result total() { return result(); }
708 bool zero() { return result() == 0.0; }
710 void reset() { data()->reset(this->info()); }
711 void prepare() { data()->prepare(this->info()); }
714 class ProxyInfo : public ScalarInfo
717 std::string str() const { return to_string(value()); }
718 size_type size() const { return 1; }
719 bool check() const { return true; }
722 bool zero() const { return value() == 0; }
724 void visit(Visit &visitor) { visitor.visit(*this); }
728 class ValueProxy : public ProxyInfo
734 ValueProxy(T &val) : scalar(&val) {}
735 Counter value() const { return *scalar; }
736 Result result() const { return *scalar; }
737 Result total() const { return *scalar; }
741 class FunctorProxy : public ProxyInfo
747 FunctorProxy(T &func) : functor(&func) {}
748 Counter value() const { return (*functor)(); }
749 Result result() const { return (*functor)(); }
750 Result total() const { return (*functor)(); }
753 template <class Derived>
754 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
760 ValueBase() : proxy(NULL) { }
761 ~ValueBase() { if (proxy) delete proxy; }
767 proxy = new ValueProxy<T>(value);
776 proxy = new FunctorProxy<T>(func);
781 Counter value() { return proxy->value(); }
782 Result result() const { return proxy->result(); }
783 Result total() const { return proxy->total(); };
784 size_type size() const { return proxy->size(); }
786 std::string str() const { return proxy->str(); }
787 bool zero() const { return proxy->zero(); }
788 bool check() const { return proxy != NULL; }
793 //////////////////////////////////////////////////////////////////////
797 //////////////////////////////////////////////////////////////////////
800 * A proxy class to access the stat at a given index in a VectorBase stat.
801 * Behaves like a ScalarBase.
803 template <class Stat>
807 /** Pointer to the parent Vector. */
810 /** The index to access in the parent VectorBase. */
815 * Return the current value of this stat as its base type.
816 * @return The current value.
818 Counter value() const { return stat.data(index)->value(); }
821 * Return the current value of this statas a result type.
822 * @return The current value.
824 Result result() const { return stat.data(index)->result(); }
828 * Create and initialize this proxy, do not register it with the database.
829 * @param i The index to access.
831 ScalarProxy(Stat &s, off_type i)
837 * Create a copy of the provided ScalarProxy.
838 * @param sp The proxy to copy.
840 ScalarProxy(const ScalarProxy &sp)
841 : stat(sp.stat), index(sp.index)
845 * Set this proxy equal to the provided one.
846 * @param sp The proxy to copy.
847 * @return A reference to this proxy.
850 operator=(const ScalarProxy &sp)
858 // Common operators for stats
860 * Increment the stat by 1. This calls the associated storage object inc
863 void operator++() { stat.data(index)->inc(1); }
865 * Decrement the stat by 1. This calls the associated storage object dec
868 void operator--() { stat.data(index)->dec(1); }
870 /** Increment the stat by 1. */
871 void operator++(int) { ++*this; }
872 /** Decrement the stat by 1. */
873 void operator--(int) { --*this; }
876 * Set the data value to the given value. This calls the associated storage
877 * object set function.
878 * @param v The new value.
880 template <typename U>
882 operator=(const U &v)
884 stat.data(index)->set(v);
888 * Increment the stat by the given value. This calls the associated
889 * storage object inc function.
890 * @param v The value to add.
892 template <typename U>
894 operator+=(const U &v)
896 stat.data(index)->inc(v);
900 * Decrement the stat by the given value. This calls the associated
901 * storage object dec function.
902 * @param v The value to substract.
904 template <typename U>
906 operator-=(const U &v)
908 stat.data(index)->dec(v);
912 * Return the number of elements, always 1 for a scalar.
915 size_type size() const { return 1; }
921 return csprintf("%s[%d]", stat.info()->name, index);
926 * Implementation of a vector of stats. The type of stat is determined by the
927 * Storage class. @sa ScalarBase
929 template <class Derived, class Stor>
930 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
933 typedef Stor Storage;
934 typedef typename Stor::Params Params;
937 typedef ScalarProxy<Derived> Proxy;
938 friend class ScalarProxy<Derived>;
939 friend class DataWrapVec<Derived, VectorInfoProxy>;
942 /** The storage of this stat. */
948 * Retrieve the storage.
949 * @param index The vector index to access.
950 * @return The storage object at the given index.
952 Storage *data(off_type index) { return &storage[index]; }
955 * Retrieve a const pointer to the storage.
956 * @param index The vector index to access.
957 * @return A const pointer to the storage object at the given index.
959 const Storage *data(off_type index) const { return &storage[index]; }
964 assert(s > 0 && "size must be positive!");
965 assert(!storage && "already initialized");
968 char *ptr = new char[_size * sizeof(Storage)];
969 storage = reinterpret_cast<Storage *>(ptr);
971 for (off_type i = 0; i < _size; ++i)
972 new (&storage[i]) Storage(this->info());
979 value(VCounter &vec) const
982 for (off_type i = 0; i < size(); ++i)
983 vec[i] = data(i)->value();
987 * Copy the values to a local vector and return a reference to it.
988 * @return A reference to a vector of the stat values.
991 result(VResult &vec) const
994 for (off_type i = 0; i < size(); ++i)
995 vec[i] = data(i)->result();
999 * Return a total of all entries in this vector.
1000 * @return The total of all vector entries.
1006 for (off_type i = 0; i < size(); ++i)
1007 total += data(i)->result();
1012 * @return the number of elements in this vector.
1014 size_type size() const { return _size; }
1019 for (off_type i = 0; i < size(); ++i)
1020 if (data(i)->zero())
1028 return storage != NULL;
1041 for (off_type i = 0; i < _size; ++i)
1042 data(i)->~Storage();
1043 delete [] reinterpret_cast<char *>(storage);
1047 * Set this vector to have the given size.
1048 * @param size The new size.
1049 * @return A reference to this stat.
1052 init(size_type size)
1054 Derived &self = this->self();
1060 * Return a reference (ScalarProxy) to the stat at the given index.
1061 * @param index The vector index to access.
1062 * @return A reference of the stat.
1065 operator[](off_type index)
1067 assert (index >= 0 && index < size());
1068 return Proxy(this->self(), index);
1072 template <class Stat>
1081 mutable VResult vec;
1083 typename Stat::Storage *
1084 data(off_type index)
1086 assert(index < len);
1087 return stat.data(offset + index);
1090 const typename Stat::Storage *
1091 data(off_type index) const
1093 assert(index < len);
1094 return stat.data(offset + index);
1103 for (off_type i = 0; i < size(); ++i)
1104 vec[i] = data(i)->result();
1113 for (off_type i = 0; i < size(); ++i)
1114 total += data(i)->result();
1119 VectorProxy(Stat &s, off_type o, size_type l)
1120 : stat(s), offset(o), len(l)
1124 VectorProxy(const VectorProxy &sp)
1125 : stat(sp.stat), offset(sp.offset), len(sp.len)
1130 operator=(const VectorProxy &sp)
1139 operator[](off_type index)
1141 assert (index >= 0 && index < size());
1142 return ScalarProxy<Stat>(stat, offset + index);
1145 size_type size() const { return len; }
1148 template <class Derived, class Stor>
1149 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1152 typedef Vector2dInfoProxy<Derived> Info;
1153 typedef Stor Storage;
1154 typedef typename Stor::Params Params;
1155 typedef VectorProxy<Derived> Proxy;
1156 friend class ScalarProxy<Derived>;
1157 friend class VectorProxy<Derived>;
1158 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1159 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1168 Storage *data(off_type index) { return &storage[index]; }
1169 const Storage *data(off_type index) const { return &storage[index]; }
1181 for (off_type i = 0; i < _size; ++i)
1182 data(i)->~Storage();
1183 delete [] reinterpret_cast<char *>(storage);
1187 init(size_type _x, size_type _y)
1189 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1190 assert(!storage && "already initialized");
1192 Derived &self = this->self();
1193 Info *info = this->info();
1201 char *ptr = new char[_size * sizeof(Storage)];
1202 storage = reinterpret_cast<Storage *>(ptr);
1204 for (off_type i = 0; i < _size; ++i)
1205 new (&storage[i]) Storage(info);
1213 operator[](off_type index)
1215 off_type offset = index * y;
1216 assert (index >= 0 && offset + index < size());
1217 return Proxy(this->self(), offset, y);
1230 return data(0)->zero();
1232 for (off_type i = 0; i < size(); ++i)
1233 if (!data(i)->zero())
1242 Info *info = this->info();
1243 size_type size = this->size();
1245 for (off_type i = 0; i < size; ++i)
1246 data(i)->prepare(info);
1248 info->cvec.resize(size);
1249 for (off_type i = 0; i < size; ++i)
1250 info->cvec[i] = data(i)->value();
1254 * Reset stat value to default
1259 Info *info = this->info();
1260 size_type size = this->size();
1261 for (off_type i = 0; i < size; ++i)
1262 data(i)->reset(info);
1268 return storage != NULL;
1272 //////////////////////////////////////////////////////////////////////
1274 // Non formula statistics
1276 //////////////////////////////////////////////////////////////////////
1277 /** The parameters for a distribution stat. */
1278 struct DistParams : public StorageParams
1280 const DistType type;
1281 DistParams(DistType t) : type(t) {}
1285 * Templatized storage and interface for a distrbution stat.
1290 /** The parameters for a distribution stat. */
1291 struct Params : public DistParams
1293 /** The minimum value to track. */
1295 /** The maximum value to track. */
1297 /** The number of entries in each bucket. */
1298 Counter bucket_size;
1299 /** The number of buckets. Equal to (max-min)/bucket_size. */
1302 Params() : DistParams(Dist) {}
1306 /** The minimum value to track. */
1308 /** The maximum value to track. */
1310 /** The number of entries in each bucket. */
1311 Counter bucket_size;
1312 /** The number of buckets. Equal to (max-min)/bucket_size. */
1315 /** The smallest value sampled. */
1317 /** The largest value sampled. */
1319 /** The number of values sampled less than min. */
1321 /** The number of values sampled more than max. */
1323 /** The current sum. */
1325 /** The sum of squares. */
1327 /** The number of samples. */
1329 /** Counter for each bucket. */
1333 DistStor(Info *info)
1334 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1340 * Add a value to the distribution for the given number of times.
1341 * @param val The value to add.
1342 * @param number The number of times to add the value.
1345 sample(Counter val, int number)
1347 if (val < min_track)
1348 underflow += number;
1349 else if (val > max_track)
1353 (size_type)std::floor((val - min_track) / bucket_size);
1354 assert(index < size());
1355 cvec[index] += number;
1364 Counter sample = val * number;
1366 squares += sample * sample;
1371 * Return the number of buckets in this distribution.
1372 * @return the number of buckets.
1374 size_type size() const { return cvec.size(); }
1377 * Returns true if any calls to sample have been made.
1378 * @return True if any values have been sampled.
1383 return samples == Counter();
1387 prepare(Info *info, DistData &data)
1389 const Params *params = safe_cast<const Params *>(info->storageParams);
1391 assert(params->type == Dist);
1392 data.type = params->type;
1393 data.min = params->min;
1394 data.max = params->max;
1395 data.bucket_size = params->bucket_size;
1397 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1398 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1399 data.underflow = underflow;
1400 data.overflow = overflow;
1402 size_type buckets = params->buckets;
1403 data.cvec.resize(buckets);
1404 for (off_type i = 0; i < buckets; ++i)
1405 data.cvec[i] = cvec[i];
1408 data.squares = squares;
1409 data.samples = samples;
1413 * Reset stat value to default
1418 const Params *params = safe_cast<const Params *>(info->storageParams);
1419 min_track = params->min;
1420 max_track = params->max;
1421 bucket_size = params->bucket_size;
1423 min_val = CounterLimits::max();
1424 max_val = CounterLimits::min();
1425 underflow = Counter();
1426 overflow = Counter();
1428 size_type size = cvec.size();
1429 for (off_type i = 0; i < size; ++i)
1430 cvec[i] = Counter();
1433 squares = Counter();
1434 samples = Counter();
1439 * Templatized storage and interface for a distribution that calculates mean
1445 struct Params : public DistParams
1447 Params() : DistParams(Deviation) {}
1451 /** The current sum. */
1453 /** The sum of squares. */
1455 /** The number of samples. */
1460 * Create and initialize this storage.
1462 SampleStor(Info *info)
1463 : sum(Counter()), squares(Counter()), samples(Counter())
1467 * Add a value the given number of times to this running average.
1468 * Update the running sum and sum of squares, increment the number of
1469 * values seen by the given number.
1470 * @param val The value to add.
1471 * @param number The number of times to add the value.
1474 sample(Counter val, int number)
1476 Counter value = val * number;
1478 squares += value * value;
1483 * Return the number of entries in this stat, 1
1486 size_type size() const { return 1; }
1489 * Return true if no samples have been added.
1490 * @return True if no samples have been added.
1492 bool zero() const { return samples == Counter(); }
1495 prepare(Info *info, DistData &data)
1497 const Params *params = safe_cast<const Params *>(info->storageParams);
1499 assert(params->type == Deviation);
1500 data.type = params->type;
1502 data.squares = squares;
1503 data.samples = samples;
1507 * Reset stat value to default
1513 squares = Counter();
1514 samples = Counter();
1519 * Templatized storage for distribution that calculates per tick mean and
1525 struct Params : public DistParams
1527 Params() : DistParams(Deviation) {}
1531 /** Current total. */
1533 /** Current sum of squares. */
1538 * Create and initialize this storage.
1540 AvgSampleStor(Info *info)
1541 : sum(Counter()), squares(Counter())
1545 * Add a value to the distribution for the given number of times.
1546 * Update the running sum and sum of squares.
1547 * @param val The value to add.
1548 * @param number The number of times to add the value.
1551 sample(Counter val, int number)
1553 Counter value = val * number;
1555 squares += value * value;
1559 * Return the number of entries, in this case 1.
1562 size_type size() const { return 1; }
1565 * Return true if no samples have been added.
1566 * @return True if the sum is zero.
1568 bool zero() const { return sum == Counter(); }
1571 prepare(Info *info, DistData &data)
1573 const Params *params = safe_cast<const Params *>(info->storageParams);
1575 assert(params->type == Deviation);
1576 data.type = params->type;
1578 data.squares = squares;
1579 data.samples = curTick;
1583 * Reset stat value to default
1589 squares = Counter();
1594 * Implementation of a distribution stat. The type of distribution is
1595 * determined by the Storage template. @sa ScalarBase
1597 template <class Derived, class Stor>
1598 class DistBase : public DataWrap<Derived, DistInfoProxy>
1601 typedef DistInfoProxy<Derived> Info;
1602 typedef Stor Storage;
1603 typedef typename Stor::Params Params;
1606 /** The storage for this stat. */
1607 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1611 * Retrieve the storage.
1612 * @return The storage object for this stat.
1617 return reinterpret_cast<Storage *>(storage);
1621 * Retrieve a const pointer to the storage.
1622 * @return A const pointer to the storage object for this stat.
1627 return reinterpret_cast<const Storage *>(storage);
1633 new (storage) Storage(this->info());
1641 * Add a value to the distribtion n times. Calls sample on the storage
1643 * @param v The value to add.
1644 * @param n The number of times to add it, defaults to 1.
1646 template <typename U>
1647 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1650 * Return the number of entries in this stat.
1651 * @return The number of entries.
1653 size_type size() const { return data()->size(); }
1655 * Return true if no samples have been added.
1656 * @return True if there haven't been any samples.
1658 bool zero() const { return data()->zero(); }
1663 Info *info = this->info();
1664 data()->prepare(info, info->data);
1668 * Reset stat value to default
1673 data()->reset(this->info());
1677 template <class Stat>
1680 template <class Derived, class Stor>
1681 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1684 typedef VectorDistInfoProxy<Derived> Info;
1685 typedef Stor Storage;
1686 typedef typename Stor::Params Params;
1687 typedef DistProxy<Derived> Proxy;
1688 friend class DistProxy<Derived>;
1689 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1697 data(off_type index)
1699 return &storage[index];
1703 data(off_type index) const
1705 return &storage[index];
1711 assert(s > 0 && "size must be positive!");
1712 assert(!storage && "already initialized");
1715 char *ptr = new char[_size * sizeof(Storage)];
1716 storage = reinterpret_cast<Storage *>(ptr);
1718 Info *info = this->info();
1719 for (off_type i = 0; i < _size; ++i)
1720 new (&storage[i]) Storage(info);
1735 for (off_type i = 0; i < _size; ++i)
1736 data(i)->~Storage();
1737 delete [] reinterpret_cast<char *>(storage);
1740 Proxy operator[](off_type index)
1742 assert(index >= 0 && index < size());
1743 return Proxy(this->self(), index);
1755 for (off_type i = 0; i < size(); ++i)
1756 if (!data(i)->zero())
1764 Info *info = this->info();
1765 size_type size = this->size();
1766 info->data.resize(size);
1767 for (off_type i = 0; i < size; ++i)
1768 data(i)->prepare(info, info->data[i]);
1774 return storage != NULL;
1778 template <class Stat>
1786 typename Stat::Storage *data() { return stat.data(index); }
1787 const typename Stat::Storage *data() const { return stat.data(index); }
1790 DistProxy(Stat &s, off_type i)
1794 DistProxy(const DistProxy &sp)
1795 : stat(sp.stat), index(sp.index)
1799 operator=(const DistProxy &sp)
1807 template <typename U>
1809 sample(const U &v, int n = 1)
1811 data()->sample(v, n);
1823 return data()->zero();
1827 * Proxy has no state. Nothing to reset.
1832 //////////////////////////////////////////////////////////////////////
1836 //////////////////////////////////////////////////////////////////////
1839 * Base class for formula statistic node. These nodes are used to build a tree
1840 * that represents the formula.
1842 class Node : public RefCounted
1846 * Return the number of nodes in the subtree starting at this node.
1847 * @return the number of nodes in this subtree.
1849 virtual size_type size() const = 0;
1851 * Return the result vector of this subtree.
1852 * @return The result vector of this subtree.
1854 virtual const VResult &result() const = 0;
1856 * Return the total of the result vector.
1857 * @return The total of the result vector.
1859 virtual Result total() const = 0;
1864 virtual std::string str() const = 0;
1867 /** Reference counting pointer to a function Node. */
1868 typedef RefCountingPtr<Node> NodePtr;
1870 class ScalarStatNode : public Node
1873 const ScalarInfo *data;
1874 mutable VResult vresult;
1877 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
1882 vresult[0] = data->result();
1886 Result total() const { return data->result(); };
1888 size_type size() const { return 1; }
1893 std::string str() const { return data->name; }
1896 template <class Stat>
1897 class ScalarProxyNode : public Node
1900 const ScalarProxy<Stat> proxy;
1901 mutable VResult vresult;
1904 ScalarProxyNode(const ScalarProxy<Stat> &p)
1905 : proxy(p), vresult(1)
1911 vresult[0] = proxy.result();
1918 return proxy.result();
1937 class VectorStatNode : public Node
1940 const VectorInfo *data;
1943 VectorStatNode(const VectorInfo *d) : data(d) { }
1944 const VResult &result() const { return data->result(); }
1945 Result total() const { return data->total(); };
1947 size_type size() const { return data->size(); }
1949 std::string str() const { return data->name; }
1953 class ConstNode : public Node
1959 ConstNode(T s) : vresult(1, (Result)s) {}
1960 const VResult &result() const { return vresult; }
1961 Result total() const { return vresult[0]; };
1962 size_type size() const { return 1; }
1963 std::string str() const { return to_string(vresult[0]); }
1967 class ConstVectorNode : public Node
1973 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
1974 const VResult &result() const { return vresult; }
1979 size_type size = this->size();
1981 for (off_type i = 0; i < size; i++)
1986 size_type size() const { return vresult.size(); }
1990 size_type size = this->size();
1991 std::string tmp = "(";
1992 for (off_type i = 0; i < size; i++)
1993 tmp += csprintf("%s ",to_string(vresult[i]));
2003 struct OpString<std::plus<Result> >
2005 static std::string str() { return "+"; }
2009 struct OpString<std::minus<Result> >
2011 static std::string str() { return "-"; }
2015 struct OpString<std::multiplies<Result> >
2017 static std::string str() { return "*"; }
2021 struct OpString<std::divides<Result> >
2023 static std::string str() { return "/"; }
2027 struct OpString<std::modulus<Result> >
2029 static std::string str() { return "%"; }
2033 struct OpString<std::negate<Result> >
2035 static std::string str() { return "-"; }
2039 class UnaryNode : public Node
2043 mutable VResult vresult;
2046 UnaryNode(NodePtr &p) : l(p) {}
2051 const VResult &lvec = l->result();
2052 size_type size = lvec.size();
2056 vresult.resize(size);
2058 for (off_type i = 0; i < size; ++i)
2059 vresult[i] = op(lvec[i]);
2067 const VResult &vec = this->result();
2069 for (off_type i = 0; i < size(); i++)
2074 size_type size() const { return l->size(); }
2079 return OpString<Op>::str() + l->str();
2084 class BinaryNode : public Node
2089 mutable VResult vresult;
2092 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2098 const VResult &lvec = l->result();
2099 const VResult &rvec = r->result();
2101 assert(lvec.size() > 0 && rvec.size() > 0);
2103 if (lvec.size() == 1 && rvec.size() == 1) {
2105 vresult[0] = op(lvec[0], rvec[0]);
2106 } else if (lvec.size() == 1) {
2107 size_type size = rvec.size();
2108 vresult.resize(size);
2109 for (off_type i = 0; i < size; ++i)
2110 vresult[i] = op(lvec[0], rvec[i]);
2111 } else if (rvec.size() == 1) {
2112 size_type size = lvec.size();
2113 vresult.resize(size);
2114 for (off_type i = 0; i < size; ++i)
2115 vresult[i] = op(lvec[i], rvec[0]);
2116 } else if (rvec.size() == lvec.size()) {
2117 size_type size = rvec.size();
2118 vresult.resize(size);
2119 for (off_type i = 0; i < size; ++i)
2120 vresult[i] = op(lvec[i], rvec[i]);
2129 const VResult &vec = this->result();
2131 for (off_type i = 0; i < size(); i++)
2139 size_type ls = l->size();
2140 size_type rs = r->size();
2143 } else if (rs == 1) {
2146 assert(ls == rs && "Node vector sizes are not equal");
2154 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2159 class SumNode : public Node
2163 mutable VResult vresult;
2166 SumNode(NodePtr &p) : l(p), vresult(1) {}
2171 const VResult &lvec = l->result();
2172 size_type size = lvec.size();
2178 for (off_type i = 0; i < size; ++i)
2179 vresult[0] = op(vresult[0], lvec[i]);
2187 const VResult &lvec = l->result();
2188 size_type size = lvec.size();
2191 Result vresult = 0.0;
2194 for (off_type i = 0; i < size; ++i)
2195 vresult = op(vresult, lvec[i]);
2200 size_type size() const { return 1; }
2205 return csprintf("total(%s)", l->str());
2210 //////////////////////////////////////////////////////////////////////
2212 // Visible Statistics Types
2214 //////////////////////////////////////////////////////////////////////
2216 * @defgroup VisibleStats "Statistic Types"
2217 * These are the statistics that are used in the simulator.
2222 * This is a simple scalar statistic, like a counter.
2223 * @sa Stat, ScalarBase, StatStor
2225 class Scalar : public ScalarBase<Scalar, StatStor>
2228 using ScalarBase<Scalar, StatStor>::operator=;
2232 * A stat that calculates the per tick average of a value.
2233 * @sa Stat, ScalarBase, AvgStor
2235 class Average : public ScalarBase<Average, AvgStor>
2238 using ScalarBase<Average, AvgStor>::operator=;
2241 class Value : public ValueBase<Value>
2246 * A vector of scalar stats.
2247 * @sa Stat, VectorBase, StatStor
2249 class Vector : public VectorBase<Vector, StatStor>
2254 * A vector of Average stats.
2255 * @sa Stat, VectorBase, AvgStor
2257 class AverageVector : public VectorBase<AverageVector, AvgStor>
2262 * A 2-Dimensional vecto of scalar stats.
2263 * @sa Stat, Vector2dBase, StatStor
2265 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2270 * A simple distribution stat.
2271 * @sa Stat, DistBase, DistStor
2273 class Distribution : public DistBase<Distribution, DistStor>
2277 * Set the parameters of this distribution. @sa DistStor::Params
2278 * @param min The minimum value of the distribution.
2279 * @param max The maximum value of the distribution.
2280 * @param bkt The number of values in each bucket.
2281 * @return A reference to this distribution.
2284 init(Counter min, Counter max, Counter bkt)
2286 DistStor::Params *params = new DistStor::Params;
2289 params->bucket_size = bkt;
2290 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2291 this->setParams(params);
2293 return this->self();
2298 * Calculates the mean and variance of all the samples.
2299 * @sa DistBase, SampleStor
2301 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2305 * Construct and initialize this distribution.
2309 SampleStor::Params *params = new SampleStor::Params;
2311 this->setParams(params);
2316 * Calculates the per tick mean and variance of the samples.
2317 * @sa DistBase, AvgSampleStor
2319 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2323 * Construct and initialize this distribution.
2327 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2329 this->setParams(params);
2334 * A vector of distributions.
2335 * @sa VectorDistBase, DistStor
2337 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2341 * Initialize storage and parameters for this distribution.
2342 * @param size The size of the vector (the number of distributions).
2343 * @param min The minimum value of the distribution.
2344 * @param max The maximum value of the distribution.
2345 * @param bkt The number of values in each bucket.
2346 * @return A reference to this distribution.
2348 VectorDistribution &
2349 init(size_type size, Counter min, Counter max, Counter bkt)
2351 DistStor::Params *params = new DistStor::Params;
2354 params->bucket_size = bkt;
2355 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2356 this->setParams(params);
2358 return this->self();
2363 * This is a vector of StandardDeviation stats.
2364 * @sa VectorDistBase, SampleStor
2366 class VectorStandardDeviation
2367 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2371 * Initialize storage for this distribution.
2372 * @param size The size of the vector.
2373 * @return A reference to this distribution.
2375 VectorStandardDeviation &
2376 init(size_type size)
2378 SampleStor::Params *params = new SampleStor::Params;
2380 this->setParams(params);
2381 return this->self();
2386 * This is a vector of AverageDeviation stats.
2387 * @sa VectorDistBase, AvgSampleStor
2389 class VectorAverageDeviation
2390 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2394 * Initialize storage for this distribution.
2395 * @param size The size of the vector.
2396 * @return A reference to this distribution.
2398 VectorAverageDeviation &
2399 init(size_type size)
2401 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2403 this->setParams(params);
2404 return this->self();
2408 template <class Stat>
2409 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2412 mutable VResult vec;
2413 mutable VCounter cvec;
2416 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2418 size_type size() const { return this->s.size(); }
2423 this->s.result(vec);
2426 Result total() const { return this->s.total(); }
2427 VCounter &value() const { return cvec; }
2429 std::string str() const { return this->s.str(); }
2434 * A formula for statistics that is calculated when printed. A formula is
2435 * stored as a tree of Nodes that represent the equation to calculate.
2436 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2438 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2441 /** The root of the tree which represents the Formula */
2447 * Create and initialize thie formula, and register it with the database.
2452 * Create a formula with the given root node, register it with the
2454 * @param r The root of the expression tree.
2459 * Set an unitialized Formula to the given root.
2460 * @param r The root of the expression tree.
2461 * @return a reference to this formula.
2463 const Formula &operator=(Temp r);
2466 * Add the given tree to the existing one.
2467 * @param r The root of the expression tree.
2468 * @return a reference to this formula.
2470 const Formula &operator+=(Temp r);
2472 * Return the result of the Fomula in a vector. If there were no Vector
2473 * components to the Formula, then the vector is size 1. If there were,
2474 * like x/y with x being a vector of size 3, then the result returned will
2475 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2476 * @return The result vector.
2478 void result(VResult &vec) const;
2481 * Return the total Formula result. If there is a Vector
2482 * component to this Formula, then this is the result of the
2483 * Formula if the formula is applied after summing all the
2484 * components of the Vector. For example, if Formula is x/y where
2485 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2486 * there is no Vector component, total() returns the same value as
2487 * the first entry in the VResult val() returns.
2488 * @return The total of the result vector.
2490 Result total() const;
2493 * Return the number of elements in the tree.
2495 size_type size() const;
2500 * Formulas don't need to be reset
2509 std::string str() const;
2512 class FormulaNode : public Node
2515 const Formula &formula;
2516 mutable VResult vec;
2519 FormulaNode(const Formula &f) : formula(f) {}
2521 size_type size() const { return formula.size(); }
2522 const VResult &result() const { formula.result(vec); return vec; }
2523 Result total() const { return formula.total(); }
2525 std::string str() const { return formula.str(); }
2529 * Helper class to construct formula node trees.
2535 * Pointer to a Node object.
2541 * Copy the given pointer to this class.
2542 * @param n A pointer to a Node object to copy.
2544 Temp(NodePtr n) : node(n) { }
2547 * Return the node pointer.
2548 * @return the node pointer.
2550 operator NodePtr&() { return node; }
2554 * Create a new ScalarStatNode.
2555 * @param s The ScalarStat to place in a node.
2557 Temp(const Scalar &s)
2558 : node(new ScalarStatNode(s.info()))
2562 * Create a new ScalarStatNode.
2563 * @param s The ScalarStat to place in a node.
2565 Temp(const Value &s)
2566 : node(new ScalarStatNode(s.info()))
2570 * Create a new ScalarStatNode.
2571 * @param s The ScalarStat to place in a node.
2573 Temp(const Average &s)
2574 : node(new ScalarStatNode(s.info()))
2578 * Create a new VectorStatNode.
2579 * @param s The VectorStat to place in a node.
2581 Temp(const Vector &s)
2582 : node(new VectorStatNode(s.info()))
2585 Temp(const AverageVector &s)
2586 : node(new VectorStatNode(s.info()))
2592 Temp(const Formula &f)
2593 : node(new FormulaNode(f))
2597 * Create a new ScalarProxyNode.
2598 * @param p The ScalarProxy to place in a node.
2600 template <class Stat>
2601 Temp(const ScalarProxy<Stat> &p)
2602 : node(new ScalarProxyNode<Stat>(p))
2606 * Create a ConstNode
2607 * @param value The value of the const node.
2609 Temp(signed char value)
2610 : node(new ConstNode<signed char>(value))
2614 * Create a ConstNode
2615 * @param value The value of the const node.
2617 Temp(unsigned char value)
2618 : node(new ConstNode<unsigned char>(value))
2622 * Create a ConstNode
2623 * @param value The value of the const node.
2625 Temp(signed short value)
2626 : node(new ConstNode<signed short>(value))
2630 * Create a ConstNode
2631 * @param value The value of the const node.
2633 Temp(unsigned short value)
2634 : node(new ConstNode<unsigned short>(value))
2638 * Create a ConstNode
2639 * @param value The value of the const node.
2641 Temp(signed int value)
2642 : node(new ConstNode<signed int>(value))
2646 * Create a ConstNode
2647 * @param value The value of the const node.
2649 Temp(unsigned int value)
2650 : node(new ConstNode<unsigned int>(value))
2654 * Create a ConstNode
2655 * @param value The value of the const node.
2657 Temp(signed long value)
2658 : node(new ConstNode<signed long>(value))
2662 * Create a ConstNode
2663 * @param value The value of the const node.
2665 Temp(unsigned long value)
2666 : node(new ConstNode<unsigned long>(value))
2670 * Create a ConstNode
2671 * @param value The value of the const node.
2673 Temp(signed long long value)
2674 : node(new ConstNode<signed long long>(value))
2678 * Create a ConstNode
2679 * @param value The value of the const node.
2681 Temp(unsigned long long value)
2682 : node(new ConstNode<unsigned long long>(value))
2686 * Create a ConstNode
2687 * @param value The value of the const node.
2690 : node(new ConstNode<float>(value))
2694 * Create a ConstNode
2695 * @param value The value of the const node.
2698 : node(new ConstNode<double>(value))
2708 operator+(Temp l, Temp r)
2710 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
2714 operator-(Temp l, Temp r)
2716 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
2720 operator*(Temp l, Temp r)
2722 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
2726 operator/(Temp l, Temp r)
2728 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
2734 return NodePtr(new UnaryNode<std::negate<Result> >(l));
2737 template <typename T>
2741 return NodePtr(new ConstNode<T>(val));
2744 template <typename T>
2746 constantVector(T val)
2748 return NodePtr(new ConstVectorNode<T>(val));
2754 return NodePtr(new SumNode<std::plus<Result> >(val));
2758 * Enable the statistics package. Before the statistics package is
2759 * enabled, all statistics must be created and initialized and once
2760 * the package is enabled, no more statistics can be created.
2765 * Prepare all stats for data access. This must be done before
2766 * dumping and serialization.
2771 * Dump all statistics data to the registered outputs
2776 * Reset all statistics to the base state
2780 * Register a callback that should be called whenever statistics are
2783 void registerResetCallback(Callback *cb);
2785 std::list<Info *> &statsList();
2787 /* namespace Stats */ }
2789 #endif // __BASE_STATISTICS_HH__