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