PostgreSQL Source Code  git master
dsm.h File Reference
#include "storage/dsm_impl.h"
Include dependency graph for dsm.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DSM_CREATE_NULL_IF_MAXSEGMENTS   0x0001
 
#define DSM_HANDLE_INVALID   0
 

Typedefs

typedef struct dsm_segment dsm_segment
 
typedef void(* on_dsm_detach_callback) (dsm_segment *, Datum arg)
 

Functions

void dsm_cleanup_using_control_segment (dsm_handle old_control_handle)
 
void dsm_postmaster_startup (struct PGShmemHeader *)
 
void dsm_backend_shutdown (void)
 
void dsm_detach_all (void)
 
dsm_segmentdsm_create (Size size, int flags)
 
dsm_segmentdsm_attach (dsm_handle h)
 
void * dsm_resize (dsm_segment *seg, Size size)
 
void * dsm_remap (dsm_segment *seg)
 
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 h)
 
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)
 

Macro Definition Documentation

◆ DSM_CREATE_NULL_IF_MAXSEGMENTS

#define DSM_CREATE_NULL_IF_MAXSEGMENTS   0x0001

Definition at line 20 of file dsm.h.

Referenced by dsm_create(), GetSessionDsmHandle(), and InitializeParallelDSM().

◆ DSM_HANDLE_INVALID

Typedef Documentation

◆ dsm_segment

Definition at line 18 of file dsm.h.

◆ on_dsm_detach_callback

typedef void(* on_dsm_detach_callback) (dsm_segment *, Datum arg)

Definition at line 56 of file dsm.h.

Function Documentation

◆ cancel_on_dsm_detach()

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

Definition at line 1039 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().

1041 {
1042  slist_mutable_iter iter;
1043 
1044  slist_foreach_modify(iter, &seg->on_detach)
1045  {
1047 
1049  if (cb->function == function && cb->arg == arg)
1050  {
1051  slist_delete_current(&iter);
1052  pfree(cb);
1053  break;
1054  }
1055  }
1056 }
slist_node * cur
Definition: ilist.h:241
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:716
void pfree(void *pointer)
Definition: mcxt.c:936
on_dsm_detach_callback function
Definition: dsm.c:61
slist_head on_detach
Definition: dsm.c:76
#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 548 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_OP_ATTACH, elog, ERROR, dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, INVALID_CONTROL_SLOT, 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, and dsm_control_item::refcnt.

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

