blob: 0534242e2e3adb4d7958bd2c1a08971e65777b0d [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_EVENTS_EVENT_LOOP_H_
2#define AOS_EVENTS_EVENT_LOOP_H_
3
Brian Silverman6a54ff32020-04-28 16:41:39 -07004#include <sched.h>
5
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include <atomic>
7#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -08008#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07009
Alex Perrycb7da4b2019-08-28 19:35:56 -070010#include "aos/configuration.h"
11#include "aos/configuration_generated.h"
Austin Schuh1af273d2020-03-07 20:11:34 -080012#include "aos/events/channel_preallocated_allocator.h"
Austin Schuh7d87b672019-12-01 20:23:49 -080013#include "aos/events/event_loop_event.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080014#include "aos/events/event_loop_generated.h"
Austin Schuh20ac95d2020-12-05 17:24:19 -080015#include "aos/events/logging/uuid.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080016#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "aos/flatbuffers.h"
Brian Silverman79ec7fc2020-06-08 20:11:22 -050018#include "aos/ftrace.h"
Brian Silvermana1652f32020-01-29 20:41:44 -080019#include "aos/ipc_lib/data_alignment.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070020#include "aos/json_to_flatbuffer.h"
21#include "aos/time/time.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080022#include "aos/util/phased_loop.h"
Brian Silverman0fc69932020-01-24 21:54:02 -080023
24#include "absl/container/btree_set.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070025#include "flatbuffers/flatbuffers.h"
26#include "glog/logging.h"
27
Austin Schuh39788ff2019-12-01 18:22:57 -080028DECLARE_bool(timing_reports);
29DECLARE_int32(timing_report_ms);
30
Alex Perrycb7da4b2019-08-28 19:35:56 -070031namespace aos {
32
Austin Schuh39788ff2019-12-01 18:22:57 -080033class EventLoop;
34class WatcherState;
35
Austin Schuh6231cc32019-12-07 13:06:15 -080036// Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context
37// about the current message.
Alex Perrycb7da4b2019-08-28 19:35:56 -070038struct Context {
Austin Schuhad154822019-12-27 15:45:13 -080039 // 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 Schuh6231cc32019-12-07 13:06:15 -080051 // The rest are only valid for Watchers and Fetchers.
Brian Silverman4f4e0612020-08-12 19:54:41 -070052
Alex Perrycb7da4b2019-08-28 19:35:56 -070053 // Index in the queue.
54 uint32_t queue_index;
Austin Schuhad154822019-12-27 15:45:13 -080055 // 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 Perrycb7da4b2019-08-28 19:35:56 -070059 // Size of the data sent.
60 size_t size;
61 // Pointer to the data.
Brian Silvermaneaa41d62020-07-08 19:47:35 -070062 const void *data;
Austin Schuh678078e2020-08-01 14:30:36 -070063
Brian Silverman4f4e0612020-08-12 19:54:41 -070064 // 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 Schuhca75b6a2020-12-15 21:12:24 -080078 // Efficiently copies the flatbuffer into a FlatbufferVector, allocating
Austin Schuh678078e2020-08-01 14:30:36 -070079 // 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 Silverman354697a2020-09-22 21:06:32 -070083 ResizeableBuffer buffer;
84 buffer.resize(size);
85 memcpy(buffer.data(), data, size);
86 return FlatbufferVector<T>(std::move(buffer));
Austin Schuh678078e2020-08-01 14:30:36 -070087 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070088};
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.
93class RawFetcher {
94 public:
Austin Schuh39788ff2019-12-01 18:22:57 -080095 RawFetcher(EventLoop *event_loop, const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 RawFetcher(const RawFetcher &) = delete;
97 RawFetcher &operator=(const RawFetcher &) = delete;
Austin Schuh39788ff2019-12-01 18:22:57 -080098 virtual ~RawFetcher();
Alex Perrycb7da4b2019-08-28 19:35:56 -070099
Austin Schuh39788ff2019-12-01 18:22:57 -0800100 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700115 Context context_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800116
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 Silverman79ec7fc2020-06-08 20:11:22 -0500123 EventLoop *const event_loop_;
124 const Channel *const channel_;
125 const std::string ftrace_prefix_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800126
127 internal::RawFetcherTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500128 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700129};
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.
133class RawSender {
134 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800135 RawSender(EventLoop *event_loop, const Channel *channel);
136 RawSender(const RawSender &) = delete;
137 RawSender &operator=(const RawSender &) = delete;
138
139 virtual ~RawSender();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700140
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 Schuhad154822019-12-27 15:45:13 -0800144 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700148 virtual void *data() = 0;
149 virtual size_t size() = 0;
Austin Schuhad154822019-12-27 15:45:13 -0800150 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 Perrycb7da4b2019-08-28 19:35:56 -0700156
157 // Sends a single block of data by copying it.
Austin Schuhad154822019-12-27 15:45:13 -0800158 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700165
Austin Schuh54cf95f2019-11-29 13:14:18 -0800166 const Channel *channel() const { return channel_; }
167
Austin Schuhad154822019-12-27 15:45:13 -0800168 // 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 Silvermana1652f32020-01-29 20:41:44 -0800178 // Returns the associated flatbuffers-style allocator. This must be
179 // deallocated before the message is sent.
Austin Schuh1af273d2020-03-07 20:11:34 -0800180 ChannelPreallocatedAllocator *fbb_allocator() {
181 fbb_allocator_ = ChannelPreallocatedAllocator(
182 reinterpret_cast<uint8_t *>(data()), size(), channel());
Brian Silvermana1652f32020-01-29 20:41:44 -0800183 return &fbb_allocator_;
184 }
185
Brian Silverman4f4e0612020-08-12 19:54:41 -0700186 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700190 protected:
Austin Schuh39788ff2019-12-01 18:22:57 -0800191 EventLoop *event_loop() { return event_loop_; }
Austin Schuh54cf95f2019-11-29 13:14:18 -0800192
Austin Schuhad154822019-12-27 15:45:13 -0800193 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 Schuh39788ff2019-12-01 18:22:57 -0800199 private:
200 friend class EventLoop;
201
Austin Schuhad154822019-12-27 15:45:13 -0800202 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 Schuh39788ff2019-12-01 18:22:57 -0800210
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500211 EventLoop *const event_loop_;
212 const Channel *const channel_;
213 const std::string ftrace_prefix_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700214
Austin Schuh39788ff2019-12-01 18:22:57 -0800215 internal::RawSenderTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500216 Ftrace ftrace_;
Brian Silvermana1652f32020-01-29 20:41:44 -0800217
Austin Schuh1af273d2020-03-07 20:11:34 -0800218 ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr};
Austin Schuh39788ff2019-12-01 18:22:57 -0800219};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700220
221// Fetches the newest message from a channel.
222// This provides a polling based interface for channels.
223template <typename T>
224class 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 Silvermana1652f32020-01-29 20:41:44 -0800230 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 Perrycb7da4b2019-08-28 19:35:56 -0700238
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 Silvermana1652f32020-01-29 20:41:44 -0800242 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 Perrycb7da4b2019-08-28 19:35:56 -0700250
251 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
252 // available message.
253 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800254 return fetcher_->context().data != nullptr
255 ? flatbuffers::GetRoot<T>(
256 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700257 : nullptr;
258 }
259
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700260 // Returns the channel this fetcher uses
261 const Channel *channel() const { return fetcher_->channel(); }
262
Alex Perrycb7da4b2019-08-28 19:35:56 -0700263 // 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 Silvermande9f3ff2020-04-28 16:56:58 -0700270 // Returns true if this fetcher is valid and connected to a channel.
271 operator bool() const { return static_cast<bool>(fetcher_); }
272
Austin Schuhca75b6a2020-12-15 21:12:24 -0800273 // Copies the current flatbuffer into a FlatbufferVector.
274 FlatbufferVector<T> CopyFlatBuffer() const {
275 return context().template CopyFlatBuffer<T>();
276 }
277
Alex Perrycb7da4b2019-08-28 19:35:56 -0700278 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.
286template <typename T>
287class 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 Schuh1af273d2020-03-07 20:11:34 -0800300 Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator)
Brian Silverman9dd793b2020-01-31 23:52:21 -0800301 : fbb_(allocator->size(), allocator),
302 allocator_(allocator),
303 sender_(sender) {
Brian Silvermana1652f32020-01-29 20:41:44 -0800304 CheckChannelDataAlignment(allocator->data(), allocator->size());
Austin Schuhd7b15da2020-02-17 15:06:11 -0800305 fbb_.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700306 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800307 Builder() {}
308 Builder(const Builder &) = delete;
309 Builder(Builder &&) = default;
310
311 Builder &operator=(const Builder &) = delete;
312 Builder &operator=(Builder &&) = default;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700313
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 Silverman9dd793b2020-01-31 23:52:21 -0800323 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 Perrycb7da4b2019-08-28 19:35:56 -0700327 }
328
329 // CHECKs that this message was sent.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800330 void CheckSent() {
331 CHECK(!allocator_->is_allocated()) << ": Message was not sent yet";
332 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700333
Brian Silverman341b57e2020-06-23 16:23:18 -0700334 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700343 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700344 flatbuffers::FlatBufferBuilder fbb_;
Austin Schuh1af273d2020-03-07 20:11:34 -0800345 ChannelPreallocatedAllocator *allocator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346 RawSender *sender_;
347 };
348
349 // Constructs an above builder.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800350 //
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 Perrycb7da4b2019-08-28 19:35:56 -0700356 Builder MakeBuilder();
357
Austin Schuha28cbc32019-12-27 16:28:04 -0800358 // Sends a prebuilt flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800359 bool Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer);
Austin Schuha28cbc32019-12-27 16:28:04 -0800360
Brian Silverman341b57e2020-06-23 16:23:18 -0700361 // 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 Schuh39788ff2019-12-01 18:22:57 -0800365 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800366 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700367
Austin Schuha0c41ba2020-09-10 22:59:14 -0700368 operator bool() const { return sender_ ? true : false; }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800369
Austin Schuh7bc59052020-02-16 23:48:33 -0800370 // 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 Silverman4f4e0612020-08-12 19:54:41 -0700380 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700384 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 Silverman4f4e0612020-08-12 19:54:41 -0700390// Interface for timers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700391class TimerHandler {
392 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800393 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700394
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 Schuh1540c2f2019-11-29 21:59:29 -0800403
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 Schuh39788ff2019-12-01 18:22:57 -0800409 protected:
410 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
411
Austin Schuhcde39fd2020-02-22 20:58:24 -0800412 monotonic_clock::time_point Call(
413 std::function<monotonic_clock::time_point()> get_time,
414 monotonic_clock::time_point event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800415
Austin Schuh1540c2f2019-11-29 21:59:29 -0800416 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800417 friend class EventLoop;
418
419 EventLoop *event_loop_;
420 // The function to call when Call is called.
421 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800422 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800423
424 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500425 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700426};
427
428// Interface for phased loops. They are built on timers.
429class PhasedLoopHandler {
430 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800431 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700432
433 // Sets the interval and offset. Any changes to interval and offset only take
434 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800435 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 Schuh1540c2f2019-11-29 21:59:29 -0800439
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 Schuh39788ff2019-12-01 18:22:57 -0800445 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 Schuh1540c2f2019-11-29 21:59:29 -0800453 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800454 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 Schuh1540c2f2019-11-29 21:59:29 -0800466 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800467 time::PhasedLoop phased_loop_;
468
469 int cycles_elapsed_ = 0;
470
471 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500472 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700473};
474
Alex Perrycb7da4b2019-08-28 19:35:56 -0700475class EventLoop {
476 public:
Austin Schuh83c7f702021-01-19 22:36:29 -0800477 EventLoop(const Configuration *configuration);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700478
Austin Schuh39788ff2019-12-01 18:22:57 -0800479 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480
481 // Current time.
482 virtual monotonic_clock::time_point monotonic_now() = 0;
483 virtual realtime_clock::time_point realtime_now() = 0;
484
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700485 // Returns true if the channel exists in the configuration.
486 template <typename T>
487 bool HasChannel(const std::string_view channel_name) {
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800488 return configuration::GetChannel(configuration(), channel_name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800489 T::GetFullyQualifiedName(), name(), node(),
490 true) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700491 }
492
Alex Perrycb7da4b2019-08-28 19:35:56 -0700493 // Note, it is supported to create:
494 // multiple fetchers, and (one sender or one watcher) per <name, type>
495 // tuple.
496
497 // Makes a class that will always fetch the most recent value
498 // sent to the provided channel.
499 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800500 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800501 const Channel *channel =
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800502 configuration::GetChannel(configuration(), channel_name,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800503 T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700504 CHECK(channel != nullptr)
505 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
506 << T::GetFullyQualifiedName() << "\" } not found in config.";
507
Austin Schuhca4828c2019-12-28 14:21:35 -0800508 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
509 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
510 << "\", \"type\": \"" << T::GetFullyQualifiedName()
511 << "\" } is not able to be fetched on this node. Check your "
512 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800513 }
514
Alex Perrycb7da4b2019-08-28 19:35:56 -0700515 return Fetcher<T>(MakeRawFetcher(channel));
516 }
517
518 // Makes class that allows constructing and sending messages to
519 // the provided channel.
520 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800521 Sender<T> MakeSender(const std::string_view channel_name) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800522 const Channel *channel =
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800523 configuration::GetChannel(configuration(), channel_name,
Austin Schuhbca6cf02019-12-22 17:28:34 -0800524 T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700525 CHECK(channel != nullptr)
526 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
Austin Schuh28fedcb2020-02-08 15:59:58 -0800527 << T::GetFullyQualifiedName() << "\" } not found in config for "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700528 << name()
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800529 << (configuration::MultiNode(configuration())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700530 ? absl::StrCat(" on node ", node()->name()->string_view())
531 : ".");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700532
Austin Schuhca4828c2019-12-28 14:21:35 -0800533 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 Schuh217a9782019-12-21 23:02:50 -0800538 }
539
Alex Perrycb7da4b2019-08-28 19:35:56 -0700540 return Sender<T>(MakeRawSender(channel));
541 }
542
543 // This will watch messages sent to the provided channel.
544 //
Brian Silverman454bc112020-03-05 14:21:25 -0800545 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700549 //
Brian Silverman454bc112020-03-05 14:21:25 -0800550 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700561 template <typename Watch>
Brian Silverman454bc112020-03-05 14:21:25 -0800562 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 Silverman6b8a3c32020-03-06 11:26:14 -0800567 // The function will still have access to context(), although that will have
568 // its data field set to nullptr.
Brian Silverman454bc112020-03-05 14:21:25 -0800569 template <typename MessageType>
570 void MakeNoArgWatcher(const std::string_view channel_name,
571 std::function<void()> w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700572
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 Schuh217a9782019-12-21 23:02:50 -0800577 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800578 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700579
Austin Schuh217a9782019-12-21 23:02:50 -0800580 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700584 // 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 Schuh217a9782019-12-21 23:02:50 -0800596 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700597
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 Schuh39788ff2019-12-01 18:22:57 -0800604 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700605
Brian Silverman6a54ff32020-04-28 16:41:39 -0700606 // 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 Schuh217a9782019-12-21 23:02:50 -0800610 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700614 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
615 const Channel *channel) = 0;
616
Austin Schuh217a9782019-12-21 23:02:50 -0800617 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618 virtual void MakeRawWatcher(
619 const Channel *channel,
620 std::function<void(const Context &context, const void *message)>
621 watcher) = 0;
622
Brian Silverman454bc112020-03-05 14:21:25 -0800623 // 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 Silverman6b8a3c32020-03-06 11:26:14 -0800629 Context new_context = context;
630 new_context.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700631 new_context.buffer_index = -1;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800632 watcher(new_context);
Brian Silverman454bc112020-03-05 14:21:25 -0800633 });
634 }
635
Austin Schuh217a9782019-12-21 23:02:50 -0800636 // 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 Schuh6231cc32019-12-07 13:06:15 -0800641 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700642 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 Schuh39788ff2019-12-01 18:22:57 -0800647 // Prevents the event loop from sending a timing report.
648 void SkipTimingReport() { skip_timing_report_ = true; }
649
Brian Silverman4f4e0612020-08-12 19:54:41 -0700650 // Prevents AOS_LOG being sent to message on /aos.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800651 void SkipAosLog() { skip_logger_ = true; }
652
Brian Silverman4f4e0612020-08-12 19:54:41 -0700653 // 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 Schuh20ac95d2020-12-05 17:24:19 -0800657 // Returns the boot UUID.
Austin Schuh83c7f702021-01-19 22:36:29 -0800658 virtual const UUID &boot_uuid() const = 0;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800659
Alex Perrycb7da4b2019-08-28 19:35:56 -0700660 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800661 // 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 Perrycb7da4b2019-08-28 19:35:56 -0700664 void set_is_running(bool value) { is_running_.store(value); }
665
Austin Schuh39788ff2019-12-01 18:22:57 -0800666 // Validates that channel exists inside configuration_ and finds its index.
667 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700668
Brian Silverman5120afb2020-01-31 17:44:35 -0800669 // 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 Silverman6d2b3592020-06-18 14:40:15 -0700673 // Returns a Sender's protected RawSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800674 template <typename T>
675 static RawSender *GetRawSender(aos::Sender<T> *sender) {
676 return sender->sender_.get();
677 }
678
Brian Silverman6d2b3592020-06-18 14:40:15 -0700679 // 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 Schuh6231cc32019-12-07 13:06:15 -0800685 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700686 Context context_;
687
Austin Schuh39788ff2019-12-01 18:22:57 -0800688 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 Silverman0fc69932020-01-24 21:54:02 -0800704 // 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 Schuh39788ff2019-12-01 18:22:57 -0800709 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800710 std::vector<RawFetcher *> fetchers_;
711
Austin Schuh39788ff2019-12-01 18:22:57 -0800712 std::vector<std::unique_ptr<TimerHandler>> timers_;
713 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800714 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 Schuh7d87b672019-12-01 20:23:49 -0800722 // 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 Silvermanbd405c02020-06-23 16:25:23 -0700734 size_t event_generation_ = 1;
Austin Schuh7d87b672019-12-01 20:23:49 -0800735
Tyler Chatow67ddb032020-01-12 14:30:04 -0800736 // If true, don't send AOS_LOG to /aos
737 bool skip_logger_ = false;
738
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800739 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800740 virtual pid_t GetTid() = 0;
741
742 FlatbufferDetachedBuffer<timing::Report> timing_report_;
743
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800744 ::std::atomic<bool> is_running_{false};
745
Alex Perrycb7da4b2019-08-28 19:35:56 -0700746 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800747
748 // If true, don't send out timing reports.
749 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800750
751 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700752};
753
754} // namespace aos
755
Austin Schuhb8075812020-10-19 09:36:49 -0700756#include "aos/events/event_loop_tmpl.h" // IWYU pragma: export
Alex Perrycb7da4b2019-08-28 19:35:56 -0700757
758#endif // AOS_EVENTS_EVENT_LOOP_H