Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EVENT_LOOP_H_ |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #define AOS_EVENTS_EVENT_LOOP_H_ |
| 4 | |
| 5 | #include <atomic> |
| 6 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 7 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include "aos/configuration.h" |
| 10 | #include "aos/configuration_generated.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 11 | #include "aos/events/event_loop_event.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 12 | #include "aos/events/event_loop_generated.h" |
| 13 | #include "aos/events/timing_statistics.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 14 | #include "aos/flatbuffers.h" |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 15 | #include "aos/ipc_lib/data_alignment.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | #include "aos/json_to_flatbuffer.h" |
| 17 | #include "aos/time/time.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 18 | #include "aos/util/phased_loop.h" |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 19 | |
| 20 | #include "absl/container/btree_set.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | #include "flatbuffers/flatbuffers.h" |
| 22 | #include "glog/logging.h" |
| 23 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 24 | DECLARE_bool(timing_reports); |
| 25 | DECLARE_int32(timing_report_ms); |
| 26 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | namespace aos { |
| 28 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 29 | class EventLoop; |
| 30 | class WatcherState; |
| 31 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 32 | // Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context |
| 33 | // about the current message. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | struct Context { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 35 | // Time that the message was sent on this node, or the timer was triggered. |
| 36 | monotonic_clock::time_point monotonic_event_time; |
| 37 | // Realtime the message was sent on this node. This is set to min_time for |
| 38 | // Timers and PhasedLoops. |
| 39 | realtime_clock::time_point realtime_event_time; |
| 40 | |
| 41 | // For a single-node configuration, these two are identical to *_event_time. |
| 42 | // In a multinode configuration, these are the times that the message was |
| 43 | // sent on the original node. |
| 44 | monotonic_clock::time_point monotonic_remote_time; |
| 45 | realtime_clock::time_point realtime_remote_time; |
| 46 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 47 | // The rest are only valid for Watchers and Fetchers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 48 | // Index in the queue. |
| 49 | uint32_t queue_index; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 50 | // Index into the remote queue. Useful to determine if data was lost. In a |
| 51 | // single-node configuration, this will match queue_index. |
| 52 | uint32_t remote_queue_index; |
| 53 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 54 | // Size of the data sent. |
| 55 | size_t size; |
| 56 | // Pointer to the data. |
| 57 | void *data; |
| 58 | }; |
| 59 | |
| 60 | // Raw version of fetcher. Contains a local variable that the fetcher will |
| 61 | // update. This is used for reflection and as an interface to implement typed |
| 62 | // fetchers. |
| 63 | class RawFetcher { |
| 64 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 65 | RawFetcher(EventLoop *event_loop, const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | RawFetcher(const RawFetcher &) = delete; |
| 67 | RawFetcher &operator=(const RawFetcher &) = delete; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 68 | virtual ~RawFetcher(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 69 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 70 | // Fetches the next message in the queue without blocking. Returns true if |
| 71 | // there was a new message and we got it. |
| 72 | bool FetchNext(); |
| 73 | |
| 74 | // Fetches the latest message without blocking. |
| 75 | bool Fetch(); |
| 76 | |
| 77 | // Returns the channel this fetcher uses. |
| 78 | const Channel *channel() const { return channel_; } |
| 79 | // Returns the context for the current message. |
| 80 | const Context &context() const { return context_; } |
| 81 | |
| 82 | protected: |
| 83 | EventLoop *event_loop() { return event_loop_; } |
| 84 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 85 | Context context_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 86 | |
| 87 | private: |
| 88 | friend class EventLoop; |
| 89 | // Implementation |
| 90 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0; |
| 91 | virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0; |
| 92 | |
| 93 | EventLoop *event_loop_; |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 94 | const Channel *channel_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 95 | |
| 96 | internal::RawFetcherTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | // Raw version of sender. Sends a block of data. This is used for reflection |
| 100 | // and as a building block to implement typed senders. |
| 101 | class RawSender { |
| 102 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 103 | RawSender(EventLoop *event_loop, const Channel *channel); |
| 104 | RawSender(const RawSender &) = delete; |
| 105 | RawSender &operator=(const RawSender &) = delete; |
| 106 | |
| 107 | virtual ~RawSender(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 108 | |
| 109 | // Sends a message without copying it. The users starts by copying up to |
| 110 | // size() bytes into the data backed by data(). They then call Send to send. |
| 111 | // Returns true on a successful send. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 112 | // If provided, monotonic_remote_time, realtime_remote_time, and |
| 113 | // remote_queue_index are attached to the message and are available in the |
| 114 | // context on the read side. If they are not populated, the read side will |
| 115 | // get the sent times instead. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 116 | virtual void *data() = 0; |
| 117 | virtual size_t size() = 0; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 118 | bool Send(size_t size, |
| 119 | aos::monotonic_clock::time_point monotonic_remote_time = |
| 120 | aos::monotonic_clock::min_time, |
| 121 | aos::realtime_clock::time_point realtime_remote_time = |
| 122 | aos::realtime_clock::min_time, |
| 123 | uint32_t remote_queue_index = 0xffffffffu); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 124 | |
| 125 | // Sends a single block of data by copying it. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 126 | // The remote arguments have the same meaning as in Send above. |
| 127 | bool Send(const void *data, size_t size, |
| 128 | aos::monotonic_clock::time_point monotonic_remote_time = |
| 129 | aos::monotonic_clock::min_time, |
| 130 | aos::realtime_clock::time_point realtime_remote_time = |
| 131 | aos::realtime_clock::min_time, |
| 132 | uint32_t remote_queue_index = 0xffffffffu); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 133 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 134 | const Channel *channel() const { return channel_; } |
| 135 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 136 | // Returns the time_points that the last message was sent at. |
| 137 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 138 | return monotonic_sent_time_; |
| 139 | } |
| 140 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 141 | return realtime_sent_time_; |
| 142 | } |
| 143 | // Returns the queue index that this was sent with. |
| 144 | uint32_t sent_queue_index() const { return sent_queue_index_; } |
| 145 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 146 | // Returns the associated flatbuffers-style allocator. This must be |
| 147 | // deallocated before the message is sent. |
| 148 | PreallocatedAllocator *fbb_allocator() { |
| 149 | fbb_allocator_ = PreallocatedAllocator(data(), size()); |
| 150 | return &fbb_allocator_; |
| 151 | } |
| 152 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 153 | protected: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 154 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 155 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 156 | aos::monotonic_clock::time_point monotonic_sent_time_ = |
| 157 | aos::monotonic_clock::min_time; |
| 158 | aos::realtime_clock::time_point realtime_sent_time_ = |
| 159 | aos::realtime_clock::min_time; |
| 160 | uint32_t sent_queue_index_ = 0xffffffff; |
| 161 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 162 | private: |
| 163 | friend class EventLoop; |
| 164 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 165 | virtual bool DoSend(const void *data, size_t size, |
| 166 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 167 | aos::realtime_clock::time_point realtime_remote_time, |
| 168 | uint32_t remote_queue_index) = 0; |
| 169 | virtual bool DoSend(size_t size, |
| 170 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 171 | aos::realtime_clock::time_point realtime_remote_time, |
| 172 | uint32_t remote_queue_index) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 173 | |
| 174 | EventLoop *event_loop_; |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 175 | const Channel *channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 177 | internal::RawSenderTiming timing_; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 178 | |
| 179 | PreallocatedAllocator fbb_allocator_{nullptr, 0}; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 180 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 181 | |
| 182 | // Fetches the newest message from a channel. |
| 183 | // This provides a polling based interface for channels. |
| 184 | template <typename T> |
| 185 | class Fetcher { |
| 186 | public: |
| 187 | Fetcher() {} |
| 188 | |
| 189 | // Fetches the next message. Returns true if it fetched a new message. This |
| 190 | // method will only return messages sent after the Fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 191 | bool FetchNext() { |
| 192 | const bool result = fetcher_->FetchNext(); |
| 193 | if (result) { |
| 194 | CheckChannelDataAlignment(fetcher_->context().data, |
| 195 | fetcher_->context().size); |
| 196 | } |
| 197 | return result; |
| 198 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 199 | |
| 200 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 201 | // This will return the latest message regardless of if it was sent before or |
| 202 | // after the fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 203 | bool Fetch() { |
| 204 | const bool result = fetcher_->Fetch(); |
| 205 | if (result) { |
| 206 | CheckChannelDataAlignment(fetcher_->context().data, |
| 207 | fetcher_->context().size); |
| 208 | } |
| 209 | return result; |
| 210 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 211 | |
| 212 | // Returns a pointer to the contained flatbuffer, or nullptr if there is no |
| 213 | // available message. |
| 214 | const T *get() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 215 | return fetcher_->context().data != nullptr |
| 216 | ? flatbuffers::GetRoot<T>( |
| 217 | reinterpret_cast<const char *>(fetcher_->context().data)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 218 | : nullptr; |
| 219 | } |
| 220 | |
| 221 | // Returns the context holding timestamps and other metadata about the |
| 222 | // message. |
| 223 | const Context &context() const { return fetcher_->context(); } |
| 224 | |
| 225 | const T &operator*() const { return *get(); } |
| 226 | const T *operator->() const { return get(); } |
| 227 | |
| 228 | private: |
| 229 | friend class EventLoop; |
| 230 | Fetcher(::std::unique_ptr<RawFetcher> fetcher) |
| 231 | : fetcher_(::std::move(fetcher)) {} |
| 232 | ::std::unique_ptr<RawFetcher> fetcher_; |
| 233 | }; |
| 234 | |
| 235 | // Sends messages to a channel. |
| 236 | template <typename T> |
| 237 | class Sender { |
| 238 | public: |
| 239 | Sender() {} |
| 240 | |
| 241 | // Represents a single message about to be sent to the queue. |
| 242 | // The lifecycle goes: |
| 243 | // |
| 244 | // Builder builder = sender.MakeBuilder(); |
| 245 | // T::Builder t_builder = builder.MakeBuilder<T>(); |
| 246 | // Populate(&t_builder); |
| 247 | // builder.Send(t_builder.Finish()); |
| 248 | class Builder { |
| 249 | public: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 250 | Builder(RawSender *sender, PreallocatedAllocator *allocator) |
| 251 | : fbb_(allocator->size(), allocator), sender_(sender) { |
| 252 | CheckChannelDataAlignment(allocator->data(), allocator->size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 253 | fbb_.ForceDefaults(1); |
| 254 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 255 | Builder() {} |
| 256 | Builder(const Builder &) = delete; |
| 257 | Builder(Builder &&) = default; |
| 258 | |
| 259 | Builder &operator=(const Builder &) = delete; |
| 260 | Builder &operator=(Builder &&) = default; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 261 | |
| 262 | flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; } |
| 263 | |
| 264 | template <typename T2> |
| 265 | typename T2::Builder MakeBuilder() { |
| 266 | return typename T2::Builder(fbb_); |
| 267 | } |
| 268 | |
| 269 | bool Send(flatbuffers::Offset<T> offset) { |
| 270 | fbb_.Finish(offset); |
| 271 | return sender_->Send(fbb_.GetSize()); |
| 272 | } |
| 273 | |
| 274 | // CHECKs that this message was sent. |
| 275 | void CheckSent() { fbb_.Finished(); } |
| 276 | |
| 277 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 278 | flatbuffers::FlatBufferBuilder fbb_; |
| 279 | RawSender *sender_; |
| 280 | }; |
| 281 | |
| 282 | // Constructs an above builder. |
| 283 | Builder MakeBuilder(); |
| 284 | |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 285 | // Sends a prebuilt flatbuffer. |
| 286 | bool Send(const Flatbuffer<T> &flatbuffer); |
| 287 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 288 | // Returns the name of the underlying queue. |
Austin Schuh | 1e86947 | 2019-12-01 13:36:10 -0800 | [diff] [blame] | 289 | const Channel *channel() const { return sender_->channel(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 290 | |
| 291 | private: |
| 292 | friend class EventLoop; |
| 293 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 294 | std::unique_ptr<RawSender> sender_; |
| 295 | }; |
| 296 | |
| 297 | // Interface for timers |
| 298 | class TimerHandler { |
| 299 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 300 | virtual ~TimerHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 301 | |
| 302 | // Timer should sleep until base, base + offset, base + offset * 2, ... |
| 303 | // If repeat_offset isn't set, the timer only expires once. |
| 304 | virtual void Setup(monotonic_clock::time_point base, |
| 305 | monotonic_clock::duration repeat_offset = |
| 306 | ::aos::monotonic_clock::zero()) = 0; |
| 307 | |
| 308 | // Stop future calls to callback(). |
| 309 | virtual void Disable() = 0; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 310 | |
| 311 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 312 | // name in the timing report. |
| 313 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 314 | const std::string_view name() const { return name_; } |
| 315 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 316 | protected: |
| 317 | TimerHandler(EventLoop *event_loop, std::function<void()> fn); |
| 318 | |
| 319 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 320 | monotonic_clock::time_point event_time); |
| 321 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 322 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 323 | friend class EventLoop; |
| 324 | |
| 325 | EventLoop *event_loop_; |
| 326 | // The function to call when Call is called. |
| 327 | std::function<void()> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 328 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 329 | |
| 330 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 331 | }; |
| 332 | |
| 333 | // Interface for phased loops. They are built on timers. |
| 334 | class PhasedLoopHandler { |
| 335 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 336 | virtual ~PhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 337 | |
| 338 | // Sets the interval and offset. Any changes to interval and offset only take |
| 339 | // effect when the handler finishes running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 340 | void set_interval_and_offset(const monotonic_clock::duration interval, |
| 341 | const monotonic_clock::duration offset) { |
| 342 | phased_loop_.set_interval_and_offset(interval, offset); |
| 343 | } |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 344 | |
| 345 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 346 | // name in the timing report. |
| 347 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 348 | const std::string_view name() const { return name_; } |
| 349 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 350 | protected: |
| 351 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 352 | std::function<void(monotonic_clock::time_point)> schedule); |
| 353 | |
| 354 | PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn, |
| 355 | const monotonic_clock::duration interval, |
| 356 | const monotonic_clock::duration offset); |
| 357 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 358 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 359 | friend class EventLoop; |
| 360 | |
| 361 | void Reschedule(std::function<void(monotonic_clock::time_point)> schedule, |
| 362 | monotonic_clock::time_point monotonic_now) { |
| 363 | cycles_elapsed_ += phased_loop_.Iterate(monotonic_now); |
| 364 | schedule(phased_loop_.sleep_time()); |
| 365 | } |
| 366 | |
| 367 | virtual void Schedule(monotonic_clock::time_point sleep_time) = 0; |
| 368 | |
| 369 | EventLoop *event_loop_; |
| 370 | std::function<void(int)> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 371 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 372 | time::PhasedLoop phased_loop_; |
| 373 | |
| 374 | int cycles_elapsed_ = 0; |
| 375 | |
| 376 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 377 | }; |
| 378 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 379 | class EventLoop { |
| 380 | public: |
| 381 | EventLoop(const Configuration *configuration) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 382 | : timing_report_(flatbuffers::DetachedBuffer()), |
| 383 | configuration_(configuration) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 384 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 385 | virtual ~EventLoop(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 386 | |
| 387 | // Current time. |
| 388 | virtual monotonic_clock::time_point monotonic_now() = 0; |
| 389 | virtual realtime_clock::time_point realtime_now() = 0; |
| 390 | |
| 391 | // Note, it is supported to create: |
| 392 | // multiple fetchers, and (one sender or one watcher) per <name, type> |
| 393 | // tuple. |
| 394 | |
| 395 | // Makes a class that will always fetch the most recent value |
| 396 | // sent to the provided channel. |
| 397 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 398 | Fetcher<T> MakeFetcher(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 399 | const Channel *channel = |
| 400 | configuration::GetChannel(configuration_, channel_name, |
| 401 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 402 | CHECK(channel != nullptr) |
| 403 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 404 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 405 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 406 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 407 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 408 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 409 | << "\" } is not able to be fetched on this node. Check your " |
| 410 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 413 | return Fetcher<T>(MakeRawFetcher(channel)); |
| 414 | } |
| 415 | |
| 416 | // Makes class that allows constructing and sending messages to |
| 417 | // the provided channel. |
| 418 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 419 | Sender<T> MakeSender(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 420 | const Channel *channel = |
| 421 | configuration::GetChannel(configuration_, channel_name, |
| 422 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 423 | CHECK(channel != nullptr) |
| 424 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 425 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 426 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 427 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 428 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 429 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 430 | << "\" } is not able to be sent on this node. Check your " |
| 431 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 432 | } |
| 433 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 434 | return Sender<T>(MakeRawSender(channel)); |
| 435 | } |
| 436 | |
| 437 | // This will watch messages sent to the provided channel. |
| 438 | // |
| 439 | // Watch is a functor that have a call signature like so: |
| 440 | // void Event(const MessageType& type); |
| 441 | // |
| 442 | // TODO(parker): Need to support ::std::bind. For now, use lambdas. |
| 443 | // TODO(austin): Do we need a functor? Or is a std::function good enough? |
| 444 | template <typename Watch> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 445 | void MakeWatcher(const std::string_view name, Watch &&w); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 446 | |
| 447 | // The passed in function will be called when the event loop starts. |
| 448 | // Use this to run code once the thread goes into "real-time-mode", |
| 449 | virtual void OnRun(::std::function<void()> on_run) = 0; |
| 450 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 451 | // Gets the name of the event loop. This is the application name. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 452 | virtual const std::string_view name() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 454 | // Returns the node that this event loop is running on. Returns nullptr if we |
| 455 | // are running in single-node mode. |
| 456 | virtual const Node *node() const = 0; |
| 457 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 458 | // Creates a timer that executes callback when the timer expires |
| 459 | // Returns a TimerHandle for configuration of the timer |
| 460 | virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0; |
| 461 | |
| 462 | // Creates a timer that executes callback periodically at the specified |
| 463 | // interval and offset. Returns a PhasedLoopHandler for interacting with the |
| 464 | // timer. |
| 465 | virtual PhasedLoopHandler *AddPhasedLoop( |
| 466 | ::std::function<void(int)> callback, |
| 467 | const monotonic_clock::duration interval, |
| 468 | const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0; |
| 469 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 470 | // TODO(austin): OnExit for cleanup. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 471 | |
| 472 | // Threadsafe. |
| 473 | bool is_running() const { return is_running_.load(); } |
| 474 | |
| 475 | // Sets the scheduler priority to run the event loop at. This may not be |
| 476 | // called after we go into "real-time-mode". |
| 477 | virtual void SetRuntimeRealtimePriority(int priority) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 478 | virtual int priority() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 479 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 480 | // Fetches new messages from the provided channel (path, type). |
| 481 | // |
| 482 | // Note: this channel must be a member of the exact configuration object this |
| 483 | // was built with. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 484 | virtual std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 485 | const Channel *channel) = 0; |
| 486 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 487 | // Watches channel (name, type) for new messages. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 488 | virtual void MakeRawWatcher( |
| 489 | const Channel *channel, |
| 490 | std::function<void(const Context &context, const void *message)> |
| 491 | watcher) = 0; |
| 492 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 493 | // Creates a raw sender for the provided channel. This is used for reflection |
| 494 | // based sending. |
| 495 | // Note: this ignores any node constraints. Ignore at your own peril. |
| 496 | virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0; |
| 497 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 498 | // Returns the context for the current callback. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 499 | const Context &context() const { return context_; } |
| 500 | |
| 501 | // Returns the configuration that this event loop was built with. |
| 502 | const Configuration *configuration() const { return configuration_; } |
| 503 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 504 | // Prevents the event loop from sending a timing report. |
| 505 | void SkipTimingReport() { skip_timing_report_ = true; } |
| 506 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 507 | protected: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 508 | // Sets the name of the event loop. This is the application name. |
| 509 | virtual void set_name(const std::string_view name) = 0; |
| 510 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 511 | void set_is_running(bool value) { is_running_.store(value); } |
| 512 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 513 | // Validates that channel exists inside configuration_ and finds its index. |
| 514 | int ChannelIndex(const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 515 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 516 | // Returns the state for the watcher on the corresponding channel. This |
| 517 | // watcher must exist before calling this. |
| 518 | WatcherState *GetWatcherState(const Channel *channel); |
| 519 | |
| 520 | // Returns a Sender's protected RawSender |
| 521 | template <typename T> |
| 522 | static RawSender *GetRawSender(aos::Sender<T> *sender) { |
| 523 | return sender->sender_.get(); |
| 524 | } |
| 525 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 526 | // Context available for watchers, timers, and phased loops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 527 | Context context_; |
| 528 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 529 | friend class RawSender; |
| 530 | friend class TimerHandler; |
| 531 | friend class RawFetcher; |
| 532 | friend class PhasedLoopHandler; |
| 533 | friend class WatcherState; |
| 534 | |
| 535 | // Methods used to implement timing reports. |
| 536 | void NewSender(RawSender *sender); |
| 537 | void DeleteSender(RawSender *sender); |
| 538 | TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer); |
| 539 | PhasedLoopHandler *NewPhasedLoop( |
| 540 | std::unique_ptr<PhasedLoopHandler> phased_loop); |
| 541 | void NewFetcher(RawFetcher *fetcher); |
| 542 | void DeleteFetcher(RawFetcher *fetcher); |
| 543 | WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher); |
| 544 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 545 | // Tracks that we have a (single) watcher on the given channel. |
| 546 | void TakeWatcher(const Channel *channel); |
| 547 | // Tracks that we have at least one sender on the given channel. |
| 548 | void TakeSender(const Channel *channel); |
| 549 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 550 | std::vector<RawSender *> senders_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 551 | std::vector<RawFetcher *> fetchers_; |
| 552 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 553 | std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 554 | std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 555 | std::vector<std::unique_ptr<WatcherState>> watchers_; |
| 556 | |
| 557 | void SendTimingReport(); |
| 558 | void UpdateTimingReport(); |
| 559 | void MaybeScheduleTimingReports(); |
| 560 | |
| 561 | std::unique_ptr<RawSender> timing_report_sender_; |
| 562 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 563 | // Tracks which event sources (timers and watchers) have data, and which |
| 564 | // don't. Added events may not change their event_time(). |
| 565 | // TODO(austin): Test case 1: timer triggers at t1, handler takes until after |
| 566 | // t2 to run, t2 should then be picked up without a context switch. |
| 567 | void AddEvent(EventLoopEvent *event); |
| 568 | void RemoveEvent(EventLoopEvent *event); |
| 569 | size_t EventCount() const { return events_.size(); } |
| 570 | EventLoopEvent *PopEvent(); |
| 571 | EventLoopEvent *PeekEvent() { return events_.front(); } |
| 572 | void ReserveEvents(); |
| 573 | |
| 574 | std::vector<EventLoopEvent *> events_; |
| 575 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 576 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 577 | virtual pid_t GetTid() = 0; |
| 578 | |
| 579 | FlatbufferDetachedBuffer<timing::Report> timing_report_; |
| 580 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 581 | ::std::atomic<bool> is_running_{false}; |
| 582 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 583 | const Configuration *configuration_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 584 | |
| 585 | // If true, don't send out timing reports. |
| 586 | bool skip_timing_report_ = false; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 587 | |
| 588 | absl::btree_set<const Channel *> taken_watchers_, taken_senders_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 589 | }; |
| 590 | |
| 591 | } // namespace aos |
| 592 | |
| 593 | #include "aos/events/event_loop_tmpl.h" |
| 594 | |
| 595 | #endif // AOS_EVENTS_EVENT_LOOP_H |