Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * ipci.c
4 : * POSTGRES inter-process communication initialization code.
5 : *
6 : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : *
10 : * IDENTIFICATION
11 : * src/backend/storage/ipc/ipci.c
12 : *
13 : *-------------------------------------------------------------------------
14 : */
15 : #include "postgres.h"
16 :
17 : #include "access/clog.h"
18 : #include "access/commit_ts.h"
19 : #include "access/multixact.h"
20 : #include "access/nbtree.h"
21 : #include "access/subtrans.h"
22 : #include "access/syncscan.h"
23 : #include "access/transam.h"
24 : #include "access/twophase.h"
25 : #include "access/xlogprefetcher.h"
26 : #include "access/xlogrecovery.h"
27 : #include "commands/async.h"
28 : #include "miscadmin.h"
29 : #include "pgstat.h"
30 : #include "postmaster/autovacuum.h"
31 : #include "postmaster/bgworker_internals.h"
32 : #include "postmaster/bgwriter.h"
33 : #include "postmaster/walsummarizer.h"
34 : #include "replication/logicallauncher.h"
35 : #include "replication/origin.h"
36 : #include "replication/slot.h"
37 : #include "replication/slotsync.h"
38 : #include "replication/walreceiver.h"
39 : #include "replication/walsender.h"
40 : #include "storage/bufmgr.h"
41 : #include "storage/dsm.h"
42 : #include "storage/dsm_registry.h"
43 : #include "storage/ipc.h"
44 : #include "storage/pg_shmem.h"
45 : #include "storage/pmsignal.h"
46 : #include "storage/predicate.h"
47 : #include "storage/proc.h"
48 : #include "storage/procarray.h"
49 : #include "storage/procsignal.h"
50 : #include "storage/sinvaladt.h"
51 : #include "utils/guc.h"
52 : #include "utils/injection_point.h"
53 :
54 : /* GUCs */
55 : int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE;
56 :
57 : shmem_startup_hook_type shmem_startup_hook = NULL;
58 :
59 : static Size total_addin_request = 0;
60 :
61 : static void CreateOrAttachShmemStructs(void);
62 :
63 : /*
64 : * RequestAddinShmemSpace
65 : * Request that extra shmem space be allocated for use by
66 : * a loadable module.
67 : *
68 : * This may only be called via the shmem_request_hook of a library that is
69 : * loaded into the postmaster via shared_preload_libraries. Calls from
70 : * elsewhere will fail.
71 : */
72 : void
73 26 : RequestAddinShmemSpace(Size size)
74 : {
75 26 : if (!process_shmem_requests_in_progress)
76 0 : elog(FATAL, "cannot request additional shared memory outside shmem_request_hook");
77 26 : total_addin_request = add_size(total_addin_request, size);
78 26 : }
79 :
80 : /*
81 : * CalculateShmemSize
82 : * Calculates the amount of shared memory and number of semaphores needed.
83 : *
84 : * If num_semaphores is not NULL, it will be set to the number of semaphores
85 : * required.
86 : */
87 : Size
88 3534 : CalculateShmemSize(int *num_semaphores)
89 : {
90 : Size size;
91 : int numSemas;
92 :
93 : /* Compute number of semaphores we'll need */
94 3534 : numSemas = ProcGlobalSemas();
95 :
96 : /* Return the number of semaphores if requested by the caller */
97 3534 : if (num_semaphores)
98 3534 : *num_semaphores = numSemas;
99 :
100 : /*
101 : * Size of the Postgres shared-memory block is estimated via moderately-
102 : * accurate estimates for the big hogs, plus 100K for the stuff that's too
103 : * small to bother with estimating.
104 : *
105 : * We take some care to ensure that the total size request doesn't
106 : * overflow size_t. If this gets through, we don't need to be so careful
107 : * during the actual allocation phase.
108 : */
109 3534 : size = 100000;
110 3534 : size = add_size(size, PGSemaphoreShmemSize(numSemas));
111 3534 : size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
112 : sizeof(ShmemIndexEnt)));
113 3534 : size = add_size(size, dsm_estimate_size());
114 3534 : size = add_size(size, DSMRegistryShmemSize());
115 3534 : size = add_size(size, BufferManagerShmemSize());
116 3534 : size = add_size(size, LockManagerShmemSize());
117 3534 : size = add_size(size, PredicateLockShmemSize());
118 3534 : size = add_size(size, ProcGlobalShmemSize());
119 3534 : size = add_size(size, XLogPrefetchShmemSize());
120 3534 : size = add_size(size, VarsupShmemSize());
121 3534 : size = add_size(size, XLOGShmemSize());
122 3534 : size = add_size(size, XLogRecoveryShmemSize());
123 3534 : size = add_size(size, CLOGShmemSize());
124 3534 : size = add_size(size, CommitTsShmemSize());
125 3534 : size = add_size(size, SUBTRANSShmemSize());
126 3534 : size = add_size(size, TwoPhaseShmemSize());
127 3534 : size = add_size(size, BackgroundWorkerShmemSize());
128 3534 : size = add_size(size, MultiXactShmemSize());
129 3534 : size = add_size(size, LWLockShmemSize());
130 3534 : size = add_size(size, ProcArrayShmemSize());
131 3534 : size = add_size(size, BackendStatusShmemSize());
132 3534 : size = add_size(size, SharedInvalShmemSize());
133 3534 : size = add_size(size, PMSignalShmemSize());
134 3534 : size = add_size(size, ProcSignalShmemSize());
135 3534 : size = add_size(size, CheckpointerShmemSize());
136 3534 : size = add_size(size, AutoVacuumShmemSize());
137 3534 : size = add_size(size, ReplicationSlotsShmemSize());
138 3534 : size = add_size(size, ReplicationOriginShmemSize());
139 3534 : size = add_size(size, WalSndShmemSize());
140 3534 : size = add_size(size, WalRcvShmemSize());
141 3534 : size = add_size(size, WalSummarizerShmemSize());
142 3534 : size = add_size(size, PgArchShmemSize());
143 3534 : size = add_size(size, ApplyLauncherShmemSize());
144 3534 : size = add_size(size, BTreeShmemSize());
145 3534 : size = add_size(size, SyncScanShmemSize());
146 3534 : size = add_size(size, AsyncShmemSize());
147 3534 : size = add_size(size, StatsShmemSize());
148 3534 : size = add_size(size, WaitEventCustomShmemSize());
149 3534 : size = add_size(size, InjectionPointShmemSize());
150 3534 : size = add_size(size, SlotSyncShmemSize());
151 :
152 : /* include additional requested shmem from preload libraries */
153 3534 : size = add_size(size, total_addin_request);
154 :
155 : /* might as well round it off to a multiple of a typical page size */
156 3534 : size = add_size(size, 8192 - (size % 8192));
157 :
158 3534 : return size;
159 : }
160 :
161 : #ifdef EXEC_BACKEND
162 : /*
163 : * AttachSharedMemoryStructs
164 : * Initialize a postmaster child process's access to shared memory
165 : * structures.
166 : *
167 : * In !EXEC_BACKEND mode, we inherit everything through the fork, and this
168 : * isn't needed.
169 : */
170 : void
171 : AttachSharedMemoryStructs(void)
172 : {
173 : /* InitProcess must've been called already */
174 : Assert(MyProc != NULL);
175 : Assert(IsUnderPostmaster);
176 :
177 : /*
178 : * In EXEC_BACKEND mode, backends don't inherit the number of fast-path
179 : * groups we calculated before setting the shmem up, so recalculate it.
180 : */
181 : InitializeFastPathLocks();
182 :
183 : CreateOrAttachShmemStructs();
184 :
185 : /*
186 : * Now give loadable modules a chance to set up their shmem allocations
187 : */
188 : if (shmem_startup_hook)
189 : shmem_startup_hook();
190 : }
191 : #endif
192 :
193 : /*
194 : * CreateSharedMemoryAndSemaphores
195 : * Creates and initializes shared memory and semaphores.
196 : */
197 : void
198 1906 : CreateSharedMemoryAndSemaphores(void)
199 : {
200 : PGShmemHeader *shim;
201 : PGShmemHeader *seghdr;
202 : Size size;
203 : int numSemas;
204 :
205 : Assert(!IsUnderPostmaster);
206 :
207 : /* Compute the size of the shared-memory block */
208 1906 : size = CalculateShmemSize(&numSemas);
209 1906 : elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
210 :
211 : /*
212 : * Create the shmem segment
213 : */
214 1906 : seghdr = PGSharedMemoryCreate(size, &shim);
215 :
216 : /*
217 : * Make sure that huge pages are never reported as "unknown" while the
218 : * server is running.
219 : */
220 : Assert(strcmp("unknown",
221 : GetConfigOption("huge_pages_status", false, false)) != 0);
222 :
223 1902 : InitShmemAccess(seghdr);
224 :
225 : /*
226 : * Create semaphores
227 : */
228 1902 : PGReserveSemaphores(numSemas);
229 :
230 : /*
231 : * Set up shared memory allocation mechanism
232 : */
233 1902 : InitShmemAllocation();
234 :
235 : /* Initialize subsystems */
236 1902 : CreateOrAttachShmemStructs();
237 :
238 : /* Initialize dynamic shared memory facilities. */
239 1902 : dsm_postmaster_startup(shim);
240 :
241 : /*
242 : * Now give loadable modules a chance to set up their shmem allocations
243 : */
244 1902 : if (shmem_startup_hook)
245 24 : shmem_startup_hook();
246 1902 : }
247 :
248 : /*
249 : * Initialize various subsystems, setting up their data structures in
250 : * shared memory.
251 : *
252 : * This is called by the postmaster or by a standalone backend.
253 : * It is also called by a backend forked from the postmaster in the
254 : * EXEC_BACKEND case. In the latter case, the shared memory segment
255 : * already exists and has been physically attached to, but we have to
256 : * initialize pointers in local memory that reference the shared structures,
257 : * because we didn't inherit the correct pointer values from the postmaster
258 : * as we do in the fork() scenario. The easiest way to do that is to run
259 : * through the same code as before. (Note that the called routines mostly
260 : * check IsUnderPostmaster, rather than EXEC_BACKEND, to detect this case.
261 : * This is a bit code-wasteful and could be cleaned up.)
262 : */
263 : static void
264 1902 : CreateOrAttachShmemStructs(void)
265 : {
266 : /*
267 : * Now initialize LWLocks, which do shared memory allocation and are
268 : * needed for InitShmemIndex.
269 : */
270 1902 : CreateLWLocks();
271 :
272 : /*
273 : * Set up shmem.c index hashtable
274 : */
275 1902 : InitShmemIndex();
276 :
277 1902 : dsm_shmem_init();
278 1902 : DSMRegistryShmemInit();
279 :
280 : /*
281 : * Set up xlog, clog, and buffers
282 : */
283 1902 : VarsupShmemInit();
284 1902 : XLOGShmemInit();
285 1902 : XLogPrefetchShmemInit();
286 1902 : XLogRecoveryShmemInit();
287 1902 : CLOGShmemInit();
288 1902 : CommitTsShmemInit();
289 1902 : SUBTRANSShmemInit();
290 1902 : MultiXactShmemInit();
291 1902 : BufferManagerShmemInit();
292 :
293 : /*
294 : * Set up lock manager
295 : */
296 1902 : LockManagerShmemInit();
297 :
298 : /*
299 : * Set up predicate lock manager
300 : */
301 1902 : PredicateLockShmemInit();
302 :
303 : /*
304 : * Set up process table
305 : */
306 1902 : if (!IsUnderPostmaster)
307 1902 : InitProcGlobal();
308 1902 : ProcArrayShmemInit();
309 1902 : BackendStatusShmemInit();
310 1902 : TwoPhaseShmemInit();
311 1902 : BackgroundWorkerShmemInit();
312 :
313 : /*
314 : * Set up shared-inval messaging
315 : */
316 1902 : SharedInvalShmemInit();
317 :
318 : /*
319 : * Set up interprocess signaling mechanisms
320 : */
321 1902 : PMSignalShmemInit();
322 1902 : ProcSignalShmemInit();
323 1902 : CheckpointerShmemInit();
324 1902 : AutoVacuumShmemInit();
325 1902 : ReplicationSlotsShmemInit();
326 1902 : ReplicationOriginShmemInit();
327 1902 : WalSndShmemInit();
328 1902 : WalRcvShmemInit();
329 1902 : WalSummarizerShmemInit();
330 1902 : PgArchShmemInit();
331 1902 : ApplyLauncherShmemInit();
332 1902 : SlotSyncShmemInit();
333 :
334 : /*
335 : * Set up other modules that need some shared memory space
336 : */
337 1902 : BTreeShmemInit();
338 1902 : SyncScanShmemInit();
339 1902 : AsyncShmemInit();
340 1902 : StatsShmemInit();
341 1902 : WaitEventCustomShmemInit();
342 1902 : InjectionPointShmemInit();
343 1902 : }
344 :
345 : /*
346 : * InitializeShmemGUCs
347 : *
348 : * This function initializes runtime-computed GUCs related to the amount of
349 : * shared memory required for the current configuration.
350 : */
351 : void
352 1628 : InitializeShmemGUCs(void)
353 : {
354 : char buf[64];
355 : Size size_b;
356 : Size size_mb;
357 : Size hp_size;
358 : int num_semas;
359 :
360 : /*
361 : * Calculate the shared memory size and round up to the nearest megabyte.
362 : */
363 1628 : size_b = CalculateShmemSize(&num_semas);
364 1628 : size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
365 1628 : sprintf(buf, "%zu", size_mb);
366 1628 : SetConfigOption("shared_memory_size", buf,
367 : PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
368 :
369 : /*
370 : * Calculate the number of huge pages required.
371 : */
372 1628 : GetHugePageSize(&hp_size, NULL);
373 1628 : if (hp_size != 0)
374 : {
375 : Size hp_required;
376 :
377 1628 : hp_required = add_size(size_b / hp_size, 1);
378 1628 : sprintf(buf, "%zu", hp_required);
379 1628 : SetConfigOption("shared_memory_size_in_huge_pages", buf,
380 : PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
381 : }
382 :
383 1628 : sprintf(buf, "%d", num_semas);
384 1628 : SetConfigOption("num_os_semaphores", buf, PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT);
385 1628 : }
|