PostgreSQL Source Code  git master
dsm.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "lib/ilist.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/pg_shmem.h"
#include "utils/freepage.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/resowner_private.h"
Include dependency graph for dsm.c:

Go to the source code of this file.

Data Structures

struct  dsm_segment_detach_callback
 
struct  dsm_segment
 
struct  dsm_control_item
 
struct  dsm_control_header
 

Macros

#define PG_DYNSHMEM_CONTROL_MAGIC   0x9a503d32
 
#define PG_DYNSHMEM_FIXED_SLOTS   64
 
#define PG_DYNSHMEM_SLOTS_PER_BACKEND   5
 
#define INVALID_CONTROL_SLOT   ((uint32) -1)
 

Typedefs

typedef struct dsm_segment_detach_callback dsm_segment_detach_callback
 
typedef struct dsm_control_item dsm_control_item
 
typedef struct dsm_control_header dsm_control_header
 

Functions

static void dsm_cleanup_for_mmap (void)
 
static void dsm_postmaster_shutdown (int code, Datum arg)
 
static dsm_segmentdsm_create_descriptor (void)
 
static bool dsm_control_segment_sane (dsm_control_header *control, Size mapped_size)
 
static uint64 dsm_control_bytes_needed (uint32 nitems)
 
static dsm_handle make_main_region_dsm_handle (int slot)
 
static bool is_main_region_dsm_handle (dsm_handle handle)
 
void dsm_postmaster_startup (PGShmemHeader *shim)
 
void dsm_cleanup_using_control_segment (dsm_handle old_control_handle)
 
static void dsm_backend_startup (void)
 
size_t dsm_estimate_size (void)
 
void dsm_shmem_init (void)
 
dsm_segmentdsm_create (Size size, int flags)
 
dsm_segmentdsm_attach (dsm_handle h)
 
void dsm_backend_shutdown (void)
 
void dsm_detach_all (void)
 
void dsm_detach (dsm_segment *seg)
 
void dsm_pin_mapping (dsm_segment *seg)
 
void dsm_unpin_mapping (dsm_segment *seg)
 
void dsm_pin_segment (dsm_segment *seg)
 
void dsm_unpin_segment (dsm_handle handle)
 
dsm_segmentdsm_find_mapping (dsm_handle h)
 
void * dsm_segment_address (dsm_segment *seg)
 
Size dsm_segment_map_length (dsm_segment *seg)
 
dsm_handle dsm_segment_handle (dsm_segment *seg)
 
void on_dsm_detach (dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
 
void cancel_on_dsm_detach (dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
 
void reset_on_dsm_detach (void)
 

Variables

static bool dsm_init_done = false
 
static void * dsm_main_space_begin = NULL
 
static dlist_head dsm_segment_list = DLIST_STATIC_INIT(dsm_segment_list)
 
static dsm_handle dsm_control_handle
 
static dsm_control_headerdsm_control
 
static Size dsm_control_mapped_size = 0
 
static void * dsm_control_impl_private = NULL
 

Macro Definition Documentation

◆ INVALID_CONTROL_SLOT

#define INVALID_CONTROL_SLOT   ((uint32) -1)

◆ PG_DYNSHMEM_CONTROL_MAGIC

#define PG_DYNSHMEM_CONTROL_MAGIC   0x9a503d32

Definition at line 48 of file dsm.c.

Referenced by dsm_control_segment_sane(), and dsm_postmaster_startup().

◆ PG_DYNSHMEM_FIXED_SLOTS

#define PG_DYNSHMEM_FIXED_SLOTS   64

Definition at line 50 of file dsm.c.

Referenced by dsm_postmaster_startup().

◆ PG_DYNSHMEM_SLOTS_PER_BACKEND

#define PG_DYNSHMEM_SLOTS_PER_BACKEND   5

Definition at line 51 of file dsm.c.

Referenced by dsm_postmaster_startup().

Typedef Documentation

◆ dsm_control_header

◆ dsm_control_item

◆ dsm_segment_detach_callback

Function Documentation

◆ cancel_on_dsm_detach()

void cancel_on_dsm_detach ( dsm_segment seg,
on_dsm_detach_callback  function,
Datum  arg 
)

Definition at line 1106 of file dsm.c.

References dsm_segment_detach_callback::arg, slist_mutable_iter::cur, dsm_segment_detach_callback::function, dsm_segment_detach_callback::node, dsm_segment::on_detach, pfree(), slist_container, slist_delete_current(), and slist_foreach_modify.

Referenced by shm_mq_detach(), and test_shm_mq_setup().

1108 {
1109  slist_mutable_iter iter;
1110 
1111  slist_foreach_modify(iter, &seg->on_detach)
1112  {
1114 
1116  if (cb->function == function && cb->arg == arg)
1117  {
1118  slist_delete_current(&iter);
1119  pfree(cb);
1120  break;
1121  }
1122  }
1123 }
slist_node * cur
Definition: ilist.h:241
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:716
void pfree(void *pointer)
Definition: mcxt.c:1057
on_dsm_detach_callback function
Definition: dsm.c:58
slist_head on_detach
Definition: dsm.c:73
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
void * arg
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:652

◆ dsm_attach()

dsm_segment* dsm_attach ( dsm_handle  h)

Definition at line 631 of file dsm.c.

References Assert, dsm_segment::control_slot, dlist_iter::cur, dlist_container, dlist_foreach, dsm_backend_startup(), dsm_create_descriptor(), dsm_detach(), dsm_impl_op(), dsm_init_done, dsm_main_space_begin, DSM_OP_ATTACH, elog, ERROR, dsm_control_item::first_page, FPM_PAGE_SIZE, dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, INVALID_CONTROL_SLOT, is_main_region_dsm_handle(), IsUnderPostmaster, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_control_header::nitems, dsm_segment_detach_callback::node, dsm_control_item::npages, and dsm_control_item::refcnt.

Referenced by AttachSession(), autoprewarm_database_main(), dsa_attach(), get_segment_by_index(), ParallelWorkerMain(), and test_shm_mq_main().

632 {
633  dsm_segment *seg;
634  dlist_iter iter;
635  uint32 i;
636  uint32 nitems;
637 
638  /* Unsafe in postmaster (and pointless in a stand-alone backend). */
640 
641  if (!dsm_init_done)
643 
644  /*
645  * Since this is just a debugging cross-check, we could leave it out
646  * altogether, or include it only in assert-enabled builds. But since the
647  * list of attached segments should normally be very short, let's include
648  * it always for right now.
649  *
650  * If you're hitting this error, you probably want to attempt to find an
651  * existing mapping via dsm_find_mapping() before calling dsm_attach() to
652  * create a new one.
653  */
655  {
656  seg = dlist_container(dsm_segment, node, iter.cur);
657  if (seg->handle == h)
658  elog(ERROR, "can't attach the same segment more than once");
659  }
660 
661  /* Create a new segment descriptor. */
662  seg = dsm_create_descriptor();
663  seg->handle = h;
664 
665  /* Bump reference count for this segment in shared memory. */
666  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
667  nitems = dsm_control->nitems;
668  for (i = 0; i < nitems; ++i)
669  {
670  /*
671  * If the reference count is 0, the slot is actually unused. If the
672  * reference count is 1, the slot is still in use, but the segment is
673  * in the process of going away; even if the handle matches, another
674  * slot may already have started using the same handle value by
675  * coincidence so we have to keep searching.
676  */
677  if (dsm_control->item[i].refcnt <= 1)
678  continue;
679 
680  /* If the handle doesn't match, it's not the slot we want. */
681  if (dsm_control->item[i].handle != seg->handle)
682  continue;
683 
684  /* Otherwise we've found a match. */
685  dsm_control->item[i].refcnt++;
686  seg->control_slot = i;
688  {
689  seg->mapped_address = (char *) dsm_main_space_begin +
692  }
693  break;
694  }
695  LWLockRelease(DynamicSharedMemoryControlLock);
696 
697  /*
698  * If we didn't find the handle we're looking for in the control segment,
699  * it probably means that everyone else who had it mapped, including the
700  * original creator, died before we got to this point. It's up to the
701  * caller to decide what to do about that.
702  */
704  {
705  dsm_detach(seg);
706  return NULL;
707  }
708 
709  /* Here's where we actually try to map the segment. */
712  &seg->mapped_address, &seg->mapped_size, ERROR);
713 
714  return seg;
715 }
static void * dsm_main_space_begin
Definition: dsm.c:109
Size mapped_size
Definition: dsm.c:72
dsm_handle handle
Definition: dsm.c:79
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static void dsm_backend_startup(void)
Definition: dsm.c:395
static dlist_head dsm_segment_list
Definition: dsm.c:128
size_t first_page
Definition: dsm.c:81
uint32 nitems
Definition: dsm.c:91
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define ERROR
Definition: elog.h:43
bool IsUnderPostmaster
Definition: globals.c:109
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:93
static bool is_main_region_dsm_handle(dsm_handle handle)
Definition: dsm.c:1240
unsigned int uint32
Definition: c.h:374
void * mapped_address
Definition: dsm.c:71
dsm_handle handle
Definition: dsm.c:68
dlist_node * cur
Definition: ilist.h:161
static dsm_control_header * dsm_control
Definition: dsm.c:138
#define FPM_PAGE_SIZE
Definition: freepage.h:30
#define Assert(condition)
Definition: c.h:745
void * impl_private
Definition: dsm.c:70
size_t npages
Definition: dsm.c:82
uint32 control_slot
Definition: dsm.c:69
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
static dsm_segment * dsm_create_descriptor(void)
Definition: dsm.c:1160
uint32 refcnt
Definition: dsm.c:80
static bool dsm_init_done
Definition: dsm.c:106
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:769
#define elog(elevel,...)
Definition: elog.h:214
int i
#define INVALID_CONTROL_SLOT
Definition: dsm.c:53
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ dsm_backend_shutdown()

void dsm_backend_shutdown ( void  )

Definition at line 723 of file dsm.c.

References dlist_head_element, dlist_is_empty(), dsm_detach(), and dsm_segment_detach_callback::node.

Referenced by shmem_exit().

724 {
726  {
727  dsm_segment *seg;
728 
730  dsm_detach(seg);
731  }
732 }
static dlist_head dsm_segment_list
Definition: dsm.c:128
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:769

◆ dsm_backend_startup()

static void dsm_backend_startup ( void  )
static

Definition at line 395 of file dsm.c.

References Assert, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_control_segment_sane(), dsm_impl_op(), dsm_init_done, DSM_OP_ATTACH, DSM_OP_DETACH, ereport, errcode(), errmsg(), ERROR, FATAL, and WARNING.

Referenced by dsm_attach(), and dsm_create().

396 {
397 #ifdef EXEC_BACKEND
398  {
399  void *control_address = NULL;
400 
401  /* Attach control segment. */
404  &dsm_control_impl_private, &control_address,
406  dsm_control = control_address;
407  /* If control segment doesn't look sane, something is badly wrong. */
409  {
411  &dsm_control_impl_private, &control_address,
413  ereport(FATAL,
414  (errcode(ERRCODE_INTERNAL_ERROR),
415  errmsg("dynamic shared memory control segment is not valid")));
416  }
417  }
418 #endif
419 
420  dsm_init_done = true;
421 }
static void * dsm_control_impl_private
Definition: dsm.c:140
static dsm_handle dsm_control_handle
Definition: dsm.c:137
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size)
Definition: dsm.c:1196
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
static dsm_control_header * dsm_control
Definition: dsm.c:138
#define Assert(condition)
Definition: c.h:745
static bool dsm_init_done
Definition: dsm.c:106
int errmsg(const char *fmt,...)
Definition: elog.c:824
static Size dsm_control_mapped_size
Definition: dsm.c:139
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ dsm_cleanup_for_mmap()