549 {
550  dsm_segment *seg;
551  dlist_iter iter;
552  uint32 i;
553  uint32 nitems;
554 
555  /* Unsafe in postmaster (and pointless in a stand-alone backend). */
557 
558  if (!dsm_init_done)
560 
561  /*
562  * Since this is just a debugging cross-check, we could leave it out
563  * altogether, or include it only in assert-enabled builds. But since the
564  * list of attached segments should normally be very short, let's include
565  * it always for right now.
566  *
567  * If you're hitting this error, you probably want to attempt to find an
568  * existing mapping via dsm_find_mapping() before calling dsm_attach() to
569  * create a new one.
570  */
572  {
573  seg = dlist_container(dsm_segment, node, iter.cur);
574  if (seg->handle == h)
575  elog(ERROR, "can't attach the same segment more than once");
576  }
577 
578  /* Create a new segment descriptor. */
579  seg = dsm_create_descriptor();
580  seg->handle = h;
581 
582  /* Bump reference count for this segment in shared memory. */
583  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
584  nitems = dsm_control->nitems;
585  for (i = 0; i < nitems; ++i)
586  {
587  /* If the reference count is 0, the slot is actually unused. */
588  if (dsm_control->item[i].refcnt == 0)
589  continue;
590 
591  /* If the handle doesn't match, it's not the slot we want. */
592  if (dsm_control->item[i].handle != seg->handle)
593  continue;
594 
595  /*
596  * If the reference count is 1, the slot is still in use, but the
597  * segment is in the process of going away. Treat that as if we
598  * didn't find a match.
599  */
600  if (dsm_control->item[i].refcnt == 1)
601  break;
602 
603  /* Otherwise we've found a match. */
604  dsm_control->item[i].refcnt++;
605  seg->control_slot = i;
606  break;
607  }
608  LWLockRelease(DynamicSharedMemoryControlLock);
609 
610  /*
611  * If we didn't find the handle we're looking for in the control segment,
612  * it probably means that everyone else who had it mapped, including the
613  * original creator, died before we got to this point. It's up to the
614  * caller to decide what to do about that.
615  */
617  {
618  dsm_detach(seg);
619  return NULL;
620  }
621 
622  /* Here's where we actually try to map the segment. */
624  &seg->mapped_address, &seg->mapped_size, ERROR);
625 
626  return seg;
627 }
Size mapped_size
Definition: dsm.c:75
dsm_handle handle
Definition: dsm.c:82
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static void dsm_backend_startup(void)
Definition: dsm.c:392
static dlist_head dsm_segment_list
Definition: dsm.c:124
uint32 nitems
Definition: dsm.c:92
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1724
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
#define ERROR
Definition: elog.h:43
bool IsUnderPostmaster
Definition: globals.c:101
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:94
unsigned int uint32
Definition: c.h:306
void * mapped_address
Definition: dsm.c:74
dsm_handle handle
Definition: dsm.c:71
dlist_node * cur
Definition: ilist.h:161
static dsm_control_header * dsm_control
Definition: dsm.c:134
#define Assert(condition)
Definition: c.h:680
void * impl_private
Definition: dsm.c:73
uint32 control_slot
Definition: dsm.c:72
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1120
static dsm_segment * dsm_create_descriptor(void)
Definition: dsm.c:1093
uint32 refcnt
Definition: dsm.c:83
static bool dsm_init_done
Definition: dsm.c:105
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:713
int i
#define INVALID_CONTROL_SLOT
Definition: dsm.c:56
#define elog
Definition: elog.h:219
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:163

◆ dsm_backend_shutdown()

void dsm_backend_shutdown ( void  )

Definition at line 635 of file dsm.c.

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

Referenced by shmem_exit().

636 {
638  {
639  dsm_segment *seg;
640 
642  dsm_detach(seg);
643  }
644 }
static dlist_head dsm_segment_list
Definition: dsm.c:124
#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:713

