PostgreSQL Source Code git master
shmem.h File Reference
#include "storage/spin.h"
#include "utils/hsearch.h"
Include dependency graph for shmem.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ShmemIndexEnt
 

Macros

#define SHMEM_INDEX_KEYSIZE   (48)
 
#define SHMEM_INDEX_SIZE   (64)
 

Functions

void InitShmemAccess (struct PGShmemHeader *seghdr)
 
void InitShmemAllocation (void)
 
void * ShmemAlloc (Size size)
 
void * ShmemAllocNoError (Size size)
 
void * ShmemAllocUnlocked (Size size)
 
bool ShmemAddrIsValid (const void *addr)
 
void InitShmemIndex (void)
 
HTABShmemInitHash (const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags)
 
void * ShmemInitStruct (const char *name, Size size, bool *foundPtr)
 
Size add_size (Size s1, Size s2)
 
Size mul_size (Size s1, Size s2)
 
void RequestAddinShmemSpace (Size size)
 

Variables

PGDLLIMPORT slock_t * ShmemLock
 

Macro Definition Documentation

◆ SHMEM_INDEX_KEYSIZE

#define SHMEM_INDEX_KEYSIZE   (48)

Definition at line 49 of file shmem.h.

◆ SHMEM_INDEX_SIZE

#define SHMEM_INDEX_SIZE   (64)

Definition at line 51 of file shmem.h.

Function Documentation

◆ add_size()

Size add_size ( Size  s1,
Size  s2 
)

Definition at line 488 of file shmem.c.

489{
490 Size result;
491
492 result = s1 + s2;
493 /* We are assuming Size is an unsigned type here... */
494 if (result < s1 || result < s2)
496 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
497 errmsg("requested shared memory size overflows size_t")));
498 return result;
499}
size_t Size
Definition: c.h:562
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
char * s1
char * s2

References ereport, errcode(), errmsg(), ERROR, s1, and s2.

Referenced by _brin_parallel_estimate_shared(), _bt_parallel_estimate_shared(), ApplyLauncherShmemSize(), AsyncShmemInit(), AsyncShmemSize(), AutoVacuumShmemSize(), BackendStatusShmemSize(), BackgroundWorkerShmemSize(), BTreeShmemSize(), BufferManagerShmemSize(), CalculateShmemSize(), CheckpointerShmemSize(), estimate_variable_size(), EstimateClientConnectionInfoSpace(), EstimateComboCIDStateSpace(), EstimateGUCStateSpace(), EstimateLibraryStateSpace(), EstimateParamExecSpace(), EstimateParamListSpace(), EstimateSnapshotSpace(), EstimateTransactionStateSpace(), ExecAggEstimate(), ExecAppendEstimate(), ExecBitmapHeapEstimate(), ExecBitmapHeapInitializeDSM(), ExecHashEstimate(), ExecIncrementalSortEstimate(), ExecMemoizeEstimate(), ExecSortEstimate(), expand_planner_arrays(), hash_estimate_size(), index_parallelscan_estimate(), index_parallelscan_initialize(), InitializeShmemGUCs(), InjectionPointShmemSize(), LockManagerShmemSize(), LWLockShmemSize(), MultiXactShmemSize(), PgArchShmemSize(), pgss_memsize(), PMSignalShmemSize(), PredicateLockShmemSize(), ProcArrayShmemInit(), ProcArrayShmemSize(), ProcGlobalShmemSize(), ProcSignalShmemSize(), ReplicationOriginShmemSize(), ReplicationSlotsShmemSize(), RequestAddinShmemSpace(), SerializeTransactionState(), SharedInvalShmemSize(), shm_toc_estimate(), StatsShmemSize(), StrategyShmemSize(), table_parallelscan_estimate(), tuplesort_estimate_shared(), TwoPhaseShmemSize(), WaitEventCustomShmemSize(), WalRcvShmemSize(), WalSndShmemSize(), and XLOGShmemSize().

◆ InitShmemAccess()

void InitShmemAccess ( struct PGShmemHeader seghdr)

Definition at line 97 of file shmem.c.

98{
99 ShmemSegHdr = seghdr;
100 ShmemBase = seghdr;
101 ShmemEnd = (char *) ShmemBase + seghdr->totalsize;
102}
static void * ShmemBase
Definition: shmem.c:83
static void * ShmemEnd
Definition: shmem.c:85
static PGShmemHeader * ShmemSegHdr
Definition: shmem.c:81
Size totalsize
Definition: pg_shmem.h:34

