scheduler.cc revision 13061:9b868a2ab73c
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#include "systemc/core/scheduler.hh" 31 32#include "base/fiber.hh" 33#include "base/logging.hh" 34#include "sim/eventq.hh" 35#include "systemc/core/kernel.hh" 36#include "systemc/ext/core/sc_main.hh" 37 38namespace sc_gem5 39{ 40 41Scheduler::Scheduler() : 42 eq(nullptr), readyEvent(this, false, ReadyPriority), 43 pauseEvent(this, false, PausePriority), 44 stopEvent(this, false, StopPriority), 45 scMain(nullptr), 46 starvationEvent(this, false, StarvationPriority), 47 _started(false), _paused(false), _stopped(false), 48 maxTickEvent(this, false, MaxTickPriority), 49 _numCycles(0), _current(nullptr), initReady(false), 50 runOnce(false) 51{} 52 53void 54Scheduler::prepareForInit() 55{ 56 for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) { 57 p->finalize(); 58 p->popListNode(); 59 } 60 61 for (Process *p = initList.getNext(); p; p = initList.getNext()) { 62 p->finalize(); 63 p->popListNode(); 64 p->ready(); 65 } 66 67 for (auto ets: eventsToSchedule) 68 eq->schedule(ets.first, ets.second); 69 eventsToSchedule.clear(); 70 71 if (_started) 72 eq->schedule(&maxTickEvent, maxTick); 73 74 initReady = true; 75} 76 77void 78Scheduler::reg(Process *p) 79{ 80 if (initReady) { 81 // If we're past initialization, finalize static sensitivity. 82 p->finalize(); 83 // Mark the process as ready. 84 p->ready(); 85 } else { 86 // Otherwise, record that this process should be initialized once we 87 // get there. 88 initList.pushLast(p); 89 } 90} 91 92void 93Scheduler::dontInitialize(Process *p) 94{ 95 if (initReady) { 96 // Pop this process off of the ready list. 97 p->popListNode(); 98 } else { 99 // Push this process onto the list of processes which still need 100 // their static sensitivity to be finalized. That implicitly pops it 101 // off the list of processes to be initialized/marked ready. 102 toFinalize.pushLast(p); 103 } 104} 105 106void 107Scheduler::yield() 108{ 109 _current = readyList.getNext(); 110 if (!_current) { 111 // There are no more processes, so return control to evaluate. 112 Fiber::primaryFiber()->run(); 113 } else { 114 _current->popListNode(); 115 // Switch to whatever Fiber is supposed to run this process. All 116 // Fibers which aren't running should be parked at this line. 117 _current->fiber()->run(); 118 // If the current process needs to be manually started, start it. 119 if (_current && _current->needsStart()) 120 _current->run(); 121 } 122 if (_current && _current->excWrapper) { 123 // Make sure this isn't a method process. 124 assert(!_current->needsStart()); 125 auto ew = _current->excWrapper; 126 _current->excWrapper = nullptr; 127 ew->throw_it(); 128 } 129} 130 131void 132Scheduler::ready(Process *p) 133{ 134 // Clump methods together to minimize context switching. 135 if (p->procKind() == ::sc_core::SC_METHOD_PROC_) 136 readyList.pushFirst(p); 137 else 138 readyList.pushLast(p); 139 140 scheduleReadyEvent(); 141} 142 143void 144Scheduler::requestUpdate(Channel *c) 145{ 146 updateList.pushLast(c); 147 if (eq) 148 scheduleReadyEvent(); 149} 150 151void 152Scheduler::scheduleReadyEvent() 153{ 154 // Schedule the evaluate and update phases. 155 if (!readyEvent.scheduled()) { 156 panic_if(!eq, "Need to schedule ready, but no event manager.\n"); 157 eq->schedule(&readyEvent, eq->getCurTick()); 158 if (starvationEvent.scheduled()) 159 eq->deschedule(&starvationEvent); 160 } 161} 162 163void 164Scheduler::scheduleStarvationEvent() 165{ 166 if (!starvationEvent.scheduled()) { 167 panic_if(!eq, "Need to schedule starvation event, " 168 "but no event manager.\n"); 169 eq->schedule(&starvationEvent, eq->getCurTick()); 170 if (readyEvent.scheduled()) 171 eq->deschedule(&readyEvent); 172 } 173} 174 175void 176Scheduler::runReady() 177{ 178 bool empty = readyList.empty(); 179 180 // The evaluation phase. 181 do { 182 yield(); 183 } while (!readyList.empty()); 184 185 if (!empty) 186 _numCycles++; 187 188 // The update phase. 189 update(); 190 191 if (starved() && !runToTime) 192 scheduleStarvationEvent(); 193 194 // The delta phase will happen naturally through the event queue. 195 196 if (runOnce) { 197 eq->reschedule(&maxTickEvent, eq->getCurTick()); 198 runOnce = false; 199 } 200} 201 202void 203Scheduler::update() 204{ 205 Channel *channel = updateList.getNext(); 206 while (channel) { 207 channel->popListNode(); 208 channel->update(); 209 channel = updateList.getNext(); 210 } 211} 212 213void 214Scheduler::pause() 215{ 216 _paused = true; 217 kernel->status(::sc_core::SC_PAUSED); 218 runOnce = false; 219 scMain->run(); 220 221 // If the ready event is supposed to run now, run it inline so that it 222 // preempts any delta notifications which were scheduled while we were 223 // paused. 224 if (readyEvent.scheduled()) { 225 eq->deschedule(&readyEvent); 226 runReady(); 227 } 228} 229 230void 231Scheduler::stop() 232{ 233 _stopped = true; 234 kernel->stop(); 235 runOnce = false; 236 scMain->run(); 237} 238 239void 240Scheduler::start(Tick max_tick, bool run_to_time) 241{ 242 // We should be running from sc_main. Keep track of that Fiber to return 243 // to later. 244 scMain = Fiber::currentFiber(); 245 246 _started = true; 247 _paused = false; 248 _stopped = false; 249 runToTime = run_to_time; 250 251 maxTick = max_tick; 252 253 if (starved() && !runToTime) 254 return; 255 256 if (initReady) { 257 kernel->status(::sc_core::SC_RUNNING); 258 eq->schedule(&maxTickEvent, maxTick); 259 } 260 261 // Return to gem5 to let it run events, etc. 262 Fiber::primaryFiber()->run(); 263 264 if (pauseEvent.scheduled()) 265 eq->deschedule(&pauseEvent); 266 if (stopEvent.scheduled()) 267 eq->deschedule(&stopEvent); 268 if (maxTickEvent.scheduled()) 269 eq->deschedule(&maxTickEvent); 270 if (starvationEvent.scheduled()) 271 eq->deschedule(&starvationEvent); 272} 273 274void 275Scheduler::oneCycle() 276{ 277 runOnce = true; 278 start(::MaxTick, false); 279} 280 281void 282Scheduler::schedulePause() 283{ 284 if (pauseEvent.scheduled()) 285 return; 286 287 eq->schedule(&pauseEvent, eq->getCurTick()); 288} 289 290void 291Scheduler::scheduleStop(bool finish_delta) 292{ 293 if (stopEvent.scheduled()) 294 return; 295 296 if (!finish_delta) { 297 // If we're not supposed to finish the delta cycle, flush the list 298 // of ready processes and scheduled updates. 299 Process *p; 300 while ((p = readyList.getNext())) 301 p->popListNode(); 302 Channel *c; 303 while ((c = updateList.getNext())) 304 c->popListNode(); 305 } 306 eq->schedule(&stopEvent, eq->getCurTick()); 307} 308 309Scheduler scheduler; 310 311} // namespace sc_gem5 312