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)
 

Typedefs

typedef struct PGShmemHeader PGShmemHeader
 

Functions

void InitShmemAccess (PGShmemHeader *seghdr)
 
void InitShmemAllocation (void)
 
void * ShmemAlloc (Size size)
 
void * ShmemAllocNoError (Size size)
 
bool ShmemAddrIsValid (const void *addr)
 
void InitShmemIndex (void)
 
HTABShmemInitHash (const char *name, int64 init_size, int64 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)
 
PGDLLIMPORT Size pg_get_shmem_pagesize (void)
 
void RequestAddinShmemSpace (Size size)
 

Variables

PGDLLIMPORT slock_t * ShmemLock
 

Macro Definition Documentation

◆ SHMEM_INDEX_KEYSIZE

#define SHMEM_INDEX_KEYSIZE   (48)

Definition at line 51 of file shmem.h.

◆ SHMEM_INDEX_SIZE

#define SHMEM_INDEX_SIZE   (64)

Definition at line 53 of file shmem.h.

Typedef Documentation

◆ PGShmemHeader

typedef struct PGShmemHeader PGShmemHeader

Definition at line 30 of file shmem.h.

Function Documentation

◆ add_size()

Size add_size ( Size  s1,
Size  s2 
)

Definition at line 495 of file shmem.c.

496{
497 Size result;
498
499 if (pg_add_size_overflow(s1, s2, &result))
501 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
502 errmsg("requested shared memory size overflows size_t")));
503 return result;
504}
size_t Size
Definition: c.h:615
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
static bool pg_add_size_overflow(size_t a, size_t b, size_t *result)
Definition: int.h:608
char * s1
char * s2

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

Referenced by _brin_parallel_estimate_shared(), _bt_parallel_estimate_shared(), _gin_parallel_estimate_shared(), AioShmemSize(), ApplyLauncherShmemSize(), AsyncShmemInit(), AsyncShmemSize(), AutoVacuumShmemSize(), BackendStatusShmemSize(), BackgroundWorkerShmemSize(), btestimateparallelscan(), 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(), FastPathLockShmemSize(), hash_estimate_size(), index_parallelscan_estimate(), index_parallelscan_initialize(), InitializeShmemGUCs(), InjectionPointShmemSize(), LockManagerShmemSize(), LWLockShmemSize(), MultiXactShmemSize(), pgaio_worker_shmem_size(), PgArchShmemSize(), PGProcShmemSize(), pgss_memsize(), PMSignalShmemSize(), PredicateLockShmemInit(), PredicateLockShmemSize(), ProcArrayShmemInit(), ProcArrayShmemSize(), ProcGlobalShmemSize(), ProcSignalShmemSize(), ReplicationOriginShmemSize(), ReplicationSlotsShmemSize(), RequestAddinShmemSpace(), SerializeTransactionState(), SharedInvalShmemSize(), shm_toc_estimate(), StatsShmemSize(), StrategyShmemSize(), table_parallelscan_estimate(), tuplesort_estimate_shared(), TwoPhaseShmemSize(), WaitEventCustomShmemSize(), WaitLSNShmemSize(), WalRcvShmemSize(), WalSndShmemSize(), and XLOGShmemSize().

◆ InitShmemAccess()

void InitShmemAccess ( PGShmemHeader seghdr)

Definition at line 104 of file shmem.c.

105{
106 ShmemSegHdr = seghdr;
107 ShmemBase = seghdr;
108 ShmemEnd = (char *) ShmemBase + seghdr->totalsize;
109}
static void * ShmemBase
Definition: shmem.c:86
static void * ShmemEnd
Definition: shmem.c:88
static PGShmemHeader * ShmemSegHdr
Definition: shmem.c:84
Size totalsize
Definition: pg_shmem.h:34

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ InitShmemAllocation()

void InitShmemAllocation ( void  )

Definition at line 117 of file shmem.c.

118{
119 PGShmemHeader *shmhdr = ShmemSegHdr;
120 char *aligned;
121
122 Assert(shmhdr != NULL);
123
124 /*
125 * Initialize the spinlock used by ShmemAlloc. We must use
126 * ShmemAllocUnlocked, since obviously ShmemAlloc can't be called yet.
127 */
128 ShmemLock = (slock_t *) ShmemAllocUnlocked(sizeof(slock_t));
129
131
132 /*
133 * Allocations after this point should go through ShmemAlloc, which
134 * expects to allocate everything on cache line boundaries. Make sure the
135 * first allocation begins on a cache line boundary.
136 */
137 aligned = (char *)
138 (CACHELINEALIGN((((char *) shmhdr) + shmhdr->freeoffset)));
139 shmhdr->freeoffset = aligned - (char *) shmhdr;
140
141 /* ShmemIndex can't be set up yet (need LWLocks first) */
142 shmhdr->index = NULL;
143 ShmemIndex = (HTAB *) NULL;
144}
#define CACHELINEALIGN(LEN)
Definition: c.h:818
Assert(PointerIsAligned(start, uint64))
slock_t * ShmemLock
Definition: shmem.c:90
static HTAB * ShmemIndex
Definition: shmem.c:93
static void * ShmemAllocUnlocked(Size size)
Definition: shmem.c:240
#define SpinLockInit(lock)
Definition: spin.h:57
Definition: dynahash.c:222

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ InitShmemIndex()

void InitShmemIndex ( void  )

Definition at line 285 of file shmem.c.

