scheduler.hh revision 13061
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#ifndef __SYSTEMC_CORE_SCHEDULER_HH__
3112953Sgabeblack@google.com#define __SYSTEMC_CORE_SCHEDULER_HH__
3212953Sgabeblack@google.com
3312957Sgabeblack@google.com#include <vector>
3412957Sgabeblack@google.com
3512961Sgabeblack@google.com#include "base/logging.hh"
3612954Sgabeblack@google.com#include "sim/eventq.hh"
3712954Sgabeblack@google.com#include "systemc/core/channel.hh"
3812953Sgabeblack@google.com#include "systemc/core/list.hh"
3912953Sgabeblack@google.com#include "systemc/core/process.hh"
4012953Sgabeblack@google.com
4112961Sgabeblack@google.comclass Fiber;
4212961Sgabeblack@google.com
4312953Sgabeblack@google.comnamespace sc_gem5
4412953Sgabeblack@google.com{
4512953Sgabeblack@google.com
4612953Sgabeblack@google.comtypedef NodeList<Process> ProcessList;
4712954Sgabeblack@google.comtypedef NodeList<Channel> ChannelList;
4812954Sgabeblack@google.com
4912954Sgabeblack@google.com/*
5012954Sgabeblack@google.com * The scheduler supports three different mechanisms, the initialization phase,
5112954Sgabeblack@google.com * delta cycles, and timed notifications.
5212954Sgabeblack@google.com *
5312954Sgabeblack@google.com * INITIALIZATION PHASE
5412954Sgabeblack@google.com *
5512954Sgabeblack@google.com * The initialization phase has three parts:
5612954Sgabeblack@google.com * 1. Run requested channel updates.
5712954Sgabeblack@google.com * 2. Make processes which need to initialize runnable (methods and threads
5812954Sgabeblack@google.com *    which didn't have dont_initialize called on them).
5912954Sgabeblack@google.com * 3. Process delta notifications.
6012954Sgabeblack@google.com *
6112954Sgabeblack@google.com * First, the Kernel SimObject calls the update() method during its startup()
6212954Sgabeblack@google.com * callback which handles the requested channel updates. The Kernel also
6312954Sgabeblack@google.com * schedules an event to be run at time 0 with a slightly elevated priority
6412954Sgabeblack@google.com * so that it happens before any "normal" event.
6512954Sgabeblack@google.com *
6612957Sgabeblack@google.com * When that t0 event happens, it calls the schedulers prepareForInit method
6712954Sgabeblack@google.com * which performs step 2 above. That indirectly causes the scheduler's
6812954Sgabeblack@google.com * readyEvent to be scheduled with slightly lowered priority, ensuring it
6912954Sgabeblack@google.com * happens after any "normal" event.
7012954Sgabeblack@google.com *
7112954Sgabeblack@google.com * Because delta notifications are scheduled at the standard priority, all
7212954Sgabeblack@google.com * of those events will happen next, performing step 3 above. Once they finish,
7312954Sgabeblack@google.com * if the readyEvent was scheduled above, there shouldn't be any higher
7412954Sgabeblack@google.com * priority events in front of it. When it runs, it will start the first
7512954Sgabeblack@google.com * evaluate phase of the first delta cycle.
7612954Sgabeblack@google.com *
7712954Sgabeblack@google.com * DELTA CYCLE
7812954Sgabeblack@google.com *
7912954Sgabeblack@google.com * A delta cycle has three phases within it.
8012954Sgabeblack@google.com * 1. The evaluate phase where runnable processes are allowed to run.
8112954Sgabeblack@google.com * 2. The update phase where requested channel updates hapen.
8212954Sgabeblack@google.com * 3. The delta notification phase where delta notifications happen.
8312954Sgabeblack@google.com *
8412954Sgabeblack@google.com * The readyEvent runs the first two steps of the delta cycle. It first goes
8512954Sgabeblack@google.com * through the list of runnable processes and executes them until the set is
8612954Sgabeblack@google.com * empty, and then immediately runs the update phase. Since these are all part
8712954Sgabeblack@google.com * of the same event, there's no chance for other events to intervene and
8812954Sgabeblack@google.com * break the required order above.
8912954Sgabeblack@google.com *
9012954Sgabeblack@google.com * During the update phase above, the spec forbids any action which would make
9112954Sgabeblack@google.com * a process runnable. That means that once the update phase finishes, the set
9212954Sgabeblack@google.com * of runnable processes will be empty. There may, however, have been some
9312954Sgabeblack@google.com * delta notifications/timeouts which will have been scheduled during either
9412954Sgabeblack@google.com * the evaluate or update phase above. Because those are scheduled at the
9512954Sgabeblack@google.com * normal priority, they will now happen together until there aren't any
9612954Sgabeblack@google.com * delta events left.
9712954Sgabeblack@google.com *
9812954Sgabeblack@google.com * If any processes became runnable during the delta notification phase, the
9912954Sgabeblack@google.com * readyEvent will have been scheduled and will have been waiting patiently
10012954Sgabeblack@google.com * behind the delta notification events. That will now run, effectively
10112954Sgabeblack@google.com * starting the next delta cycle.
10212954Sgabeblack@google.com *
10312954Sgabeblack@google.com * TIMED NOTIFICATION PHASE
10412954Sgabeblack@google.com *
10512954Sgabeblack@google.com * If no processes became runnable, the event queue will continue to process
10612954Sgabeblack@google.com * events until it comes across a timed notification, aka a notification
10712954Sgabeblack@google.com * scheduled to happen in the future. Like delta notification events, those
10812954Sgabeblack@google.com * will all happen together since the readyEvent priority is lower,
10912954Sgabeblack@google.com * potentially marking new processes as ready. Once these events finish, the
11012954Sgabeblack@google.com * readyEvent may run, starting the next delta cycle.
11112961Sgabeblack@google.com *
11212961Sgabeblack@google.com * PAUSE/STOP
11312961Sgabeblack@google.com *
11412961Sgabeblack@google.com * To inject a pause from sc_pause which should happen after the current delta
11512961Sgabeblack@google.com * cycle's delta notification phase, an event is scheduled with a lower than
11612961Sgabeblack@google.com * normal priority, but higher than the readyEvent. That ensures that any
11712961Sgabeblack@google.com * delta notifications which are scheduled with normal priority will happen
11812961Sgabeblack@google.com * first, since those are part of the current delta cycle. Then the pause
11912961Sgabeblack@google.com * event will happen before the next readyEvent which would start the next
12012961Sgabeblack@google.com * delta cycle. All of these events are scheduled for the current time, and so
12112961Sgabeblack@google.com * would happen before any timed notifications went off.
12212961Sgabeblack@google.com *
12312961Sgabeblack@google.com * To inject a stop from sc_stop, the delta cycles should stop before even the
12412961Sgabeblack@google.com * delta notifications have happened, but after the evaluate and update phases.
12512961Sgabeblack@google.com * For that, a stop event with slightly higher than normal priority will be
12612961Sgabeblack@google.com * scheduled so that it happens before any of the delta notification events
12712961Sgabeblack@google.com * which are at normal priority.
12812961Sgabeblack@google.com *
12912961Sgabeblack@google.com * MAX RUN TIME
13012961Sgabeblack@google.com *
13112961Sgabeblack@google.com * When sc_start is called, it's possible to pass in a maximum time the
13213058Sgabeblack@google.com * simulation should run to, at which point sc_pause is implicitly called. The
13313058Sgabeblack@google.com * simulation is supposed to run up to the latest timed notification phase
13413058Sgabeblack@google.com * which is less than or equal to the maximum time. In other words it should
13513058Sgabeblack@google.com * run timed notifications at the maximum time, but not the subsequent evaluate
13613058Sgabeblack@google.com * phase. That's implemented by scheduling an event at the max time with a
13713058Sgabeblack@google.com * priority which is lower than all the others except the ready event. Timed
13813058Sgabeblack@google.com * notifications will happen before it fires, but it will override any ready
13913058Sgabeblack@google.com * event and prevent the evaluate phase from starting.
14012954Sgabeblack@google.com */
14112953Sgabeblack@google.com
14212953Sgabeblack@google.comclass Scheduler
14312953Sgabeblack@google.com{
14412953Sgabeblack@google.com  public:
14512953Sgabeblack@google.com    Scheduler();
14612953Sgabeblack@google.com
14712954Sgabeblack@google.com    const std::string name() const { return "systemc_scheduler"; }
14812954Sgabeblack@google.com
14912953Sgabeblack@google.com    uint64_t numCycles() { return _numCycles; }
15012953Sgabeblack@google.com    Process *current() { return _current; }
15112953Sgabeblack@google.com
15212957Sgabeblack@google.com    // Prepare for initialization.
15312957Sgabeblack@google.com    void prepareForInit();
15412953Sgabeblack@google.com
15512957Sgabeblack@google.com    // Register a process with the scheduler.
15612957Sgabeblack@google.com    void reg(Process *p);
15712957Sgabeblack@google.com
15812957Sgabeblack@google.com    // Tell the scheduler not to initialize a process.
15912957Sgabeblack@google.com    void dontInitialize(Process *p);
16012953Sgabeblack@google.com
16112953Sgabeblack@google.com    // Run the next process, if there is one.
16212953Sgabeblack@google.com    void yield();
16312953Sgabeblack@google.com
16412953Sgabeblack@google.com    // Put a process on the ready list.
16512954Sgabeblack@google.com    void ready(Process *p);
16612954Sgabeblack@google.com
16712954Sgabeblack@google.com    // Schedule an update for a given channel.
16812954Sgabeblack@google.com    void requestUpdate(Channel *c);
16912953Sgabeblack@google.com
17012953Sgabeblack@google.com    // Run the given process immediately, preempting whatever may be running.
17112953Sgabeblack@google.com    void
17212953Sgabeblack@google.com    runNow(Process *p)
17312953Sgabeblack@google.com    {
17412953Sgabeblack@google.com        // If a process is running, schedule it/us to run again.
17512953Sgabeblack@google.com        if (_current)
17612953Sgabeblack@google.com            readyList.pushFirst(_current);
17712953Sgabeblack@google.com        // Schedule p to run first.
17812953Sgabeblack@google.com        readyList.pushFirst(p);
17912953Sgabeblack@google.com        yield();
18012953Sgabeblack@google.com    }
18112953Sgabeblack@google.com
18212954Sgabeblack@google.com    // Set an event queue for scheduling events.
18312954Sgabeblack@google.com    void setEventQueue(EventQueue *_eq) { eq = _eq; }
18412954Sgabeblack@google.com
18512962Sgabeblack@google.com    // Get the current time according to gem5.
18612962Sgabeblack@google.com    Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
18712962Sgabeblack@google.com
18812962Sgabeblack@google.com    // For scheduling delayed/timed notifications/timeouts.
18912962Sgabeblack@google.com    void
19012962Sgabeblack@google.com    schedule(::Event *event, Tick tick)
19112962Sgabeblack@google.com    {
19212962Sgabeblack@google.com        pendingTicks[tick]++;
19312985Sgabeblack@google.com
19412985Sgabeblack@google.com        if (initReady)
19512985Sgabeblack@google.com            eq->schedule(event, tick);
19612985Sgabeblack@google.com        else
19712985Sgabeblack@google.com            eventsToSchedule[event] = tick;
19812962Sgabeblack@google.com    }
19912962Sgabeblack@google.com
20012962Sgabeblack@google.com    // For descheduling delayed/timed notifications/timeouts.
20112962Sgabeblack@google.com    void
20212962Sgabeblack@google.com    deschedule(::Event *event)
20312962Sgabeblack@google.com    {
20412962Sgabeblack@google.com        auto it = pendingTicks.find(event->when());
20512962Sgabeblack@google.com        if (--it->second == 0)
20612962Sgabeblack@google.com            pendingTicks.erase(it);
20712985Sgabeblack@google.com
20812985Sgabeblack@google.com        if (initReady)
20912985Sgabeblack@google.com            eq->deschedule(event);
21012985Sgabeblack@google.com        else
21112985Sgabeblack@google.com            eventsToSchedule.erase(event);
21212962Sgabeblack@google.com    }
21312962Sgabeblack@google.com
21412962Sgabeblack@google.com    // Tell the scheduler than an event fired for bookkeeping purposes.
21512962Sgabeblack@google.com    void
21612962Sgabeblack@google.com    eventHappened()
21712962Sgabeblack@google.com    {
21812962Sgabeblack@google.com        auto it = pendingTicks.begin();
21912962Sgabeblack@google.com        if (--it->second == 0)
22012962Sgabeblack@google.com            pendingTicks.erase(it);
22112987Sgabeblack@google.com
22212987Sgabeblack@google.com        if (starved() && !runToTime)
22312987Sgabeblack@google.com            scheduleStarvationEvent();
22412962Sgabeblack@google.com    }
22512962Sgabeblack@google.com
22612962Sgabeblack@google.com    // Pending activity ignores gem5 activity, much like how a systemc
22712962Sgabeblack@google.com    // simulation wouldn't know about asynchronous external events (socket IO
22812962Sgabeblack@google.com    // for instance) that might happen before time advances in a pure
22912962Sgabeblack@google.com    // systemc simulation. Also the spec lists what specific types of pending
23012962Sgabeblack@google.com    // activity needs to be counted, which obviously doesn't include gem5
23112962Sgabeblack@google.com    // events.
23212962Sgabeblack@google.com
23312962Sgabeblack@google.com    // Return whether there's pending systemc activity at this time.
23412962Sgabeblack@google.com    bool
23512962Sgabeblack@google.com    pendingCurr()
23612962Sgabeblack@google.com    {
23712962Sgabeblack@google.com        if (!readyList.empty() || !updateList.empty())
23812962Sgabeblack@google.com            return true;
23912962Sgabeblack@google.com        return pendingTicks.size() &&
24012962Sgabeblack@google.com            pendingTicks.begin()->first == getCurTick();
24112962Sgabeblack@google.com    }
24212962Sgabeblack@google.com
24312962Sgabeblack@google.com    // Return whether there are pending timed notifications or timeouts.
24412962Sgabeblack@google.com    bool
24512962Sgabeblack@google.com    pendingFuture()
24612962Sgabeblack@google.com    {
24712962Sgabeblack@google.com        switch (pendingTicks.size()) {
24812962Sgabeblack@google.com          case 0: return false;
24912962Sgabeblack@google.com          case 1: return pendingTicks.begin()->first > getCurTick();
25012962Sgabeblack@google.com          default: return true;
25112962Sgabeblack@google.com        }
25212962Sgabeblack@google.com    }
25312962Sgabeblack@google.com
25412962Sgabeblack@google.com    // Return how many ticks there are until the first pending event, if any.
25512962Sgabeblack@google.com    Tick
25612962Sgabeblack@google.com    timeToPending()
25712962Sgabeblack@google.com    {
25812962Sgabeblack@google.com        if (!readyList.empty() || !updateList.empty())
25912962Sgabeblack@google.com            return 0;
26012962Sgabeblack@google.com        else if (pendingTicks.size())
26112962Sgabeblack@google.com            return pendingTicks.begin()->first - getCurTick();
26212962Sgabeblack@google.com        else
26312962Sgabeblack@google.com            return MaxTick - getCurTick();
26412962Sgabeblack@google.com    }
26512954Sgabeblack@google.com
26612954Sgabeblack@google.com    // Run scheduled channel updates.
26712954Sgabeblack@google.com    void update();
26812954Sgabeblack@google.com
26912961Sgabeblack@google.com    void setScMainFiber(Fiber *sc_main) { scMain = sc_main; }
27012961Sgabeblack@google.com
27112961Sgabeblack@google.com    void start(Tick max_tick, bool run_to_time);
27213061Sgabeblack@google.com    void oneCycle();
27312961Sgabeblack@google.com
27412961Sgabeblack@google.com    void schedulePause();
27512961Sgabeblack@google.com    void scheduleStop(bool finish_delta);
27612961Sgabeblack@google.com
27712961Sgabeblack@google.com    bool paused() { return _paused; }
27812961Sgabeblack@google.com    bool stopped() { return _stopped; }
27912961Sgabeblack@google.com
28012953Sgabeblack@google.com  private:
28112961Sgabeblack@google.com    typedef const EventBase::Priority Priority;
28212961Sgabeblack@google.com    static Priority DefaultPriority = EventBase::Default_Pri;
28312961Sgabeblack@google.com
28412961Sgabeblack@google.com    static Priority StopPriority = DefaultPriority - 1;
28512961Sgabeblack@google.com    static Priority PausePriority = DefaultPriority + 1;
28613058Sgabeblack@google.com    static Priority MaxTickPriority = DefaultPriority + 2;
28713058Sgabeblack@google.com    static Priority ReadyPriority = DefaultPriority + 3;
28812987Sgabeblack@google.com    static Priority StarvationPriority = ReadyPriority;
28912961Sgabeblack@google.com
29012954Sgabeblack@google.com    EventQueue *eq;
29112962Sgabeblack@google.com    std::map<Tick, int> pendingTicks;
29212954Sgabeblack@google.com
29312954Sgabeblack@google.com    void runReady();
29412954Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
29512954Sgabeblack@google.com    void scheduleReadyEvent();
29612954Sgabeblack@google.com
29712961Sgabeblack@google.com    void pause();
29812961Sgabeblack@google.com    void stop();
29912961Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
30012961Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
30112961Sgabeblack@google.com    Fiber *scMain;
30212961Sgabeblack@google.com
30312987Sgabeblack@google.com    bool
30412987Sgabeblack@google.com    starved()
30512987Sgabeblack@google.com    {
30612987Sgabeblack@google.com        return (readyList.empty() && updateList.empty() &&
30712987Sgabeblack@google.com                (pendingTicks.empty() ||
30812987Sgabeblack@google.com                 pendingTicks.begin()->first > maxTick) &&
30912987Sgabeblack@google.com                initList.empty());
31012987Sgabeblack@google.com    }
31112987Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
31212987Sgabeblack@google.com    void scheduleStarvationEvent();
31312987Sgabeblack@google.com
31412961Sgabeblack@google.com    bool _started;
31512961Sgabeblack@google.com    bool _paused;
31612961Sgabeblack@google.com    bool _stopped;
31712961Sgabeblack@google.com
31812961Sgabeblack@google.com    Tick maxTick;
31912961Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::pause> maxTickEvent;
32012961Sgabeblack@google.com
32112953Sgabeblack@google.com    uint64_t _numCycles;
32212953Sgabeblack@google.com
32312953Sgabeblack@google.com    Process *_current;
32412953Sgabeblack@google.com
32512957Sgabeblack@google.com    bool initReady;
32612987Sgabeblack@google.com    bool runToTime;
32713061Sgabeblack@google.com    bool runOnce;
32812957Sgabeblack@google.com
32912953Sgabeblack@google.com    ProcessList initList;
33012957Sgabeblack@google.com    ProcessList toFinalize;
33112953Sgabeblack@google.com    ProcessList readyList;
33212953Sgabeblack@google.com
33312954Sgabeblack@google.com    ChannelList updateList;
33412985Sgabeblack@google.com
33512985Sgabeblack@google.com    std::map<::Event *, Tick> eventsToSchedule;
33612953Sgabeblack@google.com};
33712953Sgabeblack@google.com
33812953Sgabeblack@google.comextern Scheduler scheduler;
33912953Sgabeblack@google.com
34012953Sgabeblack@google.com} // namespace sc_gem5
34112953Sgabeblack@google.com
34212953Sgabeblack@google.com#endif // __SYSTEMC_CORE_SCHEDULER_H__
343