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" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 12 | #include "flatbuffers/flatbuffers.h" |
| 13 | #include "glog/logging.h" |
| 14 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 15 | #include "aos/configuration.h" |
| 16 | #include "aos/configuration_generated.h" |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 17 | #include "aos/events/channel_preallocated_allocator.h" |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 18 | #include "aos/events/context.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 19 | #include "aos/events/event_loop_event.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 20 | #include "aos/events/event_loop_generated.h" |
| 21 | #include "aos/events/timing_statistics.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 22 | #include "aos/flatbuffers.h" |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 23 | #include "aos/flatbuffers/builder.h" |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 24 | #include "aos/ftrace.h" |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 25 | #include "aos/ipc_lib/data_alignment.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 26 | #include "aos/json_to_flatbuffer.h" |
| 27 | #include "aos/time/time.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 28 | #include "aos/util/phased_loop.h" |
Austin Schuh | 4385b14 | 2021-03-14 21:31:13 -0700 | [diff] [blame] | 29 | #include "aos/uuid.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 31 | DECLARE_bool(timing_reports); |
| 32 | DECLARE_int32(timing_report_ms); |
| 33 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 34 | namespace aos { |
| 35 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 36 | class EventLoop; |
| 37 | class WatcherState; |
| 38 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 39 | // Raw version of fetcher. Contains a local variable that the fetcher will |
| 40 | // update. This is used for reflection and as an interface to implement typed |
| 41 | // fetchers. |
| 42 | class RawFetcher { |
| 43 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 44 | RawFetcher(EventLoop *event_loop, const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 45 | RawFetcher(const RawFetcher &) = delete; |
| 46 | RawFetcher &operator=(const RawFetcher &) = delete; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 47 | virtual ~RawFetcher(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 48 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 49 | // Fetches the next message in the queue without blocking. Returns true if |
| 50 | // there was a new message and we got it. |
| 51 | bool FetchNext(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 52 | // Fetches the next message if there is one, and the provided function returns |
| 53 | // true. The data and buffer_index are the only pieces of the Context which |
| 54 | // are zeroed out. The function must be valid. |
| 55 | bool FetchNextIf(std::function<bool(const Context &context)> fn); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 56 | |
| 57 | // Fetches the latest message without blocking. |
| 58 | bool Fetch(); |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 59 | // Fetches the latest message conditionally without blocking. fn must be |
| 60 | // valid. |
| 61 | bool FetchIf(std::function<bool(const Context &context)> fn); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 62 | |
| 63 | // Returns the channel this fetcher uses. |
| 64 | const Channel *channel() const { return channel_; } |
| 65 | // Returns the context for the current message. |
| 66 | const Context &context() const { return context_; } |
| 67 | |
| 68 | protected: |
| 69 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 70 | const EventLoop *event_loop() const { return event_loop_; } |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 71 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 72 | Context context_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 73 | |
| 74 | private: |
| 75 | friend class EventLoop; |
| 76 | // Implementation |
| 77 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0; |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 78 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchNextIf( |
| 79 | std::function<bool(const Context &)> fn) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 80 | virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0; |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 81 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchIf( |
| 82 | std::function<bool(const Context &)> fn) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 83 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 84 | EventLoop *const event_loop_; |
| 85 | const Channel *const channel_; |
| 86 | const std::string ftrace_prefix_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 87 | |
| 88 | internal::RawFetcherTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 89 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Austin Schuh | e0ab4de | 2023-05-03 08:05:08 -0700 | [diff] [blame] | 92 | using SharedSpan = std::shared_ptr<const absl::Span<const uint8_t>>; |
| 93 | |
| 94 | // Holds storage for a span object and the data referenced by that span for |
| 95 | // compatibility with SharedSpan users. If constructed with MakeSharedSpan, span |
| 96 | // points to only the aligned segment of the entire data. |
| 97 | struct AlignedOwningSpan { |
| 98 | AlignedOwningSpan(absl::Span<const uint8_t> new_span) : span(new_span) {} |
| 99 | |
| 100 | AlignedOwningSpan(const AlignedOwningSpan &) = delete; |
| 101 | AlignedOwningSpan &operator=(const AlignedOwningSpan &) = delete; |
| 102 | absl::Span<const uint8_t> span; |
| 103 | char *data() { return reinterpret_cast<char *>(this + 1); } |
| 104 | }; |
| 105 | |
| 106 | // Constructs a span which owns its data through a shared_ptr. The owning span |
| 107 | // points to a const view of the data; also returns a temporary mutable span |
| 108 | // which is only valid while the const shared span is kept alive. |
| 109 | std::pair<SharedSpan, absl::Span<uint8_t>> MakeSharedSpan(size_t size); |
| 110 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | // Raw version of sender. Sends a block of data. This is used for reflection |
| 112 | // and as a building block to implement typed senders. |
| 113 | class RawSender { |
| 114 | public: |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 115 | using SharedSpan = std::shared_ptr<const absl::Span<const uint8_t>>; |
| 116 | |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 117 | enum class [[nodiscard]] Error { |
| 118 | // Represents success and no error |
| 119 | kOk, |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 120 | |
Austin Schuh | 50e3dca | 2023-07-23 14:34:27 -0700 | [diff] [blame] | 121 | // Error for messages on channels being sent faster than their |
| 122 | // frequency and channel storage duration allow |
| 123 | kMessagesSentTooFast, |
| 124 | // Access to Redzone was attempted in Sender Queue |
| 125 | kInvalidRedzone, |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 126 | }; |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 127 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 128 | RawSender(EventLoop *event_loop, const Channel *channel); |
| 129 | RawSender(const RawSender &) = delete; |
| 130 | RawSender &operator=(const RawSender &) = delete; |
| 131 | |
| 132 | virtual ~RawSender(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 133 | |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 134 | // Returns the buffer to write new messages into. This is always valid, and |
| 135 | // may change after calling any of the Send functions. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 136 | virtual void *data() = 0; |
| 137 | virtual size_t size() = 0; |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 138 | |
| 139 | // Sends a message without copying it. The users starts by copying up to |
| 140 | // size() bytes into the data backed by data(). They then call Send to send. |
| 141 | // Returns Error::kOk on a successful send, or |
| 142 | // Error::kMessagesSentTooFast if messages were sent too fast. If provided, |
| 143 | // monotonic_remote_time, realtime_remote_time, and remote_queue_index are |
| 144 | // attached to the message and are available in the context on the read side. |
| 145 | // If they are not populated, the read side will get the sent times instead. |
| 146 | Error Send(size_t size); |
| 147 | Error Send(size_t size, monotonic_clock::time_point monotonic_remote_time, |
| 148 | realtime_clock::time_point realtime_remote_time, |
| 149 | uint32_t remote_queue_index, const UUID &source_boot_uuid); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 150 | |
| 151 | // Sends a single block of data by copying it. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 152 | // The remote arguments have the same meaning as in Send above. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 153 | // Returns Error::kMessagesSentTooFast if messages were sent too fast |
| 154 | Error Send(const void *data, size_t size); |
| 155 | Error Send(const void *data, size_t size, |
| 156 | monotonic_clock::time_point monotonic_remote_time, |
| 157 | realtime_clock::time_point realtime_remote_time, |
| 158 | uint32_t remote_queue_index, const UUID &source_boot_uuid); |
| 159 | |
| 160 | // CHECKs that no sending Error occurred and logs the channel_ data if |
| 161 | // one did |
| 162 | void CheckOk(const Error err); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 163 | |
Tyler Chatow | b7c6eba | 2021-07-28 14:43:23 -0700 | [diff] [blame] | 164 | // Sends a single block of data by refcounting it to avoid copies. The data |
| 165 | // must not change after being passed into Send. The remote arguments have the |
| 166 | // same meaning as in Send above. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 167 | Error Send(const SharedSpan data); |
| 168 | Error Send(const SharedSpan data, |
| 169 | monotonic_clock::time_point monotonic_remote_time, |
| 170 | realtime_clock::time_point realtime_remote_time, |
| 171 | uint32_t remote_queue_index, const UUID &remote_boot_uuid); |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 172 | const Channel *channel() const { return channel_; } |
| 173 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 174 | // Returns the time_points that the last message was sent at. |
| 175 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 176 | return monotonic_sent_time_; |
| 177 | } |
| 178 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 179 | return realtime_sent_time_; |
| 180 | } |
| 181 | // Returns the queue index that this was sent with. |
| 182 | uint32_t sent_queue_index() const { return sent_queue_index_; } |
| 183 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 184 | // Returns the associated flatbuffers-style allocator. This must be |
| 185 | // deallocated before the message is sent. |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 186 | ChannelPreallocatedAllocator *fbb_allocator() { |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 187 | CHECK(!static_allocator_.has_value()) |
| 188 | << ": May not mix-and-match static and raw flatbuffer builders."; |
| 189 | if (fbb_allocator_.has_value()) { |
| 190 | CHECK(!fbb_allocator_.value().allocated()) |
| 191 | << ": May not have multiple active allocators on a single sender."; |
| 192 | } |
| 193 | return &fbb_allocator_.emplace(reinterpret_cast<uint8_t *>(data()), size(), |
| 194 | channel()); |
| 195 | } |
| 196 | |
| 197 | fbs::SpanAllocator *static_allocator() { |
| 198 | CHECK(!fbb_allocator_.has_value()) |
| 199 | << ": May not mix-and-match static and raw flatbuffer builders."; |
| 200 | return &static_allocator_.emplace( |
| 201 | std::span<uint8_t>{reinterpret_cast<uint8_t *>(data()), size()}); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 204 | // Index of the buffer which is currently exposed by data() and the various |
| 205 | // other accessors. This is the message the caller should be filling out. |
| 206 | virtual int buffer_index() = 0; |
| 207 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 208 | protected: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 209 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 210 | const EventLoop *event_loop() const { return event_loop_; } |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 211 | |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 212 | monotonic_clock::time_point monotonic_sent_time_ = monotonic_clock::min_time; |
| 213 | realtime_clock::time_point realtime_sent_time_ = realtime_clock::min_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 214 | uint32_t sent_queue_index_ = 0xffffffff; |
| 215 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 216 | private: |
| 217 | friend class EventLoop; |
| 218 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 219 | virtual Error DoSend(const void *data, size_t size, |
| 220 | monotonic_clock::time_point monotonic_remote_time, |
| 221 | realtime_clock::time_point realtime_remote_time, |
| 222 | uint32_t remote_queue_index, |
| 223 | const UUID &source_boot_uuid) = 0; |
| 224 | virtual Error DoSend(size_t size, |
| 225 | monotonic_clock::time_point monotonic_remote_time, |
| 226 | realtime_clock::time_point realtime_remote_time, |
| 227 | uint32_t remote_queue_index, |
| 228 | const UUID &source_boot_uuid) = 0; |
| 229 | virtual Error DoSend(const SharedSpan data, |
| 230 | monotonic_clock::time_point monotonic_remote_time, |
| 231 | realtime_clock::time_point realtime_remote_time, |
| 232 | uint32_t remote_queue_index, |
| 233 | const UUID &source_boot_uuid); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 234 | |
James Kuszmaul | 93abac1 | 2022-04-14 15:05:10 -0700 | [diff] [blame] | 235 | void RecordSendResult(const Error error, size_t message_size); |
| 236 | |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 237 | EventLoop *const event_loop_; |
| 238 | const Channel *const channel_; |
| 239 | const std::string ftrace_prefix_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 240 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 241 | internal::RawSenderTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 242 | Ftrace ftrace_; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 243 | |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 244 | // Depending on which API is being used, we will populate either |
| 245 | // fbb_allocator_ (for use with FlatBufferBuilders) or the SpanAllocator (for |
| 246 | // use with the static flatbuffer API). |
| 247 | std::optional<ChannelPreallocatedAllocator> fbb_allocator_; |
| 248 | std::optional<fbs::SpanAllocator> static_allocator_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 249 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 250 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 251 | // Needed for compatibility with glog |
| 252 | std::ostream &operator<<(std::ostream &os, const RawSender::Error err); |
| 253 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 254 | // Fetches the newest message from a channel. |
| 255 | // This provides a polling based interface for channels. |
| 256 | template <typename T> |
| 257 | class Fetcher { |
| 258 | public: |
| 259 | Fetcher() {} |
| 260 | |
| 261 | // Fetches the next message. Returns true if it fetched a new message. This |
| 262 | // method will only return messages sent after the Fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 263 | bool FetchNext() { |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 264 | const bool result = CHECK_NOTNULL(fetcher_)->FetchNext(); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 265 | if (result) { |
| 266 | CheckChannelDataAlignment(fetcher_->context().data, |
| 267 | fetcher_->context().size); |
| 268 | } |
| 269 | return result; |
| 270 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 271 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 272 | // Fetches the next message if there is one, and the provided function returns |
| 273 | // true. The data and buffer_index are the only pieces of the Context which |
| 274 | // are zeroed out. The function must be valid. |
| 275 | bool FetchNextIf(std::function<bool(const Context &)> fn) { |
| 276 | const bool result = CHECK_NOTNULL(fetcher_)->FetchNextIf(std::move(fn)); |
| 277 | if (result) { |
| 278 | CheckChannelDataAlignment(fetcher_->context().data, |
| 279 | fetcher_->context().size); |
| 280 | } |
| 281 | return result; |
| 282 | } |
| 283 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 284 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 285 | // This will return the latest message regardless of if it was sent before or |
| 286 | // after the fetcher was created. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 287 | bool Fetch() { |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 288 | const bool result = CHECK_NOTNULL(fetcher_)->Fetch(); |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 289 | if (result) { |
| 290 | CheckChannelDataAlignment(fetcher_->context().data, |
| 291 | fetcher_->context().size); |
| 292 | } |
| 293 | return result; |
| 294 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 295 | |
Austin Schuh | 98ed26f | 2023-07-19 14:12:28 -0700 | [diff] [blame] | 296 | // Fetches the most recent message conditionally. Returns true if it fetched a |
| 297 | // new message. This will return the latest message regardless of if it was |
| 298 | // sent before or after the fetcher was created. The function must be valid. |
| 299 | bool FetchIf(std::function<bool(const Context &)> fn) { |
| 300 | const bool result = CHECK_NOTNULL(fetcher_)->FetchIf(std::move(fn)); |
| 301 | if (result) { |
| 302 | CheckChannelDataAlignment(fetcher_->context().data, |
| 303 | fetcher_->context().size); |
| 304 | } |
| 305 | return result; |
| 306 | } |
| 307 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 308 | // Returns a pointer to the contained flatbuffer, or nullptr if there is no |
| 309 | // available message. |
| 310 | const T *get() const { |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 311 | return CHECK_NOTNULL(fetcher_)->context().data != nullptr |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 312 | ? flatbuffers::GetRoot<T>( |
| 313 | reinterpret_cast<const char *>(fetcher_->context().data)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 314 | : nullptr; |
| 315 | } |
| 316 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 317 | // Returns the channel this fetcher uses |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 318 | const Channel *channel() const { return CHECK_NOTNULL(fetcher_)->channel(); } |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 319 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 320 | // Returns the context holding timestamps and other metadata about the |
| 321 | // message. |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 322 | const Context &context() const { return CHECK_NOTNULL(fetcher_)->context(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 323 | |
| 324 | const T &operator*() const { return *get(); } |
| 325 | const T *operator->() const { return get(); } |
| 326 | |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 327 | // Returns true if this fetcher is valid and connected to a channel. If you, |
| 328 | // e.g., are using TryMakeFetcher, then you must check valid() before |
| 329 | // attempting to use the Fetcher. |
Milind Upadhyay | 49174a7 | 2021-04-10 16:24:57 -0700 | [diff] [blame] | 330 | bool valid() const { return static_cast<bool>(fetcher_); } |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 331 | |
Austin Schuh | ca75b6a | 2020-12-15 21:12:24 -0800 | [diff] [blame] | 332 | // Copies the current flatbuffer into a FlatbufferVector. |
| 333 | FlatbufferVector<T> CopyFlatBuffer() const { |
| 334 | return context().template CopyFlatBuffer<T>(); |
| 335 | } |
| 336 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 337 | private: |
| 338 | friend class EventLoop; |
| 339 | Fetcher(::std::unique_ptr<RawFetcher> fetcher) |
| 340 | : fetcher_(::std::move(fetcher)) {} |
| 341 | ::std::unique_ptr<RawFetcher> fetcher_; |
| 342 | }; |
| 343 | |
| 344 | // Sends messages to a channel. |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 345 | // The type T used with the Sender may either be a raw flatbuffer type (e.g., |
| 346 | // aos::examples::Ping) or the static flatbuffer type (e.g. |
| 347 | // aos::examples::PingStatic). The Builder type that you use must correspond |
| 348 | // with the flatbuffer type being used. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 349 | template <typename T> |
| 350 | class Sender { |
| 351 | public: |
| 352 | Sender() {} |
| 353 | |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 354 | // Represents a single message that is about to be sent on the channel. |
| 355 | // Uses the static flatbuffer API rather than the FlatBufferBuilder paradigm. |
| 356 | // |
| 357 | // Typical usage pattern is: |
| 358 | // |
| 359 | // Sender<PingStatic>::Builder builder = sender.MakeStaticBuilder() |
| 360 | // builder.get()->set_value(971); |
| 361 | // builder.CheckOk(builder.Send()); |
| 362 | class StaticBuilder { |
| 363 | public: |
| 364 | StaticBuilder(RawSender *sender, fbs::SpanAllocator *allocator) |
| 365 | : builder_(allocator), sender_(CHECK_NOTNULL(sender)) {} |
| 366 | StaticBuilder(const StaticBuilder &) = delete; |
| 367 | StaticBuilder(StaticBuilder &&) = default; |
| 368 | |
| 369 | StaticBuilder &operator=(const StaticBuilder &) = delete; |
| 370 | StaticBuilder &operator=(StaticBuilder &&) = default; |
| 371 | |
| 372 | fbs::Builder<T> *builder() { |
| 373 | DCHECK(builder_.has_value()); |
| 374 | return &builder_.value(); |
| 375 | } |
| 376 | |
| 377 | T *get() { return builder()->get(); } |
James Kuszmaul | dde6563 | 2023-12-07 16:12:26 -0800 | [diff] [blame] | 378 | T &operator*() { return *get(); } |
| 379 | T *operator->() { return get(); } |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 380 | |
| 381 | RawSender::Error Send() { |
| 382 | const auto err = sender_->Send(builder_.value().buffer().size()); |
| 383 | builder_.reset(); |
| 384 | return err; |
| 385 | } |
| 386 | |
| 387 | // Equivalent to RawSender::CheckOk |
| 388 | void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); }; |
| 389 | |
| 390 | private: |
| 391 | std::optional<fbs::Builder<T>> builder_; |
| 392 | RawSender *sender_; |
| 393 | }; |
| 394 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 395 | // Represents a single message about to be sent to the queue. |
| 396 | // The lifecycle goes: |
| 397 | // |
| 398 | // Builder builder = sender.MakeBuilder(); |
| 399 | // T::Builder t_builder = builder.MakeBuilder<T>(); |
| 400 | // Populate(&t_builder); |
| 401 | // builder.Send(t_builder.Finish()); |
| 402 | class Builder { |
| 403 | public: |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 404 | Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator) |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 405 | : fbb_(allocator->size(), allocator), |
| 406 | allocator_(allocator), |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 407 | sender_(CHECK_NOTNULL(sender)) { |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 408 | CheckChannelDataAlignment(allocator->data(), allocator->size()); |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 409 | fbb_.ForceDefaults(true); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 410 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 411 | Builder() {} |
| 412 | Builder(const Builder &) = delete; |
| 413 | Builder(Builder &&) = default; |
| 414 | |
| 415 | Builder &operator=(const Builder &) = delete; |
| 416 | Builder &operator=(Builder &&) = default; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 417 | |
| 418 | flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; } |
| 419 | |
| 420 | template <typename T2> |
| 421 | typename T2::Builder MakeBuilder() { |
| 422 | return typename T2::Builder(fbb_); |
| 423 | } |
| 424 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 425 | RawSender::Error Send(flatbuffers::Offset<T> offset) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 426 | fbb_.Finish(offset); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 427 | const auto err = sender_->Send(fbb_.GetSize()); |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 428 | // Ensure fbb_ knows it shouldn't access the memory any more. |
| 429 | fbb_ = flatbuffers::FlatBufferBuilder(); |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 430 | return err; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 431 | } |
| 432 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 433 | // Equivalent to RawSender::CheckOk |
| 434 | void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); }; |
| 435 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 436 | // CHECKs that this message was sent. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 437 | void CheckSent() { |
| 438 | CHECK(!allocator_->is_allocated()) << ": Message was not sent yet"; |
| 439 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 440 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 441 | // Detaches a buffer, for later use calling Sender::Send directly. |
| 442 | // |
| 443 | // Note that the underlying memory remains with the Sender, so creating |
| 444 | // another Builder before destroying the FlatbufferDetachedBuffer will fail. |
| 445 | FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) { |
| 446 | fbb_.Finish(offset); |
| 447 | return fbb_.Release(); |
| 448 | } |
| 449 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 450 | private: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 451 | flatbuffers::FlatBufferBuilder fbb_; |
Austin Schuh | 1af273d | 2020-03-07 20:11:34 -0800 | [diff] [blame] | 452 | ChannelPreallocatedAllocator *allocator_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 453 | RawSender *sender_; |
| 454 | }; |
| 455 | |
| 456 | // Constructs an above builder. |
Brian Silverman | 9dd793b | 2020-01-31 23:52:21 -0800 | [diff] [blame] | 457 | // |
| 458 | // Only a single one of these may be "alive" for this object at any point in |
| 459 | // time. After calling Send on the result, it is no longer "alive". This means |
| 460 | // that you must manually reset a variable holding the return value (by |
| 461 | // assigning a default-constructed Builder to it) before calling this method |
| 462 | // again to overwrite the value in the variable. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 463 | Builder MakeBuilder(); |
James Kuszmaul | b1c1105 | 2023-11-06 13:20:53 -0800 | [diff] [blame] | 464 | StaticBuilder MakeStaticBuilder() { |
| 465 | return StaticBuilder(sender_.get(), sender_->static_allocator()); |
| 466 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 467 | |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 468 | // Sends a prebuilt flatbuffer. |
James Kuszmaul | ad52304 | 2022-10-05 16:47:33 -0700 | [diff] [blame] | 469 | // This will copy the data out of the provided flatbuffer, and so does not |
| 470 | // have to correspond to an existing Builder. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 471 | RawSender::Error Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer); |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 472 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 473 | // Sends a prebuilt flatbuffer which was detached from a Builder created via |
| 474 | // MakeBuilder() on this object. |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 475 | RawSender::Error SendDetached(FlatbufferDetachedBuffer<T> detached); |
| 476 | |
| 477 | // Equivalent to RawSender::CheckOk |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 478 | void CheckOk(const RawSender::Error err) { |
| 479 | CHECK_NOTNULL(sender_)->CheckOk(err); |
| 480 | }; |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 481 | |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 482 | // Returns the name of the underlying queue, if valid. You must check valid() |
| 483 | // first. |
| 484 | const Channel *channel() const { return CHECK_NOTNULL(sender_)->channel(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 485 | |
James Kuszmaul | ad52304 | 2022-10-05 16:47:33 -0700 | [diff] [blame] | 486 | // Returns true if the Sender is a valid Sender. If you, e.g., are using |
| 487 | // TryMakeSender, then you must check valid() before attempting to use the |
| 488 | // Sender. |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 489 | // TODO(austin): Deprecate the operator bool. |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 490 | operator bool() const { return sender_ ? true : false; } |
Austin Schuh | e33c08d | 2022-02-03 18:15:21 -0800 | [diff] [blame] | 491 | bool valid() const { return static_cast<bool>(sender_); } |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 492 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 493 | // Returns the time_points that the last message was sent at. |
| 494 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 495 | return CHECK_NOTNULL(sender_)->monotonic_sent_time(); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 496 | } |
| 497 | aos::realtime_clock::time_point realtime_sent_time() const { |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 498 | return CHECK_NOTNULL(sender_)->realtime_sent_time(); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 499 | } |
| 500 | // Returns the queue index that this was sent with. |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 501 | uint32_t sent_queue_index() const { |
| 502 | return CHECK_NOTNULL(sender_)->sent_queue_index(); |
| 503 | } |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 504 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 505 | // Returns the buffer index which MakeBuilder() will expose access to. This is |
| 506 | // the buffer the caller can fill out. |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 507 | int buffer_index() const { return CHECK_NOTNULL(sender_)->buffer_index(); } |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 508 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 509 | private: |
| 510 | friend class EventLoop; |
| 511 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 512 | std::unique_ptr<RawSender> sender_; |
| 513 | }; |
| 514 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 515 | // Class for keeping a count of send failures on a certain channel |
| 516 | class SendFailureCounter { |
| 517 | public: |
| 518 | inline void Count(const RawSender::Error err) { |
| 519 | failures_ += static_cast<size_t>(err != RawSender::Error::kOk); |
| 520 | just_failed_ = (err != RawSender::Error::kOk); |
| 521 | } |
| 522 | |
| 523 | inline size_t failures() const { return failures_; } |
| 524 | inline bool just_failed() const { return just_failed_; } |
| 525 | |
| 526 | private: |
| 527 | size_t failures_ = 0; |
| 528 | bool just_failed_ = false; |
| 529 | }; |
| 530 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 531 | // Interface for timers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 532 | class TimerHandler { |
| 533 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 534 | virtual ~TimerHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 535 | |
| 536 | // Timer should sleep until base, base + offset, base + offset * 2, ... |
| 537 | // If repeat_offset isn't set, the timer only expires once. |
James Kuszmaul | 8866e64 | 2022-06-10 16:00:36 -0700 | [diff] [blame] | 538 | // base must be greater than or equal to zero. |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 539 | virtual void Schedule(monotonic_clock::time_point base, |
| 540 | monotonic_clock::duration repeat_offset = |
| 541 | ::aos::monotonic_clock::zero()) = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 542 | |
| 543 | // Stop future calls to callback(). |
| 544 | virtual void Disable() = 0; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 545 | |
Naman Gupta | 4d13b0a | 2022-10-19 16:41:24 -0700 | [diff] [blame] | 546 | // Check if the timer is disabled |
| 547 | virtual bool IsDisabled() = 0; |
| 548 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 549 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 550 | // name in the timing report. |
| 551 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 552 | const std::string_view name() const { return name_; } |
| 553 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 554 | protected: |
| 555 | TimerHandler(EventLoop *event_loop, std::function<void()> fn); |
| 556 | |
Austin Schuh | 9b1d628 | 2022-06-10 17:03:21 -0700 | [diff] [blame] | 557 | template <typename T> |
| 558 | monotonic_clock::time_point Call(T get_time, |
| 559 | monotonic_clock::time_point event_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 560 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 561 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 562 | friend class EventLoop; |
| 563 | |
| 564 | EventLoop *event_loop_; |
| 565 | // The function to call when Call is called. |
| 566 | std::function<void()> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 567 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 568 | |
| 569 | internal::TimerTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 570 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 571 | }; |
| 572 | |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 573 | // Interface for phased loops. They are built on timers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 574 | class PhasedLoopHandler { |
| 575 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 576 | virtual ~PhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 577 | |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 578 | // Sets the interval and offset. Any changes to interval and offset only take |
| 579 | // effect when the handler finishes running or upon a call to Reschedule(). |
| 580 | // |
| 581 | // The precise semantics of the monotonic_now parameter are defined in |
| 582 | // phased_loop.h. The way that it gets used in this interface is to control |
| 583 | // what the cycles elapsed counter will read on the following call to your |
| 584 | // handler. In an idealized simulation environment, if you call |
| 585 | // set_interval_and_offset() during the phased loop offset without setting |
| 586 | // monotonic_now, then you should always see a count of 1 on the next cycle. |
| 587 | // |
| 588 | // With the default behavior (this is called inside your handler and with |
| 589 | // monotonic_now = nullopt), the next phased loop call will occur at most |
| 590 | // interval time after the current time. Note that in many cases this will |
| 591 | // *not* be the preferred behavior (in most cases, you would likely aim to |
| 592 | // keep the average frequency of the calls reasonably consistent). |
| 593 | // |
| 594 | // A combination of the monotonic_now parameter and manually calling |
| 595 | // Reschedule() outside of the phased loop handler can be used to alter the |
| 596 | // behavior of cycles_elapsed. For the default behavior, you can set |
| 597 | // monotonic_now to the current time. If you call set_interval_and_offset() |
| 598 | // and Reschedule() with the same monotonic_now, that will cause the next |
| 599 | // callback to occur in the range (monotonic_now, monotonic_now + interval] |
| 600 | // and get called with a count of 1 (if the event is actually able to happen |
| 601 | // when it is scheduled to). E.g., if you are just adjusting the phased loop |
| 602 | // offset (but not the interval) and want to maintain a consistent frequency, |
| 603 | // you may call these functions with monotonic_now = now + interval / 2. |
| 604 | void set_interval_and_offset( |
| 605 | const monotonic_clock::duration interval, |
| 606 | const monotonic_clock::duration offset, |
| 607 | std::optional<monotonic_clock::time_point> monotonic_now = std::nullopt) { |
| 608 | phased_loop_.set_interval_and_offset(interval, offset, monotonic_now); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 609 | } |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 610 | |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 611 | // Reruns the scheduler for the phased loop, scheduling the next callback at |
| 612 | // the next available time after monotonic_now. This allows |
| 613 | // set_interval_and_offset() to be called and have the changes take effect |
| 614 | // before the next handler finishes. This will have no effect if run during |
| 615 | // the phased loop's own handler. |
| 616 | void Reschedule(monotonic_clock::time_point monotonic_now) { |
| 617 | cycles_elapsed_ += phased_loop_.Iterate(monotonic_now); |
| 618 | Schedule(phased_loop_.sleep_time()); |
| 619 | } |
| 620 | |
| 621 | // Sets and gets the name of the timer. Set this if you want a descriptive |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 622 | // name in the timing report. |
| 623 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 624 | const std::string_view name() const { return name_; } |
| 625 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 626 | protected: |
James Kuszmaul | 20dcc7c | 2023-01-20 11:06:31 -0800 | [diff] [blame] | 627 | void Call(std::function<monotonic_clock::time_point()> get_time); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 628 | |
| 629 | PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn, |
| 630 | const monotonic_clock::duration interval, |
| 631 | const monotonic_clock::duration offset); |
| 632 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 633 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 634 | friend class EventLoop; |
| 635 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 636 | virtual void Schedule(monotonic_clock::time_point sleep_time) = 0; |
| 637 | |
| 638 | EventLoop *event_loop_; |
| 639 | std::function<void(int)> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 640 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 641 | time::PhasedLoop phased_loop_; |
| 642 | |
| 643 | int cycles_elapsed_ = 0; |
| 644 | |
| 645 | internal::TimerTiming timing_; |
Brian Silverman | 79ec7fc | 2020-06-08 20:11:22 -0500 | [diff] [blame] | 646 | Ftrace ftrace_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 647 | }; |
| 648 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 649 | // Note, it is supported to create only: |
| 650 | // multiple fetchers, and (one sender or one watcher) per <name, type> |
| 651 | // tuple. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 652 | class EventLoop { |
| 653 | public: |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 654 | // Holds configuration by reference for the lifetime of this object. It may |
| 655 | // never be mutated externally in any way. |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 656 | EventLoop(const Configuration *configuration); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 657 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 658 | virtual ~EventLoop(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 659 | |
| 660 | // Current time. |
Stephan Pleines | 559fa6c | 2022-01-06 17:23:51 -0800 | [diff] [blame] | 661 | virtual monotonic_clock::time_point monotonic_now() const = 0; |
| 662 | virtual realtime_clock::time_point realtime_now() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 663 | |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 664 | template <typename T> |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 665 | const Channel *GetChannel(const std::string_view channel_name) { |
Austin Schuh | caa2a5d | 2020-11-01 22:38:20 -0800 | [diff] [blame] | 666 | return configuration::GetChannel(configuration(), channel_name, |
Austin Schuh | 0de30f3 | 2020-12-06 12:44:28 -0800 | [diff] [blame] | 667 | T::GetFullyQualifiedName(), name(), node(), |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 668 | true); |
| 669 | } |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 670 | // Returns true if the channel exists in the configuration. |
Austin Schuh | a065415 | 2021-02-21 21:38:24 -0800 | [diff] [blame] | 671 | template <typename T> |
| 672 | bool HasChannel(const std::string_view channel_name) { |
| 673 | return GetChannel<T>(channel_name) != nullptr; |
Brian Silverman | de9f3ff | 2020-04-28 16:56:58 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 676 | // Like MakeFetcher, but returns an invalid fetcher if the given channel is |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 677 | // not readable on this node or does not exist. You must check valid() on the |
| 678 | // Fetcher before using it. |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 679 | template <typename T> |
| 680 | Fetcher<T> TryMakeFetcher(const std::string_view channel_name) { |
| 681 | const Channel *const channel = GetChannel<T>(channel_name); |
| 682 | if (channel == nullptr) { |
| 683 | return Fetcher<T>(); |
| 684 | } |
| 685 | |
| 686 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 687 | return Fetcher<T>(); |
| 688 | } |
| 689 | |
| 690 | return Fetcher<T>(MakeRawFetcher(channel)); |
| 691 | } |
| 692 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 693 | // Makes a class that will always fetch the most recent value |
| 694 | // sent to the provided channel. |
| 695 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 696 | Fetcher<T> MakeFetcher(const std::string_view channel_name) { |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 697 | CHECK(HasChannel<T>(channel_name)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 698 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 699 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 700 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 701 | Fetcher<T> result = TryMakeFetcher<T>(channel_name); |
| 702 | if (!result.valid()) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 703 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 704 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 705 | << "\" } is not able to be fetched on this node. Check your " |
| 706 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 709 | return result; |
| 710 | } |
| 711 | |
| 712 | // Like MakeSender, but returns an invalid sender if the given channel is |
Sanjay Narayanan | 570dc93 | 2022-12-06 14:12:04 -0800 | [diff] [blame] | 713 | // not sendable on this node or does not exist. You must check valid() on the |
| 714 | // Sender before using it. |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 715 | template <typename T> |
| 716 | Sender<T> TryMakeSender(const std::string_view channel_name) { |
| 717 | const Channel *channel = GetChannel<T>(channel_name); |
| 718 | if (channel == nullptr) { |
| 719 | return Sender<T>(); |
| 720 | } |
| 721 | |
| 722 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 723 | return Sender<T>(); |
| 724 | } |
| 725 | |
| 726 | return Sender<T>(MakeRawSender(channel)); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | // Makes class that allows constructing and sending messages to |
| 730 | // the provided channel. |
| 731 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 732 | Sender<T> MakeSender(const std::string_view channel_name) { |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 733 | CHECK(HasChannel<T>(channel_name)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 734 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
Austin Schuh | 28fedcb | 2020-02-08 15:59:58 -0800 | [diff] [blame] | 735 | << T::GetFullyQualifiedName() << "\" } not found in config for " |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 736 | << name() |
Austin Schuh | caa2a5d | 2020-11-01 22:38:20 -0800 | [diff] [blame] | 737 | << (configuration::MultiNode(configuration()) |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 738 | ? absl::StrCat(" on node ", node()->name()->string_view()) |
| 739 | : "."); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 740 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 741 | Sender<T> result = TryMakeSender<T>(channel_name); |
| 742 | if (!result) { |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame] | 743 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 744 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 745 | << "\" } is not able to be sent on this node. Check your " |
| 746 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 749 | return result; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // This will watch messages sent to the provided channel. |
| 753 | // |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 754 | // w must have a non-polymorphic operator() (aka it can only be called with a |
| 755 | // single set of arguments; no overloading or templates). It must be callable |
| 756 | // with this signature: |
| 757 | // void(const MessageType &); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 758 | // |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 759 | // Lambdas are a common form for w. A std::function will work too. |
| 760 | // |
| 761 | // Note that bind expressions have polymorphic call operators, so they are not |
| 762 | // allowed. |
| 763 | // |
| 764 | // We template Watch as a whole instead of using std::function<void(const T |
| 765 | // &)> to allow deducing MessageType from lambdas and other things which are |
| 766 | // implicitly convertible to std::function, but not actually std::function |
| 767 | // instantiations. Template deduction guides might allow solving this |
| 768 | // differently in newer versions of C++, but those have their own corner |
| 769 | // cases. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 770 | template <typename Watch> |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 771 | void MakeWatcher(const std::string_view channel_name, Watch &&w); |
| 772 | |
| 773 | // Like MakeWatcher, but doesn't have access to the message data. This may be |
| 774 | // implemented to use less resources than an equivalent MakeWatcher. |
| 775 | // |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 776 | // The function will still have access to context(), although that will have |
| 777 | // its data field set to nullptr. |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 778 | template <typename MessageType> |
| 779 | void MakeNoArgWatcher(const std::string_view channel_name, |
| 780 | std::function<void()> w); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 781 | |
| 782 | // The passed in function will be called when the event loop starts. |
| 783 | // Use this to run code once the thread goes into "real-time-mode", |
| 784 | virtual void OnRun(::std::function<void()> on_run) = 0; |
| 785 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 786 | // Gets the name of the event loop. This is the application name. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 787 | virtual const std::string_view name() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 788 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 789 | // Returns the node that this event loop is running on. Returns nullptr if we |
| 790 | // are running in single-node mode. |
| 791 | virtual const Node *node() const = 0; |
| 792 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 793 | // Creates a timer that executes callback when the timer expires |
| 794 | // Returns a TimerHandle for configuration of the timer |
milind-u | 61227f2 | 2021-08-29 15:58:33 -0700 | [diff] [blame] | 795 | // TODO(milind): callback should take the number of cycles elapsed as a |
| 796 | // parameter. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 797 | virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0; |
| 798 | |
| 799 | // Creates a timer that executes callback periodically at the specified |
| 800 | // interval and offset. Returns a PhasedLoopHandler for interacting with the |
| 801 | // timer. |
| 802 | virtual PhasedLoopHandler *AddPhasedLoop( |
| 803 | ::std::function<void(int)> callback, |
| 804 | const monotonic_clock::duration interval, |
| 805 | const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0; |
| 806 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 807 | // TODO(austin): OnExit for cleanup. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 808 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 809 | // May be safely called from any thread. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 810 | bool is_running() const { return is_running_.load(); } |
| 811 | |
| 812 | // Sets the scheduler priority to run the event loop at. This may not be |
| 813 | // called after we go into "real-time-mode". |
| 814 | virtual void SetRuntimeRealtimePriority(int priority) = 0; |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 815 | // Defaults to 0 if this loop will not run realtime. |
| 816 | virtual int runtime_realtime_priority() const = 0; |
| 817 | |
Austin Schuh | 070019a | 2022-12-20 22:23:09 -0800 | [diff] [blame] | 818 | static cpu_set_t DefaultAffinity(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 819 | |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 820 | // Sets the scheduler affinity to run the event loop with. This may only be |
| 821 | // called before Run(). |
| 822 | virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0; |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame] | 823 | // Defaults to DefaultAffinity() if this loop will not run pinned. |
| 824 | virtual const cpu_set_t &runtime_affinity() const = 0; |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 825 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 826 | // Fetches new messages from the provided channel (path, type). |
| 827 | // |
| 828 | // Note: this channel must be a member of the exact configuration object this |
| 829 | // was built with. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 830 | virtual std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 831 | const Channel *channel) = 0; |
| 832 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 833 | // Watches channel (name, type) for new messages. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 834 | virtual void MakeRawWatcher( |
| 835 | const Channel *channel, |
| 836 | std::function<void(const Context &context, const void *message)> |
| 837 | watcher) = 0; |
| 838 | |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 839 | // Watches channel (name, type) for new messages, without needing to extract |
| 840 | // the message contents. Default implementation simply re-uses MakeRawWatcher. |
| 841 | virtual void MakeRawNoArgWatcher( |
| 842 | const Channel *channel, |
| 843 | std::function<void(const Context &context)> watcher) { |
| 844 | MakeRawWatcher(channel, [watcher](const Context &context, const void *) { |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 845 | Context new_context = context; |
| 846 | new_context.data = nullptr; |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 847 | new_context.buffer_index = -1; |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 848 | watcher(new_context); |
Brian Silverman | 454bc11 | 2020-03-05 14:21:25 -0800 | [diff] [blame] | 849 | }); |
| 850 | } |
| 851 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 852 | // Creates a raw sender for the provided channel. This is used for reflection |
| 853 | // based sending. |
| 854 | // Note: this ignores any node constraints. Ignore at your own peril. |
| 855 | virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0; |
| 856 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 857 | // Returns the context for the current callback. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 858 | const Context &context() const { return context_; } |
| 859 | |
| 860 | // Returns the configuration that this event loop was built with. |
| 861 | const Configuration *configuration() const { return configuration_; } |
| 862 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 863 | // Prevents the event loop from sending a timing report. |
Brian Silverman | bf88992 | 2021-11-10 12:41:57 -0800 | [diff] [blame] | 864 | void SkipTimingReport(); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 865 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 866 | // Prevents AOS_LOG being sent to message on /aos. |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 867 | void SkipAosLog() { skip_logger_ = true; } |
| 868 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 869 | // Returns the number of buffers for this channel. This corresponds with the |
| 870 | // range of Context::buffer_index values for this channel. |
| 871 | virtual int NumberBuffers(const Channel *channel) = 0; |
| 872 | |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 873 | // Returns the boot UUID. |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 874 | virtual const UUID &boot_uuid() const = 0; |
Austin Schuh | 20ac95d | 2020-12-05 17:24:19 -0800 | [diff] [blame] | 875 | |
James Kuszmaul | 762e869 | 2023-07-31 14:57:53 -0700 | [diff] [blame] | 876 | // Sets the version string that will be used in any newly constructed |
| 877 | // EventLoop objects. This can be overridden for individual EventLoop's by |
| 878 | // calling EventLoop::SetVersionString(). The version string is populated into |
| 879 | // the timing report message. Makes a copy of the provided string_view. |
| 880 | static void SetDefaultVersionString(std::string_view version); |
| 881 | |
| 882 | // Overrides the version string for this event loop. Makes a copy of the |
| 883 | // provided string_view. |
| 884 | void SetVersionString(std::string_view version); |
| 885 | |
James Kuszmaul | b740f45 | 2023-11-14 17:44:29 -0800 | [diff] [blame] | 886 | std::optional<std::string_view> VersionString() const { |
| 887 | return version_string_; |
| 888 | } |
| 889 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 890 | protected: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 891 | // Sets the name of the event loop. This is the application name. |
| 892 | virtual void set_name(const std::string_view name) = 0; |
| 893 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 894 | void set_is_running(bool value) { is_running_.store(value); } |
| 895 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 896 | // Validates that channel exists inside configuration_ and finds its index. |
| 897 | int ChannelIndex(const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 898 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 899 | // Returns the state for the watcher on the corresponding channel. This |
| 900 | // watcher must exist before calling this. |
| 901 | WatcherState *GetWatcherState(const Channel *channel); |
| 902 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 903 | // Returns a Sender's protected RawSender. |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 904 | template <typename T> |
| 905 | static RawSender *GetRawSender(aos::Sender<T> *sender) { |
| 906 | return sender->sender_.get(); |
| 907 | } |
| 908 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 909 | // Returns a Fetcher's protected RawFetcher. |
| 910 | template <typename T> |
| 911 | static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) { |
| 912 | return fetcher->fetcher_.get(); |
| 913 | } |
| 914 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 915 | // Context available for watchers, timers, and phased loops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 916 | Context context_; |
| 917 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 918 | friend class RawSender; |
| 919 | friend class TimerHandler; |
| 920 | friend class RawFetcher; |
| 921 | friend class PhasedLoopHandler; |
| 922 | friend class WatcherState; |
| 923 | |
| 924 | // Methods used to implement timing reports. |
| 925 | void NewSender(RawSender *sender); |
| 926 | void DeleteSender(RawSender *sender); |
| 927 | TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer); |
| 928 | PhasedLoopHandler *NewPhasedLoop( |
| 929 | std::unique_ptr<PhasedLoopHandler> phased_loop); |
| 930 | void NewFetcher(RawFetcher *fetcher); |
| 931 | void DeleteFetcher(RawFetcher *fetcher); |
| 932 | WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher); |
| 933 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 934 | // Tracks that we have a (single) watcher on the given channel. |
| 935 | void TakeWatcher(const Channel *channel); |
| 936 | // Tracks that we have at least one sender on the given channel. |
| 937 | void TakeSender(const Channel *channel); |
| 938 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 939 | std::vector<RawSender *> senders_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 940 | std::vector<RawFetcher *> fetchers_; |
| 941 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 942 | std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 943 | std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 944 | std::vector<std::unique_ptr<WatcherState>> watchers_; |
| 945 | |
Brian Silverman | ce418d0 | 2021-11-03 11:25:52 -0700 | [diff] [blame] | 946 | // Does nothing if timing reports are disabled. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 947 | void SendTimingReport(); |
Brian Silverman | ce418d0 | 2021-11-03 11:25:52 -0700 | [diff] [blame] | 948 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 949 | void UpdateTimingReport(); |
| 950 | void MaybeScheduleTimingReports(); |
| 951 | |
| 952 | std::unique_ptr<RawSender> timing_report_sender_; |
| 953 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 954 | // Tracks which event sources (timers and watchers) have data, and which |
| 955 | // don't. Added events may not change their event_time(). |
| 956 | // TODO(austin): Test case 1: timer triggers at t1, handler takes until after |
| 957 | // t2 to run, t2 should then be picked up without a context switch. |
| 958 | void AddEvent(EventLoopEvent *event); |
| 959 | void RemoveEvent(EventLoopEvent *event); |
| 960 | size_t EventCount() const { return events_.size(); } |
| 961 | EventLoopEvent *PopEvent(); |
| 962 | EventLoopEvent *PeekEvent() { return events_.front(); } |
| 963 | void ReserveEvents(); |
| 964 | |
| 965 | std::vector<EventLoopEvent *> events_; |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 966 | size_t event_generation_ = 1; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 967 | |
Tyler Chatow | 67ddb03 | 2020-01-12 14:30:04 -0800 | [diff] [blame] | 968 | // If true, don't send AOS_LOG to /aos |
| 969 | bool skip_logger_ = false; |
| 970 | |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 971 | // Sets context_ for a timed event which is supposed to happen at the provided |
| 972 | // time. |
| 973 | void SetTimerContext(monotonic_clock::time_point monotonic_event_time); |
Austin Schuh | 0debde1 | 2022-08-17 16:25:17 -0700 | [diff] [blame] | 974 | // Clears context_ so it only has invalid times and elements in it. |
| 975 | void ClearContext(); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 976 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 977 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 978 | virtual pid_t GetTid() = 0; |
| 979 | |
James Kuszmaul | 762e869 | 2023-07-31 14:57:53 -0700 | [diff] [blame] | 980 | // Default version string to be used in the timing report for any newly |
| 981 | // created EventLoop objects. |
| 982 | static std::optional<std::string> default_version_string_; |
| 983 | |
| 984 | // Timing report version string for this event loop. |
| 985 | std::optional<std::string> version_string_; |
| 986 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 987 | FlatbufferDetachedBuffer<timing::Report> timing_report_; |
| 988 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 989 | ::std::atomic<bool> is_running_{false}; |
| 990 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 991 | const Configuration *configuration_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 992 | |
| 993 | // If true, don't send out timing reports. |
| 994 | bool skip_timing_report_ = false; |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 995 | |
milind | 1f1dca3 | 2021-07-03 13:50:07 -0700 | [diff] [blame] | 996 | SendFailureCounter timing_report_failure_counter_; |
| 997 | |
Brian Silverman | 0fc6993 | 2020-01-24 21:54:02 -0800 | [diff] [blame] | 998 | absl::btree_set<const Channel *> taken_watchers_, taken_senders_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 999 | }; |
| 1000 | |
Brian Silverman | e1fe251 | 2022-08-14 23:18:50 -0700 | [diff] [blame] | 1001 | // Interface for terminating execution of an EventLoop. |
| 1002 | // |
| 1003 | // Prefer this over binding a lambda to an Exit() method when passing ownership |
| 1004 | // in complicated ways because implementations should have assertions to catch |
| 1005 | // it outliving the object it's referring to, instead of having a |
| 1006 | // use-after-free. |
| 1007 | // |
| 1008 | // This is not exposed by EventLoop directly because different EventLoop |
| 1009 | // implementations provide this functionality at different scopes, or possibly |
| 1010 | // not at all. |
| 1011 | class ExitHandle { |
| 1012 | public: |
| 1013 | ExitHandle() = default; |
| 1014 | virtual ~ExitHandle() = default; |
| 1015 | |
| 1016 | // Exits some set of event loops. Details depend on the implementation. |
| 1017 | // |
| 1018 | // This means no more events will be processed, but any currently being |
| 1019 | // processed will finish. |
| 1020 | virtual void Exit() = 0; |
| 1021 | }; |
| 1022 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1023 | } // namespace aos |
| 1024 | |
Austin Schuh | b807581 | 2020-10-19 09:36:49 -0700 | [diff] [blame] | 1025 | #include "aos/events/event_loop_tmpl.h" // IWYU pragma: export |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1026 | |
| 1027 | #endif // AOS_EVENTS_EVENT_LOOP_H |