scheduler.cc revision 12982:c7966254372e
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    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
251