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