gem5  v21.2.1.1
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 "debug/TimeSync.hh"
46 #include "sim/core.hh"
47 #include "sim/cur_tick.hh"
48 #include "sim/eventq.hh"
49 #include "sim/full_system.hh"
50 #include "sim/root.hh"
51 
52 namespace gem5
53 {
54 
55 Root *Root::_root = NULL;
56 Root::RootStats Root::RootStats::instance;
58 
60  : statistics::Group(nullptr),
61  ADD_STAT(simSeconds, statistics::units::Second::get(),
62  "Number of seconds simulated"),
63  ADD_STAT(simTicks, statistics::units::Tick::get(),
64  "Number of ticks simulated"),
65  ADD_STAT(finalTick, statistics::units::Tick::get(),
66  "Number of ticks from beginning of simulation "
67  "(restored from checkpoints and never reset)"),
68  ADD_STAT(simFreq, statistics::units::Rate<
69  statistics::units::Tick, statistics::units::Second>::get(),
70  "The number of ticks per simulated second"),
71  ADD_STAT(hostSeconds, statistics::units::Second::get(),
72  "Real time elapsed on the host"),
73  ADD_STAT(hostTickRate, statistics::units::Rate<
74  statistics::units::Tick, statistics::units::Second>::get(),
75  "The number of ticks simulated per host second (ticks/s)"),
76  ADD_STAT(hostMemory, statistics::units::Byte::get(),
77  "Number of bytes of host memory used"),
78 
79  statTime(true),
80  startTick(0)
81 {
83  simTicks.functor([this]() { return curTick() - startTick; });
85 
89  ;
90 
92  .functor([this]() {
93  Time now;
94  now.setTimer();
95  return now - statTime;
96  })
97  .precision(2)
98  ;
99 
101 
104 }
105 
106 void
108 {
109  statTime.setTimer();
110  startTick = curTick();
111 
113 }
114 
115 /*
116  * This function is called periodically by an event in M5 and ensures that
117  * at least as much real time has passed between invocations as simulated time.
118  * If not, the function either sleeps, or if the difference is small enough
119  * spin waits.
120  */
121 void
123 {
124  Time cur_time, diff, period = timeSyncPeriod();
125 
126  do {
127  cur_time.setTimer();
128  diff = cur_time - lastTime;
129  Time remainder = period - diff;
130  if (diff < period && remainder > _spinThreshold) {
131  DPRINTF(TimeSync, "Sleeping to sync with real time.\n");
132  // Sleep until the end of the period, or until a signal.
133  sleep(remainder);
134  // Refresh the current time.
135  cur_time.setTimer();
136  }
137  } while (diff < period);
138  lastTime = cur_time;
140 }
141 
142 void
144 {
145  if (en == _enabled)
146  return;
147  _enabled = en;
148  if (_enabled) {
149  // Get event going.
150  Tick periods = ((curTick() + _periodTick - 1) / _periodTick);
151  Tick nextPeriod = periods * _periodTick;
152  schedule(&syncEvent, nextPeriod);
153  } else {
154  // Stop event.
156  }
157 }
158 
160 void
162 {
163  bool en = timeSyncEnabled();
164  _period = newPeriod;
167 }
168 
170 void
172 {
173  bool en = timeSyncEnabled();
174  _spinThreshold = newThreshold;
176 }
177 
178 Root::Root(const RootParams &p, int)
179  : SimObject(p), _enabled(false), _periodTick(p.time_sync_period),
180  syncEvent([this]{ timeSync(); }, name())
181 {
182  _period.setTick(p.time_sync_period);
183  _spinThreshold.setTick(p.time_sync_spin_threshold);
184 
185  assert(_root == NULL);
186  _root = this;
187  lastTime.setTimer();
188 
189  simQuantum = p.sim_quantum;
190 
191  // Some of the statistics are global and need to be accessed by
192  // stat formulas. The most convenient way to implement that is by
193  // having a single global stat group for global stats. Merge that
194  // group into the root object here.
195  mergeStatGroup(&Root::RootStats::instance);
196 }
197 
198 void
200 {
201  timeSyncEnable(params().time_sync_enable);
202 }
203 
204 void
206 {
208 
209  globals.serializeSection(cp, "globals");
210 }
211 
212 void
214 {
215  globals.unserializeSection(cp, "globals");
216  for (uint32_t i = 0; i < numMainEventQueues; ++i)
218 }
219 
221 unsigned int FullSystemInt;
222 
223 Root *
224 RootParams::create() const
225 {
226  static bool created = false;
227  if (created)
228  panic("only one root object allowed!");
229 
230  created = true;
231 
232  FullSystem = full_system;
233  FullSystemInt = full_system ? 1 : 0;
234 
235  return new Root(*this, 0);
236 }
237 
238 } // namespace gem5
gem5::Root::timeSyncEnable
void timeSyncEnable(bool en)
Enable or disable time syncing.
Definition: root.cc:143
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:57
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:67
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:221
gem5::Root::_root
static Root * _root
Definition: root.hh:68
gem5::Root::RootStats::resetStats
void resetStats() override
Callback to reset stats.
Definition: root.cc:107
gem5::Root::unserialize
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: root.cc:213
gem5::Root::startup
void startup() override
Schedule the timesync event at startup().
Definition: root.cc:199
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:122
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:741
gem5::ArmISA::en
Bitfield< 30 > en
Definition: misc_types.hh:461
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:346
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:220
gem5::Root::globals
Globals globals
Definition: root.hh:78
gem5::Root::Root
Root(const Params &p, int)
Definition: root.cc:178
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:372
core.hh
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:732
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: tlb.cc:60
gem5::hostSeconds
statistics::Value & hostSeconds
Definition: stats.cc:48
gem5::Root::RootStats::RootStats
RootStats()
Definition: root.cc:59
gem5::Root::serialize
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: root.cc:205
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:178
eventq.hh

Generated on Wed May 4 2022 12:14:03 for gem5 by doxygen 1.8.17