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