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)
 
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_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 handle)
 
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.

◆ DSM_HANDLE_INVALID

#define DSM_HANDLE_INVALID   0

Definition at line 23 of file dsm.h.

Typedef Documentation

◆ dsm_segment

typedef struct dsm_segment dsm_segment

Definition at line 1 of file dsm.h.

◆ on_dsm_detach_callback

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

Definition at line 57 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 1119 of file dsm.c.

1121 {
1122  slist_mutable_iter iter;
1123 
1124  slist_foreach_modify(iter, &seg->on_detach)
1125  {
1127 
1129  if (cb->function == function && cb->arg == arg)
1130  {
1131  slist_delete_current(&iter);
1132  pfree(cb);
1133  break;
1134  }
1135  }
1136 }
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:1021
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:1085
#define slist_container(type, membername, ptr)
Definition: ilist.h:1043
void pfree(void *pointer)
Definition: mcxt.c:1306
void * arg
on_dsm_detach_callback function
Definition: dsm.c:59
slist_head on_detach
Definition: dsm.c:74
slist_node * cur
Definition: ilist.h:274

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

Referenced by shm_mq_detach(), and test_shm_mq_setup().

◆ dsm_attach()

dsm_segment* dsm_attach ( dsm_handle  h)

Definition at line 639 of file dsm.c.

640 {
641  dsm_segment *seg;
642  dlist_iter iter;
643  uint32 i;
644  uint32 nitems;
645 
646  /* Unsafe in postmaster (and pointless in a stand-alone backend). */
648 
649  if (!dsm_init_done)
651 
652  /*
653  * Since this is just a debugging cross-check, we could leave it out
654  * altogether, or include it only in assert-enabled builds. But since the
655  * list of attached segments should normally be very short, let's include
656  * it always for right now.
657  *
658  * If you're hitting this error, you probably want to attempt to find an
659  * existing mapping via dsm_find_mapping() before calling dsm_attach() to
660  * create a new one.
661  */
663  {
664  seg = dlist_container(dsm_segment, node, iter.cur);
665  if (seg->handle == h)
666  elog(ERROR, "can't attach the same segment more than once");
667  }
668 
669  /* Create a new segment descriptor. */
670  seg = dsm_create_descriptor();
671  seg->handle = h;
672 
673  /* Bump reference count for this segment in shared memory. */
674  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
675  nitems = dsm_control->nitems;
676  for (i = 0; i < nitems; ++i)
677  {
678  /*
679  * If the reference count is 0, the slot is actually unused. If the
680  * reference count is 1, the slot is still in use, but the segment is
681  * in the process of going away; even if the handle matches, another
682  * slot may already have started using the same handle value by
683  * coincidence so we have to keep searching.
684  */
685  if (dsm_control->item[i].refcnt <= 1)
686  continue;
687 
688  /* If the handle doesn't match, it's not the slot we want. */
689  if (dsm_control->item[i].handle != seg->handle)
690  continue;
691 
692  /* Otherwise we've found a match. */
693  dsm_control->item[i].refcnt++;
694  seg->control_slot = i;
696  {
697  seg->mapped_address = (char *) dsm_main_space_begin +
700  }
701  break;
702  }
703  LWLockRelease(DynamicSharedMemoryControlLock);
704 
705  /*
706  * If we didn't find the handle we're looking for in the control segment,
707  * it probably means that everyone else who had it mapped, including the
708  * original creator, died before we got to this point. It's up to the
709  * caller to decide what to do about that.
710  */
712  {
713  dsm_detach(seg);
714  return NULL;
715  }
716 
717  /* Here's where we actually try to map the segment. */
720  &seg->mapped_address, &seg->mapped_size, ERROR);
721 
722  return seg;
723 }
unsigned int uint32
Definition: c.h:442
static void dsm_backend_startup(void)
Definition: dsm.c:397
static void * dsm_main_space_begin
Definition: dsm.c:110
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:777
static dlist_head dsm_segment_list
Definition: dsm.c:129
static bool dsm_init_done
Definition: dsm.c:107
static dsm_control_header * dsm_control
Definition: dsm.c:139
static dsm_segment * dsm_create_descriptor(void)
Definition: dsm.c:1173
#define INVALID_CONTROL_SLOT
Definition: dsm.c:54
static bool is_main_region_dsm_handle(dsm_handle handle)
Definition: dsm.c:1253
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:159
@ DSM_OP_ATTACH
Definition: dsm_impl.h:61
#define ERROR
Definition: elog.h:35
#define FPM_PAGE_SIZE
Definition: freepage.h:30
bool IsUnderPostmaster
Definition: globals.c:113
#define dlist_foreach(iter, lhead)
Definition: ilist.h:573
#define dlist_container(type, membername, ptr)
Definition: ilist.h:543
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1194
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1802
@ LW_EXCLUSIVE
Definition: lwlock.h:112
dlist_node * cur
Definition: ilist.h:179
uint32 nitems
Definition: dsm.c:92
dsm_control_item item[FLEXIBLE_ARRAY_MEMBER]
Definition: dsm.c:94
size_t npages
Definition: dsm.c:83
dsm_handle handle
Definition: dsm.c:80
size_t first_page
Definition: dsm.c:82
uint32 refcnt
Definition: dsm.c:81
uint32 control_slot
Definition: dsm.c:70
dsm_handle handle
Definition: dsm.c:69
Size mapped_size
Definition: dsm.c:73
void * impl_private
Definition: dsm.c:71
void * mapped_address
Definition: dsm.c:72