◆ 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_NONE, dsm_impl_op(), DSM_OP_ATTACH, DSM_OP_DESTROY, DSM_OP_DETACH, dynamic_shared_memory_type, elog, dsm_control_item::handle, i, 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  /* If dynamic shared memory is disabled, there's nothing to do. */
224  return;
225 
226  /*
227  * Try to attach the segment. If this fails, it probably just means that
228  * the operating system has been rebooted and the segment no longer
229  * exists, or an unrelated process has used the same shm ID. So just fall
230  * out quietly.
231  */
232  if (!dsm_impl_op(DSM_OP_ATTACH, old_control_handle, 0, &impl_private,
233  &mapped_address, &mapped_size, DEBUG1))
234  return;
235 
236  /*
237  * We've managed to reattach it, but the contents might not be sane. If
238  * they aren't, we disregard the segment after all.
239  */
240  old_control = (dsm_control_header *) mapped_address;
241  if (!dsm_control_segment_sane(old_control, mapped_size))
242  {
243  dsm_impl_op(DSM_OP_DETACH, old_control_handle, 0, &impl_private,
244  &mapped_address, &mapped_size, LOG);
245  return;
246  }
247 
248  /*
249  * OK, the control segment looks basically valid, so we can use it to get
250  * a list of segments that need to be removed.
251  */
252  nitems = old_control->nitems;
253  for (i = 0; i < nitems; ++i)
254  {
255  dsm_handle handle;
256  uint32 refcnt;
257 
258  /* If the reference count is 0, the slot is actually unused. */
259  refcnt = old_control->item[i].refcnt;
260  if (refcnt == 0)
261  continue;
262 
263  /* Log debugging information. */
264  handle = old_control->item[i].handle;
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:82
#define LOG
Definition: elog.h:26
uint32 nitems
Definition: dsm.c:92
#define DEBUG2
Definition: elog.h:24
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:94
int dynamic_shared_memory_type
Definition: dsm_impl.c:113
unsigned int uint32
Definition: c.h:306
static bool dsm_control_segment_sane(dsm_control_header *control, Size mapped_size)
Definition: dsm.c:1129
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
size_t Size
Definition: c.h:414
uint32 refcnt
Definition: dsm.c:83
int i
#define elog
Definition: elog.h:219
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:163

◆ dsm_create()

dsm_segment* dsm_create ( Size  size,
int  flags 
)

Definition at line 452 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_OP_CREATE, DSM_OP_DESTROY, ereport, errcode(), errmsg(), ERROR, dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, dsm_control_item::impl_private_pm_handle, IsUnderPostmaster, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_control_header::maxitems, dsm_control_header::nitems, dsm_segment::node, 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().

453 {
454  dsm_segment *seg;
455  uint32 i;
456  uint32 nitems;
457 
458  /* Unsafe in postmaster (and pointless in a stand-alone backend). */
460 
461  if (!dsm_init_done)
463 
464  /* Create a new segment descriptor. */
465  seg = dsm_create_descriptor();
466 
467  /* Loop until we find an unused segment identifier. */
468  for (;;)
469  {
470  Assert(seg->mapped_address == NULL && seg->mapped_size == 0);
471  seg->handle = random();
472  if (seg->handle == DSM_HANDLE_INVALID) /* Reserve sentinel */
473  continue;
474  if (dsm_impl_op(DSM_OP_CREATE, seg->handle, size, &seg->impl_private,
475  &seg->mapped_address, &seg->mapped_size, ERROR))
476  break;
477  }
478 
479  /* Lock the control segment so we can register the new segment. */
480  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
481 
482  /* Search the control segment for an unused slot. */
483  nitems = dsm_control->nitems;
484  for (i = 0; i < nitems; ++i)
485  {
486  if (dsm_control->item[i].refcnt == 0)
487  {
488  dsm_control->item[i].handle = seg->handle;
489  /* refcnt of 1 triggers destruction, so start at 2 */
490  dsm_control->item[i].refcnt = 2;
492  dsm_control->item[i].pinned = false;
493  seg->control_slot = i;
494  LWLockRelease(DynamicSharedMemoryControlLock);
495  return seg;
496  }
497  }
498 
499  /* Verify that we can support an additional mapping. */
500  if (nitems >= dsm_control->maxitems)
501  {
502  if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
503  {
504  LWLockRelease(DynamicSharedMemoryControlLock);
506  &seg->mapped_address, &seg->mapped_size, WARNING);
507  if (seg->resowner != NULL)
508  ResourceOwnerForgetDSM(seg->resowner, seg);
509  dlist_delete(&seg->node);
510  pfree(seg);
511  return NULL;
512  }
513  ereport(ERROR,
514  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
515  errmsg("too many dynamic shared memory segments")));
516  }
517 
518  /* Enter the handle into a new array slot. */
519  dsm_control->item[nitems].handle = seg->handle;
520  /* refcnt of 1 triggers destruction, so start at 2 */
521  dsm_control->item[nitems].refcnt = 2;
522  dsm_control->item[nitems].impl_private_pm_handle = NULL;
523  dsm_control->item[nitems].pinned = false;
524  seg->control_slot = nitems;
525  dsm_control->nitems++;
526  LWLockRelease(DynamicSharedMemoryControlLock);
527 
528  return seg;
529 }
dlist_node node
Definition: dsm.c:69
uint32 maxitems
Definition: dsm.c:93
Size mapped_size
Definition: dsm.c:75
dsm_handle handle
Definition: dsm.c:82
long random(void)
Definition: random.c:22
int errcode(int sqlerrcode)
Definition: elog.c:575
static void dsm_backend_startup(void)
Definition: dsm.c:392
uint32 nitems
Definition: dsm.c:92
bool pinned
Definition: dsm.c:85
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1724
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
void pfree(void *pointer)
Definition: mcxt.c:936
#define ERROR
Definition: elog.h:43
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
bool IsUnderPostmaster
Definition: globals.c:101
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:94
unsigned int uint32
Definition: c.h:306
void * mapped_address
Definition: dsm.c:74
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
dsm_handle handle
Definition: dsm.c:71
static dsm_control_header * dsm_control
Definition: dsm.c:134
ResourceOwner resowner
Definition: dsm.c:70
#define Assert(condition)
Definition: c.h:680
void * impl_private
Definition: dsm.c:73
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1240
uint32 control_slot
Definition: dsm.c:72
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1120
static dsm_segment * dsm_create_descriptor(void)
Definition: dsm.c:1093
uint32 refcnt
Definition: dsm.c:83
static bool dsm_init_done
Definition: dsm.c:105
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
void * impl_private_pm_handle
Definition: dsm.c:84
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:163