286{
287 HASHCTL info;
288
289 /*
290 * Create the shared memory shmem index.
291 *
292 * Since ShmemInitHash calls ShmemInitStruct, which expects the ShmemIndex
293 * hashtable to exist already, we have a bit of a circularity problem in
294 * initializing the ShmemIndex itself. The special "ShmemIndex" hash
295 * table name will tell ShmemInitStruct to fake it.
296 */
298 info.entrysize = sizeof(ShmemIndexEnt);
299
300 ShmemIndex = ShmemInitHash("ShmemIndex",
302 &info,
304}
#define HASH_STRINGS
Definition: hsearch.h:96
#define HASH_ELEM
Definition: hsearch.h:95
HTAB * ShmemInitHash(const char *name, int64 init_size, int64 max_size, HASHCTL *infoP, int hash_flags)
Definition: shmem.c:334
#define SHMEM_INDEX_SIZE
Definition: shmem.h:53
#define SHMEM_INDEX_KEYSIZE
Definition: shmem.h:51
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 510 of file shmem.c.

511{
512 Size result;
513
514 if (pg_mul_size_overflow(s1, s2, &result))
516 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
517 errmsg("requested shared memory size overflows size_t")));
518 return result;
519}
static bool pg_mul_size_overflow(size_t a, size_t b, size_t *result)
Definition: int.h:642

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

Referenced by _brin_begin_parallel(), _bt_begin_parallel(), _gin_begin_parallel(), AioBackendShmemSize(), AioHandleDataShmemSize(), AioHandleIOVShmemSize(), AioHandleShmemSize(), 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(), FastPathLockShmemSize(), hash_estimate_size(), InitializeParallelDSM(), LWLockShmemSize(), parallel_vacuum_init(), PGProcShmemSize(), PGSemaphoreShmemSize(), PMSignalShmemSize(), PredicateLockShmemInit(), PredicateLockShmemSize(), ProcArrayShmemInit(), ProcArrayShmemSize(), ProcSignalShmemSize(), ReplicationOriginShmemSize(), ReplicationSlotsShmemSize(), SharedInvalShmemSize(), shm_toc_estimate(), tuplesort_estimate_shared(), TwoPhaseShmemSize(), WaitLSNShmemSize(), WalSndShmemSize(), and XLOGShmemSize().

◆ pg_get_shmem_pagesize()

PGDLLIMPORT Size pg_get_shmem_pagesize ( void  )

Definition at line 733 of file shmem.c.

734{
735 Size os_page_size;
736#ifdef WIN32
737 SYSTEM_INFO sysinfo;
738
739 GetSystemInfo(&sysinfo);
740 os_page_size = sysinfo.dwPageSize;
741#else
742 os_page_size = sysconf(_SC_PAGESIZE);
743#endif
744
747
749 GetHugePageSize(&os_page_size, NULL);
750
751 return os_page_size;
752}
bool IsUnderPostmaster
Definition: globals.c:120
int huge_pages_status
Definition: guc_tables.c:582
@ HUGE_PAGES_UNKNOWN
Definition: pg_shmem.h:56
@ HUGE_PAGES_ON
Definition: pg_shmem.h:54
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:479

References Assert(), GetHugePageSize(), HUGE_PAGES_ON, huge_pages_status, HUGE_PAGES_UNKNOWN, and IsUnderPostmaster.

Referenced by pg_buffercache_os_pages_internal(), and pg_get_shmem_allocations_numa().

◆ RequestAddinShmemSpace()

void RequestAddinShmemSpace ( Size  size)

Definition at line 75 of file ipci.c.

76{
78 elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
80}
#define FATAL
Definition: elog.h:41
#define elog(elevel,...)
Definition: elog.h:226
static Size total_addin_request
Definition: ipci.c:61
bool process_shmem_requests_in_progress
Definition: miscinit.c:1790
Size add_size(Size s1, Size s2)
Definition: shmem.c:495

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

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

◆ ShmemAddrIsValid()

bool ShmemAddrIsValid ( const void *  addr)

Definition at line 276 of file shmem.c.

277{
278 return (addr >= ShmemBase) && (addr < ShmemEnd);
279}

References ShmemBase, and ShmemEnd.

Referenced by ReleasePredXact(), and ShmemInitStruct().

◆ ShmemAlloc()

void * ShmemAlloc ( Size  size)

Definition at line 154 of file shmem.c.

155{
156 void *newSpace;
157 Size allocated_size;
158
159 newSpace = ShmemAllocRaw(size, &allocated_size);
160 if (!newSpace)
162 (errcode(ERRCODE_OUT_OF_MEMORY),
163 errmsg("out of shared memory (%zu bytes requested)",
164 size)));
165 return newSpace;
166}
static void * ShmemAllocRaw(Size size, Size *allocated_size)
Definition: shmem.c:188

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

Referenced by CreateLWLocks(), PGReserveSemaphores(), ShmemInitStruct(), and StatsShmemInit().

◆ ShmemAllocNoError()

void * ShmemAllocNoError ( Size  size)

Definition at line 174 of file shmem.c.

175{
176 Size allocated_size;
177
178 return ShmemAllocRaw(size, &allocated_size);
179}

References ShmemAllocRaw().

Referenced by ShmemInitHash().

◆ ShmemInitHash()

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

Definition at line 334 of file shmem.c.

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

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

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, and ShmemIndexEnt::size.

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

Variable Documentation

◆ ShmemLock

PGDLLIMPORT slock_t* ShmemLock
extern

Definition at line 90 of file shmem.c.

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