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