blob: 40adca65d86cad678a343efa583949d89e2b6439 [file] [log] [blame]
Markus Handellf70fbc82020-06-04 00:41:20 +02001/*
2 * Copyright 2020 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
11#include "benchmark/benchmark.h"
12#include "rtc_base/synchronization/mutex.h"
Markus Handell2d27b1a2020-06-09 17:34:41 +020013#include "rtc_base/system/unused.h"
Markus Handellf70fbc82020-06-04 00:41:20 +020014
15namespace webrtc {
16
17class PerfTestData {
18 public:
19 PerfTestData() : cache_line_barrier_1_(), cache_line_barrier_2_() {
20 cache_line_barrier_1_[0]++; // Avoid 'is not used'.
21 cache_line_barrier_2_[0]++; // Avoid 'is not used'.
22 }
23
24 int AddToCounter(int add) {
25 MutexLock mu(&mu_);
26 my_counter_ += add;
27 return 0;
28 }
29
30 private:
31 uint8_t cache_line_barrier_1_[64];
32 Mutex mu_;
33 uint8_t cache_line_barrier_2_[64];
34 int64_t my_counter_ = 0;
35};
36
Mirko Bonadeib2f73042020-06-04 17:40:21 +020037void BM_LockWithMutex(benchmark::State& state) {
Markus Handellf70fbc82020-06-04 00:41:20 +020038 static PerfTestData test_data;
Markus Handell2d27b1a2020-06-09 17:34:41 +020039 for (auto s : state) {
40 RTC_UNUSED(s);
Markus Handellf70fbc82020-06-04 00:41:20 +020041 benchmark::DoNotOptimize(test_data.AddToCounter(2));
42 }
43}
44
45BENCHMARK(BM_LockWithMutex)->Threads(1);
46BENCHMARK(BM_LockWithMutex)->Threads(2);
47BENCHMARK(BM_LockWithMutex)->Threads(4);
48BENCHMARK(BM_LockWithMutex)->ThreadPerCpu();
49
50} // namespace webrtc
51
52/*
53
54Results:
55
56NB when reproducing: Remember to turn of power management features such as CPU
57scaling before running!
58
59pthreads (Linux):
60----------------------------------------------------------------------
61Run on (12 X 4500 MHz CPU s)
62CPU Caches:
63 L1 Data 32 KiB (x6)
64 L1 Instruction 32 KiB (x6)
65 L2 Unified 1024 KiB (x6)
66 L3 Unified 8448 KiB (x1)
67Load Average: 0.26, 0.28, 0.44
68----------------------------------------------------------------------
69Benchmark Time CPU Iterations
70----------------------------------------------------------------------
71BM_LockWithMutex/threads:1 13.4 ns 13.4 ns 52192906
72BM_LockWithMutex/threads:2 44.2 ns 88.4 ns 8189944
73BM_LockWithMutex/threads:4 52.0 ns 198 ns 3743244
74BM_LockWithMutex/threads:12 84.9 ns 944 ns 733524
75
76std::mutex performs like the pthread implementation (Linux).
77
78Abseil (Linux):
79----------------------------------------------------------------------
80Run on (12 X 4500 MHz CPU s)
81CPU Caches:
82 L1 Data 32 KiB (x6)
83 L1 Instruction 32 KiB (x6)
84 L2 Unified 1024 KiB (x6)
85 L3 Unified 8448 KiB (x1)
86Load Average: 0.27, 0.24, 0.37
87----------------------------------------------------------------------
88Benchmark Time CPU Iterations
89----------------------------------------------------------------------
90BM_LockWithMutex/threads:1 15.0 ns 15.0 ns 46550231
91BM_LockWithMutex/threads:2 91.1 ns 182 ns 4059212
92BM_LockWithMutex/threads:4 40.8 ns 131 ns 5496560
93BM_LockWithMutex/threads:12 37.0 ns 130 ns 5377668
94
95*/