diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c index bfa37f1..5c5d91d 100644 --- a/src/backend/storage/buffer/buf_init.c +++ b/src/backend/storage/buffer/buf_init.c @@ -73,6 +73,7 @@ InitBufferPool(void) foundDescs, foundIOLocks, foundBufCkpt; + LWLockPadded *content_locks; /* Align descriptors to a cacheline boundary. */ BufferDescriptors = (BufferDescPadded *) @@ -98,11 +99,12 @@ InitBufferPool(void) BufferIOLWLockTranche.array_stride = sizeof(LWLockMinimallyPadded); LWLockRegisterTranche(LWTRANCHE_BUFFER_IO_IN_PROGRESS, &BufferIOLWLockTranche); - +#if 1 + content_locks = (LWLockPadded *) ShmemAlloc(sizeof(LWLockPadded) * NBuffers); +#endif BufferContentLWLockTranche.name = "buffer_content"; - BufferContentLWLockTranche.array_base = - ((char *) BufferDescriptors) + offsetof(BufferDesc, content_lock); - BufferContentLWLockTranche.array_stride = sizeof(BufferDescPadded); + BufferContentLWLockTranche.array_base = content_locks; + BufferContentLWLockTranche.array_stride = sizeof(LWLockPadded); LWLockRegisterTranche(LWTRANCHE_BUFFER_CONTENT, &BufferContentLWLockTranche); @@ -149,9 +151,12 @@ InitBufferPool(void) * management of this list is done by freelist.c. */ buf->freeNext = i + 1; +// elog(LOG, "Ashu_before_init=%d", i); - LWLockInitialize(BufferDescriptorGetContentLock(buf), + LWLockInitialize(&content_locks[i].lock, LWTRANCHE_BUFFER_CONTENT); +// elog(LOG, "Ashu_after_init=%d", i); + buf->content_lock = &content_locks[i].lock; LWLockInitialize(BufferDescriptorGetIOLock(buf), LWTRANCHE_BUFFER_IO_IN_PROGRESS); @@ -205,6 +210,7 @@ BufferShmemSize(void) /* size of checkpoint sort array in bufmgr.c */ size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem))); + size = add_size(size, (sizeof(LWLockPadded) * NBuffers)); return size; } diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index 42aa2f9..0273bbc 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -778,7 +778,7 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, if (!isLocalBuf) { if (mode == RBM_ZERO_AND_LOCK) - LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), + LWLockAcquire(bufHdr->content_lock, LW_EXCLUSIVE); else if (mode == RBM_ZERO_AND_CLEANUP_LOCK) LockBufferForCleanup(BufferDescriptorGetBuffer(bufHdr)); @@ -927,7 +927,7 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, if ((mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK) && !isLocalBuf) { - LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_EXCLUSIVE); + LWLockAcquire(bufHdr->content_lock, LW_EXCLUSIVE); } if (isLocalBuf) @@ -1093,7 +1093,7 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, * happens to be trying to split the page the first one got from * StrategyGetBuffer.) */ - if (LWLockConditionalAcquire(BufferDescriptorGetContentLock(buf), + if (LWLockConditionalAcquire(buf->content_lock, LW_SHARED)) { /* @@ -1116,7 +1116,7 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, StrategyRejectBuffer(strategy, buf)) { /* Drop lock/pin and loop around for another buffer */ - LWLockRelease(BufferDescriptorGetContentLock(buf)); + LWLockRelease(buf->content_lock); UnpinBuffer(buf, true); continue; } @@ -1129,7 +1129,7 @@ BufferAlloc(SMgrRelation smgr, char relpersistence, ForkNumber forkNum, smgr->smgr_rnode.node.relNode); FlushBuffer(buf, NULL); - LWLockRelease(BufferDescriptorGetContentLock(buf)); + LWLockRelease(buf->content_lock); ScheduleBufferTagForWriteback(&BackendWritebackContext, &buf->tag); @@ -1447,7 +1447,7 @@ MarkBufferDirty(Buffer buffer) Assert(BufferIsPinned(buffer)); /* unfortunately we can't check if the lock is held exclusively */ - Assert(LWLockHeldByMe(BufferDescriptorGetContentLock(bufHdr))); + Assert(LWLockHeldByMe(bufHdr->content_lock)); LockBufHdr(bufHdr); @@ -1647,7 +1647,7 @@ UnpinBuffer(BufferDesc *buf, bool fixOwner) if (ref->refcount == 0) { /* I'd better not still hold any locks on the buffer */ - Assert(!LWLockHeldByMe(BufferDescriptorGetContentLock(buf))); + Assert(!LWLockHeldByMe(buf->content_lock)); Assert(!LWLockHeldByMe(BufferDescriptorGetIOLock(buf))); LockBufHdr(buf); @@ -2294,11 +2294,11 @@ SyncOneBuffer(int buf_id, bool skip_recently_used, WritebackContext *wb_context) * buffer is clean by the time we've locked it.) */ PinBuffer_Locked(bufHdr); - LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED); + LWLockAcquire(bufHdr->content_lock, LW_SHARED); FlushBuffer(bufHdr, NULL); - LWLockRelease(BufferDescriptorGetContentLock(bufHdr)); + LWLockRelease(bufHdr->content_lock); tag = bufHdr->tag; @@ -3109,9 +3109,9 @@ FlushRelationBuffers(Relation rel) (bufHdr->flags & BM_VALID) && (bufHdr->flags & BM_DIRTY)) { PinBuffer_Locked(bufHdr); - LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED); + LWLockAcquire(bufHdr->content_lock, LW_SHARED); FlushBuffer(bufHdr, rel->rd_smgr); - LWLockRelease(BufferDescriptorGetContentLock(bufHdr)); + LWLockRelease(bufHdr->content_lock); UnpinBuffer(bufHdr, true); } else @@ -3161,9 +3161,9 @@ FlushDatabaseBuffers(Oid dbid) (bufHdr->flags & BM_VALID) && (bufHdr->flags & BM_DIRTY)) { PinBuffer_Locked(bufHdr); - LWLockAcquire(BufferDescriptorGetContentLock(bufHdr), LW_SHARED); + LWLockAcquire(bufHdr->content_lock, LW_SHARED); FlushBuffer(bufHdr, NULL); - LWLockRelease(BufferDescriptorGetContentLock(bufHdr)); + LWLockRelease(bufHdr->content_lock); UnpinBuffer(bufHdr, true); } else @@ -3187,7 +3187,7 @@ FlushOneBuffer(Buffer buffer) bufHdr = GetBufferDescriptor(buffer - 1); - Assert(LWLockHeldByMe(BufferDescriptorGetContentLock(bufHdr))); + Assert(LWLockHeldByMe(bufHdr->content_lock)); FlushBuffer(bufHdr, NULL); } @@ -3284,7 +3284,7 @@ MarkBufferDirtyHint(Buffer buffer, bool buffer_std) Assert(GetPrivateRefCount(buffer) > 0); /* here, either share or exclusive lock is OK */ - Assert(LWLockHeldByMe(BufferDescriptorGetContentLock(bufHdr))); + Assert(LWLockHeldByMe(bufHdr->content_lock)); /* * This routine might get called many times on the same page, if we are @@ -3437,11 +3437,11 @@ LockBuffer(Buffer buffer, int mode) buf = GetBufferDescriptor(buffer - 1); if (mode == BUFFER_LOCK_UNLOCK) - LWLockRelease(BufferDescriptorGetContentLock(buf)); + LWLockRelease(buf->content_lock); else if (mode == BUFFER_LOCK_SHARE) - LWLockAcquire(BufferDescriptorGetContentLock(buf), LW_SHARED); + LWLockAcquire(buf->content_lock, LW_SHARED); else if (mode == BUFFER_LOCK_EXCLUSIVE) - LWLockAcquire(BufferDescriptorGetContentLock(buf), LW_EXCLUSIVE); + LWLockAcquire(buf->content_lock, LW_EXCLUSIVE); else elog(ERROR, "unrecognized buffer lock mode: %d", mode); } @@ -3462,7 +3462,7 @@ ConditionalLockBuffer(Buffer buffer) buf = GetBufferDescriptor(buffer - 1); - return LWLockConditionalAcquire(BufferDescriptorGetContentLock(buf), + return LWLockConditionalAcquire(buf->content_lock, LW_EXCLUSIVE); } diff --git a/src/include/storage/buf_internals.h b/src/include/storage/buf_internals.h index d04363b..e7d06f3 100644 --- a/src/include/storage/buf_internals.h +++ b/src/include/storage/buf_internals.h @@ -151,7 +151,7 @@ typedef struct BufferDesc int buf_id; /* buffer's index number (from 0) */ int freeNext; /* link in freelist chain */ - LWLock content_lock; /* to lock access to buffer contents */ + LWLock *content_lock; /* to lock access to buffer contents */ } BufferDesc; /* @@ -189,8 +189,6 @@ typedef union BufferDescPadded #define BufferDescriptorGetIOLock(bdesc) \ (&(BufferIOLWLockArray[(bdesc)->buf_id]).lock) -#define BufferDescriptorGetContentLock(bdesc) \ - ((LWLock*) (&(bdesc)->content_lock)) extern PGDLLIMPORT LWLockMinimallyPadded *BufferIOLWLockArray;