blob: b73880546776ec37e3140b7fd1280c509cdd4262 [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
Brian Silverman7b266d92021-02-17 21:24:02 -08007#include "aos/ipc_lib/event.h"
Austin Schuh20b2b082019-09-11 20:42:56 -07008#include "gtest/gtest.h"
9
10namespace aos {
11namespace ipc_lib {
12namespace {
13
14struct ThreadPlusCount {
15 int thread;
16 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)
29 : queue_(queue), num_threads_(num_threads), num_messages_(num_messages) {
Austin Schuh20b2b082019-09-11 20:42:56 -070030 Reset();
31}
32
33void QueueRacer::RunIteration(bool race_reads, int write_wrap_count) {
34 const bool will_wrap = num_messages_ * num_threads_ *
35 static_cast<uint64_t>(1 + write_wrap_count) >
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070036 queue_.config().queue_size;
Austin Schuh20b2b082019-09-11 20:42:56 -070037
38 // Clear out shmem.
39 Reset();
40 started_writes_ = 0;
41 finished_writes_ = 0;
42
43 // Event used to start all the threads processing at once.
44 Event run;
45
Brian Silvermand05b8192019-12-22 01:06:56 -080046 ::std::atomic<bool> poll_index{true};
Austin Schuh20b2b082019-09-11 20:42:56 -070047
48 // List of threads.
49 ::std::vector<ThreadState> threads(num_threads_);
50
51 ::std::thread queue_index_racer([this, &poll_index]() {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070052 LocklessQueueReader reader(queue_);
Austin Schuh20b2b082019-09-11 20:42:56 -070053
54 // Track the number of times we wrap, and cache the modulo.
55 uint64_t wrap_count = 0;
56 uint32_t last_queue_index = 0;
57 const uint32_t max_queue_index =
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070058 QueueIndex::MaxIndex(0xffffffffu, queue_.config().queue_size);
Austin Schuh20b2b082019-09-11 20:42:56 -070059 while (poll_index) {
60 // We want to read everything backwards. This will give us conservative
61 // bounds. And with enough time and randomness, we will see all the cases
62 // we care to see.
63
64 // These 3 numbers look at the same thing, but at different points of time
65 // in the process. The process (essentially) looks like:
66 //
67 // ++started_writes;
68 // ++latest_queue_index;
69 // ++finished_writes;
70 //
71 // We want to check that latest_queue_index is bounded by the number of
72 // writes started and finished. Basically, we can say that
73 // finished_writes < latest_queue_index always. And
74 // latest_queue_index < started_writes. And everything always increases.
75 // So, if we let more time elapse between sampling finished_writes and
76 // latest_queue_index, we will only be relaxing our bounds, not
77 // invalidating the check. The same goes for started_writes.
78 //
79 // So, grab them in order.
80 const uint64_t finished_writes = finished_writes_.load();
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070081 const QueueIndex latest_queue_index_queue_index = reader.LatestIndex();
Austin Schuh20b2b082019-09-11 20:42:56 -070082 const uint64_t started_writes = started_writes_.load();
83
Alex Perrycb7da4b2019-08-28 19:35:56 -070084 const uint32_t latest_queue_index_uint32_t =
Brian Silvermand05b8192019-12-22 01:06:56 -080085 latest_queue_index_queue_index.index();
Austin Schuh20b2b082019-09-11 20:42:56 -070086 uint64_t latest_queue_index = latest_queue_index_uint32_t;
87
Brian Silvermanfc0d2e82020-08-12 19:58:35 -070088 if (latest_queue_index_queue_index != QueueIndex::Invalid()) {
Austin Schuh20b2b082019-09-11 20:42:56 -070089 // If we got smaller, we wrapped.
90 if (latest_queue_index_uint32_t < last_queue_index) {
91 ++wrap_count;
92 }
93 // And apply it.
94 latest_queue_index +=
95 static_cast<uint64_t>(max_queue_index) * wrap_count;
96 last_queue_index = latest_queue_index_uint32_t;
97 }
98
99 // For grins, check that we have always started more than we finished.
100 // Should never fail.
101 EXPECT_GE(started_writes, finished_writes);
102
103 // If we are at the beginning, the queue needs to always return empty.
104 if (started_writes == 0) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700105 EXPECT_EQ(latest_queue_index_queue_index, QueueIndex::Invalid());
Austin Schuh20b2b082019-09-11 20:42:56 -0700106 EXPECT_EQ(finished_writes, 0);
107 } else {
108 if (finished_writes == 0) {
Brian Silvermand05b8192019-12-22 01:06:56 -0800109 // Plausible to be at the beginning, in which case we don't have
110 // anything to check.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700111 if (latest_queue_index_queue_index != QueueIndex::Invalid()) {
Brian Silvermand05b8192019-12-22 01:06:56 -0800112 // Otherwise, we have started. The queue can't have any more
113 // entries than this.
Austin Schuh20b2b082019-09-11 20:42:56 -0700114 EXPECT_GE(started_writes, latest_queue_index + 1);
115 }
116 } else {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700117 EXPECT_NE(latest_queue_index_queue_index, QueueIndex::Invalid());
Austin Schuh20b2b082019-09-11 20:42:56 -0700118 // latest_queue_index is an index, not a count. So it always reads 1
119 // low.
120 EXPECT_GE(latest_queue_index + 1, finished_writes);
121 }
122 }
123 }
124 });
125
126 // Build up each thread and kick it off.
127 int thread_index = 0;
128 for (ThreadState &t : threads) {
129 if (will_wrap) {
130 t.event_count = ::std::numeric_limits<uint64_t>::max();
131 } else {
132 t.event_count = 0;
133 }
Brian Silverman177567e2020-08-12 19:51:33 -0700134 t.thread = ::std::thread([this, &t, thread_index, &run,
135 write_wrap_count]() {
136 // Build up a sender.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700137 LocklessQueueSender sender = LocklessQueueSender::Make(queue_).value();
Brian Silverman177567e2020-08-12 19:51:33 -0700138 CHECK_GE(sender.size(), sizeof(ThreadPlusCount));
Austin Schuh20b2b082019-09-11 20:42:56 -0700139
Brian Silverman177567e2020-08-12 19:51:33 -0700140 // Signal that we are ready to start sending.
141 t.ready.Set();
Austin Schuh20b2b082019-09-11 20:42:56 -0700142
Brian Silverman177567e2020-08-12 19:51:33 -0700143 // Wait until signaled to start running.
144 run.Wait();
Austin Schuh20b2b082019-09-11 20:42:56 -0700145
Brian Silverman177567e2020-08-12 19:51:33 -0700146 // Gogogo!
147 for (uint64_t i = 0;
148 i < num_messages_ * static_cast<uint64_t>(1 + write_wrap_count);
149 ++i) {
150 char *const data = static_cast<char *>(sender.Data()) + sender.size() -
151 sizeof(ThreadPlusCount);
152 const char fill = (i + 55) & 0xFF;
153 memset(data, fill, sizeof(ThreadPlusCount));
154 {
155 bool found_nonzero = false;
156 for (size_t i = 0; i < sizeof(ThreadPlusCount); ++i) {
157 if (data[i] != fill) {
158 found_nonzero = true;
Austin Schuh20b2b082019-09-11 20:42:56 -0700159 }
Austin Schuh20b2b082019-09-11 20:42:56 -0700160 }
Brian Silverman177567e2020-08-12 19:51:33 -0700161 CHECK(!found_nonzero) << ": Somebody else is writing to our buffer";
162 }
163
164 ThreadPlusCount tpc;
165 tpc.thread = thread_index;
166 tpc.count = i;
167
168 memcpy(data, &tpc, sizeof(ThreadPlusCount));
169
170 if (i % 0x800000 == 0x100000) {
171 fprintf(
172 stderr, "Sent %" PRIu64 ", %f %%\n", i,
173 static_cast<double>(i) /
174 static_cast<double>(num_messages_ * (1 + write_wrap_count)) *
175 100.0);
176 }
177
178 ++started_writes_;
Austin Schuhb5c6f972021-03-14 21:53:07 -0700179 sender.Send(sizeof(ThreadPlusCount), aos::monotonic_clock::min_time,
Austin Schuh8902fa52021-03-14 22:39:24 -0700180 aos::realtime_clock::min_time, 0xffffffff, UUID::Zero(),
Austin Schuhb5c6f972021-03-14 21:53:07 -0700181 nullptr, nullptr, nullptr);
Brian Silverman177567e2020-08-12 19:51:33 -0700182 // Blank out the new scratch buffer, to catch other people using it.
183 {
184 char *const new_data = static_cast<char *>(sender.Data()) +
185 sender.size() - sizeof(ThreadPlusCount);
186 const char new_fill = ~fill;
187 memset(new_data, new_fill, sizeof(ThreadPlusCount));
188 }
189 ++finished_writes_;
190 }
191 });
Austin Schuh20b2b082019-09-11 20:42:56 -0700192 ++thread_index;
193 }
194
195 // Wait until all the threads are ready.
196 for (ThreadState &t : threads) {
197 t.ready.Wait();
198 }
199
200 // And start them racing.
201 run.Set();
202
203 // Let all the threads finish before reading if we are supposed to not be
204 // racing reads.
205 if (!race_reads) {
206 for (ThreadState &t : threads) {
207 t.thread.join();
208 }
209 poll_index = false;
210 queue_index_racer.join();
211 }
212
213 CheckReads(race_reads, write_wrap_count, &threads);
214
215 // Reap all the threads.
216 if (race_reads) {
217 for (ThreadState &t : threads) {
218 t.thread.join();
219 }
220 poll_index = false;
221 queue_index_racer.join();
222 }
223
224 // Confirm that the number of writes matches the expected number of writes.
225 ASSERT_EQ(num_threads_ * num_messages_ * (1 + write_wrap_count),
226 started_writes_);
227 ASSERT_EQ(num_threads_ * num_messages_ * (1 + write_wrap_count),
228 finished_writes_);
229
230 // And that every thread sent the right number of messages.
231 for (ThreadState &t : threads) {
232 if (will_wrap) {
233 if (!race_reads) {
234 // If we are wrapping, there is a possibility that a thread writes
235 // everything *before* we can read any of it, and it all gets
236 // overwritten.
237 ASSERT_TRUE(t.event_count == ::std::numeric_limits<uint64_t>::max() ||
238 t.event_count == (1 + write_wrap_count) * num_messages_)
239 << ": Got " << t.event_count << " events, expected "
240 << (1 + write_wrap_count) * num_messages_;
241 }
242 } else {
243 ASSERT_EQ(t.event_count, num_messages_);
244 }
245 }
246}
247
248void QueueRacer::CheckReads(bool race_reads, int write_wrap_count,
249 ::std::vector<ThreadState> *threads) {
250 // Now read back the results to double check.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700251 LocklessQueueReader reader(queue_);
252 const bool will_wrap = num_messages_ * num_threads_ * (1 + write_wrap_count) >
253 LocklessQueueSize(queue_.memory());
Austin Schuh20b2b082019-09-11 20:42:56 -0700254
255 monotonic_clock::time_point last_monotonic_sent_time =
256 monotonic_clock::epoch();
257 uint64_t initial_i = 0;
258 if (will_wrap) {
259 initial_i = (1 + write_wrap_count) * num_messages_ * num_threads_ -
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700260 LocklessQueueSize(queue_.memory());
Austin Schuh20b2b082019-09-11 20:42:56 -0700261 }
262
263 for (uint64_t i = initial_i;
264 i < (1 + write_wrap_count) * num_messages_ * num_threads_; ++i) {
Austin Schuhb5c6f972021-03-14 21:53:07 -0700265 monotonic_clock::time_point monotonic_sent_time;
266 realtime_clock::time_point realtime_sent_time;
267 monotonic_clock::time_point monotonic_remote_time;
268 realtime_clock::time_point realtime_remote_time;
Austin Schuha9012be2021-07-21 15:19:11 -0700269 UUID source_boot_uuid;
Austin Schuhad154822019-12-27 15:45:13 -0800270 uint32_t remote_queue_index;
Austin Schuh20b2b082019-09-11 20:42:56 -0700271 size_t length;
272 char read_data[1024];
273
274 // Handle overflowing the message count for the wrap test.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700275 const uint32_t wrapped_i =
276 i % static_cast<size_t>(QueueIndex::MaxIndex(
277 0xffffffffu, LocklessQueueSize(queue_.memory())));
Austin Schuh8902fa52021-03-14 22:39:24 -0700278 LocklessQueueReader::Result read_result = reader.Read(
279 wrapped_i, &monotonic_sent_time, &realtime_sent_time,
280 &monotonic_remote_time, &realtime_remote_time, &remote_queue_index,
Austin Schuha9012be2021-07-21 15:19:11 -0700281 &source_boot_uuid, &length, &(read_data[0]));
Austin Schuh20b2b082019-09-11 20:42:56 -0700282
283 if (race_reads) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700284 if (read_result == LocklessQueueReader::Result::NOTHING_NEW) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700285 --i;
286 continue;
287 }
288 }
289
290 if (race_reads && will_wrap) {
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700291 if (read_result == LocklessQueueReader::Result::TOO_OLD) {
Austin Schuh20b2b082019-09-11 20:42:56 -0700292 continue;
293 }
294 }
295 // Every message should be good.
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700296 ASSERT_EQ(read_result, LocklessQueueReader::Result::GOOD) << ": i is " << i;
Austin Schuh20b2b082019-09-11 20:42:56 -0700297
298 // And, confirm that time never went backwards.
299 ASSERT_GT(monotonic_sent_time, last_monotonic_sent_time);
300 last_monotonic_sent_time = monotonic_sent_time;
301
Austin Schuhad154822019-12-27 15:45:13 -0800302 EXPECT_EQ(monotonic_remote_time, aos::monotonic_clock::min_time);
303 EXPECT_EQ(realtime_remote_time, aos::realtime_clock::min_time);
Austin Schuha9012be2021-07-21 15:19:11 -0700304 EXPECT_EQ(source_boot_uuid, UUID::Zero());
Austin Schuhad154822019-12-27 15:45:13 -0800305
Austin Schuh20b2b082019-09-11 20:42:56 -0700306 ThreadPlusCount tpc;
307 ASSERT_EQ(length, sizeof(ThreadPlusCount));
Brian Silvermanfc0d2e82020-08-12 19:58:35 -0700308 memcpy(&tpc,
309 read_data + LocklessQueueMessageDataSize(queue_.memory()) - length,
Austin Schuh67420a42019-12-21 21:55:04 -0800310 sizeof(ThreadPlusCount));
Austin Schuh20b2b082019-09-11 20:42:56 -0700311
312 if (will_wrap) {
313 // The queue won't chang out from under us, so we should get some amount
314 // of the tail end of the messages from a a thread.
315 // Confirm that once we get our first message, they all show up.
316 if ((*threads)[tpc.thread].event_count ==
317 ::std::numeric_limits<uint64_t>::max()) {
318 (*threads)[tpc.thread].event_count = tpc.count;
319 }
320
321 if (race_reads) {
322 // Make sure nothing goes backwards. Really not much we can do here.
Brian Silverman177567e2020-08-12 19:51:33 -0700323 ASSERT_LE((*threads)[tpc.thread].event_count, tpc.count)
324 << ": Thread " << tpc.thread;
Austin Schuh20b2b082019-09-11 20:42:56 -0700325 (*threads)[tpc.thread].event_count = tpc.count;
326 } else {
327 // Make sure nothing goes backwards. Really not much we can do here.
Brian Silverman177567e2020-08-12 19:51:33 -0700328 ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count)
329 << ": Thread " << tpc.thread;
Austin Schuh20b2b082019-09-11 20:42:56 -0700330 }
331 } else {
332 // Confirm that we see every message counter from every thread.
Brian Silverman177567e2020-08-12 19:51:33 -0700333 ASSERT_EQ((*threads)[tpc.thread].event_count, tpc.count)
334 << ": Thread " << tpc.thread;
Austin Schuh20b2b082019-09-11 20:42:56 -0700335 }
336 ++(*threads)[tpc.thread].event_count;
337 }
338}
339
340} // namespace ipc_lib
341} // namespace aos