static void dsm_cleanup_for_mmap ( void  )
static

Definition at line 292 of file dsm.c.

References AllocateDir(), buf, dirent::d_name, DEBUG2, elog, ereport, errcode_for_file_access(), errmsg(), ERROR, FreeDir(), MAXPGPATH, PG_DYNSHMEM_DIR, PG_DYNSHMEM_MMAP_FILE_PREFIX, ReadDir(), and snprintf.

Referenced by dsm_postmaster_startup().

293 {
294  DIR *dir;
295  struct dirent *dent;
296 
297  /* Scan the directory for something with a name of the correct format. */
299 
300  while ((dent = ReadDir(dir, PG_DYNSHMEM_DIR)) != NULL)
301  {
302  if (strncmp(dent->d_name, PG_DYNSHMEM_MMAP_FILE_PREFIX,
303  strlen(PG_DYNSHMEM_MMAP_FILE_PREFIX)) == 0)
304  {
305  char buf[MAXPGPATH + sizeof(PG_DYNSHMEM_DIR)];
306 
307  snprintf(buf, sizeof(buf), PG_DYNSHMEM_DIR "/%s", dent->d_name);
308 
309  elog(DEBUG2, "removing file \"%s\"", buf);
310 
311  /* We found a matching file; so remove it. */
312  if (unlink(buf) != 0)
313  ereport(ERROR,
315  errmsg("could not remove file \"%s\": %m", buf)));
316  }
317  }
318 
319  /* Cleanup complete. */
320  FreeDir(dir);
321 }
#define PG_DYNSHMEM_DIR
Definition: dsm_impl.h:51
Definition: dirent.h:9
Definition: dirent.c:25
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
#define DEBUG2
Definition: elog.h:24
static char * buf
Definition: pg_test_fsync.c:67
int errcode_for_file_access(void)
Definition: elog.c:633
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2583
#define ereport(elevel,...)
Definition: elog.h:144
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2649
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
#define PG_DYNSHMEM_MMAP_FILE_PREFIX
Definition: dsm_impl.h:52
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:193
int FreeDir(DIR *dir)
Definition: fd.c:2701

◆ dsm_cleanup_using_control_segment()

void dsm_cleanup_using_control_segment ( dsm_handle  old_control_handle)

Definition at line 210 of file dsm.c.

References DEBUG1, DEBUG2, dsm_control_segment_sane(), dsm_impl_op(), DSM_OP_ATTACH, DSM_OP_DESTROY, DSM_OP_DETACH, elog, dsm_control_item::handle, i, is_main_region_dsm_handle(), dsm_control_header::item, LOG, dsm_control_header::nitems, and dsm_control_item::refcnt.

Referenced by PGSharedMemoryCreate().

