From 68658ca5736957c91997b849b990de9d5a7b3ed2 Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Wed, 13 Aug 2025 10:43:34 -0400 Subject: [PATCH 1/2] Pad SpinLock to avoid false sharing --- ddprof-lib/src/main/cpp/arch_dd.h | 2 ++ ddprof-lib/src/main/cpp/spinLock.h | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ddprof-lib/src/main/cpp/arch_dd.h b/ddprof-lib/src/main/cpp/arch_dd.h index 2f067647e..4fb4e166d 100644 --- a/ddprof-lib/src/main/cpp/arch_dd.h +++ b/ddprof-lib/src/main/cpp/arch_dd.h @@ -5,6 +5,8 @@ #include +constexpr int DEFAULT_CACHE_LINE_SIZE = 64; + static inline long long atomicInc(volatile long long &var, long long increment = 1) { return __sync_fetch_and_add(&var, increment); diff --git a/ddprof-lib/src/main/cpp/spinLock.h b/ddprof-lib/src/main/cpp/spinLock.h index e47ff5221..9fec8bec5 100644 --- a/ddprof-lib/src/main/cpp/spinLock.h +++ b/ddprof-lib/src/main/cpp/spinLock.h @@ -21,13 +21,13 @@ // Cannot use regular mutexes inside signal handler. // This lock is based on CAS busy loop. GCC atomic builtins imply full barrier. -class SpinLock { +class alignas(DEFAULT_CACHE_LINE_SIZE) SpinLock { private: // 0 - unlocked // 1 - exclusive lock // <0 - shared lock volatile int _lock; - + char _padding[DEFAULT_CACHE_LINE_SIZE - sizeof(_lock)]; public: constexpr SpinLock(int initial_state = 0) : _lock(initial_state) {} From 8a7e99fcb3c3bf4e5b4ebd857ec8b50750ce08c2 Mon Sep 17 00:00:00 2001 From: Zhengyu Gu Date: Wed, 13 Aug 2025 11:17:00 -0400 Subject: [PATCH 2/2] Fix a compilation error --- ddprof-lib/src/main/cpp/spinLock.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ddprof-lib/src/main/cpp/spinLock.h b/ddprof-lib/src/main/cpp/spinLock.h index 9fec8bec5..a2741e145 100644 --- a/ddprof-lib/src/main/cpp/spinLock.h +++ b/ddprof-lib/src/main/cpp/spinLock.h @@ -29,7 +29,9 @@ class alignas(DEFAULT_CACHE_LINE_SIZE) SpinLock { volatile int _lock; char _padding[DEFAULT_CACHE_LINE_SIZE - sizeof(_lock)]; public: - constexpr SpinLock(int initial_state = 0) : _lock(initial_state) {} + constexpr SpinLock(int initial_state = 0) : _lock(initial_state), _padding() { + static_assert(sizeof(SpinLock) == DEFAULT_CACHE_LINE_SIZE); + } void reset() { _lock = 0; }