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 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 7 | #include "gtest/gtest.h" |
| 8 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "aos/ipc_lib/event.h" |
| 10 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 11 | namespace aos::ipc_lib { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 12 | namespace { |
| 13 | |
| 14 | struct ThreadPlusCount { |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 15 | uint64_t thread; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 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) |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 29 | : queue_(queue), |
| 30 | num_threads_(num_threads), |
| 31 | num_messages_(num_messages), |
| 32 | channel_storage_duration_(std::chrono::nanoseconds(1)), |
| 33 | expected_send_results_({LocklessQueueSender::Result::GOOD}), |
| 34 | check_writes_and_reads_(true) { |
| 35 | Reset(); |
| 36 | } |
| 37 | |
| 38 | QueueRacer::QueueRacer(LocklessQueue queue, |
| 39 | const QueueRacerConfiguration &config) |
| 40 | : queue_(queue), |
| 41 | num_threads_(config.num_threads), |
| 42 | num_messages_(config.num_messages), |
| 43 | channel_storage_duration_(config.channel_storage_duration), |
| 44 | expected_send_results_(config.expected_send_results), |
| 45 | check_writes_and_reads_(config.check_writes_and_reads) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 46 | Reset(); |
| 47 | } |
| 48 | |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 49 | void QueueRacer::RunIteration(bool race_reads, int write_wrap_count, |
| 50 | bool set_should_read, bool should_read_result) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 51 | const bool will_wrap = num_messages_ * num_threads_ * |
| 52 | static_cast<uint64_t>(1 + write_wrap_count) > |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 53 | queue_.config().queue_size; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 54 | |
| 55 | // Clear out shmem. |
| 56 | Reset(); |
| 57 | started_writes_ = 0; |
| 58 | finished_writes_ = 0; |
| 59 | |
| 60 | // Event used to start all the threads processing at once. |
| 61 | Event run; |
| 62 | |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 63 | ::std::atomic<bool> poll_index{true}; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 64 | |
| 65 | // List of threads. |
| 66 | ::std::vector<ThreadState> threads(num_threads_); |
| 67 | |
| 68 | ::std::thread queue_index_racer([this, &poll_index]() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 69 | LocklessQueueReader reader(queue_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 70 | |
| 71 | // Track the number of times we wrap, and cache the modulo. |
| 72 | uint64_t wrap_count = 0; |
| 73 | uint32_t last_queue_index = 0; |
| 74 | const uint32_t max_queue_index = |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 75 | QueueIndex::MaxIndex(0xffffffffu, queue_.config().queue_size); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 76 | while (poll_index) { |
| 77 | // We want to read everything backwards. This will give us conservative |
| 78 | // bounds. And with enough time and randomness, we will see all the cases |
| 79 | // we care to see. |
| 80 | |
| 81 | // These 3 numbers look at the same thing, but at different points of time |
| 82 | // in the process. The process (essentially) looks like: |
| 83 | // |
| 84 | // ++started_writes; |
| 85 | // ++latest_queue_index; |
| 86 | // ++finished_writes; |
| 87 | // |
| 88 | // We want to check that latest_queue_index is bounded by the number of |
| 89 | // writes started and finished. Basically, we can say that |
| 90 | // finished_writes < latest_queue_index always. And |
| 91 | // latest_queue_index < started_writes. And everything always increases. |
| 92 | // So, if we let more time elapse between sampling finished_writes and |
| 93 | // latest_queue_index, we will only be relaxing our bounds, not |
| 94 | // invalidating the check. The same goes for started_writes. |
| 95 | // |
| 96 | // So, grab them in order. |
| 97 | const uint64_t finished_writes = finished_writes_.load(); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 98 | const QueueIndex latest_queue_index_queue_index = reader.LatestIndex(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 99 | const uint64_t started_writes = started_writes_.load(); |
| 100 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 101 | const uint32_t latest_queue_index_uint32_t = |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 102 | latest_queue_index_queue_index.index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 103 | uint64_t latest_queue_index = latest_queue_index_uint32_t; |
| 104 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 105 | if (latest_queue_index_queue_index != QueueIndex::Invalid()) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 106 | // If we got smaller, we wrapped. |
| 107 | if (latest_queue_index_uint32_t < last_queue_index) { |
| 108 | ++wrap_count; |
| 109 | } |
| 110 | // And apply it. |
| 111 | latest_queue_index += |
| 112 | static_cast<uint64_t>(max_queue_index) * wrap_count; |
| 113 | last_queue_index = latest_queue_index_uint32_t; |
| 114 | } |
| 115 | |
| 116 | // For grins, check that we have always started more than we finished. |
| 117 | // Should never fail. |
| 118 | EXPECT_GE(started_writes, finished_writes); |
| 119 | |
| 120 | // If we are at the beginning, the queue needs to always return empty. |
| 121 | if (started_writes == 0) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 122 | EXPECT_EQ(latest_queue_index_queue_index, QueueIndex::Invalid()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 123 | EXPECT_EQ(finished_writes, 0); |
| 124 | } else { |
| 125 | if (finished_writes == 0) { |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 126 | // Plausible to be at the beginning, in which case we don't have |
| 127 | // anything to check. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 128 | if (latest_queue_index_queue_index != QueueIndex::Invalid()) { |
Brian Silverman | d05b819 | 2019-12-22 01:06:56 -0800 | [diff] [blame] | 129 | // Otherwise, we have started. The queue can't have any more |
| 130 | // entries than this. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 131 | EXPECT_GE(started_writes, latest_queue_index + 1); |
| 132 | } |
| 133 | } else { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 134 | EXPECT_NE(latest_queue_index_queue_index, QueueIndex::Invalid()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 135 | // latest_queue_index is an index, not a count. So it always reads 1 |
| 136 | // low. |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 137 | if (check_writes_and_reads_) { |
| 138 | EXPECT_GE(latest_queue_index + 1, finished_writes); |
| 139 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | }); |
| 144 | |
| 145 | // Build up each thread and kick it off. |
| 146 | int thread_index = 0; |
| 147 | for (ThreadState &t : threads) { |
| 148 | if (will_wrap) { |
| 149 | t.event_count = ::std::numeric_limits<uint64_t>::max(); |
| 150 | } else { |
| 151 | t.event_count = 0; |
| 152 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 153 | t.thread = ::std::thread([this, &t, thread_index, &run, |
| 154 | write_wrap_count]() { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 155 | LocklessQueueSender sender = |
| 156 | LocklessQueueSender::Make(queue_, channel_storage_duration_).value(); |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 157 | CHECK_GE(sender.size(), sizeof(ThreadPlusCount)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 158 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 159 | // Signal that we are ready to start sending. |
| 160 | t.ready.Set(); |
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 | // Wait until signaled to start running. |
| 163 | run.Wait(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 164 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 165 | // Gogogo! |
| 166 | for (uint64_t i = 0; |
| 167 | i < num_messages_ * static_cast<uint64_t>(1 + write_wrap_count); |
| 168 | ++i) { |
| 169 | char *const data = static_cast<char *>(sender.Data()) + sender.size() - |
| 170 | sizeof(ThreadPlusCount); |
| 171 | const char fill = (i + 55) & 0xFF; |
| 172 | memset(data, fill, sizeof(ThreadPlusCount)); |
| 173 | { |
| 174 | bool found_nonzero = false; |
| 175 | for (size_t i = 0; i < sizeof(ThreadPlusCount); ++i) { |
| 176 | if (data[i] != fill) { |
| 177 | found_nonzero = true; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 178 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 179 | } |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 180 | CHECK(!found_nonzero) << ": Somebody else is writing to our buffer"; |
| 181 | } |
| 182 | |
| 183 | ThreadPlusCount tpc; |
| 184 | tpc.thread = thread_index; |
| 185 | tpc.count = i; |
| 186 | |
| 187 | memcpy(data, &tpc, sizeof(ThreadPlusCount)); |
| 188 | |
| 189 | if (i % 0x800000 == 0x100000) { |
| 190 | fprintf( |
| 191 | stderr, "Sent %" PRIu64 ", %f %%\n", i, |
| 192 | static_cast<double>(i) / |
| 193 | static_cast<double>(num_messages_ * (1 + write_wrap_count)) * |
| 194 | 100.0); |
| 195 | } |
| 196 | |
| 197 | ++started_writes_; |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 198 | auto result = |
| 199 | sender.Send(sizeof(ThreadPlusCount), aos::monotonic_clock::min_time, |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 200 | aos::realtime_clock::min_time, 0xffffffff, |
| 201 | UUID::FromSpan(absl::Span<const uint8_t>( |
| 202 | reinterpret_cast<const uint8_t *>(&tpc), |
| 203 | sizeof(ThreadPlusCount))), |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 204 | nullptr, nullptr, nullptr); |
| 205 | |
| 206 | CHECK(std::find(expected_send_results_.begin(), |
| 207 | expected_send_results_.end(), |
| 208 | result) != expected_send_results_.end()) |
| 209 | << "Unexpected send result: " << result; |
| 210 | |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 211 | // Blank out the new scratch buffer, to catch other people using it. |
| 212 | { |
| 213 | char *const new_data = static_cast<char *>(sender.Data()) + |
| 214 | sender.size() - sizeof(ThreadPlusCount); |
| 215 | const char new_fill = ~fill; |
| 216 | memset(new_data, new_fill, sizeof(ThreadPlusCount)); |
| 217 | } |
| 218 | ++finished_writes_; |
| 219 | } |
| 220 | }); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 221 | ++thread_index; |
| 222 | } |
| 223 | |
| 224 | // Wait until all the threads are ready. |
| 225 | for (ThreadState &t : threads) { |
| 226 | t.ready.Wait(); |
| 227 | } |
| 228 | |
| 229 | // And start them racing. |
| 230 | run.Set(); |
| 231 | |
| 232 | // Let all the threads finish before reading if we are supposed to not be |
| 233 | // racing reads. |
| 234 | if (!race_reads) { |
| 235 | for (ThreadState &t : threads) { |
| 236 | t.thread.join(); |
| 237 | } |
| 238 | poll_index = false; |
| 239 | queue_index_racer.join(); |
| 240 | } |
| 241 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 242 | if (check_writes_and_reads_) { |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 243 | CheckReads(race_reads, write_wrap_count, &threads, set_should_read, |
| 244 | should_read_result); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 245 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 246 | |
| 247 | // Reap all the threads. |
| 248 | if (race_reads) { |
| 249 | for (ThreadState &t : threads) { |
| 250 | t.thread.join(); |
| 251 | } |
| 252 | poll_index = false; |
| 253 | queue_index_racer.join(); |
| 254 | } |
| 255 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 256 | if (check_writes_and_reads_) { |
| 257 | // Confirm that the number of writes matches the expected number of writes. |
| 258 | ASSERT_EQ(num_threads_ * num_messages_ * (1 + write_wrap_count), |
| 259 | started_writes_); |
| 260 | ASSERT_EQ(num_threads_ * num_messages_ * (1 + write_wrap_count), |
| 261 | finished_writes_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 262 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 263 | // And that every thread sent the right number of messages. |
| 264 | for (ThreadState &t : threads) { |
| 265 | if (will_wrap) { |
| 266 | if (!race_reads) { |
| 267 | // If we are wrapping, there is a possibility that a thread writes |
| 268 | // everything *before* we can read any of it, and it all gets |
| 269 | // overwritten. |
| 270 | ASSERT_TRUE(t.event_count == ::std::numeric_limits<uint64_t>::max() || |
| 271 | t.event_count == (1 + write_wrap_count) * num_messages_) |
| 272 | << ": Got " << t.event_count << " events, expected " |
| 273 | << (1 + write_wrap_count) * num_messages_; |
| 274 | } |
| 275 | } else { |
| 276 | ASSERT_EQ(t.event_count, num_messages_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 277 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | void QueueRacer::CheckReads(bool race_reads, int write_wrap_count, |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 283 | ::std::vector<ThreadState> *threads, |
| 284 | bool set_should_read, bool should_read_result) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 285 | // Now read back the results to double check. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 286 | LocklessQueueReader reader(queue_); |
| 287 | const bool will_wrap = num_messages_ * num_threads_ * (1 + write_wrap_count) > |
| 288 | LocklessQueueSize(queue_.memory()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 289 | |
| 290 | monotonic_clock::time_point last_monotonic_sent_time = |
| 291 | monotonic_clock::epoch(); |
| 292 | uint64_t initial_i = 0; |
| 293 | if (will_wrap) { |
| 294 | initial_i = (1 + write_wrap_count) * num_messages_ * num_threads_ - |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 295 | LocklessQueueSize(queue_.memory()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 298 | std::function<bool(const Context &)> nop; |
| 299 | |
| 300 | Context fetched_context; |
| 301 | std::function<bool(const Context &)> should_read = |
| 302 | [&should_read_result, &fetched_context](const Context &context) { |
| 303 | fetched_context = context; |
| 304 | return should_read_result; |
| 305 | }; |
| 306 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 307 | for (uint64_t i = initial_i; |
| 308 | i < (1 + write_wrap_count) * num_messages_ * num_threads_; ++i) { |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 309 | monotonic_clock::time_point monotonic_sent_time; |
| 310 | realtime_clock::time_point realtime_sent_time; |
| 311 | monotonic_clock::time_point monotonic_remote_time; |
| 312 | realtime_clock::time_point realtime_remote_time; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 313 | UUID source_boot_uuid; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 314 | uint32_t remote_queue_index; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 315 | size_t length; |
| 316 | char read_data[1024]; |
| 317 | |
| 318 | // Handle overflowing the message count for the wrap test. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 319 | const uint32_t wrapped_i = |
| 320 | i % static_cast<size_t>(QueueIndex::MaxIndex( |
| 321 | 0xffffffffu, LocklessQueueSize(queue_.memory()))); |
Austin Schuh | 0bd410a | 2023-11-05 12:38:12 -0800 | [diff] [blame] | 322 | LocklessQueueReader::Result read_result = |
| 323 | set_should_read |
| 324 | ? reader.Read(wrapped_i, &monotonic_sent_time, &realtime_sent_time, |
| 325 | &monotonic_remote_time, &realtime_remote_time, |
| 326 | &remote_queue_index, &source_boot_uuid, &length, |
| 327 | &(read_data[0]), std::ref(should_read)) |
| 328 | : reader.Read(wrapped_i, &monotonic_sent_time, &realtime_sent_time, |
| 329 | &monotonic_remote_time, &realtime_remote_time, |
| 330 | &remote_queue_index, &source_boot_uuid, &length, |
| 331 | &(read_data[0]), nop); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 332 | |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 333 | // The code in lockless_queue.cc reads everything but data, checks that the |
| 334 | // header hasn't changed, then reads the data. So, if we succeed and both |
| 335 | // end up not being corrupted, then we've confirmed everything works. |
| 336 | // |
| 337 | // Feed in both combos of should_read and whether or not to return true or |
| 338 | // false from should_read. By capturing the header values inside the |
| 339 | // callback, we can also verify the state in the middle of the process to |
| 340 | // make sure we have the right boundaries. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 341 | if (race_reads) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 342 | if (read_result == LocklessQueueReader::Result::NOTHING_NEW) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 343 | --i; |
| 344 | continue; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (race_reads && will_wrap) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 349 | if (read_result == LocklessQueueReader::Result::TOO_OLD) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 350 | continue; |
| 351 | } |
| 352 | } |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 353 | |
| 354 | if (!set_should_read) { |
| 355 | // Every message should be good. |
| 356 | ASSERT_EQ(read_result, LocklessQueueReader::Result::GOOD) |
| 357 | << ": i is " << i; |
| 358 | } else { |
| 359 | if (should_read_result) { |
| 360 | ASSERT_EQ(read_result, LocklessQueueReader::Result::GOOD) |
| 361 | << ": i is " << i; |
| 362 | |
| 363 | ASSERT_EQ(monotonic_sent_time, fetched_context.monotonic_event_time); |
| 364 | ASSERT_EQ(realtime_sent_time, fetched_context.realtime_event_time); |
| 365 | ASSERT_EQ(monotonic_remote_time, fetched_context.monotonic_remote_time); |
| 366 | ASSERT_EQ(realtime_remote_time, fetched_context.realtime_remote_time); |
| 367 | ASSERT_EQ(source_boot_uuid, fetched_context.source_boot_uuid); |
| 368 | ASSERT_EQ(remote_queue_index, fetched_context.remote_queue_index); |
| 369 | ASSERT_EQ(length, fetched_context.size); |
| 370 | |
| 371 | ASSERT_EQ( |
| 372 | absl::Span<const uint8_t>( |
| 373 | reinterpret_cast<const uint8_t *>( |
| 374 | read_data + LocklessQueueMessageDataSize(queue_.memory()) - |
| 375 | length), |
| 376 | length), |
| 377 | source_boot_uuid.span()); |
| 378 | } else { |
| 379 | ASSERT_EQ(read_result, LocklessQueueReader::Result::FILTERED); |
| 380 | monotonic_sent_time = fetched_context.monotonic_event_time; |
| 381 | realtime_sent_time = fetched_context.realtime_event_time; |
| 382 | monotonic_remote_time = fetched_context.monotonic_remote_time; |
| 383 | realtime_remote_time = fetched_context.realtime_remote_time; |
| 384 | source_boot_uuid = fetched_context.source_boot_uuid; |
| 385 | remote_queue_index = fetched_context.remote_queue_index; |
| 386 | length = fetched_context.size; |
| 387 | } |
| 388 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 389 | |
| 390 | // And, confirm that time never went backwards. |
| 391 | ASSERT_GT(monotonic_sent_time, last_monotonic_sent_time); |
| 392 | last_monotonic_sent_time = monotonic_sent_time; |
| 393 | |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 394 | ASSERT_EQ(monotonic_remote_time, aos::monotonic_clock::min_time); |
| 395 | ASSERT_EQ(realtime_remote_time, aos::realtime_clock::min_time); |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 396 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 397 | ThreadPlusCount tpc; |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 398 | ASSERT_EQ(source_boot_uuid.span().size(), sizeof(ThreadPlusCount)); |
| 399 | memcpy(&tpc, source_boot_uuid.span().data(), |
| 400 | source_boot_uuid.span().size()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 401 | |
| 402 | if (will_wrap) { |
| 403 | // The queue won't chang out from under us, so we should get some amount |
| 404 | // of the tail end of the messages from a a thread. |
| 405 | // Confirm that once we get our first message, they all show up. |
| 406 | if ((*threads)[tpc.thread].event_count == |
| 407 | ::std::numeric_limits<uint64_t>::max()) { |
| 408 | (*threads)[tpc.thread].event_count = tpc.count; |
| 409 | } |
| 410 | |
| 411 | if (race_reads) { |
| 412 | // Make sure nothing goes backwards. Really not much we can do here. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 413 | ASSERT_LE((*threads)[tpc.thread].event_count, tpc.count) |
| 414 | << ": Thread " << tpc.thread; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 415 | (*threads)[tpc.thread].event_count = tpc.count; |
| 416 | } else { |
| 417 | // Make sure nothing goes backwards. Really not much we can do here. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 418 | ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count) |
| 419 | << ": Thread " << tpc.thread; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 420 | } |
| 421 | } else { |
| 422 | // Confirm that we see every message counter from every thread. |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 423 | ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count) |
| 424 | << ": Thread " << tpc.thread; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 425 | } |
| 426 | ++(*threads)[tpc.thread].event_count; |
| 427 | } |
| 428 | } |
| 429 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame^] | 430 | } // namespace aos::ipc_lib |