◆ dsm_detach()

void dsm_detach ( dsm_segment seg)

Definition at line 713 of file dsm.c.

References dsm_segment_detach_callback::arg, Assert, dsm_segment::control_slot, dlist_delete(), dsm_impl_op(), DSM_OP_DESTROY, DSM_OP_DETACH, dsm_segment_detach_callback::function, dsm_segment::handle, dsm_control_item::handle, dsm_segment::impl_private, INVALID_CONTROL_SLOT, 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_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(), 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().

714 {
715  /*
716  * Invoke registered callbacks. Just in case one of those callbacks
717  * throws a further error that brings us back here, pop the callback
718  * before invoking it, to avoid infinite error recursion.
719  */
720  while (!slist_is_empty(&seg->on_detach))
721  {
722  slist_node *node;
724  on_dsm_detach_callback function;
725  Datum arg;
726 
727  node = slist_pop_head_node(&seg->on_detach);
729  function = cb->function;
730  arg = cb->arg;
731  pfree(cb);
732 
733  function(seg, arg);
734  }
735 
736  /*
737  * Try to remove the mapping, if one exists. Normally, there will be, but
738  * maybe not, if we failed partway through a create or attach operation.
739  * We remove the mapping before decrementing the reference count so that
740  * the process that sees a zero reference count can be certain that no
741  * remaining mappings exist. Even if this fails, we pretend that it
742  * works, because retrying is likely to fail in the same way.
743  */
744  if (seg->mapped_address != NULL)
745  {
747  &seg->mapped_address, &seg->mapped_size, WARNING);
748  seg->impl_private = NULL;
749  seg->mapped_address = NULL;
750  seg->mapped_size = 0;
751  }
752 
753  /* Reduce reference count, if we previously increased it. */
755  {
756  uint32 refcnt;
757  uint32 control_slot = seg->control_slot;
758 
759  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
760  Assert(dsm_control->item[control_slot].handle == seg->handle);
761  Assert(dsm_control->item[control_slot].refcnt > 1);
762  refcnt = --dsm_control->item[control_slot].refcnt;
764  LWLockRelease(DynamicSharedMemoryControlLock);
765 
766  /* If new reference count is 1, try to destroy the segment. */
767  if (refcnt == 1)
768  {
769  /* A pinned segment should never reach 1. */
770  Assert(!dsm_control->item[control_slot].pinned);
771 
772  /*
773  * If we fail to destroy the segment here, or are killed before we
774  * finish doing so, the reference count will remain at 1, which
775  * will mean that nobody else can attach to the segment. At
776  * postmaster shutdown time, or when a new postmaster is started
777  * after a hard kill, another attempt will be made to remove the
778  * segment.
779  *
780  * The main case we're worried about here is being killed by a
781  * signal before we can finish removing the segment. In that
782  * case, it's important to be sure that the segment still gets
783  * removed. If we actually fail to remove the segment for some
784  * other reason, the postmaster may not have any better luck than
785  * we did. There's not much we can do about that, though.
786  */
787  if (dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
788  &seg->mapped_address, &seg->mapped_size, WARNING))
789  {
790  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
791  Assert(dsm_control->item[control_slot].handle == seg->handle);
792  Assert(dsm_control->item[control_slot].refcnt == 1);
793  dsm_control->item[control_slot].refcnt = 0;
794  LWLockRelease(DynamicSharedMemoryControlLock);
795  }
796  }
797  }
798 
799  /* Clean up our remaining backend-private data structures. */
800  if (seg->resowner != NULL)
801  ResourceOwnerForgetDSM(seg->resowner, seg);
802  dlist_delete(&seg->node);
803  pfree(seg);
804 }
dlist_node node
Definition: dsm.c:69
Size mapped_size
Definition: dsm.c:75
dsm_handle handle
Definition: dsm.c:82
bool pinned
Definition: dsm.c:85
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1724
void pfree(void *pointer)
Definition: mcxt.c:936
on_dsm_detach_callback function
Definition: dsm.c:61
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:94
unsigned int uint32
Definition: c.h:306
void * mapped_address
Definition: dsm.c:74
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:76
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:563
#define WARNING
Definition: elog.h:40
dsm_handle handle
Definition: dsm.c:71
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
uintptr_t Datum
Definition: postgres.h:372
static dsm_control_header * dsm_control
Definition: dsm.c:134
ResourceOwner resowner
Definition: dsm.c:70
#define Assert(condition)
Definition: c.h:680
void * impl_private
Definition: dsm.c:73
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1240
uint32 control_slot
Definition: dsm.c:72
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1120
uint32 refcnt
Definition: dsm.c:83
void * arg
#define INVALID_CONTROL_SLOT
Definition: dsm.c:56
void(* on_dsm_detach_callback)(dsm_segment *, Datum arg)
Definition: dsm.h:56
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:163

