Debian Codegraph @

src/backend/storage/ipc/dsm_registry.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * dsm_registry.c
4 * Functions for interfacing with the dynamic shared memory registry.
5 *
6 * This provides a way for libraries to use shared memory without needing
7 * to request it at startup time via a shmem_request_hook. The registry
8 * stores dynamic shared memory (DSM) segment handles keyed by a
9 * library-specified string.
10 *
11 * The registry is accessed by calling GetNamedDSMSegment(). If a segment
12 * with the provided name does not yet exist, it is created and initialized
13 * with the provided init_callback callback function. Otherwise,
14 * GetNamedDSMSegment() simply ensures that the segment is attached to the
15 * current backend. This function guarantees that only one backend
16 * initializes the segment and that all other backends just attach it.
17 *
18 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
19 * Portions Copyright (c) 1994, Regents of the University of California
20 *
21 * IDENTIFICATION
22 * src/backend/storage/ipc/dsm_registry.c
23 *
24 *-------------------------------------------------------------------------
25 */
26
27 #include "postgres.h"
28
29 #include "lib/dshash.h"
30 #include "storage/dsm_registry.h"
31 #include "storage/lwlock.h"
32 #include "storage/shmem.h"
33 #include "utils/memutils.h"
34
35 typedef struct DSMRegistryCtxStruct
36 {
37 dsa_handle dsah;
38 dshash_table_handle dshh;
39 } DSMRegistryCtxStruct;
40
41 static DSMRegistryCtxStruct *DSMRegistryCtx;
42
43 typedef struct DSMRegistryEntry
44 {
45 char name[64];
46 dsm_handle handle;
47 size_t size;
48 } DSMRegistryEntry;
49
50 static const dshash_parameters dsh_params = {
51 offsetof(DSMRegistryEntry, handle),
52 sizeof(DSMRegistryEntry),
53 dshash_strcmp,
54 dshash_strhash,
55 dshash_strcpy,
56 LWTRANCHE_DSM_REGISTRY_HASH
57 };
58
59 static dsa_area *dsm_registry_dsa;
60 static dshash_table *dsm_registry_table;
61
62 Size
63 DSMRegistryShmemSize(void)
64 {
65 return MAXALIGN(sizeof(DSMRegistryCtxStruct));
66 }
67
68 void
69 DSMRegistryShmemInit(void)
70 {
71 bool found;
72
73 DSMRegistryCtx = (DSMRegistryCtxStruct *)
74 ShmemInitStruct("DSM Registry Data",
75 DSMRegistryShmemSize(),
76 &found);
77
78 if (!found)
79 {
80 DSMRegistryCtx->dsah = DSA_HANDLE_INVALID;
81 DSMRegistryCtx->dshh = DSHASH_HANDLE_INVALID;
82 }
83 }
84
85 /*
86 * Initialize or attach to the dynamic shared hash table that stores the DSM
87 * registry entries, if not already done. This must be called before accessing
88 * the table.
89 */
90 static void
91 init_dsm_registry(void)
92 {
93 /* Quick exit if we already did this. */
94 if (dsm_registry_table)
95 return;
96
97 /* Otherwise, use a lock to ensure only one process creates the table. */
98 LWLockAcquire(DSMRegistryLock, LW_EXCLUSIVE);
99
100 if (DSMRegistryCtx->dshh == DSHASH_HANDLE_INVALID)
101 {
102 /* Initialize dynamic shared hash table for registry. */
103 dsm_registry_dsa = dsa_create(LWTRANCHE_DSM_REGISTRY_DSA);
104 dsm_registry_table = dshash_create(dsm_registry_dsa, &dsh_params, NULL);
105
106 dsa_pin(dsm_registry_dsa);
107 dsa_pin_mapping(dsm_registry_dsa);
108
109 /* Store handles in shared memory for other backends to use. */
110 DSMRegistryCtx->dsah = dsa_get_handle(dsm_registry_dsa);
111 DSMRegistryCtx->dshh = dshash_get_hash_table_handle(dsm_registry_table);
112 }
113 else
114 {
115 /* Attach to existing dynamic shared hash table. */
116 dsm_registry_dsa = dsa_attach(DSMRegistryCtx->dsah);
117 dsa_pin_mapping(dsm_registry_dsa);
118 dsm_registry_table = dshash_attach(dsm_registry_dsa, &dsh_params,
119 DSMRegistryCtx->dshh, NULL);
120 }
121
122 LWLockRelease(DSMRegistryLock);
123 }
124
125 /*
126 * Initialize or attach a named DSM segment.
127 *
128 * This routine returns the address of the segment. init_callback is called to
129 * initialize the segment when it is first created.
130 */
131 void *
132 GetNamedDSMSegment(const char *name, size_t size,
133 void (*init_callback) (void *ptr), bool *found)
134 {
135 DSMRegistryEntry *entry;
136 MemoryContext oldcontext;
137 void *ret;
138 dsm_segment *seg;
139
140 Assert(found);
141
142 if (!name || *name == '\0')
143 ereport(ERROR,
144 (errmsg("DSM segment name cannot be empty")));
145
146 if (strlen(name) >= offsetof(DSMRegistryEntry, handle))
147 ereport(ERROR,
148 (errmsg("DSM segment name too long")));
149
150 if (size == 0)
151 ereport(ERROR,
152 (errmsg("DSM segment size must be nonzero")));
153
154 /* Be sure any local memory allocated by DSM/DSA routines is persistent. */
155 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
156
157 /* Connect to the registry. */
158 init_dsm_registry();
159
160 entry = dshash_find_or_insert(dsm_registry_table, name, found);
161 if (!(*found))
162 {
163 entry->handle = DSM_HANDLE_INVALID;
164 entry->size = size;
165 }
166 else if (entry->size != size)
167 ereport(ERROR,
168 (errmsg("requested DSM segment size does not match size of existing segment")));
169
170 if (entry->handle == DSM_HANDLE_INVALID)
171 {
172 *found = false;
173
174 /* Initialize the segment. */
175 seg = dsm_create(size, 0);
176
177 if (init_callback)
178 (*init_callback) (dsm_segment_address(seg));
179
180 dsm_pin_segment(seg);
181 dsm_pin_mapping(seg);
182 entry->handle = dsm_segment_handle(seg);
183 }
184 else
185 {
186 /* If the existing segment is not already attached, attach it now. */
187 seg = dsm_find_mapping(entry->handle);
188 if (seg == NULL)
189 {
190 seg = dsm_attach(entry->handle);
191 if (seg == NULL)
192 elog(ERROR, "could not map dynamic shared memory segment");
193
194 dsm_pin_mapping(seg);
195 }
196 }
197
198 ret = dsm_segment_address(seg);
199 dshash_release_lock(dsm_registry_table, entry);
200 MemoryContextSwitchTo(oldcontext);
201
202 return ret;
203 }