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/synchronization/internal/create_thread_identity.h"
22 #include "benchmark/benchmark.h"
23
24 namespace {
25
26 template <absl::base_internal::SchedulingMode scheduling_mode>
BM_TryLock(benchmark::State & state)27 static void BM_TryLock(benchmark::State& state) {
28 // Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect.
29 ABSL_INTERNAL_CHECK(
30 absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() !=
31 nullptr,
32 "GetOrCreateCurrentThreadIdentity() failed");
33
34 static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode);
35 for (auto _ : state) {
36 if (spinlock->TryLock()) spinlock->Unlock();
37 }
38 }
39
40 template <absl::base_internal::SchedulingMode scheduling_mode>
BM_SpinLock(benchmark::State & state)41 static void BM_SpinLock(benchmark::State& state) {
42 // Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect.
43 ABSL_INTERNAL_CHECK(
44 absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() !=
45 nullptr,
46 "GetOrCreateCurrentThreadIdentity() failed");
47
48 static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode);
49 for (auto _ : state) {
50 absl::base_internal::SpinLockHolder holder(spinlock);
51 }
52 }
53
54 BENCHMARK_TEMPLATE(BM_SpinLock,
55 absl::base_internal::SCHEDULE_KERNEL_ONLY)
56 ->UseRealTime()
57 ->Threads(1)
58 ->ThreadPerCpu();
59
60 BENCHMARK_TEMPLATE(BM_SpinLock,
61 absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL)
62 ->UseRealTime()
63 ->Threads(1)
64 ->ThreadPerCpu();
65
66 BENCHMARK_TEMPLATE(BM_TryLock, absl::base_internal::SCHEDULE_KERNEL_ONLY)
67 ->UseRealTime()
68 ->Threads(1)
69 ->ThreadPerCpu();
70
71 BENCHMARK_TEMPLATE(BM_TryLock,
72 absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL)
73 ->UseRealTime()
74 ->Threads(1)
75 ->ThreadPerCpu();
76
77 } // namespace
78