211 {
212  void *mapped_address = NULL;
213  void *junk_mapped_address = NULL;
214  void *impl_private = NULL;
215  void *junk_impl_private = NULL;
216  Size mapped_size = 0;
217  Size junk_mapped_size = 0;
218  uint32 nitems;
219  uint32 i;
220  dsm_control_header *old_control;
221 
222  /*
223  * Try to attach the segment. If this fails, it probably just means that
224  * the operating system has been rebooted and the segment no longer
225  * exists, or an unrelated process has used the same shm ID. So just fall
226  * out quietly.
227  */
228  if (!dsm_impl_op(DSM_OP_ATTACH, old_control_handle, 0, &impl_private,
229  &mapped_address, &mapped_size, DEBUG1))
230  return;
231 
232  /*
233  * We've managed to reattach it, but the contents might not be sane. If
234  * they aren't, we disregard the segment after all.
235  */
236  old_control = (dsm_control_header *) mapped_address;
237  if (!dsm_control_segment_sane(old_control, mapped_size))
238  {
239  dsm_impl_op(DSM_OP_DETACH, old_control_handle, 0, &impl_private,
240  &mapped_address, &mapped_size, LOG);
241  return;
242  }
243 
244  /*
245  * OK, the control segment looks basically valid, so we can use it to get
246  * a list of segments that need to be removed.
247  */
248  nitems = old_control->nitems;
249  for (i = 0; i < nitems; ++i)
250  {
251  dsm_handle handle;
252  uint32 refcnt;
253 
254  /* If the reference count is 0, the slot is actually unused. */
255  refcnt = old_control->item[i].refcnt;
256  if (refcnt == 0)
257  continue;
258 
259  /* If it was using the main shmem area, there is nothing to do. */
260  handle = old_control->item[i].handle;
261  if (is_main_region_dsm_handle(handle))
262  continue;
263 
264  /* Log debugging information. */
265  elog(DEBUG2, "cleaning up orphaned dynamic shared memory with ID %u (reference count %u)",
266  handle, refcnt);
267 
268  /* Destroy the referenced segment. */
269  dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
270  &junk_mapped_address, &junk_mapped_size, LOG);
271  }
272 
273  /* Destroy the old control segment, too. */
274  elog(DEBUG2,
275  "cleaning up dynamic shared memory control segment with ID %u",
276  old_control_handle);
277  dsm_impl_op(DSM_OP_DESTROY, old_control_handle, 0, &impl_private,
278  &mapped_address, &mapped_size, LOG);
279 }
#define DEBUG1
Definition: elog.h:25
uint32 dsm_handle
Definition: dsm_impl.h:55
dsm_handle handle
Definition: dsm.c:79
#define LOG
Definition: elog.h:26
uint32 nitems
Definition: dsm.c:91
#define DEBUG2
Definition: elog.h:24
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:93
static bool is_main_region_dsm_handle(dsm_handle handle)
Definition: dsm.c:1240
unsigned int uint32
Definition: c.h:374
static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size)
Definition: dsm.c:1196
size_t Size
Definition: c.h:473
uint32 refcnt
Definition: dsm.c:80
#define elog(elevel,...)
Definition: elog.h:214
int i
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ dsm_control_bytes_needed()

static uint64 dsm_control_bytes_needed ( uint32  nitems)
static

Definition at line 1214 of file dsm.c.

References offsetof.

Referenced by dsm_control_segment_sane(), and dsm_postmaster_startup().

1215 {
1216  return offsetof(dsm_control_header, item)
1217  + sizeof(dsm_control_item) * (uint64) nitems;
1218 }
struct dsm_control_item dsm_control_item
#define offsetof(type, field)
Definition: c.h:668

◆ dsm_control_segment_sane()

static bool dsm_control_segment_sane ( dsm_control_header control,
Size  mapped_size 
)
static

Definition at line 1196 of file dsm.c.

References dsm_control_bytes_needed(), dsm_control_header::magic, dsm_control_header::maxitems, dsm_control_header::nitems, offsetof, and PG_DYNSHMEM_CONTROL_MAGIC.

Referenced by dsm_backend_startup(), dsm_cleanup_using_control_segment(), and dsm_postmaster_shutdown().

1197 {
1198  if (mapped_size < offsetof(dsm_control_header, item))
1199  return false; /* Mapped size too short to read header. */
1200  if (control->magic != PG_DYNSHMEM_CONTROL_MAGIC)
1201  return false; /* Magic number doesn't match. */
1202  if (dsm_control_bytes_needed(control->maxitems) > mapped_size)
1203  return false; /* Max item count won't fit in map. */
1204  if (control->nitems > control->maxitems)
1205  return false; /* Overfull. */
1206  return true;
1207 }
uint32 maxitems
Definition: dsm.c:92
static uint64 dsm_control_bytes_needed(uint32 nitems)
Definition: dsm.c:1214
uint32 nitems
Definition: dsm.c:91
uint32 magic
Definition: dsm.c:90
#define PG_DYNSHMEM_CONTROL_MAGIC
Definition: dsm.c:48
#define offsetof(type, field)
Definition: c.h:668

◆ dsm_create()

dsm_segment* dsm_create ( Size  size,
int  flags 
)

Definition at line 487 of file dsm.c.