◆ dsm_detach_all()

void dsm_detach_all ( void  )

Definition at line 653 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().

654 {
655  void *control_address = dsm_control;
656 
658  {
659  dsm_segment *seg;
660 
662  dsm_detach(seg);
663  }
664 
665  if (control_address != NULL)
667  &dsm_control_impl_private, &control_address,
669 }
static void * dsm_control_impl_private
Definition: dsm.c:136
static dsm_handle dsm_control_handle
Definition: dsm.c:133
static dlist_head dsm_segment_list
Definition: dsm.c:124
#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:134
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:713
static Size dsm_control_mapped_size
Definition: dsm.c:135
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:163

◆ dsm_find_mapping()

dsm_segment* dsm_find_mapping ( dsm_handle  h)

Definition at line 968 of file dsm.c.

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

969 {
970  dlist_iter iter;
971  dsm_segment *seg;
972 
974  {
975  seg = dlist_container(dsm_segment, node, iter.cur);
976  if (seg->handle == h)
977  return seg;
978  }
979 
980  return NULL;
981 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static dlist_head dsm_segment_list
Definition: dsm.c:124
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
dsm_handle handle
Definition: dsm.c:71
dlist_node * cur
Definition: ilist.h:161

◆ dsm_pin_mapping()

void dsm_pin_mapping ( dsm_segment seg)

Definition at line 814 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().

815 {
816  if (seg->resowner != NULL)
817  {
818  ResourceOwnerForgetDSM(seg->resowner, seg);
819  seg->resowner = NULL;
820  }
821 }
ResourceOwner resowner
Definition: dsm.c:70
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1240

◆ dsm_pin_segment()

void dsm_pin_segment ( dsm_segment seg)

Definition at line 854 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().

855 {
856  void *handle;
857 
858  /*
859  * Bump reference count for this segment in shared memory. This will
860  * ensure that even if there is no session which is attached to this
861  * segment, it will remain until postmaster shutdown or an explicit call
862  * to unpin.
863  */
864  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
865  if (dsm_control->item[seg->control_slot].pinned)
866  elog(ERROR, "cannot pin a segment that is already pinned");
867  dsm_impl_pin_segment(seg->handle, seg->impl_private, &handle);
868  dsm_control->item[seg->control_slot].pinned = true;
871  LWLockRelease(DynamicSharedMemoryControlLock);
872 }
bool pinned
Definition: dsm.c:85
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1724
#define ERROR
Definition: elog.h:43
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:94
dsm_handle handle
Definition: dsm.c:71
static dsm_control_header * dsm_control
Definition: dsm.c:134
void * impl_private
Definition: dsm.c:73
uint32 control_slot
Definition: dsm.c:72
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1120
uint32 refcnt
Definition: dsm.c:83
void dsm_impl_pin_segment(dsm_handle handle, void *impl_private, void **impl_private_pm_handle)
Definition: dsm_impl.c:1056
void * impl_private_pm_handle
Definition: dsm.c:84
#define elog
Definition: elog.h:219

◆ dsm_postmaster_startup()

void dsm_postmaster_startup ( struct PGShmemHeader )

Definition at line 145 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_NONE, 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().

146 {
147  void *dsm_control_address = NULL;
148  uint32 maxitems;
149  Size segsize;
150 
152 
153  /* If dynamic shared memory is disabled, there's nothing to do. */
155  return;
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);
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);
196  shim->dsm_control = dsm_control_handle;
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:136
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:21
uint32 maxitems
Definition: dsm.c:93
#define PG_DYNSHMEM_SLOTS_PER_BACKEND
Definition: dsm.c:54
#define PG_DYNSHMEM_FIXED_SLOTS
Definition: dsm.c:53
#define PointerGetDatum(X)
Definition: postgres.h:562
long random(void)
Definition: random.c:22
static dsm_handle dsm_control_handle
Definition: dsm.c:133
static uint64 dsm_control_bytes_needed(uint32 nitems)
Definition: dsm.c:1147
uint32 nitems
Definition: dsm.c:92
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define ERROR
Definition: elog.h:43
uint32 magic
Definition: dsm.c:91
int MaxBackends
Definition: globals.c:126
#define DEBUG2
Definition: elog.h:24
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:348
bool IsUnderPostmaster
Definition: globals.c:101
static void dsm_postmaster_shutdown(int code, Datum arg)
Definition: dsm.c:330
int dynamic_shared_memory_type
Definition: dsm_impl.c:113
unsigned int uint32
Definition: c.h:306
static void dsm_cleanup_for_mmap(void)
Definition: dsm.c:292
static dsm_control_header * dsm_control
Definition: dsm.c:134
#define Assert(condition)
Definition: c.h:680
#define DSM_IMPL_NONE
Definition: dsm_impl.h:17
size_t Size
Definition: c.h:414
static Size dsm_control_mapped_size
Definition: dsm.c:135
#define elog
Definition: elog.h:219
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:163
#define PG_DYNSHMEM_CONTROL_MAGIC
Definition: dsm.c:46

