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