References Assert, dsm_segment::control_slot, dlist_delete(), dsm_backend_startup(), dsm_create_descriptor(), DSM_CREATE_NULL_IF_MAXSEGMENTS, DSM_HANDLE_INVALID, dsm_impl_op(), dsm_init_done, dsm_main_space_begin, DSM_OP_CREATE, DSM_OP_DESTROY, ereport, errcode(), errmsg(), ERROR, dsm_control_item::first_page, FPM_PAGE_SIZE, FreePageManagerGet(), FreePageManagerPut(), dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, dsm_control_item::impl_private_pm_handle, is_main_region_dsm_handle(), IsUnderPostmaster, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), make_main_region_dsm_handle(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_control_header::maxitems, dsm_control_header::nitems, dsm_segment::node, dsm_control_item::npages, pfree(), dsm_control_item::pinned, random(), dsm_control_item::refcnt, ResourceOwnerForgetDSM(), dsm_segment::resowner, and WARNING.

Referenced by apw_load_buffers(), dsa_create(), GetSessionDsmHandle(), InitializeParallelDSM(), make_new_segment(), and setup_dynamic_shared_memory().

488 {
489  dsm_segment *seg;
490  uint32 i;
491  uint32 nitems;
492  size_t npages = 0;
493  size_t first_page = 0;
494  FreePageManager *dsm_main_space_fpm = dsm_main_space_begin;
495  bool using_main_dsm_region = false;
496 
497  /* Unsafe in postmaster (and pointless in a stand-alone backend). */
499 
500  if (!dsm_init_done)
502 
503  /* Create a new segment descriptor. */
504  seg = dsm_create_descriptor();
505 
506  /*
507  * Lock the control segment while we try to allocate from the main shared
508  * memory area, if configured.
509  */
510  if (dsm_main_space_fpm)
511  {
512  npages = size / FPM_PAGE_SIZE;
513  if (size % FPM_PAGE_SIZE > 0)
514  ++npages;
515 
516  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
517  if (FreePageManagerGet(dsm_main_space_fpm, npages, &first_page))
518  {
519  /* We can carve out a piece of the main shared memory segment. */
520  seg->mapped_address = (char *) dsm_main_space_begin +
521  first_page * FPM_PAGE_SIZE;
522  seg->mapped_size = npages * FPM_PAGE_SIZE;
523  using_main_dsm_region = true;
524  /* We'll choose a handle below. */
525  }
526  }
527 
528  if (!using_main_dsm_region)
529  {
530  /*
531  * We need to create a new memory segment. Loop until we find an
532  * unused segment identifier.
533  */
534  if (dsm_main_space_fpm)
535  LWLockRelease(DynamicSharedMemoryControlLock);
536  for (;;)
537  {
538  Assert(seg->mapped_address == NULL && seg->mapped_size == 0);
539  seg->handle = random() << 1; /* Even numbers only */
540  if (seg->handle == DSM_HANDLE_INVALID) /* Reserve sentinel */
541  continue;
542  if (dsm_impl_op(DSM_OP_CREATE, seg->handle, size, &seg->impl_private,
543  &seg->mapped_address, &seg->mapped_size, ERROR))
544  break;
545  }
546  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
547  }
548 
549  /* Search the control segment for an unused slot. */
550  nitems = dsm_control->nitems;
551  for (i = 0; i < nitems; ++i)
552  {
553  if (dsm_control->item[i].refcnt == 0)
554  {
555  if (using_main_dsm_region)
556  {
558  dsm_control->item[i].first_page = first_page;
559  dsm_control->item[i].npages = npages;
560  }
561  else
563  dsm_control->item[i].handle = seg->handle;
564  /* refcnt of 1 triggers destruction, so start at 2 */
565  dsm_control->item[i].refcnt = 2;
567  dsm_control->item[i].pinned = false;
568  seg->control_slot = i;
569  LWLockRelease(DynamicSharedMemoryControlLock);
570  return seg;
571  }
572  }
573 
574  /* Verify that we can support an additional mapping. */
575  if (nitems >= dsm_control->maxitems)
576  {
577  if (using_main_dsm_region)
578  FreePageManagerPut(dsm_main_space_fpm, first_page, npages);
579  LWLockRelease(DynamicSharedMemoryControlLock);
580  if (!using_main_dsm_region)
582  &seg->mapped_address, &seg->mapped_size, WARNING);
583  if (seg->resowner != NULL)
584  ResourceOwnerForgetDSM(seg->resowner, seg);
585  dlist_delete(&seg->node);
586  pfree(seg);
587 
588  if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
589  return NULL;
590  ereport(ERROR,
591  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
592  errmsg("too many dynamic shared memory segments")));
593  }
594 
595  /* Enter the handle into a new array slot. */
596  if (using_main_dsm_region)
597  {
598  seg->handle = make_main_region_dsm_handle(nitems);
599  dsm_control->item[i].first_page = first_page;
600  dsm_control->item[i].npages = npages;
601  }
602  dsm_control->item[nitems].handle = seg->handle;
603  /* refcnt of 1 triggers destruction, so start at 2 */
604  dsm_control->item[nitems].refcnt = 2;
605  dsm_control->item[nitems].impl_private_pm_handle = NULL;
606  dsm_control->item[nitems].pinned = false;
607  seg->control_slot = nitems;
608  dsm_control->nitems++;
609  LWLockRelease(DynamicSharedMemoryControlLock);
610 
611  return seg;
612 }
dlist_node node
Definition: dsm.c:66
uint32 maxitems
Definition: dsm.c:92
static void * dsm_main_space_begin
Definition: dsm.c:109
Size mapped_size
Definition: dsm.c:72
dsm_handle handle
Definition: dsm.c:79
long random(void)
Definition: random.c:22
int errcode(int sqlerrcode)
Definition: elog.c:610
static void dsm_backend_startup(void)
Definition: dsm.c:395
static dsm_handle make_main_region_dsm_handle(int slot)
Definition: dsm.c:1221
size_t first_page
Definition: dsm.c:81
uint32 nitems
Definition: dsm.c:91
bool pinned
Definition: dsm.c:84
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
bool FreePageManagerGet(FreePageManager *fpm, Size npages, Size *first_page)
Definition: freepage.c:210
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
bool IsUnderPostmaster
Definition: globals.c:109
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:93
static bool is_main_region_dsm_handle(dsm_handle handle)
Definition: dsm.c:1240
unsigned int uint32
Definition: c.h:374
void * mapped_address
Definition: dsm.c:71
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define WARNING
Definition: elog.h:40
dsm_handle handle
Definition: dsm.c:68
#define ereport(elevel,...)
Definition: elog.h:144
static dsm_control_header * dsm_control
Definition: dsm.c:138
#define FPM_PAGE_SIZE
Definition: freepage.h:30
ResourceOwner resowner
Definition: dsm.c:67
#define Assert(condition)
Definition: c.h:745
void * impl_private
Definition: dsm.c:70
size_t npages
Definition: dsm.c:82
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1322
uint32 control_slot
Definition: dsm.c:69
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
static dsm_segment * dsm_create_descriptor(void)
Definition: dsm.c:1160
uint32 refcnt
Definition: dsm.c:80
static bool dsm_init_done
Definition: dsm.c:106
int errmsg(const char *fmt,...)
Definition: elog.c:824
int i
void * impl_private_pm_handle
Definition: dsm.c:83
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ dsm_create_descriptor()

static dsm_segment * dsm_create_descriptor ( void  )
static

Definition at line 1160 of file dsm.c.

References dsm_segment::control_slot, CurrentResourceOwner, dlist_push_head(), dsm_segment::impl_private, INVALID_CONTROL_SLOT, dsm_segment::mapped_address, dsm_segment::mapped_size, MemoryContextAlloc(), dsm_segment::node, dsm_segment::on_detach, ResourceOwnerEnlargeDSMs(), ResourceOwnerRememberDSM(), dsm_segment::resowner, slist_init(), and TopMemoryContext.

Referenced by dsm_attach(), and dsm_create().

1161 {
1162  dsm_segment *seg;
1163 
1166 
1169 
1170  /* seg->handle must be initialized by the caller */
1172  seg->impl_private = NULL;
1173  seg->mapped_address = NULL;
1174  seg->mapped_size = 0;
1175 
1179 
1180  slist_init(&seg->on_detach);
1181 
1182  return seg;
1183 }
dlist_node node
Definition: dsm.c:66
void ResourceOwnerRememberDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1313
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
Size mapped_size
Definition: dsm.c:72
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
static dlist_head dsm_segment_list
Definition: dsm.c:128
static void slist_init(slist_head *head)
Definition: ilist.h:554
void ResourceOwnerEnlargeDSMs(ResourceOwner owner)
Definition: resowner.c:1302
void * mapped_address
Definition: dsm.c:71
MemoryContext TopMemoryContext
Definition: mcxt.c:44
slist_head on_detach
Definition: dsm.c:73
ResourceOwner resowner
Definition: dsm.c:67
void * impl_private
Definition: dsm.c:70
uint32 control_slot
Definition: dsm.c:69
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define INVALID_CONTROL_SLOT
Definition: dsm.c:53

◆ dsm_detach()

void dsm_detach ( dsm_segment seg)

Definition at line 769 of file dsm.c.

References dsm_segment_detach_callback::arg, Assert, dsm_segment::control_slot, dlist_delete(), dsm_impl_op(), dsm_main_space_begin, DSM_OP_DESTROY, DSM_OP_DETACH, dsm_control_item::first_page, FreePageManagerPut(), dsm_segment_detach_callback::function, dsm_segment::handle, dsm_control_item::handle, dsm_segment::impl_private, INVALID_CONTROL_SLOT, is_main_region_dsm_handle(), dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_segment_detach_callback::node, dsm_segment::node, dsm_control_item::npages, dsm_segment::on_detach, pfree(), dsm_control_item::pinned, dsm_control_item::refcnt, ResourceOwnerForgetDSM(), dsm_segment::resowner, slist_container, slist_is_empty(), slist_pop_head_node(), and WARNING.

Referenced by apw_load_buffers(), autoprewarm_database_main(), check_for_freed_segments_locked(), destroy_superblock(), DestroyParallelContext(), DetachSession(), dsa_detach(), dsm_attach(), dsm_backend_shutdown(), dsm_detach_all(), ResourceOwnerReleaseInternal(), test_shm_mq(), test_shm_mq_main(), and test_shm_mq_pipelined().

