From e331aa9718f50d6d376d6fb8684d17d02f360d54 Mon Sep 17 00:00:00 2001 From: Hoa Nguyen Date: Thu, 7 Jan 2021 03:18:57 -0800 Subject: [PATCH] base-stats: Add Units to Stats This commit adds an option to add SI and its derived units to stats. Units are now the third parameter of every Stat class constructor. The following are convenient macros that could be used to add units to stats, * UNIT_CYCLE: represents clock cycles. * UNIT_TICK: represents the count of gem5's Tick. * UNIT_SECOND: represents the base unit of time defined by SI. * UNIT_BIT: represents the number of computer bits. * UNIT_BYTE: represents 8 bits. * UNIT_VOLT: a SI derived unit measuring potential difference. * UNIT_JOULE: represents joule, a unit of energy, as defined by SI. * UNIT_WATT: represents 1 watt, where 1 watt = 1 joule / second. * UNIT_CELSIUS: represents 1 Celsius degree as defined by SI. * UNIT_RATE(T1, T2): represents the unit of a quantity of T1 divided by a quantity of T2. * UNIT_RATIO: represents the unit of a quantity of unit T divided by a quantity of unit T. * UNIT_UNSPECIFIED: the unit of the stat is unspecified. This type of unit is mainly for backward compatibility. Newly introduced stats should have the units specified. This commit also alters the behavior of the ADD_STAT macro. Specifically, ADD_STAT syntax is unchanged, but the unit of the stat is hardcoded to be UNIT_UNSPECIFIED. JIRA link: https://gem5.atlassian.net/browse/GEM5-849 This change is an effort towards supporting new stats schema: https://github.com/gem5/stats-schema Change-Id: I791704a6c4d9e06332797dbfc5eb611cb43f4710 Signed-off-by: Hoa Nguyen Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/38855 Tested-by: kokoro Maintainer: Bobby R. Bruce Reviewed-by: Daniel Carvalho Reviewed-by: Andreas Sandberg --- src/base/statistics.cc | 19 ++- src/base/statistics.hh | 320 ++++++++++++++++++++++++++++++------ src/base/stats/group.hh | 10 +- src/base/stats/info.hh | 5 +- src/base/stats/units.hh | 350 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 648 insertions(+), 56 deletions(-) create mode 100644 src/base/stats/units.hh diff --git a/src/base/statistics.cc b/src/base/statistics.cc index 122f54be3..fd44e16c8 100644 --- a/src/base/statistics.cc +++ b/src/base/statistics.cc @@ -132,16 +132,29 @@ InfoAccess::info() const } Formula::Formula(Group *parent, const char *name, const char *desc) - : DataWrapVec(parent, name, desc) + : DataWrapVec(parent, name, UNIT_UNSPECIFIED, + desc) { } - +Formula::Formula(Group *parent, const char *name, const Units::Base *unit, + const char *desc) + : DataWrapVec(parent, name, unit, desc) +{ +} Formula::Formula(Group *parent, const char *name, const char *desc, const Temp &r) - : DataWrapVec(parent, name, desc) + : DataWrapVec(parent, name, UNIT_UNSPECIFIED, + desc) +{ + *this = r; +} + +Formula::Formula(Group *parent, const char *name, const Units::Base *unit, + const char *desc, const Temp &r) + : DataWrapVec(parent, name, unit, desc) { *this = r; } diff --git a/src/base/statistics.hh b/src/base/statistics.hh index a1994c843..63bfb5b69 100644 --- a/src/base/statistics.hh +++ b/src/base/statistics.hh @@ -72,14 +72,15 @@ #include #include +#include "base/cast.hh" +#include "base/cprintf.hh" +#include "base/intmath.hh" #include "base/stats/group.hh" #include "base/stats/info.hh" #include "base/stats/output.hh" #include "base/stats/storage.hh" #include "base/stats/types.hh" -#include "base/cast.hh" -#include "base/cprintf.hh" -#include "base/intmath.hh" +#include "base/stats/units.hh" #include "base/str.hh" #include "base/types.hh" @@ -240,8 +241,8 @@ class DataWrap : public InfoAccess DataWrap(const DataWrap &) = delete; DataWrap &operator=(const DataWrap &) = delete; - - DataWrap(Group *parent, const char *name, const char *desc) + DataWrap(Group *parent, const char *name, const Units::Base *unit, + const char *desc) { auto info = new Info(self()); this->setInfo(parent, info); @@ -254,6 +255,8 @@ class DataWrap : public InfoAccess info->flags.set(display); } + info->unit = unit; + if (desc) info->desc = desc; } @@ -290,6 +293,18 @@ class DataWrap : public InfoAccess return this->info()->separatorString; } + /** + * Set the unit of the stat. + * @param unit The new unit. + * @return A reference to this stat. + */ + Derived & + unit(const Units::Base *_unit) + { + this->info()->unit = _unit; + return this->self(); + } + /** * Set the description and marks this stat to print at the end of * simulation. @@ -349,8 +364,9 @@ class DataWrapVec : public DataWrap typedef InfoProxyType Info; DataWrapVec(Group *parent = nullptr, const char *name = nullptr, + const Units::Base *unit = UNIT_UNSPECIFIED, const char *desc = nullptr) - : DataWrap(parent, name, desc) + : DataWrap(parent, name, unit, desc) {} // The following functions are specific to vectors. If you use them @@ -429,8 +445,9 @@ class DataWrapVec2d : public DataWrapVec public: typedef InfoProxyType Info; - DataWrapVec2d(Group *parent, const char *name, const char *desc) - : DataWrapVec(parent, name, desc) + DataWrapVec2d(Group *parent, const char *name, + const Units::Base *unit, const char *desc) + : DataWrapVec(parent, name, unit, desc) { } @@ -524,8 +541,9 @@ class ScalarBase : public DataWrap public: ScalarBase(Group *parent = nullptr, const char *name = nullptr, + const Units::Base *unit = UNIT_UNSPECIFIED, const char *desc = nullptr) - : DataWrap(parent, name, desc) + : DataWrap(parent, name, unit, desc) { this->doInit(); } @@ -680,8 +698,10 @@ class ValueBase : public DataWrap ProxyInfo *proxy; public: - ValueBase(Group *parent, const char *name, const char *desc) - : DataWrap(parent, name, desc), + ValueBase(Group *parent, const char *name, + const Units::Base *unit, + const char *desc) + : DataWrap(parent, name, unit, desc), proxy(NULL) { } @@ -982,8 +1002,10 @@ class VectorBase : public DataWrapVec } public: - VectorBase(Group *parent, const char *name, const char *desc) - : DataWrapVec(parent, name, desc), + VectorBase(Group *parent, const char *name, + const Units::Base *unit, + const char *desc) + : DataWrapVec(parent, name, unit, desc), storage(nullptr), _size(0) {} @@ -1123,8 +1145,10 @@ class Vector2dBase : public DataWrapVec2d const Storage *data(off_type index) const { return &storage[index]; } public: - Vector2dBase(Group *parent, const char *name, const char *desc) - : DataWrapVec2d(parent, name, desc), + Vector2dBase(Group *parent, const char *name, + const Units::Base *unit, + const char *desc) + : DataWrapVec2d(parent, name, unit, desc), x(0), y(0), _size(0), storage(nullptr) {} @@ -1282,8 +1306,10 @@ class DistBase : public DataWrap } public: - DistBase(Group *parent, const char *name, const char *desc) - : DataWrap(parent, name, desc) + DistBase(Group *parent, const char *name, + const Units::Base *unit, + const char *desc) + : DataWrap(parent, name, unit, desc) { } @@ -1378,8 +1404,10 @@ class VectorDistBase : public DataWrapVec } public: - VectorDistBase(Group *parent, const char *name, const char *desc) - : DataWrapVec(parent, name, desc), + VectorDistBase(Group *parent, const char *name, + const Units::Base *unit, + const char *desc) + : DataWrapVec(parent, name, unit, desc), storage(NULL) {} @@ -1907,9 +1935,20 @@ class Scalar : public ScalarBase public: using ScalarBase::operator=; - Scalar(Group *parent = nullptr, const char *name = nullptr, + Scalar(Group *parent = nullptr) + : ScalarBase(parent, nullptr, UNIT_UNSPECIFIED, + nullptr) + { + } + + Scalar(Group *parent, const char *name, const char *desc = nullptr) + : ScalarBase(parent, name, UNIT_UNSPECIFIED, desc) + { + } + + Scalar(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : ScalarBase(parent, name, desc) + : ScalarBase(parent, name, unit, desc) { } }; @@ -1923,9 +1962,20 @@ class Average : public ScalarBase public: using ScalarBase::operator=; - Average(Group *parent = nullptr, const char *name = nullptr, + Average(Group *parent = nullptr) + : ScalarBase(parent, nullptr, UNIT_UNSPECIFIED, + nullptr) + { + } + + Average(Group *parent, const char *name, const char *desc = nullptr) + : ScalarBase(parent, name, UNIT_UNSPECIFIED, desc) + { + } + + Average(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : ScalarBase(parent, name, desc) + : ScalarBase(parent, name, unit, desc) { } }; @@ -1933,9 +1983,19 @@ class Average : public ScalarBase class Value : public ValueBase { public: - Value(Group *parent = nullptr, const char *name = nullptr, + Value(Group *parent = nullptr) + : ValueBase(parent, nullptr, UNIT_UNSPECIFIED, nullptr) + { + } + + Value(Group *parent, const char *name, const char *desc = nullptr) + : ValueBase(parent, name, UNIT_UNSPECIFIED, desc) + { + } + + Value(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : ValueBase(parent, name, desc) + : ValueBase(parent, name, unit, desc) { } }; @@ -1947,9 +2007,20 @@ class Value : public ValueBase class Vector : public VectorBase { public: - Vector(Group *parent = nullptr, const char *name = nullptr, + Vector(Group *parent = nullptr) + : VectorBase(parent, nullptr, UNIT_UNSPECIFIED, + nullptr) + { + } + + Vector(Group *parent, const char *name, const char *desc = nullptr) + : VectorBase(parent, name, UNIT_UNSPECIFIED, desc) + { + } + + Vector(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : VectorBase(parent, name, desc) + : VectorBase(parent, name, unit, desc) { } }; @@ -1961,9 +2032,21 @@ class Vector : public VectorBase class AverageVector : public VectorBase { public: - AverageVector(Group *parent = nullptr, const char *name = nullptr, + AverageVector(Group *parent = nullptr) + : VectorBase(parent, nullptr, UNIT_UNSPECIFIED, + nullptr) + { + } + + AverageVector(Group *parent, const char *name, const char *desc = nullptr) + : VectorBase(parent, name, UNIT_UNSPECIFIED, + desc) + { + } + + AverageVector(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : VectorBase(parent, name, desc) + : VectorBase(parent, name, unit, desc) { } }; @@ -1975,9 +2058,21 @@ class AverageVector : public VectorBase class Vector2d : public Vector2dBase { public: - Vector2d(Group *parent = nullptr, const char *name = nullptr, + Vector2d(Group *parent = nullptr) + : Vector2dBase(parent, nullptr, UNIT_UNSPECIFIED, + nullptr) + { + } + + Vector2d(Group *parent, const char *name, const char *desc = nullptr) + : Vector2dBase(parent, name, UNIT_UNSPECIFIED, + desc) + { + } + + Vector2d(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : Vector2dBase(parent, name, desc) + : Vector2dBase(parent, name, unit, desc) { } }; @@ -1989,9 +2084,21 @@ class Vector2d : public Vector2dBase class Distribution : public DistBase { public: - Distribution(Group *parent = nullptr, const char *name = nullptr, + Distribution(Group *parent = nullptr) + : DistBase(parent, nullptr, UNIT_UNSPECIFIED, + nullptr) + { + } + + Distribution(Group *parent, const char *name, const char *desc = nullptr) + : DistBase(parent, name, UNIT_UNSPECIFIED, + desc) + { + } + + Distribution(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : DistBase(parent, name, desc) + : DistBase(parent, name, unit, desc) { } @@ -2019,9 +2126,21 @@ class Distribution : public DistBase class Histogram : public DistBase { public: - Histogram(Group *parent = nullptr, const char *name = nullptr, + Histogram(Group *parent = nullptr) + : DistBase(parent, nullptr, UNIT_UNSPECIFIED, + nullptr) + { + } + + Histogram(Group *parent, const char *name, + const char *desc = nullptr) + : DistBase(parent, name, UNIT_UNSPECIFIED, desc) + { + } + + Histogram(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : DistBase(parent, name, desc) + : DistBase(parent, name, unit, desc) { } @@ -2050,9 +2169,28 @@ class StandardDeviation : public DistBase /** * Construct and initialize this distribution. */ - StandardDeviation(Group *parent = nullptr, const char *name = nullptr, + StandardDeviation(Group *parent = nullptr) + : DistBase(parent, nullptr, + UNIT_UNSPECIFIED, nullptr) + { + SampleStor::Params *params = new SampleStor::Params; + this->doInit(); + this->setParams(params); + } + + StandardDeviation(Group *parent, const char *name, + const char *desc = nullptr) + : DistBase(parent, name, + UNIT_UNSPECIFIED, desc) + { + SampleStor::Params *params = new SampleStor::Params; + this->doInit(); + this->setParams(params); + } + + StandardDeviation(Group *parent, const char *name, const Units::Base *unit, const char *desc = nullptr) - : DistBase(parent, name, desc) + : DistBase(parent, name, unit, desc) { SampleStor::Params *params = new SampleStor::Params; this->doInit(); @@ -2070,9 +2208,28 @@ class AverageDeviation : public DistBase /** * Construct and initialize this distribution. */ - AverageDeviation(Group *parent = nullptr, const char *name = nullptr, + AverageDeviation(Group *parent = nullptr) + : DistBase(parent, nullptr, + UNIT_UNSPECIFIED, nullptr) + { + AvgSampleStor::Params *params = new AvgSampleStor::Params; + this->doInit(); + this->setParams(params); + } + + AverageDeviation(Group *parent, const char *name, const char *desc = nullptr) - : DistBase(parent, name, desc) + : DistBase(parent, name, + UNIT_UNSPECIFIED, desc) + { + AvgSampleStor::Params *params = new AvgSampleStor::Params; + this->doInit(); + this->setParams(params); + } + + AverageDeviation(Group *parent, const char *name, const Units::Base *unit, + const char *desc = nullptr) + : DistBase(parent, name, unit, desc) { AvgSampleStor::Params *params = new AvgSampleStor::Params; this->doInit(); @@ -2087,9 +2244,24 @@ class AverageDeviation : public DistBase class VectorDistribution : public VectorDistBase { public: - VectorDistribution(Group *parent = nullptr, const char *name = nullptr, + VectorDistribution(Group *parent = nullptr) + : VectorDistBase(parent, nullptr, + UNIT_UNSPECIFIED, nullptr) + { + } + + VectorDistribution(Group *parent, const char *name, + const char *desc = nullptr) + : VectorDistBase(parent, name, + UNIT_UNSPECIFIED, desc) + { + } + + VectorDistribution(Group *parent, const char *name, + const Units::Base *unit, const char *desc = nullptr) - : VectorDistBase(parent, name, desc) + : VectorDistBase(parent, name, unit, + desc) { } @@ -2119,10 +2291,24 @@ class VectorStandardDeviation : public VectorDistBase { public: - VectorStandardDeviation(Group *parent = nullptr, const char *name = nullptr, + VectorStandardDeviation(Group *parent = nullptr) + : VectorDistBase(parent, nullptr, + UNIT_UNSPECIFIED, nullptr) + { + } + + VectorStandardDeviation(Group *parent, const char *name, const char *desc = nullptr) : VectorDistBase(parent, name, - desc) + UNIT_UNSPECIFIED, desc) + { + } + + VectorStandardDeviation(Group *parent, const char *name, + const Units::Base *unit, + const char *desc = nullptr) + : VectorDistBase(parent, name, + unit, desc) { } @@ -2149,10 +2335,24 @@ class VectorAverageDeviation : public VectorDistBase { public: - VectorAverageDeviation(Group *parent = nullptr, const char *name = nullptr, + VectorAverageDeviation(Group *parent = nullptr) + : VectorDistBase(parent, + nullptr, UNIT_UNSPECIFIED, nullptr) + { + } + + VectorAverageDeviation(Group *parent, const char *name, const char *desc = nullptr) : VectorDistBase(parent, name, - desc) + UNIT_UNSPECIFIED, desc) + { + } + + VectorAverageDeviation(Group *parent, const char *name, + const Units::Base *unit, + const char *desc = nullptr) + : VectorDistBase(parent, name, + unit, desc) { } @@ -2247,8 +2447,10 @@ class SparseHistBase : public DataWrap } public: - SparseHistBase(Group *parent, const char *name, const char *desc) - : DataWrap(parent, name, desc) + SparseHistBase(Group *parent, const char *name, + const Units::Base *unit, + const char *desc) + : DataWrap(parent, name, unit, desc) { } @@ -2292,9 +2494,23 @@ class SparseHistBase : public DataWrap class SparseHistogram : public SparseHistBase { public: - SparseHistogram(Group *parent = nullptr, const char *name = nullptr, + SparseHistogram(Group *parent = nullptr) + : SparseHistBase(parent, nullptr, + UNIT_UNSPECIFIED, nullptr) + { + } + + SparseHistogram(Group *parent, const char *name, const char *desc = nullptr) - : SparseHistBase(parent, name, desc) + : SparseHistBase(parent, name, + UNIT_UNSPECIFIED, desc) + { + } + + SparseHistogram(Group *parent, const char *name, const Units::Base *unit, + const char *desc = nullptr) + : SparseHistBase(parent, name, unit, + desc) { } @@ -2333,9 +2549,15 @@ class Formula : public DataWrapVec Formula(Group *parent = nullptr, const char *name = nullptr, const char *desc = nullptr); + Formula(Group *parent, const char *name, const Units::Base *unit, + const char *desc = nullptr); + Formula(Group *parent, const char *name, const char *desc, const Temp &r); + Formula(Group *parent, const char *name, const Units::Base *unit, + const char *desc, const Temp &r); + /** * Set an unitialized Formula to the given root. * @param r The root of the expression tree. diff --git a/src/base/stats/group.hh b/src/base/stats/group.hh index ef223bce6..85a77a169 100644 --- a/src/base/stats/group.hh +++ b/src/base/stats/group.hh @@ -39,8 +39,10 @@ #define __BASE_STATS_GROUP_HH__ #include -#include #include +#include + +#include "base/stats/units.hh" /** * Convenience macro to add a stat to a statistics group. @@ -58,13 +60,15 @@ * * Group() * : ADD_STAT(scalar0, "Description of scalar0"), - * scalar1(this, "scalar1", "Description of scalar1") + * scalar1(this, "scalar1", UNIT_UNSPECIFIED, + * "Description of scalar1") * { * } * }; * \endcode */ -#define ADD_STAT(n, ...) n(this, # n, __VA_ARGS__) + +#define ADD_STAT(n, ...) n(this, #n, __VA_ARGS__) namespace Stats { diff --git a/src/base/stats/info.hh b/src/base/stats/info.hh index d9a66548d..0568aa4ed 100644 --- a/src/base/stats/info.hh +++ b/src/base/stats/info.hh @@ -29,8 +29,9 @@ #ifndef __BASE_STATS_INFO_HH__ #define __BASE_STATS_INFO_HH__ -#include "base/stats/types.hh" #include "base/flags.hh" +#include "base/stats/types.hh" +#include "base/stats/units.hh" namespace Stats { @@ -73,6 +74,8 @@ class Info std::string name; /** The separator string used for vectors, dist, etc. */ static std::string separatorString; + /** The unit of the stat. */ + const Units::Base* unit = UNIT_UNSPECIFIED; /** The description of the stat. */ std::string desc; /** The formatting flags. */ diff --git a/src/base/stats/units.hh b/src/base/stats/units.hh new file mode 100644 index 000000000..5b785b426 --- /dev/null +++ b/src/base/stats/units.hh @@ -0,0 +1,350 @@ +/* + * Copyright (c) 2021 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer; + * redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution; + * neither the name of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __BASE_STATS_UNITS_HH__ +#define __BASE_STATS_UNITS_HH__ + +#include + +#include "base/cprintf.hh" + +/** + * Convenience macros to declare the unit of a stat. + */ +#define UNIT_CYCLE Stats::Units::Cycle::get() +#define UNIT_TICK Stats::Units::Tick::get() +#define UNIT_SECOND Stats::Units::Second::get() +#define UNIT_BIT Stats::Units::Bit::get() +#define UNIT_BYTE Stats::Units::Byte::get() +#define UNIT_JOULE Stats::Units::Joule::get() +#define UNIT_VOLT Stats::Units::Volt::get() +#define UNIT_CELSIUS Stats::Units::DegreeCelsius::get() +#define UNIT_RATE(T1, T2) Stats::Units::Rate::get() +#define UNIT_RATIO Stats::Units::Ratio::get() +#define UNIT_COUNT Stats::Units::Count::get() +#define UNIT_UNSPECIFIED Stats::Units::Unspecified::get() + +#define UNIT_WATT UNIT_RATE(Stats::Units::Joule, Stats::Units::Second) + +namespace Stats { + +/** + * Units for Stats. + * + * This header file provides an ability to associate a stat object with a + * specific unit. + * + * The supported units are: + * - Cycle: represents clock cycles. + * - Tick: represents the count of gem5's Tick. + * - Second: represents the base unit of time defined by SI. + * - Bit: represents the number of computer bits. + * - Byte: represents 8 bits. + * - Volt: a SI derived unit measuring potential difference. + * - Joule: represents joule, a unit of energy, as defined by SI. + * - Watt: represents 1 watt, where 1 watt = 1 joule / second. + * - Celsius: represents 1 Celsius degree as defined by SI. + * - Rate(T1, T2): represents the unit of a quantity of T1 divided by + * a quantity of T2. + * - Ratio: represents the unit of a quantity of unit T divided by a quantity + * of T. + * - Count: represents the count of a quantity that is not defined above. + * - Unspecified: the unit of the stat is unspecified. + * + * Each unit class is intended to be a singleton, which means only each unit + * class has at most one object of that class exist throughout the program. + * Therefore, copy constructors and assignment operators are deleted functions. + * + * When any of the following criteria is met, a new unit should be added, + * - The new unit is significant enough to be not included in Count unit. + * (e.g. Cycle unit, Tick unit) + */ +namespace Units { + +/** + * The Base class is the parent class of all unit classes. + * This class is intended to an abstract class specifying common behaviors of + * all unit classes. + */ +class Base +{ + public: + virtual std::string getUnitString() const = 0; +}; + +class Cycle : public Base +{ + private: + Cycle() {} + public: + Cycle(Cycle const&) = delete; + void operator=(Cycle const&) = delete; + static Cycle* + get() + { + static Cycle instance; + return &instance; + } + static std::string toString() { return "Cycle"; } + std::string getUnitString() const override { return Cycle::toString(); } +}; + +class Tick : public Base +{ + private: + Tick() {} + public: + Tick(Tick const&) = delete; + void operator=(Tick const&) = delete; + static Tick* + get() + { + static Tick instance; + return &instance; + } + static std::string toString() { return "Tick"; } + std::string getUnitString() const override { return Tick::toString(); } +}; + +class Second : public Base +{ + private: + Second() {} + public: + Second(Second const&) = delete; + void operator=(Second const&) = delete; + static Second* + get() + { + static Second instance; + return &instance; + } + static std::string toString() { return "Second"; } + std::string getUnitString() const override { return Second::toString(); } +}; + +class Bit : public Base +{ + private: + Bit() {} + public: + Bit(Bit const&) = delete; + void operator=(Bit const&) = delete; + static Bit* + get() + { + static Bit instance; + return &instance; + } + static std::string toString() { return "Bit"; } + std::string getUnitString() const override { return Bit::toString(); } +}; + +class Byte : public Base +{ + private: + Byte() {} + public: + Byte(Byte const&) = delete; + void operator=(Byte const&) = delete; + static Byte* + get() + { + static Byte instance; + return &instance; + } + static std::string toString() { return "Byte"; } + std::string getUnitString() const override { return Byte::toString(); } +}; + +class Watt : public Base +{ + private: + Watt() {} + public: + Watt(Watt const&) = delete; + void operator=(Watt const&) = delete; + static Watt* + get() + { + static Watt instance; + return &instance; + } + static std::string toString() { return "Watt"; } + std::string getUnitString() const override { return Watt::toString(); } +}; + + +class Joule : public Base +{ + private: + Joule() {} + public: + Joule(Joule const&) = delete; + void operator=(Joule const&) = delete; + static Joule* + get() + { + static Joule instance; + return &instance; + } + static std::string toString() { return "Joule"; } + std::string getUnitString() const override { return Joule::toString(); } +}; + +class Volt : public Base +{ + private: + Volt() {} + public: + Volt(Volt const&) = delete; + void operator=(Volt const&) = delete; + static Volt* + get() + { + static Volt instance; + return &instance; + } + static std::string toString() { return "Volt"; } + std::string getUnitString() const override { return Volt::toString(); } +}; + +class DegreeCelsius : public Base +{ + private: + DegreeCelsius() {} + public: + DegreeCelsius(DegreeCelsius const&) = delete; + void operator=(DegreeCelsius const&) = delete; + static DegreeCelsius* + get() + { + static DegreeCelsius instance; + return &instance; + } + static std::string toString() { return "Celsius"; } + std::string + getUnitString() const override + { + return DegreeCelsius::toString(); + } +}; + + +class Count : public Base +{ + private: + Count() {} + public: + Count(Count const&) = delete; + void operator=(Count const&) = delete; + static Count* + get() + { + static Count instance; + return &instance; + } + static std::string toString() { return "Count"; } + std::string getUnitString() const override { return Count::toString(); } +}; + +template +class Rate : public Base +{ + static_assert(std::is_base_of::value, + "Rate(T1,T2) must have T1 and T2 derived from" + "Stats::Units::Base"); + static_assert(std::is_base_of::value, + "Rate(T1,T2) must have T1 and T2 derived from" + "Stats::Units::Base"); + private: + Rate() {} + public: + Rate(Rate const&) = delete; + void operator=(Rate const&) = delete; + static Rate* + get() + { + static Rate instance; + return &instance; + } + static std::string + toString() + { + return csprintf("(%s/%s)", T1::toString(), T2::toString()); + } + std::string + getUnitString() const override + { + return Rate::toString(); + } +}; + +class Ratio : public Base +{ + private: + Ratio() {} + public: + Ratio(Ratio const&) = delete; + void operator=(Ratio const&) = delete; + static Ratio* + get() + { + static Ratio instance; + return &instance; + } + static std::string toString() { return "Ratio"; } + std::string getUnitString() const override { return Ratio::toString(); } +}; + +class Unspecified : public Base +{ + private: + Unspecified() {} + public: + Unspecified(Unspecified const&) = delete; + void operator=(Unspecified const&) = delete; + static Unspecified* + get() + { + static Unspecified instance; + return &instance; + } + static std::string toString() { return "Unspecified"; } + std::string + getUnitString() const override + { + return Unspecified::toString(); + } +}; + +} // namespace Units + +} // namespace Stats + +#endif // __BASE_STATS_UNITS_HH__ -- 2.30.2