PostgreSQL Source Code  git master
dsm_registry.c File Reference
#include "postgres.h"
#include "lib/dshash.h"
#include "storage/dsm_registry.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/memutils.h"
Include dependency graph for dsm_registry.c:

Go to the source code of this file.

Data Structures

struct  DSMRegistryCtxStruct
 
struct  DSMRegistryEntry
 

Typedefs

typedef struct DSMRegistryCtxStruct DSMRegistryCtxStruct
 
typedef struct DSMRegistryEntry DSMRegistryEntry
 

Functions

Size DSMRegistryShmemSize (void)
 
void DSMRegistryShmemInit (void)
 
static void init_dsm_registry (void)
 
void * GetNamedDSMSegment (const char *name, size_t size, void(*init_callback)(void *ptr), bool *found)
 

Variables

static DSMRegistryCtxStructDSMRegistryCtx
 
static const dshash_parameters dsh_params
 
static dsa_areadsm_registry_dsa
 
static dshash_tabledsm_registry_table
 

Typedef Documentation

◆ DSMRegistryCtxStruct

◆ DSMRegistryEntry

Function Documentation

◆ DSMRegistryShmemInit()

void DSMRegistryShmemInit ( void  )

Definition at line 69 of file dsm_registry.c.

70 {
71  bool found;
72 
74  ShmemInitStruct("DSM Registry Data",
76  &found);
77 
78  if (!found)
79  {
82  }
83 }
#define DSA_HANDLE_INVALID
Definition: dsa.h:139
#define DSHASH_HANDLE_INVALID
Definition: dshash.h:27
static DSMRegistryCtxStruct * DSMRegistryCtx
Definition: dsm_registry.c:41
Size DSMRegistryShmemSize(void)
Definition: dsm_registry.c:63
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:382
dshash_table_handle dshh
Definition: dsm_registry.c:38

References DSA_HANDLE_INVALID, DSMRegistryCtxStruct::dsah, DSHASH_HANDLE_INVALID, DSMRegistryCtxStruct::dshh, DSMRegistryCtx, DSMRegistryShmemSize(), and ShmemInitStruct().

Referenced by CreateOrAttachShmemStructs().

◆ DSMRegistryShmemSize()

Size DSMRegistryShmemSize ( void  )

Definition at line 63 of file dsm_registry.c.

64 {
65  return MAXALIGN(sizeof(DSMRegistryCtxStruct));
66 }
#define MAXALIGN(LEN)
Definition: c.h:765

References MAXALIGN.

Referenced by CalculateShmemSize(), and DSMRegistryShmemInit().

◆ GetNamedDSMSegment()

void* GetNamedDSMSegment ( const char *  name,
size_t  size,
void(*)(void *ptr)  init_callback,
bool *  found 
)

Definition at line 131 of file dsm_registry.c.

133 {
134  DSMRegistryEntry *entry;
135  MemoryContext oldcontext;
136  void *ret;
137 
138  Assert(found);
139 
140  if (!name || *name == '\0')
141  ereport(ERROR,
142  (errmsg("DSM segment name cannot be empty")));
143 
144  if (strlen(name) >= offsetof(DSMRegistryEntry, handle))
145  ereport(ERROR,
146  (errmsg("DSM segment name too long")));
147 
148  if (size == 0)
149  ereport(ERROR,
150  (errmsg("DSM segment size must be nonzero")));
151 
152  /* Be sure any local memory allocated by DSM/DSA routines is persistent. */
154 
155  /* Connect to the registry. */
157 
159  if (!(*found))
160  {
161  /* Initialize the segment. */
162  dsm_segment *seg = dsm_create(size, 0);
163 
164  dsm_pin_segment(seg);
165  dsm_pin_mapping(seg);
166  entry->handle = dsm_segment_handle(seg);
167  entry->size = size;
168  ret = dsm_segment_address(seg);
169 
170  if (init_callback)
171  (*init_callback) (ret);
172  }
173  else if (entry->size != size)
174  {
175  ereport(ERROR,
176  (errmsg("requested DSM segment size does not match size of "
177  "existing segment")));
178  }
179  else
180  {
181  dsm_segment *seg = dsm_find_mapping(entry->handle);
182 
183  /* If the existing segment is not already attached, attach it now. */
184  if (seg == NULL)
185  {
186  seg = dsm_attach(entry->handle);
187  if (seg == NULL)
188  elog(ERROR, "could not map dynamic shared memory segment");
189 
190  dsm_pin_mapping(seg);
191  }
192 
193  ret = dsm_segment_address(seg);
194  }
195 
197  MemoryContextSwitchTo(oldcontext);
198 
199  return ret;
200 }
#define Assert(condition)
Definition: c.h:812
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1123
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1095
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:665
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:516
void dsm_pin_mapping(dsm_segment *seg)
Definition: dsm.c:915
void dsm_pin_segment(dsm_segment *seg)
Definition: dsm.c:955
dsm_segment * dsm_find_mapping(dsm_handle handle)
Definition: dsm.c:1076
static void init_dsm_registry(void)
Definition: dsm_registry.c:91
static dshash_table * dsm_registry_table
Definition: dsm_registry.c:60
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
MemoryContext TopMemoryContext
Definition: mcxt.c:149
MemoryContextSwitchTo(old_ctx)
static pg_noinline void Size size
Definition: slab.c:607
dsm_handle handle
Definition: dsm_registry.c:46
const char * name