770 {
771  /*
772  * Invoke registered callbacks. Just in case one of those callbacks
773  * throws a further error that brings us back here, pop the callback
774  * before invoking it, to avoid infinite error recursion.
775  */
776  while (!slist_is_empty(&seg->on_detach))
777  {
778  slist_node *node;
780  on_dsm_detach_callback function;
781  Datum arg;
782 
783  node = slist_pop_head_node(&seg->on_detach);
785  function = cb->function;
786  arg = cb->arg;
787  pfree(cb);
788 
789  function(seg, arg);
790  }
791 
792  /*
793  * Try to remove the mapping, if one exists. Normally, there will be, but
794  * maybe not, if we failed partway through a create or attach operation.
795  * We remove the mapping before decrementing the reference count so that
796  * the process that sees a zero reference count can be certain that no
797  * remaining mappings exist. Even if this fails, we pretend that it
798  * works, because retrying is likely to fail in the same way.
799  */
800  if (seg->mapped_address != NULL)
801  {
804  &seg->mapped_address, &seg->mapped_size, WARNING);
805  seg->impl_private = NULL;
806  seg->mapped_address = NULL;
807  seg->mapped_size = 0;
808  }
809 
810  /* Reduce reference count, if we previously increased it. */
812  {
813  uint32 refcnt;
814  uint32 control_slot = seg->control_slot;
815 
816  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
817  Assert(dsm_control->item[control_slot].handle == seg->handle);
818  Assert(dsm_control->item[control_slot].refcnt > 1);
819  refcnt = --dsm_control->item[control_slot].refcnt;
821  LWLockRelease(DynamicSharedMemoryControlLock);
822 
823  /* If new reference count is 1, try to destroy the segment. */
824  if (refcnt == 1)
825  {
826  /* A pinned segment should never reach 1. */
827  Assert(!dsm_control->item[control_slot].pinned);
828 
829  /*
830  * If we fail to destroy the segment here, or are killed before we
831  * finish doing so, the reference count will remain at 1, which
832  * will mean that nobody else can attach to the segment. At
833  * postmaster shutdown time, or when a new postmaster is started
834  * after a hard kill, another attempt will be made to remove the
835  * segment.
836  *
837  * The main case we're worried about here is being killed by a
838  * signal before we can finish removing the segment. In that
839  * case, it's important to be sure that the segment still gets
840  * removed. If we actually fail to remove the segment for some
841  * other reason, the postmaster may not have any better luck than
842  * we did. There's not much we can do about that, though.
843  */
844  if (is_main_region_dsm_handle(seg->handle) ||
846  &seg->mapped_address, &seg->mapped_size, WARNING))
847  {
848  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
851  dsm_control->item[control_slot].first_page,
852  dsm_control->item[control_slot].npages);
853  Assert(dsm_control->item[control_slot].handle == seg->handle);
854  Assert(dsm_control->item[control_slot].refcnt == 1);
855  dsm_control->item[control_slot].refcnt = 0;
856  LWLockRelease(DynamicSharedMemoryControlLock);
857  }
858  }
859  }
860 
861  /* Clean up our remaining backend-private data structures. */
862  if (seg->resowner != NULL)
863  ResourceOwnerForgetDSM(seg->resowner, seg);
864  dlist_delete(&seg->node);
865  pfree(seg);
866 }
dlist_node node
Definition: dsm.c:66
static void * dsm_main_space_begin
Definition: dsm.c:109
Size mapped_size
Definition: dsm.c:72
dsm_handle handle
Definition: dsm.c:79
size_t first_page
Definition: dsm.c:81
bool pinned
Definition: dsm.c:84
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
void pfree(void *pointer)
Definition: mcxt.c:1057
on_dsm_detach_callback function
Definition: dsm.c:58
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:93
static bool is_main_region_dsm_handle(dsm_handle handle)
Definition: dsm.c:1240
unsigned int uint32
Definition: c.h:374
void * mapped_address
Definition: dsm.c:71
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
static slist_node * slist_pop_head_node(slist_head *head)
Definition: ilist.h:596
slist_head on_detach
Definition: dsm.c:73
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:563
#define WARNING
Definition: elog.h:40
dsm_handle handle
Definition: dsm.c:68
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
uintptr_t Datum
Definition: postgres.h:367
static dsm_control_header * dsm_control
Definition: dsm.c:138
ResourceOwner resowner
Definition: dsm.c:67
#define Assert(condition)
Definition: c.h:745
void * impl_private
Definition: dsm.c:70
size_t npages
Definition: dsm.c:82
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1322
uint32 control_slot
Definition: dsm.c:69
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
uint32 refcnt
Definition: dsm.c:80
void * arg
#define INVALID_CONTROL_SLOT
Definition: dsm.c:53
void(* on_dsm_detach_callback)(dsm_segment *, Datum arg)
Definition: dsm.h:57
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ dsm_detach_all()

void dsm_detach_all ( void  )

Definition at line 741 of file dsm.c.

References dlist_head_element, dlist_is_empty(), dsm_control, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_detach(), dsm_impl_op(), DSM_OP_DETACH, ERROR, and dsm_segment_detach_callback::node.

Referenced by pgarch_start(), pgstat_start(), StartBackgroundWorker(), and SysLogger_Start().

742 {
743  void *control_address = dsm_control;
744 
746  {
747  dsm_segment *seg;
748 
750  dsm_detach(seg);
751  }
752 
753  if (control_address != NULL)
755  &dsm_control_impl_private, &control_address,
757 }
static void * dsm_control_impl_private
Definition: dsm.c:140
static dsm_handle dsm_control_handle
Definition: dsm.c:137
static dlist_head dsm_segment_list
Definition: dsm.c:128
#define ERROR
Definition: elog.h:43
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:487
static dsm_control_header * dsm_control
Definition: dsm.c:138
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:769
static Size dsm_control_mapped_size
Definition: dsm.c:139
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ dsm_estimate_size()

size_t dsm_estimate_size ( void  )

Definition at line 441 of file dsm.c.

References min_dynamic_shared_memory.

Referenced by CreateSharedMemoryAndSemaphores(), and dsm_shmem_init().

442 {
443  return 1024 * 1024 * (size_t) min_dynamic_shared_memory;
444 }
int min_dynamic_shared_memory
Definition: dsm_impl.c:117

◆ dsm_find_mapping()

dsm_segment* dsm_find_mapping ( dsm_handle  h)

Definition at line 1035 of file dsm.c.

References dlist_iter::cur, dlist_container, dlist_foreach, dsm_segment::handle, and dsm_segment_detach_callback::node.

