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