Scale SpinLock and Mutex spin durations inversely with CPU count. Fleet experiments show spinning longer helps less on larger machines. Use a shared 4,000 ns reference target spin duration on 64-CPU (and smaller) hosts, scaled inversely with NumCPUs() above 64 CPUs, keeping the calibrated spin count between 500 and 12,000 iterations for both SpinLock and Mutex. PiperOrigin-RevId: 1029475437
diff --git a/absl/base/internal/spinlock.cc b/absl/base/internal/spinlock.cc index 4cd163d..8a6e509 100644 --- a/absl/base/internal/spinlock.cc +++ b/absl/base/internal/spinlock.cc
@@ -138,14 +138,22 @@ "SpinLockLoop cleared held lockword"); } +// Target spin duration on a `kReferenceCpus`-CPU host (and smaller hosts). +// Larger hosts scale the target inversely with CPU count. +static constexpr int kReferenceCpus = 64; +static constexpr int64_t kReferenceSpinNanoseconds = 4000; // Minimum spin count on multi-CPU hosts, also used if calibration fails. static constexpr int kMinSpinIterations = 500; // Maximum spin count, which bounds the spin if CycleClock::Frequency() is // wrong. static constexpr int kMaxSpinIterations = 12000; -int CalibrateSpinIterations(void (*absl_nonnull probe_fn)(int iterations), - int64_t target_ns) { +int64_t TargetSpinNanoseconds(int num_cpus) { + return kReferenceSpinNanoseconds * kReferenceCpus / + (std::max)(num_cpus, kReferenceCpus); +} + +int CalibrateSpinIterations(void (*absl_nonnull probe_fn)(int iterations)) { // Keep the fastest of a few readings to filter out preemption. int64_t min_cycles = 0; for (int reading = 0; reading < kProbeReadings; ++reading) { @@ -160,6 +168,8 @@ // CycleClock failed to advance across all readings. return kMinSpinIterations; } + const double target_ns = + static_cast<double>(TargetSpinNanoseconds(NumCPUs())); const double iterations = target_ns / kNanosecondsPerSecond * CycleClock::Frequency() * kProbeIterations / static_cast<double>(min_cycles); @@ -177,9 +187,7 @@ if (adaptive_spin_count_.load(std::memory_order_relaxed) == 0) { int current_spin_count = 0; int new_spin_count = - NumCPUs() > 1 - ? CalibrateSpinIterations(&ProbeSpinLockLoop, kSpinNanoseconds) - : 1; + NumCPUs() > 1 ? CalibrateSpinIterations(&ProbeSpinLockLoop) : 1; // If this fails, the value will remain unchanged. We may not spin for the // intended duration, but that is still safe. We will try again on the next // call to SpinLoop.
diff --git a/absl/base/internal/spinlock.h b/absl/base/internal/spinlock.h index fb814b9..fc4fb7f 100644 --- a/absl/base/internal/spinlock.h +++ b/absl/base/internal/spinlock.h
@@ -245,9 +245,6 @@ const std::atomic<uint32_t>* absl_nonnull lockword, int iterations); static void ProbeSpinLockLoop(int iterations); - // Target duration of SpinLoop() on multi-CPU hosts. - static constexpr int64_t kSpinNanoseconds = 500; - inline bool TryLockImpl() { uint32_t lock_value = lockword_.load(std::memory_order_relaxed); return (TryLockInternal(lock_value, 0) & kSpinLockHeld) == 0; @@ -304,10 +301,14 @@ void RegisterSpinLockLockProfiler(void (*absl_nonnull fn)( const void* absl_nonnull lock, int64_t wait_cycles)); -// Returns the number of `probe_fn` spin-loop iterations that take about -// `target_ns` nanoseconds, clamped to a safe range. -int CalibrateSpinIterations(void (*absl_nonnull probe_fn)(int iterations), - int64_t target_ns); +// Calibrates the number of `probe_fn` spin-loop iterations for a target +// duration scaled inversely with NumCPUs(), clamped to a safe range. +int CalibrateSpinIterations(void (*absl_nonnull probe_fn)(int iterations)); + +// Returns the target spin duration in nanoseconds for a host with `num_cpus` +// CPUs. Exposed for testing. +int64_t TargetSpinNanoseconds(int num_cpus); + //------------------------------------------------------------------------------ // Public interface ends here. //------------------------------------------------------------------------------
diff --git a/absl/base/spinlock_test_common.cc b/absl/base/spinlock_test_common.cc index 4f1bc1b..68a6fa2 100644 --- a/absl/base/spinlock_test_common.cc +++ b/absl/base/spinlock_test_common.cc
@@ -57,7 +57,6 @@ static bool IsCooperative(const SpinLock& l) { return l.IsCooperative(); } - static constexpr int64_t kSpinNanoseconds = SpinLock::kSpinNanoseconds; static uint32_t SpinLoop(SpinLock& l) { return l.SpinLoop(); } static void SetAdaptiveSpinCount(int count) { SpinLock::SetAdaptiveSpinCount(count); @@ -304,6 +303,18 @@ std::scoped_lock l(s); } +TEST(SpinLockTest, TargetSpinNanosecondsScalesWithNumCPUs) { + const int64_t ref_ns = TargetSpinNanoseconds(64); + // Smaller hosts use the 64-CPU reference duration. + EXPECT_EQ(TargetSpinNanoseconds(2), ref_ns); + EXPECT_EQ(TargetSpinNanoseconds(16), ref_ns); + EXPECT_EQ(TargetSpinNanoseconds(32), ref_ns); + // Above 64 CPUs, doubling CPU count halves the target duration. + EXPECT_EQ(TargetSpinNanoseconds(128), ref_ns / 2); + EXPECT_EQ(TargetSpinNanoseconds(256), ref_ns / 4); + EXPECT_GT(TargetSpinNanoseconds(256), TargetSpinNanoseconds(512)); +} + } // namespace } // namespace base_internal ABSL_NAMESPACE_END
diff --git a/absl/synchronization/mutex.cc b/absl/synchronization/mutex.cc index ccc835f..98b9b1a 100644 --- a/absl/synchronization/mutex.cc +++ b/absl/synchronization/mutex.cc
@@ -1590,9 +1590,6 @@ return false; } -// Target duration of TryAcquireWithSpinning() on multi-CPU hosts. -static constexpr int64_t kSpinNanoseconds = 800; - ABSL_ATTRIBUTE_NOINLINE static void ProbeMutexSpinLoop(int iterations) { std::atomic<intptr_t> mu{kMuWriter}; ABSL_RAW_CHECK(!TryAcquireWithSpinning(&mu, iterations), @@ -1916,8 +1913,7 @@ if (absl::base_internal::NumCPUs() > 1) { // If this is multiprocessor, allow spinning. globals.spinloop_iterations.store( - absl::base_internal::CalibrateSpinIterations(&ProbeMutexSpinLoop, - kSpinNanoseconds), + absl::base_internal::CalibrateSpinIterations(&ProbeMutexSpinLoop), std::memory_order_relaxed); } else { // If this a uniprocessor, only yield/sleep.