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