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 | |
| 11 | #include "aos/events/event_loop.h" |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 12 | #include "aos/logging/implementations.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "aos/time/time.h" |
| 14 | #include "glog/logging.h" |
| 15 | |
| 16 | namespace aos { |
| 17 | |
Austin Schuh | ac0771c | 2020-01-07 18:36:30 -0800 | [diff] [blame] | 18 | // This clock is the basis for distributed time. It is used to synchronize time |
| 19 | // between multiple nodes. This is a new type so conversions to and from the |
| 20 | // monotonic and realtime clocks aren't implicit. |
| 21 | class distributed_clock { |
| 22 | public: |
| 23 | typedef ::std::chrono::nanoseconds::rep rep; |
| 24 | typedef ::std::chrono::nanoseconds::period period; |
| 25 | typedef ::std::chrono::nanoseconds duration; |
| 26 | typedef ::std::chrono::time_point<distributed_clock> time_point; |
| 27 | |
| 28 | // This clock is the base clock for the simulation and everything is synced to |
| 29 | // it. It never jumps. |
| 30 | static constexpr bool is_steady = true; |
| 31 | |
| 32 | // Returns the epoch (0). |
| 33 | static constexpr time_point epoch() { return time_point(zero()); } |
| 34 | |
| 35 | static constexpr duration zero() { return duration(0); } |
| 36 | |
| 37 | static constexpr time_point min_time{ |
| 38 | time_point(duration(::std::numeric_limits<duration::rep>::min()))}; |
| 39 | static constexpr time_point max_time{ |
| 40 | time_point(duration(::std::numeric_limits<duration::rep>::max()))}; |
| 41 | }; |
| 42 | |
| 43 | std::ostream &operator<<(std::ostream &stream, |
| 44 | const aos::distributed_clock::time_point &now); |
| 45 | |
Austin Schuh | a9abc03 | 2021-01-01 16:46:19 -0800 | [diff] [blame^] | 46 | // Interface to handle converting time on a node to and from the distributed |
| 47 | // clock accurately. |
| 48 | class TimeConverter { |
| 49 | public: |
| 50 | virtual ~TimeConverter() {} |
| 51 | |
| 52 | // Converts a time to the distributed clock for scheduling and cross-node |
| 53 | // time measurement. |
| 54 | virtual distributed_clock::time_point ToDistributedClock( |
| 55 | size_t node_index, monotonic_clock::time_point time) = 0; |
| 56 | |
| 57 | // Takes the distributed time and converts it to the monotonic clock for this |
| 58 | // node. |
| 59 | virtual monotonic_clock::time_point FromDistributedClock( |
| 60 | size_t node_index, distributed_clock::time_point time) = 0; |
| 61 | }; |
| 62 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 63 | class EventSchedulerScheduler; |
| 64 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | class EventScheduler { |
| 66 | public: |
| 67 | using ChannelType = |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 68 | std::multimap<monotonic_clock::time_point, std::function<void()>>; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 69 | using Token = ChannelType::iterator; |
| 70 | |
| 71 | // Schedule an event with a callback function |
| 72 | // Returns an iterator to the event |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 73 | Token Schedule(monotonic_clock::time_point time, |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | std::function<void()> callback); |
| 75 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 76 | // Schedules a callback when the event scheduler starts. |
| 77 | void ScheduleOnRun(std::function<void()> callback) { |
| 78 | on_run_.emplace_back(std::move(callback)); |
| 79 | } |
| 80 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | Token InvalidToken() { return events_list_.end(); } |
| 82 | |
| 83 | // Deschedule an event by its iterator |
| 84 | void Deschedule(Token token); |
| 85 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 86 | // Runs the OnRun callbacks. |
| 87 | void RunOnRun(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 89 | // Returns true if events are being handled. |
| 90 | inline bool is_running() const; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 92 | // Returns the timestamp of the next event to trigger. |
| 93 | aos::monotonic_clock::time_point OldestEvent(); |
| 94 | // Handles the next event. |
| 95 | void CallOldestEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 96 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 97 | // Converts a time to the distributed clock for scheduling and cross-node time |
| 98 | // measurement. |
| 99 | distributed_clock::time_point ToDistributedClock( |
| 100 | monotonic_clock::time_point time) const { |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 101 | return distributed_clock::epoch() + |
| 102 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 103 | (time.time_since_epoch() - distributed_offset_) / |
| 104 | distributed_slope_); |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Takes the distributed time and converts it to the monotonic clock for this |
| 108 | // node. |
| 109 | monotonic_clock::time_point FromDistributedClock( |
| 110 | distributed_clock::time_point time) const { |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 111 | return monotonic_clock::epoch() + |
| 112 | std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 113 | time.time_since_epoch() * distributed_slope_) + |
| 114 | distributed_offset_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Returns the current monotonic time on this node calculated from the |
| 118 | // distributed clock. |
| 119 | inline monotonic_clock::time_point monotonic_now() const; |
| 120 | |
| 121 | // Sets the offset between the distributed and monotonic clock. |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 122 | // monotonic = distributed * slope + offset; |
| 123 | void SetDistributedOffset(std::chrono::nanoseconds distributed_offset, |
| 124 | double distributed_slope) { |
| 125 | // TODO(austin): Use a starting point to improve precision. |
| 126 | // TODO(austin): Make slope be the slope of the offset, not the input, |
| 127 | // throught the calculation process. |
| 128 | distributed_offset_ = distributed_offset; |
| 129 | distributed_slope_ = distributed_slope; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 130 | |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 131 | // Once we update the offset, now isn't going to be valid anymore. |
| 132 | // TODO(austin): Probably should instead use the piecewise linear function |
| 133 | // and evaluate it correctly. |
| 134 | monotonic_now_valid_ = false; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 135 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 136 | |
| 137 | private: |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 138 | friend class EventSchedulerScheduler; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 139 | // Current execution time. |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 140 | bool monotonic_now_valid_ = false; |
| 141 | monotonic_clock::time_point monotonic_now_ = monotonic_clock::epoch(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 142 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 143 | // Offset to the distributed clock. |
| 144 | // distributed = monotonic + offset; |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 145 | std::chrono::nanoseconds distributed_offset_ = std::chrono::seconds(0); |
| 146 | double distributed_slope_ = 1.0; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 147 | |
| 148 | // List of functions to run (once) when running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 149 | std::vector<std::function<void()>> on_run_; |
| 150 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 151 | // Multimap holding times to run functions. These are stored in order, and |
| 152 | // the order is the callback tree. |
| 153 | ChannelType events_list_; |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 154 | |
| 155 | // Pointer to the actual scheduler. |
| 156 | EventSchedulerScheduler *scheduler_scheduler_ = nullptr; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 159 | // We need a heap of heaps... |
| 160 | // |
| 161 | // Events in a node have a very well defined progression of time. It is linear |
| 162 | // and well represented by the monotonic clock. |
| 163 | // |
| 164 | // Events across nodes don't follow this well. Time skews between the two nodes |
| 165 | // all the time. We also don't know the function ahead of time which converts |
| 166 | // from each node's monotonic clock to the distributed clock (our unified base |
| 167 | // time which is likely the average time between nodes). |
| 168 | // |
| 169 | // This pushes us towards merge sort. Sorting each node's events with a heap |
| 170 | // like we used to be doing, and then sorting each of those nodes independently. |
| 171 | class EventSchedulerScheduler { |
| 172 | public: |
| 173 | // Adds an event scheduler to the list. |
| 174 | void AddEventScheduler(EventScheduler *scheduler); |
| 175 | |
| 176 | // Runs until there are no more events or Exit is called. |
| 177 | void Run(); |
| 178 | |
| 179 | // Stops running. |
| 180 | void Exit() { is_running_ = false; } |
| 181 | |
| 182 | bool is_running() const { return is_running_; } |
| 183 | |
| 184 | // Runs for a duration on the distributed clock. Time on the distributed |
| 185 | // clock should be very representative of time on each node, but won't be |
| 186 | // exactly the same. |
| 187 | void RunFor(distributed_clock::duration duration); |
| 188 | |
| 189 | // Returns the current distributed time. |
| 190 | distributed_clock::time_point distributed_now() const { return now_; } |
| 191 | |
| 192 | private: |
| 193 | // Handles running the OnRun functions. |
| 194 | void RunOnRun() { |
| 195 | CHECK(!is_running_); |
| 196 | is_running_ = true; |
| 197 | for (EventScheduler *scheduler : schedulers_) { |
| 198 | scheduler->RunOnRun(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Returns the next event time and scheduler on which to run it. |
| 203 | std::tuple<distributed_clock::time_point, EventScheduler *> OldestEvent(); |
| 204 | |
| 205 | // True if we are running. |
| 206 | bool is_running_ = false; |
| 207 | // The current time. |
| 208 | distributed_clock::time_point now_ = distributed_clock::epoch(); |
| 209 | // List of schedulers to run in sync. |
| 210 | std::vector<EventScheduler *> schedulers_; |
| 211 | }; |
| 212 | |
| 213 | inline monotonic_clock::time_point EventScheduler::monotonic_now() const { |
| 214 | // Make sure we stay in sync. |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 215 | if (monotonic_now_valid_) { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 216 | // We want time to be smooth, so confirm that it doesn't change too much |
| 217 | // while handling an event. |
| 218 | // |
| 219 | // There are 2 sources of error. There are numerical precision and interger |
| 220 | // rounding problems going from the monotonic clock to the distributed clock |
| 221 | // and back again. When we update the time function as well to transition |
| 222 | // line segments, we have a slight jump as well. |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 223 | CHECK_NEAR(monotonic_now_, |
| 224 | FromDistributedClock(scheduler_scheduler_->distributed_now()), |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 225 | std::chrono::nanoseconds(2)); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 226 | return monotonic_now_; |
| 227 | } else { |
| 228 | return FromDistributedClock(scheduler_scheduler_->distributed_now()); |
| 229 | } |
Austin Schuh | 8bd9632 | 2020-02-13 21:18:22 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | inline bool EventScheduler::is_running() const { |
| 233 | return scheduler_scheduler_->is_running(); |
| 234 | } |
| 235 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 236 | } // namespace aos |
| 237 | |
| 238 | #endif // AOS_EVENTS_EVENT_SCHEDULER_H_ |