References Assert(), dsm_segment::control_slot, dlist_iter::cur, dlist_container, dlist_foreach, dsm_backend_startup(), dsm_control, dsm_create_descriptor(), dsm_detach(), dsm_impl_op(), dsm_init_done, dsm_main_space_begin, DSM_OP_ATTACH, dsm_segment_list, 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_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().

◆ dsm_backend_shutdown()

void dsm_backend_shutdown ( void  )

Definition at line 731 of file dsm.c.

732 {
734  {
735  dsm_segment *seg;
736 
738  dsm_detach(seg);
739  }
740 }
#define dlist_head_element(type, membername, lhead)
Definition: ilist.h:553
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:325

References dlist_head_element, dlist_is_empty(), dsm_detach(), and dsm_segment_list.

Referenced by shmem_exit().

◆ dsm_cleanup_using_control_segment()

void dsm_cleanup_using_control_segment ( dsm_handle  old_control_handle)

Definition at line 212 of file dsm.c.

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

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().

◆ dsm_create()

dsm_segment* dsm_create ( Size  size,
int  flags 
)

Definition at line 490 of file dsm.c.

491 {
492  dsm_segment *seg;
493  uint32 i;
494  uint32 nitems;
495  size_t npages = 0;
496  size_t first_page = 0;
497  FreePageManager *dsm_main_space_fpm = dsm_main_space_begin;
498  bool using_main_dsm_region = false;
499 
500  /*
501  * Unsafe in postmaster. It might seem pointless to allow use of dsm in
502  * single user mode, but otherwise some subsystems will need dedicated
503  * single user mode code paths.
504  */
506 
507  if (!dsm_init_done)
509 
510  /* Create a new segment descriptor. */
511  seg = dsm_create_descriptor();
512 
513  /*
514  * Lock the control segment while we try to allocate from the main shared
515  * memory area, if configured.
516  */
517  if (dsm_main_space_fpm)
518  {
519  npages = size / FPM_PAGE_SIZE;
520  if (size % FPM_PAGE_SIZE > 0)
521  ++npages;
522 
523  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
524  if (FreePageManagerGet(dsm_main_space_fpm, npages, &first_page))
525  {
526  /* We can carve out a piece of the main shared memory segment. */
527  seg->mapped_address = (char *) dsm_main_space_begin +
528  first_page * FPM_PAGE_SIZE;
529  seg->mapped_size = npages * FPM_PAGE_SIZE;
530  using_main_dsm_region = true;
531  /* We'll choose a handle below. */
532  }
533  }
534 
535  if (!using_main_dsm_region)
536  {
537  /*
538  * We need to create a new memory segment. Loop until we find an
539  * unused segment identifier.
540  */
541  if (dsm_main_space_fpm)
542  LWLockRelease(DynamicSharedMemoryControlLock);
543  for (;;)
544  {
545  Assert(seg->mapped_address == NULL && seg->mapped_size == 0);
546  /* Use even numbers only */
548  if (seg->handle == DSM_HANDLE_INVALID) /* Reserve sentinel */
549  continue;
550  if (dsm_impl_op(DSM_OP_CREATE, seg->handle, size, &seg->impl_private,
551  &seg->mapped_address, &seg->mapped_size, ERROR))
552  break;
553  }
554  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
555  }
556 
557  /* Search the control segment for an unused slot. */
558  nitems = dsm_control->nitems;
559  for (i = 0; i < nitems; ++i)
560  {
561  if (dsm_control->item[i].refcnt == 0)
562  {
563  if (using_main_dsm_region)
564  {
566  dsm_control->item[i].first_page = first_page;
567  dsm_control->item[i].npages = npages;
568  }
569  else
571  dsm_control->item[i].handle = seg->handle;
572  /* refcnt of 1 triggers destruction, so start at 2 */
573  dsm_control->item[i].refcnt = 2;
575  dsm_control->item[i].pinned = false;
576  seg->control_slot = i;
577  LWLockRelease(DynamicSharedMemoryControlLock);
578  return seg;
579  }
580  }
581 
582  /* Verify that we can support an additional mapping. */
583  if (nitems >= dsm_control->maxitems)
584  {
585  if (using_main_dsm_region)
586  FreePageManagerPut(dsm_main_space_fpm, first_page, npages);
587  LWLockRelease(DynamicSharedMemoryControlLock);
588  if (!using_main_dsm_region)
590  &seg->mapped_address, &seg->mapped_size, WARNING);
591  if (seg->resowner != NULL)
592  ResourceOwnerForgetDSM(seg->resowner, seg);
593  dlist_delete(&seg->node);
594  pfree(seg);
595 
596  if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
597  return NULL;
598  ereport(ERROR,
599  (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
600  errmsg("too many dynamic shared memory segments")));
601  }
602 
603  /* Enter the handle into a new array slot. */
604  if (using_main_dsm_region)
605  {
606  seg->handle = make_main_region_dsm_handle(nitems);
607  dsm_control->item[i].first_page = first_page;
608  dsm_control->item[i].npages = npages;
609  }
610  dsm_control->item[nitems].handle = seg->handle;
611  /* refcnt of 1 triggers destruction, so start at 2 */
612  dsm_control->item[nitems].refcnt = 2;
613  dsm_control->item[nitems].impl_private_pm_handle = NULL;
614  dsm_control->item[nitems].pinned = false;
615  seg->control_slot = nitems;
616  dsm_control->nitems++;
617  LWLockRelease(DynamicSharedMemoryControlLock);
618 
619  return seg;
620 }
static dsm_handle make_main_region_dsm_handle(int slot)
Definition: dsm.c:1234
#define DSM_CREATE_NULL_IF_MAXSEGMENTS
Definition: dsm.h:20
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
@ DSM_OP_CREATE
Definition: dsm_impl.h:60
int errcode(int sqlerrcode)
Definition: elog.c:735
int errmsg(const char *fmt,...)
Definition: elog.c:946
#define WARNING
Definition: elog.h:32
#define ereport(elevel,...)
Definition: elog.h:145
bool FreePageManagerGet(FreePageManager *fpm, Size npages, Size *first_page)
Definition: freepage.c:210
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
bool IsPostmasterEnvironment
Definition: globals.c:112
static void dlist_delete(dlist_node *node)
Definition: ilist.h:394
uint32 pg_prng_uint32(pg_prng_state *state)
Definition: pg_prng.c:185
pg_prng_state pg_global_prng_state
Definition: pg_prng.c:28
void ResourceOwnerForgetDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1355
uint32 maxitems
Definition: dsm.c:93
bool pinned
Definition: dsm.c:85
void * impl_private_pm_handle
Definition: dsm.c:84
dlist_node node
Definition: dsm.c:67
ResourceOwner resowner
Definition: dsm.c:68

