henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 11 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | #include <set> |
| 13 | #include <vector> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/arraysize.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/criticalsection.h" |
| 18 | #include "rtc_base/event.h" |
| 19 | #include "rtc_base/gunit.h" |
| 20 | #include "rtc_base/platform_thread.h" |
| 21 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | const int kLongTime = 10000; // 10 seconds |
| 28 | const int kNumThreads = 16; |
| 29 | const int kOperationsToRun = 1000; |
| 30 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 31 | class UniqueValueVerifier { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | public: |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 33 | void Verify(const std::vector<int>& values) { |
| 34 | for (size_t i = 0; i < values.size(); ++i) { |
| 35 | std::pair<std::set<int>::iterator, bool> result = |
| 36 | all_values_.insert(values[i]); |
| 37 | // Each value should only be taken by one thread, so if this value |
| 38 | // has already been added, something went wrong. |
| 39 | EXPECT_TRUE(result.second) |
| 40 | << " Thread=" << Thread::Current() << " value=" << values[i]; |
| 41 | } |
| 42 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 44 | void Finalize() {} |
| 45 | |
| 46 | private: |
| 47 | std::set<int> all_values_; |
| 48 | }; |
| 49 | |
| 50 | class CompareAndSwapVerifier { |
| 51 | public: |
| 52 | CompareAndSwapVerifier() : zero_count_(0) {} |
| 53 | |
| 54 | void Verify(const std::vector<int>& values) { |
| 55 | for (auto v : values) { |
| 56 | if (v == 0) { |
| 57 | EXPECT_EQ(0, zero_count_) << "Thread=" << Thread::Current(); |
| 58 | ++zero_count_; |
| 59 | } else { |
| 60 | EXPECT_EQ(1, v) << " Thread=" << Thread::Current(); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void Finalize() { |
| 66 | EXPECT_EQ(1, zero_count_); |
| 67 | } |
| 68 | private: |
| 69 | int zero_count_; |
| 70 | }; |
| 71 | |
| 72 | class RunnerBase : public MessageHandler { |
| 73 | public: |
| 74 | explicit RunnerBase(int value) |
| 75 | : threads_active_(0), |
| 76 | start_event_(true, false), |
| 77 | done_event_(true, false), |
| 78 | shared_value_(value) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 79 | |
| 80 | bool Run() { |
| 81 | // Signal all threads to start. |
| 82 | start_event_.Set(); |
| 83 | |
| 84 | // Wait for all threads to finish. |
| 85 | return done_event_.Wait(kLongTime); |
| 86 | } |
| 87 | |
| 88 | void SetExpectedThreadCount(int count) { |
| 89 | threads_active_ = count; |
| 90 | } |
| 91 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 92 | int shared_value() const { return shared_value_; } |
| 93 | |
| 94 | protected: |
| 95 | // Derived classes must override OnMessage, and call BeforeStart and AfterEnd |
| 96 | // at the beginning and the end of OnMessage respectively. |
| 97 | void BeforeStart() { |
| 98 | ASSERT_TRUE(start_event_.Wait(kLongTime)); |
| 99 | } |
| 100 | |
| 101 | // Returns true if all threads have finished. |
| 102 | bool AfterEnd() { |
| 103 | if (AtomicOps::Decrement(&threads_active_) == 0) { |
| 104 | done_event_.Set(); |
| 105 | return true; |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | int threads_active_; |
| 111 | Event start_event_; |
| 112 | Event done_event_; |
| 113 | int shared_value_; |
| 114 | }; |
| 115 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 116 | class RTC_LOCKABLE CriticalSectionLock { |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 117 | public: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 118 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { cs_.Enter(); } |
| 119 | void Unlock() RTC_UNLOCK_FUNCTION() { cs_.Leave(); } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | CriticalSection cs_; |
| 123 | }; |
| 124 | |
| 125 | template <class Lock> |
| 126 | class LockRunner : public RunnerBase { |
| 127 | public: |
| 128 | LockRunner() : RunnerBase(0) {} |
| 129 | |
| 130 | void OnMessage(Message* msg) override { |
| 131 | BeforeStart(); |
| 132 | |
| 133 | lock_.Lock(); |
| 134 | |
| 135 | EXPECT_EQ(0, shared_value_); |
| 136 | int old = shared_value_; |
| 137 | |
| 138 | // Use a loop to increase the chance of race. |
| 139 | for (int i = 0; i < kOperationsToRun; ++i) { |
| 140 | ++shared_value_; |
| 141 | } |
| 142 | EXPECT_EQ(old + kOperationsToRun, shared_value_); |
| 143 | shared_value_ = 0; |
| 144 | |
| 145 | lock_.Unlock(); |
| 146 | |
| 147 | AfterEnd(); |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | Lock lock_; |
| 152 | }; |
| 153 | |
| 154 | template <class Op, class Verifier> |
| 155 | class AtomicOpRunner : public RunnerBase { |
| 156 | public: |
| 157 | explicit AtomicOpRunner(int initial_value) : RunnerBase(initial_value) {} |
| 158 | |
| 159 | void OnMessage(Message* msg) override { |
| 160 | BeforeStart(); |
| 161 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 162 | std::vector<int> values; |
| 163 | values.reserve(kOperationsToRun); |
| 164 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 165 | // Generate a bunch of values by updating shared_value_ atomically. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 166 | for (int i = 0; i < kOperationsToRun; ++i) { |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 167 | values.push_back(Op::AtomicOp(&shared_value_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | { // Add them all to the set. |
| 171 | CritScope cs(&all_values_crit_); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 172 | verifier_.Verify(values); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 175 | if (AfterEnd()) { |
| 176 | verifier_.Finalize(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 181 | CriticalSection all_values_crit_; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 182 | Verifier verifier_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | struct IncrementOp { |
| 186 | static int AtomicOp(int* i) { return AtomicOps::Increment(i); } |
| 187 | }; |
| 188 | |
| 189 | struct DecrementOp { |
| 190 | static int AtomicOp(int* i) { return AtomicOps::Decrement(i); } |
| 191 | }; |
| 192 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 193 | struct CompareAndSwapOp { |
| 194 | static int AtomicOp(int* i) { return AtomicOps::CompareAndSwap(i, 0, 1); } |
| 195 | }; |
| 196 | |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 197 | void StartThreads(std::vector<std::unique_ptr<Thread>>* threads, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 198 | MessageHandler* handler) { |
| 199 | for (int i = 0; i < kNumThreads; ++i) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 200 | std::unique_ptr<Thread> thread(Thread::Create()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 201 | thread->Start(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 202 | thread->Post(RTC_FROM_HERE, handler); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 203 | threads->push_back(std::move(thread)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | } // namespace |
| 208 | |
| 209 | TEST(AtomicOpsTest, Simple) { |
| 210 | int value = 0; |
| 211 | EXPECT_EQ(1, AtomicOps::Increment(&value)); |
| 212 | EXPECT_EQ(1, value); |
| 213 | EXPECT_EQ(2, AtomicOps::Increment(&value)); |
| 214 | EXPECT_EQ(2, value); |
| 215 | EXPECT_EQ(1, AtomicOps::Decrement(&value)); |
| 216 | EXPECT_EQ(1, value); |
| 217 | EXPECT_EQ(0, AtomicOps::Decrement(&value)); |
| 218 | EXPECT_EQ(0, value); |
| 219 | } |
| 220 | |
Peter Boström | 455a252 | 2015-12-18 17:00:25 +0100 | [diff] [blame] | 221 | TEST(AtomicOpsTest, SimplePtr) { |
| 222 | class Foo {}; |
| 223 | Foo* volatile foo = nullptr; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 224 | std::unique_ptr<Foo> a(new Foo()); |
| 225 | std::unique_ptr<Foo> b(new Foo()); |
Peter Boström | 455a252 | 2015-12-18 17:00:25 +0100 | [diff] [blame] | 226 | // Reading the initial value should work as expected. |
| 227 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == nullptr); |
| 228 | // Setting using compare and swap should work. |
| 229 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr( |
| 230 | &foo, static_cast<Foo*>(nullptr), a.get()) == nullptr); |
| 231 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); |
| 232 | // Setting another value but with the wrong previous pointer should fail |
| 233 | // (remain a). |
| 234 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr( |
| 235 | &foo, static_cast<Foo*>(nullptr), b.get()) == a.get()); |
| 236 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); |
| 237 | // Replacing a with b should work. |
| 238 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(&foo, a.get(), b.get()) == |
| 239 | a.get()); |
| 240 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == b.get()); |
| 241 | } |
| 242 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 243 | TEST(AtomicOpsTest, Increment) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 244 | // Create and start lots of threads. |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 245 | AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 246 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 247 | StartThreads(&threads, &runner); |
| 248 | runner.SetExpectedThreadCount(kNumThreads); |
| 249 | |
| 250 | // Release the hounds! |
| 251 | EXPECT_TRUE(runner.Run()); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 252 | EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 253 | } |
| 254 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 255 | TEST(AtomicOpsTest, Decrement) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 256 | // Create and start lots of threads. |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 257 | AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner( |
| 258 | kOperationsToRun * kNumThreads); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 259 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | StartThreads(&threads, &runner); |
| 261 | runner.SetExpectedThreadCount(kNumThreads); |
| 262 | |
| 263 | // Release the hounds! |
| 264 | EXPECT_TRUE(runner.Run()); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 265 | EXPECT_EQ(0, runner.shared_value()); |
| 266 | } |
| 267 | |
| 268 | TEST(AtomicOpsTest, CompareAndSwap) { |
| 269 | // Create and start lots of threads. |
| 270 | AtomicOpRunner<CompareAndSwapOp, CompareAndSwapVerifier> runner(0); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 271 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 272 | StartThreads(&threads, &runner); |
| 273 | runner.SetExpectedThreadCount(kNumThreads); |
| 274 | |
| 275 | // Release the hounds! |
| 276 | EXPECT_TRUE(runner.Run()); |
| 277 | EXPECT_EQ(1, runner.shared_value()); |
| 278 | } |
| 279 | |
| 280 | TEST(GlobalLockTest, Basic) { |
| 281 | // Create and start lots of threads. |
| 282 | LockRunner<GlobalLock> runner; |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 283 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 284 | StartThreads(&threads, &runner); |
| 285 | runner.SetExpectedThreadCount(kNumThreads); |
| 286 | |
| 287 | // Release the hounds! |
| 288 | EXPECT_TRUE(runner.Run()); |
| 289 | EXPECT_EQ(0, runner.shared_value()); |
| 290 | } |
| 291 | |
| 292 | TEST(CriticalSectionTest, Basic) { |
| 293 | // Create and start lots of threads. |
| 294 | LockRunner<CriticalSectionLock> runner; |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 295 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 296 | StartThreads(&threads, &runner); |
| 297 | runner.SetExpectedThreadCount(kNumThreads); |
| 298 | |
| 299 | // Release the hounds! |
| 300 | EXPECT_TRUE(runner.Run()); |
| 301 | EXPECT_EQ(0, runner.shared_value()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 302 | } |
| 303 | |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 304 | class PerfTestData { |
| 305 | public: |
| 306 | PerfTestData(int expected_count, Event* event) |
| 307 | : cache_line_barrier_1_(), cache_line_barrier_2_(), |
| 308 | expected_count_(expected_count), event_(event) { |
| 309 | cache_line_barrier_1_[0]++; // Avoid 'is not used'. |
| 310 | cache_line_barrier_2_[0]++; // Avoid 'is not used'. |
| 311 | } |
| 312 | ~PerfTestData() {} |
| 313 | |
| 314 | void AddToCounter(int add) { |
| 315 | rtc::CritScope cs(&lock_); |
| 316 | my_counter_ += add; |
| 317 | if (my_counter_ == expected_count_) |
| 318 | event_->Set(); |
| 319 | } |
| 320 | |
| 321 | int64_t total() const { |
| 322 | // Assume that only one thread is running now. |
| 323 | return my_counter_; |
| 324 | } |
| 325 | |
| 326 | private: |
| 327 | uint8_t cache_line_barrier_1_[64]; |
| 328 | CriticalSection lock_; |
| 329 | uint8_t cache_line_barrier_2_[64]; |
| 330 | int64_t my_counter_ = 0; |
| 331 | const int expected_count_; |
| 332 | Event* const event_; |
| 333 | }; |
| 334 | |
| 335 | class PerfTestThread { |
| 336 | public: |
| 337 | PerfTestThread() : thread_(&ThreadFunc, this, "CsPerf") {} |
| 338 | |
| 339 | void Start(PerfTestData* data, int repeats, int id) { |
| 340 | RTC_DCHECK(!thread_.IsRunning()); |
| 341 | RTC_DCHECK(!data_); |
| 342 | data_ = data; |
| 343 | repeats_ = repeats; |
| 344 | my_id_ = id; |
| 345 | thread_.Start(); |
| 346 | } |
| 347 | |
| 348 | void Stop() { |
| 349 | RTC_DCHECK(thread_.IsRunning()); |
| 350 | RTC_DCHECK(data_); |
| 351 | thread_.Stop(); |
| 352 | repeats_ = 0; |
| 353 | data_ = nullptr; |
| 354 | my_id_ = 0; |
| 355 | } |
| 356 | |
| 357 | private: |
| 358 | static bool ThreadFunc(void* param) { |
| 359 | PerfTestThread* me = static_cast<PerfTestThread*>(param); |
| 360 | for (int i = 0; i < me->repeats_; ++i) |
| 361 | me->data_->AddToCounter(me->my_id_); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | PlatformThread thread_; |
| 366 | PerfTestData* data_ = nullptr; |
| 367 | int repeats_ = 0; |
| 368 | int my_id_ = 0; |
| 369 | }; |
| 370 | |
| 371 | // Comparison of output of this test as tested on a MacBook Pro Retina, 15-inch, |
| 372 | // Mid 2014, 2,8 GHz Intel Core i7, 16 GB 1600 MHz DDR3, |
| 373 | // running OS X El Capitan, 10.11.2. |
| 374 | // |
| 375 | // Native mutex implementation: |
| 376 | // Approximate CPU usage: |
| 377 | // System: ~16% |
| 378 | // User mode: ~1.3% |
| 379 | // Idle: ~82% |
| 380 | // Unit test output: |
| 381 | // [ OK ] CriticalSectionTest.Performance (234545 ms) |
| 382 | // |
| 383 | // Special partially spin lock based implementation: |
| 384 | // Approximate CPU usage: |
| 385 | // System: ~75% |
| 386 | // User mode: ~16% |
| 387 | // Idle: ~8% |
| 388 | // Unit test output: |
| 389 | // [ OK ] CriticalSectionTest.Performance (2107 ms) |
| 390 | // |
| 391 | // The test is disabled by default to avoid unecessarily loading the bots. |
| 392 | TEST(CriticalSectionTest, DISABLED_Performance) { |
| 393 | PerfTestThread threads[8]; |
| 394 | Event event(false, false); |
| 395 | |
| 396 | static const int kThreadRepeats = 10000000; |
| 397 | static const int kExpectedCount = kThreadRepeats * arraysize(threads); |
| 398 | PerfTestData test_data(kExpectedCount, &event); |
| 399 | |
| 400 | for (auto& t : threads) |
| 401 | t.Start(&test_data, kThreadRepeats, 1); |
| 402 | |
| 403 | event.Wait(Event::kForever); |
| 404 | |
| 405 | for (auto& t : threads) |
| 406 | t.Stop(); |
| 407 | } |
| 408 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 409 | } // namespace rtc |