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