1036 {
1037  dlist_iter iter;
1038  dsm_segment *seg;
1039 
1041  {
1042  seg = dlist_container(dsm_segment, node, iter.cur);
1043  if (seg->handle == h)
1044  return seg;
1045  }
1046 
1047  return NULL;
1048 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static dlist_head dsm_segment_list
Definition: dsm.c:128
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
dsm_handle handle
Definition: dsm.c:68
dlist_node * cur
Definition: ilist.h:161

◆ dsm_pin_mapping()

void dsm_pin_mapping ( dsm_segment seg)

Definition at line 876 of file dsm.c.

References ResourceOwnerForgetDSM(), and dsm_segment::resowner.

Referenced by AttachSession(), dsa_pin_mapping(), get_segment_by_index(), GetSessionDsmHandle(), and make_new_segment().

877 {
878  if (seg->resowner != NULL)
879  {
880  ResourceOwnerForgetDSM(seg->resowner, seg);
881  seg->resowner = NULL;
882  }
883 }
ResourceOwner resowner
Definition: dsm.c:67
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1322

◆ dsm_pin_segment()

void dsm_pin_segment ( dsm_segment seg)

Definition at line 916 of file dsm.c.

References dsm_segment::control_slot, dsm_impl_pin_segment(), elog, ERROR, dsm_segment::handle, dsm_segment::impl_private, dsm_control_item::impl_private_pm_handle, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_control_item::pinned, and dsm_control_item::refcnt.

Referenced by dsa_create(), and make_new_segment().

917 {
918  void *handle;
919 
920  /*
921  * Bump reference count for this segment in shared memory. This will
922  * ensure that even if there is no session which is attached to this
923  * segment, it will remain until postmaster shutdown or an explicit call
924  * to unpin.
925  */
926  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
927  if (dsm_control->item[seg->control_slot].pinned)
928  elog(ERROR, "cannot pin a segment that is already pinned");
929  dsm_impl_pin_segment(seg->handle, seg->impl_private, &handle);
930  dsm_control->item[seg->control_slot].pinned = true;
933  LWLockRelease(DynamicSharedMemoryControlLock);
934 }
bool pinned
Definition: dsm.c:84
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define ERROR
Definition: elog.h:43
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:93
dsm_handle handle
Definition: dsm.c:68
static dsm_control_header * dsm_control
Definition: dsm.c:138
void * impl_private
Definition: dsm.c:70
uint32 control_slot
Definition: dsm.c:69
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
uint32 refcnt
Definition: dsm.c:80
void dsm_impl_pin_segment(dsm_handle handle, void *impl_private, void **impl_private_pm_handle)
Definition: dsm_impl.c:955
#define elog(elevel,...)
Definition: elog.h:214
void * impl_private_pm_handle
Definition: dsm.c:83

◆ dsm_postmaster_shutdown()

static void dsm_postmaster_shutdown ( int  code,
Datum  arg 
)
static

Definition at line 330 of file dsm.c.

References DatumGetPointer, DEBUG2, PGShmemHeader::dsm_control, dsm_control, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_control_segment_sane(), dsm_impl_op(), DSM_OP_DESTROY, elog, ereport, errmsg(), dsm_control_item::handle, i, is_main_region_dsm_handle(), dsm_control_header::item, LOG, dsm_control_header::nitems, and dsm_control_item::refcnt.

Referenced by dsm_postmaster_startup().

331 {
332  uint32 nitems;
333  uint32 i;
334  void *dsm_control_address;
335  void *junk_mapped_address = NULL;
336  void *junk_impl_private = NULL;
337  Size junk_mapped_size = 0;
339 
340  /*
341  * If some other backend exited uncleanly, it might have corrupted the
342  * control segment while it was dying. In that case, we warn and ignore
343  * the contents of the control segment. This may end up leaving behind
344  * stray shared memory segments, but there's not much we can do about that
345  * if the metadata is gone.
346  */
347  nitems = dsm_control->nitems;
349  {
350  ereport(LOG,
351  (errmsg("dynamic shared memory control segment is corrupt")));
352  return;
353  }
354 
355  /* Remove any remaining segments. */
356  for (i = 0; i < nitems; ++i)
357  {
358  dsm_handle handle;
359 
360  /* If the reference count is 0, the slot is actually unused. */
361  if (dsm_control->item[i].refcnt == 0)
362  continue;
363 
364  handle = dsm_control->item[i].handle;
365  if (is_main_region_dsm_handle(handle))
366  continue;
367 
368  /* Log debugging information. */
369  elog(DEBUG2, "cleaning up orphaned dynamic shared memory with ID %u",
370  handle);
371 
372  /* Destroy the segment. */
373  dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
374  &junk_mapped_address, &junk_mapped_size, LOG);
375  }
376 
377  /* Remove the control segment itself. */
378  elog(DEBUG2,
379  "cleaning up dynamic shared memory control segment with ID %u",
381  dsm_control_address = dsm_control;
383  &dsm_control_impl_private, &dsm_control_address,
385  dsm_control = dsm_control_address;
386  shim->dsm_control = 0;
387 }
static void * dsm_control_impl_private
Definition: dsm.c:140
uint32 dsm_handle
Definition: dsm_impl.h:55
dsm_handle dsm_control
Definition: pg_shmem.h:36
dsm_handle handle
Definition: dsm.c:79
static dsm_handle dsm_control_handle
Definition: dsm.c:137
#define LOG
Definition: elog.h:26
uint32 nitems
Definition: dsm.c:91
#define DEBUG2
Definition: elog.h:24
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:93
static bool is_main_region_dsm_handle(dsm_handle handle)
Definition: dsm.c:1240
unsigned int uint32
Definition: c.h:374
static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size)
Definition: dsm.c:1196
#define ereport(elevel,...)
Definition: elog.h:144
static dsm_control_header * dsm_control
Definition: dsm.c:138
size_t Size
Definition: c.h:473
#define DatumGetPointer(X)
Definition: postgres.h:549
uint32 refcnt
Definition: dsm.c:80
int errmsg(const char *fmt,...)
Definition: elog.c:824
static Size dsm_control_mapped_size
Definition: dsm.c:139
#define elog(elevel,...)
Definition: elog.h:214
int i
void * arg
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ dsm_postmaster_startup()

void dsm_postmaster_startup ( PGShmemHeader shim)

Definition at line 149 of file dsm.c.

References Assert, DEBUG2, dsm_cleanup_for_mmap(), PGShmemHeader::dsm_control, dsm_control_bytes_needed(), dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, DSM_HANDLE_INVALID, DSM_IMPL_MMAP, dsm_impl_op(), DSM_OP_CREATE, dsm_postmaster_shutdown(), dynamic_shared_memory_type, elog, ERROR, IsUnderPostmaster, dsm_control_header::magic, MaxBackends, dsm_control_header::maxitems, dsm_control_header::nitems, on_shmem_exit(), PG_DYNSHMEM_CONTROL_MAGIC, PG_DYNSHMEM_FIXED_SLOTS, PG_DYNSHMEM_SLOTS_PER_BACKEND, PointerGetDatum, and random().

Referenced by CreateSharedMemoryAndSemaphores().