References Assert(), dsm_segment::control_slot, dlist_delete(), dsm_backend_startup(), dsm_control, 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(), IsPostmasterEnvironment, 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(), pg_global_prng_state, pg_prng_uint32(), dsm_control_item::pinned, 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().

◆ dsm_detach()

void dsm_detach ( dsm_segment seg)

Definition at line 777 of file dsm.c.

778 {
779  /*
780  * Invoke registered callbacks. Just in case one of those callbacks
781  * throws a further error that brings us back here, pop the callback
782  * before invoking it, to avoid infinite error recursion. Don't allow
783  * interrupts while running the individual callbacks in non-error code
784  * paths, to avoid leaving cleanup work unfinished if we're interrupted by
785  * a statement timeout or similar.
786  */
787  HOLD_INTERRUPTS();
788  while (!slist_is_empty(&seg->on_detach))
789  {
790  slist_node *node;
792  on_dsm_detach_callback function;
793  Datum arg;
794 
795  node = slist_pop_head_node(&seg->on_detach);
797  function = cb->function;
798  arg = cb->arg;
799  pfree(cb);
800 
801  function(seg, arg);
802  }
804 
805  /*
806  * Try to remove the mapping, if one exists. Normally, there will be, but
807  * maybe not, if we failed partway through a create or attach operation.
808  * We remove the mapping before decrementing the reference count so that
809  * the process that sees a zero reference count can be certain that no
810  * remaining mappings exist. Even if this fails, we pretend that it
811  * works, because retrying is likely to fail in the same way.
812  */
813  if (seg->mapped_address != NULL)
814  {
817  &seg->mapped_address, &seg->mapped_size, WARNING);
818  seg->impl_private = NULL;
819  seg->mapped_address = NULL;
820  seg->mapped_size = 0;
821  }
822 
823  /* Reduce reference count, if we previously increased it. */
825  {
826  uint32 refcnt;
827  uint32 control_slot = seg->control_slot;
828 
829  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
830  Assert(dsm_control->item[control_slot].handle == seg->handle);
831  Assert(dsm_control->item[control_slot].refcnt > 1);
832  refcnt = --dsm_control->item[control_slot].refcnt;
834  LWLockRelease(DynamicSharedMemoryControlLock);
835 
836  /* If new reference count is 1, try to destroy the segment. */
837  if (refcnt == 1)
838  {
839  /* A pinned segment should never reach 1. */
840  Assert(!dsm_control->item[control_slot].pinned);
841 
842  /*
843  * If we fail to destroy the segment here, or are killed before we
844  * finish doing so, the reference count will remain at 1, which
845  * will mean that nobody else can attach to the segment. At
846  * postmaster shutdown time, or when a new postmaster is started
847  * after a hard kill, another attempt will be made to remove the
848  * segment.
849  *
850  * The main case we're worried about here is being killed by a
851  * signal before we can finish removing the segment. In that
852  * case, it's important to be sure that the segment still gets
853  * removed. If we actually fail to remove the segment for some
854  * other reason, the postmaster may not have any better luck than
855  * we did. There's not much we can do about that, though.
856  */
857  if (is_main_region_dsm_handle(seg->handle) ||
859  &seg->mapped_address, &seg->mapped_size, WARNING))
860  {
861  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
864  dsm_control->item[control_slot].first_page,
865  dsm_control->item[control_slot].npages);
866  Assert(dsm_control->item[control_slot].handle == seg->handle);
867  Assert(dsm_control->item[control_slot].refcnt == 1);
868  dsm_control->item[control_slot].refcnt = 0;
869  LWLockRelease(DynamicSharedMemoryControlLock);
870  }
871  }
872  }
873 
874  /* Clean up our remaining backend-private data structures. */
875  if (seg->resowner != NULL)
876  ResourceOwnerForgetDSM(seg->resowner, seg);
877  dlist_delete(&seg->node);
878  pfree(seg);
879 }
void(* on_dsm_detach_callback)(dsm_segment *, Datum arg)
Definition: dsm.h:57
static bool slist_is_empty(slist_head *head)
Definition: ilist.h:932
static slist_node * slist_pop_head_node(slist_head *head)
Definition: ilist.h:965
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:134
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:132
uintptr_t Datum
Definition: postgres.h:412

