From 7341add1113ebfed5f160f664bc374165586229c Mon Sep 17 00:00:00 2001 From: Rahila Syed Date: Sat, 1 Mar 2025 09:02:33 +0530 Subject: [PATCH 2/2] Replace ShmemAlloc calls by ShmemInitStruct The shared memory allocated by ShmemAlloc is not tracked by pg_shmem_allocations. This commit replaces most of the calls to ShmemAlloc by ShmemInitStruct to associate a name with the allocations and ensure that they get tracked by pg_shmem_allocations --- src/backend/storage/lmgr/lwlock.c | 1 + src/backend/storage/lmgr/predicate.c | 10 +++++----- src/backend/storage/lmgr/proc.c | 8 ++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c index 8adf273027..9101673185 100644 --- a/src/backend/storage/lmgr/lwlock.c +++ b/src/backend/storage/lmgr/lwlock.c @@ -464,6 +464,7 @@ CreateLWLocks(void) Size spaceLocks = LWLockShmemSize(); int *LWLockCounter; char *ptr; + bool found; /* Allocate space */ ptr = (char *) ShmemAlloc(spaceLocks); diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c index 5b21a05398..58afb94d96 100644 --- a/src/backend/storage/lmgr/predicate.c +++ b/src/backend/storage/lmgr/predicate.c @@ -1244,7 +1244,7 @@ PredicateLockShmemInit(void) PredXact->HavePartialClearedThrough = 0; requestSize = mul_size((Size) max_table_size, sizeof(SERIALIZABLEXACT)); - PredXact->element = ShmemAlloc(requestSize); + PredXact->element = ShmemInitStruct("SerializableXactList", requestSize, &found); /* Add all elements to available list, clean. */ memset(PredXact->element, 0, requestSize); for (i = 0; i < max_table_size; i++) @@ -1299,9 +1299,11 @@ PredicateLockShmemInit(void) * probably OK. */ max_table_size *= 5; + requestSize = mul_size((Size) max_table_size, + RWConflictDataSize); RWConflictPool = ShmemInitStruct("RWConflictPool", - RWConflictPoolHeaderDataSize, + RWConflictPoolHeaderDataSize + requestSize, &found); Assert(found == IsUnderPostmaster); if (!found) @@ -1309,9 +1311,7 @@ PredicateLockShmemInit(void) int i; dlist_init(&RWConflictPool->availableList); - requestSize = mul_size((Size) max_table_size, - RWConflictDataSize); - RWConflictPool->element = ShmemAlloc(requestSize); + RWConflictPool->element = (RWConflict) ((char *)RWConflictPool + RWConflictPoolHeaderDataSize); /* Add all elements to available list, clean. */ memset(RWConflictPool->element, 0, requestSize); for (i = 0; i < max_table_size; i++) diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index 49204f91a2..e112735d93 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -218,11 +218,11 @@ InitProcGlobal(void) * how hotly they are accessed. */ ProcGlobal->xids = - (TransactionId *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->xids)); + (TransactionId *) ShmemInitStruct("Proc Transaction Ids", TotalProcs * sizeof(*ProcGlobal->xids), &found); MemSet(ProcGlobal->xids, 0, TotalProcs * sizeof(*ProcGlobal->xids)); - ProcGlobal->subxidStates = (XidCacheStatus *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->subxidStates)); + ProcGlobal->subxidStates = (XidCacheStatus *) ShmemInitStruct("Proc Sub-transaction id states", TotalProcs * sizeof(*ProcGlobal->subxidStates), &found); MemSet(ProcGlobal->subxidStates, 0, TotalProcs * sizeof(*ProcGlobal->subxidStates)); - ProcGlobal->statusFlags = (uint8 *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->statusFlags)); + ProcGlobal->statusFlags = (uint8 *) ShmemInitStruct("Proc Status Flags", TotalProcs * sizeof(*ProcGlobal->statusFlags), &found); MemSet(ProcGlobal->statusFlags, 0, TotalProcs * sizeof(*ProcGlobal->statusFlags)); /* @@ -233,7 +233,7 @@ InitProcGlobal(void) fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64)); fpRelIdSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(Oid) * FP_LOCK_SLOTS_PER_GROUP); - fpPtr = ShmemAlloc(TotalProcs * (fpLockBitsSize + fpRelIdSize)); + fpPtr = ShmemInitStruct("Fast path lock arrays", TotalProcs * (fpLockBitsSize + fpRelIdSize), &found); MemSet(fpPtr, 0, TotalProcs * (fpLockBitsSize + fpRelIdSize)); /* For asserts checking we did not overflow. */ -- 2.34.1