150 {
151  void *dsm_control_address = NULL;
152  uint32 maxitems;
153  Size segsize;
154 
156 
157  /*
158  * If we're using the mmap implementations, clean up any leftovers.
159  * Cleanup isn't needed on Windows, and happens earlier in startup for
160  * POSIX and System V shared memory, via a direct call to
161  * dsm_cleanup_using_control_segment.
162  */
165 
166  /* Determine size for new control segment. */
167  maxitems = PG_DYNSHMEM_FIXED_SLOTS
169  elog(DEBUG2, "dynamic shared memory system will support %u segments",
170  maxitems);
171  segsize = dsm_control_bytes_needed(maxitems);
172 
173  /*
174  * Loop until we find an unused identifier for the new control segment. We
175  * sometimes use 0 as a sentinel value indicating that no control segment
176  * is known to exist, so avoid using that value for a real control
177  * segment.
178  */
179  for (;;)
180  {
181  Assert(dsm_control_address == NULL);
183  dsm_control_handle = random() << 1; /* Even numbers only */
185  continue;
187  &dsm_control_impl_private, &dsm_control_address,
189  break;
190  }
191  dsm_control = dsm_control_address;
193  elog(DEBUG2,
194  "created dynamic shared memory control segment %u (%zu bytes)",
195  dsm_control_handle, segsize);
197 
198  /* Initialize control segment. */
200  dsm_control->nitems = 0;
201  dsm_control->maxitems = maxitems;
202 }
static void * dsm_control_impl_private
Definition: dsm.c:140
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:20
uint32 maxitems
Definition: dsm.c:92
#define PG_DYNSHMEM_SLOTS_PER_BACKEND
Definition: dsm.c:51
#define PG_DYNSHMEM_FIXED_SLOTS
Definition: dsm.c:50
dsm_handle dsm_control
Definition: pg_shmem.h:36
#define PointerGetDatum(X)
Definition: postgres.h:556
long random(void)
Definition: random.c:22
static dsm_handle dsm_control_handle
Definition: dsm.c:137
static uint64 dsm_control_bytes_needed(uint32 nitems)
Definition: dsm.c:1214
uint32 nitems
Definition: dsm.c:91
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define ERROR
Definition: elog.h:43
uint32 magic
Definition: dsm.c:90
int MaxBackends
Definition: globals.c:136
#define DEBUG2
Definition: elog.h:24
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
bool IsUnderPostmaster
Definition: globals.c:109
static void dsm_postmaster_shutdown(int code, Datum arg)
Definition: dsm.c:330
int dynamic_shared_memory_type
Definition: dsm_impl.c:114
unsigned int uint32
Definition: c.h:374
static void dsm_cleanup_for_mmap(void)
Definition: dsm.c:292
static dsm_control_header * dsm_control
Definition: dsm.c:138
#define Assert(condition)
Definition: c.h:745
size_t Size
Definition: c.h:473
static Size dsm_control_mapped_size
Definition: dsm.c:139
#define elog(elevel,...)
Definition: elog.h:214
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161
#define PG_DYNSHMEM_CONTROL_MAGIC
Definition: dsm.c:48

◆ dsm_segment_address()

void* dsm_segment_address ( dsm_segment seg)

◆ dsm_segment_handle()

dsm_handle dsm_segment_handle ( dsm_segment seg)

◆ dsm_segment_map_length()

Size dsm_segment_map_length ( dsm_segment seg)

Definition at line 1064 of file dsm.c.

References Assert, dsm_segment::mapped_address, and dsm_segment::mapped_size.

1065 {
1066  Assert(seg->mapped_address != NULL);
1067  return seg->mapped_size;
1068 }
Size mapped_size
Definition: dsm.c:72
void * mapped_address
Definition: dsm.c:71
#define Assert(condition)
Definition: c.h:745

◆ dsm_shmem_init()

void dsm_shmem_init ( void  )

Definition at line 450 of file dsm.c.

References dsm_estimate_size(), dsm_main_space_begin, FPM_PAGE_SIZE, FreePageManagerInitialize(), FreePageManagerPut(), and ShmemInitStruct().

Referenced by CreateSharedMemoryAndSemaphores().

451 {
452  size_t size = dsm_estimate_size();
453  bool found;
454 
455  if (size == 0)
456  return;
457 
458  dsm_main_space_begin = ShmemInitStruct("Preallocated DSM", size, &found);
459  if (!found)
460  {
462  size_t first_page = 0;
463  size_t pages;
464 
465  /* Reserve space for the FreePageManager. */
466  while (first_page * FPM_PAGE_SIZE < sizeof(FreePageManager))
467  ++first_page;
468 
469  /* Initialize it and give it all the rest of the space. */
471  pages = (size / FPM_PAGE_SIZE) - first_page;
472  FreePageManagerPut(fpm, first_page, pages);
473  }
474 }
static void * dsm_main_space_begin
Definition: dsm.c:109
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
size_t dsm_estimate_size(void)
Definition: dsm.c:441
void FreePageManagerInitialize(FreePageManager *fpm, char *base)
Definition: freepage.c:183
#define FPM_PAGE_SIZE
Definition: freepage.h:30

◆ dsm_unpin_mapping()

void dsm_unpin_mapping ( dsm_segment seg)

Definition at line 895 of file dsm.c.

References Assert, CurrentResourceOwner, ResourceOwnerEnlargeDSMs(), ResourceOwnerRememberDSM(), and dsm_segment::resowner.

896 {
897  Assert(seg->resowner == NULL);
901 }
void ResourceOwnerRememberDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1313
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ResourceOwnerEnlargeDSMs(ResourceOwner owner)
Definition: resowner.c:1302
ResourceOwner resowner
Definition: dsm.c:67
#define Assert(condition)
Definition: c.h:745

◆ dsm_unpin_segment()

void dsm_unpin_segment ( dsm_handle  handle)

Definition at line 948 of file dsm.c.

References Assert, dsm_impl_op(), dsm_impl_unpin_segment(), dsm_main_space_begin, DSM_OP_DESTROY, elog, ERROR, dsm_control_item::first_page, FreePageManagerPut(), dsm_control_item::handle, i, dsm_control_item::impl_private_pm_handle, INVALID_CONTROL_SLOT, is_main_region_dsm_handle(), dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_control_header::nitems, dsm_control_item::npages, dsm_control_item::pinned, dsm_control_item::refcnt, and WARNING.

Referenced by destroy_superblock(), and dsa_release_in_place().

949 {
950  uint32 control_slot = INVALID_CONTROL_SLOT;
951  bool destroy = false;
952  uint32 i;
953 
954  /* Find the control slot for the given handle. */
955  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
956  for (i = 0; i < dsm_control->nitems; ++i)
957  {
958  /* Skip unused slots and segments that are concurrently going away. */
959  if (dsm_control->item[i].refcnt <= 1)
960  continue;
961 
962  /* If we've found our handle, we can stop searching. */
963  if (dsm_control->item[i].handle == handle)
964  {
965  control_slot = i;
966  break;
967  }
968  }
969 
970  /*
971  * We should definitely have found the slot, and it should not already be
972  * in the process of going away, because this function should only be
973  * called on a segment which is pinned.
974  */
975  if (control_slot == INVALID_CONTROL_SLOT)
976  elog(ERROR, "cannot unpin unknown segment handle");
977  if (!dsm_control->item[control_slot].pinned)
978  elog(ERROR, "cannot unpin a segment that is not pinned");
979  Assert(dsm_control->item[control_slot].refcnt > 1);
980 
981  /*
982  * Allow implementation-specific code to run. We have to do this before
983  * releasing the lock, because impl_private_pm_handle may get modified by
984  * dsm_impl_unpin_segment.
985  */
986  dsm_impl_unpin_segment(handle,
987  &dsm_control->item[control_slot].impl_private_pm_handle);
988 
989  /* Note that 1 means no references (0 means unused slot). */
990  if (--dsm_control->item[control_slot].refcnt == 1)
991  destroy = true;
992  dsm_control->item[control_slot].pinned = false;
993 
994  /* Now we can release the lock. */
995  LWLockRelease(DynamicSharedMemoryControlLock);
996 
997  /* Clean up resources if that was the last reference. */
998  if (destroy)
999  {
1000  void *junk_impl_private = NULL;
1001  void *junk_mapped_address = NULL;
1002  Size junk_mapped_size = 0;
1003 
1004  /*
1005  * For an explanation of how error handling works in this case, see
1006  * comments in dsm_detach. Note that if we reach this point, the
1007  * current process certainly does not have the segment mapped, because
1008  * if it did, the reference count would have still been greater than 1
1009  * even after releasing the reference count held by the pin. The fact
1010  * that there can't be a dsm_segment for this handle makes it OK to
1011  * pass the mapped size, mapped address, and private data as NULL
1012  * here.
1013  */
1014  if (is_main_region_dsm_handle(handle) ||
1015  dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
1016  &junk_mapped_address, &junk_mapped_size, WARNING))
1017  {
1018  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
1019  if (is_main_region_dsm_handle(handle))
1021  dsm_control->item[control_slot].first_page,
1022  dsm_control->item[control_slot].npages);
1023  Assert(dsm_control->item[control_slot].handle == handle);
1024  Assert(dsm_control->item[control_slot].refcnt == 1);
1025  dsm_control->item[control_slot].refcnt = 0;
1026  LWLockRelease(DynamicSharedMemoryControlLock);
1027  }
1028  }
1029 }
void dsm_impl_unpin_segment(dsm_handle handle, void **impl_private)
Definition: dsm_impl.c:1005
static void * dsm_main_space_begin
Definition: dsm.c:109
dsm_handle handle
Definition: dsm.c:79
size_t first_page
Definition: dsm.c:81
uint32 nitems
Definition: dsm.c:91
bool pinned
Definition: dsm.c:84
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define ERROR
Definition: elog.h:43
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:93
static bool is_main_region_dsm_handle(dsm_handle handle)
Definition: dsm.c:1240
unsigned int uint32
Definition: c.h:374
#define WARNING
Definition: elog.h:40
static dsm_control_header * dsm_control
Definition: dsm.c:138
#define Assert(condition)
Definition: c.h:745
size_t npages
Definition: dsm.c:82
size_t Size
Definition: c.h:473
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
uint32 refcnt
Definition: dsm.c:80
#define elog(elevel,...)
Definition: elog.h:214
int i
void * impl_private_pm_handle
Definition: dsm.c:83
#define INVALID_CONTROL_SLOT
Definition: dsm.c:53
bool dsm_impl_op(dsm_op op, dsm_handle handle, Size request_size, void **impl_private, void **mapped_address, Size *mapped_size, int elevel)
Definition: dsm_impl.c:161

