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 | |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 15 | #include "webrtc/rtc_base/arraysize.h" |
| 16 | #include "webrtc/rtc_base/checks.h" |
| 17 | #include "webrtc/rtc_base/criticalsection.h" |
| 18 | #include "webrtc/rtc_base/event.h" |
| 19 | #include "webrtc/rtc_base/gunit.h" |
| 20 | #include "webrtc/rtc_base/platform_thread.h" |
| 21 | #include "webrtc/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 | |
| 116 | class LOCKABLE CriticalSectionLock { |
| 117 | public: |
| 118 | void Lock() EXCLUSIVE_LOCK_FUNCTION() { |
| 119 | cs_.Enter(); |
| 120 | } |
| 121 | void Unlock() UNLOCK_FUNCTION() { |
| 122 | cs_.Leave(); |
| 123 | } |
| 124 | |
| 125 | private: |
| 126 | CriticalSection cs_; |
| 127 | }; |
| 128 | |
| 129 | template <class Lock> |
| 130 | class LockRunner : public RunnerBase { |
| 131 | public: |
| 132 | LockRunner() : RunnerBase(0) {} |
| 133 | |
| 134 | void OnMessage(Message* msg) override { |
| 135 | BeforeStart(); |
| 136 | |
| 137 | lock_.Lock(); |
| 138 | |
| 139 | EXPECT_EQ(0, shared_value_); |
| 140 | int old = shared_value_; |
| 141 | |
| 142 | // Use a loop to increase the chance of race. |
| 143 | for (int i = 0; i < kOperationsToRun; ++i) { |
| 144 | ++shared_value_; |
| 145 | } |
| 146 | EXPECT_EQ(old + kOperationsToRun, shared_value_); |
| 147 | shared_value_ = 0; |
| 148 | |
| 149 | lock_.Unlock(); |
| 150 | |
| 151 | AfterEnd(); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | Lock lock_; |
| 156 | }; |
| 157 | |
| 158 | template <class Op, class Verifier> |
| 159 | class AtomicOpRunner : public RunnerBase { |
| 160 | public: |
| 161 | explicit AtomicOpRunner(int initial_value) : RunnerBase(initial_value) {} |
| 162 | |
| 163 | void OnMessage(Message* msg) override { |
| 164 | BeforeStart(); |
| 165 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 166 | std::vector<int> values; |
| 167 | values.reserve(kOperationsToRun); |
| 168 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 169 | // Generate a bunch of values by updating shared_value_ atomically. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 170 | for (int i = 0; i < kOperationsToRun; ++i) { |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 171 | values.push_back(Op::AtomicOp(&shared_value_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | { // Add them all to the set. |
| 175 | CritScope cs(&all_values_crit_); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 176 | verifier_.Verify(values); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 179 | if (AfterEnd()) { |
| 180 | verifier_.Finalize(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | CriticalSection all_values_crit_; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 186 | Verifier verifier_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | struct IncrementOp { |
| 190 | static int AtomicOp(int* i) { return AtomicOps::Increment(i); } |
| 191 | }; |
| 192 | |
| 193 | struct DecrementOp { |
| 194 | static int AtomicOp(int* i) { return AtomicOps::Decrement(i); } |
| 195 | }; |
| 196 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 197 | struct CompareAndSwapOp { |
| 198 | static int AtomicOp(int* i) { return AtomicOps::CompareAndSwap(i, 0, 1); } |
| 199 | }; |
| 200 | |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 201 | void StartThreads(std::vector<std::unique_ptr<Thread>>* threads, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | MessageHandler* handler) { |
| 203 | for (int i = 0; i < kNumThreads; ++i) { |
tommi | a8a3515 | 2017-07-13 05:47:25 -0700 | [diff] [blame^] | 204 | std::unique_ptr<Thread> thread(Thread::Create()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 205 | thread->Start(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 206 | thread->Post(RTC_FROM_HERE, handler); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 207 | threads->push_back(std::move(thread)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | } // namespace |
| 212 | |
| 213 | TEST(AtomicOpsTest, Simple) { |
| 214 | int value = 0; |
| 215 | EXPECT_EQ(1, AtomicOps::Increment(&value)); |
| 216 | EXPECT_EQ(1, value); |
| 217 | EXPECT_EQ(2, AtomicOps::Increment(&value)); |
| 218 | EXPECT_EQ(2, value); |
| 219 | EXPECT_EQ(1, AtomicOps::Decrement(&value)); |
| 220 | EXPECT_EQ(1, value); |
| 221 | EXPECT_EQ(0, AtomicOps::Decrement(&value)); |
| 222 | EXPECT_EQ(0, value); |
| 223 | } |
| 224 | |
Peter Boström | 455a252 | 2015-12-18 17:00:25 +0100 | [diff] [blame] | 225 | TEST(AtomicOpsTest, SimplePtr) { |
| 226 | class Foo {}; |
| 227 | Foo* volatile foo = nullptr; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 228 | std::unique_ptr<Foo> a(new Foo()); |
| 229 | std::unique_ptr<Foo> b(new Foo()); |
Peter Boström | 455a252 | 2015-12-18 17:00:25 +0100 | [diff] [blame] | 230 | // Reading the initial value should work as expected. |
| 231 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == nullptr); |
| 232 | // Setting using compare and swap should work. |
| 233 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr( |
| 234 | &foo, static_cast<Foo*>(nullptr), a.get()) == nullptr); |
| 235 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); |
| 236 | // Setting another value but with the wrong previous pointer should fail |
| 237 | // (remain a). |
| 238 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr( |
| 239 | &foo, static_cast<Foo*>(nullptr), b.get()) == a.get()); |
| 240 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); |
| 241 | // Replacing a with b should work. |
| 242 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(&foo, a.get(), b.get()) == |
| 243 | a.get()); |
| 244 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == b.get()); |
| 245 | } |
| 246 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 247 | TEST(AtomicOpsTest, Increment) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 248 | // Create and start lots of threads. |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 249 | AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 250 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 251 | StartThreads(&threads, &runner); |
| 252 | runner.SetExpectedThreadCount(kNumThreads); |
| 253 | |
| 254 | // Release the hounds! |
| 255 | EXPECT_TRUE(runner.Run()); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 256 | EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 257 | } |
| 258 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 259 | TEST(AtomicOpsTest, Decrement) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 260 | // Create and start lots of threads. |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 261 | AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner( |
| 262 | kOperationsToRun * kNumThreads); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 263 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 264 | StartThreads(&threads, &runner); |
| 265 | runner.SetExpectedThreadCount(kNumThreads); |
| 266 | |
| 267 | // Release the hounds! |
| 268 | EXPECT_TRUE(runner.Run()); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 269 | EXPECT_EQ(0, runner.shared_value()); |
| 270 | } |
| 271 | |
| 272 | TEST(AtomicOpsTest, CompareAndSwap) { |
| 273 | // Create and start lots of threads. |
| 274 | AtomicOpRunner<CompareAndSwapOp, CompareAndSwapVerifier> runner(0); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 275 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 276 | StartThreads(&threads, &runner); |
| 277 | runner.SetExpectedThreadCount(kNumThreads); |
| 278 | |
| 279 | // Release the hounds! |
| 280 | EXPECT_TRUE(runner.Run()); |
| 281 | EXPECT_EQ(1, runner.shared_value()); |
| 282 | } |
| 283 | |
| 284 | TEST(GlobalLockTest, Basic) { |
| 285 | // Create and start lots of threads. |
| 286 | LockRunner<GlobalLock> runner; |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 287 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 288 | StartThreads(&threads, &runner); |
| 289 | runner.SetExpectedThreadCount(kNumThreads); |
| 290 | |
| 291 | // Release the hounds! |
| 292 | EXPECT_TRUE(runner.Run()); |
| 293 | EXPECT_EQ(0, runner.shared_value()); |
| 294 | } |
| 295 | |
| 296 | TEST(CriticalSectionTest, Basic) { |
| 297 | // Create and start lots of threads. |
| 298 | LockRunner<CriticalSectionLock> runner; |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 299 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 300 | StartThreads(&threads, &runner); |
| 301 | runner.SetExpectedThreadCount(kNumThreads); |
| 302 | |
| 303 | // Release the hounds! |
| 304 | EXPECT_TRUE(runner.Run()); |
| 305 | EXPECT_EQ(0, runner.shared_value()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 306 | } |
| 307 | |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 308 | class PerfTestData { |
| 309 | public: |
| 310 | PerfTestData(int expected_count, Event* event) |
| 311 | : cache_line_barrier_1_(), cache_line_barrier_2_(), |
| 312 | expected_count_(expected_count), event_(event) { |
| 313 | cache_line_barrier_1_[0]++; // Avoid 'is not used'. |
| 314 | cache_line_barrier_2_[0]++; // Avoid 'is not used'. |
| 315 | } |
| 316 | ~PerfTestData() {} |
| 317 | |
| 318 | void AddToCounter(int add) { |
| 319 | rtc::CritScope cs(&lock_); |
| 320 | my_counter_ += add; |
| 321 | if (my_counter_ == expected_count_) |
| 322 | event_->Set(); |
| 323 | } |
| 324 | |
| 325 | int64_t total() const { |
| 326 | // Assume that only one thread is running now. |
| 327 | return my_counter_; |
| 328 | } |
| 329 | |
| 330 | private: |
| 331 | uint8_t cache_line_barrier_1_[64]; |
| 332 | CriticalSection lock_; |
| 333 | uint8_t cache_line_barrier_2_[64]; |
| 334 | int64_t my_counter_ = 0; |
| 335 | const int expected_count_; |
| 336 | Event* const event_; |
| 337 | }; |
| 338 | |
| 339 | class PerfTestThread { |
| 340 | public: |
| 341 | PerfTestThread() : thread_(&ThreadFunc, this, "CsPerf") {} |
| 342 | |
| 343 | void Start(PerfTestData* data, int repeats, int id) { |
| 344 | RTC_DCHECK(!thread_.IsRunning()); |
| 345 | RTC_DCHECK(!data_); |
| 346 | data_ = data; |
| 347 | repeats_ = repeats; |
| 348 | my_id_ = id; |
| 349 | thread_.Start(); |
| 350 | } |
| 351 | |
| 352 | void Stop() { |
| 353 | RTC_DCHECK(thread_.IsRunning()); |
| 354 | RTC_DCHECK(data_); |
| 355 | thread_.Stop(); |
| 356 | repeats_ = 0; |
| 357 | data_ = nullptr; |
| 358 | my_id_ = 0; |
| 359 | } |
| 360 | |
| 361 | private: |
| 362 | static bool ThreadFunc(void* param) { |
| 363 | PerfTestThread* me = static_cast<PerfTestThread*>(param); |
| 364 | for (int i = 0; i < me->repeats_; ++i) |
| 365 | me->data_->AddToCounter(me->my_id_); |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | PlatformThread thread_; |
| 370 | PerfTestData* data_ = nullptr; |
| 371 | int repeats_ = 0; |
| 372 | int my_id_ = 0; |
| 373 | }; |
| 374 | |
| 375 | // Comparison of output of this test as tested on a MacBook Pro Retina, 15-inch, |
| 376 | // Mid 2014, 2,8 GHz Intel Core i7, 16 GB 1600 MHz DDR3, |
| 377 | // running OS X El Capitan, 10.11.2. |
| 378 | // |
| 379 | // Native mutex implementation: |
| 380 | // Approximate CPU usage: |
| 381 | // System: ~16% |
| 382 | // User mode: ~1.3% |
| 383 | // Idle: ~82% |
| 384 | // Unit test output: |
| 385 | // [ OK ] CriticalSectionTest.Performance (234545 ms) |
| 386 | // |
| 387 | // Special partially spin lock based implementation: |
| 388 | // Approximate CPU usage: |
| 389 | // System: ~75% |
| 390 | // User mode: ~16% |
| 391 | // Idle: ~8% |
| 392 | // Unit test output: |
| 393 | // [ OK ] CriticalSectionTest.Performance (2107 ms) |
| 394 | // |
| 395 | // The test is disabled by default to avoid unecessarily loading the bots. |
| 396 | TEST(CriticalSectionTest, DISABLED_Performance) { |
| 397 | PerfTestThread threads[8]; |
| 398 | Event event(false, false); |
| 399 | |
| 400 | static const int kThreadRepeats = 10000000; |
| 401 | static const int kExpectedCount = kThreadRepeats * arraysize(threads); |
| 402 | PerfTestData test_data(kExpectedCount, &event); |
| 403 | |
| 404 | for (auto& t : threads) |
| 405 | t.Start(&test_data, kThreadRepeats, 1); |
| 406 | |
| 407 | event.Wait(Event::kForever); |
| 408 | |
| 409 | for (auto& t : threads) |
| 410 | t.Stop(); |
| 411 | } |
| 412 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 413 | } // namespace rtc |