gem5  v22.1.0.0
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
#define DPRINTF(x,...)
Definition: trace.hh:186
void setCurTick(Tick newVal)
Definition: eventq.hh:1080
Tick unserializedCurTick
Definition: globals.hh:63
EventFunctionWrapper syncEvent
Definition: root.hh:81
Tick _periodTick
Definition: root.hh:73
const Time timeSyncSpinThreshold() const
Retrieve the threshold for time remaining to spin wait.
Definition: root.hh:132
Time lastTime
Definition: root.hh:76
void serialize(CheckpointOut &cp) const override
Serialize an object.
Definition: root.cc:205
Globals globals
Definition: root.hh:78
bool timeSyncEnabled() const
Check whether time syncing is enabled.
Definition: root.hh:128
Time _spinThreshold
Definition: root.hh:74
void timeSync()
Definition: root.cc:122
Time _period
Definition: root.hh:72
void unserialize(CheckpointIn &cp) override
Unserialize an object.
Definition: root.cc:213
Root(const Params &p, int)
Definition: root.cc:178
void timeSyncEnable(bool en)
Enable or disable time syncing.
Definition: root.cc:143
const Time timeSyncPeriod() const
Retrieve the period for the sync event.
Definition: root.hh:130
void startup() override
Schedule the timesync event at startup().
Definition: root.cc:199
static Root * _root
Definition: root.hh:68
bool _enabled
Definition: root.hh:71
Abstract superclass for simulation objects.
Definition: sim_object.hh:148
Tick getTick() const
Get the current time from a value measured in Ticks.
Definition: time.cc:66
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
Derived & prereq(const Stat &prereq)
Set the prerequisite stat and marks this stat to print at the end of simulation.
Definition: statistics.hh:372
Derived & precision(int _precision)
Set the precision and marks this stat to print at the end of simulation.
Definition: statistics.hh:346
Statistics container.
Definition: group.hh:94
Derived & functor(const T &func)
Definition: statistics.hh:741
Derived & scalar(T &value)
Definition: statistics.hh:732
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
void deschedule(Event &event)
Definition: eventq.hh:1028
void schedule(Event &event, Tick when)
Definition: eventq.hh:1019
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
Definition: serialize.cc:74
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Definition: serialize.cc:81
const Params & params() const
Definition: sim_object.hh:176
virtual void resetStats()
Callback to reset stats.
Definition: group.cc:86
Bitfield< 30 > en
Definition: misc_types.hh:467
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 54 > p
Definition: pagetable.hh:70
Tick Frequency
The simulated frequency of curTick(). (In ticks per second)
Definition: core.cc:48
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
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
Tick simQuantum
Simulation Quantum for multiple eventq simulation.
Definition: eventq.cc:48
Root::RootStats & rootStats
Global simulator statistics that are not associated with a specific SimObject.
Definition: root.cc:57
statistics::Value & hostSeconds
Definition: stats.cc:48
uint64_t memUsage()
Determine the simulator process' total virtual memory usage.
Definition: hostinfo.cc:76
Tick curTick()
The universal simulation clock.
Definition: cur_tick.hh:46
std::ostream CheckpointOut
Definition: serialize.hh:66
bool FullSystem
The FullSystem variable can be used to determine the current mode of simulation.
Definition: root.cc:220
uint64_t Tick
Tick count type.
Definition: types.hh:58
void sleep(const Time &time)
Definition: time.cc:142
uint32_t numMainEventQueues
Current number of allocated main event queues.
Definition: eventq.cc:56
statistics::Value & simTicks
Definition: stats.cc:46
statistics::Formula & simSeconds
Definition: stats.cc:45
statistics::Value & simFreq
Definition: stats.cc:47
std::vector< EventQueue * > mainEventQueue
Array for main event queues.
Definition: eventq.cc:57
return remainder
Definition: scfx_rep.cc:2169
#define SERIALIZE_SCALAR(scalar)
Definition: serialize.hh:568
statistics::Value hostSeconds
Definition: root.hh:108
statistics::Formula simSeconds
Definition: root.hh:104
statistics::Value finalTick
Definition: root.hh:106
void resetStats() override
Callback to reset stats.
Definition: root.cc:107
statistics::Value simTicks
Definition: root.hh:105
statistics::Formula hostTickRate
Definition: root.hh:110
statistics::Value simFreq
Definition: root.hh:107
static RootStats instance
Definition: root.hh:113
statistics::Value hostMemory
Definition: root.hh:111
const std::string & name()
Definition: trace.cc:49

Generated on Wed Dec 21 2022 10:22:40 for gem5 by doxygen 1.9.1