blob: 0967bb04bfcf8c52f23c5c4663312500ff72d840 [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>
milind1f1dca32021-07-03 13:50:07 -07007#include <ostream>
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -08009#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -070010
Austin Schuh3054f5f2021-07-21 15:38:01 -070011#include "absl/container/btree_set.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012#include "aos/configuration.h"
13#include "aos/configuration_generated.h"
Austin Schuh1af273d2020-03-07 20:11:34 -080014#include "aos/events/channel_preallocated_allocator.h"
Austin Schuh7d87b672019-12-01 20:23:49 -080015#include "aos/events/event_loop_event.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080016#include "aos/events/event_loop_generated.h"
17#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070018#include "aos/flatbuffers.h"
Brian Silverman79ec7fc2020-06-08 20:11:22 -050019#include "aos/ftrace.h"
Brian Silvermana1652f32020-01-29 20:41:44 -080020#include "aos/ipc_lib/data_alignment.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070021#include "aos/json_to_flatbuffer.h"
22#include "aos/time/time.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080023#include "aos/util/phased_loop.h"
Austin Schuh4385b142021-03-14 21:31:13 -070024#include "aos/uuid.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 Schuh8902fa52021-03-14 22:39:24 -070078 // UUID of the remote node which sent this message, or this node in the case
79 // of events which are local to this node.
Austin Schuha9012be2021-07-21 15:19:11 -070080 UUID source_boot_uuid = UUID::Zero();
Austin Schuh8902fa52021-03-14 22:39:24 -070081
Austin Schuhca75b6a2020-12-15 21:12:24 -080082 // Efficiently copies the flatbuffer into a FlatbufferVector, allocating
Austin Schuh678078e2020-08-01 14:30:36 -070083 // memory in the process. It is vital that T matches the type of the
84 // underlying flatbuffer.
85 template <typename T>
86 FlatbufferVector<T> CopyFlatBuffer() const {
Brian Silverman354697a2020-09-22 21:06:32 -070087 ResizeableBuffer buffer;
88 buffer.resize(size);
89 memcpy(buffer.data(), data, size);
90 return FlatbufferVector<T>(std::move(buffer));
Austin Schuh678078e2020-08-01 14:30:36 -070091 }
Alex Perrycb7da4b2019-08-28 19:35:56 -070092};
93
94// Raw version of fetcher. Contains a local variable that the fetcher will
95// update. This is used for reflection and as an interface to implement typed
96// fetchers.
97class RawFetcher {
98 public:
Austin Schuh39788ff2019-12-01 18:22:57 -080099 RawFetcher(EventLoop *event_loop, const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700100 RawFetcher(const RawFetcher &) = delete;
101 RawFetcher &operator=(const RawFetcher &) = delete;
Austin Schuh39788ff2019-12-01 18:22:57 -0800102 virtual ~RawFetcher();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700103
Austin Schuh39788ff2019-12-01 18:22:57 -0800104 // Fetches the next message in the queue without blocking. Returns true if
105 // there was a new message and we got it.
106 bool FetchNext();
107
108 // Fetches the latest message without blocking.
109 bool Fetch();
110
111 // Returns the channel this fetcher uses.
112 const Channel *channel() const { return channel_; }
113 // Returns the context for the current message.
114 const Context &context() const { return context_; }
115
116 protected:
117 EventLoop *event_loop() { return event_loop_; }
Austin Schuh3054f5f2021-07-21 15:38:01 -0700118 const EventLoop *event_loop() const { return event_loop_; }
Austin Schuh39788ff2019-12-01 18:22:57 -0800119
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120 Context context_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800121
122 private:
123 friend class EventLoop;
124 // Implementation
125 virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0;
126 virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0;
127
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500128 EventLoop *const event_loop_;
129 const Channel *const channel_;
130 const std::string ftrace_prefix_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800131
132 internal::RawFetcherTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500133 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134};
135
136// Raw version of sender. Sends a block of data. This is used for reflection
137// and as a building block to implement typed senders.
138class RawSender {
139 public:
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700140 using SharedSpan = std::shared_ptr<const absl::Span<const uint8_t>>;
141
Brian Silverman183859c2022-05-14 02:03:06 -0700142 // This looks a little ugly with no space, but please leave it so clang-format
143 // doesn't keep changing it. Bug is filed at
144 // <https://github.com/llvm/llvm-project/issues/55457>.
145 enum class [[nodiscard]] Error{
milind1f1dca32021-07-03 13:50:07 -0700146 // Represents success and no error
147 kOk,
148
149 // Error for messages on channels being sent faster than their
150 // frequency and channel storage duration allow
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700151 kMessagesSentTooFast,
152 // Access to Redzone was attempted in Sender Queue
Brian Silverman183859c2022-05-14 02:03:06 -0700153 kInvalidRedzone,
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700154 };
milind1f1dca32021-07-03 13:50:07 -0700155
Austin Schuh39788ff2019-12-01 18:22:57 -0800156 RawSender(EventLoop *event_loop, const Channel *channel);
157 RawSender(const RawSender &) = delete;
158 RawSender &operator=(const RawSender &) = delete;
159
160 virtual ~RawSender();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700161
Alex Perrycb7da4b2019-08-28 19:35:56 -0700162 virtual void *data() = 0;
163 virtual size_t size() = 0;
milind1f1dca32021-07-03 13:50:07 -0700164
165 // Sends a message without copying it. The users starts by copying up to
166 // size() bytes into the data backed by data(). They then call Send to send.
167 // Returns Error::kOk on a successful send, or
168 // Error::kMessagesSentTooFast if messages were sent too fast. If provided,
169 // monotonic_remote_time, realtime_remote_time, and remote_queue_index are
170 // attached to the message and are available in the context on the read side.
171 // If they are not populated, the read side will get the sent times instead.
172 Error Send(size_t size);
173 Error Send(size_t size, monotonic_clock::time_point monotonic_remote_time,
174 realtime_clock::time_point realtime_remote_time,
175 uint32_t remote_queue_index, const UUID &source_boot_uuid);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176
177 // Sends a single block of data by copying it.
Austin Schuhad154822019-12-27 15:45:13 -0800178 // The remote arguments have the same meaning as in Send above.
milind1f1dca32021-07-03 13:50:07 -0700179 // Returns Error::kMessagesSentTooFast if messages were sent too fast
180 Error Send(const void *data, size_t size);
181 Error Send(const void *data, size_t size,
182 monotonic_clock::time_point monotonic_remote_time,
183 realtime_clock::time_point realtime_remote_time,
184 uint32_t remote_queue_index, const UUID &source_boot_uuid);
185
186 // CHECKs that no sending Error occurred and logs the channel_ data if
187 // one did
188 void CheckOk(const Error err);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700190 // Sends a single block of data by refcounting it to avoid copies. The data
191 // must not change after being passed into Send. The remote arguments have the
192 // same meaning as in Send above.
milind1f1dca32021-07-03 13:50:07 -0700193 Error Send(const SharedSpan data);
194 Error Send(const SharedSpan data,
195 monotonic_clock::time_point monotonic_remote_time,
196 realtime_clock::time_point realtime_remote_time,
197 uint32_t remote_queue_index, const UUID &remote_boot_uuid);
Tyler Chatowb7c6eba2021-07-28 14:43:23 -0700198
Austin Schuh54cf95f2019-11-29 13:14:18 -0800199 const Channel *channel() const { return channel_; }
200
Austin Schuhad154822019-12-27 15:45:13 -0800201 // Returns the time_points that the last message was sent at.
202 aos::monotonic_clock::time_point monotonic_sent_time() const {
203 return monotonic_sent_time_;
204 }
205 aos::realtime_clock::time_point realtime_sent_time() const {
206 return realtime_sent_time_;
207 }
208 // Returns the queue index that this was sent with.
209 uint32_t sent_queue_index() const { return sent_queue_index_; }
210
Brian Silvermana1652f32020-01-29 20:41:44 -0800211 // Returns the associated flatbuffers-style allocator. This must be
212 // deallocated before the message is sent.
Austin Schuh1af273d2020-03-07 20:11:34 -0800213 ChannelPreallocatedAllocator *fbb_allocator() {
214 fbb_allocator_ = ChannelPreallocatedAllocator(
215 reinterpret_cast<uint8_t *>(data()), size(), channel());
Brian Silvermana1652f32020-01-29 20:41:44 -0800216 return &fbb_allocator_;
217 }
218
Brian Silverman4f4e0612020-08-12 19:54:41 -0700219 // Index of the buffer which is currently exposed by data() and the various
220 // other accessors. This is the message the caller should be filling out.
221 virtual int buffer_index() = 0;
222
Alex Perrycb7da4b2019-08-28 19:35:56 -0700223 protected:
Austin Schuh39788ff2019-12-01 18:22:57 -0800224 EventLoop *event_loop() { return event_loop_; }
Austin Schuh3054f5f2021-07-21 15:38:01 -0700225 const EventLoop *event_loop() const { return event_loop_; }
Austin Schuh54cf95f2019-11-29 13:14:18 -0800226
Austin Schuhb5c6f972021-03-14 21:53:07 -0700227 monotonic_clock::time_point monotonic_sent_time_ = monotonic_clock::min_time;
228 realtime_clock::time_point realtime_sent_time_ = realtime_clock::min_time;
Austin Schuhad154822019-12-27 15:45:13 -0800229 uint32_t sent_queue_index_ = 0xffffffff;
230
Austin Schuh39788ff2019-12-01 18:22:57 -0800231 private:
232 friend class EventLoop;
233
milind1f1dca32021-07-03 13:50:07 -0700234 virtual Error DoSend(const void *data, size_t size,
235 monotonic_clock::time_point monotonic_remote_time,
236 realtime_clock::time_point realtime_remote_time,
237 uint32_t remote_queue_index,
238 const UUID &source_boot_uuid) = 0;
239 virtual Error DoSend(size_t size,
240 monotonic_clock::time_point monotonic_remote_time,
241 realtime_clock::time_point realtime_remote_time,
242 uint32_t remote_queue_index,
243 const UUID &source_boot_uuid) = 0;
244 virtual Error DoSend(const SharedSpan data,
245 monotonic_clock::time_point monotonic_remote_time,
246 realtime_clock::time_point realtime_remote_time,
247 uint32_t remote_queue_index,
248 const UUID &source_boot_uuid);
Austin Schuh39788ff2019-12-01 18:22:57 -0800249
James Kuszmaul93abac12022-04-14 15:05:10 -0700250 void RecordSendResult(const Error error, size_t message_size);
251
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500252 EventLoop *const event_loop_;
253 const Channel *const channel_;
254 const std::string ftrace_prefix_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700255
Austin Schuh39788ff2019-12-01 18:22:57 -0800256 internal::RawSenderTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500257 Ftrace ftrace_;
Brian Silvermana1652f32020-01-29 20:41:44 -0800258
Austin Schuh1af273d2020-03-07 20:11:34 -0800259 ChannelPreallocatedAllocator fbb_allocator_{nullptr, 0, nullptr};
Austin Schuh39788ff2019-12-01 18:22:57 -0800260};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700261
milind1f1dca32021-07-03 13:50:07 -0700262// Needed for compatibility with glog
263std::ostream &operator<<(std::ostream &os, const RawSender::Error err);
264
Alex Perrycb7da4b2019-08-28 19:35:56 -0700265// Fetches the newest message from a channel.
266// This provides a polling based interface for channels.
267template <typename T>
268class Fetcher {
269 public:
270 Fetcher() {}
271
272 // Fetches the next message. Returns true if it fetched a new message. This
273 // method will only return messages sent after the Fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800274 bool FetchNext() {
275 const bool result = fetcher_->FetchNext();
276 if (result) {
277 CheckChannelDataAlignment(fetcher_->context().data,
278 fetcher_->context().size);
279 }
280 return result;
281 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700282
283 // Fetches the most recent message. Returns true if it fetched a new message.
284 // This will return the latest message regardless of if it was sent before or
285 // after the fetcher was created.
Brian Silvermana1652f32020-01-29 20:41:44 -0800286 bool Fetch() {
287 const bool result = fetcher_->Fetch();
288 if (result) {
289 CheckChannelDataAlignment(fetcher_->context().data,
290 fetcher_->context().size);
291 }
292 return result;
293 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700294
295 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
296 // available message.
297 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800298 return fetcher_->context().data != nullptr
299 ? flatbuffers::GetRoot<T>(
300 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700301 : nullptr;
302 }
303
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700304 // Returns the channel this fetcher uses
305 const Channel *channel() const { return fetcher_->channel(); }
306
Alex Perrycb7da4b2019-08-28 19:35:56 -0700307 // Returns the context holding timestamps and other metadata about the
308 // message.
309 const Context &context() const { return fetcher_->context(); }
310
311 const T &operator*() const { return *get(); }
312 const T *operator->() const { return get(); }
313
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700314 // Returns true if this fetcher is valid and connected to a channel.
Milind Upadhyay49174a72021-04-10 16:24:57 -0700315 bool valid() const { return static_cast<bool>(fetcher_); }
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700316
Austin Schuhca75b6a2020-12-15 21:12:24 -0800317 // Copies the current flatbuffer into a FlatbufferVector.
318 FlatbufferVector<T> CopyFlatBuffer() const {
319 return context().template CopyFlatBuffer<T>();
320 }
321
Alex Perrycb7da4b2019-08-28 19:35:56 -0700322 private:
323 friend class EventLoop;
324 Fetcher(::std::unique_ptr<RawFetcher> fetcher)
325 : fetcher_(::std::move(fetcher)) {}
326 ::std::unique_ptr<RawFetcher> fetcher_;
327};
328
329// Sends messages to a channel.
330template <typename T>
331class Sender {
332 public:
333 Sender() {}
334
335 // Represents a single message about to be sent to the queue.
336 // The lifecycle goes:
337 //
338 // Builder builder = sender.MakeBuilder();
339 // T::Builder t_builder = builder.MakeBuilder<T>();
340 // Populate(&t_builder);
341 // builder.Send(t_builder.Finish());
342 class Builder {
343 public:
Austin Schuh1af273d2020-03-07 20:11:34 -0800344 Builder(RawSender *sender, ChannelPreallocatedAllocator *allocator)
Brian Silverman9dd793b2020-01-31 23:52:21 -0800345 : fbb_(allocator->size(), allocator),
346 allocator_(allocator),
347 sender_(sender) {
Brian Silvermana1652f32020-01-29 20:41:44 -0800348 CheckChannelDataAlignment(allocator->data(), allocator->size());
Austin Schuhd7b15da2020-02-17 15:06:11 -0800349 fbb_.ForceDefaults(true);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700350 }
Brian Silvermana1652f32020-01-29 20:41:44 -0800351 Builder() {}
352 Builder(const Builder &) = delete;
353 Builder(Builder &&) = default;
354
355 Builder &operator=(const Builder &) = delete;
356 Builder &operator=(Builder &&) = default;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700357
358 flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; }
359
360 template <typename T2>
361 typename T2::Builder MakeBuilder() {
362 return typename T2::Builder(fbb_);
363 }
364
milind1f1dca32021-07-03 13:50:07 -0700365 RawSender::Error Send(flatbuffers::Offset<T> offset) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700366 fbb_.Finish(offset);
milind1f1dca32021-07-03 13:50:07 -0700367 const auto err = sender_->Send(fbb_.GetSize());
Brian Silverman9dd793b2020-01-31 23:52:21 -0800368 // Ensure fbb_ knows it shouldn't access the memory any more.
369 fbb_ = flatbuffers::FlatBufferBuilder();
milind1f1dca32021-07-03 13:50:07 -0700370 return err;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700371 }
372
milind1f1dca32021-07-03 13:50:07 -0700373 // Equivalent to RawSender::CheckOk
374 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
375
Alex Perrycb7da4b2019-08-28 19:35:56 -0700376 // CHECKs that this message was sent.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800377 void CheckSent() {
378 CHECK(!allocator_->is_allocated()) << ": Message was not sent yet";
379 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700380
Brian Silverman341b57e2020-06-23 16:23:18 -0700381 // Detaches a buffer, for later use calling Sender::Send directly.
382 //
383 // Note that the underlying memory remains with the Sender, so creating
384 // another Builder before destroying the FlatbufferDetachedBuffer will fail.
385 FlatbufferDetachedBuffer<T> Detach(flatbuffers::Offset<T> offset) {
386 fbb_.Finish(offset);
387 return fbb_.Release();
388 }
389
Alex Perrycb7da4b2019-08-28 19:35:56 -0700390 private:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700391 flatbuffers::FlatBufferBuilder fbb_;
Austin Schuh1af273d2020-03-07 20:11:34 -0800392 ChannelPreallocatedAllocator *allocator_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700393 RawSender *sender_;
394 };
395
396 // Constructs an above builder.
Brian Silverman9dd793b2020-01-31 23:52:21 -0800397 //
398 // Only a single one of these may be "alive" for this object at any point in
399 // time. After calling Send on the result, it is no longer "alive". This means
400 // that you must manually reset a variable holding the return value (by
401 // assigning a default-constructed Builder to it) before calling this method
402 // again to overwrite the value in the variable.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700403 Builder MakeBuilder();
404
Austin Schuha28cbc32019-12-27 16:28:04 -0800405 // Sends a prebuilt flatbuffer.
milind1f1dca32021-07-03 13:50:07 -0700406 RawSender::Error Send(const NonSizePrefixedFlatbuffer<T> &flatbuffer);
Austin Schuha28cbc32019-12-27 16:28:04 -0800407
Brian Silverman341b57e2020-06-23 16:23:18 -0700408 // Sends a prebuilt flatbuffer which was detached from a Builder created via
409 // MakeBuilder() on this object.
milind1f1dca32021-07-03 13:50:07 -0700410 RawSender::Error SendDetached(FlatbufferDetachedBuffer<T> detached);
411
412 // Equivalent to RawSender::CheckOk
413 void CheckOk(const RawSender::Error err) { sender_->CheckOk(err); };
Brian Silverman341b57e2020-06-23 16:23:18 -0700414
Austin Schuh39788ff2019-12-01 18:22:57 -0800415 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800416 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700417
Austin Schuhe33c08d2022-02-03 18:15:21 -0800418 // TODO(austin): Deprecate the operator bool.
Austin Schuha0c41ba2020-09-10 22:59:14 -0700419 operator bool() const { return sender_ ? true : false; }
Austin Schuhe33c08d2022-02-03 18:15:21 -0800420 bool valid() const { return static_cast<bool>(sender_); }
Tyler Chatow67ddb032020-01-12 14:30:04 -0800421
Austin Schuh7bc59052020-02-16 23:48:33 -0800422 // Returns the time_points that the last message was sent at.
423 aos::monotonic_clock::time_point monotonic_sent_time() const {
424 return sender_->monotonic_sent_time();
425 }
426 aos::realtime_clock::time_point realtime_sent_time() const {
427 return sender_->realtime_sent_time();
428 }
429 // Returns the queue index that this was sent with.
430 uint32_t sent_queue_index() const { return sender_->sent_queue_index(); }
431
Brian Silverman4f4e0612020-08-12 19:54:41 -0700432 // Returns the buffer index which MakeBuilder() will expose access to. This is
433 // the buffer the caller can fill out.
434 int buffer_index() const { return sender_->buffer_index(); }
435
Alex Perrycb7da4b2019-08-28 19:35:56 -0700436 private:
437 friend class EventLoop;
438 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
439 std::unique_ptr<RawSender> sender_;
440};
441
milind1f1dca32021-07-03 13:50:07 -0700442// Class for keeping a count of send failures on a certain channel
443class SendFailureCounter {
444 public:
445 inline void Count(const RawSender::Error err) {
446 failures_ += static_cast<size_t>(err != RawSender::Error::kOk);
447 just_failed_ = (err != RawSender::Error::kOk);
448 }
449
450 inline size_t failures() const { return failures_; }
451 inline bool just_failed() const { return just_failed_; }
452
453 private:
454 size_t failures_ = 0;
455 bool just_failed_ = false;
456};
457
Brian Silverman4f4e0612020-08-12 19:54:41 -0700458// Interface for timers.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700459class TimerHandler {
460 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800461 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700462
463 // Timer should sleep until base, base + offset, base + offset * 2, ...
464 // If repeat_offset isn't set, the timer only expires once.
James Kuszmaul8866e642022-06-10 16:00:36 -0700465 // base must be greater than or equal to zero.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 virtual void Setup(monotonic_clock::time_point base,
467 monotonic_clock::duration repeat_offset =
468 ::aos::monotonic_clock::zero()) = 0;
469
470 // Stop future calls to callback().
471 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800472
473 // Sets and gets the name of the timer. Set this if you want a descriptive
474 // name in the timing report.
475 void set_name(std::string_view name) { name_ = std::string(name); }
476 const std::string_view name() const { return name_; }
477
Austin Schuh39788ff2019-12-01 18:22:57 -0800478 protected:
479 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
480
Austin Schuh9b1d6282022-06-10 17:03:21 -0700481 template <typename T>
482 monotonic_clock::time_point Call(T get_time,
483 monotonic_clock::time_point event_time);
Austin Schuh39788ff2019-12-01 18:22:57 -0800484
Austin Schuh1540c2f2019-11-29 21:59:29 -0800485 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800486 friend class EventLoop;
487
488 EventLoop *event_loop_;
489 // The function to call when Call is called.
490 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800491 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800492
493 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500494 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700495};
496
497// Interface for phased loops. They are built on timers.
498class PhasedLoopHandler {
499 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800500 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700501
502 // Sets the interval and offset. Any changes to interval and offset only take
503 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800504 void set_interval_and_offset(const monotonic_clock::duration interval,
505 const monotonic_clock::duration offset) {
506 phased_loop_.set_interval_and_offset(interval, offset);
507 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800508
509 // Sets and gets the name of the timer. Set this if you want a descriptive
510 // name in the timing report.
511 void set_name(std::string_view name) { name_ = std::string(name); }
512 const std::string_view name() const { return name_; }
513
Austin Schuh39788ff2019-12-01 18:22:57 -0800514 protected:
515 void Call(std::function<monotonic_clock::time_point()> get_time,
516 std::function<void(monotonic_clock::time_point)> schedule);
517
518 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
519 const monotonic_clock::duration interval,
520 const monotonic_clock::duration offset);
521
Austin Schuh1540c2f2019-11-29 21:59:29 -0800522 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800523 friend class EventLoop;
524
525 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
526 monotonic_clock::time_point monotonic_now) {
527 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
528 schedule(phased_loop_.sleep_time());
529 }
530
531 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
532
533 EventLoop *event_loop_;
534 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800535 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800536 time::PhasedLoop phased_loop_;
537
538 int cycles_elapsed_ = 0;
539
540 internal::TimerTiming timing_;
Brian Silverman79ec7fc2020-06-08 20:11:22 -0500541 Ftrace ftrace_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700542};
543
Austin Schuh3054f5f2021-07-21 15:38:01 -0700544// Note, it is supported to create only:
545// multiple fetchers, and (one sender or one watcher) per <name, type>
546// tuple.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700547class EventLoop {
548 public:
Austin Schuh3054f5f2021-07-21 15:38:01 -0700549 // Holds configuration by reference for the lifetime of this object. It may
550 // never be mutated externally in any way.
Austin Schuh83c7f702021-01-19 22:36:29 -0800551 EventLoop(const Configuration *configuration);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700552
Austin Schuh39788ff2019-12-01 18:22:57 -0800553 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700554
555 // Current time.
Stephan Pleines559fa6c2022-01-06 17:23:51 -0800556 virtual monotonic_clock::time_point monotonic_now() const = 0;
557 virtual realtime_clock::time_point realtime_now() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700558
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700559 template <typename T>
Austin Schuha0654152021-02-21 21:38:24 -0800560 const Channel *GetChannel(const std::string_view channel_name) {
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800561 return configuration::GetChannel(configuration(), channel_name,
Austin Schuh0de30f32020-12-06 12:44:28 -0800562 T::GetFullyQualifiedName(), name(), node(),
Austin Schuha0654152021-02-21 21:38:24 -0800563 true);
564 }
milind1f1dca32021-07-03 13:50:07 -0700565 // Returns true if the channel exists in the configuration.
Austin Schuha0654152021-02-21 21:38:24 -0800566 template <typename T>
567 bool HasChannel(const std::string_view channel_name) {
568 return GetChannel<T>(channel_name) != nullptr;
Brian Silvermande9f3ff2020-04-28 16:56:58 -0700569 }
570
Brian Silverman631b6262021-11-10 12:25:08 -0800571 // Like MakeFetcher, but returns an invalid fetcher if the given channel is
572 // not readable on this node or does not exist.
573 template <typename T>
574 Fetcher<T> TryMakeFetcher(const std::string_view channel_name) {
575 const Channel *const channel = GetChannel<T>(channel_name);
576 if (channel == nullptr) {
577 return Fetcher<T>();
578 }
579
580 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
581 return Fetcher<T>();
582 }
583
584 return Fetcher<T>(MakeRawFetcher(channel));
585 }
586
Alex Perrycb7da4b2019-08-28 19:35:56 -0700587 // Makes a class that will always fetch the most recent value
588 // sent to the provided channel.
589 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800590 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800591 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
593 << T::GetFullyQualifiedName() << "\" } not found in config.";
594
Brian Silverman631b6262021-11-10 12:25:08 -0800595 Fetcher<T> result = TryMakeFetcher<T>(channel_name);
596 if (!result.valid()) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800597 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
598 << "\", \"type\": \"" << T::GetFullyQualifiedName()
599 << "\" } is not able to be fetched on this node. Check your "
600 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800601 }
602
Brian Silverman631b6262021-11-10 12:25:08 -0800603 return result;
604 }
605
606 // Like MakeSender, but returns an invalid sender if the given channel is
James Kuszmaulb8887592021-12-09 14:54:50 -0800607 // not sendable on this node or does not exist.
Brian Silverman631b6262021-11-10 12:25:08 -0800608 template <typename T>
609 Sender<T> TryMakeSender(const std::string_view channel_name) {
610 const Channel *channel = GetChannel<T>(channel_name);
611 if (channel == nullptr) {
612 return Sender<T>();
613 }
614
615 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
616 return Sender<T>();
617 }
618
619 return Sender<T>(MakeRawSender(channel));
Alex Perrycb7da4b2019-08-28 19:35:56 -0700620 }
621
622 // Makes class that allows constructing and sending messages to
623 // the provided channel.
624 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800625 Sender<T> MakeSender(const std::string_view channel_name) {
Brian Silverman631b6262021-11-10 12:25:08 -0800626 CHECK(HasChannel<T>(channel_name))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700627 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
Austin Schuh28fedcb2020-02-08 15:59:58 -0800628 << T::GetFullyQualifiedName() << "\" } not found in config for "
Austin Schuh2f8fd752020-09-01 22:38:28 -0700629 << name()
Austin Schuhcaa2a5d2020-11-01 22:38:20 -0800630 << (configuration::MultiNode(configuration())
Austin Schuh2f8fd752020-09-01 22:38:28 -0700631 ? absl::StrCat(" on node ", node()->name()->string_view())
632 : ".");
Alex Perrycb7da4b2019-08-28 19:35:56 -0700633
Brian Silverman631b6262021-11-10 12:25:08 -0800634 Sender<T> result = TryMakeSender<T>(channel_name);
635 if (!result) {
Austin Schuhca4828c2019-12-28 14:21:35 -0800636 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
637 << "\", \"type\": \"" << T::GetFullyQualifiedName()
638 << "\" } is not able to be sent on this node. Check your "
639 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800640 }
641
Brian Silverman631b6262021-11-10 12:25:08 -0800642 return result;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700643 }
644
645 // This will watch messages sent to the provided channel.
646 //
Brian Silverman454bc112020-03-05 14:21:25 -0800647 // w must have a non-polymorphic operator() (aka it can only be called with a
648 // single set of arguments; no overloading or templates). It must be callable
649 // with this signature:
650 // void(const MessageType &);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700651 //
Brian Silverman454bc112020-03-05 14:21:25 -0800652 // Lambdas are a common form for w. A std::function will work too.
653 //
654 // Note that bind expressions have polymorphic call operators, so they are not
655 // allowed.
656 //
657 // We template Watch as a whole instead of using std::function<void(const T
658 // &)> to allow deducing MessageType from lambdas and other things which are
659 // implicitly convertible to std::function, but not actually std::function
660 // instantiations. Template deduction guides might allow solving this
661 // differently in newer versions of C++, but those have their own corner
662 // cases.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700663 template <typename Watch>
Brian Silverman454bc112020-03-05 14:21:25 -0800664 void MakeWatcher(const std::string_view channel_name, Watch &&w);
665
666 // Like MakeWatcher, but doesn't have access to the message data. This may be
667 // implemented to use less resources than an equivalent MakeWatcher.
668 //
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800669 // The function will still have access to context(), although that will have
670 // its data field set to nullptr.
Brian Silverman454bc112020-03-05 14:21:25 -0800671 template <typename MessageType>
672 void MakeNoArgWatcher(const std::string_view channel_name,
673 std::function<void()> w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700674
675 // The passed in function will be called when the event loop starts.
676 // Use this to run code once the thread goes into "real-time-mode",
677 virtual void OnRun(::std::function<void()> on_run) = 0;
678
Austin Schuh217a9782019-12-21 23:02:50 -0800679 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800680 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700681
Austin Schuh217a9782019-12-21 23:02:50 -0800682 // Returns the node that this event loop is running on. Returns nullptr if we
683 // are running in single-node mode.
684 virtual const Node *node() const = 0;
685
Alex Perrycb7da4b2019-08-28 19:35:56 -0700686 // Creates a timer that executes callback when the timer expires
687 // Returns a TimerHandle for configuration of the timer
milind-u61227f22021-08-29 15:58:33 -0700688 // TODO(milind): callback should take the number of cycles elapsed as a
689 // parameter.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700690 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
691
692 // Creates a timer that executes callback periodically at the specified
693 // interval and offset. Returns a PhasedLoopHandler for interacting with the
694 // timer.
695 virtual PhasedLoopHandler *AddPhasedLoop(
696 ::std::function<void(int)> callback,
697 const monotonic_clock::duration interval,
698 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
699
Austin Schuh217a9782019-12-21 23:02:50 -0800700 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700701
Austin Schuh3054f5f2021-07-21 15:38:01 -0700702 // May be safely called from any thread.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700703 bool is_running() const { return is_running_.load(); }
704
705 // Sets the scheduler priority to run the event loop at. This may not be
706 // called after we go into "real-time-mode".
707 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh65493d62022-08-17 15:10:37 -0700708 // Defaults to 0 if this loop will not run realtime.
709 virtual int runtime_realtime_priority() const = 0;
710
711 static cpu_set_t DefaultAffinity() {
712 cpu_set_t result;
713 for (int i = 0; i < CPU_SETSIZE; ++i) {
714 CPU_SET(i, &result);
715 }
716 return result;
717 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700718
Brian Silverman6a54ff32020-04-28 16:41:39 -0700719 // Sets the scheduler affinity to run the event loop with. This may only be
720 // called before Run().
721 virtual void SetRuntimeAffinity(const cpu_set_t &cpuset) = 0;
Austin Schuh65493d62022-08-17 15:10:37 -0700722 // Defaults to DefaultAffinity() if this loop will not run pinned.
723 virtual const cpu_set_t &runtime_affinity() const = 0;
Brian Silverman6a54ff32020-04-28 16:41:39 -0700724
Austin Schuh217a9782019-12-21 23:02:50 -0800725 // Fetches new messages from the provided channel (path, type).
726 //
727 // Note: this channel must be a member of the exact configuration object this
728 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700729 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
730 const Channel *channel) = 0;
731
Austin Schuh217a9782019-12-21 23:02:50 -0800732 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700733 virtual void MakeRawWatcher(
734 const Channel *channel,
735 std::function<void(const Context &context, const void *message)>
736 watcher) = 0;
737
Brian Silverman454bc112020-03-05 14:21:25 -0800738 // Watches channel (name, type) for new messages, without needing to extract
739 // the message contents. Default implementation simply re-uses MakeRawWatcher.
740 virtual void MakeRawNoArgWatcher(
741 const Channel *channel,
742 std::function<void(const Context &context)> watcher) {
743 MakeRawWatcher(channel, [watcher](const Context &context, const void *) {
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800744 Context new_context = context;
745 new_context.data = nullptr;
Brian Silverman4f4e0612020-08-12 19:54:41 -0700746 new_context.buffer_index = -1;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800747 watcher(new_context);
Brian Silverman454bc112020-03-05 14:21:25 -0800748 });
749 }
750
Austin Schuh217a9782019-12-21 23:02:50 -0800751 // Creates a raw sender for the provided channel. This is used for reflection
752 // based sending.
753 // Note: this ignores any node constraints. Ignore at your own peril.
754 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
755
Austin Schuh6231cc32019-12-07 13:06:15 -0800756 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700757 const Context &context() const { return context_; }
758
759 // Returns the configuration that this event loop was built with.
760 const Configuration *configuration() const { return configuration_; }
761
Austin Schuh39788ff2019-12-01 18:22:57 -0800762 // Prevents the event loop from sending a timing report.
Brian Silvermanbf889922021-11-10 12:41:57 -0800763 void SkipTimingReport();
Austin Schuh39788ff2019-12-01 18:22:57 -0800764
Brian Silverman4f4e0612020-08-12 19:54:41 -0700765 // Prevents AOS_LOG being sent to message on /aos.
Tyler Chatow67ddb032020-01-12 14:30:04 -0800766 void SkipAosLog() { skip_logger_ = true; }
767
Brian Silverman4f4e0612020-08-12 19:54:41 -0700768 // Returns the number of buffers for this channel. This corresponds with the
769 // range of Context::buffer_index values for this channel.
770 virtual int NumberBuffers(const Channel *channel) = 0;
771
Austin Schuh20ac95d2020-12-05 17:24:19 -0800772 // Returns the boot UUID.
Austin Schuh83c7f702021-01-19 22:36:29 -0800773 virtual const UUID &boot_uuid() const = 0;
Austin Schuh20ac95d2020-12-05 17:24:19 -0800774
Alex Perrycb7da4b2019-08-28 19:35:56 -0700775 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800776 // Sets the name of the event loop. This is the application name.
777 virtual void set_name(const std::string_view name) = 0;
778
Alex Perrycb7da4b2019-08-28 19:35:56 -0700779 void set_is_running(bool value) { is_running_.store(value); }
780
Austin Schuh39788ff2019-12-01 18:22:57 -0800781 // Validates that channel exists inside configuration_ and finds its index.
782 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700783
Brian Silverman5120afb2020-01-31 17:44:35 -0800784 // Returns the state for the watcher on the corresponding channel. This
785 // watcher must exist before calling this.
786 WatcherState *GetWatcherState(const Channel *channel);
787
Brian Silverman6d2b3592020-06-18 14:40:15 -0700788 // Returns a Sender's protected RawSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800789 template <typename T>
790 static RawSender *GetRawSender(aos::Sender<T> *sender) {
791 return sender->sender_.get();
792 }
793
Brian Silverman6d2b3592020-06-18 14:40:15 -0700794 // Returns a Fetcher's protected RawFetcher.
795 template <typename T>
796 static RawFetcher *GetRawFetcher(aos::Fetcher<T> *fetcher) {
797 return fetcher->fetcher_.get();
798 }
799
Austin Schuh6231cc32019-12-07 13:06:15 -0800800 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700801 Context context_;
802
Austin Schuh39788ff2019-12-01 18:22:57 -0800803 friend class RawSender;
804 friend class TimerHandler;
805 friend class RawFetcher;
806 friend class PhasedLoopHandler;
807 friend class WatcherState;
808
809 // Methods used to implement timing reports.
810 void NewSender(RawSender *sender);
811 void DeleteSender(RawSender *sender);
812 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
813 PhasedLoopHandler *NewPhasedLoop(
814 std::unique_ptr<PhasedLoopHandler> phased_loop);
815 void NewFetcher(RawFetcher *fetcher);
816 void DeleteFetcher(RawFetcher *fetcher);
817 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
818
Brian Silverman0fc69932020-01-24 21:54:02 -0800819 // Tracks that we have a (single) watcher on the given channel.
820 void TakeWatcher(const Channel *channel);
821 // Tracks that we have at least one sender on the given channel.
822 void TakeSender(const Channel *channel);
823
Austin Schuh39788ff2019-12-01 18:22:57 -0800824 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800825 std::vector<RawFetcher *> fetchers_;
826
Austin Schuh39788ff2019-12-01 18:22:57 -0800827 std::vector<std::unique_ptr<TimerHandler>> timers_;
828 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800829 std::vector<std::unique_ptr<WatcherState>> watchers_;
830
Brian Silvermance418d02021-11-03 11:25:52 -0700831 // Does nothing if timing reports are disabled.
Austin Schuh39788ff2019-12-01 18:22:57 -0800832 void SendTimingReport();
Brian Silvermance418d02021-11-03 11:25:52 -0700833
Austin Schuh39788ff2019-12-01 18:22:57 -0800834 void UpdateTimingReport();
835 void MaybeScheduleTimingReports();
836
837 std::unique_ptr<RawSender> timing_report_sender_;
838
Austin Schuh7d87b672019-12-01 20:23:49 -0800839 // Tracks which event sources (timers and watchers) have data, and which
840 // don't. Added events may not change their event_time().
841 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
842 // t2 to run, t2 should then be picked up without a context switch.
843 void AddEvent(EventLoopEvent *event);
844 void RemoveEvent(EventLoopEvent *event);
845 size_t EventCount() const { return events_.size(); }
846 EventLoopEvent *PopEvent();
847 EventLoopEvent *PeekEvent() { return events_.front(); }
848 void ReserveEvents();
849
850 std::vector<EventLoopEvent *> events_;
Brian Silvermanbd405c02020-06-23 16:25:23 -0700851 size_t event_generation_ = 1;
Austin Schuh7d87b672019-12-01 20:23:49 -0800852
Tyler Chatow67ddb032020-01-12 14:30:04 -0800853 // If true, don't send AOS_LOG to /aos
854 bool skip_logger_ = false;
855
Austin Schuha9012be2021-07-21 15:19:11 -0700856 // Sets context_ for a timed event which is supposed to happen at the provided
857 // time.
858 void SetTimerContext(monotonic_clock::time_point monotonic_event_time);
859
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800860 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800861 virtual pid_t GetTid() = 0;
862
863 FlatbufferDetachedBuffer<timing::Report> timing_report_;
864
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800865 ::std::atomic<bool> is_running_{false};
866
Alex Perrycb7da4b2019-08-28 19:35:56 -0700867 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800868
869 // If true, don't send out timing reports.
870 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800871
milind1f1dca32021-07-03 13:50:07 -0700872 SendFailureCounter timing_report_failure_counter_;
873
Brian Silverman0fc69932020-01-24 21:54:02 -0800874 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700875};
876
877} // namespace aos
878
Austin Schuhb8075812020-10-19 09:36:49 -0700879#include "aos/events/event_loop_tmpl.h" // IWYU pragma: export
Alex Perrycb7da4b2019-08-28 19:35:56 -0700880
881#endif // AOS_EVENTS_EVENT_LOOP_H