scheduler.cc (13069:d894ba8f881c) scheduler.cc (13072:f2b83208ab54)
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
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
53Scheduler::~Scheduler()
54{
55 // Clear out everything that belongs to us to make sure nobody tries to
56 // clear themselves out after the scheduler goes away.
57
58 // Delta notifications.
59 for (auto &e: deltas)
60 e->deschedule();
61
62 // Timed notifications.
63 for (auto &ts: timeSlots) {
64 for (auto &e: ts.second->events)
65 e->deschedule();
66 delete ts.second;
67 ts.second = nullptr;
68 }
69
70 // gem5 events.
71 if (readyEvent.scheduled())
72 eq->deschedule(&readyEvent);
73 if (pauseEvent.scheduled())
74 eq->deschedule(&pauseEvent);
75 if (stopEvent.scheduled())
76 eq->deschedule(&stopEvent);
77 if (starvationEvent.scheduled())
78 eq->deschedule(&starvationEvent);
79 if (maxTickEvent.scheduled())
80 eq->deschedule(&maxTickEvent);
81
82 Process *p;
83 while ((p = toFinalize.getNext()))
84 p->popListNode();
85 while ((p = initList.getNext()))
86 p->popListNode();
87 while ((p = readyList.getNext()))
88 p->popListNode();
89
90 Channel *c;
91 while ((c = updateList.getNext()))
92 c->popListNode();
93}
94
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) {
78 if (starved() && !runToTime)
79 scheduleStarvationEvent();
80 kernel->status(::sc_core::SC_RUNNING);
81 }
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 scheduleReadyEvent();
157}
158
159void
160Scheduler::scheduleReadyEvent()
161{
162 // Schedule the evaluate and update phases.
163 if (!readyEvent.scheduled()) {
164 schedule(&readyEvent);
165 if (starvationEvent.scheduled())
166 deschedule(&starvationEvent);
167 }
168}
169
170void
171Scheduler::scheduleStarvationEvent()
172{
173 if (!starvationEvent.scheduled()) {
174 schedule(&starvationEvent);
175 if (readyEvent.scheduled())
176 deschedule(&readyEvent);
177 }
178}
179
180void
181Scheduler::runReady()
182{
183 bool empty = readyList.empty();
184
185 // The evaluation phase.
186 do {
187 yield();
188 } while (!readyList.empty());
189
190 if (!empty)
191 _numCycles++;
192
193 // The update phase.
194 update();
195
196 if (starved() && !runToTime)
197 scheduleStarvationEvent();
198
199 // The delta phase.
200 for (auto &e: deltas)
201 e->run();
202 deltas.clear();
203
204 if (runOnce)
205 schedulePause();
206}
207
208void
209Scheduler::update()
210{
211 Channel *channel = updateList.getNext();
212 while (channel) {
213 channel->popListNode();
214 channel->update();
215 channel = updateList.getNext();
216 }
217}
218
219void
220Scheduler::pause()
221{
222 _paused = true;
223 kernel->status(::sc_core::SC_PAUSED);
224 runOnce = false;
225 scMain->run();
226}
227
228void
229Scheduler::stop()
230{
231 _stopped = true;
232 kernel->stop();
233 runOnce = false;
234 scMain->run();
235}
236
237void
238Scheduler::start(Tick max_tick, bool run_to_time)
239{
240 // We should be running from sc_main. Keep track of that Fiber to return
241 // to later.
242 scMain = Fiber::currentFiber();
243
244 _started = true;
245 _paused = false;
246 _stopped = false;
247 runToTime = run_to_time;
248
249 maxTick = max_tick;
250
251 if (initDone) {
252 if (starved() && !runToTime)
253 scheduleStarvationEvent();
254 kernel->status(::sc_core::SC_RUNNING);
255 }
256
257 schedule(&maxTickEvent, maxTick);
258
259 // Return to gem5 to let it run events, etc.
260 Fiber::primaryFiber()->run();
261
262 if (pauseEvent.scheduled())
263 eq->deschedule(&pauseEvent);
264 if (stopEvent.scheduled())
265 eq->deschedule(&stopEvent);
266 if (maxTickEvent.scheduled())
267 eq->deschedule(&maxTickEvent);
268 if (starvationEvent.scheduled())
269 eq->deschedule(&starvationEvent);
270}
271
272void
273Scheduler::oneCycle()
274{
275 runOnce = true;
276 start(::MaxTick, false);
277}
278
279void
280Scheduler::schedulePause()
281{
282 if (pauseEvent.scheduled())
283 return;
284
285 eq->schedule(&pauseEvent, eq->getCurTick());
286}
287
288void
289Scheduler::scheduleStop(bool finish_delta)
290{
291 if (stopEvent.scheduled())
292 return;
293
294 if (!finish_delta) {
295 // If we're not supposed to finish the delta cycle, flush the list
296 // of ready processes, scheduled updates, and delta notifications.
297 Process *p;
298 while ((p = readyList.getNext()))
299 p->popListNode();
300 Channel *c;
301 while ((c = updateList.getNext()))
302 c->popListNode();
303 for (auto &e: deltas)
304 e->deschedule();
305 deltas.clear();
306 }
307 eq->schedule(&stopEvent, eq->getCurTick());
308}
309
310Scheduler scheduler;
311
312} // namespace sc_gem5
95void
96Scheduler::initPhase()
97{
98 for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) {
99 p->finalize();
100 p->popListNode();
101 }
102
103 for (Process *p = initList.getNext(); p; p = initList.getNext()) {
104 p->finalize();
105 p->popListNode();
106 p->ready();
107 }
108
109 update();
110
111 for (auto &e: deltas)
112 e->run();
113 deltas.clear();
114
115 for (auto ets: eventsToSchedule)
116 eq->schedule(ets.first, ets.second);
117 eventsToSchedule.clear();
118
119 if (_started) {
120 if (starved() && !runToTime)
121 scheduleStarvationEvent();
122 kernel->status(::sc_core::SC_RUNNING);
123 }
124
125 initDone = true;
126}
127
128void
129Scheduler::reg(Process *p)
130{
131 if (initDone) {
132 // If we're past initialization, finalize static sensitivity.
133 p->finalize();
134 // Mark the process as ready.
135 p->ready();
136 } else {
137 // Otherwise, record that this process should be initialized once we
138 // get there.
139 initList.pushLast(p);
140 }
141}
142
143void
144Scheduler::dontInitialize(Process *p)
145{
146 if (initDone) {
147 // Pop this process off of the ready list.
148 p->popListNode();
149 } else {
150 // Push this process onto the list of processes which still need
151 // their static sensitivity to be finalized. That implicitly pops it
152 // off the list of processes to be initialized/marked ready.
153 toFinalize.pushLast(p);
154 }
155}
156
157void
158Scheduler::yield()
159{
160 _current = readyList.getNext();
161 if (!_current) {
162 // There are no more processes, so return control to evaluate.
163 Fiber::primaryFiber()->run();
164 } else {
165 _current->popListNode();
166 // Switch to whatever Fiber is supposed to run this process. All
167 // Fibers which aren't running should be parked at this line.
168 _current->fiber()->run();
169 // If the current process needs to be manually started, start it.
170 if (_current && _current->needsStart())
171 _current->run();
172 }
173 if (_current && _current->excWrapper) {
174 // Make sure this isn't a method process.
175 assert(!_current->needsStart());
176 auto ew = _current->excWrapper;
177 _current->excWrapper = nullptr;
178 ew->throw_it();
179 }
180}
181
182void
183Scheduler::ready(Process *p)
184{
185 // Clump methods together to minimize context switching.
186 if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
187 readyList.pushFirst(p);
188 else
189 readyList.pushLast(p);
190
191 scheduleReadyEvent();
192}
193
194void
195Scheduler::requestUpdate(Channel *c)
196{
197 updateList.pushLast(c);
198 scheduleReadyEvent();
199}
200
201void
202Scheduler::scheduleReadyEvent()
203{
204 // Schedule the evaluate and update phases.
205 if (!readyEvent.scheduled()) {
206 schedule(&readyEvent);
207 if (starvationEvent.scheduled())
208 deschedule(&starvationEvent);
209 }
210}
211
212void
213Scheduler::scheduleStarvationEvent()
214{
215 if (!starvationEvent.scheduled()) {
216 schedule(&starvationEvent);
217 if (readyEvent.scheduled())
218 deschedule(&readyEvent);
219 }
220}
221
222void
223Scheduler::runReady()
224{
225 bool empty = readyList.empty();
226
227 // The evaluation phase.
228 do {
229 yield();
230 } while (!readyList.empty());
231
232 if (!empty)
233 _numCycles++;
234
235 // The update phase.
236 update();
237
238 if (starved() && !runToTime)
239 scheduleStarvationEvent();
240
241 // The delta phase.
242 for (auto &e: deltas)
243 e->run();
244 deltas.clear();
245
246 if (runOnce)
247 schedulePause();
248}
249
250void
251Scheduler::update()
252{
253 Channel *channel = updateList.getNext();
254 while (channel) {
255 channel->popListNode();
256 channel->update();
257 channel = updateList.getNext();
258 }
259}
260
261void
262Scheduler::pause()
263{
264 _paused = true;
265 kernel->status(::sc_core::SC_PAUSED);
266 runOnce = false;
267 scMain->run();
268}
269
270void
271Scheduler::stop()
272{
273 _stopped = true;
274 kernel->stop();
275 runOnce = false;
276 scMain->run();
277}
278
279void
280Scheduler::start(Tick max_tick, bool run_to_time)
281{
282 // We should be running from sc_main. Keep track of that Fiber to return
283 // to later.
284 scMain = Fiber::currentFiber();
285
286 _started = true;
287 _paused = false;
288 _stopped = false;
289 runToTime = run_to_time;
290
291 maxTick = max_tick;
292
293 if (initDone) {
294 if (starved() && !runToTime)
295 scheduleStarvationEvent();
296 kernel->status(::sc_core::SC_RUNNING);
297 }
298
299 schedule(&maxTickEvent, maxTick);
300
301 // Return to gem5 to let it run events, etc.
302 Fiber::primaryFiber()->run();
303
304 if (pauseEvent.scheduled())
305 eq->deschedule(&pauseEvent);
306 if (stopEvent.scheduled())
307 eq->deschedule(&stopEvent);
308 if (maxTickEvent.scheduled())
309 eq->deschedule(&maxTickEvent);
310 if (starvationEvent.scheduled())
311 eq->deschedule(&starvationEvent);
312}
313
314void
315Scheduler::oneCycle()
316{
317 runOnce = true;
318 start(::MaxTick, false);
319}
320
321void
322Scheduler::schedulePause()
323{
324 if (pauseEvent.scheduled())
325 return;
326
327 eq->schedule(&pauseEvent, eq->getCurTick());
328}
329
330void
331Scheduler::scheduleStop(bool finish_delta)
332{
333 if (stopEvent.scheduled())
334 return;
335
336 if (!finish_delta) {
337 // If we're not supposed to finish the delta cycle, flush the list
338 // of ready processes, scheduled updates, and delta notifications.
339 Process *p;
340 while ((p = readyList.getNext()))
341 p->popListNode();
342 Channel *c;
343 while ((c = updateList.getNext()))
344 c->popListNode();
345 for (auto &e: deltas)
346 e->deschedule();
347 deltas.clear();
348 }
349 eq->schedule(&stopEvent, eq->getCurTick());
350}
351
352Scheduler scheduler;
353
354} // namespace sc_gem5