References Assert, dshash_find_or_insert(), dshash_release_lock(), dsm_attach(), dsm_create(), dsm_find_mapping(), dsm_pin_mapping(), dsm_pin_segment(), dsm_registry_table, dsm_segment_address(), dsm_segment_handle(), elog, ereport, errmsg(), ERROR, DSMRegistryEntry::handle, init_dsm_registry(), MemoryContextSwitchTo(), name, DSMRegistryEntry::size, size, and TopMemoryContext.

Referenced by apw_init_shmem(), injection_init_shmem(), and tdr_attach_shmem().

◆ init_dsm_registry()

static void init_dsm_registry ( void  )
static

Definition at line 91 of file dsm_registry.c.

92 {
93  /* Quick exit if we already did this. */
95  return;
96 
97  /* Otherwise, use a lock to ensure only one process creates the table. */
98  LWLockAcquire(DSMRegistryLock, LW_EXCLUSIVE);
99 
101  {
102  /* Initialize dynamic shared hash table for registry. */
107 
108  /* Store handles in shared memory for other backends to use. */
111  }
112  else
113  {
114  /* Attach to existing dynamic shared hash table. */
118  DSMRegistryCtx->dshh, NULL);
119  }
120 
121  LWLockRelease(DSMRegistryLock);
122 }
dsa_area * dsa_attach(dsa_handle handle)
Definition: dsa.c:510
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:635
dsa_handle dsa_get_handle(dsa_area *area)
Definition: dsa.c:498
void dsa_pin(dsa_area *area)
Definition: dsa.c:975
#define dsa_create(tranch_id)
Definition: dsa.h:117
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
static dsa_area * dsm_registry_dsa
Definition: dsm_registry.c:59
static const dshash_parameters dsh_params
Definition: dsm_registry.c:50
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ LWTRANCHE_DSM_REGISTRY_DSA
Definition: lwlock.h:208
@ LW_EXCLUSIVE
Definition: lwlock.h:114

References dsa_attach(), dsa_create, dsa_get_handle(), dsa_pin(), dsa_pin_mapping(), DSMRegistryCtxStruct::dsah, dsh_params, dshash_attach(), dshash_create(), dshash_get_hash_table_handle(), DSHASH_HANDLE_INVALID, DSMRegistryCtxStruct::dshh, dsm_registry_dsa, dsm_registry_table, DSMRegistryCtx, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and LWTRANCHE_DSM_REGISTRY_DSA.

Referenced by GetNamedDSMSegment().

Variable Documentation

◆ dsh_params

const dshash_parameters dsh_params
static
Initial value:
= {
offsetof(DSMRegistryEntry, handle),
}
void dshash_strcpy(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.c:622
dshash_hash dshash_strhash(const void *v, size_t size, void *arg)
Definition: dshash.c:611
int dshash_strcmp(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.c:599
struct DSMRegistryEntry DSMRegistryEntry
@ LWTRANCHE_DSM_REGISTRY_HASH
Definition: lwlock.h:209

Definition at line 50 of file dsm_registry.c.

Referenced by init_dsm_registry().

◆ dsm_registry_dsa

dsa_area* dsm_registry_dsa
static

Definition at line 59 of file dsm_registry.c.

Referenced by init_dsm_registry().

◆ dsm_registry_table

dshash_table* dsm_registry_table
static

Definition at line 60 of file dsm_registry.c.

Referenced by GetNamedDSMSegment(), and init_dsm_registry().

◆ DSMRegistryCtx

DSMRegistryCtxStruct* DSMRegistryCtx
static

Definition at line 41 of file dsm_registry.c.

Referenced by DSMRegistryShmemInit(), and init_dsm_registry().