xref: /aosp_15_r20/external/abseil-cpp/absl/base/internal/spinlock_benchmark.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // See also //absl/synchronization:mutex_benchmark for a comparison of SpinLock
16 // and Mutex performance under varying levels of contention.
17 
18 #include "absl/base/internal/raw_logging.h"
19 #include "absl/base/internal/scheduling_mode.h"
20 #include "absl/base/internal/spinlock.h"
21 #include "absl/base/no_destructor.h"
22 #include "absl/synchronization/internal/create_thread_identity.h"
23 #include "benchmark/benchmark.h"
24 
25 namespace {
26 
27 template <absl::base_internal::SchedulingMode scheduling_mode>
BM_TryLock(benchmark::State & state)28 static void BM_TryLock(benchmark::State& state) {
29   // Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect.
30   ABSL_INTERNAL_CHECK(
31       absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() !=
32           nullptr,
33       "GetOrCreateCurrentThreadIdentity() failed");
34 
35   static absl::NoDestructor<absl::base_internal::SpinLock> spinlock(
36       scheduling_mode);
37   for (auto _ : state) {
38     if (spinlock->TryLock()) spinlock->Unlock();
39   }
40 }
41 
42 template <absl::base_internal::SchedulingMode scheduling_mode>
BM_SpinLock(benchmark::State & state)43 static void BM_SpinLock(benchmark::State& state) {
44   // Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect.
45   ABSL_INTERNAL_CHECK(
46       absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() !=
47           nullptr,
48       "GetOrCreateCurrentThreadIdentity() failed");
49 
50   static absl::NoDestructor<absl::base_internal::SpinLock> spinlock(
51       scheduling_mode);
52   for (auto _ : state) {
53     absl::base_internal::SpinLockHolder holder(spinlock.get());
54   }
55 }
56 
57 BENCHMARK_TEMPLATE(BM_SpinLock,
58                    absl::base_internal::SCHEDULE_KERNEL_ONLY)
59     ->UseRealTime()
60     ->Threads(1)
61     ->ThreadPerCpu();
62 
63 BENCHMARK_TEMPLATE(BM_SpinLock,
64                    absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL)
65     ->UseRealTime()
66     ->Threads(1)
67     ->ThreadPerCpu();
68 
69 BENCHMARK_TEMPLATE(BM_TryLock, absl::base_internal::SCHEDULE_KERNEL_ONLY)
70     ->UseRealTime()
71     ->Threads(1)
72     ->ThreadPerCpu();
73 
74 BENCHMARK_TEMPLATE(BM_TryLock,
75                    absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL)
76     ->UseRealTime()
77     ->Threads(1)
78     ->ThreadPerCpu();
79 
80 }  // namespace
81