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