scheduler.hh revision 12954
11850Sstever@eecs.umich.edu/* 2131Sstever@eecs.umich.edu * Copyright 2018 Google, Inc. 3131Sstever@eecs.umich.edu * 4131Sstever@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 5131Sstever@eecs.umich.edu * modification, are permitted provided that the following conditions are 6131Sstever@eecs.umich.edu * met: redistributions of source code must retain the above copyright 7131Sstever@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 8131Sstever@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 9131Sstever@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 10131Sstever@eecs.umich.edu * documentation and/or other materials provided with the distribution; 11131Sstever@eecs.umich.edu * neither the name of the copyright holders nor the names of its 12131Sstever@eecs.umich.edu * contributors may be used to endorse or promote products derived from 13131Sstever@eecs.umich.edu * this software without specific prior written permission. 14131Sstever@eecs.umich.edu * 151850Sstever@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16131Sstever@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 171028Ssaidi@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 181028Ssaidi@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 191028Ssaidi@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 201028Ssaidi@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 211028Ssaidi@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 221028Ssaidi@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 231028Ssaidi@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 241028Ssaidi@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 251901Sstever@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 261850Sstever@eecs.umich.edu * 271901Sstever@eecs.umich.edu * Authors: Gabe Black 281028Ssaidi@eecs.umich.edu */ 291028Ssaidi@eecs.umich.edu 301028Ssaidi@eecs.umich.edu#ifndef __SYSTEMC_CORE_SCHEDULER_HH__ 311850Sstever@eecs.umich.edu#define __SYSTEMC_CORE_SCHEDULER_HH__ 321850Sstever@eecs.umich.edu 33131Sstever@eecs.umich.edu#include "sim/eventq.hh" 34131Sstever@eecs.umich.edu#include "systemc/core/channel.hh" 35131Sstever@eecs.umich.edu#include "systemc/core/list.hh" 36131Sstever@eecs.umich.edu#include "systemc/core/process.hh" 371850Sstever@eecs.umich.edu 38131Sstever@eecs.umich.edunamespace sc_gem5 391028Ssaidi@eecs.umich.edu{ 401028Ssaidi@eecs.umich.edu 411704Sstever@eecs.umich.edutypedef NodeList<Process> ProcessList; 421850Sstever@eecs.umich.edutypedef NodeList<Channel> ChannelList; 431028Ssaidi@eecs.umich.edu 441028Ssaidi@eecs.umich.edu/* 451028Ssaidi@eecs.umich.edu * The scheduler supports three different mechanisms, the initialization phase, 461028Ssaidi@eecs.umich.edu * delta cycles, and timed notifications. 471028Ssaidi@eecs.umich.edu * 481028Ssaidi@eecs.umich.edu * INITIALIZATION PHASE 491028Ssaidi@eecs.umich.edu * 501850Sstever@eecs.umich.edu * The initialization phase has three parts: 511850Sstever@eecs.umich.edu * 1. Run requested channel updates. 521850Sstever@eecs.umich.edu * 2. Make processes which need to initialize runnable (methods and threads 531850Sstever@eecs.umich.edu * which didn't have dont_initialize called on them). 541850Sstever@eecs.umich.edu * 3. Process delta notifications. 551850Sstever@eecs.umich.edu * 561850Sstever@eecs.umich.edu * First, the Kernel SimObject calls the update() method during its startup() 571850Sstever@eecs.umich.edu * callback which handles the requested channel updates. The Kernel also 581850Sstever@eecs.umich.edu * schedules an event to be run at time 0 with a slightly elevated priority 591850Sstever@eecs.umich.edu * so that it happens before any "normal" event. 601850Sstever@eecs.umich.edu * 611850Sstever@eecs.umich.edu * When that t0 event happens, it calls the schedulers initToReady method 621850Sstever@eecs.umich.edu * which performs step 2 above. That indirectly causes the scheduler's 631850Sstever@eecs.umich.edu * readyEvent to be scheduled with slightly lowered priority, ensuring it 641869Sstever@eecs.umich.edu * happens after any "normal" event. 651869Sstever@eecs.umich.edu * 661850Sstever@eecs.umich.edu * Because delta notifications are scheduled at the standard priority, all 671850Sstever@eecs.umich.edu * of those events will happen next, performing step 3 above. Once they finish, 681850Sstever@eecs.umich.edu * if the readyEvent was scheduled above, there shouldn't be any higher 691901Sstever@eecs.umich.edu * priority events in front of it. When it runs, it will start the first 701901Sstever@eecs.umich.edu * evaluate phase of the first delta cycle. 711901Sstever@eecs.umich.edu * 721901Sstever@eecs.umich.edu * DELTA CYCLE 731901Sstever@eecs.umich.edu * 741850Sstever@eecs.umich.edu * A delta cycle has three phases within it. 751850Sstever@eecs.umich.edu * 1. The evaluate phase where runnable processes are allowed to run. 761869Sstever@eecs.umich.edu * 2. The update phase where requested channel updates hapen. 771850Sstever@eecs.umich.edu * 3. The delta notification phase where delta notifications happen. 78 * 79 * The readyEvent runs the first two steps of the delta cycle. It first goes 80 * through the list of runnable processes and executes them until the set is 81 * empty, and then immediately runs the update phase. Since these are all part 82 * of the same event, there's no chance for other events to intervene and 83 * break the required order above. 84 * 85 * During the update phase above, the spec forbids any action which would make 86 * a process runnable. That means that once the update phase finishes, the set 87 * of runnable processes will be empty. There may, however, have been some 88 * delta notifications/timeouts which will have been scheduled during either 89 * the evaluate or update phase above. Because those are scheduled at the 90 * normal priority, they will now happen together until there aren't any 91 * delta events left. 92 * 93 * If any processes became runnable during the delta notification phase, the 94 * readyEvent will have been scheduled and will have been waiting patiently 95 * behind the delta notification events. That will now run, effectively 96 * starting the next delta cycle. 97 * 98 * TIMED NOTIFICATION PHASE 99 * 100 * If no processes became runnable, the event queue will continue to process 101 * events until it comes across a timed notification, aka a notification 102 * scheduled to happen in the future. Like delta notification events, those 103 * will all happen together since the readyEvent priority is lower, 104 * potentially marking new processes as ready. Once these events finish, the 105 * readyEvent may run, starting the next delta cycle. 106 */ 107 108class Scheduler 109{ 110 public: 111 Scheduler(); 112 113 const std::string name() const { return "systemc_scheduler"; } 114 115 uint64_t numCycles() { return _numCycles; } 116 Process *current() { return _current; } 117 118 // Mark processes that need to be initialized as ready. 119 void initToReady(); 120 121 // Put a process on the list of processes to be initialized. 122 void init(Process *p) { initList.pushLast(p); } 123 124 // Run the next process, if there is one. 125 void yield(); 126 127 // Put a process on the ready list. 128 void ready(Process *p); 129 130 // Schedule an update for a given channel. 131 void requestUpdate(Channel *c); 132 133 // Run the given process immediately, preempting whatever may be running. 134 void 135 runNow(Process *p) 136 { 137 // If a process is running, schedule it/us to run again. 138 if (_current) 139 readyList.pushFirst(_current); 140 // Schedule p to run first. 141 readyList.pushFirst(p); 142 yield(); 143 } 144 145 // Set an event queue for scheduling events. 146 void setEventQueue(EventQueue *_eq) { eq = _eq; } 147 148 // Retrieve the event queue. 149 EventQueue &eventQueue() const { return *eq; } 150 151 // Run scheduled channel updates. 152 void update(); 153 154 private: 155 EventQueue *eq; 156 157 void runReady(); 158 EventWrapper<Scheduler, &Scheduler::runReady> readyEvent; 159 void scheduleReadyEvent(); 160 161 uint64_t _numCycles; 162 163 Process *_current; 164 165 ProcessList initList; 166 ProcessList readyList; 167 168 ChannelList updateList; 169}; 170 171extern Scheduler scheduler; 172 173} // namespace sc_gem5 174 175#endif // __SYSTEMC_CORE_SCHEDULER_H__ 176