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