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;
334 // The following functions are specific to vectors. If you use them
335 // in a non vector context, you will get a nice compiler error!
338 * Set the subfield name for the given index, and marks this stat to print
339 * at the end of simulation.
340 * @param index The subfield index.
341 * @param name The new name of the subfield.
342 * @return A reference to this stat.
345 subname(off_type index, const std::string &name)
347 Derived &self = this->self();
348 Info *info = self.info();
350 std::vector<std::string> &subn = info->subnames;
351 if (subn.size() <= index)
352 subn.resize(index + 1);
357 // The following functions are specific to 2d vectors. If you use
358 // them in a non vector context, you will get a nice compiler
359 // error because info doesn't have the right variables.
362 * Set the subfield description for the given index and marks this stat to
363 * print at the end of simulation.
364 * @param index The subfield index.
365 * @param desc The new description of the subfield
366 * @return A reference to this stat.
369 subdesc(off_type index, const std::string &desc)
371 Info *info = this->info();
373 std::vector<std::string> &subd = info->subdescs;
374 if (subd.size() <= index)
375 subd.resize(index + 1);
384 Derived &self = this->self();
385 Info *info = this->info();
387 size_t size = self.size();
388 for (off_type i = 0; i < size; ++i)
389 self.data(i)->prepare(info);
395 Derived &self = this->self();
396 Info *info = this->info();
398 size_t size = self.size();
399 for (off_type i = 0; i < size; ++i)
400 self.data(i)->reset(info);
404 template <class Derived, template <class> class InfoProxyType>
405 class DataWrapVec2d : public DataWrapVec<Derived, InfoProxyType>
408 typedef InfoProxyType<Derived> Info;
411 * @warning This makes the assumption that if you're gonna subnames a 2d
412 * vector, you're subnaming across all y
415 ysubnames(const char **names)
417 Derived &self = this->self();
418 Info *info = this->info();
420 info->y_subnames.resize(self.y);
421 for (off_type i = 0; i < self.y; ++i)
422 info->y_subnames[i] = names[i];
427 ysubname(off_type index, const std::string &subname)
429 Derived &self = this->self();
430 Info *info = this->info();
432 assert(index < self.y);
433 info->y_subnames.resize(self.y);
434 info->y_subnames[index] = subname.c_str();
439 ysubname(off_type i) const
441 return this->info()->y_subnames[i];
446 //////////////////////////////////////////////////////////////////////
450 //////////////////////////////////////////////////////////////////////
453 * Templatized storage and interface for a simple scalar stat.
458 /** The statistic value. */
462 struct Params : public StorageParams {};
466 * Builds this storage element and calls the base constructor of the
474 * The the stat to the given value.
475 * @param val The new value.
477 void set(Counter val) { data = val; }
479 * Increment the stat by the given value.
480 * @param val The new value.
482 void inc(Counter val) { data += val; }
484 * Decrement the stat by the given value.
485 * @param val The new value.
487 void dec(Counter val) { data -= val; }
489 * Return the value of this stat as its base type.
490 * @return The value of this stat.
492 Counter value() const { return data; }
494 * Return the value of this stat as a result type.
495 * @return The value of this stat.
497 Result result() const { return (Result)data; }
499 * Prepare stat data for dumping or serialization
501 void prepare(Info *info) { }
503 * Reset stat value to default
505 void reset(Info *info) { data = Counter(); }
508 * @return true if zero value
510 bool zero() const { return data == Counter(); }
514 * Templatized storage and interface to a per-tick average stat. This keeps
515 * a current count and updates a total (count * ticks) when this count
516 * changes. This allows the quick calculation of a per tick count of the item
517 * being watched. This is good for keeping track of residencies in structures
518 * among other things.
523 /** The current count. */
525 /** The tick of the last reset */
527 /** The total count for all tick. */
528 mutable Result total;
529 /** The tick that current last changed. */
533 struct Params : public StorageParams {};
537 * Build and initializes this stat storage.
540 : current(0), lastReset(0), total(0), last(0)
544 * Set the current count to the one provided, update the total and last
546 * @param val The new count.
551 total += current * (curTick() - last);
557 * Increment the current count by the provided value, calls set.
558 * @param val The amount to increment.
560 void inc(Counter val) { set(current + val); }
563 * Deccrement the current count by the provided value, calls set.
564 * @param val The amount to decrement.
566 void dec(Counter val) { set(current - val); }
569 * Return the current count.
570 * @return The current count.
572 Counter value() const { return current; }
575 * Return the current average.
576 * @return The current average.
581 assert(last == curTick());
582 return (Result)(total + current) / (Result)(curTick() - lastReset + 1);
586 * @return true if zero value
588 bool zero() const { return total == 0.0; }
591 * Prepare stat data for dumping or serialization
596 total += current * (curTick() - last);
601 * Reset stat value to default
608 lastReset = curTick();
614 * Implementation of a scalar stat. The type of stat is determined by the
617 template <class Derived, class Stor>
618 class ScalarBase : public DataWrap<Derived, ScalarInfoProxy>
621 typedef Stor Storage;
622 typedef typename Stor::Params Params;
625 /** The storage of this stat. */
626 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
630 * Retrieve the storage.
631 * @param index The vector index to access.
632 * @return The storage object at the given index.
637 return reinterpret_cast<Storage *>(storage);
641 * Retrieve a const pointer to the storage.
642 * for the given index.
643 * @param index The vector index to access.
644 * @return A const pointer to the storage object at the given index.
649 return reinterpret_cast<const Storage *>(storage);
655 new (storage) Storage(this->info());
661 * Return the current value of this stat as its base type.
662 * @return The current value.
664 Counter value() const { return data()->value(); }
673 // Common operators for stats
675 * Increment the stat by 1. This calls the associated storage object inc
678 void operator++() { data()->inc(1); }
680 * Decrement the stat by 1. This calls the associated storage object dec
683 void operator--() { data()->dec(1); }
685 /** Increment the stat by 1. */
686 void operator++(int) { ++*this; }
687 /** Decrement the stat by 1. */
688 void operator--(int) { --*this; }
691 * Set the data value to the given value. This calls the associated storage
692 * object set function.
693 * @param v The new value.
695 template <typename U>
696 void operator=(const U &v) { data()->set(v); }
699 * Increment the stat by the given value. This calls the associated
700 * storage object inc function.
701 * @param v The value to add.
703 template <typename U>
704 void operator+=(const U &v) { data()->inc(v); }
707 * Decrement the stat by the given value. This calls the associated
708 * storage object dec function.
709 * @param v The value to substract.
711 template <typename U>
712 void operator-=(const U &v) { data()->dec(v); }
715 * Return the number of elements, always 1 for a scalar.
718 size_type size() const { return 1; }
720 Counter value() { return data()->value(); }
722 Result result() { return data()->result(); }
724 Result total() { return result(); }
726 bool zero() { return result() == 0.0; }
728 void reset() { data()->reset(this->info()); }
729 void prepare() { data()->prepare(this->info()); }
732 class ProxyInfo : public ScalarInfo
735 std::string str() const { return to_string(value()); }
736 size_type size() const { return 1; }
737 bool check() const { return true; }
740 bool zero() const { return value() == 0; }
742 void visit(Output &visitor) { visitor.visit(*this); }
746 class ValueProxy : public ProxyInfo
752 ValueProxy(T &val) : scalar(&val) {}
753 Counter value() const { return *scalar; }
754 Result result() const { return *scalar; }
755 Result total() const { return *scalar; }
759 class FunctorProxy : public ProxyInfo
765 FunctorProxy(T &func) : functor(&func) {}
766 Counter value() const { return (*functor)(); }
767 Result result() const { return (*functor)(); }
768 Result total() const { return (*functor)(); }
771 template <class Derived>
772 class ValueBase : public DataWrap<Derived, ScalarInfoProxy>
778 ValueBase() : proxy(NULL) { }
779 ~ValueBase() { if (proxy) delete proxy; }
785 proxy = new ValueProxy<T>(value);
794 proxy = new FunctorProxy<T>(func);
799 Counter value() { return proxy->value(); }
800 Result result() const { return proxy->result(); }
801 Result total() const { return proxy->total(); };
802 size_type size() const { return proxy->size(); }
804 std::string str() const { return proxy->str(); }
805 bool zero() const { return proxy->zero(); }
806 bool check() const { return proxy != NULL; }
811 //////////////////////////////////////////////////////////////////////
815 //////////////////////////////////////////////////////////////////////
818 * A proxy class to access the stat at a given index in a VectorBase stat.
819 * Behaves like a ScalarBase.
821 template <class Stat>
825 /** Pointer to the parent Vector. */
828 /** The index to access in the parent VectorBase. */
833 * Return the current value of this stat as its base type.
834 * @return The current value.
836 Counter value() const { return stat.data(index)->value(); }
839 * Return the current value of this statas a result type.
840 * @return The current value.
842 Result result() const { return stat.data(index)->result(); }
846 * Create and initialize this proxy, do not register it with the database.
847 * @param i The index to access.
849 ScalarProxy(Stat &s, off_type i)
855 * Create a copy of the provided ScalarProxy.
856 * @param sp The proxy to copy.
858 ScalarProxy(const ScalarProxy &sp)
859 : stat(sp.stat), index(sp.index)
863 * Set this proxy equal to the provided one.
864 * @param sp The proxy to copy.
865 * @return A reference to this proxy.
868 operator=(const ScalarProxy &sp)
876 // Common operators for stats
878 * Increment the stat by 1. This calls the associated storage object inc
881 void operator++() { stat.data(index)->inc(1); }
883 * Decrement the stat by 1. This calls the associated storage object dec
886 void operator--() { stat.data(index)->dec(1); }
888 /** Increment the stat by 1. */
889 void operator++(int) { ++*this; }
890 /** Decrement the stat by 1. */
891 void operator--(int) { --*this; }
894 * Set the data value to the given value. This calls the associated storage
895 * object set function.
896 * @param v The new value.
898 template <typename U>
900 operator=(const U &v)
902 stat.data(index)->set(v);
906 * Increment the stat by the given value. This calls the associated
907 * storage object inc function.
908 * @param v The value to add.
910 template <typename U>
912 operator+=(const U &v)
914 stat.data(index)->inc(v);
918 * Decrement the stat by the given value. This calls the associated
919 * storage object dec function.
920 * @param v The value to substract.
922 template <typename U>
924 operator-=(const U &v)
926 stat.data(index)->dec(v);
930 * Return the number of elements, always 1 for a scalar.
933 size_type size() const { return 1; }
939 return csprintf("%s[%d]", stat.info()->name, index);
944 * Implementation of a vector of stats. The type of stat is determined by the
945 * Storage class. @sa ScalarBase
947 template <class Derived, class Stor>
948 class VectorBase : public DataWrapVec<Derived, VectorInfoProxy>
951 typedef Stor Storage;
952 typedef typename Stor::Params Params;
955 typedef ScalarProxy<Derived> Proxy;
956 friend class ScalarProxy<Derived>;
957 friend class DataWrapVec<Derived, VectorInfoProxy>;
960 /** The storage of this stat. */
966 * Retrieve the storage.
967 * @param index The vector index to access.
968 * @return The storage object at the given index.
970 Storage *data(off_type index) { return &storage[index]; }
973 * Retrieve a const pointer to the storage.
974 * @param index The vector index to access.
975 * @return A const pointer to the storage object at the given index.
977 const Storage *data(off_type index) const { return &storage[index]; }
982 assert(s > 0 && "size must be positive!");
983 assert(!storage && "already initialized");
986 char *ptr = new char[_size * sizeof(Storage)];
987 storage = reinterpret_cast<Storage *>(ptr);
989 for (off_type i = 0; i < _size; ++i)
990 new (&storage[i]) Storage(this->info());
997 value(VCounter &vec) const
1000 for (off_type i = 0; i < size(); ++i)
1001 vec[i] = data(i)->value();
1005 * Copy the values to a local vector and return a reference to it.
1006 * @return A reference to a vector of the stat values.
1009 result(VResult &vec) const
1012 for (off_type i = 0; i < size(); ++i)
1013 vec[i] = data(i)->result();
1017 * Return a total of all entries in this vector.
1018 * @return The total of all vector entries.
1024 for (off_type i = 0; i < size(); ++i)
1025 total += data(i)->result();
1030 * @return the number of elements in this vector.
1032 size_type size() const { return _size; }
1037 for (off_type i = 0; i < size(); ++i)
1038 if (data(i)->zero())
1046 return storage != NULL;
1059 for (off_type i = 0; i < _size; ++i)
1060 data(i)->~Storage();
1061 delete [] reinterpret_cast<char *>(storage);
1065 * Set this vector to have the given size.
1066 * @param size The new size.
1067 * @return A reference to this stat.
1070 init(size_type size)
1072 Derived &self = this->self();
1078 * Return a reference (ScalarProxy) to the stat at the given index.
1079 * @param index The vector index to access.
1080 * @return A reference of the stat.
1083 operator[](off_type index)
1085 assert (index >= 0 && index < size());
1086 return Proxy(this->self(), index);
1090 template <class Stat>
1099 mutable VResult vec;
1101 typename Stat::Storage *
1102 data(off_type index)
1104 assert(index < len);
1105 return stat.data(offset + index);
1108 const typename Stat::Storage *
1109 data(off_type index) const
1111 assert(index < len);
1112 return stat.data(offset + index);
1121 for (off_type i = 0; i < size(); ++i)
1122 vec[i] = data(i)->result();
1131 for (off_type i = 0; i < size(); ++i)
1132 total += data(i)->result();
1137 VectorProxy(Stat &s, off_type o, size_type l)
1138 : stat(s), offset(o), len(l)
1142 VectorProxy(const VectorProxy &sp)
1143 : stat(sp.stat), offset(sp.offset), len(sp.len)
1148 operator=(const VectorProxy &sp)
1157 operator[](off_type index)
1159 assert (index >= 0 && index < size());
1160 return ScalarProxy<Stat>(stat, offset + index);
1163 size_type size() const { return len; }
1166 template <class Derived, class Stor>
1167 class Vector2dBase : public DataWrapVec2d<Derived, Vector2dInfoProxy>
1170 typedef Vector2dInfoProxy<Derived> Info;
1171 typedef Stor Storage;
1172 typedef typename Stor::Params Params;
1173 typedef VectorProxy<Derived> Proxy;
1174 friend class ScalarProxy<Derived>;
1175 friend class VectorProxy<Derived>;
1176 friend class DataWrapVec<Derived, Vector2dInfoProxy>;
1177 friend class DataWrapVec2d<Derived, Vector2dInfoProxy>;
1186 Storage *data(off_type index) { return &storage[index]; }
1187 const Storage *data(off_type index) const { return &storage[index]; }
1199 for (off_type i = 0; i < _size; ++i)
1200 data(i)->~Storage();
1201 delete [] reinterpret_cast<char *>(storage);
1205 init(size_type _x, size_type _y)
1207 assert(_x > 0 && _y > 0 && "sizes must be positive!");
1208 assert(!storage && "already initialized");
1210 Derived &self = this->self();
1211 Info *info = this->info();
1219 char *ptr = new char[_size * sizeof(Storage)];
1220 storage = reinterpret_cast<Storage *>(ptr);
1222 for (off_type i = 0; i < _size; ++i)
1223 new (&storage[i]) Storage(info);
1231 operator[](off_type index)
1233 off_type offset = index * y;
1234 assert (index >= 0 && offset + index < size());
1235 return Proxy(this->self(), offset, y);
1248 return data(0)->zero();
1250 for (off_type i = 0; i < size(); ++i)
1251 if (!data(i)->zero())
1260 Info *info = this->info();
1261 size_type size = this->size();
1263 for (off_type i = 0; i < size; ++i)
1264 data(i)->prepare(info);
1266 info->cvec.resize(size);
1267 for (off_type i = 0; i < size; ++i)
1268 info->cvec[i] = data(i)->value();
1272 * Reset stat value to default
1277 Info *info = this->info();
1278 size_type size = this->size();
1279 for (off_type i = 0; i < size; ++i)
1280 data(i)->reset(info);
1286 return storage != NULL;
1290 //////////////////////////////////////////////////////////////////////
1292 // Non formula statistics
1294 //////////////////////////////////////////////////////////////////////
1295 /** The parameters for a distribution stat. */
1296 struct DistParams : public StorageParams
1298 const DistType type;
1299 DistParams(DistType t) : type(t) {}
1303 * Templatized storage and interface for a distrbution stat.
1308 /** The parameters for a distribution stat. */
1309 struct Params : public DistParams
1311 /** The minimum value to track. */
1313 /** The maximum value to track. */
1315 /** The number of entries in each bucket. */
1316 Counter bucket_size;
1317 /** The number of buckets. Equal to (max-min)/bucket_size. */
1320 Params() : DistParams(Dist) {}
1324 /** The minimum value to track. */
1326 /** The maximum value to track. */
1328 /** The number of entries in each bucket. */
1329 Counter bucket_size;
1330 /** The number of buckets. Equal to (max-min)/bucket_size. */
1333 /** The smallest value sampled. */
1335 /** The largest value sampled. */
1337 /** The number of values sampled less than min. */
1339 /** The number of values sampled more than max. */
1341 /** The current sum. */
1343 /** The sum of squares. */
1345 /** The number of samples. */
1347 /** Counter for each bucket. */
1351 DistStor(Info *info)
1352 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1358 * Add a value to the distribution for the given number of times.
1359 * @param val The value to add.
1360 * @param number The number of times to add the value.
1363 sample(Counter val, int number)
1365 if (val < min_track)
1366 underflow += number;
1367 else if (val > max_track)
1371 (size_type)std::floor((val - min_track) / bucket_size);
1372 assert(index < size());
1373 cvec[index] += number;
1382 sum += val * number;
1383 squares += val * val * number;
1388 * Return the number of buckets in this distribution.
1389 * @return the number of buckets.
1391 size_type size() const { return cvec.size(); }
1394 * Returns true if any calls to sample have been made.
1395 * @return True if any values have been sampled.
1400 return samples == Counter();
1404 prepare(Info *info, DistData &data)
1406 const Params *params = safe_cast<const Params *>(info->storageParams);
1408 assert(params->type == Dist);
1409 data.type = params->type;
1410 data.min = params->min;
1411 data.max = params->max;
1412 data.bucket_size = params->bucket_size;
1414 data.min_val = (min_val == CounterLimits::max()) ? 0 : min_val;
1415 data.max_val = (max_val == CounterLimits::min()) ? 0 : max_val;
1416 data.underflow = underflow;
1417 data.overflow = overflow;
1419 size_type buckets = params->buckets;
1420 data.cvec.resize(buckets);
1421 for (off_type i = 0; i < buckets; ++i)
1422 data.cvec[i] = cvec[i];
1425 data.squares = squares;
1426 data.samples = samples;
1430 * Reset stat value to default
1435 const Params *params = safe_cast<const Params *>(info->storageParams);
1436 min_track = params->min;
1437 max_track = params->max;
1438 bucket_size = params->bucket_size;
1440 min_val = CounterLimits::max();
1441 max_val = CounterLimits::min();
1442 underflow = Counter();
1443 overflow = Counter();
1445 size_type size = cvec.size();
1446 for (off_type i = 0; i < size; ++i)
1447 cvec[i] = Counter();
1450 squares = Counter();
1451 samples = Counter();
1456 * Templatized storage and interface for a histogram stat.
1461 /** The parameters for a distribution stat. */
1462 struct Params : public DistParams
1464 /** The number of buckets.. */
1467 Params() : DistParams(Hist) {}
1471 /** The minimum value to track. */
1473 /** The maximum value to track. */
1475 /** The number of entries in each bucket. */
1476 Counter bucket_size;
1478 /** The current sum. */
1480 /** The sum of squares. */
1482 /** The number of samples. */
1484 /** Counter for each bucket. */
1488 HistStor(Info *info)
1489 : cvec(safe_cast<const Params *>(info->storageParams)->buckets)
1496 void grow_convert();
1499 * Add a value to the distribution for the given number of times.
1500 * @param val The value to add.
1501 * @param number The number of times to add the value.
1504 sample(Counter val, int number)
1506 assert(min_bucket < max_bucket);
1507 if (val < min_bucket) {
1508 if (min_bucket == 0)
1511 while (val < min_bucket)
1513 } else if (val >= max_bucket + bucket_size) {
1514 if (min_bucket == 0) {
1515 while (val >= max_bucket + bucket_size)
1518 while (val >= max_bucket + bucket_size)
1524 (int64_t)std::floor((val - min_bucket) / bucket_size);
1526 assert(index >= 0 && index < size());
1527 cvec[index] += number;
1529 sum += val * number;
1530 squares += val * val * number;
1535 * Return the number of buckets in this distribution.
1536 * @return the number of buckets.
1538 size_type size() const { return cvec.size(); }
1541 * Returns true if any calls to sample have been made.
1542 * @return True if any values have been sampled.
1547 return samples == Counter();
1551 prepare(Info *info, DistData &data)
1553 const Params *params = safe_cast<const Params *>(info->storageParams);
1555 assert(params->type == Hist);
1556 data.type = params->type;
1557 data.min = min_bucket;
1558 data.max = max_bucket + bucket_size - 1;
1559 data.bucket_size = bucket_size;
1561 data.min_val = min_bucket;
1562 data.max_val = max_bucket;
1564 int buckets = params->buckets;
1565 data.cvec.resize(buckets);
1566 for (off_type i = 0; i < buckets; ++i)
1567 data.cvec[i] = cvec[i];
1570 data.squares = squares;
1571 data.samples = samples;
1575 * Reset stat value to default
1580 const Params *params = safe_cast<const Params *>(info->storageParams);
1582 max_bucket = params->buckets - 1;
1585 size_type size = cvec.size();
1586 for (off_type i = 0; i < size; ++i)
1587 cvec[i] = Counter();
1590 squares = Counter();
1591 samples = Counter();
1596 * Templatized storage and interface for a distribution that calculates mean
1602 struct Params : public DistParams
1604 Params() : DistParams(Deviation) {}
1608 /** The current sum. */
1610 /** The sum of squares. */
1612 /** The number of samples. */
1617 * Create and initialize this storage.
1619 SampleStor(Info *info)
1620 : sum(Counter()), squares(Counter()), samples(Counter())
1624 * Add a value the given number of times to this running average.
1625 * Update the running sum and sum of squares, increment the number of
1626 * values seen by the given number.
1627 * @param val The value to add.
1628 * @param number The number of times to add the value.
1631 sample(Counter val, int number)
1633 Counter value = val * number;
1635 squares += value * value;
1640 * Return the number of entries in this stat, 1
1643 size_type size() const { return 1; }
1646 * Return true if no samples have been added.
1647 * @return True if no samples have been added.
1649 bool zero() const { return samples == Counter(); }
1652 prepare(Info *info, DistData &data)
1654 const Params *params = safe_cast<const Params *>(info->storageParams);
1656 assert(params->type == Deviation);
1657 data.type = params->type;
1659 data.squares = squares;
1660 data.samples = samples;
1664 * Reset stat value to default
1670 squares = Counter();
1671 samples = Counter();
1676 * Templatized storage for distribution that calculates per tick mean and
1682 struct Params : public DistParams
1684 Params() : DistParams(Deviation) {}
1688 /** Current total. */
1690 /** Current sum of squares. */
1695 * Create and initialize this storage.
1697 AvgSampleStor(Info *info)
1698 : sum(Counter()), squares(Counter())
1702 * Add a value to the distribution for the given number of times.
1703 * Update the running sum and sum of squares.
1704 * @param val The value to add.
1705 * @param number The number of times to add the value.
1708 sample(Counter val, int number)
1710 Counter value = val * number;
1712 squares += value * value;
1716 * Return the number of entries, in this case 1.
1719 size_type size() const { return 1; }
1722 * Return true if no samples have been added.
1723 * @return True if the sum is zero.
1725 bool zero() const { return sum == Counter(); }
1728 prepare(Info *info, DistData &data)
1730 const Params *params = safe_cast<const Params *>(info->storageParams);
1732 assert(params->type == Deviation);
1733 data.type = params->type;
1735 data.squares = squares;
1736 data.samples = curTick();
1740 * Reset stat value to default
1746 squares = Counter();
1751 * Implementation of a distribution stat. The type of distribution is
1752 * determined by the Storage template. @sa ScalarBase
1754 template <class Derived, class Stor>
1755 class DistBase : public DataWrap<Derived, DistInfoProxy>
1758 typedef DistInfoProxy<Derived> Info;
1759 typedef Stor Storage;
1760 typedef typename Stor::Params Params;
1763 /** The storage for this stat. */
1764 char storage[sizeof(Storage)] __attribute__ ((aligned (8)));
1768 * Retrieve the storage.
1769 * @return The storage object for this stat.
1774 return reinterpret_cast<Storage *>(storage);
1778 * Retrieve a const pointer to the storage.
1779 * @return A const pointer to the storage object for this stat.
1784 return reinterpret_cast<const Storage *>(storage);
1790 new (storage) Storage(this->info());
1798 * Add a value to the distribtion n times. Calls sample on the storage
1800 * @param v The value to add.
1801 * @param n The number of times to add it, defaults to 1.
1803 template <typename U>
1804 void sample(const U &v, int n = 1) { data()->sample(v, n); }
1807 * Return the number of entries in this stat.
1808 * @return The number of entries.
1810 size_type size() const { return data()->size(); }
1812 * Return true if no samples have been added.
1813 * @return True if there haven't been any samples.
1815 bool zero() const { return data()->zero(); }
1820 Info *info = this->info();
1821 data()->prepare(info, info->data);
1825 * Reset stat value to default
1830 data()->reset(this->info());
1834 template <class Stat>
1837 template <class Derived, class Stor>
1838 class VectorDistBase : public DataWrapVec<Derived, VectorDistInfoProxy>
1841 typedef VectorDistInfoProxy<Derived> Info;
1842 typedef Stor Storage;
1843 typedef typename Stor::Params Params;
1844 typedef DistProxy<Derived> Proxy;
1845 friend class DistProxy<Derived>;
1846 friend class DataWrapVec<Derived, VectorDistInfoProxy>;
1854 data(off_type index)
1856 return &storage[index];
1860 data(off_type index) const
1862 return &storage[index];
1868 assert(s > 0 && "size must be positive!");
1869 assert(!storage && "already initialized");
1872 char *ptr = new char[_size * sizeof(Storage)];
1873 storage = reinterpret_cast<Storage *>(ptr);
1875 Info *info = this->info();
1876 for (off_type i = 0; i < _size; ++i)
1877 new (&storage[i]) Storage(info);
1892 for (off_type i = 0; i < _size; ++i)
1893 data(i)->~Storage();
1894 delete [] reinterpret_cast<char *>(storage);
1897 Proxy operator[](off_type index)
1899 assert(index >= 0 && index < size());
1900 return Proxy(this->self(), index);
1912 for (off_type i = 0; i < size(); ++i)
1913 if (!data(i)->zero())
1921 Info *info = this->info();
1922 size_type size = this->size();
1923 info->data.resize(size);
1924 for (off_type i = 0; i < size; ++i)
1925 data(i)->prepare(info, info->data[i]);
1931 return storage != NULL;
1935 template <class Stat>
1943 typename Stat::Storage *data() { return stat.data(index); }
1944 const typename Stat::Storage *data() const { return stat.data(index); }
1947 DistProxy(Stat &s, off_type i)
1951 DistProxy(const DistProxy &sp)
1952 : stat(sp.stat), index(sp.index)
1956 operator=(const DistProxy &sp)
1964 template <typename U>
1966 sample(const U &v, int n = 1)
1968 data()->sample(v, n);
1980 return data()->zero();
1984 * Proxy has no state. Nothing to reset.
1989 //////////////////////////////////////////////////////////////////////
1993 //////////////////////////////////////////////////////////////////////
1996 * Base class for formula statistic node. These nodes are used to build a tree
1997 * that represents the formula.
1999 class Node : public RefCounted
2003 * Return the number of nodes in the subtree starting at this node.
2004 * @return the number of nodes in this subtree.
2006 virtual size_type size() const = 0;
2008 * Return the result vector of this subtree.
2009 * @return The result vector of this subtree.
2011 virtual const VResult &result() const = 0;
2013 * Return the total of the result vector.
2014 * @return The total of the result vector.
2016 virtual Result total() const = 0;
2021 virtual std::string str() const = 0;
2024 /** Reference counting pointer to a function Node. */
2025 typedef RefCountingPtr<Node> NodePtr;
2027 class ScalarStatNode : public Node
2030 const ScalarInfo *data;
2031 mutable VResult vresult;
2034 ScalarStatNode(const ScalarInfo *d) : data(d), vresult(1) {}
2039 vresult[0] = data->result();
2043 Result total() const { return data->result(); };
2045 size_type size() const { return 1; }
2050 std::string str() const { return data->name; }
2053 template <class Stat>
2054 class ScalarProxyNode : public Node
2057 const ScalarProxy<Stat> proxy;
2058 mutable VResult vresult;
2061 ScalarProxyNode(const ScalarProxy<Stat> &p)
2062 : proxy(p), vresult(1)
2068 vresult[0] = proxy.result();
2075 return proxy.result();
2094 class VectorStatNode : public Node
2097 const VectorInfo *data;
2100 VectorStatNode(const VectorInfo *d) : data(d) { }
2101 const VResult &result() const { return data->result(); }
2102 Result total() const { return data->total(); };
2104 size_type size() const { return data->size(); }
2106 std::string str() const { return data->name; }
2110 class ConstNode : public Node
2116 ConstNode(T s) : vresult(1, (Result)s) {}
2117 const VResult &result() const { return vresult; }
2118 Result total() const { return vresult[0]; };
2119 size_type size() const { return 1; }
2120 std::string str() const { return to_string(vresult[0]); }
2124 class ConstVectorNode : public Node
2130 ConstVectorNode(const T &s) : vresult(s.begin(), s.end()) {}
2131 const VResult &result() const { return vresult; }
2136 size_type size = this->size();
2138 for (off_type i = 0; i < size; i++)
2143 size_type size() const { return vresult.size(); }
2147 size_type size = this->size();
2148 std::string tmp = "(";
2149 for (off_type i = 0; i < size; i++)
2150 tmp += csprintf("%s ",to_string(vresult[i]));
2160 struct OpString<std::plus<Result> >
2162 static std::string str() { return "+"; }
2166 struct OpString<std::minus<Result> >
2168 static std::string str() { return "-"; }
2172 struct OpString<std::multiplies<Result> >
2174 static std::string str() { return "*"; }
2178 struct OpString<std::divides<Result> >
2180 static std::string str() { return "/"; }
2184 struct OpString<std::modulus<Result> >
2186 static std::string str() { return "%"; }
2190 struct OpString<std::negate<Result> >
2192 static std::string str() { return "-"; }
2196 class UnaryNode : public Node
2200 mutable VResult vresult;
2203 UnaryNode(NodePtr &p) : l(p) {}
2208 const VResult &lvec = l->result();
2209 size_type size = lvec.size();
2213 vresult.resize(size);
2215 for (off_type i = 0; i < size; ++i)
2216 vresult[i] = op(lvec[i]);
2224 const VResult &vec = this->result();
2226 for (off_type i = 0; i < size(); i++)
2231 size_type size() const { return l->size(); }
2236 return OpString<Op>::str() + l->str();
2241 class BinaryNode : public Node
2246 mutable VResult vresult;
2249 BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {}
2255 const VResult &lvec = l->result();
2256 const VResult &rvec = r->result();
2258 assert(lvec.size() > 0 && rvec.size() > 0);
2260 if (lvec.size() == 1 && rvec.size() == 1) {
2262 vresult[0] = op(lvec[0], rvec[0]);
2263 } else if (lvec.size() == 1) {
2264 size_type size = rvec.size();
2265 vresult.resize(size);
2266 for (off_type i = 0; i < size; ++i)
2267 vresult[i] = op(lvec[0], rvec[i]);
2268 } else if (rvec.size() == 1) {
2269 size_type size = lvec.size();
2270 vresult.resize(size);
2271 for (off_type i = 0; i < size; ++i)
2272 vresult[i] = op(lvec[i], rvec[0]);
2273 } else if (rvec.size() == lvec.size()) {
2274 size_type size = rvec.size();
2275 vresult.resize(size);
2276 for (off_type i = 0; i < size; ++i)
2277 vresult[i] = op(lvec[i], rvec[i]);
2286 const VResult &vec = this->result();
2288 for (off_type i = 0; i < size(); i++)
2296 size_type ls = l->size();
2297 size_type rs = r->size();
2300 } else if (rs == 1) {
2303 assert(ls == rs && "Node vector sizes are not equal");
2311 return csprintf("(%s %s %s)", l->str(), OpString<Op>::str(), r->str());
2316 class SumNode : public Node
2320 mutable VResult vresult;
2323 SumNode(NodePtr &p) : l(p), vresult(1) {}
2328 const VResult &lvec = l->result();
2329 size_type size = lvec.size();
2335 for (off_type i = 0; i < size; ++i)
2336 vresult[0] = op(vresult[0], lvec[i]);
2344 const VResult &lvec = l->result();
2345 size_type size = lvec.size();
2348 Result vresult = 0.0;
2351 for (off_type i = 0; i < size; ++i)
2352 vresult = op(vresult, lvec[i]);
2357 size_type size() const { return 1; }
2362 return csprintf("total(%s)", l->str());
2367 //////////////////////////////////////////////////////////////////////
2369 // Visible Statistics Types
2371 //////////////////////////////////////////////////////////////////////
2373 * @defgroup VisibleStats "Statistic Types"
2374 * These are the statistics that are used in the simulator.
2379 * This is a simple scalar statistic, like a counter.
2380 * @sa Stat, ScalarBase, StatStor
2382 class Scalar : public ScalarBase<Scalar, StatStor>
2385 using ScalarBase<Scalar, StatStor>::operator=;
2389 * A stat that calculates the per tick average of a value.
2390 * @sa Stat, ScalarBase, AvgStor
2392 class Average : public ScalarBase<Average, AvgStor>
2395 using ScalarBase<Average, AvgStor>::operator=;
2398 class Value : public ValueBase<Value>
2403 * A vector of scalar stats.
2404 * @sa Stat, VectorBase, StatStor
2406 class Vector : public VectorBase<Vector, StatStor>
2411 * A vector of Average stats.
2412 * @sa Stat, VectorBase, AvgStor
2414 class AverageVector : public VectorBase<AverageVector, AvgStor>
2419 * A 2-Dimensional vecto of scalar stats.
2420 * @sa Stat, Vector2dBase, StatStor
2422 class Vector2d : public Vector2dBase<Vector2d, StatStor>
2427 * A simple distribution stat.
2428 * @sa Stat, DistBase, DistStor
2430 class Distribution : public DistBase<Distribution, DistStor>
2434 * Set the parameters of this distribution. @sa DistStor::Params
2435 * @param min The minimum value of the distribution.
2436 * @param max The maximum value of the distribution.
2437 * @param bkt The number of values in each bucket.
2438 * @return A reference to this distribution.
2441 init(Counter min, Counter max, Counter bkt)
2443 DistStor::Params *params = new DistStor::Params;
2446 params->bucket_size = bkt;
2447 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2448 this->setParams(params);
2450 return this->self();
2455 * A simple histogram stat.
2456 * @sa Stat, DistBase, HistStor
2458 class Histogram : public DistBase<Histogram, HistStor>
2462 * Set the parameters of this histogram. @sa HistStor::Params
2463 * @param size The number of buckets in the histogram
2464 * @return A reference to this histogram.
2467 init(size_type size)
2469 HistStor::Params *params = new HistStor::Params;
2470 params->buckets = size;
2471 this->setParams(params);
2473 return this->self();
2478 * Calculates the mean and variance of all the samples.
2479 * @sa DistBase, SampleStor
2481 class StandardDeviation : public DistBase<StandardDeviation, SampleStor>
2485 * Construct and initialize this distribution.
2489 SampleStor::Params *params = new SampleStor::Params;
2491 this->setParams(params);
2496 * Calculates the per tick mean and variance of the samples.
2497 * @sa DistBase, AvgSampleStor
2499 class AverageDeviation : public DistBase<AverageDeviation, AvgSampleStor>
2503 * Construct and initialize this distribution.
2507 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2509 this->setParams(params);
2514 * A vector of distributions.
2515 * @sa VectorDistBase, DistStor
2517 class VectorDistribution : public VectorDistBase<VectorDistribution, DistStor>
2521 * Initialize storage and parameters for this distribution.
2522 * @param size The size of the vector (the number of distributions).
2523 * @param min The minimum value of the distribution.
2524 * @param max The maximum value of the distribution.
2525 * @param bkt The number of values in each bucket.
2526 * @return A reference to this distribution.
2528 VectorDistribution &
2529 init(size_type size, Counter min, Counter max, Counter bkt)
2531 DistStor::Params *params = new DistStor::Params;
2534 params->bucket_size = bkt;
2535 params->buckets = (size_type)ceil((max - min + 1.0) / bkt);
2536 this->setParams(params);
2538 return this->self();
2543 * This is a vector of StandardDeviation stats.
2544 * @sa VectorDistBase, SampleStor
2546 class VectorStandardDeviation
2547 : public VectorDistBase<VectorStandardDeviation, SampleStor>
2551 * Initialize storage for this distribution.
2552 * @param size The size of the vector.
2553 * @return A reference to this distribution.
2555 VectorStandardDeviation &
2556 init(size_type size)
2558 SampleStor::Params *params = new SampleStor::Params;
2560 this->setParams(params);
2561 return this->self();
2566 * This is a vector of AverageDeviation stats.
2567 * @sa VectorDistBase, AvgSampleStor
2569 class VectorAverageDeviation
2570 : public VectorDistBase<VectorAverageDeviation, AvgSampleStor>
2574 * Initialize storage for this distribution.
2575 * @param size The size of the vector.
2576 * @return A reference to this distribution.
2578 VectorAverageDeviation &
2579 init(size_type size)
2581 AvgSampleStor::Params *params = new AvgSampleStor::Params;
2583 this->setParams(params);
2584 return this->self();
2588 template <class Stat>
2589 class FormulaInfoProxy : public InfoProxy<Stat, FormulaInfo>
2592 mutable VResult vec;
2593 mutable VCounter cvec;
2596 FormulaInfoProxy(Stat &stat) : InfoProxy<Stat, FormulaInfo>(stat) {}
2598 size_type size() const { return this->s.size(); }
2603 this->s.result(vec);
2606 Result total() const { return this->s.total(); }
2607 VCounter &value() const { return cvec; }
2609 std::string str() const { return this->s.str(); }
2612 template <class Stat>
2613 class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
2616 SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
2620 * Implementation of a sparse histogram stat. The storage class is
2621 * determined by the Storage template.
2623 template <class Derived, class Stor>
2624 class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
2627 typedef SparseHistInfoProxy<Derived> Info;
2628 typedef Stor Storage;
2629 typedef typename Stor::Params Params;
2632 /** The storage for this stat. */
2633 char storage[sizeof(Storage)];
2637 * Retrieve the storage.
2638 * @return The storage object for this stat.
2643 return reinterpret_cast<Storage *>(storage);
2647 * Retrieve a const pointer to the storage.
2648 * @return A const pointer to the storage object for this stat.
2653 return reinterpret_cast<const Storage *>(storage);
2659 new (storage) Storage(this->info());
2664 SparseHistBase() { }
2667 * Add a value to the distribtion n times. Calls sample on the storage
2669 * @param v The value to add.
2670 * @param n The number of times to add it, defaults to 1.
2672 template <typename U>
2673 void sample(const U &v, int n = 1) { data()->sample(v, n); }
2676 * Return the number of entries in this stat.
2677 * @return The number of entries.
2679 size_type size() const { return data()->size(); }
2681 * Return true if no samples have been added.
2682 * @return True if there haven't been any samples.
2684 bool zero() const { return data()->zero(); }
2689 Info *info = this->info();
2690 data()->prepare(info, info->data);
2694 * Reset stat value to default
2699 data()->reset(this->info());
2704 * Templatized storage and interface for a sparse histogram stat.
2706 class SparseHistStor
2709 /** The parameters for a sparse histogram stat. */
2710 struct Params : public DistParams
2712 Params() : DistParams(Hist) {}
2716 /** Counter for number of samples */
2718 /** Counter for each bucket. */
2722 SparseHistStor(Info *info)
2728 * Add a value to the distribution for the given number of times.
2729 * @param val The value to add.
2730 * @param number The number of times to add the value.
2733 sample(Counter val, int number)
2735 cmap[val] += number;
2740 * Return the number of buckets in this distribution.
2741 * @return the number of buckets.
2743 size_type size() const { return cmap.size(); }
2746 * Returns true if any calls to sample have been made.
2747 * @return True if any values have been sampled.
2752 return samples == Counter();
2756 prepare(Info *info, SparseHistData &data)
2758 MCounter::iterator it;
2760 for (it = cmap.begin(); it != cmap.end(); it++) {
2761 data.cmap[(*it).first] = (*it).second;
2764 data.samples = samples;
2768 * Reset stat value to default
2778 class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
2782 * Set the parameters of this histogram. @sa HistStor::Params
2783 * @param size The number of buckets in the histogram
2784 * @return A reference to this histogram.
2787 init(size_type size)
2789 SparseHistStor::Params *params = new SparseHistStor::Params;
2790 this->setParams(params);
2792 return this->self();
2798 * A formula for statistics that is calculated when printed. A formula is
2799 * stored as a tree of Nodes that represent the equation to calculate.
2800 * @sa Stat, ScalarStat, VectorStat, Node, Temp
2802 class Formula : public DataWrapVec<Formula, FormulaInfoProxy>
2805 /** The root of the tree which represents the Formula */
2811 * Create and initialize thie formula, and register it with the database.
2816 * Create a formula with the given root node, register it with the
2818 * @param r The root of the expression tree.
2823 * Set an unitialized Formula to the given root.
2824 * @param r The root of the expression tree.
2825 * @return a reference to this formula.
2827 const Formula &operator=(Temp r);
2830 * Add the given tree to the existing one.
2831 * @param r The root of the expression tree.
2832 * @return a reference to this formula.
2834 const Formula &operator+=(Temp r);
2836 * Return the result of the Fomula in a vector. If there were no Vector
2837 * components to the Formula, then the vector is size 1. If there were,
2838 * like x/y with x being a vector of size 3, then the result returned will
2839 * be x[0]/y, x[1]/y, x[2]/y, respectively.
2840 * @return The result vector.
2842 void result(VResult &vec) const;
2845 * Return the total Formula result. If there is a Vector
2846 * component to this Formula, then this is the result of the
2847 * Formula if the formula is applied after summing all the
2848 * components of the Vector. For example, if Formula is x/y where
2849 * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If
2850 * there is no Vector component, total() returns the same value as
2851 * the first entry in the VResult val() returns.
2852 * @return The total of the result vector.
2854 Result total() const;
2857 * Return the number of elements in the tree.
2859 size_type size() const;
2864 * Formulas don't need to be reset
2873 std::string str() const;
2876 class FormulaNode : public Node
2879 const Formula &formula;
2880 mutable VResult vec;
2883 FormulaNode(const Formula &f) : formula(f) {}
2885 size_type size() const { return formula.size(); }
2886 const VResult &result() const { formula.result(vec); return vec; }
2887 Result total() const { return formula.total(); }
2889 std::string str() const { return formula.str(); }
2893 * Helper class to construct formula node trees.
2899 * Pointer to a Node object.
2905 * Copy the given pointer to this class.
2906 * @param n A pointer to a Node object to copy.
2908 Temp(NodePtr n) : node(n) { }
2911 * Return the node pointer.
2912 * @return the node pointer.
2914 operator NodePtr&() { return node; }
2918 * Create a new ScalarStatNode.
2919 * @param s The ScalarStat to place in a node.
2921 Temp(const Scalar &s)
2922 : node(new ScalarStatNode(s.info()))
2926 * Create a new ScalarStatNode.
2927 * @param s The ScalarStat to place in a node.
2929 Temp(const Value &s)
2930 : node(new ScalarStatNode(s.info()))
2934 * Create a new ScalarStatNode.
2935 * @param s The ScalarStat to place in a node.
2937 Temp(const Average &s)
2938 : node(new ScalarStatNode(s.info()))
2942 * Create a new VectorStatNode.
2943 * @param s The VectorStat to place in a node.
2945 Temp(const Vector &s)
2946 : node(new VectorStatNode(s.info()))
2949 Temp(const AverageVector &s)
2950 : node(new VectorStatNode(s.info()))
2956 Temp(const Formula &f)
2957 : node(new FormulaNode(f))
2961 * Create a new ScalarProxyNode.
2962 * @param p The ScalarProxy to place in a node.
2964 template <class Stat>
2965 Temp(const ScalarProxy<Stat> &p)
2966 : node(new ScalarProxyNode<Stat>(p))
2970 * Create a ConstNode
2971 * @param value The value of the const node.
2973 Temp(signed char value)
2974 : node(new ConstNode<signed char>(value))
2978 * Create a ConstNode
2979 * @param value The value of the const node.
2981 Temp(unsigned char value)
2982 : node(new ConstNode<unsigned char>(value))
2986 * Create a ConstNode
2987 * @param value The value of the const node.
2989 Temp(signed short value)
2990 : node(new ConstNode<signed short>(value))
2994 * Create a ConstNode
2995 * @param value The value of the const node.
2997 Temp(unsigned short value)
2998 : node(new ConstNode<unsigned short>(value))
3002 * Create a ConstNode
3003 * @param value The value of the const node.
3005 Temp(signed int value)
3006 : node(new ConstNode<signed int>(value))
3010 * Create a ConstNode
3011 * @param value The value of the const node.
3013 Temp(unsigned int value)
3014 : node(new ConstNode<unsigned int>(value))
3018 * Create a ConstNode
3019 * @param value The value of the const node.
3021 Temp(signed long value)
3022 : node(new ConstNode<signed long>(value))
3026 * Create a ConstNode
3027 * @param value The value of the const node.
3029 Temp(unsigned long value)
3030 : node(new ConstNode<unsigned long>(value))
3034 * Create a ConstNode
3035 * @param value The value of the const node.
3037 Temp(signed long long value)
3038 : node(new ConstNode<signed long long>(value))
3042 * Create a ConstNode
3043 * @param value The value of the const node.
3045 Temp(unsigned long long value)
3046 : node(new ConstNode<unsigned long long>(value))
3050 * Create a ConstNode
3051 * @param value The value of the const node.
3054 : node(new ConstNode<float>(value))
3058 * Create a ConstNode
3059 * @param value The value of the const node.
3062 : node(new ConstNode<double>(value))
3072 operator+(Temp l, Temp r)
3074 return NodePtr(new BinaryNode<std::plus<Result> >(l, r));
3078 operator-(Temp l, Temp r)
3080 return NodePtr(new BinaryNode<std::minus<Result> >(l, r));
3084 operator*(Temp l, Temp r)
3086 return NodePtr(new BinaryNode<std::multiplies<Result> >(l, r));
3090 operator/(Temp l, Temp r)
3092 return NodePtr(new BinaryNode<std::divides<Result> >(l, r));
3098 return NodePtr(new UnaryNode<std::negate<Result> >(l));
3101 template <typename T>
3105 return NodePtr(new ConstNode<T>(val));
3108 template <typename T>
3110 constantVector(T val)
3112 return NodePtr(new ConstVectorNode<T>(val));
3118 return NodePtr(new SumNode<std::plus<Result> >(val));
3121 /** Dump all statistics data to the registered outputs */
3126 * Register a callback that should be called whenever statistics are
3129 void registerResetCallback(Callback *cb);
3131 std::list<Info *> &statsList();
3133 } // namespace Stats
3135 #endif // __BASE_STATISTICS_HH__