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