scheduler.hh (13244:deedec45898f) scheduler.hh (13245:c666c5d4996b)
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 <functional>
34#include <map>
35#include <set>
36#include <vector>
37
38#include "base/logging.hh"
39#include "sim/core.hh"
40#include "sim/eventq.hh"
41#include "systemc/core/channel.hh"
42#include "systemc/core/list.hh"
43#include "systemc/core/process.hh"
44#include "systemc/core/sched_event.hh"
45
46class Fiber;
47
48namespace sc_gem5
49{
50
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 <functional>
34#include <map>
35#include <set>
36#include <vector>
37
38#include "base/logging.hh"
39#include "sim/core.hh"
40#include "sim/eventq.hh"
41#include "systemc/core/channel.hh"
42#include "systemc/core/list.hh"
43#include "systemc/core/process.hh"
44#include "systemc/core/sched_event.hh"
45
46class Fiber;
47
48namespace sc_gem5
49{
50
51class TraceFile;
52
51typedef NodeList<Process> ProcessList;
52typedef NodeList<Channel> ChannelList;
53
54/*
55 * The scheduler supports three different mechanisms, the initialization phase,
56 * delta cycles, and timed notifications.
57 *
58 * INITIALIZATION PHASE
59 *
60 * The initialization phase has three parts:
61 * 1. Run requested channel updates.
62 * 2. Make processes which need to initialize runnable (methods and threads
63 * which didn't have dont_initialize called on them).
64 * 3. Process delta notifications.
65 *
66 * First, the Kernel SimObject calls the update() method during its startup()
67 * callback which handles the requested channel updates. The Kernel also
68 * schedules an event to be run at time 0 with a slightly elevated priority
69 * so that it happens before any "normal" event.
70 *
71 * When that t0 event happens, it calls the schedulers prepareForInit method
72 * which performs step 2 above. That indirectly causes the scheduler's
73 * readyEvent to be scheduled with slightly lowered priority, ensuring it
74 * happens after any "normal" event.
75 *
76 * Because delta notifications are scheduled at the standard priority, all
77 * of those events will happen next, performing step 3 above. Once they finish,
78 * if the readyEvent was scheduled above, there shouldn't be any higher
79 * priority events in front of it. When it runs, it will start the first
80 * evaluate phase of the first delta cycle.
81 *
82 * DELTA CYCLE
83 *
84 * A delta cycle has three phases within it.
85 * 1. The evaluate phase where runnable processes are allowed to run.
86 * 2. The update phase where requested channel updates hapen.
87 * 3. The delta notification phase where delta notifications happen.
88 *
89 * The readyEvent runs all three steps of the delta cycle. It first goes
90 * through the list of runnable processes and executes them until the set is
91 * empty, and then immediately runs the update phase. Since these are all part
92 * of the same event, there's no chance for other events to intervene and
93 * break the required order above.
94 *
95 * During the update phase above, the spec forbids any action which would make
96 * a process runnable. That means that once the update phase finishes, the set
97 * of runnable processes will be empty. There may, however, have been some
98 * delta notifications/timeouts which will have been scheduled during either
99 * the evaluate or update phase above. Those will have been accumulated in the
100 * scheduler, and are now all executed.
101 *
102 * If any processes became runnable during the delta notification phase, the
103 * readyEvent will have been scheduled and will be waiting and ready to run
104 * again, effectively starting the next delta cycle.
105 *
106 * TIMED NOTIFICATION PHASE
107 *
108 * If no processes became runnable, the event queue will continue to process
109 * events until it comes across an event which represents all the timed
110 * notifications which are supposed to happen at a particular time. The object
111 * which tracks them will execute all those notifications, and then destroy
112 * itself. If the readyEvent is now ready to run, the next delta cycle will
113 * start.
114 *
115 * PAUSE/STOP
116 *
117 * To inject a pause from sc_pause which should happen after the current delta
118 * cycle's delta notification phase, an event is scheduled with a lower than
119 * normal priority, but higher than the readyEvent. That ensures that any
120 * delta notifications which are scheduled with normal priority will happen
121 * first, since those are part of the current delta cycle. Then the pause
122 * event will happen before the next readyEvent which would start the next
123 * delta cycle. All of these events are scheduled for the current time, and so
124 * would happen before any timed notifications went off.
125 *
126 * To inject a stop from sc_stop, the delta cycles should stop before even the
127 * delta notifications have happened, but after the evaluate and update phases.
128 * For that, a stop event with slightly higher than normal priority will be
129 * scheduled so that it happens before any of the delta notification events
130 * which are at normal priority.
131 *
132 * MAX RUN TIME
133 *
134 * When sc_start is called, it's possible to pass in a maximum time the
135 * simulation should run to, at which point sc_pause is implicitly called. The
136 * simulation is supposed to run up to the latest timed notification phase
137 * which is less than or equal to the maximum time. In other words it should
138 * run timed notifications at the maximum time, but not the subsequent evaluate
139 * phase. That's implemented by scheduling an event at the max time with a
140 * priority which is lower than all the others except the ready event. Timed
141 * notifications will happen before it fires, but it will override any ready
142 * event and prevent the evaluate phase from starting.
143 */
144
145class Scheduler
146{
147 public:
148 typedef std::list<ScEvent *> ScEvents;
149
150 class TimeSlot : public ::Event
151 {
152 public:
153 TimeSlot() : ::Event(Default_Pri, AutoDelete) {}
154
155 ScEvents events;
156 void process();
157 };
158
159 typedef std::map<Tick, TimeSlot *> TimeSlots;
160
161 Scheduler();
162 ~Scheduler();
163
164 void clear();
165
166 const std::string name() const { return "systemc_scheduler"; }
167
168 uint64_t numCycles() { return _numCycles; }
169 Process *current() { return _current; }
170
171 void initPhase();
172
173 // Register a process with the scheduler.
174 void reg(Process *p);
175
176 // Run the next process, if there is one.
177 void yield();
178
179 // Put a process on the ready list.
180 void ready(Process *p);
181
182 // Mark a process as ready if init is finished, or put it on the list of
183 // processes to be initialized.
184 void resume(Process *p);
185
186 // Remove a process from the ready/init list if it was on one of them, and
187 // return if it was.
188 bool suspend(Process *p);
189
190 // Schedule an update for a given channel.
191 void requestUpdate(Channel *c);
192
193 // Run the given process immediately, preempting whatever may be running.
194 void
195 runNow(Process *p)
196 {
197 // This function may put a process on the wrong list, ie a thread
198 // the method list. That's fine since that's just a performance
199 // optimization, and the important thing here is how the processes are
200 // ordered.
201
202 // If a process is running, schedule it/us to run again.
203 if (_current)
204 readyListMethods.pushFirst(_current);
205 // Schedule p to run first.
206 readyListMethods.pushFirst(p);
207 yield();
208 }
209
210 // Set an event queue for scheduling events.
211 void setEventQueue(EventQueue *_eq) { eq = _eq; }
212
213 // Get the current time according to gem5.
214 Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
215
216 Tick
217 delayed(const ::sc_core::sc_time &delay)
218 {
219 //XXX We're assuming the systemc time resolution is in ps.
220 return getCurTick() + delay.value() * SimClock::Int::ps;
221 }
222
223 // For scheduling delayed/timed notifications/timeouts.
224 void
225 schedule(ScEvent *event, const ::sc_core::sc_time &delay)
226 {
227 Tick tick = delayed(delay);
228 if (tick < getCurTick())
229 tick = getCurTick();
230
231 // Delta notification/timeout.
232 if (delay.value() == 0) {
233 event->schedule(deltas, tick);
234 if (!inEvaluate() && !inUpdate())
235 scheduleReadyEvent();
236 return;
237 }
238
239 // Timed notification/timeout.
240 TimeSlot *&ts = timeSlots[tick];
241 if (!ts) {
242 ts = new TimeSlot;
243 schedule(ts, tick);
244 }
245 event->schedule(ts->events, tick);
246 }
247
248 // For descheduling delayed/timed notifications/timeouts.
249 void
250 deschedule(ScEvent *event)
251 {
252 ScEvents *on = event->scheduledOn();
253
254 if (on == &deltas) {
255 event->deschedule();
256 return;
257 }
258
259 // Timed notification/timeout.
260 auto tsit = timeSlots.find(event->when());
261 panic_if(tsit == timeSlots.end(),
262 "Descheduling event at time with no events.");
263 TimeSlot *ts = tsit->second;
264 ScEvents &events = ts->events;
265 assert(on == &events);
266 event->deschedule();
267
268 // If no more events are happening at this time slot, get rid of it.
269 if (events.empty()) {
270 deschedule(ts);
271 timeSlots.erase(tsit);
272 }
273 }
274
275 void
276 completeTimeSlot(TimeSlot *ts)
277 {
278 _changeStamp++;
279 assert(ts == timeSlots.begin()->second);
280 timeSlots.erase(timeSlots.begin());
281 if (!runToTime && starved())
282 scheduleStarvationEvent();
53typedef NodeList<Process> ProcessList;
54typedef NodeList<Channel> ChannelList;
55
56/*
57 * The scheduler supports three different mechanisms, the initialization phase,
58 * delta cycles, and timed notifications.
59 *
60 * INITIALIZATION PHASE
61 *
62 * The initialization phase has three parts:
63 * 1. Run requested channel updates.
64 * 2. Make processes which need to initialize runnable (methods and threads
65 * which didn't have dont_initialize called on them).
66 * 3. Process delta notifications.
67 *
68 * First, the Kernel SimObject calls the update() method during its startup()
69 * callback which handles the requested channel updates. The Kernel also
70 * schedules an event to be run at time 0 with a slightly elevated priority
71 * so that it happens before any "normal" event.
72 *
73 * When that t0 event happens, it calls the schedulers prepareForInit method
74 * which performs step 2 above. That indirectly causes the scheduler's
75 * readyEvent to be scheduled with slightly lowered priority, ensuring it
76 * happens after any "normal" event.
77 *
78 * Because delta notifications are scheduled at the standard priority, all
79 * of those events will happen next, performing step 3 above. Once they finish,
80 * if the readyEvent was scheduled above, there shouldn't be any higher
81 * priority events in front of it. When it runs, it will start the first
82 * evaluate phase of the first delta cycle.
83 *
84 * DELTA CYCLE
85 *
86 * A delta cycle has three phases within it.
87 * 1. The evaluate phase where runnable processes are allowed to run.
88 * 2. The update phase where requested channel updates hapen.
89 * 3. The delta notification phase where delta notifications happen.
90 *
91 * The readyEvent runs all three steps of the delta cycle. It first goes
92 * through the list of runnable processes and executes them until the set is
93 * empty, and then immediately runs the update phase. Since these are all part
94 * of the same event, there's no chance for other events to intervene and
95 * break the required order above.
96 *
97 * During the update phase above, the spec forbids any action which would make
98 * a process runnable. That means that once the update phase finishes, the set
99 * of runnable processes will be empty. There may, however, have been some
100 * delta notifications/timeouts which will have been scheduled during either
101 * the evaluate or update phase above. Those will have been accumulated in the
102 * scheduler, and are now all executed.
103 *
104 * If any processes became runnable during the delta notification phase, the
105 * readyEvent will have been scheduled and will be waiting and ready to run
106 * again, effectively starting the next delta cycle.
107 *
108 * TIMED NOTIFICATION PHASE
109 *
110 * If no processes became runnable, the event queue will continue to process
111 * events until it comes across an event which represents all the timed
112 * notifications which are supposed to happen at a particular time. The object
113 * which tracks them will execute all those notifications, and then destroy
114 * itself. If the readyEvent is now ready to run, the next delta cycle will
115 * start.
116 *
117 * PAUSE/STOP
118 *
119 * To inject a pause from sc_pause which should happen after the current delta
120 * cycle's delta notification phase, an event is scheduled with a lower than
121 * normal priority, but higher than the readyEvent. That ensures that any
122 * delta notifications which are scheduled with normal priority will happen
123 * first, since those are part of the current delta cycle. Then the pause
124 * event will happen before the next readyEvent which would start the next
125 * delta cycle. All of these events are scheduled for the current time, and so
126 * would happen before any timed notifications went off.
127 *
128 * To inject a stop from sc_stop, the delta cycles should stop before even the
129 * delta notifications have happened, but after the evaluate and update phases.
130 * For that, a stop event with slightly higher than normal priority will be
131 * scheduled so that it happens before any of the delta notification events
132 * which are at normal priority.
133 *
134 * MAX RUN TIME
135 *
136 * When sc_start is called, it's possible to pass in a maximum time the
137 * simulation should run to, at which point sc_pause is implicitly called. The
138 * simulation is supposed to run up to the latest timed notification phase
139 * which is less than or equal to the maximum time. In other words it should
140 * run timed notifications at the maximum time, but not the subsequent evaluate
141 * phase. That's implemented by scheduling an event at the max time with a
142 * priority which is lower than all the others except the ready event. Timed
143 * notifications will happen before it fires, but it will override any ready
144 * event and prevent the evaluate phase from starting.
145 */
146
147class Scheduler
148{
149 public:
150 typedef std::list<ScEvent *> ScEvents;
151
152 class TimeSlot : public ::Event
153 {
154 public:
155 TimeSlot() : ::Event(Default_Pri, AutoDelete) {}
156
157 ScEvents events;
158 void process();
159 };
160
161 typedef std::map<Tick, TimeSlot *> TimeSlots;
162
163 Scheduler();
164 ~Scheduler();
165
166 void clear();
167
168 const std::string name() const { return "systemc_scheduler"; }
169
170 uint64_t numCycles() { return _numCycles; }
171 Process *current() { return _current; }
172
173 void initPhase();
174
175 // Register a process with the scheduler.
176 void reg(Process *p);
177
178 // Run the next process, if there is one.
179 void yield();
180
181 // Put a process on the ready list.
182 void ready(Process *p);
183
184 // Mark a process as ready if init is finished, or put it on the list of
185 // processes to be initialized.
186 void resume(Process *p);
187
188 // Remove a process from the ready/init list if it was on one of them, and
189 // return if it was.
190 bool suspend(Process *p);
191
192 // Schedule an update for a given channel.
193 void requestUpdate(Channel *c);
194
195 // Run the given process immediately, preempting whatever may be running.
196 void
197 runNow(Process *p)
198 {
199 // This function may put a process on the wrong list, ie a thread
200 // the method list. That's fine since that's just a performance
201 // optimization, and the important thing here is how the processes are
202 // ordered.
203
204 // If a process is running, schedule it/us to run again.
205 if (_current)
206 readyListMethods.pushFirst(_current);
207 // Schedule p to run first.
208 readyListMethods.pushFirst(p);
209 yield();
210 }
211
212 // Set an event queue for scheduling events.
213 void setEventQueue(EventQueue *_eq) { eq = _eq; }
214
215 // Get the current time according to gem5.
216 Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
217
218 Tick
219 delayed(const ::sc_core::sc_time &delay)
220 {
221 //XXX We're assuming the systemc time resolution is in ps.
222 return getCurTick() + delay.value() * SimClock::Int::ps;
223 }
224
225 // For scheduling delayed/timed notifications/timeouts.
226 void
227 schedule(ScEvent *event, const ::sc_core::sc_time &delay)
228 {
229 Tick tick = delayed(delay);
230 if (tick < getCurTick())
231 tick = getCurTick();
232
233 // Delta notification/timeout.
234 if (delay.value() == 0) {
235 event->schedule(deltas, tick);
236 if (!inEvaluate() && !inUpdate())
237 scheduleReadyEvent();
238 return;
239 }
240
241 // Timed notification/timeout.
242 TimeSlot *&ts = timeSlots[tick];
243 if (!ts) {
244 ts = new TimeSlot;
245 schedule(ts, tick);
246 }
247 event->schedule(ts->events, tick);
248 }
249
250 // For descheduling delayed/timed notifications/timeouts.
251 void
252 deschedule(ScEvent *event)
253 {
254 ScEvents *on = event->scheduledOn();
255
256 if (on == &deltas) {
257 event->deschedule();
258 return;
259 }
260
261 // Timed notification/timeout.
262 auto tsit = timeSlots.find(event->when());
263 panic_if(tsit == timeSlots.end(),
264 "Descheduling event at time with no events.");
265 TimeSlot *ts = tsit->second;
266 ScEvents &events = ts->events;
267 assert(on == &events);
268 event->deschedule();
269
270 // If no more events are happening at this time slot, get rid of it.
271 if (events.empty()) {
272 deschedule(ts);
273 timeSlots.erase(tsit);
274 }
275 }
276
277 void
278 completeTimeSlot(TimeSlot *ts)
279 {
280 _changeStamp++;
281 assert(ts == timeSlots.begin()->second);
282 timeSlots.erase(timeSlots.begin());
283 if (!runToTime && starved())
284 scheduleStarvationEvent();
285 scheduleTimeAdvancesEvent();
283 }
284
285 // Pending activity ignores gem5 activity, much like how a systemc
286 // simulation wouldn't know about asynchronous external events (socket IO
287 // for instance) that might happen before time advances in a pure
288 // systemc simulation. Also the spec lists what specific types of pending
289 // activity needs to be counted, which obviously doesn't include gem5
290 // events.
291
292 // Return whether there's pending systemc activity at this time.
293 bool
294 pendingCurr()
295 {
296 return !readyListMethods.empty() || !readyListThreads.empty() ||
297 !updateList.empty() || !deltas.empty();
298 }
299
300 // Return whether there are pending timed notifications or timeouts.
301 bool
302 pendingFuture()
303 {
304 return !timeSlots.empty();
305 }
306
307 // Return how many ticks there are until the first pending event, if any.
308 Tick
309 timeToPending()
310 {
311 if (pendingCurr())
312 return 0;
313 if (pendingFuture())
314 return timeSlots.begin()->first - getCurTick();
315 return MaxTick - getCurTick();
316 }
317
318 // Run scheduled channel updates.
319 void runUpdate();
320
321 // Run delta events.
322 void runDelta();
323
324 void setScMainFiber(Fiber *sc_main) { scMain = sc_main; }
325
326 void start(Tick max_tick, bool run_to_time);
327 void oneCycle();
328
329 void schedulePause();
330 void scheduleStop(bool finish_delta);
331
332 enum Status
333 {
334 StatusOther = 0,
335 StatusEvaluate,
336 StatusUpdate,
337 StatusDelta,
338 StatusTiming,
339 StatusPaused,
340 StatusStopped
341 };
342
343 bool elaborationDone() { return _elaborationDone; }
344 void elaborationDone(bool b) { _elaborationDone = b; }
345
346 bool paused() { return status() == StatusPaused; }
347 bool stopped() { return status() == StatusStopped; }
348 bool inEvaluate() { return status() == StatusEvaluate; }
349 bool inUpdate() { return status() == StatusUpdate; }
350 bool inDelta() { return status() == StatusDelta; }
351 bool inTiming() { return status() == StatusTiming; }
352
353 uint64_t changeStamp() { return _changeStamp; }
354
355 void throwToScMain(const ::sc_core::sc_report *r=nullptr);
356
357 Status status() { return _status; }
358 void status(Status s) { _status = s; }
359
286 }
287
288 // Pending activity ignores gem5 activity, much like how a systemc
289 // simulation wouldn't know about asynchronous external events (socket IO
290 // for instance) that might happen before time advances in a pure
291 // systemc simulation. Also the spec lists what specific types of pending
292 // activity needs to be counted, which obviously doesn't include gem5
293 // events.
294
295 // Return whether there's pending systemc activity at this time.
296 bool
297 pendingCurr()
298 {
299 return !readyListMethods.empty() || !readyListThreads.empty() ||
300 !updateList.empty() || !deltas.empty();
301 }
302
303 // Return whether there are pending timed notifications or timeouts.
304 bool
305 pendingFuture()
306 {
307 return !timeSlots.empty();
308 }
309
310 // Return how many ticks there are until the first pending event, if any.
311 Tick
312 timeToPending()
313 {
314 if (pendingCurr())
315 return 0;
316 if (pendingFuture())
317 return timeSlots.begin()->first - getCurTick();
318 return MaxTick - getCurTick();
319 }
320
321 // Run scheduled channel updates.
322 void runUpdate();
323
324 // Run delta events.
325 void runDelta();
326
327 void setScMainFiber(Fiber *sc_main) { scMain = sc_main; }
328
329 void start(Tick max_tick, bool run_to_time);
330 void oneCycle();
331
332 void schedulePause();
333 void scheduleStop(bool finish_delta);
334
335 enum Status
336 {
337 StatusOther = 0,
338 StatusEvaluate,
339 StatusUpdate,
340 StatusDelta,
341 StatusTiming,
342 StatusPaused,
343 StatusStopped
344 };
345
346 bool elaborationDone() { return _elaborationDone; }
347 void elaborationDone(bool b) { _elaborationDone = b; }
348
349 bool paused() { return status() == StatusPaused; }
350 bool stopped() { return status() == StatusStopped; }
351 bool inEvaluate() { return status() == StatusEvaluate; }
352 bool inUpdate() { return status() == StatusUpdate; }
353 bool inDelta() { return status() == StatusDelta; }
354 bool inTiming() { return status() == StatusTiming; }
355
356 uint64_t changeStamp() { return _changeStamp; }
357
358 void throwToScMain(const ::sc_core::sc_report *r=nullptr);
359
360 Status status() { return _status; }
361 void status(Status s) { _status = s; }
362
363 void registerTraceFile(TraceFile *tf) { traceFiles.insert(tf); }
364 void unregisterTraceFile(TraceFile *tf) { traceFiles.erase(tf); }
365
360 private:
361 typedef const EventBase::Priority Priority;
362 static Priority DefaultPriority = EventBase::Default_Pri;
363
364 static Priority StopPriority = DefaultPriority - 1;
365 static Priority PausePriority = DefaultPriority + 1;
366 static Priority MaxTickPriority = DefaultPriority + 2;
367 static Priority ReadyPriority = DefaultPriority + 3;
368 static Priority StarvationPriority = ReadyPriority;
366 private:
367 typedef const EventBase::Priority Priority;
368 static Priority DefaultPriority = EventBase::Default_Pri;
369
370 static Priority StopPriority = DefaultPriority - 1;
371 static Priority PausePriority = DefaultPriority + 1;
372 static Priority MaxTickPriority = DefaultPriority + 2;
373 static Priority ReadyPriority = DefaultPriority + 3;
374 static Priority StarvationPriority = ReadyPriority;
375 static Priority TimeAdvancesPriority = EventBase::Maximum_Pri;
369
370 EventQueue *eq;
371
372 // For gem5 style events.
373 void
374 schedule(::Event *event, Tick tick)
375 {
376 if (initDone)
377 eq->schedule(event, tick);
378 else
379 eventsToSchedule[event] = tick;
380 }
381
382 void schedule(::Event *event) { schedule(event, getCurTick()); }
383
384 void
385 deschedule(::Event *event)
386 {
387 if (initDone)
388 eq->deschedule(event);
389 else
390 eventsToSchedule.erase(event);
391 }
392
393 ScEvents deltas;
394 TimeSlots timeSlots;
395
396 Process *
397 getNextReady()
398 {
399 Process *p = readyListMethods.getNext();
400 return p ? p : readyListThreads.getNext();
401 }
402
403 void runReady();
404 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
405 void scheduleReadyEvent();
406
407 void pause();
408 void stop();
409 EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
410 EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
411
412 Fiber *scMain;
413 const ::sc_core::sc_report *_throwToScMain;
414
415 bool
416 starved()
417 {
418 return (readyListMethods.empty() && readyListThreads.empty() &&
419 updateList.empty() && deltas.empty() &&
420 (timeSlots.empty() || timeSlots.begin()->first > maxTick) &&
421 initList.empty());
422 }
423 EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
424 void scheduleStarvationEvent();
425
426 bool _elaborationDone;
427 bool _started;
428 bool _stopNow;
429
430 Status _status;
431
432 Tick maxTick;
433 Tick lastReadyTick;
434 void
435 maxTickFunc()
436 {
437 if (lastReadyTick != getCurTick())
438 _changeStamp++;
439 pause();
440 }
441 EventWrapper<Scheduler, &Scheduler::maxTickFunc> maxTickEvent;
442
376
377 EventQueue *eq;
378
379 // For gem5 style events.
380 void
381 schedule(::Event *event, Tick tick)
382 {
383 if (initDone)
384 eq->schedule(event, tick);
385 else
386 eventsToSchedule[event] = tick;
387 }
388
389 void schedule(::Event *event) { schedule(event, getCurTick()); }
390
391 void
392 deschedule(::Event *event)
393 {
394 if (initDone)
395 eq->deschedule(event);
396 else
397 eventsToSchedule.erase(event);
398 }
399
400 ScEvents deltas;
401 TimeSlots timeSlots;
402
403 Process *
404 getNextReady()
405 {
406 Process *p = readyListMethods.getNext();
407 return p ? p : readyListThreads.getNext();
408 }
409
410 void runReady();
411 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
412 void scheduleReadyEvent();
413
414 void pause();
415 void stop();
416 EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
417 EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
418
419 Fiber *scMain;
420 const ::sc_core::sc_report *_throwToScMain;
421
422 bool
423 starved()
424 {
425 return (readyListMethods.empty() && readyListThreads.empty() &&
426 updateList.empty() && deltas.empty() &&
427 (timeSlots.empty() || timeSlots.begin()->first > maxTick) &&
428 initList.empty());
429 }
430 EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
431 void scheduleStarvationEvent();
432
433 bool _elaborationDone;
434 bool _started;
435 bool _stopNow;
436
437 Status _status;
438
439 Tick maxTick;
440 Tick lastReadyTick;
441 void
442 maxTickFunc()
443 {
444 if (lastReadyTick != getCurTick())
445 _changeStamp++;
446 pause();
447 }
448 EventWrapper<Scheduler, &Scheduler::maxTickFunc> maxTickEvent;
449
450 void timeAdvances() { trace(false); }
451 EventWrapper<Scheduler, &Scheduler::timeAdvances> timeAdvancesEvent;
452 void
453 scheduleTimeAdvancesEvent()
454 {
455 if (!traceFiles.empty() && !timeAdvancesEvent.scheduled())
456 schedule(&timeAdvancesEvent);
457 }
458
443 uint64_t _numCycles;
444 uint64_t _changeStamp;
445
446 Process *_current;
447
448 bool initDone;
449 bool runToTime;
450 bool runOnce;
451
452 ProcessList initList;
453
454 ProcessList readyListMethods;
455 ProcessList readyListThreads;
456
457 ChannelList updateList;
458
459 std::map<::Event *, Tick> eventsToSchedule;
459 uint64_t _numCycles;
460 uint64_t _changeStamp;
461
462 Process *_current;
463
464 bool initDone;
465 bool runToTime;
466 bool runOnce;
467
468 ProcessList initList;
469
470 ProcessList readyListMethods;
471 ProcessList readyListThreads;
472
473 ChannelList updateList;
474
475 std::map<::Event *, Tick> eventsToSchedule;
476
477 std::set<TraceFile *> traceFiles;
478
479 void trace(bool delta);
460};
461
462extern Scheduler scheduler;
463
464inline void
465Scheduler::TimeSlot::process()
466{
467 scheduler.status(StatusTiming);
468
469 try {
470 while (!events.empty())
471 events.front()->run();
472 } catch (...) {
473 if (events.empty())
474 scheduler.completeTimeSlot(this);
475 else
476 scheduler.schedule(this);
477 scheduler.throwToScMain();
478 }
479
480 scheduler.status(StatusOther);
481 scheduler.completeTimeSlot(this);
482}
483
484const ::sc_core::sc_report *reportifyException();
485
486} // namespace sc_gem5
487
488#endif // __SYSTEMC_CORE_SCHEDULER_H__
480};
481
482extern Scheduler scheduler;
483
484inline void
485Scheduler::TimeSlot::process()
486{
487 scheduler.status(StatusTiming);
488
489 try {
490 while (!events.empty())
491 events.front()->run();
492 } catch (...) {
493 if (events.empty())
494 scheduler.completeTimeSlot(this);
495 else
496 scheduler.schedule(this);
497 scheduler.throwToScMain();
498 }
499
500 scheduler.status(StatusOther);
501 scheduler.completeTimeSlot(this);
502}
503
504const ::sc_core::sc_report *reportifyException();
505
506} // namespace sc_gem5
507
508#endif // __SYSTEMC_CORE_SCHEDULER_H__