blob: 22f7c6f547b8a78075a6f1d99ccef15794be4a0f [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"
15#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070016#include "aos/flatbuffers.h"
Brian Silverman79ec7fc2020-06-08 20:11:22 -050017#include "aos/ftrace.h"
Brian Silvermana1652f32020-01-29 20:41:44 -080018#include "aos/ipc_lib/data_alignment.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "aos/json_to_flatbuffer.h"
20#include "aos/time/time.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080021#include "aos/util/phased_loop.h"
Austin Schuh4385b142021-03-14 21:31:13 -070022#include "aos/uuid.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 Schuhb5c6f972021-03-14 21:53:07 -0700150 bool Send(size_t size);
151 bool Send(size_t size, monotonic_clock::time_point monotonic_remote_time,
152 realtime_clock::time_point realtime_remote_time,
153 uint32_t remote_queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700154
155 // Sends a single block of data by copying it.
Austin Schuhad154822019-12-27 15:45:13 -0800156 // The remote arguments have the same meaning as in Send above.
Austin Schuhb5c6f972021-03-14 21:53:07 -0700157 bool Send(const void *data, size_t size);
Austin Schuhad154822019-12-27 15:45:13 -0800158 bool Send(const void *data, size_t size,
Austin Schuhb5c6f972021-03-14 21:53:07 -0700159 monotonic_clock::time_point monotonic_remote_time,
160 realtime_clock::time_point realtime_remote_time,
161 uint32_t remote_queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162
Austin Schuh54cf95f2019-11-29 13:14:18 -0800163 const Channel *channel() const { return channel_; }
164
Austin Schuhad154822019-12-27 15:45:13 -0800165 // Returns the time_points that the last message was sent at.
166 aos::monotonic_clock::time_point monotonic_sent_time() const {
167 return monotonic_sent_time_;
168 }
169 aos::realtime_clock::time_point realtime_sent_time() const {
170 return realtime_sent_time_;
171 }
172 // Returns the queue index that this was sent with.
173 uint32_t sent_queue_index() const { return sent_queue_index_; }
174
Brian Silvermana1652f32020-01-29 20:41:44 -0800175 // Returns the associated flatbuffers-style allocator. This must be
176 // deallocated before the message is sent.
Austin Schuh1af273d2020-03-07 20:11:34 -0800177 ChannelPreallocatedAllocator *fbb_allocator() {
178 fbb_allocator_ = ChannelPreallocatedAllocator(
179 reinterpret_cast<uint8_t *>(data()), size(), channel());
Brian Silvermana1652f32020-01-29 20:41:44 -0800180 return &fbb_allocator_;
181 }
182
Brian Silverman4f4e0612020-08-12 19:54:41 -0700183 // Index of the buffer which is currently exposed by data() and the various
184 // other accessors. This is the message the caller should be filling out.
185 virtual int buffer_index() = 0;
186
Alex Perrycb7da4b2019-08-28 19:35:56 -0700187 protected:
Austin Schuh39788ff2019-12-01 18:22:57 -0800188 EventLoop *event_loop() { return event_loop_; }
Austin Schuh54cf95f2019-11-29 13:14:18 -0800189
Austin Schuhb5c6f972021-03-14 21:53:07 -0700190 monotonic_clock::time_point monotonic_sent_time_ = monotonic_clock::min_time;
191 realtime_clock::time_point realtime_sent_time_ = realtime_clock::min_time;
Austin Schuhad154822019-12-27 15:45:13 -0800192 uint32_t sent_queue_index_ = 0xffffffff;
193
Austin Schuh39788ff2019-12-01 18:22:57 -0800194 private:
195 friend class EventLoop;
196
Austin Schuhad154822019-12-27 15:45:13 -0800197 virtual bool DoSend(const void *data, size_t size,
198 aos::monotonic_clock::time_point monotonic_remote_time,
199 aos::realtime_clock::time_point realtime_remote_time,
200 uint32_t remote_queue_index) = 0;
201 virtual bool DoSend(size_t size,
202 aos::monotonic_clock::time_point monotonic_remote_time,
203 aos::realtime_clock::time_point realtime_remote_time,
204 uint32_t remote_queue_index) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800205
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500206 EventLoop *const event_loop_;
207 const Channel *const channel_;
208 const std::string ftrace_prefix_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700209
Austin Schuh39788ff2019-12-01 18:22:57 -0800210 internal::RawSenderTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500211 Ftrace ftrace_;
Brian Silvermana1652f32020-01-29 20:41:44 -0800212
Austin Schuh1af273d2020-03-07 20:11:34 -0800213 ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr};
Austin Schuh39788ff2019-12-01 18:22:57 -0800214};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215
216// Fetches the newest message from a channel.
217// This provides a polling based interface for channels.
218template <typename T>
219class Fetcher {
220 public:
221 Fetcher() {}
222
223 // Fetches the next message. Returns true if it fetched a new message. This
224 // method will only return messages sent after the Fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800225 bool FetchNext() {
226 const bool result = fetcher_->FetchNext();
227 if (result) {
228 CheckChannelDataAlignment(fetcher_->context().data,
229 fetcher_->context().size);
230 }
231 return result;
232 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700233
234 // Fetches the most recent message. Returns true if it fetched a new message.
235 // This will return the latest message regardless of if it was sent before or
236 // after the fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800237 bool Fetch() {
238 const bool result = fetcher_->Fetch();
239 if (result) {
240 CheckChannelDataAlignment(fetcher_->context().data,
241 fetcher_->context().size);
242 }
243 return result;
244 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700245
246 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
247 // available message.
248 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800249 return fetcher_->context().data != nullptr
250 ? flatbuffers::GetRoot<T>(
251 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700252 : nullptr;
253 }
254
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700255 // Returns the channel this fetcher uses
256 const Channel *channel() const { return fetcher_->channel(); }
257
Alex Perrycb7da4b2019-08-28 19:35:56 -0700258 // Returns the context holding timestamps and other metadata about the
259 // message.
260 const Context &context() const { return fetcher_->context(); }
261
262 const T &operator*() const { return *get(); }
263 const T *operator->() const { return get(); }
264
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700265 // Returns true if this fetcher is valid and connected to a channel.
266 operator bool() const { return static_cast<bool>(fetcher_); }
267
Austin Schuhca75b6a2020-12-15 21:12:24 -0800268 // Copies the current flatbuffer into a FlatbufferVector.
269 FlatbufferVector<T> CopyFlatBuffer() const {
270 return context().template CopyFlatBuffer<T>();
271 }
272
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 private:
274 friend class EventLoop;
275 Fetcher(::std::unique_ptr<RawFetcher> fetcher)
276 : fetcher_(::std::move(fetcher)) {}
277 ::std::unique_ptr<RawFetcher> fetcher_;
278};
279
280// Sends messages to a channel.
281template <typename T>
282class Sender {
283 public:
284 Sender() {}
285
286 // Represents a single message about to be sent to the queue.
287 // The lifecycle goes:
288 //
289 // Builder builder = sender.MakeBuilder();
290 // T::Builder t_builder = builder.MakeBuilder<T>();
291 // Populate(&t_builder);
292 // builder.Send(t_builder.Finish());
293 class Builder {
294 public:
Austin Schuh1af273d2020-03-07 20:11:34 -0800295 Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator)
Brian Silverman9dd793b2020-01-31 23:52:21 -0800296 : fbb_(allocator->size(), allocator),
297 allocator_(allocator),
298 sender_(sender) {
Brian Silvermana1652f32020-01-29 20:41:44 -0800299 CheckChannelDataAlignment(allocator->data(), allocator->size());
Austin Schuhd7b15da2020-02-17 15:06:11 -0800300 fbb_.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800302 Builder() {}
303 Builder(const Builder &) = delete;
304 Builder(Builder &&) = default;
305
306 Builder &operator=(const Builder &) = delete;
307 Builder &operator=(Builder &&) = default;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700308
309 flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; }
310
311 template <typename T2>
312 typename T2::Builder MakeBuilder() {
313 return typename T2::Builder(fbb_);
314 }
315
316 bool Send(flatbuffers::Offset<T> offset) {
317 fbb_.Finish(offset);
Brian Silverman9dd793b2020-01-31 23:52:21 -0800318 const bool result = sender_->Send(fbb_.GetSize());
319 // Ensure fbb_ knows it shouldn't access the memory any more.
320 fbb_ = flatbuffers::FlatBufferBuilder();
321 return result;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322 }
323
324 // CHECKs that this message was sent.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800325 void CheckSent() {
326 CHECK(!allocator_->is_allocated()) << ": Message was not sent yet";
327 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700328
Brian Silverman341b57e2020-06-23 16:23:18 -0700329 // Detaches a buffer, for later use calling Sender::Send directly.
330 //
331 // Note that the underlying memory remains with the Sender, so creating
332 // another Builder before destroying the FlatbufferDetachedBuffer will fail.
333 FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) {
334 fbb_.Finish(offset);
335 return fbb_.Release();
336 }
337
Alex Perrycb7da4b2019-08-28 19:35:56 -0700338 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700339 flatbuffers::FlatBufferBuilder fbb_;
Austin Schuh1af273d2020-03-07 20:11:34 -0800340 ChannelPreallocatedAllocator *allocator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700341 RawSender *sender_;
342 };
343
344 // Constructs an above builder.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800345 //
346 // Only a single one of these may be "alive" for this object at any point in
347 // time. After calling Send on the result, it is no longer "alive". This means
348 // that you must manually reset a variable holding the return value (by
349 // assigning a default-constructed Builder to it) before calling this method
350 // again to overwrite the value in the variable.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700351 Builder MakeBuilder();
352
Austin Schuha28cbc32019-12-27 16:28:04 -0800353 // Sends a prebuilt flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800354 bool Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer);
Austin Schuha28cbc32019-12-27 16:28:04 -0800355
Brian Silverman341b57e2020-06-23 16:23:18 -0700356 // Sends a prebuilt flatbuffer which was detached from a Builder created via
357 // MakeBuilder() on this object.
358 bool SendDetached(FlatbufferDetachedBuffer<T> detached);
359
Austin Schuh39788ff2019-12-01 18:22:57 -0800360 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800361 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700362
Austin Schuha0c41ba2020-09-10 22:59:14 -0700363 operator bool() const { return sender_ ? true : false; }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800364
Austin Schuh7bc59052020-02-16 23:48:33 -0800365 // Returns the time_points that the last message was sent at.
366 aos::monotonic_clock::time_point monotonic_sent_time() const {
367 return sender_->monotonic_sent_time();
368 }
369 aos::realtime_clock::time_point realtime_sent_time() const {
370 return sender_->realtime_sent_time();
371 }
372 // Returns the queue index that this was sent with.
373 uint32_t sent_queue_index() const { return sender_->sent_queue_index(); }
374
Brian Silverman4f4e0612020-08-12 19:54:41 -0700375 // Returns the buffer index which MakeBuilder() will expose access to. This is
376 // the buffer the caller can fill out.
377 int buffer_index() const { return sender_->buffer_index(); }
378
Alex Perrycb7da4b2019-08-28 19:35:56 -0700379 private:
380 friend class EventLoop;
381 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
382 std::unique_ptr<RawSender> sender_;
383};
384
Brian Silverman4f4e0612020-08-12 19:54:41 -0700385// Interface for timers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700386class TimerHandler {
387 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800388 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700389
390 // Timer should sleep until base, base + offset, base + offset * 2, ...
391 // If repeat_offset isn't set, the timer only expires once.
392 virtual void Setup(monotonic_clock::time_point base,
393 monotonic_clock::duration repeat_offset =
394 ::aos::monotonic_clock::zero()) = 0;
395
396 // Stop future calls to callback().
397 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800398
399 // Sets and gets the name of the timer. Set this if you want a descriptive
400 // name in the timing report.
401 void set_name(std::string_view name) { name_ = std::string(name); }
402 const std::string_view name() const { return name_; }
403
Austin Schuh39788ff2019-12-01 18:22:57 -0800404 protected:
405 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
406
Austin Schuhcde39fd2020-02-22 20:58:24 -0800407 monotonic_clock::time_point Call(
408 std::function<monotonic_clock::time_point()> get_time,
409 monotonic_clock::time_point event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800410
Austin Schuh1540c2f2019-11-29 21:59:29 -0800411 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800412 friend class EventLoop;
413
414 EventLoop *event_loop_;
415 // The function to call when Call is called.
416 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800417 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800418
419 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500420 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700421};
422
423// Interface for phased loops. They are built on timers.
424class PhasedLoopHandler {
425 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800426 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700427
428 // Sets the interval and offset. Any changes to interval and offset only take
429 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800430 void set_interval_and_offset(const monotonic_clock::duration interval,
431 const monotonic_clock::duration offset) {
432 phased_loop_.set_interval_and_offset(interval, offset);
433 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800434
435 // Sets and gets the name of the timer. Set this if you want a descriptive
436 // name in the timing report.
437 void set_name(std::string_view name) { name_ = std::string(name); }
438 const std::string_view name() const { return name_; }
439
Austin Schuh39788ff2019-12-01 18:22:57 -0800440 protected:
441 void Call(std::function<monotonic_clock::time_point()> get_time,
442 std::function<void(monotonic_clock::time_point)> schedule);
443
444 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
445 const monotonic_clock::duration interval,
446 const monotonic_clock::duration offset);
447
Austin Schuh1540c2f2019-11-29 21:59:29 -0800448 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800449 friend class EventLoop;
450
451 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
452 monotonic_clock::time_point monotonic_now) {
453 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
454 schedule(phased_loop_.sleep_time());
455 }
456
457 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
458
459 EventLoop *event_loop_;
460 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800461 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800462 time::PhasedLoop phased_loop_;
463
464 int cycles_elapsed_ = 0;
465
466 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500467 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700468};
469
Alex Perrycb7da4b2019-08-28 19:35:56 -0700470class EventLoop {
471 public:
Austin Schuh83c7f702021-01-19 22:36:29 -0800472 EventLoop(const Configuration *configuration);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700473
Austin Schuh39788ff2019-12-01 18:22:57 -0800474 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700475
476 // Current time.
477 virtual monotonic_clock::time_point monotonic_now() = 0;
478 virtual realtime_clock::time_point realtime_now() = 0;
479
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700480 // Returns true if the channel exists in the configuration.
481 template <typename T>
Austin Schuha0654152021-02-21 21:38:24 -0800482 const Channel *GetChannel(const std::string_view channel_name) {
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800483 return configuration::GetChannel(configuration(), channel_name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800484 T::GetFullyQualifiedName(), name(), node(),
Austin Schuha0654152021-02-21 21:38:24 -0800485 true);
486 }
487 template <typename T>
488 bool HasChannel(const std::string_view channel_name) {
489 return GetChannel<T>(channel_name) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700490 }
491
Alex Perrycb7da4b2019-08-28 19:35:56 -0700492 // Note, it is supported to create:
493 // multiple fetchers, and (one sender or one watcher) per <name, type>
494 // tuple.
495
496 // Makes a class that will always fetch the most recent value
497 // sent to the provided channel.
498 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800499 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Austin Schuha0654152021-02-21 21:38:24 -0800500 const Channel *channel = GetChannel<T>(channel_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700501 CHECK(channel != nullptr)
502 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
503 << T::GetFullyQualifiedName() << "\" } not found in config.";
504
Austin Schuhca4828c2019-12-28 14:21:35 -0800505 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
506 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
507 << "\", \"type\": \"" << T::GetFullyQualifiedName()
508 << "\" } is not able to be fetched on this node. Check your "
509 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800510 }
511
Alex Perrycb7da4b2019-08-28 19:35:56 -0700512 return Fetcher<T>(MakeRawFetcher(channel));
513 }
514
515 // Makes class that allows constructing and sending messages to
516 // the provided channel.
517 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800518 Sender<T> MakeSender(const std::string_view channel_name) {
Austin Schuha0654152021-02-21 21:38:24 -0800519 const Channel *channel = GetChannel<T>(channel_name);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700520 CHECK(channel != nullptr)
521 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
Austin Schuh28fedcb2020-02-08 15:59:58 -0800522 << T::GetFullyQualifiedName() << "\" } not found in config for "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700523 << name()
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800524 << (configuration::MultiNode(configuration())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700525 ? absl::StrCat(" on node ", node()->name()->string_view())
526 : ".");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700527
Austin Schuhca4828c2019-12-28 14:21:35 -0800528 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
529 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
530 << "\", \"type\": \"" << T::GetFullyQualifiedName()
531 << "\" } is not able to be sent on this node. Check your "
532 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800533 }
534
Alex Perrycb7da4b2019-08-28 19:35:56 -0700535 return Sender<T>(MakeRawSender(channel));
536 }
537
538 // This will watch messages sent to the provided channel.
539 //
Brian Silverman454bc112020-03-05 14:21:25 -0800540 // w must have a non-polymorphic operator() (aka it can only be called with a
541 // single set of arguments; no overloading or templates). It must be callable
542 // with this signature:
543 // void(const MessageType &);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700544 //
Brian Silverman454bc112020-03-05 14:21:25 -0800545 // Lambdas are a common form for w. A std::function will work too.
546 //
547 // Note that bind expressions have polymorphic call operators, so they are not
548 // allowed.
549 //
550 // We template Watch as a whole instead of using std::function<void(const T
551 // &)> to allow deducing MessageType from lambdas and other things which are
552 // implicitly convertible to std::function, but not actually std::function
553 // instantiations. Template deduction guides might allow solving this
554 // differently in newer versions of C++, but those have their own corner
555 // cases.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700556 template <typename Watch>
Brian Silverman454bc112020-03-05 14:21:25 -0800557 void MakeWatcher(const std::string_view channel_name, Watch &&w);
558
559 // Like MakeWatcher, but doesn't have access to the message data. This may be
560 // implemented to use less resources than an equivalent MakeWatcher.
561 //
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800562 // The function will still have access to context(), although that will have
563 // its data field set to nullptr.
Brian Silverman454bc112020-03-05 14:21:25 -0800564 template <typename MessageType>
565 void MakeNoArgWatcher(const std::string_view channel_name,
566 std::function<void()> w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700567
568 // The passed in function will be called when the event loop starts.
569 // Use this to run code once the thread goes into "real-time-mode",
570 virtual void OnRun(::std::function<void()> on_run) = 0;
571
Austin Schuh217a9782019-12-21 23:02:50 -0800572 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800573 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700574
Austin Schuh217a9782019-12-21 23:02:50 -0800575 // Returns the node that this event loop is running on. Returns nullptr if we
576 // are running in single-node mode.
577 virtual const Node *node() const = 0;
578
Alex Perrycb7da4b2019-08-28 19:35:56 -0700579 // Creates a timer that executes callback when the timer expires
580 // Returns a TimerHandle for configuration of the timer
581 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
582
583 // Creates a timer that executes callback periodically at the specified
584 // interval and offset. Returns a PhasedLoopHandler for interacting with the
585 // timer.
586 virtual PhasedLoopHandler *AddPhasedLoop(
587 ::std::function<void(int)> callback,
588 const monotonic_clock::duration interval,
589 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
590
Austin Schuh217a9782019-12-21 23:02:50 -0800591 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592
593 // Threadsafe.
594 bool is_running() const { return is_running_.load(); }
595
596 // Sets the scheduler priority to run the event loop at. This may not be
597 // called after we go into "real-time-mode".
598 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800599 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700600
Brian Silverman6a54ff32020-04-28 16:41:39 -0700601 // Sets the scheduler affinity to run the event loop with. This may only be
602 // called before Run().
603 virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0;
604
Austin Schuh217a9782019-12-21 23:02:50 -0800605 // Fetches new messages from the provided channel (path, type).
606 //
607 // Note: this channel must be a member of the exact configuration object this
608 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700609 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
610 const Channel *channel) = 0;
611
Austin Schuh217a9782019-12-21 23:02:50 -0800612 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700613 virtual void MakeRawWatcher(
614 const Channel *channel,
615 std::function<void(const Context &context, const void *message)>
616 watcher) = 0;
617
Brian Silverman454bc112020-03-05 14:21:25 -0800618 // Watches channel (name, type) for new messages, without needing to extract
619 // the message contents. Default implementation simply re-uses MakeRawWatcher.
620 virtual void MakeRawNoArgWatcher(
621 const Channel *channel,
622 std::function<void(const Context &context)> watcher) {
623 MakeRawWatcher(channel, [watcher](const Context &context, const void *) {
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800624 Context new_context = context;
625 new_context.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700626 new_context.buffer_index = -1;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800627 watcher(new_context);
Brian Silverman454bc112020-03-05 14:21:25 -0800628 });
629 }
630
Austin Schuh217a9782019-12-21 23:02:50 -0800631 // Creates a raw sender for the provided channel. This is used for reflection
632 // based sending.
633 // Note: this ignores any node constraints. Ignore at your own peril.
634 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
635
Austin Schuh6231cc32019-12-07 13:06:15 -0800636 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700637 const Context &context() const { return context_; }
638
639 // Returns the configuration that this event loop was built with.
640 const Configuration *configuration() const { return configuration_; }
641
Austin Schuh39788ff2019-12-01 18:22:57 -0800642 // Prevents the event loop from sending a timing report.
643 void SkipTimingReport() { skip_timing_report_ = true; }
644
Brian Silverman4f4e0612020-08-12 19:54:41 -0700645 // Prevents AOS_LOG being sent to message on /aos.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800646 void SkipAosLog() { skip_logger_ = true; }
647
Brian Silverman4f4e0612020-08-12 19:54:41 -0700648 // Returns the number of buffers for this channel. This corresponds with the
649 // range of Context::buffer_index values for this channel.
650 virtual int NumberBuffers(const Channel *channel) = 0;
651
Austin Schuh20ac95d2020-12-05 17:24:19 -0800652 // Returns the boot UUID.
Austin Schuh83c7f702021-01-19 22:36:29 -0800653 virtual const UUID &boot_uuid() const = 0;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800654
Alex Perrycb7da4b2019-08-28 19:35:56 -0700655 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800656 // Sets the name of the event loop. This is the application name.
657 virtual void set_name(const std::string_view name) = 0;
658
Alex Perrycb7da4b2019-08-28 19:35:56 -0700659 void set_is_running(bool value) { is_running_.store(value); }
660
Austin Schuh39788ff2019-12-01 18:22:57 -0800661 // Validates that channel exists inside configuration_ and finds its index.
662 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700663
Brian Silverman5120afb2020-01-31 17:44:35 -0800664 // Returns the state for the watcher on the corresponding channel. This
665 // watcher must exist before calling this.
666 WatcherState *GetWatcherState(const Channel *channel);
667
Brian Silverman6d2b3592020-06-18 14:40:15 -0700668 // Returns a Sender's protected RawSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800669 template <typename T>
670 static RawSender *GetRawSender(aos::Sender<T> *sender) {
671 return sender->sender_.get();
672 }
673
Brian Silverman6d2b3592020-06-18 14:40:15 -0700674 // Returns a Fetcher's protected RawFetcher.
675 template <typename T>
676 static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) {
677 return fetcher->fetcher_.get();
678 }
679
Austin Schuh6231cc32019-12-07 13:06:15 -0800680 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700681 Context context_;
682
Austin Schuh39788ff2019-12-01 18:22:57 -0800683 friend class RawSender;
684 friend class TimerHandler;
685 friend class RawFetcher;
686 friend class PhasedLoopHandler;
687 friend class WatcherState;
688
689 // Methods used to implement timing reports.
690 void NewSender(RawSender *sender);
691 void DeleteSender(RawSender *sender);
692 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
693 PhasedLoopHandler *NewPhasedLoop(
694 std::unique_ptr<PhasedLoopHandler> phased_loop);
695 void NewFetcher(RawFetcher *fetcher);
696 void DeleteFetcher(RawFetcher *fetcher);
697 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
698
Brian Silverman0fc69932020-01-24 21:54:02 -0800699 // Tracks that we have a (single) watcher on the given channel.
700 void TakeWatcher(const Channel *channel);
701 // Tracks that we have at least one sender on the given channel.
702 void TakeSender(const Channel *channel);
703
Austin Schuh39788ff2019-12-01 18:22:57 -0800704 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800705 std::vector<RawFetcher *> fetchers_;
706
Austin Schuh39788ff2019-12-01 18:22:57 -0800707 std::vector<std::unique_ptr<TimerHandler>> timers_;
708 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800709 std::vector<std::unique_ptr<WatcherState>> watchers_;
710
711 void SendTimingReport();
712 void UpdateTimingReport();
713 void MaybeScheduleTimingReports();
714
715 std::unique_ptr<RawSender> timing_report_sender_;
716
Austin Schuh7d87b672019-12-01 20:23:49 -0800717 // Tracks which event sources (timers and watchers) have data, and which
718 // don't. Added events may not change their event_time().
719 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
720 // t2 to run, t2 should then be picked up without a context switch.
721 void AddEvent(EventLoopEvent *event);
722 void RemoveEvent(EventLoopEvent *event);
723 size_t EventCount() const { return events_.size(); }
724 EventLoopEvent *PopEvent();
725 EventLoopEvent *PeekEvent() { return events_.front(); }
726 void ReserveEvents();
727
728 std::vector<EventLoopEvent *> events_;
Brian Silvermanbd405c02020-06-23 16:25:23 -0700729 size_t event_generation_ = 1;
Austin Schuh7d87b672019-12-01 20:23:49 -0800730
Tyler Chatow67ddb032020-01-12 14:30:04 -0800731 // If true, don't send AOS_LOG to /aos
732 bool skip_logger_ = false;
733
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800734 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800735 virtual pid_t GetTid() = 0;
736
737 FlatbufferDetachedBuffer<timing::Report> timing_report_;
738
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800739 ::std::atomic<bool> is_running_{false};
740
Alex Perrycb7da4b2019-08-28 19:35:56 -0700741 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800742
743 // If true, don't send out timing reports.
744 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800745
746 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700747};
748
749} // namespace aos
750
Austin Schuhb8075812020-10-19 09:36:49 -0700751#include "aos/events/event_loop_tmpl.h" // IWYU pragma: export
Alex Perrycb7da4b2019-08-28 19:35:56 -0700752
753#endif // AOS_EVENTS_EVENT_LOOP_H