◆ dsm_remap()

void* dsm_remap ( dsm_segment seg)

Definition at line 695 of file dsm.c.

References dsm_impl_op(), DSM_OP_ATTACH, ERROR, dsm_segment::handle, dsm_segment::impl_private, dsm_segment::mapped_address, and dsm_segment::mapped_size.

696 {
698  &seg->mapped_address, &seg->mapped_size, ERROR);
699 
700  return seg->mapped_address;
701 }
Size mapped_size
Definition: dsm.c:75
#define ERROR
Definition: elog.h:43
void * mapped_address
Definition: dsm.c:74
dsm_handle handle
Definition: dsm.c:71
void * impl_private
Definition: dsm.c:73
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:163

◆ dsm_resize()

void* dsm_resize ( dsm_segment seg,
Size  size 
)

Definition at line 678 of file dsm.c.

References Assert, dsm_segment::control_slot, dsm_impl_op(), DSM_OP_RESIZE, ERROR, dsm_segment::handle, dsm_segment::impl_private, INVALID_CONTROL_SLOT, dsm_segment::mapped_address, and dsm_segment::mapped_size.

679 {
681  dsm_impl_op(DSM_OP_RESIZE, seg->handle, size, &seg->impl_private,
682  &seg->mapped_address, &seg->mapped_size, ERROR);
683  return seg->mapped_address;
684 }
Size mapped_size
Definition: dsm.c:75
#define ERROR
Definition: elog.h:43
void * mapped_address
Definition: dsm.c:74
dsm_handle handle
Definition: dsm.c:71
#define Assert(condition)
Definition: c.h:680
void * impl_private
Definition: dsm.c:73
uint32 control_slot
Definition: dsm.c:72
#define INVALID_CONTROL_SLOT
Definition: dsm.c:56
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:163