References dsm_segment_detach_callback::arg, arg, Assert(), dsm_segment::control_slot, dlist_delete(), dsm_control, 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, HOLD_INTERRUPTS, 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::node, dsm_control_item::npages, dsm_segment::on_detach, pfree(), dsm_control_item::pinned, dsm_control_item::refcnt, ResourceOwnerForgetDSM(), dsm_segment::resowner, RESUME_INTERRUPTS, 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(), ParallelWorkerShutdown(), ResourceOwnerReleaseInternal(), test_shm_mq(), test_shm_mq_main(), and test_shm_mq_pipelined().

◆ dsm_detach_all()

void dsm_detach_all ( void  )

Definition at line 749 of file dsm.c.

750 {
751  void *control_address = dsm_control;
752 
754  {
755  dsm_segment *seg;
756 
758  dsm_detach(seg);
759  }
760 
761  if (control_address != NULL)
763  &dsm_control_impl_private, &control_address,
765 }
static dsm_handle dsm_control_handle
Definition: dsm.c:138
static Size dsm_control_mapped_size
Definition: dsm.c:140
static void * dsm_control_impl_private
Definition: dsm.c:141

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, dsm_segment_list, and ERROR.

Referenced by SysLogger_Start().

◆ dsm_estimate_size()

size_t dsm_estimate_size ( void  )

