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