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 tick of the last reset */
497 /** The total count for all tick. */
498 mutable Result total;
499 /** The tick that current last changed. */
503 struct Params : public StorageParams {};
507 * Build and initializes this stat storage.
510 : current(0), lastReset(0), total(0), last(0)
514 * Set the current count to the one provided, update the total and last
516 * @param val The new count.
521 total += current * (curTick - last);
527 * Increment the current count by the provided value, calls set.
528 * @param val The amount to increment.
530 void inc(Counter val) { set(current + val); }
533 * Deccrement the current count by the provided value, calls set.
534 * @param val The amount to decrement.
536 void dec(Counter val) { set(current - val); }
539 * Return the current count.
540 * @return The current count.
542 Counter value() const { return current; }
545 * Return the current average.
546 * @return The current average.
551 assert(last == curTick);
552 return (Result)(total + current) / (Result)(curTick - lastReset + 1);
556 * @return true if zero value
558 bool zero() const { return total == 0.0; }
561 * Prepare stat data for dumping or serialization
566 total += current * (curTick - last);
571 * Reset stat value to default
584 * Implementation of a scalar stat. The type of stat is determined by the
587 template <class Derived, class Stor>
588 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
591 typedef Stor Storage;
592 typedef typename Stor::Params Params;
595 /** The storage of this stat. */
596 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
600 * Retrieve the storage.
601 * @param index The vector index to access.
602 * @return The storage object at the given index.
607 return reinterpret_cast<Storage *>(storage);
611 * Retrieve a const pointer to the storage.
612 * for the given index.
613 * @param index The vector index to access.
614 * @return A const pointer to the storage object at the given index.
619 return reinterpret_cast<const Storage *>(storage);
625 new (storage) Storage(this->info());
631 * Return the current value of this stat as its base type.
632 * @return The current value.
634 Counter value() const { return data()->value(); }
643 // Common operators for stats
645 * Increment the stat by 1. This calls the associated storage object inc
648 void operator++() { data()->inc(1); }
650 * Decrement the stat by 1. This calls the associated storage object dec
653 void operator--() { data()->dec(1); }
655 /** Increment the stat by 1. */
656 void operator++(int) { ++*this; }
657 /** Decrement the stat by 1. */
658 void operator--(int) { --*this; }
661 * Set the data value to the given value. This calls the associated storage
662 * object set function.
663 * @param v The new value.
665 template <typename U>
666 void operator=(const U &v) { data()->set(v); }
669 * Increment the stat by the given value. This calls the associated
670 * storage object inc function.
671 * @param v The value to add.
673 template <typename U>
674 void operator+=(const U &v) { data()->inc(v); }
677 * Decrement the stat by the given value. This calls the associated
678 * storage object dec function.
679 * @param v The value to substract.
681 template <typename U>
682 void operator-=(const U &v) { data()->dec(v); }
685 * Return the number of elements, always 1 for a scalar.
688 size_type size() const { return 1; }
690 Counter value() { return data()->value(); }
692 Result result() { return data()->result(); }
694 Result total() { return result(); }
696 bool zero() { return result() == 0.0; }
698 void reset() { data()->reset(this->info()); }
699 void prepare() { data()->prepare(this->info()); }
702 class ProxyInfo : public ScalarInfo
705 std::string str() const { return to_string(value()); }
706 size_type size() const { return 1; }
707 bool check() const { return true; }
710 bool zero() const { return value() == 0; }
712 void visit(Visit &visitor) { visitor.visit(*this); }
716 class ValueProxy : public ProxyInfo
722 ValueProxy(T &val) : scalar(&val) {}
723 Counter value() const { return *scalar; }
724 Result result() const { return *scalar; }
725 Result total() const { return *scalar; }
729 class FunctorProxy : public ProxyInfo
735 FunctorProxy(T &func) : functor(&func) {}
736 Counter value() const { return (*functor)(); }
737 Result result() const { return (*functor)(); }
738 Result total() const { return (*functor)(); }
741 template <class Derived>
742 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
748 ValueBase() : proxy(NULL) { }
749 ~ValueBase() { if (proxy) delete proxy; }
755 proxy = new ValueProxy<T>(value);
764 proxy = new FunctorProxy<T>(func);
769 Counter value() { return proxy->value(); }
770 Result result() const { return proxy->result(); }
771 Result total() const { return proxy->total(); };
772 size_type size() const { return proxy->size(); }
774 std::string str() const { return proxy->str(); }
775 bool zero() const { return proxy->zero(); }
776 bool check() const { return proxy != NULL; }
781 //////////////////////////////////////////////////////////////////////
785 //////////////////////////////////////////////////////////////////////
788 * A proxy class to access the stat at a given index in a VectorBase stat.
789 * Behaves like a ScalarBase.
791 template <class Stat>
795 /** Pointer to the parent Vector. */
798 /** The index to access in the parent VectorBase. */
803 * Return the current value of this stat as its base type.
804 * @return The current value.
806 Counter value() const { return stat.data(index)->value(); }
809 * Return the current value of this statas a result type.
810 * @return The current value.
812 Result result() const { return stat.data(index)->result(); }
816 * Create and initialize this proxy, do not register it with the database.
817 * @param i The index to access.
819 ScalarProxy(Stat &s, off_type i)
825 * Create a copy of the provided ScalarProxy.
826 * @param sp The proxy to copy.
828 ScalarProxy(const ScalarProxy &sp)
829 : stat(sp.stat), index(sp.index)
833 * Set this proxy equal to the provided one.
834 * @param sp The proxy to copy.
835 * @return A reference to this proxy.
838 operator=(const ScalarProxy &sp)
846 // Common operators for stats
848 * Increment the stat by 1. This calls the associated storage object inc
851 void operator++() { stat.data(index)->inc(1); }
853 * Decrement the stat by 1. This calls the associated storage object dec
856 void operator--() { stat.data(index)->dec(1); }
858 /** Increment the stat by 1. */
859 void operator++(int) { ++*this; }
860 /** Decrement the stat by 1. */
861 void operator--(int) { --*this; }
864 * Set the data value to the given value. This calls the associated storage
865 * object set function.
866 * @param v The new value.
868 template <typename U>
870 operator=(const U &v)
872 stat.data(index)->set(v);
876 * Increment the stat by the given value. This calls the associated
877 * storage object inc function.
878 * @param v The value to add.
880 template <typename U>
882 operator+=(const U &v)
884 stat.data(index)->inc(v);
888 * Decrement the stat by the given value. This calls the associated
889 * storage object dec function.
890 * @param v The value to substract.
892 template <typename U>
894 operator-=(const U &v)
896 stat.data(index)->dec(v);
900 * Return the number of elements, always 1 for a scalar.
903 size_type size() const { return 1; }
909 return csprintf("%s[%d]", stat.info()->name, index);
914 * Implementation of a vector of stats. The type of stat is determined by the
915 * Storage class. @sa ScalarBase
917 template <class Derived, class Stor>
918 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
921 typedef Stor Storage;
922 typedef typename Stor::Params Params;
925 typedef ScalarProxy<Derived> Proxy;
926 friend class ScalarProxy<Derived>;
927 friend class DataWrapVec<Derived, VectorInfoProxy>;
930 /** The storage of this stat. */
936 * Retrieve the storage.
937 * @param index The vector index to access.
938 * @return The storage object at the given index.
940 Storage *data(off_type index) { return &storage[index]; }
943 * Retrieve a const pointer to the storage.
944 * @param index The vector index to access.
945 * @return A const pointer to the storage object at the given index.
947 const Storage *data(off_type index) const { return &storage[index]; }
952 assert(s > 0 && "size must be positive!");
953 assert(!storage && "already initialized");
956 char *ptr = new char[_size * sizeof(Storage)];
957 storage = reinterpret_cast<Storage *>(ptr);
959 for (off_type i = 0; i < _size; ++i)
960 new (&storage[i]) Storage(this->info());
967 value(VCounter &vec) const
970 for (off_type i = 0; i < size(); ++i)
971 vec[i] = data(i)->value();
975 * Copy the values to a local vector and return a reference to it.
976 * @return A reference to a vector of the stat values.
979 result(VResult &vec) const
982 for (off_type i = 0; i < size(); ++i)
983 vec[i] = data(i)->result();
987 * Return a total of all entries in this vector.
988 * @return The total of all vector entries.
994 for (off_type i = 0; i < size(); ++i)
995 total += data(i)->result();
1000 * @return the number of elements in this vector.
1002 size_type size() const { return _size; }
1007 for (off_type i = 0; i < size(); ++i)
1008 if (data(i)->zero())
1016 return storage != NULL;
1029 for (off_type i = 0; i < _size; ++i)
1030 data(i)->~Storage();
1031 delete [] reinterpret_cast<char *>(storage);
1035 * Set this vector to have the given size.
1036 * @param size The new size.
1037 * @return A reference to this stat.
1040 init(size_type size)
1042 Derived &self = this->self();
1048 * Return a reference (ScalarProxy) to the stat at the given index.
1049 * @param index The vector index to access.
1050 * @return A reference of the stat.
1053 operator[](off_type index)
1055 assert (index >= 0 && index < size());
1056 return Proxy(this->self(), index);
1060 template <class Stat>
1069 mutable VResult vec;
1071 typename Stat::Storage *
1072 data(off_type index)
1074 assert(index < len);
1075 return stat.data(offset + index);
1078 const typename Stat::Storage *
1079 data(off_type index) const
1081 assert(index < len);
1082 return stat.data(offset + index);
1091 for (off_type i = 0; i < size(); ++i)
1092 vec[i] = data(i)->result();
1101 for (off_type i = 0; i < size(); ++i)
1102 total += data(i)->result();
1107 VectorProxy(Stat &s, off_type o, size_type l)
1108 : stat(s), offset(o), len(l)
1112 VectorProxy(const VectorProxy &sp)
1113 : stat(sp.stat), offset(sp.offset), len(sp.len)
1118 operator=(const VectorProxy &sp)
1127 operator[](off_type index)
1129 assert (index >= 0 && index < size());
1130 return ScalarProxy<Stat>(stat, offset + index);
1133 size_type size() const { return len; }
1136 template <class Derived, class Stor>
1137 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1140 typedef Vector2dInfoProxy<Derived> Info;
1141 typedef Stor Storage;
1142 typedef typename Stor::Params Params;
1143 typedef VectorProxy<Derived> Proxy;
1144 friend class ScalarProxy<Derived>;
1145 friend class VectorProxy<Derived>;
1146 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1147 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1156 Storage *data(off_type index) { return &storage[index]; }
1157 const Storage *data(off_type index) const { return &storage[index]; }
1169 for (off_type i = 0; i < _size; ++i)
1170 data(i)->~Storage();
1171 delete [] reinterpret_cast<char *>(storage);
1175 init(size_type _x, size_type _y)
1177 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1178 assert(!storage && "already initialized");
1180 Derived &self = this->self();
1181 Info *info = this->info();
1189 char *ptr = new char[_size * sizeof(Storage)];
1190 storage = reinterpret_cast<Storage *>(ptr);
1192 for (off_type i = 0; i < _size; ++i)
1193 new (&storage[i]) Storage(info);
1200 std::string ysubname(off_type i) const { return (*this->y_subnames)[i]; }
1203 operator[](off_type index)
1205 off_type offset = index * y;
1206 assert (index >= 0 && offset + index < size());
1207 return Proxy(this->self(), offset, y);
1220 return data(0)->zero();
1222 for (off_type i = 0; i < size(); ++i)
1223 if (!data(i)->zero())
1232 Info *info = this->info();
1233 size_type size = this->size();
1235 for (off_type i = 0; i < size; ++i)
1236 data(i)->prepare(info);
1238 info->cvec.resize(size);
1239 for (off_type i = 0; i < size; ++i)
1240 info->cvec[i] = data(i)->value();
1244 * Reset stat value to default
1249 Info *info = this->info();
1250 size_type size = this->size();
1251 for (off_type i = 0; i < size; ++i)
1252 data(i)->reset(info);
1258 return storage != NULL;
1262 //////////////////////////////////////////////////////////////////////
1264 // Non formula statistics
1266 //////////////////////////////////////////////////////////////////////
1269 * Templatized storage and interface for a distrbution stat.
1274 /** The parameters for a distribution stat. */
1275 struct Params : public DistParams
1277 Params() : DistParams(Dist) {}
1281 /** The minimum value to track. */
1283 /** The maximum value to track. */
1285 /** The number of entries in each bucket. */
1286 Counter bucket_size;
1287 /** The number of buckets. Equal to (max-min)/bucket_size. */
1290 /** The smallest value sampled. */
1292 /** The largest value sampled. */
1294 /** The number of values sampled less than min. */
1296 /** The number of values sampled more than max. */
1298 /** The current sum. */
1300 /** The sum of squares. */
1302 /** The number of samples. */
1304 /** Counter for each bucket. */
1308 DistStor(Info *info)
1309 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1315 * Add a value to the distribution for the given number of times.
1316 * @param val The value to add.
1317 * @param number The number of times to add the value.
1320 sample(Counter val, int number)
1322 if (val < min_track)
1323 underflow += number;
1324 else if (val > max_track)
1328 (size_type)std::floor((val - min_track) / bucket_size);
1329 assert(index < size());
1330 cvec[index] += number;
1339 Counter sample = val * number;
1341 squares += sample * sample;
1346 * Return the number of buckets in this distribution.
1347 * @return the number of buckets.
1349 size_type size() const { return cvec.size(); }
1352 * Returns true if any calls to sample have been made.
1353 * @return True if any values have been sampled.
1358 return samples == Counter();
1362 prepare(Info *info, DistData &data)
1364 const Params *params = safe_cast<const Params *>(info->storageParams);
1366 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1367 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1368 data.underflow = underflow;
1369 data.overflow = overflow;
1371 size_type buckets = params->buckets;
1372 data.cvec.resize(buckets);
1373 for (off_type i = 0; i < buckets; ++i)
1374 data.cvec[i] = cvec[i];
1377 data.squares = squares;
1378 data.samples = samples;
1382 * Reset stat value to default
1387 const Params *params = safe_cast<const Params *>(info->storageParams);
1388 min_track = params->min;
1389 max_track = params->max;
1390 bucket_size = params->bucket_size;
1392 min_val = CounterLimits::max();
1393 max_val = CounterLimits::min();
1394 underflow = Counter();
1395 overflow = Counter();
1397 size_type size = cvec.size();
1398 for (off_type i = 0; i < size; ++i)
1399 cvec[i] = Counter();
1402 squares = Counter();
1403 samples = Counter();
1408 * Templatized storage and interface for a distribution that calculates mean
1414 struct Params : public DistParams
1416 Params() : DistParams(Deviation) {}
1420 /** The current sum. */
1422 /** The sum of squares. */
1424 /** The number of samples. */
1429 * Create and initialize this storage.
1431 SampleStor(Info *info)
1432 : sum(Counter()), squares(Counter()), samples(Counter())
1436 * Add a value the given number of times to this running average.
1437 * Update the running sum and sum of squares, increment the number of
1438 * values seen by the given number.
1439 * @param val The value to add.
1440 * @param number The number of times to add the value.
1443 sample(Counter val, int number)
1445 Counter value = val * number;
1447 squares += value * value;
1452 * Return the number of entries in this stat, 1
1455 size_type size() const { return 1; }
1458 * Return true if no samples have been added.
1459 * @return True if no samples have been added.
1461 bool zero() const { return samples == Counter(); }
1464 prepare(Info *info, DistData &data)
1467 data.squares = squares;
1468 data.samples = samples;
1472 * Reset stat value to default
1478 squares = Counter();
1479 samples = Counter();
1484 * Templatized storage for distribution that calculates per tick mean and
1490 struct Params : public DistParams
1492 Params() : DistParams(Deviation) {}
1496 /** Current total. */
1498 /** Current sum of squares. */
1503 * Create and initialize this storage.
1505 AvgSampleStor(Info *info)
1506 : sum(Counter()), squares(Counter())
1510 * Add a value to the distribution for the given number of times.
1511 * Update the running sum and sum of squares.
1512 * @param val The value to add.
1513 * @param number The number of times to add the value.
1516 sample(Counter val, int number)
1518 Counter value = val * number;
1520 squares += value * value;
1524 * Return the number of entries, in this case 1.
1527 size_type size() const { return 1; }
1530 * Return true if no samples have been added.
1531 * @return True if the sum is zero.
1533 bool zero() const { return sum == Counter(); }
1536 prepare(Info *info, DistData &data)
1539 data.squares = squares;
1540 data.samples = curTick;
1544 * Reset stat value to default
1550 squares = Counter();
1555 * Implementation of a distribution stat. The type of distribution is
1556 * determined by the Storage template. @sa ScalarBase
1558 template <class Derived, class Stor>
1559 class DistBase : public DataWrap<Derived, DistInfoProxy>
1562 typedef DistInfoProxy<Derived> Info;
1563 typedef Stor Storage;
1564 typedef typename Stor::Params Params;
1567 /** The storage for this stat. */
1568 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1572 * Retrieve the storage.
1573 * @return The storage object for this stat.
1578 return reinterpret_cast<Storage *>(storage);
1582 * Retrieve a const pointer to the storage.
1583 * @return A const pointer to the storage object for this stat.
1588 return reinterpret_cast<const Storage *>(storage);
1594 new (storage) Storage(this->info());
1602 * Add a value to the distribtion n times. Calls sample on the storage
1604 * @param v The value to add.
1605 * @param n The number of times to add it, defaults to 1.
1607 template <typename U>
1608 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1611 * Return the number of entries in this stat.
1612 * @return The number of entries.
1614 size_type size() const { return data()->size(); }
1616 * Return true if no samples have been added.
1617 * @return True if there haven't been any samples.
1619 bool zero() const { return data()->zero(); }
1624 Info *info = this->info();
1625 data()->prepare(info, info->data);
1629 * Reset stat value to default
1634 data()->reset(this->info());
1638 template <class Stat>
1641 template <class Derived, class Stor>
1642 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1645 typedef VectorDistInfoProxy<Derived> Info;
1646 typedef Stor Storage;
1647 typedef typename Stor::Params Params;
1648 typedef DistProxy<Derived> Proxy;
1649 friend class DistProxy<Derived>;
1650 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1658 data(off_type index)
1660 return &storage[index];
1664 data(off_type index) const
1666 return &storage[index];
1672 assert(s > 0 && "size must be positive!");
1673 assert(!storage && "already initialized");
1676 char *ptr = new char[_size * sizeof(Storage)];
1677 storage = reinterpret_cast<Storage *>(ptr);
1679 Info *info = this->info();
1680 for (off_type i = 0; i < _size; ++i)
1681 new (&storage[i]) Storage(info);
1696 for (off_type i = 0; i < _size; ++i)
1697 data(i)->~Storage();
1698 delete [] reinterpret_cast<char *>(storage);
1701 Proxy operator[](off_type index);
1714 for (off_type i = 0; i < size(); ++i)
1715 if (!data(i)->zero())
1724 Info *info = this->info();
1725 size_type size = this->size();
1726 info->data.resize(size);
1727 for (off_type i = 0; i < size; ++i)
1728 data(i)->prepare(info, info->data[i]);
1734 return storage != NULL;
1738 template <class Stat>
1746 typename Stat::Storage *data() { return stat->data(index); }
1747 const typename Stat::Storage *data() const { return stat->data(index); }
1750 DistProxy(Stat *s, off_type i)
1754 DistProxy(const DistProxy &sp)
1755 : stat(sp.stat), index(sp.index)
1759 operator=(const DistProxy &sp)
1767 template <typename U>
1769 sample(const U &v, int n = 1)
1771 data()->sample(v, n);
1783 return data()->zero();
1787 * Proxy has no state. Nothing to reset.
1792 template <class Derived, class Stor>
1793 inline typename VectorDistBase<Derived, Stor>::Proxy
1794 VectorDistBase<Derived, Stor>::operator[](off_type index)
1796 assert (index >= 0 && index < size());
1797 typedef typename VectorDistBase<Derived, Stor>::Proxy Proxy;
1798 return Proxy(this, index);
1802 template <class Storage>
1804 VectorDistBase<Storage>::total(off_type index) const
1807 for (off_type i = 0; i < x_size(); ++i)
1808 total += data(i)->result();
1812 //////////////////////////////////////////////////////////////////////
1816 //////////////////////////////////////////////////////////////////////
1819 * Base class for formula statistic node. These nodes are used to build a tree
1820 * that represents the formula.
1822 class Node : public RefCounted
1826 * Return the number of nodes in the subtree starting at this node.
1827 * @return the number of nodes in this subtree.
1829 virtual size_type size() const = 0;
1831 * Return the result vector of this subtree.
1832 * @return The result vector of this subtree.
1834 virtual const VResult &result() const = 0;
1836 * Return the total of the result vector.
1837 * @return The total of the result vector.
1839 virtual Result total() const = 0;
1844 virtual std::string str() const = 0;
1847 /** Reference counting pointer to a function Node. */
1848 typedef RefCountingPtr<Node> NodePtr;
1850 class ScalarStatNode : public Node
1853 const ScalarInfo *data;
1854 mutable VResult vresult;
1857 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
1862 vresult[0] = data->result();
1866 Result total() const { return data->result(); };
1868 size_type size() const { return 1; }
1873 std::string str() const { return data->name; }
1876 template <class Stat>
1877 class ScalarProxyNode : public Node
1880 const ScalarProxy<Stat> proxy;
1881 mutable VResult vresult;
1884 ScalarProxyNode(const ScalarProxy<Stat> &p)
1885 : proxy(p), vresult(1)
1891 vresult[0] = proxy.result();
1898 return proxy.result();
1917 class VectorStatNode : public Node
1920 const VectorInfo *data;
1923 VectorStatNode(const VectorInfo *d) : data(d) { }
1924 const VResult &result() const { return data->result(); }
1925 Result total() const { return data->total(); };
1927 size_type size() const { return data->size(); }
1929 std::string str() const { return data->name; }
1933 class ConstNode : public Node
1939 ConstNode(T s) : vresult(1, (Result)s) {}
1940 const VResult &result() const { return vresult; }
1941 Result total() const { return vresult[0]; };
1942 size_type size() const { return 1; }
1943 std::string str() const { return to_string(vresult[0]); }
1947 class ConstVectorNode : public Node
1953 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
1954 const VResult &result() const { return vresult; }
1959 size_type size = this->size();
1961 for (off_type i = 0; i < size; i++)
1966 size_type size() const { return vresult.size(); }
1970 size_type size = this->size();
1971 std::string tmp = "(";
1972 for (off_type i = 0; i < size; i++)
1973 tmp += csprintf("%s ",to_string(vresult[i]));
1983 struct OpString<std::plus<Result> >
1985 static std::string str() { return "+"; }
1989 struct OpString<std::minus<Result> >
1991 static std::string str() { return "-"; }
1995 struct OpString<std::multiplies<Result> >
1997 static std::string str() { return "*"; }
2001 struct OpString<std::divides<Result> >
2003 static std::string str() { return "/"; }
2007 struct OpString<std::modulus<Result> >
2009 static std::string str() { return "%"; }
2013 struct OpString<std::negate<Result> >
2015 static std::string str() { return "-"; }
2019 class UnaryNode : public Node
2023 mutable VResult vresult;
2026 UnaryNode(NodePtr &p) : l(p) {}
2031 const VResult &lvec = l->result();
2032 size_type size = lvec.size();
2036 vresult.resize(size);
2038 for (off_type i = 0; i < size; ++i)
2039 vresult[i] = op(lvec[i]);
2047 const VResult &vec = this->result();
2049 for (off_type i = 0; i < size(); i++)
2054 size_type size() const { return l->size(); }
2059 return OpString<Op>::str() + l->str();
2064 class BinaryNode : public Node
2069 mutable VResult vresult;
2072 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2078 const VResult &lvec = l->result();
2079 const VResult &rvec = r->result();
2081 assert(lvec.size() > 0 && rvec.size() > 0);
2083 if (lvec.size() == 1 && rvec.size() == 1) {
2085 vresult[0] = op(lvec[0], rvec[0]);
2086 } else if (lvec.size() == 1) {
2087 size_type size = rvec.size();
2088 vresult.resize(size);
2089 for (off_type i = 0; i < size; ++i)
2090 vresult[i] = op(lvec[0], rvec[i]);
2091 } else if (rvec.size() == 1) {
2092 size_type size = lvec.size();
2093 vresult.resize(size);
2094 for (off_type i = 0; i < size; ++i)
2095 vresult[i] = op(lvec[i], rvec[0]);
2096 } else if (rvec.size() == lvec.size()) {
2097 size_type size = rvec.size();
2098 vresult.resize(size);
2099 for (off_type i = 0; i < size; ++i)
2100 vresult[i] = op(lvec[i], rvec[i]);
2109 const VResult &vec = this->result();
2111 for (off_type i = 0; i < size(); i++)
2119 size_type ls = l->size();
2120 size_type rs = r->size();
2123 } else if (rs == 1) {
2126 assert(ls == rs && "Node vector sizes are not equal");
2134 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2139 class SumNode : public Node
2143 mutable VResult vresult;
2146 SumNode(NodePtr &p) : l(p), vresult(1) {}
2151 const VResult &lvec = l->result();
2152 size_type size = lvec.size();
2158 for (off_type i = 0; i < size; ++i)
2159 vresult[0] = op(vresult[0], lvec[i]);
2167 const VResult &lvec = l->result();
2168 size_type size = lvec.size();
2171 Result vresult = 0.0;
2174 for (off_type i = 0; i < size; ++i)
2175 vresult = op(vresult, lvec[i]);
2180 size_type size() const { return 1; }
2185 return csprintf("total(%s)", l->str());
2190 //////////////////////////////////////////////////////////////////////
2192 // Visible Statistics Types
2194 //////////////////////////////////////////////////////////////////////
2196 * @defgroup VisibleStats "Statistic Types"
2197 * These are the statistics that are used in the simulator.
2202 * This is a simple scalar statistic, like a counter.
2203 * @sa Stat, ScalarBase, StatStor
2205 class Scalar : public ScalarBase<Scalar, StatStor>
2208 using ScalarBase<Scalar, StatStor>::operator=;
2212 * A stat that calculates the per tick average of a value.
2213 * @sa Stat, ScalarBase, AvgStor
2215 class Average : public ScalarBase<Average, AvgStor>
2218 using ScalarBase<Average, AvgStor>::operator=;
2221 class Value : public ValueBase<Value>
2226 * A vector of scalar stats.
2227 * @sa Stat, VectorBase, StatStor
2229 class Vector : public VectorBase<Vector, StatStor>
2234 * A vector of Average stats.
2235 * @sa Stat, VectorBase, AvgStor
2237 class AverageVector : public VectorBase<AverageVector, AvgStor>
2242 * A 2-Dimensional vecto of scalar stats.
2243 * @sa Stat, Vector2dBase, StatStor
2245 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2250 * A simple distribution stat.
2251 * @sa Stat, DistBase, DistStor
2253 class Distribution : public DistBase<Distribution, DistStor>
2257 * Set the parameters of this distribution. @sa DistStor::Params
2258 * @param min The minimum value of the distribution.
2259 * @param max The maximum value of the distribution.
2260 * @param bkt The number of values in each bucket.
2261 * @return A reference to this distribution.
2264 init(Counter min, Counter max, Counter bkt)
2266 DistStor::Params *params = new DistStor::Params;
2269 params->bucket_size = bkt;
2270 params->buckets = (size_type)rint((max - min) / bkt + 1.0);
2271 this->setParams(params);
2273 return this->self();
2278 * Calculates the mean and variance of all the samples.
2279 * @sa DistBase, SampleStor
2281 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2285 * Construct and initialize this distribution.
2294 * Calculates the per tick mean and variance of the samples.
2295 * @sa DistBase, AvgSampleStor
2297 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2301 * Construct and initialize this distribution.
2310 * A vector of distributions.
2311 * @sa VectorDistBase, DistStor
2313 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2317 * Initialize storage and parameters for this distribution.
2318 * @param size The size of the vector (the number of distributions).
2319 * @param min The minimum value of the distribution.
2320 * @param max The maximum value of the distribution.
2321 * @param bkt The number of values in each bucket.
2322 * @return A reference to this distribution.
2324 VectorDistribution &
2325 init(size_type size, Counter min, Counter max, Counter bkt)
2327 DistStor::Params *params = new DistStor::Params;
2330 params->bucket_size = bkt;
2331 params->buckets = (size_type)rint((max - min) / bkt + 1.0);
2332 this->setParams(params);
2334 return this->self();
2339 * This is a vector of StandardDeviation stats.
2340 * @sa VectorDistBase, SampleStor
2342 class VectorStandardDeviation
2343 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2347 * Initialize storage for this distribution.
2348 * @param size The size of the vector.
2349 * @return A reference to this distribution.
2351 VectorStandardDeviation &
2352 init(size_type size)
2355 return this->self();
2360 * This is a vector of AverageDeviation stats.
2361 * @sa VectorDistBase, AvgSampleStor
2363 class VectorAverageDeviation
2364 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2368 * Initialize storage for this distribution.
2369 * @param size The size of the vector.
2370 * @return A reference to this distribution.
2372 VectorAverageDeviation &
2373 init(size_type size)
2376 return this->self();
2380 template <class Stat>
2381 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2384 mutable VResult vec;
2385 mutable VCounter cvec;
2388 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2390 size_type size() const { return this->s.size(); }
2395 this->s.result(vec);
2398 Result total() const { return this->s.total(); }
2399 VCounter &value() const { return cvec; }
2401 std::string str() const { return this->s.str(); }
2406 * A formula for statistics that is calculated when printed. A formula is
2407 * stored as a tree of Nodes that represent the equation to calculate.
2408 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2410 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2413 /** The root of the tree which represents the Formula */
2419 * Create and initialize thie formula, and register it with the database.
2424 * Create a formula with the given root node, register it with the
2426 * @param r The root of the expression tree.
2431 * Set an unitialized Formula to the given root.
2432 * @param r The root of the expression tree.
2433 * @return a reference to this formula.
2435 const Formula &operator=(Temp r);
2438 * Add the given tree to the existing one.
2439 * @param r The root of the expression tree.
2440 * @return a reference to this formula.
2442 const Formula &operator+=(Temp r);
2444 * Return the result of the Fomula in a vector. If there were no Vector
2445 * components to the Formula, then the vector is size 1. If there were,
2446 * like x/y with x being a vector of size 3, then the result returned will
2447 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2448 * @return The result vector.
2450 void result(VResult &vec) const;
2453 * Return the total Formula result. If there is a Vector
2454 * component to this Formula, then this is the result of the
2455 * Formula if the formula is applied after summing all the
2456 * components of the Vector. For example, if Formula is x/y where
2457 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2458 * there is no Vector component, total() returns the same value as
2459 * the first entry in the VResult val() returns.
2460 * @return The total of the result vector.
2462 Result total() const;
2465 * Return the number of elements in the tree.
2467 size_type size() const;
2472 * Formulas don't need to be reset
2481 std::string str() const;
2484 class FormulaNode : public Node
2487 const Formula &formula;
2488 mutable VResult vec;
2491 FormulaNode(const Formula &f) : formula(f) {}
2493 size_type size() const { return formula.size(); }
2494 const VResult &result() const { formula.result(vec); return vec; }
2495 Result total() const { return formula.total(); }
2497 std::string str() const { return formula.str(); }
2501 * Helper class to construct formula node trees.
2507 * Pointer to a Node object.
2513 * Copy the given pointer to this class.
2514 * @param n A pointer to a Node object to copy.
2516 Temp(NodePtr n) : node(n) { }
2519 * Return the node pointer.
2520 * @return the node pointer.
2522 operator NodePtr&() { return node; }
2526 * Create a new ScalarStatNode.
2527 * @param s The ScalarStat to place in a node.
2529 Temp(const Scalar &s)
2530 : node(new ScalarStatNode(s.info()))
2534 * Create a new ScalarStatNode.
2535 * @param s The ScalarStat to place in a node.
2537 Temp(const Value &s)
2538 : node(new ScalarStatNode(s.info()))
2542 * Create a new ScalarStatNode.
2543 * @param s The ScalarStat to place in a node.
2545 Temp(const Average &s)
2546 : node(new ScalarStatNode(s.info()))
2550 * Create a new VectorStatNode.
2551 * @param s The VectorStat to place in a node.
2553 Temp(const Vector &s)
2554 : node(new VectorStatNode(s.info()))
2557 Temp(const AverageVector &s)
2558 : node(new VectorStatNode(s.info()))
2564 Temp(const Formula &f)
2565 : node(new FormulaNode(f))
2569 * Create a new ScalarProxyNode.
2570 * @param p The ScalarProxy to place in a node.
2572 template <class Stat>
2573 Temp(const ScalarProxy<Stat> &p)
2574 : node(new ScalarProxyNode<Stat>(p))
2578 * Create a ConstNode
2579 * @param value The value of the const node.
2581 Temp(signed char value)
2582 : node(new ConstNode<signed char>(value))
2586 * Create a ConstNode
2587 * @param value The value of the const node.
2589 Temp(unsigned char value)
2590 : node(new ConstNode<unsigned char>(value))
2594 * Create a ConstNode
2595 * @param value The value of the const node.
2597 Temp(signed short value)
2598 : node(new ConstNode<signed short>(value))
2602 * Create a ConstNode
2603 * @param value The value of the const node.
2605 Temp(unsigned short value)
2606 : node(new ConstNode<unsigned short>(value))
2610 * Create a ConstNode
2611 * @param value The value of the const node.
2613 Temp(signed int value)
2614 : node(new ConstNode<signed int>(value))
2618 * Create a ConstNode
2619 * @param value The value of the const node.
2621 Temp(unsigned int value)
2622 : node(new ConstNode<unsigned int>(value))
2626 * Create a ConstNode
2627 * @param value The value of the const node.
2629 Temp(signed long value)
2630 : node(new ConstNode<signed long>(value))
2634 * Create a ConstNode
2635 * @param value The value of the const node.
2637 Temp(unsigned long value)
2638 : node(new ConstNode<unsigned long>(value))
2642 * Create a ConstNode
2643 * @param value The value of the const node.
2645 Temp(signed long long value)
2646 : node(new ConstNode<signed long long>(value))
2650 * Create a ConstNode
2651 * @param value The value of the const node.
2653 Temp(unsigned long long value)
2654 : node(new ConstNode<unsigned long long>(value))
2658 * Create a ConstNode
2659 * @param value The value of the const node.
2662 : node(new ConstNode<float>(value))
2666 * Create a ConstNode
2667 * @param value The value of the const node.
2670 : node(new ConstNode<double>(value))
2680 operator+(Temp l, Temp r)
2682 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
2686 operator-(Temp l, Temp r)
2688 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
2692 operator*(Temp l, Temp r)
2694 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
2698 operator/(Temp l, Temp r)
2700 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
2706 return NodePtr(new UnaryNode<std::negate<Result> >(l));
2709 template <typename T>
2713 return NodePtr(new ConstNode<T>(val));
2716 template <typename T>
2718 constantVector(T val)
2720 return NodePtr(new ConstVectorNode<T>(val));
2726 return NodePtr(new SumNode<std::plus<Result> >(val));
2730 * Enable the statistics package. Before the statistics package is
2731 * enabled, all statistics must be created and initialized and once
2732 * the package is enabled, no more statistics can be created.
2737 * Prepare all stats for data access. This must be done before
2738 * dumping and serialization.
2743 * Dump all statistics data to the registered outputs
2748 * Reset all statistics to the base state
2752 * Register a callback that should be called whenever statistics are
2755 void registerResetCallback(Callback *cb);
2757 std::list<Info *> &statsList();
2759 /* namespace Stats */ }
2761 #endif // __BASE_STATISTICS_HH__