Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #include "aos/ipc_lib/queue_racer.h" |
| 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 3 | #include <cinttypes> |
| 4 | #include <cstring> |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 5 | #include <limits> |
| 6 | |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 7 | #include "aos/ipc_lib/event.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 8 | #include "gtest/gtest.h" |
| 9 | |
| 10 | namespace aos { |
| 11 | namespace ipc_lib { |
| 12 | namespace { |
| 13 | |
| 14 | struct ThreadPlusCount { |
| 15 | int thread; |
| 16 | uint64_t count; |
| 17 | }; |
| 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | struct ThreadState { |
| 22 | ::std::thread thread; |
| 23 | Event ready; |
| 24 | uint64_t event_count = ::std::numeric_limits<uint64_t>::max(); |
| 25 | }; |
| 26 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 27 | QueueRacer::QueueRacer(LocklessQueue queue, int num_threads, |
| 28 | uint64_t num_messages) |
| 29 | : queue_(queue), num_threads_(num_threads), num_messages_(num_messages) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 30 | Reset(); |
| 31 | } |
| 32 | |
| 33 | void QueueRacer::RunIteration(bool race_reads, int write_wrap_count) { |
| 34 | const bool will_wrap = num_messages_ * num_threads_ * |
| 35 | static_cast<uint64_t>(1 + write_wrap_count) > |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 36 | queue_.config().queue_size; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 37 | |
| 38 | // Clear out shmem. |
| 39 | Reset(); |
| 40 | started_writes_ = 0; |
| 41 | finished_writes_ = 0; |
| 42 | |
| 43 | // Event used to start all the threads processing at once. |
| 44 | Event run; |
| 45 | |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 46 | ::std::atomic<bool> poll_index{true}; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 47 | |
| 48 | // List of threads. |
| 49 | ::std::vector<ThreadState> threads(num_threads_); |
| 50 | |
| 51 | ::std::thread queue_index_racer([this, &poll_index]() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 52 | LocklessQueueReader reader(queue_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 53 | |
| 54 | // Track the number of times we wrap, and cache the modulo. |
| 55 | uint64_t wrap_count = 0; |
| 56 | uint32_t last_queue_index = 0; |
| 57 | const uint32_t max_queue_index = |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 58 | QueueIndex::MaxIndex(0xffffffffu, queue_.config().queue_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 59 | while (poll_index) { |
| 60 | // We want to read everything backwards. This will give us conservative |
| 61 | // bounds. And with enough time and randomness, we will see all the cases |
| 62 | // we care to see. |
| 63 | |
| 64 | // These 3 numbers look at the same thing, but at different points of time |
| 65 | // in the process. The process (essentially) looks like: |
| 66 | // |
| 67 | // ++started_writes; |
| 68 | // ++latest_queue_index; |
| 69 | // ++finished_writes; |
| 70 | // |
| 71 | // We want to check that latest_queue_index is bounded by the number of |
| 72 | // writes started and finished. Basically, we can say that |
| 73 | // finished_writes < latest_queue_index always. And |
| 74 | // latest_queue_index < started_writes. And everything always increases. |
| 75 | // So, if we let more time elapse between sampling finished_writes and |
| 76 | // latest_queue_index, we will only be relaxing our bounds, not |
| 77 | // invalidating the check. The same goes for started_writes. |
| 78 | // |
| 79 | // So, grab them in order. |
| 80 | const uint64_t finished_writes = finished_writes_.load(); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 81 | const QueueIndex latest_queue_index_queue_index = reader.LatestIndex(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 82 | const uint64_t started_writes = started_writes_.load(); |
| 83 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | const uint32_t latest_queue_index_uint32_t = |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 85 | latest_queue_index_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 86 | uint64_t latest_queue_index = latest_queue_index_uint32_t; |
| 87 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 88 | if (latest_queue_index_queue_index != QueueIndex::Invalid()) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 89 | // If we got smaller, we wrapped. |
| 90 | if (latest_queue_index_uint32_t < last_queue_index) { |
| 91 | ++wrap_count; |
| 92 | } |
| 93 | // And apply it. |
| 94 | latest_queue_index += |
| 95 | static_cast<uint64_t>(max_queue_index) * wrap_count; |
| 96 | last_queue_index = latest_queue_index_uint32_t; |
| 97 | } |
| 98 | |
| 99 | // For grins, check that we have always started more than we finished. |
| 100 | // Should never fail. |
| 101 | EXPECT_GE(started_writes, finished_writes); |
| 102 | |
| 103 | // If we are at the beginning, the queue needs to always return empty. |
| 104 | if (started_writes == 0) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 105 | EXPECT_EQ(latest_queue_index_queue_index, QueueIndex::Invalid()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 106 | EXPECT_EQ(finished_writes, 0); |
| 107 | } else { |
| 108 | if (finished_writes == 0) { |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 109 | // Plausible to be at the beginning, in which case we don't have |
| 110 | // anything to check. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 111 | if (latest_queue_index_queue_index != QueueIndex::Invalid()) { |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 112 | // Otherwise, we have started. The queue can't have any more |
| 113 | // entries than this. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 114 | EXPECT_GE(started_writes, latest_queue_index + 1); |
| 115 | } |
| 116 | } else { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 117 | EXPECT_NE(latest_queue_index_queue_index, QueueIndex::Invalid()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 118 | // latest_queue_index is an index, not a count. So it always reads 1 |
| 119 | // low. |
| 120 | EXPECT_GE(latest_queue_index + 1, finished_writes); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | // Build up each thread and kick it off. |
| 127 | int thread_index = 0; |
| 128 | for (ThreadState &t : threads) { |
| 129 | if (will_wrap) { |
| 130 | t.event_count = ::std::numeric_limits<uint64_t>::max(); |
| 131 | } else { |
| 132 | t.event_count = 0; |
| 133 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 134 | t.thread = ::std::thread([this, &t, thread_index, &run, |
| 135 | write_wrap_count]() { |
| 136 | // Build up a sender. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 137 | LocklessQueueSender sender = LocklessQueueSender::Make(queue_).value(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 138 | CHECK_GE(sender.size(), sizeof(ThreadPlusCount)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 139 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 140 | // Signal that we are ready to start sending. |
| 141 | t.ready.Set(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 142 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 143 | // Wait until signaled to start running. |
| 144 | run.Wait(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 145 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 146 | // Gogogo! |
| 147 | for (uint64_t i = 0; |
| 148 | i < num_messages_ * static_cast<uint64_t>(1 + write_wrap_count); |
| 149 | ++i) { |
| 150 | char *const data = static_cast<char *>(sender.Data()) + sender.size() - |
| 151 | sizeof(ThreadPlusCount); |
| 152 | const char fill = (i + 55) & 0xFF; |
| 153 | memset(data, fill, sizeof(ThreadPlusCount)); |
| 154 | { |
| 155 | bool found_nonzero = false; |
| 156 | for (size_t i = 0; i < sizeof(ThreadPlusCount); ++i) { |
| 157 | if (data[i] != fill) { |
| 158 | found_nonzero = true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 159 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 160 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 161 | CHECK(!found_nonzero) << ": Somebody else is writing to our buffer"; |
| 162 | } |
| 163 | |
| 164 | ThreadPlusCount tpc; |
| 165 | tpc.thread = thread_index; |
| 166 | tpc.count = i; |
| 167 | |
| 168 | memcpy(data, &tpc, sizeof(ThreadPlusCount)); |
| 169 | |
| 170 | if (i % 0x800000 == 0x100000) { |
| 171 | fprintf( |
| 172 | stderr, "Sent %" PRIu64 ", %f %%\n", i, |
| 173 | static_cast<double>(i) / |
| 174 | static_cast<double>(num_messages_ * (1 + write_wrap_count)) * |
| 175 | 100.0); |
| 176 | } |
| 177 | |
| 178 | ++started_writes_; |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 179 | sender.Send(sizeof(ThreadPlusCount), aos::monotonic_clock::min_time, |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 180 | aos::realtime_clock::min_time, 0xffffffff, UUID::Zero(), |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 181 | nullptr, nullptr, nullptr); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 182 | // Blank out the new scratch buffer, to catch other people using it. |
| 183 | { |
| 184 | char *const new_data = static_cast<char *>(sender.Data()) + |
| 185 | sender.size() - sizeof(ThreadPlusCount); |
| 186 | const char new_fill = ~fill; |
| 187 | memset(new_data, new_fill, sizeof(ThreadPlusCount)); |
| 188 | } |
| 189 | ++finished_writes_; |
| 190 | } |
| 191 | }); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 192 | ++thread_index; |
| 193 | } |
| 194 | |
| 195 | // Wait until all the threads are ready. |
| 196 | for (ThreadState &t : threads) { |
| 197 | t.ready.Wait(); |
| 198 | } |
| 199 | |
| 200 | // And start them racing. |
| 201 | run.Set(); |
| 202 | |
| 203 | // Let all the threads finish before reading if we are supposed to not be |
| 204 | // racing reads. |
| 205 | if (!race_reads) { |
| 206 | for (ThreadState &t : threads) { |
| 207 | t.thread.join(); |
| 208 | } |
| 209 | poll_index = false; |
| 210 | queue_index_racer.join(); |
| 211 | } |
| 212 | |
| 213 | CheckReads(race_reads, write_wrap_count, &threads); |
| 214 | |
| 215 | // Reap all the threads. |
| 216 | if (race_reads) { |
| 217 | for (ThreadState &t : threads) { |
| 218 | t.thread.join(); |
| 219 | } |
| 220 | poll_index = false; |
| 221 | queue_index_racer.join(); |
| 222 | } |
| 223 | |
| 224 | // Confirm that the number of writes matches the expected number of writes. |
| 225 | ASSERT_EQ(num_threads_ * num_messages_ * (1 + write_wrap_count), |
| 226 | started_writes_); |
| 227 | ASSERT_EQ(num_threads_ * num_messages_ * (1 + write_wrap_count), |
| 228 | finished_writes_); |
| 229 | |
| 230 | // And that every thread sent the right number of messages. |
| 231 | for (ThreadState &t : threads) { |
| 232 | if (will_wrap) { |
| 233 | if (!race_reads) { |
| 234 | // If we are wrapping, there is a possibility that a thread writes |
| 235 | // everything *before* we can read any of it, and it all gets |
| 236 | // overwritten. |
| 237 | ASSERT_TRUE(t.event_count == ::std::numeric_limits<uint64_t>::max() || |
| 238 | t.event_count == (1 + write_wrap_count) * num_messages_) |
| 239 | << ": Got " << t.event_count << " events, expected " |
| 240 | << (1 + write_wrap_count) * num_messages_; |
| 241 | } |
| 242 | } else { |
| 243 | ASSERT_EQ(t.event_count, num_messages_); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void QueueRacer::CheckReads(bool race_reads, int write_wrap_count, |
| 249 | ::std::vector<ThreadState> *threads) { |
| 250 | // Now read back the results to double check. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 251 | LocklessQueueReader reader(queue_); |
| 252 | const bool will_wrap = num_messages_ * num_threads_ * (1 + write_wrap_count) > |
| 253 | LocklessQueueSize(queue_.memory()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 254 | |
| 255 | monotonic_clock::time_point last_monotonic_sent_time = |
| 256 | monotonic_clock::epoch(); |
| 257 | uint64_t initial_i = 0; |
| 258 | if (will_wrap) { |
| 259 | initial_i = (1 + write_wrap_count) * num_messages_ * num_threads_ - |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 260 | LocklessQueueSize(queue_.memory()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | for (uint64_t i = initial_i; |
| 264 | i < (1 + write_wrap_count) * num_messages_ * num_threads_; ++i) { |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 265 | monotonic_clock::time_point monotonic_sent_time; |
| 266 | realtime_clock::time_point realtime_sent_time; |
| 267 | monotonic_clock::time_point monotonic_remote_time; |
| 268 | realtime_clock::time_point realtime_remote_time; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 269 | UUID source_boot_uuid; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 270 | uint32_t remote_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 271 | size_t length; |
| 272 | char read_data[1024]; |
| 273 | |
| 274 | // Handle overflowing the message count for the wrap test. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 275 | const uint32_t wrapped_i = |
| 276 | i % static_cast<size_t>(QueueIndex::MaxIndex( |
| 277 | 0xffffffffu, LocklessQueueSize(queue_.memory()))); |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 278 | LocklessQueueReader::Result read_result = reader.Read( |
| 279 | wrapped_i, &monotonic_sent_time, &realtime_sent_time, |
| 280 | &monotonic_remote_time, &realtime_remote_time, &remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 281 | &source_boot_uuid, &length, &(read_data[0])); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 282 | |
| 283 | if (race_reads) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 284 | if (read_result == LocklessQueueReader::Result::NOTHING_NEW) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 285 | --i; |
| 286 | continue; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | if (race_reads && will_wrap) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 291 | if (read_result == LocklessQueueReader::Result::TOO_OLD) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 292 | continue; |
| 293 | } |
| 294 | } |
| 295 | // Every message should be good. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 296 | ASSERT_EQ(read_result, LocklessQueueReader::Result::GOOD) << ": i is " << i; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 297 | |
| 298 | // And, confirm that time never went backwards. |
| 299 | ASSERT_GT(monotonic_sent_time, last_monotonic_sent_time); |
| 300 | last_monotonic_sent_time = monotonic_sent_time; |
| 301 | |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 302 | EXPECT_EQ(monotonic_remote_time, aos::monotonic_clock::min_time); |
| 303 | EXPECT_EQ(realtime_remote_time, aos::realtime_clock::min_time); |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 304 | EXPECT_EQ(source_boot_uuid, UUID::Zero()); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 305 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 306 | ThreadPlusCount tpc; |
| 307 | ASSERT_EQ(length, sizeof(ThreadPlusCount)); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 308 | memcpy(&tpc, |
| 309 | read_data + LocklessQueueMessageDataSize(queue_.memory()) - length, |
Austin Schuh | 67420a4 | 2019-12-21 21:55:04 -0800 | [diff] [blame] | 310 | sizeof(ThreadPlusCount)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 311 | |
| 312 | if (will_wrap) { |
| 313 | // The queue won't chang out from under us, so we should get some amount |
| 314 | // of the tail end of the messages from a a thread. |
| 315 | // Confirm that once we get our first message, they all show up. |
| 316 | if ((*threads)[tpc.thread].event_count == |
| 317 | ::std::numeric_limits<uint64_t>::max()) { |
| 318 | (*threads)[tpc.thread].event_count = tpc.count; |
| 319 | } |
| 320 | |
| 321 | if (race_reads) { |
| 322 | // Make sure nothing goes backwards. Really not much we can do here. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 323 | ASSERT_LE((*threads)[tpc.thread].event_count, tpc.count) |
| 324 | << ": Thread " << tpc.thread; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 325 | (*threads)[tpc.thread].event_count = tpc.count; |
| 326 | } else { |
| 327 | // Make sure nothing goes backwards. Really not much we can do here. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 328 | ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count) |
| 329 | << ": Thread " << tpc.thread; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | // Confirm that we see every message counter from every thread. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 333 | ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count) |
| 334 | << ": Thread " << tpc.thread; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 335 | } |
| 336 | ++(*threads)[tpc.thread].event_count; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | } // namespace ipc_lib |
| 341 | } // namespace aos |