xref: /aosp_15_r20/external/webrtc/rtc_base/synchronization/mutex_benchmark.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2020 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "benchmark/benchmark.h"
12*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
13*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/unused.h"
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker class PerfTestData {
18*d9f75844SAndroid Build Coastguard Worker  public:
PerfTestData()19*d9f75844SAndroid Build Coastguard Worker   PerfTestData() : cache_line_barrier_1_(), cache_line_barrier_2_() {
20*d9f75844SAndroid Build Coastguard Worker     cache_line_barrier_1_[0]++;  // Avoid 'is not used'.
21*d9f75844SAndroid Build Coastguard Worker     cache_line_barrier_2_[0]++;  // Avoid 'is not used'.
22*d9f75844SAndroid Build Coastguard Worker   }
23*d9f75844SAndroid Build Coastguard Worker 
AddToCounter(int add)24*d9f75844SAndroid Build Coastguard Worker   int AddToCounter(int add) {
25*d9f75844SAndroid Build Coastguard Worker     MutexLock mu(&mu_);
26*d9f75844SAndroid Build Coastguard Worker     my_counter_ += add;
27*d9f75844SAndroid Build Coastguard Worker     return 0;
28*d9f75844SAndroid Build Coastguard Worker   }
29*d9f75844SAndroid Build Coastguard Worker 
30*d9f75844SAndroid Build Coastguard Worker  private:
31*d9f75844SAndroid Build Coastguard Worker   uint8_t cache_line_barrier_1_[64];
32*d9f75844SAndroid Build Coastguard Worker   Mutex mu_;
33*d9f75844SAndroid Build Coastguard Worker   uint8_t cache_line_barrier_2_[64];
34*d9f75844SAndroid Build Coastguard Worker   int64_t my_counter_ = 0;
35*d9f75844SAndroid Build Coastguard Worker };
36*d9f75844SAndroid Build Coastguard Worker 
BM_LockWithMutex(benchmark::State & state)37*d9f75844SAndroid Build Coastguard Worker void BM_LockWithMutex(benchmark::State& state) {
38*d9f75844SAndroid Build Coastguard Worker   static PerfTestData test_data;
39*d9f75844SAndroid Build Coastguard Worker   for (auto s : state) {
40*d9f75844SAndroid Build Coastguard Worker     RTC_UNUSED(s);
41*d9f75844SAndroid Build Coastguard Worker     benchmark::DoNotOptimize(test_data.AddToCounter(2));
42*d9f75844SAndroid Build Coastguard Worker   }
43*d9f75844SAndroid Build Coastguard Worker }
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker BENCHMARK(BM_LockWithMutex)->Threads(1);
46*d9f75844SAndroid Build Coastguard Worker BENCHMARK(BM_LockWithMutex)->Threads(2);
47*d9f75844SAndroid Build Coastguard Worker BENCHMARK(BM_LockWithMutex)->Threads(4);
48*d9f75844SAndroid Build Coastguard Worker BENCHMARK(BM_LockWithMutex)->ThreadPerCpu();
49*d9f75844SAndroid Build Coastguard Worker 
50*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
51*d9f75844SAndroid Build Coastguard Worker 
52*d9f75844SAndroid Build Coastguard Worker /*
53*d9f75844SAndroid Build Coastguard Worker 
54*d9f75844SAndroid Build Coastguard Worker Results:
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker NB when reproducing: Remember to turn of power management features such as CPU
57*d9f75844SAndroid Build Coastguard Worker scaling before running!
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker pthreads (Linux):
60*d9f75844SAndroid Build Coastguard Worker ----------------------------------------------------------------------
61*d9f75844SAndroid Build Coastguard Worker Run on (12 X 4500 MHz CPU s)
62*d9f75844SAndroid Build Coastguard Worker CPU Caches:
63*d9f75844SAndroid Build Coastguard Worker   L1 Data 32 KiB (x6)
64*d9f75844SAndroid Build Coastguard Worker   L1 Instruction 32 KiB (x6)
65*d9f75844SAndroid Build Coastguard Worker   L2 Unified 1024 KiB (x6)
66*d9f75844SAndroid Build Coastguard Worker   L3 Unified 8448 KiB (x1)
67*d9f75844SAndroid Build Coastguard Worker Load Average: 0.26, 0.28, 0.44
68*d9f75844SAndroid Build Coastguard Worker ----------------------------------------------------------------------
69*d9f75844SAndroid Build Coastguard Worker Benchmark                            Time             CPU   Iterations
70*d9f75844SAndroid Build Coastguard Worker ----------------------------------------------------------------------
71*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:1        13.4 ns         13.4 ns     52192906
72*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:2        44.2 ns         88.4 ns      8189944
73*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:4        52.0 ns          198 ns      3743244
74*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:12       84.9 ns          944 ns       733524
75*d9f75844SAndroid Build Coastguard Worker 
76*d9f75844SAndroid Build Coastguard Worker std::mutex performs like the pthread implementation (Linux).
77*d9f75844SAndroid Build Coastguard Worker 
78*d9f75844SAndroid Build Coastguard Worker Abseil (Linux):
79*d9f75844SAndroid Build Coastguard Worker ----------------------------------------------------------------------
80*d9f75844SAndroid Build Coastguard Worker Run on (12 X 4500 MHz CPU s)
81*d9f75844SAndroid Build Coastguard Worker CPU Caches:
82*d9f75844SAndroid Build Coastguard Worker   L1 Data 32 KiB (x6)
83*d9f75844SAndroid Build Coastguard Worker   L1 Instruction 32 KiB (x6)
84*d9f75844SAndroid Build Coastguard Worker   L2 Unified 1024 KiB (x6)
85*d9f75844SAndroid Build Coastguard Worker   L3 Unified 8448 KiB (x1)
86*d9f75844SAndroid Build Coastguard Worker Load Average: 0.27, 0.24, 0.37
87*d9f75844SAndroid Build Coastguard Worker ----------------------------------------------------------------------
88*d9f75844SAndroid Build Coastguard Worker Benchmark                            Time             CPU   Iterations
89*d9f75844SAndroid Build Coastguard Worker ----------------------------------------------------------------------
90*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:1        15.0 ns         15.0 ns     46550231
91*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:2        91.1 ns          182 ns      4059212
92*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:4        40.8 ns          131 ns      5496560
93*d9f75844SAndroid Build Coastguard Worker BM_LockWithMutex/threads:12       37.0 ns          130 ns      5377668
94*d9f75844SAndroid Build Coastguard Worker 
95*d9f75844SAndroid Build Coastguard Worker */
96