simulate.cc (10756:f9c0692f73ec) simulate.cc (10762:fe0972727902)
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * Copyright (c) 2013 Mark D. Hill and David A. Wood
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Authors: Nathan Binkert
31 * Steve Reinhardt
32 */
33
34#include <mutex>
35#include <thread>
36
37#include "base/misc.hh"
38#include "base/pollevent.hh"
39#include "base/types.hh"
40#include "sim/async.hh"
41#include "sim/eventq_impl.hh"
42#include "sim/sim_events.hh"
43#include "sim/sim_exit.hh"
44#include "sim/simulate.hh"
45#include "sim/stat_control.hh"
46
47//! Mutex for handling async events.
48std::mutex asyncEventMutex;
49
50//! Global barrier for synchronizing threads entering/exiting the
51//! simulation loop.
52Barrier *threadBarrier;
53
54//! forward declaration
55Event *doSimLoop(EventQueue *);
56
57/**
58 * The main function for all subordinate threads (i.e., all threads
59 * other than the main thread). These threads start by waiting on
60 * threadBarrier. Once all threads have arrived at threadBarrier,
61 * they enter the simulation loop concurrently. When they exit the
62 * loop, they return to waiting on threadBarrier. This process is
63 * repeated until the simulation terminates.
64 */
65static void
66thread_loop(EventQueue *queue)
67{
68 while (true) {
69 threadBarrier->wait();
70 doSimLoop(queue);
71 }
72}
73
1/*
2 * Copyright (c) 2006 The Regents of The University of Michigan
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * Copyright (c) 2013 Mark D. Hill and David A. Wood
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met: redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer;
11 * redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution;
14 * neither the name of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Authors: Nathan Binkert
31 * Steve Reinhardt
32 */
33
34#include <mutex>
35#include <thread>
36
37#include "base/misc.hh"
38#include "base/pollevent.hh"
39#include "base/types.hh"
40#include "sim/async.hh"
41#include "sim/eventq_impl.hh"
42#include "sim/sim_events.hh"
43#include "sim/sim_exit.hh"
44#include "sim/simulate.hh"
45#include "sim/stat_control.hh"
46
47//! Mutex for handling async events.
48std::mutex asyncEventMutex;
49
50//! Global barrier for synchronizing threads entering/exiting the
51//! simulation loop.
52Barrier *threadBarrier;
53
54//! forward declaration
55Event *doSimLoop(EventQueue *);
56
57/**
58 * The main function for all subordinate threads (i.e., all threads
59 * other than the main thread). These threads start by waiting on
60 * threadBarrier. Once all threads have arrived at threadBarrier,
61 * they enter the simulation loop concurrently. When they exit the
62 * loop, they return to waiting on threadBarrier. This process is
63 * repeated until the simulation terminates.
64 */
65static void
66thread_loop(EventQueue *queue)
67{
68 while (true) {
69 threadBarrier->wait();
70 doSimLoop(queue);
71 }
72}
73
74GlobalEvent*
75getLimitEvent(void) {
76 static GlobalSimLoopExitEvent
77 simulate_limit_event(mainEventQueue[0]->getCurTick(),
78 "simulate() limit reached", 0);
79 return &simulate_limit_event;
80}
74GlobalSimLoopExitEvent *simulate_limit_event = nullptr;
81
82/** Simulate for num_cycles additional cycles. If num_cycles is -1
83 * (the default), do not limit simulation; some other event must
84 * terminate the loop. Exported to Python via SWIG.
85 * @return The SimLoopExitEvent that caused the loop to exit.
86 */
87GlobalSimLoopExitEvent *
88simulate(Tick num_cycles)
89{
90 // The first time simulate() is called from the Python code, we need to
91 // create a thread for each of event queues referenced by the
92 // instantiated sim objects.
93 static bool threads_initialized = false;
94 static std::vector<std::thread *> threads;
95
96 if (!threads_initialized) {
97 threadBarrier = new Barrier(numMainEventQueues);
98
99 // the main thread (the one we're currently running on)
100 // handles queue 0, so we only need to allocate new threads
101 // for queues 1..N-1. We'll call these the "subordinate" threads.
102 for (uint32_t i = 1; i < numMainEventQueues; i++) {
103 threads.push_back(new std::thread(thread_loop, mainEventQueue[i]));
104 }
105
106 threads_initialized = true;
75
76/** Simulate for num_cycles additional cycles. If num_cycles is -1
77 * (the default), do not limit simulation; some other event must
78 * terminate the loop. Exported to Python via SWIG.
79 * @return The SimLoopExitEvent that caused the loop to exit.
80 */
81GlobalSimLoopExitEvent *
82simulate(Tick num_cycles)
83{
84 // The first time simulate() is called from the Python code, we need to
85 // create a thread for each of event queues referenced by the
86 // instantiated sim objects.
87 static bool threads_initialized = false;
88 static std::vector<std::thread *> threads;
89
90 if (!threads_initialized) {
91 threadBarrier = new Barrier(numMainEventQueues);
92
93 // the main thread (the one we're currently running on)
94 // handles queue 0, so we only need to allocate new threads
95 // for queues 1..N-1. We'll call these the "subordinate" threads.
96 for (uint32_t i = 1; i < numMainEventQueues; i++) {
97 threads.push_back(new std::thread(thread_loop, mainEventQueue[i]));
98 }
99
100 threads_initialized = true;
101 simulate_limit_event =
102 new GlobalSimLoopExitEvent(mainEventQueue[0]->getCurTick(),
103 "simulate() limit reached", 0);
107 }
108
109 inform("Entering event queue @ %d. Starting simulation...\n", curTick());
110
111 if (num_cycles < MaxTick - curTick())
112 num_cycles = curTick() + num_cycles;
113 else // counter would roll over or be set to MaxTick anyhow
114 num_cycles = MaxTick;
115
104 }
105
106 inform("Entering event queue @ %d. Starting simulation...\n", curTick());
107
108 if (num_cycles < MaxTick - curTick())
109 num_cycles = curTick() + num_cycles;
110 else // counter would roll over or be set to MaxTick anyhow
111 num_cycles = MaxTick;
112
116 getLimitEvent()->reschedule(num_cycles);
113 simulate_limit_event->reschedule(num_cycles);
117
118 GlobalSyncEvent *quantum_event = NULL;
119 if (numMainEventQueues > 1) {
120 if (simQuantum == 0) {
121 fatal("Quantum for multi-eventq simulation not specified");
122 }
123
124 quantum_event = new GlobalSyncEvent(curTick() + simQuantum, simQuantum,
125 EventBase::Progress_Event_Pri, 0);
126
127 inParallelMode = true;
128 }
129
130 // all subordinate (created) threads should be waiting on the
131 // barrier; the arrival of the main thread here will satisfy the
132 // barrier, and all threads will enter doSimLoop in parallel
133 threadBarrier->wait();
134 Event *local_event = doSimLoop(mainEventQueue[0]);
135 assert(local_event != NULL);
136
137 inParallelMode = false;
138
139 // locate the global exit event and return it to Python
140 BaseGlobalEvent *global_event = local_event->globalEvent();
141 assert(global_event != NULL);
142
143 GlobalSimLoopExitEvent *global_exit_event =
144 dynamic_cast<GlobalSimLoopExitEvent *>(global_event);
145 assert(global_exit_event != NULL);
146
147 //! Delete the simulation quantum event.
148 if (quantum_event != NULL) {
149 quantum_event->deschedule();
150 delete quantum_event;
151 }
152
153 return global_exit_event;
154}
155
156/**
157 * Test and clear the global async_event flag, such that each time the
158 * flag is cleared, only one thread returns true (and thus is assigned
159 * to handle the corresponding async event(s)).
160 */
161static bool
162testAndClearAsyncEvent()
163{
164 bool was_set = false;
165 asyncEventMutex.lock();
166
167 if (async_event) {
168 was_set = true;
169 async_event = false;
170 }
171
172 asyncEventMutex.unlock();
173 return was_set;
174}
175
176/**
177 * The main per-thread simulation loop. This loop is executed by all
178 * simulation threads (the main thread and the subordinate threads) in
179 * parallel.
180 */
181Event *
182doSimLoop(EventQueue *eventq)
183{
184 // set the per thread current eventq pointer
185 curEventQueue(eventq);
186 eventq->handleAsyncInsertions();
187
188 while (1) {
189 // there should always be at least one event (the SimLoopExitEvent
190 // we just scheduled) in the queue
191 assert(!eventq->empty());
192 assert(curTick() <= eventq->nextTick() &&
193 "event scheduled in the past");
194
195 if (async_event && testAndClearAsyncEvent()) {
196 // Take the event queue lock in case any of the service
197 // routines want to schedule new events.
198 std::lock_guard<EventQueue> lock(*eventq);
199 if (async_statdump || async_statreset) {
200 Stats::schedStatEvent(async_statdump, async_statreset);
201 async_statdump = false;
202 async_statreset = false;
203 }
204
205 if (async_io) {
206 async_io = false;
207 pollQueue.service();
208 }
209
210 if (async_exit) {
211 async_exit = false;
212 exitSimLoop("user interrupt received");
213 }
214
215 if (async_exception) {
216 async_exception = false;
217 return NULL;
218 }
219 }
220
221 Event *exit_event = eventq->serviceOne();
222 if (exit_event != NULL) {
223 return exit_event;
224 }
225 }
226
227 // not reached... only exit is return on SimLoopExitEvent
228}
114
115 GlobalSyncEvent *quantum_event = NULL;
116 if (numMainEventQueues > 1) {
117 if (simQuantum == 0) {
118 fatal("Quantum for multi-eventq simulation not specified");
119 }
120
121 quantum_event = new GlobalSyncEvent(curTick() + simQuantum, simQuantum,
122 EventBase::Progress_Event_Pri, 0);
123
124 inParallelMode = true;
125 }
126
127 // all subordinate (created) threads should be waiting on the
128 // barrier; the arrival of the main thread here will satisfy the
129 // barrier, and all threads will enter doSimLoop in parallel
130 threadBarrier->wait();
131 Event *local_event = doSimLoop(mainEventQueue[0]);
132 assert(local_event != NULL);
133
134 inParallelMode = false;
135
136 // locate the global exit event and return it to Python
137 BaseGlobalEvent *global_event = local_event->globalEvent();
138 assert(global_event != NULL);
139
140 GlobalSimLoopExitEvent *global_exit_event =
141 dynamic_cast<GlobalSimLoopExitEvent *>(global_event);
142 assert(global_exit_event != NULL);
143
144 //! Delete the simulation quantum event.
145 if (quantum_event != NULL) {
146 quantum_event->deschedule();
147 delete quantum_event;
148 }
149
150 return global_exit_event;
151}
152
153/**
154 * Test and clear the global async_event flag, such that each time the
155 * flag is cleared, only one thread returns true (and thus is assigned
156 * to handle the corresponding async event(s)).
157 */
158static bool
159testAndClearAsyncEvent()
160{
161 bool was_set = false;
162 asyncEventMutex.lock();
163
164 if (async_event) {
165 was_set = true;
166 async_event = false;
167 }
168
169 asyncEventMutex.unlock();
170 return was_set;
171}
172
173/**
174 * The main per-thread simulation loop. This loop is executed by all
175 * simulation threads (the main thread and the subordinate threads) in
176 * parallel.
177 */
178Event *
179doSimLoop(EventQueue *eventq)
180{
181 // set the per thread current eventq pointer
182 curEventQueue(eventq);
183 eventq->handleAsyncInsertions();
184
185 while (1) {
186 // there should always be at least one event (the SimLoopExitEvent
187 // we just scheduled) in the queue
188 assert(!eventq->empty());
189 assert(curTick() <= eventq->nextTick() &&
190 "event scheduled in the past");
191
192 if (async_event && testAndClearAsyncEvent()) {
193 // Take the event queue lock in case any of the service
194 // routines want to schedule new events.
195 std::lock_guard<EventQueue> lock(*eventq);
196 if (async_statdump || async_statreset) {
197 Stats::schedStatEvent(async_statdump, async_statreset);
198 async_statdump = false;
199 async_statreset = false;
200 }
201
202 if (async_io) {
203 async_io = false;
204 pollQueue.service();
205 }
206
207 if (async_exit) {
208 async_exit = false;
209 exitSimLoop("user interrupt received");
210 }
211
212 if (async_exception) {
213 async_exception = false;
214 return NULL;
215 }
216 }
217
218 Event *exit_event = eventq->serviceOne();
219 if (exit_event != NULL) {
220 return exit_event;
221 }
222 }
223
224 // not reached... only exit is return on SimLoopExitEvent
225}