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> |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 7 | #include <ostream> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 9 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 10 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 11 | #include "absl/container/btree_set.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | #include "aos/configuration.h" |
| 13 | #include "aos/configuration_generated.h" |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 14 | #include "aos/events/channel_preallocated_allocator.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 15 | #include "aos/events/event_loop_event.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 16 | #include "aos/events/event_loop_generated.h" |
| 17 | #include "aos/events/timing_statistics.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | #include "aos/flatbuffers.h" |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 19 | #include "aos/ftrace.h" |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 20 | #include "aos/ipc_lib/data_alignment.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | #include "aos/json_to_flatbuffer.h" |
| 22 | #include "aos/time/time.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 23 | #include "aos/util/phased_loop.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 24 | #include "aos/uuid.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. |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 80 | UUID source_boot_uuid = UUID::Zero(); |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 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_; } |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 118 | const EventLoop *event_loop() const { return event_loop_; } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 119 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 120 | Context context_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | friend class EventLoop; |
| 124 | // Implementation |
| 125 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0; |
| 126 | virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0; |
| 127 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 128 | EventLoop *const event_loop_; |
| 129 | const Channel *const channel_; |
| 130 | const std::string ftrace_prefix_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 131 | |
| 132 | internal::RawFetcherTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 133 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | // Raw version of sender. Sends a block of data. This is used for reflection |
| 137 | // and as a building block to implement typed senders. |
| 138 | class RawSender { |
| 139 | public: |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 140 | using SharedSpan = std::shared_ptr<const absl::Span<const uint8_t>>; |
| 141 | |
Brian Silverman | 183859c | 2022-05-14 02:03:06 -0700 | [diff] [blame] | 142 | // This looks a little ugly with no space, but please leave it so clang-format |
| 143 | // doesn't keep changing it. Bug is filed at |
| 144 | // <https://github.com/llvm/llvm-project/issues/55457>. |
| 145 | enum class [[nodiscard]] Error{ |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 146 | // Represents success and no error |
| 147 | kOk, |
| 148 | |
| 149 | // Error for messages on channels being sent faster than their |
| 150 | // frequency and channel storage duration allow |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 151 | kMessagesSentTooFast, |
| 152 | // Access to Redzone was attempted in Sender Queue |
Brian Silverman | 183859c | 2022-05-14 02:03:06 -0700 | [diff] [blame] | 153 | kInvalidRedzone, |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 154 | }; |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 155 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 156 | RawSender(EventLoop *event_loop, const Channel *channel); |
| 157 | RawSender(const RawSender &) = delete; |
| 158 | RawSender &operator=(const RawSender &) = delete; |
| 159 | |
| 160 | virtual ~RawSender(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 161 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 162 | virtual void *data() = 0; |
| 163 | virtual size_t size() = 0; |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 164 | |
| 165 | // Sends a message without copying it. The users starts by copying up to |
| 166 | // size() bytes into the data backed by data(). They then call Send to send. |
| 167 | // Returns Error::kOk on a successful send, or |
| 168 | // Error::kMessagesSentTooFast if messages were sent too fast. If provided, |
| 169 | // monotonic_remote_time, realtime_remote_time, and remote_queue_index are |
| 170 | // attached to the message and are available in the context on the read side. |
| 171 | // If they are not populated, the read side will get the sent times instead. |
| 172 | Error Send(size_t size); |
| 173 | Error Send(size_t size, monotonic_clock::time_point monotonic_remote_time, |
| 174 | realtime_clock::time_point realtime_remote_time, |
| 175 | uint32_t remote_queue_index, const UUID &source_boot_uuid); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 176 | |
| 177 | // Sends a single block of data by copying it. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 178 | // The remote arguments have the same meaning as in Send above. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 179 | // Returns Error::kMessagesSentTooFast if messages were sent too fast |
| 180 | Error Send(const void *data, size_t size); |
| 181 | Error Send(const void *data, size_t size, |
| 182 | monotonic_clock::time_point monotonic_remote_time, |
| 183 | realtime_clock::time_point realtime_remote_time, |
| 184 | uint32_t remote_queue_index, const UUID &source_boot_uuid); |
| 185 | |
| 186 | // CHECKs that no sending Error occurred and logs the channel_ data if |
| 187 | // one did |
| 188 | void CheckOk(const Error err); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 189 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 190 | // Sends a single block of data by refcounting it to avoid copies. The data |
| 191 | // must not change after being passed into Send. The remote arguments have the |
| 192 | // same meaning as in Send above. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 193 | Error Send(const SharedSpan data); |
| 194 | Error Send(const SharedSpan data, |
| 195 | monotonic_clock::time_point monotonic_remote_time, |
| 196 | realtime_clock::time_point realtime_remote_time, |
| 197 | uint32_t remote_queue_index, const UUID &remote_boot_uuid); |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 198 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 199 | const Channel *channel() const { return channel_; } |
| 200 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 201 | // Returns the time_points that the last message was sent at. |
| 202 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 203 | return monotonic_sent_time_; |
| 204 | } |
| 205 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 206 | return realtime_sent_time_; |
| 207 | } |
| 208 | // Returns the queue index that this was sent with. |
| 209 | uint32_t sent_queue_index() const { return sent_queue_index_; } |
| 210 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 211 | // Returns the associated flatbuffers-style allocator. This must be |
| 212 | // deallocated before the message is sent. |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 213 | ChannelPreallocatedAllocator *fbb_allocator() { |
| 214 | fbb_allocator_ = ChannelPreallocatedAllocator( |
| 215 | reinterpret_cast<uint8_t *>(data()), size(), channel()); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 216 | return &fbb_allocator_; |
| 217 | } |
| 218 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 219 | // Index of the buffer which is currently exposed by data() and the various |
| 220 | // other accessors. This is the message the caller should be filling out. |
| 221 | virtual int buffer_index() = 0; |
| 222 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 223 | protected: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 224 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 225 | const EventLoop *event_loop() const { return event_loop_; } |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 226 | |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 227 | monotonic_clock::time_point monotonic_sent_time_ = monotonic_clock::min_time; |
| 228 | realtime_clock::time_point realtime_sent_time_ = realtime_clock::min_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 229 | uint32_t sent_queue_index_ = 0xffffffff; |
| 230 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 231 | private: |
| 232 | friend class EventLoop; |
| 233 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 234 | virtual Error DoSend(const void *data, size_t size, |
| 235 | monotonic_clock::time_point monotonic_remote_time, |
| 236 | realtime_clock::time_point realtime_remote_time, |
| 237 | uint32_t remote_queue_index, |
| 238 | const UUID &source_boot_uuid) = 0; |
| 239 | virtual Error DoSend(size_t size, |
| 240 | monotonic_clock::time_point monotonic_remote_time, |
| 241 | realtime_clock::time_point realtime_remote_time, |
| 242 | uint32_t remote_queue_index, |
| 243 | const UUID &source_boot_uuid) = 0; |
| 244 | virtual Error DoSend(const SharedSpan data, |
| 245 | monotonic_clock::time_point monotonic_remote_time, |
| 246 | realtime_clock::time_point realtime_remote_time, |
| 247 | uint32_t remote_queue_index, |
| 248 | const UUID &source_boot_uuid); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 249 | |
James Kuszmaul | 93abac1 | 2022-04-14 15:05:10 -0700 | [diff] [blame] | 250 | void RecordSendResult(const Error error, size_t message_size); |
| 251 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 252 | EventLoop *const event_loop_; |
| 253 | const Channel *const channel_; |
| 254 | const std::string ftrace_prefix_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 255 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 256 | internal::RawSenderTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 257 | Ftrace ftrace_; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 258 | |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 259 | ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr}; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 260 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 261 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 262 | // Needed for compatibility with glog |
| 263 | std::ostream &operator<<(std::ostream &os, const RawSender::Error err); |
| 264 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 265 | // Fetches the newest message from a channel. |
| 266 | // This provides a polling based interface for channels. |
| 267 | template <typename T> |
| 268 | class Fetcher { |
| 269 | public: |
| 270 | Fetcher() {} |
| 271 | |
| 272 | // Fetches the next message. Returns true if it fetched a new message. This |
| 273 | // method will only return messages sent after the Fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 274 | bool FetchNext() { |
| 275 | const bool result = fetcher_->FetchNext(); |
| 276 | if (result) { |
| 277 | CheckChannelDataAlignment(fetcher_->context().data, |
| 278 | fetcher_->context().size); |
| 279 | } |
| 280 | return result; |
| 281 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 282 | |
| 283 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 284 | // This will return the latest message regardless of if it was sent before or |
| 285 | // after the fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 286 | bool Fetch() { |
| 287 | const bool result = fetcher_->Fetch(); |
| 288 | if (result) { |
| 289 | CheckChannelDataAlignment(fetcher_->context().data, |
| 290 | fetcher_->context().size); |
| 291 | } |
| 292 | return result; |
| 293 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 294 | |
| 295 | // Returns a pointer to the contained flatbuffer, or nullptr if there is no |
| 296 | // available message. |
| 297 | const T *get() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 298 | return fetcher_->context().data != nullptr |
| 299 | ? flatbuffers::GetRoot<T>( |
| 300 | reinterpret_cast<const char *>(fetcher_->context().data)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 301 | : nullptr; |
| 302 | } |
| 303 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 304 | // Returns the channel this fetcher uses |
| 305 | const Channel *channel() const { return fetcher_->channel(); } |
| 306 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 307 | // Returns the context holding timestamps and other metadata about the |
| 308 | // message. |
| 309 | const Context &context() const { return fetcher_->context(); } |
| 310 | |
| 311 | const T &operator*() const { return *get(); } |
| 312 | const T *operator->() const { return get(); } |
| 313 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 314 | // Returns true if this fetcher is valid and connected to a channel. |
Milind Upadhyay | 49174a7 | 2021-04-10 16:24:57 -0700 | [diff] [blame] | 315 | bool valid() const { return static_cast<bool>(fetcher_); } |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 316 | |
Austin Schuh | ca75b6a | 2020-12-15 21:12:24 -0800 | [diff] [blame] | 317 | // Copies the current flatbuffer into a FlatbufferVector. |
| 318 | FlatbufferVector<T> CopyFlatBuffer() const { |
| 319 | return context().template CopyFlatBuffer<T>(); |
| 320 | } |
| 321 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 322 | private: |
| 323 | friend class EventLoop; |
| 324 | Fetcher(::std::unique_ptr<RawFetcher> fetcher) |
| 325 | : fetcher_(::std::move(fetcher)) {} |
| 326 | ::std::unique_ptr<RawFetcher> fetcher_; |
| 327 | }; |
| 328 | |
| 329 | // Sends messages to a channel. |
| 330 | template <typename T> |
| 331 | class Sender { |
| 332 | public: |
| 333 | Sender() {} |
| 334 | |
| 335 | // Represents a single message about to be sent to the queue. |
| 336 | // The lifecycle goes: |
| 337 | // |
| 338 | // Builder builder = sender.MakeBuilder(); |
| 339 | // T::Builder t_builder = builder.MakeBuilder<T>(); |
| 340 | // Populate(&t_builder); |
| 341 | // builder.Send(t_builder.Finish()); |
| 342 | class Builder { |
| 343 | public: |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 344 | Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator) |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 345 | : fbb_(allocator->size(), allocator), |
| 346 | allocator_(allocator), |
| 347 | sender_(sender) { |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 348 | CheckChannelDataAlignment(allocator->data(), allocator->size()); |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 349 | fbb_.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 350 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 351 | Builder() {} |
| 352 | Builder(const Builder &) = delete; |
| 353 | Builder(Builder &&) = default; |
| 354 | |
| 355 | Builder &operator=(const Builder &) = delete; |
| 356 | Builder &operator=(Builder &&) = default; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 357 | |
| 358 | flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; } |
| 359 | |
| 360 | template <typename T2> |
| 361 | typename T2::Builder MakeBuilder() { |
| 362 | return typename T2::Builder(fbb_); |
| 363 | } |
| 364 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 365 | RawSender::Error Send(flatbuffers::Offset<T> offset) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 366 | fbb_.Finish(offset); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 367 | const auto err = sender_->Send(fbb_.GetSize()); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 368 | // Ensure fbb_ knows it shouldn't access the memory any more. |
| 369 | fbb_ = flatbuffers::FlatBufferBuilder(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 370 | return err; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 371 | } |
| 372 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 373 | // Equivalent to RawSender::CheckOk |
| 374 | void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); }; |
| 375 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 376 | // CHECKs that this message was sent. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 377 | void CheckSent() { |
| 378 | CHECK(!allocator_->is_allocated()) << ": Message was not sent yet"; |
| 379 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 380 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 381 | // Detaches a buffer, for later use calling Sender::Send directly. |
| 382 | // |
| 383 | // Note that the underlying memory remains with the Sender, so creating |
| 384 | // another Builder before destroying the FlatbufferDetachedBuffer will fail. |
| 385 | FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) { |
| 386 | fbb_.Finish(offset); |
| 387 | return fbb_.Release(); |
| 388 | } |
| 389 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 390 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 391 | flatbuffers::FlatBufferBuilder fbb_; |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 392 | ChannelPreallocatedAllocator *allocator_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 393 | RawSender *sender_; |
| 394 | }; |
| 395 | |
| 396 | // Constructs an above builder. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 397 | // |
| 398 | // Only a single one of these may be "alive" for this object at any point in |
| 399 | // time. After calling Send on the result, it is no longer "alive". This means |
| 400 | // that you must manually reset a variable holding the return value (by |
| 401 | // assigning a default-constructed Builder to it) before calling this method |
| 402 | // again to overwrite the value in the variable. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 403 | Builder MakeBuilder(); |
| 404 | |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 405 | // Sends a prebuilt flatbuffer. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 406 | RawSender::Error Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer); |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 407 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 408 | // Sends a prebuilt flatbuffer which was detached from a Builder created via |
| 409 | // MakeBuilder() on this object. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 410 | RawSender::Error SendDetached(FlatbufferDetachedBuffer<T> detached); |
| 411 | |
| 412 | // Equivalent to RawSender::CheckOk |
| 413 | void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); }; |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 414 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 415 | // Returns the name of the underlying queue. |
Austin Schuh | 1e86947 | 2019-12-01 13:36:10 -0800 | [diff] [blame] | 416 | const Channel *channel() const { return sender_->channel(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 417 | |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 418 | // TODO(austin): Deprecate the operator bool. |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 419 | operator bool() const { return sender_ ? true : false; } |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 420 | bool valid() const { return static_cast<bool>(sender_); } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 421 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 422 | // Returns the time_points that the last message was sent at. |
| 423 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 424 | return sender_->monotonic_sent_time(); |
| 425 | } |
| 426 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 427 | return sender_->realtime_sent_time(); |
| 428 | } |
| 429 | // Returns the queue index that this was sent with. |
| 430 | uint32_t sent_queue_index() const { return sender_->sent_queue_index(); } |
| 431 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 432 | // Returns the buffer index which MakeBuilder() will expose access to. This is |
| 433 | // the buffer the caller can fill out. |
| 434 | int buffer_index() const { return sender_->buffer_index(); } |
| 435 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 436 | private: |
| 437 | friend class EventLoop; |
| 438 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 439 | std::unique_ptr<RawSender> sender_; |
| 440 | }; |
| 441 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 442 | // Class for keeping a count of send failures on a certain channel |
| 443 | class SendFailureCounter { |
| 444 | public: |
| 445 | inline void Count(const RawSender::Error err) { |
| 446 | failures_ += static_cast<size_t>(err != RawSender::Error::kOk); |
| 447 | just_failed_ = (err != RawSender::Error::kOk); |
| 448 | } |
| 449 | |
| 450 | inline size_t failures() const { return failures_; } |
| 451 | inline bool just_failed() const { return just_failed_; } |
| 452 | |
| 453 | private: |
| 454 | size_t failures_ = 0; |
| 455 | bool just_failed_ = false; |
| 456 | }; |
| 457 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 458 | // Interface for timers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 459 | class TimerHandler { |
| 460 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 461 | virtual ~TimerHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 462 | |
| 463 | // Timer should sleep until base, base + offset, base + offset * 2, ... |
| 464 | // If repeat_offset isn't set, the timer only expires once. |
James Kuszmaul | 8866e64 | 2022-06-10 16:00:36 -0700 | [diff] [blame^] | 465 | // base must be greater than or equal to zero. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 466 | virtual void Setup(monotonic_clock::time_point base, |
| 467 | monotonic_clock::duration repeat_offset = |
| 468 | ::aos::monotonic_clock::zero()) = 0; |
| 469 | |
| 470 | // Stop future calls to callback(). |
| 471 | virtual void Disable() = 0; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 472 | |
| 473 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 474 | // name in the timing report. |
| 475 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 476 | const std::string_view name() const { return name_; } |
| 477 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 478 | protected: |
| 479 | TimerHandler(EventLoop *event_loop, std::function<void()> fn); |
| 480 | |
Austin Schuh | 9b1d628 | 2022-06-10 17:03:21 -0700 | [diff] [blame] | 481 | template <typename T> |
| 482 | monotonic_clock::time_point Call(T get_time, |
| 483 | monotonic_clock::time_point event_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 484 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 485 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 486 | friend class EventLoop; |
| 487 | |
| 488 | EventLoop *event_loop_; |
| 489 | // The function to call when Call is called. |
| 490 | std::function<void()> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 491 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 492 | |
| 493 | internal::TimerTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 494 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 495 | }; |
| 496 | |
| 497 | // Interface for phased loops. They are built on timers. |
| 498 | class PhasedLoopHandler { |
| 499 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 500 | virtual ~PhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 501 | |
| 502 | // Sets the interval and offset. Any changes to interval and offset only take |
| 503 | // effect when the handler finishes running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 504 | void set_interval_and_offset(const monotonic_clock::duration interval, |
| 505 | const monotonic_clock::duration offset) { |
| 506 | phased_loop_.set_interval_and_offset(interval, offset); |
| 507 | } |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 508 | |
| 509 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 510 | // name in the timing report. |
| 511 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 512 | const std::string_view name() const { return name_; } |
| 513 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 514 | protected: |
| 515 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 516 | std::function<void(monotonic_clock::time_point)> schedule); |
| 517 | |
| 518 | PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn, |
| 519 | const monotonic_clock::duration interval, |
| 520 | const monotonic_clock::duration offset); |
| 521 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 522 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 523 | friend class EventLoop; |
| 524 | |
| 525 | void Reschedule(std::function<void(monotonic_clock::time_point)> schedule, |
| 526 | monotonic_clock::time_point monotonic_now) { |
| 527 | cycles_elapsed_ += phased_loop_.Iterate(monotonic_now); |
| 528 | schedule(phased_loop_.sleep_time()); |
| 529 | } |
| 530 | |
| 531 | virtual void Schedule(monotonic_clock::time_point sleep_time) = 0; |
| 532 | |
| 533 | EventLoop *event_loop_; |
| 534 | std::function<void(int)> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 535 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 536 | time::PhasedLoop phased_loop_; |
| 537 | |
| 538 | int cycles_elapsed_ = 0; |
| 539 | |
| 540 | internal::TimerTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 541 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 542 | }; |
| 543 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 544 | // Note, it is supported to create only: |
| 545 | // multiple fetchers, and (one sender or one watcher) per <name, type> |
| 546 | // tuple. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 547 | class EventLoop { |
| 548 | public: |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 549 | // Holds configuration by reference for the lifetime of this object. It may |
| 550 | // never be mutated externally in any way. |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 551 | EventLoop(const Configuration *configuration); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 552 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 553 | virtual ~EventLoop(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 554 | |
| 555 | // Current time. |
Stephan Pleines | 559fa6c | 2022-01-06 17:23:51 -0800 | [diff] [blame] | 556 | virtual monotonic_clock::time_point monotonic_now() const = 0; |
| 557 | virtual realtime_clock::time_point realtime_now() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 558 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 559 | template <typename T> |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 560 | const Channel *GetChannel(const std::string_view channel_name) { |
Austin Schuh | caa2a5d | 2020-11-01 22:38:20 -0800 | [diff] [blame] | 561 | return configuration::GetChannel(configuration(), channel_name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 562 | T::GetFullyQualifiedName(), name(), node(), |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 563 | true); |
| 564 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 565 | // Returns true if the channel exists in the configuration. |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 566 | template <typename T> |
| 567 | bool HasChannel(const std::string_view channel_name) { |
| 568 | return GetChannel<T>(channel_name) != nullptr; |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 571 | // Like MakeFetcher, but returns an invalid fetcher if the given channel is |
| 572 | // not readable on this node or does not exist. |
| 573 | template <typename T> |
| 574 | Fetcher<T> TryMakeFetcher(const std::string_view channel_name) { |
| 575 | const Channel *const channel = GetChannel<T>(channel_name); |
| 576 | if (channel == nullptr) { |
| 577 | return Fetcher<T>(); |
| 578 | } |
| 579 | |
| 580 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 581 | return Fetcher<T>(); |
| 582 | } |
| 583 | |
| 584 | return Fetcher<T>(MakeRawFetcher(channel)); |
| 585 | } |
| 586 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 587 | // Makes a class that will always fetch the most recent value |
| 588 | // sent to the provided channel. |
| 589 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 590 | Fetcher<T> MakeFetcher(const std::string_view channel_name) { |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 591 | CHECK(HasChannel<T>(channel_name)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 592 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 593 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 594 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 595 | Fetcher<T> result = TryMakeFetcher<T>(channel_name); |
| 596 | if (!result.valid()) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 597 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 598 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 599 | << "\" } is not able to be fetched on this node. Check your " |
| 600 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 603 | return result; |
| 604 | } |
| 605 | |
| 606 | // Like MakeSender, but returns an invalid sender if the given channel is |
James Kuszmaul | b888759 | 2021-12-09 14:54:50 -0800 | [diff] [blame] | 607 | // not sendable on this node or does not exist. |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 608 | template <typename T> |
| 609 | Sender<T> TryMakeSender(const std::string_view channel_name) { |
| 610 | const Channel *channel = GetChannel<T>(channel_name); |
| 611 | if (channel == nullptr) { |
| 612 | return Sender<T>(); |
| 613 | } |
| 614 | |
| 615 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 616 | return Sender<T>(); |
| 617 | } |
| 618 | |
| 619 | return Sender<T>(MakeRawSender(channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | // Makes class that allows constructing and sending messages to |
| 623 | // the provided channel. |
| 624 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 625 | Sender<T> MakeSender(const std::string_view channel_name) { |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 626 | CHECK(HasChannel<T>(channel_name)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 627 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
Austin Schuh | 28fedcb | 2020-02-08 15:59:58 -0800 | [diff] [blame] | 628 | << T::GetFullyQualifiedName() << "\" } not found in config for " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 629 | << name() |
Austin Schuh | caa2a5d | 2020-11-01 22:38:20 -0800 | [diff] [blame] | 630 | << (configuration::MultiNode(configuration()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 631 | ? absl::StrCat(" on node ", node()->name()->string_view()) |
| 632 | : "."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 633 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 634 | Sender<T> result = TryMakeSender<T>(channel_name); |
| 635 | if (!result) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 636 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 637 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 638 | << "\" } is not able to be sent on this node. Check your " |
| 639 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 642 | return result; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | // This will watch messages sent to the provided channel. |
| 646 | // |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 647 | // w must have a non-polymorphic operator() (aka it can only be called with a |
| 648 | // single set of arguments; no overloading or templates). It must be callable |
| 649 | // with this signature: |
| 650 | // void(const MessageType &); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 651 | // |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 652 | // Lambdas are a common form for w. A std::function will work too. |
| 653 | // |
| 654 | // Note that bind expressions have polymorphic call operators, so they are not |
| 655 | // allowed. |
| 656 | // |
| 657 | // We template Watch as a whole instead of using std::function<void(const T |
| 658 | // &)> to allow deducing MessageType from lambdas and other things which are |
| 659 | // implicitly convertible to std::function, but not actually std::function |
| 660 | // instantiations. Template deduction guides might allow solving this |
| 661 | // differently in newer versions of C++, but those have their own corner |
| 662 | // cases. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 663 | template <typename Watch> |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 664 | void MakeWatcher(const std::string_view channel_name, Watch &&w); |
| 665 | |
| 666 | // Like MakeWatcher, but doesn't have access to the message data. This may be |
| 667 | // implemented to use less resources than an equivalent MakeWatcher. |
| 668 | // |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 669 | // The function will still have access to context(), although that will have |
| 670 | // its data field set to nullptr. |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 671 | template <typename MessageType> |
| 672 | void MakeNoArgWatcher(const std::string_view channel_name, |
| 673 | std::function<void()> w); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 674 | |
| 675 | // The passed in function will be called when the event loop starts. |
| 676 | // Use this to run code once the thread goes into "real-time-mode", |
| 677 | virtual void OnRun(::std::function<void()> on_run) = 0; |
| 678 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 679 | // Gets the name of the event loop. This is the application name. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 680 | virtual const std::string_view name() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 681 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 682 | // Returns the node that this event loop is running on. Returns nullptr if we |
| 683 | // are running in single-node mode. |
| 684 | virtual const Node *node() const = 0; |
| 685 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 686 | // Creates a timer that executes callback when the timer expires |
| 687 | // Returns a TimerHandle for configuration of the timer |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 688 | // TODO(milind): callback should take the number of cycles elapsed as a |
| 689 | // parameter. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 690 | virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0; |
| 691 | |
| 692 | // Creates a timer that executes callback periodically at the specified |
| 693 | // interval and offset. Returns a PhasedLoopHandler for interacting with the |
| 694 | // timer. |
| 695 | virtual PhasedLoopHandler *AddPhasedLoop( |
| 696 | ::std::function<void(int)> callback, |
| 697 | const monotonic_clock::duration interval, |
| 698 | const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0; |
| 699 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 700 | // TODO(austin): OnExit for cleanup. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 701 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 702 | // May be safely called from any thread. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 703 | bool is_running() const { return is_running_.load(); } |
| 704 | |
| 705 | // Sets the scheduler priority to run the event loop at. This may not be |
| 706 | // called after we go into "real-time-mode". |
| 707 | virtual void SetRuntimeRealtimePriority(int priority) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 708 | virtual int priority() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 709 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 710 | // Sets the scheduler affinity to run the event loop with. This may only be |
| 711 | // called before Run(). |
| 712 | virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0; |
| 713 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 714 | // Fetches new messages from the provided channel (path, type). |
| 715 | // |
| 716 | // Note: this channel must be a member of the exact configuration object this |
| 717 | // was built with. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 718 | virtual std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 719 | const Channel *channel) = 0; |
| 720 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 721 | // Watches channel (name, type) for new messages. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 722 | virtual void MakeRawWatcher( |
| 723 | const Channel *channel, |
| 724 | std::function<void(const Context &context, const void *message)> |
| 725 | watcher) = 0; |
| 726 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 727 | // Watches channel (name, type) for new messages, without needing to extract |
| 728 | // the message contents. Default implementation simply re-uses MakeRawWatcher. |
| 729 | virtual void MakeRawNoArgWatcher( |
| 730 | const Channel *channel, |
| 731 | std::function<void(const Context &context)> watcher) { |
| 732 | MakeRawWatcher(channel, [watcher](const Context &context, const void *) { |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 733 | Context new_context = context; |
| 734 | new_context.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 735 | new_context.buffer_index = -1; |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 736 | watcher(new_context); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 737 | }); |
| 738 | } |
| 739 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 740 | // Creates a raw sender for the provided channel. This is used for reflection |
| 741 | // based sending. |
| 742 | // Note: this ignores any node constraints. Ignore at your own peril. |
| 743 | virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0; |
| 744 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 745 | // Returns the context for the current callback. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 746 | const Context &context() const { return context_; } |
| 747 | |
| 748 | // Returns the configuration that this event loop was built with. |
| 749 | const Configuration *configuration() const { return configuration_; } |
| 750 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 751 | // Prevents the event loop from sending a timing report. |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 752 | void SkipTimingReport(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 753 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 754 | // Prevents AOS_LOG being sent to message on /aos. |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 755 | void SkipAosLog() { skip_logger_ = true; } |
| 756 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 757 | // Returns the number of buffers for this channel. This corresponds with the |
| 758 | // range of Context::buffer_index values for this channel. |
| 759 | virtual int NumberBuffers(const Channel *channel) = 0; |
| 760 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 761 | // Returns the boot UUID. |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 762 | virtual const UUID &boot_uuid() const = 0; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 763 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 764 | protected: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 765 | // Sets the name of the event loop. This is the application name. |
| 766 | virtual void set_name(const std::string_view name) = 0; |
| 767 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 768 | void set_is_running(bool value) { is_running_.store(value); } |
| 769 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 770 | // Validates that channel exists inside configuration_ and finds its index. |
| 771 | int ChannelIndex(const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 772 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 773 | // Returns the state for the watcher on the corresponding channel. This |
| 774 | // watcher must exist before calling this. |
| 775 | WatcherState *GetWatcherState(const Channel *channel); |
| 776 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 777 | // Returns a Sender's protected RawSender. |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 778 | template <typename T> |
| 779 | static RawSender *GetRawSender(aos::Sender<T> *sender) { |
| 780 | return sender->sender_.get(); |
| 781 | } |
| 782 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 783 | // Returns a Fetcher's protected RawFetcher. |
| 784 | template <typename T> |
| 785 | static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) { |
| 786 | return fetcher->fetcher_.get(); |
| 787 | } |
| 788 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 789 | // Context available for watchers, timers, and phased loops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 790 | Context context_; |
| 791 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 792 | friend class RawSender; |
| 793 | friend class TimerHandler; |
| 794 | friend class RawFetcher; |
| 795 | friend class PhasedLoopHandler; |
| 796 | friend class WatcherState; |
| 797 | |
| 798 | // Methods used to implement timing reports. |
| 799 | void NewSender(RawSender *sender); |
| 800 | void DeleteSender(RawSender *sender); |
| 801 | TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer); |
| 802 | PhasedLoopHandler *NewPhasedLoop( |
| 803 | std::unique_ptr<PhasedLoopHandler> phased_loop); |
| 804 | void NewFetcher(RawFetcher *fetcher); |
| 805 | void DeleteFetcher(RawFetcher *fetcher); |
| 806 | WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher); |
| 807 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 808 | // Tracks that we have a (single) watcher on the given channel. |
| 809 | void TakeWatcher(const Channel *channel); |
| 810 | // Tracks that we have at least one sender on the given channel. |
| 811 | void TakeSender(const Channel *channel); |
| 812 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 813 | std::vector<RawSender *> senders_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 814 | std::vector<RawFetcher *> fetchers_; |
| 815 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 816 | std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 817 | std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 818 | std::vector<std::unique_ptr<WatcherState>> watchers_; |
| 819 | |
Brian Silverman | ce418d0 | 2021-11-03 11:25:52 -0700 | [diff] [blame] | 820 | // Does nothing if timing reports are disabled. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 821 | void SendTimingReport(); |
Brian Silverman | ce418d0 | 2021-11-03 11:25:52 -0700 | [diff] [blame] | 822 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 823 | void UpdateTimingReport(); |
| 824 | void MaybeScheduleTimingReports(); |
| 825 | |
| 826 | std::unique_ptr<RawSender> timing_report_sender_; |
| 827 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 828 | // Tracks which event sources (timers and watchers) have data, and which |
| 829 | // don't. Added events may not change their event_time(). |
| 830 | // TODO(austin): Test case 1: timer triggers at t1, handler takes until after |
| 831 | // t2 to run, t2 should then be picked up without a context switch. |
| 832 | void AddEvent(EventLoopEvent *event); |
| 833 | void RemoveEvent(EventLoopEvent *event); |
| 834 | size_t EventCount() const { return events_.size(); } |
| 835 | EventLoopEvent *PopEvent(); |
| 836 | EventLoopEvent *PeekEvent() { return events_.front(); } |
| 837 | void ReserveEvents(); |
| 838 | |
| 839 | std::vector<EventLoopEvent *> events_; |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 840 | size_t event_generation_ = 1; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 841 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 842 | // If true, don't send AOS_LOG to /aos |
| 843 | bool skip_logger_ = false; |
| 844 | |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 845 | // Sets context_ for a timed event which is supposed to happen at the provided |
| 846 | // time. |
| 847 | void SetTimerContext(monotonic_clock::time_point monotonic_event_time); |
| 848 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 849 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 850 | virtual pid_t GetTid() = 0; |
| 851 | |
| 852 | FlatbufferDetachedBuffer<timing::Report> timing_report_; |
| 853 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 854 | ::std::atomic<bool> is_running_{false}; |
| 855 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 856 | const Configuration *configuration_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 857 | |
| 858 | // If true, don't send out timing reports. |
| 859 | bool skip_timing_report_ = false; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 860 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 861 | SendFailureCounter timing_report_failure_counter_; |
| 862 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 863 | absl::btree_set<const Channel *> taken_watchers_, taken_senders_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 864 | }; |
| 865 | |
| 866 | } // namespace aos |
| 867 | |
Austin Schuh | b807581 | 2020-10-19 09:36:49 -0700 | [diff] [blame] | 868 | #include "aos/events/event_loop_tmpl.h" // IWYU pragma: export |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 869 | |
| 870 | #endif // AOS_EVENTS_EVENT_LOOP_H |