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