Files
gem5/src/base/statistics.cc
Hoa Nguyen e331aa9718 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 <hoanguyen@ucdavis.edu>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/38855
Tested-by: kokoro <noreply+kokoro@google.com>
Maintainer: Bobby R. Bruce <bbruce@ucdavis.edu>
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com>
2021-02-10 09:03:09 +00:00

331 lines
7.1 KiB
C++

/*
* Copyright (c) 2019-2020 Arm Limited
* All rights reserved.
*
* The license below extends only to copyright in the software and shall
* not be construed as granting a license to any other intellectual
* property including but not limited to intellectual property relating
* to a hardware implementation of the functionality of the software
* licensed hereunder. You may use the software subject to the license
* terms below provided that you ensure that this notice is replicated
* unmodified and in its entirety in all distributions of the software,
* modified or unmodified, in source code or in binary form.
*
* 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
* 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.
*/
#include "base/statistics.hh"
#include <cassert>
#include <list>
#include <map>
#include <string>
#include <utility>
#include "base/callback.hh"
#include "base/logging.hh"
#include "sim/root.hh"
namespace Stats {
// We wrap these in a function to make sure they're built in time.
std::list<Info *> &
statsList()
{
static std::list<Info *> the_list;
return the_list;
}
MapType &
statsMap()
{
static MapType the_map;
return the_map;
}
void
InfoAccess::setInfo(Group *parent, Info *info)
{
panic_if(statsMap().find(this) != statsMap().end() ||
_info != nullptr,
"shouldn't register stat twice!");
// New-style stats are reachable through the hierarchy and
// shouldn't be added to the global lists.
if (parent) {
_info = info;
return;
}
statsList().push_back(info);
#ifndef NDEBUG
std::pair<MapType::iterator, bool> result =
#endif
statsMap().insert(std::make_pair(this, info));
assert(result.second && "this should never fail");
assert(statsMap().find(this) != statsMap().end());
}
void
InfoAccess::setParams(const StorageParams *params)
{
info()->storageParams = params;
}
void
InfoAccess::setInit()
{
info()->flags.set(init);
}
Info *
InfoAccess::info()
{
if (_info) {
// New-style stats
return _info;
} else {
// Legacy stats
MapType::const_iterator i = statsMap().find(this);
assert(i != statsMap().end());
return (*i).second;
}
}
const Info *
InfoAccess::info() const
{
if (_info) {
// New-style stats
return _info;
} else {
// Legacy stats
MapType::const_iterator i = statsMap().find(this);
assert(i != statsMap().end());
return (*i).second;
}
}
Formula::Formula(Group *parent, const char *name, const char *desc)
: DataWrapVec<Formula, FormulaInfoProxy>(parent, name, UNIT_UNSPECIFIED,
desc)
{
}
Formula::Formula(Group *parent, const char *name, const Units::Base *unit,
const char *desc)
: DataWrapVec<Formula, FormulaInfoProxy>(parent, name, unit, desc)
{
}
Formula::Formula(Group *parent, const char *name, const char *desc,
const Temp &r)
: DataWrapVec<Formula, FormulaInfoProxy>(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<Formula, FormulaInfoProxy>(parent, name, unit, desc)
{
*this = r;
}
const Formula &
Formula::operator=(const Temp &r)
{
assert(!root && "Can't change formulas");
root = r.getNodePtr();
setInit();
assert(size());
return *this;
}
const Formula &
Formula::operator+=(Temp r)
{
if (root)
root = NodePtr(new BinaryNode<std::plus<Result> >(root, r));
else {
root = r.getNodePtr();
setInit();
}
assert(size());
return *this;
}
const Formula &
Formula::operator/=(Temp r)
{
assert (root);
root = NodePtr(new BinaryNode<std::divides<Result> >(root, r));
assert(size());
return *this;
}
void
Formula::result(VResult &vec) const
{
if (root)
vec = root->result();
}
Result
Formula::total() const
{
return root ? root->total() : 0.0;
}
size_type
Formula::size() const
{
if (!root)
return 0;
else
return root->size();
}
void
Formula::reset()
{
}
bool
Formula::zero() const
{
VResult vec;
result(vec);
for (VResult::size_type i = 0; i < vec.size(); ++i)
if (vec[i] != 0.0)
return false;
return true;
}
std::string
Formula::str() const
{
return root ? root->str() : "";
}
Handler resetHandler = NULL;
Handler dumpHandler = NULL;
void
registerHandlers(Handler reset_handler, Handler dump_handler)
{
resetHandler = reset_handler;
dumpHandler = dump_handler;
}
CallbackQueue dumpQueue;
CallbackQueue resetQueue;
void
processResetQueue()
{
resetQueue.process();
}
void
processDumpQueue()
{
dumpQueue.process();
}
void
registerResetCallback(const std::function<void()> &callback)
{
resetQueue.push_back(callback);
}
bool _enabled = false;
bool
enabled()
{
return _enabled;
}
void
enable()
{
if (_enabled)
fatal("Stats are already enabled");
_enabled = true;
}
void
dump()
{
if (dumpHandler)
dumpHandler();
else
fatal("No registered Stats::dump handler");
}
void
reset()
{
if (resetHandler)
resetHandler();
else
fatal("No registered Stats::reset handler");
}
const Info *
resolve(const std::string &name)
{
const auto &it = nameMap().find(name);
if (it != nameMap().cend()) {
return it->second;
} else {
return Root::root()->resolveStat(name);
}
}
void
registerDumpCallback(const std::function<void()> &callback)
{
dumpQueue.push_back(callback);
}
} // namespace Stats
void
debugDumpStats()
{
Stats::dump();
}