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