Definition at line 444 of file dsm.c.

445 {
446  return 1024 * 1024 * (size_t) min_dynamic_shared_memory;
447 }
int min_dynamic_shared_memory
Definition: dsm_impl.c:115

References min_dynamic_shared_memory.

Referenced by CalculateShmemSize(), and dsm_shmem_init().

◆ dsm_find_mapping()

dsm_segment* dsm_find_mapping ( dsm_handle  handle)

Definition at line 1048 of file dsm.c.

1049 {
1050  dlist_iter iter;
1051  dsm_segment *seg;
1052 
1054  {
1055  seg = dlist_container(dsm_segment, node, iter.cur);
1056  if (seg->handle == handle)
1057  return seg;
1058  }
1059 
1060  return NULL;
1061 }

References dlist_iter::cur, dlist_container, dlist_foreach, dsm_segment_list, and dsm_segment::handle.

◆ dsm_pin_mapping()

void dsm_pin_mapping ( dsm_segment seg)

Definition at line 889 of file dsm.c.

890 {
891  if (seg->resowner != NULL)
892  {
893  ResourceOwnerForgetDSM(seg->resowner, seg);
894  seg->resowner = NULL;
895  }
896 }

References ResourceOwnerForgetDSM(), and dsm_segment::resowner.

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

◆ dsm_pin_segment()

void dsm_pin_segment ( dsm_segment seg)

Definition at line 929 of file dsm.c.

930 {
931  void *handle;
932 
933  /*
934  * Bump reference count for this segment in shared memory. This will
935  * ensure that even if there is no session which is attached to this
936  * segment, it will remain until postmaster shutdown or an explicit call
937  * to unpin.
938  */
939  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
940  if (dsm_control->item[seg->control_slot].pinned)
941  elog(ERROR, "cannot pin a segment that is already pinned");
942  dsm_impl_pin_segment(seg->handle, seg->impl_private, &handle);
943  dsm_control->item[seg->control_slot].pinned = true;
946  LWLockRelease(DynamicSharedMemoryControlLock);
947 }
void dsm_impl_pin_segment(dsm_handle handle, void *impl_private, void **impl_private_pm_handle)
Definition: dsm_impl.c:962

References dsm_segment::control_slot, dsm_control, 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().

◆ dsm_postmaster_startup()

void dsm_postmaster_startup ( struct PGShmemHeader shim)

Definition at line 150 of file dsm.c.

