blob: 59435b107ea932a9bfcb3b4b8abf87eb4a0155a2 [file] [log] [blame]
Austin Schuh20b2b082019-09-11 20:42:56 -07001#ifndef AOS_IPC_LIB_QUEUE_RACER_H_
2#define AOS_IPC_LIB_QUEUE_RACER_H_
3
4#include <string.h>
5
6#include "aos/ipc_lib/lockless_queue.h"
7
8namespace aos {
9namespace ipc_lib {
10
11struct ThreadState;
12
13// Class to test the queue by spinning up a bunch of writing threads and racing
14// them together to all write at once.
15class QueueRacer {
16 public:
17 QueueRacer(LocklessQueueMemory *memory, int num_threads,
18 uint64_t num_messages, LocklessQueueConfiguration config);
19
20 // Runs an iteration of the race.
21 //
22 // This spins up num_threads, each of which sends num_messages. These must
23 // both be able to fit in the queue without wrapping.
24 //
25 // Then, this reads back all the messages and confirms that all were received
26 // in order, and none were missed.
27 //
28 // If race_reads is set, start reading (and retry if data isn't ready yet)
29 // while writes are still happening.
30 //
31 // If wrap_writes is nonzero, write enough to overwrite old data. This
32 // necesitates a loser check at the end.
33 //
34 // If both are set, run an even looser test.
35 void RunIteration(bool race_reads, int write_wrap_count);
36
37 size_t CurrentIndex() {
38 LocklessQueue queue(memory_, config_);
39 return queue.LatestQueueIndex();
40 }
41
42 private:
43 // Wipes the queue memory out so we get a clean start.
44 void Reset() { memset(memory_, 0, LocklessQueueMemorySize(config_)); }
45
46 // This is a separate method so that when all the ASSERT_* methods, we still
47 // clean up all the threads. Otherwise we get an assert on the way out of
48 // RunIteration instead of getting all the way back to gtest.
49 void CheckReads(bool race_reads, int write_wrap_count,
50 ::std::vector<ThreadState> *threads);
51
52 LocklessQueueMemory *memory_;
53 const uint64_t num_threads_;
54 const uint64_t num_messages_;
55
56 // The overall number of writes executed will always be between the two of
57 // these. We can't atomically count writes, so we have to bound them.
58 //
59 // Number of writes about to be started.
60 ::std::atomic<uint64_t> started_writes_;
61 // Number of writes completed.
62 ::std::atomic<uint64_t> finished_writes_;
63
64 const LocklessQueueConfiguration config_;
65};
66
67} // namespace ipc_lib
68} // namespace aos
69
70#endif // AOS_IPC_LIB_QUEUE_RACER_H_