Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EVENT_SCHEDULER_H_ |
| 2 | #define AOS_EVENTS_EVENT_SCHEDULER_H_ |
| 3 | |
| 4 | #include <algorithm> |
| 5 | #include <map> |
| 6 | #include <memory> |
| 7 | #include <unordered_set> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame^] | 11 | #include "aos/events/epoll.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "aos/events/event_loop.h" |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 13 | #include "aos/events/logging/boot_timestamp.h" |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 14 | #include "aos/logging/implementations.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | #include "aos/time/time.h" |
| 16 | #include "glog/logging.h" |
| 17 | |
| 18 | namespace aos { |
| 19 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 20 | // This clock is the basis for distributed time. It is used to synchronize time |
| 21 | // between multiple nodes. This is a new type so conversions to and from the |
| 22 | // monotonic and realtime clocks aren't implicit. |
| 23 | class distributed_clock { |
| 24 | public: |
| 25 | typedef ::std::chrono::nanoseconds::rep rep; |
| 26 | typedef ::std::chrono::nanoseconds::period period; |
| 27 | typedef ::std::chrono::nanoseconds duration; |
| 28 | typedef ::std::chrono::time_point<distributed_clock> time_point; |
| 29 | |
| 30 | // This clock is the base clock for the simulation and everything is synced to |
| 31 | // it. It never jumps. |
| 32 | static constexpr bool is_steady = true; |
| 33 | |
| 34 | // Returns the epoch (0). |
| 35 | static constexpr time_point epoch() { return time_point(zero()); } |
| 36 | |
| 37 | static constexpr duration zero() { return duration(0); } |
| 38 | |
| 39 | static constexpr time_point min_time{ |
| 40 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
| 41 | static constexpr time_point max_time{ |
| 42 | time_point(duration(::std::numeric_limits<duration::rep>::max()))}; |
| 43 | }; |
| 44 | |
| 45 | std::ostream &operator<<(std::ostream &stream, |
| 46 | const aos::distributed_clock::time_point &now); |
| 47 | |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 48 | // Interface to handle converting time on a node to and from the distributed |
| 49 | // clock accurately. |
| 50 | class TimeConverter { |
| 51 | public: |
| 52 | virtual ~TimeConverter() {} |
| 53 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 54 | // Returns the boot UUID for a node and boot. Note: the boot UUID for |
| 55 | // subsequent calls needs to be the same each time. |
| 56 | virtual UUID boot_uuid(size_t node_index, size_t boot_count) = 0; |
| 57 | |
| 58 | void set_reboot_found( |
| 59 | std::function<void(distributed_clock::time_point, |
| 60 | const std::vector<logger::BootTimestamp> &)> |
| 61 | fn) { |
| 62 | reboot_found_ = fn; |
| 63 | } |
| 64 | |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 65 | // Converts a time to the distributed clock for scheduling and cross-node |
| 66 | // time measurement. |
| 67 | virtual distributed_clock::time_point ToDistributedClock( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 68 | size_t node_index, logger::BootTimestamp time) = 0; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 69 | |
| 70 | // Takes the distributed time and converts it to the monotonic clock for this |
| 71 | // node. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 72 | virtual logger::BootTimestamp FromDistributedClock( |
| 73 | size_t node_index, distributed_clock::time_point time, |
| 74 | size_t boot_count) = 0; |
Austin Schuh | b7c8d2a | 2021-07-19 19:22:12 -0700 | [diff] [blame] | 75 | |
| 76 | // Called whenever time passes this point and we can forget about it. |
| 77 | virtual void ObserveTimePassed(distributed_clock::time_point time) = 0; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 78 | |
| 79 | protected: |
| 80 | std::function<void(distributed_clock::time_point, |
| 81 | const std::vector<logger::BootTimestamp> &)> |
| 82 | reboot_found_; |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 85 | class EventSchedulerScheduler; |
| 86 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | class EventScheduler { |
| 88 | public: |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 89 | class Event { |
| 90 | public: |
| 91 | virtual void Handle() noexcept = 0; |
| 92 | virtual ~Event() {} |
| 93 | }; |
| 94 | |
| 95 | using ChannelType = std::multimap<monotonic_clock::time_point, Event *>; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | using Token = ChannelType::iterator; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 97 | EventScheduler(size_t node_index) : node_index_(node_index) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 98 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 99 | // Sets the time converter in use for this scheduler (and the corresponding |
| 100 | // node index) |
| 101 | void SetTimeConverter(size_t node_index, TimeConverter *converter) { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 102 | CHECK_EQ(node_index_, node_index); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 103 | converter_ = converter; |
| 104 | } |
| 105 | |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 106 | UUID boot_uuid() { return converter_->boot_uuid(node_index_, boot_count_); } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 107 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 108 | // Schedule an event with a callback function |
| 109 | // Returns an iterator to the event |
Austin Schuh | ef8f1ae | 2021-12-11 12:35:05 -0800 | [diff] [blame] | 110 | Token Schedule(monotonic_clock::time_point time, Event *callback); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 112 | // Schedules a callback when the event scheduler starts. |
| 113 | void ScheduleOnRun(std::function<void()> callback) { |
| 114 | on_run_.emplace_back(std::move(callback)); |
| 115 | } |
| 116 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 117 | // Schedules a callback when the event scheduler starts. |
| 118 | void ScheduleOnStartup(std::function<void()> callback) { |
| 119 | on_startup_.emplace_back(std::move(callback)); |
| 120 | } |
| 121 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 122 | void set_on_shutdown(std::function<void()> callback) { |
| 123 | on_shutdown_ = std::move(callback); |
| 124 | } |
| 125 | |
| 126 | void set_started(std::function<void()> callback) { |
| 127 | started_ = std::move(callback); |
| 128 | } |
| 129 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 130 | void set_stopped(std::function<void()> callback) { |
| 131 | stopped_ = std::move(callback); |
| 132 | } |
| 133 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 134 | std::function<void()> started_; |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 135 | std::function<void()> stopped_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 136 | std::function<void()> on_shutdown_; |
| 137 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 138 | Token InvalidToken() { return events_list_.end(); } |
| 139 | |
| 140 | // Deschedule an event by its iterator |
| 141 | void Deschedule(Token token); |
| 142 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 143 | // Runs the OnRun callbacks. |
| 144 | void RunOnRun(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 145 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 146 | // Runs the OnStartup callbacks. |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 147 | void RunOnStartup() noexcept; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 148 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 149 | // Runs the Started callback. |
| 150 | void RunStarted(); |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 151 | // Runs the Started callback. |
| 152 | void RunStopped(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 153 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 154 | // Returns true if events are being handled. |
| 155 | inline bool is_running() const; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 156 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 157 | // Returns the timestamp of the next event to trigger. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 158 | monotonic_clock::time_point OldestEvent(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 159 | // Handles the next event. |
| 160 | void CallOldestEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 161 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 162 | // Converts a time to the distributed clock for scheduling and cross-node time |
| 163 | // measurement. |
| 164 | distributed_clock::time_point ToDistributedClock( |
| 165 | monotonic_clock::time_point time) const { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 166 | return converter_->ToDistributedClock(node_index_, |
| 167 | {.boot = boot_count_, .time = time}); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Takes the distributed time and converts it to the monotonic clock for this |
| 171 | // node. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 172 | logger::BootTimestamp FromDistributedClock( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 173 | distributed_clock::time_point time) const { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 174 | return converter_->FromDistributedClock(node_index_, time, boot_count_); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Returns the current monotonic time on this node calculated from the |
| 178 | // distributed clock. |
| 179 | inline monotonic_clock::time_point monotonic_now() const; |
| 180 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 181 | // Returns the current monotonic time on this node calculated from the |
| 182 | // distributed clock. |
| 183 | inline distributed_clock::time_point distributed_now() const; |
| 184 | |
| 185 | size_t boot_count() const { return boot_count_; } |
| 186 | |
| 187 | size_t node_index() const { return node_index_; } |
| 188 | |
| 189 | // For implementing reboots. |
| 190 | void Shutdown(); |
| 191 | void Startup(); |
| 192 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 193 | private: |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 194 | friend class EventSchedulerScheduler; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 195 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 196 | // Current execution time. |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 197 | monotonic_clock::time_point monotonic_now_ = monotonic_clock::epoch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 198 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 199 | size_t boot_count_ = 0; |
| 200 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 201 | // List of functions to run (once) when running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 202 | std::vector<std::function<void()>> on_run_; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 203 | std::vector<std::function<void()>> on_startup_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 204 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 205 | // Multimap holding times to run functions. These are stored in order, and |
| 206 | // the order is the callback tree. |
| 207 | ChannelType events_list_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 208 | |
| 209 | // Pointer to the actual scheduler. |
| 210 | EventSchedulerScheduler *scheduler_scheduler_ = nullptr; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 211 | |
| 212 | // Node index handle to be handed back to the TimeConverter. This lets the |
| 213 | // same time converter be used for all the nodes, and the node index |
| 214 | // distinguish which one. |
| 215 | size_t node_index_ = 0; |
| 216 | |
| 217 | // Converts time by doing nothing to it. |
| 218 | class UnityConverter final : public TimeConverter { |
| 219 | public: |
| 220 | distributed_clock::time_point ToDistributedClock( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 221 | size_t /*node_index*/, logger::BootTimestamp time) override { |
| 222 | CHECK_EQ(time.boot, 0u) << ": Reboots unsupported by default."; |
| 223 | return distributed_clock::epoch() + time.time.time_since_epoch(); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 226 | logger::BootTimestamp FromDistributedClock( |
| 227 | size_t /*node_index*/, distributed_clock::time_point time, |
| 228 | size_t boot_count) override { |
| 229 | CHECK_EQ(boot_count, 0u); |
| 230 | return logger::BootTimestamp{ |
| 231 | .boot = boot_count, |
| 232 | .time = monotonic_clock::epoch() + time.time_since_epoch()}; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 233 | } |
Austin Schuh | b7c8d2a | 2021-07-19 19:22:12 -0700 | [diff] [blame] | 234 | |
| 235 | void ObserveTimePassed(distributed_clock::time_point /*time*/) override {} |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 236 | |
| 237 | UUID boot_uuid(size_t /*node_index*/, size_t boot_count) override { |
| 238 | CHECK_EQ(boot_count, 0u); |
| 239 | return uuid_; |
| 240 | } |
| 241 | |
| 242 | private: |
| 243 | const UUID uuid_ = UUID::Random(); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | UnityConverter unity_converter_; |
| 247 | |
| 248 | TimeConverter *converter_ = &unity_converter_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 251 | // We need a heap of heaps... |
| 252 | // |
| 253 | // Events in a node have a very well defined progression of time. It is linear |
| 254 | // and well represented by the monotonic clock. |
| 255 | // |
| 256 | // Events across nodes don't follow this well. Time skews between the two nodes |
| 257 | // all the time. We also don't know the function ahead of time which converts |
| 258 | // from each node's monotonic clock to the distributed clock (our unified base |
| 259 | // time which is likely the average time between nodes). |
| 260 | // |
| 261 | // This pushes us towards merge sort. Sorting each node's events with a heap |
| 262 | // like we used to be doing, and then sorting each of those nodes independently. |
| 263 | class EventSchedulerScheduler { |
| 264 | public: |
| 265 | // Adds an event scheduler to the list. |
| 266 | void AddEventScheduler(EventScheduler *scheduler); |
| 267 | |
| 268 | // Runs until there are no more events or Exit is called. |
| 269 | void Run(); |
| 270 | |
| 271 | // Stops running. |
| 272 | void Exit() { is_running_ = false; } |
| 273 | |
| 274 | bool is_running() const { return is_running_; } |
| 275 | |
| 276 | // Runs for a duration on the distributed clock. Time on the distributed |
| 277 | // clock should be very representative of time on each node, but won't be |
| 278 | // exactly the same. |
| 279 | void RunFor(distributed_clock::duration duration); |
| 280 | |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame^] | 281 | // Sets the realtime replay rate. A value of 1.0 will cause the scheduler to |
| 282 | // try to play events in realtime. 0.5 will run at half speed. Use infinity |
| 283 | // (the default) to run as fast as possible. This can be changed during |
| 284 | // run-time. |
| 285 | void SetReplayRate(double replay_rate) { replay_rate_ = replay_rate; } |
| 286 | internal::EPoll *epoll() { return &epoll_; } |
| 287 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 288 | // Returns the current distributed time. |
| 289 | distributed_clock::time_point distributed_now() const { return now_; } |
| 290 | |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 291 | void RunOnStartup() { |
| 292 | CHECK(!is_running_); |
| 293 | for (EventScheduler *scheduler : schedulers_) { |
| 294 | scheduler->RunOnStartup(); |
| 295 | } |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 296 | for (EventScheduler *scheduler : schedulers_) { |
| 297 | scheduler->RunStarted(); |
| 298 | } |
| 299 | } |
| 300 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 301 | void RunStopped() { |
| 302 | CHECK(!is_running_); |
| 303 | for (EventScheduler *scheduler : schedulers_) { |
| 304 | scheduler->RunStopped(); |
| 305 | } |
| 306 | } |
| 307 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 308 | void SetTimeConverter(TimeConverter *time_converter) { |
| 309 | time_converter->set_reboot_found( |
| 310 | [this](distributed_clock::time_point reboot_time, |
| 311 | const std::vector<logger::BootTimestamp> &node_times) { |
| 312 | if (!reboots_.empty()) { |
| 313 | CHECK_GT(reboot_time, std::get<0>(reboots_.back())); |
| 314 | } |
| 315 | reboots_.emplace_back(reboot_time, node_times); |
| 316 | }); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 319 | // Runs the provided callback now. Stops everything, runs the callback, then |
| 320 | // starts it all up again. This lets us do operations like starting and |
| 321 | // stopping applications while running. |
| 322 | void TemporarilyStopAndRun(std::function<void()> fn); |
| 323 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 324 | private: |
| 325 | // Handles running the OnRun functions. |
| 326 | void RunOnRun() { |
| 327 | CHECK(!is_running_); |
| 328 | is_running_ = true; |
| 329 | for (EventScheduler *scheduler : schedulers_) { |
| 330 | scheduler->RunOnRun(); |
| 331 | } |
| 332 | } |
| 333 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 334 | void Reboot(); |
| 335 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 336 | // Returns the next event time and scheduler on which to run it. |
| 337 | std::tuple<distributed_clock::time_point, EventScheduler *> OldestEvent(); |
| 338 | |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame^] | 339 | // Handles running loop_body repeatedly until complete. loop_body should |
| 340 | // return the next time at which it wants to be called, and set is_running_ to |
| 341 | // false once we should stop. |
| 342 | template <typename F> |
| 343 | void RunMaybeRealtimeLoop(F loop_body); |
| 344 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 345 | // True if we are running. |
| 346 | bool is_running_ = false; |
| 347 | // The current time. |
| 348 | distributed_clock::time_point now_ = distributed_clock::epoch(); |
| 349 | // List of schedulers to run in sync. |
| 350 | std::vector<EventScheduler *> schedulers_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 351 | |
| 352 | // List of when to reboot each node. |
| 353 | std::vector<std::tuple<distributed_clock::time_point, |
| 354 | std::vector<logger::BootTimestamp>>> |
| 355 | reboots_; |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame^] | 356 | |
| 357 | double replay_rate_ = std::numeric_limits<double>::infinity(); |
| 358 | internal::EPoll epoll_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 359 | }; |
| 360 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 361 | inline distributed_clock::time_point EventScheduler::distributed_now() const { |
| 362 | return scheduler_scheduler_->distributed_now(); |
| 363 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 364 | inline monotonic_clock::time_point EventScheduler::monotonic_now() const { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 365 | const logger::BootTimestamp t = |
| 366 | FromDistributedClock(scheduler_scheduler_->distributed_now()); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 367 | CHECK_EQ(t.boot, boot_count_) |
| 368 | << ": " |
| 369 | << " " << t << " d " << scheduler_scheduler_->distributed_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 370 | return t.time; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | inline bool EventScheduler::is_running() const { |
| 374 | return scheduler_scheduler_->is_running(); |
| 375 | } |
| 376 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 377 | } // namespace aos |
| 378 | |
| 379 | #endif // AOS_EVENTS_EVENT_SCHEDULER_H_ |