blob: 55c1cf87a58e0f5fec408bf8f35ac00ffff33244 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#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
11#include "aos/events/event_loop.h"
Austin Schuh58646e22021-08-23 23:51:46 -070012#include "aos/events/logging/boot_timestamp.h"
Austin Schuh8bd96322020-02-13 21:18:22 -080013#include "aos/logging/implementations.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "aos/time/time.h"
15#include "glog/logging.h"
16
17namespace aos {
18
Austin Schuhac0771c2020-01-07 18:36:30 -080019// This clock is the basis for distributed time. It is used to synchronize time
20// between multiple nodes. This is a new type so conversions to and from the
21// monotonic and realtime clocks aren't implicit.
22class distributed_clock {
23 public:
24 typedef ::std::chrono::nanoseconds::rep rep;
25 typedef ::std::chrono::nanoseconds::period period;
26 typedef ::std::chrono::nanoseconds duration;
27 typedef ::std::chrono::time_point<distributed_clock> time_point;
28
29 // This clock is the base clock for the simulation and everything is synced to
30 // it. It never jumps.
31 static constexpr bool is_steady = true;
32
33 // Returns the epoch (0).
34 static constexpr time_point epoch() { return time_point(zero()); }
35
36 static constexpr duration zero() { return duration(0); }
37
38 static constexpr time_point min_time{
39 time_point(duration(::std::numeric_limits<duration::rep>::min()))};
40 static constexpr time_point max_time{
41 time_point(duration(::std::numeric_limits<duration::rep>::max()))};
42};
43
44std::ostream &operator<<(std::ostream &stream,
45 const aos::distributed_clock::time_point &now);
46
Austin Schuha9abc032021-01-01 16:46:19 -080047// Interface to handle converting time on a node to and from the distributed
48// clock accurately.
49class TimeConverter {
50 public:
51 virtual ~TimeConverter() {}
52
Austin Schuh58646e22021-08-23 23:51:46 -070053 // Returns the boot UUID for a node and boot. Note: the boot UUID for
54 // subsequent calls needs to be the same each time.
55 virtual UUID boot_uuid(size_t node_index, size_t boot_count) = 0;
56
57 void set_reboot_found(
58 std::function<void(distributed_clock::time_point,
59 const std::vector<logger::BootTimestamp> &)>
60 fn) {
61 reboot_found_ = fn;
62 }
63
Austin Schuha9abc032021-01-01 16:46:19 -080064 // Converts a time to the distributed clock for scheduling and cross-node
65 // time measurement.
66 virtual distributed_clock::time_point ToDistributedClock(
Austin Schuh58646e22021-08-23 23:51:46 -070067 size_t node_index, logger::BootTimestamp time) = 0;
Austin Schuha9abc032021-01-01 16:46:19 -080068
69 // Takes the distributed time and converts it to the monotonic clock for this
70 // node.
Austin Schuh58646e22021-08-23 23:51:46 -070071 virtual logger::BootTimestamp FromDistributedClock(
72 size_t node_index, distributed_clock::time_point time,
73 size_t boot_count) = 0;
Austin Schuhb7c8d2a2021-07-19 19:22:12 -070074
75 // Called whenever time passes this point and we can forget about it.
76 virtual void ObserveTimePassed(distributed_clock::time_point time) = 0;
Austin Schuh58646e22021-08-23 23:51:46 -070077
78 protected:
79 std::function<void(distributed_clock::time_point,
80 const std::vector<logger::BootTimestamp> &)>
81 reboot_found_;
Austin Schuha9abc032021-01-01 16:46:19 -080082};
83
Austin Schuh8bd96322020-02-13 21:18:22 -080084class EventSchedulerScheduler;
85
Alex Perrycb7da4b2019-08-28 19:35:56 -070086class EventScheduler {
87 public:
Austin Schuhef8f1ae2021-12-11 12:35:05 -080088 class Event {
89 public:
90 virtual void Handle() noexcept = 0;
91 virtual ~Event() {}
92 };
93
94 using ChannelType = std::multimap<monotonic_clock::time_point, Event *>;
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 using Token = ChannelType::iterator;
Austin Schuh58646e22021-08-23 23:51:46 -070096 EventScheduler(size_t node_index) : node_index_(node_index) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -070097
Austin Schuh87dd3832021-01-01 23:07:31 -080098 // Sets the time converter in use for this scheduler (and the corresponding
99 // node index)
100 void SetTimeConverter(size_t node_index, TimeConverter *converter) {
Austin Schuh58646e22021-08-23 23:51:46 -0700101 CHECK_EQ(node_index_, node_index);
Austin Schuh87dd3832021-01-01 23:07:31 -0800102 converter_ = converter;
103 }
104
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800105 UUID boot_uuid() { return converter_->boot_uuid(node_index_, boot_count_); }
Austin Schuh58646e22021-08-23 23:51:46 -0700106
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107 // Schedule an event with a callback function
108 // Returns an iterator to the event
Austin Schuhef8f1ae2021-12-11 12:35:05 -0800109 Token Schedule(monotonic_clock::time_point time, Event *callback);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700110
Austin Schuh39788ff2019-12-01 18:22:57 -0800111 // Schedules a callback when the event scheduler starts.
112 void ScheduleOnRun(std::function<void()> callback) {
113 on_run_.emplace_back(std::move(callback));
114 }
115
Austin Schuh057d29f2021-08-21 23:05:15 -0700116 // Schedules a callback when the event scheduler starts.
117 void ScheduleOnStartup(std::function<void()> callback) {
118 on_startup_.emplace_back(std::move(callback));
119 }
120
Austin Schuh58646e22021-08-23 23:51:46 -0700121 void set_on_shutdown(std::function<void()> callback) {
122 on_shutdown_ = std::move(callback);
123 }
124
125 void set_started(std::function<void()> callback) {
126 started_ = std::move(callback);
127 }
128
129 std::function<void()> started_;
130 std::function<void()> on_shutdown_;
131
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132 Token InvalidToken() { return events_list_.end(); }
133
134 // Deschedule an event by its iterator
135 void Deschedule(Token token);
136
Austin Schuh8bd96322020-02-13 21:18:22 -0800137 // Runs the OnRun callbacks.
138 void RunOnRun();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139
Austin Schuh057d29f2021-08-21 23:05:15 -0700140 // Runs the OnStartup callbacks.
141 void RunOnStartup();
142
Austin Schuh58646e22021-08-23 23:51:46 -0700143 // Runs the Started callback.
144 void RunStarted();
145
Austin Schuh8bd96322020-02-13 21:18:22 -0800146 // Returns true if events are being handled.
147 inline bool is_running() const;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148
Austin Schuh8bd96322020-02-13 21:18:22 -0800149 // Returns the timestamp of the next event to trigger.
Austin Schuh58646e22021-08-23 23:51:46 -0700150 monotonic_clock::time_point OldestEvent();
Austin Schuh8bd96322020-02-13 21:18:22 -0800151 // Handles the next event.
152 void CallOldestEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153
Austin Schuh8bd96322020-02-13 21:18:22 -0800154 // Converts a time to the distributed clock for scheduling and cross-node time
155 // measurement.
156 distributed_clock::time_point ToDistributedClock(
157 monotonic_clock::time_point time) const {
Austin Schuh58646e22021-08-23 23:51:46 -0700158 return converter_->ToDistributedClock(node_index_,
159 {.boot = boot_count_, .time = time});
Austin Schuh8bd96322020-02-13 21:18:22 -0800160 }
161
162 // Takes the distributed time and converts it to the monotonic clock for this
163 // node.
Austin Schuh58646e22021-08-23 23:51:46 -0700164 logger::BootTimestamp FromDistributedClock(
Austin Schuh8bd96322020-02-13 21:18:22 -0800165 distributed_clock::time_point time) const {
Austin Schuh58646e22021-08-23 23:51:46 -0700166 return converter_->FromDistributedClock(node_index_, time, boot_count_);
Austin Schuh8bd96322020-02-13 21:18:22 -0800167 }
168
169 // Returns the current monotonic time on this node calculated from the
170 // distributed clock.
171 inline monotonic_clock::time_point monotonic_now() const;
172
Austin Schuh58646e22021-08-23 23:51:46 -0700173 // Returns the current monotonic time on this node calculated from the
174 // distributed clock.
175 inline distributed_clock::time_point distributed_now() const;
176
177 size_t boot_count() const { return boot_count_; }
178
179 size_t node_index() const { return node_index_; }
180
181 // For implementing reboots.
182 void Shutdown();
183 void Startup();
184
Alex Perrycb7da4b2019-08-28 19:35:56 -0700185 private:
Austin Schuh8bd96322020-02-13 21:18:22 -0800186 friend class EventSchedulerScheduler;
Austin Schuh58646e22021-08-23 23:51:46 -0700187
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188 // Current execution time.
Austin Schuhbe69cf32020-08-27 11:38:33 -0700189 monotonic_clock::time_point monotonic_now_ = monotonic_clock::epoch();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700190
Austin Schuh58646e22021-08-23 23:51:46 -0700191 size_t boot_count_ = 0;
192
Austin Schuh8bd96322020-02-13 21:18:22 -0800193 // List of functions to run (once) when running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800194 std::vector<std::function<void()>> on_run_;
Austin Schuh057d29f2021-08-21 23:05:15 -0700195 std::vector<std::function<void()>> on_startup_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800196
Alex Perrycb7da4b2019-08-28 19:35:56 -0700197 // Multimap holding times to run functions. These are stored in order, and
198 // the order is the callback tree.
199 ChannelType events_list_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800200
201 // Pointer to the actual scheduler.
202 EventSchedulerScheduler *scheduler_scheduler_ = nullptr;
Austin Schuh87dd3832021-01-01 23:07:31 -0800203
204 // Node index handle to be handed back to the TimeConverter. This lets the
205 // same time converter be used for all the nodes, and the node index
206 // distinguish which one.
207 size_t node_index_ = 0;
208
209 // Converts time by doing nothing to it.
210 class UnityConverter final : public TimeConverter {
211 public:
212 distributed_clock::time_point ToDistributedClock(
Austin Schuh58646e22021-08-23 23:51:46 -0700213 size_t /*node_index*/, logger::BootTimestamp time) override {
214 CHECK_EQ(time.boot, 0u) << ": Reboots unsupported by default.";
215 return distributed_clock::epoch() + time.time.time_since_epoch();
Austin Schuh87dd3832021-01-01 23:07:31 -0800216 }
217
Austin Schuh58646e22021-08-23 23:51:46 -0700218 logger::BootTimestamp FromDistributedClock(
219 size_t /*node_index*/, distributed_clock::time_point time,
220 size_t boot_count) override {
221 CHECK_EQ(boot_count, 0u);
222 return logger::BootTimestamp{
223 .boot = boot_count,
224 .time = monotonic_clock::epoch() + time.time_since_epoch()};
Austin Schuh87dd3832021-01-01 23:07:31 -0800225 }
Austin Schuhb7c8d2a2021-07-19 19:22:12 -0700226
227 void ObserveTimePassed(distributed_clock::time_point /*time*/) override {}
Austin Schuh58646e22021-08-23 23:51:46 -0700228
229 UUID boot_uuid(size_t /*node_index*/, size_t boot_count) override {
230 CHECK_EQ(boot_count, 0u);
231 return uuid_;
232 }
233
234 private:
235 const UUID uuid_ = UUID::Random();
Austin Schuh87dd3832021-01-01 23:07:31 -0800236 };
237
238 UnityConverter unity_converter_;
239
240 TimeConverter *converter_ = &unity_converter_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700241};
242
Austin Schuh8bd96322020-02-13 21:18:22 -0800243// We need a heap of heaps...
244//
245// Events in a node have a very well defined progression of time. It is linear
246// and well represented by the monotonic clock.
247//
248// Events across nodes don't follow this well. Time skews between the two nodes
249// all the time. We also don't know the function ahead of time which converts
250// from each node's monotonic clock to the distributed clock (our unified base
251// time which is likely the average time between nodes).
252//
253// This pushes us towards merge sort. Sorting each node's events with a heap
254// like we used to be doing, and then sorting each of those nodes independently.
255class EventSchedulerScheduler {
256 public:
257 // Adds an event scheduler to the list.
258 void AddEventScheduler(EventScheduler *scheduler);
259
260 // Runs until there are no more events or Exit is called.
261 void Run();
262
263 // Stops running.
264 void Exit() { is_running_ = false; }
265
266 bool is_running() const { return is_running_; }
267
268 // Runs for a duration on the distributed clock. Time on the distributed
269 // clock should be very representative of time on each node, but won't be
270 // exactly the same.
271 void RunFor(distributed_clock::duration duration);
272
273 // Returns the current distributed time.
274 distributed_clock::time_point distributed_now() const { return now_; }
275
Austin Schuh057d29f2021-08-21 23:05:15 -0700276 void RunOnStartup() {
277 CHECK(!is_running_);
278 for (EventScheduler *scheduler : schedulers_) {
279 scheduler->RunOnStartup();
280 }
Austin Schuh58646e22021-08-23 23:51:46 -0700281 for (EventScheduler *scheduler : schedulers_) {
282 scheduler->RunStarted();
283 }
284 }
285
286 void SetTimeConverter(TimeConverter *time_converter) {
287 time_converter->set_reboot_found(
288 [this](distributed_clock::time_point reboot_time,
289 const std::vector<logger::BootTimestamp> &node_times) {
290 if (!reboots_.empty()) {
291 CHECK_GT(reboot_time, std::get<0>(reboots_.back()));
292 }
293 reboots_.emplace_back(reboot_time, node_times);
294 });
Austin Schuh057d29f2021-08-21 23:05:15 -0700295 }
296
Austin Schuh8bd96322020-02-13 21:18:22 -0800297 private:
298 // Handles running the OnRun functions.
299 void RunOnRun() {
300 CHECK(!is_running_);
301 is_running_ = true;
302 for (EventScheduler *scheduler : schedulers_) {
303 scheduler->RunOnRun();
304 }
305 }
306
Austin Schuh58646e22021-08-23 23:51:46 -0700307 void Reboot();
308
Austin Schuh8bd96322020-02-13 21:18:22 -0800309 // Returns the next event time and scheduler on which to run it.
310 std::tuple<distributed_clock::time_point, EventScheduler *> OldestEvent();
311
312 // True if we are running.
313 bool is_running_ = false;
314 // The current time.
315 distributed_clock::time_point now_ = distributed_clock::epoch();
316 // List of schedulers to run in sync.
317 std::vector<EventScheduler *> schedulers_;
Austin Schuh58646e22021-08-23 23:51:46 -0700318
319 // List of when to reboot each node.
320 std::vector<std::tuple<distributed_clock::time_point,
321 std::vector<logger::BootTimestamp>>>
322 reboots_;
Austin Schuh8bd96322020-02-13 21:18:22 -0800323};
324
Austin Schuh58646e22021-08-23 23:51:46 -0700325inline distributed_clock::time_point EventScheduler::distributed_now() const {
326 return scheduler_scheduler_->distributed_now();
327}
Austin Schuh8bd96322020-02-13 21:18:22 -0800328inline monotonic_clock::time_point EventScheduler::monotonic_now() const {
Austin Schuh58646e22021-08-23 23:51:46 -0700329 const logger::BootTimestamp t =
330 FromDistributedClock(scheduler_scheduler_->distributed_now());
331 CHECK_EQ(t.boot, boot_count_) << ": " << " " << t << " d "
332 << scheduler_scheduler_->distributed_now();
333 return t.time;
Austin Schuh8bd96322020-02-13 21:18:22 -0800334}
335
336inline bool EventScheduler::is_running() const {
337 return scheduler_scheduler_->is_running();
338}
339
Alex Perrycb7da4b2019-08-28 19:35:56 -0700340} // namespace aos
341
342#endif // AOS_EVENTS_EVENT_SCHEDULER_H_