scheduler.cc revision 12954:8ea3a185354c
12155SN/A/*
22155SN/A * Copyright 2018 Google, Inc.
32155SN/A *
42155SN/A * Redistribution and use in source and binary forms, with or without
52155SN/A * modification, are permitted provided that the following conditions are
62155SN/A * met: redistributions of source code must retain the above copyright
72155SN/A * notice, this list of conditions and the following disclaimer;
82155SN/A * redistributions in binary form must reproduce the above copyright
92155SN/A * notice, this list of conditions and the following disclaimer in the
102155SN/A * documentation and/or other materials provided with the distribution;
112155SN/A * neither the name of the copyright holders nor the names of its
122155SN/A * contributors may be used to endorse or promote products derived from
132155SN/A * this software without specific prior written permission.
142155SN/A *
152155SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
162155SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
172155SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
182155SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
192155SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
202155SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
212155SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222155SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232155SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242155SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
252155SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262155SN/A *
272155SN/A * Authors: Gabe Black
282665Ssaidi@eecs.umich.edu */
292665Ssaidi@eecs.umich.edu
302155SN/A#include "systemc/core/scheduler.hh"
314202Sbinkertn@umich.edu
322155SN/A#include "base/fiber.hh"
337768SAli.Saidi@ARM.com#include "base/logging.hh"
347768SAli.Saidi@ARM.com#include "sim/eventq.hh"
357768SAli.Saidi@ARM.com
362178SN/Anamespace sc_gem5
372178SN/A{
382178SN/A
392178SN/AScheduler::Scheduler() :
402178SN/A    eq(nullptr), readyEvent(this, false, EventBase::Default_Pri + 1),
412178SN/A    _numCycles(0), _current(nullptr)
422178SN/A{}
432178SN/A
442178SN/Avoid
452178SN/AScheduler::initToReady()
462178SN/A{
472155SN/A    while (!initList.empty())
485865Sksewell@umich.edu        ready(initList.getNext());
496181Sksewell@umich.edu}
506181Sksewell@umich.edu
515865Sksewell@umich.eduvoid
523918Ssaidi@eecs.umich.eduScheduler::yield()
535865Sksewell@umich.edu{
542623SN/A    _current = readyList.getNext();
553918Ssaidi@eecs.umich.edu    if (!_current) {
562155SN/A        // There are no more processes, so return control to evaluate.
572155SN/A        Fiber::primaryFiber()->run();
582292SN/A    } else {
596181Sksewell@umich.edu        _current->popListNode();
606181Sksewell@umich.edu        // Switch to whatever Fiber is supposed to run this process. All
613918Ssaidi@eecs.umich.edu        // Fibers which aren't running should be parked at this line.
622292SN/A        _current->fiber()->run();
632292SN/A        // If the current process hasn't been started yet, start it. This
642292SN/A        // should always be true for methods, but may not be true for threads.
653918Ssaidi@eecs.umich.edu        if (_current && !_current->running())
662292SN/A            _current->run();
672292SN/A    }
682766Sktlim@umich.edu}
692766Sktlim@umich.edu
702766Sktlim@umich.eduvoid
712921Sktlim@umich.eduScheduler::ready(Process *p)
728887Sgeoffrey.blake@arm.com{
738887Sgeoffrey.blake@arm.com    // Clump methods together to minimize context switching.
742766Sktlim@umich.edu    if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
754762Snate@binkert.org        readyList.pushFirst(p);
762155SN/A    else
772155SN/A        readyList.pushLast(p);
782155SN/A
792155SN/A    scheduleReadyEvent();
802155SN/A}
812155SN/A
822766Sktlim@umich.eduvoid
832155SN/AScheduler::requestUpdate(Channel *c)
845865Sksewell@umich.edu{
852155SN/A    updateList.pushLast(c);
862155SN/A    scheduleReadyEvent();
872155SN/A}
882155SN/A
892178SN/Avoid
902178SN/AScheduler::scheduleReadyEvent()
917756SAli.Saidi@ARM.com{
922766Sktlim@umich.edu    // Schedule the evaluate and update phases.
932178SN/A    if (!readyEvent.scheduled()) {
942178SN/A        panic_if(!eq, "Need to schedule ready, but no event manager.\n");
956994Snate@binkert.org        eq->schedule(&readyEvent, eq->getCurTick());
962178SN/A    }
972766Sktlim@umich.edu}
982766Sktlim@umich.edu
992788Sktlim@umich.eduvoid
1002178SN/AScheduler::runReady()
1014486Sbinkertn@umich.edu{
1024486Sbinkertn@umich.edu    bool empty = readyList.empty();
1034776Sgblack@eecs.umich.edu
1044776Sgblack@eecs.umich.edu    // The evaluation phase.
1058739Sgblack@eecs.umich.edu    do {
1066365Sgblack@eecs.umich.edu        yield();
1074486Sbinkertn@umich.edu    } while (!readyList.empty());
1084202Sbinkertn@umich.edu
1094202Sbinkertn@umich.edu    if (!empty)
1104202Sbinkertn@umich.edu        _numCycles++;
1118541Sgblack@eecs.umich.edu
1124202Sbinkertn@umich.edu    // The update phase.
1134202Sbinkertn@umich.edu    update();
1144776Sgblack@eecs.umich.edu
1158739Sgblack@eecs.umich.edu    // The delta phase will happen naturally through the event queue.
1166365Sgblack@eecs.umich.edu}
1174202Sbinkertn@umich.edu
1188777Sgblack@eecs.umich.eduvoid
1194202Sbinkertn@umich.eduScheduler::update()
1204202Sbinkertn@umich.edu{
1214202Sbinkertn@umich.edu    Channel *channel = updateList.getNext();
1225217Ssaidi@eecs.umich.edu    while (channel) {
1234202Sbinkertn@umich.edu        channel->popListNode();
1242155SN/A        channel->update();
1258793Sgblack@eecs.umich.edu        channel = updateList.getNext();
1268793Sgblack@eecs.umich.edu    }
1278793Sgblack@eecs.umich.edu}
1284776Sgblack@eecs.umich.edu
1298887Sgeoffrey.blake@arm.comScheduler scheduler;
1308887Sgeoffrey.blake@arm.com
1318887Sgeoffrey.blake@arm.com} // namespace sc_gem5
1328887Sgeoffrey.blake@arm.com