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