References ShmemBase, ShmemEnd, ShmemSegHdr, and PGShmemHeader::totalsize.

Referenced by CreateSharedMemoryAndSemaphores().

◆ InitShmemAllocation()

void InitShmemAllocation ( void  )

Definition at line 110 of file shmem.c.

111{
112 PGShmemHeader *shmhdr = ShmemSegHdr;
113 char *aligned;
114
115 Assert(shmhdr != NULL);
116
117 /*
118 * Initialize the spinlock used by ShmemAlloc. We must use
119 * ShmemAllocUnlocked, since obviously ShmemAlloc can't be called yet.
120 */
121 ShmemLock = (slock_t *) ShmemAllocUnlocked(sizeof(slock_t));
122
124
125 /*
126 * Allocations after this point should go through ShmemAlloc, which
127 * expects to allocate everything on cache line boundaries. Make sure the
128 * first allocation begins on a cache line boundary.
129 */
130 aligned = (char *)
131 (CACHELINEALIGN((((char *) shmhdr) + shmhdr->freeoffset)));
132 shmhdr->freeoffset = aligned - (char *) shmhdr;
133
134 /* ShmemIndex can't be set up yet (need LWLocks first) */
135 shmhdr->index = NULL;
136 ShmemIndex = (HTAB *) NULL;
137}
#define CACHELINEALIGN(LEN)
Definition: c.h:771
#define Assert(condition)
Definition: c.h:815
void * ShmemAllocUnlocked(Size size)
Definition: shmem.c:233
slock_t * ShmemLock
Definition: shmem.c:87
static HTAB * ShmemIndex
Definition: shmem.c:90
#define SpinLockInit(lock)
Definition: spin.h:57
Definition: dynahash.c:220

References Assert, CACHELINEALIGN, PGShmemHeader::freeoffset, PGShmemHeader::index, ShmemAllocUnlocked(), ShmemIndex, ShmemLock, ShmemSegHdr, and SpinLockInit.

Referenced by CreateSharedMemoryAndSemaphores().

◆ InitShmemIndex()

void InitShmemIndex ( void  )

Definition at line 278 of file shmem.c.

279{
280 HASHCTL info;
281
282 /*
283 * Create the shared memory shmem index.
284 *
285 * Since ShmemInitHash calls ShmemInitStruct, which expects the ShmemIndex
286 * hashtable to exist already, we have a bit of a circularity problem in
287 * initializing the ShmemIndex itself. The special "ShmemIndex" hash
288 * table name will tell ShmemInitStruct to fake it.
289 */
291 info.entrysize = sizeof(ShmemIndexEnt);
292
293 ShmemIndex = ShmemInitHash("ShmemIndex",
295 &info,
297}
#define HASH_STRINGS
Definition: hsearch.h:96
#define HASH_ELEM
Definition: hsearch.h:95
HTAB * ShmemInitHash(const char *name, long init_size, long max_size, HASHCTL *infoP, int hash_flags)
Definition: shmem.c:327
#define SHMEM_INDEX_SIZE
Definition: shmem.h:51
#define SHMEM_INDEX_KEYSIZE
Definition: shmem.h:49
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76

References HASHCTL::entrysize, HASH_ELEM, HASH_STRINGS, HASHCTL::keysize, SHMEM_INDEX_KEYSIZE, SHMEM_INDEX_SIZE, ShmemIndex, and ShmemInitHash().

Referenced by CreateOrAttachShmemStructs().

◆ mul_size()

Size mul_size ( Size  s1,
Size  s2 
)

Definition at line 505 of file shmem.c.

506{
507 Size result;
508
509 if (s1 == 0 || s2 == 0)
510 return 0;
511 result = s1 * s2;
512 /* We are assuming Size is an unsigned type here... */
513 if (result / s2 != s1)
515 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
516 errmsg("requested shared memory size overflows size_t")));
517 return result;
518}

