scheduler.hh (13078:f11496886d1a) scheduler.hh (13096:9295fa397b3f)
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
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::set<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 // Tell the scheduler not to initialize a process.
177 void dontInitialize(Process *p);
178
179 // Run the next process, if there is one.
180 void yield();
181
182 // Put a process on the ready list.
183 void ready(Process *p);
184
185 // Schedule an update for a given channel.
186 void requestUpdate(Channel *c);
187
188 // Run the given process immediately, preempting whatever may be running.
189 void
190 runNow(Process *p)
191 {
192 // If a process is running, schedule it/us to run again.
193 if (_current)
194 readyList.pushFirst(_current);
195 // Schedule p to run first.
196 readyList.pushFirst(p);
197 yield();
198 }
199
200 // Set an event queue for scheduling events.
201 void setEventQueue(EventQueue *_eq) { eq = _eq; }
202
203 // Get the current time according to gem5.
204 Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
205
206 Tick
207 delayed(const ::sc_core::sc_time &delay)
208 {
209 //XXX We're assuming the systemc time resolution is in ps.
210 return getCurTick() + delay.value() * SimClock::Int::ps;
211 }
212
213 // For scheduling delayed/timed notifications/timeouts.
214 void
215 schedule(ScEvent *event, const ::sc_core::sc_time &delay)
216 {
217 Tick tick = delayed(delay);
218 event->schedule(tick);
219
220 // Delta notification/timeout.
221 if (delay.value() == 0) {
222 deltas.insert(event);
223 scheduleReadyEvent();
224 return;
225 }
226
227 // Timed notification/timeout.
228 TimeSlot *&ts = timeSlots[tick];
229 if (!ts) {
230 ts = new TimeSlot;
231 schedule(ts, tick);
232 }
233 ts->events.insert(event);
234 }
235
236 // For descheduling delayed/timed notifications/timeouts.
237 void
238 deschedule(ScEvent *event)
239 {
240 if (event->when() == getCurTick()) {
241 // Attempt to remove from delta notifications.
242 if (deltas.erase(event) == 1) {
243 event->deschedule();
244 return;
245 }
246 }
247
248 // Timed notification/timeout.
249 auto tsit = timeSlots.find(event->when());
250 panic_if(tsit == timeSlots.end(),
251 "Descheduling event at time with no events.");
252 TimeSlot *ts = tsit->second;
253 ScEvents &events = ts->events;
254 assert(events.erase(event));
255 event->deschedule();
256
257 // If no more events are happening at this time slot, get rid of it.
258 if (events.empty()) {
259 deschedule(ts);
260 timeSlots.erase(tsit);
261 }
262 }
263
264 void
265 completeTimeSlot(TimeSlot *ts)
266 {
267 assert(ts == timeSlots.begin()->second);
268 timeSlots.erase(timeSlots.begin());
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
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::set<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 // Tell the scheduler not to initialize a process.
177 void dontInitialize(Process *p);
178
179 // Run the next process, if there is one.
180 void yield();
181
182 // Put a process on the ready list.
183 void ready(Process *p);
184
185 // Schedule an update for a given channel.
186 void requestUpdate(Channel *c);
187
188 // Run the given process immediately, preempting whatever may be running.
189 void
190 runNow(Process *p)
191 {
192 // If a process is running, schedule it/us to run again.
193 if (_current)
194 readyList.pushFirst(_current);
195 // Schedule p to run first.
196 readyList.pushFirst(p);
197 yield();
198 }
199
200 // Set an event queue for scheduling events.
201 void setEventQueue(EventQueue *_eq) { eq = _eq; }
202
203 // Get the current time according to gem5.
204 Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
205
206 Tick
207 delayed(const ::sc_core::sc_time &delay)
208 {
209 //XXX We're assuming the systemc time resolution is in ps.
210 return getCurTick() + delay.value() * SimClock::Int::ps;
211 }
212
213 // For scheduling delayed/timed notifications/timeouts.
214 void
215 schedule(ScEvent *event, const ::sc_core::sc_time &delay)
216 {
217 Tick tick = delayed(delay);
218 event->schedule(tick);
219
220 // Delta notification/timeout.
221 if (delay.value() == 0) {
222 deltas.insert(event);
223 scheduleReadyEvent();
224 return;
225 }
226
227 // Timed notification/timeout.
228 TimeSlot *&ts = timeSlots[tick];
229 if (!ts) {
230 ts = new TimeSlot;
231 schedule(ts, tick);
232 }
233 ts->events.insert(event);
234 }
235
236 // For descheduling delayed/timed notifications/timeouts.
237 void
238 deschedule(ScEvent *event)
239 {
240 if (event->when() == getCurTick()) {
241 // Attempt to remove from delta notifications.
242 if (deltas.erase(event) == 1) {
243 event->deschedule();
244 return;
245 }
246 }
247
248 // Timed notification/timeout.
249 auto tsit = timeSlots.find(event->when());
250 panic_if(tsit == timeSlots.end(),
251 "Descheduling event at time with no events.");
252 TimeSlot *ts = tsit->second;
253 ScEvents &events = ts->events;
254 assert(events.erase(event));
255 event->deschedule();
256
257 // If no more events are happening at this time slot, get rid of it.
258 if (events.empty()) {
259 deschedule(ts);
260 timeSlots.erase(tsit);
261 }
262 }
263
264 void
265 completeTimeSlot(TimeSlot *ts)
266 {
267 assert(ts == timeSlots.begin()->second);
268 timeSlots.erase(timeSlots.begin());
269 if (!runToTime && starved())
270 scheduleStarvationEvent();
269 }
270
271 // Pending activity ignores gem5 activity, much like how a systemc
272 // simulation wouldn't know about asynchronous external events (socket IO
273 // for instance) that might happen before time advances in a pure
274 // systemc simulation. Also the spec lists what specific types of pending
275 // activity needs to be counted, which obviously doesn't include gem5
276 // events.
277
278 // Return whether there's pending systemc activity at this time.
279 bool
280 pendingCurr()
281 {
282 return !readyList.empty() || !updateList.empty() || !deltas.empty();
283 }
284
285 // Return whether there are pending timed notifications or timeouts.
286 bool
287 pendingFuture()
288 {
289 return !timeSlots.empty();
290 }
291
292 // Return how many ticks there are until the first pending event, if any.
293 Tick
294 timeToPending()
295 {
296 if (pendingCurr())
297 return 0;
298 if (pendingFuture())
299 return timeSlots.begin()->first - getCurTick();
300 return MaxTick - getCurTick();
301 }
302
303 // Run scheduled channel updates.
304 void update();
305
306 void setScMainFiber(Fiber *sc_main) { scMain = sc_main; }
307
308 void start(Tick max_tick, bool run_to_time);
309 void oneCycle();
310
311 void schedulePause();
312 void scheduleStop(bool finish_delta);
313
314 bool paused() { return _paused; }
315 bool stopped() { return _stopped; }
316
317 private:
318 typedef const EventBase::Priority Priority;
319 static Priority DefaultPriority = EventBase::Default_Pri;
320
321 static Priority StopPriority = DefaultPriority - 1;
322 static Priority PausePriority = DefaultPriority + 1;
323 static Priority MaxTickPriority = DefaultPriority + 2;
324 static Priority ReadyPriority = DefaultPriority + 3;
325 static Priority StarvationPriority = ReadyPriority;
326
327 EventQueue *eq;
328
329 // For gem5 style events.
330 void
331 schedule(::Event *event, Tick tick)
332 {
333 if (initDone)
334 eq->schedule(event, tick);
335 else
336 eventsToSchedule[event] = tick;
337 }
338
339 void schedule(::Event *event) { schedule(event, getCurTick()); }
340
341 void
342 deschedule(::Event *event)
343 {
344 if (initDone)
345 eq->deschedule(event);
346 else
347 eventsToSchedule.erase(event);
348 }
349
350 ScEvents deltas;
351 TimeSlots timeSlots;
352
353 void runReady();
354 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
355 void scheduleReadyEvent();
356
357 void pause();
358 void stop();
359 EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
360 EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
361 Fiber *scMain;
362
363 bool
364 starved()
365 {
366 return (readyList.empty() && updateList.empty() && deltas.empty() &&
367 (timeSlots.empty() || timeSlots.begin()->first > maxTick) &&
368 initList.empty());
369 }
370 EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
371 void scheduleStarvationEvent();
372
373 bool _started;
374 bool _paused;
375 bool _stopped;
376
377 Tick maxTick;
378 EventWrapper<Scheduler, &Scheduler::pause> maxTickEvent;
379
380 uint64_t _numCycles;
381
382 Process *_current;
383
384 bool initDone;
385 bool runToTime;
386 bool runOnce;
387
388 ProcessList initList;
389 ProcessList toFinalize;
390 ProcessList readyList;
391
392 ChannelList updateList;
393
394 std::map<::Event *, Tick> eventsToSchedule;
395};
396
397extern Scheduler scheduler;
398
399inline void
400Scheduler::TimeSlot::process()
401{
402 for (auto &e: events)
403 e->run();
404 scheduler.completeTimeSlot(this);
405}
406
407} // namespace sc_gem5
408
409#endif // __SYSTEMC_CORE_SCHEDULER_H__
271 }
272
273 // Pending activity ignores gem5 activity, much like how a systemc
274 // simulation wouldn't know about asynchronous external events (socket IO
275 // for instance) that might happen before time advances in a pure
276 // systemc simulation. Also the spec lists what specific types of pending
277 // activity needs to be counted, which obviously doesn't include gem5
278 // events.
279
280 // Return whether there's pending systemc activity at this time.
281 bool
282 pendingCurr()
283 {
284 return !readyList.empty() || !updateList.empty() || !deltas.empty();
285 }
286
287 // Return whether there are pending timed notifications or timeouts.
288 bool
289 pendingFuture()
290 {
291 return !timeSlots.empty();
292 }
293
294 // Return how many ticks there are until the first pending event, if any.
295 Tick
296 timeToPending()
297 {
298 if (pendingCurr())
299 return 0;
300 if (pendingFuture())
301 return timeSlots.begin()->first - getCurTick();
302 return MaxTick - getCurTick();
303 }
304
305 // Run scheduled channel updates.
306 void update();
307
308 void setScMainFiber(Fiber *sc_main) { scMain = sc_main; }
309
310 void start(Tick max_tick, bool run_to_time);
311 void oneCycle();
312
313 void schedulePause();
314 void scheduleStop(bool finish_delta);
315
316 bool paused() { return _paused; }
317 bool stopped() { return _stopped; }
318
319 private:
320 typedef const EventBase::Priority Priority;
321 static Priority DefaultPriority = EventBase::Default_Pri;
322
323 static Priority StopPriority = DefaultPriority - 1;
324 static Priority PausePriority = DefaultPriority + 1;
325 static Priority MaxTickPriority = DefaultPriority + 2;
326 static Priority ReadyPriority = DefaultPriority + 3;
327 static Priority StarvationPriority = ReadyPriority;
328
329 EventQueue *eq;
330
331 // For gem5 style events.
332 void
333 schedule(::Event *event, Tick tick)
334 {
335 if (initDone)
336 eq->schedule(event, tick);
337 else
338 eventsToSchedule[event] = tick;
339 }
340
341 void schedule(::Event *event) { schedule(event, getCurTick()); }
342
343 void
344 deschedule(::Event *event)
345 {
346 if (initDone)
347 eq->deschedule(event);
348 else
349 eventsToSchedule.erase(event);
350 }
351
352 ScEvents deltas;
353 TimeSlots timeSlots;
354
355 void runReady();
356 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
357 void scheduleReadyEvent();
358
359 void pause();
360 void stop();
361 EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
362 EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
363 Fiber *scMain;
364
365 bool
366 starved()
367 {
368 return (readyList.empty() && updateList.empty() && deltas.empty() &&
369 (timeSlots.empty() || timeSlots.begin()->first > maxTick) &&
370 initList.empty());
371 }
372 EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
373 void scheduleStarvationEvent();
374
375 bool _started;
376 bool _paused;
377 bool _stopped;
378
379 Tick maxTick;
380 EventWrapper<Scheduler, &Scheduler::pause> maxTickEvent;
381
382 uint64_t _numCycles;
383
384 Process *_current;
385
386 bool initDone;
387 bool runToTime;
388 bool runOnce;
389
390 ProcessList initList;
391 ProcessList toFinalize;
392 ProcessList readyList;
393
394 ChannelList updateList;
395
396 std::map<::Event *, Tick> eventsToSchedule;
397};
398
399extern Scheduler scheduler;
400
401inline void
402Scheduler::TimeSlot::process()
403{
404 for (auto &e: events)
405 e->run();
406 scheduler.completeTimeSlot(this);
407}
408
409} // namespace sc_gem5
410
411#endif // __SYSTEMC_CORE_SCHEDULER_H__