gem5 v24.0.0.0
Loading...
Searching...
No Matches
simulate.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 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) 2006 The Regents of The University of Michigan
15 * Copyright (c) 2013 Advanced Micro Devices, Inc.
16 * Copyright (c) 2013 Mark D. Hill and David A. Wood
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer;
23 * redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution;
26 * neither the name of the copyright holders nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43#include "sim/simulate.hh"
44
45#include <atomic>
46#include <thread>
47
48#include "base/logging.hh"
49#include "base/pollevent.hh"
50#include "base/types.hh"
51#include "sim/async.hh"
52#include "sim/eventq.hh"
53#include "sim/init_signals.hh"
54#include "sim/sim_events.hh"
55#include "sim/sim_exit.hh"
56#include "sim/stat_control.hh"
57
58namespace gem5
59{
60
62Event *doSimLoop(EventQueue *);
63
65
67{
68 public:
69 SimulatorThreads() = delete;
72
73 SimulatorThreads(uint32_t num_queues)
74 : terminate(false),
75 numQueues(num_queues),
76 barrier(num_queues)
77 {
78 threads.reserve(num_queues);
79 }
80
82 {
83 // This should only happen after exit has been
84 // called. Subordinate event queues should normally (assuming
85 // exit is called from Python) be waiting on the barrier when
86 // this happens.
87 //
88 // N.B.: Not terminating here would make it impossible to
89 // safely destroy the barrier.
91 }
92
94 {
95 assert(!terminate);
96
97 // Start subordinate threads if needed.
98 if (threads.empty()) {
99 // the main thread (the one running Python) handles queue 0,
100 // so we only need to allocate new threads for queues 1..N-1.
101 // We'll call these the "subordinate" threads.
102 for (uint32_t i = 1; i < numQueues; i++) {
103 threads.emplace_back(
104 [this](EventQueue *eq) {
106 }, mainEventQueue[i]);
107 }
108 }
109
110 // This method is called from the main thread. All subordinate
111 // threads should be waiting on the barrier when the function
112 // is called. The arrival of the main thread here will satisfy
113 // the barrier and start another iteration in the thread loop.
114 barrier.wait();
115 }
116
117 void
119 {
120 assert(!terminate);
121 if (threads.empty())
122 return;
123
124 /* This function should only be called when the simulator is
125 * handling a global exit event (typically from Python). This
126 * means that the helper threads will be waiting on the
127 * barrier. Tell the helper threads to exit and release them from
128 * their barrier. */
129 terminate = true;
130 barrier.wait();
131
132 /* Wait for all of the threads to terminate */
133 for (auto &t : threads) {
134 t.join();
135 }
136
137 terminate = false;
138 threads.clear();
139 }
140
141 protected:
150 void
152 {
153 /* Wait for all initialisation to complete */
154 barrier.wait();
155
156 while (!terminate) {
157 doSimLoop(queue);
158 barrier.wait();
159 }
160 }
161
162 std::atomic<bool> terminate;
163 uint32_t numQueues;
166};
167
168static std::unique_ptr<SimulatorThreads> simulatorThreads;
169
171{
173 {
174 if (!event)
175 return;
176
177 event->deschedule();
178 delete event;
179 }
180};
181
189simulate(Tick num_cycles)
190{
191 // install the sigint handler to catch ctrl-c and exit the sim loop cleanly
192 // Note: This should be done before initializing the threads
193 initSigInt();
194
195 if (global_exit_event)//cleaning last global exit event
197 std::unique_ptr<GlobalSyncEvent, DescheduleDeleter> quantum_event;
198
199 inform("Entering event queue @ %d. Starting simulation...\n", curTick());
200
201 if (!simulatorThreads)
203
205 // If the simulate_limit_event is not set, we set it to MaxTick.
207 }
208
209 if (num_cycles != -1) {
210 // If the user has specified an exit event after X cycles, do so here.
211 // Note: This will override any prior set max_tick behaviour (such as
212 // that above when it is set to MAxTick).
213 const Tick max_tick = num_cycles < MaxTick - curTick() ?
214 curTick() + num_cycles : MaxTick;
215
216 // This is kept to `set_max_tick` instead of `schedule_tick_exit` to
217 // preserve backwards functionality. It may be better to deprecate this
218 // behaviour at some point in favor of `schedule_tick_exit`.
219 set_max_tick(max_tick);
220 }
221
222 if (numMainEventQueues > 1) {
223 fatal_if(simQuantum == 0,
224 "Quantum for multi-eventq simulation not specified");
225
226 quantum_event.reset(
229
230 inParallelMode = true;
231 }
232
233 simulatorThreads->runUntilLocalExit();
234 Event *local_event = doSimLoop(mainEventQueue[0]);
235 assert(local_event);
236
237 // Restore normal ctrl-c operation as soon as the event queue is done
239
240 inParallelMode = false;
241
242 // locate the global exit event and return it to Python
243 BaseGlobalEvent *global_event = local_event->globalEvent();
244 assert(global_event);
245
247 dynamic_cast<GlobalSimLoopExitEvent *>(global_event);
248 assert(global_exit_event);
249
250 return global_exit_event;
251}
252
254{
257 mainEventQueue[0]->getCurTick(),
258 "simulate() limit reached", 0);
259 }
261}
262
263
265{
267 /* If the GlobalSimLoopExitEvent has not been setup, the maximum tick
268 * is `MaxTick` as declared in "src/base/types.hh".
269 */
270 return MaxTick;
271 }
272
273 return simulate_limit_event->when();
274}
275
276void
278{
279 simulatorThreads->terminateThreads();
280}
281
282
288Event *
290{
291 // set the per thread current eventq pointer
292 curEventQueue(eventq);
293 eventq->handleAsyncInsertions();
294
295 bool mainQueue = eventq == getEventQueue(0);
296
297 while (1) {
298 // there should always be at least one event (the SimLoopExitEvent
299 // we just scheduled) in the queue
300 assert(!eventq->empty());
301 assert(curTick() <= eventq->nextTick() &&
302 "event scheduled in the past");
303
304 if (mainQueue && async_event) {
305 async_event = false;
306 // Take the event queue lock in case any of the service
307 // routines want to schedule new events.
308 std::lock_guard<EventQueue> lock(*eventq);
311 async_statdump = false;
312 async_statreset = false;
313 }
314
315 if (async_io) {
316 async_io = false;
318 }
319
320 if (async_exit) {
321 async_exit = false;
322 exitSimLoop("user interrupt received");
323 }
324
325 if (async_exception) {
326 async_exception = false;
327 return NULL;
328 }
329 }
330
331 Event *exit_event = eventq->serviceOne();
332 if (exit_event != NULL) {
333 return exit_event;
334 }
335 }
336
337 // not reached... only exit is return on SimLoopExitEvent
338}
339
340} // namespace gem5
This file defines flags used to handle asynchronous simulator events.
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
bool wait()
Definition barrier.hh:66
Common base class for GlobalEvent and GlobalSyncEvent.
void reschedule(Tick when)
Queue of events sorted in time order.
Definition eventq.hh:616
Event * serviceOne()
Definition eventq.cc:224
Tick nextTick() const
Definition eventq.hh:836
void handleAsyncInsertions()
Function for moving events from the async_queue to the main queue.
Definition eventq.cc:458
virtual BaseGlobalEvent * globalEvent()
If this is part of a GlobalEvent, return the pointer to the Global Event.
Definition eventq.hh:513
A special global event that synchronizes all threads and forces them to process asynchronously enqueu...
SimulatorThreads(uint32_t num_queues)
Definition simulate.cc:73
std::vector< std::thread > threads
Definition simulate.cc:164
SimulatorThreads & operator=(SimulatorThreads &)=delete
void thread_main(EventQueue *queue)
The main function for all subordinate threads (i.e., all threads other than the main thread).
Definition simulate.cc:151
std::atomic< bool > terminate
Definition simulate.cc:162
SimulatorThreads(const SimulatorThreads &)=delete
STL vector class.
Definition stl.hh:37
static const Priority Progress_Event_Pri
Progress events come at the end.
Definition eventq.hh:229
bool empty() const
Returns true if no events are queued.
Definition eventq.hh:891
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
PollQueue pollQueue
Definition pollevent.cc:55
#define inform(...)
Definition logging.hh:257
Bitfield< 5 > t
Definition misc_types.hh:71
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 10, 5 > event
Bitfield< 29 > eq
Definition misc.hh:58
Bitfield< 5 > lock
Definition types.hh:82
void schedStatEvent(bool dump, bool reset, Tick when, Tick repeat)
Schedule statistics dumping.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Tick simQuantum
Simulation Quantum for multiple eventq simulation.
Definition eventq.cc:48
GlobalSimLoopExitEvent * global_exit_event
Simulate for num_cycles additional cycles.
Definition simulate.cc:187
void set_max_tick(Tick tick)
Set the maximum tick.
Definition simulate.cc:253
volatile bool async_event
Some asynchronous event has happened.
Definition async.cc:32
volatile bool async_statdump
Async request to dump stats.
Definition async.cc:33
void restoreSigInt()
GlobalSimLoopExitEvent * simulate_limit_event
Definition simulate.cc:64
Tick curTick()
The universal simulation clock.
Definition cur_tick.hh:46
static std::unique_ptr< SimulatorThreads > simulatorThreads
Definition simulate.cc:168
Tick get_max_tick()
Get the maximum simulation tick.
Definition simulate.cc:264
uint64_t Tick
Tick count type.
Definition types.hh:58
void exitSimLoop(const std::string &message, int exit_code, Tick when, Tick repeat, bool serialize)
Schedule an event to exit the simulation loop (returning to Python) at the end of the current cycle (...
Definition sim_events.cc:88
const Tick MaxTick
Definition types.hh:60
uint32_t numMainEventQueues
Current number of allocated main event queues.
Definition eventq.cc:56
void terminateEventQueueThreads()
Terminate helper threads when running in parallel mode.
Definition simulate.cc:277
GlobalSimLoopExitEvent * simulate(Tick num_cycles)
Definition simulate.cc:189
volatile bool async_io
Async I/O request (SIGIO).
Definition async.cc:36
volatile bool async_exit
Async request to exit simulator.
Definition async.cc:35
volatile bool async_statreset
Async request to reset stats.
Definition async.cc:34
EventQueue * curEventQueue()
Definition eventq.hh:91
bool inParallelMode
Current mode of execution: parallel / serial.
Definition eventq.cc:59
void initSigInt()
Event * doSimLoop(EventQueue *)
forward declaration
Definition simulate.cc:289
EventQueue * getEventQueue(uint32_t index)
Function for returning eventq queue for the provided index.
Definition eventq.cc:62
volatile bool async_exception
Python exception.
Definition async.cc:37
std::vector< EventQueue * > mainEventQueue
Array for main event queues.
Definition eventq.cc:57
void operator()(BaseGlobalEvent *event)
Definition simulate.cc:172

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0