2 * Copyright 2018 Google, Inc.
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.
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.
30 #ifndef __SYSTEMC_CORE_SCHEDULER_HH__
31 #define __SYSTEMC_CORE_SCHEDULER_HH__
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"
51 typedef NodeList<Process> ProcessList;
52 typedef NodeList<Channel> ChannelList;
55 * The scheduler supports three different mechanisms, the initialization phase,
56 * delta cycles, and timed notifications.
58 * INITIALIZATION PHASE
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.
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.
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.
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.
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.
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.
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.
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.
106 * TIMED NOTIFICATION PHASE
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
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.
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.
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.
148 typedef std::list<ScEvent *> ScEvents;
150 class TimeSlot : public ::Event
153 TimeSlot() : ::Event(Default_Pri, AutoDelete) {}
159 typedef std::map<Tick, TimeSlot *> TimeSlots;
166 const std::string name() const { return "systemc_scheduler"; }
168 uint64_t numCycles() { return _numCycles; }
169 Process *current() { return _current; }
173 // Register a process with the scheduler.
174 void reg(Process *p);
176 // Run the next process, if there is one.
179 // Put a process on the ready list.
180 void ready(Process *p);
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);
186 // Remove a process from the ready/init list if it was on one of them, and
188 bool suspend(Process *p);
190 // Schedule an update for a given channel.
191 void requestUpdate(Channel *c);
193 // Run the given process immediately, preempting whatever may be running.
197 // This function may put a process on the wrong list, ie a method on
198 // the process list or vice versa. That's fine since that's just a
199 // performance optimization, and the important thing here is how the
200 // processes are ordered.
202 // If a process is running, schedule it/us to run again.
204 readyList->pushFirst(_current);
205 // Schedule p to run first.
206 readyList->pushFirst(p);
210 // Set an event queue for scheduling events.
211 void setEventQueue(EventQueue *_eq) { eq = _eq; }
213 // Get the current time according to gem5.
214 Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
217 delayed(const ::sc_core::sc_time &delay)
219 //XXX We're assuming the systemc time resolution is in ps.
220 return getCurTick() + delay.value() * SimClock::Int::ps;
223 // For scheduling delayed/timed notifications/timeouts.
225 schedule(ScEvent *event, const ::sc_core::sc_time &delay)
227 Tick tick = delayed(delay);
228 if (tick < getCurTick())
231 // Delta notification/timeout.
232 if (delay.value() == 0) {
233 event->schedule(deltas, tick);
234 scheduleReadyEvent();
238 // Timed notification/timeout.
239 TimeSlot *&ts = timeSlots[tick];
244 event->schedule(ts->events, tick);
247 // For descheduling delayed/timed notifications/timeouts.
249 deschedule(ScEvent *event)
251 ScEvents *on = event->scheduledOn();
258 // Timed notification/timeout.
259 auto tsit = timeSlots.find(event->when());
260 panic_if(tsit == timeSlots.end(),
261 "Descheduling event at time with no events.");
262 TimeSlot *ts = tsit->second;
263 ScEvents &events = ts->events;
264 assert(on == &events);
267 // If no more events are happening at this time slot, get rid of it.
268 if (events.empty()) {
270 timeSlots.erase(tsit);
275 completeTimeSlot(TimeSlot *ts)
278 assert(ts == timeSlots.begin()->second);
279 timeSlots.erase(timeSlots.begin());
280 if (!runToTime && starved())
281 scheduleStarvationEvent();
284 // Pending activity ignores gem5 activity, much like how a systemc
285 // simulation wouldn't know about asynchronous external events (socket IO
286 // for instance) that might happen before time advances in a pure
287 // systemc simulation. Also the spec lists what specific types of pending
288 // activity needs to be counted, which obviously doesn't include gem5
291 // Return whether there's pending systemc activity at this time.
295 return !readyListMethods.empty() || !readyListThreads.empty() ||
296 !updateList.empty() || !deltas.empty();
299 // Return whether there are pending timed notifications or timeouts.
303 return !timeSlots.empty();
306 // Return how many ticks there are until the first pending event, if any.
313 return timeSlots.begin()->first - getCurTick();
314 return MaxTick - getCurTick();
317 // Run scheduled channel updates.
323 void setScMainFiber(Fiber *sc_main) { scMain = sc_main; }
325 void start(Tick max_tick, bool run_to_time);
328 void schedulePause();
329 void scheduleStop(bool finish_delta);
341 bool paused() { return status() == StatusPaused; }
342 bool stopped() { return status() == StatusStopped; }
343 bool inDelta() { return status() == StatusDelta; }
344 bool inUpdate() { return status() == StatusUpdate; }
345 bool inTiming() { return status() == StatusTiming; }
347 uint64_t changeStamp() { return _changeStamp; }
349 void throwToScMain(const ::sc_core::sc_report *r=nullptr);
351 Status status() { return _status; }
352 void status(Status s) { _status = s; }
355 typedef const EventBase::Priority Priority;
356 static Priority DefaultPriority = EventBase::Default_Pri;
358 static Priority StopPriority = DefaultPriority - 1;
359 static Priority PausePriority = DefaultPriority + 1;
360 static Priority MaxTickPriority = DefaultPriority + 2;
361 static Priority ReadyPriority = DefaultPriority + 3;
362 static Priority StarvationPriority = ReadyPriority;
366 // For gem5 style events.
368 schedule(::Event *event, Tick tick)
371 eq->schedule(event, tick);
373 eventsToSchedule[event] = tick;
376 void schedule(::Event *event) { schedule(event, getCurTick()); }
379 deschedule(::Event *event)
382 eq->deschedule(event);
384 eventsToSchedule.erase(event);
391 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
392 void scheduleReadyEvent();
396 EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
397 EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
400 const ::sc_core::sc_report *_throwToScMain;
405 return (readyListMethods.empty() && readyListThreads.empty() &&
406 updateList.empty() && deltas.empty() &&
407 (timeSlots.empty() || timeSlots.begin()->first > maxTick) &&
410 EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
411 void scheduleStarvationEvent();
423 if (lastReadyTick != getCurTick())
427 EventWrapper<Scheduler, &Scheduler::maxTickFunc> maxTickEvent;
430 uint64_t _changeStamp;
438 ProcessList initList;
440 ProcessList *readyList;
441 ProcessList readyListMethods;
442 ProcessList readyListThreads;
444 ChannelList updateList;
446 std::map<::Event *, Tick> eventsToSchedule;
449 extern Scheduler scheduler;
452 Scheduler::TimeSlot::process()
454 scheduler.status(StatusTiming);
457 while (!events.empty())
458 events.front()->run();
461 scheduler.completeTimeSlot(this);
463 scheduler.schedule(this);
464 scheduler.throwToScMain();
467 scheduler.status(StatusOther);
468 scheduler.completeTimeSlot(this);
471 const ::sc_core::sc_report *reportifyException();
473 } // namespace sc_gem5
475 #endif // __SYSTEMC_CORE_SCHEDULER_H__