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. |
Brian Silverman | ba3b04d | 2020-07-02 19:44:37 -0700 | [diff] [blame^] | 184 | if (to_replace.valid()) { |
| 185 | CHECK_LE(to_replace.message_index(), accounted_for.size()); |
| 186 | } |
| 187 | if (scratch_index.valid()) { |
| 188 | CHECK_LE(scratch_index.message_index(), accounted_for.size()); |
| 189 | } |
| 190 | if (!to_replace.valid() || accounted_for[to_replace.message_index()]) { |
| 191 | CHECK(scratch_index.valid()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 192 | VLOG(3) << "Sender " << i |
| 193 | << " died, to_replace is already accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 194 | // If both are accounted for, we are corrupt... |
| 195 | CHECK(!accounted_for[scratch_index.message_index()]); |
| 196 | |
| 197 | // to_replace is already accounted for. This means that we didn't |
| 198 | // atomically insert scratch_index into the queue yet. So |
| 199 | // invalidate to_replace. |
| 200 | sender->to_replace.Invalidate(); |
| 201 | |
| 202 | // And then mark this sender clean. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 203 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 204 | |
| 205 | // And account for scratch_index. |
| 206 | accounted_for[scratch_index.message_index()] = true; |
| 207 | --num_missing; |
| 208 | ++num_accounted_for; |
Brian Silverman | ba3b04d | 2020-07-02 19:44:37 -0700 | [diff] [blame^] | 209 | } else if (!scratch_index.valid() || |
| 210 | accounted_for[scratch_index.message_index()]) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 211 | VLOG(3) << "Sender " << i |
| 212 | << " died, scratch_index is already accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 213 | // scratch_index is accounted for. That means we did the insert, |
| 214 | // but didn't record it. |
| 215 | CHECK(to_replace.valid()); |
| 216 | // Finish the transaction. Copy to_replace, then clear it. |
| 217 | |
| 218 | sender->scratch_index.Store(to_replace); |
| 219 | sender->to_replace.Invalidate(); |
| 220 | |
| 221 | // And then mark this sender clean. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 222 | __atomic_store_n(&(sender->tid.futex), 0, __ATOMIC_RELEASE); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 223 | |
| 224 | // And account for to_replace. |
| 225 | accounted_for[to_replace.message_index()] = true; |
| 226 | --num_missing; |
| 227 | ++num_accounted_for; |
| 228 | } else { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 229 | VLOG(3) << "Sender " << i << " died, neither is accounted for"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 230 | // Ambiguous. There will be an unambiguous one somewhere that we |
| 231 | // can do first. |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | // CHECK that we are making progress. |
| 236 | CHECK_NE(num_missing, starting_num_missing); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Exposes rt_tgsigqueueinfo so we can send the signal *just* to the target |
| 241 | // thread. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 242 | // TODO(Brian): Do directly in assembly for armhf at least for efficiency. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 243 | int rt_tgsigqueueinfo(pid_t tgid, pid_t tid, int sig, siginfo_t *si) { |
| 244 | return syscall(SYS_rt_tgsigqueueinfo, tgid, tid, sig, si); |
| 245 | } |
| 246 | |
| 247 | } // namespace |
| 248 | |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 249 | size_t LocklessQueueConfiguration::message_size() const { |
| 250 | // Round up the message size so following data is aligned appropriately. |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 251 | return LocklessQueueMemory::AlignmentRoundUp(message_data_size + |
| 252 | (kChannelDataAlignment - 1)) + |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 253 | sizeof(Message); |
| 254 | } |
| 255 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 256 | size_t LocklessQueueMemorySize(LocklessQueueConfiguration config) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 257 | // Round up the message size so following data is aligned appropriately. |
| 258 | config.message_data_size = |
| 259 | LocklessQueueMemory::AlignmentRoundUp(config.message_data_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 260 | |
| 261 | // As we build up the size, confirm that everything is aligned to the |
| 262 | // alignment requirements of the type. |
| 263 | size_t size = sizeof(LocklessQueueMemory); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 264 | CHECK_EQ(size % alignof(LocklessQueueMemory), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 265 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 266 | CHECK_EQ(size % alignof(AtomicIndex), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 267 | size += LocklessQueueMemory::SizeOfQueue(config); |
| 268 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 269 | CHECK_EQ(size % alignof(Message), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 270 | size += LocklessQueueMemory::SizeOfMessages(config); |
| 271 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 272 | CHECK_EQ(size % alignof(Watcher), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 273 | size += LocklessQueueMemory::SizeOfWatchers(config); |
| 274 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 275 | CHECK_EQ(size % alignof(Sender), 0u); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 276 | size += LocklessQueueMemory::SizeOfSenders(config); |
| 277 | |
| 278 | return size; |
| 279 | } |
| 280 | |
| 281 | LocklessQueueMemory *InitializeLocklessQueueMemory( |
| 282 | LocklessQueueMemory *memory, LocklessQueueConfiguration config) { |
| 283 | // Everything should be zero initialized already. So we just need to fill |
| 284 | // everything out properly. |
| 285 | |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 286 | // This is the UID we will use for checking signal-sending permission |
| 287 | // compatibility. |
| 288 | // |
| 289 | // The manpage says: |
| 290 | // For a process to have permission to send a signal, it must either be |
| 291 | // privileged [...], or the real or effective user ID of the sending process |
| 292 | // must equal the real or saved set-user-ID of the target process. |
| 293 | // |
| 294 | // Processes typically initialize a queue in random order as they start up. |
| 295 | // This means we need an algorithm for verifying all processes have |
| 296 | // permissions to send each other signals which gives the same answer no |
| 297 | // matter what order they attach in. We would also like to avoid maintaining a |
| 298 | // shared list of the UIDs of all processes. |
| 299 | // |
| 300 | // To do this while still giving sufficient flexibility for all current use |
| 301 | // cases, we track a single UID for the queue. All processes with a matching |
| 302 | // euid+suid must have this UID. Any processes with distinct euid/suid must |
| 303 | // instead have a matching ruid. This guarantees signals can be sent between |
| 304 | // all processes attached to the queue. |
| 305 | // |
| 306 | // In particular, this allows a process to change only its euid (to interact |
| 307 | // with a queue) while still maintaining privileges via its ruid. However, it |
| 308 | // can only use privileges in ways that do not require changing the euid back, |
| 309 | // because while the euid is different it will not be able to receive signals. |
| 310 | // We can't actually verify that, but we can sanity check that things are |
| 311 | // valid when the queue is initialized. |
| 312 | |
| 313 | uid_t uid; |
| 314 | { |
| 315 | uid_t ruid, euid, suid; |
| 316 | PCHECK(getresuid(&ruid, &euid, &suid) == 0); |
| 317 | // If these are equal, then use them, even if that's different from the real |
| 318 | // UID. This allows processes to keep a real UID of 0 (to have permissions |
| 319 | // to perform system-level changes) while still being able to communicate |
| 320 | // with processes running unprivileged as a distinct user. |
| 321 | if (euid == suid) { |
| 322 | uid = euid; |
| 323 | VLOG(1) << "Using euid==suid " << uid; |
| 324 | } else { |
| 325 | uid = ruid; |
| 326 | VLOG(1) << "Using ruid " << ruid; |
| 327 | } |
| 328 | } |
| 329 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 330 | // Grab the mutex. We don't care if the previous reader died. We are going |
| 331 | // to check everything anyways. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 332 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 333 | |
| 334 | if (!memory->initialized) { |
| 335 | // TODO(austin): Check these for out of bounds. |
| 336 | memory->config.num_watchers = config.num_watchers; |
| 337 | memory->config.num_senders = config.num_senders; |
| 338 | memory->config.queue_size = config.queue_size; |
Austin Schuh | 4bc4f90 | 2019-12-23 18:04:51 -0800 | [diff] [blame] | 339 | memory->config.message_data_size = config.message_data_size; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 340 | |
| 341 | const size_t num_messages = memory->num_messages(); |
| 342 | // There need to be at most MaxMessages() messages allocated. |
| 343 | CHECK_LE(num_messages, Index::MaxMessages()); |
| 344 | |
| 345 | for (size_t i = 0; i < num_messages; ++i) { |
| 346 | memory->GetMessage(Index(QueueIndex::Zero(memory->queue_size()), i)) |
| 347 | ->header.queue_index.Invalidate(); |
| 348 | } |
| 349 | |
| 350 | for (size_t i = 0; i < memory->queue_size(); ++i) { |
| 351 | // Make the initial counter be the furthest away number. That means that |
| 352 | // index 0 should be 0xffff, 1 should be 0, etc. |
| 353 | memory->GetQueue(i)->Store(Index(QueueIndex::Zero(memory->queue_size()) |
| 354 | .IncrementBy(i) |
| 355 | .DecrementBy(memory->queue_size()), |
| 356 | i)); |
| 357 | } |
| 358 | |
| 359 | memory->next_queue_index.Invalidate(); |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 360 | memory->uid = uid; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 361 | |
| 362 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 363 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 364 | // Nobody else can possibly be touching these because we haven't set |
| 365 | // initialized to true yet. |
| 366 | s->scratch_index.RelaxedStore(Index(0xffff, i + memory->queue_size())); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 367 | s->to_replace.RelaxedInvalidate(); |
| 368 | } |
| 369 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 370 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 371 | // Signal everything is done. This needs to be done last, so if we die, we |
| 372 | // redo initialization. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 373 | memory->initialized = true; |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 374 | } else { |
Brian Silverman | c57ff0a | 2020-04-28 16:45:13 -0700 | [diff] [blame] | 375 | CHECK_EQ(uid, memory->uid) << ": UIDs must match for all processes"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 378 | return memory; |
| 379 | } |
| 380 | |
| 381 | LocklessQueue::LocklessQueue(LocklessQueueMemory *memory, |
| 382 | LocklessQueueConfiguration config) |
| 383 | : memory_(InitializeLocklessQueueMemory(memory, config)), |
| 384 | watcher_copy_(memory_->num_watchers()), |
| 385 | pid_(getpid()), |
| 386 | uid_(getuid()) {} |
| 387 | |
| 388 | LocklessQueue::~LocklessQueue() { |
| 389 | CHECK_EQ(watcher_index_, -1); |
| 390 | |
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 | const int num_watchers = memory_->num_watchers(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 393 | // Cleanup is cheap. The next user will do it anyways, so no need for us to do |
| 394 | // anything right now. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 395 | |
| 396 | // And confirm that nothing is owned by us. |
| 397 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 398 | CHECK(!death_notification_is_held(&(memory_->GetWatcher(i)->tid))); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 399 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | size_t LocklessQueue::QueueSize() const { return memory_->queue_size(); } |
| 403 | |
| 404 | bool LocklessQueue::RegisterWakeup(int priority) { |
| 405 | // TODO(austin): Make sure signal coalescing is turned on. We don't need |
| 406 | // duplicates. That will improve performance under high load. |
| 407 | |
| 408 | // Since everything is self consistent, all we need to do is make sure nobody |
| 409 | // else is running. Someone dying will get caught in the generic consistency |
| 410 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 411 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 412 | const int num_watchers = memory_->num_watchers(); |
| 413 | |
| 414 | // Now, find the first empty watcher and grab it. |
| 415 | CHECK_EQ(watcher_index_, -1); |
| 416 | for (int i = 0; i < num_watchers; ++i) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 417 | // If we see a slot the kernel has marked as dead, everything we do reusing |
| 418 | // it needs to happen-after whatever that process did before dying. |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 419 | auto *const futex = &(memory_->GetWatcher(i)->tid.futex); |
| 420 | const uint32_t tid = __atomic_load_n(futex, __ATOMIC_ACQUIRE); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 421 | if (tid == 0 || (tid & FUTEX_OWNER_DIED)) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 422 | watcher_index_ = i; |
Brian Silverman | 2484eea | 2019-12-21 16:48:46 -0800 | [diff] [blame] | 423 | // Relaxed is OK here because we're the only task going to touch it |
| 424 | // between here and the write in death_notification_init below (other |
| 425 | // recovery is blocked by us holding the setup lock). |
| 426 | __atomic_store_n(futex, 0, __ATOMIC_RELAXED); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // Bail if we failed to find an open slot. |
| 432 | if (watcher_index_ == -1) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 433 | return false; |
| 434 | } |
| 435 | |
| 436 | Watcher *w = memory_->GetWatcher(watcher_index_); |
| 437 | |
| 438 | w->pid = getpid(); |
| 439 | w->priority = priority; |
| 440 | |
| 441 | // Grabbing a mutex is a compiler and memory barrier, so nothing before will |
| 442 | // get rearranged afterwords. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 443 | death_notification_init(&(w->tid)); |
| 444 | return true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void LocklessQueue::UnregisterWakeup() { |
| 448 | // Since everything is self consistent, all we need to do is make sure nobody |
| 449 | // else is running. Someone dying will get caught in the generic consistency |
| 450 | // check. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 451 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 452 | |
| 453 | // Make sure we are registered. |
| 454 | CHECK_NE(watcher_index_, -1); |
| 455 | |
| 456 | // Make sure we still own the slot we are supposed to. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 457 | CHECK( |
| 458 | death_notification_is_held(&(memory_->GetWatcher(watcher_index_)->tid))); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 459 | |
| 460 | // The act of unlocking invalidates the entry. Invalidate it. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 461 | death_notification_release(&(memory_->GetWatcher(watcher_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 462 | // And internally forget the slot. |
| 463 | watcher_index_ = -1; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | int LocklessQueue::Wakeup(const int current_priority) { |
| 467 | const size_t num_watchers = memory_->num_watchers(); |
| 468 | |
| 469 | CHECK_EQ(watcher_copy_.size(), num_watchers); |
| 470 | |
| 471 | // Grab a copy so it won't change out from underneath us, and we can sort it |
| 472 | // nicely in C++. |
| 473 | // Do note that there is still a window where the process can die *after* we |
| 474 | // read everything. We will still PI boost and send a signal to the thread in |
| 475 | // question. There is no way without pidfd's to close this window, and |
| 476 | // creating a pidfd is likely not RT. |
| 477 | for (size_t i = 0; i < num_watchers; ++i) { |
| 478 | Watcher *w = memory_->GetWatcher(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 479 | watcher_copy_[i].tid = __atomic_load_n(&(w->tid.futex), __ATOMIC_RELAXED); |
| 480 | // Force the load of the TID to come first. |
| 481 | aos_compiler_memory_barrier(); |
| 482 | watcher_copy_[i].pid = w->pid.load(std::memory_order_relaxed); |
| 483 | watcher_copy_[i].priority = w->priority.load(std::memory_order_relaxed); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 484 | |
| 485 | // Use a priority of -1 to mean an invalid entry to make sorting easier. |
| 486 | if (watcher_copy_[i].tid & FUTEX_OWNER_DIED || watcher_copy_[i].tid == 0) { |
| 487 | watcher_copy_[i].priority = -1; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 488 | } else { |
| 489 | // Ensure all of this happens after we're done looking at the pid+priority |
| 490 | // in shared memory. |
| 491 | aos_compiler_memory_barrier(); |
| 492 | if (watcher_copy_[i].tid != static_cast<pid_t>(__atomic_load_n( |
| 493 | &(w->tid.futex), __ATOMIC_RELAXED))) { |
| 494 | // Confirm that the watcher hasn't been re-used and modified while we |
| 495 | // read it. If it has, mark it invalid again. |
| 496 | watcher_copy_[i].priority = -1; |
| 497 | watcher_copy_[i].tid = 0; |
| 498 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | // Now sort. |
| 503 | ::std::sort(watcher_copy_.begin(), watcher_copy_.end(), |
| 504 | [](const WatcherCopy &a, const WatcherCopy &b) { |
| 505 | return a.priority > b.priority; |
| 506 | }); |
| 507 | |
| 508 | int count = 0; |
| 509 | if (watcher_copy_[0].priority != -1) { |
| 510 | const int max_priority = |
| 511 | ::std::max(current_priority, watcher_copy_[0].priority); |
| 512 | // Boost if we are RT and there is a higher priority sender out there. |
| 513 | // Otherwise we might run into priority inversions. |
| 514 | if (max_priority > current_priority && current_priority > 0) { |
| 515 | SetCurrentThreadRealtimePriority(max_priority); |
| 516 | } |
| 517 | |
| 518 | // Build up the siginfo to send. |
| 519 | siginfo_t uinfo; |
| 520 | memset(&uinfo, 0, sizeof(uinfo)); |
| 521 | |
| 522 | uinfo.si_code = SI_QUEUE; |
| 523 | uinfo.si_pid = pid_; |
| 524 | uinfo.si_uid = uid_; |
| 525 | uinfo.si_value.sival_int = 0; |
| 526 | |
| 527 | for (const WatcherCopy &watcher_copy : watcher_copy_) { |
| 528 | // The first -1 priority means we are at the end of the valid list. |
| 529 | if (watcher_copy.priority == -1) { |
| 530 | break; |
| 531 | } |
| 532 | |
| 533 | // Send the signal. Target just the thread that sent it so that we can |
| 534 | // support multiple watchers in a process (when someone creates multiple |
| 535 | // event loops in different threads). |
| 536 | rt_tgsigqueueinfo(watcher_copy.pid, watcher_copy.tid, kWakeupSignal, |
| 537 | &uinfo); |
| 538 | |
| 539 | ++count; |
| 540 | } |
| 541 | |
| 542 | // Drop back down if we were boosted. |
| 543 | if (max_priority > current_priority && current_priority > 0) { |
| 544 | SetCurrentThreadRealtimePriority(current_priority); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | return count; |
| 549 | } |
| 550 | |
| 551 | LocklessQueue::Sender::Sender(LocklessQueueMemory *memory) : memory_(memory) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 552 | GrabQueueSetupLockOrDie grab_queue_setup_lock(memory_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 553 | |
| 554 | // Since we already have the lock, go ahead and try cleaning up. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 555 | Cleanup(memory_, grab_queue_setup_lock); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 556 | |
| 557 | const int num_senders = memory_->num_senders(); |
| 558 | |
| 559 | for (int i = 0; i < num_senders; ++i) { |
| 560 | ::aos::ipc_lib::Sender *s = memory->GetSender(i); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 561 | // This doesn't need synchronization because we're the only process doing |
| 562 | // initialization right now, and nobody else will be touching senders which |
| 563 | // we're interested in. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 564 | const uint32_t tid = __atomic_load_n(&(s->tid.futex), __ATOMIC_RELAXED); |
| 565 | if (tid == 0) { |
| 566 | sender_index_ = i; |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | if (sender_index_ == -1) { |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 572 | VLOG(1) << "Too many senders, starting to bail."; |
| 573 | return; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | ::aos::ipc_lib::Sender *s = memory_->GetSender(sender_index_); |
| 577 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 578 | // Indicate that we are now alive by taking over the slot. If the previous |
| 579 | // owner died, we still want to do this. |
| 580 | death_notification_init(&(s->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | LocklessQueue::Sender::~Sender() { |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 584 | if (valid()) { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 585 | death_notification_release(&(memory_->GetSender(sender_index_)->tid)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 589 | std::optional<LocklessQueue::Sender> LocklessQueue::MakeSender() { |
| 590 | LocklessQueue::Sender result = LocklessQueue::Sender(memory_); |
| 591 | if (result.valid()) { |
| 592 | return std::move(result); |
| 593 | } else { |
| 594 | return std::nullopt; |
| 595 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | QueueIndex ZeroOrValid(QueueIndex index) { |
| 599 | if (!index.valid()) { |
| 600 | return index.Clear(); |
| 601 | } |
| 602 | return index; |
| 603 | } |
| 604 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 605 | size_t LocklessQueue::Sender::size() { return memory_->message_data_size(); } |
| 606 | |
| 607 | void *LocklessQueue::Sender::Data() { |
| 608 | ::aos::ipc_lib::Sender *sender = memory_->GetSender(sender_index_); |
| 609 | Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 610 | Message *message = memory_->GetMessage(scratch_index); |
| 611 | message->header.queue_index.Invalidate(); |
| 612 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 613 | return message->data(memory_->message_data_size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 616 | void LocklessQueue::Sender::Send( |
| 617 | const char *data, size_t length, |
| 618 | aos::monotonic_clock::time_point monotonic_remote_time, |
| 619 | aos::realtime_clock::time_point realtime_remote_time, |
| 620 | uint32_t remote_queue_index, |
| 621 | aos::monotonic_clock::time_point *monotonic_sent_time, |
| 622 | aos::realtime_clock::time_point *realtime_sent_time, |
| 623 | uint32_t *queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 624 | CHECK_LE(length, size()); |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 625 | // Flatbuffers write from the back of the buffer to the front. If we are |
| 626 | // going to write an explicit chunk of memory into the buffer, we need to |
| 627 | // adhere to this convention and place it at the end. |
| 628 | memcpy((reinterpret_cast<char *>(Data()) + size() - length), data, length); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 629 | Send(length, monotonic_remote_time, realtime_remote_time, remote_queue_index, |
| 630 | monotonic_sent_time, realtime_sent_time, queue_index); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 633 | void LocklessQueue::Sender::Send( |
| 634 | size_t length, aos::monotonic_clock::time_point monotonic_remote_time, |
| 635 | aos::realtime_clock::time_point realtime_remote_time, |
| 636 | uint32_t remote_queue_index, |
| 637 | aos::monotonic_clock::time_point *monotonic_sent_time, |
| 638 | aos::realtime_clock::time_point *realtime_sent_time, |
| 639 | uint32_t *queue_index) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 640 | const size_t queue_size = memory_->queue_size(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 641 | CHECK_LE(length, size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 642 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 643 | ::aos::ipc_lib::Sender *const sender = memory_->GetSender(sender_index_); |
| 644 | // We can do a relaxed load on our sender because we're the only person |
| 645 | // modifying it right now. |
| 646 | const Index scratch_index = sender->scratch_index.RelaxedLoad(); |
| 647 | Message *const message = memory_->GetMessage(scratch_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 648 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 649 | message->header.length = length; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 650 | // Pass these through. Any alternative behavior can be implemented out a |
| 651 | // layer. |
| 652 | message->header.remote_queue_index = remote_queue_index; |
| 653 | message->header.monotonic_remote_time = monotonic_remote_time; |
| 654 | message->header.realtime_remote_time = realtime_remote_time; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 655 | |
| 656 | while (true) { |
| 657 | const QueueIndex actual_next_queue_index = |
| 658 | memory_->next_queue_index.Load(queue_size); |
| 659 | const QueueIndex next_queue_index = ZeroOrValid(actual_next_queue_index); |
| 660 | |
| 661 | const QueueIndex incremented_queue_index = next_queue_index.Increment(); |
| 662 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 663 | // This needs to synchronize with whoever the previous writer at this |
| 664 | // location was. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 665 | const Index to_replace = memory_->LoadIndex(next_queue_index); |
| 666 | |
| 667 | const QueueIndex decremented_queue_index = |
| 668 | next_queue_index.DecrementBy(queue_size); |
| 669 | |
| 670 | // See if we got beat. If we did, try to atomically update |
| 671 | // next_queue_index in case the previous writer failed and retry. |
| 672 | if (!to_replace.IsPlausible(decremented_queue_index)) { |
| 673 | // We don't care about the result. It will either succeed, or we got |
| 674 | // beat in fixing it and just need to give up and try again. If we got |
| 675 | // beat multiple times, the only way progress can be made is if the queue |
| 676 | // is updated as well. This means that if we retry reading |
| 677 | // next_queue_index, we will be at most off by one and can retry. |
| 678 | // |
| 679 | // Both require no further action from us. |
| 680 | // |
| 681 | // TODO(austin): If we are having fairness issues under contention, we |
| 682 | // could have a mode bit in next_queue_index, and could use a lock or some |
| 683 | // other form of PI boosting to let the higher priority task win. |
| 684 | memory_->next_queue_index.CompareAndExchangeStrong( |
| 685 | actual_next_queue_index, incremented_queue_index); |
| 686 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 687 | VLOG(3) << "We were beat. Try again. Was " << std::hex |
| 688 | << to_replace.get() << ", is " << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 689 | continue; |
| 690 | } |
| 691 | |
| 692 | // Confirm that the message is what it should be. |
| 693 | { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 694 | const QueueIndex previous_index = |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 695 | memory_->GetMessage(to_replace)->header.queue_index.Load(queue_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 696 | if (previous_index != decremented_queue_index && previous_index.valid()) { |
| 697 | // Retry. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 698 | VLOG(3) << "Something fishy happened, queue index doesn't match. " |
| 699 | "Retrying. Previous index was " |
| 700 | << std::hex << previous_index.index() << ", should be " |
| 701 | << decremented_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 702 | continue; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | message->header.monotonic_sent_time = ::aos::monotonic_clock::now(); |
| 707 | message->header.realtime_sent_time = ::aos::realtime_clock::now(); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 708 | if (monotonic_sent_time != nullptr) { |
| 709 | *monotonic_sent_time = message->header.monotonic_sent_time; |
| 710 | } |
| 711 | if (realtime_sent_time != nullptr) { |
| 712 | *realtime_sent_time = message->header.realtime_sent_time; |
| 713 | } |
| 714 | if (queue_index != nullptr) { |
| 715 | *queue_index = next_queue_index.index(); |
| 716 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 717 | |
| 718 | // Before we are fully done filling out the message, update the Sender state |
| 719 | // with the new index to write. This re-uses the barrier for the |
| 720 | // queue_index store. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 721 | const Index index_to_write(next_queue_index, scratch_index.message_index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 722 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 723 | aos_compiler_memory_barrier(); |
| 724 | // We're the only person who cares about our scratch index, besides somebody |
| 725 | // cleaning up after us. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 726 | sender->scratch_index.RelaxedStore(index_to_write); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 727 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 728 | |
| 729 | message->header.queue_index.Store(next_queue_index); |
| 730 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 731 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 732 | // The message is now filled out, and we have a confirmed slot to store |
| 733 | // into. |
| 734 | // |
| 735 | // 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] | 736 | // was Invalid before now. Only person who will read this is whoever cleans |
| 737 | // up after us, so no synchronization necessary. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 738 | sender->to_replace.RelaxedStore(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 739 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 740 | |
| 741 | // Then exchange the next index into the queue. |
| 742 | if (!memory_->GetQueue(next_queue_index.Wrapped()) |
| 743 | ->CompareAndExchangeStrong(to_replace, index_to_write)) { |
| 744 | // Aw, didn't succeed. Retry. |
| 745 | sender->to_replace.RelaxedInvalidate(); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 746 | aos_compiler_memory_barrier(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 747 | VLOG(3) << "Failed to wrap into queue"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 748 | continue; |
| 749 | } |
| 750 | |
| 751 | // Then update next_queue_index to save the next user some computation time. |
| 752 | memory_->next_queue_index.CompareAndExchangeStrong(actual_next_queue_index, |
| 753 | incremented_queue_index); |
| 754 | |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 755 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 756 | // Now update the scratch space and record that we succeeded. |
| 757 | sender->scratch_index.Store(to_replace); |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 758 | aos_compiler_memory_barrier(); |
| 759 | // And then record that we succeeded, but definitely after the above store. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 760 | sender->to_replace.RelaxedInvalidate(); |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | LocklessQueue::ReadResult LocklessQueue::Read( |
| 766 | uint32_t uint32_queue_index, |
| 767 | ::aos::monotonic_clock::time_point *monotonic_sent_time, |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 768 | ::aos::realtime_clock::time_point *realtime_sent_time, |
| 769 | ::aos::monotonic_clock::time_point *monotonic_remote_time, |
| 770 | ::aos::realtime_clock::time_point *realtime_remote_time, |
| 771 | uint32_t *remote_queue_index, size_t *length, char *data) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 772 | const size_t queue_size = memory_->queue_size(); |
| 773 | |
| 774 | // Build up the QueueIndex. |
| 775 | const QueueIndex queue_index = |
| 776 | QueueIndex::Zero(queue_size).IncrementBy(uint32_queue_index); |
| 777 | |
| 778 | // Read the message stored at the requested location. |
| 779 | Index mi = memory_->LoadIndex(queue_index); |
| 780 | Message *m = memory_->GetMessage(mi); |
| 781 | |
| 782 | while (true) { |
| 783 | // We need to confirm that the data doesn't change while we are reading it. |
| 784 | // Do that by first confirming that the message points to the queue index we |
| 785 | // want. |
| 786 | const QueueIndex starting_queue_index = |
| 787 | m->header.queue_index.Load(queue_size); |
| 788 | if (starting_queue_index != queue_index) { |
| 789 | // If we found a message that is exactly 1 loop old, we just wrapped. |
| 790 | if (starting_queue_index == queue_index.DecrementBy(queue_size)) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 791 | VLOG(3) << "Matches: " << std::hex << starting_queue_index.index() |
| 792 | << ", " << queue_index.DecrementBy(queue_size).index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 793 | return ReadResult::NOTHING_NEW; |
| 794 | } else { |
| 795 | // Someone has re-used this message between when we pulled it out of the |
| 796 | // queue and when we grabbed its index. It is pretty hard to deduce |
| 797 | // what happened. Just try again. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 798 | Message *const new_m = memory_->GetMessage(queue_index); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 799 | if (m != new_m) { |
| 800 | m = new_m; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 801 | VLOG(3) << "Retrying, m doesn't match"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 802 | continue; |
| 803 | } |
| 804 | |
| 805 | // We have confirmed that message still points to the same message. This |
| 806 | // means that the message didn't get swapped out from under us, so |
| 807 | // starting_queue_index is correct. |
| 808 | // |
| 809 | // Either we got too far behind (signaled by this being a valid |
| 810 | // message), or this is one of the initial messages which are invalid. |
| 811 | if (starting_queue_index.valid()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 812 | VLOG(3) << "Too old. Tried for " << std::hex << queue_index.index() |
| 813 | << ", got " << starting_queue_index.index() << ", behind by " |
| 814 | << std::dec |
| 815 | << (starting_queue_index.index() - queue_index.index()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 816 | return ReadResult::TOO_OLD; |
| 817 | } |
| 818 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 819 | VLOG(3) << "Initial"; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 820 | |
| 821 | // There isn't a valid message at this location. |
| 822 | // |
| 823 | // If someone asks for one of the messages within the first go around, |
| 824 | // then they need to wait. They got ahead. Otherwise, they are |
| 825 | // asking for something crazy, like something before the beginning of |
| 826 | // the queue. Tell them that they are behind. |
| 827 | if (uint32_queue_index < memory_->queue_size()) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 828 | VLOG(3) << "Near zero, " << std::hex << uint32_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 829 | return ReadResult::NOTHING_NEW; |
| 830 | } else { |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 831 | VLOG(3) << "Not near zero, " << std::hex << uint32_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 832 | return ReadResult::TOO_OLD; |
| 833 | } |
| 834 | } |
| 835 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 836 | VLOG(3) << "Eq: " << std::hex << starting_queue_index.index() << ", " |
| 837 | << queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 838 | break; |
| 839 | } |
| 840 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 841 | // Then read the data out. Copy it all out to be deterministic and so we can |
| 842 | // make length be from either end. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 843 | *monotonic_sent_time = m->header.monotonic_sent_time; |
| 844 | *realtime_sent_time = m->header.realtime_sent_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 845 | if (m->header.remote_queue_index == 0xffffffffu) { |
| 846 | *remote_queue_index = queue_index.index(); |
| 847 | } else { |
| 848 | *remote_queue_index = m->header.remote_queue_index; |
| 849 | } |
| 850 | *monotonic_remote_time = m->header.monotonic_remote_time; |
| 851 | *realtime_remote_time = m->header.realtime_remote_time; |
Brian Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 852 | if (data) { |
| 853 | memcpy(data, m->data(memory_->message_data_size()), message_data_size()); |
| 854 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 855 | *length = m->header.length; |
| 856 | |
| 857 | // And finally, confirm that the message *still* points to the queue index we |
| 858 | // want. This means it didn't change out from under us. |
| 859 | // If something changed out from under us, we were reading it much too late in |
| 860 | // it's lifetime. |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 861 | aos_compiler_memory_barrier(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 862 | const QueueIndex final_queue_index = m->header.queue_index.Load(queue_size); |
| 863 | if (final_queue_index != queue_index) { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 864 | VLOG(3) << "Changed out from under us. Reading " << std::hex |
| 865 | << queue_index.index() << ", finished with " |
| 866 | << final_queue_index.index() << ", delta: " << std::dec |
| 867 | << (final_queue_index.index() - queue_index.index()); |
| 868 | return ReadResult::OVERWROTE; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | return ReadResult::GOOD; |
| 872 | } |
| 873 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 874 | size_t LocklessQueue::queue_size() const { return memory_->queue_size(); } |
| 875 | size_t LocklessQueue::message_data_size() const { |
| 876 | return memory_->message_data_size(); |
| 877 | } |
| 878 | |
| 879 | QueueIndex LocklessQueue::LatestQueueIndex() { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 880 | const size_t queue_size = memory_->queue_size(); |
| 881 | |
| 882 | // There is only one interesting case. We need to know if the queue is empty. |
| 883 | // That is done with a sentinel value. At worst, this will be off by one. |
| 884 | const QueueIndex next_queue_index = |
| 885 | memory_->next_queue_index.Load(queue_size); |
| 886 | if (next_queue_index.valid()) { |
| 887 | const QueueIndex current_queue_index = next_queue_index.DecrementBy(1u); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 888 | return current_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 889 | } else { |
| 890 | return empty_queue_index(); |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | namespace { |
| 895 | |
| 896 | // Prints out the mutex state. Not safe to use while the mutex is being |
| 897 | // changed. |
| 898 | ::std::string PrintMutex(aos_mutex *mutex) { |
| 899 | ::std::stringstream s; |
| 900 | s << "aos_mutex(" << ::std::hex << mutex->futex; |
| 901 | |
| 902 | if (mutex->futex != 0) { |
| 903 | s << ":"; |
| 904 | if (mutex->futex & FUTEX_OWNER_DIED) { |
| 905 | s << "FUTEX_OWNER_DIED|"; |
| 906 | } |
| 907 | s << "tid=" << (mutex->futex & FUTEX_TID_MASK); |
| 908 | } |
| 909 | |
| 910 | s << ")"; |
| 911 | return s.str(); |
| 912 | } |
| 913 | |
| 914 | } // namespace |
| 915 | |
| 916 | void PrintLocklessQueueMemory(LocklessQueueMemory *memory) { |
| 917 | const size_t queue_size = memory->queue_size(); |
| 918 | ::std::cout << "LocklessQueueMemory (" << memory << ") {" << ::std::endl; |
| 919 | ::std::cout << " aos_mutex queue_setup_lock = " |
| 920 | << PrintMutex(&memory->queue_setup_lock) << ::std::endl; |
Brian Silverman | fafe1fa | 2019-12-18 21:42:18 -0800 | [diff] [blame] | 921 | ::std::cout << " bool initialized = " << memory->initialized << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 922 | ::std::cout << " config {" << ::std::endl; |
| 923 | ::std::cout << " size_t num_watchers = " << memory->config.num_watchers |
| 924 | << ::std::endl; |
| 925 | ::std::cout << " size_t num_senders = " << memory->config.num_senders |
| 926 | << ::std::endl; |
| 927 | ::std::cout << " size_t queue_size = " << memory->config.queue_size |
| 928 | << ::std::endl; |
| 929 | ::std::cout << " size_t message_data_size = " |
| 930 | << memory->config.message_data_size << ::std::endl; |
| 931 | |
| 932 | ::std::cout << " AtomicQueueIndex next_queue_index = " |
| 933 | << memory->next_queue_index.Load(queue_size).DebugString() |
| 934 | << ::std::endl; |
| 935 | |
Austin Schuh | 3328d13 | 2020-02-28 13:54:57 -0800 | [diff] [blame] | 936 | ::std::cout << " uid_t uid = " << memory->uid << ::std::endl; |
| 937 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 938 | ::std::cout << " }" << ::std::endl; |
| 939 | ::std::cout << " AtomicIndex queue[" << queue_size << "] {" << ::std::endl; |
| 940 | for (size_t i = 0; i < queue_size; ++i) { |
| 941 | ::std::cout << " [" << i << "] -> " |
| 942 | << memory->GetQueue(i)->Load().DebugString() << ::std::endl; |
| 943 | } |
| 944 | ::std::cout << " }" << ::std::endl; |
| 945 | ::std::cout << " Message messages[" << memory->num_messages() << "] {" |
| 946 | << ::std::endl; |
| 947 | for (size_t i = 0; i < memory->num_messages(); ++i) { |
| 948 | Message *m = memory->GetMessage(Index(i, i)); |
| 949 | ::std::cout << " [" << i << "] -> Message {" << ::std::endl; |
| 950 | ::std::cout << " Header {" << ::std::endl; |
| 951 | ::std::cout << " AtomicQueueIndex queue_index = " |
| 952 | << m->header.queue_index.Load(queue_size).DebugString() |
| 953 | << ::std::endl; |
| 954 | ::std::cout << " size_t length = " << m->header.length |
| 955 | << ::std::endl; |
| 956 | ::std::cout << " }" << ::std::endl; |
| 957 | ::std::cout << " data: {"; |
| 958 | |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 959 | const char *const m_data = m->data(memory->message_data_size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 960 | for (size_t j = 0; j < m->header.length; ++j) { |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 961 | char data = m_data[j]; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 962 | if (j != 0) { |
| 963 | ::std::cout << " "; |
| 964 | } |
| 965 | if (::std::isprint(data)) { |
| 966 | ::std::cout << ::std::setfill(' ') << ::std::setw(2) << ::std::hex |
| 967 | << data; |
| 968 | } else { |
| 969 | ::std::cout << "0x" << ::std::setfill('0') << ::std::setw(2) |
| 970 | << ::std::hex << (static_cast<unsigned>(data) & 0xff); |
| 971 | } |
| 972 | } |
| 973 | ::std::cout << ::std::setfill(' ') << ::std::dec << "}" << ::std::endl; |
| 974 | ::std::cout << " }," << ::std::endl; |
| 975 | } |
| 976 | ::std::cout << " }" << ::std::endl; |
| 977 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 978 | ::std::cout << " Sender senders[" << memory->num_senders() << "] {" |
| 979 | << ::std::endl; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 980 | for (size_t i = 0; i < memory->num_senders(); ++i) { |
| 981 | Sender *s = memory->GetSender(i); |
| 982 | ::std::cout << " [" << i << "] -> Sender {" << ::std::endl; |
| 983 | ::std::cout << " aos_mutex tid = " << PrintMutex(&s->tid) |
| 984 | << ::std::endl; |
| 985 | ::std::cout << " AtomicIndex scratch_index = " |
| 986 | << s->scratch_index.Load().DebugString() << ::std::endl; |
| 987 | ::std::cout << " AtomicIndex to_replace = " |
| 988 | << s->to_replace.Load().DebugString() << ::std::endl; |
| 989 | ::std::cout << " }" << ::std::endl; |
| 990 | } |
| 991 | ::std::cout << " }" << ::std::endl; |
| 992 | |
| 993 | ::std::cout << " Watcher watchers[" << memory->num_watchers() << "] {" |
| 994 | << ::std::endl; |
| 995 | for (size_t i = 0; i < memory->num_watchers(); ++i) { |
| 996 | Watcher *w = memory->GetWatcher(i); |
| 997 | ::std::cout << " [" << i << "] -> Watcher {" << ::std::endl; |
| 998 | ::std::cout << " aos_mutex tid = " << PrintMutex(&w->tid) |
| 999 | << ::std::endl; |
| 1000 | ::std::cout << " pid_t pid = " << w->pid << ::std::endl; |
| 1001 | ::std::cout << " int priority = " << w->priority << ::std::endl; |
| 1002 | ::std::cout << " }" << ::std::endl; |
| 1003 | } |
| 1004 | ::std::cout << " }" << ::std::endl; |
| 1005 | |
| 1006 | ::std::cout << "}" << ::std::endl; |
| 1007 | } |
| 1008 | |
| 1009 | } // namespace ipc_lib |
| 1010 | } // namespace aos |