diff --git a/src/sim/SConscript b/src/sim/SConscript index 6bda82859b..78d9bf535f 100644 --- a/src/sim/SConscript +++ b/src/sim/SConscript @@ -81,6 +81,7 @@ Source('clocked_object.cc') Source('mathexpr.cc') Source('power_state.cc') Source('power_domain.cc') +Source('stats.cc') GTest('byteswap.test', 'byteswap.test.cc', '../base/types.cc') GTest('guest_abi.test', 'guest_abi.test.cc') diff --git a/src/sim/root.cc b/src/sim/root.cc index 5a174424e3..e1c6a7b6ff 100644 --- a/src/sim/root.cc +++ b/src/sim/root.cc @@ -1,4 +1,16 @@ /* + * Copyright (c) 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) 2002-2005 The Regents of The University of Michigan * Copyright (c) 2011 Advanced Micro Devices, Inc. * All rights reserved. @@ -27,6 +39,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "base/hostinfo.hh" #include "base/logging.hh" #include "base/trace.hh" #include "config/the_isa.hh" @@ -36,6 +49,56 @@ #include "sim/root.hh" Root *Root::_root = NULL; +Root::Stats Root::Stats::instance; +Root::Stats &rootStats = Root::Stats::instance; + +Root::Stats::Stats() + : Stats::Group(nullptr), + simSeconds(this, "sim_seconds", "Number of seconds simulated"), + simTicks(this, "sim_ticks", "Number of ticks simulated"), + finalTick(this, "final_tick", + "Number of ticks from beginning of simulation " + "(restored from checkpoints and never reset)"), + simFreq(this, "sim_freq", "Frequency of simulated ticks"), + hostSeconds(this, "host_seconds", "Real time elapsed on the host"), + hostTickRate(this, "host_tick_rate", "Simulator tick rate (ticks/s)"), + hostMemory(this, "host_mem_usage", "Number of bytes of host memory used"), + + statTime(true), + startTick(0) +{ + simFreq.scalar(SimClock::Frequency); + simTicks.functor([this]() { return curTick() - startTick; }); + finalTick.functor(curTick); + + hostMemory + .functor(memUsage) + .prereq(hostMemory) + ; + + hostSeconds + .functor([this]() { + Time now; + now.setTimer(); + return now - statTime; + }) + .precision(2) + ; + + hostTickRate.precision(0); + + simSeconds = simTicks / simFreq; + hostTickRate = simTicks / hostSeconds; +} + +void +Root::Stats::resetStats() +{ + statTime.setTimer(); + startTick = curTick(); + + Stats::Group::resetStats(); +} /* * This function is called periodically by an event in M5 and ensures that @@ -112,6 +175,12 @@ Root::Root(RootParams *p) lastTime.setTimer(); simQuantum = p->sim_quantum; + + // Some of the statistics are global and need to be accessed by + // stat formulas. The most convenient way to implement that is by + // having a single global stat group for global stats. Merge that + // group into the root object here. + mergeStatGroup(&Root::Stats::instance); } void diff --git a/src/sim/root.hh b/src/sim/root.hh index 0638559a72..a88673a7b7 100644 --- a/src/sim/root.hh +++ b/src/sim/root.hh @@ -1,4 +1,16 @@ /* + * Copyright (c) 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) 2011 Advanced Micro Devices, Inc. * All rights reserved. * @@ -39,6 +51,7 @@ #ifndef __SIM_ROOT_HH__ #define __SIM_ROOT_HH__ +#include "base/statistics.hh" #include "base/time.hh" #include "params/Root.hh" #include "sim/eventq.hh" @@ -76,6 +89,32 @@ class Root : public SimObject return _root; } + public: // Global statistics + struct Stats : public ::Stats::Group + { + void resetStats() override; + + ::Stats::Formula simSeconds; + ::Stats::Value simTicks; + ::Stats::Value finalTick; + ::Stats::Value simFreq; + ::Stats::Value hostSeconds; + + ::Stats::Formula hostTickRate; + ::Stats::Value hostMemory; + + static Stats instance; + + private: + Stats(); + + Stats(const Stats &) = delete; + Stats &operator=(const Stats &) = delete; + + Time statTime; + Tick startTick; + }; + public: /// Check whether time syncing is enabled. @@ -108,4 +147,10 @@ class Root : public SimObject void serialize(CheckpointOut &cp) const override; }; +/** + * Global simulator statistics that are not associated with a + * specific SimObject. + */ +extern Root::Stats &rootStats; + #endif // __SIM_ROOT_HH__ diff --git a/src/sim/stat_control.cc b/src/sim/stat_control.cc index 5b667860b0..8189bfe349 100644 --- a/src/sim/stat_control.cc +++ b/src/sim/stat_control.cc @@ -50,115 +50,19 @@ #include #include "base/callback.hh" -#include "base/hostinfo.hh" #include "base/statistics.hh" #include "base/time.hh" #include "sim/global_event.hh" using namespace std; -Stats::Formula simSeconds; -Stats::Value simTicks; -Stats::Value finalTick; -Stats::Value simFreq; -Stats::Value hostSeconds; - namespace Stats { -Time statTime(true); -Tick startTick; - GlobalEvent *dumpEvent; -double -statElapsedTime() -{ - Time now; - now.setTimer(); - - Time elapsed = now - statTime; - return elapsed; -} - -Tick -statElapsedTicks() -{ - return curTick() - startTick; -} - -Tick -statFinalTick() -{ - return curTick(); -} - -struct Global -{ - Stats::Formula hostTickRate; - Stats::Value hostMemory; - - Global(); -}; - -Global::Global() -{ - simSeconds - .name("sim_seconds") - .desc("Number of seconds simulated") - ; - - simFreq - .scalar(SimClock::Frequency) - .name("sim_freq") - .desc("Frequency of simulated ticks") - ; - - simTicks - .functor(statElapsedTicks) - .name("sim_ticks") - .desc("Number of ticks simulated") - ; - - finalTick - .functor(statFinalTick) - .name("final_tick") - .desc("Number of ticks from beginning of simulation " - "(restored from checkpoints and never reset)") - ; - - hostMemory - .functor(memUsage) - .name("host_mem_usage") - .desc("Number of bytes of host memory used") - .prereq(hostMemory) - ; - - hostSeconds - .functor(statElapsedTime) - .name("host_seconds") - .desc("Real time elapsed on the host") - .precision(2) - ; - - hostTickRate - .name("host_tick_rate") - .desc("Simulator tick rate (ticks/s)") - .precision(0) - ; - - simSeconds = simTicks / simFreq; - hostTickRate = simTicks / hostSeconds; - - registerResetCallback([]() { - statTime.setTimer(); - startTick = curTick(); - }); -} - void initSimStats() { - static Global global; } /** diff --git a/src/sim/stat_control.hh b/src/sim/stat_control.hh index 3b869ad4f1..55930b4561 100644 --- a/src/sim/stat_control.hh +++ b/src/sim/stat_control.hh @@ -46,11 +46,6 @@ namespace Stats { -double statElapsedTime(); - -Tick statElapsedTicks(); - -Tick statFinalTick(); void initSimStats(); diff --git a/src/sim/stats.cc b/src/sim/stats.cc new file mode 100644 index 0000000000..b5a3ca18e9 --- /dev/null +++ b/src/sim/stats.cc @@ -0,0 +1,45 @@ +/* + * Copyright (c) 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. + * + * 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 "sim/stats.hh" + +#include "sim/root.hh" + +Stats::Formula &simSeconds = rootStats.simSeconds; +Stats::Value &simTicks = rootStats.simTicks; +Stats::Value &simFreq = rootStats.simFreq; +Stats::Value &hostSeconds = rootStats.hostSeconds; diff --git a/src/sim/stats.hh b/src/sim/stats.hh index 4e17f64f06..a46539e27d 100644 --- a/src/sim/stats.hh +++ b/src/sim/stats.hh @@ -31,9 +31,9 @@ #include "base/statistics.hh" -extern Stats::Formula simSeconds; -extern Stats::Value simTicks; -extern Stats::Value simFreq; -extern Stats::Value hostSeconds; +extern Stats::Formula &simSeconds; +extern Stats::Value &simTicks; +extern Stats::Value &simFreq; +extern Stats::Value &hostSeconds; #endif // __SIM_SIM_STATS_HH__