scheduler.cc (13403:cebee63981d3) scheduler.cc (13488:2e12afaa6cc7)
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/scheduler.hh"
31
32#include "base/fiber.hh"
33#include "base/logging.hh"
34#include "sim/eventq.hh"
35#include "systemc/core/kernel.hh"
36#include "systemc/core/sc_main_fiber.hh"
37#include "systemc/ext/core/messages.hh"
38#include "systemc/ext/core/sc_main.hh"
39#include "systemc/ext/utils/sc_report.hh"
40#include "systemc/ext/utils/sc_report_handler.hh"
41#include "systemc/utils/report.hh"
42#include "systemc/utils/tracefile.hh"
43
44namespace sc_gem5
45{
46
47Scheduler::Scheduler() :
48 eq(nullptr), readyEvent(this, false, ReadyPriority),
49 pauseEvent(this, false, PausePriority),
50 stopEvent(this, false, StopPriority), _throwToScMain(nullptr),
51 starvationEvent(this, false, StarvationPriority),
52 _elaborationDone(false), _started(false), _stopNow(false),
53 _status(StatusOther), maxTickEvent(this, false, MaxTickPriority),
54 timeAdvancesEvent(this, false, TimeAdvancesPriority), _numCycles(0),
55 _changeStamp(0), _current(nullptr), initDone(false), runOnce(false)
56{}
57
58Scheduler::~Scheduler()
59{
60 // Clear out everything that belongs to us to make sure nobody tries to
61 // clear themselves out after the scheduler goes away.
62 clear();
63}
64
65void
66Scheduler::clear()
67{
68 // Delta notifications.
69 while (!deltas.empty())
70 deltas.front()->deschedule();
71
72 // Timed notifications.
73 for (auto &tsp: timeSlots) {
74 TimeSlot *&ts = tsp.second;
75 while (!ts->events.empty())
76 ts->events.front()->deschedule();
77 deschedule(ts);
78 }
79 timeSlots.clear();
80
81 // gem5 events.
82 if (readyEvent.scheduled())
83 deschedule(&readyEvent);
84 if (pauseEvent.scheduled())
85 deschedule(&pauseEvent);
86 if (stopEvent.scheduled())
87 deschedule(&stopEvent);
88 if (starvationEvent.scheduled())
89 deschedule(&starvationEvent);
90 if (maxTickEvent.scheduled())
91 deschedule(&maxTickEvent);
92 if (timeAdvancesEvent.scheduled())
93 deschedule(&timeAdvancesEvent);
94
95 Process *p;
96 while ((p = initList.getNext()))
97 p->popListNode();
98 while ((p = readyListMethods.getNext()))
99 p->popListNode();
100 while ((p = readyListThreads.getNext()))
101 p->popListNode();
102
103 Channel *c;
104 while ((c = updateList.getNext()))
105 c->popListNode();
106}
107
108void
109Scheduler::initPhase()
110{
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#include "systemc/core/scheduler.hh"
31
32#include "base/fiber.hh"
33#include "base/logging.hh"
34#include "sim/eventq.hh"
35#include "systemc/core/kernel.hh"
36#include "systemc/core/sc_main_fiber.hh"
37#include "systemc/ext/core/messages.hh"
38#include "systemc/ext/core/sc_main.hh"
39#include "systemc/ext/utils/sc_report.hh"
40#include "systemc/ext/utils/sc_report_handler.hh"
41#include "systemc/utils/report.hh"
42#include "systemc/utils/tracefile.hh"
43
44namespace sc_gem5
45{
46
47Scheduler::Scheduler() :
48 eq(nullptr), readyEvent(this, false, ReadyPriority),
49 pauseEvent(this, false, PausePriority),
50 stopEvent(this, false, StopPriority), _throwToScMain(nullptr),
51 starvationEvent(this, false, StarvationPriority),
52 _elaborationDone(false), _started(false), _stopNow(false),
53 _status(StatusOther), maxTickEvent(this, false, MaxTickPriority),
54 timeAdvancesEvent(this, false, TimeAdvancesPriority), _numCycles(0),
55 _changeStamp(0), _current(nullptr), initDone(false), runOnce(false)
56{}
57
58Scheduler::~Scheduler()
59{
60 // Clear out everything that belongs to us to make sure nobody tries to
61 // clear themselves out after the scheduler goes away.
62 clear();
63}
64
65void
66Scheduler::clear()
67{
68 // Delta notifications.
69 while (!deltas.empty())
70 deltas.front()->deschedule();
71
72 // Timed notifications.
73 for (auto &tsp: timeSlots) {
74 TimeSlot *&ts = tsp.second;
75 while (!ts->events.empty())
76 ts->events.front()->deschedule();
77 deschedule(ts);
78 }
79 timeSlots.clear();
80
81 // gem5 events.
82 if (readyEvent.scheduled())
83 deschedule(&readyEvent);
84 if (pauseEvent.scheduled())
85 deschedule(&pauseEvent);
86 if (stopEvent.scheduled())
87 deschedule(&stopEvent);
88 if (starvationEvent.scheduled())
89 deschedule(&starvationEvent);
90 if (maxTickEvent.scheduled())
91 deschedule(&maxTickEvent);
92 if (timeAdvancesEvent.scheduled())
93 deschedule(&timeAdvancesEvent);
94
95 Process *p;
96 while ((p = initList.getNext()))
97 p->popListNode();
98 while ((p = readyListMethods.getNext()))
99 p->popListNode();
100 while ((p = readyListThreads.getNext()))
101 p->popListNode();
102
103 Channel *c;
104 while ((c = updateList.getNext()))
105 c->popListNode();
106}
107
108void
109Scheduler::initPhase()
110{
111 runUpdate();
112
111 for (Process *p = initList.getNext(); p; p = initList.getNext()) {
112 p->popListNode();
113
114 if (p->dontInitialize()) {
115 if (!p->hasStaticSensitivities() && !p->internal()) {
116 SC_REPORT_WARNING(sc_core::SC_ID_DISABLE_WILL_ORPHAN_PROCESS_,
117 p->name());
118 }
119 } else {
120 p->ready();
121 }
122 }
123
113 for (Process *p = initList.getNext(); p; p = initList.getNext()) {
114 p->popListNode();
115
116 if (p->dontInitialize()) {
117 if (!p->hasStaticSensitivities() && !p->internal()) {
118 SC_REPORT_WARNING(sc_core::SC_ID_DISABLE_WILL_ORPHAN_PROCESS_,
119 p->name());
120 }
121 } else {
122 p->ready();
123 }
124 }
125
124 runUpdate();
125 runDelta();
126
127 for (auto ets: eventsToSchedule)
128 eq->schedule(ets.first, ets.second);
129 eventsToSchedule.clear();
130
131 if (_started) {
132 if (!runToTime && starved())
133 scheduleStarvationEvent();
134 kernel->status(::sc_core::SC_RUNNING);
135 }
136
137 initDone = true;
138
139 status(StatusOther);
140
141 scheduleTimeAdvancesEvent();
142}
143
144void
145Scheduler::reg(Process *p)
146{
147 if (initDone) {
148 // If not marked as dontInitialize, mark as ready.
149 if (!p->dontInitialize())
150 p->ready();
151 } else {
152 // Otherwise, record that this process should be initialized once we
153 // get there.
154 initList.pushLast(p);
155 }
156}
157
158void
159Scheduler::yield()
160{
161 // Pull a process from the active list.
162 _current = getNextReady();
163 if (!_current) {
164 // There are no more processes, so return control to evaluate.
165 Fiber::primaryFiber()->run();
166 } else {
167 _current->popListNode();
168 _current->scheduled(false);
169 // Switch to whatever Fiber is supposed to run this process. All
170 // Fibers which aren't running should be parked at this line.
171 _current->fiber()->run();
172 // If the current process needs to be manually started, start it.
173 if (_current && _current->needsStart()) {
174 _current->needsStart(false);
175 // If a process hasn't started yet, "resetting" it just starts it
176 // and signals its reset event.
177 if (_current->inReset())
178 _current->resetEvent().notify();
179 try {
180 _current->run();
181 } catch (...) {
182 throwToScMain();
183 }
184 }
185 }
186 if (_current && !_current->needsStart()) {
187 if (_current->excWrapper) {
188 auto ew = _current->excWrapper;
189 _current->excWrapper = nullptr;
190 ew->throw_it();
191 } else if (_current->inReset()) {
192 _current->reset(false);
193 }
194 }
195}
196
197void
198Scheduler::ready(Process *p)
199{
200 if (_stopNow)
201 return;
202
203 p->scheduled(true);
204
205 if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
206 readyListMethods.pushLast(p);
207 else
208 readyListThreads.pushLast(p);
209
210 if (!inEvaluate())
211 scheduleReadyEvent();
212}
213
214void
215Scheduler::resume(Process *p)
216{
217 if (initDone)
218 ready(p);
219 else
220 initList.pushLast(p);
221}
222
223bool
224listContains(ListNode *list, ListNode *target)
225{
226 ListNode *n = list->nextListNode;
227 while (n != list)
228 if (n == target)
229 return true;
230 return false;
231}
232
233bool
234Scheduler::suspend(Process *p)
235{
236 bool was_ready;
237 if (initDone) {
238 // After initialization, check if we're on a ready list.
239 was_ready = (p->nextListNode != nullptr);
240 p->popListNode();
241 } else {
242 // Nothing is ready before init.
243 was_ready = false;
244 }
245 return was_ready;
246}
247
248void
249Scheduler::requestUpdate(Channel *c)
250{
251 updateList.pushLast(c);
252 if (!inEvaluate())
253 scheduleReadyEvent();
254}
255
256void
257Scheduler::scheduleReadyEvent()
258{
259 // Schedule the evaluate and update phases.
260 if (!readyEvent.scheduled()) {
261 schedule(&readyEvent);
262 if (starvationEvent.scheduled())
263 deschedule(&starvationEvent);
264 }
265}
266
267void
268Scheduler::scheduleStarvationEvent()
269{
270 if (!starvationEvent.scheduled()) {
271 schedule(&starvationEvent);
272 if (readyEvent.scheduled())
273 deschedule(&readyEvent);
274 }
275}
276
277void
278Scheduler::runReady()
279{
280 scheduleTimeAdvancesEvent();
281
282 bool empty = readyListMethods.empty() && readyListThreads.empty();
283 lastReadyTick = getCurTick();
284
285 // The evaluation phase.
286 status(StatusEvaluate);
287 do {
288 yield();
289 } while (getNextReady());
290 _current = nullptr;
291
292 if (!empty) {
293 _numCycles++;
294 _changeStamp++;
295 }
296
297 if (_stopNow) {
298 status(StatusOther);
299 return;
300 }
301
302 runUpdate();
303 if (!traceFiles.empty())
304 trace(true);
305 runDelta();
306
307 if (!runToTime && starved())
308 scheduleStarvationEvent();
309
310 if (runOnce)
311 schedulePause();
312
313 status(StatusOther);
314}
315
316void
317Scheduler::runUpdate()
318{
319 status(StatusUpdate);
320
321 try {
322 Channel *channel = updateList.getNext();
323 while (channel) {
324 channel->popListNode();
325 channel->update();
326 channel = updateList.getNext();
327 }
328 } catch (...) {
329 throwToScMain();
330 }
331}
332
333void
334Scheduler::runDelta()
335{
336 status(StatusDelta);
337
338 try {
339 while (!deltas.empty())
340 deltas.back()->run();
341 } catch (...) {
342 throwToScMain();
343 }
344}
345
346void
347Scheduler::pause()
348{
349 status(StatusPaused);
350 kernel->status(::sc_core::SC_PAUSED);
351 runOnce = false;
352 if (scMainFiber.called() && !scMainFiber.finished())
353 scMainFiber.run();
354}
355
356void
357Scheduler::stop()
358{
359 status(StatusStopped);
360 kernel->stop();
361
362 clear();
363
364 runOnce = false;
365 if (scMainFiber.called() && !scMainFiber.finished())
366 scMainFiber.run();
367}
368
369void
370Scheduler::start(Tick max_tick, bool run_to_time)
371{
372 _started = true;
373 status(StatusOther);
374 runToTime = run_to_time;
375
376 maxTick = max_tick;
377 lastReadyTick = getCurTick();
378
379 if (initDone) {
380 if (!runToTime && starved())
381 scheduleStarvationEvent();
382 kernel->status(::sc_core::SC_RUNNING);
383 }
384
385 schedule(&maxTickEvent, maxTick);
386 scheduleTimeAdvancesEvent();
387
388 // Return to gem5 to let it run events, etc.
389 Fiber::primaryFiber()->run();
390
391 if (pauseEvent.scheduled())
392 deschedule(&pauseEvent);
393 if (stopEvent.scheduled())
394 deschedule(&stopEvent);
395 if (maxTickEvent.scheduled())
396 deschedule(&maxTickEvent);
397 if (starvationEvent.scheduled())
398 deschedule(&starvationEvent);
399
400 if (_throwToScMain) {
401 const ::sc_core::sc_report *to_throw = _throwToScMain;
402 _throwToScMain = nullptr;
403 throw *to_throw;
404 }
405}
406
407void
408Scheduler::oneCycle()
409{
410 runOnce = true;
411 scheduleReadyEvent();
412 start(::MaxTick, false);
413}
414
415void
416Scheduler::schedulePause()
417{
418 if (pauseEvent.scheduled())
419 return;
420
421 schedule(&pauseEvent);
422}
423
424void
425Scheduler::throwToScMain()
426{
427 ::sc_core::sc_report report = reportifyException();
428 _throwToScMain = &report;
429 status(StatusOther);
430 scMainFiber.run();
431}
432
433void
434Scheduler::scheduleStop(bool finish_delta)
435{
436 if (stopEvent.scheduled())
437 return;
438
439 if (!finish_delta) {
440 _stopNow = true;
441 // If we're not supposed to finish the delta cycle, flush all
442 // pending activity.
443 clear();
444 }
445 schedule(&stopEvent);
446}
447
448void
449Scheduler::trace(bool delta)
450{
451 for (auto tf: traceFiles)
452 tf->trace(delta);
453}
454
455Scheduler scheduler;
456Process *getCurrentProcess() { return scheduler.current(); }
457
458namespace {
459
460void
461throwingReportHandler(const ::sc_core::sc_report &r,
462 const ::sc_core::sc_actions &)
463{
464 throw r;
465}
466
467} // anonymous namespace
468
469const ::sc_core::sc_report
470reportifyException()
471{
472 ::sc_core::sc_report_handler_proc old_handler = reportHandlerProc;
473 ::sc_core::sc_report_handler::set_handler(&throwingReportHandler);
474
475 try {
476 try {
477 // Rethrow the current exception so we can catch it and throw an
478 // sc_report instead if it's not a type we recognize/can handle.
479 throw;
480 } catch (const ::sc_core::sc_report &) {
481 // It's already a sc_report, so nothing to do.
482 throw;
483 } catch (const ::sc_core::sc_unwind_exception &) {
484 panic("Kill/reset exception escaped a Process::run()");
485 } catch (const std::exception &e) {
486 SC_REPORT_ERROR(
487 sc_core::SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_, e.what());
488 } catch (const char *msg) {
489 SC_REPORT_ERROR(
490 sc_core::SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_, msg);
491 } catch (...) {
492 SC_REPORT_ERROR(
493 sc_core::SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_,
494 "UNKNOWN EXCEPTION");
495 }
496 } catch (const ::sc_core::sc_report &r) {
497 ::sc_core::sc_report_handler::set_handler(old_handler);
498 return r;
499 }
500 panic("No exception thrown in reportifyException.");
501}
502
503} // namespace sc_gem5
126 runDelta();
127
128 for (auto ets: eventsToSchedule)
129 eq->schedule(ets.first, ets.second);
130 eventsToSchedule.clear();
131
132 if (_started) {
133 if (!runToTime && starved())
134 scheduleStarvationEvent();
135 kernel->status(::sc_core::SC_RUNNING);
136 }
137
138 initDone = true;
139
140 status(StatusOther);
141
142 scheduleTimeAdvancesEvent();
143}
144
145void
146Scheduler::reg(Process *p)
147{
148 if (initDone) {
149 // If not marked as dontInitialize, mark as ready.
150 if (!p->dontInitialize())
151 p->ready();
152 } else {
153 // Otherwise, record that this process should be initialized once we
154 // get there.
155 initList.pushLast(p);
156 }
157}
158
159void
160Scheduler::yield()
161{
162 // Pull a process from the active list.
163 _current = getNextReady();
164 if (!_current) {
165 // There are no more processes, so return control to evaluate.
166 Fiber::primaryFiber()->run();
167 } else {
168 _current->popListNode();
169 _current->scheduled(false);
170 // Switch to whatever Fiber is supposed to run this process. All
171 // Fibers which aren't running should be parked at this line.
172 _current->fiber()->run();
173 // If the current process needs to be manually started, start it.
174 if (_current && _current->needsStart()) {
175 _current->needsStart(false);
176 // If a process hasn't started yet, "resetting" it just starts it
177 // and signals its reset event.
178 if (_current->inReset())
179 _current->resetEvent().notify();
180 try {
181 _current->run();
182 } catch (...) {
183 throwToScMain();
184 }
185 }
186 }
187 if (_current && !_current->needsStart()) {
188 if (_current->excWrapper) {
189 auto ew = _current->excWrapper;
190 _current->excWrapper = nullptr;
191 ew->throw_it();
192 } else if (_current->inReset()) {
193 _current->reset(false);
194 }
195 }
196}
197
198void
199Scheduler::ready(Process *p)
200{
201 if (_stopNow)
202 return;
203
204 p->scheduled(true);
205
206 if (p->procKind() == ::sc_core::SC_METHOD_PROC_)
207 readyListMethods.pushLast(p);
208 else
209 readyListThreads.pushLast(p);
210
211 if (!inEvaluate())
212 scheduleReadyEvent();
213}
214
215void
216Scheduler::resume(Process *p)
217{
218 if (initDone)
219 ready(p);
220 else
221 initList.pushLast(p);
222}
223
224bool
225listContains(ListNode *list, ListNode *target)
226{
227 ListNode *n = list->nextListNode;
228 while (n != list)
229 if (n == target)
230 return true;
231 return false;
232}
233
234bool
235Scheduler::suspend(Process *p)
236{
237 bool was_ready;
238 if (initDone) {
239 // After initialization, check if we're on a ready list.
240 was_ready = (p->nextListNode != nullptr);
241 p->popListNode();
242 } else {
243 // Nothing is ready before init.
244 was_ready = false;
245 }
246 return was_ready;
247}
248
249void
250Scheduler::requestUpdate(Channel *c)
251{
252 updateList.pushLast(c);
253 if (!inEvaluate())
254 scheduleReadyEvent();
255}
256
257void
258Scheduler::scheduleReadyEvent()
259{
260 // Schedule the evaluate and update phases.
261 if (!readyEvent.scheduled()) {
262 schedule(&readyEvent);
263 if (starvationEvent.scheduled())
264 deschedule(&starvationEvent);
265 }
266}
267
268void
269Scheduler::scheduleStarvationEvent()
270{
271 if (!starvationEvent.scheduled()) {
272 schedule(&starvationEvent);
273 if (readyEvent.scheduled())
274 deschedule(&readyEvent);
275 }
276}
277
278void
279Scheduler::runReady()
280{
281 scheduleTimeAdvancesEvent();
282
283 bool empty = readyListMethods.empty() && readyListThreads.empty();
284 lastReadyTick = getCurTick();
285
286 // The evaluation phase.
287 status(StatusEvaluate);
288 do {
289 yield();
290 } while (getNextReady());
291 _current = nullptr;
292
293 if (!empty) {
294 _numCycles++;
295 _changeStamp++;
296 }
297
298 if (_stopNow) {
299 status(StatusOther);
300 return;
301 }
302
303 runUpdate();
304 if (!traceFiles.empty())
305 trace(true);
306 runDelta();
307
308 if (!runToTime && starved())
309 scheduleStarvationEvent();
310
311 if (runOnce)
312 schedulePause();
313
314 status(StatusOther);
315}
316
317void
318Scheduler::runUpdate()
319{
320 status(StatusUpdate);
321
322 try {
323 Channel *channel = updateList.getNext();
324 while (channel) {
325 channel->popListNode();
326 channel->update();
327 channel = updateList.getNext();
328 }
329 } catch (...) {
330 throwToScMain();
331 }
332}
333
334void
335Scheduler::runDelta()
336{
337 status(StatusDelta);
338
339 try {
340 while (!deltas.empty())
341 deltas.back()->run();
342 } catch (...) {
343 throwToScMain();
344 }
345}
346
347void
348Scheduler::pause()
349{
350 status(StatusPaused);
351 kernel->status(::sc_core::SC_PAUSED);
352 runOnce = false;
353 if (scMainFiber.called() && !scMainFiber.finished())
354 scMainFiber.run();
355}
356
357void
358Scheduler::stop()
359{
360 status(StatusStopped);
361 kernel->stop();
362
363 clear();
364
365 runOnce = false;
366 if (scMainFiber.called() && !scMainFiber.finished())
367 scMainFiber.run();
368}
369
370void
371Scheduler::start(Tick max_tick, bool run_to_time)
372{
373 _started = true;
374 status(StatusOther);
375 runToTime = run_to_time;
376
377 maxTick = max_tick;
378 lastReadyTick = getCurTick();
379
380 if (initDone) {
381 if (!runToTime && starved())
382 scheduleStarvationEvent();
383 kernel->status(::sc_core::SC_RUNNING);
384 }
385
386 schedule(&maxTickEvent, maxTick);
387 scheduleTimeAdvancesEvent();
388
389 // Return to gem5 to let it run events, etc.
390 Fiber::primaryFiber()->run();
391
392 if (pauseEvent.scheduled())
393 deschedule(&pauseEvent);
394 if (stopEvent.scheduled())
395 deschedule(&stopEvent);
396 if (maxTickEvent.scheduled())
397 deschedule(&maxTickEvent);
398 if (starvationEvent.scheduled())
399 deschedule(&starvationEvent);
400
401 if (_throwToScMain) {
402 const ::sc_core::sc_report *to_throw = _throwToScMain;
403 _throwToScMain = nullptr;
404 throw *to_throw;
405 }
406}
407
408void
409Scheduler::oneCycle()
410{
411 runOnce = true;
412 scheduleReadyEvent();
413 start(::MaxTick, false);
414}
415
416void
417Scheduler::schedulePause()
418{
419 if (pauseEvent.scheduled())
420 return;
421
422 schedule(&pauseEvent);
423}
424
425void
426Scheduler::throwToScMain()
427{
428 ::sc_core::sc_report report = reportifyException();
429 _throwToScMain = &report;
430 status(StatusOther);
431 scMainFiber.run();
432}
433
434void
435Scheduler::scheduleStop(bool finish_delta)
436{
437 if (stopEvent.scheduled())
438 return;
439
440 if (!finish_delta) {
441 _stopNow = true;
442 // If we're not supposed to finish the delta cycle, flush all
443 // pending activity.
444 clear();
445 }
446 schedule(&stopEvent);
447}
448
449void
450Scheduler::trace(bool delta)
451{
452 for (auto tf: traceFiles)
453 tf->trace(delta);
454}
455
456Scheduler scheduler;
457Process *getCurrentProcess() { return scheduler.current(); }
458
459namespace {
460
461void
462throwingReportHandler(const ::sc_core::sc_report &r,
463 const ::sc_core::sc_actions &)
464{
465 throw r;
466}
467
468} // anonymous namespace
469
470const ::sc_core::sc_report
471reportifyException()
472{
473 ::sc_core::sc_report_handler_proc old_handler = reportHandlerProc;
474 ::sc_core::sc_report_handler::set_handler(&throwingReportHandler);
475
476 try {
477 try {
478 // Rethrow the current exception so we can catch it and throw an
479 // sc_report instead if it's not a type we recognize/can handle.
480 throw;
481 } catch (const ::sc_core::sc_report &) {
482 // It's already a sc_report, so nothing to do.
483 throw;
484 } catch (const ::sc_core::sc_unwind_exception &) {
485 panic("Kill/reset exception escaped a Process::run()");
486 } catch (const std::exception &e) {
487 SC_REPORT_ERROR(
488 sc_core::SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_, e.what());
489 } catch (const char *msg) {
490 SC_REPORT_ERROR(
491 sc_core::SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_, msg);
492 } catch (...) {
493 SC_REPORT_ERROR(
494 sc_core::SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_,
495 "UNKNOWN EXCEPTION");
496 }
497 } catch (const ::sc_core::sc_report &r) {
498 ::sc_core::sc_report_handler::set_handler(old_handler);
499 return r;
500 }
501 panic("No exception thrown in reportifyException.");
502}
503
504} // namespace sc_gem5