X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=base%2Fstatistics.hh;h=c46744cac6b4a9b74528748f3e3d94ddf351ca3a;hb=5aa71721193c49016ffa69934b44ce38672e4eed;hp=aa348972786d44c0e51b4b745c675be028c641a0;hpb=921b1ee87b6927e7310c747e03efd223d8c65642;p=gem5.git diff --git a/base/statistics.hh b/base/statistics.hh index aa3489727..c46744cac 100644 --- a/base/statistics.hh +++ b/base/statistics.hh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003 The Regents of The University of Michigan + * Copyright (c) 2003-2005 The Regents of The University of Michigan * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -42,238 +42,443 @@ * VectorStandardDeviation totals * Document Namespaces */ -#ifndef __STATISTICS_HH__ -#define __STATISTICS_HH__ +#ifndef __BASE_STATISTICS_HH__ +#define __BASE_STATISTICS_HH__ #include +#include +#include #include #include #include #include #include -#include - +#include "base/cprintf.hh" +#include "base/intmath.hh" #include "base/refcnt.hh" #include "base/str.hh" -#include "base/intmath.hh" -#include +#include "base/stats/bin.hh" +#include "base/stats/flags.hh" +#include "base/stats/visit.hh" +#include "base/stats/types.hh" +#include "config/stats_binning.hh" #include "sim/host.hh" -#ifdef FS_MEASURE -#include "base/trace.hh" -#endif -// -// Un-comment this to enable weirdo-stat debugging -// -// #define STAT_DEBUG - - -#ifndef NAN -float __nan(); -/** Define Not a number. */ -#define NAN (__nan()) -/** Need to define __nan() */ -#define __M5_NAN -#endif - -/** Print stats out in SS format. */ -#define STAT_DISPLAY_COMPAT - class Callback; /** The current simulated cycle. */ extern Tick curTick; /* A namespace for all of the Statistics */ -namespace Statistics { -/** All results are doubles. */ -typedef double result_t; -/** A vector to hold results. */ -typedef std::vector rvec_t; - -/** - * Define the storage for format flags. - * @todo Can probably shrink this. - */ -typedef u_int32_t FormatFlags; -/** Nothing extra to print. */ -const FormatFlags none = 0x0000; -/** Print the total. */ -const FormatFlags total = 0x0001; -/** Print the percent of the total that this entry represents. */ -const FormatFlags pdf = 0x0002; -/** Don't print if this is zero. */ -const FormatFlags nozero = 0x0004; -/** Don't print if this is NAN */ -const FormatFlags nonan = 0x0008; -/** Print the cumulative percentage of total upto this entry. */ -const FormatFlags cdf = 0x0010; -/** Print the distribution. */ -const FormatFlags dist = 0x0020; -/** Used for SS compatability. */ -const FormatFlags __substat = 0x8000; -/** Mask of flags that can't be set directly */ -const FormatFlags __reserved = __substat; +namespace Stats { /* Contains the statistic implementation details */ -namespace Detail { ////////////////////////////////////////////////////////////////////// // // Statistics Framework Base classes // ////////////////////////////////////////////////////////////////////// -struct StatData; -struct SubData; - -/** - * Common base class for all statistics, used to maintain a list and print. - * This class holds no data itself but is used to find the associated - * StatData in the stat database @sa Statistics::Database. - */ -class Stat +struct StatData { - protected: - /** Mark this statistics as initialized. */ - void setInit(); - /** - * Finds and returns the associated StatData from the database. - * @return The formatting and output data of this statistic. - */ - StatData *mydata(); - /** - * Finds and returns a const pointer to the associated StatData. - * @return The formatting and output data of this statistic. - */ - const StatData *mydata() const; - /** - * Mark this stat for output at the end of simulation. - * @return The formatting and output data of this statistic. - */ - StatData *print(); - /** - * Finds and returns the SubData at the given index. - * @param index The index of the SubData to find. - * @return The name and description of the given index. - */ - const SubData *mysubdata(int index) const; + /** 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; /** - * Create and return a new SubData field for the given index. - * @param index The index to create a SubData for. - * @return A pointer to the created SubData. + * A unique stat ID for each stat in the simulator. + * Can be used externally for lookups as well as for debugging. */ - SubData *mysubdata_create(int index); + int id; + + StatData(); + virtual ~StatData(); - public: /** - * Return the name of this stat. - * @return the name of the stat. + * @return true if the stat is binned. */ - virtual std::string myname() const; + virtual bool binned() const = 0; + /** - * Return the name of the sub field at the given index. - * @param index the subfield index. - * @return the name of the subfield. + * Reset the corresponding stat to the default state. */ - virtual std::string mysubname(int index) const; + virtual void reset() = 0; + /** - * Return the description of this stat. - * @return the description of this stat. + * @return true if this stat has a value and satisfies its + * requirement as a prereq */ - virtual std::string mydesc() const; - /** - * Return the description of the subfield at the given index. - * @param index The subfield index. - * @return the description of the subfield. - */ - virtual std::string mysubdesc(int index) const; + virtual bool zero() const = 0; + /** - * Return the format flags of this stat. - * @return the format flags. + * Check that this stat has been set up properly and is ready for + * use + * @return true for success */ - virtual FormatFlags myflags() const; + virtual bool check() const = 0; + bool baseCheck() const; + /** - * Return true if this stat's prereqs have been satisfied (they are non - * zero). - * @return true if the prerequisite stats aren't zero. + * Visitor entry for outputing statistics data */ - virtual bool dodisplay() const; + virtual void visit(Visit &visitor) = 0; + /** - * Return the display percision. - * @return The display precision. + * 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 */ - virtual int myprecision() const; + static bool less(StatData *stat1, StatData *stat2); +}; +class ScalarData : public StatData +{ public: - /** - * Create this stat and perhaps register it with the stat database. To be - * printed a stat must be registered with the database. - * @param reg If true, register this stat in the database. - */ - Stat(bool reg); - /** - * Destructor - */ - virtual ~Stat() {} + 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 ScalarStatData : public ScalarData +{ + protected: + Stat &s; + + public: + ScalarStatData(Stat &stat) : s(stat) {} + + virtual bool binned() const { return s.binned(); } + 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 subnames; + mutable std::vector 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 VectorStatData : public VectorData +{ + protected: + Stat &s; + mutable VCounter cvec; + mutable VResult rvec; + + public: + VectorStatData(Stat &stat) : s(stat) {} + + virtual bool binned() const { return s.binned(); } + 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 DistStatData : public DistData +{ + protected: + Stat &s; + + public: + DistStatData(Stat &stat) : s(stat) {} + + virtual bool binned() const { return s.binned(); } + 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 data; + + /** Names and descriptions of subfields. */ + mutable std::vector subnames; + mutable std::vector subdescs; + + /** Local storage for the entry values, used for printing. */ + mutable VResult rvec; - /** - * Print this stat to the given ostream. - * @param stream The stream to print to. - */ - virtual void display(std::ostream &stream) const = 0; - /** - * Reset this stat to the default state. - */ - virtual void reset() = 0; - /** - * Return the number of entries in this stat. - * @return The number of entries. - */ 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 VectorDistStatData : public VectorDistData +{ + protected: + Stat &s; + typedef typename Stat::bin_t bin_t; + + public: + VectorDistStatData(Stat &stat) : s(stat) {} + + virtual bool binned() const { return bin_t::binned; } + 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 subnames; + std::vector subdescs; + std::vector 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 Vector2dStatData : public Vector2dData +{ + protected: + Stat &s; + typedef typename Stat::bin_t bin_t; + + public: + Vector2dStatData(Stat &stat) : s(stat) {} + + virtual bool binned() const { return bin_t::binned; } + 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 Data> +class Wrap : public Child +{ + protected: + Parent &self() { return *reinterpret_cast(this); } + + protected: + Data *statData() + { + StatData *__data = DataAccess::statData(); + Data *ptr = dynamic_cast *>(__data); + assert(ptr); + return ptr; + } + + public: + const Data *statData() const + { + const StatData *__data = DataAccess::statData(); + const Data *ptr = dynamic_cast *>(__data); + assert(ptr); + return ptr; + } + + protected: /** - * Return true if the stat has value zero. - * @return True if the stat is zero. + * Copy constructor, copies are not allowed. */ - virtual bool zero() const = 0; - + Wrap(const Wrap &stat); /** - * Return true if stat is binned. - *@return True is stat is binned. + * Can't copy stats. */ - virtual bool binned() const = 0; + void operator=(const Wrap &); + + public: + Wrap() + { + map(new Data(*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. */ - Stat &name(const std::string &name); + Parent &name(const std::string &_name) + { + Data *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. */ - Stat &desc(const std::string &desc); + 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. */ - Stat &precision(int p); + 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. */ - Stat &flags(FormatFlags f); + 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. */ - Stat &prereq(const Stat &prereq); + template + Parent &prereq(const Stat &prereq) + { + this->statData()->prereq = prereq.statData(); + return this->self(); + } +}; + +template class Data> +class WrapVec : public Wrap +{ + 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. @@ -281,7 +486,15 @@ class Stat * @param name The new name of the subfield. * @return A reference to this stat. */ - Stat &subname(int index, const std::string &name); + Parent &subname(int index, const std::string &name) + { + std::vector &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. @@ -289,107 +502,42 @@ class Stat * @param desc The new description of the subfield * @return A reference to this stat. */ - Stat &subdesc(int index, const std::string &desc); - - public: - /** - * 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(Stat *stat1, Stat *stat2); - -#ifdef STAT_DEBUG - /** A unique ID used for debugging. */ - int number; -#endif -}; + Parent &subdesc(int index, const std::string &desc) + { + std::vector &subd = this->statData()->subdescs; + if (subd.size() <= index) + subd.resize(index + 1); + subd[index] = desc; -/** - * Base class for all scalar stats. The class provides an interface to access - * the current value of the stat. This class can be used in formulas. - */ -class ScalarStat : public Stat -{ - public: - /** - * Create and perhaps register this stat with the database. - * @param reg If true, register this stat with the database. - */ - ScalarStat(bool reg) : Stat(reg) {} - /** - * Return the current value of this statistic as a result type. - * @return The current value of this statistic. - */ - virtual result_t val() const = 0; - /** - * Return true if this stat has value zero. - * @return True if this stat is zero. - */ - virtual bool zero() const; - /** - * Print this stat to the provided ostream. - * @param stream The output stream. - */ - virtual void display(std::ostream &stream) const; + return this->self(); + } - /** - * Return true if stat is binned. - *@return True is stat is binned. - */ - virtual bool binned() const = 0; }; -void -VectorDisplay(std::ostream &stream, const std::string &myname, - const std::vector *mysubnames, - const std::string &mydesc, - const std::vector *mysubdescs, - int myprecision, FormatFlags myflags, const rvec_t &vec, - result_t mytotal); - -/** - * Base class for all vector stats. This class provides interfaces to access - * the current values of the stats as well as the totals. This class can be - * used in formulas. - */ -class VectorStat : public Stat +template class Data> +class WrapVec2d : public WrapVec { public: /** - * Create and perhaps register this stat with the database. - * @param reg If true, register this stat with the database. - */ - VectorStat(bool reg) : Stat(reg) {} - /** - * Return a vector of result typesd of all the values in the vector. - * @return The values of the vector. - */ - virtual const rvec_t &val() const = 0; - /** - * Return the total of all the entries in the vector. - * @return The total of the vector. - */ - virtual result_t total() const = 0; - /** - * Return true if this stat has value zero. - * @return True if this stat is zero. - */ - virtual bool zero() const; - /** - * Print this stat to the provided ostream. - * @param stream The output stream. - */ - virtual void display(std::ostream &stream) const; - - /** - * Return true if stat is binned. - *@return True is stat is binned. + * @warning This makes the assumption that if you're gonna subnames a 2d + * vector, you're subnaming across all y */ - virtual bool binned() const = 0; + Parent &ysubnames(const char **names) + { + Data *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 *data = this->statData(); + assert(index < this->y); + data->y_subnames.resize(this->y); + data->y_subnames[index] = subname.c_str(); + return this->self(); + } }; ////////////////////////////////////////////////////////////////////// @@ -401,7 +549,6 @@ class VectorStat : public Stat /** * Templatized storage and interface for a simple scalar stat. */ -template struct StatStor { public: @@ -410,49 +557,54 @@ struct StatStor private: /** The statistic value. */ - T data; + Counter data; public: /** * Builds this storage element and calls the base constructor of the * datatype. */ - StatStor(const Params &) : data(T()) {} + 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(T val, const Params &p) { data = val; } + 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(T val, const Params &p) { data += val; } + 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(T val, const Params &p) { data -= val; } + void dec(Counter val, const Params &p) { data -= val; } /** - * Return the value of this stat as a result type. - * @param p The parameters of this storage type. + * Return the value of this stat as its base type. + * @param p The params of this storage type. * @return The value of this stat. */ - result_t val(const Params &p) const { return (result_t)data; } + Counter value(const Params &p) const { return data; } /** - * Return the value of this stat as its base type. - * @param p The params of this storage type. + * Return the value of this stat as a result type. + * @param p The parameters of this storage type. * @return The value of this stat. */ - T value(const Params &p) const { return data; } + Result result(const Params &p) const { return (Result)data; } /** * Reset stat value to default */ - void reset() { data = T(); } + void reset() { data = Counter(); } + + /** + * @return true if zero value + */ + bool zero() const { return data == Counter(); } }; /** @@ -463,18 +615,22 @@ struct StatStor * among other things. * @todo add lateny to the stat and fix binning. */ -template struct AvgStor { public: - /** The paramaters for this storage type, none for this average. */ - struct Params { }; + /** The paramaters for this storage type */ + struct Params + { + /** + * The current count. We stash this here because the current + * value is not a binned value. + */ + Counter current; + }; private: - /** The current count. */ - T current; /** The total count for all cycles. */ - mutable result_t total; + mutable Result total; /** The cycle that current last changed. */ mutable Tick last; @@ -482,7 +638,7 @@ struct AvgStor /** * Build and initializes this stat storage. */ - AvgStor(const Params &) : current(T()), total(0), last(0) { } + AvgStor(Params &p) : total(0), last(0) { p.current = Counter(); } /** * Set the current count to the one provided, update the total and last @@ -490,48 +646,58 @@ struct AvgStor * @param val The new count. * @param p The parameters for this storage. */ - void set(T val, const Params &p) { - total += current * (curTick - last); + void set(Counter val, Params &p) { + total += p.current * (curTick - last); last = curTick; - current = val; + p.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(T val, const Params &p) { set(current + val, p); } + void inc(Counter val, Params &p) { set(p.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(p.current - val, p); } + /** - * Deccrement the current count by the provided value, calls set. - * @param val The amount to decrement. + * Return the current count. * @param p The parameters for this storage. + * @return The current count. */ - void dec(T val, const Params &p) { set(current - val, p); } + Counter value(const Params &p) const { return p.current; } + /** * Return the current average. * @param p The parameters for this storage. * @return The current average. */ - result_t val(const Params &p) const { - total += current * (curTick - last); + Result result(const Params &p) const + { + total += p.current * (curTick - last); last = curTick; - return (result_t)(total + current) / (result_t)(curTick + 1); + return (Result)(total + p.current) / (Result)(curTick + 1); } - /** - * Return the current count. - * @param p The parameters for this storage. - * @return The current count. - */ - T value(const Params &p) const { return current; } + /** * Reset stat value to default */ void reset() { - current = T(); total = 0; last = curTick; } + + /** + * @return true if zero value + */ + bool zero() const { return total == 0.0; } }; /** @@ -539,16 +705,14 @@ struct AvgStor * Storage template. The storage for this stat is held within the Bin class. * This allows for breaking down statistics across multiple bins easily. */ -template class Storage, class Bin> -class ScalarBase : public ScalarStat +template +class ScalarBase : public DataAccess { - protected: - /** Define the type of the storage class. */ - typedef Storage storage_t; + public: /** Define the params of the storage class. */ - typedef typename storage_t::Params params_t; + typedef typename Storage::Params params_t; /** Define the bin type. */ - typedef typename Bin::Bin bin_t; + typedef typename Bin::template Bin bin_t; protected: /** The bin of this stat. */ @@ -561,44 +725,32 @@ class ScalarBase : public ScalarStat * Retrieve the storage from the bin. * @return The storage object for this stat. */ - storage_t *data() { return bin.data(params); } + Storage *data() { return bin.data(params); } /** * Retrieve a const pointer to the storage from the bin. * @return A const pointer to the storage object for this stat. */ - const storage_t *data() const { - return (const_cast(&bin))->data(params); + const Storage *data() const + { + bin_t *_bin = const_cast(&bin); + params_t *_params = const_cast(¶ms); + return _bin->data(*_params); } - protected: - /** - * Copy constructor, copies are not allowed. - */ - ScalarBase(const ScalarBase &stat); - /** - * Can't copy stats. - */ - const ScalarBase &operator=(const ScalarBase &); - public: - /** - * Return the current value of this stat as a result type. - * @return The current value. - */ - result_t val() const { return data()->val(params); } /** * Return the current value of this stat as its base type. * @return The current value. */ - T value() const { return data()->value(params); } + Counter value() const { return data()->value(params); } public: /** * Create and initialize this stat, register it with the database. */ - ScalarBase() : ScalarStat(true) { + ScalarBase() + { bin.init(params); - setInit(); } public: @@ -625,7 +777,7 @@ class ScalarBase : public ScalarStat * @param v The new value. */ template - void operator=(const U& v) { data()->set(v, params); } + void operator=(const U &v) { data()->set(v, params); } /** * Increment the stat by the given value. This calls the associated @@ -633,7 +785,7 @@ class ScalarBase : public ScalarStat * @param v The value to add. */ template - void operator+=(const U& v) { data()->inc(v, params); } + void operator+=(const U &v) { data()->inc(v, params); } /** * Decrement the stat by the given value. This calls the associated @@ -641,23 +793,107 @@ class ScalarBase : public ScalarStat * @param v The value to substract. */ template - void operator-=(const U& v) { data()->dec(v, params); } + void operator-=(const U &v) { data()->dec(v, params); } /** * Return the number of elements, always 1 for a scalar. * @return 1. */ - virtual size_t size() const { return 1; } + size_t size() const { return 1; } /** * Return true if stat is binned. *@return True is stat is binned. */ - virtual bool binned() const { return bin_t::binned; } + bool binned() const { return bin_t::binned; } + + bool check() const { return bin.initialized(); } /** * Reset stat value to default */ void reset() { bin.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 bool binned() const { return false; } + 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 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 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 + void scalar(T &value) + { + proxy = new ValueProxy(value); + setInit(); + } + + template + void functor(T &func) + { + proxy = new FunctorProxy(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(); } + + bool binned() const { return proxy->binned(); } + std::string str() const { return proxy->str(); } + bool zero() const { return proxy->zero(); } + bool check() const { return proxy != NULL; } + void reset() { } }; ////////////////////////////////////////////////////////////////////// @@ -665,27 +901,21 @@ class ScalarBase : public ScalarStat // Vector Statistics // ////////////////////////////////////////////////////////////////////// -template class Storage, class Bin> +template class ScalarProxy; /** * Implementation of a vector of stats. The type of stat is determined by the * Storage class. @sa ScalarBase */ -template class Storage, class Bin> -class VectorBase : public VectorStat +template +class VectorBase : public DataAccess { - protected: - /** Define the type of the storage class. */ - typedef Storage storage_t; + public: /** Define the params of the storage class. */ - typedef typename storage_t::Params params_t; + typedef typename Storage::Params params_t; /** Define the bin type. */ - typedef typename Bin::VectorBin bin_t; - - private: - /** Local storage for the entry values, used for printing. */ - mutable rvec_t *vec; + typedef typename Bin::template VectorBin bin_t; protected: /** The bin of this stat. */ @@ -699,114 +929,101 @@ class VectorBase : public VectorStat * @param index The vector index to access. * @return The storage object at the given index. */ - storage_t *data(int index) { return bin.data(index, params); } + Storage *data(int index) { return bin.data(index, params); } /** * Retrieve a const pointer to the storage from the bin * 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_t *data(int index) const { - return (const_cast(&bin))->data(index, params); + const Storage *data(int index) const + { + bin_t *_bin = const_cast(&bin); + params_t *_params = const_cast(¶ms); + return _bin->data(index, *_params); } - protected: - // Copying stats is not allowed - /** Copying stats isn't allowed. */ - VectorBase(const VectorBase &stat); - /** Copying stats isn't allowed. */ - const VectorBase &operator=(const VectorBase &); - 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. */ - const rvec_t &val() const { - if (vec) - vec->resize(size()); - else - vec = new rvec_t(size()); - + void result(VResult &vec) const + { + vec.resize(size()); for (int i = 0; i < size(); ++i) - (*vec)[i] = data(i)->val(params); - - return *vec; + vec[i] = data(i)->result(params); } + /** + * @return True is stat is binned. + */ + bool binned() const { return bin_t::binned; } + /** * Return a total of all entries in this vector. * @return The total of all vector entries. */ - result_t total() const { - result_t total = 0.0; + Result total() const { + Result total = 0.0; for (int i = 0; i < size(); ++i) - total += data(i)->val(params); + total += data(i)->result(params); return total; } - public: - /** - * Create this vector and register it with the database. - */ - VectorBase() : VectorStat(true), vec(NULL) {} - /** - * Destructor. - */ - ~VectorBase() { if (vec) delete vec; } - /** - * Set this vector to have the given size. - * @param size The new size. - * @return A reference to this stat. + * @return the number of elements in this vector. */ - VectorBase &init(size_t size) { - bin.init(size, params); - setInit(); + size_t size() const { return bin.size(); } - return *this; + bool zero() const + { + for (int i = 0; i < size(); ++i) + if (data(i)->zero()) + return true; + return false; } + bool check() const { return bin.initialized(); } + void reset() { bin.reset(); } + + public: + VectorBase() {} + /** Friend this class with the associated scalar proxy. */ - friend class ScalarProxy; + friend class ScalarProxy; /** * Return a reference (ScalarProxy) to the stat at the given index. * @param index The vector index to access. * @return A reference of the stat. */ - ScalarProxy operator[](int index); + ScalarProxy operator[](int index); - /** - * Return the number of elements in this vector. - * @return The size of the vector. - */ - virtual size_t size() const { return bin.size(); } - /** - * Return true if stat is binned. - *@return True is stat is binned. - */ - virtual bool binned() const { return bin_t::binned; } - /** - * Reset stat value to default - */ - virtual void reset() { bin.reset(); } + void update(StatData *data) {} }; +const StatData * getStatData(const void *stat); + /** * A proxy class to access the stat at a given index in a VectorBase stat. * Behaves like a ScalarBase. */ -template class Storage, class Bin> -class ScalarProxy : public ScalarStat +template +class ScalarProxy { - protected: - /** Define the type of the storage class. */ - typedef Storage storage_t; + public: /** Define the params of the storage class. */ - typedef typename storage_t::Params params_t; + typedef typename Storage::Params params_t; /** Define the bin type. */ - typedef typename Bin::VectorBin bin_t; + typedef typename Bin::template VectorBin bin_t; private: /** Pointer to the bin in the parent VectorBase. */ @@ -815,30 +1032,38 @@ class ScalarProxy : public ScalarStat params_t *params; /** The index to access in the parent VectorBase. */ int index; + /** Keep a pointer to the original stat so was can get data */ + void *stat; protected: /** * Retrieve the storage from the bin. * @return The storage from the bin for this stat. */ - storage_t *data() { return bin->data(index, *params); } + Storage *data() { return bin->data(index, *params); } /** * Retrieve a const pointer to the storage from the bin. * @return A const pointer to the storage for this stat. */ - const storage_t *data() const { return bin->data(index, *params); } + const Storage *data() const + { + bin_t *_bin = const_cast(bin); + params_t *_params = const_cast(params); + return _bin->data(index, *_params); + } public: /** - * Return the current value of this statas a result type. + * Return the current value of this stat as its base type. * @return The current value. */ - result_t val() const { return data()->val(*params); } + Counter value() const { return data()->value(*params); } + /** - * Return the current value of this stat as its base type. + * Return the current value of this statas a result type. * @return The current value. */ - T value() const { return data()->value(*params); } + Result result() const { return data()->result(*params); } public: /** @@ -847,14 +1072,14 @@ class ScalarProxy : public ScalarStat * @param p The params to use. * @param i The index to access. */ - ScalarProxy(bin_t &b, params_t &p, int i) - : ScalarStat(false), bin(&b), params(&p), index(i) {} + ScalarProxy(bin_t &b, params_t &p, int i, void *s) + : bin(&b), params(&p), index(i), stat(s) {} /** * Create a copy of the provided ScalarProxy. * @param sp The proxy to copy. */ ScalarProxy(const ScalarProxy &sp) - : ScalarStat(false), bin(sp.bin), params(sp.params), index(sp.index) {} + : bin(sp.bin), params(sp.params), index(sp.index), stat(sp.stat) {} /** * Set this proxy equal to the provided one. * @param sp The proxy to copy. @@ -864,6 +1089,7 @@ class ScalarProxy : public ScalarStat bin = sp.bin; params = sp.params; index = sp.index; + stat = sp.stat; return *this; } @@ -891,7 +1117,7 @@ class ScalarProxy : public ScalarStat * @param v The new value. */ template - void operator=(const U& v) { data()->set(v, *params); } + void operator=(const U &v) { data()->set(v, *params); } /** * Increment the stat by the given value. This calls the associated @@ -899,7 +1125,7 @@ class ScalarProxy : public ScalarStat * @param v The value to add. */ template - void operator+=(const U& v) { data()->inc(v, *params); } + void operator+=(const U &v) { data()->inc(v, *params); } /** * Decrement the stat by the given value. This calls the associated @@ -907,253 +1133,200 @@ class ScalarProxy : public ScalarStat * @param v The value to substract. */ template - void operator-=(const U& v) { data()->dec(v, *params); } + void operator-=(const U &v) { data()->dec(v, *params); } /** * Return the number of elements, always 1 for a scalar. * @return 1. */ - virtual size_t size() const { return 1; } + size_t size() const { return 1; } + /** * Return true if stat is binned. *@return false since Proxies aren't printed/binned */ - virtual bool binned() const { return false; } + bool binned() const { return false; } + /** * This stat has no state. Nothing to reset */ - virtual void reset() { } + void reset() { } + + public: + const StatData *statData() const { return getStatData(stat); } + std::string str() const + { + return csprintf("%s[%d]", this->statData()->name, index); + + } }; -template class Storage, class Bin> -inline ScalarProxy -VectorBase::operator[](int index) +template +inline ScalarProxy +VectorBase::operator[](int index) { assert (index >= 0 && index < size()); - return ScalarProxy(bin, params, index); + return ScalarProxy(bin, params, index, this); } -template class Storage, class Bin> +template class VectorProxy; -template class Storage, class Bin> -class Vector2dBase : public Stat +template +class Vector2dBase : public DataAccess { - protected: - typedef Storage storage_t; - typedef typename storage_t::Params params_t; - typedef typename Bin::VectorBin bin_t; + public: + typedef typename Storage::Params params_t; + typedef typename Bin::template VectorBin bin_t; protected: size_t x; size_t y; bin_t bin; params_t params; - std::vector *y_subnames; protected: - storage_t *data(int index) { return bin.data(index, params); } - const storage_t *data(int index) const { - return (const_cast(&bin))->data(index, params); + Storage *data(int index) { return bin.data(index, params); } + const Storage *data(int index) const + { + bin_t *_bin = const_cast(&bin); + params_t *_params = const_cast(¶ms); + return _bin->data(index, *_params); } - protected: - // Copying stats is not allowed - Vector2dBase(const Vector2dBase &stat); - const Vector2dBase &operator=(const Vector2dBase &); - public: - Vector2dBase() : Stat(true) {} - ~Vector2dBase() { } - - Vector2dBase &init(size_t _x, size_t _y) { - x = _x; - y = _y; - bin.init(x * y, params); - setInit(); - y_subnames = new std::vector(y); - - return *this; - } + Vector2dBase() {} - /** - * @warning This makes the assumption that if you're gonna subnames a 2d - * vector, you're subnaming across all y - */ - Vector2dBase &ysubnames(const char **names) - { - for (int i=0; i < y; ++i) { - (*y_subnames)[i] = names[i]; - } - return *this; - } - Vector2dBase &ysubname(int index, const std::string subname) + void update(Vector2dData *data) { - (*y_subnames)[i] = subname.c_str(); - return *this; + int size = this->size(); + data->cvec.resize(size); + for (int i = 0; i < size; ++i) + data->cvec[i] = this->data(i)->value(params); } - std::string ysubname(int i) const { return (*y_subnames)[i]; } - - friend class VectorProxy; - VectorProxy operator[](int index); - - virtual size_t size() const { return bin.size(); } - virtual bool zero() const { return data(0)->value(params) == 0.0; } - /** - * Return true if stat is binned. - *@return True is stat is binned. - */ - virtual bool binned() const { return bin_t::binned; } - virtual void - display(std::ostream &out) const - { - bool have_subname = false; - for (int i = 0; i < x; ++i) { - if (!mysubname(i).empty()) - have_subname = true; - } + std::string ysubname(int i) const { return (*this->y_subnames)[i]; } - rvec_t tot_vec(y); - result_t super_total = 0.0; - for (int i = 0; i < x; ++i) { - std::string subname; - if (have_subname) { - subname = mysubname(i); - if (subname.empty()) - continue; - } else - subname = to_string(i); - - int iy = i * y; - rvec_t vec(y); - - result_t total = 0.0; - for (int j = 0; j < y; ++j) { - vec[j] = data(iy + j)->val(params); - tot_vec[j] += vec[j]; - total += vec[j]; - super_total += vec[j]; - } - - std::string desc; - if (mysubdesc(i).empty()) { - desc = mydesc(); - } else { - desc = mysubdesc(i); - } - - VectorDisplay(out, myname() + "_" + subname, y_subnames, desc, 0, - myprecision(), myflags(), vec, total); + friend class VectorProxy; + VectorProxy operator[](int index); - } - if ((myflags() & ::Statistics::total) && (x > 1)) { - VectorDisplay(out, myname(), y_subnames, mydesc(), 0, - myprecision(), myflags(), tot_vec, super_total); + size_t size() const { return bin.size(); } + bool zero() const { return data(0)->value(params) == 0.0; } - } - } /** * Reset stat value to default */ - virtual void reset() { bin.reset(); } + void reset() { bin.reset(); } + + bool check() { return bin.initialized(); } }; -template class Storage, class Bin> -class VectorProxy : public VectorStat +template +class VectorProxy { - protected: - typedef Storage storage_t; - typedef typename storage_t::Params params_t; - typedef typename Bin::VectorBin bin_t; + public: + typedef typename Storage::Params params_t; + typedef typename Bin::template VectorBin bin_t; private: bin_t *bin; params_t *params; int offset; int len; + void *stat; private: - mutable rvec_t *vec; + mutable VResult *vec; - storage_t *data(int index) { + Storage *data(int index) { assert(index < len); return bin->data(offset + index, *params); } - const storage_t *data(int index) const { - return (const_cast(bin))->data(offset + index, *params); + const Storage *data(int index) const { + bin_t *_bin = const_cast(bin); + params_t *_params = const_cast(params); + return _bin->data(offset + index, *_params); } public: - const rvec_t &val() const { + const VResult &result() const { if (vec) vec->resize(size()); else - vec = new rvec_t(size()); + vec = new VResult(size()); for (int i = 0; i < size(); ++i) - (*vec)[i] = data(i)->val(*params); + (*vec)[i] = data(i)->result(*params); return *vec; } - result_t total() const { - result_t total = 0.0; + Result total() const { + Result total = 0.0; for (int i = 0; i < size(); ++i) - total += data(i)->val(*params); + total += data(i)->result(*params); return total; } public: - VectorProxy(bin_t &b, params_t &p, int o, int l) - : VectorStat(false), bin(&b), params(&p), offset(o), len(l), vec(NULL) - { } + VectorProxy(bin_t &b, params_t &p, int o, int l, void *s) + : bin(&b), params(&p), offset(o), len(l), stat(s), vec(NULL) + { + } + VectorProxy(const VectorProxy &sp) - : VectorStat(false), bin(sp.bin), params(sp.params), offset(sp.offset), - len(sp.len), vec(NULL) - { } - ~VectorProxy() { + : bin(sp.bin), params(sp.params), offset(sp.offset), len(sp.len), + stat(sp.stat), vec(NULL) + { + } + + ~VectorProxy() + { if (vec) delete vec; } - const VectorProxy &operator=(const VectorProxy &sp) { + const VectorProxy &operator=(const VectorProxy &sp) + { bin = sp.bin; params = sp.params; offset = sp.offset; len = sp.len; + stat = sp.stat; if (vec) delete vec; vec = NULL; return *this; } - virtual size_t size() const { return len; } - - ScalarProxy operator[](int index) { + ScalarProxy operator[](int index) + { assert (index >= 0 && index < size()); - return ScalarProxy(*bin, *params, offset + index); + return ScalarProxy(*bin, *params, offset + index, stat); } + + size_t size() const { return len; } + /** * Return true if stat is binned. *@return false since Proxies aren't printed/binned */ - virtual bool binned() const { return false; } + bool binned() const { return false; } /** * This stat has no state. Nothing to reset. */ - virtual void reset() { } + void reset() { } }; -template class Storage, class Bin> -inline VectorProxy -Vector2dBase::operator[](int index) +template +inline VectorProxy +Vector2dBase::operator[](int index) { int offset = index * y; assert (index >= 0 && offset < size()); - return VectorProxy(bin, params, offset, y); + return VectorProxy(bin, params, offset, y, this); } ////////////////////////////////////////////////////////////////////// @@ -1162,16 +1335,9 @@ Vector2dBase::operator[](int index) // ////////////////////////////////////////////////////////////////////// -void DistDisplay(std::ostream &stream, const std::string &name, - const std::string &desc, int precision, FormatFlags flags, - result_t min_val, result_t max_val, - result_t underflow, result_t overflow, - const rvec_t &vec, int min, int max, int bucket_size, - int size); /** * Templatized storage and interface for a distrbution stat. */ -template struct DistStor { public: @@ -1179,26 +1345,33 @@ struct DistStor struct Params { /** The minimum value to track. */ - int min; + Counter min; /** The maximum value to track. */ - int max; + Counter max; /** The number of entries in each bucket. */ - int bucket_size; + Counter bucket_size; /** The number of buckets. Equal to (max-min)/bucket_size. */ int size; }; + enum { fancy = false }; private: /** The smallest value sampled. */ - T min_val; + Counter min_val; /** The largest value sampled. */ - T max_val; + Counter max_val; /** The number of values sampled less than min. */ - T underflow; + Counter underflow; /** The number of values sampled more than max. */ - T overflow; + Counter overflow; + /** The current sum. */ + Counter sum; + /** The sum of squares. */ + Counter squares; + /** The number of samples. */ + Counter samples; /** Counter for each bucket. */ - std::vector vec; + VCounter cvec; public: /** @@ -1206,8 +1379,9 @@ struct DistStor * @param params The parameters. */ DistStor(const Params ¶ms) - : min_val(INT_MAX), max_val(INT_MIN), underflow(0), overflow(0), - vec(params.size) + : min_val(INT_MAX), max_val(INT_MIN), underflow(Counter()), + overflow(Counter()), sum(Counter()), squares(Counter()), + samples(Counter()), cvec(params.size) { reset(); } @@ -1218,15 +1392,16 @@ struct DistStor * @param number The number of times to add the value. * @param params The paramters of the distribution. */ - void sample(T val, int number, const Params ¶ms) { + void sample(Counter val, int number, const Params ¶ms) + { if (val < params.min) underflow += number; else if (val > params.max) overflow += number; else { - int index = (val - params.min) / params.bucket_size; + int index = (int)floor((val - params.min) / params.bucket_size); assert(index < size(params)); - vec[index] += number; + cvec[index] += number; } if (val < min_val) @@ -1234,6 +1409,11 @@ struct DistStor if (val > max_val) max_val = val; + + Counter sample = val * number; + sum += sample; + squares += sample * sample; + samples += number; } /** @@ -1241,55 +1421,38 @@ struct DistStor * @return the number of buckets. * @todo Is it faster to return the size from the parameters? */ - size_t size(const Params &) const { return vec.size(); } + size_t size(const Params &) const { return cvec.size(); } /** * Returns true if any calls to sample have been made. * @param params The paramters of the distribution. * @return True if any values have been sampled. */ - bool zero(const Params ¶ms) const { - if (underflow != 0 || overflow != 0) - return false; - - int s = size(params); - for (int i = 0; i < s; i++) - if (vec[i] != 0) - return false; - - return true; + bool zero(const Params ¶ms) const + { + return samples == Counter(); } - /** - * Print this distribution and the given print data to the given ostream. - * @param stream The output stream. - * @param name The name of this stat (from StatData). - * @param desc The description of this stat (from StatData). - * @param precision The print precision (from StatData). - * @param flags The format flags (from StatData). - * @param params The paramters of this distribution. - */ - void display(std::ostream &stream, const std::string &name, - const std::string &desc, int precision, FormatFlags flags, - const Params ¶ms) const { - -#ifdef STAT_DISPLAY_COMPAT - result_t min = params.min; -#else - result_t min = (min_val == INT_MAX) ? params.min : min_val; -#endif - result_t max = (max_val == INT_MIN) ? 0 : max_val; - - rvec_t rvec(params.size); + void update(DistDataData *data, const Params ¶ms) + { + data->min = params.min; + data->max = params.max; + data->bucket_size = params.bucket_size; + data->size = params.size; + + data->min_val = (min_val == INT_MAX) ? 0 : min_val; + data->max_val = (max_val == INT_MIN) ? 0 : max_val; + data->underflow = underflow; + data->overflow = overflow; + data->cvec.resize(params.size); for (int i = 0; i < params.size; ++i) - rvec[i] = vec[i]; + data->cvec[i] = cvec[i]; - DistDisplay(stream, name, desc, precision, flags, - (result_t)min, (result_t)max, - (result_t)underflow, (result_t)overflow, - rvec, params.min, params.max, params.bucket_size, - params.size); + data->sum = sum; + data->squares = squares; + data->samples = samples; } + /** * Reset stat value to default */ @@ -1300,22 +1463,20 @@ struct DistStor underflow = 0; overflow = 0; - int size = vec.size(); + int size = cvec.size(); for (int i = 0; i < size; ++i) - vec[i] = T(); - } + cvec[i] = Counter(); + sum = Counter(); + squares = Counter(); + samples = Counter(); + } }; -void FancyDisplay(std::ostream &stream, const std::string &name, - const std::string &desc, int precision, FormatFlags flags, - result_t mean, result_t variance); - /** * Templatized storage and interface for a distribution that calculates mean * and variance. */ -template struct FancyStor { public: @@ -1323,20 +1484,23 @@ struct FancyStor * No paramters for this storage. */ struct Params {}; + enum { fancy = true }; private: /** The current sum. */ - T sum; + Counter sum; /** The sum of squares. */ - T squares; - /** The total number of samples. */ - int total; + Counter squares; + /** The number of samples. */ + Counter samples; public: /** * Create and initialize this storage. */ - FancyStor(const Params &) : sum(T()), squares(T()), total(0) {} + FancyStor(const Params &) + : sum(Counter()), squares(Counter()), samples(Counter()) + { } /** * Add a value the given number of times to this running average. @@ -1346,39 +1510,19 @@ struct FancyStor * @param number The number of times to add the value. * @param p The parameters of this stat. */ - void sample(T val, int number, const Params &p) { - T value = val * number; + void sample(Counter val, int number, const Params &p) + { + Counter value = val * number; sum += value; squares += value * value; - total += number; + samples += number; } - /** - * Print this distribution and the given print data to the given ostream. - * @param stream The output stream. - * @param name The name of this stat (from StatData). - * @param desc The description of this stat (from StatData). - * @param precision The print precision (from StatData). - * @param flags The format flags (from StatData). - * @param params The paramters of this distribution. - */ - void display(std::ostream &stream, const std::string &name, - const std::string &desc, int precision, FormatFlags flags, - const Params ¶ms) const { - - result_t mean = NAN; - result_t variance = NAN; - - if (total != 0) { - result_t fsum = sum; - result_t fsq = squares; - result_t ftot = total; - - mean = fsum / ftot; - variance = (ftot * fsq - (fsum * fsum)) / (ftot * (ftot - 1.0)); - } - - FancyDisplay(stream, name, desc, precision, flags, mean, variance); + void update(DistDataData *data, const Params ¶ms) + { + data->sum = sum; + data->squares = squares; + data->samples = samples; } /** @@ -1386,19 +1530,21 @@ struct FancyStor * @return 1. */ size_t size(const Params &) const { return 1; } + /** * Return true if no samples have been added. * @return True if no samples have been added. */ - bool zero(const Params &) const { return total == 0; } + bool zero(const Params &) const { return samples == Counter(); } + /** * Reset stat value to default */ - virtual void reset() + void reset() { - sum = T(); - squares = T(); - total = 0; + sum = Counter(); + squares = Counter(); + samples = Counter(); } }; @@ -1406,24 +1552,24 @@ struct FancyStor * Templatized storage for distribution that calculates per cycle mean and * variance. */ -template struct AvgFancy { public: /** No parameters for this storage. */ struct Params {}; + enum { fancy = true }; private: /** Current total. */ - T sum; + Counter sum; /** Current sum of squares. */ - T squares; + Counter squares; public: /** * Create and initialize this storage. */ - AvgFancy(const Params &) : sum(T()), squares(T()) {} + AvgFancy(const Params &) : sum(Counter()), squares(Counter()) {} /** * Add a value to the distribution for the given number of times. @@ -1432,28 +1578,18 @@ struct AvgFancy * @param number The number of times to add the value. * @param p The paramters of the distribution. */ - void sample(T val, int number, const Params& p) { - T value = val * number; + void sample(Counter val, int number, const Params &p) + { + Counter value = val * number; sum += value; squares += value * value; } - /** - * Print this distribution and the given print data to the given ostream. - * @param stream The output stream. - * @param name The name of this stat (from StatData). - * @param desc The description of this stat (from StatData). - * @param precision The print precision (from StatData). - * @param flags The format flags (from StatData). - * @param params The paramters of this distribution. - */ - void display(std::ostream &stream, const std::string &name, - const std::string &desc, int precision, FormatFlags flags, - const Params ¶ms) const { - result_t mean = sum / curTick; - result_t variance = (squares - sum * sum) / curTick; - - FancyDisplay(stream, name, desc, precision, flags, mean, variance); + void update(DistDataData *data, const Params ¶ms) + { + data->sum = sum; + data->squares = squares; + data->samples = curTick; } /** @@ -1465,14 +1601,14 @@ struct AvgFancy * Return true if no samples have been added. * @return True if the sum is zero. */ - bool zero(const Params ¶ms) const { return sum == 0; } + bool zero(const Params ¶ms) const { return sum == Counter(); } /** * Reset stat value to default */ - virtual void reset() + void reset() { - sum = T(); - squares = T(); + sum = Counter(); + squares = Counter(); } }; @@ -1480,16 +1616,14 @@ struct AvgFancy * Implementation of a distribution stat. The type of distribution is * determined by the Storage template. @sa ScalarBase */ -template class Storage, class Bin> -class DistBase : public Stat +template +class DistBase : public DataAccess { - protected: - /** Define the type of the storage class. */ - typedef Storage storage_t; + public: /** Define the params of the storage class. */ - typedef typename storage_t::Params params_t; + typedef typename Storage::Params params_t; /** Define the bin type. */ - typedef typename Bin::Bin bin_t; + typedef typename Bin::template Bin bin_t; protected: /** The bin of this stat. */ @@ -1502,31 +1636,20 @@ class DistBase : public Stat * Retrieve the storage from the bin. * @return The storage object for this stat. */ - storage_t *data() { return bin.data(params); } + Storage *data() { return bin.data(params); } /** * Retrieve a const pointer to the storage from the bin. * @return A const pointer to the storage object for this stat. */ - const storage_t *data() const { - return (const_cast(&bin))->data(params); + const Storage *data() const + { + bin_t *_bin = const_cast(&bin); + params_t *_params = const_cast(¶ms); + return _bin->data(*_params); } - protected: - // Copying stats is not allowed - /** Copies are not allowed. */ - DistBase(const DistBase &stat); - /** Copies are not allowed. */ - const DistBase &operator=(const DistBase &); - public: - /** - * Create this distrubition and register it with the database. - */ - DistBase() : Stat(true) { } - /** - * Destructor. - */ - ~DistBase() { } + DistBase() { } /** * Add a value to the distribtion n times. Calls sample on the storage @@ -1535,99 +1658,100 @@ class DistBase : public Stat * @param n The number of times to add it, defaults to 1. */ template - void sample(const U& v, int n = 1) { data()->sample(v, n, params); } + void sample(const U &v, int n = 1) { data()->sample(v, n, params); } /** * Return the number of entries in this stat. * @return The number of entries. */ - virtual size_t size() const { return data()->size(params); } + size_t size() const { return data()->size(params); } /** * Return true if no samples have been added. * @return True if there haven't been any samples. */ - virtual bool zero() const { return data()->zero(params); } - /** - * Print this distribution to the given ostream. - * @param stream The output stream. - */ - virtual void display(std::ostream &stream) const { - data()->display(stream, myname(), mydesc(), myprecision(), myflags(), - params); + bool zero() const { return data()->zero(params); } + + void update(DistData *base) + { + base->data.fancy = Storage::fancy; + data()->update(&(base->data), params); } /** - * Return true if stat is binned. - *@return True is stat is binned. + * @return True is stat is binned. */ - virtual bool binned() const { return bin_t::binned; } + bool binned() const { return bin_t::binned; } /** * Reset stat value to default */ - virtual void reset() + void reset() { bin.reset(); } + + bool check() { return bin.initialized(); } }; -template class Storage, class Bin> +template class DistProxy; -template class Storage, class Bin> -class VectorDistBase : public Stat +template +class VectorDistBase : public DataAccess { - protected: - typedef Storage storage_t; - typedef typename storage_t::Params params_t; - typedef typename Bin::VectorBin bin_t; + public: + typedef typename Storage::Params params_t; + typedef typename Bin::template VectorBin bin_t; protected: bin_t bin; params_t params; protected: - storage_t *data(int index) { return bin.data(index, params); } - const storage_t *data(int index) const { - return (const_cast(&bin))->data(index, params); - } - - protected: - // Copying stats is not allowed - VectorDistBase(const VectorDistBase &stat); - const VectorDistBase &operator=(const VectorDistBase &); + Storage *data(int index) { return bin.data(index, params); } + const Storage *data(int index) const + { + bin_t *_bin = const_cast(&bin); + params_t *_params = const_cast(¶ms); + return _bin->data(index, *_params); + } public: - VectorDistBase() : Stat(true) { } - ~VectorDistBase() { } + VectorDistBase() {} - friend class DistProxy; - DistProxy operator[](int index); - const DistProxy operator[](int index) const; + friend class DistProxy; + DistProxy operator[](int index); + const DistProxy operator[](int index) const; - virtual size_t size() const { return bin.size(); } - virtual bool zero() const { return false; } - virtual void display(std::ostream &stream) const; + size_t size() const { return bin.size(); } + bool zero() const { return false; } /** * Return true if stat is binned. *@return True is stat is binned. */ - virtual bool binned() const { return bin_t::binned; } + bool binned() const { return bin_t::binned; } /** * Reset stat value to default */ - virtual void reset() + void reset() { bin.reset(); } + + bool check() { return bin.initialized(); } + void update(VectorDistData *base) { - bin.reset(); + int size = this->size(); + base->data.resize(size); + for (int i = 0; i < size; ++i) { + base->data[i].fancy = Storage::fancy; + data(i)->update(&(base->data[i]), params); + } } }; -template class Storage, class Bin> -class DistProxy : public Stat +template +class DistProxy { - protected: - typedef Storage storage_t; - typedef typename storage_t::Params params_t; - typedef typename Bin::Bin bin_t; - typedef VectorDistBase base_t; + public: + typedef typename Storage::Params params_t; + typedef typename Bin::template Bin bin_t; + typedef VectorDistBase base_t; private: union { @@ -1637,90 +1761,59 @@ class DistProxy : public Stat int index; protected: - storage_t *data() { return stat->data(index); } - const storage_t *data() const { return cstat->data(index); } + Storage *data() { return stat->data(index); } + const Storage *data() const { return cstat->data(index); } public: - DistProxy(const VectorDistBase &s, int i) - : Stat(false), cstat(&s), index(i) {} + DistProxy(const VectorDistBase &s, int i) + : cstat(&s), index(i) {} DistProxy(const DistProxy &sp) - : Stat(false), cstat(sp.cstat), index(sp.index) {} + : cstat(sp.cstat), index(sp.index) {} const DistProxy &operator=(const DistProxy &sp) { cstat = sp.cstat; index = sp.index; return *this; } public: template - void sample(const U& v, int n = 1) { data()->sample(v, n, cstat->params); } + void sample(const U &v, int n = 1) { data()->sample(v, n, cstat->params); } - virtual size_t size() const { return 1; } - virtual bool zero() const { - return data()->zero(cstat->params); - } - virtual void display(std::ostream &stream) const { - std::stringstream name, desc; - - if (!(cstat->mysubname(index).empty())) { - name << cstat->myname() << cstat->mysubname(index); - } else { - name << cstat->myname() << "_" << index; - } - if (!(cstat->mysubdesc(index).empty())) { - desc << cstat->mysubdesc(index); - } else { - desc << cstat->mydesc(); - } - - data()->display(stream, name.str(), desc.str(), - cstat->myprecision(), cstat->myflags(), cstat->params); - } + size_t size() const { return 1; } + bool zero() const { return data()->zero(cstat->params); } /** * Return true if stat is binned. *@return false since Proxies are not binned/printed. */ - virtual bool binned() const { return false; } + bool binned() const { return false; } /** * Proxy has no state. Nothing to reset. */ - virtual void reset() { } + void reset() { } }; -template class Storage, class Bin> -inline DistProxy -VectorDistBase::operator[](int index) +template +inline DistProxy +VectorDistBase::operator[](int index) { assert (index >= 0 && index < size()); - return DistProxy(*this, index); + return DistProxy(*this, index); } -template class Storage, class Bin> -inline const DistProxy -VectorDistBase::operator[](int index) const +template +inline const DistProxy +VectorDistBase::operator[](int index) const { assert (index >= 0 && index < size()); - return DistProxy(*this, index); -} - -/** - * @todo Need a way to print Distribution totals across the Vector - */ -template class Storage, class Bin> -void -VectorDistBase::display(std::ostream &stream) const -{ - for (int i = 0; i < size(); ++i) { - DistProxy proxy(*this, i); - proxy.display(stream); - } + return DistProxy(*this, index); } #if 0 -result_t -VectorDistBase::total(int index) const +template +Result +VectorDistBase::total(int index) const { int total = 0; for (int i=0; i < x_size(); ++i) { - total += data(i)->val(*params); + total += data(i)->result(*params); } } #endif @@ -1747,17 +1840,22 @@ class Node : public RefCounted * Return the result vector of this subtree. * @return The result vector of this subtree. */ - virtual const rvec_t &val() const = 0; + virtual const VResult &result() const = 0; /** * Return the total of the result vector. * @return The total of the result vector. */ - virtual result_t total() const = 0; + virtual Result total() const = 0; /** * Return true if stat is binned. *@return True is stat is binned. */ virtual bool binned() const = 0; + + /** + * + */ + virtual std::string str() const = 0; }; /** Reference counting pointer to a function Node. */ @@ -1766,34 +1864,47 @@ typedef RefCountingPtr NodePtr; class ScalarStatNode : public Node { private: - const ScalarStat &stat; - mutable rvec_t result; + const ScalarData *data; + mutable VResult vresult; public: - ScalarStatNode(const ScalarStat &s) : stat(s), result(1) {} - const rvec_t &val() const { result[0] = stat.val(); return result; } - virtual result_t total() const { return stat.val(); }; + ScalarStatNode(const ScalarData *d) : data(d), vresult(1) {} + virtual const VResult &result() const + { + vresult[0] = data->result(); + return vresult; + } + virtual Result total() const { return data->result(); }; virtual size_t size() const { return 1; } /** * Return true if stat is binned. *@return True is stat is binned. */ - virtual bool binned() const { return stat.binned(); } + virtual bool binned() const { return data->binned(); } + + /** + * + */ + virtual std::string str() const { return data->name; } }; -template class Storage, class Bin> +template class ScalarProxyNode : public Node { private: - const ScalarProxy proxy; - mutable rvec_t result; + const ScalarProxy proxy; + mutable VResult vresult; public: - ScalarProxyNode(const ScalarProxy &p) - : proxy(p), result(1) { } - const rvec_t &val() const { result[0] = proxy.val(); return result; } - virtual result_t total() const { return proxy.val(); }; + ScalarProxyNode(const ScalarProxy &p) + : proxy(p), vresult(1) { } + virtual const VResult &result() const + { + vresult[0] = proxy.result(); + return vresult; + } + virtual Result total() const { return proxy.result(); }; virtual size_t size() const { return 1; } /** @@ -1801,89 +1912,91 @@ class ScalarProxyNode : public Node *@return True is stat is binned. */ virtual bool binned() const { return proxy.binned(); } + + /** + * + */ + virtual std::string str() const { return proxy.str(); } }; class VectorStatNode : public Node { private: - const VectorStat &stat; + const VectorData *data; public: - VectorStatNode(const VectorStat &s) : stat(s) {} - const rvec_t &val() const { return stat.val(); } - virtual result_t total() const { return stat.total(); }; + VectorStatNode(const VectorData *d) : data(d) { } + virtual const VResult &result() const { return data->result(); } + virtual Result total() const { return data->total(); }; - virtual size_t size() const { return stat.size(); } + virtual size_t size() const { return data->size(); } /** * Return true if stat is binned. *@return True is stat is binned. */ - virtual bool binned() const { return stat.binned(); } + virtual bool binned() const { return data->binned(); } + + virtual std::string str() const { return data->name; } }; -template +template class ConstNode : public Node { private: - rvec_t data; + VResult vresult; public: - ConstNode(T s) : data(1, (result_t)s) {} - const rvec_t &val() const { return data; } - virtual result_t total() const { return data[0]; }; - + ConstNode(T s) : vresult(1, (Result)s) {} + const VResult &result() const { return vresult; } + virtual Result total() const { return vresult[0]; }; virtual size_t size() const { return 1; } + /** * Return true if stat is binned. *@return False since constants aren't binned. */ virtual bool binned() const { return false; } + + virtual std::string str() const { return to_string(vresult[0]); } }; -template -class FunctorNode : public Node +template +struct OpString; + +template<> +struct OpString > { - private: - T &functor; - mutable rvec_t result; + static std::string str() { return "+"; } +}; - public: - FunctorNode(T &f) : functor(f) { result.resize(1); } - const rvec_t &val() const { - result[0] = (result_t)functor(); - return result; - } - virtual result_t total() const { return (result_t)functor(); }; +template<> +struct OpString > +{ + static std::string str() { return "-"; } +}; - virtual size_t size() const { return 1; } - /** - * Return true if stat is binned. - *@return False since Functors aren't binned - */ - virtual bool binned() const { return false; } +template<> +struct OpString > +{ + static std::string str() { return "*"; } }; -template -class ScalarNode : public Node +template<> +struct OpString > { - private: - T &scalar; - mutable rvec_t result; + static std::string str() { return "/"; } +}; - public: - ScalarNode(T &s) : scalar(s) { result.resize(1); } - const rvec_t &val() const { - result[0] = (result_t)scalar; - return result; - } - virtual result_t total() const { return (result_t)scalar; }; +template<> +struct OpString > +{ + static std::string str() { return "%"; } +}; - virtual size_t size() const { return 1; } - /** - * Return true if stat is binned. - *@return False since Scalar's aren't binned - */ - virtual bool binned() const { return false; } +template<> +struct OpString > +{ + static std::string str() { return "-"; } }; template @@ -1891,26 +2004,27 @@ class UnaryNode : public Node { public: NodePtr l; - mutable rvec_t result; + mutable VResult vresult; public: - UnaryNode(NodePtr p) : l(p) {} + UnaryNode(NodePtr &p) : l(p) {} - const rvec_t &val() const { - const rvec_t &lvec = l->val(); + const VResult &result() const + { + const VResult &lvec = l->result(); int size = lvec.size(); assert(size > 0); - result.resize(size); + vresult.resize(size); Op op; for (int i = 0; i < size; ++i) - result[i] = op(lvec[i]); + vresult[i] = op(lvec[i]); - return result; + return vresult; } - result_t total() const { + Result total() const { Op op; return op(l->total()); } @@ -1921,6 +2035,11 @@ class UnaryNode : public Node *@return True if child of node is binned. */ virtual bool binned() const { return l->binned(); } + + virtual std::string str() const + { + return OpString::str() + l->str(); + } }; template @@ -1929,42 +2048,43 @@ class BinaryNode : public Node public: NodePtr l; NodePtr r; - mutable rvec_t result; + mutable VResult vresult; public: - BinaryNode(NodePtr a, NodePtr b) : l(a), r(b) {} + BinaryNode(NodePtr &a, NodePtr &b) : l(a), r(b) {} - const rvec_t &val() const { + const VResult &result() const + { Op op; - const rvec_t &lvec = l->val(); - const rvec_t &rvec = r->val(); + const VResult &lvec = l->result(); + const VResult &rvec = r->result(); assert(lvec.size() > 0 && rvec.size() > 0); if (lvec.size() == 1 && rvec.size() == 1) { - result.resize(1); - result[0] = op(lvec[0], rvec[0]); + vresult.resize(1); + vresult[0] = op(lvec[0], rvec[0]); } else if (lvec.size() == 1) { int size = rvec.size(); - result.resize(size); + vresult.resize(size); for (int i = 0; i < size; ++i) - result[i] = op(lvec[0], rvec[i]); + vresult[i] = op(lvec[0], rvec[i]); } else if (rvec.size() == 1) { int size = lvec.size(); - result.resize(size); + vresult.resize(size); for (int i = 0; i < size; ++i) - result[i] = op(lvec[i], rvec[0]); + vresult[i] = op(lvec[i], rvec[0]); } else if (rvec.size() == lvec.size()) { int size = rvec.size(); - result.resize(size); + vresult.resize(size); for (int i = 0; i < size; ++i) - result[i] = op(lvec[i], rvec[i]); + vresult[i] = op(lvec[i], rvec[i]); } - return result; + return vresult; } - result_t total() const { + Result total() const { Op op; return op(l->total(), r->total()); } @@ -1986,6 +2106,11 @@ class BinaryNode : public Node *@return True if either child of node is binned. */ virtual bool binned() const { return (l->binned() || r->binned()); } + + virtual std::string str() const + { + return csprintf("(%s %s %s)", l->str(), OpString::str(), r->str()); + } }; template @@ -1993,37 +2118,39 @@ class SumNode : public Node { public: NodePtr l; - mutable rvec_t result; + mutable VResult vresult; public: - SumNode(NodePtr p) : l(p), result(1) {} + SumNode(NodePtr &p) : l(p), vresult(1) {} - const rvec_t &val() const { - const rvec_t &lvec = l->val(); + const VResult &result() const + { + const VResult &lvec = l->result(); int size = lvec.size(); assert(size > 0); - result[0] = 0.0; + vresult[0] = 0.0; Op op; for (int i = 0; i < size; ++i) - result[0] = op(result[0], lvec[i]); + vresult[0] = op(vresult[0], lvec[i]); - return result; + return vresult; } - result_t total() const { - const rvec_t &lvec = l->val(); + Result total() const + { + const VResult &lvec = l->result(); int size = lvec.size(); assert(size > 0); - result_t result = 0.0; + Result vresult = 0.0; Op op; for (int i = 0; i < size; ++i) - result = op(result, lvec[i]); + vresult = op(vresult, lvec[i]); - return result; + return vresult; } virtual size_t size() const { return 1; } @@ -2032,389 +2159,13 @@ class SumNode : public Node *@return True if child of node is binned. */ virtual bool binned() const { return l->binned(); } -}; - -/** - * Helper class to construct formula node trees. - */ -class Temp -{ - private: - /** - * 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) {} - /** - * Create a new ScalarStatNode. - * @param s The ScalarStat to place in a node. - */ - Temp(const ScalarStat &s) : node(new ScalarStatNode(s)) {} - /** - * Create a new ScalarProxyNode. - * @param p The ScalarProxy to place in a node. - */ - template class Storage, class Bin> - Temp(const ScalarProxy &p) - : node(new ScalarProxyNode(p)) {} - /** - * Create a new VectorStatNode. - * @param s The VectorStat to place in a node. - */ - Temp(const VectorStat &s) : node(new VectorStatNode(s)) {} - - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(signed char value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(unsigned char value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(signed short value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(unsigned short value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(signed int value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(unsigned int value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(signed long value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(unsigned long value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(signed long long value) - : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(unsigned long long value) - : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(float value) : node(new ConstNode(value)) {} - /** - * Create a ConstNode - * @param value The value of the const node. - */ - Temp(double value) : node(new ConstNode(value)) {} - - /** - * Return the node pointer. - * @return the node pointer. - */ - operator NodePtr() { return node;} -}; - - -////////////////////////////////////////////////////////////////////// -// -// Binning Interface -// -////////////////////////////////////////////////////////////////////// - -class BinBase -{ - private: - char *mem; - - protected: - off_t memsize; - off_t size() const { return memsize; } - char *memory(); - - public: - BinBase(); - virtual ~BinBase(); -}; - -} // namespace Detail - -class GenBin : public Detail::BinBase -{ - public: - GenBin() : BinBase() {} - virtual ~GenBin() {}; - - virtual void activate() = 0; - virtual std::string name() const = 0; - void regBin(GenBin *bin, std::string name); -}; - -template -struct StatBin : public GenBin -{ - private: - std::string _name; - - public: - std::string name() const { return _name;} - - static StatBin *&curBin() { - static StatBin *current = NULL; - return current; - } - - static void setCurBin(StatBin *bin) { curBin() = bin; } - static StatBin *current() { assert(curBin()); return curBin(); } - - static off_t &offset() { - static off_t offset = 0; - return offset; - } - - static off_t new_offset(size_t size) { - size_t mask = sizeof(u_int64_t) - 1; - off_t off = offset(); - - // That one is for the last trailing flags byte. - offset() += (size + 1 + mask) & ~mask; - return off; - } - - explicit StatBin(std::string name) : GenBin() { _name = name; this->regBin(this, name); } - - char *memory(off_t off) { - if (memsize == -1) { - memsize = CeilPow2((size_t) offset()); - } - assert(offset() <= size()); - return Detail::BinBase::memory() + off; - } - - virtual void activate() { - setCurBin(this); -#ifdef FS_MEASURE - DPRINTF(TCPIP, "activating %s Bin\n", name()); -#endif - } - static void activate(StatBin &bin) { setCurBin(&bin); } - class BinBase + virtual std::string str() const { - private: - int offset; - - public: - BinBase() : offset(-1) {} - void allocate(size_t size) { - offset = new_offset(size); - } - char *access() { - assert(offset != -1); - return current()->memory(offset); - } - }; - - template - class Bin : public BinBase - { - public: - typedef typename Storage::Params Params; - - public: - enum { binned = true }; - Bin() { allocate(sizeof(Storage)); } - bool initialized() const { return true; } - void init(const Params ¶ms) { } - - int size() const { return 1; } - - Storage *data(const Params ¶ms) { - assert(initialized()); - char *ptr = access(); - char *flags = ptr + sizeof(Storage); - if (!(*flags & 0x1)) { - *flags |= 0x1; - new (ptr) Storage(params); - } - return reinterpret_cast(ptr); - } - void reset() - { - char *ptr = access(); - char *flags = ptr + size() * sizeof(Storage); - if (!(*flags & 0x1)) - return; - - Storage *s = reinterpret_cast(ptr); - s->reset(); - } - }; - - template - class VectorBin : public BinBase - { - public: - typedef typename Storage::Params Params; - - private: - int _size; - - public: - enum { binned = true }; - VectorBin() : _size(0) {} - - bool initialized() const { return _size > 0; } - void init(int s, const Params ¶ms) { - assert(!initialized()); - assert(s > 0); - _size = s; - allocate(_size * sizeof(Storage)); - } - - int size() const { return _size; } - - Storage *data(int index, const Params ¶ms) { - assert(initialized()); - assert(index >= 0 && index < size()); - char *ptr = access(); - char *flags = ptr + size() * sizeof(Storage); - if (!(*flags & 0x1)) { - *flags |= 0x1; - for (int i = 0; i < size(); ++i) - new (ptr + i * sizeof(Storage)) Storage(params); - } - return reinterpret_cast(ptr + index * sizeof(Storage)); - } - void reset() - { - char *ptr = access(); - char *flags = ptr + size() * sizeof(Storage); - if (!(*flags & 0x1)) - return; - - for (int i = 0; i < _size; ++i) { - char *p = ptr + i * sizeof(Storage); - Storage *s = reinterpret_cast(p); - s->reset(); - } - } - }; + return csprintf("total(%s)", l->str()); + } }; -class MainBinType {}; -typedef StatBin MainBin; - -struct NoBin -{ - template - struct Bin - { - public: - typedef typename Storage::Params Params; - enum { binned = false }; - - private: - char ptr[sizeof(Storage)]; - - public: - ~Bin() - { - reinterpret_cast(ptr)->~Storage(); - } - - bool initialized() const { return true; } - void init(const Params ¶ms) { - new (ptr) Storage(params); - } - int size() const{ return 1; } - Storage *data(const Params ¶ms) { - assert(initialized()); - return reinterpret_cast(ptr); - } - void reset() - { - Storage *s = reinterpret_cast(ptr); - s->reset(); - } - }; - - template - struct VectorBin - { - public: - typedef typename Storage::Params Params; - enum { binned = false }; - - private: - char *ptr; - int _size; - - public: - VectorBin() : ptr(NULL) { } - ~VectorBin() - { - if (!initialized()) - return; - - for (int i = 0; i < _size; ++i) { - char *p = ptr + i * sizeof(Storage); - reinterpret_cast(p)->~Storage(); - } - delete [] ptr; - } - - bool initialized() const { return ptr != NULL; } - void init(int s, const Params ¶ms) { - assert(s > 0 && "size must be positive!"); - assert(!initialized()); - _size = s; - ptr = new char[_size * sizeof(Storage)]; - for (int i = 0; i < _size; ++i) - new (ptr + i * sizeof(Storage)) Storage(params); - } - - int size() const { return _size; } - - Storage *data(int index, const Params ¶ms) { - assert(initialized()); - assert(index >= 0 && index < size()); - return reinterpret_cast(ptr + index * sizeof(Storage)); - } - void reset() - { - for (int i = 0; i < _size; ++i) { - char *p = ptr + i * sizeof(Storage); - Storage *s = reinterpret_cast(p); - s->reset(); - } - } - }; -}; ////////////////////////////////////////////////////////////////////// // @@ -2430,11 +2181,11 @@ struct NoBin */ /** - * This is an easy way to assign all your stats to be binned or not binned. If the typedef - * is NoBin, nothing is binned. If it is MainBin (or whatever *Bin), then all stats are binned - * under that Bin. + * This is an easy way to assign all your stats to be binned or not + * binned. If the typedef is NoBin, nothing is binned. If it is + * MainBin, then all stats are binned under that Bin. */ -#ifdef FS_MEASURE +#if STATS_BINNING typedef MainBin DefaultBin; #else typedef NoBin DefaultBin; @@ -2444,12 +2195,20 @@ typedef NoBin DefaultBin; * This is a simple scalar statistic, like a counter. * @sa Stat, ScalarBase, StatStor */ -template -class Scalar : public Detail::ScalarBase +template +class Scalar + : public Wrap, + ScalarBase, + ScalarStatData> { public: /** The base implementation. */ - typedef Detail::ScalarBase Base; + typedef ScalarBase Base; + + Scalar() + { + this->setInit(); + } /** * Sets the stat equal to the given value. Calls the base implementation @@ -2457,19 +2216,51 @@ class Scalar : public Detail::ScalarBase * @param v The new value. */ template - void operator=(const U& v) { Base::operator=(v); } + void operator=(const U &v) { Base::operator=(v); } +}; + +class Value + : public Wrap +{ + public: + /** The base implementation. */ + typedef ValueBase Base; + + template + Value &scalar(T &value) + { + Base::scalar(value); + return *this; + } + + template + Value &functor(T &func) + { + Base::functor(func); + return *this; + } }; /** * A stat that calculates the per cycle average of a value. * @sa Stat, ScalarBase, AvgStor */ -template -class Average : public Detail::ScalarBase +template +class Average + : public Wrap, + ScalarBase, + ScalarStatData> { public: /** The base implementation. */ - typedef Detail::ScalarBase Base; + typedef ScalarBase Base; + + Average() + { + this->setInit(); + } /** * Sets the stat equal to the given value. Calls the base implementation @@ -2477,45 +2268,96 @@ class Average : public Detail::ScalarBase * @param v The new value. */ template - void operator=(const U& v) { Base::operator=(v); } + void operator=(const U &v) { Base::operator=(v); } }; /** * A vector of scalar stats. * @sa Stat, VectorBase, StatStor */ -template -class Vector : public Detail::VectorBase -{ }; +template +class Vector + : public WrapVec, + VectorBase, + VectorStatData> +{ + public: + /** The base implementation. */ + typedef ScalarBase 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->bin.init(size, this->params); + this->setInit(); + + return *this; + } +}; /** * A vector of Average stats. * @sa Stat, VectorBase, AvgStor */ -template -class AverageVector : public Detail::VectorBase -{ }; +template +class AverageVector + : public WrapVec, + VectorBase, + 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->bin.init(size, this->params); + this->setInit(); + + return *this; + } +}; /** * A 2-Dimensional vecto of scalar stats. * @sa Stat, Vector2dBase, StatStor */ -template -class Vector2d : public Detail::Vector2dBase -{ }; +template +class Vector2d + : public WrapVec2d, + Vector2dBase, + Vector2dStatData> +{ + public: + Vector2d &init(size_t _x, size_t _y) { + this->statData()->x = this->x = _x; + this->statData()->y = this->y = _y; + this->bin.init(this->x * this->y, this->params); + this->setInit(); + + return *this; + } +}; /** * A simple distribution stat. * @sa Stat, DistBase, DistStor */ -template -class Distribution : public Detail::DistBase +template +class Distribution + : public Wrap, + DistBase, + DistStatData> { - private: + public: /** Base implementation. */ - typedef Detail::DistBase Base; + typedef DistBase Base; /** The Parameter type. */ - typedef typename Detail::DistStor::Params Params; + typedef typename DistStor::Params Params; public: /** @@ -2525,13 +2367,13 @@ class Distribution : public Detail::DistBase * @param bkt The number of values in each bucket. * @return A reference to this distribution. */ - Distribution &init(T min, T max, int bkt) { - params.min = min; - params.max = max; - params.bucket_size = bkt; - params.size = (max - min) / bkt + 1; - bin.init(params); - setInit(); + 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->bin.init(this->params); + this->setInit(); return *this; } @@ -2541,22 +2383,25 @@ class Distribution : public Detail::DistBase * Calculates the mean and variance of all the samples. * @sa Stat, DistBase, FancyStor */ -template -class StandardDeviation : public Detail::DistBase +template +class StandardDeviation + : public Wrap, + DistBase, + DistStatData> { - private: + public: /** The base implementation */ - typedef Detail::DistBase Base; + typedef DistBase Base; /** The parameter type. */ - typedef typename Detail::DistStor::Params Params; + typedef typename DistStor::Params Params; public: /** * Construct and initialize this distribution. */ StandardDeviation() { - bin.init(params); - setInit(); + this->bin.init(this->params); + this->setInit(); } }; @@ -2564,22 +2409,26 @@ class StandardDeviation : public Detail::DistBase * Calculates the per cycle mean and variance of the samples. * @sa Stat, DistBase, AvgFancy */ -template -class AverageDeviation : public Detail::DistBase +template +class AverageDeviation + : public Wrap, + DistBase, + DistStatData> { - private: + public: /** The base implementation */ - typedef Detail::DistBase Base; + typedef DistBase Base; /** The parameter type. */ - typedef typename Detail::DistStor::Params Params; + typedef typename DistStor::Params Params; public: /** * Construct and initialize this distribution. */ - AverageDeviation() { - bin.init(params); - setInit(); + AverageDeviation() + { + this->bin.init(this->params); + this->setInit(); } }; @@ -2587,15 +2436,17 @@ class AverageDeviation : public Detail::DistBase * A vector of distributions. * @sa Stat, VectorDistBase, DistStor */ -template +template class VectorDistribution - : public Detail::VectorDistBase + : public WrapVec, + VectorDistBase, + VectorDistStatData> { - private: + public: /** The base implementation */ - typedef Detail::VectorDistBase Base; + typedef VectorDistBase Base; /** The parameter type. */ - typedef typename Detail::DistStor::Params Params; + typedef typename DistStor::Params Params; public: /** @@ -2606,13 +2457,13 @@ class VectorDistribution * @param bkt The number of values in each bucket. * @return A reference to this distribution. */ - VectorDistribution &init(int size, T min, T max, int bkt) { - params.min = min; - params.max = max; - params.bucket_size = bkt; - params.size = (max - min) / bkt + 1; - bin.init(size, params); - setInit(); + 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->bin.init(size, this->params); + this->setInit(); return *this; } @@ -2622,15 +2473,17 @@ class VectorDistribution * This is a vector of StandardDeviation stats. * @sa Stat, VectorDistBase, FancyStor */ -template +template class VectorStandardDeviation - : public Detail::VectorDistBase + : public WrapVec, + VectorDistBase, + VectorDistStatData> { - private: + public: /** The base implementation */ - typedef Detail::VectorDistBase Base; + typedef VectorDistBase Base; /** The parameter type. */ - typedef typename Detail::DistStor::Params Params; + typedef typename DistStor::Params Params; public: /** @@ -2639,8 +2492,8 @@ class VectorStandardDeviation * @return A reference to this distribution. */ VectorStandardDeviation &init(int size) { - bin.init(size, params); - setInit(); + this->bin.init(size, this->params); + this->setInit(); return *this; } @@ -2650,15 +2503,17 @@ class VectorStandardDeviation * This is a vector of AverageDeviation stats. * @sa Stat, VectorDistBase, AvgFancy */ -template +template class VectorAverageDeviation - : public Detail::VectorDistBase + : public WrapVec, + VectorDistBase, + VectorDistStatData> { - private: + public: /** The base implementation */ - typedef Detail::VectorDistBase Base; + typedef VectorDistBase Base; /** The parameter type. */ - typedef typename Detail::DistStor::Params Params; + typedef typename DistStor::Params Params; public: /** @@ -2667,8 +2522,8 @@ class VectorAverageDeviation * @return A reference to this distribution. */ VectorAverageDeviation &init(int size) { - bin.init(size, params); - setInit(); + this->bin.init(size, this->params); + this->setInit(); return *this; } @@ -2677,181 +2532,366 @@ class VectorAverageDeviation /** * 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, Detail::Temp + * @sa Stat, ScalarStat, VectorStat, Node, Temp */ -class Formula : public Detail::VectorStat +class FormulaBase : public DataAccess { - private: + protected: /** The root of the tree which represents the Formula */ - Detail::NodePtr root; - friend class Statistics::Detail::Temp; + 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; + + /** + * Return true if Formula is binned. i.e. any of its children + * nodes are binned + * @return True if Formula is binned. + */ + bool binned() 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 FormulaStatData : public FormulaData +{ + protected: + Stat &s; + mutable VResult vec; + mutable VCounter cvec; + + public: + FormulaStatData(Stat &stat) : s(stat) {} + + virtual bool binned() const { return s.binned(); } + 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 +{ public: /** * Create and initialize thie formula, and register it with the database. */ - Formula() : VectorStat(true) { setInit(); } + Formula(); + /** * Create a formula with the given root node, register it with the * database. * @param r The root of the expression tree. */ - Formula(Detail::Temp r) : VectorStat(true) { - root = r; - assert(size()); - } + 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=(Detail::Temp r) { - assert(!root && "Can't change formulas"); - root = r; - assert(size()); - return *this; - } + 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+=(Detail::Temp r) { - using namespace Detail; - if (root) - root = NodePtr(new BinaryNode >(root, r)); - else - root = r; - assert(size()); - return *this; - } + 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 bool binned() const { return formula.binned(); } + + virtual std::string str() const { return formula.str(); } +}; + +/** + * Helper class to construct formula node trees. + */ +class Temp +{ + protected: /** - * 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. + * Pointer to a Node object. */ - const rvec_t &val() const { return root->val(); } + NodePtr node; + + public: /** - * 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 rvec_t - * val() returns. - * @return The total of the result vector. + * Copy the given pointer to this class. + * @param n A pointer to a Node object to copy. */ - result_t total() const { return root->total(); } + Temp(NodePtr n) : node(n) { } /** - * Return the number of elements in the tree. + * Return the node pointer. + * @return the node pointer. */ - size_t size() const { - if (!root) - return 0; - else - return root->size(); - } + operator NodePtr&() { return node;} + + public: /** - * Return true if Formula is binned. i.e. any of its children nodes are binned - *@return True if Formula is binned. + * Create a new ScalarStatNode. + * @param s The ScalarStat to place in a node. */ - virtual bool binned() const { return root->binned(); } + template + Temp(const Scalar &s) + : node(new ScalarStatNode(s.statData())) { } /** - * Formulas don't need to be reset + * 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 + Temp(const Average &s) + : node(new ScalarStatNode(s.statData())) { } + + /** + * Create a new VectorStatNode. + * @param s The VectorStat to place in a node. + */ + template + Temp(const Vector &s) + : node(new VectorStatNode(s.statData())) { } + + /** + * + */ + Temp(const Formula &f) + : node(new FormulaNode(f)) { } + + /** + * Create a new ScalarProxyNode. + * @param p The ScalarProxy to place in a node. + */ + template + Temp(const ScalarProxy &p) + : node(new ScalarProxyNode(p)) { } + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(signed char value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(unsigned char value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(signed short value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(unsigned short value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(signed int value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(unsigned int value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(signed long value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(unsigned long value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. */ - virtual void reset() {} + Temp(signed long long value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(unsigned long long value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(float value) + : node(new ConstNode(value)) {} + + /** + * Create a ConstNode + * @param value The value of the const node. + */ + Temp(double value) + : node(new ConstNode(value)) {} }; + /** * @} */ void check(); -void dump(std::ostream &stream); void reset(); -void RegResetCallback(Callback *cb); +void registerResetCallback(Callback *cb); -inline Detail::Temp -operator+(Detail::Temp l, Detail::Temp r) +inline Temp +operator+(Temp l, Temp r) { - using namespace Detail; - return NodePtr(new BinaryNode >(l, r)); + return NodePtr(new BinaryNode >(l, r)); } -inline Detail::Temp -operator-(Detail::Temp l, Detail::Temp r) +inline Temp +operator-(Temp l, Temp r) { - using namespace Detail; - return NodePtr(new BinaryNode >(l, r)); + return NodePtr(new BinaryNode >(l, r)); } -inline Detail::Temp -operator*(Detail::Temp l, Detail::Temp r) +inline Temp +operator*(Temp l, Temp r) { - using namespace Detail; - return NodePtr(new BinaryNode >(l, r)); + return NodePtr(new BinaryNode >(l, r)); } -inline Detail::Temp -operator/(Detail::Temp l, Detail::Temp r) +inline Temp +operator/(Temp l, Temp r) { - using namespace Detail; - return NodePtr(new BinaryNode >(l, r)); + return NodePtr(new BinaryNode >(l, r)); } -inline Detail::Temp -operator%(Detail::Temp l, Detail::Temp r) +inline Temp +operator-(Temp l) { - using namespace Detail; - return NodePtr(new BinaryNode >(l, r)); -} - -inline Detail::Temp -operator-(Detail::Temp l) -{ - using namespace Detail; - return NodePtr(new UnaryNode >(l)); + return NodePtr(new UnaryNode >(l)); } template -inline Detail::Temp +inline Temp constant(T val) { - using namespace Detail; return NodePtr(new ConstNode(val)); } -template -inline Detail::Temp -functor(T &val) -{ - using namespace Detail; - return NodePtr(new FunctorNode(val)); -} - -template -inline Detail::Temp -scalar(T &val) -{ - using namespace Detail; - return NodePtr(new ScalarNode(val)); -} - -inline Detail::Temp -sum(Detail::Temp val) +inline Temp +sum(Temp val) { - using namespace Detail; - return NodePtr(new SumNode >(val)); + return NodePtr(new SumNode >(val)); } -extern bool PrintDescriptions; - -} // namespace statistics +/* namespace Stats */ } -#endif // __STATISTICS_HH__ +#endif // __BASE_STATISTICS_HH__