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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "rtc_base/critical_section.h" |
| 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 16 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | #include <set> |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame] | 18 | #include <type_traits> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 19 | #include <utility> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame] | 22 | #include "absl/base/attributes.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/arraysize.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "rtc_base/event.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 27 | #include "rtc_base/location.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/message_handler.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/platform_thread.h" |
| 30 | #include "rtc_base/thread.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 31 | #include "test/gtest.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | |
| 33 | namespace rtc { |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | const int kLongTime = 10000; // 10 seconds |
| 38 | const int kNumThreads = 16; |
| 39 | const int kOperationsToRun = 1000; |
| 40 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 41 | class UniqueValueVerifier { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | public: |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 43 | void Verify(const std::vector<int>& values) { |
| 44 | for (size_t i = 0; i < values.size(); ++i) { |
| 45 | std::pair<std::set<int>::iterator, bool> result = |
| 46 | all_values_.insert(values[i]); |
| 47 | // Each value should only be taken by one thread, so if this value |
| 48 | // has already been added, something went wrong. |
| 49 | EXPECT_TRUE(result.second) |
| 50 | << " Thread=" << Thread::Current() << " value=" << values[i]; |
| 51 | } |
| 52 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 53 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 54 | void Finalize() {} |
| 55 | |
| 56 | private: |
| 57 | std::set<int> all_values_; |
| 58 | }; |
| 59 | |
| 60 | class CompareAndSwapVerifier { |
| 61 | public: |
| 62 | CompareAndSwapVerifier() : zero_count_(0) {} |
| 63 | |
| 64 | void Verify(const std::vector<int>& values) { |
| 65 | for (auto v : values) { |
| 66 | if (v == 0) { |
| 67 | EXPECT_EQ(0, zero_count_) << "Thread=" << Thread::Current(); |
| 68 | ++zero_count_; |
| 69 | } else { |
| 70 | EXPECT_EQ(1, v) << " Thread=" << Thread::Current(); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 75 | void Finalize() { EXPECT_EQ(1, zero_count_); } |
| 76 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 77 | private: |
| 78 | int zero_count_; |
| 79 | }; |
| 80 | |
| 81 | class RunnerBase : public MessageHandler { |
| 82 | public: |
| 83 | explicit RunnerBase(int value) |
| 84 | : threads_active_(0), |
| 85 | start_event_(true, false), |
| 86 | done_event_(true, false), |
| 87 | shared_value_(value) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | |
| 89 | bool Run() { |
| 90 | // Signal all threads to start. |
| 91 | start_event_.Set(); |
| 92 | |
| 93 | // Wait for all threads to finish. |
| 94 | return done_event_.Wait(kLongTime); |
| 95 | } |
| 96 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 97 | void SetExpectedThreadCount(int count) { threads_active_ = count; } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 98 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 99 | int shared_value() const { return shared_value_; } |
| 100 | |
| 101 | protected: |
| 102 | // Derived classes must override OnMessage, and call BeforeStart and AfterEnd |
| 103 | // at the beginning and the end of OnMessage respectively. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 104 | void BeforeStart() { ASSERT_TRUE(start_event_.Wait(kLongTime)); } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 105 | |
| 106 | // Returns true if all threads have finished. |
| 107 | bool AfterEnd() { |
| 108 | if (AtomicOps::Decrement(&threads_active_) == 0) { |
| 109 | done_event_.Set(); |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | int threads_active_; |
| 116 | Event start_event_; |
| 117 | Event done_event_; |
| 118 | int shared_value_; |
| 119 | }; |
| 120 | |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 121 | class RTC_LOCKABLE CriticalSectionLock { |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 122 | public: |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 123 | void Lock() RTC_EXCLUSIVE_LOCK_FUNCTION() { cs_.Enter(); } |
| 124 | void Unlock() RTC_UNLOCK_FUNCTION() { cs_.Leave(); } |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 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 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 175 | { // Add them all to the set. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 202 | void StartThreads(std::vector<std::unique_ptr<Thread>>* threads, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 203 | MessageHandler* handler) { |
| 204 | for (int i = 0; i < kNumThreads; ++i) { |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 205 | std::unique_ptr<Thread> thread(Thread::Create()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 206 | thread->Start(); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 207 | thread->Post(RTC_FROM_HERE, handler); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 208 | threads->push_back(std::move(thread)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 251 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 262 | AtomicOpRunner<DecrementOp, UniqueValueVerifier> runner(kOperationsToRun * |
| 263 | kNumThreads); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 264 | std::vector<std::unique_ptr<Thread>> threads; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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); |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 276 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 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 | |
Danil Chapovalov | 5740f3e | 2019-10-10 11:12:15 +0200 | [diff] [blame] | 285 | TEST(GlobalLockTest, CanHaveStaticStorageDuration) { |
| 286 | static_assert(std::is_trivially_destructible<GlobalLock>::value, ""); |
| 287 | ABSL_CONST_INIT static GlobalLock global_lock; |
| 288 | global_lock.Lock(); |
| 289 | global_lock.Unlock(); |
| 290 | } |
| 291 | |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 292 | TEST(GlobalLockTest, Basic) { |
| 293 | // Create and start lots of threads. |
| 294 | LockRunner<GlobalLock> 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()); |
| 302 | } |
| 303 | |
| 304 | TEST(CriticalSectionTest, Basic) { |
| 305 | // Create and start lots of threads. |
| 306 | LockRunner<CriticalSectionLock> runner; |
nisse | b9c2f7c | 2017-04-20 02:23:08 -0700 | [diff] [blame] | 307 | std::vector<std::unique_ptr<Thread>> threads; |
Jiayang Liu | bef8d2d | 2015-03-26 14:38:46 -0700 | [diff] [blame] | 308 | StartThreads(&threads, &runner); |
| 309 | runner.SetExpectedThreadCount(kNumThreads); |
| 310 | |
| 311 | // Release the hounds! |
| 312 | EXPECT_TRUE(runner.Run()); |
| 313 | EXPECT_EQ(0, runner.shared_value()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 314 | } |
| 315 | |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 316 | class PerfTestData { |
| 317 | public: |
| 318 | PerfTestData(int expected_count, Event* event) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 319 | : cache_line_barrier_1_(), |
| 320 | cache_line_barrier_2_(), |
| 321 | expected_count_(expected_count), |
| 322 | event_(event) { |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 323 | cache_line_barrier_1_[0]++; // Avoid 'is not used'. |
| 324 | cache_line_barrier_2_[0]++; // Avoid 'is not used'. |
| 325 | } |
| 326 | ~PerfTestData() {} |
| 327 | |
| 328 | void AddToCounter(int add) { |
| 329 | rtc::CritScope cs(&lock_); |
| 330 | my_counter_ += add; |
| 331 | if (my_counter_ == expected_count_) |
| 332 | event_->Set(); |
| 333 | } |
| 334 | |
| 335 | int64_t total() const { |
| 336 | // Assume that only one thread is running now. |
| 337 | return my_counter_; |
| 338 | } |
| 339 | |
| 340 | private: |
| 341 | uint8_t cache_line_barrier_1_[64]; |
| 342 | CriticalSection lock_; |
| 343 | uint8_t cache_line_barrier_2_[64]; |
| 344 | int64_t my_counter_ = 0; |
| 345 | const int expected_count_; |
| 346 | Event* const event_; |
| 347 | }; |
| 348 | |
| 349 | class PerfTestThread { |
| 350 | public: |
| 351 | PerfTestThread() : thread_(&ThreadFunc, this, "CsPerf") {} |
| 352 | |
| 353 | void Start(PerfTestData* data, int repeats, int id) { |
| 354 | RTC_DCHECK(!thread_.IsRunning()); |
| 355 | RTC_DCHECK(!data_); |
| 356 | data_ = data; |
| 357 | repeats_ = repeats; |
| 358 | my_id_ = id; |
| 359 | thread_.Start(); |
| 360 | } |
| 361 | |
| 362 | void Stop() { |
| 363 | RTC_DCHECK(thread_.IsRunning()); |
| 364 | RTC_DCHECK(data_); |
| 365 | thread_.Stop(); |
| 366 | repeats_ = 0; |
| 367 | data_ = nullptr; |
| 368 | my_id_ = 0; |
| 369 | } |
| 370 | |
| 371 | private: |
Niels Möller | 4731f00 | 2019-05-03 09:34:24 +0200 | [diff] [blame] | 372 | static void ThreadFunc(void* param) { |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 373 | PerfTestThread* me = static_cast<PerfTestThread*>(param); |
| 374 | for (int i = 0; i < me->repeats_; ++i) |
| 375 | me->data_->AddToCounter(me->my_id_); |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | PlatformThread thread_; |
| 379 | PerfTestData* data_ = nullptr; |
| 380 | int repeats_ = 0; |
| 381 | int my_id_ = 0; |
| 382 | }; |
| 383 | |
Oskar Sundbom | 13471a4 | 2019-03-01 16:11:49 +0100 | [diff] [blame] | 384 | // Comparison of output of this test as tested on a MacBook Pro, 13-inch, |
| 385 | // 2017, 3,5 GHz Intel Core i7, 16 GB 2133 MHz LPDDR3, |
| 386 | // running macOS Mojave, 10.14.3. |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 387 | // |
Oskar Sundbom | 13471a4 | 2019-03-01 16:11:49 +0100 | [diff] [blame] | 388 | // Native mutex implementation using fair policy (previously macOS default): |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 389 | // Approximate CPU usage: |
Oskar Sundbom | 13471a4 | 2019-03-01 16:11:49 +0100 | [diff] [blame] | 390 | // real 4m54.612s |
| 391 | // user 1m20.575s |
| 392 | // sys 3m48.872s |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 393 | // Unit test output: |
Oskar Sundbom | 13471a4 | 2019-03-01 16:11:49 +0100 | [diff] [blame] | 394 | // [ OK ] CriticalSectionTest.Performance (294375 ms) |
| 395 | // |
| 396 | // Native mutex implementation using first fit policy (current macOS default): |
| 397 | // Approximate CPU usage: |
| 398 | // real 0m11.535s |
| 399 | // user 0m12.738s |
| 400 | // sys 0m31.207s |
| 401 | // Unit test output: |
| 402 | // [ OK ] CriticalSectionTest.Performance (11444 ms) |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 403 | // |
| 404 | // Special partially spin lock based implementation: |
| 405 | // Approximate CPU usage: |
Oskar Sundbom | 13471a4 | 2019-03-01 16:11:49 +0100 | [diff] [blame] | 406 | // real 0m2.113s |
| 407 | // user 0m3.014s |
| 408 | // sys 0m4.495s |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 409 | // Unit test output: |
Oskar Sundbom | 13471a4 | 2019-03-01 16:11:49 +0100 | [diff] [blame] | 410 | // [ OK ] CriticalSectionTest.Performance (1885 ms) |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 411 | // |
| 412 | // The test is disabled by default to avoid unecessarily loading the bots. |
| 413 | TEST(CriticalSectionTest, DISABLED_Performance) { |
| 414 | PerfTestThread threads[8]; |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 415 | Event event; |
tommi | ed281e9 | 2016-01-21 23:47:25 -0800 | [diff] [blame] | 416 | |
| 417 | static const int kThreadRepeats = 10000000; |
| 418 | static const int kExpectedCount = kThreadRepeats * arraysize(threads); |
| 419 | PerfTestData test_data(kExpectedCount, &event); |
| 420 | |
| 421 | for (auto& t : threads) |
| 422 | t.Start(&test_data, kThreadRepeats, 1); |
| 423 | |
| 424 | event.Wait(Event::kForever); |
| 425 | |
| 426 | for (auto& t : threads) |
| 427 | t.Stop(); |
| 428 | } |
| 429 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 430 | } // namespace rtc |