scheduler.cc revision 13093
112953Sgabeblack@google.com/* 212953Sgabeblack@google.com * Copyright 2018 Google, Inc. 312953Sgabeblack@google.com * 412953Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512953Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612953Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712953Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812953Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912953Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012953Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112953Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212953Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312953Sgabeblack@google.com * this software without specific prior written permission. 1412953Sgabeblack@google.com * 1512953Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612953Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712953Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812953Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912953Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012953Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112953Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212953Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312953Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412953Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512953Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612953Sgabeblack@google.com * 2712953Sgabeblack@google.com * Authors: Gabe Black 2812953Sgabeblack@google.com */ 2912953Sgabeblack@google.com 3012953Sgabeblack@google.com#include "systemc/core/scheduler.hh" 3112953Sgabeblack@google.com 3212953Sgabeblack@google.com#include "base/fiber.hh" 3312954Sgabeblack@google.com#include "base/logging.hh" 3412954Sgabeblack@google.com#include "sim/eventq.hh" 3512982Sgabeblack@google.com#include "systemc/core/kernel.hh" 3612982Sgabeblack@google.com#include "systemc/ext/core/sc_main.hh" 3712953Sgabeblack@google.com 3812953Sgabeblack@google.comnamespace sc_gem5 3912953Sgabeblack@google.com{ 4012953Sgabeblack@google.com 4112954Sgabeblack@google.comScheduler::Scheduler() : 4212962Sgabeblack@google.com eq(nullptr), readyEvent(this, false, ReadyPriority), 4312961Sgabeblack@google.com pauseEvent(this, false, PausePriority), 4412961Sgabeblack@google.com stopEvent(this, false, StopPriority), 4512987Sgabeblack@google.com scMain(nullptr), 4612987Sgabeblack@google.com starvationEvent(this, false, StarvationPriority), 4712987Sgabeblack@google.com _started(false), _paused(false), _stopped(false), 4812961Sgabeblack@google.com maxTickEvent(this, false, MaxTickPriority), 4913067Sgabeblack@google.com _numCycles(0), _current(nullptr), initDone(false), 5013061Sgabeblack@google.com runOnce(false) 5112954Sgabeblack@google.com{} 5212953Sgabeblack@google.com 5313072Sgabeblack@google.comScheduler::~Scheduler() 5413072Sgabeblack@google.com{ 5513072Sgabeblack@google.com // Clear out everything that belongs to us to make sure nobody tries to 5613072Sgabeblack@google.com // clear themselves out after the scheduler goes away. 5713076Sgabeblack@google.com clear(); 5813076Sgabeblack@google.com} 5913072Sgabeblack@google.com 6013076Sgabeblack@google.comvoid 6113076Sgabeblack@google.comScheduler::clear() 6213076Sgabeblack@google.com{ 6313072Sgabeblack@google.com // Delta notifications. 6413072Sgabeblack@google.com for (auto &e: deltas) 6513072Sgabeblack@google.com e->deschedule(); 6613076Sgabeblack@google.com deltas.clear(); 6713072Sgabeblack@google.com 6813072Sgabeblack@google.com // Timed notifications. 6913076Sgabeblack@google.com for (auto &tsp: timeSlots) { 7013076Sgabeblack@google.com TimeSlot *&ts = tsp.second; 7113076Sgabeblack@google.com for (auto &e: ts->events) 7213072Sgabeblack@google.com e->deschedule(); 7313088Sgabeblack@google.com deschedule(ts); 7413072Sgabeblack@google.com } 7513076Sgabeblack@google.com timeSlots.clear(); 7613072Sgabeblack@google.com 7713072Sgabeblack@google.com // gem5 events. 7813072Sgabeblack@google.com if (readyEvent.scheduled()) 7913088Sgabeblack@google.com deschedule(&readyEvent); 8013072Sgabeblack@google.com if (pauseEvent.scheduled()) 8113088Sgabeblack@google.com deschedule(&pauseEvent); 8213072Sgabeblack@google.com if (stopEvent.scheduled()) 8313088Sgabeblack@google.com deschedule(&stopEvent); 8413072Sgabeblack@google.com if (starvationEvent.scheduled()) 8513088Sgabeblack@google.com deschedule(&starvationEvent); 8613072Sgabeblack@google.com if (maxTickEvent.scheduled()) 8713088Sgabeblack@google.com deschedule(&maxTickEvent); 8813072Sgabeblack@google.com 8913072Sgabeblack@google.com Process *p; 9013072Sgabeblack@google.com while ((p = toFinalize.getNext())) 9113072Sgabeblack@google.com p->popListNode(); 9213072Sgabeblack@google.com while ((p = initList.getNext())) 9313072Sgabeblack@google.com p->popListNode(); 9413072Sgabeblack@google.com while ((p = readyList.getNext())) 9513072Sgabeblack@google.com p->popListNode(); 9613072Sgabeblack@google.com 9713072Sgabeblack@google.com Channel *c; 9813072Sgabeblack@google.com while ((c = updateList.getNext())) 9913072Sgabeblack@google.com c->popListNode(); 10013072Sgabeblack@google.com} 10113072Sgabeblack@google.com 10212953Sgabeblack@google.comvoid 10313067Sgabeblack@google.comScheduler::initPhase() 10412953Sgabeblack@google.com{ 10512957Sgabeblack@google.com for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) { 10612957Sgabeblack@google.com p->finalize(); 10712957Sgabeblack@google.com p->popListNode(); 10812957Sgabeblack@google.com } 10912957Sgabeblack@google.com 11012957Sgabeblack@google.com for (Process *p = initList.getNext(); p; p = initList.getNext()) { 11112957Sgabeblack@google.com p->finalize(); 11212996Sgabeblack@google.com p->popListNode(); 11312959Sgabeblack@google.com p->ready(); 11412957Sgabeblack@google.com } 11512957Sgabeblack@google.com 11613067Sgabeblack@google.com update(); 11713067Sgabeblack@google.com 11813067Sgabeblack@google.com for (auto &e: deltas) 11913067Sgabeblack@google.com e->run(); 12013067Sgabeblack@google.com deltas.clear(); 12113067Sgabeblack@google.com 12212985Sgabeblack@google.com for (auto ets: eventsToSchedule) 12312985Sgabeblack@google.com eq->schedule(ets.first, ets.second); 12412985Sgabeblack@google.com eventsToSchedule.clear(); 12512985Sgabeblack@google.com 12613068Sgabeblack@google.com if (_started) { 12713068Sgabeblack@google.com if (starved() && !runToTime) 12813068Sgabeblack@google.com scheduleStarvationEvent(); 12913069Sgabeblack@google.com kernel->status(::sc_core::SC_RUNNING); 13013068Sgabeblack@google.com } 13112961Sgabeblack@google.com 13213067Sgabeblack@google.com initDone = true; 13312957Sgabeblack@google.com} 13412957Sgabeblack@google.com 13512957Sgabeblack@google.comvoid 13612957Sgabeblack@google.comScheduler::reg(Process *p) 13712957Sgabeblack@google.com{ 13813067Sgabeblack@google.com if (initDone) { 13912957Sgabeblack@google.com // If we're past initialization, finalize static sensitivity. 14012957Sgabeblack@google.com p->finalize(); 14112957Sgabeblack@google.com // Mark the process as ready. 14212959Sgabeblack@google.com p->ready(); 14312957Sgabeblack@google.com } else { 14412957Sgabeblack@google.com // Otherwise, record that this process should be initialized once we 14512957Sgabeblack@google.com // get there. 14612957Sgabeblack@google.com initList.pushLast(p); 14712957Sgabeblack@google.com } 14812957Sgabeblack@google.com} 14912957Sgabeblack@google.com 15012957Sgabeblack@google.comvoid 15112957Sgabeblack@google.comScheduler::dontInitialize(Process *p) 15212957Sgabeblack@google.com{ 15313067Sgabeblack@google.com if (initDone) { 15412957Sgabeblack@google.com // Pop this process off of the ready list. 15512957Sgabeblack@google.com p->popListNode(); 15612957Sgabeblack@google.com } else { 15712957Sgabeblack@google.com // Push this process onto the list of processes which still need 15812957Sgabeblack@google.com // their static sensitivity to be finalized. That implicitly pops it 15912957Sgabeblack@google.com // off the list of processes to be initialized/marked ready. 16012957Sgabeblack@google.com toFinalize.pushLast(p); 16112957Sgabeblack@google.com } 16212953Sgabeblack@google.com} 16312953Sgabeblack@google.com 16412953Sgabeblack@google.comvoid 16512953Sgabeblack@google.comScheduler::yield() 16612953Sgabeblack@google.com{ 16712953Sgabeblack@google.com _current = readyList.getNext(); 16812953Sgabeblack@google.com if (!_current) { 16912953Sgabeblack@google.com // There are no more processes, so return control to evaluate. 17012953Sgabeblack@google.com Fiber::primaryFiber()->run(); 17112953Sgabeblack@google.com } else { 17212953Sgabeblack@google.com _current->popListNode(); 17312953Sgabeblack@google.com // Switch to whatever Fiber is supposed to run this process. All 17412953Sgabeblack@google.com // Fibers which aren't running should be parked at this line. 17512953Sgabeblack@google.com _current->fiber()->run(); 17612961Sgabeblack@google.com // If the current process needs to be manually started, start it. 17713093Sgabeblack@google.com if (_current && _current->needsStart()) { 17813093Sgabeblack@google.com _current->needsStart(false); 17912953Sgabeblack@google.com _current->run(); 18013093Sgabeblack@google.com } 18112953Sgabeblack@google.com } 18212995Sgabeblack@google.com if (_current && _current->excWrapper) { 18312995Sgabeblack@google.com // Make sure this isn't a method process. 18412995Sgabeblack@google.com assert(!_current->needsStart()); 18512995Sgabeblack@google.com auto ew = _current->excWrapper; 18612995Sgabeblack@google.com _current->excWrapper = nullptr; 18712995Sgabeblack@google.com ew->throw_it(); 18812995Sgabeblack@google.com } 18912953Sgabeblack@google.com} 19012953Sgabeblack@google.com 19112953Sgabeblack@google.comvoid 19212954Sgabeblack@google.comScheduler::ready(Process *p) 19312953Sgabeblack@google.com{ 19412954Sgabeblack@google.com // Clump methods together to minimize context switching. 19512954Sgabeblack@google.com if (p->procKind() == ::sc_core::SC_METHOD_PROC_) 19612954Sgabeblack@google.com readyList.pushFirst(p); 19712954Sgabeblack@google.com else 19812954Sgabeblack@google.com readyList.pushLast(p); 19912953Sgabeblack@google.com 20012954Sgabeblack@google.com scheduleReadyEvent(); 20112954Sgabeblack@google.com} 20212954Sgabeblack@google.com 20312954Sgabeblack@google.comvoid 20412954Sgabeblack@google.comScheduler::requestUpdate(Channel *c) 20512954Sgabeblack@google.com{ 20612954Sgabeblack@google.com updateList.pushLast(c); 20713069Sgabeblack@google.com scheduleReadyEvent(); 20812954Sgabeblack@google.com} 20912954Sgabeblack@google.com 21012954Sgabeblack@google.comvoid 21112954Sgabeblack@google.comScheduler::scheduleReadyEvent() 21212954Sgabeblack@google.com{ 21312954Sgabeblack@google.com // Schedule the evaluate and update phases. 21412954Sgabeblack@google.com if (!readyEvent.scheduled()) { 21513069Sgabeblack@google.com schedule(&readyEvent); 21612987Sgabeblack@google.com if (starvationEvent.scheduled()) 21713069Sgabeblack@google.com deschedule(&starvationEvent); 21812987Sgabeblack@google.com } 21912987Sgabeblack@google.com} 22012987Sgabeblack@google.com 22112987Sgabeblack@google.comvoid 22212987Sgabeblack@google.comScheduler::scheduleStarvationEvent() 22312987Sgabeblack@google.com{ 22412987Sgabeblack@google.com if (!starvationEvent.scheduled()) { 22513069Sgabeblack@google.com schedule(&starvationEvent); 22612987Sgabeblack@google.com if (readyEvent.scheduled()) 22713069Sgabeblack@google.com deschedule(&readyEvent); 22812954Sgabeblack@google.com } 22912954Sgabeblack@google.com} 23012954Sgabeblack@google.com 23112954Sgabeblack@google.comvoid 23212954Sgabeblack@google.comScheduler::runReady() 23312954Sgabeblack@google.com{ 23412954Sgabeblack@google.com bool empty = readyList.empty(); 23512954Sgabeblack@google.com 23612954Sgabeblack@google.com // The evaluation phase. 23712953Sgabeblack@google.com do { 23812953Sgabeblack@google.com yield(); 23912953Sgabeblack@google.com } while (!readyList.empty()); 24012954Sgabeblack@google.com 24112954Sgabeblack@google.com if (!empty) 24212954Sgabeblack@google.com _numCycles++; 24312954Sgabeblack@google.com 24412954Sgabeblack@google.com // The update phase. 24512954Sgabeblack@google.com update(); 24612954Sgabeblack@google.com 24712987Sgabeblack@google.com if (starved() && !runToTime) 24812987Sgabeblack@google.com scheduleStarvationEvent(); 24912987Sgabeblack@google.com 25013063Sgabeblack@google.com // The delta phase. 25113063Sgabeblack@google.com for (auto &e: deltas) 25213063Sgabeblack@google.com e->run(); 25313063Sgabeblack@google.com deltas.clear(); 25413061Sgabeblack@google.com 25513064Sgabeblack@google.com if (runOnce) 25613064Sgabeblack@google.com schedulePause(); 25712953Sgabeblack@google.com} 25812953Sgabeblack@google.com 25912953Sgabeblack@google.comvoid 26012953Sgabeblack@google.comScheduler::update() 26112953Sgabeblack@google.com{ 26212954Sgabeblack@google.com Channel *channel = updateList.getNext(); 26312954Sgabeblack@google.com while (channel) { 26412954Sgabeblack@google.com channel->popListNode(); 26512954Sgabeblack@google.com channel->update(); 26612954Sgabeblack@google.com channel = updateList.getNext(); 26712954Sgabeblack@google.com } 26812953Sgabeblack@google.com} 26912953Sgabeblack@google.com 27012961Sgabeblack@google.comvoid 27112961Sgabeblack@google.comScheduler::pause() 27212961Sgabeblack@google.com{ 27312961Sgabeblack@google.com _paused = true; 27412982Sgabeblack@google.com kernel->status(::sc_core::SC_PAUSED); 27513061Sgabeblack@google.com runOnce = false; 27612961Sgabeblack@google.com scMain->run(); 27712961Sgabeblack@google.com} 27812961Sgabeblack@google.com 27912961Sgabeblack@google.comvoid 28012961Sgabeblack@google.comScheduler::stop() 28112961Sgabeblack@google.com{ 28212961Sgabeblack@google.com _stopped = true; 28312982Sgabeblack@google.com kernel->stop(); 28413074Sgabeblack@google.com 28513076Sgabeblack@google.com clear(); 28613074Sgabeblack@google.com 28713061Sgabeblack@google.com runOnce = false; 28812961Sgabeblack@google.com scMain->run(); 28912961Sgabeblack@google.com} 29012961Sgabeblack@google.com 29112961Sgabeblack@google.comvoid 29212961Sgabeblack@google.comScheduler::start(Tick max_tick, bool run_to_time) 29312961Sgabeblack@google.com{ 29412961Sgabeblack@google.com // We should be running from sc_main. Keep track of that Fiber to return 29512961Sgabeblack@google.com // to later. 29612961Sgabeblack@google.com scMain = Fiber::currentFiber(); 29712961Sgabeblack@google.com 29812961Sgabeblack@google.com _started = true; 29912961Sgabeblack@google.com _paused = false; 30012961Sgabeblack@google.com _stopped = false; 30112987Sgabeblack@google.com runToTime = run_to_time; 30212961Sgabeblack@google.com 30312961Sgabeblack@google.com maxTick = max_tick; 30412961Sgabeblack@google.com 30513067Sgabeblack@google.com if (initDone) { 30613068Sgabeblack@google.com if (starved() && !runToTime) 30713068Sgabeblack@google.com scheduleStarvationEvent(); 30812982Sgabeblack@google.com kernel->status(::sc_core::SC_RUNNING); 30912982Sgabeblack@google.com } 31012961Sgabeblack@google.com 31113069Sgabeblack@google.com schedule(&maxTickEvent, maxTick); 31213069Sgabeblack@google.com 31312961Sgabeblack@google.com // Return to gem5 to let it run events, etc. 31412961Sgabeblack@google.com Fiber::primaryFiber()->run(); 31512961Sgabeblack@google.com 31612961Sgabeblack@google.com if (pauseEvent.scheduled()) 31713088Sgabeblack@google.com deschedule(&pauseEvent); 31812961Sgabeblack@google.com if (stopEvent.scheduled()) 31913088Sgabeblack@google.com deschedule(&stopEvent); 32012961Sgabeblack@google.com if (maxTickEvent.scheduled()) 32113088Sgabeblack@google.com deschedule(&maxTickEvent); 32212987Sgabeblack@google.com if (starvationEvent.scheduled()) 32313088Sgabeblack@google.com deschedule(&starvationEvent); 32412961Sgabeblack@google.com} 32512961Sgabeblack@google.com 32612961Sgabeblack@google.comvoid 32713061Sgabeblack@google.comScheduler::oneCycle() 32813061Sgabeblack@google.com{ 32913061Sgabeblack@google.com runOnce = true; 33013061Sgabeblack@google.com start(::MaxTick, false); 33113061Sgabeblack@google.com} 33213061Sgabeblack@google.com 33313061Sgabeblack@google.comvoid 33412961Sgabeblack@google.comScheduler::schedulePause() 33512961Sgabeblack@google.com{ 33612961Sgabeblack@google.com if (pauseEvent.scheduled()) 33712961Sgabeblack@google.com return; 33812961Sgabeblack@google.com 33913088Sgabeblack@google.com schedule(&pauseEvent); 34012961Sgabeblack@google.com} 34112961Sgabeblack@google.com 34212961Sgabeblack@google.comvoid 34312961Sgabeblack@google.comScheduler::scheduleStop(bool finish_delta) 34412961Sgabeblack@google.com{ 34512961Sgabeblack@google.com if (stopEvent.scheduled()) 34612961Sgabeblack@google.com return; 34712961Sgabeblack@google.com 34812961Sgabeblack@google.com if (!finish_delta) { 34913076Sgabeblack@google.com // If we're not supposed to finish the delta cycle, flush all 35013076Sgabeblack@google.com // pending activity. 35113076Sgabeblack@google.com clear(); 35212961Sgabeblack@google.com } 35313088Sgabeblack@google.com schedule(&stopEvent); 35412961Sgabeblack@google.com} 35512961Sgabeblack@google.com 35612953Sgabeblack@google.comScheduler scheduler; 35712953Sgabeblack@google.com 35812953Sgabeblack@google.com} // namespace sc_gem5 359