blob: 2797c243cb0ed39f9ee6b6ff74b752e0c72aba5e [file] [log] [blame]
Austin Schuh20b2b082019-09-11 20:42:56 -07001#include "aos/ipc_lib/queue_racer.h"
2
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cinttypes>
4#include <cstring>
Austin Schuh20b2b082019-09-11 20:42:56 -07005#include <limits>
6
Austin Schuh20b2b082019-09-11 20:42:56 -07007#include "gtest/gtest.h"
8
Philipp Schrader790cb542023-07-05 21:06:52 -07009#include "aos/ipc_lib/event.h"
10
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace aos::ipc_lib {
Austin Schuh20b2b082019-09-11 20:42:56 -070012namespace {
13
14struct ThreadPlusCount {
Austin Schuh82ea7382023-07-14 15:17:34 -070015 uint64_t thread;
Austin Schuh20b2b082019-09-11 20:42:56 -070016 uint64_t count;
17};
18
19} // namespace
20
21struct ThreadState {
22 ::std::thread thread;
23 Event ready;
24 uint64_t event_count = ::std::numeric_limits<uint64_t>::max();
25};
26
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070027QueueRacer::QueueRacer(LocklessQueue queue, int num_threads,
28 uint64_t num_messages)
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -070029 : 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
38QueueRacer::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 Schuh20b2b082019-09-11 20:42:56 -070046 Reset();
47}
48
Austin Schuh82ea7382023-07-14 15:17:34 -070049void QueueRacer::RunIteration(bool race_reads, int write_wrap_count,
50 bool set_should_read, bool should_read_result) {
Austin Schuh20b2b082019-09-11 20:42:56 -070051 const bool will_wrap = num_messages_ * num_threads_ *
52 static_cast<uint64_t>(1 + write_wrap_count) >
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070053 queue_.config().queue_size;
Austin Schuh20b2b082019-09-11 20:42:56 -070054
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 Silvermand05b8192019-12-22 01:06:56 -080063 ::std::atomic<bool> poll_index{true};
Austin Schuh20b2b082019-09-11 20:42:56 -070064
65 // List of threads.
66 ::std::vector<ThreadState> threads(num_threads_);
67
68 ::std::thread queue_index_racer([this, &poll_index]() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070069 LocklessQueueReader reader(queue_);
Austin Schuh20b2b082019-09-11 20:42:56 -070070
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 Silvermanfc0d2e82020-08-12 19:58:35 -070075 QueueIndex::MaxIndex(0xffffffffu, queue_.config().queue_size);
Austin Schuh20b2b082019-09-11 20:42:56 -070076 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 Silvermanfc0d2e82020-08-12 19:58:35 -070098 const QueueIndex latest_queue_index_queue_index = reader.LatestIndex();
Austin Schuh20b2b082019-09-11 20:42:56 -070099 const uint64_t started_writes = started_writes_.load();
100
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 const uint32_t latest_queue_index_uint32_t =
Brian Silvermand05b8192019-12-22 01:06:56 -0800102 latest_queue_index_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -0700103 uint64_t latest_queue_index = latest_queue_index_uint32_t;
104
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700105 if (latest_queue_index_queue_index != QueueIndex::Invalid()) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700106 // 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 Silvermanfc0d2e82020-08-12 19:58:35 -0700122 EXPECT_EQ(latest_queue_index_queue_index, QueueIndex::Invalid());
Austin Schuh20b2b082019-09-11 20:42:56 -0700123 EXPECT_EQ(finished_writes, 0);
124 } else {
125 if (finished_writes == 0) {
Brian Silvermand05b8192019-12-22 01:06:56 -0800126 // Plausible to be at the beginning, in which case we don't have
127 // anything to check.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700128 if (latest_queue_index_queue_index != QueueIndex::Invalid()) {
Brian Silvermand05b8192019-12-22 01:06:56 -0800129 // Otherwise, we have started. The queue can't have any more
130 // entries than this.
Austin Schuh20b2b082019-09-11 20:42:56 -0700131 EXPECT_GE(started_writes, latest_queue_index + 1);
132 }
133 } else {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700134 EXPECT_NE(latest_queue_index_queue_index, QueueIndex::Invalid());
Austin Schuh20b2b082019-09-11 20:42:56 -0700135 // latest_queue_index is an index, not a count. So it always reads 1
136 // low.
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700137 if (check_writes_and_reads_) {
138 EXPECT_GE(latest_queue_index + 1, finished_writes);
139 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700140 }
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 Silverman177567e2020-08-12 19:51:33 -0700153 t.thread = ::std::thread([this, &t, thread_index, &run,
154 write_wrap_count]() {
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700155 LocklessQueueSender sender =
156 LocklessQueueSender::Make(queue_, channel_storage_duration_).value();
Brian Silverman177567e2020-08-12 19:51:33 -0700157 CHECK_GE(sender.size(), sizeof(ThreadPlusCount));
Austin Schuh20b2b082019-09-11 20:42:56 -0700158
Brian Silverman177567e2020-08-12 19:51:33 -0700159 // Signal that we are ready to start sending.
160 t.ready.Set();
Austin Schuh20b2b082019-09-11 20:42:56 -0700161
Brian Silverman177567e2020-08-12 19:51:33 -0700162 // Wait until signaled to start running.
163 run.Wait();
Austin Schuh20b2b082019-09-11 20:42:56 -0700164
Brian Silverman177567e2020-08-12 19:51:33 -0700165 // 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 Schuh20b2b082019-09-11 20:42:56 -0700178 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700179 }
Brian Silverman177567e2020-08-12 19:51:33 -0700180 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 Schmiedebergef44b8a2022-02-28 17:30:38 -0700198 auto result =
199 sender.Send(sizeof(ThreadPlusCount), aos::monotonic_clock::min_time,
Austin Schuhac6d89e2024-03-27 14:56:09 -0700200 aos::realtime_clock::min_time,
201 aos::monotonic_clock::min_time, 0xffffffff,
Austin Schuh82ea7382023-07-14 15:17:34 -0700202 UUID::FromSpan(absl::Span<const uint8_t>(
203 reinterpret_cast<const uint8_t *>(&tpc),
204 sizeof(ThreadPlusCount))),
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700205 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 Silverman177567e2020-08-12 19:51:33 -0700212 // 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 Schuh20b2b082019-09-11 20:42:56 -0700222 ++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 Schmiedebergef44b8a2022-02-28 17:30:38 -0700243 if (check_writes_and_reads_) {
Austin Schuh82ea7382023-07-14 15:17:34 -0700244 CheckReads(race_reads, write_wrap_count, &threads, set_should_read,
245 should_read_result);
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700246 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700247
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 Schmiedebergef44b8a2022-02-28 17:30:38 -0700257 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 Schuh20b2b082019-09-11 20:42:56 -0700263
Eric Schmiedebergef44b8a2022-02-28 17:30:38 -0700264 // 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 Schuh20b2b082019-09-11 20:42:56 -0700278 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700279 }
280 }
281}
282
283void QueueRacer::CheckReads(bool race_reads, int write_wrap_count,
Austin Schuh82ea7382023-07-14 15:17:34 -0700284 ::std::vector<ThreadState> *threads,
285 bool set_should_read, bool should_read_result) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700286 // Now read back the results to double check.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700287 LocklessQueueReader reader(queue_);
288 const bool will_wrap = num_messages_ * num_threads_ * (1 + write_wrap_count) >
289 LocklessQueueSize(queue_.memory());
Austin Schuh20b2b082019-09-11 20:42:56 -0700290
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 Silvermanfc0d2e82020-08-12 19:58:35 -0700296 LocklessQueueSize(queue_.memory());
Austin Schuh20b2b082019-09-11 20:42:56 -0700297 }
298
Austin Schuh82ea7382023-07-14 15:17:34 -0700299 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 Schuh20b2b082019-09-11 20:42:56 -0700308 for (uint64_t i = initial_i;
309 i < (1 + write_wrap_count) * num_messages_ * num_threads_; ++i) {
Austin Schuhb5c6f972021-03-14 21:53:07 -0700310 monotonic_clock::time_point monotonic_sent_time;
311 realtime_clock::time_point realtime_sent_time;
312 monotonic_clock::time_point monotonic_remote_time;
Austin Schuhac6d89e2024-03-27 14:56:09 -0700313 monotonic_clock::time_point monotonic_remote_transmit_time;
Austin Schuhb5c6f972021-03-14 21:53:07 -0700314 realtime_clock::time_point realtime_remote_time;
Austin Schuha9012be2021-07-21 15:19:11 -0700315 UUID source_boot_uuid;
Austin Schuhad154822019-12-27 15:45:13 -0800316 uint32_t remote_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -0700317 size_t length;
318 char read_data[1024];
319
320 // Handle overflowing the message count for the wrap test.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700321 const uint32_t wrapped_i =
322 i % static_cast<size_t>(QueueIndex::MaxIndex(
323 0xffffffffu, LocklessQueueSize(queue_.memory())));
Austin Schuh0bd410a2023-11-05 12:38:12 -0800324 LocklessQueueReader::Result read_result =
325 set_should_read
Austin Schuhac6d89e2024-03-27 14:56:09 -0700326 ? reader.Read(
327 wrapped_i, &monotonic_sent_time, &realtime_sent_time,
328 &monotonic_remote_time, &monotonic_remote_transmit_time,
329 &realtime_remote_time, &remote_queue_index, &source_boot_uuid,
330 &length, &(read_data[0]), std::ref(should_read))
Austin Schuh0bd410a2023-11-05 12:38:12 -0800331 : reader.Read(wrapped_i, &monotonic_sent_time, &realtime_sent_time,
Austin Schuhac6d89e2024-03-27 14:56:09 -0700332 &monotonic_remote_time,
333 &monotonic_remote_transmit_time,
334 &realtime_remote_time, &remote_queue_index,
335 &source_boot_uuid, &length, &(read_data[0]), nop);
Austin Schuh20b2b082019-09-11 20:42:56 -0700336
Austin Schuh82ea7382023-07-14 15:17:34 -0700337 // The code in lockless_queue.cc reads everything but data, checks that the
338 // header hasn't changed, then reads the data. So, if we succeed and both
339 // end up not being corrupted, then we've confirmed everything works.
340 //
341 // Feed in both combos of should_read and whether or not to return true or
342 // false from should_read. By capturing the header values inside the
343 // callback, we can also verify the state in the middle of the process to
344 // make sure we have the right boundaries.
Austin Schuh20b2b082019-09-11 20:42:56 -0700345 if (race_reads) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700346 if (read_result == LocklessQueueReader::Result::NOTHING_NEW) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700347 --i;
348 continue;
349 }
350 }
351
352 if (race_reads && will_wrap) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700353 if (read_result == LocklessQueueReader::Result::TOO_OLD) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700354 continue;
355 }
356 }
Austin Schuh82ea7382023-07-14 15:17:34 -0700357
358 if (!set_should_read) {
359 // Every message should be good.
360 ASSERT_EQ(read_result, LocklessQueueReader::Result::GOOD)
361 << ": i is " << i;
362 } else {
363 if (should_read_result) {
364 ASSERT_EQ(read_result, LocklessQueueReader::Result::GOOD)
365 << ": i is " << i;
366
367 ASSERT_EQ(monotonic_sent_time, fetched_context.monotonic_event_time);
368 ASSERT_EQ(realtime_sent_time, fetched_context.realtime_event_time);
369 ASSERT_EQ(monotonic_remote_time, fetched_context.monotonic_remote_time);
370 ASSERT_EQ(realtime_remote_time, fetched_context.realtime_remote_time);
371 ASSERT_EQ(source_boot_uuid, fetched_context.source_boot_uuid);
372 ASSERT_EQ(remote_queue_index, fetched_context.remote_queue_index);
373 ASSERT_EQ(length, fetched_context.size);
374
375 ASSERT_EQ(
376 absl::Span<const uint8_t>(
377 reinterpret_cast<const uint8_t *>(
378 read_data + LocklessQueueMessageDataSize(queue_.memory()) -
379 length),
380 length),
381 source_boot_uuid.span());
382 } else {
383 ASSERT_EQ(read_result, LocklessQueueReader::Result::FILTERED);
384 monotonic_sent_time = fetched_context.monotonic_event_time;
385 realtime_sent_time = fetched_context.realtime_event_time;
386 monotonic_remote_time = fetched_context.monotonic_remote_time;
387 realtime_remote_time = fetched_context.realtime_remote_time;
388 source_boot_uuid = fetched_context.source_boot_uuid;
389 remote_queue_index = fetched_context.remote_queue_index;
390 length = fetched_context.size;
391 }
392 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700393
394 // And, confirm that time never went backwards.
395 ASSERT_GT(monotonic_sent_time, last_monotonic_sent_time);
396 last_monotonic_sent_time = monotonic_sent_time;
397
Austin Schuh82ea7382023-07-14 15:17:34 -0700398 ASSERT_EQ(monotonic_remote_time, aos::monotonic_clock::min_time);
399 ASSERT_EQ(realtime_remote_time, aos::realtime_clock::min_time);
Austin Schuhad154822019-12-27 15:45:13 -0800400
Austin Schuh20b2b082019-09-11 20:42:56 -0700401 ThreadPlusCount tpc;
Austin Schuh82ea7382023-07-14 15:17:34 -0700402 ASSERT_EQ(source_boot_uuid.span().size(), sizeof(ThreadPlusCount));
403 memcpy(&tpc, source_boot_uuid.span().data(),
404 source_boot_uuid.span().size());
Austin Schuh20b2b082019-09-11 20:42:56 -0700405
406 if (will_wrap) {
407 // The queue won't chang out from under us, so we should get some amount
408 // of the tail end of the messages from a a thread.
409 // Confirm that once we get our first message, they all show up.
410 if ((*threads)[tpc.thread].event_count ==
411 ::std::numeric_limits<uint64_t>::max()) {
412 (*threads)[tpc.thread].event_count = tpc.count;
413 }
414
415 if (race_reads) {
416 // Make sure nothing goes backwards. Really not much we can do here.
Brian Silverman177567e2020-08-12 19:51:33 -0700417 ASSERT_LE((*threads)[tpc.thread].event_count, tpc.count)
418 << ": Thread " << tpc.thread;
Austin Schuh20b2b082019-09-11 20:42:56 -0700419 (*threads)[tpc.thread].event_count = tpc.count;
420 } else {
421 // Make sure nothing goes backwards. Really not much we can do here.
Brian Silverman177567e2020-08-12 19:51:33 -0700422 ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count)
423 << ": Thread " << tpc.thread;
Austin Schuh20b2b082019-09-11 20:42:56 -0700424 }
425 } else {
426 // Confirm that we see every message counter from every thread.
Brian Silverman177567e2020-08-12 19:51:33 -0700427 ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count)
428 << ": Thread " << tpc.thread;
Austin Schuh20b2b082019-09-11 20:42:56 -0700429 }
430 ++(*threads)[tpc.thread].event_count;
431 }
432}
433
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800434} // namespace aos::ipc_lib