blob: 9d14d8f6b0224640426115844d932e576a89d37f [file] [log] [blame]
Austin Schuh20b2b082019-09-11 20:42:56 -07001#include "aos/ipc_lib/lockless_queue.h"
2
3#include <linux/futex.h>
Brennan Coslett6af53bb2023-07-18 15:22:46 -05004#include <pwd.h>
Austin Schuh20b2b082019-09-11 20:42:56 -07005#include <sys/types.h>
6#include <syscall.h>
7#include <unistd.h>
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07008
Austin Schuh20b2b082019-09-11 20:42:56 -07009#include <algorithm>
10#include <iomanip>
11#include <iostream>
12#include <sstream>
13
Austin Schuhbe416742020-10-03 17:24:26 -070014#include "absl/strings/escaping.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070015#include "gflags/gflags.h"
16#include "glog/logging.h"
17
Austin Schuh20b2b082019-09-11 20:42:56 -070018#include "aos/ipc_lib/lockless_queue_memory.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "aos/realtime.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070020#include "aos/util/compiler_memory_barrier.h"
21
Brian Silverman001f24d2020-08-12 19:33:20 -070022DEFINE_bool(dump_lockless_queue_data, false,
23 "If true, print the data out when dumping the queue.");
Austin Schuh151f7822024-03-16 12:52:32 -070024DECLARE_bool(skip_realtime_scheduler);
Brian Silverman001f24d2020-08-12 19:33:20 -070025
Stephan Pleinesf63bde82024-01-13 15:59:33 -080026namespace aos::ipc_lib {
Austin Schuh20b2b082019-09-11 20:42:56 -070027namespace {
28
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080029class GrabQueueSetupLockOrDie {
30 public:
31 GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) : memory_(memory) {
32 const int result = mutex_grab(&(memory->queue_setup_lock));
33 CHECK(result == 0 || result == 1) << ": " << result;
34 }
Austin Schuh20b2b082019-09-11 20:42:56 -070035
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080036 ~GrabQueueSetupLockOrDie() { mutex_unlock(&(memory_->queue_setup_lock)); }
37
38 GrabQueueSetupLockOrDie(const GrabQueueSetupLockOrDie &) = delete;
39 GrabQueueSetupLockOrDie &operator=(const GrabQueueSetupLockOrDie &) = delete;
40
41 private:
42 LocklessQueueMemory *const memory_;
43};
44
Brian Silverman177567e2020-08-12 19:51:33 -070045bool IsPinned(LocklessQueueMemory *memory, Index index) {
46 DCHECK(index.valid());
47 const size_t queue_size = memory->queue_size();
48 const QueueIndex message_index =
49 memory->GetMessage(index)->header.queue_index.Load(queue_size);
50 if (!message_index.valid()) {
51 return false;
52 }
53 DCHECK(memory->GetQueue(message_index.Wrapped())->Load() != index)
54 << ": Message is in the queue";
55 for (int pinner_index = 0;
56 pinner_index < static_cast<int>(memory->config.num_pinners);
57 ++pinner_index) {
58 ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index);
59
60 if (pinner->pinned.RelaxedLoad(queue_size) == message_index) {
61 return true;
62 }
63 }
64 return false;
65}
66
67// Ensures sender->scratch_index (which must contain to_replace) is not pinned.
68//
69// Returns the new scratch_index value.
70Index SwapPinnedSenderScratch(LocklessQueueMemory *const memory,
71 ipc_lib::Sender *const sender,
72 const Index to_replace) {
73 // If anybody's trying to pin this message, then grab a message from a pinner
74 // to write into instead, and leave the message we pulled out of the queue
75 // (currently in our scratch_index) with a pinner.
76 //
77 // This loop will terminate in at most one iteration through the pinners in
78 // any steady-state configuration of the memory. There are only as many
79 // Pinner::pinned values to worry about as there are Pinner::scratch_index
80 // values to check against, plus to_replace, which means there will always be
81 // a free one. We might have to make multiple passes if things are being
82 // changed concurrently though, but nobody dying can make this loop fail to
83 // terminate (because the number of processes that can die is bounded, because
84 // no new ones can start while we've got the lock).
85 for (int pinner_index = 0; true;
86 pinner_index = (pinner_index + 1) % memory->config.num_pinners) {
87 if (!IsPinned(memory, to_replace)) {
88 // No pinners on our current scratch_index, so we're fine now.
89 VLOG(3) << "No pinners: " << to_replace.DebugString();
90 return to_replace;
91 }
92
93 ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index);
94
95 const Index pinner_scratch = pinner->scratch_index.RelaxedLoad();
96 CHECK(pinner_scratch.valid())
97 << ": Pinner scratch_index should always be valid";
98 if (IsPinned(memory, pinner_scratch)) {
99 // Wouldn't do us any good to swap with this one, so don't bother, and
100 // move onto the next one.
101 VLOG(3) << "Also pinned: " << pinner_scratch.DebugString();
102 continue;
103 }
104
105 sender->to_replace.RelaxedStore(pinner_scratch);
106 aos_compiler_memory_barrier();
107 // Give the pinner the message (which is currently in
108 // sender->scratch_index).
109 if (!pinner->scratch_index.CompareAndExchangeStrong(pinner_scratch,
110 to_replace)) {
111 // Somebody swapped into this pinner before us. The new value is probably
112 // pinned, so we don't want to look at it again immediately.
113 VLOG(3) << "Pinner " << pinner_index
114 << " scratch_index changed: " << pinner_scratch.DebugString()
115 << ", " << to_replace.DebugString();
116 sender->to_replace.RelaxedInvalidate();
117 continue;
118 }
119 aos_compiler_memory_barrier();
120 // Now update the sender's scratch space and record that we succeeded.
121 sender->scratch_index.Store(pinner_scratch);
122 aos_compiler_memory_barrier();
123 // And then record that we succeeded, but definitely after the above
124 // store.
125 sender->to_replace.RelaxedInvalidate();
126 VLOG(3) << "Got new scratch message: " << pinner_scratch.DebugString();
127
128 // If it's in a pinner's scratch_index, it should not be in the queue, which
129 // means nobody new can pin it for real. However, they can still attempt to
130 // pin it, which means we can't verify !IsPinned down here.
131
132 return pinner_scratch;
133 }
134}
135
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700136// Returns true if it succeeded. Returns false if another sender died in the
137// middle.
138bool DoCleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800139 // Make sure we start looking at shared memory fresh right now. We'll handle
140 // people dying partway through by either cleaning up after them or not, but
141 // we want to ensure we clean up after anybody who has already died when we
142 // start.
143 aos_compiler_memory_barrier();
144
Austin Schuh20b2b082019-09-11 20:42:56 -0700145 const size_t num_senders = memory->num_senders();
Brian Silverman177567e2020-08-12 19:51:33 -0700146 const size_t num_pinners = memory->num_pinners();
Austin Schuh20b2b082019-09-11 20:42:56 -0700147 const size_t queue_size = memory->queue_size();
148 const size_t num_messages = memory->num_messages();
149
150 // There are a large number of crazy cases here for how things can go wrong
151 // and how we have to recover. They either require us to keep extra track of
152 // what is going on, slowing down the send path, or require a large number of
153 // cases.
154 //
155 // The solution here is to not over-think it. This is running while not real
156 // time during construction. It is allowed to be slow. It will also very
157 // rarely trigger. There is a small uS window where process death is
158 // ambiguous.
159 //
160 // So, build up a list N long, where N is the number of messages. Search
161 // through the entire queue and the sender list (ignoring any dead senders),
162 // and mark down which ones we have seen. Once we have seen all the messages
163 // except the N dead senders, we know which messages are dead. Because the
164 // queue is active while we do this, it may take a couple of go arounds to see
165 // everything.
166
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700167 ::std::vector<bool> need_recovery(num_senders, false);
168
Austin Schuh20b2b082019-09-11 20:42:56 -0700169 // Do the easy case. Find all senders who have died. See if they are either
170 // consistent already, or if they have copied over to_replace to the scratch
171 // index, but haven't cleared to_replace. Count them.
172 size_t valid_senders = 0;
173 for (size_t i = 0; i < num_senders; ++i) {
174 Sender *sender = memory->GetSender(i);
Philipp Schrader81fa3fb2023-09-17 18:58:35 -0700175 if (!sender->ownership_tracker.OwnerIsDefinitelyAbsolutelyDead()) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700176 // Not dead.
177 ++valid_senders;
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700178 continue;
Austin Schuh20b2b082019-09-11 20:42:56 -0700179 }
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700180 VLOG(3) << "Found an easy death for sender " << i;
181 // We can do a relaxed load here because we're the only person touching
182 // this sender at this point.
183 const Index to_replace = sender->to_replace.RelaxedLoad();
184 const Index scratch_index = sender->scratch_index.Load();
185
186 // I find it easiest to think about this in terms of the set of observable
187 // states. The main code progresses through the following states:
188
189 // 1) scratch_index = xxx
190 // to_replace = invalid
191 // This is unambiguous. Already good.
192
193 // 2) scratch_index = xxx
194 // to_replace = yyy
195 // Very ambiguous. Is xxx or yyy the correct one? Need to either roll
196 // this forwards or backwards.
197
198 // 3) scratch_index = yyy
199 // to_replace = yyy
200 // We are in the act of moving to_replace to scratch_index, but didn't
201 // finish. Easy.
Brian Silverman177567e2020-08-12 19:51:33 -0700202 //
203 // If doing a pinner swap, we've definitely done it.
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700204
205 // 4) scratch_index = yyy
206 // to_replace = invalid
207 // Finished, but died. Looks like 1)
208
Brian Silverman177567e2020-08-12 19:51:33 -0700209 // Swapping with a pinner's scratch_index passes through the same states.
210 // We just need to ensure the message that ends up in the senders's
211 // scratch_index isn't pinned, using the same code as sending does.
212
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700213 // Any cleanup code needs to follow the same set of states to be robust to
214 // death, so death can be restarted.
215
216 if (!to_replace.valid()) {
217 // 1) or 4). Make sure we aren't corrupted and declare victory.
218 CHECK(scratch_index.valid());
219
Brian Silverman177567e2020-08-12 19:51:33 -0700220 // If it's in 1) with a pinner, the sender might have a pinned message,
221 // so fix that.
222 SwapPinnedSenderScratch(memory, sender, scratch_index);
223
224 // If it's in 4), it may not have completed this step yet. This will
225 // always be a NOP if it's in 1), verified by a DCHECK.
226 memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate();
227
Philipp Schraderab2f8432023-09-17 18:58:06 -0700228 sender->ownership_tracker.ForceClear();
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700229 ++valid_senders;
230 continue;
231 }
232
233 // Could be 2) or 3) at this point.
234
235 if (to_replace == scratch_index) {
236 // 3) for sure.
237 // Just need to invalidate to_replace to finish.
238 sender->to_replace.Invalidate();
239
Brian Silverman177567e2020-08-12 19:51:33 -0700240 // Make sure to indicate it's an unused message before a sender gets its
241 // hands on it.
242 memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate();
243 aos_compiler_memory_barrier();
244
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700245 // And mark that we succeeded.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700246 sender->ownership_tracker.ForceClear();
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700247 ++valid_senders;
248 continue;
249 }
250
251 // Must be 2). Mark it for later.
252 need_recovery[i] = true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700253 }
254
Brian Silverman177567e2020-08-12 19:51:33 -0700255 // Cleaning up pinners is easy. We don't actually have to do anything, but
256 // invalidating its pinned field might help catch bugs elsewhere trying to
257 // read it before it's set.
258 for (size_t i = 0; i < num_pinners; ++i) {
259 Pinner *const pinner = memory->GetPinner(i);
Philipp Schrader81fa3fb2023-09-17 18:58:35 -0700260 if (!pinner->ownership_tracker.OwnerIsDefinitelyAbsolutelyDead()) {
Brian Silverman177567e2020-08-12 19:51:33 -0700261 continue;
262 }
263 pinner->pinned.Invalidate();
Philipp Schraderab2f8432023-09-17 18:58:06 -0700264 pinner->ownership_tracker.ForceClear();
Brian Silverman177567e2020-08-12 19:51:33 -0700265 }
266
Austin Schuh20b2b082019-09-11 20:42:56 -0700267 // If all the senders are (or were made) good, there is no need to do the hard
268 // case.
269 if (valid_senders == num_senders) {
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700270 return true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700271 }
272
Alex Perrycb7da4b2019-08-28 19:35:56 -0700273 VLOG(3) << "Starting hard cleanup";
Austin Schuh20b2b082019-09-11 20:42:56 -0700274
275 size_t num_accounted_for = 0;
276 size_t num_missing = 0;
277 ::std::vector<bool> accounted_for(num_messages, false);
278
279 while ((num_accounted_for + num_missing) != num_messages) {
280 num_missing = 0;
281 for (size_t i = 0; i < num_senders; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800282 Sender *const sender = memory->GetSender(i);
Philipp Schrader81fa3fb2023-09-17 18:58:35 -0700283 if (sender->ownership_tracker.OwnerIsDefinitelyAbsolutelyDead()) {
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700284 if (!need_recovery[i]) {
285 return false;
286 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700287 ++num_missing;
Brian Silverman177567e2020-08-12 19:51:33 -0700288 continue;
Austin Schuh20b2b082019-09-11 20:42:56 -0700289 }
Brian Silverman177567e2020-08-12 19:51:33 -0700290 CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i;
291 // We can do a relaxed load here because we're the only person touching
292 // this sender at this point, if it matters. If it's not a dead sender,
293 // then any message it ever has will eventually be accounted for if we
294 // make enough tries through the outer loop.
295 const Index scratch_index = sender->scratch_index.RelaxedLoad();
296 if (!accounted_for[scratch_index.message_index()]) {
297 ++num_accounted_for;
298 }
299 accounted_for[scratch_index.message_index()] = true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700300 }
301
302 for (size_t i = 0; i < queue_size; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800303 // Same logic as above for scratch_index applies here too.
Austin Schuh20b2b082019-09-11 20:42:56 -0700304 const Index index = memory->GetQueue(i)->RelaxedLoad();
305 if (!accounted_for[index.message_index()]) {
306 ++num_accounted_for;
307 }
308 accounted_for[index.message_index()] = true;
309 }
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700310
Brian Silverman177567e2020-08-12 19:51:33 -0700311 for (size_t pinner_index = 0; pinner_index < num_pinners; ++pinner_index) {
312 // Same logic as above for scratch_index applies here too.
313 const Index index =
314 memory->GetPinner(pinner_index)->scratch_index.RelaxedLoad();
315 if (!accounted_for[index.message_index()]) {
316 ++num_accounted_for;
317 }
318 accounted_for[index.message_index()] = true;
319 }
320
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700321 CHECK_LE(num_accounted_for + num_missing, num_messages);
Austin Schuh20b2b082019-09-11 20:42:56 -0700322 }
323
324 while (num_missing != 0) {
325 const size_t starting_num_missing = num_missing;
326 for (size_t i = 0; i < num_senders; ++i) {
327 Sender *sender = memory->GetSender(i);
Philipp Schrader81fa3fb2023-09-17 18:58:35 -0700328 if (!sender->ownership_tracker.OwnerIsDefinitelyAbsolutelyDead()) {
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700329 CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i;
330 continue;
331 }
332 if (!need_recovery[i]) {
333 return false;
334 }
335 // We can do relaxed loads here because we're the only person touching
336 // this sender at this point.
337 const Index scratch_index = sender->scratch_index.RelaxedLoad();
338 const Index to_replace = sender->to_replace.RelaxedLoad();
Austin Schuh20b2b082019-09-11 20:42:56 -0700339
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700340 // Candidate.
341 if (to_replace.valid()) {
342 CHECK_LE(to_replace.message_index(), accounted_for.size());
343 }
344 if (scratch_index.valid()) {
345 CHECK_LE(scratch_index.message_index(), accounted_for.size());
346 }
347 if (!to_replace.valid() || accounted_for[to_replace.message_index()]) {
348 CHECK(scratch_index.valid());
349 VLOG(3) << "Sender " << i
350 << " died, to_replace is already accounted for";
351 // If both are accounted for, we are corrupt...
352 CHECK(!accounted_for[scratch_index.message_index()]);
Austin Schuh20b2b082019-09-11 20:42:56 -0700353
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700354 // to_replace is already accounted for. This means that we didn't
355 // atomically insert scratch_index into the queue yet. So
356 // invalidate to_replace.
357 sender->to_replace.Invalidate();
Brian Silverman177567e2020-08-12 19:51:33 -0700358 // Sender definitely will not have gotten here, so finish for it.
359 memory->GetMessage(scratch_index)
360 ->header.queue_index.RelaxedInvalidate();
Austin Schuh20b2b082019-09-11 20:42:56 -0700361
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700362 // And then mark this sender clean.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700363 sender->ownership_tracker.ForceClear();
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700364 need_recovery[i] = false;
Austin Schuh20b2b082019-09-11 20:42:56 -0700365
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700366 // And account for scratch_index.
367 accounted_for[scratch_index.message_index()] = true;
368 --num_missing;
369 ++num_accounted_for;
370 } else if (!scratch_index.valid() ||
371 accounted_for[scratch_index.message_index()]) {
372 VLOG(3) << "Sender " << i
373 << " died, scratch_index is already accounted for";
374 // scratch_index is accounted for. That means we did the insert,
375 // but didn't record it.
376 CHECK(to_replace.valid());
Brian Silverman177567e2020-08-12 19:51:33 -0700377
378 // Make sure to indicate it's an unused message before a sender gets its
379 // hands on it.
380 memory->GetMessage(to_replace)->header.queue_index.RelaxedInvalidate();
381 aos_compiler_memory_barrier();
382
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700383 // Finish the transaction. Copy to_replace, then clear it.
Austin Schuh20b2b082019-09-11 20:42:56 -0700384
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700385 sender->scratch_index.Store(to_replace);
386 sender->to_replace.Invalidate();
Austin Schuh20b2b082019-09-11 20:42:56 -0700387
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700388 // And then mark this sender clean.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700389 sender->ownership_tracker.ForceClear();
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700390 need_recovery[i] = false;
Austin Schuh20b2b082019-09-11 20:42:56 -0700391
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700392 // And account for to_replace.
393 accounted_for[to_replace.message_index()] = true;
394 --num_missing;
395 ++num_accounted_for;
396 } else {
397 VLOG(3) << "Sender " << i << " died, neither is accounted for";
398 // Ambiguous. There will be an unambiguous one somewhere that we
399 // can do first.
Austin Schuh20b2b082019-09-11 20:42:56 -0700400 }
401 }
402 // CHECK that we are making progress.
403 CHECK_NE(num_missing, starting_num_missing);
404 }
Brian Silvermand5ca8c62020-08-12 19:51:03 -0700405 return true;
406}
407
408void Cleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &lock) {
409 // The number of iterations is bounded here because there are only a finite
410 // number of senders in existence which could die, and no new ones can be
411 // created while we're in here holding the lock.
412 while (!DoCleanup(memory, lock)) {
413 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700414}
415
416// Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target
417// thread.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800418// TODO(Brian): Do directly in assembly for armhf at least for efficiency.
Austin Schuh20b2b082019-09-11 20:42:56 -0700419int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) {
420 return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si);
421}
422
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700423QueueIndex ZeroOrValid(QueueIndex index) {
424 if (!index.valid()) {
425 return index.Clear();
426 }
427 return index;
428}
429
Austin Schuh20b2b082019-09-11 20:42:56 -0700430} // namespace
431
Philipp Schraderab2f8432023-09-17 18:58:06 -0700432bool PretendThatOwnerIsDeadForTesting(aos_mutex *mutex, pid_t tid) {
433 if (static_cast<pid_t>(mutex->futex & FUTEX_TID_MASK) == tid) {
434 mutex->futex = FUTEX_OWNER_DIED;
435 return true;
436 }
437 return false;
438}
439
Austin Schuh4bc4f902019-12-23 18:04:51 -0800440size_t LocklessQueueConfiguration::message_size() const {
441 // Round up the message size so following data is aligned appropriately.
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700442 // Make sure to leave space to align the message data. It will be aligned
443 // relative to the start of the shared memory region, but that might not be
444 // aligned for some use cases.
Brian Silvermana1652f32020-01-29 20:41:44 -0800445 return LocklessQueueMemory::AlignmentRoundUp(message_data_size +
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700446 kChannelDataRedzone * 2 +
Brian Silvermana1652f32020-01-29 20:41:44 -0800447 (kChannelDataAlignment - 1)) +
Austin Schuh4bc4f902019-12-23 18:04:51 -0800448 sizeof(Message);
449}
450
Austin Schuh20b2b082019-09-11 20:42:56 -0700451size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800452 // Round up the message size so following data is aligned appropriately.
453 config.message_data_size =
454 LocklessQueueMemory::AlignmentRoundUp(config.message_data_size);
Austin Schuh20b2b082019-09-11 20:42:56 -0700455
456 // As we build up the size, confirm that everything is aligned to the
457 // alignment requirements of the type.
458 size_t size = sizeof(LocklessQueueMemory);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800459 CHECK_EQ(size % alignof(LocklessQueueMemory), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700460
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800461 CHECK_EQ(size % alignof(AtomicIndex), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700462 size += LocklessQueueMemory::SizeOfQueue(config);
463
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800464 CHECK_EQ(size % alignof(Message), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700465 size += LocklessQueueMemory::SizeOfMessages(config);
466
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800467 CHECK_EQ(size % alignof(Watcher), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700468 size += LocklessQueueMemory::SizeOfWatchers(config);
469
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800470 CHECK_EQ(size % alignof(Sender), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700471 size += LocklessQueueMemory::SizeOfSenders(config);
472
Brian Silverman177567e2020-08-12 19:51:33 -0700473 CHECK_EQ(size % alignof(Pinner), 0u);
474 size += LocklessQueueMemory::SizeOfPinners(config);
475
Austin Schuh20b2b082019-09-11 20:42:56 -0700476 return size;
477}
478
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700479// Calculates the starting byte for a redzone in shared memory. This starting
480// value is simply incremented for subsequent bytes.
481//
482// The result is based on the offset of the region in shared memor, to ensure it
483// is the same for each region when we generate and verify, but different for
484// each region to help catch forms of corruption like copying out-of-bounds data
485// from one place to another.
486//
487// memory is the base pointer to the shared memory. It is used to calculated
488// offsets. starting_data is the start of the redzone's data. Each one will
489// get a unique pattern.
490uint8_t RedzoneStart(const LocklessQueueMemory *memory,
491 const char *starting_data) {
492 const auto memory_int = reinterpret_cast<uintptr_t>(memory);
493 const auto starting_int = reinterpret_cast<uintptr_t>(starting_data);
494 DCHECK(starting_int >= memory_int);
495 DCHECK(starting_int < memory_int + LocklessQueueMemorySize(memory->config));
496 const uintptr_t starting_offset = starting_int - memory_int;
497 // Just XOR the lower 2 bytes. They higher-order bytes are probably 0
498 // anyways.
499 return (starting_offset & 0xFF) ^ ((starting_offset >> 8) & 0xFF);
500}
501
502// Returns true if the given redzone has invalid data.
503bool CheckRedzone(const LocklessQueueMemory *memory,
504 absl::Span<const char> redzone) {
505 uint8_t redzone_value = RedzoneStart(memory, redzone.data());
506
507 bool bad = false;
508
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700509 for (size_t i = 0; i < redzone.size() && !bad; ++i) {
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700510 if (memcmp(&redzone[i], &redzone_value, 1)) {
511 bad = true;
512 }
513 ++redzone_value;
514 }
515
516 return bad;
517}
518
519// Returns true if either of message's redzones has invalid data.
520bool CheckBothRedzones(const LocklessQueueMemory *memory,
521 const Message *message) {
522 return CheckRedzone(memory,
523 message->PreRedzone(memory->message_data_size())) ||
524 CheckRedzone(memory, message->PostRedzone(memory->message_data_size(),
525 memory->message_size()));
526}
527
528// Fills the given redzone with the expected data.
529void FillRedzone(LocklessQueueMemory *memory, absl::Span<char> redzone) {
530 uint8_t redzone_value = RedzoneStart(memory, redzone.data());
531 for (size_t i = 0; i < redzone.size(); ++i) {
532 memcpy(&redzone[i], &redzone_value, 1);
533 ++redzone_value;
534 }
535
536 // Just double check that the implementations match.
537 CHECK(!CheckRedzone(memory, redzone));
538}
539
Austin Schuh20b2b082019-09-11 20:42:56 -0700540LocklessQueueMemory *InitializeLocklessQueueMemory(
541 LocklessQueueMemory *memory, LocklessQueueConfiguration config) {
542 // Everything should be zero initialized already. So we just need to fill
543 // everything out properly.
544
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700545 // This is the UID we will use for checking signal-sending permission
546 // compatibility.
547 //
548 // The manpage says:
549 // For a process to have permission to send a signal, it must either be
550 // privileged [...], or the real or effective user ID of the sending process
551 // must equal the real or saved set-user-ID of the target process.
552 //
553 // Processes typically initialize a queue in random order as they start up.
554 // This means we need an algorithm for verifying all processes have
555 // permissions to send each other signals which gives the same answer no
556 // matter what order they attach in. We would also like to avoid maintaining a
557 // shared list of the UIDs of all processes.
558 //
559 // To do this while still giving sufficient flexibility for all current use
560 // cases, we track a single UID for the queue. All processes with a matching
561 // euid+suid must have this UID. Any processes with distinct euid/suid must
562 // instead have a matching ruid. This guarantees signals can be sent between
563 // all processes attached to the queue.
564 //
565 // In particular, this allows a process to change only its euid (to interact
566 // with a queue) while still maintaining privileges via its ruid. However, it
567 // can only use privileges in ways that do not require changing the euid back,
568 // because while the euid is different it will not be able to receive signals.
569 // We can't actually verify that, but we can sanity check that things are
570 // valid when the queue is initialized.
571
572 uid_t uid;
573 {
574 uid_t ruid, euid, suid;
575 PCHECK(getresuid(&ruid, &euid, &suid) == 0);
576 // If these are equal, then use them, even if that's different from the real
577 // UID. This allows processes to keep a real UID of 0 (to have permissions
578 // to perform system-level changes) while still being able to communicate
579 // with processes running unprivileged as a distinct user.
580 if (euid == suid) {
581 uid = euid;
582 VLOG(1) << "Using euid==suid " << uid;
583 } else {
584 uid = ruid;
585 VLOG(1) << "Using ruid " << ruid;
586 }
587 }
588
Austin Schuh20b2b082019-09-11 20:42:56 -0700589 // Grab the mutex. We don't care if the previous reader died. We are going
590 // to check everything anyways.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800591 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory);
Austin Schuh20b2b082019-09-11 20:42:56 -0700592
593 if (!memory->initialized) {
594 // TODO(austin): Check these for out of bounds.
595 memory->config.num_watchers = config.num_watchers;
596 memory->config.num_senders = config.num_senders;
Brian Silverman177567e2020-08-12 19:51:33 -0700597 memory->config.num_pinners = config.num_pinners;
Austin Schuh20b2b082019-09-11 20:42:56 -0700598 memory->config.queue_size = config.queue_size;
Austin Schuh4bc4f902019-12-23 18:04:51 -0800599 memory->config.message_data_size = config.message_data_size;
Austin Schuh20b2b082019-09-11 20:42:56 -0700600
601 const size_t num_messages = memory->num_messages();
602 // There need to be at most MaxMessages() messages allocated.
603 CHECK_LE(num_messages, Index::MaxMessages());
604
605 for (size_t i = 0; i < num_messages; ++i) {
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700606 Message *const message =
607 memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i));
608 message->header.queue_index.Invalidate();
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700609 message->header.monotonic_sent_time = monotonic_clock::min_time;
Brian Silverman0eaa1da2020-08-12 20:03:52 -0700610 FillRedzone(memory, message->PreRedzone(memory->message_data_size()));
611 FillRedzone(memory, message->PostRedzone(memory->message_data_size(),
612 memory->message_size()));
Austin Schuh20b2b082019-09-11 20:42:56 -0700613 }
614
615 for (size_t i = 0; i < memory->queue_size(); ++i) {
616 // Make the initial counter be the furthest away number. That means that
617 // index 0 should be 0xffff, 1 should be 0, etc.
618 memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size())
619 .IncrementBy(i)
620 .DecrementBy(memory->queue_size()),
621 i));
622 }
623
624 memory->next_queue_index.Invalidate();
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700625 memory->uid = uid;
Austin Schuh20b2b082019-09-11 20:42:56 -0700626
627 for (size_t i = 0; i < memory->num_senders(); ++i) {
628 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800629 // Nobody else can possibly be touching these because we haven't set
630 // initialized to true yet.
Austin Schuh83cbb1e2023-06-23 12:59:02 -0700631 s->scratch_index.RelaxedStore(
632 Index(QueueIndex::Invalid(), i + memory->queue_size()));
Austin Schuh20b2b082019-09-11 20:42:56 -0700633 s->to_replace.RelaxedInvalidate();
634 }
635
Brian Silverman177567e2020-08-12 19:51:33 -0700636 for (size_t i = 0; i < memory->num_pinners(); ++i) {
637 ::aos::ipc_lib::Pinner *pinner = memory->GetPinner(i);
638 // Nobody else can possibly be touching these because we haven't set
639 // initialized to true yet.
640 pinner->scratch_index.RelaxedStore(
Austin Schuh83cbb1e2023-06-23 12:59:02 -0700641 Index(QueueIndex::Invalid(),
642 i + memory->num_senders() + memory->queue_size()));
Brian Silverman177567e2020-08-12 19:51:33 -0700643 pinner->pinned.Invalidate();
644 }
645
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800646 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700647 // Signal everything is done. This needs to be done last, so if we die, we
648 // redo initialization.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800649 memory->initialized = true;
Austin Schuh3328d132020-02-28 13:54:57 -0800650 } else {
Brennan Coslett6af53bb2023-07-18 15:22:46 -0500651 if (memory->uid != uid) {
652 // Subsequent calls to getpwuid() overwrite this
653 // pointer, pull the thing we care about into a
654 // string.
655 struct passwd const *user_pw = getpwuid(uid);
656 std::string user_username = user_pw->pw_name;
657 struct passwd const *memory_pw = getpwuid(memory->uid);
658 std::string memory_username = memory_pw->pw_name;
659 LOG(FATAL) << "Current user " << user_username << " (uid:" << uid << ") "
660 << "doesn't match shared memory user " << memory_username
661 << " (uid:" << memory->uid << "). "
Philipp Schrader5f832612023-08-21 10:29:57 -0700662 << "Log in as " << memory_username
Brennan Coslett6af53bb2023-07-18 15:22:46 -0500663 << " user to access this channel.";
664 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700665 }
666
Austin Schuh20b2b082019-09-11 20:42:56 -0700667 return memory;
668}
669
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700670void LocklessQueue::Initialize() {
671 InitializeLocklessQueueMemory(memory_, config_);
672}
Austin Schuh20b2b082019-09-11 20:42:56 -0700673
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700674LocklessQueueWatcher::~LocklessQueueWatcher() {
675 if (watcher_index_ == -1) {
676 return;
677 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700678
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700679 // Since everything is self consistent, all we need to do is make sure nobody
680 // else is running. Someone dying will get caught in the generic consistency
681 // check.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800682 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700683
684 // Make sure we are registered.
685 CHECK_NE(watcher_index_, -1);
686
687 // Make sure we still own the slot we are supposed to.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700688 CHECK(memory_->GetWatcher(watcher_index_)->ownership_tracker.IsHeldBySelf());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700689
690 // The act of unlocking invalidates the entry. Invalidate it.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700691 memory_->GetWatcher(watcher_index_)->ownership_tracker.Release();
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700692 // And internally forget the slot.
693 watcher_index_ = -1;
694
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800695 // Cleanup is cheap. The next user will do it anyways, so no need for us to do
696 // anything right now.
Austin Schuh20b2b082019-09-11 20:42:56 -0700697
698 // And confirm that nothing is owned by us.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700699 const int num_watchers = memory_->num_watchers();
Austin Schuh20b2b082019-09-11 20:42:56 -0700700 for (int i = 0; i < num_watchers; ++i) {
Philipp Schraderab2f8432023-09-17 18:58:06 -0700701 CHECK(!memory_->GetWatcher(i)->ownership_tracker.IsHeldBySelf())
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700702 << ": " << i;
Austin Schuh20b2b082019-09-11 20:42:56 -0700703 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700704}
705
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700706std::optional<LocklessQueueWatcher> LocklessQueueWatcher::Make(
707 LocklessQueue queue, int priority) {
708 queue.Initialize();
709 LocklessQueueWatcher result(queue.memory(), priority);
710 if (result.watcher_index_ != -1) {
James Kuszmaul9776b392023-01-14 14:08:08 -0800711 return result;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700712 } else {
713 return std::nullopt;
714 }
715}
Austin Schuh20b2b082019-09-11 20:42:56 -0700716
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700717LocklessQueueWatcher::LocklessQueueWatcher(LocklessQueueMemory *memory,
718 int priority)
719 : memory_(memory) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700720 // TODO(austin): Make sure signal coalescing is turned on. We don't need
721 // duplicates. That will improve performance under high load.
722
723 // Since everything is self consistent, all we need to do is make sure nobody
724 // else is running. Someone dying will get caught in the generic consistency
725 // check.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800726 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700727 const int num_watchers = memory_->num_watchers();
728
729 // Now, find the first empty watcher and grab it.
730 CHECK_EQ(watcher_index_, -1);
731 for (int i = 0; i < num_watchers; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800732 // If we see a slot the kernel has marked as dead, everything we do reusing
733 // it needs to happen-after whatever that process did before dying.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700734 auto *const ownership_tracker =
735 &(memory_->GetWatcher(i)->ownership_tracker);
Philipp Schrader81fa3fb2023-09-17 18:58:35 -0700736 if (ownership_tracker->LoadAcquire().IsUnclaimed() ||
737 ownership_tracker->OwnerIsDefinitelyAbsolutelyDead()) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700738 watcher_index_ = i;
Brian Silverman2484eea2019-12-21 16:48:46 -0800739 // Relaxed is OK here because we're the only task going to touch it
740 // between here and the write in death_notification_init below (other
741 // recovery is blocked by us holding the setup lock).
Philipp Schraderab2f8432023-09-17 18:58:06 -0700742 ownership_tracker->ForceClear();
Austin Schuh20b2b082019-09-11 20:42:56 -0700743 break;
744 }
745 }
746
747 // Bail if we failed to find an open slot.
748 if (watcher_index_ == -1) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700749 return;
Austin Schuh20b2b082019-09-11 20:42:56 -0700750 }
751
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700752 Watcher *const w = memory_->GetWatcher(watcher_index_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700753
754 w->pid = getpid();
755 w->priority = priority;
756
757 // Grabbing a mutex is a compiler and memory barrier, so nothing before will
758 // get rearranged afterwords.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700759 w->ownership_tracker.Acquire();
Austin Schuh20b2b082019-09-11 20:42:56 -0700760}
761
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700762LocklessQueueWakeUpper::LocklessQueueWakeUpper(LocklessQueue queue)
763 : memory_(queue.const_memory()), pid_(getpid()), uid_(getuid()) {
764 queue.Initialize();
765 watcher_copy_.resize(memory_->num_watchers());
Austin Schuh20b2b082019-09-11 20:42:56 -0700766}
767
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700768int LocklessQueueWakeUpper::Wakeup(const int current_priority) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700769 const size_t num_watchers = memory_->num_watchers();
770
771 CHECK_EQ(watcher_copy_.size(), num_watchers);
772
773 // Grab a copy so it won't change out from underneath us, and we can sort it
774 // nicely in C++.
775 // Do note that there is still a window where the process can die *after* we
776 // read everything. We will still PI boost and send a signal to the thread in
777 // question. There is no way without pidfd's to close this window, and
778 // creating a pidfd is likely not RT.
779 for (size_t i = 0; i < num_watchers; ++i) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700780 const Watcher *w = memory_->GetWatcher(i);
Philipp Schraderab2f8432023-09-17 18:58:06 -0700781 watcher_copy_[i].ownership_snapshot = w->ownership_tracker.LoadRelaxed();
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800782 // Force the load of the TID to come first.
783 aos_compiler_memory_barrier();
784 watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed);
785 watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed);
Austin Schuh20b2b082019-09-11 20:42:56 -0700786
787 // Use a priority of -1 to mean an invalid entry to make sorting easier.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700788 if (watcher_copy_[i].ownership_snapshot.OwnerIsDead() ||
789 watcher_copy_[i].ownership_snapshot.IsUnclaimed()) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700790 watcher_copy_[i].priority = -1;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800791 } else {
792 // Ensure all of this happens after we're done looking at the pid+priority
793 // in shared memory.
794 aos_compiler_memory_barrier();
Philipp Schraderab2f8432023-09-17 18:58:06 -0700795 if (watcher_copy_[i].ownership_snapshot !=
796 w->ownership_tracker.LoadRelaxed()) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800797 // Confirm that the watcher hasn't been re-used and modified while we
798 // read it. If it has, mark it invalid again.
799 watcher_copy_[i].priority = -1;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800800 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700801 }
802 }
803
804 // Now sort.
805 ::std::sort(watcher_copy_.begin(), watcher_copy_.end(),
806 [](const WatcherCopy &a, const WatcherCopy &b) {
807 return a.priority > b.priority;
808 });
809
810 int count = 0;
811 if (watcher_copy_[0].priority != -1) {
812 const int max_priority =
813 ::std::max(current_priority, watcher_copy_[0].priority);
814 // Boost if we are RT and there is a higher priority sender out there.
815 // Otherwise we might run into priority inversions.
816 if (max_priority > current_priority && current_priority > 0) {
Austin Schuh151f7822024-03-16 12:52:32 -0700817 // Inline the setscheduler call rather than using aos/realtime.h. This is
818 // quite performance sensitive, and halves the time needed to send a
819 // message when pi boosting is in effect.
820 if (!FLAGS_skip_realtime_scheduler) {
821 // TODO(austin): Do we need to boost the soft limit here too like we
822 // were before?
823 struct sched_param param;
824 param.sched_priority = max_priority;
825 PCHECK(sched_setscheduler(0, SCHED_FIFO, &param) == 0)
826 << ": changing to SCHED_FIFO with " << max_priority
827 << ", if you want to bypass this check for testing, use "
828 "--skip_realtime_scheduler";
829 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700830 }
831
832 // Build up the siginfo to send.
833 siginfo_t uinfo;
834 memset(&uinfo, 0, sizeof(uinfo));
835
836 uinfo.si_code = SI_QUEUE;
837 uinfo.si_pid = pid_;
838 uinfo.si_uid = uid_;
839 uinfo.si_value.sival_int = 0;
840
841 for (const WatcherCopy &watcher_copy : watcher_copy_) {
842 // The first -1 priority means we are at the end of the valid list.
843 if (watcher_copy.priority == -1) {
844 break;
845 }
846
847 // Send the signal. Target just the thread that sent it so that we can
848 // support multiple watchers in a process (when someone creates multiple
849 // event loops in different threads).
Philipp Schraderab2f8432023-09-17 18:58:06 -0700850 rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.ownership_snapshot.tid(),
851 kWakeupSignal, &uinfo);
Austin Schuh20b2b082019-09-11 20:42:56 -0700852
853 ++count;
854 }
855
856 // Drop back down if we were boosted.
857 if (max_priority > current_priority && current_priority > 0) {
Austin Schuh151f7822024-03-16 12:52:32 -0700858 if (!FLAGS_skip_realtime_scheduler) {
859 struct sched_param param;
860 param.sched_priority = current_priority;
861 PCHECK(sched_setscheduler(0, SCHED_FIFO, &param) == 0)
862 << ": changing to SCHED_FIFO with " << max_priority
863 << ", if you want to bypass this check for testing, use "
864 "--skip_realtime_scheduler";
865 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700866 }
867 }
868
869 return count;
870}
871
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700872std::ostream &operator<<(std::ostream &os,
873 const LocklessQueueSender::Result r) {
874 os << static_cast<int>(r);
875 return os;
876}
877
878LocklessQueueSender::LocklessQueueSender(
879 LocklessQueueMemory *memory,
880 monotonic_clock::duration channel_storage_duration)
881 : memory_(memory), channel_storage_duration_(channel_storage_duration) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800882 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700883
884 // Since we already have the lock, go ahead and try cleaning up.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800885 Cleanup(memory_, grab_queue_setup_lock);
Austin Schuh20b2b082019-09-11 20:42:56 -0700886
887 const int num_senders = memory_->num_senders();
888
889 for (int i = 0; i < num_senders; ++i) {
890 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800891 // This doesn't need synchronization because we're the only process doing
892 // initialization right now, and nobody else will be touching senders which
893 // we're interested in.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700894 if (s->ownership_tracker.LoadRelaxed().IsUnclaimed()) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700895 sender_index_ = i;
896 break;
897 }
898 }
899
900 if (sender_index_ == -1) {
Austin Schuhe516ab02020-05-06 21:37:04 -0700901 VLOG(1) << "Too many senders, starting to bail.";
902 return;
Austin Schuh20b2b082019-09-11 20:42:56 -0700903 }
904
Brian Silverman177567e2020-08-12 19:51:33 -0700905 ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700906
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800907 // Indicate that we are now alive by taking over the slot. If the previous
908 // owner died, we still want to do this.
Philipp Schraderab2f8432023-09-17 18:58:06 -0700909 sender->ownership_tracker.Acquire();
Brian Silverman177567e2020-08-12 19:51:33 -0700910
911 const Index scratch_index = sender->scratch_index.RelaxedLoad();
912 Message *const message = memory_->GetMessage(scratch_index);
913 CHECK(!message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid())
914 << ": " << std::hex << scratch_index.get();
Austin Schuh20b2b082019-09-11 20:42:56 -0700915}
916
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700917LocklessQueueSender::~LocklessQueueSender() {
918 if (sender_index_ != -1) {
919 CHECK(memory_ != nullptr);
Philipp Schraderab2f8432023-09-17 18:58:06 -0700920 memory_->GetSender(sender_index_)->ownership_tracker.Release();
Austin Schuh20b2b082019-09-11 20:42:56 -0700921 }
922}
923
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700924std::optional<LocklessQueueSender> LocklessQueueSender::Make(
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700925 LocklessQueue queue, monotonic_clock::duration channel_storage_duration) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700926 queue.Initialize();
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700927 LocklessQueueSender result(queue.memory(), channel_storage_duration);
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700928 if (result.sender_index_ != -1) {
James Kuszmaul9776b392023-01-14 14:08:08 -0800929 return result;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700930 } else {
931 return std::nullopt;
932 }
933}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700934
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700935size_t LocklessQueueSender::size() const {
936 return memory_->message_data_size();
937}
938
939void *LocklessQueueSender::Data() {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700940 ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_);
Brian Silverman177567e2020-08-12 19:51:33 -0700941 const Index scratch_index = sender->scratch_index.RelaxedLoad();
942 Message *const message = memory_->GetMessage(scratch_index);
943 // We should have invalidated this when we first got the buffer. Verify that
944 // in debug mode.
945 DCHECK(
946 !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid())
947 << ": " << std::hex << scratch_index.get();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700948
Brian Silvermana1652f32020-01-29 20:41:44 -0800949 return message->data(memory_->message_data_size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700950}
951
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700952LocklessQueueSender::Result LocklessQueueSender::Send(
Austin Schuhad154822019-12-27 15:45:13 -0800953 const char *data, size_t length,
Austin Schuhb5c6f972021-03-14 21:53:07 -0700954 monotonic_clock::time_point monotonic_remote_time,
955 realtime_clock::time_point realtime_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -0700956 uint32_t remote_queue_index, const UUID &source_boot_uuid,
Austin Schuhb5c6f972021-03-14 21:53:07 -0700957 monotonic_clock::time_point *monotonic_sent_time,
958 realtime_clock::time_point *realtime_sent_time, uint32_t *queue_index) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700959 CHECK_LE(length, size());
Austin Schuh67420a42019-12-21 21:55:04 -0800960 // Flatbuffers write from the back of the buffer to the front. If we are
961 // going to write an explicit chunk of memory into the buffer, we need to
962 // adhere to this convention and place it at the end.
963 memcpy((reinterpret_cast<char *>(Data()) + size() - length), data, length);
Austin Schuh91ba6392020-10-03 13:27:47 -0700964 return Send(length, monotonic_remote_time, realtime_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -0700965 remote_queue_index, source_boot_uuid, monotonic_sent_time,
Austin Schuhb5c6f972021-03-14 21:53:07 -0700966 realtime_sent_time, queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700967}
968
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700969LocklessQueueSender::Result LocklessQueueSender::Send(
Austin Schuhb5c6f972021-03-14 21:53:07 -0700970 size_t length, monotonic_clock::time_point monotonic_remote_time,
971 realtime_clock::time_point realtime_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -0700972 uint32_t remote_queue_index, const UUID &source_boot_uuid,
Austin Schuhb5c6f972021-03-14 21:53:07 -0700973 monotonic_clock::time_point *monotonic_sent_time,
974 realtime_clock::time_point *realtime_sent_time, uint32_t *queue_index) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700975 const size_t queue_size = memory_->queue_size();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700976 CHECK_LE(length, size());
Austin Schuh20b2b082019-09-11 20:42:56 -0700977
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800978 ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
979 // We can do a relaxed load on our sender because we're the only person
980 // modifying it right now.
981 const Index scratch_index = sender->scratch_index.RelaxedLoad();
982 Message *const message = memory_->GetMessage(scratch_index);
Austin Schuh91ba6392020-10-03 13:27:47 -0700983 if (CheckBothRedzones(memory_, message)) {
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700984 return Result::INVALID_REDZONE;
Austin Schuh91ba6392020-10-03 13:27:47 -0700985 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700986
Brian Silverman177567e2020-08-12 19:51:33 -0700987 // We should have invalidated this when we first got the buffer. Verify that
988 // in debug mode.
989 DCHECK(
990 !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid())
991 << ": " << std::hex << scratch_index.get();
992
Austin Schuh20b2b082019-09-11 20:42:56 -0700993 message->header.length = length;
Austin Schuhad154822019-12-27 15:45:13 -0800994 // Pass these through. Any alternative behavior can be implemented out a
995 // layer.
996 message->header.remote_queue_index = remote_queue_index;
Austin Schuha9012be2021-07-21 15:19:11 -0700997 message->header.source_boot_uuid = source_boot_uuid;
Austin Schuhad154822019-12-27 15:45:13 -0800998 message->header.monotonic_remote_time = monotonic_remote_time;
999 message->header.realtime_remote_time = realtime_remote_time;
Austin Schuh20b2b082019-09-11 20:42:56 -07001000
Brian Silverman177567e2020-08-12 19:51:33 -07001001 Index to_replace = Index::Invalid();
Austin Schuh20b2b082019-09-11 20:42:56 -07001002 while (true) {
1003 const QueueIndex actual_next_queue_index =
1004 memory_->next_queue_index.Load(queue_size);
1005 const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index);
1006
1007 const QueueIndex incremented_queue_index = next_queue_index.Increment();
1008
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001009 // This needs to synchronize with whoever the previous writer at this
1010 // location was.
Brian Silverman177567e2020-08-12 19:51:33 -07001011 to_replace = memory_->LoadIndex(next_queue_index);
Austin Schuh20b2b082019-09-11 20:42:56 -07001012
1013 const QueueIndex decremented_queue_index =
1014 next_queue_index.DecrementBy(queue_size);
1015
1016 // See if we got beat. If we did, try to atomically update
1017 // next_queue_index in case the previous writer failed and retry.
1018 if (!to_replace.IsPlausible(decremented_queue_index)) {
1019 // We don't care about the result. It will either succeed, or we got
1020 // beat in fixing it and just need to give up and try again. If we got
1021 // beat multiple times, the only way progress can be made is if the queue
1022 // is updated as well. This means that if we retry reading
1023 // next_queue_index, we will be at most off by one and can retry.
1024 //
1025 // Both require no further action from us.
1026 //
1027 // TODO(austin): If we are having fairness issues under contention, we
1028 // could have a mode bit in next_queue_index, and could use a lock or some
1029 // other form of PI boosting to let the higher priority task win.
1030 memory_->next_queue_index.CompareAndExchangeStrong(
1031 actual_next_queue_index, incremented_queue_index);
1032
Alex Perrycb7da4b2019-08-28 19:35:56 -07001033 VLOG(3) << "We were beat. Try again. Was " << std::hex
1034 << to_replace.get() << ", is " << decremented_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -07001035 continue;
1036 }
1037
1038 // Confirm that the message is what it should be.
Brian Silverman177567e2020-08-12 19:51:33 -07001039 //
1040 // This is just a best-effort check to skip reading the clocks if possible.
1041 // If this fails, then the compare-exchange below definitely would, so we
1042 // can bail out now.
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001043 const Message *message_to_replace = memory_->GetMessage(to_replace);
1044 bool is_previous_index_valid = false;
Austin Schuh20b2b082019-09-11 20:42:56 -07001045 {
Austin Schuh20b2b082019-09-11 20:42:56 -07001046 const QueueIndex previous_index =
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001047 message_to_replace->header.queue_index.RelaxedLoad(queue_size);
1048 is_previous_index_valid = previous_index.valid();
1049 if (previous_index != decremented_queue_index &&
1050 is_previous_index_valid) {
Austin Schuh20b2b082019-09-11 20:42:56 -07001051 // Retry.
Alex Perrycb7da4b2019-08-28 19:35:56 -07001052 VLOG(3) << "Something fishy happened, queue index doesn't match. "
1053 "Retrying. Previous index was "
1054 << std::hex << previous_index.index() << ", should be "
1055 << decremented_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -07001056 continue;
1057 }
1058 }
1059
1060 message->header.monotonic_sent_time = ::aos::monotonic_clock::now();
1061 message->header.realtime_sent_time = ::aos::realtime_clock::now();
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001062
Austin Schuhad154822019-12-27 15:45:13 -08001063 if (monotonic_sent_time != nullptr) {
1064 *monotonic_sent_time = message->header.monotonic_sent_time;
1065 }
1066 if (realtime_sent_time != nullptr) {
1067 *realtime_sent_time = message->header.realtime_sent_time;
1068 }
1069 if (queue_index != nullptr) {
1070 *queue_index = next_queue_index.index();
1071 }
Austin Schuh20b2b082019-09-11 20:42:56 -07001072
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001073 const auto to_replace_monotonic_sent_time =
1074 message_to_replace->header.monotonic_sent_time;
1075
1076 // If we are overwriting a message sent in the last
1077 // channel_storage_duration_, that means that we would be sending more than
1078 // queue_size messages and would therefore be sending too fast. If the
1079 // previous index is not valid then the message hasn't been filled out yet
1080 // so we aren't sending too fast. And, if it is not less than the sent time
1081 // of the message that we are going to write, someone else beat us and the
1082 // compare and exchange below will fail.
1083 if (is_previous_index_valid &&
1084 (to_replace_monotonic_sent_time <
1085 message->header.monotonic_sent_time) &&
1086 (message->header.monotonic_sent_time - to_replace_monotonic_sent_time <
1087 channel_storage_duration_)) {
1088 // There is a possibility that another context beat us to writing out the
1089 // message in the queue, but we beat that context to acquiring the sent
1090 // time. In this case our sent time is *greater than* the other context's
1091 // sent time. Therefore, we can check if we got beat filling out this
1092 // message *after* doing the above check to determine if we hit this edge
1093 // case. Otherwise, messages are being sent too fast.
1094 const QueueIndex previous_index =
1095 message_to_replace->header.queue_index.Load(queue_size);
1096 if (previous_index != decremented_queue_index && previous_index.valid()) {
1097 VLOG(3) << "Got beat during check for messages being sent too fast"
1098 "Retrying.";
1099 continue;
1100 } else {
Austin Schuh71e72142023-05-03 13:10:07 -07001101 VLOG(1) << "Messages sent too fast. Returning. Attempted index: "
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001102 << decremented_queue_index.index()
1103 << " message sent time: " << message->header.monotonic_sent_time
1104 << " message to replace sent time: "
1105 << to_replace_monotonic_sent_time;
Austin Schuh71e72142023-05-03 13:10:07 -07001106
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001107 // Since we are not using the message obtained from scratch_index
1108 // and we are not retrying, we need to invalidate its queue_index.
1109 message->header.queue_index.Invalidate();
1110 return Result::MESSAGES_SENT_TOO_FAST;
1111 }
1112 }
1113
Austin Schuh20b2b082019-09-11 20:42:56 -07001114 // Before we are fully done filling out the message, update the Sender state
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001115 // with the new index to write. This re-uses the barrier for the
Austin Schuh20b2b082019-09-11 20:42:56 -07001116 // queue_index store.
Alex Perrycb7da4b2019-08-28 19:35:56 -07001117 const Index index_to_write(next_queue_index, scratch_index.message_index());
Austin Schuh20b2b082019-09-11 20:42:56 -07001118
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001119 aos_compiler_memory_barrier();
1120 // We're the only person who cares about our scratch index, besides somebody
1121 // cleaning up after us.
Austin Schuh20b2b082019-09-11 20:42:56 -07001122 sender->scratch_index.RelaxedStore(index_to_write);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001123 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001124
1125 message->header.queue_index.Store(next_queue_index);
1126
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001127 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001128 // The message is now filled out, and we have a confirmed slot to store
1129 // into.
1130 //
1131 // Start by writing down what we are going to pull out of the queue. This
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001132 // was Invalid before now. Only person who will read this is whoever cleans
1133 // up after us, so no synchronization necessary.
Austin Schuh20b2b082019-09-11 20:42:56 -07001134 sender->to_replace.RelaxedStore(to_replace);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001135 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001136
1137 // Then exchange the next index into the queue.
1138 if (!memory_->GetQueue(next_queue_index.Wrapped())
1139 ->CompareAndExchangeStrong(to_replace, index_to_write)) {
1140 // Aw, didn't succeed. Retry.
1141 sender->to_replace.RelaxedInvalidate();
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001142 aos_compiler_memory_barrier();
Alex Perrycb7da4b2019-08-28 19:35:56 -07001143 VLOG(3) << "Failed to wrap into queue";
Austin Schuh20b2b082019-09-11 20:42:56 -07001144 continue;
1145 }
1146
1147 // Then update next_queue_index to save the next user some computation time.
1148 memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index,
1149 incremented_queue_index);
1150
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001151 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -07001152 // Now update the scratch space and record that we succeeded.
1153 sender->scratch_index.Store(to_replace);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001154 aos_compiler_memory_barrier();
1155 // And then record that we succeeded, but definitely after the above store.
Austin Schuh20b2b082019-09-11 20:42:56 -07001156 sender->to_replace.RelaxedInvalidate();
Brian Silverman177567e2020-08-12 19:51:33 -07001157
Austin Schuh20b2b082019-09-11 20:42:56 -07001158 break;
1159 }
Brian Silverman177567e2020-08-12 19:51:33 -07001160
Brian Silverman0eaa1da2020-08-12 20:03:52 -07001161 DCHECK(!CheckBothRedzones(memory_, memory_->GetMessage(to_replace)))
1162 << ": Invalid message found in shared memory";
Brian Silverman177567e2020-08-12 19:51:33 -07001163 // to_replace is our current scratch_index. It isn't in the queue, which means
1164 // nobody new can pin it. They can set their `pinned` to it, but they will
1165 // back it out, so they don't count. This means that we just need to find a
1166 // message for which no pinner had it in `pinned`, and then we know this
1167 // message will never be pinned. We'll start with to_replace, and if that is
1168 // pinned then we'll look for a new one to use instead.
1169 const Index new_scratch =
1170 SwapPinnedSenderScratch(memory_, sender, to_replace);
Brian Silverman0eaa1da2020-08-12 20:03:52 -07001171 DCHECK(!CheckBothRedzones(
1172 memory_, memory_->GetMessage(sender->scratch_index.RelaxedLoad())))
1173 << ": Invalid message found in shared memory";
Brian Silverman177567e2020-08-12 19:51:33 -07001174
1175 // If anybody is looking at this message (they shouldn't be), then try telling
1176 // them about it (best-effort).
1177 memory_->GetMessage(new_scratch)->header.queue_index.RelaxedInvalidate();
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -07001178 return Result::GOOD;
Austin Schuh20b2b082019-09-11 20:42:56 -07001179}
1180
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001181int LocklessQueueSender::buffer_index() const {
Brian Silverman4f4e0612020-08-12 19:54:41 -07001182 ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
1183 // We can do a relaxed load on our sender because we're the only person
1184 // modifying it right now.
1185 const Index scratch_index = sender->scratch_index.RelaxedLoad();
1186 return scratch_index.message_index();
1187}
1188
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001189LocklessQueuePinner::LocklessQueuePinner(
1190 LocklessQueueMemory *memory, const LocklessQueueMemory *const_memory)
1191 : memory_(memory), const_memory_(const_memory) {
1192 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
1193
1194 // Since we already have the lock, go ahead and try cleaning up.
1195 Cleanup(memory_, grab_queue_setup_lock);
1196
1197 const int num_pinners = memory_->num_pinners();
1198
1199 for (int i = 0; i < num_pinners; ++i) {
1200 ::aos::ipc_lib::Pinner *p = memory->GetPinner(i);
1201 // This doesn't need synchronization because we're the only process doing
1202 // initialization right now, and nobody else will be touching pinners which
1203 // we're interested in.
Philipp Schraderab2f8432023-09-17 18:58:06 -07001204 if (p->ownership_tracker.LoadRelaxed().IsUnclaimed()) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001205 pinner_index_ = i;
1206 break;
1207 }
1208 }
1209
1210 if (pinner_index_ == -1) {
1211 VLOG(1) << "Too many pinners, starting to bail.";
1212 return;
1213 }
1214
1215 ::aos::ipc_lib::Pinner *p = memory_->GetPinner(pinner_index_);
1216 p->pinned.Invalidate();
1217
1218 // Indicate that we are now alive by taking over the slot. If the previous
1219 // owner died, we still want to do this.
Philipp Schraderab2f8432023-09-17 18:58:06 -07001220 p->ownership_tracker.Acquire();
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001221}
1222
1223LocklessQueuePinner::~LocklessQueuePinner() {
1224 if (pinner_index_ != -1) {
1225 CHECK(memory_ != nullptr);
1226 memory_->GetPinner(pinner_index_)->pinned.Invalidate();
1227 aos_compiler_memory_barrier();
Philipp Schraderab2f8432023-09-17 18:58:06 -07001228 memory_->GetPinner(pinner_index_)->ownership_tracker.Release();
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001229 }
1230}
1231
1232std::optional<LocklessQueuePinner> LocklessQueuePinner::Make(
1233 LocklessQueue queue) {
1234 queue.Initialize();
1235 LocklessQueuePinner result(queue.memory(), queue.const_memory());
1236 if (result.pinner_index_ != -1) {
James Kuszmaul9776b392023-01-14 14:08:08 -08001237 return result;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001238 } else {
1239 return std::nullopt;
1240 }
1241}
1242
1243// This method doesn't mess with any scratch_index, so it doesn't have to worry
1244// about message ownership.
1245int LocklessQueuePinner::PinIndex(uint32_t uint32_queue_index) {
1246 const size_t queue_size = memory_->queue_size();
1247 const QueueIndex queue_index =
1248 QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index);
1249 ipc_lib::Pinner *const pinner = memory_->GetPinner(pinner_index_);
1250
1251 AtomicIndex *const queue_slot = memory_->GetQueue(queue_index.Wrapped());
1252
1253 // Indicate that we want to pin this message.
1254 pinner->pinned.Store(queue_index);
1255 aos_compiler_memory_barrier();
1256
1257 {
1258 const Index message_index = queue_slot->Load();
1259 Message *const message = memory_->GetMessage(message_index);
Brian Silverman0eaa1da2020-08-12 20:03:52 -07001260 DCHECK(!CheckBothRedzones(memory_, message))
1261 << ": Invalid message found in shared memory";
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001262
1263 const QueueIndex message_queue_index =
1264 message->header.queue_index.Load(queue_size);
1265 if (message_queue_index == queue_index) {
1266 VLOG(3) << "Eq: " << std::hex << message_queue_index.index();
1267 aos_compiler_memory_barrier();
1268 return message_index.message_index();
1269 }
1270 VLOG(3) << "Message reused: " << std::hex << message_queue_index.index()
1271 << ", " << queue_index.index();
1272 }
1273
1274 // Being down here means we asked to pin a message before realizing it's no
1275 // longer in the queue, so back that out now.
1276 pinner->pinned.Invalidate();
1277 VLOG(3) << "Unpinned: " << std::hex << queue_index.index();
1278 return -1;
1279}
1280
1281size_t LocklessQueuePinner::size() const {
1282 return const_memory_->message_data_size();
1283}
1284
1285const void *LocklessQueuePinner::Data() const {
1286 const size_t queue_size = const_memory_->queue_size();
1287 const ::aos::ipc_lib::Pinner *const pinner =
1288 const_memory_->GetPinner(pinner_index_);
1289 QueueIndex pinned = pinner->pinned.RelaxedLoad(queue_size);
1290 CHECK(pinned.valid());
1291 const Message *message = const_memory_->GetMessage(pinned);
1292
1293 return message->data(const_memory_->message_data_size());
1294}
1295
1296LocklessQueueReader::Result LocklessQueueReader::Read(
Austin Schuh20b2b082019-09-11 20:42:56 -07001297 uint32_t uint32_queue_index,
Austin Schuhb5c6f972021-03-14 21:53:07 -07001298 monotonic_clock::time_point *monotonic_sent_time,
1299 realtime_clock::time_point *realtime_sent_time,
1300 monotonic_clock::time_point *monotonic_remote_time,
1301 realtime_clock::time_point *realtime_remote_time,
Austin Schuha9012be2021-07-21 15:19:11 -07001302 uint32_t *remote_queue_index, UUID *source_boot_uuid, size_t *length,
Austin Schuhfaec51a2023-09-08 17:43:32 -07001303 char *data,
1304 std::function<bool(const Context &)> should_read_callback) const {
1305 const size_t queue_size = const_memory_->queue_size();
Austin Schuh20b2b082019-09-11 20:42:56 -07001306
1307 // Build up the QueueIndex.
1308 const QueueIndex queue_index =
1309 QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index);
1310
1311 // Read the message stored at the requested location.
Austin Schuhfaec51a2023-09-08 17:43:32 -07001312 Index mi = const_memory_->LoadIndex(queue_index);
1313 const Message *m = const_memory_->GetMessage(mi);
Austin Schuh20b2b082019-09-11 20:42:56 -07001314
1315 while (true) {
Austin Schuhfaec51a2023-09-08 17:43:32 -07001316 DCHECK(!CheckBothRedzones(const_memory_, m))
Brian Silverman0eaa1da2020-08-12 20:03:52 -07001317 << ": Invalid message found in shared memory";
Austin Schuh20b2b082019-09-11 20:42:56 -07001318 // We need to confirm that the data doesn't change while we are reading it.
1319 // Do that by first confirming that the message points to the queue index we
1320 // want.
1321 const QueueIndex starting_queue_index =
1322 m->header.queue_index.Load(queue_size);
1323 if (starting_queue_index != queue_index) {
1324 // If we found a message that is exactly 1 loop old, we just wrapped.
1325 if (starting_queue_index == queue_index.DecrementBy(queue_size)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -07001326 VLOG(3) << "Matches: " << std::hex << starting_queue_index.index()
1327 << ", " << queue_index.DecrementBy(queue_size).index();
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001328 return Result::NOTHING_NEW;
Brian Silverman177567e2020-08-12 19:51:33 -07001329 }
1330
1331 // Someone has re-used this message between when we pulled it out of the
1332 // queue and when we grabbed its index. It is pretty hard to deduce
1333 // what happened. Just try again.
Austin Schuhfaec51a2023-09-08 17:43:32 -07001334 const Message *const new_m = const_memory_->GetMessage(queue_index);
Brian Silverman177567e2020-08-12 19:51:33 -07001335 if (m != new_m) {
1336 m = new_m;
1337 VLOG(3) << "Retrying, m doesn't match";
1338 continue;
1339 }
1340
1341 // We have confirmed that message still points to the same message. This
1342 // means that the message didn't get swapped out from under us, so
1343 // starting_queue_index is correct.
1344 //
1345 // Either we got too far behind (signaled by this being a valid
1346 // message), or this is one of the initial messages which are invalid.
1347 if (starting_queue_index.valid()) {
1348 VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index()
1349 << ", got " << starting_queue_index.index() << ", behind by "
1350 << std::dec
1351 << (starting_queue_index.index() - queue_index.index());
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001352 return Result::TOO_OLD;
Brian Silverman177567e2020-08-12 19:51:33 -07001353 }
1354
1355 VLOG(3) << "Initial";
1356
1357 // There isn't a valid message at this location.
1358 //
1359 // If someone asks for one of the messages within the first go around,
1360 // then they need to wait. They got ahead. Otherwise, they are
1361 // asking for something crazy, like something before the beginning of
1362 // the queue. Tell them that they are behind.
Austin Schuhfaec51a2023-09-08 17:43:32 -07001363 if (uint32_queue_index < const_memory_->queue_size()) {
Brian Silverman177567e2020-08-12 19:51:33 -07001364 VLOG(3) << "Near zero, " << std::hex << uint32_queue_index;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001365 return Result::NOTHING_NEW;
Austin Schuh20b2b082019-09-11 20:42:56 -07001366 } else {
Brian Silverman177567e2020-08-12 19:51:33 -07001367 VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index;
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001368 return Result::TOO_OLD;
Austin Schuh20b2b082019-09-11 20:42:56 -07001369 }
1370 }
Alex Perrycb7da4b2019-08-28 19:35:56 -07001371 VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", "
1372 << queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -07001373 break;
1374 }
1375
Alex Perrycb7da4b2019-08-28 19:35:56 -07001376 // Then read the data out. Copy it all out to be deterministic and so we can
1377 // make length be from either end.
Austin Schuhfaec51a2023-09-08 17:43:32 -07001378 Context context;
1379 context.monotonic_event_time = m->header.monotonic_sent_time;
1380 context.realtime_event_time = m->header.realtime_sent_time;
1381 context.monotonic_remote_time = m->header.monotonic_remote_time;
1382 context.realtime_remote_time = m->header.realtime_remote_time;
1383 context.queue_index = queue_index.index();
1384 if (m->header.remote_queue_index == 0xffffffffu) {
1385 context.remote_queue_index = context.queue_index;
Austin Schuhad154822019-12-27 15:45:13 -08001386 } else {
Austin Schuhfaec51a2023-09-08 17:43:32 -07001387 context.remote_queue_index = m->header.remote_queue_index;
1388 }
1389 context.source_boot_uuid = m->header.source_boot_uuid;
1390 context.size = m->header.length;
1391 context.data = nullptr;
1392 context.buffer_index = -1;
Austin Schuh82ea7382023-07-14 15:17:34 -07001393
Austin Schuhfaec51a2023-09-08 17:43:32 -07001394 // If the callback is provided, use it.
1395 if (should_read_callback) {
Austin Schuh82ea7382023-07-14 15:17:34 -07001396 // And finally, confirm that the message *still* points to the queue index
1397 // we want. This means it didn't change out from under us. If something
1398 // changed out from under us, we were reading it much too late in its
1399 // lifetime.
1400 aos_compiler_memory_barrier();
1401 const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size);
1402 if (final_queue_index != queue_index) {
1403 VLOG(3) << "Changed out from under us. Reading " << std::hex
1404 << queue_index.index() << ", finished with "
1405 << final_queue_index.index() << ", delta: " << std::dec
1406 << (final_queue_index.index() - queue_index.index());
1407 return Result::OVERWROTE;
1408 }
1409
1410 // We now know that the context is safe to use. See if we are supposed to
1411 // take the message or not.
Austin Schuhfaec51a2023-09-08 17:43:32 -07001412 if (!should_read_callback(context)) {
Austin Schuh82ea7382023-07-14 15:17:34 -07001413 return Result::FILTERED;
1414 }
Austin Schuhad154822019-12-27 15:45:13 -08001415 }
Austin Schuh20b2b082019-09-11 20:42:56 -07001416
Austin Schuhfaec51a2023-09-08 17:43:32 -07001417 // Read the data if requested.
1418 if (data) {
1419 memcpy(data, m->data(const_memory_->message_data_size()),
1420 const_memory_->message_data_size());
1421 }
1422
1423 // Now, we need to confirm that nothing has changed by re-reading the queue
1424 // index from the header since we've read all the body. We only need to do it
1425 // if we have read anything new after the previous check up above, which
1426 // happens if we read the data, or if we didn't check for the filtered case.
1427 if (data || !should_read_callback) {
Austin Schuh82ea7382023-07-14 15:17:34 -07001428 aos_compiler_memory_barrier();
1429 const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size);
1430 if (final_queue_index != queue_index) {
1431 VLOG(3) << "Changed out from under us. Reading " << std::hex
1432 << queue_index.index() << ", finished with "
1433 << final_queue_index.index() << ", delta: " << std::dec
1434 << (final_queue_index.index() - queue_index.index());
1435 return Result::OVERWROTE;
1436 }
Austin Schuh20b2b082019-09-11 20:42:56 -07001437 }
1438
Austin Schuhfaec51a2023-09-08 17:43:32 -07001439 // And now take it and make it visible to the user. By doing it here, we will
1440 // never present partial or corrupted state to the user in the output
1441 // pointers.
1442 *monotonic_sent_time = context.monotonic_event_time;
1443 *realtime_sent_time = context.realtime_event_time;
1444 *remote_queue_index = context.remote_queue_index;
1445 *monotonic_remote_time = context.monotonic_remote_time;
1446 *realtime_remote_time = context.realtime_remote_time;
1447 *source_boot_uuid = context.source_boot_uuid;
1448 *length = context.size;
1449
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001450 return Result::GOOD;
Austin Schuh20b2b082019-09-11 20:42:56 -07001451}
1452
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001453QueueIndex LocklessQueueReader::LatestIndex() const {
Austin Schuhfaec51a2023-09-08 17:43:32 -07001454 const size_t queue_size = const_memory_->queue_size();
Austin Schuh20b2b082019-09-11 20:42:56 -07001455
Austin Schuhfaec51a2023-09-08 17:43:32 -07001456 // There are 2 main cases. Either the next queue index is right, or it is
1457 // behind by 1 and wrong. If nothing has been published, the next queue index
1458 // will be the reserved "Invalid" value, otherwise it will point to the next
1459 // place to write. We need to figure out if it is right or wrong, and it if
1460 // is wrong, fix it. If we don't, Read() can find the next message before
1461 // LatestIndex() sees it if someone is hammering on Read() until it returns
1462 // nothing new is left, which mean watchers and fetchers may disagree on when
1463 // a message is published.
1464 QueueIndex actual_next_queue_index =
1465 const_memory_->next_queue_index.Load(queue_size);
1466
1467 // Handle the "nothing has been published" case by making next_queue_index
1468 // point to the 0th index.
1469 const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index);
1470
1471 // This needs to synchronize with whoever the previous writer at this
1472 // location was. Read what is there to see if the message has been published
1473 // and next_queue_index is just behind.
1474 Index to_replace = const_memory_->LoadIndex(next_queue_index);
1475
1476 // See if next_queue_index is consistent with the state of the queue. If it
1477 // is not, try to atomically update next_queue_index in case the previous
1478 // writer failed and retry.
1479 if (to_replace.IsPlausible(next_queue_index)) {
1480 // If next_queue_index ends up pointing to a message with a matching index,
1481 // this is what next_queue_index needs to be updated to
1482 const QueueIndex incremented_queue_index = next_queue_index.Increment();
1483
1484 // We don't care about the result. It will either succeed, or we got
1485 // beat in fixing it. The way the Send logic works, the pointer can never
1486 // get more than 1 behind or the next send will repair it. So, if we fail,
1487 // that means that someone else got there first and fixed it up (and
1488 // potentially someone further continued to send).
1489 //
1490 // Both require no further action from us. Worst case, our Next pointer
1491 // will not be the latest message, but there will always be a point after
1492 // which the index can change. We just need a consistent snapshot where
1493 // there is nothing in the queue that isn't accounted for by
1494 // next_queue_index.
1495 memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index,
1496 incremented_queue_index);
1497
1498 VLOG(3) << "next_queue_index is lagging, fixed it. Found " << std::hex
1499 << to_replace.get() << ", expected "
1500 << next_queue_index.DecrementBy(queue_size).index();
1501
1502 actual_next_queue_index = incremented_queue_index;
1503 }
1504
1505 if (actual_next_queue_index.valid()) {
1506 const QueueIndex current_queue_index =
1507 actual_next_queue_index.DecrementBy(1u);
Alex Perrycb7da4b2019-08-28 19:35:56 -07001508 return current_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -07001509 }
Brian Silvermanfc0d2e82020-08-12 19:58:35 -07001510 return QueueIndex::Invalid();
1511}
1512
1513size_t LocklessQueueSize(const LocklessQueueMemory *memory) {
1514 return memory->queue_size();
1515}
1516
1517size_t LocklessQueueMessageDataSize(const LocklessQueueMemory *memory) {
1518 return memory->message_data_size();
Austin Schuh20b2b082019-09-11 20:42:56 -07001519}
1520
1521namespace {
1522
1523// Prints out the mutex state. Not safe to use while the mutex is being
1524// changed.
Austin Schuh83cbb1e2023-06-23 12:59:02 -07001525::std::string PrintMutex(const aos_mutex *mutex) {
Austin Schuh20b2b082019-09-11 20:42:56 -07001526 ::std::stringstream s;
1527 s << "aos_mutex(" << ::std::hex << mutex->futex;
1528
1529 if (mutex->futex != 0) {
1530 s << ":";
1531 if (mutex->futex & FUTEX_OWNER_DIED) {
1532 s << "FUTEX_OWNER_DIED|";
1533 }
1534 s << "tid=" << (mutex->futex & FUTEX_TID_MASK);
1535 }
1536
1537 s << ")";
1538 return s.str();
1539}
1540
1541} // namespace
1542
Austin Schuh83cbb1e2023-06-23 12:59:02 -07001543void PrintLocklessQueueMemory(const LocklessQueueMemory *memory) {
Austin Schuh20b2b082019-09-11 20:42:56 -07001544 const size_t queue_size = memory->queue_size();
1545 ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl;
1546 ::std::cout << " aos_mutex queue_setup_lock = "
1547 << PrintMutex(&memory->queue_setup_lock) << ::std::endl;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -08001548 ::std::cout << " bool initialized = " << memory->initialized << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001549 ::std::cout << " config {" << ::std::endl;
1550 ::std::cout << " size_t num_watchers = " << memory->config.num_watchers
1551 << ::std::endl;
1552 ::std::cout << " size_t num_senders = " << memory->config.num_senders
1553 << ::std::endl;
Brian Silverman177567e2020-08-12 19:51:33 -07001554 ::std::cout << " size_t num_pinners = " << memory->config.num_pinners
1555 << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001556 ::std::cout << " size_t queue_size = " << memory->config.queue_size
1557 << ::std::endl;
1558 ::std::cout << " size_t message_data_size = "
1559 << memory->config.message_data_size << ::std::endl;
1560
1561 ::std::cout << " AtomicQueueIndex next_queue_index = "
1562 << memory->next_queue_index.Load(queue_size).DebugString()
1563 << ::std::endl;
1564
Austin Schuh3328d132020-02-28 13:54:57 -08001565 ::std::cout << " uid_t uid = " << memory->uid << ::std::endl;
1566
Austin Schuh20b2b082019-09-11 20:42:56 -07001567 ::std::cout << " }" << ::std::endl;
1568 ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl;
1569 for (size_t i = 0; i < queue_size; ++i) {
1570 ::std::cout << " [" << i << "] -> "
1571 << memory->GetQueue(i)->Load().DebugString() << ::std::endl;
1572 }
1573 ::std::cout << " }" << ::std::endl;
1574 ::std::cout << " Message messages[" << memory->num_messages() << "] {"
1575 << ::std::endl;
1576 for (size_t i = 0; i < memory->num_messages(); ++i) {
Austin Schuh83cbb1e2023-06-23 12:59:02 -07001577 const Message *m = memory->GetMessage(Index(i, i));
Brian Silverman001f24d2020-08-12 19:33:20 -07001578 ::std::cout << " [" << i << "] -> Message 0x" << std::hex
1579 << (reinterpret_cast<uintptr_t>(
1580 memory->GetMessage(Index(i, i))) -
1581 reinterpret_cast<uintptr_t>(memory))
1582 << std::dec << " {" << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001583 ::std::cout << " Header {" << ::std::endl;
1584 ::std::cout << " AtomicQueueIndex queue_index = "
1585 << m->header.queue_index.Load(queue_size).DebugString()
1586 << ::std::endl;
Brian Silverman001f24d2020-08-12 19:33:20 -07001587 ::std::cout << " monotonic_clock::time_point monotonic_sent_time = "
1588 << m->header.monotonic_sent_time << " 0x" << std::hex
1589 << m->header.monotonic_sent_time.time_since_epoch().count()
1590 << std::dec << ::std::endl;
1591 ::std::cout << " realtime_clock::time_point realtime_sent_time = "
1592 << m->header.realtime_sent_time << " 0x" << std::hex
1593 << m->header.realtime_sent_time.time_since_epoch().count()
1594 << std::dec << ::std::endl;
1595 ::std::cout
1596 << " monotonic_clock::time_point monotonic_remote_time = "
1597 << m->header.monotonic_remote_time << " 0x" << std::hex
1598 << m->header.monotonic_remote_time.time_since_epoch().count()
1599 << std::dec << ::std::endl;
1600 ::std::cout << " realtime_clock::time_point realtime_remote_time = "
1601 << m->header.realtime_remote_time << " 0x" << std::hex
1602 << m->header.realtime_remote_time.time_since_epoch().count()
1603 << std::dec << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001604 ::std::cout << " size_t length = " << m->header.length
1605 << ::std::endl;
1606 ::std::cout << " }" << ::std::endl;
Austin Schuhbe416742020-10-03 17:24:26 -07001607 const bool corrupt = CheckBothRedzones(memory, m);
1608 if (corrupt) {
Austin Schuh83cbb1e2023-06-23 12:59:02 -07001609 absl::Span<const char> pre_redzone =
1610 m->PreRedzone(memory->message_data_size());
1611 absl::Span<const char> post_redzone =
Austin Schuhbe416742020-10-03 17:24:26 -07001612 m->PostRedzone(memory->message_data_size(), memory->message_size());
1613
1614 ::std::cout << " pre-redzone: \""
1615 << absl::BytesToHexString(std::string_view(
1616 pre_redzone.data(), pre_redzone.size()))
1617 << std::endl;
Brian Silverman0eaa1da2020-08-12 20:03:52 -07001618 ::std::cout << " // *** DATA REDZONES ARE CORRUPTED ***"
1619 << ::std::endl;
Austin Schuhbe416742020-10-03 17:24:26 -07001620 ::std::cout << " post-redzone: \""
1621 << absl::BytesToHexString(std::string_view(
1622 post_redzone.data(), post_redzone.size()))
1623 << std::endl;
Brian Silverman0eaa1da2020-08-12 20:03:52 -07001624 }
Austin Schuh20b2b082019-09-11 20:42:56 -07001625 ::std::cout << " data: {";
1626
Brian Silverman001f24d2020-08-12 19:33:20 -07001627 if (FLAGS_dump_lockless_queue_data) {
1628 const char *const m_data = m->data(memory->message_data_size());
Austin Schuhbe416742020-10-03 17:24:26 -07001629 std::cout << absl::BytesToHexString(std::string_view(
1630 m_data, corrupt ? memory->message_data_size() : m->header.length));
Austin Schuh20b2b082019-09-11 20:42:56 -07001631 }
1632 ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl;
1633 ::std::cout << " }," << ::std::endl;
1634 }
1635 ::std::cout << " }" << ::std::endl;
1636
Alex Perrycb7da4b2019-08-28 19:35:56 -07001637 ::std::cout << " Sender senders[" << memory->num_senders() << "] {"
1638 << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001639 for (size_t i = 0; i < memory->num_senders(); ++i) {
Austin Schuh83cbb1e2023-06-23 12:59:02 -07001640 const Sender *s = memory->GetSender(i);
Austin Schuh20b2b082019-09-11 20:42:56 -07001641 ::std::cout << " [" << i << "] -> Sender {" << ::std::endl;
Philipp Schraderab2f8432023-09-17 18:58:06 -07001642 ::std::cout << " RobustOwnershipTracker ownership_tracker = "
1643 << s->ownership_tracker.DebugString() << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001644 ::std::cout << " AtomicIndex scratch_index = "
1645 << s->scratch_index.Load().DebugString() << ::std::endl;
1646 ::std::cout << " AtomicIndex to_replace = "
1647 << s->to_replace.Load().DebugString() << ::std::endl;
1648 ::std::cout << " }" << ::std::endl;
1649 }
1650 ::std::cout << " }" << ::std::endl;
1651
Brian Silverman177567e2020-08-12 19:51:33 -07001652 ::std::cout << " Pinner pinners[" << memory->num_pinners() << "] {"
1653 << ::std::endl;
1654 for (size_t i = 0; i < memory->num_pinners(); ++i) {
Austin Schuh83cbb1e2023-06-23 12:59:02 -07001655 const Pinner *p = memory->GetPinner(i);
Brian Silverman177567e2020-08-12 19:51:33 -07001656 ::std::cout << " [" << i << "] -> Pinner {" << ::std::endl;
Philipp Schraderab2f8432023-09-17 18:58:06 -07001657 ::std::cout << " RobustOwnershipTracker ownership_tracker = "
1658 << p->ownership_tracker.DebugString() << ::std::endl;
Brian Silverman177567e2020-08-12 19:51:33 -07001659 ::std::cout << " AtomicIndex scratch_index = "
1660 << p->scratch_index.Load().DebugString() << ::std::endl;
1661 ::std::cout << " AtomicIndex pinned = "
1662 << p->pinned.Load(memory->queue_size()).DebugString()
1663 << ::std::endl;
1664 ::std::cout << " }" << ::std::endl;
1665 }
1666 ::std::cout << " }" << ::std::endl;
1667
Austin Schuh20b2b082019-09-11 20:42:56 -07001668 ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {"
1669 << ::std::endl;
1670 for (size_t i = 0; i < memory->num_watchers(); ++i) {
Austin Schuh83cbb1e2023-06-23 12:59:02 -07001671 const Watcher *w = memory->GetWatcher(i);
Austin Schuh20b2b082019-09-11 20:42:56 -07001672 ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl;
Philipp Schraderab2f8432023-09-17 18:58:06 -07001673 ::std::cout << " RobustOwnershipTracker ownership_tracker = "
1674 << w->ownership_tracker.DebugString() << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -07001675 ::std::cout << " pid_t pid = " << w->pid << ::std::endl;
1676 ::std::cout << " int priority = " << w->priority << ::std::endl;
1677 ::std::cout << " }" << ::std::endl;
1678 }
1679 ::std::cout << " }" << ::std::endl;
1680
1681 ::std::cout << "}" << ::std::endl;
1682}
1683
Stephan Pleinesf63bde82024-01-13 15:59:33 -08001684} // namespace aos::ipc_lib