Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0bd305e

Browse files
committed
Move the shared memory size calculation to its own function
This change refactors the shared memory size calculation in CreateSharedMemoryAndSemaphores() to its own function. This is intended for use in a future change related to the setup of huge pages and shared memory with some GUCs, while useful on its own for extensions. Author: Nathan Bossart Discussion: https://postgr.es/m/F2772387-CE0F-46BF-B5F1-CC55516EB885@amazon.com
1 parent 5fcb23c commit 0bd305e

File tree

2 files changed

+84
-59
lines changed

2 files changed

+84
-59
lines changed

src/backend/storage/ipc/ipci.c

Lines changed: 83 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,87 @@ RequestAddinShmemSpace(Size size)
7575
total_addin_request = add_size(total_addin_request, size);
7676
}
7777

78+
/*
79+
* CalculateShmemSize
80+
* Calculates the amount of shared memory and number of semaphores needed.
81+
*
82+
* If num_semaphores is not NULL, it will be set to the number of semaphores
83+
* required.
84+
*
85+
* Note that this function freezes the additional shared memory request size
86+
* from loadable modules.
87+
*/
88+
Size
89+
CalculateShmemSize(int *num_semaphores)
90+
{
91+
Size size;
92+
int numSemas;
93+
94+
/* Compute number of semaphores we'll need */
95+
numSemas = ProcGlobalSemas();
96+
numSemas += SpinlockSemas();
97+
98+
/* Return the number of semaphores if requested by the caller */
99+
if (num_semaphores)
100+
*num_semaphores = numSemas;
101+
102+
/*
103+
* Size of the Postgres shared-memory block is estimated via moderately-
104+
* accurate estimates for the big hogs, plus 100K for the stuff that's too
105+
* small to bother with estimating.
106+
*
107+
* We take some care to ensure that the total size request doesn't
108+
* overflow size_t. If this gets through, we don't need to be so careful
109+
* during the actual allocation phase.
110+
*/
111+
size = 100000;
112+
size = add_size(size, PGSemaphoreShmemSize(numSemas));
113+
size = add_size(size, SpinlockSemaSize());
114+
size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
115+
sizeof(ShmemIndexEnt)));
116+
size = add_size(size, dsm_estimate_size());
117+
size = add_size(size, BufferShmemSize());
118+
size = add_size(size, LockShmemSize());
119+
size = add_size(size, PredicateLockShmemSize());
120+
size = add_size(size, ProcGlobalShmemSize());
121+
size = add_size(size, XLOGShmemSize());
122+
size = add_size(size, CLOGShmemSize());
123+
size = add_size(size, CommitTsShmemSize());
124+
size = add_size(size, SUBTRANSShmemSize());
125+
size = add_size(size, TwoPhaseShmemSize());
126+
size = add_size(size, BackgroundWorkerShmemSize());
127+
size = add_size(size, MultiXactShmemSize());
128+
size = add_size(size, LWLockShmemSize());
129+
size = add_size(size, ProcArrayShmemSize());
130+
size = add_size(size, BackendStatusShmemSize());
131+
size = add_size(size, SInvalShmemSize());
132+
size = add_size(size, PMSignalShmemSize());
133+
size = add_size(size, ProcSignalShmemSize());
134+
size = add_size(size, CheckpointerShmemSize());
135+
size = add_size(size, AutoVacuumShmemSize());
136+
size = add_size(size, ReplicationSlotsShmemSize());
137+
size = add_size(size, ReplicationOriginShmemSize());
138+
size = add_size(size, WalSndShmemSize());
139+
size = add_size(size, WalRcvShmemSize());
140+
size = add_size(size, PgArchShmemSize());
141+
size = add_size(size, ApplyLauncherShmemSize());
142+
size = add_size(size, SnapMgrShmemSize());
143+
size = add_size(size, BTreeShmemSize());
144+
size = add_size(size, SyncScanShmemSize());
145+
size = add_size(size, AsyncShmemSize());
146+
#ifdef EXEC_BACKEND
147+
size = add_size(size, ShmemBackendArraySize());
148+
#endif
149+
150+
/* freeze the addin request size and include it */
151+
addin_request_allowed = false;
152+
size = add_size(size, total_addin_request);
153+
154+
/* might as well round it off to a multiple of a typical page size */
155+
size = add_size(size, 8192 - (size % 8192));
156+
157+
return size;
158+
}
78159

