Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9f84280

Browse files
committed
Fix assorted defects in 09adc9a.
That commit increased all shared memory allocations to the next higher multiple of PG_CACHE_LINE_SIZE, but it didn't ensure that allocation started on a cache line boundary. It also failed to remove a couple other pieces of now-useless code. BUFFERALIGN() is perhaps obsolete at this point, and likely should be removed at some point, too, but that seems like it can be left to a future cleanup. Mistakes all pointed out by Andres Freund. The patch is mine, with a few extra assertions which I adopted from his version of this fix.
1 parent 7cb1db1 commit 9f84280

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/backend/storage/buffer/buf_init.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,19 @@ InitBufferPool(void)
7676

7777
/* Align descriptors to a cacheline boundary. */
7878
BufferDescriptors = (BufferDescPadded *)
79-
CACHELINEALIGN(
80-
ShmemInitStruct("Buffer Descriptors",
81-
NBuffers * sizeof(BufferDescPadded)
82-
+ PG_CACHE_LINE_SIZE,
83-
&foundDescs));
79+
ShmemInitStruct("Buffer Descriptors",
80+
NBuffers * sizeof(BufferDescPadded),
81+
&foundDescs);
8482

8583
BufferBlocks = (char *)
8684
ShmemInitStruct("Buffer Blocks",
8785
NBuffers * (Size) BLCKSZ, &foundBufs);
8886

8987
/* Align lwlocks to cacheline boundary */
9088
BufferIOLWLockArray = (LWLockMinimallyPadded *)
91-
CACHELINEALIGN(ShmemInitStruct("Buffer IO Locks",
92-
NBuffers * (Size) sizeof(LWLockMinimallyPadded)
93-
+ PG_CACHE_LINE_SIZE,
94-
&foundIOLocks));
89+
ShmemInitStruct("Buffer IO Locks",
90+
NBuffers * (Size) sizeof(LWLockMinimallyPadded),
91+
&foundIOLocks);
9592

9693
BufferIOLWLockTranche.name = "buffer_io";
9794
BufferIOLWLockTranche.array_base = BufferIOLWLockArray;

src/backend/storage/ipc/shmem.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ void
112112
InitShmemAllocation(void)
113113
{
114114
PGShmemHeader *shmhdr = ShmemSegHdr;
115+
char *aligned;
115116

116117
Assert(shmhdr != NULL);
117118

@@ -139,6 +140,11 @@ InitShmemAllocation(void)
139140
shmhdr->freeoffset += MAXALIGN(sizeof(slock_t));
140141
Assert(shmhdr->freeoffset <= shmhdr->totalsize);
141142

143+
/* Make sure the first allocation begins on a cache line boundary. */
144+
aligned = (char *)
145+
(CACHELINEALIGN((((char *) shmhdr) + shmhdr->freeoffset)));
146+
shmhdr->freeoffset = aligned - (char *) shmhdr;
147+
142148
SpinLockInit(ShmemLock);
143149

144150
/* ShmemIndex can't be set up yet (need LWLocks first) */
@@ -189,10 +195,6 @@ ShmemAlloc(Size size)
189195

190196
newStart = ShmemSegHdr->freeoffset;
191197

192-
/* extra alignment for large requests, since they are probably buffers */
193-
if (size >= BLCKSZ)
194-
newStart = BUFFERALIGN(newStart);
195-
196198
newFree = newStart + size;
197199
if (newFree <= ShmemSegHdr->totalsize)
198200
{
@@ -209,6 +211,8 @@ ShmemAlloc(Size size)
209211
(errcode(ERRCODE_OUT_OF_MEMORY),
210212
errmsg("out of shared memory")));
211213

214+
Assert(newSpace == (void *) CACHELINEALIGN(newSpace));
215+
212216
return newSpace;
213217
}
214218

@@ -425,6 +429,9 @@ ShmemInitStruct(const char *name, Size size, bool *foundPtr)
425429
LWLockRelease(ShmemIndexLock);
426430

427431
Assert(ShmemAddrIsValid(structPtr));
432+
433+
Assert(structPtr == (void *) CACHELINEALIGN(structPtr));
434+
428435
return structPtr;
429436
}
430437

0 commit comments

Comments
 (0)