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 <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; --- 28 unchanged lines hidden (view full) --- 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 --- 18 unchanged lines hidden (view full) --- 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 163 const std::string name() const { return "systemc_scheduler"; } 164 165 uint64_t numCycles() { return _numCycles; } 166 Process *current() { return _current; } 167 168 // Prepare for initialization. --- 27 unchanged lines hidden (view full) --- 196 } 197 198 // Set an event queue for scheduling events. 199 void setEventQueue(EventQueue *_eq) { eq = _eq; } 200 201 // Get the current time according to gem5. 202 Tick getCurTick() { return eq ? eq->getCurTick() : 0; } 203 |
204 Tick 205 delayed(const ::sc_core::sc_time &delay) 206 { 207 //XXX We're assuming the systemc time resolution is in ps. 208 return getCurTick() + delay.value() * SimClock::Int::ps; 209 } 210 |
211 // For scheduling delayed/timed notifications/timeouts. 212 void |
213 schedule(ScEvent *event, const ::sc_core::sc_time &delay) |
214 { |
215 Tick tick = delayed(delay); 216 event->schedule(tick); |
217 |
218 // Delta notification/timeout. 219 if (delay.value() == 0) { 220 deltas.insert(event); 221 scheduleReadyEvent(); 222 return; 223 } 224 225 // Timed notification/timeout. 226 TimeSlot *&ts = timeSlots[tick]; 227 if (!ts) { 228 ts = new TimeSlot; 229 if (initReady) 230 eq->schedule(ts, tick); 231 else 232 eventsToSchedule[ts] = tick; 233 } 234 ts->events.insert(event); |
235 } 236 237 // For descheduling delayed/timed notifications/timeouts. 238 void |
239 deschedule(ScEvent *event) |
240 { |
241 if (event->when() == getCurTick()) { 242 // Remove from delta notifications. 243 deltas.erase(event); 244 event->deschedule(); 245 return; 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 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 if (initReady) 260 eq->deschedule(ts); 261 else 262 eventsToSchedule.erase(ts); 263 timeSlots.erase(tsit); 264 } |
265 } 266 |
267 void |
268 completeTimeSlot(TimeSlot *ts) |
269 { |
270 assert(ts == timeSlots.begin()->second); 271 timeSlots.erase(timeSlots.begin()); |
272 } 273 274 // Pending activity ignores gem5 activity, much like how a systemc 275 // simulation wouldn't know about asynchronous external events (socket IO 276 // for instance) that might happen before time advances in a pure 277 // systemc simulation. Also the spec lists what specific types of pending 278 // activity needs to be counted, which obviously doesn't include gem5 279 // events. 280 281 // Return whether there's pending systemc activity at this time. 282 bool 283 pendingCurr() 284 { |
285 return !readyList.empty() || !updateList.empty() || !deltas.empty(); |
286 } 287 288 // Return whether there are pending timed notifications or timeouts. 289 bool 290 pendingFuture() 291 { |
292 return !timeSlots.empty(); |
293 } 294 295 // Return how many ticks there are until the first pending event, if any. 296 Tick 297 timeToPending() 298 { |
299 if (pendingCurr()) |
300 return 0; |
301 if (pendingFuture()) 302 return timeSlots.begin()->first - getCurTick(); 303 return MaxTick - getCurTick(); |
304 } 305 306 // Run scheduled channel updates. 307 void update(); 308 309 void setScMainFiber(Fiber *sc_main) { scMain = sc_main; } 310 311 void start(Tick max_tick, bool run_to_time); --- 11 unchanged lines hidden (view full) --- 323 324 static Priority StopPriority = DefaultPriority - 1; 325 static Priority PausePriority = DefaultPriority + 1; 326 static Priority MaxTickPriority = DefaultPriority + 2; 327 static Priority ReadyPriority = DefaultPriority + 3; 328 static Priority StarvationPriority = ReadyPriority; 329 330 EventQueue *eq; |
331 |
332 ScEvents deltas; 333 TimeSlots timeSlots; 334 |
335 void runReady(); 336 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent; 337 void scheduleReadyEvent(); 338 339 void pause(); 340 void stop(); 341 EventWrapper<Scheduler, &Scheduler::pause> pauseEvent; 342 EventWrapper<Scheduler, &Scheduler::stop> stopEvent; 343 Fiber *scMain; 344 345 bool 346 starved() 347 { |
348 return (readyList.empty() && updateList.empty() && deltas.empty() && 349 (timeSlots.empty() || timeSlots.begin()->first > maxTick) && |
350 initList.empty()); 351 } 352 EventWrapper<Scheduler, &Scheduler::pause> starvationEvent; 353 void scheduleStarvationEvent(); 354 355 bool _started; 356 bool _paused; 357 bool _stopped; --- 15 unchanged lines hidden (view full) --- 373 374 ChannelList updateList; 375 376 std::map<::Event *, Tick> eventsToSchedule; 377}; 378 379extern Scheduler scheduler; 380 |
381inline void 382Scheduler::TimeSlot::process() 383{ 384 for (auto &e: events) 385 e->run(); 386 scheduler.completeTimeSlot(this); 387} 388 |
389} // namespace sc_gem5 390 391#endif // __SYSTEMC_CORE_SCHEDULER_H__ |