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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 13 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | #include <set> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <utility> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/arraysize.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 19 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 21 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/event.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/location.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 24 | #include "rtc_base/message_handler.h" |
| 25 | #include "rtc_base/message_queue.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/platform_thread.h" |
| 27 | #include "rtc_base/thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 28 | #include "test/gtest.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | |
| 30 | namespace rtc { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | const int kLongTime = 10000; // 10 seconds |
| 35 | const int kNumThreads = 16; |
| 36 | const int kOperationsToRun = 1000; |
| 37 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 38 | class UniqueValueVerifier { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | public: |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 40 | void Verify(const std::vector<int>& values) { |
| 41 | for (size_t i = 0; i < values.size(); ++i) { |
| 42 | std::pair<std::set<int>::iterator, bool> result = |
| 43 | all_values_.insert(values[i]); |
| 44 | // Each value should only be taken by one thread, so if this value |
| 45 | // has already been added, something went wrong. |
| 46 | EXPECT_TRUE(result.second) |
| 47 | << " Thread=" << Thread::Current() << " value=" << values[i]; |
| 48 | } |
| 49 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 51 | void Finalize() {} |
| 52 | |
| 53 | private: |
| 54 | std::set<int> all_values_; |
| 55 | }; |
| 56 | |
| 57 | class CompareAndSwapVerifier { |
| 58 | public: |
| 59 | CompareAndSwapVerifier() : zero_count_(0) {} |
| 60 | |
| 61 | void Verify(const std::vector<int>& values) { |
| 62 | for (auto v : values) { |
| 63 | if (v == 0) { |
| 64 | EXPECT_EQ(0, zero_count_) << "Thread=" << Thread::Current(); |
| 65 | ++zero_count_; |
| 66 | } else { |
| 67 | EXPECT_EQ(1, v) << " Thread=" << Thread::Current(); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 72 | void Finalize() { EXPECT_EQ(1, zero_count_); } |
| 73 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 74 | private: |
| 75 | int zero_count_; |
| 76 | }; |
| 77 | |
| 78 | class RunnerBase : public MessageHandler { |
| 79 | public: |
| 80 | explicit RunnerBase(int value) |
| 81 | : threads_active_(0), |
| 82 | start_event_(true, false), |
| 83 | done_event_(true, false), |
| 84 | shared_value_(value) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | |
| 86 | bool Run() { |
| 87 | // Signal all threads to start. |
| 88 | start_event_.Set(); |
| 89 | |
| 90 | // Wait for all threads to finish. |
| 91 | return done_event_.Wait(kLongTime); |
| 92 | } |
| 93 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | void SetExpectedThreadCount(int count) { threads_active_ = count; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 95 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 96 | int shared_value() const { return shared_value_; } |
| 97 | |
| 98 | protected: |
| 99 | // Derived classes must override OnMessage, and call BeforeStart and AfterEnd |
| 100 | // at the beginning and the end of OnMessage respectively. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 101 | void BeforeStart() { ASSERT_TRUE(start_event_.Wait(kLongTime)); } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 102 | |
| 103 | // Returns true if all threads have finished. |
| 104 | bool AfterEnd() { |
| 105 | if (AtomicOps::Decrement(&threads_active_) == 0) { |
| 106 | done_event_.Set(); |
| 107 | return true; |
| 108 | } |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | int threads_active_; |
| 113 | Event start_event_; |
| 114 | Event done_event_; |
| 115 | int shared_value_; |
| 116 | }; |
| 117 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 118 | class RTC_LOCKABLE CriticalSectionLock { |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 119 | public: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 120 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { cs_.Enter(); } |
| 121 | void Unlock() RTC_UNLOCK_FUNCTION() { cs_.Leave(); } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | CriticalSection cs_; |
| 125 | }; |
| 126 | |
| 127 | template <class Lock> |
| 128 | class LockRunner : public RunnerBase { |
| 129 | public: |
| 130 | LockRunner() : RunnerBase(0) {} |
| 131 | |
| 132 | void OnMessage(Message* msg) override { |
| 133 | BeforeStart(); |
| 134 | |
| 135 | lock_.Lock(); |
| 136 | |
| 137 | EXPECT_EQ(0, shared_value_); |
| 138 | int old = shared_value_; |
| 139 | |
| 140 | // Use a loop to increase the chance of race. |
| 141 | for (int i = 0; i < kOperationsToRun; ++i) { |
| 142 | ++shared_value_; |
| 143 | } |
| 144 | EXPECT_EQ(old + kOperationsToRun, shared_value_); |
| 145 | shared_value_ = 0; |
| 146 | |
| 147 | lock_.Unlock(); |
| 148 | |
| 149 | AfterEnd(); |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | Lock lock_; |
| 154 | }; |
| 155 | |
| 156 | template <class Op, class Verifier> |
| 157 | class AtomicOpRunner : public RunnerBase { |
| 158 | public: |
| 159 | explicit AtomicOpRunner(int initial_value) : RunnerBase(initial_value) {} |
| 160 | |
| 161 | void OnMessage(Message* msg) override { |
| 162 | BeforeStart(); |
| 163 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 164 | std::vector<int> values; |
| 165 | values.reserve(kOperationsToRun); |
| 166 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 167 | // Generate a bunch of values by updating shared_value_ atomically. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 168 | for (int i = 0; i < kOperationsToRun; ++i) { |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 169 | values.push_back(Op::AtomicOp(&shared_value_)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 172 | { // Add them all to the set. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | CritScope cs(&all_values_crit_); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 174 | verifier_.Verify(values); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 177 | if (AfterEnd()) { |
| 178 | verifier_.Finalize(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | private: |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 183 | CriticalSection all_values_crit_; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 184 | Verifier verifier_; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | struct IncrementOp { |
| 188 | static int AtomicOp(int* i) { return AtomicOps::Increment(i); } |
| 189 | }; |
| 190 | |
| 191 | struct DecrementOp { |
| 192 | static int AtomicOp(int* i) { return AtomicOps::Decrement(i); } |
| 193 | }; |
| 194 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 195 | struct CompareAndSwapOp { |
| 196 | static int AtomicOp(int* i) { return AtomicOps::CompareAndSwap(i, 0, 1); } |
| 197 | }; |
| 198 | |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 199 | void StartThreads(std::vector<std::unique_ptr<Thread>>* threads, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 200 | MessageHandler* handler) { |
| 201 | for (int i = 0; i < kNumThreads; ++i) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 202 | std::unique_ptr<Thread> thread(Thread::Create()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 203 | thread->Start(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 204 | thread->Post(RTC_FROM_HERE, handler); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 205 | threads->push_back(std::move(thread)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | } // namespace |
| 210 | |
| 211 | TEST(AtomicOpsTest, Simple) { |
| 212 | int value = 0; |
| 213 | EXPECT_EQ(1, AtomicOps::Increment(&value)); |
| 214 | EXPECT_EQ(1, value); |
| 215 | EXPECT_EQ(2, AtomicOps::Increment(&value)); |
| 216 | EXPECT_EQ(2, value); |
| 217 | EXPECT_EQ(1, AtomicOps::Decrement(&value)); |
| 218 | EXPECT_EQ(1, value); |
| 219 | EXPECT_EQ(0, AtomicOps::Decrement(&value)); |
| 220 | EXPECT_EQ(0, value); |
| 221 | } |
| 222 | |
Peter Boström | 455a252 | 2015-12-18 17:00:25 +0100 | [diff] [blame] | 223 | TEST(AtomicOpsTest, SimplePtr) { |
| 224 | class Foo {}; |
| 225 | Foo* volatile foo = nullptr; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 226 | std::unique_ptr<Foo> a(new Foo()); |
| 227 | std::unique_ptr<Foo> b(new Foo()); |
Peter Boström | 455a252 | 2015-12-18 17:00:25 +0100 | [diff] [blame] | 228 | // Reading the initial value should work as expected. |
| 229 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == nullptr); |
| 230 | // Setting using compare and swap should work. |
| 231 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr( |
| 232 | &foo, static_cast<Foo*>(nullptr), a.get()) == nullptr); |
| 233 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); |
| 234 | // Setting another value but with the wrong previous pointer should fail |
| 235 | // (remain a). |
| 236 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr( |
| 237 | &foo, static_cast<Foo*>(nullptr), b.get()) == a.get()); |
| 238 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == a.get()); |
| 239 | // Replacing a with b should work. |
| 240 | EXPECT_TRUE(rtc::AtomicOps::CompareAndSwapPtr(&foo, a.get(), b.get()) == |
| 241 | a.get()); |
| 242 | EXPECT_TRUE(rtc::AtomicOps::AcquireLoadPtr(&foo) == b.get()); |
| 243 | } |
| 244 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 245 | TEST(AtomicOpsTest, Increment) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 246 | // Create and start lots of threads. |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 247 | AtomicOpRunner<IncrementOp, UniqueValueVerifier> runner(0); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 248 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | StartThreads(&threads, &runner); |
| 250 | runner.SetExpectedThreadCount(kNumThreads); |
| 251 | |
| 252 | // Release the hounds! |
| 253 | EXPECT_TRUE(runner.Run()); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 254 | EXPECT_EQ(kOperationsToRun * kNumThreads, runner.shared_value()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 255 | } |
| 256 | |
henrike@webrtc.org | c732a3e | 2014-10-09 22:08:15 +0000 | [diff] [blame] | 257 | TEST(AtomicOpsTest, Decrement) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 258 | // Create and start lots of threads. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 259 | AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner(kOperationsToRun * |
| 260 | kNumThreads); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 261 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 262 | StartThreads(&threads, &runner); |
| 263 | runner.SetExpectedThreadCount(kNumThreads); |
| 264 | |
| 265 | // Release the hounds! |
| 266 | EXPECT_TRUE(runner.Run()); |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 267 | EXPECT_EQ(0, runner.shared_value()); |
| 268 | } |
| 269 | |
| 270 | TEST(AtomicOpsTest, CompareAndSwap) { |
| 271 | // Create and start lots of threads. |
| 272 | AtomicOpRunner<CompareAndSwapOp, CompareAndSwapVerifier> runner(0); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 273 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 274 | StartThreads(&threads, &runner); |
| 275 | runner.SetExpectedThreadCount(kNumThreads); |
| 276 | |
| 277 | // Release the hounds! |
| 278 | EXPECT_TRUE(runner.Run()); |
| 279 | EXPECT_EQ(1, runner.shared_value()); |
| 280 | } |
| 281 | |
| 282 | TEST(GlobalLockTest, Basic) { |
| 283 | // Create and start lots of threads. |
| 284 | LockRunner<GlobalLock> runner; |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 285 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 286 | StartThreads(&threads, &runner); |
| 287 | runner.SetExpectedThreadCount(kNumThreads); |
| 288 | |
| 289 | // Release the hounds! |
| 290 | EXPECT_TRUE(runner.Run()); |
| 291 | EXPECT_EQ(0, runner.shared_value()); |
| 292 | } |
| 293 | |
| 294 | TEST(CriticalSectionTest, Basic) { |
| 295 | // Create and start lots of threads. |
| 296 | LockRunner<CriticalSectionLock> runner; |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 297 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 298 | StartThreads(&threads, &runner); |
| 299 | runner.SetExpectedThreadCount(kNumThreads); |
| 300 | |
| 301 | // Release the hounds! |
| 302 | EXPECT_TRUE(runner.Run()); |
| 303 | EXPECT_EQ(0, runner.shared_value()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 304 | } |
| 305 | |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 306 | class PerfTestData { |
| 307 | public: |
| 308 | PerfTestData(int expected_count, Event* event) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 309 | : cache_line_barrier_1_(), |
| 310 | cache_line_barrier_2_(), |
| 311 | expected_count_(expected_count), |
| 312 | event_(event) { |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 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]; |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 398 | Event event; |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 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 |