00001 // Copyright 2018 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 00015 // See also //absl/synchronization:mutex_benchmark for a comparison of SpinLock 00016 // and Mutex performance under varying levels of contention. 00017 00018 #include "absl/base/internal/raw_logging.h" 00019 #include "absl/base/internal/scheduling_mode.h" 00020 #include "absl/base/internal/spinlock.h" 00021 #include "absl/synchronization/internal/create_thread_identity.h" 00022 #include "benchmark/benchmark.h" 00023 00024 namespace { 00025 00026 template <absl::base_internal::SchedulingMode scheduling_mode> 00027 static void BM_SpinLock(benchmark::State& state) { 00028 // Ensure a ThreadIdentity is installed. 00029 ABSL_INTERNAL_CHECK( 00030 absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() != 00031 nullptr, 00032 "GetOrCreateCurrentThreadIdentity() failed"); 00033 00034 static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode); 00035 for (auto _ : state) { 00036 absl::base_internal::SpinLockHolder holder(spinlock); 00037 } 00038 } 00039 00040 BENCHMARK_TEMPLATE(BM_SpinLock, 00041 absl::base_internal::SCHEDULE_KERNEL_ONLY) 00042 ->UseRealTime() 00043 ->Threads(1) 00044 ->ThreadPerCpu(); 00045 00046 BENCHMARK_TEMPLATE(BM_SpinLock, 00047 absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL) 00048 ->UseRealTime() 00049 ->Threads(1) 00050 ->ThreadPerCpu(); 00051 00052 } // namespace