statistics.hh
来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· HH 代码 · 共 2,775 行 · 第 1/5 页
HH
2,775 行
*/ StatStor(const Params &) : data(Counter()) {} /** * The the stat to the given value. * @param val The new value. * @param p The paramters of this storage type. */ void set(Counter val, const Params &p) { data = val; } /** * Increment the stat by the given value. * @param val The new value. * @param p The paramters of this storage type. */ void inc(Counter val, const Params &p) { data += val; } /** * Decrement the stat by the given value. * @param val The new value. * @param p The paramters of this storage type. */ void dec(Counter val, const Params &p) { data -= val; } /** * Return the value of this stat as its base type. * @param p The params of this storage type. * @return The value of this stat. */ Counter value(const Params &p) const { return data; } /** * Return the value of this stat as a result type. * @param p The parameters of this storage type. * @return The value of this stat. */ Result result(const Params &p) const { return (Result)data; } /** * Reset stat value to default */ void reset() { data = Counter(); } /** * @return true if zero value */ bool zero() const { return data == Counter(); }};/** * Templatized storage and interface to a per-tick average stat. This keeps * a current count and updates a total (count * ticks) when this count * changes. This allows the quick calculation of a per tick count of the item * being watched. This is good for keeping track of residencies in structures * among other things. */struct AvgStor{ public: /** The paramaters for this storage type */ struct Params { }; private: /** The current count. */ Counter current; /** The total count for all tick. */ mutable Result total; /** The tick that current last changed. */ mutable Tick last; public: /** * Build and initializes this stat storage. */ AvgStor(Params &p) : current(0), total(0), last(0) { } /** * Set the current count to the one provided, update the total and last * set values. * @param val The new count. * @param p The parameters for this storage. */ void set(Counter val, Params &p) { total += current * (curTick - last); last = curTick; current = val; } /** * Increment the current count by the provided value, calls set. * @param val The amount to increment. * @param p The parameters for this storage. */ void inc(Counter val, Params &p) { set(current + val, p); } /** * Deccrement the current count by the provided value, calls set. * @param val The amount to decrement. * @param p The parameters for this storage. */ void dec(Counter val, Params &p) { set(current - val, p); } /** * Return the current count. * @param p The parameters for this storage. * @return The current count. */ Counter value(const Params &p) const { return current; } /** * Return the current average. * @param p The parameters for this storage. * @return The current average. */ Result result(const Params &p) const { total += current * (curTick - last); last = curTick; return (Result)(total + current) / (Result)(curTick + 1); } /** * Reset stat value to default */ void reset() { total = 0; last = curTick; } /** * @return true if zero value */ bool zero() const { return total == 0.0; }};/** * Implementation of a scalar stat. The type of stat is determined by the * Storage template. */template <class Stor>class ScalarBase : public DataAccess{ public: typedef Stor Storage; /** Define the params of the storage class. */ typedef typename Storage::Params Params; protected: /** The storage of this stat. */ char storage[sizeof(Storage)] __attribute__ ((aligned (8))); /** The parameters for this stat. */ Params params; protected: /** * Retrieve the storage. * @param index The vector index to access. * @return The storage object at the given index. */ Storage * data() { return reinterpret_cast<Storage *>(storage); } /** * Retrieve a const pointer to the storage. * for the given index. * @param index The vector index to access. * @return A const pointer to the storage object at the given index. */ const Storage * data() const { return reinterpret_cast<const Storage *>(storage); } void doInit() { new (storage) Storage(params); setInit(); } public: /** * Return the current value of this stat as its base type. * @return The current value. */ Counter value() const { return data()->value(params); } public: /** * Create and initialize this stat, register it with the database. */ ScalarBase() { } public: // Common operators for stats /** * Increment the stat by 1. This calls the associated storage object inc * function. */ void operator++() { data()->inc(1, params); } /** * Decrement the stat by 1. This calls the associated storage object dec * function. */ void operator--() { data()->dec(1, params); } /** Increment the stat by 1. */ void operator++(int) { ++*this; } /** Decrement the stat by 1. */ void operator--(int) { --*this; } /** * Set the data value to the given value. This calls the associated storage * object set function. * @param v The new value. */ template <typename U> void operator=(const U &v) { data()->set(v, params); } /** * Increment the stat by the given value. This calls the associated * storage object inc function. * @param v The value to add. */ template <typename U> void operator+=(const U &v) { data()->inc(v, params); } /** * Decrement the stat by the given value. This calls the associated * storage object dec function. * @param v The value to substract. */ template <typename U> void operator-=(const U &v) { data()->dec(v, params); } /** * Return the number of elements, always 1 for a scalar. * @return 1. */ size_t size() const { return 1; } bool check() const { return true; } /** * Reset stat value to default */ void reset() { data()->reset(); } Counter value() { return data()->value(params); } Result result() { return data()->result(params); } Result total() { return result(); } bool zero() { return result() == 0.0; }};class ProxyData : public ScalarData{ public: virtual void visit(Visit &visitor) { visitor.visit(*this); } virtual std::string str() const { return to_string(value()); } virtual size_t size() const { return 1; } virtual bool zero() const { return value() == 0; } virtual bool check() const { return true; } virtual void reset() { }};template <class T>class ValueProxy : public ProxyData{ private: T *scalar; public: ValueProxy(T &val) : scalar(&val) {} virtual Counter value() const { return *scalar; } virtual Result result() const { return *scalar; } virtual Result total() const { return *scalar; }};template <class T>class FunctorProxy : public ProxyData{ private: T *functor; public: FunctorProxy(T &func) : functor(&func) {} virtual Counter value() const { return (*functor)(); } virtual Result result() const { return (*functor)(); } virtual Result total() const { return (*functor)(); }};class ValueBase : public DataAccess{ private: ProxyData *proxy; public: ValueBase() : proxy(NULL) { } ~ValueBase() { if (proxy) delete proxy; } template <class T> void scalar(T &value) { proxy = new ValueProxy<T>(value); setInit(); } template <class T> void functor(T &func) { proxy = new FunctorProxy<T>(func); setInit(); } Counter value() { return proxy->value(); } Result result() const { return proxy->result(); } Result total() const { return proxy->total(); }; size_t size() const { return proxy->size(); } std::string str() const { return proxy->str(); } bool zero() const { return proxy->zero(); } bool check() const { return proxy != NULL; } void reset() { }};////////////////////////////////////////////////////////////////////////// Vector Statistics/////////////////////////////////////////////////////////////////////////** * A proxy class to access the stat at a given index in a VectorBase stat. * Behaves like a ScalarBase. */template <class Stat>class ScalarProxy{ private: /** Pointer to the parent Vector. */ Stat *stat; /** The index to access in the parent VectorBase. */ int index; public: /** * Return the current value of this stat as its base type. * @return The current value. */ Counter value() const { return stat->data(index)->value(stat->params); } /** * Return the current value of this statas a result type. * @return The current value. */ Result result() const { return stat->data(index)->result(stat->params); } public: /** * Create and initialize this proxy, do not register it with the database. * @param p The params to use. * @param i The index to access. */ ScalarProxy(Stat *s, int i) : stat(s), index(i) { assert(stat); } /** * Create a copy of the provided ScalarProxy. * @param sp The proxy to copy. */ ScalarProxy(const ScalarProxy &sp) : stat(sp.stat), index(sp.index) {} /** * Set this proxy equal to the provided one. * @param sp The proxy to copy. * @return A reference to this proxy. */ const ScalarProxy &operator=(const ScalarProxy &sp) { stat = sp.stat; index = sp.index; return *this; } public: // Common operators for stats /** * Increment the stat by 1. This calls the associated storage object inc * function. */ void operator++() { stat->data(index)->inc(1, stat->params); } /** * Decrement the stat by 1. This calls the associated storage object dec * function. */ void operator--() { stat->data(index)->dec(1, stat->params); } /** Increment the stat by 1. */ void operator++(int) { ++*this; } /** Decrement the stat by 1. */ void operator--(int) { --*this; } /** * Set the data value to the given value. This calls the associated storage * object set function. * @param v The new value. */ template <typename U> void operator=(const U &v) { stat->data(index)->set(v, stat->params); } /** * Increment the stat by the given value. This calls the associated * storage object inc function. * @param v The value to add. */ template <typename U> void operator+=(const U &v) { stat->data(index)->inc(v, stat->params); } /** * Decrement the stat by the given value. This calls the associated * storage object dec function. * @param v The value to substract. */ template <typename U> void operator-=(const U &v) { stat->data(index)->dec(v, stat->params); } /** * Return the number of elements, always 1 for a scalar. * @return 1. */ size_t size() const { return 1; } /** * This stat has no state. Nothing to reset */ void reset() { } public: std::string str() const { return csprintf("%s[%d]", stat->str(), index); }};/** * Implementation of a vector of stats. The type of stat is determined by the * Storage class. @sa ScalarBase */template <class Stor>class VectorBase : public DataAccess{ public: typedef Stor Storage; /** Define the params of the storage class. */ typedef typename Storage::Params Params; /** Proxy type */ typedef ScalarProxy<VectorBase<Storage> > Proxy; friend class ScalarProxy<VectorBase<Storage> >; protected: /** The storage of this stat. */ Storage *storage; size_t _size; /** The parameters for this stat. */ Params params; protected: /** * Retrieve the storage. * @param index The vector index to access. * @return The storage object at the given index. */ Storage *data(int index) { return &storage[index]; } /** * Retrieve a const pointer to the storage. * @param index The vector index to access. * @return A const pointer to the storage object at the given index. */ const Storage *data(int index) const { return &storage[index]; } void doInit(int s) { assert(s > 0 && "size must be positive!"); assert(!storage && "already initialized"); _size = s; char *ptr = new char[_size * sizeof(Storage)]; storage = reinterpret_cast<Storage *>(ptr); for (int i = 0; i < _size; ++i) new (&storage[i]) Storage(params); setInit(); } public: void value(VCounter &vec) const { vec.resize(size()); for (int i = 0; i < size(); ++i) vec[i] = data(i)->value(params); } /** * Copy the values to a local vector and return a reference to it. * @return A reference to a vector of the stat values. */ void result(VResult &vec) const { vec.resize(size()); for (int i = 0; i < size(); ++i) vec[i] = data(i)->result(params); } /** * Return a total of all entries in this vector. * @return The total of all vector entries. */ Result total() const { Result total = 0.0; for (int i = 0; i < size(); ++i) total += data(i)->result(params); return total; } /** * @return the number of elements in this vector. */ size_t size() const { return _size; } bool zero() const { for (int i = 0; i < size(); ++i) if (data(i)->zero())
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?