Deleted Added
sdiff udiff text old ( 13061:9b868a2ab73c ) new ( 13063:c9905ead0041 )
full compact
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

--- 16 unchanged lines hidden (view full) ---

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 "base/logging.hh"
36#include "sim/eventq.hh"
37#include "systemc/core/channel.hh"
38#include "systemc/core/list.hh"
39#include "systemc/core/process.hh"
40
41class Fiber;
42
43namespace sc_gem5
44{
45
46typedef NodeList<Process> ProcessList;
47typedef NodeList<Channel> ChannelList;

--- 28 unchanged lines hidden (view full) ---

76 *
77 * DELTA CYCLE
78 *
79 * A delta cycle has three phases within it.
80 * 1. The evaluate phase where runnable processes are allowed to run.
81 * 2. The update phase where requested channel updates hapen.
82 * 3. The delta notification phase where delta notifications happen.
83 *
84 * The readyEvent runs the first two steps of the delta cycle. It first goes
85 * through the list of runnable processes and executes them until the set is
86 * empty, and then immediately runs the update phase. Since these are all part
87 * of the same event, there's no chance for other events to intervene and
88 * break the required order above.
89 *
90 * During the update phase above, the spec forbids any action which would make
91 * a process runnable. That means that once the update phase finishes, the set
92 * of runnable processes will be empty. There may, however, have been some
93 * delta notifications/timeouts which will have been scheduled during either
94 * the evaluate or update phase above. Because those are scheduled at the
95 * normal priority, they will now happen together until there aren't any
96 * delta events left.
97 *
98 * If any processes became runnable during the delta notification phase, the
99 * readyEvent will have been scheduled and will have been waiting patiently
100 * behind the delta notification events. That will now run, effectively
101 * starting the next delta cycle.
102 *
103 * TIMED NOTIFICATION PHASE
104 *
105 * If no processes became runnable, the event queue will continue to process
106 * events until it comes across a timed notification, aka a notification
107 * scheduled to happen in the future. Like delta notification events, those
108 * will all happen together since the readyEvent priority is lower,
109 * potentially marking new processes as ready. Once these events finish, the
110 * readyEvent may run, starting the next delta cycle.
111 *
112 * PAUSE/STOP
113 *
114 * To inject a pause from sc_pause which should happen after the current delta
115 * cycle's delta notification phase, an event is scheduled with a lower than
116 * normal priority, but higher than the readyEvent. That ensures that any
117 * delta notifications which are scheduled with normal priority will happen
118 * first, since those are part of the current delta cycle. Then the pause

--- 18 unchanged lines hidden (view full) ---

137 * priority which is lower than all the others except the ready event. Timed
138 * notifications will happen before it fires, but it will override any ready
139 * event and prevent the evaluate phase from starting.
140 */
141
142class Scheduler
143{
144 public:
145 Scheduler();
146
147 const std::string name() const { return "systemc_scheduler"; }
148
149 uint64_t numCycles() { return _numCycles; }
150 Process *current() { return _current; }
151
152 // Prepare for initialization.

--- 27 unchanged lines hidden (view full) ---

180 }
181
182 // Set an event queue for scheduling events.
183 void setEventQueue(EventQueue *_eq) { eq = _eq; }
184
185 // Get the current time according to gem5.
186 Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
187
188 // For scheduling delayed/timed notifications/timeouts.
189 void
190 schedule(::Event *event, Tick tick)
191 {
192 pendingTicks[tick]++;
193
194 if (initReady)
195 eq->schedule(event, tick);
196 else
197 eventsToSchedule[event] = tick;
198 }
199
200 // For descheduling delayed/timed notifications/timeouts.
201 void
202 deschedule(::Event *event)
203 {
204 auto it = pendingTicks.find(event->when());
205 if (--it->second == 0)
206 pendingTicks.erase(it);
207
208 if (initReady)
209 eq->deschedule(event);
210 else
211 eventsToSchedule.erase(event);
212 }
213
214 // Tell the scheduler than an event fired for bookkeeping purposes.
215 void
216 eventHappened()
217 {
218 auto it = pendingTicks.begin();
219 if (--it->second == 0)
220 pendingTicks.erase(it);
221
222 if (starved() && !runToTime)
223 scheduleStarvationEvent();
224 }
225
226 // Pending activity ignores gem5 activity, much like how a systemc
227 // simulation wouldn't know about asynchronous external events (socket IO
228 // for instance) that might happen before time advances in a pure
229 // systemc simulation. Also the spec lists what specific types of pending
230 // activity needs to be counted, which obviously doesn't include gem5
231 // events.
232
233 // Return whether there's pending systemc activity at this time.
234 bool
235 pendingCurr()
236 {
237 if (!readyList.empty() || !updateList.empty())
238 return true;
239 return pendingTicks.size() &&
240 pendingTicks.begin()->first == getCurTick();
241 }
242
243 // Return whether there are pending timed notifications or timeouts.
244 bool
245 pendingFuture()
246 {
247 switch (pendingTicks.size()) {
248 case 0: return false;
249 case 1: return pendingTicks.begin()->first > getCurTick();
250 default: return true;
251 }
252 }
253
254 // Return how many ticks there are until the first pending event, if any.
255 Tick
256 timeToPending()
257 {
258 if (!readyList.empty() || !updateList.empty())
259 return 0;
260 else if (pendingTicks.size())
261 return pendingTicks.begin()->first - getCurTick();
262 else
263 return MaxTick - getCurTick();
264 }
265
266 // Run scheduled channel updates.
267 void update();
268
269 void setScMainFiber(Fiber *sc_main) { scMain = sc_main; }
270
271 void start(Tick max_tick, bool run_to_time);

--- 11 unchanged lines hidden (view full) ---

283
284 static Priority StopPriority = DefaultPriority - 1;
285 static Priority PausePriority = DefaultPriority + 1;
286 static Priority MaxTickPriority = DefaultPriority + 2;
287 static Priority ReadyPriority = DefaultPriority + 3;
288 static Priority StarvationPriority = ReadyPriority;
289
290 EventQueue *eq;
291 std::map<Tick, int> pendingTicks;
292
293 void runReady();
294 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
295 void scheduleReadyEvent();
296
297 void pause();
298 void stop();
299 EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
300 EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
301 Fiber *scMain;
302
303 bool
304 starved()
305 {
306 return (readyList.empty() && updateList.empty() &&
307 (pendingTicks.empty() ||
308 pendingTicks.begin()->first > maxTick) &&
309 initList.empty());
310 }
311 EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
312 void scheduleStarvationEvent();
313
314 bool _started;
315 bool _paused;
316 bool _stopped;

--- 15 unchanged lines hidden (view full) ---

332
333 ChannelList updateList;
334
335 std::map<::Event *, Tick> eventsToSchedule;
336};
337
338extern Scheduler scheduler;
339
340} // namespace sc_gem5
341
342#endif // __SYSTEMC_CORE_SCHEDULER_H__