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) {}
166 /** Set up an info class for this statistic */
167 void setInfo(Info *info);
168 /** Save Storage class parameters if any */
169 void setParams(const StorageParams *params);
170 /** Save Storage class parameters if any */
173 /** Grab the information class for this statistic */
175 /** Grab the information class for this statistic */
176 const Info *info() const;
180 * Reset the stat to the default state.
185 * @return true if this stat has a value and satisfies its
186 * requirement as a prereq
188 bool zero() const { return true; }
191 * Check that this stat has been set up properly and is ready for
193 * @return true for success
195 bool check() const { return true; }
198 template <class Derived, template <class> class InfoProxyType>
199 class DataWrap : public InfoAccess
202 typedef InfoProxyType<Derived> Info;
205 Derived &self() { return *static_cast<Derived *>(this); }
211 return safe_cast<Info *>(InfoAccess::info());
218 return safe_cast<const Info *>(InfoAccess::info());
223 * Copy constructor, copies are not allowed.
225 DataWrap(const DataWrap &stat);
230 void operator=(const DataWrap &);
235 this->setInfo(new Info(self()));
239 * Set the name and marks this stat to print at the end of simulation.
240 * @param name The new name.
241 * @return A reference to this stat.
244 name(const std::string &name)
246 Info *info = this->info();
248 info->flags.set(print);
251 const std::string &name() const { return this->info()->name; }
254 * Set the description and marks this stat to print at the end of
256 * @param desc The new description.
257 * @return A reference to this stat.
260 desc(const std::string &_desc)
262 this->info()->desc = _desc;
267 * Set the precision and marks this stat to print at the end of simulation.
268 * @param _precision The new precision
269 * @return A reference to this stat.
272 precision(int _precision)
274 this->info()->precision = _precision;
279 * Set the flags and marks this stat to print at the end of simulation.
280 * @param f The new flags.
281 * @return A reference to this stat.
286 this->info()->flags.set(_flags);
291 * Set the prerequisite stat and marks this stat to print at the end of
293 * @param prereq The prerequisite stat.
294 * @return A reference to this stat.
296 template <class Stat>
298 prereq(const Stat &prereq)
300 this->info()->prereq = prereq.info();
305 template <class Derived, template <class> class InfoProxyType>
306 class DataWrapVec : public DataWrap<Derived, InfoProxyType>
309 typedef InfoProxyType<Derived> Info;
311 // The following functions are specific to vectors. If you use them
312 // in a non vector context, you will get a nice compiler error!
315 * Set the subfield name for the given index, and marks this stat to print
316 * at the end of simulation.
317 * @param index The subfield index.
318 * @param name The new name of the subfield.
319 * @return A reference to this stat.
322 subname(off_type index, const std::string &name)
324 Derived &self = this->self();
325 Info *info = self.info();
327 std::vector<std::string> &subn = info->subnames;
328 if (subn.size() <= index)
329 subn.resize(index + 1);
334 // The following functions are specific to 2d vectors. If you use
335 // them in a non vector context, you will get a nice compiler
336 // error because info doesn't have the right variables.
339 * Set the subfield description for the given index and marks this stat to
340 * print at the end of simulation.
341 * @param index The subfield index.
342 * @param desc The new description of the subfield
343 * @return A reference to this stat.
346 subdesc(off_type index, const std::string &desc)
348 Info *info = this->info();
350 std::vector<std::string> &subd = info->subdescs;
351 if (subd.size() <= index)
352 subd.resize(index + 1);
361 Derived &self = this->self();
362 Info *info = this->info();
364 size_t size = self.size();
365 for (off_type i = 0; i < size; ++i)
366 self.data(i)->prepare(info);
372 Derived &self = this->self();
373 Info *info = this->info();
375 size_t size = self.size();
376 for (off_type i = 0; i < size; ++i)
377 self.data(i)->reset(info);
381 template <class Derived, template <class> class InfoProxyType>
382 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
385 typedef InfoProxyType<Derived> Info;
388 * @warning This makes the assumption that if you're gonna subnames a 2d
389 * vector, you're subnaming across all y
392 ysubnames(const char **names)
394 Derived &self = this->self();
395 Info *info = this->info();
397 info->y_subnames.resize(self.y);
398 for (off_type i = 0; i < self.y; ++i)
399 info->y_subnames[i] = names[i];
404 ysubname(off_type index, const std::string subname)
406 Derived &self = this->self();
407 Info *info = this->info();
409 assert(index < self.y);
410 info->y_subnames.resize(self.y);
411 info->y_subnames[index] = subname.c_str();
416 //////////////////////////////////////////////////////////////////////
420 //////////////////////////////////////////////////////////////////////
423 * Templatized storage and interface for a simple scalar stat.
428 /** The statistic value. */
432 struct Params : public StorageParams {};
436 * Builds this storage element and calls the base constructor of the
444 * The the stat to the given value.
445 * @param val The new value.
447 void set(Counter val) { data = val; }
449 * Increment the stat by the given value.
450 * @param val The new value.
452 void inc(Counter val) { data += val; }
454 * Decrement the stat by the given value.
455 * @param val The new value.
457 void dec(Counter val) { data -= val; }
459 * Return the value of this stat as its base type.
460 * @return The value of this stat.
462 Counter value() const { return data; }
464 * Return the value of this stat as a result type.
465 * @return The value of this stat.
467 Result result() const { return (Result)data; }
469 * Prepare stat data for dumping or serialization
471 void prepare(Info *info) { }
473 * Reset stat value to default
475 void reset(Info *info) { data = Counter(); }
478 * @return true if zero value
480 bool zero() const { return data == Counter(); }
484 * Templatized storage and interface to a per-tick average stat. This keeps
485 * a current count and updates a total (count * ticks) when this count
486 * changes. This allows the quick calculation of a per tick count of the item
487 * being watched. This is good for keeping track of residencies in structures
488 * among other things.
493 /** The current count. */
495 /** The total count for all tick. */
496 mutable Result total;
497 /** The tick that current last changed. */
501 struct Params : public StorageParams {};
505 * Build and initializes this stat storage.
508 : current(0), total(0), last(0)
512 * Set the current count to the one provided, update the total and last
514 * @param val The new count.
519 total += current * (curTick - last);
525 * Increment the current count by the provided value, calls set.
526 * @param val The amount to increment.
528 void inc(Counter val) { set(current + val); }
531 * Deccrement the current count by the provided value, calls set.
532 * @param val The amount to decrement.
534 void dec(Counter val) { set(current - val); }
537 * Return the current count.
538 * @return The current count.
540 Counter value() const { return current; }
543 * Return the current average.
544 * @return The current average.
549 assert(last == curTick);
550 return (Result)(total + current) / (Result)(curTick + 1);
554 * @return true if zero value
556 bool zero() const { return total == 0.0; }
559 * Prepare stat data for dumping or serialization
564 total += current * (curTick - last);
569 * Reset stat value to default
581 * Implementation of a scalar stat. The type of stat is determined by the
584 template <class Derived, class Stor>
585 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
588 typedef Stor Storage;
589 typedef typename Stor::Params Params;
592 /** The storage of this stat. */
593 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
597 * Retrieve the storage.
598 * @param index The vector index to access.
599 * @return The storage object at the given index.
604 return reinterpret_cast<Storage *>(storage);
608 * Retrieve a const pointer to the storage.
609 * for the given index.
610 * @param index The vector index to access.
611 * @return A const pointer to the storage object at the given index.
616 return reinterpret_cast<const Storage *>(storage);
622 new (storage) Storage(this->info());
628 * Return the current value of this stat as its base type.
629 * @return The current value.
631 Counter value() const { return data()->value(); }
640 // Common operators for stats
642 * Increment the stat by 1. This calls the associated storage object inc
645 void operator++() { data()->inc(1); }
647 * Decrement the stat by 1. This calls the associated storage object dec
650 void operator--() { data()->dec(1); }
652 /** Increment the stat by 1. */
653 void operator++(int) { ++*this; }
654 /** Decrement the stat by 1. */
655 void operator--(int) { --*this; }
658 * Set the data value to the given value. This calls the associated storage
659 * object set function.
660 * @param v The new value.
662 template <typename U>
663 void operator=(const U &v) { data()->set(v); }
666 * Increment the stat by the given value. This calls the associated
667 * storage object inc function.
668 * @param v The value to add.
670 template <typename U>
671 void operator+=(const U &v) { data()->inc(v); }
674 * Decrement the stat by the given value. This calls the associated
675 * storage object dec function.
676 * @param v The value to substract.
678 template <typename U>
679 void operator-=(const U &v) { data()->dec(v); }
682 * Return the number of elements, always 1 for a scalar.
685 size_type size() const { return 1; }
687 Counter value() { return data()->value(); }
689 Result result() { return data()->result(); }
691 Result total() { return result(); }
693 bool zero() { return result() == 0.0; }
695 void reset() { data()->reset(this->info()); }
696 void prepare() { data()->prepare(this->info()); }
699 class ProxyInfo : public ScalarInfo
702 std::string str() const { return to_string(value()); }
703 size_type size() const { return 1; }
704 bool check() const { return true; }
707 bool zero() const { return value() == 0; }
709 void visit(Visit &visitor) { visitor.visit(*this); }
713 class ValueProxy : public ProxyInfo
719 ValueProxy(T &val) : scalar(&val) {}
720 Counter value() const { return *scalar; }
721 Result result() const { return *scalar; }
722 Result total() const { return *scalar; }
726 class FunctorProxy : public ProxyInfo
732 FunctorProxy(T &func) : functor(&func) {}
733 Counter value() const { return (*functor)(); }
734 Result result() const { return (*functor)(); }
735 Result total() const { return (*functor)(); }
738 template <class Derived>
739 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
745 ValueBase() : proxy(NULL) { }
746 ~ValueBase() { if (proxy) delete proxy; }
752 proxy = new ValueProxy<T>(value);
761 proxy = new FunctorProxy<T>(func);
766 Counter value() { return proxy->value(); }
767 Result result() const { return proxy->result(); }
768 Result total() const { return proxy->total(); };
769 size_type size() const { return proxy->size(); }
771 std::string str() const { return proxy->str(); }
772 bool zero() const { return proxy->zero(); }
773 bool check() const { return proxy != NULL; }
778 //////////////////////////////////////////////////////////////////////
782 //////////////////////////////////////////////////////////////////////
785 * A proxy class to access the stat at a given index in a VectorBase stat.
786 * Behaves like a ScalarBase.
788 template <class Stat>
792 /** Pointer to the parent Vector. */
795 /** The index to access in the parent VectorBase. */
800 * Return the current value of this stat as its base type.
801 * @return The current value.
803 Counter value() const { return stat.data(index)->value(); }
806 * Return the current value of this statas a result type.
807 * @return The current value.
809 Result result() const { return stat.data(index)->result(); }
813 * Create and initialize this proxy, do not register it with the database.
814 * @param i The index to access.
816 ScalarProxy(Stat &s, off_type i)
822 * Create a copy of the provided ScalarProxy.
823 * @param sp The proxy to copy.
825 ScalarProxy(const ScalarProxy &sp)
826 : stat(sp.stat), index(sp.index)
830 * Set this proxy equal to the provided one.
831 * @param sp The proxy to copy.
832 * @return A reference to this proxy.
835 operator=(const ScalarProxy &sp)
843 // Common operators for stats
845 * Increment the stat by 1. This calls the associated storage object inc
848 void operator++() { stat.data(index)->inc(1); }
850 * Decrement the stat by 1. This calls the associated storage object dec
853 void operator--() { stat.data(index)->dec(1); }
855 /** Increment the stat by 1. */
856 void operator++(int) { ++*this; }
857 /** Decrement the stat by 1. */
858 void operator--(int) { --*this; }
861 * Set the data value to the given value. This calls the associated storage
862 * object set function.
863 * @param v The new value.
865 template <typename U>
867 operator=(const U &v)
869 stat.data(index)->set(v);
873 * Increment the stat by the given value. This calls the associated
874 * storage object inc function.
875 * @param v The value to add.
877 template <typename U>
879 operator+=(const U &v)
881 stat.data(index)->inc(v);
885 * Decrement the stat by the given value. This calls the associated
886 * storage object dec function.
887 * @param v The value to substract.
889 template <typename U>
891 operator-=(const U &v)
893 stat.data(index)->dec(v);
897 * Return the number of elements, always 1 for a scalar.
900 size_type size() const { return 1; }
906 return csprintf("%s[%d]", stat.info()->name, index);
911 * Implementation of a vector of stats. The type of stat is determined by the
912 * Storage class. @sa ScalarBase
914 template <class Derived, class Stor>
915 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
918 typedef Stor Storage;
919 typedef typename Stor::Params Params;
922 typedef ScalarProxy<Derived> Proxy;
923 friend class ScalarProxy<Derived>;
924 friend class DataWrapVec<Derived, VectorInfoProxy>;
927 /** The storage of this stat. */
933 * Retrieve the storage.
934 * @param index The vector index to access.
935 * @return The storage object at the given index.
937 Storage *data(off_type index) { return &storage[index]; }
940 * Retrieve a const pointer to the storage.
941 * @param index The vector index to access.
942 * @return A const pointer to the storage object at the given index.
944 const Storage *data(off_type index) const { return &storage[index]; }
949 assert(s > 0 && "size must be positive!");
950 assert(!storage && "already initialized");
953 char *ptr = new char[_size * sizeof(Storage)];
954 storage = reinterpret_cast<Storage *>(ptr);
956 for (off_type i = 0; i < _size; ++i)
957 new (&storage[i]) Storage(this->info());
964 value(VCounter &vec) const
967 for (off_type i = 0; i < size(); ++i)
968 vec[i] = data(i)->value();
972 * Copy the values to a local vector and return a reference to it.
973 * @return A reference to a vector of the stat values.
976 result(VResult &vec) const
979 for (off_type i = 0; i < size(); ++i)
980 vec[i] = data(i)->result();
984 * Return a total of all entries in this vector.
985 * @return The total of all vector entries.
991 for (off_type i = 0; i < size(); ++i)
992 total += data(i)->result();
997 * @return the number of elements in this vector.
999 size_type size() const { return _size; }
1004 for (off_type i = 0; i < size(); ++i)
1005 if (data(i)->zero())
1013 return storage != NULL;
1026 for (off_type i = 0; i < _size; ++i)
1027 data(i)->~Storage();
1028 delete [] reinterpret_cast<char *>(storage);
1032 * Set this vector to have the given size.
1033 * @param size The new size.
1034 * @return A reference to this stat.
1037 init(size_type size)
1039 Derived &self = this->self();
1045 * Return a reference (ScalarProxy) to the stat at the given index.
1046 * @param index The vector index to access.
1047 * @return A reference of the stat.
1050 operator[](off_type index)
1052 assert (index >= 0 && index < size());
1053 return Proxy(this->self(), index);
1057 template <class Stat>
1066 mutable VResult vec;
1068 typename Stat::Storage *
1069 data(off_type index)
1071 assert(index < len);
1072 return stat.data(offset + index);
1075 const typename Stat::Storage *
1076 data(off_type index) const
1078 assert(index < len);
1079 return stat.data(offset + index);
1088 for (off_type i = 0; i < size(); ++i)
1089 vec[i] = data(i)->result();
1098 for (off_type i = 0; i < size(); ++i)
1099 total += data(i)->result();
1104 VectorProxy(Stat &s, off_type o, size_type l)
1105 : stat(s), offset(o), len(l)
1109 VectorProxy(const VectorProxy &sp)
1110 : stat(sp.stat), offset(sp.offset), len(sp.len)
1115 operator=(const VectorProxy &sp)
1124 operator[](off_type index)
1126 assert (index >= 0 && index < size());
1127 return ScalarProxy<Stat>(stat, offset + index);
1130 size_type size() const { return len; }
1133 template <class Derived, class Stor>
1134 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1137 typedef Vector2dInfoProxy<Derived> Info;
1138 typedef Stor Storage;
1139 typedef typename Stor::Params Params;
1140 typedef VectorProxy<Derived> Proxy;
1141 friend class ScalarProxy<Derived>;
1142 friend class VectorProxy<Derived>;
1143 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1144 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1153 Storage *data(off_type index) { return &storage[index]; }
1154 const Storage *data(off_type index) const { return &storage[index]; }
1166 for (off_type i = 0; i < _size; ++i)
1167 data(i)->~Storage();
1168 delete [] reinterpret_cast<char *>(storage);
1172 init(size_type _x, size_type _y)
1174 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1175 assert(!storage && "already initialized");
1177 Derived &self = this->self();
1178 Info *info = this->info();
1186 char *ptr = new char[_size * sizeof(Storage)];
1187 storage = reinterpret_cast<Storage *>(ptr);
1189 for (off_type i = 0; i < _size; ++i)
1190 new (&storage[i]) Storage(info);
1197 std::string ysubname(off_type i) const { return (*this->y_subnames)[i]; }
1200 operator[](off_type index)
1202 off_type offset = index * y;
1203 assert (index >= 0 && offset + index < size());
1204 return Proxy(this->self(), offset, y);
1217 return data(0)->zero();
1219 for (off_type i = 0; i < size(); ++i)
1220 if (!data(i)->zero())
1229 Info *info = this->info();
1230 size_type size = this->size();
1232 for (off_type i = 0; i < size; ++i)
1233 data(i)->prepare(info);
1235 info->cvec.resize(size);
1236 for (off_type i = 0; i < size; ++i)
1237 info->cvec[i] = data(i)->value();
1241 * Reset stat value to default
1246 Info *info = this->info();
1247 size_type size = this->size();
1248 for (off_type i = 0; i < size; ++i)
1249 data(i)->reset(info);
1255 return storage != NULL;
1259 //////////////////////////////////////////////////////////////////////
1261 // Non formula statistics
1263 //////////////////////////////////////////////////////////////////////
1266 * Templatized storage and interface for a distrbution stat.
1271 /** The parameters for a distribution stat. */
1272 struct Params : public DistParams
1274 Params() : DistParams(Dist) {}
1278 /** The minimum value to track. */
1280 /** The maximum value to track. */
1282 /** The number of entries in each bucket. */
1283 Counter bucket_size;
1284 /** The number of buckets. Equal to (max-min)/bucket_size. */
1287 /** The smallest value sampled. */
1289 /** The largest value sampled. */
1291 /** The number of values sampled less than min. */
1293 /** The number of values sampled more than max. */
1295 /** The current sum. */
1297 /** The sum of squares. */
1299 /** The number of samples. */
1301 /** Counter for each bucket. */
1305 DistStor(Info *info)
1306 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1312 * Add a value to the distribution for the given number of times.
1313 * @param val The value to add.
1314 * @param number The number of times to add the value.
1317 sample(Counter val, int number)
1319 if (val < min_track)
1320 underflow += number;
1321 else if (val > max_track)
1325 (size_type)std::floor((val - min_track) / bucket_size);
1326 assert(index < size());
1327 cvec[index] += number;
1336 Counter sample = val * number;
1338 squares += sample * sample;
1343 * Return the number of buckets in this distribution.
1344 * @return the number of buckets.
1346 size_type size() const { return cvec.size(); }
1349 * Returns true if any calls to sample have been made.
1350 * @return True if any values have been sampled.
1355 return samples == Counter();
1359 prepare(Info *info, DistData &data)
1361 const Params *params = safe_cast<const Params *>(info->storageParams);
1363 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1364 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1365 data.underflow = underflow;
1366 data.overflow = overflow;
1368 int buckets = params->buckets;
1369 data.cvec.resize(buckets);
1370 for (off_type i = 0; i < buckets; ++i)
1371 data.cvec[i] = cvec[i];
1374 data.squares = squares;
1375 data.samples = samples;
1379 * Reset stat value to default
1384 const Params *params = safe_cast<const Params *>(info->storageParams);
1385 min_track = params->min;
1386 max_track = params->max;
1387 bucket_size = params->bucket_size;
1389 min_val = CounterLimits::max();
1390 max_val = CounterLimits::min();
1391 underflow = Counter();
1392 overflow = Counter();
1394 size_type size = cvec.size();
1395 for (off_type i = 0; i < size; ++i)
1396 cvec[i] = Counter();
1399 squares = Counter();
1400 samples = Counter();
1405 * Templatized storage and interface for a distribution that calculates mean
1411 struct Params : public DistParams
1413 Params() : DistParams(Deviation) {}
1417 /** The current sum. */
1419 /** The sum of squares. */
1421 /** The number of samples. */
1426 * Create and initialize this storage.
1428 SampleStor(Info *info)
1429 : sum(Counter()), squares(Counter()), samples(Counter())
1433 * Add a value the given number of times to this running average.
1434 * Update the running sum and sum of squares, increment the number of
1435 * values seen by the given number.
1436 * @param val The value to add.
1437 * @param number The number of times to add the value.
1440 sample(Counter val, int number)
1442 Counter value = val * number;
1444 squares += value * value;
1449 * Return the number of entries in this stat, 1
1452 size_type size() const { return 1; }
1455 * Return true if no samples have been added.
1456 * @return True if no samples have been added.
1458 bool zero() const { return samples == Counter(); }
1461 prepare(Info *info, DistData &data)
1464 data.squares = squares;
1465 data.samples = samples;
1469 * Reset stat value to default
1475 squares = Counter();
1476 samples = Counter();
1481 * Templatized storage for distribution that calculates per tick mean and
1487 struct Params : public DistParams
1489 Params() : DistParams(Deviation) {}
1493 /** Current total. */
1495 /** Current sum of squares. */
1500 * Create and initialize this storage.
1502 AvgSampleStor(Info *info)
1503 : sum(Counter()), squares(Counter())
1507 * Add a value to the distribution for the given number of times.
1508 * Update the running sum and sum of squares.
1509 * @param val The value to add.
1510 * @param number The number of times to add the value.
1513 sample(Counter val, int number)
1515 Counter value = val * number;
1517 squares += value * value;
1521 * Return the number of entries, in this case 1.
1524 size_type size() const { return 1; }
1527 * Return true if no samples have been added.
1528 * @return True if the sum is zero.
1530 bool zero() const { return sum == Counter(); }
1533 prepare(Info *info, DistData &data)
1536 data.squares = squares;
1537 data.samples = curTick;
1541 * Reset stat value to default
1547 squares = Counter();
1552 * Implementation of a distribution stat. The type of distribution is
1553 * determined by the Storage template. @sa ScalarBase
1555 template <class Derived, class Stor>
1556 class DistBase : public DataWrap<Derived, DistInfoProxy>
1559 typedef DistInfoProxy<Derived> Info;
1560 typedef Stor Storage;
1561 typedef typename Stor::Params Params;
1564 /** The storage for this stat. */
1565 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1569 * Retrieve the storage.
1570 * @return The storage object for this stat.
1575 return reinterpret_cast<Storage *>(storage);
1579 * Retrieve a const pointer to the storage.
1580 * @return A const pointer to the storage object for this stat.
1585 return reinterpret_cast<const Storage *>(storage);
1591 new (storage) Storage(this->info());
1599 * Add a value to the distribtion n times. Calls sample on the storage
1601 * @param v The value to add.
1602 * @param n The number of times to add it, defaults to 1.
1604 template <typename U>
1605 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1608 * Return the number of entries in this stat.
1609 * @return The number of entries.
1611 size_type size() const { return data()->size(); }
1613 * Return true if no samples have been added.
1614 * @return True if there haven't been any samples.
1616 bool zero() const { return data()->zero(); }
1621 Info *info = this->info();
1622 data()->prepare(info, info->data);
1626 * Reset stat value to default
1631 data()->reset(this->info());
1635 template <class Stat>
1638 template <class Derived, class Stor>
1639 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1642 typedef VectorDistInfoProxy<Derived> Info;
1643 typedef Stor Storage;
1644 typedef typename Stor::Params Params;
1645 typedef DistProxy<Derived> Proxy;
1646 friend class DistProxy<Derived>;
1647 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1655 data(off_type index)
1657 return &storage[index];
1661 data(off_type index) const
1663 return &storage[index];
1669 assert(s > 0 && "size must be positive!");
1670 assert(!storage && "already initialized");
1673 char *ptr = new char[_size * sizeof(Storage)];
1674 storage = reinterpret_cast<Storage *>(ptr);
1676 Info *info = this->info();
1677 for (off_type i = 0; i < _size; ++i)
1678 new (&storage[i]) Storage(info);
1693 for (off_type i = 0; i < _size; ++i)
1694 data(i)->~Storage();
1695 delete [] reinterpret_cast<char *>(storage);
1698 Proxy operator[](off_type index);
1711 for (off_type i = 0; i < size(); ++i)
1712 if (!data(i)->zero())
1721 Info *info = this->info();
1722 size_type size = this->size();
1723 info->data.resize(size);
1724 for (off_type i = 0; i < size; ++i)
1725 data(i)->prepare(info, info->data[i]);
1731 return storage != NULL;
1735 template <class Stat>
1743 typename Stat::Storage *data() { return stat->data(index); }
1744 const typename Stat::Storage *data() const { return stat->data(index); }
1747 DistProxy(Stat *s, off_type i)
1751 DistProxy(const DistProxy &sp)
1752 : stat(sp.stat), index(sp.index)
1756 operator=(const DistProxy &sp)
1764 template <typename U>
1766 sample(const U &v, int n = 1)
1768 data()->sample(v, n);
1780 return data()->zero();
1784 * Proxy has no state. Nothing to reset.
1789 template <class Derived, class Stor>
1790 inline typename VectorDistBase<Derived, Stor>::Proxy
1791 VectorDistBase<Derived, Stor>::operator[](off_type index)
1793 assert (index >= 0 && index < size());
1794 typedef typename VectorDistBase<Derived, Stor>::Proxy Proxy;
1795 return Proxy(this, index);
1799 template <class Storage>
1801 VectorDistBase<Storage>::total(off_type index) const
1804 for (off_type i = 0; i < x_size(); ++i)
1805 total += data(i)->result();
1809 //////////////////////////////////////////////////////////////////////
1813 //////////////////////////////////////////////////////////////////////
1816 * Base class for formula statistic node. These nodes are used to build a tree
1817 * that represents the formula.
1819 class Node : public RefCounted
1823 * Return the number of nodes in the subtree starting at this node.
1824 * @return the number of nodes in this subtree.
1826 virtual size_type size() const = 0;
1828 * Return the result vector of this subtree.
1829 * @return The result vector of this subtree.
1831 virtual const VResult &result() const = 0;
1833 * Return the total of the result vector.
1834 * @return The total of the result vector.
1836 virtual Result total() const = 0;
1841 virtual std::string str() const = 0;
1844 /** Reference counting pointer to a function Node. */
1845 typedef RefCountingPtr<Node> NodePtr;
1847 class ScalarStatNode : public Node
1850 const ScalarInfo *data;
1851 mutable VResult vresult;
1854 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
1859 vresult[0] = data->result();
1863 Result total() const { return data->result(); };
1865 size_type size() const { return 1; }
1870 std::string str() const { return data->name; }
1873 template <class Stat>
1874 class ScalarProxyNode : public Node
1877 const ScalarProxy<Stat> proxy;
1878 mutable VResult vresult;
1881 ScalarProxyNode(const ScalarProxy<Stat> &p)
1882 : proxy(p), vresult(1)
1888 vresult[0] = proxy.result();
1895 return proxy.result();
1914 class VectorStatNode : public Node
1917 const VectorInfo *data;
1920 VectorStatNode(const VectorInfo *d) : data(d) { }
1921 const VResult &result() const { return data->result(); }
1922 Result total() const { return data->total(); };
1924 size_type size() const { return data->size(); }
1926 std::string str() const { return data->name; }
1930 class ConstNode : public Node
1936 ConstNode(T s) : vresult(1, (Result)s) {}
1937 const VResult &result() const { return vresult; }
1938 Result total() const { return vresult[0]; };
1939 size_type size() const { return 1; }
1940 std::string str() const { return to_string(vresult[0]); }
1944 class ConstVectorNode : public Node
1950 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
1951 const VResult &result() const { return vresult; }
1956 size_type size = this->size();
1958 for (off_type i = 0; i < size; i++)
1963 size_type size() const { return vresult.size(); }
1967 size_type size = this->size();
1968 std::string tmp = "(";
1969 for (off_type i = 0; i < size; i++)
1970 tmp += csprintf("%s ",to_string(vresult[i]));
1980 struct OpString<std::plus<Result> >
1982 static std::string str() { return "+"; }
1986 struct OpString<std::minus<Result> >
1988 static std::string str() { return "-"; }
1992 struct OpString<std::multiplies<Result> >
1994 static std::string str() { return "*"; }
1998 struct OpString<std::divides<Result> >
2000 static std::string str() { return "/"; }
2004 struct OpString<std::modulus<Result> >
2006 static std::string str() { return "%"; }
2010 struct OpString<std::negate<Result> >
2012 static std::string str() { return "-"; }
2016 class UnaryNode : public Node
2020 mutable VResult vresult;
2023 UnaryNode(NodePtr &p) : l(p) {}
2028 const VResult &lvec = l->result();
2029 size_type size = lvec.size();
2033 vresult.resize(size);
2035 for (off_type i = 0; i < size; ++i)
2036 vresult[i] = op(lvec[i]);
2044 const VResult &vec = this->result();
2046 for (off_type i = 0; i < size(); i++)
2051 size_type size() const { return l->size(); }
2056 return OpString<Op>::str() + l->str();
2061 class BinaryNode : public Node
2066 mutable VResult vresult;
2069 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2075 const VResult &lvec = l->result();
2076 const VResult &rvec = r->result();
2078 assert(lvec.size() > 0 && rvec.size() > 0);
2080 if (lvec.size() == 1 && rvec.size() == 1) {
2082 vresult[0] = op(lvec[0], rvec[0]);
2083 } else if (lvec.size() == 1) {
2084 size_type size = rvec.size();
2085 vresult.resize(size);
2086 for (off_type i = 0; i < size; ++i)
2087 vresult[i] = op(lvec[0], rvec[i]);
2088 } else if (rvec.size() == 1) {
2089 size_type size = lvec.size();
2090 vresult.resize(size);
2091 for (off_type i = 0; i < size; ++i)
2092 vresult[i] = op(lvec[i], rvec[0]);
2093 } else if (rvec.size() == lvec.size()) {
2094 size_type size = rvec.size();
2095 vresult.resize(size);
2096 for (off_type i = 0; i < size; ++i)
2097 vresult[i] = op(lvec[i], rvec[i]);
2106 const VResult &vec = this->result();
2108 for (off_type i = 0; i < size(); i++)
2116 size_type ls = l->size();
2117 size_type rs = r->size();
2120 } else if (rs == 1) {
2123 assert(ls == rs && "Node vector sizes are not equal");
2131 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2136 class SumNode : public Node
2140 mutable VResult vresult;
2143 SumNode(NodePtr &p) : l(p), vresult(1) {}
2148 const VResult &lvec = l->result();
2149 size_type size = lvec.size();
2155 for (off_type i = 0; i < size; ++i)
2156 vresult[0] = op(vresult[0], lvec[i]);
2164 const VResult &lvec = l->result();
2165 size_type size = lvec.size();
2168 Result vresult = 0.0;
2171 for (off_type i = 0; i < size; ++i)
2172 vresult = op(vresult, lvec[i]);
2177 size_type size() const { return 1; }
2182 return csprintf("total(%s)", l->str());
2187 //////////////////////////////////////////////////////////////////////
2189 // Visible Statistics Types
2191 //////////////////////////////////////////////////////////////////////
2193 * @defgroup VisibleStats "Statistic Types"
2194 * These are the statistics that are used in the simulator.
2199 * This is a simple scalar statistic, like a counter.
2200 * @sa Stat, ScalarBase, StatStor
2202 class Scalar : public ScalarBase<Scalar, StatStor>
2205 using ScalarBase<Scalar, StatStor>::operator=;
2209 * A stat that calculates the per tick average of a value.
2210 * @sa Stat, ScalarBase, AvgStor
2212 class Average : public ScalarBase<Average, AvgStor>
2215 using ScalarBase<Average, AvgStor>::operator=;
2218 class Value : public ValueBase<Value>
2223 * A vector of scalar stats.
2224 * @sa Stat, VectorBase, StatStor
2226 class Vector : public VectorBase<Vector, StatStor>
2231 * A vector of Average stats.
2232 * @sa Stat, VectorBase, AvgStor
2234 class AverageVector : public VectorBase<AverageVector, AvgStor>
2239 * A 2-Dimensional vecto of scalar stats.
2240 * @sa Stat, Vector2dBase, StatStor
2242 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2247 * A simple distribution stat.
2248 * @sa Stat, DistBase, DistStor
2250 class Distribution : public DistBase<Distribution, DistStor>
2254 * Set the parameters of this distribution. @sa DistStor::Params
2255 * @param min The minimum value of the distribution.
2256 * @param max The maximum value of the distribution.
2257 * @param bkt The number of values in each bucket.
2258 * @return A reference to this distribution.
2261 init(Counter min, Counter max, Counter bkt)
2263 DistStor::Params *params = new DistStor::Params;
2266 params->bucket_size = bkt;
2267 params->buckets = (size_type)rint((max - min) / bkt + 1.0);
2268 this->setParams(params);
2270 return this->self();
2275 * Calculates the mean and variance of all the samples.
2276 * @sa DistBase, SampleStor
2278 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2282 * Construct and initialize this distribution.
2291 * Calculates the per tick mean and variance of the samples.
2292 * @sa DistBase, AvgSampleStor
2294 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2298 * Construct and initialize this distribution.
2307 * A vector of distributions.
2308 * @sa VectorDistBase, DistStor
2310 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2314 * Initialize storage and parameters for this distribution.
2315 * @param size The size of the vector (the number of distributions).
2316 * @param min The minimum value of the distribution.
2317 * @param max The maximum value of the distribution.
2318 * @param bkt The number of values in each bucket.
2319 * @return A reference to this distribution.
2321 VectorDistribution &
2322 init(size_type size, Counter min, Counter max, Counter bkt)
2324 DistStor::Params *params = new DistStor::Params;
2327 params->bucket_size = bkt;
2328 params->buckets = (size_type)rint((max - min) / bkt + 1.0);
2329 this->setParams(params);
2331 return this->self();
2336 * This is a vector of StandardDeviation stats.
2337 * @sa VectorDistBase, SampleStor
2339 class VectorStandardDeviation
2340 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2344 * Initialize storage for this distribution.
2345 * @param size The size of the vector.
2346 * @return A reference to this distribution.
2348 VectorStandardDeviation &
2349 init(size_type size)
2352 return this->self();
2357 * This is a vector of AverageDeviation stats.
2358 * @sa VectorDistBase, AvgSampleStor
2360 class VectorAverageDeviation
2361 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2365 * Initialize storage for this distribution.
2366 * @param size The size of the vector.
2367 * @return A reference to this distribution.
2369 VectorAverageDeviation &
2370 init(size_type size)
2373 return this->self();
2377 template <class Stat>
2378 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2381 mutable VResult vec;
2382 mutable VCounter cvec;
2385 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2387 size_type size() const { return this->s.size(); }
2392 this->s.result(vec);
2395 Result total() const { return this->s.total(); }
2396 VCounter &value() const { return cvec; }
2398 std::string str() const { return this->s.str(); }
2403 * A formula for statistics that is calculated when printed. A formula is
2404 * stored as a tree of Nodes that represent the equation to calculate.
2405 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2407 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2410 /** The root of the tree which represents the Formula */
2416 * Create and initialize thie formula, and register it with the database.
2421 * Create a formula with the given root node, register it with the
2423 * @param r The root of the expression tree.
2428 * Set an unitialized Formula to the given root.
2429 * @param r The root of the expression tree.
2430 * @return a reference to this formula.
2432 const Formula &operator=(Temp r);
2435 * Add the given tree to the existing one.
2436 * @param r The root of the expression tree.
2437 * @return a reference to this formula.
2439 const Formula &operator+=(Temp r);
2441 * Return the result of the Fomula in a vector. If there were no Vector
2442 * components to the Formula, then the vector is size 1. If there were,
2443 * like x/y with x being a vector of size 3, then the result returned will
2444 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2445 * @return The result vector.
2447 void result(VResult &vec) const;
2450 * Return the total Formula result. If there is a Vector
2451 * component to this Formula, then this is the result of the
2452 * Formula if the formula is applied after summing all the
2453 * components of the Vector. For example, if Formula is x/y where
2454 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2455 * there is no Vector component, total() returns the same value as
2456 * the first entry in the VResult val() returns.
2457 * @return The total of the result vector.
2459 Result total() const;
2462 * Return the number of elements in the tree.
2464 size_type size() const;
2469 * Formulas don't need to be reset
2478 std::string str() const;
2481 class FormulaNode : public Node
2484 const Formula &formula;
2485 mutable VResult vec;
2488 FormulaNode(const Formula &f) : formula(f) {}
2490 size_type size() const { return formula.size(); }
2491 const VResult &result() const { formula.result(vec); return vec; }
2492 Result total() const { return formula.total(); }
2494 std::string str() const { return formula.str(); }
2498 * Helper class to construct formula node trees.
2504 * Pointer to a Node object.
2510 * Copy the given pointer to this class.
2511 * @param n A pointer to a Node object to copy.
2513 Temp(NodePtr n) : node(n) { }
2516 * Return the node pointer.
2517 * @return the node pointer.
2519 operator NodePtr&() { return node; }
2523 * Create a new ScalarStatNode.
2524 * @param s The ScalarStat to place in a node.
2526 Temp(const Scalar &s)
2527 : node(new ScalarStatNode(s.info()))
2531 * Create a new ScalarStatNode.
2532 * @param s The ScalarStat to place in a node.
2534 Temp(const Value &s)
2535 : node(new ScalarStatNode(s.info()))
2539 * Create a new ScalarStatNode.
2540 * @param s The ScalarStat to place in a node.
2542 Temp(const Average &s)
2543 : node(new ScalarStatNode(s.info()))
2547 * Create a new VectorStatNode.
2548 * @param s The VectorStat to place in a node.
2550 Temp(const Vector &s)
2551 : node(new VectorStatNode(s.info()))
2557 Temp(const Formula &f)
2558 : node(new FormulaNode(f))
2562 * Create a new ScalarProxyNode.
2563 * @param p The ScalarProxy to place in a node.
2565 template <class Stat>
2566 Temp(const ScalarProxy<Stat> &p)
2567 : node(new ScalarProxyNode<Stat>(p))
2571 * Create a ConstNode
2572 * @param value The value of the const node.
2574 Temp(signed char value)
2575 : node(new ConstNode<signed char>(value))
2579 * Create a ConstNode
2580 * @param value The value of the const node.
2582 Temp(unsigned char value)
2583 : node(new ConstNode<unsigned char>(value))
2587 * Create a ConstNode
2588 * @param value The value of the const node.
2590 Temp(signed short value)
2591 : node(new ConstNode<signed short>(value))
2595 * Create a ConstNode
2596 * @param value The value of the const node.
2598 Temp(unsigned short value)
2599 : node(new ConstNode<unsigned short>(value))
2603 * Create a ConstNode
2604 * @param value The value of the const node.
2606 Temp(signed int value)
2607 : node(new ConstNode<signed int>(value))
2611 * Create a ConstNode
2612 * @param value The value of the const node.
2614 Temp(unsigned int value)
2615 : node(new ConstNode<unsigned int>(value))
2619 * Create a ConstNode
2620 * @param value The value of the const node.
2622 Temp(signed long value)
2623 : node(new ConstNode<signed long>(value))
2627 * Create a ConstNode
2628 * @param value The value of the const node.
2630 Temp(unsigned long value)
2631 : node(new ConstNode<unsigned long>(value))
2635 * Create a ConstNode
2636 * @param value The value of the const node.
2638 Temp(signed long long value)
2639 : node(new ConstNode<signed long long>(value))
2643 * Create a ConstNode
2644 * @param value The value of the const node.
2646 Temp(unsigned long long value)
2647 : node(new ConstNode<unsigned long long>(value))
2651 * Create a ConstNode
2652 * @param value The value of the const node.
2655 : node(new ConstNode<float>(value))
2659 * Create a ConstNode
2660 * @param value The value of the const node.
2663 : node(new ConstNode<double>(value))
2673 operator+(Temp l, Temp r)
2675 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
2679 operator-(Temp l, Temp r)
2681 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
2685 operator*(Temp l, Temp r)
2687 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
2691 operator/(Temp l, Temp r)
2693 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
2699 return NodePtr(new UnaryNode<std::negate<Result> >(l));
2702 template <typename T>
2706 return NodePtr(new ConstNode<T>(val));
2709 template <typename T>
2711 constantVector(T val)
2713 return NodePtr(new ConstVectorNode<T>(val));
2719 return NodePtr(new SumNode<std::plus<Result> >(val));
2723 * Enable the statistics package. Before the statistics package is
2724 * enabled, all statistics must be created and initialized and once
2725 * the package is enabled, no more statistics can be created.
2730 * Prepare all stats for data access. This must be done before
2731 * dumping and serialization.
2736 * Dump all statistics data to the registered outputs
2741 * Reset all statistics to the base state
2745 * Register a callback that should be called whenever statistics are
2748 void registerResetCallback(Callback *cb);
2750 std::list<Info *> &statsList();
2752 /* namespace Stats */ }
2754 #endif // __BASE_STATISTICS_HH__