• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SpinLock(benchmark::State & state)27 static void BM_SpinLock(benchmark::State& state) {
28   // Ensure a ThreadIdentity is installed.
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     absl::base_internal::SpinLockHolder holder(spinlock);
37   }
38 }
39 
40 BENCHMARK_TEMPLATE(BM_SpinLock,
41                    absl::base_internal::SCHEDULE_KERNEL_ONLY)
42     ->UseRealTime()
43     ->Threads(1)
44     ->ThreadPerCpu();
45 
46 BENCHMARK_TEMPLATE(BM_SpinLock,
47                    absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL)
48     ->UseRealTime()
49     ->Threads(1)
50     ->ThreadPerCpu();
51 
52 }  // namespace
53