Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_IPC_LIB_QUEUE_RACER_H_ |
| 2 | #define AOS_IPC_LIB_QUEUE_RACER_H_ |
| 3 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstring> |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 5 | |
| 6 | #include "aos/ipc_lib/lockless_queue.h" |
| 7 | |
| 8 | namespace aos { |
| 9 | namespace ipc_lib { |
| 10 | |
| 11 | struct ThreadState; |
| 12 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 13 | struct QueueRacerConfiguration { |
| 14 | // Number of threads that send messages |
| 15 | const int num_threads; |
| 16 | // Number of messages sent by each thread |
| 17 | const uint64_t num_messages; |
| 18 | // Allows QueueRacer to check for multiple returns from calling Send() |
| 19 | const std::vector<LocklessQueueSender::Result> expected_send_results = { |
| 20 | LocklessQueueSender::Result::GOOD}; |
| 21 | // Channel Storage Duration for queue used by QueueRacer |
| 22 | const monotonic_clock::duration channel_storage_duration = |
| 23 | std::chrono::nanoseconds(1); |
| 24 | // Set to true if all writes and reads are expected to be successful |
| 25 | // This allows QueueRacer to be used for checking failure scenarios |
| 26 | const bool check_writes_and_reads; |
| 27 | }; |
| 28 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 29 | // Class to test the queue by spinning up a bunch of writing threads and racing |
| 30 | // them together to all write at once. |
| 31 | class QueueRacer { |
| 32 | public: |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 33 | QueueRacer(LocklessQueue queue, int num_threads, uint64_t num_messages); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 34 | QueueRacer(LocklessQueue queue, const QueueRacerConfiguration &config); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 35 | |
| 36 | // Runs an iteration of the race. |
| 37 | // |
| 38 | // This spins up num_threads, each of which sends num_messages. These must |
| 39 | // both be able to fit in the queue without wrapping. |
| 40 | // |
| 41 | // Then, this reads back all the messages and confirms that all were received |
| 42 | // in order, and none were missed. |
| 43 | // |
| 44 | // If race_reads is set, start reading (and retry if data isn't ready yet) |
| 45 | // while writes are still happening. |
| 46 | // |
| 47 | // If wrap_writes is nonzero, write enough to overwrite old data. This |
| 48 | // necesitates a loser check at the end. |
| 49 | // |
| 50 | // If both are set, run an even looser test. |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 51 | // |
| 52 | // set_should_read is used to determine if we should pass in a valid |
| 53 | // should_read function, and should_read_result is the return result of that |
| 54 | // function. |
| 55 | void RunIteration(bool race_reads, int write_wrap_count, bool set_should_read, |
| 56 | bool should_read_result); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 57 | |
| 58 | size_t CurrentIndex() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 59 | return LocklessQueueReader(queue_).LatestIndex().index(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private: |
| 63 | // Wipes the queue memory out so we get a clean start. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 64 | void Reset() { |
Austin Schuh | faec51a | 2023-09-08 17:43:32 -0700 | [diff] [blame] | 65 | memset(reinterpret_cast<void *>(queue_.memory()), 0, |
| 66 | LocklessQueueMemorySize(queue_.config())); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 67 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 68 | |
| 69 | // This is a separate method so that when all the ASSERT_* methods, we still |
| 70 | // clean up all the threads. Otherwise we get an assert on the way out of |
| 71 | // RunIteration instead of getting all the way back to gtest. |
| 72 | void CheckReads(bool race_reads, int write_wrap_count, |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 73 | ::std::vector<ThreadState> *threads, bool set_should_read, |
| 74 | bool should_read_result); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 75 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 76 | LocklessQueue queue_; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 77 | const uint64_t num_threads_; |
| 78 | const uint64_t num_messages_; |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 79 | const monotonic_clock::duration channel_storage_duration_; |
| 80 | // Allows QueueRacer to check for multiple returns from calling Send() |
| 81 | const std::vector<LocklessQueueSender::Result> expected_send_results_; |
| 82 | const bool check_writes_and_reads_; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 83 | // The overall number of writes executed will always be between the two of |
| 84 | // these. We can't atomically count writes, so we have to bound them. |
| 85 | // |
| 86 | // Number of writes about to be started. |
| 87 | ::std::atomic<uint64_t> started_writes_; |
| 88 | // Number of writes completed. |
| 89 | ::std::atomic<uint64_t> finished_writes_; |
Austin Schuh | 82ea738 | 2023-07-14 15:17:34 -0700 | [diff] [blame] | 90 | |
| 91 | std::function<bool(uint32_t, monotonic_clock::time_point, |
| 92 | realtime_clock::time_point, monotonic_clock::time_point, |
| 93 | realtime_clock::time_point, uint32_t, UUID, size_t)> |
| 94 | should_read_ = [](uint32_t, monotonic_clock::time_point, |
| 95 | realtime_clock::time_point, monotonic_clock::time_point, |
| 96 | realtime_clock::time_point, uint32_t, UUID, |
| 97 | size_t) { return true; }; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace ipc_lib |
| 101 | } // namespace aos |
| 102 | |
| 103 | #endif // AOS_IPC_LIB_QUEUE_RACER_H_ |