gem5  v21.1.0.2
root.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2002-2005 The Regents of The University of Michigan
15  * Copyright (c) 2011 Advanced Micro Devices, Inc.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions are
20  * met: redistributions of source code must retain the above copyright
21  * notice, this list of conditions and the following disclaimer;
22  * redistributions in binary form must reproduce the above copyright
23  * notice, this list of conditions and the following disclaimer in the
24  * documentation and/or other materials provided with the distribution;
25  * neither the name of the copyright holders nor the names of its
26  * contributors may be used to endorse or promote products derived from
27  * this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include "base/hostinfo.hh"
43 #include "base/logging.hh"
44 #include "base/trace.hh"
45 #include "config/the_isa.hh"
46 #include "debug/TimeSync.hh"
47 #include "sim/core.hh"
48 #include "sim/cur_tick.hh"
49 #include "sim/eventq.hh"
50 #include "sim/full_system.hh"
51 #include "sim/root.hh"
52 
53 namespace gem5
54 {
55 
56 Root *Root::_root = NULL;
57 Root::RootStats Root::RootStats::instance;
59 
61  : statistics::Group(nullptr),
62  ADD_STAT(simSeconds, statistics::units::Second::get(),
63  "Number of seconds simulated"),
64  ADD_STAT(simTicks, statistics::units::Tick::get(),
65  "Number of ticks simulated"),
66  ADD_STAT(finalTick, statistics::units::Tick::get(),
67  "Number of ticks from beginning of simulation "
68  "(restored from checkpoints and never reset)"),
69  ADD_STAT(simFreq, statistics::units::Rate<
70  statistics::units::Tick, statistics::units::Second>::get(),
71  "The number of ticks per simulated second"),
72  ADD_STAT(hostSeconds, statistics::units::Second::get(),
73  "Real time elapsed on the host"),
74  ADD_STAT(hostTickRate, statistics::units::Rate<
75  statistics::units::Tick, statistics::units::Second>::get(),
76  "The number of ticks simulated per host second (ticks/s)"),
77  ADD_STAT(hostMemory, statistics::units::Byte::get(),
78  "Number of bytes of host memory used"),
79 
80  statTime(true),
81  startTick(0)
82 {
84  simTicks.functor([this]() { return curTick() - startTick; });
86 
90  ;
91 
93  .functor([this]() {
94  Time now;
95  now.setTimer();
96  return now - statTime;
97  })
98  .precision(2)
99  ;
100 
102 
105 }
106 
107 void
109 {
110  statTime.setTimer();
111  startTick = curTick();
112 
114 }
115 
116 /*
117  * This function is called periodically by an event in M5 and ensures that
118  * at least as much real time has passed between invocations as simulated time.
119  * If not, the function either sleeps, or if the difference is small enough
120  * spin waits.
121  */
122 void
124 {
125  Time cur_time, diff, period = timeSyncPeriod();
126 
127  do {
128  cur_time.setTimer();
129  diff = cur_time - lastTime;
130  Time remainder = period - diff;
131  if (diff < period && remainder > _spinThreshold) {
132  DPRINTF(TimeSync, "Sleeping to sync with real time.\n");
133  // Sleep until the end of the period, or until a signal.
134  sleep(remainder);
135  // Refresh the current time.
136  cur_time.setTimer();
137  }
138  } while (diff < period);
139  lastTime = cur_time;
141 }
142 
143 void
145 {
146  if (en == _enabled)
147  return;
148  _enabled = en;
149  if (_enabled) {
150  // Get event going.
151  Tick periods = ((curTick() + _periodTick - 1) / _periodTick);
152  Tick nextPeriod = periods * _periodTick;
153  schedule(&syncEvent, nextPeriod);
154  } else {
155  // Stop event.
157  }
158 }
159 
161 void
163 {
164  bool en = timeSyncEnabled();
165  _period = newPeriod;
168 }
169 
171 void
173 {
174  bool en = timeSyncEnabled();
175  _spinThreshold = newThreshold;
177 }
178 
179 Root::Root(const RootParams &p, int)
180  : SimObject(p), _enabled(false), _periodTick(p.time_sync_period),
181  syncEvent([this]{ timeSync(); }, name())
182 {
183  _period.setTick(p.time_sync_period);
184  _spinThreshold.setTick(p.time_sync_spin_threshold);
185 
186  assert(_root == NULL);
187  _root = this;
188  lastTime.setTimer();
189 
190  simQuantum = p.sim_quantum;
191 
192  // Some of the statistics are global and need to be accessed by
193  // stat formulas. The most convenient way to implement that is by
194  // having a single global stat group for global stats. Merge that
195  // group into the root object here.
196  mergeStatGroup(&Root::RootStats::instance);
197 }
198 
199 void
201 {
202  timeSyncEnable(params().time_sync_enable);
203 }
204 
205 void
207 {
209  std::string isa = THE_ISA_STR;
210  SERIALIZE_SCALAR(isa);
211 
212  globals.serializeSection(cp, "globals");
213 }
214 
215 void
217 {
218  globals.unserializeSection(cp, "globals");
219  for (uint32_t i = 0; i < numMainEventQueues; ++i)
221 }
222 
224 unsigned int FullSystemInt;
225 
226 Root *
227 RootParams::create() const
228 {
229  static bool created = false;
230  if (created)
231  panic("only one root object allowed!");
232 
233  created = true;
234 
235  FullSystem = full_system;
236  FullSystemInt = full_system ? 1 : 0;
237 
238  return new Root(*this, 0);
239 }
240 
241 } // namespace gem5
gem5::Root::timeSyncEnable
void timeSyncEnable(bool en)
Enable or disable time syncing.
Definition: root.cc:144
gem5::curTick
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
gem5::Root::RootStats::hostTickRate
statistics::Formula hostTickRate
Definition: root.hh:110
gem5::Root::timeSyncPeriod
const Time timeSyncPeriod() const
Retrieve the period for the sync event.
Definition: root.hh:130
gem5::Root::RootStats
Definition: root.hh:100
gem5::simFreq
statistics::Value & simFreq
Definition: stats.cc:47
gem5::Root::RootStats::statTime
Time statTime
Definition: root.hh:121
gem5::mainEventQueue
std::vector< EventQueue * > mainEventQueue
Array for main event queues.
Definition: eventq.cc:57
gem5::Root::_periodTick
Tick _periodTick
Definition: root.hh:73
gem5::sleep
void sleep(const Time &time)
Definition: time.cc:142
gem5::Root::_spinThreshold
Time _spinThreshold
Definition: root.hh:74
gem5::CheckpointIn
Definition: serialize.hh:68
gem5::rootStats
Root::RootStats & rootStats
Global simulator statistics that are not associated with a specific SimObject.
Definition: root.cc:58
gem5::Serializable::serializeSection
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition: serialize.cc:74
cur_tick.hh
gem5::sim_clock::Frequency
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition: core.cc:48
gem5::Root::timeSyncEnabled
bool timeSyncEnabled() const
Check whether time syncing is enabled.
Definition: root.hh:128
gem5::EventManager::schedule
void schedule(Event &event, Tick when)
Definition: eventq.hh:1019
sc_dt::remainder
return remainder
Definition: scfx_rep.cc:2201
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::statistics::Group::resetStats
virtual void resetStats()
Callback to reset stats.
Definition: group.cc:86
gem5::Root::RootStats::hostSeconds
statistics::Value hostSeconds
Definition: root.hh:108
gem5::Time::getTick
Tick getTick() const
Get the current time from a value measured in Ticks.
Definition: time.cc:66
gem5::Root::_enabled
bool _enabled
Definition: root.hh:71
root.hh
gem5::Root::RootStats::simTicks
statistics::Value simTicks
Definition: root.hh:105
gem5::Root::RootStats::hostMemory
statistics::Value hostMemory
Definition: root.hh:111
gem5::FullSystemInt
unsigned int FullSystemInt
In addition to the boolean flag we make use of an unsigned int since the CPU instruction decoder make...
Definition: root.cc:224
gem5::Root::_root
static Root * _root
Definition: root.hh:68
gem5::Root::RootStats::resetStats
void resetStats() override
Callback to reset stats.
Definition: root.cc:108
gem5::Root::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: root.cc:216
gem5::Root::startup
void startup() override
Schedule the timesync event at startup().
Definition: root.cc:200
gem5::Root::RootStats::instance
static RootStats instance
Definition: root.hh:113
gem5::EventManager::setCurTick
void setCurTick(Tick newVal)
Definition: eventq.hh:1080
gem5::Serializable::unserializeSection
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition: serialize.cc:81
gem5::SimObject::params
const Params & params() const
Definition: sim_object.hh:176
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
gem5::Globals::unserializedCurTick
Tick unserializedCurTick
Definition: globals.hh:63
gem5::memUsage
uint64_t memUsage()
Determine the simulator process' total virtual memory usage.
Definition: hostinfo.cc:76
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::Tick
uint64_t Tick
Tick count type.
Definition: types.hh:58
hostinfo.hh
gem5::Root
Definition: root.hh:65
gem5::Time::setTimer
void setTimer()
Use this to set time for the purposes of time measurement (use a monotonic clock if it is available.
Definition: time.hh:93
gem5::Root::timeSync
void timeSync()
Definition: root.cc:123
gem5::Root::_period
Time _period
Definition: root.hh:72
gem5::simQuantum
Tick simQuantum
Simulation Quantum for multiple eventq simulation.
Definition: eventq.cc:48
gem5::Time
Definition: time.hh:48
gem5::Root::RootStats::finalTick
statistics::Value finalTick
Definition: root.hh:106
gem5::statistics::ValueBase::functor
Derived & functor(const T &func)
Definition: statistics.hh:738
gem5::ArmISA::en
Bitfield< 30 > en
Definition: misc_types.hh:460
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::Root::timeSyncSpinThreshold
const Time timeSyncSpinThreshold() const
Retrieve the threshold for time remaining to spin wait.
Definition: root.hh:132
gem5::statistics::DataWrap::precision
Derived & precision(int _precision)
Set the precision and marks this stat to print at the end of simulation.
Definition: statistics.hh:343
core.hh
name
const std::string & name()
Definition: trace.cc:49
SERIALIZE_SCALAR
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:568
gem5::Root::syncEvent
EventFunctionWrapper syncEvent
Definition: root.hh:81
gem5::EventManager::deschedule
void deschedule(Event &event)
Definition: eventq.hh:1028
full_system.hh
gem5::FullSystem
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition: root.cc:223
gem5::Root::globals
Globals globals
Definition: root.hh:78
gem5::Root::Root
Root(const Params &p, int)
Definition: root.cc:179
gem5::Root::RootStats::simSeconds
statistics::Formula simSeconds
Definition: root.hh:104
gem5::Root::RootStats::simFreq
statistics::Value simFreq
Definition: root.hh:107
gem5::statistics::DataWrap::prereq
Derived & prereq(const Stat &prereq)
Set the prerequisite stat and marks this stat to print at the end of simulation.
Definition: statistics.hh:369
gem5::numMainEventQueues
uint32_t numMainEventQueues
Current number of allocated main event queues.
Definition: eventq.cc:56
gem5::simSeconds
statistics::Formula & simSeconds
Definition: stats.cc:45
logging.hh
gem5::statistics::Group
Statistics container.
Definition: group.hh:93
gem5::statistics::ValueBase::scalar
Derived & scalar(T &value)
Definition: statistics.hh:729
gem5::CheckpointOut
std::ostream CheckpointOut
Definition: serialize.hh:66
gem5::Root::lastTime
Time lastTime
Definition: root.hh:76
trace.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::hostSeconds
statistics::Value & hostSeconds
Definition: stats.cc:48
gem5::Root::RootStats::RootStats
RootStats()
Definition: root.cc:60
gem5::Root::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: root.cc:206
gem5::Root::RootStats::startTick
Tick startTick
Definition: root.hh:122
gem5::simTicks
statistics::Value & simTicks
Definition: stats.cc:46
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177
eventq.hh

Generated on Tue Sep 21 2021 12:25:48 for gem5 by doxygen 1.8.17