Skip to content

Commit 46201d6

Browse files
committed
Pad semaphores to avoid false sharing.
In a USE_UNNAMED_SEMAPHORES build, the default on Linux and FreeBSD since commit ecb0d20, 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 cache lines. This matches what we do for similar hot arrays (see LWLockPadded, WALInsertLockPadded). Back-patch to 10, where unnamed semaphores were adopted as the default semaphore interface on those operating systems. Author: Thomas Munro Reviewed-by: Andres Freund Reported-by: Mithun Cy Tested-by: Mithun Cy, Tom Lane, Thomas Munro Discussion: https://postgr.es/m/CAD__OugYDM3O%2BdyZnnZSbJprSfsGFJcQ1R%3De59T3hcLmDug4_w%40mail.gmail.com
1 parent 360cbf1 commit 46201d6

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/backend/port/posix_sema.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@
4141
#error cannot use named POSIX semaphores with EXEC_BACKEND
4242
#endif
4343

44+
typedef union SemTPadded
45+
{
46+
sem_t pgsem;
47+
char pad[PG_CACHE_LINE_SIZE];
48+
} SemTPadded;
49+
4450
/* typedef PGSemaphore is equivalent to pointer to sem_t */
4551
typedef struct PGSemaphoreData
4652
{
47-
sem_t pgsem;
53+
SemTPadded sem_padded;
4854
} PGSemaphoreData;
4955

50-
#define PG_SEM_REF(x) (&(x)->pgsem)
56+
#define PG_SEM_REF(x) (&(x)->sem_padded.pgsem)
5157

5258
#define IPCProtection (0600) /* access/modify by user only */
5359

0 commit comments

Comments
 (0)