scheduler.cc revision 12995:3421144dd03e
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->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
287