Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 1 | #include "aos/ipc_lib/lockless_queue.h" |
| 2 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | #include <wait.h> |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 5 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 6 | #include <chrono> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include <cinttypes> |
| 8 | #include <csignal> |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <random> |
| 11 | #include <thread> |
| 12 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 13 | #include "aos/events/epoll.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 14 | #include "aos/ipc_lib/aos_sync.h" |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 15 | #include "aos/ipc_lib/event.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 16 | #include "aos/ipc_lib/queue_racer.h" |
| 17 | #include "aos/ipc_lib/signalfd.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | #include "aos/realtime.h" |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 19 | #include "aos/util/phased_loop.h" |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 20 | #include "gflags/gflags.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | DEFINE_int32(min_iterations, 100, |
| 24 | "Minimum number of stress test iterations to run"); |
| 25 | DEFINE_int32(duration, 5, "Number of seconds to test for"); |
| 26 | DEFINE_int32(print_rate, 60, "Number of seconds between status prints"); |
| 27 | |
| 28 | // The roboRIO can only handle 10 threads before exploding. Set the default for |
| 29 | // ARM to 10. |
| 30 | DEFINE_int32(thread_count, |
| 31 | #if defined(__ARM_EABI__) |
| 32 | 10, |
| 33 | #else |
| 34 | 100, |
| 35 | #endif |
| 36 | "Number of threads to race"); |
| 37 | |
| 38 | namespace aos { |
| 39 | namespace ipc_lib { |
| 40 | namespace testing { |
| 41 | |
| 42 | namespace chrono = ::std::chrono; |
| 43 | |
| 44 | class LocklessQueueTest : public ::testing::Test { |
| 45 | public: |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 46 | static constexpr monotonic_clock::duration kChannelStorageDuration = |
| 47 | std::chrono::milliseconds(500); |
| 48 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 49 | LocklessQueueTest() { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 50 | config_.num_watchers = 10; |
| 51 | config_.num_senders = 100; |
Brian Silverman | 177567e | 2020-08-12 19:51:33 -0700 | [diff] [blame] | 52 | config_.num_pinners = 5; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 53 | config_.queue_size = 10000; |
| 54 | // Exercise the alignment code. This would throw off alignment. |
| 55 | config_.message_data_size = 101; |
| 56 | |
| 57 | // Since our backing store is an array of uint64_t for alignment purposes, |
| 58 | // normalize by the size. |
| 59 | memory_.resize(LocklessQueueMemorySize(config_) / sizeof(uint64_t)); |
| 60 | |
| 61 | Reset(); |
| 62 | } |
| 63 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 64 | LocklessQueue queue() { |
| 65 | return LocklessQueue(reinterpret_cast<LocklessQueueMemory *>(&(memory_[0])), |
| 66 | reinterpret_cast<LocklessQueueMemory *>(&(memory_[0])), |
| 67 | config_); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 70 | void Reset() { memset(&memory_[0], 0, LocklessQueueMemorySize(config_)); } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 71 | |
| 72 | // Runs until the signal is received. |
| 73 | void RunUntilWakeup(Event *ready, int priority) { |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 74 | internal::EPoll epoll; |
| 75 | SignalFd signalfd({kWakeupSignal}); |
| 76 | |
| 77 | epoll.OnReadable(signalfd.fd(), [&signalfd, &epoll]() { |
| 78 | signalfd_siginfo result = signalfd.Read(); |
| 79 | |
| 80 | fprintf(stderr, "Got signal: %d\n", result.ssi_signo); |
| 81 | epoll.Quit(); |
| 82 | }); |
| 83 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 84 | { |
| 85 | // Register to be woken up *after* the signalfd is catching the signals. |
| 86 | LocklessQueueWatcher watcher = |
| 87 | LocklessQueueWatcher::Make(queue(), priority).value(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 88 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 89 | // And signal we are now ready. |
| 90 | ready->Set(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 91 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 92 | epoll.Run(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 93 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 94 | // Cleanup, ensuring the watcher is destroyed before the signalfd. |
| 95 | } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 96 | epoll.DeleteFd(signalfd.fd()); |
| 97 | } |
| 98 | |
| 99 | // Use a type with enough alignment that we are guarenteed that everything |
| 100 | // will be aligned properly on the target platform. |
| 101 | ::std::vector<uint64_t> memory_; |
| 102 | |
| 103 | LocklessQueueConfiguration config_; |
| 104 | }; |
| 105 | |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 106 | // Tests that wakeup doesn't do anything if nothing was registered. |
| 107 | TEST_F(LocklessQueueTest, NoWatcherWakeup) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 108 | LocklessQueueWakeUpper wake_upper(queue()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 109 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 110 | EXPECT_EQ(wake_upper.Wakeup(7), 0); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // Tests that wakeup doesn't do anything if a wakeup was registered and then |
| 114 | // unregistered. |
| 115 | TEST_F(LocklessQueueTest, UnregisteredWatcherWakeup) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 116 | LocklessQueueWakeUpper wake_upper(queue()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 117 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 118 | { LocklessQueueWatcher::Make(queue(), 5).value(); } |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 119 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 120 | EXPECT_EQ(wake_upper.Wakeup(7), 0); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Tests that wakeup doesn't do anything if the thread dies. |
| 124 | TEST_F(LocklessQueueTest, DiedWatcherWakeup) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 125 | LocklessQueueWakeUpper wake_upper(queue()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 126 | |
| 127 | ::std::thread([this]() { |
| 128 | // Use placement new so the destructor doesn't get run. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 129 | ::std::aligned_storage<sizeof(LocklessQueueWatcher), |
| 130 | alignof(LocklessQueueWatcher)>::type data; |
| 131 | new (&data) |
| 132 | LocklessQueueWatcher(LocklessQueueWatcher::Make(queue(), 5).value()); |
Brian Silverman | 7b266d9 | 2021-02-17 21:24:02 -0800 | [diff] [blame] | 133 | }).join(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 134 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 135 | EXPECT_EQ(wake_upper.Wakeup(7), 0); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | struct WatcherState { |
| 139 | ::std::thread t; |
| 140 | Event ready; |
| 141 | }; |
| 142 | |
| 143 | // Tests that too many watchers fails like expected. |
| 144 | TEST_F(LocklessQueueTest, TooManyWatchers) { |
| 145 | // This is going to be a barrel of monkeys. |
| 146 | // We need to spin up a bunch of watchers. But, they all need to be in |
| 147 | // different threads so they have different tids. |
| 148 | ::std::vector<WatcherState> queues; |
| 149 | // Reserve num_watchers WatcherState objects so the pointer value doesn't |
| 150 | // change out from under us below. |
| 151 | queues.reserve(config_.num_watchers); |
| 152 | |
| 153 | // Event used to trigger all the threads to unregister. |
| 154 | Event cleanup; |
| 155 | |
| 156 | // Start all the threads. |
| 157 | for (size_t i = 0; i < config_.num_watchers; ++i) { |
| 158 | queues.emplace_back(); |
| 159 | |
| 160 | WatcherState *s = &queues.back(); |
| 161 | queues.back().t = ::std::thread([this, &cleanup, s]() { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 162 | LocklessQueueWatcher q = LocklessQueueWatcher::Make(queue(), 0).value(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 163 | |
| 164 | // Signal that this thread is ready. |
| 165 | s->ready.Set(); |
| 166 | |
| 167 | // And wait until we are asked to shut down. |
| 168 | cleanup.Wait(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 169 | }); |
| 170 | } |
| 171 | |
| 172 | // Wait until all the threads are actually going. |
| 173 | for (WatcherState &w : queues) { |
| 174 | w.ready.Wait(); |
| 175 | } |
| 176 | |
| 177 | // Now try to allocate another one. This will fail. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 178 | EXPECT_FALSE(LocklessQueueWatcher::Make(queue(), 0)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 179 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 180 | // Trigger the threads to cleanup their resources, and wait until they are |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 181 | // done. |
| 182 | cleanup.Set(); |
| 183 | for (WatcherState &w : queues) { |
| 184 | w.t.join(); |
| 185 | } |
| 186 | |
| 187 | // We should now be able to allocate a wakeup. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 188 | EXPECT_TRUE(LocklessQueueWatcher::Make(queue(), 0)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // Tests that too many watchers dies like expected. |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 192 | TEST_F(LocklessQueueTest, TooManySenders) { |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 193 | ::std::vector<LocklessQueueSender> senders; |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 194 | for (size_t i = 0; i < config_.num_senders; ++i) { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 195 | senders.emplace_back( |
| 196 | LocklessQueueSender::Make(queue(), kChannelStorageDuration).value()); |
Austin Schuh | e516ab0 | 2020-05-06 21:37:04 -0700 | [diff] [blame] | 197 | } |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 198 | EXPECT_FALSE(LocklessQueueSender::Make(queue(), kChannelStorageDuration)); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Now, start 2 threads and have them receive the signals. |
| 202 | TEST_F(LocklessQueueTest, WakeUpThreads) { |
| 203 | // Confirm that the wakeup signal is in range. |
| 204 | EXPECT_LE(kWakeupSignal, SIGRTMAX); |
| 205 | EXPECT_GE(kWakeupSignal, SIGRTMIN); |
| 206 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 207 | LocklessQueueWakeUpper wake_upper(queue()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 208 | |
| 209 | // Event used to make sure the thread is ready before the test starts. |
| 210 | Event ready1; |
| 211 | Event ready2; |
| 212 | |
| 213 | // Start the thread. |
Austin Schuh | 07290cd | 2022-08-16 18:01:14 -0700 | [diff] [blame^] | 214 | ::std::thread t1([this, &ready1]() { RunUntilWakeup(&ready1, 2); }); |
| 215 | ::std::thread t2([this, &ready2]() { RunUntilWakeup(&ready2, 1); }); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 216 | |
| 217 | ready1.Wait(); |
| 218 | ready2.Wait(); |
| 219 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 220 | EXPECT_EQ(wake_upper.Wakeup(3), 2); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 221 | |
| 222 | t1.join(); |
| 223 | t2.join(); |
| 224 | |
| 225 | // Clean up afterwords. We are pretending to be RT when we are really not. |
| 226 | // So we will be PI boosted up. |
| 227 | UnsetCurrentThreadRealtimePriority(); |
| 228 | } |
| 229 | |
| 230 | // Do a simple send test. |
| 231 | TEST_F(LocklessQueueTest, Send) { |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 232 | LocklessQueueSender sender = |
| 233 | LocklessQueueSender::Make(queue(), kChannelStorageDuration).value(); |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 234 | LocklessQueueReader reader(queue()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 235 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 236 | time::PhasedLoop loop(std::chrono::microseconds(1), monotonic_clock::now()); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 237 | // Send enough messages to wrap. |
| 238 | for (int i = 0; i < 20000; ++i) { |
| 239 | // Confirm that the queue index makes sense given the number of sends. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 240 | EXPECT_EQ(reader.LatestIndex().index(), |
| 241 | i == 0 ? QueueIndex::Invalid().index() : i - 1); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 242 | |
| 243 | // Send a trivial piece of data. |
| 244 | char data[100]; |
| 245 | size_t s = snprintf(data, sizeof(data), "foobar%d", i); |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 246 | EXPECT_EQ(sender.Send(data, s, monotonic_clock::min_time, |
| 247 | realtime_clock::min_time, 0xffffffffu, UUID::Zero(), |
| 248 | nullptr, nullptr, nullptr), |
| 249 | LocklessQueueSender::Result::GOOD); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 250 | |
| 251 | // Confirm that the queue index still makes sense. This is easier since the |
| 252 | // empty case has been handled. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 253 | EXPECT_EQ(reader.LatestIndex().index(), i); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 254 | |
| 255 | // Read a result from 5 in the past. |
Austin Schuh | b5c6f97 | 2021-03-14 21:53:07 -0700 | [diff] [blame] | 256 | monotonic_clock::time_point monotonic_sent_time; |
| 257 | realtime_clock::time_point realtime_sent_time; |
| 258 | monotonic_clock::time_point monotonic_remote_time; |
| 259 | realtime_clock::time_point realtime_remote_time; |
Austin Schuh | ad15482 | 2019-12-27 15:45:13 -0800 | [diff] [blame] | 260 | uint32_t remote_queue_index; |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 261 | UUID source_boot_uuid; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 262 | char read_data[1024]; |
| 263 | size_t length; |
| 264 | |
| 265 | QueueIndex index = QueueIndex::Zero(config_.queue_size); |
| 266 | if (i - 5 < 0) { |
| 267 | index = index.DecrementBy(5 - i); |
| 268 | } else { |
| 269 | index = index.IncrementBy(i - 5); |
| 270 | } |
Austin Schuh | 8902fa5 | 2021-03-14 22:39:24 -0700 | [diff] [blame] | 271 | LocklessQueueReader::Result read_result = reader.Read( |
| 272 | index.index(), &monotonic_sent_time, &realtime_sent_time, |
| 273 | &monotonic_remote_time, &realtime_remote_time, &remote_queue_index, |
Austin Schuh | a9012be | 2021-07-21 15:19:11 -0700 | [diff] [blame] | 274 | &source_boot_uuid, &length, &(read_data[0])); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 275 | |
| 276 | // This should either return GOOD, or TOO_OLD if it is before the start of |
| 277 | // the queue. |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 278 | if (read_result != LocklessQueueReader::Result::GOOD) { |
| 279 | EXPECT_EQ(read_result, LocklessQueueReader::Result::TOO_OLD); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 280 | } |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 281 | |
| 282 | loop.SleepUntilNext(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | // Races a bunch of sending threads to see if it all works. |
| 287 | TEST_F(LocklessQueueTest, SendRace) { |
| 288 | const size_t kNumMessages = 10000 / FLAGS_thread_count; |
| 289 | |
| 290 | ::std::mt19937 generator(0); |
| 291 | ::std::uniform_int_distribution<> write_wrap_count_distribution(0, 10); |
| 292 | ::std::bernoulli_distribution race_reads_distribution; |
| 293 | ::std::bernoulli_distribution wrap_writes_distribution; |
| 294 | |
| 295 | const chrono::seconds print_frequency(FLAGS_print_rate); |
| 296 | |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 297 | QueueRacer racer(queue(), FLAGS_thread_count, kNumMessages); |
| 298 | const monotonic_clock::time_point start_time = monotonic_clock::now(); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 299 | const monotonic_clock::time_point end_time = |
| 300 | start_time + chrono::seconds(FLAGS_duration); |
| 301 | |
| 302 | monotonic_clock::time_point monotonic_now = start_time; |
| 303 | monotonic_clock::time_point next_print_time = start_time + print_frequency; |
| 304 | uint64_t messages = 0; |
| 305 | for (int i = 0; i < FLAGS_min_iterations || monotonic_now < end_time; ++i) { |
| 306 | bool race_reads = race_reads_distribution(generator); |
| 307 | int write_wrap_count = write_wrap_count_distribution(generator); |
| 308 | if (!wrap_writes_distribution(generator)) { |
| 309 | write_wrap_count = 0; |
| 310 | } |
| 311 | EXPECT_NO_FATAL_FAILURE(racer.RunIteration(race_reads, write_wrap_count)) |
| 312 | << ": Running with race_reads: " << race_reads |
| 313 | << ", and write_wrap_count " << write_wrap_count << " and on iteration " |
| 314 | << i; |
| 315 | |
| 316 | messages += racer.CurrentIndex(); |
| 317 | |
| 318 | monotonic_now = monotonic_clock::now(); |
| 319 | if (monotonic_now > next_print_time) { |
| 320 | double elapsed_seconds = chrono::duration_cast<chrono::duration<double>>( |
| 321 | monotonic_now - start_time) |
| 322 | .count(); |
| 323 | printf("Finished iteration %d, %f iterations/sec, %f messages/second\n", |
| 324 | i, i / elapsed_seconds, |
| 325 | static_cast<double>(messages) / elapsed_seconds); |
| 326 | next_print_time = monotonic_now + print_frequency; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 331 | namespace { |
| 332 | |
Austin Schuh | 07290cd | 2022-08-16 18:01:14 -0700 | [diff] [blame^] | 333 | // Temporarily pins the current thread to the first 2 available CPUs. |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 334 | // This speeds up the test on some machines a lot (~4x). It also preserves |
| 335 | // opportunities for the 2 threads to race each other. |
| 336 | class PinForTest { |
| 337 | public: |
| 338 | PinForTest() { |
Austin Schuh | 07290cd | 2022-08-16 18:01:14 -0700 | [diff] [blame^] | 339 | cpu_set_t cpus = GetCurrentThreadAffinity(); |
| 340 | old_ = cpus; |
| 341 | int number_found = 0; |
| 342 | for (int i = 0; i < CPU_SETSIZE; ++i) { |
| 343 | if (CPU_ISSET(i, &cpus)) { |
| 344 | if (number_found < 2) { |
| 345 | ++number_found; |
| 346 | } else { |
| 347 | CPU_CLR(i, &cpus); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | SetCurrentThreadAffinity(cpus); |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 352 | } |
Austin Schuh | 07290cd | 2022-08-16 18:01:14 -0700 | [diff] [blame^] | 353 | ~PinForTest() { SetCurrentThreadAffinity(old_); } |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 354 | |
| 355 | private: |
| 356 | cpu_set_t old_; |
| 357 | }; |
| 358 | |
| 359 | } // namespace |
| 360 | |
Eric Schmiedeberg | ef44b8a | 2022-02-28 17:30:38 -0700 | [diff] [blame] | 361 | class LocklessQueueTestTooFast : public LocklessQueueTest { |
| 362 | public: |
| 363 | LocklessQueueTestTooFast() { |
| 364 | // Force a scenario where senders get rate limited |
| 365 | config_.num_watchers = 1000; |
| 366 | config_.num_senders = 100; |
| 367 | config_.num_pinners = 5; |
| 368 | config_.queue_size = 100; |
| 369 | // Exercise the alignment code. This would throw off alignment. |
| 370 | config_.message_data_size = 101; |
| 371 | |
| 372 | // Since our backing store is an array of uint64_t for alignment purposes, |
| 373 | // normalize by the size. |
| 374 | memory_.resize(LocklessQueueMemorySize(config_) / sizeof(uint64_t)); |
| 375 | |
| 376 | Reset(); |
| 377 | } |
| 378 | }; |
| 379 | |
| 380 | // Ensure we always return OK or MESSAGES_SENT_TOO_FAST under an extreme load |
| 381 | // on the Sender Queue. |
| 382 | TEST_F(LocklessQueueTestTooFast, MessagesSentTooFast) { |
| 383 | PinForTest pin_cpu; |
| 384 | uint64_t kNumMessages = 1000000; |
| 385 | QueueRacer racer(queue(), |
| 386 | {FLAGS_thread_count, |
| 387 | kNumMessages, |
| 388 | {LocklessQueueSender::Result::GOOD, |
| 389 | LocklessQueueSender::Result::MESSAGES_SENT_TOO_FAST}, |
| 390 | std::chrono::milliseconds(500), |
| 391 | false}); |
| 392 | |
| 393 | EXPECT_NO_FATAL_FAILURE(racer.RunIteration(false, 0)); |
| 394 | } |
| 395 | |
| 396 | // // Send enough messages to wrap the 32 bit send counter. |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 397 | TEST_F(LocklessQueueTest, WrappedSend) { |
Brian Silverman | 0eaa1da | 2020-08-12 20:03:52 -0700 | [diff] [blame] | 398 | PinForTest pin_cpu; |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 399 | uint64_t kNumMessages = 0x100010000ul; |
Brian Silverman | fc0d2e8 | 2020-08-12 19:58:35 -0700 | [diff] [blame] | 400 | QueueRacer racer(queue(), 1, kNumMessages); |
Austin Schuh | 20b2b08 | 2019-09-11 20:42:56 -0700 | [diff] [blame] | 401 | |
| 402 | const monotonic_clock::time_point start_time = monotonic_clock::now(); |
| 403 | EXPECT_NO_FATAL_FAILURE(racer.RunIteration(false, 0)); |
| 404 | const monotonic_clock::time_point monotonic_now = monotonic_clock::now(); |
| 405 | double elapsed_seconds = chrono::duration_cast<chrono::duration<double>>( |
| 406 | monotonic_now - start_time) |
| 407 | .count(); |
| 408 | printf("Took %f seconds to write %" PRIu64 " messages, %f messages/s\n", |
| 409 | elapsed_seconds, kNumMessages, |
| 410 | static_cast<double>(kNumMessages) / elapsed_seconds); |
| 411 | } |
| 412 | |
| 413 | } // namespace testing |
| 414 | } // namespace ipc_lib |
| 415 | } // namespace aos |