Skip to content

Commit

Permalink
Add a new microbenchmark for spinlock TryLock and codegen functions.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 579852413
Change-Id: I1eddd70a4bee974e3a5109109ddfc4b4fd8f421e
  • Loading branch information
Abseil Team authored and copybara-github committed Nov 6, 2023
1 parent 1cb3345 commit 3be3177
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion absl/base/internal/spinlock_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,23 @@

namespace {

template <absl::base_internal::SchedulingMode scheduling_mode>
static void BM_TryLock(benchmark::State& state) {
// Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect.
ABSL_INTERNAL_CHECK(
absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() !=
nullptr,
"GetOrCreateCurrentThreadIdentity() failed");

static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode);
for (auto _ : state) {
if (spinlock->TryLock()) spinlock->Unlock();
}
}

template <absl::base_internal::SchedulingMode scheduling_mode>
static void BM_SpinLock(benchmark::State& state) {
// Ensure a ThreadIdentity is installed.
// Ensure a ThreadIdentity is installed so that KERNEL_ONLY has an effect.
ABSL_INTERNAL_CHECK(
absl::synchronization_internal::GetOrCreateCurrentThreadIdentity() !=
nullptr,
Expand All @@ -49,4 +63,15 @@ BENCHMARK_TEMPLATE(BM_SpinLock,
->Threads(1)
->ThreadPerCpu();

BENCHMARK_TEMPLATE(BM_TryLock, absl::base_internal::SCHEDULE_KERNEL_ONLY)
->UseRealTime()
->Threads(1)
->ThreadPerCpu();

BENCHMARK_TEMPLATE(BM_TryLock,
absl::base_internal::SCHEDULE_COOPERATIVE_AND_KERNEL)
->UseRealTime()
->Threads(1)
->ThreadPerCpu();

} // namespace

0 comments on commit 3be3177

Please sign in to comment.