◆ is_main_region_dsm_handle()

static bool is_main_region_dsm_handle ( dsm_handle  handle)
inlinestatic

Definition at line 1240 of file dsm.c.

Referenced by dsm_attach(), dsm_cleanup_using_control_segment(), dsm_create(), dsm_detach(), dsm_postmaster_shutdown(), and dsm_unpin_segment().

1241 {
1242  return handle & 1;
1243 }

◆ make_main_region_dsm_handle()

static dsm_handle make_main_region_dsm_handle ( int  slot)
inlinestatic

Definition at line 1221 of file dsm.c.

References dsm_control_header::maxitems, pg_leftmost_one_pos32(), and random().

Referenced by dsm_create().

1222 {
1223  dsm_handle handle;
1224 
1225  /*
1226  * We need to create a handle that doesn't collide with any existing extra
1227  * segment created by dsm_impl_op(), so we'll make it odd. It also
1228  * mustn't collide with any other main area pseudo-segment, so we'll
1229  * include the slot number in some of the bits. We also want to make an
1230  * effort to avoid newly created and recently destroyed handles from being
1231  * confused, so we'll make the rest of the bits random.
1232  */
1233  handle = 1;
1234  handle |= slot << 1;
1235  handle |= random() << (pg_leftmost_one_pos32(dsm_control->maxitems) + 1);
1236  return handle;
1237 }
uint32 maxitems
Definition: dsm.c:92
uint32 dsm_handle
Definition: dsm_impl.h:55
long random(void)
Definition: random.c:22
static int pg_leftmost_one_pos32(uint32 word)
Definition: pg_bitutils.h:32
static dsm_control_header * dsm_control
Definition: dsm.c:138

◆ on_dsm_detach()

void on_dsm_detach ( dsm_segment seg,
on_dsm_detach_callback  function,
Datum  arg 
)

Definition at line 1091 of file dsm.c.

References dsm_segment_detach_callback::arg, dsm_segment_detach_callback::function, MemoryContextAlloc(), dsm_segment_detach_callback::node, dsm_segment::on_detach, slist_push_head(), and TopMemoryContext.

Referenced by dsa_attach(), dsa_attach_in_place(), dsa_create(), dsa_create_in_place(), pq_redirect_to_shm_mq(), setup_background_workers(), SharedFileSetAttach(), SharedFileSetInit(), SharedRecordTypmodRegistryAttach(), SharedRecordTypmodRegistryInit(), and shm_mq_attach().

1092 {
1094 
1096  sizeof(dsm_segment_detach_callback));
1097  cb->function = function;
1098  cb->arg = arg;
1099  slist_push_head(&seg->on_detach, &cb->node);
1100 }
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
on_dsm_detach_callback function
Definition: dsm.c:58
MemoryContext TopMemoryContext
Definition: mcxt.c:44
slist_head on_detach
Definition: dsm.c:73
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
void * arg

◆ reset_on_dsm_detach()

void reset_on_dsm_detach ( void  )

Definition at line 1129 of file dsm.c.

References dsm_segment::control_slot, dlist_iter::cur, dlist_container, dlist_foreach, INVALID_CONTROL_SLOT, dsm_segment_detach_callback::node, dsm_segment::on_detach, pfree(), slist_container, slist_is_empty(), and slist_pop_head_node().

Referenced by on_exit_reset().

1130 {
1131  dlist_iter iter;
1132 
1134  {
1135  dsm_segment *seg = dlist_container(dsm_segment, node, iter.cur);
1136 
1137  /* Throw away explicit on-detach actions one by one. */
1138  while (!slist_is_empty(&seg->on_detach))
1139  {
1140  slist_node *node;
1142 
1143  node = slist_pop_head_node(&seg->on_detach);
1144  cb = slist_container(dsm_segment_detach_callback, node, node);
1145  pfree(cb);
1146  }
1147 
1148  /*
1149  * Decrementing the reference count is a sort of implicit on-detach
1150  * action; make sure we don't do that, either.
1151  */
1153  }
1154 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static dlist_head dsm_segment_list
Definition: dsm.c:128
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
void pfree(void *pointer)
Definition: mcxt.c:1057
static slist_node * slist_pop_head_node(slist_head *head)
Definition: ilist.h:596
slist_head on_detach
Definition: dsm.c:73
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:563
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
dlist_node * cur
Definition: ilist.h:161
uint32 control_slot
Definition: dsm.c:69
#define INVALID_CONTROL_SLOT
Definition: dsm.c:53

Variable Documentation

◆ dsm_control

dsm_control_header* dsm_control
static

Definition at line 138 of file dsm.c.

Referenced by dsm_detach_all(), and dsm_postmaster_shutdown().

◆ dsm_control_handle

dsm_handle dsm_control_handle
static

◆ dsm_control_impl_private

void* dsm_control_impl_private = NULL
static

◆ dsm_control_mapped_size

Size dsm_control_mapped_size = 0
static

◆ dsm_init_done

bool dsm_init_done = false
static

Definition at line 106 of file dsm.c.

Referenced by dsm_attach(), dsm_backend_startup(), and dsm_create().

◆ dsm_main_space_begin

void* dsm_main_space_begin = NULL
static

Definition at line 109 of file dsm.c.

Referenced by dsm_attach(), dsm_create(), dsm_detach(), dsm_shmem_init(), and dsm_unpin_segment().

◆ dsm_segment_list

dlist_head dsm_segment_list = DLIST_STATIC_INIT(dsm_segment_list)
static

Definition at line 128 of file dsm.c.