151 {
152  void *dsm_control_address = NULL;
153  uint32 maxitems;
154  Size segsize;
155 
157 
158  /*
159  * If we're using the mmap implementations, clean up any leftovers.
160  * Cleanup isn't needed on Windows, and happens earlier in startup for
161  * POSIX and System V shared memory, via a direct call to
162  * dsm_cleanup_using_control_segment.
163  */
166 
167  /* Determine size for new control segment. */
168  maxitems = PG_DYNSHMEM_FIXED_SLOTS
170  elog(DEBUG2, "dynamic shared memory system will support %u segments",
171  maxitems);
172  segsize = dsm_control_bytes_needed(maxitems);
173 
174  /*
175  * Loop until we find an unused identifier for the new control segment. We
176  * sometimes use 0 as a sentinel value indicating that no control segment
177  * is known to exist, so avoid using that value for a real control
178  * segment.
179  */
180  for (;;)
181  {
182  Assert(dsm_control_address == NULL);
184  /* Use even numbers only */
187  continue;
189  &dsm_control_impl_private, &dsm_control_address,
191  break;
192  }
193  dsm_control = dsm_control_address;
195  elog(DEBUG2,
196  "created dynamic shared memory control segment %u (%zu bytes)",
197  dsm_control_handle, segsize);
199 
200  /* Initialize control segment. */
202  dsm_control->nitems = 0;
203  dsm_control->maxitems = maxitems;
204 }
static void dsm_postmaster_shutdown(int code, Datum arg)
Definition: dsm.c:332
#define PG_DYNSHMEM_CONTROL_MAGIC
Definition: dsm.c:49
static uint64 dsm_control_bytes_needed(uint32 nitems)
Definition: dsm.c:1227
#define PG_DYNSHMEM_SLOTS_PER_BACKEND
Definition: dsm.c:52
#define PG_DYNSHMEM_FIXED_SLOTS
Definition: dsm.c:51
static void dsm_cleanup_for_mmap(void)
Definition: dsm.c:294
int dynamic_shared_memory_type
Definition: dsm_impl.c:112
#define DSM_IMPL_MMAP
Definition: dsm_impl.h:20
int MaxBackends
Definition: globals.c:140
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:670
dsm_handle dsm_control
Definition: pg_shmem.h:36
uint32 magic
Definition: dsm.c:91

References Assert(), DEBUG2, dsm_cleanup_for_mmap(), dsm_control, 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, pg_global_prng_state, pg_prng_uint32(), and PointerGetDatum().

Referenced by CreateSharedMemoryAndSemaphores().

◆ dsm_segment_address()

◆ dsm_segment_handle()

◆ dsm_segment_map_length()

Size dsm_segment_map_length ( dsm_segment seg)

Definition at line 1077 of file dsm.c.

1078 {
1079  Assert(seg->mapped_address != NULL);
1080  return seg->mapped_size;
1081 }

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

◆ dsm_shmem_init()

void dsm_shmem_init ( void  )

Definition at line 453 of file dsm.c.

454 {
455  size_t size = dsm_estimate_size();
456  bool found;
457 
458  if (size == 0)
459  return;
460 
461  dsm_main_space_begin = ShmemInitStruct("Preallocated DSM", size, &found);
462  if (!found)
463  {
465  size_t first_page = 0;
466  size_t pages;
467 
468  /* Reserve space for the FreePageManager. */
469  while (first_page * FPM_PAGE_SIZE < sizeof(FreePageManager))
470  ++first_page;
471 
472  /* Initialize it and give it all the rest of the space. */
474  pages = (size / FPM_PAGE_SIZE) - first_page;
475  FreePageManagerPut(fpm, first_page, pages);
476  }
477 }
size_t dsm_estimate_size(void)
Definition: dsm.c:444
void FreePageManagerInitialize(FreePageManager *fpm, char *base)
Definition: freepage.c:183
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ dsm_unpin_mapping()

void dsm_unpin_mapping ( dsm_segment seg)

Definition at line 908 of file dsm.c.

