scheduler.cc revision 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();
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    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
288