scheduler.cc revision 12985
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), 4512961Sgabeblack@google.com scMain(nullptr), _started(false), _paused(false), _stopped(false), 4612961Sgabeblack@google.com maxTickEvent(this, false, MaxTickPriority), 4712957Sgabeblack@google.com _numCycles(0), _current(nullptr), initReady(false) 4812954Sgabeblack@google.com{} 4912953Sgabeblack@google.com 5012953Sgabeblack@google.comvoid 5112957Sgabeblack@google.comScheduler::prepareForInit() 5212953Sgabeblack@google.com{ 5312957Sgabeblack@google.com for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) { 5412957Sgabeblack@google.com p->finalize(); 5512957Sgabeblack@google.com p->popListNode(); 5612957Sgabeblack@google.com } 5712957Sgabeblack@google.com 5812957Sgabeblack@google.com for (Process *p = initList.getNext(); p; p = initList.getNext()) { 5912957Sgabeblack@google.com p->finalize(); 6012959Sgabeblack@google.com p->ready(); 6112957Sgabeblack@google.com } 6212957Sgabeblack@google.com 6312985Sgabeblack@google.com for (auto ets: eventsToSchedule) 6412985Sgabeblack@google.com eq->schedule(ets.first, ets.second); 6512985Sgabeblack@google.com eventsToSchedule.clear(); 6612985Sgabeblack@google.com 6712961Sgabeblack@google.com if (_started) 6812961Sgabeblack@google.com eq->schedule(&maxTickEvent, maxTick); 6912961Sgabeblack@google.com 7012957Sgabeblack@google.com initReady = true; 7112957Sgabeblack@google.com} 7212957Sgabeblack@google.com 7312957Sgabeblack@google.comvoid 7412957Sgabeblack@google.comScheduler::reg(Process *p) 7512957Sgabeblack@google.com{ 7612957Sgabeblack@google.com if (initReady) { 7712957Sgabeblack@google.com // If we're past initialization, finalize static sensitivity. 7812957Sgabeblack@google.com p->finalize(); 7912957Sgabeblack@google.com // Mark the process as ready. 8012959Sgabeblack@google.com p->ready(); 8112957Sgabeblack@google.com } else { 8212957Sgabeblack@google.com // Otherwise, record that this process should be initialized once we 8312957Sgabeblack@google.com // get there. 8412957Sgabeblack@google.com initList.pushLast(p); 8512957Sgabeblack@google.com } 8612957Sgabeblack@google.com} 8712957Sgabeblack@google.com 8812957Sgabeblack@google.comvoid 8912957Sgabeblack@google.comScheduler::dontInitialize(Process *p) 9012957Sgabeblack@google.com{ 9112957Sgabeblack@google.com if (initReady) { 9212957Sgabeblack@google.com // Pop this process off of the ready list. 9312957Sgabeblack@google.com p->popListNode(); 9412957Sgabeblack@google.com } else { 9512957Sgabeblack@google.com // Push this process onto the list of processes which still need 9612957Sgabeblack@google.com // their static sensitivity to be finalized. That implicitly pops it 9712957Sgabeblack@google.com // off the list of processes to be initialized/marked ready. 9812957Sgabeblack@google.com toFinalize.pushLast(p); 9912957Sgabeblack@google.com } 10012953Sgabeblack@google.com} 10112953Sgabeblack@google.com 10212953Sgabeblack@google.comvoid 10312953Sgabeblack@google.comScheduler::yield() 10412953Sgabeblack@google.com{ 10512953Sgabeblack@google.com _current = readyList.getNext(); 10612953Sgabeblack@google.com if (!_current) { 10712953Sgabeblack@google.com // There are no more processes, so return control to evaluate. 10812953Sgabeblack@google.com Fiber::primaryFiber()->run(); 10912953Sgabeblack@google.com } else { 11012953Sgabeblack@google.com _current->popListNode(); 11112953Sgabeblack@google.com // Switch to whatever Fiber is supposed to run this process. All 11212953Sgabeblack@google.com // Fibers which aren't running should be parked at this line. 11312953Sgabeblack@google.com _current->fiber()->run(); 11412961Sgabeblack@google.com // If the current process needs to be manually started, start it. 11512961Sgabeblack@google.com if (_current && _current->needsStart()) 11612953Sgabeblack@google.com _current->run(); 11712953Sgabeblack@google.com } 11812953Sgabeblack@google.com} 11912953Sgabeblack@google.com 12012953Sgabeblack@google.comvoid 12112954Sgabeblack@google.comScheduler::ready(Process *p) 12212953Sgabeblack@google.com{ 12312954Sgabeblack@google.com // Clump methods together to minimize context switching. 12412954Sgabeblack@google.com if (p->procKind() == ::sc_core::SC_METHOD_PROC_) 12512954Sgabeblack@google.com readyList.pushFirst(p); 12612954Sgabeblack@google.com else 12712954Sgabeblack@google.com readyList.pushLast(p); 12812953Sgabeblack@google.com 12912954Sgabeblack@google.com scheduleReadyEvent(); 13012954Sgabeblack@google.com} 13112954Sgabeblack@google.com 13212954Sgabeblack@google.comvoid 13312954Sgabeblack@google.comScheduler::requestUpdate(Channel *c) 13412954Sgabeblack@google.com{ 13512954Sgabeblack@google.com updateList.pushLast(c); 13612954Sgabeblack@google.com scheduleReadyEvent(); 13712954Sgabeblack@google.com} 13812954Sgabeblack@google.com 13912954Sgabeblack@google.comvoid 14012954Sgabeblack@google.comScheduler::scheduleReadyEvent() 14112954Sgabeblack@google.com{ 14212954Sgabeblack@google.com // Schedule the evaluate and update phases. 14312954Sgabeblack@google.com if (!readyEvent.scheduled()) { 14412954Sgabeblack@google.com panic_if(!eq, "Need to schedule ready, but no event manager.\n"); 14512954Sgabeblack@google.com eq->schedule(&readyEvent, eq->getCurTick()); 14612954Sgabeblack@google.com } 14712954Sgabeblack@google.com} 14812954Sgabeblack@google.com 14912954Sgabeblack@google.comvoid 15012954Sgabeblack@google.comScheduler::runReady() 15112954Sgabeblack@google.com{ 15212954Sgabeblack@google.com bool empty = readyList.empty(); 15312954Sgabeblack@google.com 15412954Sgabeblack@google.com // The evaluation phase. 15512953Sgabeblack@google.com do { 15612953Sgabeblack@google.com yield(); 15712953Sgabeblack@google.com } while (!readyList.empty()); 15812954Sgabeblack@google.com 15912954Sgabeblack@google.com if (!empty) 16012954Sgabeblack@google.com _numCycles++; 16112954Sgabeblack@google.com 16212954Sgabeblack@google.com // The update phase. 16312954Sgabeblack@google.com update(); 16412954Sgabeblack@google.com 16512954Sgabeblack@google.com // The delta phase will happen naturally through the event queue. 16612953Sgabeblack@google.com} 16712953Sgabeblack@google.com 16812953Sgabeblack@google.comvoid 16912953Sgabeblack@google.comScheduler::update() 17012953Sgabeblack@google.com{ 17112954Sgabeblack@google.com Channel *channel = updateList.getNext(); 17212954Sgabeblack@google.com while (channel) { 17312954Sgabeblack@google.com channel->popListNode(); 17412954Sgabeblack@google.com channel->update(); 17512954Sgabeblack@google.com channel = updateList.getNext(); 17612954Sgabeblack@google.com } 17712953Sgabeblack@google.com} 17812953Sgabeblack@google.com 17912961Sgabeblack@google.comvoid 18012961Sgabeblack@google.comScheduler::pause() 18112961Sgabeblack@google.com{ 18212961Sgabeblack@google.com _paused = true; 18312982Sgabeblack@google.com kernel->status(::sc_core::SC_PAUSED); 18412961Sgabeblack@google.com scMain->run(); 18512961Sgabeblack@google.com} 18612961Sgabeblack@google.com 18712961Sgabeblack@google.comvoid 18812961Sgabeblack@google.comScheduler::stop() 18912961Sgabeblack@google.com{ 19012961Sgabeblack@google.com _stopped = true; 19112982Sgabeblack@google.com kernel->stop(); 19212961Sgabeblack@google.com scMain->run(); 19312961Sgabeblack@google.com} 19412961Sgabeblack@google.com 19512961Sgabeblack@google.comvoid 19612961Sgabeblack@google.comScheduler::start(Tick max_tick, bool run_to_time) 19712961Sgabeblack@google.com{ 19812961Sgabeblack@google.com // We should be running from sc_main. Keep track of that Fiber to return 19912961Sgabeblack@google.com // to later. 20012961Sgabeblack@google.com scMain = Fiber::currentFiber(); 20112961Sgabeblack@google.com 20212961Sgabeblack@google.com _started = true; 20312961Sgabeblack@google.com _paused = false; 20412961Sgabeblack@google.com _stopped = false; 20512961Sgabeblack@google.com 20612961Sgabeblack@google.com maxTick = max_tick; 20712961Sgabeblack@google.com 20812982Sgabeblack@google.com if (initReady) { 20912982Sgabeblack@google.com kernel->status(::sc_core::SC_RUNNING); 21012961Sgabeblack@google.com eq->schedule(&maxTickEvent, maxTick); 21112982Sgabeblack@google.com } 21212961Sgabeblack@google.com 21312961Sgabeblack@google.com // Return to gem5 to let it run events, etc. 21412961Sgabeblack@google.com Fiber::primaryFiber()->run(); 21512961Sgabeblack@google.com 21612961Sgabeblack@google.com if (pauseEvent.scheduled()) 21712961Sgabeblack@google.com eq->deschedule(&pauseEvent); 21812961Sgabeblack@google.com if (stopEvent.scheduled()) 21912961Sgabeblack@google.com eq->deschedule(&stopEvent); 22012961Sgabeblack@google.com if (maxTickEvent.scheduled()) 22112961Sgabeblack@google.com eq->deschedule(&maxTickEvent); 22212961Sgabeblack@google.com} 22312961Sgabeblack@google.com 22412961Sgabeblack@google.comvoid 22512961Sgabeblack@google.comScheduler::schedulePause() 22612961Sgabeblack@google.com{ 22712961Sgabeblack@google.com if (pauseEvent.scheduled()) 22812961Sgabeblack@google.com return; 22912961Sgabeblack@google.com 23012961Sgabeblack@google.com eq->schedule(&pauseEvent, eq->getCurTick()); 23112961Sgabeblack@google.com} 23212961Sgabeblack@google.com 23312961Sgabeblack@google.comvoid 23412961Sgabeblack@google.comScheduler::scheduleStop(bool finish_delta) 23512961Sgabeblack@google.com{ 23612961Sgabeblack@google.com if (stopEvent.scheduled()) 23712961Sgabeblack@google.com return; 23812961Sgabeblack@google.com 23912961Sgabeblack@google.com if (!finish_delta) { 24012961Sgabeblack@google.com // If we're not supposed to finish the delta cycle, flush the list 24112961Sgabeblack@google.com // of ready processes and scheduled updates. 24212961Sgabeblack@google.com Process *p; 24312961Sgabeblack@google.com while ((p = readyList.getNext())) 24412961Sgabeblack@google.com p->popListNode(); 24512961Sgabeblack@google.com Channel *c; 24612961Sgabeblack@google.com while ((c = updateList.getNext())) 24712961Sgabeblack@google.com c->popListNode(); 24812961Sgabeblack@google.com } 24912961Sgabeblack@google.com eq->schedule(&stopEvent, eq->getCurTick()); 25012961Sgabeblack@google.com} 25112961Sgabeblack@google.com 25212953Sgabeblack@google.comScheduler scheduler; 25312953Sgabeblack@google.com 25412953Sgabeblack@google.com} // namespace sc_gem5 255