Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #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 Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 12 | #include "aos/ipc_lib/lockless_queue_memory.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "aos/realtime.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 14 | #include "aos/util/compiler_memory_barrier.h" |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 15 | #include "gflags/gflags.h" |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 16 | #include "glog/logging.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 17 | |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 18 | DEFINE_bool(dump_lockless_queue_data, false, |
| 19 | "If true, print the data out when dumping the queue."); |
| 20 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 21 | namespace aos { |
| 22 | namespace ipc_lib { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 23 | namespace { |
| 24 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 25 | class GrabQueueSetupLockOrDie { |
| 26 | public: |
| 27 | GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) : memory_(memory) { |
| 28 | const int result = mutex_grab(&(memory->queue_setup_lock)); |
| 29 | CHECK(result == 0 || result == 1) << ": " << result; |
| 30 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 31 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 32 | ~GrabQueueSetupLockOrDie() { mutex_unlock(&(memory_->queue_setup_lock)); } |
| 33 | |
| 34 | GrabQueueSetupLockOrDie(const GrabQueueSetupLockOrDie &) = delete; |
| 35 | GrabQueueSetupLockOrDie &operator=(const GrabQueueSetupLockOrDie &) = delete; |
| 36 | |
| 37 | private: |
| 38 | LocklessQueueMemory *const memory_; |
| 39 | }; |
| 40 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 41 | bool IsPinned(LocklessQueueMemory *memory, Index index) { |
| 42 | DCHECK(index.valid()); |
| 43 | const size_t queue_size = memory->queue_size(); |
| 44 | const QueueIndex message_index = |
| 45 | memory->GetMessage(index)->header.queue_index.Load(queue_size); |
| 46 | if (!message_index.valid()) { |
| 47 | return false; |
| 48 | } |
| 49 | DCHECK(memory->GetQueue(message_index.Wrapped())->Load() != index) |
| 50 | << ": Message is in the queue"; |
| 51 | for (int pinner_index = 0; |
| 52 | pinner_index < static_cast<int>(memory->config.num_pinners); |
| 53 | ++pinner_index) { |
| 54 | ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index); |
| 55 | |
| 56 | if (pinner->pinned.RelaxedLoad(queue_size) == message_index) { |
| 57 | return true; |
| 58 | } |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // Ensures sender->scratch_index (which must contain to_replace) is not pinned. |
| 64 | // |
| 65 | // Returns the new scratch_index value. |
| 66 | Index SwapPinnedSenderScratch(LocklessQueueMemory *const memory, |
| 67 | ipc_lib::Sender *const sender, |
| 68 | const Index to_replace) { |
| 69 | // If anybody's trying to pin this message, then grab a message from a pinner |
| 70 | // to write into instead, and leave the message we pulled out of the queue |
| 71 | // (currently in our scratch_index) with a pinner. |
| 72 | // |
| 73 | // This loop will terminate in at most one iteration through the pinners in |
| 74 | // any steady-state configuration of the memory. There are only as many |
| 75 | // Pinner::pinned values to worry about as there are Pinner::scratch_index |
| 76 | // values to check against, plus to_replace, which means there will always be |
| 77 | // a free one. We might have to make multiple passes if things are being |
| 78 | // changed concurrently though, but nobody dying can make this loop fail to |
| 79 | // terminate (because the number of processes that can die is bounded, because |
| 80 | // no new ones can start while we've got the lock). |
| 81 | for (int pinner_index = 0; true; |
| 82 | pinner_index = (pinner_index + 1) % memory->config.num_pinners) { |
| 83 | if (!IsPinned(memory, to_replace)) { |
| 84 | // No pinners on our current scratch_index, so we're fine now. |
| 85 | VLOG(3) << "No pinners: " << to_replace.DebugString(); |
| 86 | return to_replace; |
| 87 | } |
| 88 | |
| 89 | ipc_lib::Pinner *const pinner = memory->GetPinner(pinner_index); |
| 90 | |
| 91 | const Index pinner_scratch = pinner->scratch_index.RelaxedLoad(); |
| 92 | CHECK(pinner_scratch.valid()) |
| 93 | << ": Pinner scratch_index should always be valid"; |
| 94 | if (IsPinned(memory, pinner_scratch)) { |
| 95 | // Wouldn't do us any good to swap with this one, so don't bother, and |
| 96 | // move onto the next one. |
| 97 | VLOG(3) << "Also pinned: " << pinner_scratch.DebugString(); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | sender->to_replace.RelaxedStore(pinner_scratch); |
| 102 | aos_compiler_memory_barrier(); |
| 103 | // Give the pinner the message (which is currently in |
| 104 | // sender->scratch_index). |
| 105 | if (!pinner->scratch_index.CompareAndExchangeStrong(pinner_scratch, |
| 106 | to_replace)) { |
| 107 | // Somebody swapped into this pinner before us. The new value is probably |
| 108 | // pinned, so we don't want to look at it again immediately. |
| 109 | VLOG(3) << "Pinner " << pinner_index |
| 110 | << " scratch_index changed: " << pinner_scratch.DebugString() |
| 111 | << ", " << to_replace.DebugString(); |
| 112 | sender->to_replace.RelaxedInvalidate(); |
| 113 | continue; |
| 114 | } |
| 115 | aos_compiler_memory_barrier(); |
| 116 | // Now update the sender's scratch space and record that we succeeded. |
| 117 | sender->scratch_index.Store(pinner_scratch); |
| 118 | aos_compiler_memory_barrier(); |
| 119 | // And then record that we succeeded, but definitely after the above |
| 120 | // store. |
| 121 | sender->to_replace.RelaxedInvalidate(); |
| 122 | VLOG(3) << "Got new scratch message: " << pinner_scratch.DebugString(); |
| 123 | |
| 124 | // If it's in a pinner's scratch_index, it should not be in the queue, which |
| 125 | // means nobody new can pin it for real. However, they can still attempt to |
| 126 | // pin it, which means we can't verify !IsPinned down here. |
| 127 | |
| 128 | return pinner_scratch; |
| 129 | } |
| 130 | } |
| 131 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 132 | // Returns true if it succeeded. Returns false if another sender died in the |
| 133 | // middle. |
| 134 | bool DoCleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 135 | // Make sure we start looking at shared memory fresh right now. We'll handle |
| 136 | // people dying partway through by either cleaning up after them or not, but |
| 137 | // we want to ensure we clean up after anybody who has already died when we |
| 138 | // start. |
| 139 | aos_compiler_memory_barrier(); |
| 140 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 141 | const size_t num_senders = memory->num_senders(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 142 | const size_t num_pinners = memory->num_pinners(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 143 | const size_t queue_size = memory->queue_size(); |
| 144 | const size_t num_messages = memory->num_messages(); |
| 145 | |
| 146 | // There are a large number of crazy cases here for how things can go wrong |
| 147 | // and how we have to recover. They either require us to keep extra track of |
| 148 | // what is going on, slowing down the send path, or require a large number of |
| 149 | // cases. |
| 150 | // |
| 151 | // The solution here is to not over-think it. This is running while not real |
| 152 | // time during construction. It is allowed to be slow. It will also very |
| 153 | // rarely trigger. There is a small uS window where process death is |
| 154 | // ambiguous. |
| 155 | // |
| 156 | // So, build up a list N long, where N is the number of messages. Search |
| 157 | // through the entire queue and the sender list (ignoring any dead senders), |
| 158 | // and mark down which ones we have seen. Once we have seen all the messages |
| 159 | // except the N dead senders, we know which messages are dead. Because the |
| 160 | // queue is active while we do this, it may take a couple of go arounds to see |
| 161 | // everything. |
| 162 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 163 | ::std::vector<bool> need_recovery(num_senders, false); |
| 164 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 165 | // Do the easy case. Find all senders who have died. See if they are either |
| 166 | // consistent already, or if they have copied over to_replace to the scratch |
| 167 | // index, but haven't cleared to_replace. Count them. |
| 168 | size_t valid_senders = 0; |
| 169 | for (size_t i = 0; i < num_senders; ++i) { |
| 170 | Sender *sender = memory->GetSender(i); |
| 171 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 172 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 173 | if (!(tid & FUTEX_OWNER_DIED)) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 174 | // Not dead. |
| 175 | ++valid_senders; |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 176 | continue; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 177 | } |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 178 | VLOG(3) << "Found an easy death for sender " << i; |
| 179 | // We can do a relaxed load here because we're the only person touching |
| 180 | // this sender at this point. |
| 181 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
| 182 | const Index scratch_index = sender->scratch_index.Load(); |
| 183 | |
| 184 | // I find it easiest to think about this in terms of the set of observable |
| 185 | // states. The main code progresses through the following states: |
| 186 | |
| 187 | // 1) scratch_index = xxx |
| 188 | // to_replace = invalid |
| 189 | // This is unambiguous. Already good. |
| 190 | |
| 191 | // 2) scratch_index = xxx |
| 192 | // to_replace = yyy |
| 193 | // Very ambiguous. Is xxx or yyy the correct one? Need to either roll |
| 194 | // this forwards or backwards. |
| 195 | |
| 196 | // 3) scratch_index = yyy |
| 197 | // to_replace = yyy |
| 198 | // We are in the act of moving to_replace to scratch_index, but didn't |
| 199 | // finish. Easy. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 200 | // |
| 201 | // If doing a pinner swap, we've definitely done it. |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 202 | |
| 203 | // 4) scratch_index = yyy |
| 204 | // to_replace = invalid |
| 205 | // Finished, but died. Looks like 1) |
| 206 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 207 | // Swapping with a pinner's scratch_index passes through the same states. |
| 208 | // We just need to ensure the message that ends up in the senders's |
| 209 | // scratch_index isn't pinned, using the same code as sending does. |
| 210 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 211 | // Any cleanup code needs to follow the same set of states to be robust to |
| 212 | // death, so death can be restarted. |
| 213 | |
| 214 | if (!to_replace.valid()) { |
| 215 | // 1) or 4). Make sure we aren't corrupted and declare victory. |
| 216 | CHECK(scratch_index.valid()); |
| 217 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 218 | // If it's in 1) with a pinner, the sender might have a pinned message, |
| 219 | // so fix that. |
| 220 | SwapPinnedSenderScratch(memory, sender, scratch_index); |
| 221 | |
| 222 | // If it's in 4), it may not have completed this step yet. This will |
| 223 | // always be a NOP if it's in 1), verified by a DCHECK. |
| 224 | memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate(); |
| 225 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 226 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 227 | ++valid_senders; |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | // Could be 2) or 3) at this point. |
| 232 | |
| 233 | if (to_replace == scratch_index) { |
| 234 | // 3) for sure. |
| 235 | // Just need to invalidate to_replace to finish. |
| 236 | sender->to_replace.Invalidate(); |
| 237 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 238 | // Make sure to indicate it's an unused message before a sender gets its |
| 239 | // hands on it. |
| 240 | memory->GetMessage(scratch_index)->header.queue_index.RelaxedInvalidate(); |
| 241 | aos_compiler_memory_barrier(); |
| 242 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 243 | // And mark that we succeeded. |
| 244 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 245 | ++valid_senders; |
| 246 | continue; |
| 247 | } |
| 248 | |
| 249 | // Must be 2). Mark it for later. |
| 250 | need_recovery[i] = true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 253 | // Cleaning up pinners is easy. We don't actually have to do anything, but |
| 254 | // invalidating its pinned field might help catch bugs elsewhere trying to |
| 255 | // read it before it's set. |
| 256 | for (size_t i = 0; i < num_pinners; ++i) { |
| 257 | Pinner *const pinner = memory->GetPinner(i); |
| 258 | const uint32_t tid = |
| 259 | __atomic_load_n(&(pinner->tid.futex), __ATOMIC_ACQUIRE); |
| 260 | if (!(tid & FUTEX_OWNER_DIED)) { |
| 261 | continue; |
| 262 | } |
| 263 | pinner->pinned.Invalidate(); |
| 264 | __atomic_store_n(&(pinner->tid.futex), 0, __ATOMIC_RELEASE); |
| 265 | } |
| 266 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 267 | // 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 Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 270 | return true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 273 | VLOG(3) << "Starting hard cleanup"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 274 | |
| 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 Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 282 | Sender *const sender = memory->GetSender(i); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 283 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 284 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 285 | if (tid & FUTEX_OWNER_DIED) { |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 286 | if (!need_recovery[i]) { |
| 287 | return false; |
| 288 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 289 | ++num_missing; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 290 | continue; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 291 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 292 | CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i; |
| 293 | // We can do a relaxed load here because we're the only person touching |
| 294 | // this sender at this point, if it matters. If it's not a dead sender, |
| 295 | // then any message it ever has will eventually be accounted for if we |
| 296 | // make enough tries through the outer loop. |
| 297 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 298 | if (!accounted_for[scratch_index.message_index()]) { |
| 299 | ++num_accounted_for; |
| 300 | } |
| 301 | accounted_for[scratch_index.message_index()] = true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | for (size_t i = 0; i < queue_size; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 305 | // Same logic as above for scratch_index applies here too. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 306 | const Index index = memory->GetQueue(i)->RelaxedLoad(); |
| 307 | if (!accounted_for[index.message_index()]) { |
| 308 | ++num_accounted_for; |
| 309 | } |
| 310 | accounted_for[index.message_index()] = true; |
| 311 | } |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 312 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 313 | for (size_t pinner_index = 0; pinner_index < num_pinners; ++pinner_index) { |
| 314 | // Same logic as above for scratch_index applies here too. |
| 315 | const Index index = |
| 316 | memory->GetPinner(pinner_index)->scratch_index.RelaxedLoad(); |
| 317 | if (!accounted_for[index.message_index()]) { |
| 318 | ++num_accounted_for; |
| 319 | } |
| 320 | accounted_for[index.message_index()] = true; |
| 321 | } |
| 322 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 323 | CHECK_LE(num_accounted_for + num_missing, num_messages); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | while (num_missing != 0) { |
| 327 | const size_t starting_num_missing = num_missing; |
| 328 | for (size_t i = 0; i < num_senders; ++i) { |
| 329 | Sender *sender = memory->GetSender(i); |
| 330 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 331 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 332 | if (!(tid & FUTEX_OWNER_DIED)) { |
| 333 | CHECK(!need_recovery[i]) << ": Somebody else recovered a sender: " << i; |
| 334 | continue; |
| 335 | } |
| 336 | if (!need_recovery[i]) { |
| 337 | return false; |
| 338 | } |
| 339 | // We can do relaxed loads here because we're the only person touching |
| 340 | // this sender at this point. |
| 341 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 342 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 343 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 344 | // Candidate. |
| 345 | if (to_replace.valid()) { |
| 346 | CHECK_LE(to_replace.message_index(), accounted_for.size()); |
| 347 | } |
| 348 | if (scratch_index.valid()) { |
| 349 | CHECK_LE(scratch_index.message_index(), accounted_for.size()); |
| 350 | } |
| 351 | if (!to_replace.valid() || accounted_for[to_replace.message_index()]) { |
| 352 | CHECK(scratch_index.valid()); |
| 353 | VLOG(3) << "Sender " << i |
| 354 | << " died, to_replace is already accounted for"; |
| 355 | // If both are accounted for, we are corrupt... |
| 356 | CHECK(!accounted_for[scratch_index.message_index()]); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 357 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 358 | // to_replace is already accounted for. This means that we didn't |
| 359 | // atomically insert scratch_index into the queue yet. So |
| 360 | // invalidate to_replace. |
| 361 | sender->to_replace.Invalidate(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 362 | // Sender definitely will not have gotten here, so finish for it. |
| 363 | memory->GetMessage(scratch_index) |
| 364 | ->header.queue_index.RelaxedInvalidate(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 365 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 366 | // And then mark this sender clean. |
| 367 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 368 | need_recovery[i] = false; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 369 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 370 | // And account for scratch_index. |
| 371 | accounted_for[scratch_index.message_index()] = true; |
| 372 | --num_missing; |
| 373 | ++num_accounted_for; |
| 374 | } else if (!scratch_index.valid() || |
| 375 | accounted_for[scratch_index.message_index()]) { |
| 376 | VLOG(3) << "Sender " << i |
| 377 | << " died, scratch_index is already accounted for"; |
| 378 | // scratch_index is accounted for. That means we did the insert, |
| 379 | // but didn't record it. |
| 380 | CHECK(to_replace.valid()); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 381 | |
| 382 | // Make sure to indicate it's an unused message before a sender gets its |
| 383 | // hands on it. |
| 384 | memory->GetMessage(to_replace)->header.queue_index.RelaxedInvalidate(); |
| 385 | aos_compiler_memory_barrier(); |
| 386 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 387 | // Finish the transaction. Copy to_replace, then clear it. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 388 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 389 | sender->scratch_index.Store(to_replace); |
| 390 | sender->to_replace.Invalidate(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 391 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 392 | // And then mark this sender clean. |
| 393 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
| 394 | need_recovery[i] = false; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 395 | |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 396 | // And account for to_replace. |
| 397 | accounted_for[to_replace.message_index()] = true; |
| 398 | --num_missing; |
| 399 | ++num_accounted_for; |
| 400 | } else { |
| 401 | VLOG(3) << "Sender " << i << " died, neither is accounted for"; |
| 402 | // Ambiguous. There will be an unambiguous one somewhere that we |
| 403 | // can do first. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | // CHECK that we are making progress. |
| 407 | CHECK_NE(num_missing, starting_num_missing); |
| 408 | } |
Brian Silverman | d5ca8c6 | 2020-08-12 19:51:03 -0700 | [diff] [blame] | 409 | return true; |
| 410 | } |
| 411 | |
| 412 | void Cleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &lock) { |
| 413 | // The number of iterations is bounded here because there are only a finite |
| 414 | // number of senders in existence which could die, and no new ones can be |
| 415 | // created while we're in here holding the lock. |
| 416 | while (!DoCleanup(memory, lock)) { |
| 417 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | // Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target |
| 421 | // thread. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 422 | // TODO(Brian): Do directly in assembly for armhf at least for efficiency. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 423 | int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) { |
| 424 | return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si); |
| 425 | } |
| 426 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 427 | QueueIndex ZeroOrValid(QueueIndex index) { |
| 428 | if (!index.valid()) { |
| 429 | return index.Clear(); |
| 430 | } |
| 431 | return index; |
| 432 | } |
| 433 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 434 | } // namespace |
| 435 | |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 436 | size_t LocklessQueueConfiguration::message_size() const { |
| 437 | // Round up the message size so following data is aligned appropriately. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 438 | return LocklessQueueMemory::AlignmentRoundUp(message_data_size + |
| 439 | (kChannelDataAlignment - 1)) + |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 440 | sizeof(Message); |
| 441 | } |
| 442 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 443 | size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 444 | // Round up the message size so following data is aligned appropriately. |
| 445 | config.message_data_size = |
| 446 | LocklessQueueMemory::AlignmentRoundUp(config.message_data_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 447 | |
| 448 | // As we build up the size, confirm that everything is aligned to the |
| 449 | // alignment requirements of the type. |
| 450 | size_t size = sizeof(LocklessQueueMemory); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 451 | CHECK_EQ(size % alignof(LocklessQueueMemory), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 452 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 453 | CHECK_EQ(size % alignof(AtomicIndex), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 454 | size += LocklessQueueMemory::SizeOfQueue(config); |
| 455 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 456 | CHECK_EQ(size % alignof(Message), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 457 | size += LocklessQueueMemory::SizeOfMessages(config); |
| 458 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 459 | CHECK_EQ(size % alignof(Watcher), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 460 | size += LocklessQueueMemory::SizeOfWatchers(config); |
| 461 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 462 | CHECK_EQ(size % alignof(Sender), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 463 | size += LocklessQueueMemory::SizeOfSenders(config); |
| 464 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 465 | CHECK_EQ(size % alignof(Pinner), 0u); |
| 466 | size += LocklessQueueMemory::SizeOfPinners(config); |
| 467 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 468 | return size; |
| 469 | } |
| 470 | |
| 471 | LocklessQueueMemory *InitializeLocklessQueueMemory( |
| 472 | LocklessQueueMemory *memory, LocklessQueueConfiguration config) { |
| 473 | // Everything should be zero initialized already. So we just need to fill |
| 474 | // everything out properly. |
| 475 | |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 476 | // This is the UID we will use for checking signal-sending permission |
| 477 | // compatibility. |
| 478 | // |
| 479 | // The manpage says: |
| 480 | // For a process to have permission to send a signal, it must either be |
| 481 | // privileged [...], or the real or effective user ID of the sending process |
| 482 | // must equal the real or saved set-user-ID of the target process. |
| 483 | // |
| 484 | // Processes typically initialize a queue in random order as they start up. |
| 485 | // This means we need an algorithm for verifying all processes have |
| 486 | // permissions to send each other signals which gives the same answer no |
| 487 | // matter what order they attach in. We would also like to avoid maintaining a |
| 488 | // shared list of the UIDs of all processes. |
| 489 | // |
| 490 | // To do this while still giving sufficient flexibility for all current use |
| 491 | // cases, we track a single UID for the queue. All processes with a matching |
| 492 | // euid+suid must have this UID. Any processes with distinct euid/suid must |
| 493 | // instead have a matching ruid. This guarantees signals can be sent between |
| 494 | // all processes attached to the queue. |
| 495 | // |
| 496 | // In particular, this allows a process to change only its euid (to interact |
| 497 | // with a queue) while still maintaining privileges via its ruid. However, it |
| 498 | // can only use privileges in ways that do not require changing the euid back, |
| 499 | // because while the euid is different it will not be able to receive signals. |
| 500 | // We can't actually verify that, but we can sanity check that things are |
| 501 | // valid when the queue is initialized. |
| 502 | |
| 503 | uid_t uid; |
| 504 | { |
| 505 | uid_t ruid, euid, suid; |
| 506 | PCHECK(getresuid(&ruid, &euid, &suid) == 0); |
| 507 | // If these are equal, then use them, even if that's different from the real |
| 508 | // UID. This allows processes to keep a real UID of 0 (to have permissions |
| 509 | // to perform system-level changes) while still being able to communicate |
| 510 | // with processes running unprivileged as a distinct user. |
| 511 | if (euid == suid) { |
| 512 | uid = euid; |
| 513 | VLOG(1) << "Using euid==suid " << uid; |
| 514 | } else { |
| 515 | uid = ruid; |
| 516 | VLOG(1) << "Using ruid " << ruid; |
| 517 | } |
| 518 | } |
| 519 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 520 | // Grab the mutex. We don't care if the previous reader died. We are going |
| 521 | // to check everything anyways. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 522 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 523 | |
| 524 | if (!memory->initialized) { |
| 525 | // TODO(austin): Check these for out of bounds. |
| 526 | memory->config.num_watchers = config.num_watchers; |
| 527 | memory->config.num_senders = config.num_senders; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 528 | memory->config.num_pinners = config.num_pinners; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 529 | memory->config.queue_size = config.queue_size; |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 530 | memory->config.message_data_size = config.message_data_size; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 531 | |
| 532 | const size_t num_messages = memory->num_messages(); |
| 533 | // There need to be at most MaxMessages() messages allocated. |
| 534 | CHECK_LE(num_messages, Index::MaxMessages()); |
| 535 | |
| 536 | for (size_t i = 0; i < num_messages; ++i) { |
| 537 | memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i)) |
| 538 | ->header.queue_index.Invalidate(); |
| 539 | } |
| 540 | |
| 541 | for (size_t i = 0; i < memory->queue_size(); ++i) { |
| 542 | // Make the initial counter be the furthest away number. That means that |
| 543 | // index 0 should be 0xffff, 1 should be 0, etc. |
| 544 | memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size()) |
| 545 | .IncrementBy(i) |
| 546 | .DecrementBy(memory->queue_size()), |
| 547 | i)); |
| 548 | } |
| 549 | |
| 550 | memory->next_queue_index.Invalidate(); |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 551 | memory->uid = uid; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 552 | |
| 553 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 554 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 555 | // Nobody else can possibly be touching these because we haven't set |
| 556 | // initialized to true yet. |
| 557 | s->scratch_index.RelaxedStore(Index(0xffff, i + memory->queue_size())); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 558 | s->to_replace.RelaxedInvalidate(); |
| 559 | } |
| 560 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 561 | for (size_t i = 0; i < memory->num_pinners(); ++i) { |
| 562 | ::aos::ipc_lib::Pinner *pinner = memory->GetPinner(i); |
| 563 | // Nobody else can possibly be touching these because we haven't set |
| 564 | // initialized to true yet. |
| 565 | pinner->scratch_index.RelaxedStore( |
| 566 | Index(0xffff, i + memory->num_senders() + memory->queue_size())); |
| 567 | pinner->pinned.Invalidate(); |
| 568 | } |
| 569 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 570 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 571 | // Signal everything is done. This needs to be done last, so if we die, we |
| 572 | // redo initialization. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 573 | memory->initialized = true; |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 574 | } else { |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 575 | CHECK_EQ(uid, memory->uid) << ": UIDs must match for all processes"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 578 | return memory; |
| 579 | } |
| 580 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 581 | void LocklessQueue::Initialize() { |
| 582 | InitializeLocklessQueueMemory(memory_, config_); |
| 583 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 584 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 585 | LocklessQueueWatcher::~LocklessQueueWatcher() { |
| 586 | if (watcher_index_ == -1) { |
| 587 | return; |
| 588 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 589 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 590 | // Since everything is self consistent, all we need to do is make sure nobody |
| 591 | // else is running. Someone dying will get caught in the generic consistency |
| 592 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 593 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 594 | |
| 595 | // Make sure we are registered. |
| 596 | CHECK_NE(watcher_index_, -1); |
| 597 | |
| 598 | // Make sure we still own the slot we are supposed to. |
| 599 | CHECK( |
| 600 | death_notification_is_held(&(memory_->GetWatcher(watcher_index_)->tid))); |
| 601 | |
| 602 | // The act of unlocking invalidates the entry. Invalidate it. |
| 603 | death_notification_release(&(memory_->GetWatcher(watcher_index_)->tid)); |
| 604 | // And internally forget the slot. |
| 605 | watcher_index_ = -1; |
| 606 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 607 | // Cleanup is cheap. The next user will do it anyways, so no need for us to do |
| 608 | // anything right now. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 609 | |
| 610 | // And confirm that nothing is owned by us. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 611 | const int num_watchers = memory_->num_watchers(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 612 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 613 | CHECK(!death_notification_is_held(&(memory_->GetWatcher(i)->tid))) |
| 614 | << ": " << i; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 615 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 618 | std::optional<LocklessQueueWatcher> LocklessQueueWatcher::Make( |
| 619 | LocklessQueue queue, int priority) { |
| 620 | queue.Initialize(); |
| 621 | LocklessQueueWatcher result(queue.memory(), priority); |
| 622 | if (result.watcher_index_ != -1) { |
| 623 | return std::move(result); |
| 624 | } else { |
| 625 | return std::nullopt; |
| 626 | } |
| 627 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 628 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 629 | LocklessQueueWatcher::LocklessQueueWatcher(LocklessQueueMemory *memory, |
| 630 | int priority) |
| 631 | : memory_(memory) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 632 | // TODO(austin): Make sure signal coalescing is turned on. We don't need |
| 633 | // duplicates. That will improve performance under high load. |
| 634 | |
| 635 | // Since everything is self consistent, all we need to do is make sure nobody |
| 636 | // else is running. Someone dying will get caught in the generic consistency |
| 637 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 638 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 639 | const int num_watchers = memory_->num_watchers(); |
| 640 | |
| 641 | // Now, find the first empty watcher and grab it. |
| 642 | CHECK_EQ(watcher_index_, -1); |
| 643 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 644 | // If we see a slot the kernel has marked as dead, everything we do reusing |
| 645 | // it needs to happen-after whatever that process did before dying. |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 646 | auto *const futex = &(memory_->GetWatcher(i)->tid.futex); |
| 647 | const uint32_t tid = __atomic_load_n(futex, __ATOMIC_ACQUIRE); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 648 | if (tid == 0 || (tid & FUTEX_OWNER_DIED)) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 649 | watcher_index_ = i; |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 650 | // Relaxed is OK here because we're the only task going to touch it |
| 651 | // between here and the write in death_notification_init below (other |
| 652 | // recovery is blocked by us holding the setup lock). |
| 653 | __atomic_store_n(futex, 0, __ATOMIC_RELAXED); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 654 | break; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | // Bail if we failed to find an open slot. |
| 659 | if (watcher_index_ == -1) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 660 | return; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 663 | Watcher *const w = memory_->GetWatcher(watcher_index_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 664 | |
| 665 | w->pid = getpid(); |
| 666 | w->priority = priority; |
| 667 | |
| 668 | // Grabbing a mutex is a compiler and memory barrier, so nothing before will |
| 669 | // get rearranged afterwords. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 670 | death_notification_init(&(w->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 673 | LocklessQueueWakeUpper::LocklessQueueWakeUpper(LocklessQueue queue) |
| 674 | : memory_(queue.const_memory()), pid_(getpid()), uid_(getuid()) { |
| 675 | queue.Initialize(); |
| 676 | watcher_copy_.resize(memory_->num_watchers()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 679 | int LocklessQueueWakeUpper::Wakeup(const int current_priority) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 680 | const size_t num_watchers = memory_->num_watchers(); |
| 681 | |
| 682 | CHECK_EQ(watcher_copy_.size(), num_watchers); |
| 683 | |
| 684 | // Grab a copy so it won't change out from underneath us, and we can sort it |
| 685 | // nicely in C++. |
| 686 | // Do note that there is still a window where the process can die *after* we |
| 687 | // read everything. We will still PI boost and send a signal to the thread in |
| 688 | // question. There is no way without pidfd's to close this window, and |
| 689 | // creating a pidfd is likely not RT. |
| 690 | for (size_t i = 0; i < num_watchers; ++i) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 691 | const Watcher *w = memory_->GetWatcher(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 692 | watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_RELAXED); |
| 693 | // Force the load of the TID to come first. |
| 694 | aos_compiler_memory_barrier(); |
| 695 | watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed); |
| 696 | watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 697 | |
| 698 | // Use a priority of -1 to mean an invalid entry to make sorting easier. |
| 699 | if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) { |
| 700 | watcher_copy_[i].priority = -1; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 701 | } else { |
| 702 | // Ensure all of this happens after we're done looking at the pid+priority |
| 703 | // in shared memory. |
| 704 | aos_compiler_memory_barrier(); |
| 705 | if (watcher_copy_[i].tid != static_cast<pid_t>(__atomic_load_n( |
| 706 | &(w->tid.futex), __ATOMIC_RELAXED))) { |
| 707 | // Confirm that the watcher hasn't been re-used and modified while we |
| 708 | // read it. If it has, mark it invalid again. |
| 709 | watcher_copy_[i].priority = -1; |
| 710 | watcher_copy_[i].tid = 0; |
| 711 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | // Now sort. |
| 716 | ::std::sort(watcher_copy_.begin(), watcher_copy_.end(), |
| 717 | [](const WatcherCopy &a, const WatcherCopy &b) { |
| 718 | return a.priority > b.priority; |
| 719 | }); |
| 720 | |
| 721 | int count = 0; |
| 722 | if (watcher_copy_[0].priority != -1) { |
| 723 | const int max_priority = |
| 724 | ::std::max(current_priority, watcher_copy_[0].priority); |
| 725 | // Boost if we are RT and there is a higher priority sender out there. |
| 726 | // Otherwise we might run into priority inversions. |
| 727 | if (max_priority > current_priority && current_priority > 0) { |
| 728 | SetCurrentThreadRealtimePriority(max_priority); |
| 729 | } |
| 730 | |
| 731 | // Build up the siginfo to send. |
| 732 | siginfo_t uinfo; |
| 733 | memset(&uinfo, 0, sizeof(uinfo)); |
| 734 | |
| 735 | uinfo.si_code = SI_QUEUE; |
| 736 | uinfo.si_pid = pid_; |
| 737 | uinfo.si_uid = uid_; |
| 738 | uinfo.si_value.sival_int = 0; |
| 739 | |
| 740 | for (const WatcherCopy &watcher_copy : watcher_copy_) { |
| 741 | // The first -1 priority means we are at the end of the valid list. |
| 742 | if (watcher_copy.priority == -1) { |
| 743 | break; |
| 744 | } |
| 745 | |
| 746 | // Send the signal. Target just the thread that sent it so that we can |
| 747 | // support multiple watchers in a process (when someone creates multiple |
| 748 | // event loops in different threads). |
| 749 | rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal, |
| 750 | &uinfo); |
| 751 | |
| 752 | ++count; |
| 753 | } |
| 754 | |
| 755 | // Drop back down if we were boosted. |
| 756 | if (max_priority > current_priority && current_priority > 0) { |
| 757 | SetCurrentThreadRealtimePriority(current_priority); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | return count; |
| 762 | } |
| 763 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 764 | LocklessQueueSender::LocklessQueueSender(LocklessQueueMemory *memory) |
| 765 | : memory_(memory) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 766 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 767 | |
| 768 | // Since we already have the lock, go ahead and try cleaning up. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 769 | Cleanup(memory_, grab_queue_setup_lock); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 770 | |
| 771 | const int num_senders = memory_->num_senders(); |
| 772 | |
| 773 | for (int i = 0; i < num_senders; ++i) { |
| 774 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 775 | // This doesn't need synchronization because we're the only process doing |
| 776 | // initialization right now, and nobody else will be touching senders which |
| 777 | // we're interested in. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 778 | const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED); |
| 779 | if (tid == 0) { |
| 780 | sender_index_ = i; |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (sender_index_ == -1) { |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 786 | VLOG(1) << "Too many senders, starting to bail."; |
| 787 | return; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 788 | } |
| 789 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 790 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 791 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 792 | // Indicate that we are now alive by taking over the slot. If the previous |
| 793 | // owner died, we still want to do this. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 794 | death_notification_init(&(sender->tid)); |
| 795 | |
| 796 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 797 | Message *const message = memory_->GetMessage(scratch_index); |
| 798 | CHECK(!message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid()) |
| 799 | << ": " << std::hex << scratch_index.get(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 800 | } |
| 801 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 802 | LocklessQueueSender::~LocklessQueueSender() { |
| 803 | if (sender_index_ != -1) { |
| 804 | CHECK(memory_ != nullptr); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 805 | death_notification_release(&(memory_->GetSender(sender_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 809 | std::optional<LocklessQueueSender> LocklessQueueSender::Make( |
| 810 | LocklessQueue queue) { |
| 811 | queue.Initialize(); |
| 812 | LocklessQueueSender result(queue.memory()); |
| 813 | if (result.sender_index_ != -1) { |
| 814 | return std::move(result); |
| 815 | } else { |
| 816 | return std::nullopt; |
| 817 | } |
| 818 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 819 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 820 | size_t LocklessQueueSender::size() const { |
| 821 | return memory_->message_data_size(); |
| 822 | } |
| 823 | |
| 824 | void *LocklessQueueSender::Data() { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 825 | ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 826 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 827 | Message *const message = memory_->GetMessage(scratch_index); |
| 828 | // We should have invalidated this when we first got the buffer. Verify that |
| 829 | // in debug mode. |
| 830 | DCHECK( |
| 831 | !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid()) |
| 832 | << ": " << std::hex << scratch_index.get(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 833 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 834 | return message->data(memory_->message_data_size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 837 | void LocklessQueueSender::Send( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 838 | const char *data, size_t length, |
| 839 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 840 | aos::realtime_clock::time_point realtime_remote_time, |
| 841 | uint32_t remote_queue_index, |
| 842 | aos::monotonic_clock::time_point *monotonic_sent_time, |
| 843 | aos::realtime_clock::time_point *realtime_sent_time, |
| 844 | uint32_t *queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 845 | CHECK_LE(length, size()); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 846 | // Flatbuffers write from the back of the buffer to the front. If we are |
| 847 | // going to write an explicit chunk of memory into the buffer, we need to |
| 848 | // adhere to this convention and place it at the end. |
| 849 | memcpy((reinterpret_cast<char *>(Data()) + size() - length), data, length); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 850 | Send(length, monotonic_remote_time, realtime_remote_time, remote_queue_index, |
| 851 | monotonic_sent_time, realtime_sent_time, queue_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 854 | void LocklessQueueSender::Send( |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 855 | size_t length, aos::monotonic_clock::time_point monotonic_remote_time, |
| 856 | aos::realtime_clock::time_point realtime_remote_time, |
| 857 | uint32_t remote_queue_index, |
| 858 | aos::monotonic_clock::time_point *monotonic_sent_time, |
| 859 | aos::realtime_clock::time_point *realtime_sent_time, |
| 860 | uint32_t *queue_index) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 861 | const size_t queue_size = memory_->queue_size(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 862 | CHECK_LE(length, size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 863 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 864 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
| 865 | // We can do a relaxed load on our sender because we're the only person |
| 866 | // modifying it right now. |
| 867 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 868 | Message *const message = memory_->GetMessage(scratch_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 869 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 870 | // We should have invalidated this when we first got the buffer. Verify that |
| 871 | // in debug mode. |
| 872 | DCHECK( |
| 873 | !message->header.queue_index.RelaxedLoad(memory_->queue_size()).valid()) |
| 874 | << ": " << std::hex << scratch_index.get(); |
| 875 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 876 | message->header.length = length; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 877 | // Pass these through. Any alternative behavior can be implemented out a |
| 878 | // layer. |
| 879 | message->header.remote_queue_index = remote_queue_index; |
| 880 | message->header.monotonic_remote_time = monotonic_remote_time; |
| 881 | message->header.realtime_remote_time = realtime_remote_time; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 882 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 883 | Index to_replace = Index::Invalid(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 884 | while (true) { |
| 885 | const QueueIndex actual_next_queue_index = |
| 886 | memory_->next_queue_index.Load(queue_size); |
| 887 | const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index); |
| 888 | |
| 889 | const QueueIndex incremented_queue_index = next_queue_index.Increment(); |
| 890 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 891 | // This needs to synchronize with whoever the previous writer at this |
| 892 | // location was. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 893 | to_replace = memory_->LoadIndex(next_queue_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 894 | |
| 895 | const QueueIndex decremented_queue_index = |
| 896 | next_queue_index.DecrementBy(queue_size); |
| 897 | |
| 898 | // See if we got beat. If we did, try to atomically update |
| 899 | // next_queue_index in case the previous writer failed and retry. |
| 900 | if (!to_replace.IsPlausible(decremented_queue_index)) { |
| 901 | // We don't care about the result. It will either succeed, or we got |
| 902 | // beat in fixing it and just need to give up and try again. If we got |
| 903 | // beat multiple times, the only way progress can be made is if the queue |
| 904 | // is updated as well. This means that if we retry reading |
| 905 | // next_queue_index, we will be at most off by one and can retry. |
| 906 | // |
| 907 | // Both require no further action from us. |
| 908 | // |
| 909 | // TODO(austin): If we are having fairness issues under contention, we |
| 910 | // could have a mode bit in next_queue_index, and could use a lock or some |
| 911 | // other form of PI boosting to let the higher priority task win. |
| 912 | memory_->next_queue_index.CompareAndExchangeStrong( |
| 913 | actual_next_queue_index, incremented_queue_index); |
| 914 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 915 | VLOG(3) << "We were beat. Try again. Was " << std::hex |
| 916 | << to_replace.get() << ", is " << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 917 | continue; |
| 918 | } |
| 919 | |
| 920 | // Confirm that the message is what it should be. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 921 | // |
| 922 | // This is just a best-effort check to skip reading the clocks if possible. |
| 923 | // If this fails, then the compare-exchange below definitely would, so we |
| 924 | // can bail out now. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 925 | { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 926 | const QueueIndex previous_index = |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 927 | memory_->GetMessage(to_replace) |
| 928 | ->header.queue_index.RelaxedLoad(queue_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 929 | if (previous_index != decremented_queue_index && previous_index.valid()) { |
| 930 | // Retry. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 931 | VLOG(3) << "Something fishy happened, queue index doesn't match. " |
| 932 | "Retrying. Previous index was " |
| 933 | << std::hex << previous_index.index() << ", should be " |
| 934 | << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 935 | continue; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | message->header.monotonic_sent_time = ::aos::monotonic_clock::now(); |
| 940 | message->header.realtime_sent_time = ::aos::realtime_clock::now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 941 | if (monotonic_sent_time != nullptr) { |
| 942 | *monotonic_sent_time = message->header.monotonic_sent_time; |
| 943 | } |
| 944 | if (realtime_sent_time != nullptr) { |
| 945 | *realtime_sent_time = message->header.realtime_sent_time; |
| 946 | } |
| 947 | if (queue_index != nullptr) { |
| 948 | *queue_index = next_queue_index.index(); |
| 949 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 950 | |
| 951 | // Before we are fully done filling out the message, update the Sender state |
| 952 | // with the new index to write. This re-uses the barrier for the |
| 953 | // queue_index store. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 954 | const Index index_to_write(next_queue_index, scratch_index.message_index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 955 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 956 | aos_compiler_memory_barrier(); |
| 957 | // We're the only person who cares about our scratch index, besides somebody |
| 958 | // cleaning up after us. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 959 | sender->scratch_index.RelaxedStore(index_to_write); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 960 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 961 | |
| 962 | message->header.queue_index.Store(next_queue_index); |
| 963 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 964 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 965 | // The message is now filled out, and we have a confirmed slot to store |
| 966 | // into. |
| 967 | // |
| 968 | // Start by writing down what we are going to pull out of the queue. This |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 969 | // was Invalid before now. Only person who will read this is whoever cleans |
| 970 | // up after us, so no synchronization necessary. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 971 | sender->to_replace.RelaxedStore(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 972 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 973 | |
| 974 | // Then exchange the next index into the queue. |
| 975 | if (!memory_->GetQueue(next_queue_index.Wrapped()) |
| 976 | ->CompareAndExchangeStrong(to_replace, index_to_write)) { |
| 977 | // Aw, didn't succeed. Retry. |
| 978 | sender->to_replace.RelaxedInvalidate(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 979 | aos_compiler_memory_barrier(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 980 | VLOG(3) << "Failed to wrap into queue"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 981 | continue; |
| 982 | } |
| 983 | |
| 984 | // Then update next_queue_index to save the next user some computation time. |
| 985 | memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index, |
| 986 | incremented_queue_index); |
| 987 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 988 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 989 | // Now update the scratch space and record that we succeeded. |
| 990 | sender->scratch_index.Store(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 991 | aos_compiler_memory_barrier(); |
| 992 | // And then record that we succeeded, but definitely after the above store. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 993 | sender->to_replace.RelaxedInvalidate(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 994 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 995 | break; |
| 996 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 997 | |
| 998 | // to_replace is our current scratch_index. It isn't in the queue, which means |
| 999 | // nobody new can pin it. They can set their `pinned` to it, but they will |
| 1000 | // back it out, so they don't count. This means that we just need to find a |
| 1001 | // message for which no pinner had it in `pinned`, and then we know this |
| 1002 | // message will never be pinned. We'll start with to_replace, and if that is |
| 1003 | // pinned then we'll look for a new one to use instead. |
| 1004 | const Index new_scratch = |
| 1005 | SwapPinnedSenderScratch(memory_, sender, to_replace); |
| 1006 | |
| 1007 | // If anybody is looking at this message (they shouldn't be), then try telling |
| 1008 | // them about it (best-effort). |
| 1009 | memory_->GetMessage(new_scratch)->header.queue_index.RelaxedInvalidate(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1012 | int LocklessQueueSender::buffer_index() const { |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 1013 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
| 1014 | // We can do a relaxed load on our sender because we're the only person |
| 1015 | // modifying it right now. |
| 1016 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 1017 | return scratch_index.message_index(); |
| 1018 | } |
| 1019 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1020 | LocklessQueuePinner::LocklessQueuePinner( |
| 1021 | LocklessQueueMemory *memory, const LocklessQueueMemory *const_memory) |
| 1022 | : memory_(memory), const_memory_(const_memory) { |
| 1023 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
| 1024 | |
| 1025 | // Since we already have the lock, go ahead and try cleaning up. |
| 1026 | Cleanup(memory_, grab_queue_setup_lock); |
| 1027 | |
| 1028 | const int num_pinners = memory_->num_pinners(); |
| 1029 | |
| 1030 | for (int i = 0; i < num_pinners; ++i) { |
| 1031 | ::aos::ipc_lib::Pinner *p = memory->GetPinner(i); |
| 1032 | // This doesn't need synchronization because we're the only process doing |
| 1033 | // initialization right now, and nobody else will be touching pinners which |
| 1034 | // we're interested in. |
| 1035 | const uint32_t tid = __atomic_load_n(&(p->tid.futex), __ATOMIC_RELAXED); |
| 1036 | if (tid == 0) { |
| 1037 | pinner_index_ = i; |
| 1038 | break; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | if (pinner_index_ == -1) { |
| 1043 | VLOG(1) << "Too many pinners, starting to bail."; |
| 1044 | return; |
| 1045 | } |
| 1046 | |
| 1047 | ::aos::ipc_lib::Pinner *p = memory_->GetPinner(pinner_index_); |
| 1048 | p->pinned.Invalidate(); |
| 1049 | |
| 1050 | // Indicate that we are now alive by taking over the slot. If the previous |
| 1051 | // owner died, we still want to do this. |
| 1052 | death_notification_init(&(p->tid)); |
| 1053 | } |
| 1054 | |
| 1055 | LocklessQueuePinner::~LocklessQueuePinner() { |
| 1056 | if (pinner_index_ != -1) { |
| 1057 | CHECK(memory_ != nullptr); |
| 1058 | memory_->GetPinner(pinner_index_)->pinned.Invalidate(); |
| 1059 | aos_compiler_memory_barrier(); |
| 1060 | death_notification_release(&(memory_->GetPinner(pinner_index_)->tid)); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | std::optional<LocklessQueuePinner> LocklessQueuePinner::Make( |
| 1065 | LocklessQueue queue) { |
| 1066 | queue.Initialize(); |
| 1067 | LocklessQueuePinner result(queue.memory(), queue.const_memory()); |
| 1068 | if (result.pinner_index_ != -1) { |
| 1069 | return std::move(result); |
| 1070 | } else { |
| 1071 | return std::nullopt; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | // This method doesn't mess with any scratch_index, so it doesn't have to worry |
| 1076 | // about message ownership. |
| 1077 | int LocklessQueuePinner::PinIndex(uint32_t uint32_queue_index) { |
| 1078 | const size_t queue_size = memory_->queue_size(); |
| 1079 | const QueueIndex queue_index = |
| 1080 | QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index); |
| 1081 | ipc_lib::Pinner *const pinner = memory_->GetPinner(pinner_index_); |
| 1082 | |
| 1083 | AtomicIndex *const queue_slot = memory_->GetQueue(queue_index.Wrapped()); |
| 1084 | |
| 1085 | // Indicate that we want to pin this message. |
| 1086 | pinner->pinned.Store(queue_index); |
| 1087 | aos_compiler_memory_barrier(); |
| 1088 | |
| 1089 | { |
| 1090 | const Index message_index = queue_slot->Load(); |
| 1091 | Message *const message = memory_->GetMessage(message_index); |
| 1092 | |
| 1093 | const QueueIndex message_queue_index = |
| 1094 | message->header.queue_index.Load(queue_size); |
| 1095 | if (message_queue_index == queue_index) { |
| 1096 | VLOG(3) << "Eq: " << std::hex << message_queue_index.index(); |
| 1097 | aos_compiler_memory_barrier(); |
| 1098 | return message_index.message_index(); |
| 1099 | } |
| 1100 | VLOG(3) << "Message reused: " << std::hex << message_queue_index.index() |
| 1101 | << ", " << queue_index.index(); |
| 1102 | } |
| 1103 | |
| 1104 | // Being down here means we asked to pin a message before realizing it's no |
| 1105 | // longer in the queue, so back that out now. |
| 1106 | pinner->pinned.Invalidate(); |
| 1107 | VLOG(3) << "Unpinned: " << std::hex << queue_index.index(); |
| 1108 | return -1; |
| 1109 | } |
| 1110 | |
| 1111 | size_t LocklessQueuePinner::size() const { |
| 1112 | return const_memory_->message_data_size(); |
| 1113 | } |
| 1114 | |
| 1115 | const void *LocklessQueuePinner::Data() const { |
| 1116 | const size_t queue_size = const_memory_->queue_size(); |
| 1117 | const ::aos::ipc_lib::Pinner *const pinner = |
| 1118 | const_memory_->GetPinner(pinner_index_); |
| 1119 | QueueIndex pinned = pinner->pinned.RelaxedLoad(queue_size); |
| 1120 | CHECK(pinned.valid()); |
| 1121 | const Message *message = const_memory_->GetMessage(pinned); |
| 1122 | |
| 1123 | return message->data(const_memory_->message_data_size()); |
| 1124 | } |
| 1125 | |
| 1126 | LocklessQueueReader::Result LocklessQueueReader::Read( |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1127 | uint32_t uint32_queue_index, |
| 1128 | ::aos::monotonic_clock::time_point *monotonic_sent_time, |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1129 | ::aos::realtime_clock::time_point *realtime_sent_time, |
| 1130 | ::aos::monotonic_clock::time_point *monotonic_remote_time, |
| 1131 | ::aos::realtime_clock::time_point *realtime_remote_time, |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1132 | uint32_t *remote_queue_index, size_t *length, char *data) const { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1133 | const size_t queue_size = memory_->queue_size(); |
| 1134 | |
| 1135 | // Build up the QueueIndex. |
| 1136 | const QueueIndex queue_index = |
| 1137 | QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index); |
| 1138 | |
| 1139 | // Read the message stored at the requested location. |
| 1140 | Index mi = memory_->LoadIndex(queue_index); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1141 | const Message *m = memory_->GetMessage(mi); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1142 | |
| 1143 | while (true) { |
| 1144 | // We need to confirm that the data doesn't change while we are reading it. |
| 1145 | // Do that by first confirming that the message points to the queue index we |
| 1146 | // want. |
| 1147 | const QueueIndex starting_queue_index = |
| 1148 | m->header.queue_index.Load(queue_size); |
| 1149 | if (starting_queue_index != queue_index) { |
| 1150 | // If we found a message that is exactly 1 loop old, we just wrapped. |
| 1151 | if (starting_queue_index == queue_index.DecrementBy(queue_size)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1152 | VLOG(3) << "Matches: " << std::hex << starting_queue_index.index() |
| 1153 | << ", " << queue_index.DecrementBy(queue_size).index(); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1154 | return Result::NOTHING_NEW; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | // Someone has re-used this message between when we pulled it out of the |
| 1158 | // queue and when we grabbed its index. It is pretty hard to deduce |
| 1159 | // what happened. Just try again. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1160 | const Message *const new_m = memory_->GetMessage(queue_index); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1161 | if (m != new_m) { |
| 1162 | m = new_m; |
| 1163 | VLOG(3) << "Retrying, m doesn't match"; |
| 1164 | continue; |
| 1165 | } |
| 1166 | |
| 1167 | // We have confirmed that message still points to the same message. This |
| 1168 | // means that the message didn't get swapped out from under us, so |
| 1169 | // starting_queue_index is correct. |
| 1170 | // |
| 1171 | // Either we got too far behind (signaled by this being a valid |
| 1172 | // message), or this is one of the initial messages which are invalid. |
| 1173 | if (starting_queue_index.valid()) { |
| 1174 | VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index() |
| 1175 | << ", got " << starting_queue_index.index() << ", behind by " |
| 1176 | << std::dec |
| 1177 | << (starting_queue_index.index() - queue_index.index()); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1178 | return Result::TOO_OLD; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | VLOG(3) << "Initial"; |
| 1182 | |
| 1183 | // There isn't a valid message at this location. |
| 1184 | // |
| 1185 | // If someone asks for one of the messages within the first go around, |
| 1186 | // then they need to wait. They got ahead. Otherwise, they are |
| 1187 | // asking for something crazy, like something before the beginning of |
| 1188 | // the queue. Tell them that they are behind. |
| 1189 | if (uint32_queue_index < memory_->queue_size()) { |
| 1190 | VLOG(3) << "Near zero, " << std::hex << uint32_queue_index; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1191 | return Result::NOTHING_NEW; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1192 | } else { |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1193 | VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1194 | return Result::TOO_OLD; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1195 | } |
| 1196 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1197 | VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", " |
| 1198 | << queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1199 | break; |
| 1200 | } |
| 1201 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1202 | // Then read the data out. Copy it all out to be deterministic and so we can |
| 1203 | // make length be from either end. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1204 | *monotonic_sent_time = m->header.monotonic_sent_time; |
| 1205 | *realtime_sent_time = m->header.realtime_sent_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 1206 | if (m->header.remote_queue_index == 0xffffffffu) { |
| 1207 | *remote_queue_index = queue_index.index(); |
| 1208 | } else { |
| 1209 | *remote_queue_index = m->header.remote_queue_index; |
| 1210 | } |
| 1211 | *monotonic_remote_time = m->header.monotonic_remote_time; |
| 1212 | *realtime_remote_time = m->header.realtime_remote_time; |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 1213 | if (data) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1214 | memcpy(data, m->data(memory_->message_data_size()), |
| 1215 | memory_->message_data_size()); |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 1216 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1217 | *length = m->header.length; |
| 1218 | |
| 1219 | // And finally, confirm that the message *still* points to the queue index we |
| 1220 | // want. This means it didn't change out from under us. |
| 1221 | // If something changed out from under us, we were reading it much too late in |
| 1222 | // it's lifetime. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1223 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1224 | const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size); |
| 1225 | if (final_queue_index != queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1226 | VLOG(3) << "Changed out from under us. Reading " << std::hex |
| 1227 | << queue_index.index() << ", finished with " |
| 1228 | << final_queue_index.index() << ", delta: " << std::dec |
| 1229 | << (final_queue_index.index() - queue_index.index()); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1230 | return Result::OVERWROTE; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1233 | return Result::GOOD; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1236 | QueueIndex LocklessQueueReader::LatestIndex() const { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1237 | const size_t queue_size = memory_->queue_size(); |
| 1238 | |
| 1239 | // There is only one interesting case. We need to know if the queue is empty. |
| 1240 | // That is done with a sentinel value. At worst, this will be off by one. |
| 1241 | const QueueIndex next_queue_index = |
| 1242 | memory_->next_queue_index.Load(queue_size); |
| 1243 | if (next_queue_index.valid()) { |
| 1244 | const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1245 | return current_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1246 | } |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame^] | 1247 | return QueueIndex::Invalid(); |
| 1248 | } |
| 1249 | |
| 1250 | size_t LocklessQueueSize(const LocklessQueueMemory *memory) { |
| 1251 | return memory->queue_size(); |
| 1252 | } |
| 1253 | |
| 1254 | size_t LocklessQueueMessageDataSize(const LocklessQueueMemory *memory) { |
| 1255 | return memory->message_data_size(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | namespace { |
| 1259 | |
| 1260 | // Prints out the mutex state. Not safe to use while the mutex is being |
| 1261 | // changed. |
| 1262 | ::std::string PrintMutex(aos_mutex *mutex) { |
| 1263 | ::std::stringstream s; |
| 1264 | s << "aos_mutex(" << ::std::hex << mutex->futex; |
| 1265 | |
| 1266 | if (mutex->futex != 0) { |
| 1267 | s << ":"; |
| 1268 | if (mutex->futex & FUTEX_OWNER_DIED) { |
| 1269 | s << "FUTEX_OWNER_DIED|"; |
| 1270 | } |
| 1271 | s << "tid=" << (mutex->futex & FUTEX_TID_MASK); |
| 1272 | } |
| 1273 | |
| 1274 | s << ")"; |
| 1275 | return s.str(); |
| 1276 | } |
| 1277 | |
| 1278 | } // namespace |
| 1279 | |
| 1280 | void PrintLocklessQueueMemory(LocklessQueueMemory *memory) { |
| 1281 | const size_t queue_size = memory->queue_size(); |
| 1282 | ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl; |
| 1283 | ::std::cout << " aos_mutex queue_setup_lock = " |
| 1284 | << PrintMutex(&memory->queue_setup_lock) << ::std::endl; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 1285 | ::std::cout << " bool initialized = " << memory->initialized << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1286 | ::std::cout << " config {" << ::std::endl; |
| 1287 | ::std::cout << " size_t num_watchers = " << memory->config.num_watchers |
| 1288 | << ::std::endl; |
| 1289 | ::std::cout << " size_t num_senders = " << memory->config.num_senders |
| 1290 | << ::std::endl; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1291 | ::std::cout << " size_t num_pinners = " << memory->config.num_pinners |
| 1292 | << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1293 | ::std::cout << " size_t queue_size = " << memory->config.queue_size |
| 1294 | << ::std::endl; |
| 1295 | ::std::cout << " size_t message_data_size = " |
| 1296 | << memory->config.message_data_size << ::std::endl; |
| 1297 | |
| 1298 | ::std::cout << " AtomicQueueIndex next_queue_index = " |
| 1299 | << memory->next_queue_index.Load(queue_size).DebugString() |
| 1300 | << ::std::endl; |
| 1301 | |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 1302 | ::std::cout << " uid_t uid = " << memory->uid << ::std::endl; |
| 1303 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1304 | ::std::cout << " }" << ::std::endl; |
| 1305 | ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl; |
| 1306 | for (size_t i = 0; i < queue_size; ++i) { |
| 1307 | ::std::cout << " [" << i << "] -> " |
| 1308 | << memory->GetQueue(i)->Load().DebugString() << ::std::endl; |
| 1309 | } |
| 1310 | ::std::cout << " }" << ::std::endl; |
| 1311 | ::std::cout << " Message messages[" << memory->num_messages() << "] {" |
| 1312 | << ::std::endl; |
| 1313 | for (size_t i = 0; i < memory->num_messages(); ++i) { |
| 1314 | Message *m = memory->GetMessage(Index(i, i)); |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 1315 | ::std::cout << " [" << i << "] -> Message 0x" << std::hex |
| 1316 | << (reinterpret_cast<uintptr_t>( |
| 1317 | memory->GetMessage(Index(i, i))) - |
| 1318 | reinterpret_cast<uintptr_t>(memory)) |
| 1319 | << std::dec << " {" << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1320 | ::std::cout << " Header {" << ::std::endl; |
| 1321 | ::std::cout << " AtomicQueueIndex queue_index = " |
| 1322 | << m->header.queue_index.Load(queue_size).DebugString() |
| 1323 | << ::std::endl; |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 1324 | ::std::cout << " monotonic_clock::time_point monotonic_sent_time = " |
| 1325 | << m->header.monotonic_sent_time << " 0x" << std::hex |
| 1326 | << m->header.monotonic_sent_time.time_since_epoch().count() |
| 1327 | << std::dec << ::std::endl; |
| 1328 | ::std::cout << " realtime_clock::time_point realtime_sent_time = " |
| 1329 | << m->header.realtime_sent_time << " 0x" << std::hex |
| 1330 | << m->header.realtime_sent_time.time_since_epoch().count() |
| 1331 | << std::dec << ::std::endl; |
| 1332 | ::std::cout |
| 1333 | << " monotonic_clock::time_point monotonic_remote_time = " |
| 1334 | << m->header.monotonic_remote_time << " 0x" << std::hex |
| 1335 | << m->header.monotonic_remote_time.time_since_epoch().count() |
| 1336 | << std::dec << ::std::endl; |
| 1337 | ::std::cout << " realtime_clock::time_point realtime_remote_time = " |
| 1338 | << m->header.realtime_remote_time << " 0x" << std::hex |
| 1339 | << m->header.realtime_remote_time.time_since_epoch().count() |
| 1340 | << std::dec << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1341 | ::std::cout << " size_t length = " << m->header.length |
| 1342 | << ::std::endl; |
| 1343 | ::std::cout << " }" << ::std::endl; |
| 1344 | ::std::cout << " data: {"; |
| 1345 | |
Brian Silverman | 001f24d | 2020-08-12 19:33:20 -0700 | [diff] [blame] | 1346 | if (FLAGS_dump_lockless_queue_data) { |
| 1347 | const char *const m_data = m->data(memory->message_data_size()); |
| 1348 | for (size_t j = 0; j < m->header.length; ++j) { |
| 1349 | char data = m_data[j]; |
| 1350 | if (j != 0) { |
| 1351 | ::std::cout << " "; |
| 1352 | } |
| 1353 | if (::std::isprint(data)) { |
| 1354 | ::std::cout << ::std::setfill(' ') << ::std::setw(2) << ::std::hex |
| 1355 | << data; |
| 1356 | } else { |
| 1357 | ::std::cout << "0x" << ::std::setfill('0') << ::std::setw(2) |
| 1358 | << ::std::hex << (static_cast<unsigned>(data) & 0xff); |
| 1359 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1360 | } |
| 1361 | } |
| 1362 | ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl; |
| 1363 | ::std::cout << " }," << ::std::endl; |
| 1364 | } |
| 1365 | ::std::cout << " }" << ::std::endl; |
| 1366 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1367 | ::std::cout << " Sender senders[" << memory->num_senders() << "] {" |
| 1368 | << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1369 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 1370 | Sender *s = memory->GetSender(i); |
| 1371 | ::std::cout << " [" << i << "] -> Sender {" << ::std::endl; |
| 1372 | ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid) |
| 1373 | << ::std::endl; |
| 1374 | ::std::cout << " AtomicIndex scratch_index = " |
| 1375 | << s->scratch_index.Load().DebugString() << ::std::endl; |
| 1376 | ::std::cout << " AtomicIndex to_replace = " |
| 1377 | << s->to_replace.Load().DebugString() << ::std::endl; |
| 1378 | ::std::cout << " }" << ::std::endl; |
| 1379 | } |
| 1380 | ::std::cout << " }" << ::std::endl; |
| 1381 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 1382 | ::std::cout << " Pinner pinners[" << memory->num_pinners() << "] {" |
| 1383 | << ::std::endl; |
| 1384 | for (size_t i = 0; i < memory->num_pinners(); ++i) { |
| 1385 | Pinner *p = memory->GetPinner(i); |
| 1386 | ::std::cout << " [" << i << "] -> Pinner {" << ::std::endl; |
| 1387 | ::std::cout << " aos_mutex tid = " << PrintMutex(&p->tid) |
| 1388 | << ::std::endl; |
| 1389 | ::std::cout << " AtomicIndex scratch_index = " |
| 1390 | << p->scratch_index.Load().DebugString() << ::std::endl; |
| 1391 | ::std::cout << " AtomicIndex pinned = " |
| 1392 | << p->pinned.Load(memory->queue_size()).DebugString() |
| 1393 | << ::std::endl; |
| 1394 | ::std::cout << " }" << ::std::endl; |
| 1395 | } |
| 1396 | ::std::cout << " }" << ::std::endl; |
| 1397 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1398 | ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {" |
| 1399 | << ::std::endl; |
| 1400 | for (size_t i = 0; i < memory->num_watchers(); ++i) { |
| 1401 | Watcher *w = memory->GetWatcher(i); |
| 1402 | ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl; |
| 1403 | ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid) |
| 1404 | << ::std::endl; |
| 1405 | ::std::cout << " pid_t pid = " << w->pid << ::std::endl; |
| 1406 | ::std::cout << " int priority = " << w->priority << ::std::endl; |
| 1407 | ::std::cout << " }" << ::std::endl; |
| 1408 | } |
| 1409 | ::std::cout << " }" << ::std::endl; |
| 1410 | |
| 1411 | ::std::cout << "}" << ::std::endl; |
| 1412 | } |
| 1413 | |
| 1414 | } // namespace ipc_lib |
| 1415 | } // namespace aos |