scheduler.cc revision 13049:181358d628b7
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();
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    if (eq)
147        scheduleReadyEvent();
148}
149
150void
151Scheduler::scheduleReadyEvent()
152{
153    // Schedule the evaluate and update phases.
154    if (!readyEvent.scheduled()) {
155        panic_if(!eq, "Need to schedule ready, but no event manager.\n");
156        eq->schedule(&readyEvent, eq->getCurTick());
157        if (starvationEvent.scheduled())
158            eq->deschedule(&starvationEvent);
159    }
160}
161
162void
163Scheduler::scheduleStarvationEvent()
164{
165    if (!starvationEvent.scheduled()) {
166        panic_if(!eq, "Need to schedule starvation event, "
167                "but no event manager.\n");
168        eq->schedule(&starvationEvent, eq->getCurTick());
169        if (readyEvent.scheduled())
170            eq->deschedule(&readyEvent);
171    }
172}
173
174void
175Scheduler::runReady()
176{
177    bool empty = readyList.empty();
178
179    // The evaluation phase.
180    do {
181        yield();
182    } while (!readyList.empty());
183
184    if (!empty)
185        _numCycles++;
186
187    // The update phase.
188    update();
189
190    if (starved() && !runToTime)
191        scheduleStarvationEvent();
192
193    // The delta phase will happen naturally through the event queue.
194}
195
196void
197Scheduler::update()
198{
199    Channel *channel = updateList.getNext();
200    while (channel) {
201        channel->popListNode();
202        channel->update();
203        channel = updateList.getNext();
204    }
205}
206
207void
208Scheduler::pause()
209{
210    _paused = true;
211    kernel->status(::sc_core::SC_PAUSED);
212    scMain->run();
213
214    // If the ready event is supposed to run now, run it inline so that it
215    // preempts any delta notifications which were scheduled while we were
216    // paused.
217    if (readyEvent.scheduled()) {
218        eq->deschedule(&readyEvent);
219        runReady();
220    }
221}
222
223void
224Scheduler::stop()
225{
226    _stopped = true;
227    kernel->stop();
228    scMain->run();
229}
230
231void
232Scheduler::start(Tick max_tick, bool run_to_time)
233{
234    // We should be running from sc_main. Keep track of that Fiber to return
235    // to later.
236    scMain = Fiber::currentFiber();
237
238    _started = true;
239    _paused = false;
240    _stopped = false;
241    runToTime = run_to_time;
242
243    maxTick = max_tick;
244
245    if (starved() && !runToTime)
246        return;
247
248    if (initReady) {
249        kernel->status(::sc_core::SC_RUNNING);
250        eq->schedule(&maxTickEvent, maxTick);
251    }
252
253    // Return to gem5 to let it run events, etc.
254    Fiber::primaryFiber()->run();
255
256    if (pauseEvent.scheduled())
257        eq->deschedule(&pauseEvent);
258    if (stopEvent.scheduled())
259        eq->deschedule(&stopEvent);
260    if (maxTickEvent.scheduled())
261        eq->deschedule(&maxTickEvent);
262    if (starvationEvent.scheduled())
263        eq->deschedule(&starvationEvent);
264}
265
266void
267Scheduler::schedulePause()
268{
269    if (pauseEvent.scheduled())
270        return;
271
272    eq->schedule(&pauseEvent, eq->getCurTick());
273}
274
275void
276Scheduler::scheduleStop(bool finish_delta)
277{
278    if (stopEvent.scheduled())
279        return;
280
281    if (!finish_delta) {
282        // If we're not supposed to finish the delta cycle, flush the list
283        // of ready processes and scheduled updates.
284        Process *p;
285        while ((p = readyList.getNext()))
286            p->popListNode();
287        Channel *c;
288        while ((c = updateList.getNext()))
289            c->popListNode();
290    }
291    eq->schedule(&stopEvent, eq->getCurTick());
292}
293
294Scheduler scheduler;
295
296} // namespace sc_gem5
297