Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #include "aos/ipc_lib/lockless_queue.h" |
| 2 | |
| 3 | #include <linux/futex.h> |
| 4 | #include <sys/types.h> |
| 5 | #include <syscall.h> |
| 6 | #include <unistd.h> |
| 7 | #include <algorithm> |
| 8 | #include <iomanip> |
| 9 | #include <iostream> |
| 10 | #include <sstream> |
| 11 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 12 | #include "aos/ipc_lib/lockless_queue_memory.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "aos/realtime.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 14 | #include "aos/util/compiler_memory_barrier.h" |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 15 | #include "glog/logging.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 16 | |
| 17 | namespace aos { |
| 18 | namespace ipc_lib { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 21 | class GrabQueueSetupLockOrDie { |
| 22 | public: |
| 23 | GrabQueueSetupLockOrDie(LocklessQueueMemory *memory) : memory_(memory) { |
| 24 | const int result = mutex_grab(&(memory->queue_setup_lock)); |
| 25 | CHECK(result == 0 || result == 1) << ": " << result; |
| 26 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 27 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 28 | ~GrabQueueSetupLockOrDie() { mutex_unlock(&(memory_->queue_setup_lock)); } |
| 29 | |
| 30 | GrabQueueSetupLockOrDie(const GrabQueueSetupLockOrDie &) = delete; |
| 31 | GrabQueueSetupLockOrDie &operator=(const GrabQueueSetupLockOrDie &) = delete; |
| 32 | |
| 33 | private: |
| 34 | LocklessQueueMemory *const memory_; |
| 35 | }; |
| 36 | |
| 37 | void Cleanup(LocklessQueueMemory *memory, const GrabQueueSetupLockOrDie &) { |
| 38 | // Make sure we start looking at shared memory fresh right now. We'll handle |
| 39 | // people dying partway through by either cleaning up after them or not, but |
| 40 | // we want to ensure we clean up after anybody who has already died when we |
| 41 | // start. |
| 42 | aos_compiler_memory_barrier(); |
| 43 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 44 | const size_t num_senders = memory->num_senders(); |
| 45 | const size_t queue_size = memory->queue_size(); |
| 46 | const size_t num_messages = memory->num_messages(); |
| 47 | |
| 48 | // There are a large number of crazy cases here for how things can go wrong |
| 49 | // and how we have to recover. They either require us to keep extra track of |
| 50 | // what is going on, slowing down the send path, or require a large number of |
| 51 | // cases. |
| 52 | // |
| 53 | // The solution here is to not over-think it. This is running while not real |
| 54 | // time during construction. It is allowed to be slow. It will also very |
| 55 | // rarely trigger. There is a small uS window where process death is |
| 56 | // ambiguous. |
| 57 | // |
| 58 | // So, build up a list N long, where N is the number of messages. Search |
| 59 | // through the entire queue and the sender list (ignoring any dead senders), |
| 60 | // and mark down which ones we have seen. Once we have seen all the messages |
| 61 | // except the N dead senders, we know which messages are dead. Because the |
| 62 | // queue is active while we do this, it may take a couple of go arounds to see |
| 63 | // everything. |
| 64 | |
| 65 | // Do the easy case. Find all senders who have died. See if they are either |
| 66 | // consistent already, or if they have copied over to_replace to the scratch |
| 67 | // index, but haven't cleared to_replace. Count them. |
| 68 | size_t valid_senders = 0; |
| 69 | for (size_t i = 0; i < num_senders; ++i) { |
| 70 | Sender *sender = memory->GetSender(i); |
| 71 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 72 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 73 | if (tid & FUTEX_OWNER_DIED) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | VLOG(3) << "Found an easy death for sender " << i; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 75 | // We can do a relaxed load here because we're the only person touching |
| 76 | // this sender at this point. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 77 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
| 78 | const Index scratch_index = sender->scratch_index.Load(); |
| 79 | |
| 80 | // I find it easiest to think about this in terms of the set of observable |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 81 | // states. The main code progresses through the following states: |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 82 | |
| 83 | // 1) scratch_index = xxx |
| 84 | // to_replace = invalid |
| 85 | // This is unambiguous. Already good. |
| 86 | |
| 87 | // 2) scratch_index = xxx |
| 88 | // to_replace = yyy |
| 89 | // Very ambiguous. Is xxx or yyy the correct one? Need to either roll |
| 90 | // this forwards or backwards. |
| 91 | |
| 92 | // 3) scratch_index = yyy |
| 93 | // to_replace = yyy |
| 94 | // We are in the act of moving to_replace to scratch_index, but didn't |
| 95 | // finish. Easy. |
| 96 | |
| 97 | // 4) scratch_index = yyy |
| 98 | // to_replace = invalid |
| 99 | // Finished, but died. Looks like 1) |
| 100 | |
| 101 | // Any cleanup code needs to follow the same set of states to be robust to |
| 102 | // death, so death can be restarted. |
| 103 | |
| 104 | // Could be 2) or 3). |
| 105 | if (to_replace.valid()) { |
| 106 | // 3) |
| 107 | if (to_replace == scratch_index) { |
| 108 | // Just need to invalidate to_replace to finish. |
| 109 | sender->to_replace.Invalidate(); |
| 110 | |
| 111 | // And mark that we succeeded. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 112 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 113 | ++valid_senders; |
| 114 | } |
| 115 | } else { |
| 116 | // 1) or 4). Make sure we aren't corrupted and declare victory. |
| 117 | CHECK(scratch_index.valid()); |
| 118 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 119 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 120 | ++valid_senders; |
| 121 | } |
| 122 | } else { |
| 123 | // Not dead. |
| 124 | ++valid_senders; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // If all the senders are (or were made) good, there is no need to do the hard |
| 129 | // case. |
| 130 | if (valid_senders == num_senders) { |
| 131 | return; |
| 132 | } |
| 133 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 134 | VLOG(3) << "Starting hard cleanup"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 135 | |
| 136 | size_t num_accounted_for = 0; |
| 137 | size_t num_missing = 0; |
| 138 | ::std::vector<bool> accounted_for(num_messages, false); |
| 139 | |
| 140 | while ((num_accounted_for + num_missing) != num_messages) { |
| 141 | num_missing = 0; |
| 142 | for (size_t i = 0; i < num_senders; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 143 | Sender *const sender = memory->GetSender(i); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 144 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 145 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 146 | if (tid & FUTEX_OWNER_DIED) { |
| 147 | ++num_missing; |
| 148 | } else { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 149 | // We can do a relaxed load here because we're the only person touching |
| 150 | // this sender at this point, if it matters. If it's not a dead sender, |
| 151 | // then any message it every has will already be accounted for, so this |
| 152 | // will always be a NOP. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 153 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 154 | if (!accounted_for[scratch_index.message_index()]) { |
| 155 | ++num_accounted_for; |
| 156 | } |
| 157 | accounted_for[scratch_index.message_index()] = true; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | for (size_t i = 0; i < queue_size; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 162 | // Same logic as above for scratch_index applies here too. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 163 | const Index index = memory->GetQueue(i)->RelaxedLoad(); |
| 164 | if (!accounted_for[index.message_index()]) { |
| 165 | ++num_accounted_for; |
| 166 | } |
| 167 | accounted_for[index.message_index()] = true; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | while (num_missing != 0) { |
| 172 | const size_t starting_num_missing = num_missing; |
| 173 | for (size_t i = 0; i < num_senders; ++i) { |
| 174 | Sender *sender = memory->GetSender(i); |
| 175 | const uint32_t tid = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 176 | __atomic_load_n(&(sender->tid.futex), __ATOMIC_ACQUIRE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 177 | if (tid & FUTEX_OWNER_DIED) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 178 | // We can do relaxed loads here because we're the only person touching |
| 179 | // this sender at this point. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 180 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 181 | const Index to_replace = sender->to_replace.RelaxedLoad(); |
| 182 | |
| 183 | // Candidate. |
| 184 | CHECK_LE(to_replace.message_index(), accounted_for.size()); |
| 185 | if (accounted_for[to_replace.message_index()]) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 186 | VLOG(3) << "Sender " << i |
| 187 | << " died, to_replace is already accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 188 | // If both are accounted for, we are corrupt... |
| 189 | CHECK(!accounted_for[scratch_index.message_index()]); |
| 190 | |
| 191 | // to_replace is already accounted for. This means that we didn't |
| 192 | // atomically insert scratch_index into the queue yet. So |
| 193 | // invalidate to_replace. |
| 194 | sender->to_replace.Invalidate(); |
| 195 | |
| 196 | // And then mark this sender clean. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 197 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 198 | |
| 199 | // And account for scratch_index. |
| 200 | accounted_for[scratch_index.message_index()] = true; |
| 201 | --num_missing; |
| 202 | ++num_accounted_for; |
| 203 | } else if (accounted_for[scratch_index.message_index()]) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 204 | VLOG(3) << "Sender " << i |
| 205 | << " died, scratch_index is already accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 206 | // scratch_index is accounted for. That means we did the insert, |
| 207 | // but didn't record it. |
| 208 | CHECK(to_replace.valid()); |
| 209 | // Finish the transaction. Copy to_replace, then clear it. |
| 210 | |
| 211 | sender->scratch_index.Store(to_replace); |
| 212 | sender->to_replace.Invalidate(); |
| 213 | |
| 214 | // And then mark this sender clean. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 215 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 216 | |
| 217 | // And account for to_replace. |
| 218 | accounted_for[to_replace.message_index()] = true; |
| 219 | --num_missing; |
| 220 | ++num_accounted_for; |
| 221 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 222 | VLOG(3) << "Sender " << i << " died, neither is accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 223 | // Ambiguous. There will be an unambiguous one somewhere that we |
| 224 | // can do first. |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | // CHECK that we are making progress. |
| 229 | CHECK_NE(num_missing, starting_num_missing); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target |
| 234 | // thread. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 235 | // TODO(Brian): Do directly in assembly for armhf at least for efficiency. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 236 | int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) { |
| 237 | return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si); |
| 238 | } |
| 239 | |
| 240 | } // namespace |
| 241 | |
| 242 | size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 243 | // Round up the message size so following data is aligned appropriately. |
| 244 | config.message_data_size = |
| 245 | LocklessQueueMemory::AlignmentRoundUp(config.message_data_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 246 | |
| 247 | // As we build up the size, confirm that everything is aligned to the |
| 248 | // alignment requirements of the type. |
| 249 | size_t size = sizeof(LocklessQueueMemory); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 250 | CHECK_EQ(size % alignof(LocklessQueueMemory), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 251 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 252 | CHECK_EQ(size % alignof(AtomicIndex), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 253 | size += LocklessQueueMemory::SizeOfQueue(config); |
| 254 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 255 | CHECK_EQ(size % alignof(Message), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 256 | size += LocklessQueueMemory::SizeOfMessages(config); |
| 257 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 258 | CHECK_EQ(size % alignof(Watcher), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 259 | size += LocklessQueueMemory::SizeOfWatchers(config); |
| 260 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 261 | CHECK_EQ(size % alignof(Sender), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 262 | size += LocklessQueueMemory::SizeOfSenders(config); |
| 263 | |
| 264 | return size; |
| 265 | } |
| 266 | |
| 267 | LocklessQueueMemory *InitializeLocklessQueueMemory( |
| 268 | LocklessQueueMemory *memory, LocklessQueueConfiguration config) { |
| 269 | // Everything should be zero initialized already. So we just need to fill |
| 270 | // everything out properly. |
| 271 | |
| 272 | // Grab the mutex. We don't care if the previous reader died. We are going |
| 273 | // to check everything anyways. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 274 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 275 | |
| 276 | if (!memory->initialized) { |
| 277 | // TODO(austin): Check these for out of bounds. |
| 278 | memory->config.num_watchers = config.num_watchers; |
| 279 | memory->config.num_senders = config.num_senders; |
| 280 | memory->config.queue_size = config.queue_size; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 281 | memory->config.message_data_size = |
| 282 | LocklessQueueMemory::AlignmentRoundUp(config.message_data_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 283 | |
| 284 | const size_t num_messages = memory->num_messages(); |
| 285 | // There need to be at most MaxMessages() messages allocated. |
| 286 | CHECK_LE(num_messages, Index::MaxMessages()); |
| 287 | |
| 288 | for (size_t i = 0; i < num_messages; ++i) { |
| 289 | memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i)) |
| 290 | ->header.queue_index.Invalidate(); |
| 291 | } |
| 292 | |
| 293 | for (size_t i = 0; i < memory->queue_size(); ++i) { |
| 294 | // Make the initial counter be the furthest away number. That means that |
| 295 | // index 0 should be 0xffff, 1 should be 0, etc. |
| 296 | memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size()) |
| 297 | .IncrementBy(i) |
| 298 | .DecrementBy(memory->queue_size()), |
| 299 | i)); |
| 300 | } |
| 301 | |
| 302 | memory->next_queue_index.Invalidate(); |
| 303 | |
| 304 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 305 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 306 | // Nobody else can possibly be touching these because we haven't set |
| 307 | // initialized to true yet. |
| 308 | s->scratch_index.RelaxedStore(Index(0xffff, i + memory->queue_size())); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 309 | s->to_replace.RelaxedInvalidate(); |
| 310 | } |
| 311 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 312 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 313 | // Signal everything is done. This needs to be done last, so if we die, we |
| 314 | // redo initialization. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 315 | memory->initialized = true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 318 | return memory; |
| 319 | } |
| 320 | |
| 321 | LocklessQueue::LocklessQueue(LocklessQueueMemory *memory, |
| 322 | LocklessQueueConfiguration config) |
| 323 | : memory_(InitializeLocklessQueueMemory(memory, config)), |
| 324 | watcher_copy_(memory_->num_watchers()), |
| 325 | pid_(getpid()), |
| 326 | uid_(getuid()) {} |
| 327 | |
| 328 | LocklessQueue::~LocklessQueue() { |
| 329 | CHECK_EQ(watcher_index_, -1); |
| 330 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 331 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 332 | const int num_watchers = memory_->num_watchers(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 333 | // Cleanup is cheap. The next user will do it anyways, so no need for us to do |
| 334 | // anything right now. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 335 | |
| 336 | // And confirm that nothing is owned by us. |
| 337 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 338 | CHECK(!death_notification_is_held(&(memory_->GetWatcher(i)->tid))); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 339 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | size_t LocklessQueue::QueueSize() const { return memory_->queue_size(); } |
| 343 | |
| 344 | bool LocklessQueue::RegisterWakeup(int priority) { |
| 345 | // TODO(austin): Make sure signal coalescing is turned on. We don't need |
| 346 | // duplicates. That will improve performance under high load. |
| 347 | |
| 348 | // Since everything is self consistent, all we need to do is make sure nobody |
| 349 | // else is running. Someone dying will get caught in the generic consistency |
| 350 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 351 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 352 | const int num_watchers = memory_->num_watchers(); |
| 353 | |
| 354 | // Now, find the first empty watcher and grab it. |
| 355 | CHECK_EQ(watcher_index_, -1); |
| 356 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 357 | // If we see a slot the kernel has marked as dead, everything we do reusing |
| 358 | // it needs to happen-after whatever that process did before dying. |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame^] | 359 | auto *const futex = &(memory_->GetWatcher(i)->tid.futex); |
| 360 | const uint32_t tid = __atomic_load_n(futex, __ATOMIC_ACQUIRE); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 361 | if (tid == 0 || (tid & FUTEX_OWNER_DIED)) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 362 | watcher_index_ = i; |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame^] | 363 | // Relaxed is OK here because we're the only task going to touch it |
| 364 | // between here and the write in death_notification_init below (other |
| 365 | // recovery is blocked by us holding the setup lock). |
| 366 | __atomic_store_n(futex, 0, __ATOMIC_RELAXED); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // Bail if we failed to find an open slot. |
| 372 | if (watcher_index_ == -1) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | |
| 376 | Watcher *w = memory_->GetWatcher(watcher_index_); |
| 377 | |
| 378 | w->pid = getpid(); |
| 379 | w->priority = priority; |
| 380 | |
| 381 | // Grabbing a mutex is a compiler and memory barrier, so nothing before will |
| 382 | // get rearranged afterwords. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 383 | death_notification_init(&(w->tid)); |
| 384 | return true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void LocklessQueue::UnregisterWakeup() { |
| 388 | // Since everything is self consistent, all we need to do is make sure nobody |
| 389 | // else is running. Someone dying will get caught in the generic consistency |
| 390 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 391 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 392 | |
| 393 | // Make sure we are registered. |
| 394 | CHECK_NE(watcher_index_, -1); |
| 395 | |
| 396 | // Make sure we still own the slot we are supposed to. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 397 | CHECK( |
| 398 | death_notification_is_held(&(memory_->GetWatcher(watcher_index_)->tid))); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 399 | |
| 400 | // The act of unlocking invalidates the entry. Invalidate it. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 401 | death_notification_release(&(memory_->GetWatcher(watcher_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 402 | // And internally forget the slot. |
| 403 | watcher_index_ = -1; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | int LocklessQueue::Wakeup(const int current_priority) { |
| 407 | const size_t num_watchers = memory_->num_watchers(); |
| 408 | |
| 409 | CHECK_EQ(watcher_copy_.size(), num_watchers); |
| 410 | |
| 411 | // Grab a copy so it won't change out from underneath us, and we can sort it |
| 412 | // nicely in C++. |
| 413 | // Do note that there is still a window where the process can die *after* we |
| 414 | // read everything. We will still PI boost and send a signal to the thread in |
| 415 | // question. There is no way without pidfd's to close this window, and |
| 416 | // creating a pidfd is likely not RT. |
| 417 | for (size_t i = 0; i < num_watchers; ++i) { |
| 418 | Watcher *w = memory_->GetWatcher(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 419 | watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_RELAXED); |
| 420 | // Force the load of the TID to come first. |
| 421 | aos_compiler_memory_barrier(); |
| 422 | watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed); |
| 423 | watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 424 | |
| 425 | // Use a priority of -1 to mean an invalid entry to make sorting easier. |
| 426 | if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) { |
| 427 | watcher_copy_[i].priority = -1; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 428 | } else { |
| 429 | // Ensure all of this happens after we're done looking at the pid+priority |
| 430 | // in shared memory. |
| 431 | aos_compiler_memory_barrier(); |
| 432 | if (watcher_copy_[i].tid != static_cast<pid_t>(__atomic_load_n( |
| 433 | &(w->tid.futex), __ATOMIC_RELAXED))) { |
| 434 | // Confirm that the watcher hasn't been re-used and modified while we |
| 435 | // read it. If it has, mark it invalid again. |
| 436 | watcher_copy_[i].priority = -1; |
| 437 | watcher_copy_[i].tid = 0; |
| 438 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | // Now sort. |
| 443 | ::std::sort(watcher_copy_.begin(), watcher_copy_.end(), |
| 444 | [](const WatcherCopy &a, const WatcherCopy &b) { |
| 445 | return a.priority > b.priority; |
| 446 | }); |
| 447 | |
| 448 | int count = 0; |
| 449 | if (watcher_copy_[0].priority != -1) { |
| 450 | const int max_priority = |
| 451 | ::std::max(current_priority, watcher_copy_[0].priority); |
| 452 | // Boost if we are RT and there is a higher priority sender out there. |
| 453 | // Otherwise we might run into priority inversions. |
| 454 | if (max_priority > current_priority && current_priority > 0) { |
| 455 | SetCurrentThreadRealtimePriority(max_priority); |
| 456 | } |
| 457 | |
| 458 | // Build up the siginfo to send. |
| 459 | siginfo_t uinfo; |
| 460 | memset(&uinfo, 0, sizeof(uinfo)); |
| 461 | |
| 462 | uinfo.si_code = SI_QUEUE; |
| 463 | uinfo.si_pid = pid_; |
| 464 | uinfo.si_uid = uid_; |
| 465 | uinfo.si_value.sival_int = 0; |
| 466 | |
| 467 | for (const WatcherCopy &watcher_copy : watcher_copy_) { |
| 468 | // The first -1 priority means we are at the end of the valid list. |
| 469 | if (watcher_copy.priority == -1) { |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | // Send the signal. Target just the thread that sent it so that we can |
| 474 | // support multiple watchers in a process (when someone creates multiple |
| 475 | // event loops in different threads). |
| 476 | rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal, |
| 477 | &uinfo); |
| 478 | |
| 479 | ++count; |
| 480 | } |
| 481 | |
| 482 | // Drop back down if we were boosted. |
| 483 | if (max_priority > current_priority && current_priority > 0) { |
| 484 | SetCurrentThreadRealtimePriority(current_priority); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return count; |
| 489 | } |
| 490 | |
| 491 | LocklessQueue::Sender::Sender(LocklessQueueMemory *memory) : memory_(memory) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 492 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 493 | |
| 494 | // Since we already have the lock, go ahead and try cleaning up. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 495 | Cleanup(memory_, grab_queue_setup_lock); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 496 | |
| 497 | const int num_senders = memory_->num_senders(); |
| 498 | |
| 499 | for (int i = 0; i < num_senders; ++i) { |
| 500 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 501 | // This doesn't need synchronization because we're the only process doing |
| 502 | // initialization right now, and nobody else will be touching senders which |
| 503 | // we're interested in. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 504 | const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED); |
| 505 | if (tid == 0) { |
| 506 | sender_index_ = i; |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | if (sender_index_ == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 512 | LOG(FATAL) << "Too many senders"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | ::aos::ipc_lib::Sender *s = memory_->GetSender(sender_index_); |
| 516 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 517 | // Indicate that we are now alive by taking over the slot. If the previous |
| 518 | // owner died, we still want to do this. |
| 519 | death_notification_init(&(s->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | LocklessQueue::Sender::~Sender() { |
| 523 | if (memory_ != nullptr) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 524 | death_notification_release(&(memory_->GetSender(sender_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | LocklessQueue::Sender LocklessQueue::MakeSender() { |
| 529 | return LocklessQueue::Sender(memory_); |
| 530 | } |
| 531 | |
| 532 | QueueIndex ZeroOrValid(QueueIndex index) { |
| 533 | if (!index.valid()) { |
| 534 | return index.Clear(); |
| 535 | } |
| 536 | return index; |
| 537 | } |
| 538 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 539 | size_t LocklessQueue::Sender::size() { return memory_->message_data_size(); } |
| 540 | |
| 541 | void *LocklessQueue::Sender::Data() { |
| 542 | ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_); |
| 543 | Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 544 | Message *message = memory_->GetMessage(scratch_index); |
| 545 | message->header.queue_index.Invalidate(); |
| 546 | |
| 547 | return &message->data[0]; |
| 548 | } |
| 549 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 550 | void LocklessQueue::Sender::Send(const char *data, size_t length) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 551 | CHECK_LE(length, size()); |
| 552 | memcpy(Data(), data, length); |
| 553 | Send(length); |
| 554 | } |
| 555 | |
| 556 | void LocklessQueue::Sender::Send(size_t length) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 557 | const size_t queue_size = memory_->queue_size(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 558 | CHECK_LE(length, size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 559 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 560 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
| 561 | // We can do a relaxed load on our sender because we're the only person |
| 562 | // modifying it right now. |
| 563 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 564 | Message *const message = memory_->GetMessage(scratch_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 565 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 566 | message->header.length = length; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 567 | |
| 568 | while (true) { |
| 569 | const QueueIndex actual_next_queue_index = |
| 570 | memory_->next_queue_index.Load(queue_size); |
| 571 | const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index); |
| 572 | |
| 573 | const QueueIndex incremented_queue_index = next_queue_index.Increment(); |
| 574 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 575 | // This needs to synchronize with whoever the previous writer at this |
| 576 | // location was. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 577 | const Index to_replace = memory_->LoadIndex(next_queue_index); |
| 578 | |
| 579 | const QueueIndex decremented_queue_index = |
| 580 | next_queue_index.DecrementBy(queue_size); |
| 581 | |
| 582 | // See if we got beat. If we did, try to atomically update |
| 583 | // next_queue_index in case the previous writer failed and retry. |
| 584 | if (!to_replace.IsPlausible(decremented_queue_index)) { |
| 585 | // We don't care about the result. It will either succeed, or we got |
| 586 | // beat in fixing it and just need to give up and try again. If we got |
| 587 | // beat multiple times, the only way progress can be made is if the queue |
| 588 | // is updated as well. This means that if we retry reading |
| 589 | // next_queue_index, we will be at most off by one and can retry. |
| 590 | // |
| 591 | // Both require no further action from us. |
| 592 | // |
| 593 | // TODO(austin): If we are having fairness issues under contention, we |
| 594 | // could have a mode bit in next_queue_index, and could use a lock or some |
| 595 | // other form of PI boosting to let the higher priority task win. |
| 596 | memory_->next_queue_index.CompareAndExchangeStrong( |
| 597 | actual_next_queue_index, incremented_queue_index); |
| 598 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 599 | VLOG(3) << "We were beat. Try again. Was " << std::hex |
| 600 | << to_replace.get() << ", is " << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 601 | continue; |
| 602 | } |
| 603 | |
| 604 | // Confirm that the message is what it should be. |
| 605 | { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 606 | const QueueIndex previous_index = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 607 | memory_->GetMessage(to_replace)->header.queue_index.Load(queue_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 608 | if (previous_index != decremented_queue_index && previous_index.valid()) { |
| 609 | // Retry. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 610 | VLOG(3) << "Something fishy happened, queue index doesn't match. " |
| 611 | "Retrying. Previous index was " |
| 612 | << std::hex << previous_index.index() << ", should be " |
| 613 | << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 614 | continue; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | message->header.monotonic_sent_time = ::aos::monotonic_clock::now(); |
| 619 | message->header.realtime_sent_time = ::aos::realtime_clock::now(); |
| 620 | |
| 621 | // Before we are fully done filling out the message, update the Sender state |
| 622 | // with the new index to write. This re-uses the barrier for the |
| 623 | // queue_index store. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 624 | const Index index_to_write(next_queue_index, scratch_index.message_index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 625 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 626 | aos_compiler_memory_barrier(); |
| 627 | // We're the only person who cares about our scratch index, besides somebody |
| 628 | // cleaning up after us. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 629 | sender->scratch_index.RelaxedStore(index_to_write); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 630 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 631 | |
| 632 | message->header.queue_index.Store(next_queue_index); |
| 633 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 634 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 635 | // The message is now filled out, and we have a confirmed slot to store |
| 636 | // into. |
| 637 | // |
| 638 | // 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] | 639 | // was Invalid before now. Only person who will read this is whoever cleans |
| 640 | // up after us, so no synchronization necessary. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 641 | sender->to_replace.RelaxedStore(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 642 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 643 | |
| 644 | // Then exchange the next index into the queue. |
| 645 | if (!memory_->GetQueue(next_queue_index.Wrapped()) |
| 646 | ->CompareAndExchangeStrong(to_replace, index_to_write)) { |
| 647 | // Aw, didn't succeed. Retry. |
| 648 | sender->to_replace.RelaxedInvalidate(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 649 | aos_compiler_memory_barrier(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 650 | VLOG(3) << "Failed to wrap into queue"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 651 | continue; |
| 652 | } |
| 653 | |
| 654 | // Then update next_queue_index to save the next user some computation time. |
| 655 | memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index, |
| 656 | incremented_queue_index); |
| 657 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 658 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 659 | // Now update the scratch space and record that we succeeded. |
| 660 | sender->scratch_index.Store(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 661 | aos_compiler_memory_barrier(); |
| 662 | // And then record that we succeeded, but definitely after the above store. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 663 | sender->to_replace.RelaxedInvalidate(); |
| 664 | break; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | LocklessQueue::ReadResult LocklessQueue::Read( |
| 669 | uint32_t uint32_queue_index, |
| 670 | ::aos::monotonic_clock::time_point *monotonic_sent_time, |
| 671 | ::aos::realtime_clock::time_point *realtime_sent_time, size_t *length, |
| 672 | char *data) { |
| 673 | const size_t queue_size = memory_->queue_size(); |
| 674 | |
| 675 | // Build up the QueueIndex. |
| 676 | const QueueIndex queue_index = |
| 677 | QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index); |
| 678 | |
| 679 | // Read the message stored at the requested location. |
| 680 | Index mi = memory_->LoadIndex(queue_index); |
| 681 | Message *m = memory_->GetMessage(mi); |
| 682 | |
| 683 | while (true) { |
| 684 | // We need to confirm that the data doesn't change while we are reading it. |
| 685 | // Do that by first confirming that the message points to the queue index we |
| 686 | // want. |
| 687 | const QueueIndex starting_queue_index = |
| 688 | m->header.queue_index.Load(queue_size); |
| 689 | if (starting_queue_index != queue_index) { |
| 690 | // If we found a message that is exactly 1 loop old, we just wrapped. |
| 691 | if (starting_queue_index == queue_index.DecrementBy(queue_size)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 692 | VLOG(3) << "Matches: " << std::hex << starting_queue_index.index() |
| 693 | << ", " << queue_index.DecrementBy(queue_size).index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 694 | return ReadResult::NOTHING_NEW; |
| 695 | } else { |
| 696 | // Someone has re-used this message between when we pulled it out of the |
| 697 | // queue and when we grabbed its index. It is pretty hard to deduce |
| 698 | // what happened. Just try again. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 699 | Message *const new_m = memory_->GetMessage(queue_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 700 | if (m != new_m) { |
| 701 | m = new_m; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 702 | VLOG(3) << "Retrying, m doesn't match"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 703 | continue; |
| 704 | } |
| 705 | |
| 706 | // We have confirmed that message still points to the same message. This |
| 707 | // means that the message didn't get swapped out from under us, so |
| 708 | // starting_queue_index is correct. |
| 709 | // |
| 710 | // Either we got too far behind (signaled by this being a valid |
| 711 | // message), or this is one of the initial messages which are invalid. |
| 712 | if (starting_queue_index.valid()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 713 | VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index() |
| 714 | << ", got " << starting_queue_index.index() << ", behind by " |
| 715 | << std::dec |
| 716 | << (starting_queue_index.index() - queue_index.index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 717 | return ReadResult::TOO_OLD; |
| 718 | } |
| 719 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 720 | VLOG(3) << "Initial"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 721 | |
| 722 | // There isn't a valid message at this location. |
| 723 | // |
| 724 | // If someone asks for one of the messages within the first go around, |
| 725 | // then they need to wait. They got ahead. Otherwise, they are |
| 726 | // asking for something crazy, like something before the beginning of |
| 727 | // the queue. Tell them that they are behind. |
| 728 | if (uint32_queue_index < memory_->queue_size()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 729 | VLOG(3) << "Near zero, " << std::hex << uint32_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 730 | return ReadResult::NOTHING_NEW; |
| 731 | } else { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 732 | VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 733 | return ReadResult::TOO_OLD; |
| 734 | } |
| 735 | } |
| 736 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 737 | VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", " |
| 738 | << queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 739 | break; |
| 740 | } |
| 741 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 742 | // Then read the data out. Copy it all out to be deterministic and so we can |
| 743 | // make length be from either end. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 744 | *monotonic_sent_time = m->header.monotonic_sent_time; |
| 745 | *realtime_sent_time = m->header.realtime_sent_time; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 746 | memcpy(data, &m->data[0], message_data_size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 747 | *length = m->header.length; |
| 748 | |
| 749 | // And finally, confirm that the message *still* points to the queue index we |
| 750 | // want. This means it didn't change out from under us. |
| 751 | // If something changed out from under us, we were reading it much too late in |
| 752 | // it's lifetime. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 753 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 754 | const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size); |
| 755 | if (final_queue_index != queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 756 | VLOG(3) << "Changed out from under us. Reading " << std::hex |
| 757 | << queue_index.index() << ", finished with " |
| 758 | << final_queue_index.index() << ", delta: " << std::dec |
| 759 | << (final_queue_index.index() - queue_index.index()); |
| 760 | return ReadResult::OVERWROTE; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return ReadResult::GOOD; |
| 764 | } |
| 765 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 766 | size_t LocklessQueue::queue_size() const { return memory_->queue_size(); } |
| 767 | size_t LocklessQueue::message_data_size() const { |
| 768 | return memory_->message_data_size(); |
| 769 | } |
| 770 | |
| 771 | QueueIndex LocklessQueue::LatestQueueIndex() { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 772 | const size_t queue_size = memory_->queue_size(); |
| 773 | |
| 774 | // There is only one interesting case. We need to know if the queue is empty. |
| 775 | // That is done with a sentinel value. At worst, this will be off by one. |
| 776 | const QueueIndex next_queue_index = |
| 777 | memory_->next_queue_index.Load(queue_size); |
| 778 | if (next_queue_index.valid()) { |
| 779 | const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 780 | return current_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 781 | } else { |
| 782 | return empty_queue_index(); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | namespace { |
| 787 | |
| 788 | // Prints out the mutex state. Not safe to use while the mutex is being |
| 789 | // changed. |
| 790 | ::std::string PrintMutex(aos_mutex *mutex) { |
| 791 | ::std::stringstream s; |
| 792 | s << "aos_mutex(" << ::std::hex << mutex->futex; |
| 793 | |
| 794 | if (mutex->futex != 0) { |
| 795 | s << ":"; |
| 796 | if (mutex->futex & FUTEX_OWNER_DIED) { |
| 797 | s << "FUTEX_OWNER_DIED|"; |
| 798 | } |
| 799 | s << "tid=" << (mutex->futex & FUTEX_TID_MASK); |
| 800 | } |
| 801 | |
| 802 | s << ")"; |
| 803 | return s.str(); |
| 804 | } |
| 805 | |
| 806 | } // namespace |
| 807 | |
| 808 | void PrintLocklessQueueMemory(LocklessQueueMemory *memory) { |
| 809 | const size_t queue_size = memory->queue_size(); |
| 810 | ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl; |
| 811 | ::std::cout << " aos_mutex queue_setup_lock = " |
| 812 | << PrintMutex(&memory->queue_setup_lock) << ::std::endl; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 813 | ::std::cout << " bool initialized = " << memory->initialized << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 814 | ::std::cout << " config {" << ::std::endl; |
| 815 | ::std::cout << " size_t num_watchers = " << memory->config.num_watchers |
| 816 | << ::std::endl; |
| 817 | ::std::cout << " size_t num_senders = " << memory->config.num_senders |
| 818 | << ::std::endl; |
| 819 | ::std::cout << " size_t queue_size = " << memory->config.queue_size |
| 820 | << ::std::endl; |
| 821 | ::std::cout << " size_t message_data_size = " |
| 822 | << memory->config.message_data_size << ::std::endl; |
| 823 | |
| 824 | ::std::cout << " AtomicQueueIndex next_queue_index = " |
| 825 | << memory->next_queue_index.Load(queue_size).DebugString() |
| 826 | << ::std::endl; |
| 827 | |
| 828 | ::std::cout << " }" << ::std::endl; |
| 829 | ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl; |
| 830 | for (size_t i = 0; i < queue_size; ++i) { |
| 831 | ::std::cout << " [" << i << "] -> " |
| 832 | << memory->GetQueue(i)->Load().DebugString() << ::std::endl; |
| 833 | } |
| 834 | ::std::cout << " }" << ::std::endl; |
| 835 | ::std::cout << " Message messages[" << memory->num_messages() << "] {" |
| 836 | << ::std::endl; |
| 837 | for (size_t i = 0; i < memory->num_messages(); ++i) { |
| 838 | Message *m = memory->GetMessage(Index(i, i)); |
| 839 | ::std::cout << " [" << i << "] -> Message {" << ::std::endl; |
| 840 | ::std::cout << " Header {" << ::std::endl; |
| 841 | ::std::cout << " AtomicQueueIndex queue_index = " |
| 842 | << m->header.queue_index.Load(queue_size).DebugString() |
| 843 | << ::std::endl; |
| 844 | ::std::cout << " size_t length = " << m->header.length |
| 845 | << ::std::endl; |
| 846 | ::std::cout << " }" << ::std::endl; |
| 847 | ::std::cout << " data: {"; |
| 848 | |
| 849 | for (size_t j = 0; j < m->header.length; ++j) { |
| 850 | char data = m->data[j]; |
| 851 | if (j != 0) { |
| 852 | ::std::cout << " "; |
| 853 | } |
| 854 | if (::std::isprint(data)) { |
| 855 | ::std::cout << ::std::setfill(' ') << ::std::setw(2) << ::std::hex |
| 856 | << data; |
| 857 | } else { |
| 858 | ::std::cout << "0x" << ::std::setfill('0') << ::std::setw(2) |
| 859 | << ::std::hex << (static_cast<unsigned>(data) & 0xff); |
| 860 | } |
| 861 | } |
| 862 | ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl; |
| 863 | ::std::cout << " }," << ::std::endl; |
| 864 | } |
| 865 | ::std::cout << " }" << ::std::endl; |
| 866 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 867 | ::std::cout << " Sender senders[" << memory->num_senders() << "] {" |
| 868 | << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 869 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 870 | Sender *s = memory->GetSender(i); |
| 871 | ::std::cout << " [" << i << "] -> Sender {" << ::std::endl; |
| 872 | ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid) |
| 873 | << ::std::endl; |
| 874 | ::std::cout << " AtomicIndex scratch_index = " |
| 875 | << s->scratch_index.Load().DebugString() << ::std::endl; |
| 876 | ::std::cout << " AtomicIndex to_replace = " |
| 877 | << s->to_replace.Load().DebugString() << ::std::endl; |
| 878 | ::std::cout << " }" << ::std::endl; |
| 879 | } |
| 880 | ::std::cout << " }" << ::std::endl; |
| 881 | |
| 882 | ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {" |
| 883 | << ::std::endl; |
| 884 | for (size_t i = 0; i < memory->num_watchers(); ++i) { |
| 885 | Watcher *w = memory->GetWatcher(i); |
| 886 | ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl; |
| 887 | ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid) |
| 888 | << ::std::endl; |
| 889 | ::std::cout << " pid_t pid = " << w->pid << ::std::endl; |
| 890 | ::std::cout << " int priority = " << w->priority << ::std::endl; |
| 891 | ::std::cout << " }" << ::std::endl; |
| 892 | } |
| 893 | ::std::cout << " }" << ::std::endl; |
| 894 | |
| 895 | ::std::cout << "}" << ::std::endl; |
| 896 | } |
| 897 | |
| 898 | } // namespace ipc_lib |
| 899 | } // namespace aos |