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