scheduler.cc (12995:3421144dd03e) scheduler.cc (12996:17ec70f9841e)
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), initReady(false)
50{}
51
52void
53Scheduler::prepareForInit()
54{
55 for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) {
56 p->finalize();
57 p->popListNode();
58 }
59
60 for (Process *p = initList.getNext(); p; p = initList.getNext()) {
61 p->finalize();
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), initReady(false)
50{}
51
52void
53Scheduler::prepareForInit()
54{
55 for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) {
56 p->finalize();
57 p->popListNode();
58 }
59
60 for (Process *p = initList.getNext(); p; p = initList.getNext()) {
61 p->finalize();
62 p->popListNode();
62 p->ready();
63 }
64
65 for (auto ets: eventsToSchedule)
66 eq->schedule(ets.first, ets.second);
67 eventsToSchedule.clear();
68
69 if (_started)
70 eq->schedule(&maxTickEvent, maxTick);
71
72 initReady = true;
73}
74
75void
76Scheduler::reg(Process *p)
77{
78 if (initReady) {
79 // If we're past initialization, finalize static sensitivity.
80 p->finalize();
81 // Mark the process as ready.
82 p->ready();
83 } else {
84 // Otherwise, record that this process should be initialized once we
85 // get there.
86 initList.pushLast(p);
87 }
88}
89
90void
91Scheduler::dontInitialize(Process *p)
92{
93 if (initReady) {
94 // Pop this process off of the ready list.
95 p->popListNode();
96 } else {
97 // Push this process onto the list of processes which still need
98 // their static sensitivity to be finalized. That implicitly pops it
99 // off the list of processes to be initialized/marked ready.
100 toFinalize.pushLast(p);
101 }
102}
103
104void
105Scheduler::yield()
106{
107 _current = readyList.getNext();
108 if (!_current) {
109 // There are no more processes, so return control to evaluate.
110 Fiber::primaryFiber()->run();
111 } else {
112 _current->popListNode();
113 // Switch to whatever Fiber is supposed to run this process. All
114 // Fibers which aren't running should be parked at this line.
115 _current->fiber()->run();
116 // If the current process needs to be manually started, start it.
117 if (_current && _current->needsStart())
118 _current->run();
119 }
120 if (_current && _current->excWrapper) {
121 // Make sure this isn't a method process.
122 assert(!_current->needsStart());
123 auto ew = _current->excWrapper;
124 _current->excWrapper = nullptr;
125 ew->throw_it();
126 }
127}
128
129void
130Scheduler::ready(Process *p)
131{
132 // Clump methods together to minimize context switching.
133 if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
134 readyList.pushFirst(p);
135 else
136 readyList.pushLast(p);
137
138 scheduleReadyEvent();
139}
140
141void
142Scheduler::requestUpdate(Channel *c)
143{
144 updateList.pushLast(c);
145 scheduleReadyEvent();
146}
147
148void
149Scheduler::scheduleReadyEvent()
150{
151 // Schedule the evaluate and update phases.
152 if (!readyEvent.scheduled()) {
153 panic_if(!eq, "Need to schedule ready, but no event manager.\n");
154 eq->schedule(&readyEvent, eq->getCurTick());
155 if (starvationEvent.scheduled())
156 eq->deschedule(&starvationEvent);
157 }
158}
159
160void
161Scheduler::scheduleStarvationEvent()
162{
163 if (!starvationEvent.scheduled()) {
164 panic_if(!eq, "Need to schedule starvation event, "
165 "but no event manager.\n");
166 eq->schedule(&starvationEvent, eq->getCurTick());
167 if (readyEvent.scheduled())
168 eq->deschedule(&readyEvent);
169 }
170}
171
172void
173Scheduler::runReady()
174{
175 bool empty = readyList.empty();
176
177 // The evaluation phase.
178 do {
179 yield();
180 } while (!readyList.empty());
181
182 if (!empty)
183 _numCycles++;
184
185 // The update phase.
186 update();
187
188 if (starved() && !runToTime)
189 scheduleStarvationEvent();
190
191 // The delta phase will happen naturally through the event queue.
192}
193
194void
195Scheduler::update()
196{
197 Channel *channel = updateList.getNext();
198 while (channel) {
199 channel->popListNode();
200 channel->update();
201 channel = updateList.getNext();
202 }
203}
204
205void
206Scheduler::pause()
207{
208 _paused = true;
209 kernel->status(::sc_core::SC_PAUSED);
210 scMain->run();
211}
212
213void
214Scheduler::stop()
215{
216 _stopped = true;
217 kernel->stop();
218 scMain->run();
219}
220
221void
222Scheduler::start(Tick max_tick, bool run_to_time)
223{
224 // We should be running from sc_main. Keep track of that Fiber to return
225 // to later.
226 scMain = Fiber::currentFiber();
227
228 _started = true;
229 _paused = false;
230 _stopped = false;
231 runToTime = run_to_time;
232
233 maxTick = max_tick;
234
235 if (starved() && !runToTime)
236 return;
237
238 if (initReady) {
239 kernel->status(::sc_core::SC_RUNNING);
240 eq->schedule(&maxTickEvent, maxTick);
241 }
242
243 // Return to gem5 to let it run events, etc.
244 Fiber::primaryFiber()->run();
245
246 if (pauseEvent.scheduled())
247 eq->deschedule(&pauseEvent);
248 if (stopEvent.scheduled())
249 eq->deschedule(&stopEvent);
250 if (maxTickEvent.scheduled())
251 eq->deschedule(&maxTickEvent);
252 if (starvationEvent.scheduled())
253 eq->deschedule(&starvationEvent);
254}
255
256void
257Scheduler::schedulePause()
258{
259 if (pauseEvent.scheduled())
260 return;
261
262 eq->schedule(&pauseEvent, eq->getCurTick());
263}
264
265void
266Scheduler::scheduleStop(bool finish_delta)
267{
268 if (stopEvent.scheduled())
269 return;
270
271 if (!finish_delta) {
272 // If we're not supposed to finish the delta cycle, flush the list
273 // of ready processes and scheduled updates.
274 Process *p;
275 while ((p = readyList.getNext()))
276 p->popListNode();
277 Channel *c;
278 while ((c = updateList.getNext()))
279 c->popListNode();
280 }
281 eq->schedule(&stopEvent, eq->getCurTick());
282}
283
284Scheduler scheduler;
285
286} // namespace sc_gem5
63 p->ready();
64 }
65
66 for (auto ets: eventsToSchedule)
67 eq->schedule(ets.first, ets.second);
68 eventsToSchedule.clear();
69
70 if (_started)
71 eq->schedule(&maxTickEvent, maxTick);
72
73 initReady = true;
74}
75
76void
77Scheduler::reg(Process *p)
78{
79 if (initReady) {
80 // If we're past initialization, finalize static sensitivity.
81 p->finalize();
82 // Mark the process as ready.
83 p->ready();
84 } else {
85 // Otherwise, record that this process should be initialized once we
86 // get there.
87 initList.pushLast(p);
88 }
89}
90
91void
92Scheduler::dontInitialize(Process *p)
93{
94 if (initReady) {
95 // Pop this process off of the ready list.
96 p->popListNode();
97 } else {
98 // Push this process onto the list of processes which still need
99 // their static sensitivity to be finalized. That implicitly pops it
100 // off the list of processes to be initialized/marked ready.
101 toFinalize.pushLast(p);
102 }
103}
104
105void
106Scheduler::yield()
107{
108 _current = readyList.getNext();
109 if (!_current) {
110 // There are no more processes, so return control to evaluate.
111 Fiber::primaryFiber()->run();
112 } else {
113 _current->popListNode();
114 // Switch to whatever Fiber is supposed to run this process. All
115 // Fibers which aren't running should be parked at this line.
116 _current->fiber()->run();
117 // If the current process needs to be manually started, start it.
118 if (_current && _current->needsStart())
119 _current->run();
120 }
121 if (_current && _current->excWrapper) {
122 // Make sure this isn't a method process.
123 assert(!_current->needsStart());
124 auto ew = _current->excWrapper;
125 _current->excWrapper = nullptr;
126 ew->throw_it();
127 }
128}
129
130void
131Scheduler::ready(Process *p)
132{
133 // Clump methods together to minimize context switching.
134 if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
135 readyList.pushFirst(p);
136 else
137 readyList.pushLast(p);
138
139 scheduleReadyEvent();
140}
141
142void
143Scheduler::requestUpdate(Channel *c)
144{
145 updateList.pushLast(c);
146 scheduleReadyEvent();
147}
148
149void
150Scheduler::scheduleReadyEvent()
151{
152 // Schedule the evaluate and update phases.
153 if (!readyEvent.scheduled()) {
154 panic_if(!eq, "Need to schedule ready, but no event manager.\n");
155 eq->schedule(&readyEvent, eq->getCurTick());
156 if (starvationEvent.scheduled())
157 eq->deschedule(&starvationEvent);
158 }
159}
160
161void
162Scheduler::scheduleStarvationEvent()
163{
164 if (!starvationEvent.scheduled()) {
165 panic_if(!eq, "Need to schedule starvation event, "
166 "but no event manager.\n");
167 eq->schedule(&starvationEvent, eq->getCurTick());
168 if (readyEvent.scheduled())
169 eq->deschedule(&readyEvent);
170 }
171}
172
173void
174Scheduler::runReady()
175{
176 bool empty = readyList.empty();
177
178 // The evaluation phase.
179 do {
180 yield();
181 } while (!readyList.empty());
182
183 if (!empty)
184 _numCycles++;
185
186 // The update phase.
187 update();
188
189 if (starved() && !runToTime)
190 scheduleStarvationEvent();
191
192 // The delta phase will happen naturally through the event queue.
193}
194
195void
196Scheduler::update()
197{
198 Channel *channel = updateList.getNext();
199 while (channel) {
200 channel->popListNode();
201 channel->update();
202 channel = updateList.getNext();
203 }
204}
205
206void
207Scheduler::pause()
208{
209 _paused = true;
210 kernel->status(::sc_core::SC_PAUSED);
211 scMain->run();
212}
213
214void
215Scheduler::stop()
216{
217 _stopped = true;
218 kernel->stop();
219 scMain->run();
220}
221
222void
223Scheduler::start(Tick max_tick, bool run_to_time)
224{
225 // We should be running from sc_main. Keep track of that Fiber to return
226 // to later.
227 scMain = Fiber::currentFiber();
228
229 _started = true;
230 _paused = false;
231 _stopped = false;
232 runToTime = run_to_time;
233
234 maxTick = max_tick;
235
236 if (starved() && !runToTime)
237 return;
238
239 if (initReady) {
240 kernel->status(::sc_core::SC_RUNNING);
241 eq->schedule(&maxTickEvent, maxTick);
242 }
243
244 // Return to gem5 to let it run events, etc.
245 Fiber::primaryFiber()->run();
246
247 if (pauseEvent.scheduled())
248 eq->deschedule(&pauseEvent);
249 if (stopEvent.scheduled())
250 eq->deschedule(&stopEvent);
251 if (maxTickEvent.scheduled())
252 eq->deschedule(&maxTickEvent);
253 if (starvationEvent.scheduled())
254 eq->deschedule(&starvationEvent);
255}
256
257void
258Scheduler::schedulePause()
259{
260 if (pauseEvent.scheduled())
261 return;
262
263 eq->schedule(&pauseEvent, eq->getCurTick());
264}
265
266void
267Scheduler::scheduleStop(bool finish_delta)
268{
269 if (stopEvent.scheduled())
270 return;
271
272 if (!finish_delta) {
273 // If we're not supposed to finish the delta cycle, flush the list
274 // of ready processes and scheduled updates.
275 Process *p;
276 while ((p = readyList.getNext()))
277 p->popListNode();
278 Channel *c;
279 while ((c = updateList.getNext()))
280 c->popListNode();
281 }
282 eq->schedule(&stopEvent, eq->getCurTick());
283}
284
285Scheduler scheduler;
286
287} // namespace sc_gem5