Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EVENT_LOOP_H_ |
| 2 | #define AOS_EVENTS_EVENT_LOOP_H_ |
| 3 | |
| 4 | #include <atomic> |
| 5 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 6 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/configuration.h" |
| 9 | #include "aos/configuration_generated.h" |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame^] | 10 | #include "aos/events/channel_preallocated_allocator.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. |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame^] | 148 | ChannelPreallocatedAllocator *fbb_allocator() { |
| 149 | fbb_allocator_ = ChannelPreallocatedAllocator( |
| 150 | reinterpret_cast<uint8_t *>(data()), size(), channel()); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 151 | return &fbb_allocator_; |
| 152 | } |
| 153 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 154 | protected: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 155 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 156 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 157 | aos::monotonic_clock::time_point monotonic_sent_time_ = |
| 158 | aos::monotonic_clock::min_time; |
| 159 | aos::realtime_clock::time_point realtime_sent_time_ = |
| 160 | aos::realtime_clock::min_time; |
| 161 | uint32_t sent_queue_index_ = 0xffffffff; |
| 162 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 163 | private: |
| 164 | friend class EventLoop; |
| 165 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 166 | virtual bool DoSend(const void *data, size_t size, |
| 167 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 168 | aos::realtime_clock::time_point realtime_remote_time, |
| 169 | uint32_t remote_queue_index) = 0; |
| 170 | virtual bool DoSend(size_t size, |
| 171 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 172 | aos::realtime_clock::time_point realtime_remote_time, |
| 173 | uint32_t remote_queue_index) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 174 | |
| 175 | EventLoop *event_loop_; |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 176 | const Channel *channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 177 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 178 | internal::RawSenderTiming timing_; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 179 | |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame^] | 180 | ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr}; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 181 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 182 | |
| 183 | // Fetches the newest message from a channel. |
| 184 | // This provides a polling based interface for channels. |
| 185 | template <typename T> |
| 186 | class Fetcher { |
| 187 | public: |
| 188 | Fetcher() {} |
| 189 | |
| 190 | // Fetches the next message. Returns true if it fetched a new message. This |
| 191 | // method will only return messages sent after the Fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 192 | bool FetchNext() { |
| 193 | const bool result = fetcher_->FetchNext(); |
| 194 | if (result) { |
| 195 | CheckChannelDataAlignment(fetcher_->context().data, |
| 196 | fetcher_->context().size); |
| 197 | } |
| 198 | return result; |
| 199 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 200 | |
| 201 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 202 | // This will return the latest message regardless of if it was sent before or |
| 203 | // after the fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 204 | bool Fetch() { |
| 205 | const bool result = fetcher_->Fetch(); |
| 206 | if (result) { |
| 207 | CheckChannelDataAlignment(fetcher_->context().data, |
| 208 | fetcher_->context().size); |
| 209 | } |
| 210 | return result; |
| 211 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 212 | |
| 213 | // Returns a pointer to the contained flatbuffer, or nullptr if there is no |
| 214 | // available message. |
| 215 | const T *get() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 216 | return fetcher_->context().data != nullptr |
| 217 | ? flatbuffers::GetRoot<T>( |
| 218 | reinterpret_cast<const char *>(fetcher_->context().data)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 219 | : nullptr; |
| 220 | } |
| 221 | |
| 222 | // Returns the context holding timestamps and other metadata about the |
| 223 | // message. |
| 224 | const Context &context() const { return fetcher_->context(); } |
| 225 | |
| 226 | const T &operator*() const { return *get(); } |
| 227 | const T *operator->() const { return get(); } |
| 228 | |
| 229 | private: |
| 230 | friend class EventLoop; |
| 231 | Fetcher(::std::unique_ptr<RawFetcher> fetcher) |
| 232 | : fetcher_(::std::move(fetcher)) {} |
| 233 | ::std::unique_ptr<RawFetcher> fetcher_; |
| 234 | }; |
| 235 | |
| 236 | // Sends messages to a channel. |
| 237 | template <typename T> |
| 238 | class Sender { |
| 239 | public: |
| 240 | Sender() {} |
| 241 | |
| 242 | // Represents a single message about to be sent to the queue. |
| 243 | // The lifecycle goes: |
| 244 | // |
| 245 | // Builder builder = sender.MakeBuilder(); |
| 246 | // T::Builder t_builder = builder.MakeBuilder<T>(); |
| 247 | // Populate(&t_builder); |
| 248 | // builder.Send(t_builder.Finish()); |
| 249 | class Builder { |
| 250 | public: |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame^] | 251 | Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator) |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 252 | : fbb_(allocator->size(), allocator), |
| 253 | allocator_(allocator), |
| 254 | sender_(sender) { |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 255 | CheckChannelDataAlignment(allocator->data(), allocator->size()); |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 256 | fbb_.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 257 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 258 | Builder() {} |
| 259 | Builder(const Builder &) = delete; |
| 260 | Builder(Builder &&) = default; |
| 261 | |
| 262 | Builder &operator=(const Builder &) = delete; |
| 263 | Builder &operator=(Builder &&) = default; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 264 | |
| 265 | flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; } |
| 266 | |
| 267 | template <typename T2> |
| 268 | typename T2::Builder MakeBuilder() { |
| 269 | return typename T2::Builder(fbb_); |
| 270 | } |
| 271 | |
| 272 | bool Send(flatbuffers::Offset<T> offset) { |
| 273 | fbb_.Finish(offset); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 274 | const bool result = sender_->Send(fbb_.GetSize()); |
| 275 | // Ensure fbb_ knows it shouldn't access the memory any more. |
| 276 | fbb_ = flatbuffers::FlatBufferBuilder(); |
| 277 | return result; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // CHECKs that this message was sent. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 281 | void CheckSent() { |
| 282 | CHECK(!allocator_->is_allocated()) << ": Message was not sent yet"; |
| 283 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 284 | |
| 285 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 286 | flatbuffers::FlatBufferBuilder fbb_; |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame^] | 287 | ChannelPreallocatedAllocator *allocator_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 288 | RawSender *sender_; |
| 289 | }; |
| 290 | |
| 291 | // Constructs an above builder. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 292 | // |
| 293 | // Only a single one of these may be "alive" for this object at any point in |
| 294 | // time. After calling Send on the result, it is no longer "alive". This means |
| 295 | // that you must manually reset a variable holding the return value (by |
| 296 | // assigning a default-constructed Builder to it) before calling this method |
| 297 | // again to overwrite the value in the variable. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 298 | Builder MakeBuilder(); |
| 299 | |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 300 | // Sends a prebuilt flatbuffer. |
| 301 | bool Send(const Flatbuffer<T> &flatbuffer); |
| 302 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 303 | // Returns the name of the underlying queue. |
Austin Schuh | 1e86947 | 2019-12-01 13:36:10 -0800 | [diff] [blame] | 304 | const Channel *channel() const { return sender_->channel(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 305 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 306 | operator bool() { |
| 307 | return sender_ ? true : false; |
| 308 | } |
| 309 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 310 | // Returns the time_points that the last message was sent at. |
| 311 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 312 | return sender_->monotonic_sent_time(); |
| 313 | } |
| 314 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 315 | return sender_->realtime_sent_time(); |
| 316 | } |
| 317 | // Returns the queue index that this was sent with. |
| 318 | uint32_t sent_queue_index() const { return sender_->sent_queue_index(); } |
| 319 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 320 | private: |
| 321 | friend class EventLoop; |
| 322 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 323 | std::unique_ptr<RawSender> sender_; |
| 324 | }; |
| 325 | |
| 326 | // Interface for timers |
| 327 | class TimerHandler { |
| 328 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 329 | virtual ~TimerHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 330 | |
| 331 | // Timer should sleep until base, base + offset, base + offset * 2, ... |
| 332 | // If repeat_offset isn't set, the timer only expires once. |
| 333 | virtual void Setup(monotonic_clock::time_point base, |
| 334 | monotonic_clock::duration repeat_offset = |
| 335 | ::aos::monotonic_clock::zero()) = 0; |
| 336 | |
| 337 | // Stop future calls to callback(). |
| 338 | virtual void Disable() = 0; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 339 | |
| 340 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 341 | // name in the timing report. |
| 342 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 343 | const std::string_view name() const { return name_; } |
| 344 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 345 | protected: |
| 346 | TimerHandler(EventLoop *event_loop, std::function<void()> fn); |
| 347 | |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 348 | monotonic_clock::time_point Call( |
| 349 | std::function<monotonic_clock::time_point()> get_time, |
| 350 | monotonic_clock::time_point event_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 351 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 352 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 353 | friend class EventLoop; |
| 354 | |
| 355 | EventLoop *event_loop_; |
| 356 | // The function to call when Call is called. |
| 357 | std::function<void()> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 358 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 359 | |
| 360 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 361 | }; |
| 362 | |
| 363 | // Interface for phased loops. They are built on timers. |
| 364 | class PhasedLoopHandler { |
| 365 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 366 | virtual ~PhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | |
| 368 | // Sets the interval and offset. Any changes to interval and offset only take |
| 369 | // effect when the handler finishes running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 370 | void set_interval_and_offset(const monotonic_clock::duration interval, |
| 371 | const monotonic_clock::duration offset) { |
| 372 | phased_loop_.set_interval_and_offset(interval, offset); |
| 373 | } |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 374 | |
| 375 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 376 | // name in the timing report. |
| 377 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 378 | const std::string_view name() const { return name_; } |
| 379 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 380 | protected: |
| 381 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 382 | std::function<void(monotonic_clock::time_point)> schedule); |
| 383 | |
| 384 | PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn, |
| 385 | const monotonic_clock::duration interval, |
| 386 | const monotonic_clock::duration offset); |
| 387 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 388 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 389 | friend class EventLoop; |
| 390 | |
| 391 | void Reschedule(std::function<void(monotonic_clock::time_point)> schedule, |
| 392 | monotonic_clock::time_point monotonic_now) { |
| 393 | cycles_elapsed_ += phased_loop_.Iterate(monotonic_now); |
| 394 | schedule(phased_loop_.sleep_time()); |
| 395 | } |
| 396 | |
| 397 | virtual void Schedule(monotonic_clock::time_point sleep_time) = 0; |
| 398 | |
| 399 | EventLoop *event_loop_; |
| 400 | std::function<void(int)> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 401 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 402 | time::PhasedLoop phased_loop_; |
| 403 | |
| 404 | int cycles_elapsed_ = 0; |
| 405 | |
| 406 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 407 | }; |
| 408 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 409 | class EventLoop { |
| 410 | public: |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 411 | EventLoop(const Configuration *configuration); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 412 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 413 | virtual ~EventLoop(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 414 | |
| 415 | // Current time. |
| 416 | virtual monotonic_clock::time_point monotonic_now() = 0; |
| 417 | virtual realtime_clock::time_point realtime_now() = 0; |
| 418 | |
| 419 | // Note, it is supported to create: |
| 420 | // multiple fetchers, and (one sender or one watcher) per <name, type> |
| 421 | // tuple. |
| 422 | |
| 423 | // Makes a class that will always fetch the most recent value |
| 424 | // sent to the provided channel. |
| 425 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 426 | Fetcher<T> MakeFetcher(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 427 | const Channel *channel = |
| 428 | configuration::GetChannel(configuration_, channel_name, |
| 429 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 430 | CHECK(channel != nullptr) |
| 431 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 432 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 433 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 434 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 435 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 436 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 437 | << "\" } is not able to be fetched on this node. Check your " |
| 438 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 441 | return Fetcher<T>(MakeRawFetcher(channel)); |
| 442 | } |
| 443 | |
| 444 | // Makes class that allows constructing and sending messages to |
| 445 | // the provided channel. |
| 446 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 447 | Sender<T> MakeSender(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 448 | const Channel *channel = |
| 449 | configuration::GetChannel(configuration_, channel_name, |
| 450 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 451 | CHECK(channel != nullptr) |
| 452 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
Austin Schuh | 28fedcb | 2020-02-08 15:59:58 -0800 | [diff] [blame] | 453 | << T::GetFullyQualifiedName() << "\" } not found in config for " |
| 454 | << name() << "."; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 456 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 457 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 458 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 459 | << "\" } is not able to be sent on this node. Check your " |
| 460 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 463 | return Sender<T>(MakeRawSender(channel)); |
| 464 | } |
| 465 | |
| 466 | // This will watch messages sent to the provided channel. |
| 467 | // |
| 468 | // Watch is a functor that have a call signature like so: |
| 469 | // void Event(const MessageType& type); |
| 470 | // |
| 471 | // TODO(parker): Need to support ::std::bind. For now, use lambdas. |
| 472 | // TODO(austin): Do we need a functor? Or is a std::function good enough? |
| 473 | template <typename Watch> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 474 | void MakeWatcher(const std::string_view name, Watch &&w); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 475 | |
| 476 | // The passed in function will be called when the event loop starts. |
| 477 | // Use this to run code once the thread goes into "real-time-mode", |
| 478 | virtual void OnRun(::std::function<void()> on_run) = 0; |
| 479 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 480 | // Gets the name of the event loop. This is the application name. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 481 | virtual const std::string_view name() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 482 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 483 | // Returns the node that this event loop is running on. Returns nullptr if we |
| 484 | // are running in single-node mode. |
| 485 | virtual const Node *node() const = 0; |
| 486 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 487 | // Creates a timer that executes callback when the timer expires |
| 488 | // Returns a TimerHandle for configuration of the timer |
| 489 | virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0; |
| 490 | |
| 491 | // Creates a timer that executes callback periodically at the specified |
| 492 | // interval and offset. Returns a PhasedLoopHandler for interacting with the |
| 493 | // timer. |
| 494 | virtual PhasedLoopHandler *AddPhasedLoop( |
| 495 | ::std::function<void(int)> callback, |
| 496 | const monotonic_clock::duration interval, |
| 497 | const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0; |
| 498 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 499 | // TODO(austin): OnExit for cleanup. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 500 | |
| 501 | // Threadsafe. |
| 502 | bool is_running() const { return is_running_.load(); } |
| 503 | |
| 504 | // Sets the scheduler priority to run the event loop at. This may not be |
| 505 | // called after we go into "real-time-mode". |
| 506 | virtual void SetRuntimeRealtimePriority(int priority) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 507 | virtual int priority() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 508 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 509 | // Fetches new messages from the provided channel (path, type). |
| 510 | // |
| 511 | // Note: this channel must be a member of the exact configuration object this |
| 512 | // was built with. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 513 | virtual std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 514 | const Channel *channel) = 0; |
| 515 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 516 | // Watches channel (name, type) for new messages. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 517 | virtual void MakeRawWatcher( |
| 518 | const Channel *channel, |
| 519 | std::function<void(const Context &context, const void *message)> |
| 520 | watcher) = 0; |
| 521 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 522 | // Creates a raw sender for the provided channel. This is used for reflection |
| 523 | // based sending. |
| 524 | // Note: this ignores any node constraints. Ignore at your own peril. |
| 525 | virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0; |
| 526 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 527 | // Returns the context for the current callback. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 528 | const Context &context() const { return context_; } |
| 529 | |
| 530 | // Returns the configuration that this event loop was built with. |
| 531 | const Configuration *configuration() const { return configuration_; } |
| 532 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 533 | // Prevents the event loop from sending a timing report. |
| 534 | void SkipTimingReport() { skip_timing_report_ = true; } |
| 535 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 536 | // Prevents AOS_LOG being sent to message on /aos |
| 537 | void SkipAosLog() { skip_logger_ = true; } |
| 538 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 539 | protected: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 540 | // Sets the name of the event loop. This is the application name. |
| 541 | virtual void set_name(const std::string_view name) = 0; |
| 542 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 543 | void set_is_running(bool value) { is_running_.store(value); } |
| 544 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 545 | // Validates that channel exists inside configuration_ and finds its index. |
| 546 | int ChannelIndex(const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 547 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 548 | // Returns the state for the watcher on the corresponding channel. This |
| 549 | // watcher must exist before calling this. |
| 550 | WatcherState *GetWatcherState(const Channel *channel); |
| 551 | |
| 552 | // Returns a Sender's protected RawSender |
| 553 | template <typename T> |
| 554 | static RawSender *GetRawSender(aos::Sender<T> *sender) { |
| 555 | return sender->sender_.get(); |
| 556 | } |
| 557 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 558 | // Context available for watchers, timers, and phased loops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 559 | Context context_; |
| 560 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 561 | friend class RawSender; |
| 562 | friend class TimerHandler; |
| 563 | friend class RawFetcher; |
| 564 | friend class PhasedLoopHandler; |
| 565 | friend class WatcherState; |
| 566 | |
| 567 | // Methods used to implement timing reports. |
| 568 | void NewSender(RawSender *sender); |
| 569 | void DeleteSender(RawSender *sender); |
| 570 | TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer); |
| 571 | PhasedLoopHandler *NewPhasedLoop( |
| 572 | std::unique_ptr<PhasedLoopHandler> phased_loop); |
| 573 | void NewFetcher(RawFetcher *fetcher); |
| 574 | void DeleteFetcher(RawFetcher *fetcher); |
| 575 | WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher); |
| 576 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 577 | // Tracks that we have a (single) watcher on the given channel. |
| 578 | void TakeWatcher(const Channel *channel); |
| 579 | // Tracks that we have at least one sender on the given channel. |
| 580 | void TakeSender(const Channel *channel); |
| 581 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 582 | std::vector<RawSender *> senders_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 583 | std::vector<RawFetcher *> fetchers_; |
| 584 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 585 | std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 586 | std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 587 | std::vector<std::unique_ptr<WatcherState>> watchers_; |
| 588 | |
| 589 | void SendTimingReport(); |
| 590 | void UpdateTimingReport(); |
| 591 | void MaybeScheduleTimingReports(); |
| 592 | |
| 593 | std::unique_ptr<RawSender> timing_report_sender_; |
| 594 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 595 | // Tracks which event sources (timers and watchers) have data, and which |
| 596 | // don't. Added events may not change their event_time(). |
| 597 | // TODO(austin): Test case 1: timer triggers at t1, handler takes until after |
| 598 | // t2 to run, t2 should then be picked up without a context switch. |
| 599 | void AddEvent(EventLoopEvent *event); |
| 600 | void RemoveEvent(EventLoopEvent *event); |
| 601 | size_t EventCount() const { return events_.size(); } |
| 602 | EventLoopEvent *PopEvent(); |
| 603 | EventLoopEvent *PeekEvent() { return events_.front(); } |
| 604 | void ReserveEvents(); |
| 605 | |
| 606 | std::vector<EventLoopEvent *> events_; |
| 607 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 608 | // If true, don't send AOS_LOG to /aos |
| 609 | bool skip_logger_ = false; |
| 610 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 611 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 612 | virtual pid_t GetTid() = 0; |
| 613 | |
| 614 | FlatbufferDetachedBuffer<timing::Report> timing_report_; |
| 615 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 616 | ::std::atomic<bool> is_running_{false}; |
| 617 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 618 | const Configuration *configuration_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 619 | |
| 620 | // If true, don't send out timing reports. |
| 621 | bool skip_timing_report_ = false; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 622 | |
| 623 | absl::btree_set<const Channel *> taken_watchers_, taken_senders_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 624 | }; |
| 625 | |
| 626 | } // namespace aos |
| 627 | |
| 628 | #include "aos/events/event_loop_tmpl.h" |
| 629 | |
| 630 | #endif // AOS_EVENTS_EVENT_LOOP_H |