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
3313063Sgabeblack@google.com#include <functional>
3413063Sgabeblack@google.com#include <map>
3513901Sgabeblack@google.com#include <mutex>
3613063Sgabeblack@google.com#include <set>
3712957Sgabeblack@google.com#include <vector>
3812957Sgabeblack@google.com
3912961Sgabeblack@google.com#include "base/logging.hh"
4013063Sgabeblack@google.com#include "sim/core.hh"
4112954Sgabeblack@google.com#include "sim/eventq.hh"
4212954Sgabeblack@google.com#include "systemc/core/channel.hh"
4312953Sgabeblack@google.com#include "systemc/core/list.hh"
4412953Sgabeblack@google.com#include "systemc/core/process.hh"
4513063Sgabeblack@google.com#include "systemc/core/sched_event.hh"
4612953Sgabeblack@google.com
4712961Sgabeblack@google.comclass Fiber;
4812961Sgabeblack@google.com
4912953Sgabeblack@google.comnamespace sc_gem5
5012953Sgabeblack@google.com{
5112953Sgabeblack@google.com
5213245Sgabeblack@google.comclass TraceFile;
5313245Sgabeblack@google.com
5412953Sgabeblack@google.comtypedef NodeList<Process> ProcessList;
5512954Sgabeblack@google.comtypedef NodeList<Channel> ChannelList;
5612954Sgabeblack@google.com
5712954Sgabeblack@google.com/*
5812954Sgabeblack@google.com * The scheduler supports three different mechanisms, the initialization phase,
5912954Sgabeblack@google.com * delta cycles, and timed notifications.
6012954Sgabeblack@google.com *
6112954Sgabeblack@google.com * INITIALIZATION PHASE
6212954Sgabeblack@google.com *
6312954Sgabeblack@google.com * The initialization phase has three parts:
6412954Sgabeblack@google.com * 1. Run requested channel updates.
6512954Sgabeblack@google.com * 2. Make processes which need to initialize runnable (methods and threads
6612954Sgabeblack@google.com *    which didn't have dont_initialize called on them).
6712954Sgabeblack@google.com * 3. Process delta notifications.
6812954Sgabeblack@google.com *
6912954Sgabeblack@google.com * First, the Kernel SimObject calls the update() method during its startup()
7012954Sgabeblack@google.com * callback which handles the requested channel updates. The Kernel also
7112954Sgabeblack@google.com * schedules an event to be run at time 0 with a slightly elevated priority
7212954Sgabeblack@google.com * so that it happens before any "normal" event.
7312954Sgabeblack@google.com *
7412957Sgabeblack@google.com * When that t0 event happens, it calls the schedulers prepareForInit method
7512954Sgabeblack@google.com * which performs step 2 above. That indirectly causes the scheduler's
7612954Sgabeblack@google.com * readyEvent to be scheduled with slightly lowered priority, ensuring it
7712954Sgabeblack@google.com * happens after any "normal" event.
7812954Sgabeblack@google.com *
7912954Sgabeblack@google.com * Because delta notifications are scheduled at the standard priority, all
8012954Sgabeblack@google.com * of those events will happen next, performing step 3 above. Once they finish,
8112954Sgabeblack@google.com * if the readyEvent was scheduled above, there shouldn't be any higher
8212954Sgabeblack@google.com * priority events in front of it. When it runs, it will start the first
8312954Sgabeblack@google.com * evaluate phase of the first delta cycle.
8412954Sgabeblack@google.com *
8512954Sgabeblack@google.com * DELTA CYCLE
8612954Sgabeblack@google.com *
8712954Sgabeblack@google.com * A delta cycle has three phases within it.
8812954Sgabeblack@google.com * 1. The evaluate phase where runnable processes are allowed to run.
8912954Sgabeblack@google.com * 2. The update phase where requested channel updates hapen.
9012954Sgabeblack@google.com * 3. The delta notification phase where delta notifications happen.
9112954Sgabeblack@google.com *
9213063Sgabeblack@google.com * The readyEvent runs all three steps of the delta cycle. It first goes
9312954Sgabeblack@google.com * through the list of runnable processes and executes them until the set is
9412954Sgabeblack@google.com * empty, and then immediately runs the update phase. Since these are all part
9512954Sgabeblack@google.com * of the same event, there's no chance for other events to intervene and
9612954Sgabeblack@google.com * break the required order above.
9712954Sgabeblack@google.com *
9812954Sgabeblack@google.com * During the update phase above, the spec forbids any action which would make
9912954Sgabeblack@google.com * a process runnable. That means that once the update phase finishes, the set
10012954Sgabeblack@google.com * of runnable processes will be empty. There may, however, have been some
10112954Sgabeblack@google.com * delta notifications/timeouts which will have been scheduled during either
10213063Sgabeblack@google.com * the evaluate or update phase above. Those will have been accumulated in the
10313063Sgabeblack@google.com * scheduler, and are now all executed.
10412954Sgabeblack@google.com *
10512954Sgabeblack@google.com * If any processes became runnable during the delta notification phase, the
10613063Sgabeblack@google.com * readyEvent will have been scheduled and will be waiting and ready to run
10713063Sgabeblack@google.com * again, effectively starting the next delta cycle.
10812954Sgabeblack@google.com *
10912954Sgabeblack@google.com * TIMED NOTIFICATION PHASE
11012954Sgabeblack@google.com *
11112954Sgabeblack@google.com * If no processes became runnable, the event queue will continue to process
11213063Sgabeblack@google.com * events until it comes across an event which represents all the timed
11313063Sgabeblack@google.com * notifications which are supposed to happen at a particular time. The object
11413063Sgabeblack@google.com * which tracks them will execute all those notifications, and then destroy
11513063Sgabeblack@google.com * itself. If the readyEvent is now ready to run, the next delta cycle will
11613063Sgabeblack@google.com * start.
11712961Sgabeblack@google.com *
11812961Sgabeblack@google.com * PAUSE/STOP
11912961Sgabeblack@google.com *
12012961Sgabeblack@google.com * To inject a pause from sc_pause which should happen after the current delta
12112961Sgabeblack@google.com * cycle's delta notification phase, an event is scheduled with a lower than
12212961Sgabeblack@google.com * normal priority, but higher than the readyEvent. That ensures that any
12312961Sgabeblack@google.com * delta notifications which are scheduled with normal priority will happen
12412961Sgabeblack@google.com * first, since those are part of the current delta cycle. Then the pause
12512961Sgabeblack@google.com * event will happen before the next readyEvent which would start the next
12612961Sgabeblack@google.com * delta cycle. All of these events are scheduled for the current time, and so
12712961Sgabeblack@google.com * would happen before any timed notifications went off.
12812961Sgabeblack@google.com *
12912961Sgabeblack@google.com * To inject a stop from sc_stop, the delta cycles should stop before even the
13012961Sgabeblack@google.com * delta notifications have happened, but after the evaluate and update phases.
13112961Sgabeblack@google.com * For that, a stop event with slightly higher than normal priority will be
13212961Sgabeblack@google.com * scheduled so that it happens before any of the delta notification events
13312961Sgabeblack@google.com * which are at normal priority.
13412961Sgabeblack@google.com *
13512961Sgabeblack@google.com * MAX RUN TIME
13612961Sgabeblack@google.com *
13712961Sgabeblack@google.com * When sc_start is called, it's possible to pass in a maximum time the
13813058Sgabeblack@google.com * simulation should run to, at which point sc_pause is implicitly called. The
13913058Sgabeblack@google.com * simulation is supposed to run up to the latest timed notification phase
14013058Sgabeblack@google.com * which is less than or equal to the maximum time. In other words it should
14113058Sgabeblack@google.com * run timed notifications at the maximum time, but not the subsequent evaluate
14213058Sgabeblack@google.com * phase. That's implemented by scheduling an event at the max time with a
14313058Sgabeblack@google.com * priority which is lower than all the others except the ready event. Timed
14413058Sgabeblack@google.com * notifications will happen before it fires, but it will override any ready
14513058Sgabeblack@google.com * event and prevent the evaluate phase from starting.
14612954Sgabeblack@google.com */
14712953Sgabeblack@google.com
14812953Sgabeblack@google.comclass Scheduler
14912953Sgabeblack@google.com{
15012953Sgabeblack@google.com  public:
15113144Sgabeblack@google.com    typedef std::list<ScEvent *> ScEvents;
15213063Sgabeblack@google.com
15313063Sgabeblack@google.com    class TimeSlot : public ::Event
15413063Sgabeblack@google.com    {
15513063Sgabeblack@google.com      public:
15613063Sgabeblack@google.com        TimeSlot() : ::Event(Default_Pri, AutoDelete) {}
15713063Sgabeblack@google.com
15813063Sgabeblack@google.com        ScEvents events;
15913063Sgabeblack@google.com        void process();
16013063Sgabeblack@google.com    };
16113063Sgabeblack@google.com
16213063Sgabeblack@google.com    typedef std::map<Tick, TimeSlot *> TimeSlots;
16313063Sgabeblack@google.com
16412953Sgabeblack@google.com    Scheduler();
16513072Sgabeblack@google.com    ~Scheduler();
16612953Sgabeblack@google.com
16713076Sgabeblack@google.com    void clear();
16813076Sgabeblack@google.com
16912954Sgabeblack@google.com    const std::string name() const { return "systemc_scheduler"; }
17012954Sgabeblack@google.com
17112953Sgabeblack@google.com    uint64_t numCycles() { return _numCycles; }
17212953Sgabeblack@google.com    Process *current() { return _current; }
17312953Sgabeblack@google.com
17413067Sgabeblack@google.com    void initPhase();
17512953Sgabeblack@google.com
17612957Sgabeblack@google.com    // Register a process with the scheduler.
17712957Sgabeblack@google.com    void reg(Process *p);
17812957Sgabeblack@google.com
17912953Sgabeblack@google.com    // Run the next process, if there is one.
18012953Sgabeblack@google.com    void yield();
18112953Sgabeblack@google.com
18212953Sgabeblack@google.com    // Put a process on the ready list.
18312954Sgabeblack@google.com    void ready(Process *p);
18412954Sgabeblack@google.com
18513133Sgabeblack@google.com    // Mark a process as ready if init is finished, or put it on the list of
18613133Sgabeblack@google.com    // processes to be initialized.
18713133Sgabeblack@google.com    void resume(Process *p);
18813133Sgabeblack@google.com
18913133Sgabeblack@google.com    // Remove a process from the ready/init list if it was on one of them, and
19013133Sgabeblack@google.com    // return if it was.
19113133Sgabeblack@google.com    bool suspend(Process *p);
19213133Sgabeblack@google.com
19312954Sgabeblack@google.com    // Schedule an update for a given channel.
19412954Sgabeblack@google.com    void requestUpdate(Channel *c);
19513901Sgabeblack@google.com    // Same as above, but may be called from a different thread.
19613901Sgabeblack@google.com    void asyncRequestUpdate(Channel *c);
19712953Sgabeblack@google.com
19812953Sgabeblack@google.com    // Run the given process immediately, preempting whatever may be running.
19912953Sgabeblack@google.com    void
20012953Sgabeblack@google.com    runNow(Process *p)
20112953Sgabeblack@google.com    {
20213209Sgabeblack@google.com        // This function may put a process on the wrong list, ie a thread
20313209Sgabeblack@google.com        // the method list. That's fine since that's just a performance
20413209Sgabeblack@google.com        // optimization, and the important thing here is how the processes are
20513209Sgabeblack@google.com        // ordered.
20613176Sgabeblack@google.com
20712953Sgabeblack@google.com        // If a process is running, schedule it/us to run again.
20812953Sgabeblack@google.com        if (_current)
20913209Sgabeblack@google.com            readyListMethods.pushFirst(_current);
21012953Sgabeblack@google.com        // Schedule p to run first.
21113209Sgabeblack@google.com        readyListMethods.pushFirst(p);
21212953Sgabeblack@google.com        yield();
21312953Sgabeblack@google.com    }
21412953Sgabeblack@google.com
21513260Sgabeblack@google.com    // Run this process at the next opportunity.
21613260Sgabeblack@google.com    void
21713260Sgabeblack@google.com    runNext(Process *p)
21813260Sgabeblack@google.com    {
21913260Sgabeblack@google.com        // Like above, it's ok if this isn't a method. Putting it on this list
22013260Sgabeblack@google.com        // just gives it priority.
22113260Sgabeblack@google.com        readyListMethods.pushFirst(p);
22213260Sgabeblack@google.com        if (!inEvaluate())
22313260Sgabeblack@google.com            scheduleReadyEvent();
22413260Sgabeblack@google.com    }
22513260Sgabeblack@google.com
22612954Sgabeblack@google.com    // Set an event queue for scheduling events.
22712954Sgabeblack@google.com    void setEventQueue(EventQueue *_eq) { eq = _eq; }
22812954Sgabeblack@google.com
22912962Sgabeblack@google.com    // Get the current time according to gem5.
23012962Sgabeblack@google.com    Tick getCurTick() { return eq ? eq->getCurTick() : 0; }
23112962Sgabeblack@google.com
23213063Sgabeblack@google.com    Tick
23313063Sgabeblack@google.com    delayed(const ::sc_core::sc_time &delay)
23413063Sgabeblack@google.com    {
23513257Sgabeblack@google.com        return getCurTick() + delay.value();
23613063Sgabeblack@google.com    }
23713063Sgabeblack@google.com
23812962Sgabeblack@google.com    // For scheduling delayed/timed notifications/timeouts.
23912962Sgabeblack@google.com    void
24013063Sgabeblack@google.com    schedule(ScEvent *event, const ::sc_core::sc_time &delay)
24112962Sgabeblack@google.com    {
24213063Sgabeblack@google.com        Tick tick = delayed(delay);
24313125Sgabeblack@google.com        if (tick < getCurTick())
24413125Sgabeblack@google.com            tick = getCurTick();
24513125Sgabeblack@google.com
24613063Sgabeblack@google.com        // Delta notification/timeout.
24713063Sgabeblack@google.com        if (delay.value() == 0) {
24813144Sgabeblack@google.com            event->schedule(deltas, tick);
24913244Sgabeblack@google.com            if (!inEvaluate() && !inUpdate())
25013244Sgabeblack@google.com                scheduleReadyEvent();
25113063Sgabeblack@google.com            return;
25213063Sgabeblack@google.com        }
25313063Sgabeblack@google.com
25413063Sgabeblack@google.com        // Timed notification/timeout.
25513063Sgabeblack@google.com        TimeSlot *&ts = timeSlots[tick];
25613063Sgabeblack@google.com        if (!ts) {
25713063Sgabeblack@google.com            ts = new TimeSlot;
25813069Sgabeblack@google.com            schedule(ts, tick);
25913063Sgabeblack@google.com        }
26013144Sgabeblack@google.com        event->schedule(ts->events, tick);
26112962Sgabeblack@google.com    }
26212962Sgabeblack@google.com
26312962Sgabeblack@google.com    // For descheduling delayed/timed notifications/timeouts.
26412962Sgabeblack@google.com    void
26513063Sgabeblack@google.com    deschedule(ScEvent *event)
26612962Sgabeblack@google.com    {
26713144Sgabeblack@google.com        ScEvents *on = event->scheduledOn();
26813144Sgabeblack@google.com
26913144Sgabeblack@google.com        if (on == &deltas) {
27013144Sgabeblack@google.com            event->deschedule();
27113144Sgabeblack@google.com            return;
27213063Sgabeblack@google.com        }
27312985Sgabeblack@google.com
27413063Sgabeblack@google.com        // Timed notification/timeout.
27513063Sgabeblack@google.com        auto tsit = timeSlots.find(event->when());
27613063Sgabeblack@google.com        panic_if(tsit == timeSlots.end(),
27713063Sgabeblack@google.com                "Descheduling event at time with no events.");
27813063Sgabeblack@google.com        TimeSlot *ts = tsit->second;
27913063Sgabeblack@google.com        ScEvents &events = ts->events;
28013144Sgabeblack@google.com        assert(on == &events);
28113063Sgabeblack@google.com        event->deschedule();
28213063Sgabeblack@google.com
28313063Sgabeblack@google.com        // If no more events are happening at this time slot, get rid of it.
28413063Sgabeblack@google.com        if (events.empty()) {
28513069Sgabeblack@google.com            deschedule(ts);
28613063Sgabeblack@google.com            timeSlots.erase(tsit);
28713063Sgabeblack@google.com        }
28812962Sgabeblack@google.com    }
28912962Sgabeblack@google.com
29012962Sgabeblack@google.com    void
29113063Sgabeblack@google.com    completeTimeSlot(TimeSlot *ts)
29212962Sgabeblack@google.com    {
29313063Sgabeblack@google.com        assert(ts == timeSlots.begin()->second);
29413063Sgabeblack@google.com        timeSlots.erase(timeSlots.begin());
29513096Sgabeblack@google.com        if (!runToTime && starved())
29613096Sgabeblack@google.com            scheduleStarvationEvent();
29713245Sgabeblack@google.com        scheduleTimeAdvancesEvent();
29812962Sgabeblack@google.com    }
29912962Sgabeblack@google.com
30012962Sgabeblack@google.com    // Pending activity ignores gem5 activity, much like how a systemc
30112962Sgabeblack@google.com    // simulation wouldn't know about asynchronous external events (socket IO
30212962Sgabeblack@google.com    // for instance) that might happen before time advances in a pure
30312962Sgabeblack@google.com    // systemc simulation. Also the spec lists what specific types of pending
30412962Sgabeblack@google.com    // activity needs to be counted, which obviously doesn't include gem5
30512962Sgabeblack@google.com    // events.
30612962Sgabeblack@google.com
30712962Sgabeblack@google.com    // Return whether there's pending systemc activity at this time.
30812962Sgabeblack@google.com    bool
30912962Sgabeblack@google.com    pendingCurr()
31012962Sgabeblack@google.com    {
31113176Sgabeblack@google.com        return !readyListMethods.empty() || !readyListThreads.empty() ||
31213176Sgabeblack@google.com            !updateList.empty() || !deltas.empty();
31312962Sgabeblack@google.com    }
31412962Sgabeblack@google.com
31512962Sgabeblack@google.com    // Return whether there are pending timed notifications or timeouts.
31612962Sgabeblack@google.com    bool
31712962Sgabeblack@google.com    pendingFuture()
31812962Sgabeblack@google.com    {
31913063Sgabeblack@google.com        return !timeSlots.empty();
32012962Sgabeblack@google.com    }
32112962Sgabeblack@google.com
32212962Sgabeblack@google.com    // Return how many ticks there are until the first pending event, if any.
32312962Sgabeblack@google.com    Tick
32412962Sgabeblack@google.com    timeToPending()
32512962Sgabeblack@google.com    {
32613063Sgabeblack@google.com        if (pendingCurr())
32712962Sgabeblack@google.com            return 0;
32813063Sgabeblack@google.com        if (pendingFuture())
32913063Sgabeblack@google.com            return timeSlots.begin()->first - getCurTick();
33013063Sgabeblack@google.com        return MaxTick - getCurTick();
33112962Sgabeblack@google.com    }
33212954Sgabeblack@google.com
33312954Sgabeblack@google.com    // Run scheduled channel updates.
33413186Sgabeblack@google.com    void runUpdate();
33513186Sgabeblack@google.com
33613186Sgabeblack@google.com    // Run delta events.
33713186Sgabeblack@google.com    void runDelta();
33812954Sgabeblack@google.com
33912961Sgabeblack@google.com    void start(Tick max_tick, bool run_to_time);
34013061Sgabeblack@google.com    void oneCycle();
34112961Sgabeblack@google.com
34212961Sgabeblack@google.com    void schedulePause();
34312961Sgabeblack@google.com    void scheduleStop(bool finish_delta);
34412961Sgabeblack@google.com
34513186Sgabeblack@google.com    enum Status
34613186Sgabeblack@google.com    {
34713186Sgabeblack@google.com        StatusOther = 0,
34813244Sgabeblack@google.com        StatusEvaluate,
34913244Sgabeblack@google.com        StatusUpdate,
35013186Sgabeblack@google.com        StatusDelta,
35113186Sgabeblack@google.com        StatusTiming,
35213186Sgabeblack@google.com        StatusPaused,
35313186Sgabeblack@google.com        StatusStopped
35413186Sgabeblack@google.com    };
35513186Sgabeblack@google.com
35613203Sgabeblack@google.com    bool elaborationDone() { return _elaborationDone; }
35713203Sgabeblack@google.com    void elaborationDone(bool b) { _elaborationDone = b; }
35813203Sgabeblack@google.com
35913186Sgabeblack@google.com    bool paused() { return status() == StatusPaused; }
36013186Sgabeblack@google.com    bool stopped() { return status() == StatusStopped; }
36113244Sgabeblack@google.com    bool inEvaluate() { return status() == StatusEvaluate; }
36213244Sgabeblack@google.com    bool inUpdate() { return status() == StatusUpdate; }
36313186Sgabeblack@google.com    bool inDelta() { return status() == StatusDelta; }
36413186Sgabeblack@google.com    bool inTiming() { return status() == StatusTiming; }
36512961Sgabeblack@google.com
36613140Sgabeblack@google.com    uint64_t changeStamp() { return _changeStamp; }
36713287Sgabeblack@google.com    void stepChangeStamp() { _changeStamp++; }
36813140Sgabeblack@google.com
36913701Sgabeblack@google.com    // Throw upwards, either to sc_main or to the report handler if sc_main
37013701Sgabeblack@google.com    // isn't running.
37113701Sgabeblack@google.com    void throwUp();
37213182Sgabeblack@google.com
37313186Sgabeblack@google.com    Status status() { return _status; }
37413186Sgabeblack@google.com    void status(Status s) { _status = s; }
37513186Sgabeblack@google.com
37613245Sgabeblack@google.com    void registerTraceFile(TraceFile *tf) { traceFiles.insert(tf); }
37713245Sgabeblack@google.com    void unregisterTraceFile(TraceFile *tf) { traceFiles.erase(tf); }
37813245Sgabeblack@google.com
37912953Sgabeblack@google.com  private:
38012961Sgabeblack@google.com    typedef const EventBase::Priority Priority;
38112961Sgabeblack@google.com    static Priority DefaultPriority = EventBase::Default_Pri;
38212961Sgabeblack@google.com
38312961Sgabeblack@google.com    static Priority StopPriority = DefaultPriority - 1;
38412961Sgabeblack@google.com    static Priority PausePriority = DefaultPriority + 1;
38513058Sgabeblack@google.com    static Priority MaxTickPriority = DefaultPriority + 2;
38613058Sgabeblack@google.com    static Priority ReadyPriority = DefaultPriority + 3;
38712987Sgabeblack@google.com    static Priority StarvationPriority = ReadyPriority;
38813245Sgabeblack@google.com    static Priority TimeAdvancesPriority = EventBase::Maximum_Pri;
38912961Sgabeblack@google.com
39012954Sgabeblack@google.com    EventQueue *eq;
39113063Sgabeblack@google.com
39213069Sgabeblack@google.com    // For gem5 style events.
39313069Sgabeblack@google.com    void
39413069Sgabeblack@google.com    schedule(::Event *event, Tick tick)
39513069Sgabeblack@google.com    {
39613069Sgabeblack@google.com        if (initDone)
39713069Sgabeblack@google.com            eq->schedule(event, tick);
39813069Sgabeblack@google.com        else
39913069Sgabeblack@google.com            eventsToSchedule[event] = tick;
40013069Sgabeblack@google.com    }
40113069Sgabeblack@google.com
40213069Sgabeblack@google.com    void schedule(::Event *event) { schedule(event, getCurTick()); }
40313069Sgabeblack@google.com
40413069Sgabeblack@google.com    void
40513069Sgabeblack@google.com    deschedule(::Event *event)
40613069Sgabeblack@google.com    {
40713069Sgabeblack@google.com        if (initDone)
40813069Sgabeblack@google.com            eq->deschedule(event);
40913069Sgabeblack@google.com        else
41013069Sgabeblack@google.com            eventsToSchedule.erase(event);
41113069Sgabeblack@google.com    }
41213069Sgabeblack@google.com
41313063Sgabeblack@google.com    ScEvents deltas;
41413063Sgabeblack@google.com    TimeSlots timeSlots;
41512954Sgabeblack@google.com
41613209Sgabeblack@google.com    Process *
41713209Sgabeblack@google.com    getNextReady()
41813209Sgabeblack@google.com    {
41913209Sgabeblack@google.com        Process *p = readyListMethods.getNext();
42013209Sgabeblack@google.com        return p ? p : readyListThreads.getNext();
42113209Sgabeblack@google.com    }
42213209Sgabeblack@google.com
42312954Sgabeblack@google.com    void runReady();
42412954Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::runReady> readyEvent;
42512954Sgabeblack@google.com    void scheduleReadyEvent();
42612954Sgabeblack@google.com
42712961Sgabeblack@google.com    void pause();
42812961Sgabeblack@google.com    void stop();
42912961Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::pause> pauseEvent;
43012961Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::stop> stopEvent;
43113182Sgabeblack@google.com
43213701Sgabeblack@google.com    const ::sc_core::sc_report *_throwUp;
43312961Sgabeblack@google.com
43412987Sgabeblack@google.com    bool
43512987Sgabeblack@google.com    starved()
43612987Sgabeblack@google.com    {
43713176Sgabeblack@google.com        return (readyListMethods.empty() && readyListThreads.empty() &&
43813176Sgabeblack@google.com                updateList.empty() && deltas.empty() &&
43913063Sgabeblack@google.com                (timeSlots.empty() || timeSlots.begin()->first > maxTick) &&
44012987Sgabeblack@google.com                initList.empty());
44112987Sgabeblack@google.com    }
44212987Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::pause> starvationEvent;
44312987Sgabeblack@google.com    void scheduleStarvationEvent();
44412987Sgabeblack@google.com
44513203Sgabeblack@google.com    bool _elaborationDone;
44612961Sgabeblack@google.com    bool _started;
44713154Sgabeblack@google.com    bool _stopNow;
44812961Sgabeblack@google.com
44913186Sgabeblack@google.com    Status _status;
45013186Sgabeblack@google.com
45112961Sgabeblack@google.com    Tick maxTick;
45213140Sgabeblack@google.com    Tick lastReadyTick;
45313140Sgabeblack@google.com    void
45413140Sgabeblack@google.com    maxTickFunc()
45513140Sgabeblack@google.com    {
45613140Sgabeblack@google.com        if (lastReadyTick != getCurTick())
45713140Sgabeblack@google.com            _changeStamp++;
45813140Sgabeblack@google.com        pause();
45913140Sgabeblack@google.com    }
46013140Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::maxTickFunc> maxTickEvent;
46112961Sgabeblack@google.com
46213245Sgabeblack@google.com    void timeAdvances() { trace(false); }
46313245Sgabeblack@google.com    EventWrapper<Scheduler, &Scheduler::timeAdvances> timeAdvancesEvent;
46413245Sgabeblack@google.com    void
46513245Sgabeblack@google.com    scheduleTimeAdvancesEvent()
46613245Sgabeblack@google.com    {
46713245Sgabeblack@google.com        if (!traceFiles.empty() && !timeAdvancesEvent.scheduled())
46813245Sgabeblack@google.com            schedule(&timeAdvancesEvent);
46913245Sgabeblack@google.com    }
47013245Sgabeblack@google.com
47112953Sgabeblack@google.com    uint64_t _numCycles;
47213140Sgabeblack@google.com    uint64_t _changeStamp;
47312953Sgabeblack@google.com
47412953Sgabeblack@google.com    Process *_current;
47512953Sgabeblack@google.com
47613067Sgabeblack@google.com    bool initDone;
47712987Sgabeblack@google.com    bool runToTime;
47813061Sgabeblack@google.com    bool runOnce;
47912957Sgabeblack@google.com
48012953Sgabeblack@google.com    ProcessList initList;
48113176Sgabeblack@google.com
48213176Sgabeblack@google.com    ProcessList readyListMethods;
48313176Sgabeblack@google.com    ProcessList readyListThreads;
48412953Sgabeblack@google.com
48512954Sgabeblack@google.com    ChannelList updateList;
48612985Sgabeblack@google.com
48713901Sgabeblack@google.com    ChannelList asyncUpdateList;
48813901Sgabeblack@google.com    std::mutex asyncListMutex;
48913901Sgabeblack@google.com
49012985Sgabeblack@google.com    std::map<::Event *, Tick> eventsToSchedule;
49113245Sgabeblack@google.com
49213245Sgabeblack@google.com    std::set<TraceFile *> traceFiles;
49313245Sgabeblack@google.com
49413245Sgabeblack@google.com    void trace(bool delta);
49512953Sgabeblack@google.com};
49612953Sgabeblack@google.com
49712953Sgabeblack@google.comextern Scheduler scheduler;
49812953Sgabeblack@google.com
49913329Sgabeblack@google.com// A proxy function to avoid having to expose the scheduler in header files.
50013329Sgabeblack@google.comProcess *getCurrentProcess();
50113329Sgabeblack@google.com
50213063Sgabeblack@google.cominline void
50313063Sgabeblack@google.comScheduler::TimeSlot::process()
50413063Sgabeblack@google.com{
50513287Sgabeblack@google.com    scheduler.stepChangeStamp();
50613186Sgabeblack@google.com    scheduler.status(StatusTiming);
50713186Sgabeblack@google.com
50813188Sgabeblack@google.com    try {
50913188Sgabeblack@google.com        while (!events.empty())
51013188Sgabeblack@google.com            events.front()->run();
51113188Sgabeblack@google.com    } catch (...) {
51213188Sgabeblack@google.com        if (events.empty())
51313188Sgabeblack@google.com            scheduler.completeTimeSlot(this);
51413188Sgabeblack@google.com        else
51513188Sgabeblack@google.com            scheduler.schedule(this);
51613701Sgabeblack@google.com        scheduler.throwUp();
51713188Sgabeblack@google.com    }
51813186Sgabeblack@google.com
51913186Sgabeblack@google.com    scheduler.status(StatusOther);
52013063Sgabeblack@google.com    scheduler.completeTimeSlot(this);
52113063Sgabeblack@google.com}
52213063Sgabeblack@google.com
52313264Sgabeblack@google.comconst ::sc_core::sc_report reportifyException();
52413182Sgabeblack@google.com
52512953Sgabeblack@google.com} // namespace sc_gem5
52612953Sgabeblack@google.com
52712953Sgabeblack@google.com#endif // __SYSTEMC_CORE_SCHEDULER_H__
528