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 91 of file ipci.c.

92 {
93  Size size;
94  int numSemas;
95 
96  /* Compute number of semaphores we'll need */
97  numSemas = ProcGlobalSemas();
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));
115  sizeof(ShmemIndexEnt)));
155 
156  /* include additional requested shmem from preload libraries */
158 
159  /* might as well round it off to a multiple of a typical page size */
160  size = add_size(size, 8192 - (size % 8192));
161 
162  return size;
163 }
Size AsyncShmemSize(void)
Definition: async.c:485
Size AutoVacuumShmemSize(void)
Definition: autovacuum.c:3266
Size BackendStatusShmemSize(void)
Size BackgroundWorkerShmemSize(void)
Definition: bgworker.c:146
Size BufferManagerShmemSize(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:62
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:905
Size LockManagerShmemSize(void)
Definition: lock.c:3573
Size LWLockShmemSize(void)
Definition: lwlock.c:420
Size MultiXactShmemSize(void)
Definition: multixact.c:1947
Size BTreeShmemSize(void)
Definition: nbtutils.c:4510
Size ReplicationOriginShmemSize(void)
Definition: origin.c:510
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:116
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
#define SHMEM_INDEX_SIZE
Definition: shmem.h:50
Size SharedInvalShmemSize(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:1660
Size ProcGlobalShmemSize(void)
Definition: proc.c:101
int ProcGlobalSemas(void)
Definition: proc.c:123
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 WaitEventCustomShmemSize(void)
Definition: wait_event.c:104
Size WaitLSNShmemSize(void)
Definition: waitlsn.c:41
Size WalRcvShmemSize(void)
Size WalSndShmemSize(void)
Definition: walsender.c:3612
Size WalSummarizerShmemSize(void)
Size XLOGShmemSize(void)
Definition: xlog.c:4861
size_t XLogPrefetchShmemSize(void)
Size XLogRecoveryShmemSize(void)
Definition: xlogrecovery.c:448

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

Referenced by CreateSharedMemoryAndSemaphores(), and InitializeShmemGUCs().

◆ CreateOrAttachShmemStructs()

static void CreateOrAttachShmemStructs ( void  )
static

Definition at line 262 of file ipci.c.

263 {
264  /*
265  * Now initialize LWLocks, which do shared memory allocation and are
266  * needed for InitShmemIndex.
267  */
268  CreateLWLocks();
269 
270  /*
271  * Set up shmem.c index hashtable
272  */
273  InitShmemIndex();
274 
275  dsm_shmem_init();
277 
278  /*
279  * Set up xlog, clog, and buffers
280  */
281  VarsupShmemInit();
282  XLOGShmemInit();
285  CLOGShmemInit();
290 
291  /*
292  * Set up lock manager
293  */
295 
296  /*
297  * Set up predicate lock manager
298  */
300 
301  /*
302  * Set up process table
303  */
304  if (!IsUnderPostmaster)
305  InitProcGlobal();
310 
311  /*
312  * Set up shared-inval messaging
313  */
315 
316  /*
317  * Set up interprocess signaling mechanisms
318  */
325  WalSndShmemInit();
326  WalRcvShmemInit();
328  PgArchShmemInit();
331 
332  /*
333  * Set up other modules that need some shared memory space
334  */
335  BTreeShmemInit();
337  AsyncShmemInit();
338  StatsShmemInit();
342 }
void AsyncShmemInit(void)
Definition: async.c:502
void AutoVacuumShmemInit(void)
Definition: autovacuum.c:3285
void BackendStatusShmemInit(void)
void BackgroundWorkerShmemInit(void)
Definition: bgworker.c:162
void BufferManagerShmemInit(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:119
void InjectionPointShmemInit(void)
void ApplyLauncherShmemInit(void)
Definition: launcher.c:960
void LockManagerShmemInit(void)
Definition: lock.c:390
void CreateLWLocks(void)
Definition: lwlock.c:450
void MultiXactShmemInit(void)
Definition: multixact.c:1964
void BTreeShmemInit(void)
Definition: nbtutils.c:4523
void ReplicationOriginShmemInit(void)
Definition: origin.c:530
void PgArchShmemInit(void)
Definition: pgarch.c:166
void StatsShmemInit(void)
Definition: pgstat_shmem.c:156
void PMSignalShmemInit(void)
Definition: pmsignal.c:144
void PredicateLockShmemInit(void)
Definition: predicate.c:1140
void ProcArrayShmemInit(void)
Definition: procarray.c:418
void ProcSignalShmemInit(void)
Definition: procsignal.c:130
void InitShmemIndex(void)
Definition: shmem.c:283
void SharedInvalShmemInit(void)
Definition: sinvaladt.c:234
void ReplicationSlotsShmemInit(void)
Definition: slot.c:189
void SlotSyncShmemInit(void)
Definition: slotsync.c:1669
void InitProcGlobal(void)
Definition: proc.c:158
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 WaitEventCustomShmemInit(void)
Definition: wait_event.c:120
void WaitLSNShmemInit(void)
Definition: waitlsn.c:52
void WalRcvShmemInit(void)
void WalSndShmemInit(void)
Definition: walsender.c:3624
void WalSummarizerShmemInit(void)
void XLOGShmemInit(void)
Definition: xlog.c:4911
void XLogPrefetchShmemInit(void)
void XLogRecoveryShmemInit(void)
Definition: xlogrecovery.c:459

References ApplyLauncherShmemInit(), AsyncShmemInit(), AutoVacuumShmemInit(), BackendStatusShmemInit(), BackgroundWorkerShmemInit(), BTreeShmemInit(), BufferManagerShmemInit(), CheckpointerShmemInit(), CLOGShmemInit(), CommitTsShmemInit(), CreateLWLocks(), dsm_shmem_init(), DSMRegistryShmemInit(), InitProcGlobal(), InitShmemIndex(), InjectionPointShmemInit(), IsUnderPostmaster, LockManagerShmemInit(), MultiXactShmemInit(), PgArchShmemInit(), PMSignalShmemInit(), PredicateLockShmemInit(), ProcArrayShmemInit(), ProcSignalShmemInit(), ReplicationOriginShmemInit(), ReplicationSlotsShmemInit(), SharedInvalShmemInit(), SlotSyncShmemInit(), StatsShmemInit(), SUBTRANSShmemInit(), SyncScanShmemInit(), TwoPhaseShmemInit(), VarsupShmemInit(), WaitEventCustomShmemInit(), WaitLSNShmemInit(), WalRcvShmemInit(), WalSndShmemInit(), WalSummarizerShmemInit(), XLogPrefetchShmemInit(), XLogRecoveryShmemInit(), and XLOGShmemInit().

Referenced by CreateSharedMemoryAndSemaphores().

◆ CreateSharedMemoryAndSemaphores()

void CreateSharedMemoryAndSemaphores ( void  )

Definition at line 196 of file ipci.c.

197 {
198  PGShmemHeader *shim;
199  PGShmemHeader *seghdr;
200  Size size;
201  int numSemas;
202 
204 
205  /* Compute the size of the shared-memory block */
206  size = CalculateShmemSize(&numSemas);
207  elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
208 
209  /*
210  * Create the shmem segment
211  */
212  seghdr = PGSharedMemoryCreate(size, &shim);
213 
214  /*
215  * Make sure that huge pages are never reported as "unknown" while the
216  * server is running.
217  */
218  Assert(strcmp("unknown",
219  GetConfigOption("huge_pages_status", false, false)) != 0);
220 
221  InitShmemAccess(seghdr);
222 
223  /*
224  * Create semaphores
225  */
226  PGReserveSemaphores(numSemas);
227 
228  /*
229  * Set up shared memory allocation mechanism
230  */
232 
233  /* Initialize subsystems */
235 
236  /* Initialize dynamic shared memory facilities. */
238 
239  /*
240  * Now give loadable modules a chance to set up their shmem allocations
241  */
242  if (shmem_startup_hook)
244 }
#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:225
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4314
shmem_startup_hook_type shmem_startup_hook
Definition: ipci.c:60
Size CalculateShmemSize(int *num_semaphores)
Definition: ipci.c:91
static void CreateOrAttachShmemStructs(void)
Definition: ipci.c:262
void PGReserveSemaphores(int maxSemas)
Definition: posix_sema.c:196
void InitShmemAllocation(void)
Definition: shmem.c:115
void InitShmemAccess(void *seghdr)
Definition: shmem.c:100
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, and size.

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

◆ InitializeShmemGUCs()

void InitializeShmemGUCs ( void  )

Definition at line 351 of file ipci.c.

352 {
353  char buf[64];
354  Size size_b;
355  Size size_mb;
356  Size hp_size;
357  int num_semas;
358 
359  /*
360  * Calculate the shared memory size and round up to the nearest megabyte.
361  */
362  size_b = CalculateShmemSize(&num_semas);
363  size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
364  sprintf(buf, "%zu", size_mb);
365  SetConfigOption("shared_memory_size", buf,
367 
368  /*
369  * Calculate the number of huge pages required.
370  */
371  GetHugePageSize(&hp_size, NULL);
372  if (hp_size != 0)
373  {
374  Size hp_required;
375 
376  hp_required = add_size(size_b / hp_size, 1);
377  sprintf(buf, "%zu", hp_required);
378  SetConfigOption("shared_memory_size_in_huge_pages", buf,
380  }
381 
382  sprintf(buf, "%d", num_semas);
383  SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
384 }
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4291
@ 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 76 of file ipci.c.

77 {
79  elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
81 }
#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 injection_shmem_request(), pgss_shmem_request(), and test_slru_shmem_request().

Variable Documentation

◆ shared_memory_type

int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE

Definition at line 58 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ shmem_startup_hook

shmem_startup_hook_type shmem_startup_hook = NULL

Definition at line 60 of file ipci.c.

Referenced by _PG_init(), and CreateSharedMemoryAndSemaphores().

◆ total_addin_request

Size total_addin_request = 0
static

Definition at line 62 of file ipci.c.

Referenced by CalculateShmemSize(), and RequestAddinShmemSpace().