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