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 | |
| 4 | #include <atomic> |
| 5 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 6 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/configuration.h" |
| 9 | #include "aos/configuration_generated.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 10 | #include "aos/events/event_loop_event.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 11 | #include "aos/events/event_loop_generated.h" |
| 12 | #include "aos/events/timing_statistics.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "aos/flatbuffers.h" |
| 14 | #include "aos/json_to_flatbuffer.h" |
| 15 | #include "aos/time/time.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 16 | #include "aos/util/phased_loop.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "flatbuffers/flatbuffers.h" |
| 18 | #include "glog/logging.h" |
| 19 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 20 | DECLARE_bool(timing_reports); |
| 21 | DECLARE_int32(timing_report_ms); |
| 22 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | namespace aos { |
| 24 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 25 | class EventLoop; |
| 26 | class WatcherState; |
| 27 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 28 | // Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context |
| 29 | // about the current message. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | struct Context { |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 31 | // Time that the message was sent on this node, or the timer was triggered. |
| 32 | monotonic_clock::time_point monotonic_event_time; |
| 33 | // Realtime the message was sent on this node. This is set to min_time for |
| 34 | // Timers and PhasedLoops. |
| 35 | realtime_clock::time_point realtime_event_time; |
| 36 | |
| 37 | // For a single-node configuration, these two are identical to *_event_time. |
| 38 | // In a multinode configuration, these are the times that the message was |
| 39 | // sent on the original node. |
| 40 | monotonic_clock::time_point monotonic_remote_time; |
| 41 | realtime_clock::time_point realtime_remote_time; |
| 42 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 43 | // The rest are only valid for Watchers and Fetchers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | // Index in the queue. |
| 45 | uint32_t queue_index; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 46 | // Index into the remote queue. Useful to determine if data was lost. In a |
| 47 | // single-node configuration, this will match queue_index. |
| 48 | uint32_t remote_queue_index; |
| 49 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 50 | // Size of the data sent. |
| 51 | size_t size; |
| 52 | // Pointer to the data. |
| 53 | void *data; |
| 54 | }; |
| 55 | |
| 56 | // Raw version of fetcher. Contains a local variable that the fetcher will |
| 57 | // update. This is used for reflection and as an interface to implement typed |
| 58 | // fetchers. |
| 59 | class RawFetcher { |
| 60 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 61 | RawFetcher(EventLoop *event_loop, const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 62 | RawFetcher(const RawFetcher &) = delete; |
| 63 | RawFetcher &operator=(const RawFetcher &) = delete; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 64 | virtual ~RawFetcher(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 65 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 66 | // Fetches the next message in the queue without blocking. Returns true if |
| 67 | // there was a new message and we got it. |
| 68 | bool FetchNext(); |
| 69 | |
| 70 | // Fetches the latest message without blocking. |
| 71 | bool Fetch(); |
| 72 | |
| 73 | // Returns the channel this fetcher uses. |
| 74 | const Channel *channel() const { return channel_; } |
| 75 | // Returns the context for the current message. |
| 76 | const Context &context() const { return context_; } |
| 77 | |
| 78 | protected: |
| 79 | EventLoop *event_loop() { return event_loop_; } |
| 80 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | Context context_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | friend class EventLoop; |
| 85 | // Implementation |
| 86 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0; |
| 87 | virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0; |
| 88 | |
| 89 | EventLoop *event_loop_; |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 90 | const Channel *channel_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 91 | |
| 92 | internal::RawFetcherTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | // Raw version of sender. Sends a block of data. This is used for reflection |
| 96 | // and as a building block to implement typed senders. |
| 97 | class RawSender { |
| 98 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 99 | RawSender(EventLoop *event_loop, const Channel *channel); |
| 100 | RawSender(const RawSender &) = delete; |
| 101 | RawSender &operator=(const RawSender &) = delete; |
| 102 | |
| 103 | virtual ~RawSender(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 104 | |
| 105 | // Sends a message without copying it. The users starts by copying up to |
| 106 | // size() bytes into the data backed by data(). They then call Send to send. |
| 107 | // Returns true on a successful send. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 108 | // If provided, monotonic_remote_time, realtime_remote_time, and |
| 109 | // remote_queue_index are attached to the message and are available in the |
| 110 | // context on the read side. If they are not populated, the read side will |
| 111 | // get the sent times instead. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 112 | virtual void *data() = 0; |
| 113 | virtual size_t size() = 0; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 114 | bool Send(size_t size, |
| 115 | aos::monotonic_clock::time_point monotonic_remote_time = |
| 116 | aos::monotonic_clock::min_time, |
| 117 | aos::realtime_clock::time_point realtime_remote_time = |
| 118 | aos::realtime_clock::min_time, |
| 119 | uint32_t remote_queue_index = 0xffffffffu); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 120 | |
| 121 | // Sends a single block of data by copying it. |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 122 | // The remote arguments have the same meaning as in Send above. |
| 123 | bool Send(const void *data, 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 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 130 | const Channel *channel() const { return channel_; } |
| 131 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 132 | // Returns the time_points that the last message was sent at. |
| 133 | aos::monotonic_clock::time_point monotonic_sent_time() const { |
| 134 | return monotonic_sent_time_; |
| 135 | } |
| 136 | aos::realtime_clock::time_point realtime_sent_time() const { |
| 137 | return realtime_sent_time_; |
| 138 | } |
| 139 | // Returns the queue index that this was sent with. |
| 140 | uint32_t sent_queue_index() const { return sent_queue_index_; } |
| 141 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 142 | protected: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 143 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 144 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 145 | aos::monotonic_clock::time_point monotonic_sent_time_ = |
| 146 | aos::monotonic_clock::min_time; |
| 147 | aos::realtime_clock::time_point realtime_sent_time_ = |
| 148 | aos::realtime_clock::min_time; |
| 149 | uint32_t sent_queue_index_ = 0xffffffff; |
| 150 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 151 | private: |
| 152 | friend class EventLoop; |
| 153 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 154 | virtual bool DoSend(const void *data, size_t size, |
| 155 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 156 | aos::realtime_clock::time_point realtime_remote_time, |
| 157 | uint32_t remote_queue_index) = 0; |
| 158 | virtual bool DoSend(size_t size, |
| 159 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 160 | aos::realtime_clock::time_point realtime_remote_time, |
| 161 | uint32_t remote_queue_index) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 162 | |
| 163 | EventLoop *event_loop_; |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 164 | const Channel *channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 165 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 166 | internal::RawSenderTiming timing_; |
| 167 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 168 | |
| 169 | // Fetches the newest message from a channel. |
| 170 | // This provides a polling based interface for channels. |
| 171 | template <typename T> |
| 172 | class Fetcher { |
| 173 | public: |
| 174 | Fetcher() {} |
| 175 | |
| 176 | // Fetches the next message. Returns true if it fetched a new message. This |
| 177 | // method will only return messages sent after the Fetcher was created. |
| 178 | bool FetchNext() { return fetcher_->FetchNext(); } |
| 179 | |
| 180 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 181 | // This will return the latest message regardless of if it was sent before or |
| 182 | // after the fetcher was created. |
| 183 | bool Fetch() { return fetcher_->Fetch(); } |
| 184 | |
| 185 | // Returns a pointer to the contained flatbuffer, or nullptr if there is no |
| 186 | // available message. |
| 187 | const T *get() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 188 | return fetcher_->context().data != nullptr |
| 189 | ? flatbuffers::GetRoot<T>( |
| 190 | reinterpret_cast<const char *>(fetcher_->context().data)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 191 | : nullptr; |
| 192 | } |
| 193 | |
| 194 | // Returns the context holding timestamps and other metadata about the |
| 195 | // message. |
| 196 | const Context &context() const { return fetcher_->context(); } |
| 197 | |
| 198 | const T &operator*() const { return *get(); } |
| 199 | const T *operator->() const { return get(); } |
| 200 | |
| 201 | private: |
| 202 | friend class EventLoop; |
| 203 | Fetcher(::std::unique_ptr<RawFetcher> fetcher) |
| 204 | : fetcher_(::std::move(fetcher)) {} |
| 205 | ::std::unique_ptr<RawFetcher> fetcher_; |
| 206 | }; |
| 207 | |
| 208 | // Sends messages to a channel. |
| 209 | template <typename T> |
| 210 | class Sender { |
| 211 | public: |
| 212 | Sender() {} |
| 213 | |
| 214 | // Represents a single message about to be sent to the queue. |
| 215 | // The lifecycle goes: |
| 216 | // |
| 217 | // Builder builder = sender.MakeBuilder(); |
| 218 | // T::Builder t_builder = builder.MakeBuilder<T>(); |
| 219 | // Populate(&t_builder); |
| 220 | // builder.Send(t_builder.Finish()); |
| 221 | class Builder { |
| 222 | public: |
| 223 | Builder(RawSender *sender, void *data, size_t size) |
| 224 | : alloc_(data, size), fbb_(size, &alloc_), sender_(sender) { |
| 225 | fbb_.ForceDefaults(1); |
| 226 | } |
| 227 | |
| 228 | flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; } |
| 229 | |
| 230 | template <typename T2> |
| 231 | typename T2::Builder MakeBuilder() { |
| 232 | return typename T2::Builder(fbb_); |
| 233 | } |
| 234 | |
| 235 | bool Send(flatbuffers::Offset<T> offset) { |
| 236 | fbb_.Finish(offset); |
| 237 | return sender_->Send(fbb_.GetSize()); |
| 238 | } |
| 239 | |
| 240 | // CHECKs that this message was sent. |
| 241 | void CheckSent() { fbb_.Finished(); } |
| 242 | |
| 243 | private: |
| 244 | PreallocatedAllocator alloc_; |
| 245 | flatbuffers::FlatBufferBuilder fbb_; |
| 246 | RawSender *sender_; |
| 247 | }; |
| 248 | |
| 249 | // Constructs an above builder. |
| 250 | Builder MakeBuilder(); |
| 251 | |
Austin Schuh | a28cbc3 | 2019-12-27 16:28:04 -0800 | [diff] [blame] | 252 | // Sends a prebuilt flatbuffer. |
| 253 | bool Send(const Flatbuffer<T> &flatbuffer); |
| 254 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 255 | // Returns the name of the underlying queue. |
Austin Schuh | 1e86947 | 2019-12-01 13:36:10 -0800 | [diff] [blame] | 256 | const Channel *channel() const { return sender_->channel(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 257 | |
| 258 | private: |
| 259 | friend class EventLoop; |
| 260 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 261 | std::unique_ptr<RawSender> sender_; |
| 262 | }; |
| 263 | |
| 264 | // Interface for timers |
| 265 | class TimerHandler { |
| 266 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 267 | virtual ~TimerHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 268 | |
| 269 | // Timer should sleep until base, base + offset, base + offset * 2, ... |
| 270 | // If repeat_offset isn't set, the timer only expires once. |
| 271 | virtual void Setup(monotonic_clock::time_point base, |
| 272 | monotonic_clock::duration repeat_offset = |
| 273 | ::aos::monotonic_clock::zero()) = 0; |
| 274 | |
| 275 | // Stop future calls to callback(). |
| 276 | virtual void Disable() = 0; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 277 | |
| 278 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 279 | // name in the timing report. |
| 280 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 281 | const std::string_view name() const { return name_; } |
| 282 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 283 | protected: |
| 284 | TimerHandler(EventLoop *event_loop, std::function<void()> fn); |
| 285 | |
| 286 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 287 | monotonic_clock::time_point event_time); |
| 288 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 289 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 290 | friend class EventLoop; |
| 291 | |
| 292 | EventLoop *event_loop_; |
| 293 | // The function to call when Call is called. |
| 294 | std::function<void()> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 295 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 296 | |
| 297 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | // Interface for phased loops. They are built on timers. |
| 301 | class PhasedLoopHandler { |
| 302 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 303 | virtual ~PhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 304 | |
| 305 | // Sets the interval and offset. Any changes to interval and offset only take |
| 306 | // effect when the handler finishes running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 307 | void set_interval_and_offset(const monotonic_clock::duration interval, |
| 308 | const monotonic_clock::duration offset) { |
| 309 | phased_loop_.set_interval_and_offset(interval, offset); |
| 310 | } |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 311 | |
| 312 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 313 | // name in the timing report. |
| 314 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 315 | const std::string_view name() const { return name_; } |
| 316 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 317 | protected: |
| 318 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 319 | std::function<void(monotonic_clock::time_point)> schedule); |
| 320 | |
| 321 | PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn, |
| 322 | const monotonic_clock::duration interval, |
| 323 | const monotonic_clock::duration offset); |
| 324 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 325 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 326 | friend class EventLoop; |
| 327 | |
| 328 | void Reschedule(std::function<void(monotonic_clock::time_point)> schedule, |
| 329 | monotonic_clock::time_point monotonic_now) { |
| 330 | cycles_elapsed_ += phased_loop_.Iterate(monotonic_now); |
| 331 | schedule(phased_loop_.sleep_time()); |
| 332 | } |
| 333 | |
| 334 | virtual void Schedule(monotonic_clock::time_point sleep_time) = 0; |
| 335 | |
| 336 | EventLoop *event_loop_; |
| 337 | std::function<void(int)> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 338 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 339 | time::PhasedLoop phased_loop_; |
| 340 | |
| 341 | int cycles_elapsed_ = 0; |
| 342 | |
| 343 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 344 | }; |
| 345 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 346 | class EventLoop { |
| 347 | public: |
| 348 | EventLoop(const Configuration *configuration) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 349 | : timing_report_(flatbuffers::DetachedBuffer()), |
| 350 | configuration_(configuration) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 351 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 352 | virtual ~EventLoop(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 353 | |
| 354 | // Current time. |
| 355 | virtual monotonic_clock::time_point monotonic_now() = 0; |
| 356 | virtual realtime_clock::time_point realtime_now() = 0; |
| 357 | |
| 358 | // Note, it is supported to create: |
| 359 | // multiple fetchers, and (one sender or one watcher) per <name, type> |
| 360 | // tuple. |
| 361 | |
| 362 | // Makes a class that will always fetch the most recent value |
| 363 | // sent to the provided channel. |
| 364 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 365 | Fetcher<T> MakeFetcher(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 366 | const Channel *channel = |
| 367 | configuration::GetChannel(configuration_, channel_name, |
| 368 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 369 | CHECK(channel != nullptr) |
| 370 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 371 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 372 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame^] | 373 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 374 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 375 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 376 | << "\" } is not able to be fetched on this node. Check your " |
| 377 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 380 | return Fetcher<T>(MakeRawFetcher(channel)); |
| 381 | } |
| 382 | |
| 383 | // Makes class that allows constructing and sending messages to |
| 384 | // the provided channel. |
| 385 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 386 | Sender<T> MakeSender(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 387 | const Channel *channel = |
| 388 | configuration::GetChannel(configuration_, channel_name, |
| 389 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 390 | CHECK(channel != nullptr) |
| 391 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 392 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 393 | |
Austin Schuh | ca4828c | 2019-12-28 14:21:35 -0800 | [diff] [blame^] | 394 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 395 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 396 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 397 | << "\" } is not able to be sent on this node. Check your " |
| 398 | "configuration."; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 401 | return Sender<T>(MakeRawSender(channel)); |
| 402 | } |
| 403 | |
| 404 | // This will watch messages sent to the provided channel. |
| 405 | // |
| 406 | // Watch is a functor that have a call signature like so: |
| 407 | // void Event(const MessageType& type); |
| 408 | // |
| 409 | // TODO(parker): Need to support ::std::bind. For now, use lambdas. |
| 410 | // TODO(austin): Do we need a functor? Or is a std::function good enough? |
| 411 | template <typename Watch> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 412 | void MakeWatcher(const std::string_view name, Watch &&w); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 413 | |
| 414 | // The passed in function will be called when the event loop starts. |
| 415 | // Use this to run code once the thread goes into "real-time-mode", |
| 416 | virtual void OnRun(::std::function<void()> on_run) = 0; |
| 417 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 418 | // Gets the name of the event loop. This is the application name. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 419 | virtual const std::string_view name() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 421 | // Returns the node that this event loop is running on. Returns nullptr if we |
| 422 | // are running in single-node mode. |
| 423 | virtual const Node *node() const = 0; |
| 424 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 425 | // Creates a timer that executes callback when the timer expires |
| 426 | // Returns a TimerHandle for configuration of the timer |
| 427 | virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0; |
| 428 | |
| 429 | // Creates a timer that executes callback periodically at the specified |
| 430 | // interval and offset. Returns a PhasedLoopHandler for interacting with the |
| 431 | // timer. |
| 432 | virtual PhasedLoopHandler *AddPhasedLoop( |
| 433 | ::std::function<void(int)> callback, |
| 434 | const monotonic_clock::duration interval, |
| 435 | const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0; |
| 436 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 437 | // TODO(austin): OnExit for cleanup. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 438 | |
| 439 | // Threadsafe. |
| 440 | bool is_running() const { return is_running_.load(); } |
| 441 | |
| 442 | // Sets the scheduler priority to run the event loop at. This may not be |
| 443 | // called after we go into "real-time-mode". |
| 444 | virtual void SetRuntimeRealtimePriority(int priority) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 445 | virtual int priority() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 446 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 447 | // Fetches new messages from the provided channel (path, type). |
| 448 | // |
| 449 | // Note: this channel must be a member of the exact configuration object this |
| 450 | // was built with. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 451 | virtual std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 452 | const Channel *channel) = 0; |
| 453 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 454 | // Watches channel (name, type) for new messages. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 455 | virtual void MakeRawWatcher( |
| 456 | const Channel *channel, |
| 457 | std::function<void(const Context &context, const void *message)> |
| 458 | watcher) = 0; |
| 459 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 460 | // Creates a raw sender for the provided channel. This is used for reflection |
| 461 | // based sending. |
| 462 | // Note: this ignores any node constraints. Ignore at your own peril. |
| 463 | virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0; |
| 464 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 465 | // Returns the context for the current callback. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 466 | const Context &context() const { return context_; } |
| 467 | |
| 468 | // Returns the configuration that this event loop was built with. |
| 469 | const Configuration *configuration() const { return configuration_; } |
| 470 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 471 | // Prevents the event loop from sending a timing report. |
| 472 | void SkipTimingReport() { skip_timing_report_ = true; } |
| 473 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 474 | protected: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 475 | // Sets the name of the event loop. This is the application name. |
| 476 | virtual void set_name(const std::string_view name) = 0; |
| 477 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 478 | void set_is_running(bool value) { is_running_.store(value); } |
| 479 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 480 | // Validates that channel exists inside configuration_ and finds its index. |
| 481 | int ChannelIndex(const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 482 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 483 | // Context available for watchers, timers, and phased loops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 484 | Context context_; |
| 485 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 486 | friend class RawSender; |
| 487 | friend class TimerHandler; |
| 488 | friend class RawFetcher; |
| 489 | friend class PhasedLoopHandler; |
| 490 | friend class WatcherState; |
| 491 | |
| 492 | // Methods used to implement timing reports. |
| 493 | void NewSender(RawSender *sender); |
| 494 | void DeleteSender(RawSender *sender); |
| 495 | TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer); |
| 496 | PhasedLoopHandler *NewPhasedLoop( |
| 497 | std::unique_ptr<PhasedLoopHandler> phased_loop); |
| 498 | void NewFetcher(RawFetcher *fetcher); |
| 499 | void DeleteFetcher(RawFetcher *fetcher); |
| 500 | WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher); |
| 501 | |
| 502 | std::vector<RawSender *> senders_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 503 | std::vector<RawFetcher *> fetchers_; |
| 504 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 505 | std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 506 | std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 507 | std::vector<std::unique_ptr<WatcherState>> watchers_; |
| 508 | |
| 509 | void SendTimingReport(); |
| 510 | void UpdateTimingReport(); |
| 511 | void MaybeScheduleTimingReports(); |
| 512 | |
| 513 | std::unique_ptr<RawSender> timing_report_sender_; |
| 514 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 515 | // Tracks which event sources (timers and watchers) have data, and which |
| 516 | // don't. Added events may not change their event_time(). |
| 517 | // TODO(austin): Test case 1: timer triggers at t1, handler takes until after |
| 518 | // t2 to run, t2 should then be picked up without a context switch. |
| 519 | void AddEvent(EventLoopEvent *event); |
| 520 | void RemoveEvent(EventLoopEvent *event); |
| 521 | size_t EventCount() const { return events_.size(); } |
| 522 | EventLoopEvent *PopEvent(); |
| 523 | EventLoopEvent *PeekEvent() { return events_.front(); } |
| 524 | void ReserveEvents(); |
| 525 | |
| 526 | std::vector<EventLoopEvent *> events_; |
| 527 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 528 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 529 | virtual pid_t GetTid() = 0; |
| 530 | |
| 531 | FlatbufferDetachedBuffer<timing::Report> timing_report_; |
| 532 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 533 | ::std::atomic<bool> is_running_{false}; |
| 534 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 535 | const Configuration *configuration_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 536 | |
| 537 | // If true, don't send out timing reports. |
| 538 | bool skip_timing_report_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 539 | }; |
| 540 | |
| 541 | } // namespace aos |
| 542 | |
| 543 | #include "aos/events/event_loop_tmpl.h" |
| 544 | |
| 545 | #endif // AOS_EVENTS_EVENT_LOOP_H |