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