statistics.hh
来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· HH 代码 · 共 2,775 行 · 第 1/5 页
HH
2,775 行
NodePtr l; mutable VResult vresult; public: SumNode(NodePtr &p) : l(p), vresult(1) {} const VResult &result() const { const VResult &lvec = l->result(); int size = lvec.size(); assert(size > 0); vresult[0] = 0.0; Op op; for (int i = 0; i < size; ++i) vresult[0] = op(vresult[0], lvec[i]); return vresult; } Result total() const { const VResult &lvec = l->result(); int size = lvec.size(); assert(size > 0); Result vresult = 0.0; Op op; for (int i = 0; i < size; ++i) vresult = op(vresult, lvec[i]); return vresult; } virtual size_t size() const { return 1; } virtual std::string str() const { return csprintf("total(%s)", l->str()); }};////////////////////////////////////////////////////////////////////////// Visible Statistics Types/////////////////////////////////////////////////////////////////////////** * @defgroup VisibleStats "Statistic Types" * These are the statistics that are used in the simulator. * @{ *//** * This is a simple scalar statistic, like a counter. * @sa Stat, ScalarBase, StatStor */template<int N = 0>class Scalar : public Wrap<Scalar<N>, ScalarBase<StatStor>, ScalarStatData>{ public: /** The base implementation. */ typedef ScalarBase<StatStor> Base; Scalar() { this->doInit(); } /** * Sets the stat equal to the given value. Calls the base implementation * of operator= * @param v The new value. */ template <typename U> void operator=(const U &v) { Base::operator=(v); }};class Value : public Wrap<Value, ValueBase, ScalarStatData>{ public: /** The base implementation. */ typedef ValueBase Base; template <class T> Value &scalar(T &value) { Base::scalar(value); return *this; } template <class T> Value &functor(T &func) { Base::functor(func); return *this; }};/** * A stat that calculates the per tick average of a value. * @sa Stat, ScalarBase, AvgStor */template<int N = 0>class Average : public Wrap<Average<N>, ScalarBase<AvgStor>, ScalarStatData>{ public: /** The base implementation. */ typedef ScalarBase<AvgStor> Base; Average() { this->doInit(); } /** * Sets the stat equal to the given value. Calls the base implementation * of operator= * @param v The new value. */ template <typename U> void operator=(const U &v) { Base::operator=(v); }};/** * A vector of scalar stats. * @sa Stat, VectorBase, StatStor */template<int N = 0>class Vector : public WrapVec<Vector<N>, VectorBase<StatStor>, VectorStatData>{ public: /** The base implementation. */ typedef ScalarBase<StatStor> Base; /** * Set this vector to have the given size. * @param size The new size. * @return A reference to this stat. */ Vector &init(size_t size) { this->doInit(size); return *this; }};/** * A vector of Average stats. * @sa Stat, VectorBase, AvgStor */template<int N = 0>class AverageVector : public WrapVec<AverageVector<N>, VectorBase<AvgStor>, VectorStatData>{ public: /** * Set this vector to have the given size. * @param size The new size. * @return A reference to this stat. */ AverageVector &init(size_t size) { this->doInit(size); return *this; }};/** * A 2-Dimensional vecto of scalar stats. * @sa Stat, Vector2dBase, StatStor */template<int N = 0>class Vector2d : public WrapVec2d<Vector2d<N>, Vector2dBase<StatStor>, Vector2dStatData>{ public: Vector2d &init(size_t x, size_t y) { this->doInit(x, y); return *this; }};/** * A simple distribution stat. * @sa Stat, DistBase, DistStor */template<int N = 0>class Distribution : public Wrap<Distribution<N>, DistBase<DistStor>, DistStatData>{ public: /** Base implementation. */ typedef DistBase<DistStor> Base; /** The Parameter type. */ typedef DistStor::Params Params; public: /** * Set the parameters of this distribution. @sa DistStor::Params * @param min The minimum value of the distribution. * @param max The maximum value of the distribution. * @param bkt The number of values in each bucket. * @return A reference to this distribution. */ Distribution &init(Counter min, Counter max, Counter bkt) { this->params.min = min; this->params.max = max; this->params.bucket_size = bkt; this->params.size = (int)rint((max - min) / bkt + 1.0); this->doInit(); return *this; }};/** * Calculates the mean and variance of all the samples. * @sa Stat, DistBase, FancyStor */template<int N = 0>class StandardDeviation : public Wrap<StandardDeviation<N>, DistBase<FancyStor>, DistStatData>{ public: /** The base implementation */ typedef DistBase<DistStor> Base; /** The parameter type. */ typedef DistStor::Params Params; public: /** * Construct and initialize this distribution. */ StandardDeviation() { this->doInit(); }};/** * Calculates the per tick mean and variance of the samples. * @sa Stat, DistBase, AvgFancy */template<int N = 0>class AverageDeviation : public Wrap<AverageDeviation<N>, DistBase<AvgFancy>, DistStatData>{ public: /** The base implementation */ typedef DistBase<DistStor> Base; /** The parameter type. */ typedef DistStor::Params Params; public: /** * Construct and initialize this distribution. */ AverageDeviation() { this->doInit(); }};/** * A vector of distributions. * @sa Stat, VectorDistBase, DistStor */template<int N = 0>class VectorDistribution : public WrapVec<VectorDistribution<N>, VectorDistBase<DistStor>, VectorDistStatData>{ public: /** The base implementation */ typedef VectorDistBase<DistStor> Base; /** The parameter type. */ typedef DistStor::Params Params; public: /** * Initialize storage and parameters for this distribution. * @param size The size of the vector (the number of distributions). * @param min The minimum value of the distribution. * @param max The maximum value of the distribution. * @param bkt The number of values in each bucket. * @return A reference to this distribution. */ VectorDistribution &init(int size, Counter min, Counter max, Counter bkt) { this->params.min = min; this->params.max = max; this->params.bucket_size = bkt; this->params.size = (int)rint((max - min) / bkt + 1.0); this->doInit(size); return *this; }};/** * This is a vector of StandardDeviation stats. * @sa Stat, VectorDistBase, FancyStor */template<int N = 0>class VectorStandardDeviation : public WrapVec<VectorStandardDeviation<N>, VectorDistBase<FancyStor>, VectorDistStatData>{ public: /** The base implementation */ typedef VectorDistBase<FancyStor> Base; /** The parameter type. */ typedef DistStor::Params Params; public: /** * Initialize storage for this distribution. * @param size The size of the vector. * @return A reference to this distribution. */ VectorStandardDeviation &init(int size) { this->doInit(size); return *this; }};/** * This is a vector of AverageDeviation stats. * @sa Stat, VectorDistBase, AvgFancy */template<int N = 0>class VectorAverageDeviation : public WrapVec<VectorAverageDeviation<N>, VectorDistBase<AvgFancy>, VectorDistStatData>{ public: /** The base implementation */ typedef VectorDistBase<AvgFancy> Base; /** The parameter type. */ typedef DistStor::Params Params; public: /** * Initialize storage for this distribution. * @param size The size of the vector. * @return A reference to this distribution. */ VectorAverageDeviation &init(int size) { this->doInit(size); return *this; }};/** * A formula for statistics that is calculated when printed. A formula is * stored as a tree of Nodes that represent the equation to calculate. * @sa Stat, ScalarStat, VectorStat, Node, Temp */class FormulaBase : public DataAccess{ protected: /** The root of the tree which represents the Formula */ NodePtr root; friend class Temp; public: /** * Return the result of the Fomula in a vector. If there were no Vector * components to the Formula, then the vector is size 1. If there were, * like x/y with x being a vector of size 3, then the result returned will * be x[0]/y, x[1]/y, x[2]/y, respectively. * @return The result vector. */ void result(VResult &vec) const; /** * Return the total Formula result. If there is a Vector * component to this Formula, then this is the result of the * Formula if the formula is applied after summing all the * components of the Vector. For example, if Formula is x/y where * x is size 3, then total() will return (x[1]+x[2]+x[3])/y. If * there is no Vector component, total() returns the same value as * the first entry in the VResult val() returns. * @return The total of the result vector. */ Result total() const; /** * Return the number of elements in the tree. */ size_t size() const; bool check() const { return true; } /** * Formulas don't need to be reset */ void reset(); /** * */ bool zero() const; /** * */ void update(StatData *); std::string str() const;};class FormulaData : public VectorData{ public: virtual std::string str() const = 0; virtual bool check() const { return true; }};template <class Stat>class FormulaStatData : public FormulaData{ protected: Stat &s; mutable VResult vec; mutable VCounter cvec; public: FormulaStatData(Stat &stat) : s(stat) {} virtual bool zero() const { return s.zero(); } virtual void reset() { s.reset(); } virtual size_t size() const { return s.size(); } virtual const VResult &result() const { s.result(vec); return vec; } virtual Result total() const { return s.total(); } virtual VCounter &value() const { return cvec; } virtual void visit(Visit &visitor) { update(); s.update(this); visitor.visit(*this); } virtual std::string str() const { return s.str(); }};class Temp;class Formula : public WrapVec<Formula, FormulaBase, FormulaStatData>{ public: /** * Create and initialize thie formula, and register it with the database. */ Formula(); /** * Create a formula with the given root node, register it with the * database. * @param r The root of the expression tree. */ Formula(Temp r); /** * Set an unitialized Formula to the given root. * @param r The root of the expression tree. * @return a reference to this formula. */ const Formula &operator=(Temp r); /** * Add the given tree to the existing one. * @param r The root of the expression tree. * @return a reference to this formula. */ const Formula &operator+=(Temp r);};class FormulaNode : public Node{ private: const Formula &formula; mutable VResult vec; public: FormulaNode(const Formula &f) : formula(f) {} virtual size_t size() const { return formula.size(); } virtual const VResult &result() const { formula.result(vec); return vec; } virtual Result total() const { return formula.total(); } virtual std::string str() const { return formula.str(); }};/** * Helper class to construct formula node trees. */class Temp{ protected: /** * Pointer to a Node object. */ NodePtr node; public: /** * Copy the given pointer to this class. * @param n A pointer to a Node object to copy. */ Temp(NodePtr n) : node(n) { } /** * Return the node pointer. * @return the node pointer. */ operator NodePtr&() { return node;} public: /** * Create a new ScalarStatNode. * @param s The ScalarStat to place in a node. */ template <int N> Temp(const Scalar<N> &s) : node(new ScalarStatNode(s.statData())) { } /** * Create a new ScalarStatNode. * @param s The ScalarStat to place in a node. */ Temp(const Value &s) : node(new ScalarStatNode(s.statData())) { } /** * Create a new ScalarStatNode. * @param s The ScalarStat to place in a node. */ template <int N> Temp(const Average<N> &s) : node(new ScalarStatNode(s.statData())) { } /** * Create a new VectorStatNode. * @param s The VectorStat to place in a node. */ template <int N> Temp(const Vector<N>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?