scheduler.cc revision 12959:33d9a39e40a3
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
36namespace sc_gem5
37{
38
39Scheduler::Scheduler() :
40    eq(nullptr), readyEvent(this, false, EventBase::Default_Pri + 1),
41    _numCycles(0), _current(nullptr), initReady(false)
42{}
43
44void
45Scheduler::prepareForInit()
46{
47    for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) {
48        p->finalize();
49        p->popListNode();
50    }
51
52    for (Process *p = initList.getNext(); p; p = initList.getNext()) {
53        p->finalize();
54        p->ready();
55    }
56
57    initReady = true;
58}
59
60void
61Scheduler::reg(Process *p)
62{
63    if (initReady) {
64        // If we're past initialization, finalize static sensitivity.
65        p->finalize();
66        // Mark the process as ready.
67        p->ready();
68    } else {
69        // Otherwise, record that this process should be initialized once we
70        // get there.
71        initList.pushLast(p);
72    }
73}
74
75void
76Scheduler::dontInitialize(Process *p)
77{
78    if (initReady) {
79        // Pop this process off of the ready list.
80        p->popListNode();
81    } else {
82        // Push this process onto the list of processes which still need
83        // their static sensitivity to be finalized. That implicitly pops it
84        // off the list of processes to be initialized/marked ready.
85        toFinalize.pushLast(p);
86    }
87}
88
89void
90Scheduler::yield()
91{
92    _current = readyList.getNext();
93    if (!_current) {
94        // There are no more processes, so return control to evaluate.
95        Fiber::primaryFiber()->run();
96    } else {
97        _current->popListNode();
98        // Switch to whatever Fiber is supposed to run this process. All
99        // Fibers which aren't running should be parked at this line.
100        _current->fiber()->run();
101        // If the current process hasn't been started yet, start it. This
102        // should always be true for methods, but may not be true for threads.
103        if (_current && !_current->running())
104            _current->run();
105    }
106}
107
108void
109Scheduler::ready(Process *p)
110{
111    // Clump methods together to minimize context switching.
112    if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
113        readyList.pushFirst(p);
114    else
115        readyList.pushLast(p);
116
117    scheduleReadyEvent();
118}
119
120void
121Scheduler::requestUpdate(Channel *c)
122{
123    updateList.pushLast(c);
124    scheduleReadyEvent();
125}
126
127void
128Scheduler::scheduleReadyEvent()
129{
130    // Schedule the evaluate and update phases.
131    if (!readyEvent.scheduled()) {
132        panic_if(!eq, "Need to schedule ready, but no event manager.\n");
133        eq->schedule(&readyEvent, eq->getCurTick());
134    }
135}
136
137void
138Scheduler::runReady()
139{
140    bool empty = readyList.empty();
141
142    // The evaluation phase.
143    do {
144        yield();
145    } while (!readyList.empty());
146
147    if (!empty)
148        _numCycles++;
149
150    // The update phase.
151    update();
152
153    // The delta phase will happen naturally through the event queue.
154}
155
156void
157Scheduler::update()
158{
159    Channel *channel = updateList.getNext();
160    while (channel) {
161        channel->popListNode();
162        channel->update();
163        channel = updateList.getNext();
164    }
165}
166
167Scheduler scheduler;
168
169} // namespace sc_gem5
170