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 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 4 | #include <sched.h> |
| 5 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include <atomic> |
| 7 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 8 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | #include "aos/configuration.h" |
| 11 | #include "aos/configuration_generated.h" |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 12 | #include "aos/events/channel_preallocated_allocator.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 13 | #include "aos/events/event_loop_event.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 14 | #include "aos/events/event_loop_generated.h" |
| 15 | #include "aos/events/timing_statistics.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | #include "aos/flatbuffers.h" |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 17 | #include "aos/ftrace.h" |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 18 | #include "aos/ipc_lib/data_alignment.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 19 | #include "aos/json_to_flatbuffer.h" |
| 20 | #include "aos/time/time.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 21 | #include "aos/util/phased_loop.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 22 | #include "aos/uuid.h" |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 23 | |
| 24 | #include "absl/container/btree_set.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | #include "flatbuffers/flatbuffers.h" |
| 26 | #include "glog/logging.h" |
| 27 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 28 | DECLARE_bool(timing_reports); |
| 29 | DECLARE_int32(timing_report_ms); |
| 30 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | namespace aos { |
| 32 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 33 | class EventLoop; |
| 34 | class WatcherState; |
| 35 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 36 | // Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context |
| 37 | // about the current message. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 38 | struct Context { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 39 | // Time that the message was sent on this node, or the timer was triggered. |
| 40 | monotonic_clock::time_point monotonic_event_time; |
| 41 | // Realtime the message was sent on this node. This is set to min_time for |
| 42 | // Timers and PhasedLoops. |
| 43 | realtime_clock::time_point realtime_event_time; |
| 44 | |
| 45 | // For a single-node configuration, these two are identical to *_event_time. |
| 46 | // In a multinode configuration, these are the times that the message was |
| 47 | // sent on the original node. |
| 48 | monotonic_clock::time_point monotonic_remote_time; |
| 49 | realtime_clock::time_point realtime_remote_time; |
| 50 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 51 | // The rest are only valid for Watchers and Fetchers. |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 52 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 53 | // Index in the queue. |
| 54 | uint32_t queue_index; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 55 | // Index into the remote queue. Useful to determine if data was lost. In a |
| 56 | // single-node configuration, this will match queue_index. |
| 57 | uint32_t remote_queue_index; |
| 58 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 59 | // Size of the data sent. |
| 60 | size_t size; |
| 61 | // Pointer to the data. |
Brian Silverman | eaa41d6 | 2020-07-08 19:47:35 -0700 | [diff] [blame] | 62 | const void *data; |
Austin Schuh | 678078e | 2020-08-01 14:30:36 -0700 | [diff] [blame] | 63 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 64 | // Index of the message buffer. This will be in [0, NumberBuffers) on |
| 65 | // read_method=PIN channels, and -1 for other channels. |
| 66 | // |
| 67 | // This only tells you about the underlying storage for this message, not |
| 68 | // anything about its position in the queue. This is only useful for advanced |
| 69 | // zero-copy use cases, on read_method=PIN channels. |
| 70 | // |
| 71 | // This will uniquely identify a message on this channel at a point in time. |
| 72 | // For senders, this point in time is while the sender has the message. With |
| 73 | // read_method==PIN, this point in time includes while the caller has access |
| 74 | // to this context. For other read_methods, this point in time may be before |
| 75 | // the caller has access to this context, which makes this pretty useless. |
| 76 | int buffer_index; |
| 77 | |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 78 | // UUID of the remote node which sent this message, or this node in the case |
| 79 | // of events which are local to this node. |
| 80 | UUID remote_boot_uuid = UUID::Zero(); |
| 81 | |
Austin Schuh | ca75b6a | 2020-12-15 21:12:24 -0800 | [diff] [blame] | 82 | // Efficiently copies the flatbuffer into a FlatbufferVector, allocating |
Austin Schuh | 678078e | 2020-08-01 14:30:36 -0700 | [diff] [blame] | 83 | // memory in the process. It is vital that T matches the type of the |
| 84 | // underlying flatbuffer. |
| 85 | template <typename T> |
| 86 | FlatbufferVector<T> CopyFlatBuffer() const { |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 87 | ResizeableBuffer buffer; |
| 88 | buffer.resize(size); |
| 89 | memcpy(buffer.data(), data, size); |
| 90 | return FlatbufferVector<T>(std::move(buffer)); |
Austin Schuh | 678078e | 2020-08-01 14:30:36 -0700 | [diff] [blame] | 91 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | // Raw version of fetcher. Contains a local variable that the fetcher will |
| 95 | // update. This is used for reflection and as an interface to implement typed |
| 96 | // fetchers. |
| 97 | class RawFetcher { |
| 98 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 99 | RawFetcher(EventLoop *event_loop, const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 100 | RawFetcher(const RawFetcher &) = delete; |
| 101 | RawFetcher &operator=(const RawFetcher &) = delete; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 102 | virtual ~RawFetcher(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 104 | // Fetches the next message in the queue without blocking. Returns true if |
| 105 | // there was a new message and we got it. |
| 106 | bool FetchNext(); |
| 107 | |
| 108 | // Fetches the latest message without blocking. |
| 109 | bool Fetch(); |
| 110 | |
| 111 | // Returns the channel this fetcher uses. |
| 112 | const Channel *channel() const { return channel_; } |
| 113 | // Returns the context for the current message. |
| 114 | const Context &context() const { return context_; } |
| 115 | |
| 116 | protected: |
| 117 | EventLoop *event_loop() { return event_loop_; } |
| 118 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 119 | Context context_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | friend class EventLoop; |
| 123 | // Implementation |
| 124 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0; |
| 125 | virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0; |
| 126 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 127 | EventLoop *const event_loop_; |
| 128 | const Channel *const channel_; |
| 129 | const std::string ftrace_prefix_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 130 | |
| 131 | internal::RawFetcherTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 132 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | // Raw version of sender. Sends a block of data. This is used for reflection |
| 136 | // and as a building block to implement typed senders. |
| 137 | class RawSender { |
| 138 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 139 | RawSender(EventLoop *event_loop, const Channel *channel); |
| 140 | RawSender(const RawSender &) = delete; |
| 141 | RawSender &operator=(const RawSender &) = delete; |
| 142 | |
| 143 | virtual ~RawSender(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 144 | |
| 145 | // Sends a message without copying it. The users starts by copying up to |
| 146 | // size() bytes into the data backed by data(). They then call Send to send. |
| 147 | // Returns true on a successful send. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 148 | // If provided, monotonic_remote_time, realtime_remote_time, and |
| 149 | // remote_queue_index are attached to the message and are available in the |
| 150 | // context on the read side. If they are not populated, the read side will |
| 151 | // get the sent times instead. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 152 | virtual void *data() = 0; |
| 153 | virtual size_t size() = 0; |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 154 | bool Send(size_t size); |
| 155 | bool Send(size_t size, monotonic_clock::time_point monotonic_remote_time, |
| 156 | realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 157 | uint32_t remote_queue_index, const UUID &remote_boot_uuid); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 158 | |
| 159 | // Sends a single block of data by copying it. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 160 | // The remote arguments have the same meaning as in Send above. |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 161 | bool Send(const void *data, size_t size); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 162 | bool Send(const void *data, size_t size, |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 163 | monotonic_clock::time_point monotonic_remote_time, |
| 164 | realtime_clock::time_point realtime_remote_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 165 | uint32_t remote_queue_index, const UUID &remote_boot_uuid); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 166 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 167 | const Channel *channel() const { return channel_; } |
| 168 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 169 | // Returns the time_points that the last message was sent at. |
| 170 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 171 | return monotonic_sent_time_; |
| 172 | } |
| 173 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 174 | return realtime_sent_time_; |
| 175 | } |
| 176 | // Returns the queue index that this was sent with. |
| 177 | uint32_t sent_queue_index() const { return sent_queue_index_; } |
| 178 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 179 | // Returns the associated flatbuffers-style allocator. This must be |
| 180 | // deallocated before the message is sent. |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 181 | ChannelPreallocatedAllocator *fbb_allocator() { |
| 182 | fbb_allocator_ = ChannelPreallocatedAllocator( |
| 183 | reinterpret_cast<uint8_t *>(data()), size(), channel()); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 184 | return &fbb_allocator_; |
| 185 | } |
| 186 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 187 | // Index of the buffer which is currently exposed by data() and the various |
| 188 | // other accessors. This is the message the caller should be filling out. |
| 189 | virtual int buffer_index() = 0; |
| 190 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | protected: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 192 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 193 | |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 194 | monotonic_clock::time_point monotonic_sent_time_ = monotonic_clock::min_time; |
| 195 | realtime_clock::time_point realtime_sent_time_ = realtime_clock::min_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 196 | uint32_t sent_queue_index_ = 0xffffffff; |
| 197 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 198 | private: |
| 199 | friend class EventLoop; |
| 200 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 201 | virtual bool DoSend(const void *data, size_t size, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 202 | monotonic_clock::time_point monotonic_remote_time, |
| 203 | realtime_clock::time_point realtime_remote_time, |
| 204 | uint32_t remote_queue_index, |
| 205 | const UUID &remote_boot_uuid) = 0; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 206 | virtual bool DoSend(size_t size, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame^] | 207 | monotonic_clock::time_point monotonic_remote_time, |
| 208 | realtime_clock::time_point realtime_remote_time, |
| 209 | uint32_t remote_queue_index, |
| 210 | const UUID &remote_boot_uuid) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 211 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 212 | EventLoop *const event_loop_; |
| 213 | const Channel *const channel_; |
| 214 | const std::string ftrace_prefix_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 215 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 216 | internal::RawSenderTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 217 | Ftrace ftrace_; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 218 | |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 219 | ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr}; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 220 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 221 | |
| 222 | // Fetches the newest message from a channel. |
| 223 | // This provides a polling based interface for channels. |
| 224 | template <typename T> |
| 225 | class Fetcher { |
| 226 | public: |
| 227 | Fetcher() {} |
| 228 | |
| 229 | // Fetches the next message. Returns true if it fetched a new message. This |
| 230 | // method will only return messages sent after the Fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 231 | bool FetchNext() { |
| 232 | const bool result = fetcher_->FetchNext(); |
| 233 | if (result) { |
| 234 | CheckChannelDataAlignment(fetcher_->context().data, |
| 235 | fetcher_->context().size); |
| 236 | } |
| 237 | return result; |
| 238 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 239 | |
| 240 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 241 | // This will return the latest message regardless of if it was sent before or |
| 242 | // after the fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 243 | bool Fetch() { |
| 244 | const bool result = fetcher_->Fetch(); |
| 245 | if (result) { |
| 246 | CheckChannelDataAlignment(fetcher_->context().data, |
| 247 | fetcher_->context().size); |
| 248 | } |
| 249 | return result; |
| 250 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 251 | |
| 252 | // Returns a pointer to the contained flatbuffer, or nullptr if there is no |
| 253 | // available message. |
| 254 | const T *get() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 255 | return fetcher_->context().data != nullptr |
| 256 | ? flatbuffers::GetRoot<T>( |
| 257 | reinterpret_cast<const char *>(fetcher_->context().data)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 258 | : nullptr; |
| 259 | } |
| 260 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 261 | // Returns the channel this fetcher uses |
| 262 | const Channel *channel() const { return fetcher_->channel(); } |
| 263 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 264 | // Returns the context holding timestamps and other metadata about the |
| 265 | // message. |
| 266 | const Context &context() const { return fetcher_->context(); } |
| 267 | |
| 268 | const T &operator*() const { return *get(); } |
| 269 | const T *operator->() const { return get(); } |
| 270 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 271 | // Returns true if this fetcher is valid and connected to a channel. |
| 272 | operator bool() const { return static_cast<bool>(fetcher_); } |
| 273 | |
Austin Schuh | ca75b6a | 2020-12-15 21:12:24 -0800 | [diff] [blame] | 274 | // Copies the current flatbuffer into a FlatbufferVector. |
| 275 | FlatbufferVector<T> CopyFlatBuffer() const { |
| 276 | return context().template CopyFlatBuffer<T>(); |
| 277 | } |
| 278 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 279 | private: |
| 280 | friend class EventLoop; |
| 281 | Fetcher(::std::unique_ptr<RawFetcher> fetcher) |
| 282 | : fetcher_(::std::move(fetcher)) {} |
| 283 | ::std::unique_ptr<RawFetcher> fetcher_; |
| 284 | }; |
| 285 | |
| 286 | // Sends messages to a channel. |
| 287 | template <typename T> |
| 288 | class Sender { |
| 289 | public: |
| 290 | Sender() {} |
| 291 | |
| 292 | // Represents a single message about to be sent to the queue. |
| 293 | // The lifecycle goes: |
| 294 | // |
| 295 | // Builder builder = sender.MakeBuilder(); |
| 296 | // T::Builder t_builder = builder.MakeBuilder<T>(); |
| 297 | // Populate(&t_builder); |
| 298 | // builder.Send(t_builder.Finish()); |
| 299 | class Builder { |
| 300 | public: |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 301 | Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator) |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 302 | : fbb_(allocator->size(), allocator), |
| 303 | allocator_(allocator), |
| 304 | sender_(sender) { |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 305 | CheckChannelDataAlignment(allocator->data(), allocator->size()); |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 306 | fbb_.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 307 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 308 | Builder() {} |
| 309 | Builder(const Builder &) = delete; |
| 310 | Builder(Builder &&) = default; |
| 311 | |
| 312 | Builder &operator=(const Builder &) = delete; |
| 313 | Builder &operator=(Builder &&) = default; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 314 | |
| 315 | flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; } |
| 316 | |
| 317 | template <typename T2> |
| 318 | typename T2::Builder MakeBuilder() { |
| 319 | return typename T2::Builder(fbb_); |
| 320 | } |
| 321 | |
| 322 | bool Send(flatbuffers::Offset<T> offset) { |
| 323 | fbb_.Finish(offset); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 324 | const bool result = sender_->Send(fbb_.GetSize()); |
| 325 | // Ensure fbb_ knows it shouldn't access the memory any more. |
| 326 | fbb_ = flatbuffers::FlatBufferBuilder(); |
| 327 | return result; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | // CHECKs that this message was sent. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 331 | void CheckSent() { |
| 332 | CHECK(!allocator_->is_allocated()) << ": Message was not sent yet"; |
| 333 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 334 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 335 | // Detaches a buffer, for later use calling Sender::Send directly. |
| 336 | // |
| 337 | // Note that the underlying memory remains with the Sender, so creating |
| 338 | // another Builder before destroying the FlatbufferDetachedBuffer will fail. |
| 339 | FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) { |
| 340 | fbb_.Finish(offset); |
| 341 | return fbb_.Release(); |
| 342 | } |
| 343 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 344 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 345 | flatbuffers::FlatBufferBuilder fbb_; |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 346 | ChannelPreallocatedAllocator *allocator_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 347 | RawSender *sender_; |
| 348 | }; |
| 349 | |
| 350 | // Constructs an above builder. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 351 | // |
| 352 | // Only a single one of these may be "alive" for this object at any point in |
| 353 | // time. After calling Send on the result, it is no longer "alive". This means |
| 354 | // that you must manually reset a variable holding the return value (by |
| 355 | // assigning a default-constructed Builder to it) before calling this method |
| 356 | // again to overwrite the value in the variable. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 357 | Builder MakeBuilder(); |
| 358 | |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 359 | // Sends a prebuilt flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 360 | bool Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer); |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 361 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 362 | // Sends a prebuilt flatbuffer which was detached from a Builder created via |
| 363 | // MakeBuilder() on this object. |
| 364 | bool SendDetached(FlatbufferDetachedBuffer<T> detached); |
| 365 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 366 | // Returns the name of the underlying queue. |
Austin Schuh | 1e86947 | 2019-12-01 13:36:10 -0800 | [diff] [blame] | 367 | const Channel *channel() const { return sender_->channel(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 368 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 369 | operator bool() const { return sender_ ? true : false; } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 370 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 371 | // Returns the time_points that the last message was sent at. |
| 372 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 373 | return sender_->monotonic_sent_time(); |
| 374 | } |
| 375 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 376 | return sender_->realtime_sent_time(); |
| 377 | } |
| 378 | // Returns the queue index that this was sent with. |
| 379 | uint32_t sent_queue_index() const { return sender_->sent_queue_index(); } |
| 380 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 381 | // Returns the buffer index which MakeBuilder() will expose access to. This is |
| 382 | // the buffer the caller can fill out. |
| 383 | int buffer_index() const { return sender_->buffer_index(); } |
| 384 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 385 | private: |
| 386 | friend class EventLoop; |
| 387 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 388 | std::unique_ptr<RawSender> sender_; |
| 389 | }; |
| 390 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 391 | // Interface for timers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 392 | class TimerHandler { |
| 393 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 394 | virtual ~TimerHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 395 | |
| 396 | // Timer should sleep until base, base + offset, base + offset * 2, ... |
| 397 | // If repeat_offset isn't set, the timer only expires once. |
| 398 | virtual void Setup(monotonic_clock::time_point base, |
| 399 | monotonic_clock::duration repeat_offset = |
| 400 | ::aos::monotonic_clock::zero()) = 0; |
| 401 | |
| 402 | // Stop future calls to callback(). |
| 403 | virtual void Disable() = 0; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 404 | |
| 405 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 406 | // name in the timing report. |
| 407 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 408 | const std::string_view name() const { return name_; } |
| 409 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 410 | protected: |
| 411 | TimerHandler(EventLoop *event_loop, std::function<void()> fn); |
| 412 | |
Austin Schuh | cde39fd | 2020-02-22 20:58:24 -0800 | [diff] [blame] | 413 | monotonic_clock::time_point Call( |
| 414 | std::function<monotonic_clock::time_point()> get_time, |
| 415 | monotonic_clock::time_point event_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 416 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 417 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 418 | friend class EventLoop; |
| 419 | |
| 420 | EventLoop *event_loop_; |
| 421 | // The function to call when Call is called. |
| 422 | std::function<void()> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 423 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 424 | |
| 425 | internal::TimerTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 426 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 427 | }; |
| 428 | |
| 429 | // Interface for phased loops. They are built on timers. |
| 430 | class PhasedLoopHandler { |
| 431 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 432 | virtual ~PhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 433 | |
| 434 | // Sets the interval and offset. Any changes to interval and offset only take |
| 435 | // effect when the handler finishes running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 436 | void set_interval_and_offset(const monotonic_clock::duration interval, |
| 437 | const monotonic_clock::duration offset) { |
| 438 | phased_loop_.set_interval_and_offset(interval, offset); |
| 439 | } |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 440 | |
| 441 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 442 | // name in the timing report. |
| 443 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 444 | const std::string_view name() const { return name_; } |
| 445 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 446 | protected: |
| 447 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 448 | std::function<void(monotonic_clock::time_point)> schedule); |
| 449 | |
| 450 | PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn, |
| 451 | const monotonic_clock::duration interval, |
| 452 | const monotonic_clock::duration offset); |
| 453 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 454 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 455 | friend class EventLoop; |
| 456 | |
| 457 | void Reschedule(std::function<void(monotonic_clock::time_point)> schedule, |
| 458 | monotonic_clock::time_point monotonic_now) { |
| 459 | cycles_elapsed_ += phased_loop_.Iterate(monotonic_now); |
| 460 | schedule(phased_loop_.sleep_time()); |
| 461 | } |
| 462 | |
| 463 | virtual void Schedule(monotonic_clock::time_point sleep_time) = 0; |
| 464 | |
| 465 | EventLoop *event_loop_; |
| 466 | std::function<void(int)> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 467 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 468 | time::PhasedLoop phased_loop_; |
| 469 | |
| 470 | int cycles_elapsed_ = 0; |
| 471 | |
| 472 | internal::TimerTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 473 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 474 | }; |
| 475 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 476 | class EventLoop { |
| 477 | public: |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 478 | EventLoop(const Configuration *configuration); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 479 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 480 | virtual ~EventLoop(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 481 | |
| 482 | // Current time. |
| 483 | virtual monotonic_clock::time_point monotonic_now() = 0; |
| 484 | virtual realtime_clock::time_point realtime_now() = 0; |
| 485 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 486 | // Returns true if the channel exists in the configuration. |
| 487 | template <typename T> |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 488 | const Channel *GetChannel(const std::string_view channel_name) { |
Austin Schuh | caa2a5d | 2020-11-01 22:38:20 -0800 | [diff] [blame] | 489 | return configuration::GetChannel(configuration(), channel_name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 490 | T::GetFullyQualifiedName(), name(), node(), |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 491 | true); |
| 492 | } |
| 493 | template <typename T> |
| 494 | bool HasChannel(const std::string_view channel_name) { |
| 495 | return GetChannel<T>(channel_name) != nullptr; |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 498 | // Note, it is supported to create: |
| 499 | // multiple fetchers, and (one sender or one watcher) per <name, type> |
| 500 | // tuple. |
| 501 | |
| 502 | // Makes a class that will always fetch the most recent value |
| 503 | // sent to the provided channel. |
| 504 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 505 | Fetcher<T> MakeFetcher(const std::string_view channel_name) { |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 506 | const Channel *channel = GetChannel<T>(channel_name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 507 | CHECK(channel != nullptr) |
| 508 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 509 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 510 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 511 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 512 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 513 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 514 | << "\" } is not able to be fetched on this node. Check your " |
| 515 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 518 | return Fetcher<T>(MakeRawFetcher(channel)); |
| 519 | } |
| 520 | |
| 521 | // Makes class that allows constructing and sending messages to |
| 522 | // the provided channel. |
| 523 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 524 | Sender<T> MakeSender(const std::string_view channel_name) { |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 525 | const Channel *channel = GetChannel<T>(channel_name); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 526 | CHECK(channel != nullptr) |
| 527 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
Austin Schuh | 28fedcb | 2020-02-08 15:59:58 -0800 | [diff] [blame] | 528 | << T::GetFullyQualifiedName() << "\" } not found in config for " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 529 | << name() |
Austin Schuh | caa2a5d | 2020-11-01 22:38:20 -0800 | [diff] [blame] | 530 | << (configuration::MultiNode(configuration()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 531 | ? absl::StrCat(" on node ", node()->name()->string_view()) |
| 532 | : "."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 533 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 534 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 535 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 536 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 537 | << "\" } is not able to be sent on this node. Check your " |
| 538 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 541 | return Sender<T>(MakeRawSender(channel)); |
| 542 | } |
| 543 | |
| 544 | // This will watch messages sent to the provided channel. |
| 545 | // |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 546 | // w must have a non-polymorphic operator() (aka it can only be called with a |
| 547 | // single set of arguments; no overloading or templates). It must be callable |
| 548 | // with this signature: |
| 549 | // void(const MessageType &); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 550 | // |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 551 | // Lambdas are a common form for w. A std::function will work too. |
| 552 | // |
| 553 | // Note that bind expressions have polymorphic call operators, so they are not |
| 554 | // allowed. |
| 555 | // |
| 556 | // We template Watch as a whole instead of using std::function<void(const T |
| 557 | // &)> to allow deducing MessageType from lambdas and other things which are |
| 558 | // implicitly convertible to std::function, but not actually std::function |
| 559 | // instantiations. Template deduction guides might allow solving this |
| 560 | // differently in newer versions of C++, but those have their own corner |
| 561 | // cases. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 562 | template <typename Watch> |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 563 | void MakeWatcher(const std::string_view channel_name, Watch &&w); |
| 564 | |
| 565 | // Like MakeWatcher, but doesn't have access to the message data. This may be |
| 566 | // implemented to use less resources than an equivalent MakeWatcher. |
| 567 | // |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 568 | // The function will still have access to context(), although that will have |
| 569 | // its data field set to nullptr. |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 570 | template <typename MessageType> |
| 571 | void MakeNoArgWatcher(const std::string_view channel_name, |
| 572 | std::function<void()> w); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 573 | |
| 574 | // The passed in function will be called when the event loop starts. |
| 575 | // Use this to run code once the thread goes into "real-time-mode", |
| 576 | virtual void OnRun(::std::function<void()> on_run) = 0; |
| 577 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 578 | // Gets the name of the event loop. This is the application name. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 579 | virtual const std::string_view name() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 580 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 581 | // Returns the node that this event loop is running on. Returns nullptr if we |
| 582 | // are running in single-node mode. |
| 583 | virtual const Node *node() const = 0; |
| 584 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 585 | // Creates a timer that executes callback when the timer expires |
| 586 | // Returns a TimerHandle for configuration of the timer |
| 587 | virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0; |
| 588 | |
| 589 | // Creates a timer that executes callback periodically at the specified |
| 590 | // interval and offset. Returns a PhasedLoopHandler for interacting with the |
| 591 | // timer. |
| 592 | virtual PhasedLoopHandler *AddPhasedLoop( |
| 593 | ::std::function<void(int)> callback, |
| 594 | const monotonic_clock::duration interval, |
| 595 | const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0; |
| 596 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 597 | // TODO(austin): OnExit for cleanup. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 598 | |
| 599 | // Threadsafe. |
| 600 | bool is_running() const { return is_running_.load(); } |
| 601 | |
| 602 | // Sets the scheduler priority to run the event loop at. This may not be |
| 603 | // called after we go into "real-time-mode". |
| 604 | virtual void SetRuntimeRealtimePriority(int priority) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 605 | virtual int priority() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 606 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 607 | // Sets the scheduler affinity to run the event loop with. This may only be |
| 608 | // called before Run(). |
| 609 | virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0; |
| 610 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 611 | // Fetches new messages from the provided channel (path, type). |
| 612 | // |
| 613 | // Note: this channel must be a member of the exact configuration object this |
| 614 | // was built with. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 615 | virtual std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 616 | const Channel *channel) = 0; |
| 617 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 618 | // Watches channel (name, type) for new messages. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 619 | virtual void MakeRawWatcher( |
| 620 | const Channel *channel, |
| 621 | std::function<void(const Context &context, const void *message)> |
| 622 | watcher) = 0; |
| 623 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 624 | // Watches channel (name, type) for new messages, without needing to extract |
| 625 | // the message contents. Default implementation simply re-uses MakeRawWatcher. |
| 626 | virtual void MakeRawNoArgWatcher( |
| 627 | const Channel *channel, |
| 628 | std::function<void(const Context &context)> watcher) { |
| 629 | MakeRawWatcher(channel, [watcher](const Context &context, const void *) { |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 630 | Context new_context = context; |
| 631 | new_context.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 632 | new_context.buffer_index = -1; |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 633 | watcher(new_context); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 634 | }); |
| 635 | } |
| 636 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 637 | // Creates a raw sender for the provided channel. This is used for reflection |
| 638 | // based sending. |
| 639 | // Note: this ignores any node constraints. Ignore at your own peril. |
| 640 | virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0; |
| 641 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 642 | // Returns the context for the current callback. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 643 | const Context &context() const { return context_; } |
| 644 | |
| 645 | // Returns the configuration that this event loop was built with. |
| 646 | const Configuration *configuration() const { return configuration_; } |
| 647 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 648 | // Prevents the event loop from sending a timing report. |
| 649 | void SkipTimingReport() { skip_timing_report_ = true; } |
| 650 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 651 | // Prevents AOS_LOG being sent to message on /aos. |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 652 | void SkipAosLog() { skip_logger_ = true; } |
| 653 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 654 | // Returns the number of buffers for this channel. This corresponds with the |
| 655 | // range of Context::buffer_index values for this channel. |
| 656 | virtual int NumberBuffers(const Channel *channel) = 0; |
| 657 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 658 | // Returns the boot UUID. |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 659 | virtual const UUID &boot_uuid() const = 0; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 660 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 661 | protected: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 662 | // Sets the name of the event loop. This is the application name. |
| 663 | virtual void set_name(const std::string_view name) = 0; |
| 664 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 665 | void set_is_running(bool value) { is_running_.store(value); } |
| 666 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 667 | // Validates that channel exists inside configuration_ and finds its index. |
| 668 | int ChannelIndex(const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 669 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 670 | // Returns the state for the watcher on the corresponding channel. This |
| 671 | // watcher must exist before calling this. |
| 672 | WatcherState *GetWatcherState(const Channel *channel); |
| 673 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 674 | // Returns a Sender's protected RawSender. |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 675 | template <typename T> |
| 676 | static RawSender *GetRawSender(aos::Sender<T> *sender) { |
| 677 | return sender->sender_.get(); |
| 678 | } |
| 679 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 680 | // Returns a Fetcher's protected RawFetcher. |
| 681 | template <typename T> |
| 682 | static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) { |
| 683 | return fetcher->fetcher_.get(); |
| 684 | } |
| 685 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 686 | // Context available for watchers, timers, and phased loops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 687 | Context context_; |
| 688 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 689 | friend class RawSender; |
| 690 | friend class TimerHandler; |
| 691 | friend class RawFetcher; |
| 692 | friend class PhasedLoopHandler; |
| 693 | friend class WatcherState; |
| 694 | |
| 695 | // Methods used to implement timing reports. |
| 696 | void NewSender(RawSender *sender); |
| 697 | void DeleteSender(RawSender *sender); |
| 698 | TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer); |
| 699 | PhasedLoopHandler *NewPhasedLoop( |
| 700 | std::unique_ptr<PhasedLoopHandler> phased_loop); |
| 701 | void NewFetcher(RawFetcher *fetcher); |
| 702 | void DeleteFetcher(RawFetcher *fetcher); |
| 703 | WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher); |
| 704 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 705 | // Tracks that we have a (single) watcher on the given channel. |
| 706 | void TakeWatcher(const Channel *channel); |
| 707 | // Tracks that we have at least one sender on the given channel. |
| 708 | void TakeSender(const Channel *channel); |
| 709 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 710 | std::vector<RawSender *> senders_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 711 | std::vector<RawFetcher *> fetchers_; |
| 712 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 713 | std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 714 | std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 715 | std::vector<std::unique_ptr<WatcherState>> watchers_; |
| 716 | |
| 717 | void SendTimingReport(); |
| 718 | void UpdateTimingReport(); |
| 719 | void MaybeScheduleTimingReports(); |
| 720 | |
| 721 | std::unique_ptr<RawSender> timing_report_sender_; |
| 722 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 723 | // Tracks which event sources (timers and watchers) have data, and which |
| 724 | // don't. Added events may not change their event_time(). |
| 725 | // TODO(austin): Test case 1: timer triggers at t1, handler takes until after |
| 726 | // t2 to run, t2 should then be picked up without a context switch. |
| 727 | void AddEvent(EventLoopEvent *event); |
| 728 | void RemoveEvent(EventLoopEvent *event); |
| 729 | size_t EventCount() const { return events_.size(); } |
| 730 | EventLoopEvent *PopEvent(); |
| 731 | EventLoopEvent *PeekEvent() { return events_.front(); } |
| 732 | void ReserveEvents(); |
| 733 | |
| 734 | std::vector<EventLoopEvent *> events_; |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 735 | size_t event_generation_ = 1; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 736 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 737 | // If true, don't send AOS_LOG to /aos |
| 738 | bool skip_logger_ = false; |
| 739 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 740 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 741 | virtual pid_t GetTid() = 0; |
| 742 | |
| 743 | FlatbufferDetachedBuffer<timing::Report> timing_report_; |
| 744 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 745 | ::std::atomic<bool> is_running_{false}; |
| 746 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 747 | const Configuration *configuration_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 748 | |
| 749 | // If true, don't send out timing reports. |
| 750 | bool skip_timing_report_ = false; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 751 | |
| 752 | absl::btree_set<const Channel *> taken_watchers_, taken_senders_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | } // namespace aos |
| 756 | |
Austin Schuh | b807581 | 2020-10-19 09:36:49 -0700 | [diff] [blame] | 757 | #include "aos/events/event_loop_tmpl.h" // IWYU pragma: export |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 758 | |
| 759 | #endif // AOS_EVENTS_EVENT_LOOP_H |