1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-16 15:02:33 +03:00

Refactor shared memory allocation for semaphores

Before commit e25626677f, spinlocks were implemented using semaphores
on some platforms (--disable-spinlocks). That made it necessary to
initialize semaphores early, before any spinlocks could be used. Now
that we don't support --disable-spinlocks anymore, we can allocate the
shared memory needed for semaphores the same way as other shared
memory structures. Since the semaphores are used only in the PGPROC
array, move the semaphore shmem size estimation and initialization
calls to ProcGlobalShmemSize() and InitProcGlobal().

Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAExHW5seSZpPx-znjidVZNzdagGHOk06F+Ds88MpPUbxd1kTaA@mail.gmail.com
This commit is contained in:
Heikki Linnakangas
2025-11-06 14:45:00 +02:00
parent daf3d99d2b
commit aa9c5fd3e3
7 changed files with 14 additions and 39 deletions

View File

@@ -215,12 +215,8 @@ PGReserveSemaphores(int maxSemas)
elog(PANIC, "out of memory");
#else
/*
* We must use ShmemAllocUnlocked(), since the spinlock protecting
* ShmemAlloc() won't be ready yet.
*/
sharedSemas = (PGSemaphore)
ShmemAllocUnlocked(PGSemaphoreShmemSize(maxSemas));
ShmemAlloc(PGSemaphoreShmemSize(maxSemas));
#endif
numSems = 0;

View File

@@ -343,12 +343,8 @@ PGReserveSemaphores(int maxSemas)
errmsg("could not stat data directory \"%s\": %m",
DataDir)));
/*
* We must use ShmemAllocUnlocked(), since the spinlock protecting
* ShmemAlloc() won't be ready yet.
*/
sharedSemas = (PGSemaphore)
ShmemAllocUnlocked(PGSemaphoreShmemSize(maxSemas));
ShmemAlloc(PGSemaphoreShmemSize(maxSemas));
numSharedSemas = 0;
maxSharedSemas = maxSemas;

View File

@@ -81,23 +81,12 @@ RequestAddinShmemSpace(Size size)
/*
* CalculateShmemSize
* Calculates the amount of shared memory and number of semaphores needed.
*
* If num_semaphores is not NULL, it will be set to the number of semaphores
* required.
* Calculates the amount of shared memory needed.
*/
Size
CalculateShmemSize(int *num_semaphores)
CalculateShmemSize(void)
{
Size size;
int numSemas;
/* Compute number of semaphores we'll need */
numSemas = ProcGlobalSemas();
/* Return the number of semaphores if requested by the caller */
if (num_semaphores)
*num_semaphores = numSemas;
/*
* Size of the Postgres shared-memory block is estimated via moderately-
@@ -109,7 +98,6 @@ CalculateShmemSize(int *num_semaphores)
* during the actual allocation phase.
*/
size = 100000;
size = add_size(size, PGSemaphoreShmemSize(numSemas));
size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
sizeof(ShmemIndexEnt)));
size = add_size(size, dsm_estimate_size());
@@ -204,12 +192,11 @@ CreateSharedMemoryAndSemaphores(void)
PGShmemHeader *shim;
PGShmemHeader *seghdr;
Size size;
int numSemas;
Assert(!IsUnderPostmaster);
/* Compute the size of the shared-memory block */
size = CalculateShmemSize(&numSemas);
size = CalculateShmemSize();
elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
/*
@@ -226,13 +213,6 @@ CreateSharedMemoryAndSemaphores(void)
InitShmemAccess(seghdr);
/*
* Create semaphores. (This is done here for historical reasons. We used
* to support emulating spinlocks with semaphores, which required
* initializing semaphores early.)
*/
PGReserveSemaphores(numSemas);
/*
* Set up shared memory allocation mechanism
*/
@@ -363,12 +343,11 @@ InitializeShmemGUCs(void)
Size size_b;
Size size_mb;
Size hp_size;
int num_semas;
/*
* Calculate the shared memory size and round up to the nearest megabyte.
*/
size_b = CalculateShmemSize(&num_semas);
size_b = CalculateShmemSize();
size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
sprintf(buf, "%zu", size_mb);
SetConfigOption("shared_memory_size", buf,
@@ -388,6 +367,6 @@ InitializeShmemGUCs(void)
PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
}
sprintf(buf, "%d", num_semas);
sprintf(buf, "%d", ProcGlobalSemas());
SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
}

View File

@@ -76,6 +76,7 @@
#include "utils/builtins.h"
static void *ShmemAllocRaw(Size size, Size *allocated_size);
static void *ShmemAllocUnlocked(Size size);
/* shared memory global variables */
@@ -234,7 +235,7 @@ ShmemAllocRaw(Size size, Size *allocated_size)
*
* We consider maxalign, rather than cachealign, sufficient here.
*/
void *
static void *
ShmemAllocUnlocked(Size size)
{
Size newStart;

View File

@@ -145,6 +145,7 @@ ProcGlobalShmemSize(void)
size = add_size(size, sizeof(PROC_HDR));
size = add_size(size, sizeof(slock_t));
size = add_size(size, PGSemaphoreShmemSize(ProcGlobalSemas()));
size = add_size(size, PGProcShmemSize());
size = add_size(size, FastPathLockShmemSize());
@@ -287,6 +288,9 @@ InitProcGlobal(void)
/* For asserts checking we did not overflow. */
fpEndPtr = fpPtr + requestSize;
/* Reserve space for semaphores. */
PGReserveSemaphores(ProcGlobalSemas());
for (i = 0; i < TotalProcs; i++)
{
PGPROC *proc = &procs[i];