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