blob: ccbe88785671871f079d748d0685a99330caae27 [file] [log] [blame]
Austin Schuh20b2b082019-09-11 20:42:56 -07001#include "aos/ipc_lib/lockless_queue.h"
2
3#include <linux/futex.h>
4#include <sys/types.h>
5#include <syscall.h>
6#include <unistd.h>
7#include <algorithm>
8#include <iomanip>
9#include <iostream>
10#include <sstream>
11
Austin Schuh20b2b082019-09-11 20:42:56 -070012#include "aos/ipc_lib/lockless_queue_memory.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "aos/realtime.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070014#include "aos/util/compiler_memory_barrier.h"
Austin Schuhf257f3c2019-10-27 21:00:43 -070015#include "glog/logging.h"
Austin Schuh20b2b082019-09-11 20:42:56 -070016
17namespace aos {
18namespace ipc_lib {
Austin Schuh20b2b082019-09-11 20:42:56 -070019namespace {
20
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080021class GrabQueueSetupLockOrDie {
22 public:
23 GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) : memory_(memory) {
24 const int result = mutex_grab(&(memory->queue_setup_lock));
25 CHECK(result == 0 || result == 1) << ": " << result;
26 }
Austin Schuh20b2b082019-09-11 20:42:56 -070027
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080028 ~GrabQueueSetupLockOrDie() { mutex_unlock(&(memory_->queue_setup_lock)); }
29
30 GrabQueueSetupLockOrDie(const GrabQueueSetupLockOrDie &) = delete;
31 GrabQueueSetupLockOrDie &operator=(const GrabQueueSetupLockOrDie &) = delete;
32
33 private:
34 LocklessQueueMemory *const memory_;
35};
36
37void Cleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &) {
38 // Make sure we start looking at shared memory fresh right now. We'll handle
39 // people dying partway through by either cleaning up after them or not, but
40 // we want to ensure we clean up after anybody who has already died when we
41 // start.
42 aos_compiler_memory_barrier();
43
Austin Schuh20b2b082019-09-11 20:42:56 -070044 const size_t num_senders = memory->num_senders();
45 const size_t queue_size = memory->queue_size();
46 const size_t num_messages = memory->num_messages();
47
48 // There are a large number of crazy cases here for how things can go wrong
49 // and how we have to recover. They either require us to keep extra track of
50 // what is going on, slowing down the send path, or require a large number of
51 // cases.
52 //
53 // The solution here is to not over-think it. This is running while not real
54 // time during construction. It is allowed to be slow. It will also very
55 // rarely trigger. There is a small uS window where process death is
56 // ambiguous.
57 //
58 // So, build up a list N long, where N is the number of messages. Search
59 // through the entire queue and the sender list (ignoring any dead senders),
60 // and mark down which ones we have seen. Once we have seen all the messages
61 // except the N dead senders, we know which messages are dead. Because the
62 // queue is active while we do this, it may take a couple of go arounds to see
63 // everything.
64
65 // Do the easy case. Find all senders who have died. See if they are either
66 // consistent already, or if they have copied over to_replace to the scratch
67 // index, but haven't cleared to_replace. Count them.
68 size_t valid_senders = 0;
69 for (size_t i = 0; i < num_senders; ++i) {
70 Sender *sender = memory->GetSender(i);
71 const uint32_t tid =
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080072 __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE);
Austin Schuh20b2b082019-09-11 20:42:56 -070073 if (tid & FUTEX_OWNER_DIED) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070074 VLOG(3) << "Found an easy death for sender " << i;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080075 // We can do a relaxed load here because we're the only person touching
76 // this sender at this point.
Austin Schuh20b2b082019-09-11 20:42:56 -070077 const Index to_replace = sender->to_replace.RelaxedLoad();
78 const Index scratch_index = sender->scratch_index.Load();
79
80 // I find it easiest to think about this in terms of the set of observable
Brian Silvermanfafe1fa2019-12-18 21:42:18 -080081 // states. The main code progresses through the following states:
Austin Schuh20b2b082019-09-11 20:42:56 -070082
83 // 1) scratch_index = xxx
84 // to_replace = invalid
85 // This is unambiguous. Already good.
86
87 // 2) scratch_index = xxx
88 // to_replace = yyy
89 // Very ambiguous. Is xxx or yyy the correct one? Need to either roll
90 // this forwards or backwards.
91
92 // 3) scratch_index = yyy
93 // to_replace = yyy
94 // We are in the act of moving to_replace to scratch_index, but didn't
95 // finish. Easy.
96
97 // 4) scratch_index = yyy
98 // to_replace = invalid
99 // Finished, but died. Looks like 1)
100
101 // Any cleanup code needs to follow the same set of states to be robust to
102 // death, so death can be restarted.
103
104 // Could be 2) or 3).
105 if (to_replace.valid()) {
106 // 3)
107 if (to_replace == scratch_index) {
108 // Just need to invalidate to_replace to finish.
109 sender->to_replace.Invalidate();
110
111 // And mark that we succeeded.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800112 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
Austin Schuh20b2b082019-09-11 20:42:56 -0700113 ++valid_senders;
114 }
115 } else {
116 // 1) or 4). Make sure we aren't corrupted and declare victory.
117 CHECK(scratch_index.valid());
118
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800119 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
Austin Schuh20b2b082019-09-11 20:42:56 -0700120 ++valid_senders;
121 }
122 } else {
123 // Not dead.
124 ++valid_senders;
125 }
126 }
127
128 // If all the senders are (or were made) good, there is no need to do the hard
129 // case.
130 if (valid_senders == num_senders) {
131 return;
132 }
133
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134 VLOG(3) << "Starting hard cleanup";
Austin Schuh20b2b082019-09-11 20:42:56 -0700135
136 size_t num_accounted_for = 0;
137 size_t num_missing = 0;
138 ::std::vector<bool> accounted_for(num_messages, false);
139
140 while ((num_accounted_for + num_missing) != num_messages) {
141 num_missing = 0;
142 for (size_t i = 0; i < num_senders; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800143 Sender *const sender = memory->GetSender(i);
Austin Schuh20b2b082019-09-11 20:42:56 -0700144 const uint32_t tid =
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800145 __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE);
Austin Schuh20b2b082019-09-11 20:42:56 -0700146 if (tid & FUTEX_OWNER_DIED) {
147 ++num_missing;
148 } else {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800149 // We can do a relaxed load here because we're the only person touching
150 // this sender at this point, if it matters. If it's not a dead sender,
151 // then any message it every has will already be accounted for, so this
152 // will always be a NOP.
Austin Schuh20b2b082019-09-11 20:42:56 -0700153 const Index scratch_index = sender->scratch_index.RelaxedLoad();
154 if (!accounted_for[scratch_index.message_index()]) {
155 ++num_accounted_for;
156 }
157 accounted_for[scratch_index.message_index()] = true;
158 }
159 }
160
161 for (size_t i = 0; i < queue_size; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800162 // Same logic as above for scratch_index applies here too.
Austin Schuh20b2b082019-09-11 20:42:56 -0700163 const Index index = memory->GetQueue(i)->RelaxedLoad();
164 if (!accounted_for[index.message_index()]) {
165 ++num_accounted_for;
166 }
167 accounted_for[index.message_index()] = true;
168 }
169 }
170
171 while (num_missing != 0) {
172 const size_t starting_num_missing = num_missing;
173 for (size_t i = 0; i < num_senders; ++i) {
174 Sender *sender = memory->GetSender(i);
175 const uint32_t tid =
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800176 __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE);
Austin Schuh20b2b082019-09-11 20:42:56 -0700177 if (tid & FUTEX_OWNER_DIED) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800178 // We can do relaxed loads here because we're the only person touching
179 // this sender at this point.
Austin Schuh20b2b082019-09-11 20:42:56 -0700180 const Index scratch_index = sender->scratch_index.RelaxedLoad();
181 const Index to_replace = sender->to_replace.RelaxedLoad();
182
183 // Candidate.
184 CHECK_LE(to_replace.message_index(), accounted_for.size());
185 if (accounted_for[to_replace.message_index()]) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186 VLOG(3) << "Sender " << i
187 << " died, to_replace is already accounted for";
Austin Schuh20b2b082019-09-11 20:42:56 -0700188 // If both are accounted for, we are corrupt...
189 CHECK(!accounted_for[scratch_index.message_index()]);
190
191 // to_replace is already accounted for. This means that we didn't
192 // atomically insert scratch_index into the queue yet. So
193 // invalidate to_replace.
194 sender->to_replace.Invalidate();
195
196 // And then mark this sender clean.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800197 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
Austin Schuh20b2b082019-09-11 20:42:56 -0700198
199 // And account for scratch_index.
200 accounted_for[scratch_index.message_index()] = true;
201 --num_missing;
202 ++num_accounted_for;
203 } else if (accounted_for[scratch_index.message_index()]) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700204 VLOG(3) << "Sender " << i
205 << " died, scratch_index is already accounted for";
Austin Schuh20b2b082019-09-11 20:42:56 -0700206 // scratch_index is accounted for. That means we did the insert,
207 // but didn't record it.
208 CHECK(to_replace.valid());
209 // Finish the transaction. Copy to_replace, then clear it.
210
211 sender->scratch_index.Store(to_replace);
212 sender->to_replace.Invalidate();
213
214 // And then mark this sender clean.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800215 __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE);
Austin Schuh20b2b082019-09-11 20:42:56 -0700216
217 // And account for to_replace.
218 accounted_for[to_replace.message_index()] = true;
219 --num_missing;
220 ++num_accounted_for;
221 } else {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700222 VLOG(3) << "Sender " << i << " died, neither is accounted for";
Austin Schuh20b2b082019-09-11 20:42:56 -0700223 // Ambiguous. There will be an unambiguous one somewhere that we
224 // can do first.
225 }
226 }
227 }
228 // CHECK that we are making progress.
229 CHECK_NE(num_missing, starting_num_missing);
230 }
231}
232
233// Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target
234// thread.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800235// TODO(Brian): Do directly in assembly for armhf at least for efficiency.
Austin Schuh20b2b082019-09-11 20:42:56 -0700236int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) {
237 return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si);
238}
239
240} // namespace
241
Austin Schuh4bc4f902019-12-23 18:04:51 -0800242size_t LocklessQueueConfiguration::message_size() const {
243 // Round up the message size so following data is aligned appropriately.
Brian Silvermana1652f32020-01-29 20:41:44 -0800244 return LocklessQueueMemory::AlignmentRoundUp(message_data_size +
245 (kChannelDataAlignment - 1)) +
Austin Schuh4bc4f902019-12-23 18:04:51 -0800246 sizeof(Message);
247}
248
Austin Schuh20b2b082019-09-11 20:42:56 -0700249size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800250 // Round up the message size so following data is aligned appropriately.
251 config.message_data_size =
252 LocklessQueueMemory::AlignmentRoundUp(config.message_data_size);
Austin Schuh20b2b082019-09-11 20:42:56 -0700253
254 // As we build up the size, confirm that everything is aligned to the
255 // alignment requirements of the type.
256 size_t size = sizeof(LocklessQueueMemory);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800257 CHECK_EQ(size % alignof(LocklessQueueMemory), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700258
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800259 CHECK_EQ(size % alignof(AtomicIndex), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700260 size += LocklessQueueMemory::SizeOfQueue(config);
261
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800262 CHECK_EQ(size % alignof(Message), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700263 size += LocklessQueueMemory::SizeOfMessages(config);
264
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800265 CHECK_EQ(size % alignof(Watcher), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700266 size += LocklessQueueMemory::SizeOfWatchers(config);
267
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800268 CHECK_EQ(size % alignof(Sender), 0u);
Austin Schuh20b2b082019-09-11 20:42:56 -0700269 size += LocklessQueueMemory::SizeOfSenders(config);
270
271 return size;
272}
273
274LocklessQueueMemory *InitializeLocklessQueueMemory(
275 LocklessQueueMemory *memory, LocklessQueueConfiguration config) {
276 // Everything should be zero initialized already. So we just need to fill
277 // everything out properly.
278
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700279 // This is the UID we will use for checking signal-sending permission
280 // compatibility.
281 //
282 // The manpage says:
283 // For a process to have permission to send a signal, it must either be
284 // privileged [...], or the real or effective user ID of the sending process
285 // must equal the real or saved set-user-ID of the target process.
286 //
287 // Processes typically initialize a queue in random order as they start up.
288 // This means we need an algorithm for verifying all processes have
289 // permissions to send each other signals which gives the same answer no
290 // matter what order they attach in. We would also like to avoid maintaining a
291 // shared list of the UIDs of all processes.
292 //
293 // To do this while still giving sufficient flexibility for all current use
294 // cases, we track a single UID for the queue. All processes with a matching
295 // euid+suid must have this UID. Any processes with distinct euid/suid must
296 // instead have a matching ruid. This guarantees signals can be sent between
297 // all processes attached to the queue.
298 //
299 // In particular, this allows a process to change only its euid (to interact
300 // with a queue) while still maintaining privileges via its ruid. However, it
301 // can only use privileges in ways that do not require changing the euid back,
302 // because while the euid is different it will not be able to receive signals.
303 // We can't actually verify that, but we can sanity check that things are
304 // valid when the queue is initialized.
305
306 uid_t uid;
307 {
308 uid_t ruid, euid, suid;
309 PCHECK(getresuid(&ruid, &euid, &suid) == 0);
310 // If these are equal, then use them, even if that's different from the real
311 // UID. This allows processes to keep a real UID of 0 (to have permissions
312 // to perform system-level changes) while still being able to communicate
313 // with processes running unprivileged as a distinct user.
314 if (euid == suid) {
315 uid = euid;
316 VLOG(1) << "Using euid==suid " << uid;
317 } else {
318 uid = ruid;
319 VLOG(1) << "Using ruid " << ruid;
320 }
321 }
322
Austin Schuh20b2b082019-09-11 20:42:56 -0700323 // Grab the mutex. We don't care if the previous reader died. We are going
324 // to check everything anyways.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800325 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory);
Austin Schuh20b2b082019-09-11 20:42:56 -0700326
327 if (!memory->initialized) {
328 // TODO(austin): Check these for out of bounds.
329 memory->config.num_watchers = config.num_watchers;
330 memory->config.num_senders = config.num_senders;
331 memory->config.queue_size = config.queue_size;
Austin Schuh4bc4f902019-12-23 18:04:51 -0800332 memory->config.message_data_size = config.message_data_size;
Austin Schuh20b2b082019-09-11 20:42:56 -0700333
334 const size_t num_messages = memory->num_messages();
335 // There need to be at most MaxMessages() messages allocated.
336 CHECK_LE(num_messages, Index::MaxMessages());
337
338 for (size_t i = 0; i < num_messages; ++i) {
339 memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i))
340 ->header.queue_index.Invalidate();
341 }
342
343 for (size_t i = 0; i < memory->queue_size(); ++i) {
344 // Make the initial counter be the furthest away number. That means that
345 // index 0 should be 0xffff, 1 should be 0, etc.
346 memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size())
347 .IncrementBy(i)
348 .DecrementBy(memory->queue_size()),
349 i));
350 }
351
352 memory->next_queue_index.Invalidate();
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700353 memory->uid = uid;
Austin Schuh20b2b082019-09-11 20:42:56 -0700354
355 for (size_t i = 0; i < memory->num_senders(); ++i) {
356 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800357 // Nobody else can possibly be touching these because we haven't set
358 // initialized to true yet.
359 s->scratch_index.RelaxedStore(Index(0xffff, i + memory->queue_size()));
Austin Schuh20b2b082019-09-11 20:42:56 -0700360 s->to_replace.RelaxedInvalidate();
361 }
362
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800363 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700364 // Signal everything is done. This needs to be done last, so if we die, we
365 // redo initialization.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800366 memory->initialized = true;
Austin Schuh3328d132020-02-28 13:54:57 -0800367 } else {
Brian Silvermanc57ff0a2020-04-28 16:45:13 -0700368 CHECK_EQ(uid, memory->uid) << ": UIDs must match for all processes";
Austin Schuh20b2b082019-09-11 20:42:56 -0700369 }
370
Austin Schuh20b2b082019-09-11 20:42:56 -0700371 return memory;
372}
373
374LocklessQueue::LocklessQueue(LocklessQueueMemory *memory,
375 LocklessQueueConfiguration config)
376 : memory_(InitializeLocklessQueueMemory(memory, config)),
377 watcher_copy_(memory_->num_watchers()),
378 pid_(getpid()),
379 uid_(getuid()) {}
380
381LocklessQueue::~LocklessQueue() {
382 CHECK_EQ(watcher_index_, -1);
383
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800384 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700385 const int num_watchers = memory_->num_watchers();
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800386 // Cleanup is cheap. The next user will do it anyways, so no need for us to do
387 // anything right now.
Austin Schuh20b2b082019-09-11 20:42:56 -0700388
389 // And confirm that nothing is owned by us.
390 for (int i = 0; i < num_watchers; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800391 CHECK(!death_notification_is_held(&(memory_->GetWatcher(i)->tid)));
Austin Schuh20b2b082019-09-11 20:42:56 -0700392 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700393}
394
395size_t LocklessQueue::QueueSize() const { return memory_->queue_size(); }
396
397bool LocklessQueue::RegisterWakeup(int priority) {
398 // TODO(austin): Make sure signal coalescing is turned on. We don't need
399 // duplicates. That will improve performance under high load.
400
401 // Since everything is self consistent, all we need to do is make sure nobody
402 // else is running. Someone dying will get caught in the generic consistency
403 // check.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800404 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700405 const int num_watchers = memory_->num_watchers();
406
407 // Now, find the first empty watcher and grab it.
408 CHECK_EQ(watcher_index_, -1);
409 for (int i = 0; i < num_watchers; ++i) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800410 // If we see a slot the kernel has marked as dead, everything we do reusing
411 // it needs to happen-after whatever that process did before dying.
Brian Silverman2484eea2019-12-21 16:48:46 -0800412 auto *const futex = &(memory_->GetWatcher(i)->tid.futex);
413 const uint32_t tid = __atomic_load_n(futex, __ATOMIC_ACQUIRE);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800414 if (tid == 0 || (tid & FUTEX_OWNER_DIED)) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700415 watcher_index_ = i;
Brian Silverman2484eea2019-12-21 16:48:46 -0800416 // Relaxed is OK here because we're the only task going to touch it
417 // between here and the write in death_notification_init below (other
418 // recovery is blocked by us holding the setup lock).
419 __atomic_store_n(futex, 0, __ATOMIC_RELAXED);
Austin Schuh20b2b082019-09-11 20:42:56 -0700420 break;
421 }
422 }
423
424 // Bail if we failed to find an open slot.
425 if (watcher_index_ == -1) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700426 return false;
427 }
428
429 Watcher *w = memory_->GetWatcher(watcher_index_);
430
431 w->pid = getpid();
432 w->priority = priority;
433
434 // Grabbing a mutex is a compiler and memory barrier, so nothing before will
435 // get rearranged afterwords.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800436 death_notification_init(&(w->tid));
437 return true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700438}
439
440void LocklessQueue::UnregisterWakeup() {
441 // Since everything is self consistent, all we need to do is make sure nobody
442 // else is running. Someone dying will get caught in the generic consistency
443 // check.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800444 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700445
446 // Make sure we are registered.
447 CHECK_NE(watcher_index_, -1);
448
449 // Make sure we still own the slot we are supposed to.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800450 CHECK(
451 death_notification_is_held(&(memory_->GetWatcher(watcher_index_)->tid)));
Austin Schuh20b2b082019-09-11 20:42:56 -0700452
453 // The act of unlocking invalidates the entry. Invalidate it.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800454 death_notification_release(&(memory_->GetWatcher(watcher_index_)->tid));
Austin Schuh20b2b082019-09-11 20:42:56 -0700455 // And internally forget the slot.
456 watcher_index_ = -1;
Austin Schuh20b2b082019-09-11 20:42:56 -0700457}
458
459int LocklessQueue::Wakeup(const int current_priority) {
460 const size_t num_watchers = memory_->num_watchers();
461
462 CHECK_EQ(watcher_copy_.size(), num_watchers);
463
464 // Grab a copy so it won't change out from underneath us, and we can sort it
465 // nicely in C++.
466 // Do note that there is still a window where the process can die *after* we
467 // read everything. We will still PI boost and send a signal to the thread in
468 // question. There is no way without pidfd's to close this window, and
469 // creating a pidfd is likely not RT.
470 for (size_t i = 0; i < num_watchers; ++i) {
471 Watcher *w = memory_->GetWatcher(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800472 watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_RELAXED);
473 // Force the load of the TID to come first.
474 aos_compiler_memory_barrier();
475 watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed);
476 watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed);
Austin Schuh20b2b082019-09-11 20:42:56 -0700477
478 // Use a priority of -1 to mean an invalid entry to make sorting easier.
479 if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) {
480 watcher_copy_[i].priority = -1;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800481 } else {
482 // Ensure all of this happens after we're done looking at the pid+priority
483 // in shared memory.
484 aos_compiler_memory_barrier();
485 if (watcher_copy_[i].tid != static_cast<pid_t>(__atomic_load_n(
486 &(w->tid.futex), __ATOMIC_RELAXED))) {
487 // Confirm that the watcher hasn't been re-used and modified while we
488 // read it. If it has, mark it invalid again.
489 watcher_copy_[i].priority = -1;
490 watcher_copy_[i].tid = 0;
491 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700492 }
493 }
494
495 // Now sort.
496 ::std::sort(watcher_copy_.begin(), watcher_copy_.end(),
497 [](const WatcherCopy &a, const WatcherCopy &b) {
498 return a.priority > b.priority;
499 });
500
501 int count = 0;
502 if (watcher_copy_[0].priority != -1) {
503 const int max_priority =
504 ::std::max(current_priority, watcher_copy_[0].priority);
505 // Boost if we are RT and there is a higher priority sender out there.
506 // Otherwise we might run into priority inversions.
507 if (max_priority > current_priority && current_priority > 0) {
508 SetCurrentThreadRealtimePriority(max_priority);
509 }
510
511 // Build up the siginfo to send.
512 siginfo_t uinfo;
513 memset(&uinfo, 0, sizeof(uinfo));
514
515 uinfo.si_code = SI_QUEUE;
516 uinfo.si_pid = pid_;
517 uinfo.si_uid = uid_;
518 uinfo.si_value.sival_int = 0;
519
520 for (const WatcherCopy &watcher_copy : watcher_copy_) {
521 // The first -1 priority means we are at the end of the valid list.
522 if (watcher_copy.priority == -1) {
523 break;
524 }
525
526 // Send the signal. Target just the thread that sent it so that we can
527 // support multiple watchers in a process (when someone creates multiple
528 // event loops in different threads).
529 rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal,
530 &uinfo);
531
532 ++count;
533 }
534
535 // Drop back down if we were boosted.
536 if (max_priority > current_priority && current_priority > 0) {
537 SetCurrentThreadRealtimePriority(current_priority);
538 }
539 }
540
541 return count;
542}
543
544LocklessQueue::Sender::Sender(LocklessQueueMemory *memory) : memory_(memory) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800545 GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_);
Austin Schuh20b2b082019-09-11 20:42:56 -0700546
547 // Since we already have the lock, go ahead and try cleaning up.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800548 Cleanup(memory_, grab_queue_setup_lock);
Austin Schuh20b2b082019-09-11 20:42:56 -0700549
550 const int num_senders = memory_->num_senders();
551
552 for (int i = 0; i < num_senders; ++i) {
553 ::aos::ipc_lib::Sender *s = memory->GetSender(i);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800554 // This doesn't need synchronization because we're the only process doing
555 // initialization right now, and nobody else will be touching senders which
556 // we're interested in.
Austin Schuh20b2b082019-09-11 20:42:56 -0700557 const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED);
558 if (tid == 0) {
559 sender_index_ = i;
560 break;
561 }
562 }
563
564 if (sender_index_ == -1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700565 LOG(FATAL) << "Too many senders";
Austin Schuh20b2b082019-09-11 20:42:56 -0700566 }
567
568 ::aos::ipc_lib::Sender *s = memory_->GetSender(sender_index_);
569
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800570 // Indicate that we are now alive by taking over the slot. If the previous
571 // owner died, we still want to do this.
572 death_notification_init(&(s->tid));
Austin Schuh20b2b082019-09-11 20:42:56 -0700573}
574
575LocklessQueue::Sender::~Sender() {
576 if (memory_ != nullptr) {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800577 death_notification_release(&(memory_->GetSender(sender_index_)->tid));
Austin Schuh20b2b082019-09-11 20:42:56 -0700578 }
579}
580
581LocklessQueue::Sender LocklessQueue::MakeSender() {
582 return LocklessQueue::Sender(memory_);
583}
584
585QueueIndex ZeroOrValid(QueueIndex index) {
586 if (!index.valid()) {
587 return index.Clear();
588 }
589 return index;
590}
591
Alex Perrycb7da4b2019-08-28 19:35:56 -0700592size_t LocklessQueue::Sender::size() { return memory_->message_data_size(); }
593
594void *LocklessQueue::Sender::Data() {
595 ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_);
596 Index scratch_index = sender->scratch_index.RelaxedLoad();
597 Message *message = memory_->GetMessage(scratch_index);
598 message->header.queue_index.Invalidate();
599
Brian Silvermana1652f32020-01-29 20:41:44 -0800600 return message->data(memory_->message_data_size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700601}
602
Austin Schuhad154822019-12-27 15:45:13 -0800603void LocklessQueue::Sender::Send(
604 const char *data, size_t length,
605 aos::monotonic_clock::time_point monotonic_remote_time,
606 aos::realtime_clock::time_point realtime_remote_time,
607 uint32_t remote_queue_index,
608 aos::monotonic_clock::time_point *monotonic_sent_time,
609 aos::realtime_clock::time_point *realtime_sent_time,
610 uint32_t *queue_index) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700611 CHECK_LE(length, size());
Austin Schuh67420a42019-12-21 21:55:04 -0800612 // Flatbuffers write from the back of the buffer to the front. If we are
613 // going to write an explicit chunk of memory into the buffer, we need to
614 // adhere to this convention and place it at the end.
615 memcpy((reinterpret_cast<char *>(Data()) + size() - length), data, length);
Austin Schuhad154822019-12-27 15:45:13 -0800616 Send(length, monotonic_remote_time, realtime_remote_time, remote_queue_index,
617 monotonic_sent_time, realtime_sent_time, queue_index);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700618}
619
Austin Schuhad154822019-12-27 15:45:13 -0800620void LocklessQueue::Sender::Send(
621 size_t length, aos::monotonic_clock::time_point monotonic_remote_time,
622 aos::realtime_clock::time_point realtime_remote_time,
623 uint32_t remote_queue_index,
624 aos::monotonic_clock::time_point *monotonic_sent_time,
625 aos::realtime_clock::time_point *realtime_sent_time,
626 uint32_t *queue_index) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700627 const size_t queue_size = memory_->queue_size();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700628 CHECK_LE(length, size());
Austin Schuh20b2b082019-09-11 20:42:56 -0700629
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800630 ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_);
631 // We can do a relaxed load on our sender because we're the only person
632 // modifying it right now.
633 const Index scratch_index = sender->scratch_index.RelaxedLoad();
634 Message *const message = memory_->GetMessage(scratch_index);
Austin Schuh20b2b082019-09-11 20:42:56 -0700635
Austin Schuh20b2b082019-09-11 20:42:56 -0700636 message->header.length = length;
Austin Schuhad154822019-12-27 15:45:13 -0800637 // Pass these through. Any alternative behavior can be implemented out a
638 // layer.
639 message->header.remote_queue_index = remote_queue_index;
640 message->header.monotonic_remote_time = monotonic_remote_time;
641 message->header.realtime_remote_time = realtime_remote_time;
Austin Schuh20b2b082019-09-11 20:42:56 -0700642
643 while (true) {
644 const QueueIndex actual_next_queue_index =
645 memory_->next_queue_index.Load(queue_size);
646 const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index);
647
648 const QueueIndex incremented_queue_index = next_queue_index.Increment();
649
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800650 // This needs to synchronize with whoever the previous writer at this
651 // location was.
Austin Schuh20b2b082019-09-11 20:42:56 -0700652 const Index to_replace = memory_->LoadIndex(next_queue_index);
653
654 const QueueIndex decremented_queue_index =
655 next_queue_index.DecrementBy(queue_size);
656
657 // See if we got beat. If we did, try to atomically update
658 // next_queue_index in case the previous writer failed and retry.
659 if (!to_replace.IsPlausible(decremented_queue_index)) {
660 // We don't care about the result. It will either succeed, or we got
661 // beat in fixing it and just need to give up and try again. If we got
662 // beat multiple times, the only way progress can be made is if the queue
663 // is updated as well. This means that if we retry reading
664 // next_queue_index, we will be at most off by one and can retry.
665 //
666 // Both require no further action from us.
667 //
668 // TODO(austin): If we are having fairness issues under contention, we
669 // could have a mode bit in next_queue_index, and could use a lock or some
670 // other form of PI boosting to let the higher priority task win.
671 memory_->next_queue_index.CompareAndExchangeStrong(
672 actual_next_queue_index, incremented_queue_index);
673
Alex Perrycb7da4b2019-08-28 19:35:56 -0700674 VLOG(3) << "We were beat. Try again. Was " << std::hex
675 << to_replace.get() << ", is " << decremented_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -0700676 continue;
677 }
678
679 // Confirm that the message is what it should be.
680 {
Austin Schuh20b2b082019-09-11 20:42:56 -0700681 const QueueIndex previous_index =
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800682 memory_->GetMessage(to_replace)->header.queue_index.Load(queue_size);
Austin Schuh20b2b082019-09-11 20:42:56 -0700683 if (previous_index != decremented_queue_index && previous_index.valid()) {
684 // Retry.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700685 VLOG(3) << "Something fishy happened, queue index doesn't match. "
686 "Retrying. Previous index was "
687 << std::hex << previous_index.index() << ", should be "
688 << decremented_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -0700689 continue;
690 }
691 }
692
693 message->header.monotonic_sent_time = ::aos::monotonic_clock::now();
694 message->header.realtime_sent_time = ::aos::realtime_clock::now();
Austin Schuhad154822019-12-27 15:45:13 -0800695 if (monotonic_sent_time != nullptr) {
696 *monotonic_sent_time = message->header.monotonic_sent_time;
697 }
698 if (realtime_sent_time != nullptr) {
699 *realtime_sent_time = message->header.realtime_sent_time;
700 }
701 if (queue_index != nullptr) {
702 *queue_index = next_queue_index.index();
703 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700704
705 // Before we are fully done filling out the message, update the Sender state
706 // with the new index to write. This re-uses the barrier for the
707 // queue_index store.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700708 const Index index_to_write(next_queue_index, scratch_index.message_index());
Austin Schuh20b2b082019-09-11 20:42:56 -0700709
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800710 aos_compiler_memory_barrier();
711 // We're the only person who cares about our scratch index, besides somebody
712 // cleaning up after us.
Austin Schuh20b2b082019-09-11 20:42:56 -0700713 sender->scratch_index.RelaxedStore(index_to_write);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800714 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700715
716 message->header.queue_index.Store(next_queue_index);
717
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800718 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700719 // The message is now filled out, and we have a confirmed slot to store
720 // into.
721 //
722 // Start by writing down what we are going to pull out of the queue. This
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800723 // was Invalid before now. Only person who will read this is whoever cleans
724 // up after us, so no synchronization necessary.
Austin Schuh20b2b082019-09-11 20:42:56 -0700725 sender->to_replace.RelaxedStore(to_replace);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800726 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700727
728 // Then exchange the next index into the queue.
729 if (!memory_->GetQueue(next_queue_index.Wrapped())
730 ->CompareAndExchangeStrong(to_replace, index_to_write)) {
731 // Aw, didn't succeed. Retry.
732 sender->to_replace.RelaxedInvalidate();
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800733 aos_compiler_memory_barrier();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700734 VLOG(3) << "Failed to wrap into queue";
Austin Schuh20b2b082019-09-11 20:42:56 -0700735 continue;
736 }
737
738 // Then update next_queue_index to save the next user some computation time.
739 memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index,
740 incremented_queue_index);
741
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800742 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700743 // Now update the scratch space and record that we succeeded.
744 sender->scratch_index.Store(to_replace);
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800745 aos_compiler_memory_barrier();
746 // And then record that we succeeded, but definitely after the above store.
Austin Schuh20b2b082019-09-11 20:42:56 -0700747 sender->to_replace.RelaxedInvalidate();
748 break;
749 }
750}
751
752LocklessQueue::ReadResult LocklessQueue::Read(
753 uint32_t uint32_queue_index,
754 ::aos::monotonic_clock::time_point *monotonic_sent_time,
Austin Schuhad154822019-12-27 15:45:13 -0800755 ::aos::realtime_clock::time_point *realtime_sent_time,
756 ::aos::monotonic_clock::time_point *monotonic_remote_time,
757 ::aos::realtime_clock::time_point *realtime_remote_time,
758 uint32_t *remote_queue_index, size_t *length, char *data) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700759 const size_t queue_size = memory_->queue_size();
760
761 // Build up the QueueIndex.
762 const QueueIndex queue_index =
763 QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index);
764
765 // Read the message stored at the requested location.
766 Index mi = memory_->LoadIndex(queue_index);
767 Message *m = memory_->GetMessage(mi);
768
769 while (true) {
770 // We need to confirm that the data doesn't change while we are reading it.
771 // Do that by first confirming that the message points to the queue index we
772 // want.
773 const QueueIndex starting_queue_index =
774 m->header.queue_index.Load(queue_size);
775 if (starting_queue_index != queue_index) {
776 // If we found a message that is exactly 1 loop old, we just wrapped.
777 if (starting_queue_index == queue_index.DecrementBy(queue_size)) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700778 VLOG(3) << "Matches: " << std::hex << starting_queue_index.index()
779 << ", " << queue_index.DecrementBy(queue_size).index();
Austin Schuh20b2b082019-09-11 20:42:56 -0700780 return ReadResult::NOTHING_NEW;
781 } else {
782 // Someone has re-used this message between when we pulled it out of the
783 // queue and when we grabbed its index. It is pretty hard to deduce
784 // what happened. Just try again.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800785 Message *const new_m = memory_->GetMessage(queue_index);
Austin Schuh20b2b082019-09-11 20:42:56 -0700786 if (m != new_m) {
787 m = new_m;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700788 VLOG(3) << "Retrying, m doesn't match";
Austin Schuh20b2b082019-09-11 20:42:56 -0700789 continue;
790 }
791
792 // We have confirmed that message still points to the same message. This
793 // means that the message didn't get swapped out from under us, so
794 // starting_queue_index is correct.
795 //
796 // Either we got too far behind (signaled by this being a valid
797 // message), or this is one of the initial messages which are invalid.
798 if (starting_queue_index.valid()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700799 VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index()
800 << ", got " << starting_queue_index.index() << ", behind by "
801 << std::dec
802 << (starting_queue_index.index() - queue_index.index());
Austin Schuh20b2b082019-09-11 20:42:56 -0700803 return ReadResult::TOO_OLD;
804 }
805
Alex Perrycb7da4b2019-08-28 19:35:56 -0700806 VLOG(3) << "Initial";
Austin Schuh20b2b082019-09-11 20:42:56 -0700807
808 // There isn't a valid message at this location.
809 //
810 // If someone asks for one of the messages within the first go around,
811 // then they need to wait. They got ahead. Otherwise, they are
812 // asking for something crazy, like something before the beginning of
813 // the queue. Tell them that they are behind.
814 if (uint32_queue_index < memory_->queue_size()) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700815 VLOG(3) << "Near zero, " << std::hex << uint32_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -0700816 return ReadResult::NOTHING_NEW;
817 } else {
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800818 VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -0700819 return ReadResult::TOO_OLD;
820 }
821 }
822 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700823 VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", "
824 << queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -0700825 break;
826 }
827
Alex Perrycb7da4b2019-08-28 19:35:56 -0700828 // Then read the data out. Copy it all out to be deterministic and so we can
829 // make length be from either end.
Austin Schuh20b2b082019-09-11 20:42:56 -0700830 *monotonic_sent_time = m->header.monotonic_sent_time;
831 *realtime_sent_time = m->header.realtime_sent_time;
Austin Schuhad154822019-12-27 15:45:13 -0800832 if (m->header.remote_queue_index == 0xffffffffu) {
833 *remote_queue_index = queue_index.index();
834 } else {
835 *remote_queue_index = m->header.remote_queue_index;
836 }
837 *monotonic_remote_time = m->header.monotonic_remote_time;
838 *realtime_remote_time = m->header.realtime_remote_time;
Brian Silverman6b8a3c32020-03-06 11:26:14 -0800839 if (data) {
840 memcpy(data, m->data(memory_->message_data_size()), message_data_size());
841 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700842 *length = m->header.length;
843
844 // And finally, confirm that the message *still* points to the queue index we
845 // want. This means it didn't change out from under us.
846 // If something changed out from under us, we were reading it much too late in
847 // it's lifetime.
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800848 aos_compiler_memory_barrier();
Austin Schuh20b2b082019-09-11 20:42:56 -0700849 const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size);
850 if (final_queue_index != queue_index) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700851 VLOG(3) << "Changed out from under us. Reading " << std::hex
852 << queue_index.index() << ", finished with "
853 << final_queue_index.index() << ", delta: " << std::dec
854 << (final_queue_index.index() - queue_index.index());
855 return ReadResult::OVERWROTE;
Austin Schuh20b2b082019-09-11 20:42:56 -0700856 }
857
858 return ReadResult::GOOD;
859}
860
Alex Perrycb7da4b2019-08-28 19:35:56 -0700861size_t LocklessQueue::queue_size() const { return memory_->queue_size(); }
862size_t LocklessQueue::message_data_size() const {
863 return memory_->message_data_size();
864}
865
866QueueIndex LocklessQueue::LatestQueueIndex() {
Austin Schuh20b2b082019-09-11 20:42:56 -0700867 const size_t queue_size = memory_->queue_size();
868
869 // There is only one interesting case. We need to know if the queue is empty.
870 // That is done with a sentinel value. At worst, this will be off by one.
871 const QueueIndex next_queue_index =
872 memory_->next_queue_index.Load(queue_size);
873 if (next_queue_index.valid()) {
874 const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700875 return current_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -0700876 } else {
877 return empty_queue_index();
878 }
879}
880
881namespace {
882
883// Prints out the mutex state. Not safe to use while the mutex is being
884// changed.
885::std::string PrintMutex(aos_mutex *mutex) {
886 ::std::stringstream s;
887 s << "aos_mutex(" << ::std::hex << mutex->futex;
888
889 if (mutex->futex != 0) {
890 s << ":";
891 if (mutex->futex & FUTEX_OWNER_DIED) {
892 s << "FUTEX_OWNER_DIED|";
893 }
894 s << "tid=" << (mutex->futex & FUTEX_TID_MASK);
895 }
896
897 s << ")";
898 return s.str();
899}
900
901} // namespace
902
903void PrintLocklessQueueMemory(LocklessQueueMemory *memory) {
904 const size_t queue_size = memory->queue_size();
905 ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl;
906 ::std::cout << " aos_mutex queue_setup_lock = "
907 << PrintMutex(&memory->queue_setup_lock) << ::std::endl;
Brian Silvermanfafe1fa2019-12-18 21:42:18 -0800908 ::std::cout << " bool initialized = " << memory->initialized << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -0700909 ::std::cout << " config {" << ::std::endl;
910 ::std::cout << " size_t num_watchers = " << memory->config.num_watchers
911 << ::std::endl;
912 ::std::cout << " size_t num_senders = " << memory->config.num_senders
913 << ::std::endl;
914 ::std::cout << " size_t queue_size = " << memory->config.queue_size
915 << ::std::endl;
916 ::std::cout << " size_t message_data_size = "
917 << memory->config.message_data_size << ::std::endl;
918
919 ::std::cout << " AtomicQueueIndex next_queue_index = "
920 << memory->next_queue_index.Load(queue_size).DebugString()
921 << ::std::endl;
922
Austin Schuh3328d132020-02-28 13:54:57 -0800923 ::std::cout << " uid_t uid = " << memory->uid << ::std::endl;
924
Austin Schuh20b2b082019-09-11 20:42:56 -0700925 ::std::cout << " }" << ::std::endl;
926 ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl;
927 for (size_t i = 0; i < queue_size; ++i) {
928 ::std::cout << " [" << i << "] -> "
929 << memory->GetQueue(i)->Load().DebugString() << ::std::endl;
930 }
931 ::std::cout << " }" << ::std::endl;
932 ::std::cout << " Message messages[" << memory->num_messages() << "] {"
933 << ::std::endl;
934 for (size_t i = 0; i < memory->num_messages(); ++i) {
935 Message *m = memory->GetMessage(Index(i, i));
936 ::std::cout << " [" << i << "] -> Message {" << ::std::endl;
937 ::std::cout << " Header {" << ::std::endl;
938 ::std::cout << " AtomicQueueIndex queue_index = "
939 << m->header.queue_index.Load(queue_size).DebugString()
940 << ::std::endl;
941 ::std::cout << " size_t length = " << m->header.length
942 << ::std::endl;
943 ::std::cout << " }" << ::std::endl;
944 ::std::cout << " data: {";
945
Brian Silvermana1652f32020-01-29 20:41:44 -0800946 const char *const m_data = m->data(memory->message_data_size());
Austin Schuh20b2b082019-09-11 20:42:56 -0700947 for (size_t j = 0; j < m->header.length; ++j) {
Brian Silvermana1652f32020-01-29 20:41:44 -0800948 char data = m_data[j];
Austin Schuh20b2b082019-09-11 20:42:56 -0700949 if (j != 0) {
950 ::std::cout << " ";
951 }
952 if (::std::isprint(data)) {
953 ::std::cout << ::std::setfill(' ') << ::std::setw(2) << ::std::hex
954 << data;
955 } else {
956 ::std::cout << "0x" << ::std::setfill('0') << ::std::setw(2)
957 << ::std::hex << (static_cast<unsigned>(data) & 0xff);
958 }
959 }
960 ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl;
961 ::std::cout << " }," << ::std::endl;
962 }
963 ::std::cout << " }" << ::std::endl;
964
Alex Perrycb7da4b2019-08-28 19:35:56 -0700965 ::std::cout << " Sender senders[" << memory->num_senders() << "] {"
966 << ::std::endl;
Austin Schuh20b2b082019-09-11 20:42:56 -0700967 for (size_t i = 0; i < memory->num_senders(); ++i) {
968 Sender *s = memory->GetSender(i);
969 ::std::cout << " [" << i << "] -> Sender {" << ::std::endl;
970 ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid)
971 << ::std::endl;
972 ::std::cout << " AtomicIndex scratch_index = "
973 << s->scratch_index.Load().DebugString() << ::std::endl;
974 ::std::cout << " AtomicIndex to_replace = "
975 << s->to_replace.Load().DebugString() << ::std::endl;
976 ::std::cout << " }" << ::std::endl;
977 }
978 ::std::cout << " }" << ::std::endl;
979
980 ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {"
981 << ::std::endl;
982 for (size_t i = 0; i < memory->num_watchers(); ++i) {
983 Watcher *w = memory->GetWatcher(i);
984 ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl;
985 ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid)
986 << ::std::endl;
987 ::std::cout << " pid_t pid = " << w->pid << ::std::endl;
988 ::std::cout << " int priority = " << w->priority << ::std::endl;
989 ::std::cout << " }" << ::std::endl;
990 }
991 ::std::cout << " }" << ::std::endl;
992
993 ::std::cout << "}" << ::std::endl;
994}
995
996} // namespace ipc_lib
997} // namespace aos