scheduler.cc (13067:3d6ef32002ef) scheduler.cc (13068:822c1be3550e)
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/scheduler.hh"
31
32#include "base/fiber.hh"
33#include "base/logging.hh"
34#include "sim/eventq.hh"
35#include "systemc/core/kernel.hh"
36#include "systemc/ext/core/sc_main.hh"
37
38namespace sc_gem5
39{
40
41Scheduler::Scheduler() :
42 eq(nullptr), readyEvent(this, false, ReadyPriority),
43 pauseEvent(this, false, PausePriority),
44 stopEvent(this, false, StopPriority),
45 scMain(nullptr),
46 starvationEvent(this, false, StarvationPriority),
47 _started(false), _paused(false), _stopped(false),
48 maxTickEvent(this, false, MaxTickPriority),
49 _numCycles(0), _current(nullptr), initDone(false),
50 runOnce(false)
51{}
52
53void
54Scheduler::initPhase()
55{
56 for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) {
57 p->finalize();
58 p->popListNode();
59 }
60
61 for (Process *p = initList.getNext(); p; p = initList.getNext()) {
62 p->finalize();
63 p->popListNode();
64 p->ready();
65 }
66
67 update();
68
69 for (auto &e: deltas)
70 e->run();
71 deltas.clear();
72
73 for (auto ets: eventsToSchedule)
74 eq->schedule(ets.first, ets.second);
75 eventsToSchedule.clear();
76
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/scheduler.hh"
31
32#include "base/fiber.hh"
33#include "base/logging.hh"
34#include "sim/eventq.hh"
35#include "systemc/core/kernel.hh"
36#include "systemc/ext/core/sc_main.hh"
37
38namespace sc_gem5
39{
40
41Scheduler::Scheduler() :
42 eq(nullptr), readyEvent(this, false, ReadyPriority),
43 pauseEvent(this, false, PausePriority),
44 stopEvent(this, false, StopPriority),
45 scMain(nullptr),
46 starvationEvent(this, false, StarvationPriority),
47 _started(false), _paused(false), _stopped(false),
48 maxTickEvent(this, false, MaxTickPriority),
49 _numCycles(0), _current(nullptr), initDone(false),
50 runOnce(false)
51{}
52
53void
54Scheduler::initPhase()
55{
56 for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) {
57 p->finalize();
58 p->popListNode();
59 }
60
61 for (Process *p = initList.getNext(); p; p = initList.getNext()) {
62 p->finalize();
63 p->popListNode();
64 p->ready();
65 }
66
67 update();
68
69 for (auto &e: deltas)
70 e->run();
71 deltas.clear();
72
73 for (auto ets: eventsToSchedule)
74 eq->schedule(ets.first, ets.second);
75 eventsToSchedule.clear();
76
77 if (_started)
77 if (_started) {
78 if (starved() && !runToTime)
79 scheduleStarvationEvent();
78 eq->schedule(&maxTickEvent, maxTick);
80 eq->schedule(&maxTickEvent, maxTick);
81 }
79
80 initDone = true;
81}
82
83void
84Scheduler::reg(Process *p)
85{
86 if (initDone) {
87 // If we're past initialization, finalize static sensitivity.
88 p->finalize();
89 // Mark the process as ready.
90 p->ready();
91 } else {
92 // Otherwise, record that this process should be initialized once we
93 // get there.
94 initList.pushLast(p);
95 }
96}
97
98void
99Scheduler::dontInitialize(Process *p)
100{
101 if (initDone) {
102 // Pop this process off of the ready list.
103 p->popListNode();
104 } else {
105 // Push this process onto the list of processes which still need
106 // their static sensitivity to be finalized. That implicitly pops it
107 // off the list of processes to be initialized/marked ready.
108 toFinalize.pushLast(p);
109 }
110}
111
112void
113Scheduler::yield()
114{
115 _current = readyList.getNext();
116 if (!_current) {
117 // There are no more processes, so return control to evaluate.
118 Fiber::primaryFiber()->run();
119 } else {
120 _current->popListNode();
121 // Switch to whatever Fiber is supposed to run this process. All
122 // Fibers which aren't running should be parked at this line.
123 _current->fiber()->run();
124 // If the current process needs to be manually started, start it.
125 if (_current && _current->needsStart())
126 _current->run();
127 }
128 if (_current && _current->excWrapper) {
129 // Make sure this isn't a method process.
130 assert(!_current->needsStart());
131 auto ew = _current->excWrapper;
132 _current->excWrapper = nullptr;
133 ew->throw_it();
134 }
135}
136
137void
138Scheduler::ready(Process *p)
139{
140 // Clump methods together to minimize context switching.
141 if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
142 readyList.pushFirst(p);
143 else
144 readyList.pushLast(p);
145
146 scheduleReadyEvent();
147}
148
149void
150Scheduler::requestUpdate(Channel *c)
151{
152 updateList.pushLast(c);
153 if (eq)
154 scheduleReadyEvent();
155}
156
157void
158Scheduler::scheduleReadyEvent()
159{
160 // Schedule the evaluate and update phases.
161 if (!readyEvent.scheduled()) {
162 panic_if(!eq, "Need to schedule ready, but no event manager.\n");
163 eq->schedule(&readyEvent, eq->getCurTick());
164 if (starvationEvent.scheduled())
165 eq->deschedule(&starvationEvent);
166 }
167}
168
169void
170Scheduler::scheduleStarvationEvent()
171{
172 if (!starvationEvent.scheduled()) {
82
83 initDone = true;
84}
85
86void
87Scheduler::reg(Process *p)
88{
89 if (initDone) {
90 // If we're past initialization, finalize static sensitivity.
91 p->finalize();
92 // Mark the process as ready.
93 p->ready();
94 } else {
95 // Otherwise, record that this process should be initialized once we
96 // get there.
97 initList.pushLast(p);
98 }
99}
100
101void
102Scheduler::dontInitialize(Process *p)
103{
104 if (initDone) {
105 // Pop this process off of the ready list.
106 p->popListNode();
107 } else {
108 // Push this process onto the list of processes which still need
109 // their static sensitivity to be finalized. That implicitly pops it
110 // off the list of processes to be initialized/marked ready.
111 toFinalize.pushLast(p);
112 }
113}
114
115void
116Scheduler::yield()
117{
118 _current = readyList.getNext();
119 if (!_current) {
120 // There are no more processes, so return control to evaluate.
121 Fiber::primaryFiber()->run();
122 } else {
123 _current->popListNode();
124 // Switch to whatever Fiber is supposed to run this process. All
125 // Fibers which aren't running should be parked at this line.
126 _current->fiber()->run();
127 // If the current process needs to be manually started, start it.
128 if (_current && _current->needsStart())
129 _current->run();
130 }
131 if (_current && _current->excWrapper) {
132 // Make sure this isn't a method process.
133 assert(!_current->needsStart());
134 auto ew = _current->excWrapper;
135 _current->excWrapper = nullptr;
136 ew->throw_it();
137 }
138}
139
140void
141Scheduler::ready(Process *p)
142{
143 // Clump methods together to minimize context switching.
144 if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
145 readyList.pushFirst(p);
146 else
147 readyList.pushLast(p);
148
149 scheduleReadyEvent();
150}
151
152void
153Scheduler::requestUpdate(Channel *c)
154{
155 updateList.pushLast(c);
156 if (eq)
157 scheduleReadyEvent();
158}
159
160void
161Scheduler::scheduleReadyEvent()
162{
163 // Schedule the evaluate and update phases.
164 if (!readyEvent.scheduled()) {
165 panic_if(!eq, "Need to schedule ready, but no event manager.\n");
166 eq->schedule(&readyEvent, eq->getCurTick());
167 if (starvationEvent.scheduled())
168 eq->deschedule(&starvationEvent);
169 }
170}
171
172void
173Scheduler::scheduleStarvationEvent()
174{
175 if (!starvationEvent.scheduled()) {
173 panic_if(!eq, "Need to schedule starvation event, "
174 "but no event manager.\n");
175 eq->schedule(&starvationEvent, eq->getCurTick());
176 Tick now = getCurTick();
177 if (initDone)
178 eq->schedule(&starvationEvent, now);
179 else
180 eventsToSchedule[&starvationEvent] = now;
176 if (readyEvent.scheduled())
177 eq->deschedule(&readyEvent);
178 }
179}
180
181void
182Scheduler::runReady()
183{
184 bool empty = readyList.empty();
185
186 // The evaluation phase.
187 do {
188 yield();
189 } while (!readyList.empty());
190
191 if (!empty)
192 _numCycles++;
193
194 // The update phase.
195 update();
196
197 if (starved() && !runToTime)
198 scheduleStarvationEvent();
199
200 // The delta phase.
201 for (auto &e: deltas)
202 e->run();
203 deltas.clear();
204
205 if (runOnce)
206 schedulePause();
207}
208
209void
210Scheduler::update()
211{
212 Channel *channel = updateList.getNext();
213 while (channel) {
214 channel->popListNode();
215 channel->update();
216 channel = updateList.getNext();
217 }
218}
219
220void
221Scheduler::pause()
222{
223 _paused = true;
224 kernel->status(::sc_core::SC_PAUSED);
225 runOnce = false;
226 scMain->run();
227}
228
229void
230Scheduler::stop()
231{
232 _stopped = true;
233 kernel->stop();
234 runOnce = false;
235 scMain->run();
236}
237
238void
239Scheduler::start(Tick max_tick, bool run_to_time)
240{
241 // We should be running from sc_main. Keep track of that Fiber to return
242 // to later.
243 scMain = Fiber::currentFiber();
244
245 _started = true;
246 _paused = false;
247 _stopped = false;
248 runToTime = run_to_time;
249
250 maxTick = max_tick;
251
181 if (readyEvent.scheduled())
182 eq->deschedule(&readyEvent);
183 }
184}
185
186void
187Scheduler::runReady()
188{
189 bool empty = readyList.empty();
190
191 // The evaluation phase.
192 do {
193 yield();
194 } while (!readyList.empty());
195
196 if (!empty)
197 _numCycles++;
198
199 // The update phase.
200 update();
201
202 if (starved() && !runToTime)
203 scheduleStarvationEvent();
204
205 // The delta phase.
206 for (auto &e: deltas)
207 e->run();
208 deltas.clear();
209
210 if (runOnce)
211 schedulePause();
212}
213
214void
215Scheduler::update()
216{
217 Channel *channel = updateList.getNext();
218 while (channel) {
219 channel->popListNode();
220 channel->update();
221 channel = updateList.getNext();
222 }
223}
224
225void
226Scheduler::pause()
227{
228 _paused = true;
229 kernel->status(::sc_core::SC_PAUSED);
230 runOnce = false;
231 scMain->run();
232}
233
234void
235Scheduler::stop()
236{
237 _stopped = true;
238 kernel->stop();
239 runOnce = false;
240 scMain->run();
241}
242
243void
244Scheduler::start(Tick max_tick, bool run_to_time)
245{
246 // We should be running from sc_main. Keep track of that Fiber to return
247 // to later.
248 scMain = Fiber::currentFiber();
249
250 _started = true;
251 _paused = false;
252 _stopped = false;
253 runToTime = run_to_time;
254
255 maxTick = max_tick;
256
252 if (starved() && !runToTime)
253 return;
254
255 if (initDone) {
257 if (initDone) {
258 if (starved() && !runToTime)
259 scheduleStarvationEvent();
256 kernel->status(::sc_core::SC_RUNNING);
257 eq->schedule(&maxTickEvent, maxTick);
258 }
259
260 // Return to gem5 to let it run events, etc.
261 Fiber::primaryFiber()->run();
262
263 if (pauseEvent.scheduled())
264 eq->deschedule(&pauseEvent);
265 if (stopEvent.scheduled())
266 eq->deschedule(&stopEvent);
267 if (maxTickEvent.scheduled())
268 eq->deschedule(&maxTickEvent);
269 if (starvationEvent.scheduled())
270 eq->deschedule(&starvationEvent);
271}
272
273void
274Scheduler::oneCycle()
275{
276 runOnce = true;
277 start(::MaxTick, false);
278}
279
280void
281Scheduler::schedulePause()
282{
283 if (pauseEvent.scheduled())
284 return;
285
286 eq->schedule(&pauseEvent, eq->getCurTick());
287}
288
289void
290Scheduler::scheduleStop(bool finish_delta)
291{
292 if (stopEvent.scheduled())
293 return;
294
295 if (!finish_delta) {
296 // If we're not supposed to finish the delta cycle, flush the list
297 // of ready processes, scheduled updates, and delta notifications.
298 Process *p;
299 while ((p = readyList.getNext()))
300 p->popListNode();
301 Channel *c;
302 while ((c = updateList.getNext()))
303 c->popListNode();
304 for (auto &e: deltas)
305 e->deschedule();
306 deltas.clear();
307 }
308 eq->schedule(&stopEvent, eq->getCurTick());
309}
310
311Scheduler scheduler;
312
313} // namespace sc_gem5
260 kernel->status(::sc_core::SC_RUNNING);
261 eq->schedule(&maxTickEvent, maxTick);
262 }
263
264 // Return to gem5 to let it run events, etc.
265 Fiber::primaryFiber()->run();
266
267 if (pauseEvent.scheduled())
268 eq->deschedule(&pauseEvent);
269 if (stopEvent.scheduled())
270 eq->deschedule(&stopEvent);
271 if (maxTickEvent.scheduled())
272 eq->deschedule(&maxTickEvent);
273 if (starvationEvent.scheduled())
274 eq->deschedule(&starvationEvent);
275}
276
277void
278Scheduler::oneCycle()
279{
280 runOnce = true;
281 start(::MaxTick, false);
282}
283
284void
285Scheduler::schedulePause()
286{
287 if (pauseEvent.scheduled())
288 return;
289
290 eq->schedule(&pauseEvent, eq->getCurTick());
291}
292
293void
294Scheduler::scheduleStop(bool finish_delta)
295{
296 if (stopEvent.scheduled())
297 return;
298
299 if (!finish_delta) {
300 // If we're not supposed to finish the delta cycle, flush the list
301 // of ready processes, scheduled updates, and delta notifications.
302 Process *p;
303 while ((p = readyList.getNext()))
304 p->popListNode();
305 Channel *c;
306 while ((c = updateList.getNext()))
307 c->popListNode();
308 for (auto &e: deltas)
309 e->deschedule();
310 deltas.clear();
311 }
312 eq->schedule(&stopEvent, eq->getCurTick());
313}
314
315Scheduler scheduler;
316
317} // namespace sc_gem5