◆ 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 997 of file dsm.c.

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

998 {
999  Assert(seg->mapped_address != NULL);
1000  return seg->mapped_size;
1001 }
Size mapped_size
Definition: dsm.c:75
void * mapped_address
Definition: dsm.c:74
#define Assert(condition)
Definition: c.h:680

◆ dsm_unpin_mapping()

void dsm_unpin_mapping ( dsm_segment seg)

Definition at line 833 of file dsm.c.

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

834 {
835  Assert(seg->resowner == NULL);
839 }
void ResourceOwnerRememberDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1231
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ResourceOwnerEnlargeDSMs(ResourceOwner owner)
Definition: resowner.c:1220
ResourceOwner resowner
Definition: dsm.c:70
#define Assert(condition)
Definition: c.h:680

◆ dsm_unpin_segment()

void dsm_unpin_segment ( dsm_handle  h)

Definition at line 886 of file dsm.c.

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

Referenced by destroy_superblock(), and dsa_release_in_place().

887 {
888  uint32 control_slot = INVALID_CONTROL_SLOT;
889  bool destroy = false;
890  uint32 i;
891 
892  /* Find the control slot for the given handle. */
893  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
894  for (i = 0; i < dsm_control->nitems; ++i)
895  {
896  /* Skip unused slots. */
897  if (dsm_control->item[i].refcnt == 0)
898  continue;
899 
900  /* If we've found our handle, we can stop searching. */
901  if (dsm_control->item[i].handle == handle)
902  {
903  control_slot = i;
904  break;
905  }
906  }
907 
908  /*
909  * We should definitely have found the slot, and it should not already be
910  * in the process of going away, because this function should only be
911  * called on a segment which is pinned.
912  */
913  if (control_slot == INVALID_CONTROL_SLOT)
914  elog(ERROR, "cannot unpin unknown segment handle");
915  if (!dsm_control->item[control_slot].pinned)
916  elog(ERROR, "cannot unpin a segment that is not pinned");
917  Assert(dsm_control->item[control_slot].refcnt > 1);
918 
919  /*
920  * Allow implementation-specific code to run. We have to do this before
921  * releasing the lock, because impl_private_pm_handle may get modified by
922  * dsm_impl_unpin_segment.
923  */
924  dsm_impl_unpin_segment(handle,
925  &dsm_control->item[control_slot].impl_private_pm_handle);
926 
927  /* Note that 1 means no references (0 means unused slot). */
928  if (--dsm_control->item[control_slot].refcnt == 1)
929  destroy = true;
930  dsm_control->item[control_slot].pinned = false;
931 
932  /* Now we can release the lock. */
933  LWLockRelease(DynamicSharedMemoryControlLock);
934 
935  /* Clean up resources if that was the last reference. */
936  if (destroy)
937  {
938  void *junk_impl_private = NULL;
939  void *junk_mapped_address = NULL;
940  Size junk_mapped_size = 0;
941 
942  /*
943  * For an explanation of how error handling works in this case, see
944  * comments in dsm_detach. Note that if we reach this point, the
945  * current process certainly does not have the segment mapped, because
946  * if it did, the reference count would have still been greater than 1
947  * even after releasing the reference count held by the pin. The fact
948  * that there can't be a dsm_segment for this handle makes it OK to
949  * pass the mapped size, mapped address, and private data as NULL
950  * here.
951  */
952  if (dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
953  &junk_mapped_address, &junk_mapped_size, WARNING))
954  {
955  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
956  Assert(dsm_control->item[control_slot].handle == handle);
957  Assert(dsm_control->item[control_slot].refcnt == 1);
958  dsm_control->item[control_slot].refcnt = 0;
959  LWLockRelease(DynamicSharedMemoryControlLock);
960  }
961  }
962 }
void dsm_impl_unpin_segment(dsm_handle handle, void **impl_private)
Definition: dsm_impl.c:1106
dsm_handle handle
Definition: dsm.c:82
uint32 nitems
Definition: dsm.c:92
bool pinned
Definition: dsm.c:85
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1724
#define ERROR
Definition: elog.h:43
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:94
unsigned int uint32
Definition: c.h:306
#define WARNING
Definition: elog.h:40
static dsm_control_header * dsm_control
Definition: dsm.c:134
#define Assert(condition)
Definition: c.h:680
size_t Size
Definition: c.h:414
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1120
uint32 refcnt
Definition: dsm.c:83
int i
void * impl_private_pm_handle
Definition: dsm.c:84
#define INVALID_CONTROL_SLOT
Definition: dsm.c:56
#define elog
Definition: elog.h:219
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:163

