statistics.hh
来自「M5,一个功能强大的多处理器系统模拟器.很多针对处理器架构,性能的研究都使用它作」· HH 代码 · 共 2,775 行 · 第 1/5 页
HH
2,775 行
/* * Copyright (c) 2003, 2004, 2005 * The Regents of The University of Michigan * All Rights Reserved * * This code is part of the M5 simulator. * * Permission is granted to use, copy, create derivative works and * redistribute this software and such derivative works for any * purpose, so long as the copyright notice above, this grant of * permission, and the disclaimer below appear in all copies made; and * so long as the name of The University of Michigan is not used in * any advertising or publicity pertaining to the use or distribution * of this software without specific, written prior authorization. * * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE * UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND * WITHOUT WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE * LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, SPECIAL, INDIRECT, * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM * ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH * DAMAGES. * * Authors: Nathan L. Binkert * Erik G. Hallnor *//** @file * Declaration of Statistics objects. *//*** @todo** Generalized N-dimensinal vector* documentation* key stats* interval stats* -- these both can use the same function that prints out a* specific set of stats* VectorStandardDeviation totals* Document Namespaces*/#ifndef __BASE_STATISTICS_HH__#define __BASE_STATISTICS_HH__#include <algorithm>#include <cassert>#ifdef __SUNPRO_CC#include <math.h>#endif#include <cmath>#include <functional>#include <iosfwd>#include <string>#include <vector>#include "base/cprintf.hh"#include "base/intmath.hh"#include "base/refcnt.hh"#include "base/str.hh"#include "base/stats/flags.hh"#include "base/stats/visit.hh"#include "base/stats/types.hh"#include "sim/host.hh"class Callback;/** The current simulated tick. */extern Tick curTick;/* A namespace for all of the Statistics */namespace Stats {/* Contains the statistic implementation details */////////////////////////////////////////////////////////////////////////// Statistics Framework Base classes////////////////////////////////////////////////////////////////////////struct StatData{ /** The name of the stat. */ std::string name; /** The description of the stat. */ std::string desc; /** The formatting flags. */ StatFlags flags; /** The display precision. */ int precision; /** A pointer to a prerequisite Stat. */ const StatData *prereq; /** * A unique stat ID for each stat in the simulator. * Can be used externally for lookups as well as for debugging. */ int id; StatData(); virtual ~StatData(); /** * Reset the corresponding stat to the default state. */ virtual void reset() = 0; /** * @return true if this stat has a value and satisfies its * requirement as a prereq */ virtual bool zero() const = 0; /** * Check that this stat has been set up properly and is ready for * use * @return true for success */ virtual bool check() const = 0; bool baseCheck() const; /** * Visitor entry for outputing statistics data */ virtual void visit(Visit &visitor) = 0; /** * Checks if the first stat's name is alphabetically less than the second. * This function breaks names up at periods and considers each subname * separately. * @param stat1 The first stat. * @param stat2 The second stat. * @return stat1's name is alphabetically before stat2's */ static bool less(StatData *stat1, StatData *stat2);};class ScalarData : public StatData{ public: virtual Counter value() const = 0; virtual Result result() const = 0; virtual Result total() const = 0; virtual void visit(Visit &visitor) { visitor.visit(*this); }};template <class Stat>class ScalarStatData : public ScalarData{ protected: Stat &s; public: ScalarStatData(Stat &stat) : s(stat) {} virtual bool check() const { return s.check(); } virtual Counter value() const { return s.value(); } virtual Result result() const { return s.result(); } virtual Result total() const { return s.total(); } virtual void reset() { s.reset(); } virtual bool zero() const { return s.zero(); }};struct VectorData : public StatData{ /** Names and descriptions of subfields. */ mutable std::vector<std::string> subnames; mutable std::vector<std::string> subdescs; virtual size_t size() const = 0; virtual const VCounter &value() const = 0; virtual const VResult &result() const = 0; virtual Result total() const = 0; void update() { if (!subnames.empty()) { int s = size(); if (subnames.size() < s) subnames.resize(s); if (subdescs.size() < s) subdescs.resize(s); } }};template <class Stat>class VectorStatData : public VectorData{ protected: Stat &s; mutable VCounter cvec; mutable VResult rvec; public: VectorStatData(Stat &stat) : s(stat) {} virtual bool check() const { return s.check(); } virtual bool zero() const { return s.zero(); } virtual void reset() { s.reset(); } virtual size_t size() const { return s.size(); } virtual VCounter &value() const { s.value(cvec); return cvec; } virtual const VResult &result() const { s.result(rvec); return rvec; } virtual Result total() const { return s.total(); } virtual void visit(Visit &visitor) { update(); s.update(this); visitor.visit(*this); }};struct DistDataData{ Counter min_val; Counter max_val; Counter underflow; Counter overflow; VCounter cvec; Counter sum; Counter squares; Counter samples; Counter min; Counter max; Counter bucket_size; int size; bool fancy;};struct DistData : public StatData{ /** Local storage for the entry values, used for printing. */ DistDataData data;};template <class Stat>class DistStatData : public DistData{ protected: Stat &s; public: DistStatData(Stat &stat) : s(stat) {} virtual bool check() const { return s.check(); } virtual void reset() { s.reset(); } virtual bool zero() const { return s.zero(); } virtual void visit(Visit &visitor) { s.update(this); visitor.visit(*this); }};struct VectorDistData : public StatData{ std::vector<DistDataData> data; /** Names and descriptions of subfields. */ mutable std::vector<std::string> subnames; mutable std::vector<std::string> subdescs; /** Local storage for the entry values, used for printing. */ mutable VResult rvec; virtual size_t size() const = 0; void update() { int s = size(); if (subnames.size() < s) subnames.resize(s); if (subdescs.size() < s) subdescs.resize(s); }};template <class Stat>class VectorDistStatData : public VectorDistData{ protected: Stat &s; public: VectorDistStatData(Stat &stat) : s(stat) {} virtual bool check() const { return s.check(); } virtual void reset() { s.reset(); } virtual size_t size() const { return s.size(); } virtual bool zero() const { return s.zero(); } virtual void visit(Visit &visitor) { update(); s.update(this); visitor.visit(*this); }};struct Vector2dData : public StatData{ /** Names and descriptions of subfields. */ std::vector<std::string> subnames; std::vector<std::string> subdescs; std::vector<std::string> y_subnames; /** Local storage for the entry values, used for printing. */ mutable VCounter cvec; mutable int x; mutable int y; void update() { if (subnames.size() < x) subnames.resize(x); }};template <class Stat>class Vector2dStatData : public Vector2dData{ protected: Stat &s; public: Vector2dStatData(Stat &stat) : s(stat) {} virtual bool check() const { return s.check(); } virtual void reset() { s.reset(); } virtual bool zero() const { return s.zero(); } virtual void visit(Visit &visitor) { update(); s.update(this); visitor.visit(*this); }};class DataAccess{ protected: StatData *find() const; void map(StatData *data); StatData *statData(); const StatData *statData() const; void setInit(); void setPrint();};template <class Parent, class Child, template <class> class Data>class Wrap : public Child{ protected: Parent &self() { return *reinterpret_cast<Parent *>(this); } protected: Data<Child> *statData() { StatData *__data = DataAccess::statData(); Data<Child> *ptr = dynamic_cast<Data<Child> *>(__data); assert(ptr); return ptr; } public: const Data<Child> *statData() const { const StatData *__data = DataAccess::statData(); const Data<Child> *ptr = dynamic_cast<const Data<Child> *>(__data); assert(ptr); return ptr; } protected: /** * Copy constructor, copies are not allowed. */ Wrap(const Wrap &stat); /** * Can't copy stats. */ void operator=(const Wrap &); public: Wrap() { this->map(new Data<Child>(*this)); } /** * Set the name and marks this stat to print at the end of simulation. * @param name The new name. * @return A reference to this stat. */ Parent &name(const std::string &_name) { Data<Child> *data = this->statData(); data->name = _name; this->setPrint(); return this->self(); } /** * Set the description and marks this stat to print at the end of * simulation. * @param desc The new description. * @return A reference to this stat. */ Parent &desc(const std::string &_desc) { this->statData()->desc = _desc; return this->self(); } /** * Set the precision and marks this stat to print at the end of simulation. * @param p The new precision * @return A reference to this stat. */ Parent &precision(int _precision) { this->statData()->precision = _precision; return this->self(); } /** * Set the flags and marks this stat to print at the end of simulation. * @param f The new flags. * @return A reference to this stat. */ Parent &flags(StatFlags _flags) { this->statData()->flags |= _flags; return this->self(); } /** * Set the prerequisite stat and marks this stat to print at the end of * simulation. * @param prereq The prerequisite stat. * @return A reference to this stat. */ template <class Stat> Parent &prereq(const Stat &prereq) { this->statData()->prereq = prereq.statData(); return this->self(); }};template <class Parent, class Child, template <class Child> class Data>class WrapVec : public Wrap<Parent, Child, Data>{ public: // The following functions are specific to vectors. If you use them // in a non vector context, you will get a nice compiler error! /** * Set the subfield name for the given index, and marks this stat to print * at the end of simulation. * @param index The subfield index. * @param name The new name of the subfield. * @return A reference to this stat. */ Parent &subname(int index, const std::string &name) { std::vector<std::string> &subn = this->statData()->subnames; if (subn.size() <= index) subn.resize(index + 1); subn[index] = name; return this->self(); } /** * Set the subfield description for the given index and marks this stat to * print at the end of simulation. * @param index The subfield index. * @param desc The new description of the subfield * @return A reference to this stat. */ Parent &subdesc(int index, const std::string &desc) { std::vector<std::string> &subd = this->statData()->subdescs; if (subd.size() <= index) subd.resize(index + 1); subd[index] = desc; return this->self(); }};template <class Parent, class Child, template <class Child> class Data>class WrapVec2d : public WrapVec<Parent, Child, Data>{ public: /** * @warning This makes the assumption that if you're gonna subnames a 2d * vector, you're subnaming across all y */ Parent &ysubnames(const char **names) { Data<Child> *data = this->statData(); data->y_subnames.resize(this->y); for (int i = 0; i < this->y; ++i) data->y_subnames[i] = names[i]; return this->self(); } Parent &ysubname(int index, const std::string subname) { Data<Child> *data = this->statData(); assert(index < this->y); data->y_subnames.resize(this->y); data->y_subnames[index] = subname.c_str(); return this->self(); }};////////////////////////////////////////////////////////////////////////// Simple Statistics/////////////////////////////////////////////////////////////////////////** * Templatized storage and interface for a simple scalar stat. */struct StatStor{ public: /** The paramaters for this storage type, none for a scalar. */ struct Params { }; private: /** The statistic value. */ Counter data; public: /** * Builds this storage element and calls the base constructor of the * datatype.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?