From 82756e109a0d38a590402881a6094c8bef7f5ece Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Mon, 23 Jul 2018 20:49:45 +1200 Subject: [PATCH] Pad semaphores to avoid false sharing. In a USE_UNNAMED_SEMAPHORES build, the default on Linux and FreeBSD since commit ecb0d20a, we have an array of sem_t objects. This turned out to reduce performance compared to the previous default USE_SYSV_SEMAPHORES on an 8 socket system. Testing showed that the lost performance could be regained by padding the array elements so that they have their own cachelines. This matches what we do for similar hot arrays (see LWLockPadded, WALInsertLockPadded). Author: Thomas Munro Reviewed-by: Andres Freund Reported-by: Mithun Cy Tested-by: Mithun Cy, Tom Lane Discussion: https://postgr.es/m/CAD__OugYDM3O%2BdyZnnZSbJprSfsGFJcQ1R%3De59T3hcLmDug4_w%40mail.gmail.com --- src/backend/port/posix_sema.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/backend/port/posix_sema.c b/src/backend/port/posix_sema.c index a2cabe58fc..5174550794 100644 --- a/src/backend/port/posix_sema.c +++ b/src/backend/port/posix_sema.c @@ -41,13 +41,19 @@ #error cannot use named POSIX semaphores with EXEC_BACKEND #endif +typedef union SemTPadded +{ + sem_t pgsem; + char pad[PG_CACHE_LINE_SIZE]; +} SemTPadded; + /* typedef PGSemaphore is equivalent to pointer to sem_t */ typedef struct PGSemaphoreData { - sem_t pgsem; + SemTPadded sem_padded; } PGSemaphoreData; -#define PG_SEM_REF(x) (&(x)->pgsem) +#define PG_SEM_REF(x) (&(x)->sem_padded.pgsem) #define IPCProtection (0600) /* access/modify by user only */ -- 2.17.0