References ereport, errcode(), errmsg(), ERROR, s1, and s2.

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), ApplyLauncherShmemSize(), AsyncShmemInit(), AsyncShmemSize(), AutoVacuumShmemSize(), BackendStatusShmemInit(), BackendStatusShmemSize(), BackgroundWorkerShmemSize(), BTreeShmemSize(), BufferManagerShmemSize(), CheckpointerShmemSize(), EstimateComboCIDStateSpace(), EstimatePendingSyncsSpace(), EstimateReindexStateSpace(), EstimateSnapshotSpace(), EstimateTransactionStateSpace(), ExecAggEstimate(), ExecBitmapHeapEstimate(), ExecBitmapHeapInitializeDSM(), ExecHashEstimate(), ExecIncrementalSortEstimate(), ExecInitParallelPlan(), ExecMemoizeEstimate(), ExecParallelRetrieveInstrumentation(), ExecParallelRetrieveJitInstrumentation(), ExecParallelSetupTupleQueues(), ExecSortEstimate(), hash_estimate_size(), InitializeParallelDSM(), LWLockShmemSize(), parallel_vacuum_init(), PGSemaphoreShmemSize(), PMSignalShmemSize(), PredicateLockShmemInit(), PredicateLockShmemSize(), ProcArrayShmemInit(), ProcArrayShmemSize(), ProcGlobalShmemSize(), ProcSignalShmemSize(), ReplicationOriginShmemSize(), ReplicationSlotsShmemSize(), SharedInvalShmemSize(), shm_toc_estimate(), tuplesort_estimate_shared(), TwoPhaseShmemSize(), WalSndShmemSize(), and XLOGShmemSize().

◆ RequestAddinShmemSpace()

void RequestAddinShmemSpace ( Size  size)

Definition at line 73 of file ipci.c.

74{
76 elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
78}
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:225
static Size total_addin_request
Definition: ipci.c:59
bool process_shmem_requests_in_progress
Definition: miscinit.c:1838
Size add_size(Size s1, Size s2)
Definition: shmem.c:488
static pg_noinline void Size size
Definition: slab.c:607

References add_size(), elog, FATAL, process_shmem_requests_in_progress, size, and total_addin_request.

Referenced by injection_shmem_request(), pgss_shmem_request(), and test_slru_shmem_request().

◆ ShmemAddrIsValid()

bool ShmemAddrIsValid ( const void *  addr)

Definition at line 269 of file shmem.c.

270{
271 return (addr >= ShmemBase) && (addr < ShmemEnd);
272}

References ShmemBase, and ShmemEnd.

Referenced by ReleasePredXact(), and ShmemInitStruct().

◆ ShmemAlloc()

void * ShmemAlloc ( Size  size)

Definition at line 147 of file shmem.c.

148{
149 void *newSpace;
150 Size allocated_size;
151
152 newSpace = ShmemAllocRaw(size, &allocated_size);
153 if (!newSpace)
155 (errcode(ERRCODE_OUT_OF_MEMORY),
156 errmsg("out of shared memory (%zu bytes requested)",
157 size)));
158 return newSpace;
159}
static void * ShmemAllocRaw(Size size, Size *allocated_size)
Definition: shmem.c:181

References ereport, errcode(), errmsg(), ERROR, ShmemAllocRaw(), and size.

Referenced by CreateLWLocks(), InitProcGlobal(), PredicateLockShmemInit(), ShmemInitStruct(), and StatsShmemInit().

◆ ShmemAllocNoError()

void * ShmemAllocNoError ( Size  size)

Definition at line 167 of file shmem.c.

168{
169 Size allocated_size;
170
171 return ShmemAllocRaw(size, &allocated_size);
172}

References ShmemAllocRaw(), and size.

Referenced by ShmemInitHash().

◆ ShmemAllocUnlocked()

void * ShmemAllocUnlocked ( Size  size)

Definition at line 233 of file shmem.c.

234{
235 Size newStart;
236 Size newFree;
237 void *newSpace;
238
239 /*
240 * Ensure allocated space is adequately aligned.
241 */
242 size = MAXALIGN(size);
243
244 Assert(ShmemSegHdr != NULL);
245
246 newStart = ShmemSegHdr->freeoffset;
247
248 newFree = newStart + size;
249 if (newFree > ShmemSegHdr->totalsize)
251 (errcode(ERRCODE_OUT_OF_MEMORY),
252 errmsg("out of shared memory (%zu bytes requested)",
253 size)));
254 ShmemSegHdr->freeoffset = newFree;
255
256 newSpace = (char *) ShmemBase + newStart;
257
258 Assert(newSpace == (void *) MAXALIGN(newSpace));
259
260 return newSpace;
261}
#define MAXALIGN(LEN)
Definition: c.h:768
Size freeoffset
Definition: pg_shmem.h:35

