Skip to content

Commit 375d852

Browse files
committed
Keep heavily-contended fields in XLogCtlInsert on different cache lines.
Performance testing shows that if the insertpos_lck spinlock and the fields that it protects are on the same cache line with other variables that are frequently accessed, the false sharing can hurt performance a lot. Keep them apart by adding some padding.
1 parent cc52d5b commit 375d852

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/backend/access/transam/xlog.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ typedef struct
408408
typedef union XLogInsertSlotPadded
409409
{
410410
XLogInsertSlot slot;
411-
char pad[64];
411+
char pad[CACHE_LINE_SIZE];
412412
} XLogInsertSlotPadded;
413413

414414
/*
@@ -428,8 +428,14 @@ typedef struct XLogCtlInsert
428428
uint64 CurrBytePos;
429429
uint64 PrevBytePos;
430430

431-
/* insertion slots, see above for details */
432-
XLogInsertSlotPadded *insertSlots;
431+
/*
432+
* Make sure the above heavily-contended spinlock and byte positions are
433+
* on their own cache line. In particular, the RedoRecPtr and full page
434+
* write variables below should be on a different cache line. They are
435+
* read on every WAL insertion, but updated rarely, and we don't want
436+
* those reads to steal the cache line containing Curr/PrevBytePos.
437+
*/
438+
char pad[CACHE_LINE_SIZE];
433439

434440
/*
435441
* fullPageWrites is the master copy used by all backends to determine
@@ -455,6 +461,9 @@ typedef struct XLogCtlInsert
455461
bool exclusiveBackup;
456462
int nonExclusiveBackups;
457463
XLogRecPtr lastBackupStart;
464+
465+
/* insertion slots, see XLogInsertSlot struct above for details */
466+
XLogInsertSlotPadded *insertSlots;
458467
} XLogCtlInsert;
459468

460469
/*

src/include/pg_config_manual.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@
199199
#define USE_PPC_LWSYNC
200200
#endif
201201

202+
/*
203+
* Assumed cache line size. This doesn't affect correctness, but can be
204+
* used for low-level optimizations. Currently, this is only used to pad
205+
* some data structures in xlog.c, to ensure that highly-contended fields
206+
* are on different cache lines. Too small a value can hurt performance due
207+
* to false sharing, while the only downside of too large a value is a few
208+
* bytes of wasted memory. The default is 128, which should be large enough
209+
* for all supported platforms.
210+
*/
211+
#define CACHE_LINE_SIZE 128
212+
202213
/*
203214
*------------------------------------------------------------------------
204215
* The following symbols are for enabling debugging code, not for

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy