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->popListNode(); 63 p->ready(); 64 } 65 66 for (auto ets: eventsToSchedule) 67 eq->schedule(ets.first, ets.second); 68 eventsToSchedule.clear(); 69 70 if (_started) 71 eq->schedule(&maxTickEvent, maxTick); 72 73 initReady = true; 74} 75 76void 77Scheduler::reg(Process *p) 78{ 79 if (initReady) { 80 // If we're past initialization, finalize static sensitivity. 81 p->finalize(); 82 // Mark the process as ready. 83 p->ready(); 84 } else { 85 // Otherwise, record that this process should be initialized once we 86 // get there. 87 initList.pushLast(p); 88 } 89} 90 91void 92Scheduler::dontInitialize(Process *p) 93{ 94 if (initReady) { 95 // Pop this process off of the ready list. 96 p->popListNode(); 97 } else { 98 // Push this process onto the list of processes which still need 99 // their static sensitivity to be finalized. That implicitly pops it 100 // off the list of processes to be initialized/marked ready. 101 toFinalize.pushLast(p); 102 } 103} 104 105void 106Scheduler::yield() 107{ 108 _current = readyList.getNext(); 109 if (!_current) { 110 // There are no more processes, so return control to evaluate. 111 Fiber::primaryFiber()->run(); 112 } else { 113 _current->popListNode(); 114 // Switch to whatever Fiber is supposed to run this process. All 115 // Fibers which aren't running should be parked at this line. 116 _current->fiber()->run(); 117 // If the current process needs to be manually started, start it. 118 if (_current && _current->needsStart()) 119 _current->run(); 120 } 121 if (_current && _current->excWrapper) { 122 // Make sure this isn't a method process. 123 assert(!_current->needsStart()); 124 auto ew = _current->excWrapper; 125 _current->excWrapper = nullptr; 126 ew->throw_it(); 127 } 128} 129 130void 131Scheduler::ready(Process *p) 132{ 133 // Clump methods together to minimize context switching. 134 if (p->procKind() == ::sc_core::SC_METHOD_PROC_) 135 readyList.pushFirst(p); 136 else 137 readyList.pushLast(p); 138 139 scheduleReadyEvent(); 140} 141 142void 143Scheduler::requestUpdate(Channel *c) 144{ 145 updateList.pushLast(c); 146 if (eq) 147 scheduleReadyEvent(); 148} 149 150void 151Scheduler::scheduleReadyEvent() 152{ 153 // Schedule the evaluate and update phases. 154 if (!readyEvent.scheduled()) { 155 panic_if(!eq, "Need to schedule ready, but no event manager.\n"); 156 eq->schedule(&readyEvent, eq->getCurTick()); 157 if (starvationEvent.scheduled()) 158 eq->deschedule(&starvationEvent); 159 } 160} 161 162void 163Scheduler::scheduleStarvationEvent() 164{ 165 if (!starvationEvent.scheduled()) { 166 panic_if(!eq, "Need to schedule starvation event, " 167 "but no event manager.\n"); 168 eq->schedule(&starvationEvent, eq->getCurTick()); 169 if (readyEvent.scheduled()) 170 eq->deschedule(&readyEvent); 171 } 172} 173 174void 175Scheduler::runReady() 176{ 177 bool empty = readyList.empty(); 178 179 // The evaluation phase. 180 do { 181 yield(); 182 } while (!readyList.empty()); 183 184 if (!empty) 185 _numCycles++; 186 187 // The update phase. 188 update(); 189 190 if (starved() && !runToTime) 191 scheduleStarvationEvent(); 192 193 // The delta phase will happen naturally through the event queue. 194} 195 196void 197Scheduler::update() 198{ 199 Channel *channel = updateList.getNext(); 200 while (channel) { 201 channel->popListNode(); 202 channel->update(); 203 channel = updateList.getNext(); 204 } 205} 206 207void 208Scheduler::pause() 209{ 210 _paused = true; 211 kernel->status(::sc_core::SC_PAUSED); 212 scMain->run();
| 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->popListNode(); 63 p->ready(); 64 } 65 66 for (auto ets: eventsToSchedule) 67 eq->schedule(ets.first, ets.second); 68 eventsToSchedule.clear(); 69 70 if (_started) 71 eq->schedule(&maxTickEvent, maxTick); 72 73 initReady = true; 74} 75 76void 77Scheduler::reg(Process *p) 78{ 79 if (initReady) { 80 // If we're past initialization, finalize static sensitivity. 81 p->finalize(); 82 // Mark the process as ready. 83 p->ready(); 84 } else { 85 // Otherwise, record that this process should be initialized once we 86 // get there. 87 initList.pushLast(p); 88 } 89} 90 91void 92Scheduler::dontInitialize(Process *p) 93{ 94 if (initReady) { 95 // Pop this process off of the ready list. 96 p->popListNode(); 97 } else { 98 // Push this process onto the list of processes which still need 99 // their static sensitivity to be finalized. That implicitly pops it 100 // off the list of processes to be initialized/marked ready. 101 toFinalize.pushLast(p); 102 } 103} 104 105void 106Scheduler::yield() 107{ 108 _current = readyList.getNext(); 109 if (!_current) { 110 // There are no more processes, so return control to evaluate. 111 Fiber::primaryFiber()->run(); 112 } else { 113 _current->popListNode(); 114 // Switch to whatever Fiber is supposed to run this process. All 115 // Fibers which aren't running should be parked at this line. 116 _current->fiber()->run(); 117 // If the current process needs to be manually started, start it. 118 if (_current && _current->needsStart()) 119 _current->run(); 120 } 121 if (_current && _current->excWrapper) { 122 // Make sure this isn't a method process. 123 assert(!_current->needsStart()); 124 auto ew = _current->excWrapper; 125 _current->excWrapper = nullptr; 126 ew->throw_it(); 127 } 128} 129 130void 131Scheduler::ready(Process *p) 132{ 133 // Clump methods together to minimize context switching. 134 if (p->procKind() == ::sc_core::SC_METHOD_PROC_) 135 readyList.pushFirst(p); 136 else 137 readyList.pushLast(p); 138 139 scheduleReadyEvent(); 140} 141 142void 143Scheduler::requestUpdate(Channel *c) 144{ 145 updateList.pushLast(c); 146 if (eq) 147 scheduleReadyEvent(); 148} 149 150void 151Scheduler::scheduleReadyEvent() 152{ 153 // Schedule the evaluate and update phases. 154 if (!readyEvent.scheduled()) { 155 panic_if(!eq, "Need to schedule ready, but no event manager.\n"); 156 eq->schedule(&readyEvent, eq->getCurTick()); 157 if (starvationEvent.scheduled()) 158 eq->deschedule(&starvationEvent); 159 } 160} 161 162void 163Scheduler::scheduleStarvationEvent() 164{ 165 if (!starvationEvent.scheduled()) { 166 panic_if(!eq, "Need to schedule starvation event, " 167 "but no event manager.\n"); 168 eq->schedule(&starvationEvent, eq->getCurTick()); 169 if (readyEvent.scheduled()) 170 eq->deschedule(&readyEvent); 171 } 172} 173 174void 175Scheduler::runReady() 176{ 177 bool empty = readyList.empty(); 178 179 // The evaluation phase. 180 do { 181 yield(); 182 } while (!readyList.empty()); 183 184 if (!empty) 185 _numCycles++; 186 187 // The update phase. 188 update(); 189 190 if (starved() && !runToTime) 191 scheduleStarvationEvent(); 192 193 // The delta phase will happen naturally through the event queue. 194} 195 196void 197Scheduler::update() 198{ 199 Channel *channel = updateList.getNext(); 200 while (channel) { 201 channel->popListNode(); 202 channel->update(); 203 channel = updateList.getNext(); 204 } 205} 206 207void 208Scheduler::pause() 209{ 210 _paused = true; 211 kernel->status(::sc_core::SC_PAUSED); 212 scMain->run();
|