qualcomm/qbox
Loading...
Searching...
No Matches
realtimelimiter.h
1/*
2 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 * Author: GreenSocs 2022
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef REALTIMLIMITER_H
9#define REALTIMLIMITER_H
10
11#include <thread>
12#include <mutex>
13#include <queue>
14#include <future>
15
16#include <systemc>
17#include <cci_configuration>
18#include <scp/report.h>
19
20#include <async_event.h>
21#include <module_factory_registery.h>
22
23namespace gs {
30SC_MODULE (realtimelimiter) {
31 SCP_LOGGER();
32 cci::cci_param<double> p_RTquantum_ms;
33 cci::cci_param<double> p_SCTimeout_ms;
34 cci::cci_param<double> p_MaxTime_ms;
35
36 std::chrono::high_resolution_clock::time_point startRT;
37 sc_core::sc_time startSC;
38 sc_core::sc_time runto;
39 std::thread m_tick_thread;
40 bool running = false;
41 bool suspended = false;
42 sc_core::sc_time suspend_at = sc_core::SC_ZERO_TIME;
43 async_event tick;
44
45 void SCticker()
46 {
47 if (!running) {
48 tick.async_detach_suspending();
49 sc_core::sc_unsuspend_all();
50 return;
51 }
52 if (suspended == true && (sc_core::sc_time_stamp() > suspend_at)) {
53 /* If we were suspended, and time has drifted, then account for it here. */
54 sc_core::sc_time drift = sc_core::sc_time_stamp() - suspend_at;
55 startSC += drift;
56 SCP_WARN(())("Time drift of {}s", drift.to_seconds());
57 }
58 if (p_MaxTime_ms && sc_core::sc_time_stamp() > (sc_core::sc_time(p_MaxTime_ms, sc_core::SC_MS) + startSC)) {
59 SCP_FATAL(())("Max timeout expired");
60 }
61
62 if (sc_core::sc_time_stamp() >= runto) {
63 SCP_TRACE(())("Suspending");
64 tick.async_attach_suspending();
65 sc_core::sc_suspend_all(); // Dont starve while we're waiting
66 // for realtime.
67 suspend_at = sc_core::sc_time_stamp();
68 suspended = true;
69 } else {
70 SCP_TRACE(())("Resuming");
71 suspended = false;
72 // lets go with +=1/2 a RTquantum
73 tick.notify((runto + sc_core::sc_time(p_RTquantum_ms, sc_core::SC_MS) / 2) - sc_core::sc_time_stamp());
74 tick.async_detach_suspending(); // We could even starve !
75 sc_core::sc_unsuspend_all();
76 }
77 }
78
79 void RTticker()
80 {
81 sc_core::sc_time last = sc_core::SC_ZERO_TIME;
82 while (running) {
83 std::this_thread::sleep_for(std::chrono::milliseconds(p_RTquantum_ms));
84
85 runto = sc_core::sc_time(std::chrono::duration_cast<std::chrono::microseconds>(
86 std::chrono::high_resolution_clock::now() - startRT)
87 .count(),
88 sc_core::SC_US) +
89 startSC;
90 if (last > sc_core::SC_ZERO_TIME && sc_core::sc_time_stamp() == last) {
91 p_RTquantum_ms.set_value(p_RTquantum_ms.get_value() + 100);
92 if (p_SCTimeout_ms) {
93 SCP_WARN(())
94 ("Stalled ? (runto is {}s ahead, systemc time has not changed, new limit {}ms {}s)",
95 (runto - sc_core::sc_time_stamp()).to_seconds(), p_RTquantum_ms.get_value(), runto.to_seconds());
96 }
97 /* Only check for exsessive runto's if we're stalled */
98 if (p_SCTimeout_ms &&
99 (runto > sc_core::sc_time_stamp() + sc_core::sc_time(p_SCTimeout_ms, sc_core::SC_MS))) {
100 SCP_FATAL(())
101 ("Requested runto is {}s ahead (SCTimeout_ms set to {}s)",
102 (runto - sc_core::sc_time_stamp()).to_seconds(), p_SCTimeout_ms / 1000);
103 }
104 } else {
105 last = sc_core::sc_time_stamp();
106 }
107
108 tick.notify();
109 }
110 }
111
112public:
113 /* NB all these functions should be called from the SystemC thread of course*/
114 void enable()
115 {
116 assert(running == false);
117
118 running = true;
119
120 startRT = std::chrono::high_resolution_clock::now();
121 startSC = sc_core::sc_time_stamp();
122 runto = sc_core::sc_time(p_RTquantum_ms, sc_core::SC_MS) + sc_core::sc_time_stamp();
123 tick.notify(sc_core::sc_time(p_RTquantum_ms, sc_core::SC_MS));
124
125 m_tick_thread = std::thread(&realtimelimiter::RTticker, this);
126 }
127
128 void disable()
129 {
130 if (running) {
131 running = false;
132 m_tick_thread.join();
133 }
134 }
135 realtimelimiter(const sc_core::sc_module_name& name): realtimelimiter(name, true) {}
136 realtimelimiter(const sc_core::sc_module_name& name, bool autostart)
137 : sc_module(name)
138 , p_RTquantum_ms("RTquantum_ms", 100, "Real time quantum in milliseconds")
139 , p_SCTimeout_ms("SCTimeout_ms", 0, "Timeout for SystemC in milliseconds")
140 , p_MaxTime_ms("MaxTime_ms", 0, "Maximum run time in ms (0=no limit)")
141 , tick(false) // handle attach manually
142 {
143 SCP_TRACE(())("realtimelimiter constructor");
144 SC_METHOD(SCticker);
145 dont_initialize();
146 sensitive << tick;
147 if (autostart) {
148 SC_THREAD(enable);
149 }
150 }
151
152 void end_of_simulation() { disable(); }
153};
154} // namespace gs
155
156extern "C" void module_register();
157
158#endif // REALTIMLIMITER_H
Definition async_event.h:22
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10