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