References Assert, ereport, errcode(), errmsg(), ERROR, PGShmemHeader::freeoffset, MAXALIGN, ShmemBase, ShmemSegHdr, size, and PGShmemHeader::totalsize.

Referenced by InitShmemAllocation(), and PGReserveSemaphores().

◆ ShmemInitHash()

HTAB * ShmemInitHash ( const char *  name,
long  init_size,
long  max_size,
HASHCTL infoP,
int  hash_flags 
)

Definition at line 327 of file shmem.c.

332{
333 bool found;
334 void *location;
335
336 /*
337 * Hash tables allocated in shared memory have a fixed directory; it can't
338 * grow or other backends wouldn't be able to find it. So, make sure we
339 * make it big enough to start with.
340 *
341 * The shared memory allocator must be specified too.
342 */
343 infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size);
344 infoP->alloc = ShmemAllocNoError;
345 hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE;
346
347 /* look it up in the shmem index */
348 location = ShmemInitStruct(name,
349 hash_get_shared_size(infoP, hash_flags),
350 &found);
351
352 /*
353 * if it already exists, attach to it rather than allocate and initialize
354 * new space
355 */
356 if (found)
357 hash_flags |= HASH_ATTACH;
358
359 /* Pass location of hashtable header to hash_create */
360 infoP->hctl = (HASHHDR *) location;
361
362 return hash_create(name, init_size, infoP, hash_flags);
363}
Size hash_get_shared_size(HASHCTL *info, int flags)
Definition: dynahash.c:854
long hash_select_dirsize(long num_entries)
Definition: dynahash.c:830
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_ALLOC
Definition: hsearch.h:101
#define HASH_DIRSIZE
Definition: hsearch.h:94
#define HASH_ATTACH
Definition: hsearch.h:104
#define HASH_SHARED_MEM
Definition: hsearch.h:103
void * ShmemAllocNoError(Size size)
Definition: shmem.c:167
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382
HashAllocFunc alloc
Definition: hsearch.h:84
long dsize
Definition: hsearch.h:72
HASHHDR * hctl
Definition: hsearch.h:88
long max_dsize
Definition: hsearch.h:73
const char * name

References HASHCTL::alloc, HASHCTL::dsize, HASH_ALLOC, HASH_ATTACH, hash_create(), HASH_DIRSIZE, hash_get_shared_size(), hash_select_dirsize(), HASH_SHARED_MEM, HASHCTL::hctl, HASHCTL::max_dsize, name, ShmemAllocNoError(), and ShmemInitStruct().

Referenced by InitBufTable(), InitShmemIndex(), LockManagerShmemInit(), pgss_shmem_startup(), PredicateLockShmemInit(), and WaitEventCustomShmemInit().

◆ ShmemInitStruct()

void * ShmemInitStruct ( const char *  name,
Size  size,
bool *  foundPtr 
)

Definition at line 382 of file shmem.c.

