blob: 2cafb48ad9949bdb0cbece937780940ec5913e4c [file] [log] [blame]
Austin Schuh20b2b082019-09-11 20:42:56 -07001#ifndef AOS_IPC_LIB_LOCKLESS_QUEUE_H_
2#define AOS_IPC_LIB_LOCKLESS_QUEUE_H_
3
Austin Schuh20b2b082019-09-11 20:42:56 -07004#include <sys/signalfd.h>
5#include <sys/types.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07006
7#include <csignal>
Austin Schuhe516ab02020-05-06 21:37:04 -07008#include <optional>
Brian Silverman177567e2020-08-12 19:51:33 -07009#include <vector>
Austin Schuh20b2b082019-09-11 20:42:56 -070010
Brian Silverman0eaa1da2020-08-12 20:03:52 -070011#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070012
Austin Schuh82ea7382023-07-14 15:17:34 -070013#include "aos/events/context.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070014#include "aos/ipc_lib/aos_sync.h"
Brian Silvermana1652f32020-01-29 20:41:44 -080015#include "aos/ipc_lib/data_alignment.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070016#include "aos/ipc_lib/index.h"
17#include "aos/time/time.h"
Austin Schuh8902fa52021-03-14 22:39:24 -070018#include "aos/uuid.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070019
20namespace aos {
21namespace ipc_lib {
22
23// Structure to hold the state required to wake a watcher.
24struct Watcher {
25 // Mutex that the watcher locks. If the futex is 0 (or FUTEX_OWNER_DIED),
26 // then this watcher is invalid. The futex variable will then hold the tid of
27 // the watcher, or FUTEX_OWNER_DIED if the task died.
28 //
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080029 // Note: this is only modified with the queue_setup_lock lock held, but may
30 // always be read.
Austin Schuh20b2b082019-09-11 20:42:56 -070031 // Any state modification should happen before the lock is acquired.
32 aos_mutex tid;
33
34 // PID of the watcher.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080035 std::atomic<pid_t> pid;
Austin Schuh20b2b082019-09-11 20:42:56 -070036
37 // RT priority of the watcher.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080038 std::atomic<int> priority;
Austin Schuh20b2b082019-09-11 20:42:56 -070039};
40
41// Structure to hold the state required to send messages.
42struct Sender {
43 // Mutex that the sender locks. If the futex is 0 (or FUTEX_OWNER_DIED), then
44 // this sender is invalid. The futex variable will then hold the tid of the
45 // sender, or FUTEX_OWNER_DIED if the task died.
46 //
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080047 // Note: this is only modified with the queue_setup_lock lock held, but may
48 // always be read.
Austin Schuh20b2b082019-09-11 20:42:56 -070049 aos_mutex tid;
50
51 // Index of the message we will be filling out.
52 AtomicIndex scratch_index;
53
54 // Index of the element being swapped with scratch_index, or Invalid if there
55 // is nothing to do.
56 AtomicIndex to_replace;
57};
58
Brian Silverman177567e2020-08-12 19:51:33 -070059// Structure to hold the state required to pin messages.
60struct Pinner {
61 // The same as Sender::tid. See there for docs.
62 aos_mutex tid;
63
64 // Queue index of the message we have pinned, or Invalid if there isn't one.
65 AtomicQueueIndex pinned;
66
67 // This should always be valid.
68 //
69 // Note that this is fully independent from pinned. It's just a place to stash
70 // a message, to ensure there's always an unpinned one for a writer to grab.
71 AtomicIndex scratch_index;
72};
73
Austin Schuh20b2b082019-09-11 20:42:56 -070074// Structure representing a message.
75struct Message {
76 struct Header {
77 // Index of this message in the queue. Needs to match the index this
78 // message is written into the queue at. The data in this message is only
79 // valid if it matches the index in the queue both before and after all the
80 // data is read.
81 //
82 // Note: a value of 0xffffffff always means that the contents aren't valid.
83 AtomicQueueIndex queue_index;
84
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080085 // Timestamp of the message. Needs to be monotonically incrementing in the
Austin Schuh20b2b082019-09-11 20:42:56 -070086 // queue, which means that time needs to be re-sampled every time a write
87 // fails.
Austin Schuhb5c6f972021-03-14 21:53:07 -070088 monotonic_clock::time_point monotonic_sent_time;
89 realtime_clock::time_point realtime_sent_time;
Austin Schuhad154822019-12-27 15:45:13 -080090 // Timestamps of the message from the remote node. These are transparently
91 // passed through.
Austin Schuhb5c6f972021-03-14 21:53:07 -070092 monotonic_clock::time_point monotonic_remote_time;
93 realtime_clock::time_point realtime_remote_time;
Austin Schuhad154822019-12-27 15:45:13 -080094
95 // Queue index from the remote node.
96 uint32_t remote_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -070097
Austin Schuh8902fa52021-03-14 22:39:24 -070098 // Remote boot UUID for this message.
Austin Schuha9012be2021-07-21 15:19:11 -070099 UUID source_boot_uuid;
Austin Schuh8902fa52021-03-14 22:39:24 -0700100
Austin Schuh20b2b082019-09-11 20:42:56 -0700101 size_t length;
102 } header;
103
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700104 // Returns the start of the data buffer, given that message_data_size is
105 // the same one used to allocate this message's memory.
106 char *data(size_t message_data_size) {
107 return RoundedData(message_data_size);
108 }
109 const char *data(size_t message_data_size) const {
110 return RoundedData(message_data_size);
111 }
112
113 // Returns the pre-buffer redzone, given that message_data_size is the same
114 // one used to allocate this message's memory.
115 absl::Span<char> PreRedzone(size_t message_data_size) {
116 char *const end = data(message_data_size);
117 const auto result =
118 absl::Span<char>(&data_pointer[0], end - &data_pointer[0]);
119 DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment);
120 return result;
121 }
122 absl::Span<const char> PreRedzone(size_t message_data_size) const {
123 const char *const end = data(message_data_size);
124 const auto result =
125 absl::Span<const char>(&data_pointer[0], end - &data_pointer[0]);
126 DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment);
127 return result;
128 }
129
130 // Returns the post-buffer redzone, given that message_data_size is the same
131 // one used to allocate this message's memory.
132 absl::Span<char> PostRedzone(size_t message_data_size, size_t message_size) {
133 DCHECK_LT(message_data_size, message_size);
134 char *const redzone_end = reinterpret_cast<char *>(this) + message_size;
135 char *const data_end = data(message_data_size) + message_data_size;
136 DCHECK_GT(static_cast<void *>(redzone_end), static_cast<void *>(data_end));
137 const auto result = absl::Span<char>(data_end, redzone_end - data_end);
138 DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment * 2);
139 return result;
140 }
141 absl::Span<const char> PostRedzone(size_t message_data_size,
142 size_t message_size) const {
143 DCHECK_LT(message_data_size, message_size);
144 const char *const redzone_end =
145 reinterpret_cast<const char *>(this) + message_size;
146 const char *const data_end = data(message_data_size) + message_data_size;
147 DCHECK_GT(static_cast<const void *>(redzone_end),
148 static_cast<const void *>(data_end));
149 const auto result =
150 absl::Span<const char>(data_end, redzone_end - data_end);
151 DCHECK_LT(result.size(), kChannelDataRedzone + kChannelDataAlignment * 2);
152 return result;
Brian Silvermana1652f32020-01-29 20:41:44 -0800153 }
154
155 private:
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700156 // This returns a non-const pointer into a const object. Be very careful
157 // about const correctness in publicly accessible APIs using it.
158 char *RoundedData(size_t message_data_size) const {
159 return RoundChannelData(
160 const_cast<char *>(&data_pointer[0] + kChannelDataRedzone),
161 message_data_size);
Brian Silvermana1652f32020-01-29 20:41:44 -0800162 }
163
164 char data_pointer[];
Austin Schuh20b2b082019-09-11 20:42:56 -0700165};
166
167struct LocklessQueueConfiguration {
168 // Size of the watchers list.
169 size_t num_watchers;
170 // Size of the sender list.
171 size_t num_senders;
Brian Silverman177567e2020-08-12 19:51:33 -0700172 // Size of the pinner list.
173 size_t num_pinners;
Austin Schuh20b2b082019-09-11 20:42:56 -0700174
175 // Size of the list of pointers into the messages list.
176 size_t queue_size;
177 // Size in bytes of the data stored in each Message.
178 size_t message_data_size;
179
Austin Schuh4bc4f902019-12-23 18:04:51 -0800180 size_t message_size() const;
Austin Schuh20b2b082019-09-11 20:42:56 -0700181
Brian Silverman177567e2020-08-12 19:51:33 -0700182 size_t num_messages() const { return num_senders + num_pinners + queue_size; }
Austin Schuh20b2b082019-09-11 20:42:56 -0700183};
184
185// Structure to hold the state of the queue.
186//
187// Reads and writes are lockless and constant time.
188//
189// Adding a new watcher doesn't need to be constant time for the watcher (this
190// is done before the watcher goes RT), but needs to be RT for the sender.
191struct LocklessQueueMemory;
192
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700193// Returns the size of the LocklessQueueMemory.
194size_t LocklessQueueMemorySize(LocklessQueueConfiguration config);
195
Austin Schuh20b2b082019-09-11 20:42:56 -0700196// Initializes the queue memory. memory must be either a valid pointer to the
197// queue datastructure, or must be zero initialized.
198LocklessQueueMemory *InitializeLocklessQueueMemory(
199 LocklessQueueMemory *memory, LocklessQueueConfiguration config);
200
Alex Perrycb7da4b2019-08-28 19:35:56 -0700201const static unsigned int kWakeupSignal = SIGRTMIN + 2;
Austin Schuh20b2b082019-09-11 20:42:56 -0700202
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700203// A convenient wrapper for accessing a lockless queue.
Austin Schuh20b2b082019-09-11 20:42:56 -0700204class LocklessQueue {
205 public:
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700206 LocklessQueue(const LocklessQueueMemory *const_memory,
207 LocklessQueueMemory *memory, LocklessQueueConfiguration config)
208 : const_memory_(const_memory), memory_(memory), config_(config) {}
Austin Schuh20b2b082019-09-11 20:42:56 -0700209
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700210 void Initialize();
Austin Schuh20b2b082019-09-11 20:42:56 -0700211
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700212 LocklessQueueConfiguration config() const { return config_; }
Austin Schuh20b2b082019-09-11 20:42:56 -0700213
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700214 const LocklessQueueMemory *const_memory() { return const_memory_; }
215 LocklessQueueMemory *memory() { return memory_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700216
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700217 private:
218 const LocklessQueueMemory *const_memory_;
219 LocklessQueueMemory *memory_;
220 LocklessQueueConfiguration config_;
221};
222
223class LocklessQueueWatcher {
224 public:
225 LocklessQueueWatcher(const LocklessQueueWatcher &) = delete;
226 LocklessQueueWatcher &operator=(const LocklessQueueWatcher &) = delete;
227 LocklessQueueWatcher(LocklessQueueWatcher &&other)
228 : memory_(other.memory_), watcher_index_(other.watcher_index_) {
229 other.watcher_index_ = -1;
230 }
231 LocklessQueueWatcher &operator=(LocklessQueueWatcher &&other) {
232 std::swap(memory_, other.memory_);
233 std::swap(watcher_index_, other.watcher_index_);
234 return *this;
235 }
236
237 ~LocklessQueueWatcher();
238
239 // Registers this thread to receive the kWakeupSignal signal when
240 // LocklessQueueWakeUpper::Wakeup is called. Returns nullopt if there was an
241 // error in registration.
242 // TODO(austin): Change the API if we find ourselves with more errors.
243 static std::optional<LocklessQueueWatcher> Make(LocklessQueue queue,
244 int priority);
245
246 private:
247 LocklessQueueWatcher(LocklessQueueMemory *memory, int priority);
248
249 LocklessQueueMemory *memory_ = nullptr;
250
251 // Index in the watcher list that our entry is, or -1 if no watcher is
252 // registered.
253 int watcher_index_ = -1;
254};
255
256class LocklessQueueWakeUpper {
257 public:
258 LocklessQueueWakeUpper(LocklessQueue queue);
Austin Schuh20b2b082019-09-11 20:42:56 -0700259
260 // Sends the kWakeupSignal to all threads which have called RegisterWakeup.
261 //
262 // priority of 0 means nonrt. nonrt could have issues, so we don't PI boost
263 // if nonrt.
264 int Wakeup(int current_priority);
265
Austin Schuh20b2b082019-09-11 20:42:56 -0700266 private:
Austin Schuh20b2b082019-09-11 20:42:56 -0700267 // Memory and datastructure used to sort a list of watchers to wake
268 // up. This isn't a copy of Watcher since tid is simpler to work with here
269 // than the futex above.
270 struct WatcherCopy {
271 pid_t tid;
272 pid_t pid;
273 int priority;
274 };
Austin Schuh20b2b082019-09-11 20:42:56 -0700275
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700276 const LocklessQueueMemory *const memory_;
Austin Schuh20b2b082019-09-11 20:42:56 -0700277 const int pid_;
278 const uid_t uid_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700279
280 ::std::vector<WatcherCopy> watcher_copy_;
Austin Schuh20b2b082019-09-11 20:42:56 -0700281};
282
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700283// Sender for blocks of data. The resources associated with a sender are
284// scoped to this object's lifetime.
285class LocklessQueueSender {
286 public:
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700287 // Enum of possible sending errors
288 // Send returns GOOD if the messages was sent successfully, INVALID_REDZONE if
289 // one of a message's redzones has invalid data, or MESSAGES_SENT_TOO_FAST if
290 // more than queue_size messages were going to be sent in a
291 // channel_storage_duration_.
292 enum class Result { GOOD, INVALID_REDZONE, MESSAGES_SENT_TOO_FAST };
293
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700294 LocklessQueueSender(const LocklessQueueSender &) = delete;
295 LocklessQueueSender &operator=(const LocklessQueueSender &) = delete;
296 LocklessQueueSender(LocklessQueueSender &&other)
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700297 : memory_(other.memory_),
298 sender_index_(other.sender_index_),
299 channel_storage_duration_(other.channel_storage_duration_) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700300 other.memory_ = nullptr;
301 other.sender_index_ = -1;
302 }
303 LocklessQueueSender &operator=(LocklessQueueSender &&other) {
304 std::swap(memory_, other.memory_);
305 std::swap(sender_index_, other.sender_index_);
306 return *this;
307 }
308
309 ~LocklessQueueSender();
310
311 // Creates a sender. If we couldn't allocate a sender, returns nullopt.
312 // TODO(austin): Change the API if we find ourselves with more errors.
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700313 static std::optional<LocklessQueueSender> Make(
314 LocklessQueue queue, monotonic_clock::duration channel_storage_duration);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700315
316 // Sends a message without copying the data.
317 // Copy at most size() bytes of data into the memory pointed to by Data(),
318 // and then call Send().
319 // Note: calls to Data() are expensive enough that you should cache it.
320 size_t size() const;
321 void *Data();
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700322 LocklessQueueSender::Result Send(
323 size_t length, monotonic_clock::time_point monotonic_remote_time,
324 realtime_clock::time_point realtime_remote_time,
325 uint32_t remote_queue_index, const UUID &source_boot_uuid,
326 monotonic_clock::time_point *monotonic_sent_time = nullptr,
327 realtime_clock::time_point *realtime_sent_time = nullptr,
328 uint32_t *queue_index = nullptr);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700329
330 // Sends up to length data. Does not wakeup the target.
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700331 LocklessQueueSender::Result Send(
332 const char *data, size_t length,
333 monotonic_clock::time_point monotonic_remote_time,
334 realtime_clock::time_point realtime_remote_time,
335 uint32_t remote_queue_index, const UUID &source_boot_uuid,
336 monotonic_clock::time_point *monotonic_sent_time = nullptr,
337 realtime_clock::time_point *realtime_sent_time = nullptr,
338 uint32_t *queue_index = nullptr);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700339
340 int buffer_index() const;
341
342 private:
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700343 LocklessQueueSender(LocklessQueueMemory *memory,
344 monotonic_clock::duration channel_storage_duration);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700345
346 // Pointer to the backing memory.
347 LocklessQueueMemory *memory_ = nullptr;
348
349 // Index into the sender list.
350 int sender_index_ = -1;
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700351
352 // Storage duration of the channel used to check if messages were sent too
353 // fast
354 const monotonic_clock::duration channel_storage_duration_;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700355};
356
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700357std::ostream &operator<<(std::ostream &os, const LocklessQueueSender::Result r);
358
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700359// Pinner for blocks of data. The resources associated with a pinner are
360// scoped to this object's lifetime.
361class LocklessQueuePinner {
362 public:
363 LocklessQueuePinner(const LocklessQueuePinner &) = delete;
364 LocklessQueuePinner &operator=(const LocklessQueuePinner &) = delete;
365 LocklessQueuePinner(LocklessQueuePinner &&other)
366 : memory_(other.memory_),
367 const_memory_(other.const_memory_),
368 pinner_index_(other.pinner_index_) {
369 other.pinner_index_ = -1;
370 }
371 LocklessQueuePinner &operator=(LocklessQueuePinner &&other) {
372 std::swap(memory_, other.memory_);
373 std::swap(const_memory_, other.const_memory_);
374 std::swap(pinner_index_, other.pinner_index_);
375 return *this;
376 }
377
378 ~LocklessQueuePinner();
379
380 // Creates a pinner. If we couldn't allocate a pinner, returns nullopt.
381 // TODO(austin): Change the API if we find ourselves with more errors.
382 static std::optional<LocklessQueuePinner> Make(LocklessQueue queue);
383
384 // Attempts to pin the message at queue_index.
385 // Un-pins the previous message.
386 // Returns the buffer index (non-negative) if it succeeds.
387 // Returns -1 if that message is no longer in the queue.
388 int PinIndex(uint32_t queue_index);
389
390 // Read at most size() bytes of data into the memory pointed to by Data().
391 // Note: calls to Data() are expensive enough that you should cache it.
392 // Don't call Data() before a successful PinIndex call.
393 size_t size() const;
394 const void *Data() const;
395
396 private:
397 LocklessQueuePinner(LocklessQueueMemory *memory,
398 const LocklessQueueMemory *const_memory);
399
400 // Pointer to the backing memory.
401 LocklessQueueMemory *memory_ = nullptr;
402 const LocklessQueueMemory *const_memory_ = nullptr;
403
404 // Index into the pinner list.
405 int pinner_index_ = -1;
406};
407
408class LocklessQueueReader {
409 public:
Austin Schuh82ea7382023-07-14 15:17:34 -0700410 enum class Result {
411 // Message we read was too old and no longer is in the queue.
412 TOO_OLD,
413 // Success!
414 GOOD,
415 // The message is in the future and we haven't written it yet.
416 NOTHING_NEW,
417 // There is a message, but should_read() returned false so we didn't fetch
418 // it.
419 FILTERED,
420 // The message got overwritten while we were reading it.
421 OVERWROTE,
422 };
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700423
424 LocklessQueueReader(LocklessQueue queue) : memory_(queue.const_memory()) {
425 queue.Initialize();
426 }
427
428 // If you ask for a queue index 2 past the newest, you will still get
429 // NOTHING_NEW until that gets overwritten with new data. If you ask for an
430 // element newer than QueueSize() from the current message, we consider it
431 // behind by a large amount and return TOO_OLD. If the message is modified
Austin Schuh82ea7382023-07-14 15:17:34 -0700432 // out from underneath us as we read it, return OVERWROTE. If we found a new
433 // message, but the filter function returned false, return FILTERED.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700434 //
435 // data may be nullptr to indicate the data should not be copied.
436 Result Read(uint32_t queue_index,
Austin Schuhb5c6f972021-03-14 21:53:07 -0700437 monotonic_clock::time_point *monotonic_sent_time,
438 realtime_clock::time_point *realtime_sent_time,
439 monotonic_clock::time_point *monotonic_remote_time,
440 realtime_clock::time_point *realtime_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -0700441 uint32_t *remote_queue_index, UUID *source_boot_uuid,
Austin Schuh82ea7382023-07-14 15:17:34 -0700442 size_t *length, char *data,
443 std::function<bool(const Context &context)> should_read) const;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700444
445 // Returns the index to the latest queue message. Returns empty_queue_index()
446 // if there are no messages in the queue. Do note that this index wraps if
447 // more than 2^32 messages are sent.
448 QueueIndex LatestIndex() const;
449
450 private:
451 const LocklessQueueMemory *const memory_;
452};
453
454// Returns the number of messages which are logically in the queue at a time.
455size_t LocklessQueueSize(const LocklessQueueMemory *memory);
456
457// Returns the number of bytes queue users are allowed to read/write within each
458// message.
459size_t LocklessQueueMessageDataSize(const LocklessQueueMemory *memory);
460
461// TODO(austin): Return the oldest queue index. This lets us catch up nicely
462// if we got behind.
463// The easiest way to implement this is likely going to be to reserve the
464// first modulo of values for the initial time around, and never reuse them.
465// That lets us do a simple atomic read of the next index and deduce what has
466// happened. It will involve the simplest atomic operations.
467
468// TODO(austin): Make it so we can find the indices which were sent just
469// before and after a time with a binary search.
470
471// Prints to stdout the data inside the queue for debugging.
Austin Schuh83cbb1e2023-06-23 12:59:02 -0700472void PrintLocklessQueueMemory(const LocklessQueueMemory *memory);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700473
Austin Schuh20b2b082019-09-11 20:42:56 -0700474} // namespace ipc_lib
475} // namespace aos
476
477#endif // AOS_IPC_LIB_LOCKLESS_QUEUE_H_