gem5 [DEVELOP-FOR-25.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/trace.hh"
51#include "base/types.hh"
52#include "debug/EnteringEventQueue.hh"
53#include "sim/async.hh"
54#include "sim/eventq.hh"
55#include "sim/init_signals.hh"
56#include "sim/sim_events.hh"
57#include "sim/sim_exit.hh"
58#include "sim/stat_control.hh"
59
60namespace gem5
61{
62
65
67
69{
70 public:
71 SimulatorThreads() = delete;
74
75 SimulatorThreads(uint32_t num_queues)
76 : terminate(false),
77 numQueues(num_queues),
78 barrier(num_queues)
79 {
80 threads.reserve(num_queues);
81 }
82
84 {
85 // This should only happen after exit has been
86 // called. Subordinate event queues should normally (assuming
87 // exit is called from Python) be waiting on the barrier when
88 // this happens.
89 //
90 // N.B.: Not terminating here would make it impossible to
91 // safely destroy the barrier.
93 }
94
96 {
97 assert(!terminate);
98
99 // Start subordinate threads if needed.
100 if (threads.empty()) {
101 // the main thread (the one running Python) handles queue 0,
102 // so we only need to allocate new threads for queues 1..N-1.
103 // We'll call these the "subordinate" threads.
104 for (uint32_t i = 1; i < numQueues; i++) {
105 threads.emplace_back(
106 [this](EventQueue *eq) {
108 }, mainEventQueue[i]);
109 }
110 }
111
112 // This method is called from the main thread. All subordinate
113 // threads should be waiting on the barrier when the function
114 // is called. The arrival of the main thread here will satisfy
115 // the barrier and start another iteration in the thread loop.
116 barrier.wait();
117 }
118
119 void
121 {
122 assert(!terminate);
123 if (threads.empty())
124 return;
125
126 /* This function should only be called when the simulator is
127 * handling a global exit event (typically from Python). This
128 * means that the helper threads will be waiting on the
129 * barrier. Tell the helper threads to exit and release them from
130 * their barrier. */
131 terminate = true;
132 barrier.wait();
133
134 /* Wait for all of the threads to terminate */
135 for (auto &t : threads) {
136 t.join();
137 }
138
139 terminate = false;
140 threads.clear();
141 }
142
143 protected:
152 void
154 {
155 /* Wait for all initialisation to complete */
156 barrier.wait();
157
158 while (!terminate) {
159 doSimLoop(queue);
160 barrier.wait();
161 }
162 }
163
164 std::atomic<bool> terminate;
165 uint32_t numQueues;
168};
169
170static std::unique_ptr<SimulatorThreads> simulatorThreads;
171
173{
175 {
176 if (!event)
177 return;
178
179 event->deschedule();
180 delete event;
181 }
182};
183
191simulate(Tick num_cycles)
192{
193 // install the sigint handler to catch ctrl-c and exit the sim loop cleanly
194 // Note: This should be done before initializing the threads
195 initSigInt();
196 initSigCont();
197
198 if (global_exit_event)//cleaning last global exit event
199 global_exit_event->clean();
200 std::unique_ptr<GlobalSyncEvent, DescheduleDeleter> quantum_event;
201
202 DPRINTF(EnteringEventQueue, "Entering event queue @ %d. Starting "
203 "simulation...\n", curTick());
204
205 if (!simulatorThreads)
207
209 // If the simulate_limit_event is not set, we set it to MaxTick.
211 }
212
213 if (num_cycles != -1) {
214 // If the user has specified an exit event after X cycles, do so here.
215 // Note: This will override any prior set max_tick behaviour (such as
216 // that above when it is set to MAxTick).
217 const Tick max_tick = num_cycles < MaxTick - curTick() ?
218 curTick() + num_cycles : MaxTick;
219
220 // This is kept to `set_max_tick` instead of `schedule_tick_exit` to
221 // preserve backwards functionality. It may be better to deprecate this
222 // behaviour at some point in favor of `schedule_tick_exit`.
223 set_max_tick(max_tick);
224 }
225
226 if (numMainEventQueues > 1) {
227 fatal_if(simQuantum == 0,
228 "Quantum for multi-eventq simulation not specified");
229
230 quantum_event.reset(
233
234 inParallelMode = true;
235 }
236
237 simulatorThreads->runUntilLocalExit();
238 Event *local_event = doSimLoop(mainEventQueue[0]);
239 assert(local_event);
240
241 // Restore normal ctrl-c operation as soon as the event queue is done
243
244 inParallelMode = false;
245
246 // locate the global exit event and return it to Python
247 BaseGlobalEvent *global_event = local_event->globalEvent();
248 assert(global_event);
249
251 dynamic_cast<GlobalSimLoopExitEvent *>(global_event);
252 assert(global_exit_event);
253
254 return global_exit_event;
255}
256
258{
261 mainEventQueue[0]->getCurTick(),
262 "simulate() limit reached", 0);
263 }
264 simulate_limit_event->reschedule(tick);
265}
266
267
269{
271 /* If the GlobalSimLoopExitEvent has not been setup, the maximum tick
272 * is `MaxTick` as declared in "src/base/types.hh".
273 */
274 return MaxTick;
275 }
276
277 return simulate_limit_event->when();
278}
279
280void
282{
283 simulatorThreads->terminateThreads();
284}
285
286
292Event *
294{
295 // set the per thread current eventq pointer
296 curEventQueue(eventq);
297 eventq->handleAsyncInsertions();
298
299 bool mainQueue = eventq == getEventQueue(0);
300
301 while (1) {
302 // there should always be at least one event (the SimLoopExitEvent
303 // we just scheduled) in the queue
304 assert(!eventq->empty());
305 assert(curTick() <= eventq->nextTick() &&
306 "event scheduled in the past");
307
308 if (mainQueue && async_event) {
309 async_event = false;
310 // Take the event queue lock in case any of the service
311 // routines want to schedule new events.
312 std::lock_guard<EventQueue> lock(*eventq);
315 async_statdump = false;
316 async_statreset = false;
317 }
318
319 if (async_io) {
320 async_io = false;
321 pollQueue.service();
322 }
323
324 if (async_exit) {
325 async_exit = false;
326 exitSimLoop("user interrupt received");
327 }
328
329 if (async_exception) {
330 async_exception = false;
331 return NULL;
332 }
333 if (async_hypercall) {
334 async_hypercall = false;
336 }
337 }
338
339 Event *exit_event = eventq->serviceOne();
340 if (exit_event != NULL) {
341 return exit_event;
342 }
343 }
344
345 // not reached... only exit is return on SimLoopExitEvent
346}
347
348} // namespace gem5
This file defines flags used to handle asynchronous simulator events.
#define DPRINTF(x,...)
Definition trace.hh:209
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
Common base class for GlobalEvent and GlobalSyncEvent.
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:75
std::vector< std::thread > threads
Definition simulate.cc:166
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:153
std::atomic< bool > terminate
Definition simulate.cc:164
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:268
PollQueue pollQueue
Definition pollevent.cc:55
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 Arm Limited All rights reserved.
Definition binary32.hh:36
void processExternalSignal(void)
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:189
void set_max_tick(Tick tick)
Set the maximum tick.
Definition simulate.cc:257
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:66
Tick curTick()
The universal simulation clock.
Definition cur_tick.hh:46
static std::unique_ptr< SimulatorThreads > simulatorThreads
Definition simulate.cc:170
Tick get_max_tick()
Get the maximum simulation tick.
Definition simulate.cc:268
void exitSimLoop(const std::string &message, int exit_code, Tick when, Tick repeat, bool serialize)
The "old style" exitSimLoop functions.
uint64_t Tick
Tick count type.
Definition types.hh:58
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:281
volatile bool async_hypercall
Definition async.cc:38
GlobalSimLoopExitEvent * simulate(Tick num_cycles)
Definition simulate.cc:191
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
void initSigCont()
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:293
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:174

Generated on Mon May 26 2025 09:19:13 for gem5 by doxygen 1.13.2