909 {
910  Assert(seg->resowner == NULL);
914 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void ResourceOwnerRememberDSM(ResourceOwner owner, dsm_segment *seg)
Definition: resowner.c:1346
void ResourceOwnerEnlargeDSMs(ResourceOwner owner)
Definition: resowner.c:1335

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

◆ dsm_unpin_segment()

void dsm_unpin_segment ( dsm_handle  handle)

Definition at line 961 of file dsm.c.

962 {
963  uint32 control_slot = INVALID_CONTROL_SLOT;
964  bool destroy = false;
965  uint32 i;
966 
967  /* Find the control slot for the given handle. */
968  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
969  for (i = 0; i < dsm_control->nitems; ++i)
970  {
971  /* Skip unused slots and segments that are concurrently going away. */
972  if (dsm_control->item[i].refcnt <= 1)
973  continue;
974 
975  /* If we've found our handle, we can stop searching. */
976  if (dsm_control->item[i].handle == handle)
977  {
978  control_slot = i;
979  break;
980  }
981  }
982 
983  /*
984  * We should definitely have found the slot, and it should not already be
985  * in the process of going away, because this function should only be
986  * called on a segment which is pinned.
987  */
988  if (control_slot == INVALID_CONTROL_SLOT)
989  elog(ERROR, "cannot unpin unknown segment handle");
990  if (!dsm_control->item[control_slot].pinned)
991  elog(ERROR, "cannot unpin a segment that is not pinned");
992  Assert(dsm_control->item[control_slot].refcnt > 1);
993 
994  /*
995  * Allow implementation-specific code to run. We have to do this before
996  * releasing the lock, because impl_private_pm_handle may get modified by
997  * dsm_impl_unpin_segment.
998  */
999  dsm_impl_unpin_segment(handle,
1000  &dsm_control->item[control_slot].impl_private_pm_handle);
1001 
1002  /* Note that 1 means no references (0 means unused slot). */
1003  if (--dsm_control->item[control_slot].refcnt == 1)
1004  destroy = true;
1005  dsm_control->item[control_slot].pinned = false;
1006 
1007  /* Now we can release the lock. */
1008  LWLockRelease(DynamicSharedMemoryControlLock);
1009 
1010  /* Clean up resources if that was the last reference. */
1011  if (destroy)
1012  {
1013  void *junk_impl_private = NULL;
1014  void *junk_mapped_address = NULL;
1015  Size junk_mapped_size = 0;
1016 
1017  /*
1018  * For an explanation of how error handling works in this case, see
1019  * comments in dsm_detach. Note that if we reach this point, the
1020  * current process certainly does not have the segment mapped, because
1021  * if it did, the reference count would have still been greater than 1
1022  * even after releasing the reference count held by the pin. The fact
1023  * that there can't be a dsm_segment for this handle makes it OK to
1024  * pass the mapped size, mapped address, and private data as NULL
1025  * here.
1026  */
1027  if (is_main_region_dsm_handle(handle) ||
1028  dsm_impl_op(DSM_OP_DESTROY, handle, 0, &junk_impl_private,
1029  &junk_mapped_address, &junk_mapped_size, WARNING))
1030  {
1031  LWLockAcquire(DynamicSharedMemoryControlLock, LW_EXCLUSIVE);
1032  if (is_main_region_dsm_handle(handle))
1034  dsm_control->item[control_slot].first_page,
1035  dsm_control->item[control_slot].npages);
1036  Assert(dsm_control->item[control_slot].handle == handle);
1037  Assert(dsm_control->item[control_slot].refcnt == 1);
1038  dsm_control->item[control_slot].refcnt = 0;
1039  LWLockRelease(DynamicSharedMemoryControlLock);
1040  }
1041  }
1042 }
void dsm_impl_unpin_segment(dsm_handle handle, void **impl_private)
Definition: dsm_impl.c:1013

References Assert(), dsm_control, 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().

◆ on_dsm_detach()

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

Definition at line 1104 of file dsm.c.

1105 {
1107 
1109  sizeof(dsm_segment_detach_callback));
1110  cb->function = function;
1111  cb->arg = arg;
1112  slist_push_head(&seg->on_detach, &cb->node);
1113 }
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:943
MemoryContext TopMemoryContext
Definition: mcxt.c:130
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994

References dsm_segment_detach_callback::arg, 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().

◆ reset_on_dsm_detach()

void reset_on_dsm_detach ( void  )

Definition at line 1142 of file dsm.c.

1143 {
1144  dlist_iter iter;
1145 
1147  {
1148  dsm_segment *seg = dlist_container(dsm_segment, node, iter.cur);
1149 
1150  /* Throw away explicit on-detach actions one by one. */
1151  while (!slist_is_empty(&seg->on_detach))
1152  {
1153  slist_node *node;
1155 
1156  node = slist_pop_head_node(&seg->on_detach);
1157  cb = slist_container(dsm_segment_detach_callback, node, node);
1158  pfree(cb);
1159  }
1160 
1161  /*
1162  * Decrementing the reference count is a sort of implicit on-detach
1163  * action; make sure we don't do that, either.
1164  */
1166  }
1167 }

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

Referenced by on_exit_reset().