◆ on_dsm_detach()

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

Definition at line 1024 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().

1025 {
1027 
1029  sizeof(dsm_segment_detach_callback));
1030  cb->function = function;
1031  cb->arg = arg;
1032  slist_push_head(&seg->on_detach, &cb->node);
1033 }
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:574
on_dsm_detach_callback function
Definition: dsm.c:61
MemoryContext TopMemoryContext
Definition: mcxt.c:43
slist_head on_detach
Definition: dsm.c:76
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:693
void * arg

◆ reset_on_dsm_detach()

void reset_on_dsm_detach ( void  )

Definition at line 1062 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().

1063 {
1064  dlist_iter iter;
1065 
1067  {
1068  dsm_segment *seg = dlist_container(dsm_segment, node, iter.cur);
1069 
1070  /* Throw away explicit on-detach actions one by one. */
1071  while (!slist_is_empty(&seg->on_detach))
1072  {
1073  slist_node *node;
1075 
1076  node = slist_pop_head_node(&seg->on_detach);
1077  cb = slist_container(dsm_segment_detach_callback, node, node);
1078  pfree(cb);
1079  }
1080 
1081  /*
1082  * Decrementing the reference count is a sort of implicit on-detach
1083  * action; make sure we don't do that, either.
1084  */
1086  }
1087 }
#define dlist_foreach(iter, lhead)
Definition: ilist.h:507
static dlist_head dsm_segment_list
Definition: dsm.c:124
#define dlist_container(type, membername, ptr)
Definition: ilist.h:477
void pfree(void *pointer)
Definition: mcxt.c:936
static slist_node * slist_pop_head_node(slist_head *head)
Definition: ilist.h:596
slist_head on_detach
Definition: dsm.c:76
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:72
#define INVALID_CONTROL_SLOT
Definition: dsm.c:56