383{
384 ShmemIndexEnt *result;
385 void *structPtr;
386
387 LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE);
388
389 if (!ShmemIndex)
390 {
391 PGShmemHeader *shmemseghdr = ShmemSegHdr;
392
393 /* Must be trying to create/attach to ShmemIndex itself */
394 Assert(strcmp(name, "ShmemIndex") == 0);
395
397 {
398 /* Must be initializing a (non-standalone) backend */
399 Assert(shmemseghdr->index != NULL);
400 structPtr = shmemseghdr->index;
401 *foundPtr = true;
402 }
403 else
404 {
405 /*
406 * If the shmem index doesn't exist, we are bootstrapping: we must
407 * be trying to init the shmem index itself.
408 *
409 * Notice that the ShmemIndexLock is released before the shmem
410 * index has been initialized. This should be OK because no other
411 * process can be accessing shared memory yet.
412 */
413 Assert(shmemseghdr->index == NULL);
414 structPtr = ShmemAlloc(size);
415 shmemseghdr->index = structPtr;
416 *foundPtr = false;
417 }
418 LWLockRelease(ShmemIndexLock);
419 return structPtr;
420 }
421
422 /* look it up in the shmem index */
423 result = (ShmemIndexEnt *)
425
426 if (!result)
427 {
428 LWLockRelease(ShmemIndexLock);
430 (errcode(ERRCODE_OUT_OF_MEMORY),
431 errmsg("could not create ShmemIndex entry for data structure \"%s\"",
432 name)));
433 }
434
435 if (*foundPtr)
436 {
437 /*
438 * Structure is in the shmem index so someone else has allocated it
439 * already. The size better be the same as the size we are trying to
440 * initialize to, or there is a name conflict (or worse).
441 */
442 if (result->size != size)
443 {
444 LWLockRelease(ShmemIndexLock);
446 (errmsg("ShmemIndex entry size is wrong for data structure"
447 " \"%s\": expected %zu, actual %zu",
448 name, size, result->size)));
449 }
450 structPtr = result->location;
451 }
452 else
453 {
454 Size allocated_size;
455
456 /* It isn't in the table yet. allocate and initialize it */
457 structPtr = ShmemAllocRaw(size, &allocated_size);
458 if (structPtr == NULL)
459 {
460 /* out of memory; remove the failed ShmemIndex entry */
462 LWLockRelease(ShmemIndexLock);
464 (errcode(ERRCODE_OUT_OF_MEMORY),
465 errmsg("not enough shared memory for data structure"
466 " \"%s\" (%zu bytes requested)",
467 name, size)));
468 }
469 result->size = size;
470 result->allocated_size = allocated_size;
471 result->location = structPtr;
472 }
473
474 LWLockRelease(ShmemIndexLock);
475
476 Assert(ShmemAddrIsValid(structPtr));
477
478 Assert(structPtr == (void *) CACHELINEALIGN(structPtr));
479
480 return structPtr;
481}
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
bool IsUnderPostmaster
Definition: globals.c:119
@ HASH_REMOVE
Definition: hsearch.h:115
@ HASH_ENTER_NULL
Definition: hsearch.h:116
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LW_EXCLUSIVE
Definition: lwlock.h:114
bool ShmemAddrIsValid(const void *addr)
Definition: shmem.c:269
void * ShmemAlloc(Size size)
Definition: shmem.c:147
void * index
Definition: pg_shmem.h:37
void * location
Definition: shmem.h:57
Size size
Definition: shmem.h:58
Size allocated_size
Definition: shmem.h:59

References ShmemIndexEnt::allocated_size, Assert, CACHELINEALIGN, ereport, errcode(), errmsg(), ERROR, HASH_ENTER_NULL, HASH_REMOVE, hash_search(), PGShmemHeader::index, IsUnderPostmaster, ShmemIndexEnt::location, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), name, ShmemAddrIsValid(), ShmemAlloc(), ShmemAllocRaw(), ShmemIndex, ShmemSegHdr, size, and ShmemIndexEnt::size.

Referenced by ApplyLauncherShmemInit(), AsyncShmemInit(), AutoVacuumShmemInit(), BackendStatusShmemInit(), BackgroundWorkerShmemInit(), BTreeShmemInit(), BufferManagerShmemInit(), CheckpointerShmemInit(), CommitTsShmemInit(), dsm_shmem_init(), DSMRegistryShmemInit(), InitProcGlobal(), injection_shmem_startup(), InjectionPointShmemInit(), LockManagerShmemInit(), MultiXactShmemInit(), PgArchShmemInit(), pgss_shmem_startup(), PMSignalShmemInit(), PredicateLockShmemInit(), ProcArrayShmemInit(), ProcSignalShmemInit(), ReplicationOriginShmemInit(), ReplicationSlotsShmemInit(), SerialInit(), SharedInvalShmemInit(), ShmemInitHash(), SimpleLruInit(), SlotSyncShmemInit(), StatsShmemInit(), StrategyInitialize(), SyncScanShmemInit(), TwoPhaseShmemInit(), VarsupShmemInit(), WaitEventCustomShmemInit(), WalRcvShmemInit(), WalSndShmemInit(), WalSummarizerShmemInit(), XLogPrefetchShmemInit(), XLogRecoveryShmemInit(), and XLOGShmemInit().

Variable Documentation

◆ ShmemLock

PGDLLIMPORT slock_t* ShmemLock
extern

Definition at line 87 of file shmem.c.

Referenced by InitShmemAllocation(), LWLockNewTrancheId(), and ShmemAllocRaw().