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__
63 #include "base/stats/info.hh"
64 #include "base/stats/output.hh"
65 #include "base/stats/types.hh"
66 #include "base/cast.hh"
67 #include "base/cprintf.hh"
68 #include "base/intmath.hh"
69 #include "base/refcnt.hh"
70 #include "base/str.hh"
71 #include "base/types.hh"
75 /** The current simulated tick. */
76 extern Tick curTick();
78 /* A namespace for all of the Statistics */
81 template <class Stat, class Base>
82 class InfoProxy : public Base
88 InfoProxy(Stat &stat) : s(stat) {}
90 bool check() const { return s.check(); }
91 void prepare() { s.prepare(); }
92 void reset() { s.reset(); }
94 visit(Output &visitor)
96 visitor.visit(*static_cast<Base *>(this));
98 bool zero() const { return s.zero(); }
101 template <class Stat>
102 class ScalarInfoProxy : public InfoProxy<Stat, ScalarInfo>
105 ScalarInfoProxy(Stat &stat) : InfoProxy<Stat, ScalarInfo>(stat) {}
107 Counter value() const { return this->s.value(); }
108 Result result() const { return this->s.result(); }
109 Result total() const { return this->s.total(); }
112 template <class Stat>
113 class VectorInfoProxy : public InfoProxy<Stat, VectorInfo>
116 mutable VCounter cvec;
117 mutable VResult rvec;
120 VectorInfoProxy(Stat &stat) : InfoProxy<Stat, VectorInfo>(stat) {}
122 size_type size() const { return this->s.size(); }
134 this->s.result(rvec);
138 Result total() const { return this->s.total(); }
141 template <class Stat>
142 class DistInfoProxy : public InfoProxy<Stat, DistInfo>
145 DistInfoProxy(Stat &stat) : InfoProxy<Stat, DistInfo>(stat) {}
148 template <class Stat>
149 class VectorDistInfoProxy : public InfoProxy<Stat, VectorDistInfo>
152 VectorDistInfoProxy(Stat &stat) : InfoProxy<Stat, VectorDistInfo>(stat) {}
154 size_type size() const { return this->s.size(); }
157 template <class Stat>
158 class Vector2dInfoProxy : public InfoProxy<Stat, Vector2dInfo>
161 Vector2dInfoProxy(Stat &stat) : InfoProxy<Stat, Vector2dInfo>(stat) {}
166 virtual ~StorageParams();
172 /** Set up an info class for this statistic */
173 void setInfo(Info *info);
174 /** Save Storage class parameters if any */
175 void setParams(const StorageParams *params);
176 /** Save Storage class parameters if any */
179 /** Grab the information class for this statistic */
181 /** Grab the information class for this statistic */
182 const Info *info() const;
186 * Reset the stat to the default state.
191 * @return true if this stat has a value and satisfies its
192 * requirement as a prereq
194 bool zero() const { return true; }
197 * Check that this stat has been set up properly and is ready for
199 * @return true for success
201 bool check() const { return true; }
204 template <class Derived, template <class> class InfoProxyType>
205 class DataWrap : public InfoAccess
208 typedef InfoProxyType<Derived> Info;
211 Derived &self() { return *static_cast<Derived *>(this); }
217 return safe_cast<Info *>(InfoAccess::info());
224 return safe_cast<const Info *>(InfoAccess::info());
229 * Copy constructor, copies are not allowed.
231 DataWrap(const DataWrap &stat);
236 void operator=(const DataWrap &);
241 this->setInfo(new Info(self()));
245 * Set the name and marks this stat to print at the end of simulation.
246 * @param name The new name.
247 * @return A reference to this stat.
250 name(const std::string &name)
252 Info *info = this->info();
254 info->flags.set(display);
257 const std::string &name() const { return this->info()->name; }
260 * Set the character(s) used between the name and vector number
261 * on vectors, dist, etc.
262 * @param _sep The new separator string
263 * @return A reference to this stat.
266 setSeparator(const std::string &_sep)
268 this->info()->setSeparator(_sep);
271 const std::string &setSeparator() const
273 return this->info()->separatorString;
277 * Set the description and marks this stat to print at the end of
279 * @param desc The new description.
280 * @return A reference to this stat.
283 desc(const std::string &_desc)
285 this->info()->desc = _desc;
290 * Set the precision and marks this stat to print at the end of simulation.
291 * @param _precision The new precision
292 * @return A reference to this stat.
295 precision(int _precision)
297 this->info()->precision = _precision;
302 * Set the flags and marks this stat to print at the end of simulation.
303 * @param f The new flags.
304 * @return A reference to this stat.
309 this->info()->flags.set(_flags);
314 * Set the prerequisite stat and marks this stat to print at the end of
316 * @param prereq The prerequisite stat.
317 * @return A reference to this stat.
319 template <class Stat>
321 prereq(const Stat &prereq)
323 this->info()->prereq = prereq.info();
328 template <class Derived, template <class> class InfoProxyType>
329 class DataWrapVec : public DataWrap<Derived, InfoProxyType>
332 typedef InfoProxyType<Derived> Info;
337 DataWrapVec(const DataWrapVec &ref)
340 void operator=(const DataWrapVec &)
343 // The following functions are specific to vectors. If you use them
344 // in a non vector context, you will get a nice compiler error!
347 * Set the subfield name for the given index, and marks this stat to print
348 * at the end of simulation.
349 * @param index The subfield index.
350 * @param name The new name of the subfield.
351 * @return A reference to this stat.
354 subname(off_type index, const std::string &name)
356 Derived &self = this->self();
357 Info *info = self.info();
359 std::vector<std::string> &subn = info->subnames;
360 if (subn.size() <= index)
361 subn.resize(index + 1);
366 // The following functions are specific to 2d vectors. If you use
367 // them in a non vector context, you will get a nice compiler
368 // error because info doesn't have the right variables.
371 * Set the subfield description for the given index and marks this stat to
372 * print at the end of simulation.
373 * @param index The subfield index.
374 * @param desc The new description of the subfield
375 * @return A reference to this stat.
378 subdesc(off_type index, const std::string &desc)
380 Info *info = this->info();
382 std::vector<std::string> &subd = info->subdescs;
383 if (subd.size() <= index)
384 subd.resize(index + 1);
393 Derived &self = this->self();
394 Info *info = this->info();
396 size_t size = self.size();
397 for (off_type i = 0; i < size; ++i)
398 self.data(i)->prepare(info);
404 Derived &self = this->self();
405 Info *info = this->info();
407 size_t size = self.size();
408 for (off_type i = 0; i < size; ++i)
409 self.data(i)->reset(info);
413 template <class Derived, template <class> class InfoProxyType>
414 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
417 typedef InfoProxyType<Derived> Info;
420 * @warning This makes the assumption that if you're gonna subnames a 2d
421 * vector, you're subnaming across all y
424 ysubnames(const char **names)
426 Derived &self = this->self();
427 Info *info = this->info();
429 info->y_subnames.resize(self.y);
430 for (off_type i = 0; i < self.y; ++i)
431 info->y_subnames[i] = names[i];
436 ysubname(off_type index, const std::string &subname)
438 Derived &self = this->self();
439 Info *info = this->info();
441 assert(index < self.y);
442 info->y_subnames.resize(self.y);
443 info->y_subnames[index] = subname.c_str();
448 ysubname(off_type i) const
450 return this->info()->y_subnames[i];
455 //////////////////////////////////////////////////////////////////////
459 //////////////////////////////////////////////////////////////////////
462 * Templatized storage and interface for a simple scalar stat.
467 /** The statistic value. */
471 struct Params : public StorageParams {};
475 * Builds this storage element and calls the base constructor of the
483 * The the stat to the given value.
484 * @param val The new value.
486 void set(Counter val) { data = val; }
488 * Increment the stat by the given value.
489 * @param val The new value.
491 void inc(Counter val) { data += val; }
493 * Decrement the stat by the given value.
494 * @param val The new value.
496 void dec(Counter val) { data -= val; }
498 * Return the value of this stat as its base type.
499 * @return The value of this stat.
501 Counter value() const { return data; }
503 * Return the value of this stat as a result type.
504 * @return The value of this stat.
506 Result result() const { return (Result)data; }
508 * Prepare stat data for dumping or serialization
510 void prepare(Info *info) { }
512 * Reset stat value to default
514 void reset(Info *info) { data = Counter(); }
517 * @return true if zero value
519 bool zero() const { return data == Counter(); }
523 * Templatized storage and interface to a per-tick average stat. This keeps
524 * a current count and updates a total (count * ticks) when this count
525 * changes. This allows the quick calculation of a per tick count of the item
526 * being watched. This is good for keeping track of residencies in structures
527 * among other things.
532 /** The current count. */
534 /** The tick of the last reset */
536 /** The total count for all tick. */
537 mutable Result total;
538 /** The tick that current last changed. */
542 struct Params : public StorageParams {};
546 * Build and initializes this stat storage.
549 : current(0), lastReset(0), total(0), last(0)
553 * Set the current count to the one provided, update the total and last
555 * @param val The new count.
560 total += current * (curTick() - last);
566 * Increment the current count by the provided value, calls set.
567 * @param val The amount to increment.
569 void inc(Counter val) { set(current + val); }
572 * Deccrement the current count by the provided value, calls set.
573 * @param val The amount to decrement.
575 void dec(Counter val) { set(current - val); }
578 * Return the current count.
579 * @return The current count.
581 Counter value() const { return current; }
584 * Return the current average.
585 * @return The current average.
590 assert(last == curTick());
591 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
595 * @return true if zero value
597 bool zero() const { return total == 0.0; }
600 * Prepare stat data for dumping or serialization
605 total += current * (curTick() - last);
610 * Reset stat value to default
617 lastReset = curTick();
623 * Implementation of a scalar stat. The type of stat is determined by the
626 template <class Derived, class Stor>
627 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
630 typedef Stor Storage;
631 typedef typename Stor::Params Params;
634 /** The storage of this stat. */
635 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
639 * Retrieve the storage.
640 * @param index The vector index to access.
641 * @return The storage object at the given index.
646 return reinterpret_cast<Storage *>(storage);
650 * Retrieve a const pointer to the storage.
651 * for the given index.
652 * @param index The vector index to access.
653 * @return A const pointer to the storage object at the given index.
658 return reinterpret_cast<const Storage *>(storage);
664 new (storage) Storage(this->info());
670 * Return the current value of this stat as its base type.
671 * @return The current value.
673 Counter value() const { return data()->value(); }
682 // Common operators for stats
684 * Increment the stat by 1. This calls the associated storage object inc
687 void operator++() { data()->inc(1); }
689 * Decrement the stat by 1. This calls the associated storage object dec
692 void operator--() { data()->dec(1); }
694 /** Increment the stat by 1. */
695 void operator++(int) { ++*this; }
696 /** Decrement the stat by 1. */
697 void operator--(int) { --*this; }
700 * Set the data value to the given value. This calls the associated storage
701 * object set function.
702 * @param v The new value.
704 template <typename U>
705 void operator=(const U &v) { data()->set(v); }
708 * Increment the stat by the given value. This calls the associated
709 * storage object inc function.
710 * @param v The value to add.
712 template <typename U>
713 void operator+=(const U &v) { data()->inc(v); }
716 * Decrement the stat by the given value. This calls the associated
717 * storage object dec function.
718 * @param v The value to substract.
720 template <typename U>
721 void operator-=(const U &v) { data()->dec(v); }
724 * Return the number of elements, always 1 for a scalar.
727 size_type size() const { return 1; }
729 Counter value() { return data()->value(); }
731 Result result() { return data()->result(); }
733 Result total() { return result(); }
735 bool zero() { return result() == 0.0; }
737 void reset() { data()->reset(this->info()); }
738 void prepare() { data()->prepare(this->info()); }
741 class ProxyInfo : public ScalarInfo
744 std::string str() const { return to_string(value()); }
745 size_type size() const { return 1; }
746 bool check() const { return true; }
749 bool zero() const { return value() == 0; }
751 void visit(Output &visitor) { visitor.visit(*this); }
755 class ValueProxy : public ProxyInfo
761 ValueProxy(T &val) : scalar(&val) {}
762 Counter value() const { return *scalar; }
763 Result result() const { return *scalar; }
764 Result total() const { return *scalar; }
768 class FunctorProxy : public ProxyInfo
774 FunctorProxy(T &func) : functor(&func) {}
775 Counter value() const { return (*functor)(); }
776 Result result() const { return (*functor)(); }
777 Result total() const { return (*functor)(); }
780 template <class Derived>
781 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
787 ValueBase() : proxy(NULL) { }
788 ~ValueBase() { if (proxy) delete proxy; }
794 proxy = new ValueProxy<T>(value);
803 proxy = new FunctorProxy<T>(func);
808 Counter value() { return proxy->value(); }
809 Result result() const { return proxy->result(); }
810 Result total() const { return proxy->total(); };
811 size_type size() const { return proxy->size(); }
813 std::string str() const { return proxy->str(); }
814 bool zero() const { return proxy->zero(); }
815 bool check() const { return proxy != NULL; }
820 //////////////////////////////////////////////////////////////////////
824 //////////////////////////////////////////////////////////////////////
827 * A proxy class to access the stat at a given index in a VectorBase stat.
828 * Behaves like a ScalarBase.
830 template <class Stat>
834 /** Pointer to the parent Vector. */
837 /** The index to access in the parent VectorBase. */
842 * Return the current value of this stat as its base type.
843 * @return The current value.
845 Counter value() const { return stat.data(index)->value(); }
848 * Return the current value of this statas a result type.
849 * @return The current value.
851 Result result() const { return stat.data(index)->result(); }
855 * Create and initialize this proxy, do not register it with the database.
856 * @param i The index to access.
858 ScalarProxy(Stat &s, off_type i)
864 * Create a copy of the provided ScalarProxy.
865 * @param sp The proxy to copy.
867 ScalarProxy(const ScalarProxy &sp)
868 : stat(sp.stat), index(sp.index)
872 * Set this proxy equal to the provided one.
873 * @param sp The proxy to copy.
874 * @return A reference to this proxy.
877 operator=(const ScalarProxy &sp)
885 // Common operators for stats
887 * Increment the stat by 1. This calls the associated storage object inc
890 void operator++() { stat.data(index)->inc(1); }
892 * Decrement the stat by 1. This calls the associated storage object dec
895 void operator--() { stat.data(index)->dec(1); }
897 /** Increment the stat by 1. */
898 void operator++(int) { ++*this; }
899 /** Decrement the stat by 1. */
900 void operator--(int) { --*this; }
903 * Set the data value to the given value. This calls the associated storage
904 * object set function.
905 * @param v The new value.
907 template <typename U>
909 operator=(const U &v)
911 stat.data(index)->set(v);
915 * Increment the stat by the given value. This calls the associated
916 * storage object inc function.
917 * @param v The value to add.
919 template <typename U>
921 operator+=(const U &v)
923 stat.data(index)->inc(v);
927 * Decrement the stat by the given value. This calls the associated
928 * storage object dec function.
929 * @param v The value to substract.
931 template <typename U>
933 operator-=(const U &v)
935 stat.data(index)->dec(v);
939 * Return the number of elements, always 1 for a scalar.
942 size_type size() const { return 1; }
948 return csprintf("%s[%d]", stat.info()->name, index);
953 * Implementation of a vector of stats. The type of stat is determined by the
954 * Storage class. @sa ScalarBase
956 template <class Derived, class Stor>
957 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
960 typedef Stor Storage;
961 typedef typename Stor::Params Params;
964 typedef ScalarProxy<Derived> Proxy;
965 friend class ScalarProxy<Derived>;
966 friend class DataWrapVec<Derived, VectorInfoProxy>;
969 /** The storage of this stat. */
975 * Retrieve the storage.
976 * @param index The vector index to access.
977 * @return The storage object at the given index.
979 Storage *data(off_type index) { return &storage[index]; }
982 * Retrieve a const pointer to the storage.
983 * @param index The vector index to access.
984 * @return A const pointer to the storage object at the given index.
986 const Storage *data(off_type index) const { return &storage[index]; }
991 assert(s > 0 && "size must be positive!");
992 assert(!storage && "already initialized");
995 char *ptr = new char[_size * sizeof(Storage)];
996 storage = reinterpret_cast<Storage *>(ptr);
998 for (off_type i = 0; i < _size; ++i)
999 new (&storage[i]) Storage(this->info());
1006 value(VCounter &vec) const
1009 for (off_type i = 0; i < size(); ++i)
1010 vec[i] = data(i)->value();
1014 * Copy the values to a local vector and return a reference to it.
1015 * @return A reference to a vector of the stat values.
1018 result(VResult &vec) const
1021 for (off_type i = 0; i < size(); ++i)
1022 vec[i] = data(i)->result();
1026 * Return a total of all entries in this vector.
1027 * @return The total of all vector entries.
1033 for (off_type i = 0; i < size(); ++i)
1034 total += data(i)->result();
1039 * @return the number of elements in this vector.
1041 size_type size() const { return _size; }
1046 for (off_type i = 0; i < size(); ++i)
1047 if (data(i)->zero())
1055 return storage != NULL;
1068 for (off_type i = 0; i < _size; ++i)
1069 data(i)->~Storage();
1070 delete [] reinterpret_cast<char *>(storage);
1074 * Set this vector to have the given size.
1075 * @param size The new size.
1076 * @return A reference to this stat.
1079 init(size_type size)
1081 Derived &self = this->self();
1087 * Return a reference (ScalarProxy) to the stat at the given index.
1088 * @param index The vector index to access.
1089 * @return A reference of the stat.
1092 operator[](off_type index)
1094 assert (index >= 0 && index < size());
1095 return Proxy(this->self(), index);
1099 template <class Stat>
1108 mutable VResult vec;
1110 typename Stat::Storage *
1111 data(off_type index)
1113 assert(index < len);
1114 return stat.data(offset + index);
1117 const typename Stat::Storage *
1118 data(off_type index) const
1120 assert(index < len);
1121 return stat.data(offset + index);
1130 for (off_type i = 0; i < size(); ++i)
1131 vec[i] = data(i)->result();
1140 for (off_type i = 0; i < size(); ++i)
1141 total += data(i)->result();
1146 VectorProxy(Stat &s, off_type o, size_type l)
1147 : stat(s), offset(o), len(l)
1151 VectorProxy(const VectorProxy &sp)
1152 : stat(sp.stat), offset(sp.offset), len(sp.len)
1157 operator=(const VectorProxy &sp)
1166 operator[](off_type index)
1168 assert (index >= 0 && index < size());
1169 return ScalarProxy<Stat>(stat, offset + index);
1172 size_type size() const { return len; }
1175 template <class Derived, class Stor>
1176 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1179 typedef Vector2dInfoProxy<Derived> Info;
1180 typedef Stor Storage;
1181 typedef typename Stor::Params Params;
1182 typedef VectorProxy<Derived> Proxy;
1183 friend class ScalarProxy<Derived>;
1184 friend class VectorProxy<Derived>;
1185 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1186 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1195 Storage *data(off_type index) { return &storage[index]; }
1196 const Storage *data(off_type index) const { return &storage[index]; }
1208 for (off_type i = 0; i < _size; ++i)
1209 data(i)->~Storage();
1210 delete [] reinterpret_cast<char *>(storage);
1214 init(size_type _x, size_type _y)
1216 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1217 assert(!storage && "already initialized");
1219 Derived &self = this->self();
1220 Info *info = this->info();
1228 char *ptr = new char[_size * sizeof(Storage)];
1229 storage = reinterpret_cast<Storage *>(ptr);
1231 for (off_type i = 0; i < _size; ++i)
1232 new (&storage[i]) Storage(info);
1240 operator[](off_type index)
1242 off_type offset = index * y;
1243 assert (index >= 0 && offset + y <= size());
1244 return Proxy(this->self(), offset, y);
1257 return data(0)->zero();
1259 for (off_type i = 0; i < size(); ++i)
1260 if (!data(i)->zero())
1269 Info *info = this->info();
1270 size_type size = this->size();
1272 for (off_type i = 0; i < size; ++i)
1273 data(i)->prepare(info);
1275 info->cvec.resize(size);
1276 for (off_type i = 0; i < size; ++i)
1277 info->cvec[i] = data(i)->value();
1281 * Reset stat value to default
1286 Info *info = this->info();
1287 size_type size = this->size();
1288 for (off_type i = 0; i < size; ++i)
1289 data(i)->reset(info);
1295 return storage != NULL;
1299 //////////////////////////////////////////////////////////////////////
1301 // Non formula statistics
1303 //////////////////////////////////////////////////////////////////////
1304 /** The parameters for a distribution stat. */
1305 struct DistParams : public StorageParams
1307 const DistType type;
1308 DistParams(DistType t) : type(t) {}
1312 * Templatized storage and interface for a distrbution stat.
1317 /** The parameters for a distribution stat. */
1318 struct Params : public DistParams
1320 /** The minimum value to track. */
1322 /** The maximum value to track. */
1324 /** The number of entries in each bucket. */
1325 Counter bucket_size;
1326 /** The number of buckets. Equal to (max-min)/bucket_size. */
1329 Params() : DistParams(Dist) {}
1333 /** The minimum value to track. */
1335 /** The maximum value to track. */
1337 /** The number of entries in each bucket. */
1338 Counter bucket_size;
1340 /** The smallest value sampled. */
1342 /** The largest value sampled. */
1344 /** The number of values sampled less than min. */
1346 /** The number of values sampled more than max. */
1348 /** The current sum. */
1350 /** The sum of squares. */
1352 /** The number of samples. */
1354 /** Counter for each bucket. */
1358 DistStor(Info *info)
1359 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1365 * Add a value to the distribution for the given number of times.
1366 * @param val The value to add.
1367 * @param number The number of times to add the value.
1370 sample(Counter val, int number)
1372 if (val < min_track)
1373 underflow += number;
1374 else if (val > max_track)
1378 (size_type)std::floor((val - min_track) / bucket_size);
1379 assert(index < size());
1380 cvec[index] += number;
1389 sum += val * number;
1390 squares += val * val * number;
1395 * Return the number of buckets in this distribution.
1396 * @return the number of buckets.
1398 size_type size() const { return cvec.size(); }
1401 * Returns true if any calls to sample have been made.
1402 * @return True if any values have been sampled.
1407 return samples == Counter();
1411 prepare(Info *info, DistData &data)
1413 const Params *params = safe_cast<const Params *>(info->storageParams);
1415 assert(params->type == Dist);
1416 data.type = params->type;
1417 data.min = params->min;
1418 data.max = params->max;
1419 data.bucket_size = params->bucket_size;
1421 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1422 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1423 data.underflow = underflow;
1424 data.overflow = overflow;
1426 data.cvec.resize(params->buckets);
1427 for (off_type i = 0; i < params->buckets; ++i)
1428 data.cvec[i] = cvec[i];
1431 data.squares = squares;
1432 data.samples = samples;
1436 * Reset stat value to default
1441 const Params *params = safe_cast<const Params *>(info->storageParams);
1442 min_track = params->min;
1443 max_track = params->max;
1444 bucket_size = params->bucket_size;
1446 min_val = CounterLimits::max();
1447 max_val = CounterLimits::min();
1448 underflow = Counter();
1449 overflow = Counter();
1451 size_type size = cvec.size();
1452 for (off_type i = 0; i < size; ++i)
1453 cvec[i] = Counter();
1456 squares = Counter();
1457 samples = Counter();
1462 * Templatized storage and interface for a histogram stat.
1467 /** The parameters for a distribution stat. */
1468 struct Params : public DistParams
1470 /** The number of buckets.. */
1473 Params() : DistParams(Hist) {}
1477 /** The minimum value to track. */
1479 /** The maximum value to track. */
1481 /** The number of entries in each bucket. */
1482 Counter bucket_size;
1484 /** The current sum. */
1486 /** The sum of logarithm of each sample, used to compute geometric mean. */
1488 /** The sum of squares. */
1490 /** The number of samples. */
1492 /** Counter for each bucket. */
1496 HistStor(Info *info)
1497 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1504 void grow_convert();
1507 * Add a value to the distribution for the given number of times.
1508 * @param val The value to add.
1509 * @param number The number of times to add the value.
1512 sample(Counter val, int number)
1514 assert(min_bucket < max_bucket);
1515 if (val < min_bucket) {
1516 if (min_bucket == 0)
1519 while (val < min_bucket)
1521 } else if (val >= max_bucket + bucket_size) {
1522 if (min_bucket == 0) {
1523 while (val >= max_bucket + bucket_size)
1526 while (val >= max_bucket + bucket_size)
1532 (int64_t)std::floor((val - min_bucket) / bucket_size);
1534 assert(index >= 0 && index < size());
1535 cvec[index] += number;
1537 sum += val * number;
1538 squares += val * val * number;
1539 logs += log(val) * number;
1544 * Return the number of buckets in this distribution.
1545 * @return the number of buckets.
1547 size_type size() const { return cvec.size(); }
1550 * Returns true if any calls to sample have been made.
1551 * @return True if any values have been sampled.
1556 return samples == Counter();
1560 prepare(Info *info, DistData &data)
1562 const Params *params = safe_cast<const Params *>(info->storageParams);
1564 assert(params->type == Hist);
1565 data.type = params->type;
1566 data.min = min_bucket;
1567 data.max = max_bucket + bucket_size - 1;
1568 data.bucket_size = bucket_size;
1570 data.min_val = min_bucket;
1571 data.max_val = max_bucket;
1573 int buckets = params->buckets;
1574 data.cvec.resize(buckets);
1575 for (off_type i = 0; i < buckets; ++i)
1576 data.cvec[i] = cvec[i];
1580 data.squares = squares;
1581 data.samples = samples;
1585 * Reset stat value to default
1590 const Params *params = safe_cast<const Params *>(info->storageParams);
1592 max_bucket = params->buckets - 1;
1595 size_type size = cvec.size();
1596 for (off_type i = 0; i < size; ++i)
1597 cvec[i] = Counter();
1600 squares = Counter();
1601 samples = Counter();
1607 * Templatized storage and interface for a distribution that calculates mean
1613 struct Params : public DistParams
1615 Params() : DistParams(Deviation) {}
1619 /** The current sum. */
1621 /** The sum of squares. */
1623 /** The number of samples. */
1628 * Create and initialize this storage.
1630 SampleStor(Info *info)
1631 : sum(Counter()), squares(Counter()), samples(Counter())
1635 * Add a value the given number of times to this running average.
1636 * Update the running sum and sum of squares, increment the number of
1637 * values seen by the given number.
1638 * @param val The value to add.
1639 * @param number The number of times to add the value.
1642 sample(Counter val, int number)
1644 Counter value = val * number;
1646 squares += value * value;
1651 * Return the number of entries in this stat, 1
1654 size_type size() const { return 1; }
1657 * Return true if no samples have been added.
1658 * @return True if no samples have been added.
1660 bool zero() const { return samples == Counter(); }
1663 prepare(Info *info, DistData &data)
1665 const Params *params = safe_cast<const Params *>(info->storageParams);
1667 assert(params->type == Deviation);
1668 data.type = params->type;
1670 data.squares = squares;
1671 data.samples = samples;
1675 * Reset stat value to default
1681 squares = Counter();
1682 samples = Counter();
1687 * Templatized storage for distribution that calculates per tick mean and
1693 struct Params : public DistParams
1695 Params() : DistParams(Deviation) {}
1699 /** Current total. */
1701 /** Current sum of squares. */
1706 * Create and initialize this storage.
1708 AvgSampleStor(Info *info)
1709 : sum(Counter()), squares(Counter())
1713 * Add a value to the distribution for the given number of times.
1714 * Update the running sum and sum of squares.
1715 * @param val The value to add.
1716 * @param number The number of times to add the value.
1719 sample(Counter val, int number)
1721 Counter value = val * number;
1723 squares += value * value;
1727 * Return the number of entries, in this case 1.
1730 size_type size() const { return 1; }
1733 * Return true if no samples have been added.
1734 * @return True if the sum is zero.
1736 bool zero() const { return sum == Counter(); }
1739 prepare(Info *info, DistData &data)
1741 const Params *params = safe_cast<const Params *>(info->storageParams);
1743 assert(params->type == Deviation);
1744 data.type = params->type;
1746 data.squares = squares;
1747 data.samples = curTick();
1751 * Reset stat value to default
1757 squares = Counter();
1762 * Implementation of a distribution stat. The type of distribution is
1763 * determined by the Storage template. @sa ScalarBase
1765 template <class Derived, class Stor>
1766 class DistBase : public DataWrap<Derived, DistInfoProxy>
1769 typedef DistInfoProxy<Derived> Info;
1770 typedef Stor Storage;
1771 typedef typename Stor::Params Params;
1774 /** The storage for this stat. */
1775 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1779 * Retrieve the storage.
1780 * @return The storage object for this stat.
1785 return reinterpret_cast<Storage *>(storage);
1789 * Retrieve a const pointer to the storage.
1790 * @return A const pointer to the storage object for this stat.
1795 return reinterpret_cast<const Storage *>(storage);
1801 new (storage) Storage(this->info());
1809 * Add a value to the distribtion n times. Calls sample on the storage
1811 * @param v The value to add.
1812 * @param n The number of times to add it, defaults to 1.
1814 template <typename U>
1815 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1818 * Return the number of entries in this stat.
1819 * @return The number of entries.
1821 size_type size() const { return data()->size(); }
1823 * Return true if no samples have been added.
1824 * @return True if there haven't been any samples.
1826 bool zero() const { return data()->zero(); }
1831 Info *info = this->info();
1832 data()->prepare(info, info->data);
1836 * Reset stat value to default
1841 data()->reset(this->info());
1845 template <class Stat>
1848 template <class Derived, class Stor>
1849 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1852 typedef VectorDistInfoProxy<Derived> Info;
1853 typedef Stor Storage;
1854 typedef typename Stor::Params Params;
1855 typedef DistProxy<Derived> Proxy;
1856 friend class DistProxy<Derived>;
1857 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1865 data(off_type index)
1867 return &storage[index];
1871 data(off_type index) const
1873 return &storage[index];
1879 assert(s > 0 && "size must be positive!");
1880 assert(!storage && "already initialized");
1883 char *ptr = new char[_size * sizeof(Storage)];
1884 storage = reinterpret_cast<Storage *>(ptr);
1886 Info *info = this->info();
1887 for (off_type i = 0; i < _size; ++i)
1888 new (&storage[i]) Storage(info);
1903 for (off_type i = 0; i < _size; ++i)
1904 data(i)->~Storage();
1905 delete [] reinterpret_cast<char *>(storage);
1908 Proxy operator[](off_type index)
1910 assert(index >= 0 && index < size());
1911 return Proxy(this->self(), index);
1923 for (off_type i = 0; i < size(); ++i)
1924 if (!data(i)->zero())
1932 Info *info = this->info();
1933 size_type size = this->size();
1934 info->data.resize(size);
1935 for (off_type i = 0; i < size; ++i)
1936 data(i)->prepare(info, info->data[i]);
1942 return storage != NULL;
1946 template <class Stat>
1954 typename Stat::Storage *data() { return stat.data(index); }
1955 const typename Stat::Storage *data() const { return stat.data(index); }
1958 DistProxy(Stat &s, off_type i)
1962 DistProxy(const DistProxy &sp)
1963 : stat(sp.stat), index(sp.index)
1967 operator=(const DistProxy &sp)
1975 template <typename U>
1977 sample(const U &v, int n = 1)
1979 data()->sample(v, n);
1991 return data()->zero();
1995 * Proxy has no state. Nothing to reset.
2000 //////////////////////////////////////////////////////////////////////
2004 //////////////////////////////////////////////////////////////////////
2007 * Base class for formula statistic node. These nodes are used to build a tree
2008 * that represents the formula.
2010 class Node : public RefCounted
2014 * Return the number of nodes in the subtree starting at this node.
2015 * @return the number of nodes in this subtree.
2017 virtual size_type size() const = 0;
2019 * Return the result vector of this subtree.
2020 * @return The result vector of this subtree.
2022 virtual const VResult &result() const = 0;
2024 * Return the total of the result vector.
2025 * @return The total of the result vector.
2027 virtual Result total() const = 0;
2032 virtual std::string str() const = 0;
2035 /** Reference counting pointer to a function Node. */
2036 typedef RefCountingPtr<Node> NodePtr;
2038 class ScalarStatNode : public Node
2041 const ScalarInfo *data;
2042 mutable VResult vresult;
2045 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2050 vresult[0] = data->result();
2054 Result total() const { return data->result(); };
2056 size_type size() const { return 1; }
2061 std::string str() const { return data->name; }
2064 template <class Stat>
2065 class ScalarProxyNode : public Node
2068 const ScalarProxy<Stat> proxy;
2069 mutable VResult vresult;
2072 ScalarProxyNode(const ScalarProxy<Stat> &p)
2073 : proxy(p), vresult(1)
2079 vresult[0] = proxy.result();
2086 return proxy.result();
2105 class VectorStatNode : public Node
2108 const VectorInfo *data;
2111 VectorStatNode(const VectorInfo *d) : data(d) { }
2112 const VResult &result() const { return data->result(); }
2113 Result total() const { return data->total(); };
2115 size_type size() const { return data->size(); }
2117 std::string str() const { return data->name; }
2121 class ConstNode : public Node
2127 ConstNode(T s) : vresult(1, (Result)s) {}
2128 const VResult &result() const { return vresult; }
2129 Result total() const { return vresult[0]; };
2130 size_type size() const { return 1; }
2131 std::string str() const { return to_string(vresult[0]); }
2135 class ConstVectorNode : public Node
2141 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2142 const VResult &result() const { return vresult; }
2147 size_type size = this->size();
2149 for (off_type i = 0; i < size; i++)
2154 size_type size() const { return vresult.size(); }
2158 size_type size = this->size();
2159 std::string tmp = "(";
2160 for (off_type i = 0; i < size; i++)
2161 tmp += csprintf("%s ",to_string(vresult[i]));
2171 struct OpString<std::plus<Result> >
2173 static std::string str() { return "+"; }
2177 struct OpString<std::minus<Result> >
2179 static std::string str() { return "-"; }
2183 struct OpString<std::multiplies<Result> >
2185 static std::string str() { return "*"; }
2189 struct OpString<std::divides<Result> >
2191 static std::string str() { return "/"; }
2195 struct OpString<std::modulus<Result> >
2197 static std::string str() { return "%"; }
2201 struct OpString<std::negate<Result> >
2203 static std::string str() { return "-"; }
2207 class UnaryNode : public Node
2211 mutable VResult vresult;
2214 UnaryNode(NodePtr &p) : l(p) {}
2219 const VResult &lvec = l->result();
2220 size_type size = lvec.size();
2224 vresult.resize(size);
2226 for (off_type i = 0; i < size; ++i)
2227 vresult[i] = op(lvec[i]);
2235 const VResult &vec = this->result();
2237 for (off_type i = 0; i < size(); i++)
2242 size_type size() const { return l->size(); }
2247 return OpString<Op>::str() + l->str();
2252 class BinaryNode : public Node
2257 mutable VResult vresult;
2260 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2266 const VResult &lvec = l->result();
2267 const VResult &rvec = r->result();
2269 assert(lvec.size() > 0 && rvec.size() > 0);
2271 if (lvec.size() == 1 && rvec.size() == 1) {
2273 vresult[0] = op(lvec[0], rvec[0]);
2274 } else if (lvec.size() == 1) {
2275 size_type size = rvec.size();
2276 vresult.resize(size);
2277 for (off_type i = 0; i < size; ++i)
2278 vresult[i] = op(lvec[0], rvec[i]);
2279 } else if (rvec.size() == 1) {
2280 size_type size = lvec.size();
2281 vresult.resize(size);
2282 for (off_type i = 0; i < size; ++i)
2283 vresult[i] = op(lvec[i], rvec[0]);
2284 } else if (rvec.size() == lvec.size()) {
2285 size_type size = rvec.size();
2286 vresult.resize(size);
2287 for (off_type i = 0; i < size; ++i)
2288 vresult[i] = op(lvec[i], rvec[i]);
2297 const VResult &vec = this->result();
2298 const VResult &lvec = l->result();
2299 const VResult &rvec = r->result();
2305 assert(lvec.size() > 0 && rvec.size() > 0);
2306 assert(lvec.size() == rvec.size() ||
2307 lvec.size() == 1 || rvec.size() == 1);
2309 /** If vectors are the same divide their sums (x0+x1)/(y0+y1) */
2310 if (lvec.size() == rvec.size() && lvec.size() > 1) {
2311 for (off_type i = 0; i < size(); ++i) {
2315 return op(lsum, rsum);
2318 /** Otherwise divide each item by the divisor */
2319 for (off_type i = 0; i < size(); ++i) {
2329 size_type ls = l->size();
2330 size_type rs = r->size();
2333 } else if (rs == 1) {
2336 assert(ls == rs && "Node vector sizes are not equal");
2344 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2349 class SumNode : public Node
2353 mutable VResult vresult;
2356 SumNode(NodePtr &p) : l(p), vresult(1) {}
2361 const VResult &lvec = l->result();
2362 size_type size = lvec.size();
2368 for (off_type i = 0; i < size; ++i)
2369 vresult[0] = op(vresult[0], lvec[i]);
2377 const VResult &lvec = l->result();
2378 size_type size = lvec.size();
2381 Result result = 0.0;
2384 for (off_type i = 0; i < size; ++i)
2385 result = op(result, lvec[i]);
2390 size_type size() const { return 1; }
2395 return csprintf("total(%s)", l->str());
2400 //////////////////////////////////////////////////////////////////////
2402 // Visible Statistics Types
2404 //////////////////////////////////////////////////////////////////////
2406 * @defgroup VisibleStats "Statistic Types"
2407 * These are the statistics that are used in the simulator.
2412 * This is a simple scalar statistic, like a counter.
2413 * @sa Stat, ScalarBase, StatStor
2415 class Scalar : public ScalarBase<Scalar, StatStor>
2418 using ScalarBase<Scalar, StatStor>::operator=;
2422 * A stat that calculates the per tick average of a value.
2423 * @sa Stat, ScalarBase, AvgStor
2425 class Average : public ScalarBase<Average, AvgStor>
2428 using ScalarBase<Average, AvgStor>::operator=;
2431 class Value : public ValueBase<Value>
2436 * A vector of scalar stats.
2437 * @sa Stat, VectorBase, StatStor
2439 class Vector : public VectorBase<Vector, StatStor>
2444 * A vector of Average stats.
2445 * @sa Stat, VectorBase, AvgStor
2447 class AverageVector : public VectorBase<AverageVector, AvgStor>
2452 * A 2-Dimensional vecto of scalar stats.
2453 * @sa Stat, Vector2dBase, StatStor
2455 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2460 * A simple distribution stat.
2461 * @sa Stat, DistBase, DistStor
2463 class Distribution : public DistBase<Distribution, DistStor>
2467 * Set the parameters of this distribution. @sa DistStor::Params
2468 * @param min The minimum value of the distribution.
2469 * @param max The maximum value of the distribution.
2470 * @param bkt The number of values in each bucket.
2471 * @return A reference to this distribution.
2474 init(Counter min, Counter max, Counter bkt)
2476 DistStor::Params *params = new DistStor::Params;
2479 params->bucket_size = bkt;
2480 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2481 this->setParams(params);
2483 return this->self();
2488 * A simple histogram stat.
2489 * @sa Stat, DistBase, HistStor
2491 class Histogram : public DistBase<Histogram, HistStor>
2495 * Set the parameters of this histogram. @sa HistStor::Params
2496 * @param size The number of buckets in the histogram
2497 * @return A reference to this histogram.
2500 init(size_type size)
2502 HistStor::Params *params = new HistStor::Params;
2503 params->buckets = size;
2504 this->setParams(params);
2506 return this->self();
2511 * Calculates the mean and variance of all the samples.
2512 * @sa DistBase, SampleStor
2514 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2518 * Construct and initialize this distribution.
2522 SampleStor::Params *params = new SampleStor::Params;
2524 this->setParams(params);
2529 * Calculates the per tick mean and variance of the samples.
2530 * @sa DistBase, AvgSampleStor
2532 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2536 * Construct and initialize this distribution.
2540 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2542 this->setParams(params);
2547 * A vector of distributions.
2548 * @sa VectorDistBase, DistStor
2550 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2554 * Initialize storage and parameters for this distribution.
2555 * @param size The size of the vector (the number of distributions).
2556 * @param min The minimum value of the distribution.
2557 * @param max The maximum value of the distribution.
2558 * @param bkt The number of values in each bucket.
2559 * @return A reference to this distribution.
2561 VectorDistribution &
2562 init(size_type size, Counter min, Counter max, Counter bkt)
2564 DistStor::Params *params = new DistStor::Params;
2567 params->bucket_size = bkt;
2568 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2569 this->setParams(params);
2571 return this->self();
2576 * This is a vector of StandardDeviation stats.
2577 * @sa VectorDistBase, SampleStor
2579 class VectorStandardDeviation
2580 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2584 * Initialize storage for this distribution.
2585 * @param size The size of the vector.
2586 * @return A reference to this distribution.
2588 VectorStandardDeviation &
2589 init(size_type size)
2591 SampleStor::Params *params = new SampleStor::Params;
2593 this->setParams(params);
2594 return this->self();
2599 * This is a vector of AverageDeviation stats.
2600 * @sa VectorDistBase, AvgSampleStor
2602 class VectorAverageDeviation
2603 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2607 * Initialize storage for this distribution.
2608 * @param size The size of the vector.
2609 * @return A reference to this distribution.
2611 VectorAverageDeviation &
2612 init(size_type size)
2614 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2616 this->setParams(params);
2617 return this->self();
2621 template <class Stat>
2622 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2625 mutable VResult vec;
2626 mutable VCounter cvec;
2629 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2631 size_type size() const { return this->s.size(); }
2636 this->s.result(vec);
2639 Result total() const { return this->s.total(); }
2640 VCounter &value() const { return cvec; }
2642 std::string str() const { return this->s.str(); }
2645 template <class Stat>
2646 class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2649 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2653 * Implementation of a sparse histogram stat. The storage class is
2654 * determined by the Storage template.
2656 template <class Derived, class Stor>
2657 class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2660 typedef SparseHistInfoProxy<Derived> Info;
2661 typedef Stor Storage;
2662 typedef typename Stor::Params Params;
2665 /** The storage for this stat. */
2666 char storage[sizeof(Storage)];
2670 * Retrieve the storage.
2671 * @return The storage object for this stat.
2676 return reinterpret_cast<Storage *>(storage);
2680 * Retrieve a const pointer to the storage.
2681 * @return A const pointer to the storage object for this stat.
2686 return reinterpret_cast<const Storage *>(storage);
2692 new (storage) Storage(this->info());
2697 SparseHistBase() { }
2700 * Add a value to the distribtion n times. Calls sample on the storage
2702 * @param v The value to add.
2703 * @param n The number of times to add it, defaults to 1.
2705 template <typename U>
2706 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2709 * Return the number of entries in this stat.
2710 * @return The number of entries.
2712 size_type size() const { return data()->size(); }
2714 * Return true if no samples have been added.
2715 * @return True if there haven't been any samples.
2717 bool zero() const { return data()->zero(); }
2722 Info *info = this->info();
2723 data()->prepare(info, info->data);
2727 * Reset stat value to default
2732 data()->reset(this->info());
2737 * Templatized storage and interface for a sparse histogram stat.
2739 class SparseHistStor
2742 /** The parameters for a sparse histogram stat. */
2743 struct Params : public DistParams
2745 Params() : DistParams(Hist) {}
2749 /** Counter for number of samples */
2751 /** Counter for each bucket. */
2755 SparseHistStor(Info *info)
2761 * Add a value to the distribution for the given number of times.
2762 * @param val The value to add.
2763 * @param number The number of times to add the value.
2766 sample(Counter val, int number)
2768 cmap[val] += number;
2773 * Return the number of buckets in this distribution.
2774 * @return the number of buckets.
2776 size_type size() const { return cmap.size(); }
2779 * Returns true if any calls to sample have been made.
2780 * @return True if any values have been sampled.
2785 return samples == Counter();
2789 prepare(Info *info, SparseHistData &data)
2791 MCounter::iterator it;
2793 for (it = cmap.begin(); it != cmap.end(); it++) {
2794 data.cmap[(*it).first] = (*it).second;
2797 data.samples = samples;
2801 * Reset stat value to default
2811 class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2815 * Set the parameters of this histogram. @sa HistStor::Params
2816 * @param size The number of buckets in the histogram
2817 * @return A reference to this histogram.
2820 init(size_type size)
2822 SparseHistStor::Params *params = new SparseHistStor::Params;
2823 this->setParams(params);
2825 return this->self();
2831 * A formula for statistics that is calculated when printed. A formula is
2832 * stored as a tree of Nodes that represent the equation to calculate.
2833 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2835 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2838 /** The root of the tree which represents the Formula */
2844 * Create and initialize thie formula, and register it with the database.
2849 * Create a formula with the given root node, register it with the
2851 * @param r The root of the expression tree.
2856 * Set an unitialized Formula to the given root.
2857 * @param r The root of the expression tree.
2858 * @return a reference to this formula.
2860 const Formula &operator=(Temp r);
2863 * Add the given tree to the existing one.
2864 * @param r The root of the expression tree.
2865 * @return a reference to this formula.
2867 const Formula &operator+=(Temp r);
2870 * Divide the existing tree by the given one.
2871 * @param r The root of the expression tree.
2872 * @return a reference to this formula.
2874 const Formula &operator/=(Temp r);
2877 * Return the result of the Fomula in a vector. If there were no Vector
2878 * components to the Formula, then the vector is size 1. If there were,
2879 * like x/y with x being a vector of size 3, then the result returned will
2880 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2881 * @return The result vector.
2883 void result(VResult &vec) const;
2886 * Return the total Formula result. If there is a Vector
2887 * component to this Formula, then this is the result of the
2888 * Formula if the formula is applied after summing all the
2889 * components of the Vector. For example, if Formula is x/y where
2890 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2891 * there is no Vector component, total() returns the same value as
2892 * the first entry in the VResult val() returns.
2893 * @return The total of the result vector.
2895 Result total() const;
2898 * Return the number of elements in the tree.
2900 size_type size() const;
2905 * Formulas don't need to be reset
2914 std::string str() const;
2917 class FormulaNode : public Node
2920 const Formula &formula;
2921 mutable VResult vec;
2924 FormulaNode(const Formula &f) : formula(f) {}
2926 size_type size() const { return formula.size(); }
2927 const VResult &result() const { formula.result(vec); return vec; }
2928 Result total() const { return formula.total(); }
2930 std::string str() const { return formula.str(); }
2934 * Helper class to construct formula node trees.
2940 * Pointer to a Node object.
2946 * Copy the given pointer to this class.
2947 * @param n A pointer to a Node object to copy.
2949 Temp(NodePtr n) : node(n) { }
2952 * Return the node pointer.
2953 * @return the node pointer.
2955 operator NodePtr&() { return node; }
2959 * Create a new ScalarStatNode.
2960 * @param s The ScalarStat to place in a node.
2962 Temp(const Scalar &s)
2963 : node(new ScalarStatNode(s.info()))
2967 * Create a new ScalarStatNode.
2968 * @param s The ScalarStat to place in a node.
2970 Temp(const Value &s)
2971 : node(new ScalarStatNode(s.info()))
2975 * Create a new ScalarStatNode.
2976 * @param s The ScalarStat to place in a node.
2978 Temp(const Average &s)
2979 : node(new ScalarStatNode(s.info()))
2983 * Create a new VectorStatNode.
2984 * @param s The VectorStat to place in a node.
2986 Temp(const Vector &s)
2987 : node(new VectorStatNode(s.info()))
2990 Temp(const AverageVector &s)
2991 : node(new VectorStatNode(s.info()))
2997 Temp(const Formula &f)
2998 : node(new FormulaNode(f))
3002 * Create a new ScalarProxyNode.
3003 * @param p The ScalarProxy to place in a node.
3005 template <class Stat>
3006 Temp(const ScalarProxy<Stat> &p)
3007 : node(new ScalarProxyNode<Stat>(p))
3011 * Create a ConstNode
3012 * @param value The value of the const node.
3014 Temp(signed char value)
3015 : node(new ConstNode<signed char>(value))
3019 * Create a ConstNode
3020 * @param value The value of the const node.
3022 Temp(unsigned char value)
3023 : node(new ConstNode<unsigned char>(value))
3027 * Create a ConstNode
3028 * @param value The value of the const node.
3030 Temp(signed short value)
3031 : node(new ConstNode<signed short>(value))
3035 * Create a ConstNode
3036 * @param value The value of the const node.
3038 Temp(unsigned short value)
3039 : node(new ConstNode<unsigned short>(value))
3043 * Create a ConstNode
3044 * @param value The value of the const node.
3046 Temp(signed int value)
3047 : node(new ConstNode<signed int>(value))
3051 * Create a ConstNode
3052 * @param value The value of the const node.
3054 Temp(unsigned int value)
3055 : node(new ConstNode<unsigned int>(value))
3059 * Create a ConstNode
3060 * @param value The value of the const node.
3062 Temp(signed long value)
3063 : node(new ConstNode<signed long>(value))
3067 * Create a ConstNode
3068 * @param value The value of the const node.
3070 Temp(unsigned long value)
3071 : node(new ConstNode<unsigned long>(value))
3075 * Create a ConstNode
3076 * @param value The value of the const node.
3078 Temp(signed long long value)
3079 : node(new ConstNode<signed long long>(value))
3083 * Create a ConstNode
3084 * @param value The value of the const node.
3086 Temp(unsigned long long value)
3087 : node(new ConstNode<unsigned long long>(value))
3091 * Create a ConstNode
3092 * @param value The value of the const node.
3095 : node(new ConstNode<float>(value))
3099 * Create a ConstNode
3100 * @param value The value of the const node.
3103 : node(new ConstNode<double>(value))
3113 operator+(Temp l, Temp r)
3115 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
3119 operator-(Temp l, Temp r)
3121 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
3125 operator*(Temp l, Temp r)
3127 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
3131 operator/(Temp l, Temp r)
3133 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
3139 return NodePtr(new UnaryNode<std::negate<Result> >(l));
3142 template <typename T>
3146 return NodePtr(new ConstNode<T>(val));
3149 template <typename T>
3151 constantVector(T val)
3153 return NodePtr(new ConstVectorNode<T>(val));
3159 return NodePtr(new SumNode<std::plus<Result> >(val));
3162 /** Dump all statistics data to the registered outputs */
3169 * Register a callback that should be called whenever statistics are
3172 void registerResetCallback(Callback *cb);
3175 * Register a callback that should be called whenever statistics are
3176 * about to be dumped
3178 void registerDumpCallback(Callback *cb);
3180 std::list<Info *> &statsList();
3182 typedef std::map<const void *, Info *> MapType;
3183 MapType &statsMap();
3185 typedef std::map<std::string, Info *> NameMapType;
3186 NameMapType &nameMap();
3188 bool validateStatName(const std::string &name);
3190 } // namespace Stats
3192 void debugDumpStats();
3194 #endif // __BASE_STATISTICS_HH__