79160
/*
80161
* CreateSharedMemoryAndSemaphores
@@ -102,65 +183,8 @@ CreateSharedMemoryAndSemaphores(void)
102183
Size size;
103184
int numSemas;
104185

105-
/* Compute number of semaphores we'll need */
106-
numSemas = ProcGlobalSemas();
107-
numSemas += SpinlockSemas();
108-
109-
/*
110-
* Size of the Postgres shared-memory block is estimated via
111-
* moderately-accurate estimates for the big hogs, plus 100K for the
112-
* stuff that's too small to bother with estimating.
113-
*
114-
* We take some care during this phase to ensure that the total size
115-
* request doesn't overflow size_t. If this gets through, we don't
116-
* need to be so careful during the actual allocation phase.
117-
*/
118-
size = 100000;
119-
size = add_size(size, PGSemaphoreShmemSize(numSemas));
120-
size = add_size(size, SpinlockSemaSize());
121-
size = add_size(size, hash_estimate_size(SHMEM_INDEX_SIZE,
122-
sizeof(ShmemIndexEnt)));
123-
size = add_size(size, dsm_estimate_size());
124-
size = add_size(size, BufferShmemSize());
125-
size = add_size(size, LockShmemSize());
126-
size = add_size(size, PredicateLockShmemSize());
127-
size = add_size(size, ProcGlobalShmemSize());
128-
size = add_size(size, XLOGShmemSize());
129-
size = add_size(size, CLOGShmemSize());
130-
size = add_size(size, CommitTsShmemSize());
131-
size = add_size(size, SUBTRANSShmemSize());
132-
size = add_size(size, TwoPhaseShmemSize());
133-
size = add_size(size, BackgroundWorkerShmemSize());
134-
size = add_size(size, MultiXactShmemSize());
135-
size = add_size(size, LWLockShmemSize());
136-
size = add_size(size, ProcArrayShmemSize());
137-
size = add_size(size, BackendStatusShmemSize());
138-
size = add_size(size, SInvalShmemSize());
139-
size = add_size(size, PMSignalShmemSize());
140-
size = add_size(size, ProcSignalShmemSize());
141-
size = add_size(size, CheckpointerShmemSize());
142-
size = add_size(size, AutoVacuumShmemSize());
143-
size = add_size(size, ReplicationSlotsShmemSize());
144-
size = add_size(size, ReplicationOriginShmemSize());
145-
size = add_size(size, WalSndShmemSize());
146-
size = add_size(size, WalRcvShmemSize());
147-
size = add_size(size, PgArchShmemSize());
148-
size = add_size(size, ApplyLauncherShmemSize());
149-
size = add_size(size, SnapMgrShmemSize());
150-
size = add_size(size, BTreeShmemSize());
151-
size = add_size(size, SyncScanShmemSize());
152-
size = add_size(size, AsyncShmemSize());
153-
#ifdef EXEC_BACKEND
154-
size = add_size(size, ShmemBackendArraySize());
155-
#endif
156-
157-
/* freeze the addin request size and include it */
158-
addin_request_allowed = false;
159-
size = add_size(size, total_addin_request);
160-
161-
/* might as well round it off to a multiple of a typical page size */
162-
size = add_size(size, 8192 - (size % 8192));
163-
186+
/* Compute the size of the shared-memory block */
187+
size = CalculateShmemSize(&numSemas);
164188
elog(DEBUG3, "invoking IpcMemoryCreate(size=%zu)", size);
165189

166190
/*

src/include/storage/ipc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extern void check_on_shmem_exit_lists_are_empty(void);
7777
/* ipci.c */
7878
extern PGDLLIMPORT shmem_startup_hook_type shmem_startup_hook;
7979

80+
extern Size CalculateShmemSize(int *num_semaphores);
8081
extern void CreateSharedMemoryAndSemaphores(void);
8182

8283
#endif /* IPC_H */

0 commit comments

Comments
 (0)