scheduler.hh revision 12957:e54f9890363d
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#ifndef __SYSTEMC_CORE_SCHEDULER_HH__
31#define __SYSTEMC_CORE_SCHEDULER_HH__
32
33#include <vector>
34
35#include "sim/eventq.hh"
36#include "systemc/core/channel.hh"
37#include "systemc/core/list.hh"
38#include "systemc/core/process.hh"
39
40namespace sc_gem5
41{
42
43typedef NodeList<Process> ProcessList;
44typedef NodeList<Channel> ChannelList;
45
46/*
47 * The scheduler supports three different mechanisms, the initialization phase,
48 * delta cycles, and timed notifications.
49 *
50 * INITIALIZATION PHASE
51 *
52 * The initialization phase has three parts:
53 * 1. Run requested channel updates.
54 * 2. Make processes which need to initialize runnable (methods and threads
55 *    which didn't have dont_initialize called on them).
56 * 3. Process delta notifications.
57 *
58 * First, the Kernel SimObject calls the update() method during its startup()
59 * callback which handles the requested channel updates. The Kernel also
60 * schedules an event to be run at time 0 with a slightly elevated priority
61 * so that it happens before any "normal" event.
62 *
63 * When that t0 event happens, it calls the schedulers prepareForInit method
64 * which performs step 2 above. That indirectly causes the scheduler's
65 * readyEvent to be scheduled with slightly lowered priority, ensuring it
66 * happens after any "normal" event.
67 *
68 * Because delta notifications are scheduled at the standard priority, all
69 * of those events will happen next, performing step 3 above. Once they finish,
70 * if the readyEvent was scheduled above, there shouldn't be any higher
71 * priority events in front of it. When it runs, it will start the first
72 * evaluate phase of the first delta cycle.
73 *
74 * DELTA CYCLE
75 *
76 * A delta cycle has three phases within it.
77 * 1. The evaluate phase where runnable processes are allowed to run.
78 * 2. The update phase where requested channel updates hapen.
79 * 3. The delta notification phase where delta notifications happen.
80 *
81 * The readyEvent runs the first two steps of the delta cycle. It first goes
82 * through the list of runnable processes and executes them until the set is
83 * empty, and then immediately runs the update phase. Since these are all part
84 * of the same event, there's no chance for other events to intervene and
85 * break the required order above.
86 *
87 * During the update phase above, the spec forbids any action which would make
88 * a process runnable. That means that once the update phase finishes, the set
89 * of runnable processes will be empty. There may, however, have been some
90 * delta notifications/timeouts which will have been scheduled during either
91 * the evaluate or update phase above. Because those are scheduled at the
92 * normal priority, they will now happen together until there aren't any
93 * delta events left.
94 *
95 * If any processes became runnable during the delta notification phase, the
96 * readyEvent will have been scheduled and will have been waiting patiently
97 * behind the delta notification events. That will now run, effectively
98 * starting the next delta cycle.
99 *
100 * TIMED NOTIFICATION PHASE
101 *
102 * If no processes became runnable, the event queue will continue to process
103 * events until it comes across a timed notification, aka a notification
104 * scheduled to happen in the future. Like delta notification events, those
105 * will all happen together since the readyEvent priority is lower,
106 * potentially marking new processes as ready. Once these events finish, the
107 * readyEvent may run, starting the next delta cycle.
108 */
109
110class Scheduler
111{
112  public:
113    Scheduler();
114
115    const std::string name() const { return "systemc_scheduler"; }
116
117    uint64_t numCycles() { return _numCycles; }
118    Process *current() { return _current; }
119
120    // Prepare for initialization.
121    void prepareForInit();
122
123    // Register a process with the scheduler.
124    void reg(Process *p);
125
126    // Tell the scheduler not to initialize a process.
127    void dontInitialize(Process *p);
128
129    // Run the next process, if there is one.
130    void yield();
131
132    // Put a process on the ready list.
133    void ready(Process *p);
134
135    // Schedule an update for a given channel.
136    void requestUpdate(Channel *c);
137
138    // Run the given process immediately, preempting whatever may be running.
139    void
140    runNow(Process *p)
141    {
142        // If a process is running, schedule it/us to run again.
143        if (_current)
144            readyList.pushFirst(_current);
145        // Schedule p to run first.
146        readyList.pushFirst(p);
147        yield();
148    }
149
150    // Set an event queue for scheduling events.
151    void setEventQueue(EventQueue *_eq) { eq = _eq; }
152
153    // Retrieve the event queue.
154    EventQueue &eventQueue() const { return *eq; }
155
156    // Run scheduled channel updates.
157    void update();
158
159  private:
160    EventQueue *eq;
161
162    void runReady();
163    EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
164    void scheduleReadyEvent();
165
166    uint64_t _numCycles;
167
168    Process *_current;
169
170    bool initReady;
171
172    ProcessList initList;
173    ProcessList toFinalize;
174    ProcessList readyList;
175
176    ChannelList updateList;
177};
178
179extern Scheduler scheduler;
180
181} // namespace sc_gem5
182
183#endif // __SYSTEMC_CORE_SCHEDULER_H__
184