From 5fedbc8ec1d2509435a968ba2e3830e6fc75bd53 Mon Sep 17 00:00:00 2001 From: Raphael Steiner Date: Tue, 31 Mar 2026 07:04:51 +0200 Subject: [PATCH 1/2] affinity in Ringbuffer Benchmark --- benchmarks/RingBuffer.cpp | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/benchmarks/RingBuffer.cpp b/benchmarks/RingBuffer.cpp index 7099833..9f83392 100644 --- a/benchmarks/RingBuffer.cpp +++ b/benchmarks/RingBuffer.cpp @@ -19,6 +19,9 @@ limitations under the License. #include "RingBuffer/RingBuffer.hpp" #include +#include + +#include using namespace spapq; @@ -101,8 +104,46 @@ static void BM_RingBuffer_2Threads_optional(benchmark::State &state) { RingBuffer chan_opt; if (producer) { + pthread_t self = pthread_self(); + cpu_set_t cpuset; + CPU_ZERO(&cpuset); + const std::size_t logicalCore = 0U; + CPU_SET(logicalCore, &cpuset); + + int rc = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset); + if (rc != 0) { + const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + + std::to_string(rc) + + ".\nFailed to pin worker number " + + std::to_string(N) + + "'s thread to logical core " + + std::to_string(logicalCore) + + ".\n"; + std::cerr << errorMessage; + std::exit(EXIT_FAILURE); + } + start_optional.wait(false, std::memory_order_acquire); } else { + pthread_t self = pthread_self(); + cpu_set_t cpuset; + CPU_ZERO(&cpuset); + const std::size_t logicalCore = 1U; + CPU_SET(logicalCore, &cpuset); + + int rc = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset); + if (rc != 0) { + const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + + std::to_string(rc) + + ".\nFailed to pin worker number " + + std::to_string(N) + + "'s thread to logical core " + + std::to_string(logicalCore) + + ".\n"; + std::cerr << errorMessage; + std::exit(EXIT_FAILURE); + } + channel_optional = &chan_opt; start_optional.test_and_set(std::memory_order_release); start_optional.notify_all(); @@ -150,8 +191,46 @@ static void BM_RingBuffer_2Threads_reference(benchmark::State &state) { RingBuffer chan_opt; if (producer) { + pthread_t self = pthread_self(); + cpu_set_t cpuset; + CPU_ZERO(&cpuset); + const std::size_t logicalCore = 0U; + CPU_SET(logicalCore, &cpuset); + + int rc = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset); + if (rc != 0) { + const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + + std::to_string(rc) + + ".\nFailed to pin worker number " + + std::to_string(N) + + "'s thread to logical core " + + std::to_string(logicalCore) + + ".\n"; + std::cerr << errorMessage; + std::exit(EXIT_FAILURE); + } + start_reference.wait(false, std::memory_order_acquire); } else { + pthread_t self = pthread_self(); + cpu_set_t cpuset; + CPU_ZERO(&cpuset); + const std::size_t logicalCore = 1U; + CPU_SET(logicalCore, &cpuset); + + int rc = pthread_setaffinity_np(self, sizeof(cpu_set_t), &cpuset); + if (rc != 0) { + const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + + std::to_string(rc) + + ".\nFailed to pin worker number " + + std::to_string(N) + + "'s thread to logical core " + + std::to_string(logicalCore) + + ".\n"; + std::cerr << errorMessage; + std::exit(EXIT_FAILURE); + } + channel_reference = &chan_opt; start_reference.test_and_set(std::memory_order_release); start_reference.notify_all(); From 8086d86f5a1ebe1cb2b62df676d1036491a27d53 Mon Sep 17 00:00:00 2001 From: Raphael Steiner Date: Tue, 31 Mar 2026 07:08:26 +0200 Subject: [PATCH 2/2] corrected error messages --- benchmarks/RingBuffer.cpp | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/benchmarks/RingBuffer.cpp b/benchmarks/RingBuffer.cpp index 9f83392..c7259d4 100644 --- a/benchmarks/RingBuffer.cpp +++ b/benchmarks/RingBuffer.cpp @@ -114,9 +114,7 @@ static void BM_RingBuffer_2Threads_optional(benchmark::State &state) { if (rc != 0) { const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + std::to_string(rc) - + ".\nFailed to pin worker number " - + std::to_string(N) - + "'s thread to logical core " + + ".\nFailed to pin producer's thread to logical core " + std::to_string(logicalCore) + ".\n"; std::cerr << errorMessage; @@ -135,9 +133,7 @@ static void BM_RingBuffer_2Threads_optional(benchmark::State &state) { if (rc != 0) { const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + std::to_string(rc) - + ".\nFailed to pin worker number " - + std::to_string(N) - + "'s thread to logical core " + + ".\nFailed to pin consumer's thread to logical core " + std::to_string(logicalCore) + ".\n"; std::cerr << errorMessage; @@ -201,9 +197,7 @@ static void BM_RingBuffer_2Threads_reference(benchmark::State &state) { if (rc != 0) { const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + std::to_string(rc) - + ".\nFailed to pin worker number " - + std::to_string(N) - + "'s thread to logical core " + + ".\nFailed to pin producer's thread to logical core " + std::to_string(logicalCore) + ".\n"; std::cerr << errorMessage; @@ -222,9 +216,7 @@ static void BM_RingBuffer_2Threads_reference(benchmark::State &state) { if (rc != 0) { const std::string errorMessage = "Call to pthread_setaffinity_np returned error " + std::to_string(rc) - + ".\nFailed to pin worker number " - + std::to_string(N) - + "'s thread to logical core " + + ".\nFailed to pin consumer's thread to logical core " + std::to_string(logicalCore) + ".\n"; std::cerr << errorMessage;