scheduler.cc revision 12957
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" 3512953Sgabeblack@google.com 3612953Sgabeblack@google.comnamespace sc_gem5 3712953Sgabeblack@google.com{ 3812953Sgabeblack@google.com 3912954Sgabeblack@google.comScheduler::Scheduler() : 4012954Sgabeblack@google.com eq(nullptr), readyEvent(this, false, EventBase::Default_Pri + 1), 4112957Sgabeblack@google.com _numCycles(0), _current(nullptr), initReady(false) 4212954Sgabeblack@google.com{} 4312953Sgabeblack@google.com 4412953Sgabeblack@google.comvoid 4512957Sgabeblack@google.comScheduler::prepareForInit() 4612953Sgabeblack@google.com{ 4712957Sgabeblack@google.com for (Process *p = toFinalize.getNext(); p; p = toFinalize.getNext()) { 4812957Sgabeblack@google.com p->finalize(); 4912957Sgabeblack@google.com p->popListNode(); 5012957Sgabeblack@google.com } 5112957Sgabeblack@google.com 5212957Sgabeblack@google.com for (Process *p = initList.getNext(); p; p = initList.getNext()) { 5312957Sgabeblack@google.com p->finalize(); 5412957Sgabeblack@google.com ready(p); 5512957Sgabeblack@google.com } 5612957Sgabeblack@google.com 5712957Sgabeblack@google.com initReady = true; 5812957Sgabeblack@google.com} 5912957Sgabeblack@google.com 6012957Sgabeblack@google.comvoid 6112957Sgabeblack@google.comScheduler::reg(Process *p) 6212957Sgabeblack@google.com{ 6312957Sgabeblack@google.com if (initReady) { 6412957Sgabeblack@google.com // If we're past initialization, finalize static sensitivity. 6512957Sgabeblack@google.com p->finalize(); 6612957Sgabeblack@google.com // Mark the process as ready. 6712957Sgabeblack@google.com ready(p); 6812957Sgabeblack@google.com } else { 6912957Sgabeblack@google.com // Otherwise, record that this process should be initialized once we 7012957Sgabeblack@google.com // get there. 7112957Sgabeblack@google.com initList.pushLast(p); 7212957Sgabeblack@google.com } 7312957Sgabeblack@google.com} 7412957Sgabeblack@google.com 7512957Sgabeblack@google.comvoid 7612957Sgabeblack@google.comScheduler::dontInitialize(Process *p) 7712957Sgabeblack@google.com{ 7812957Sgabeblack@google.com if (initReady) { 7912957Sgabeblack@google.com // Pop this process off of the ready list. 8012957Sgabeblack@google.com p->popListNode(); 8112957Sgabeblack@google.com } else { 8212957Sgabeblack@google.com // Push this process onto the list of processes which still need 8312957Sgabeblack@google.com // their static sensitivity to be finalized. That implicitly pops it 8412957Sgabeblack@google.com // off the list of processes to be initialized/marked ready. 8512957Sgabeblack@google.com toFinalize.pushLast(p); 8612957Sgabeblack@google.com } 8712953Sgabeblack@google.com} 8812953Sgabeblack@google.com 8912953Sgabeblack@google.comvoid 9012953Sgabeblack@google.comScheduler::yield() 9112953Sgabeblack@google.com{ 9212953Sgabeblack@google.com _current = readyList.getNext(); 9312953Sgabeblack@google.com if (!_current) { 9412953Sgabeblack@google.com // There are no more processes, so return control to evaluate. 9512953Sgabeblack@google.com Fiber::primaryFiber()->run(); 9612953Sgabeblack@google.com } else { 9712953Sgabeblack@google.com _current->popListNode(); 9812953Sgabeblack@google.com // Switch to whatever Fiber is supposed to run this process. All 9912953Sgabeblack@google.com // Fibers which aren't running should be parked at this line. 10012953Sgabeblack@google.com _current->fiber()->run(); 10112953Sgabeblack@google.com // If the current process hasn't been started yet, start it. This 10212953Sgabeblack@google.com // should always be true for methods, but may not be true for threads. 10312953Sgabeblack@google.com if (_current && !_current->running()) 10412953Sgabeblack@google.com _current->run(); 10512953Sgabeblack@google.com } 10612953Sgabeblack@google.com} 10712953Sgabeblack@google.com 10812953Sgabeblack@google.comvoid 10912954Sgabeblack@google.comScheduler::ready(Process *p) 11012953Sgabeblack@google.com{ 11112954Sgabeblack@google.com // Clump methods together to minimize context switching. 11212954Sgabeblack@google.com if (p->procKind() == ::sc_core::SC_METHOD_PROC_) 11312954Sgabeblack@google.com readyList.pushFirst(p); 11412954Sgabeblack@google.com else 11512954Sgabeblack@google.com readyList.pushLast(p); 11612953Sgabeblack@google.com 11712954Sgabeblack@google.com scheduleReadyEvent(); 11812954Sgabeblack@google.com} 11912954Sgabeblack@google.com 12012954Sgabeblack@google.comvoid 12112954Sgabeblack@google.comScheduler::requestUpdate(Channel *c) 12212954Sgabeblack@google.com{ 12312954Sgabeblack@google.com updateList.pushLast(c); 12412954Sgabeblack@google.com scheduleReadyEvent(); 12512954Sgabeblack@google.com} 12612954Sgabeblack@google.com 12712954Sgabeblack@google.comvoid 12812954Sgabeblack@google.comScheduler::scheduleReadyEvent() 12912954Sgabeblack@google.com{ 13012954Sgabeblack@google.com // Schedule the evaluate and update phases. 13112954Sgabeblack@google.com if (!readyEvent.scheduled()) { 13212954Sgabeblack@google.com panic_if(!eq, "Need to schedule ready, but no event manager.\n"); 13312954Sgabeblack@google.com eq->schedule(&readyEvent, eq->getCurTick()); 13412954Sgabeblack@google.com } 13512954Sgabeblack@google.com} 13612954Sgabeblack@google.com 13712954Sgabeblack@google.comvoid 13812954Sgabeblack@google.comScheduler::runReady() 13912954Sgabeblack@google.com{ 14012954Sgabeblack@google.com bool empty = readyList.empty(); 14112954Sgabeblack@google.com 14212954Sgabeblack@google.com // The evaluation phase. 14312953Sgabeblack@google.com do { 14412953Sgabeblack@google.com yield(); 14512953Sgabeblack@google.com } while (!readyList.empty()); 14612954Sgabeblack@google.com 14712954Sgabeblack@google.com if (!empty) 14812954Sgabeblack@google.com _numCycles++; 14912954Sgabeblack@google.com 15012954Sgabeblack@google.com // The update phase. 15112954Sgabeblack@google.com update(); 15212954Sgabeblack@google.com 15312954Sgabeblack@google.com // The delta phase will happen naturally through the event queue. 15412953Sgabeblack@google.com} 15512953Sgabeblack@google.com 15612953Sgabeblack@google.comvoid 15712953Sgabeblack@google.comScheduler::update() 15812953Sgabeblack@google.com{ 15912954Sgabeblack@google.com Channel *channel = updateList.getNext(); 16012954Sgabeblack@google.com while (channel) { 16112954Sgabeblack@google.com channel->popListNode(); 16212954Sgabeblack@google.com channel->update(); 16312954Sgabeblack@google.com channel = updateList.getNext(); 16412954Sgabeblack@google.com } 16512953Sgabeblack@google.com} 16612953Sgabeblack@google.com 16712953Sgabeblack@google.comScheduler scheduler; 16812953Sgabeblack@google.com 16912953Sgabeblack@google.com} // namespace sc_gem5 170