PostgreSQL Source Code  git master
ipci.c File Reference
Include dependency graph for ipci.c:

Go to the source code of this file.

Functions

static void CreateOrAttachShmemStructs (void)
 
void RequestAddinShmemSpace (Size size)
 
Size CalculateShmemSize (int *num_semaphores)
 
void CreateSharedMemoryAndSemaphores (void)
 
void InitializeShmemGUCs (void)
 

Variables

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE
 
shmem_startup_hook_type shmem_startup_hook = NULL
 
static Size total_addin_request = 0
 

Function Documentation

◆ CalculateShmemSize()

Size CalculateShmemSize ( int *  num_semaphores)

Definition at line 90 of file ipci.c.

91 {
92  Size size;
93  int numSemas;
94 
95  /* Compute number of semaphores we'll need */
96  numSemas = ProcGlobalSemas();
97  numSemas += SpinlockSemas();
98 
99  /* Return the number of semaphores if requested by the caller */
100  if (num_semaphores)
101  *num_semaphores = numSemas;
102 
103  /*
104  * Size of the Postgres shared-memory block is estimated via moderately-
105  * accurate estimates for the big hogs, plus 100K for the stuff that's too
106  * small to bother with estimating.
107  *
108  * We take some care to ensure that the total size request doesn't
109  * overflow size_t. If this gets through, we don't need to be so careful
110  * during the actual allocation phase.
111  */
112  size = 100000;
113  size = add_size(size, PGSemaphoreShmemSize(numSemas));
116  sizeof(ShmemIndexEnt)));
155 #ifdef EXEC_BACKEND
156  size = add_size(size, ShmemBackendArraySize());
157 #endif
158 
159  /* include additional requested shmem from preload libraries */
161 
162  /* might as well round it off to a multiple of a typical page size */
163  size = add_size(size, 8192 - (size % 8192));
164 
165  return size;
166 }
Size AsyncShmemSize(void)
Definition: async.c:485
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3256
Size BackendStatusShmemSize(void)
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:146
Size BufferShmemSize(void)
Definition: buf_init.c:160
size_t Size
Definition: c.h:605
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:879
Size CLOGShmemSize(void)
Definition: clog.c:781
Size CommitTsShmemSize(void)
Definition: commit_ts.c:519
size_t dsm_estimate_size(void)
Definition: dsm.c:470
Size DSMRegistryShmemSize(void)
Definition: dsm_registry.c:63
Size hash_estimate_size(long num_entries, Size entrysize)
Definition: dynahash.c:783
Size InjectionPointShmemSize(void)
static Size total_addin_request
Definition: ipci.c:61
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:899
Size LockShmemSize(void)
Definition: lock.c:3552
Size LWLockShmemSize(void)
Definition: lwlock.c:423
Size MultiXactShmemSize(void)
Definition: multixact.c:1898
Size BTreeShmemSize(void)
Definition: nbtutils.c:4517
Size ReplicationOriginShmemSize(void)
Definition: origin.c:506
Size PgArchShmemSize(void)
Definition: pgarch.c:155
Size StatsShmemSize(void)
Definition: pgstat_shmem.c:127
Size PMSignalShmemSize(void)
Definition: pmsignal.c:129
Size PGSemaphoreShmemSize(int maxSemas)
Definition: posix_sema.c:165
Size PredicateLockShmemSize(void)
Definition: predicate.c:1342
Size ProcArrayShmemSize(void)
Definition: procarray.c:376
Size ProcSignalShmemSize(void)
Definition: procsignal.c:111
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
#define SHMEM_INDEX_SIZE
Definition: shmem.h:48
Size SInvalShmemSize(void)
Definition: sinvaladt.c:218
static pg_noinline void Size size
Definition: slab.c:607
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:171
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1578
Size SpinlockSemaSize(void)
Definition: spin.c:55
int SpinlockSemas(void)
Definition: spin.c:64
Size ProcGlobalShmemSize(void)
Definition: proc.c:100
int ProcGlobalSemas(void)
Definition: proc.c:122
Size SUBTRANSShmemSize(void)
Definition: subtrans.c:214
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
Size TwoPhaseShmemSize(void)
Definition: twophase.c:237
Size VarsupShmemSize(void)
Definition: varsup.c:41
Size WaitEventExtensionShmemSize(void)
Definition: wait_event.c:104
Size WalRcvShmemSize(void)
Size WalSndShmemSize(void)
Definition: walsender.c:3609
Size WalSummarizerShmemSize(void)
Size XLOGShmemSize(void)
Definition: xlog.c:4835
size_t XLogPrefetchShmemSize(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:447

References add_size(), ApplyLauncherShmemSize(), AsyncShmemSize(), AutoVacuumShmemSize(), BackendStatusShmemSize(), BackgroundWorkerShmemSize(), BTreeShmemSize(), BufferShmemSize(), CheckpointerShmemSize(), CLOGShmemSize(), CommitTsShmemSize(), dsm_estimate_size(), DSMRegistryShmemSize(), hash_estimate_size(), InjectionPointShmemSize(), LockShmemSize(), LWLockShmemSize(), MultiXactShmemSize(), PgArchShmemSize(), PGSemaphoreShmemSize(), PMSignalShmemSize(), PredicateLockShmemSize(), ProcArrayShmemSize(), ProcGlobalSemas(), ProcGlobalShmemSize(), ProcSignalShmemSize(), ReplicationOriginShmemSize(), ReplicationSlotsShmemSize(), SHMEM_INDEX_SIZE, SInvalShmemSize(), size, SlotSyncShmemSize(), SpinlockSemas(), SpinlockSemaSize(), StatsShmemSize(), SUBTRANSShmemSize(), SyncScanShmemSize(), total_addin_request, TwoPhaseShmemSize(), VarsupShmemSize(), WaitEventExtensionShmemSize(), WalRcvShmemSize(), WalSndShmemSize(), WalSummarizerShmemSize(), XLogPrefetchShmemSize(), XLogRecoveryShmemSize(), and XLOGShmemSize().

Referenced by CreateSharedMemoryAndSemaphores(), and InitializeShmemGUCs().

◆ CreateOrAttachShmemStructs()

static void CreateOrAttachShmemStructs ( void  )
static

Definition at line 281 of file ipci.c.

282 {
283  /*
284  * Now initialize LWLocks, which do shared memory allocation and are
285  * needed for InitShmemIndex.
286  */
287  CreateLWLocks();
288 
289  /*
290  * Set up shmem.c index hashtable
291  */
292  InitShmemIndex();
293 
294  dsm_shmem_init();
296 
297  /*
298  * Set up xlog, clog, and buffers
299  */
300  VarsupShmemInit();
301  XLOGShmemInit();
304  CLOGShmemInit();
308  InitBufferPool();
309 
310  /*
311  * Set up lock manager
312  */
313  InitLocks();
314 
315  /*
316  * Set up predicate lock manager
317  */
319 
320  /*
321  * Set up process table
322  */
323  if (!IsUnderPostmaster)
324  InitProcGlobal();
329 
330  /*
331  * Set up shared-inval messaging
332  */
334 
335  /*
336  * Set up interprocess signaling mechanisms
337  */
344  WalSndShmemInit();
345  WalRcvShmemInit();
347  PgArchShmemInit();
350 
351  /*
352  * Set up other modules that need some shared memory space
353  */
354  BTreeShmemInit();
356  AsyncShmemInit();
357  StatsShmemInit();
360 }
void AsyncShmemInit(void)
Definition: async.c:502
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3275
void CreateSharedBackendStatus(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:162
void InitBufferPool(void)
Definition: buf_init.c:68
void CheckpointerShmemInit(void)
Definition: checkpointer.c:898
void CLOGShmemInit(void)
Definition: clog.c:787
void CommitTsShmemInit(void)
Definition: commit_ts.c:530
void dsm_shmem_init(void)
Definition: dsm.c:479
void DSMRegistryShmemInit(void)
Definition: dsm_registry.c:69
bool IsUnderPostmaster
Definition: globals.c:117
void InjectionPointShmemInit(void)
void ApplyLauncherShmemInit(void)
Definition: launcher.c:954
void InitLocks(void)
Definition: lock.c:392
void CreateLWLocks(void)
Definition: lwlock.c:453
void MultiXactShmemInit(void)
Definition: multixact.c:1915
void BTreeShmemInit(void)
Definition: nbtutils.c:4530
void ReplicationOriginShmemInit(void)
Definition: origin.c:526
void PgArchShmemInit(void)
Definition: pgarch.c:166
void StatsShmemInit(void)
Definition: pgstat_shmem.c:141
void PMSignalShmemInit(void)
Definition: pmsignal.c:144
void InitPredicateLocks(void)
Definition: predicate.c:1140
void CreateSharedProcArray(void)
Definition: procarray.c:418
void ProcSignalShmemInit(void)
Definition: procsignal.c:125
void InitShmemIndex(void)
Definition: shmem.c:283
void CreateSharedInvalidationState(void)
Definition: sinvaladt.c:234
void ReplicationSlotsShmemInit(void)
Definition: slot.c:189
void SlotSyncShmemInit(void)
Definition: slotsync.c:1587
void InitProcGlobal(void)
Definition: proc.c:157
void SUBTRANSShmemInit(void)
Definition: subtrans.c:220
void SyncScanShmemInit(void)
Definition: syncscan.c:135
void TwoPhaseShmemInit(void)
Definition: twophase.c:253
void VarsupShmemInit(void)
Definition: varsup.c:47
void WaitEventExtensionShmemInit(void)
Definition: wait_event.c:120
void WalRcvShmemInit(void)
void WalSndShmemInit(void)
Definition: walsender.c:3621
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:4885
void XLogPrefetchShmemInit(void)
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:458

References ApplyLauncherShmemInit(), AsyncShmemInit(), AutoVacuumShmemInit(), BackgroundWorkerShmemInit(), BTreeShmemInit(), CheckpointerShmemInit(), CLOGShmemInit(), CommitTsShmemInit(), CreateLWLocks(), CreateSharedBackendStatus(), CreateSharedInvalidationState(), CreateSharedProcArray(), dsm_shmem_init(), DSMRegistryShmemInit(), InitBufferPool(), InitLocks(), InitPredicateLocks(), InitProcGlobal(), InitShmemIndex(), InjectionPointShmemInit(), IsUnderPostmaster, MultiXactShmemInit(), PgArchShmemInit(), PMSignalShmemInit(), ProcSignalShmemInit(), ReplicationOriginShmemInit(), ReplicationSlotsShmemInit(), SlotSyncShmemInit(), StatsShmemInit(), SUBTRANSShmemInit(), SyncScanShmemInit(), TwoPhaseShmemInit(), VarsupShmemInit(), WaitEventExtensionShmemInit(), WalRcvShmemInit(), WalSndShmemInit(), WalSummarizerShmemInit(), XLogPrefetchShmemInit(), XLogRecoveryShmemInit(), and XLOGShmemInit().

Referenced by CreateSharedMemoryAndSemaphores().

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( void  )

Definition at line 199 of file ipci.c.

200 {
201  PGShmemHeader *shim;
202  PGShmemHeader *seghdr;
203  Size size;
204  int numSemas;
205 
207 
208  /* Compute the size of the shared-memory block */
209  size = CalculateShmemSize(&numSemas);
210  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
211 
212  /*
213  * Create the shmem segment
214  */
215  seghdr = PGSharedMemoryCreate(size, &shim);
216 
217  /*
218  * Make sure that huge pages are never reported as "unknown" while the
219  * server is running.
220  */
221  Assert(strcmp("unknown",
222  GetConfigOption("huge_pages_status", false, false)) != 0);
223 
224  InitShmemAccess(seghdr);
225 
226  /*
227  * Create semaphores
228  */
229  PGReserveSemaphores(numSemas);
230 
231  /*
232  * If spinlocks are disabled, initialize emulation layer (which depends on
233  * semaphores, so the order is important here).
234  */
235 #ifndef HAVE_SPINLOCKS
237 #endif
238 
239  /*
240  * Set up shared memory allocation mechanism
241  */
243 
244  /* Initialize subsystems */
246 
247 #ifdef EXEC_BACKEND
248 
249  /*
250  * Alloc the win32 shared backend array
251  */
252  ShmemBackendArrayAllocation();
253 #endif
254 
255  /* Initialize dynamic shared memory facilities. */
257 
258  /*
259  * Now give loadable modules a chance to set up their shmem allocations
260  */
261  if (shmem_startup_hook)
263 }
#define Assert(condition)
Definition: c.h:858
void dsm_postmaster_startup(PGShmemHeader *shim)
Definition: dsm.c:177
#define DEBUG3
Definition: elog.h:28
#define elog(elevel,...)
Definition: elog.h:224
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4298
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:59
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:90
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:281
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitShmemAllocation(void)
Definition: shmem.c:115
void InitShmemAccess(void *seghdr)
Definition: shmem.c:100
void SpinlockSemaInit(void)
Definition: spin.c:77
PGShmemHeader * PGSharedMemoryCreate(Size size, PGShmemHeader **shim)
Definition: sysv_shmem.c:700

References Assert, CalculateShmemSize(), CreateOrAttachShmemStructs(), DEBUG3, dsm_postmaster_startup(), elog, GetConfigOption(), InitShmemAccess(), InitShmemAllocation(), IsUnderPostmaster, PGReserveSemaphores(), PGSharedMemoryCreate(), shmem_startup_hook, size, and SpinlockSemaInit().

Referenced by BootstrapModeMain(), PostgresSingleUserMain(), PostmasterMain(), and PostmasterStateMachine().

◆ InitializeShmemGUCs()

void InitializeShmemGUCs ( void  )

Definition at line 369 of file ipci.c.

370 {
371  char buf[64];
372  Size size_b;
373  Size size_mb;
374  Size hp_size;
375 
376  /*
377  * Calculate the shared memory size and round up to the nearest megabyte.
378  */
379  size_b = CalculateShmemSize(NULL);
380  size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
381  sprintf(buf, "%zu", size_mb);
382  SetConfigOption("shared_memory_size", buf,
384 
385  /*
386  * Calculate the number of huge pages required.
387  */
388  GetHugePageSize(&hp_size, NULL);
389  if (hp_size != 0)
390  {
391  Size hp_required;
392 
393  hp_required = add_size(size_b / hp_size, 1);
394  sprintf(buf, "%zu", hp_required);
395  SetConfigOption("shared_memory_size_in_huge_pages", buf,
397  }
398 }
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4275
@ PGC_S_DYNAMIC_DEFAULT
Definition: guc.h:110
@ PGC_INTERNAL
Definition: guc.h:69
static char * buf
Definition: pg_test_fsync.c:73
#define sprintf
Definition: port.h:240
void GetHugePageSize(Size *hugepagesize, int *mmap_flags)
Definition: sysv_shmem.c:479

References add_size(), buf, CalculateShmemSize(), GetHugePageSize(), PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, SetConfigOption(), and sprintf.

Referenced by PostgresSingleUserMain(), and PostmasterMain().

◆ 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
bool process_shmem_requests_in_progress
Definition: miscinit.c:1782

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

Referenced by pgss_shmem_request(), and test_slru_shmem_request().

Variable Documentation

◆ shared_memory_type

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE

Definition at line 57 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ shmem_startup_hook

shmem_startup_hook_type shmem_startup_hook = NULL

Definition at line 59 of file ipci.c.

Referenced by _PG_init(), and CreateSharedMemoryAndSemaphores().

◆ total_addin_request

Size total_addin_request = 0
static

Definition at line 61 of file ipci.c.

Referenced by CalculateShmemSize(), and RequestAddinShmemSpace().