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 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 112 | // Schedules a callback whenever the event scheduler starts, after we have |
| 113 | // entered the running state. Callbacks are cleared after being called once. |
| 114 | // Will not get called until a node starts (a node does not start until its |
| 115 | // monotonic clock has reached at least monotonic_clock::epoch()). |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 116 | void ScheduleOnRun(std::function<void()> callback) { |
| 117 | on_run_.emplace_back(std::move(callback)); |
| 118 | } |
| 119 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 120 | // Schedules a callback whenever the event scheduler starts, before we have |
| 121 | // entered the running state. Callbacks are cleared after being called once. |
| 122 | // Will not get called until a node starts (a node does not start until its |
| 123 | // monotonic clock has reached at least monotonic_clock::epoch()). |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 124 | void ScheduleOnStartup(std::function<void()> callback) { |
| 125 | on_startup_.emplace_back(std::move(callback)); |
| 126 | } |
| 127 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 128 | // Schedules a callback for whenever a node reboots, after we have exited the |
| 129 | // running state. Does not get called when the event scheduler stops (unless |
| 130 | // it is stopping to execute the reboot). |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 131 | void set_on_shutdown(std::function<void()> callback) { |
| 132 | on_shutdown_ = std::move(callback); |
| 133 | } |
| 134 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 135 | // Identical to ScheduleOnStartup, except that only one callback may get set |
| 136 | // and it will not be cleared after being called. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 137 | void set_started(std::function<void()> callback) { |
| 138 | started_ = std::move(callback); |
| 139 | } |
| 140 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 141 | // Schedules a callback for whenever the scheduler exits the running state |
| 142 | // (running will be false during the callback). This includes both node |
| 143 | // reboots and the end of regular execution. Will not be called if the node |
| 144 | // never started. |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 145 | void set_stopped(std::function<void()> callback) { |
| 146 | stopped_ = std::move(callback); |
| 147 | } |
| 148 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 149 | Token InvalidToken() { return events_list_.end(); } |
| 150 | |
| 151 | // Deschedule an event by its iterator |
| 152 | void Deschedule(Token token); |
| 153 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 154 | // Runs the Started callback. |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 155 | void MaybeRunStopped(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 156 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 157 | // Returns true if events are being handled. |
| 158 | inline bool is_running() const; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 159 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 160 | // Returns the timestamp of the next event to trigger. |
Austin Schuh | e12b5eb | 2022-08-29 12:39:27 -0700 | [diff] [blame^] | 161 | std::pair<distributed_clock::time_point, monotonic_clock::time_point> |
| 162 | OldestEvent(); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 163 | // Handles the next event. |
| 164 | void CallOldestEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 165 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 166 | // Converts a time to the distributed clock for scheduling and cross-node time |
| 167 | // measurement. |
| 168 | distributed_clock::time_point ToDistributedClock( |
| 169 | monotonic_clock::time_point time) const { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 170 | return converter_->ToDistributedClock(node_index_, |
| 171 | {.boot = boot_count_, .time = time}); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Takes the distributed time and converts it to the monotonic clock for this |
| 175 | // node. |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 176 | logger::BootTimestamp FromDistributedClock( |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 177 | distributed_clock::time_point time) const { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 178 | return converter_->FromDistributedClock(node_index_, time, boot_count_); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // Returns the current monotonic time on this node calculated from the |
| 182 | // distributed clock. |
| 183 | inline monotonic_clock::time_point monotonic_now() const; |
| 184 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 185 | // Returns the current monotonic time on this node calculated from the |
| 186 | // distributed clock. |
| 187 | inline distributed_clock::time_point distributed_now() const; |
| 188 | |
| 189 | size_t boot_count() const { return boot_count_; } |
| 190 | |
| 191 | size_t node_index() const { return node_index_; } |
| 192 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 193 | private: |
| 194 | friend class EventSchedulerScheduler; |
| 195 | |
| 196 | // Runs the OnRun callbacks. |
| 197 | void RunOnRun(); |
| 198 | |
| 199 | // Runs the OnStartup callbacks. |
| 200 | void RunOnStartup() noexcept; |
| 201 | |
| 202 | // Runs the Started callback. |
| 203 | void RunStarted(); |
| 204 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 205 | // For implementing reboots. |
| 206 | void Shutdown(); |
| 207 | void Startup(); |
| 208 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 209 | void MaybeRunOnStartup(); |
| 210 | void MaybeRunOnRun(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 211 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 212 | // Current execution time. |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 213 | monotonic_clock::time_point monotonic_now_ = monotonic_clock::epoch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 214 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 215 | size_t boot_count_ = 0; |
| 216 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 217 | // List of functions to run (once) when running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 218 | std::vector<std::function<void()>> on_run_; |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 219 | std::vector<std::function<void()>> on_startup_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 220 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 221 | // Multimap holding times to run functions. These are stored in order, and |
| 222 | // the order is the callback tree. |
| 223 | ChannelType events_list_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 224 | |
| 225 | // Pointer to the actual scheduler. |
| 226 | EventSchedulerScheduler *scheduler_scheduler_ = nullptr; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 227 | |
| 228 | // Node index handle to be handed back to the TimeConverter. This lets the |
| 229 | // same time converter be used for all the nodes, and the node index |
| 230 | // distinguish which one. |
| 231 | size_t node_index_ = 0; |
| 232 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 233 | // Whether this individual scheduler is currently running. |
| 234 | bool is_running_ = false; |
| 235 | // Whether we have called all the startup handlers during this boot. |
| 236 | bool called_started_ = false; |
Austin Schuh | e12b5eb | 2022-08-29 12:39:27 -0700 | [diff] [blame^] | 237 | std::optional<distributed_clock::time_point> cached_epoch_; |
| 238 | monotonic_clock::time_point cached_event_list_monotonic_time_ = |
| 239 | monotonic_clock::max_time; |
| 240 | distributed_clock::time_point cached_event_list_time_ = |
| 241 | distributed_clock::max_time; |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 242 | |
| 243 | std::function<void()> started_; |
| 244 | std::function<void()> stopped_; |
| 245 | std::function<void()> on_shutdown_; |
| 246 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 247 | // Converts time by doing nothing to it. |
| 248 | class UnityConverter final : public TimeConverter { |
| 249 | public: |
| 250 | distributed_clock::time_point ToDistributedClock( |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 251 | size_t /*node_index*/, logger::BootTimestamp time) override { |
| 252 | CHECK_EQ(time.boot, 0u) << ": Reboots unsupported by default."; |
| 253 | return distributed_clock::epoch() + time.time.time_since_epoch(); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 256 | logger::BootTimestamp FromDistributedClock( |
| 257 | size_t /*node_index*/, distributed_clock::time_point time, |
| 258 | size_t boot_count) override { |
| 259 | CHECK_EQ(boot_count, 0u); |
| 260 | return logger::BootTimestamp{ |
| 261 | .boot = boot_count, |
| 262 | .time = monotonic_clock::epoch() + time.time_since_epoch()}; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 263 | } |
Austin Schuh | b7c8d2a | 2021-07-19 19:22:12 -0700 | [diff] [blame] | 264 | |
| 265 | void ObserveTimePassed(distributed_clock::time_point /*time*/) override {} |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 266 | |
| 267 | UUID boot_uuid(size_t /*node_index*/, size_t boot_count) override { |
| 268 | CHECK_EQ(boot_count, 0u); |
| 269 | return uuid_; |
| 270 | } |
| 271 | |
| 272 | private: |
| 273 | const UUID uuid_ = UUID::Random(); |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | UnityConverter unity_converter_; |
| 277 | |
| 278 | TimeConverter *converter_ = &unity_converter_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 279 | }; |
| 280 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 281 | // We need a heap of heaps... |
| 282 | // |
| 283 | // Events in a node have a very well defined progression of time. It is linear |
| 284 | // and well represented by the monotonic clock. |
| 285 | // |
| 286 | // Events across nodes don't follow this well. Time skews between the two nodes |
| 287 | // all the time. We also don't know the function ahead of time which converts |
| 288 | // from each node's monotonic clock to the distributed clock (our unified base |
| 289 | // time which is likely the average time between nodes). |
| 290 | // |
| 291 | // This pushes us towards merge sort. Sorting each node's events with a heap |
| 292 | // like we used to be doing, and then sorting each of those nodes independently. |
| 293 | class EventSchedulerScheduler { |
| 294 | public: |
| 295 | // Adds an event scheduler to the list. |
| 296 | void AddEventScheduler(EventScheduler *scheduler); |
| 297 | |
| 298 | // Runs until there are no more events or Exit is called. |
| 299 | void Run(); |
| 300 | |
| 301 | // Stops running. |
| 302 | void Exit() { is_running_ = false; } |
| 303 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 304 | // Runs for a duration on the distributed clock. Time on the distributed |
| 305 | // clock should be very representative of time on each node, but won't be |
| 306 | // exactly the same. |
| 307 | void RunFor(distributed_clock::duration duration); |
| 308 | |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame] | 309 | // Sets the realtime replay rate. A value of 1.0 will cause the scheduler to |
| 310 | // try to play events in realtime. 0.5 will run at half speed. Use infinity |
| 311 | // (the default) to run as fast as possible. This can be changed during |
| 312 | // run-time. |
| 313 | void SetReplayRate(double replay_rate) { replay_rate_ = replay_rate; } |
| 314 | internal::EPoll *epoll() { return &epoll_; } |
| 315 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 316 | // Run until time. fn_realtime_offset is a function that returns the |
| 317 | // realtime offset. |
| 318 | // Returns true if it ran until time (i.e., Exit() was not called before |
| 319 | // end_time). |
| 320 | bool RunUntil(realtime_clock::time_point end_time, EventScheduler *scheduler, |
| 321 | std::function<std::chrono::nanoseconds()> fn_realtime_offset); |
| 322 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 323 | // Returns the current distributed time. |
| 324 | distributed_clock::time_point distributed_now() const { return now_; } |
| 325 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 326 | void SetTimeConverter(TimeConverter *time_converter) { |
| 327 | time_converter->set_reboot_found( |
| 328 | [this](distributed_clock::time_point reboot_time, |
| 329 | const std::vector<logger::BootTimestamp> &node_times) { |
| 330 | if (!reboots_.empty()) { |
| 331 | CHECK_GT(reboot_time, std::get<0>(reboots_.back())); |
| 332 | } |
| 333 | reboots_.emplace_back(reboot_time, node_times); |
| 334 | }); |
Austin Schuh | 057d29f | 2021-08-21 23:05:15 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 337 | // Runs the provided callback now. Stops everything, runs the callback, then |
| 338 | // starts it all up again. This lets us do operations like starting and |
| 339 | // stopping applications while running. |
| 340 | void TemporarilyStopAndRun(std::function<void()> fn); |
| 341 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 342 | private: |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 343 | void Reboot(); |
| 344 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 345 | void MaybeRunStopped(); |
| 346 | void MaybeRunOnStartup(); |
| 347 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 348 | // Returns the next event time and scheduler on which to run it. |
| 349 | std::tuple<distributed_clock::time_point, EventScheduler *> OldestEvent(); |
| 350 | |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame] | 351 | // Handles running loop_body repeatedly until complete. loop_body should |
| 352 | // return the next time at which it wants to be called, and set is_running_ to |
| 353 | // false once we should stop. |
| 354 | template <typename F> |
| 355 | void RunMaybeRealtimeLoop(F loop_body); |
| 356 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 357 | // True if we are running. |
| 358 | bool is_running_ = false; |
| 359 | // The current time. |
| 360 | distributed_clock::time_point now_ = distributed_clock::epoch(); |
| 361 | // List of schedulers to run in sync. |
| 362 | std::vector<EventScheduler *> schedulers_; |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 363 | |
| 364 | // List of when to reboot each node. |
| 365 | std::vector<std::tuple<distributed_clock::time_point, |
| 366 | std::vector<logger::BootTimestamp>>> |
| 367 | reboots_; |
James Kuszmaul | b67409b | 2022-06-20 16:25:03 -0700 | [diff] [blame] | 368 | |
| 369 | double replay_rate_ = std::numeric_limits<double>::infinity(); |
| 370 | internal::EPoll epoll_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 371 | }; |
| 372 | |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 373 | inline distributed_clock::time_point EventScheduler::distributed_now() const { |
| 374 | return scheduler_scheduler_->distributed_now(); |
| 375 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 376 | inline monotonic_clock::time_point EventScheduler::monotonic_now() const { |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 377 | const logger::BootTimestamp t = |
| 378 | FromDistributedClock(scheduler_scheduler_->distributed_now()); |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 379 | CHECK_EQ(t.boot, boot_count_) |
| 380 | << ": " |
| 381 | << " " << t << " d " << scheduler_scheduler_->distributed_now(); |
Austin Schuh | 58646e2 | 2021-08-23 23:51:46 -0700 | [diff] [blame] | 382 | return t.time; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 383 | } |
| 384 | |
James Kuszmaul | 86e86c3 | 2022-07-21 17:39:47 -0700 | [diff] [blame] | 385 | inline bool EventScheduler::is_running() const { return is_running_; } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 386 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 387 | } // namespace aos |
| 388 | |
| 389 | #endif // AOS_EVENTS_EVENT_SCHEDULER_H_ |