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

Go to the source code of this file.

Macros

#define SIZEOF_DSA_POINTER   8
 
#define dsa_pointer_atomic_init   pg_atomic_init_u64
 
#define dsa_pointer_atomic_read   pg_atomic_read_u64
 
#define dsa_pointer_atomic_write   pg_atomic_write_u64
 
#define dsa_pointer_atomic_fetch_add   pg_atomic_fetch_add_u64
 
#define dsa_pointer_atomic_compare_exchange   pg_atomic_compare_exchange_u64
 
#define DSA_POINTER_FORMAT   "%016" INT64_MODIFIER "x"
 
#define DSA_ALLOC_HUGE   0x01 /* allow huge allocation (> 1 GB) */
 
#define DSA_ALLOC_NO_OOM   0x02 /* no failure if out-of-memory */
 
#define DSA_ALLOC_ZERO   0x04 /* zero allocated memory */
 
#define InvalidDsaPointer   ((dsa_pointer) 0)
 
#define DsaPointerIsValid(x)   ((x) != InvalidDsaPointer)
 
#define dsa_allocate(area, size)    dsa_allocate_extended(area, size, 0)
 
#define dsa_allocate0(area, size)    dsa_allocate_extended(area, size, DSA_ALLOC_ZERO)
 
#define DSA_HANDLE_INVALID   ((dsa_handle) DSM_HANDLE_INVALID)
 

Typedefs

typedef struct dsa_area dsa_area
 
typedef uint64 dsa_pointer
 
typedef pg_atomic_uint64 dsa_pointer_atomic
 
typedef dsm_handle dsa_handle
 

Functions

dsa_areadsa_create (int tranche_id)
 
dsa_areadsa_create_in_place (void *place, size_t size, int tranche_id, dsm_segment *segment)
 
dsa_areadsa_attach (dsa_handle handle)
 
dsa_areadsa_attach_in_place (void *place, dsm_segment *segment)
 
void dsa_release_in_place (void *place)
 
void dsa_on_dsm_detach_release_in_place (dsm_segment *, Datum)
 
void dsa_on_shmem_exit_release_in_place (int, Datum)
 
void dsa_pin_mapping (dsa_area *area)
 
void dsa_detach (dsa_area *area)
 
void dsa_pin (dsa_area *area)
 
void dsa_unpin (dsa_area *area)
 
void dsa_set_size_limit (dsa_area *area, size_t limit)
 
size_t dsa_minimum_size (void)
 
dsa_handle dsa_get_handle (dsa_area *area)
 
dsa_pointer dsa_allocate_extended (dsa_area *area, size_t size, int flags)
 
void dsa_free (dsa_area *area, dsa_pointer dp)
 
void * dsa_get_address (dsa_area *area, dsa_pointer dp)
 
size_t dsa_get_total_size (dsa_area *area)
 
void dsa_trim (dsa_area *area)
 
void dsa_dump (dsa_area *area)
 

Macro Definition Documentation

◆ DSA_ALLOC_HUGE

#define DSA_ALLOC_HUGE   0x01 /* allow huge allocation (> 1 GB) */

Definition at line 73 of file dsa.h.

◆ DSA_ALLOC_NO_OOM

#define DSA_ALLOC_NO_OOM   0x02 /* no failure if out-of-memory */

Definition at line 74 of file dsa.h.

◆ DSA_ALLOC_ZERO

#define DSA_ALLOC_ZERO   0x04 /* zero allocated memory */

Definition at line 75 of file dsa.h.

◆ dsa_allocate

#define dsa_allocate (   area,
  size 
)     dsa_allocate_extended(area, size, 0)

Definition at line 84 of file dsa.h.

◆ dsa_allocate0

#define dsa_allocate0 (   area,
  size 
)     dsa_allocate_extended(area, size, DSA_ALLOC_ZERO)

Definition at line 88 of file dsa.h.

◆ DSA_HANDLE_INVALID

#define DSA_HANDLE_INVALID   ((dsa_handle) DSM_HANDLE_INVALID)

Definition at line 103 of file dsa.h.

◆ dsa_pointer_atomic_compare_exchange

#define dsa_pointer_atomic_compare_exchange   pg_atomic_compare_exchange_u64

Definition at line 68 of file dsa.h.

◆ dsa_pointer_atomic_fetch_add

#define dsa_pointer_atomic_fetch_add   pg_atomic_fetch_add_u64

Definition at line 67 of file dsa.h.

◆ dsa_pointer_atomic_init

#define dsa_pointer_atomic_init   pg_atomic_init_u64

Definition at line 64 of file dsa.h.

◆ dsa_pointer_atomic_read

#define dsa_pointer_atomic_read   pg_atomic_read_u64

Definition at line 65 of file dsa.h.

◆ dsa_pointer_atomic_write

#define dsa_pointer_atomic_write   pg_atomic_write_u64

Definition at line 66 of file dsa.h.

◆ DSA_POINTER_FORMAT

#define DSA_POINTER_FORMAT   "%016" INT64_MODIFIER "x"

Definition at line 69 of file dsa.h.

◆ DsaPointerIsValid

#define DsaPointerIsValid (   x)    ((x) != InvalidDsaPointer)

Definition at line 81 of file dsa.h.

◆ InvalidDsaPointer

#define InvalidDsaPointer   ((dsa_pointer) 0)

Definition at line 78 of file dsa.h.

◆ SIZEOF_DSA_POINTER

#define SIZEOF_DSA_POINTER   8

Definition at line 42 of file dsa.h.

Typedef Documentation

◆ dsa_area

typedef struct dsa_area dsa_area

Definition at line 1 of file dsa.h.

◆ dsa_handle

Definition at line 100 of file dsa.h.

◆ dsa_pointer

typedef uint64 dsa_pointer

Definition at line 62 of file dsa.h.

◆ dsa_pointer_atomic

Definition at line 63 of file dsa.h.

Function Documentation

◆ dsa_allocate_extended()

dsa_pointer dsa_allocate_extended ( dsa_area area,
size_t  size,
int  flags 
)

Definition at line 684 of file dsa.c.

685 {
686  uint16 size_class;
687  dsa_pointer start_pointer;
688  dsa_segment_map *segment_map;
689  dsa_pointer result;
690 
691  Assert(size > 0);
692 
693  /* Sanity check on huge individual allocation size. */
694  if (((flags & DSA_ALLOC_HUGE) != 0 && !AllocHugeSizeIsValid(size)) ||
695  ((flags & DSA_ALLOC_HUGE) == 0 && !AllocSizeIsValid(size)))
696  elog(ERROR, "invalid DSA memory alloc request size %zu", size);
697 
698  /*
699  * If bigger than the largest size class, just grab a run of pages from
700  * the free page manager, instead of allocating an object from a pool.
701  * There will still be a span, but it's a special class of span that
702  * manages this whole allocation and simply gives all pages back to the
703  * free page manager when dsa_free is called.
704  */
706  {
707  size_t npages = fpm_size_to_pages(size);
708  size_t first_page;
709  dsa_pointer span_pointer;
711 
712  /* Obtain a span object. */
713  span_pointer = alloc_object(area, DSA_SCLASS_BLOCK_OF_SPANS);
714  if (!DsaPointerIsValid(span_pointer))
715  {
716  /* Raise error unless asked not to. */
717  if ((flags & DSA_ALLOC_NO_OOM) == 0)
718  ereport(ERROR,
719  (errcode(ERRCODE_OUT_OF_MEMORY),
720  errmsg("out of memory"),
721  errdetail("Failed on DSA request of size %zu.",
722  size)));
723  return InvalidDsaPointer;
724  }
725 
727 
728  /* Find a segment from which to allocate. */
729  segment_map = get_best_segment(area, npages);
730  if (segment_map == NULL)
731  segment_map = make_new_segment(area, npages);
732  if (segment_map == NULL)
733  {
734  /* Can't make any more segments: game over. */
736  dsa_free(area, span_pointer);
737 
738  /* Raise error unless asked not to. */
739  if ((flags & DSA_ALLOC_NO_OOM) == 0)
740  ereport(ERROR,
741  (errcode(ERRCODE_OUT_OF_MEMORY),
742  errmsg("out of memory"),
743  errdetail("Failed on DSA request of size %zu.",
744  size)));
745  return InvalidDsaPointer;
746  }
747 
748  /*
749  * Ask the free page manager for a run of pages. This should always
750  * succeed, since both get_best_segment and make_new_segment should
751  * only return a non-NULL pointer if it actually contains enough
752  * contiguous freespace. If it does fail, something in our backend
753  * private state is out of whack, so use FATAL to kill the process.
754  */
755  if (!FreePageManagerGet(segment_map->fpm, npages, &first_page))
756  elog(FATAL,
757  "dsa_allocate could not find %zu free pages", npages);
759 
760  start_pointer = DSA_MAKE_POINTER(get_segment_index(area, segment_map),
761  first_page * FPM_PAGE_SIZE);
762 
763  /* Initialize span and pagemap. */
765  LW_EXCLUSIVE);
766  init_span(area, span_pointer, pool, start_pointer, npages,
768  segment_map->pagemap[first_page] = span_pointer;
770 
771  /* Zero-initialize the memory if requested. */
772  if ((flags & DSA_ALLOC_ZERO) != 0)
773  memset(dsa_get_address(area, start_pointer), 0, size);
774 
775  return start_pointer;
776  }
777 
778  /* Map allocation to a size class. */
780  {
781  int mapidx;
782 
783  /* For smaller sizes we have a lookup table... */
784  mapidx = ((size + DSA_SIZE_CLASS_MAP_QUANTUM - 1) /
786  size_class = dsa_size_class_map[mapidx];
787  }
788  else
789  {
790  uint16 min;
791  uint16 max;
792 
793  /* ... and for the rest we search by binary chop. */
795  max = lengthof(dsa_size_classes) - 1;
796 
797  while (min < max)
798  {
799  uint16 mid = (min + max) / 2;
800  uint16 class_size = dsa_size_classes[mid];
801 
802  if (class_size < size)
803  min = mid + 1;
804  else
805  max = mid;
806  }
807 
808  size_class = min;
809  }
810  Assert(size <= dsa_size_classes[size_class]);
811  Assert(size_class == 0 || size > dsa_size_classes[size_class - 1]);
812 
813  /* Attempt to allocate an object from the appropriate pool. */
814  result = alloc_object(area, size_class);
815 
816  /* Check for failure to allocate. */
817  if (!DsaPointerIsValid(result))
818  {
819  /* Raise error unless asked not to. */
820  if ((flags & DSA_ALLOC_NO_OOM) == 0)
821  ereport(ERROR,
822  (errcode(ERRCODE_OUT_OF_MEMORY),
823  errmsg("out of memory"),
824  errdetail("Failed on DSA request of size %zu.", size)));
825  return InvalidDsaPointer;
826  }
827 
828  /* Zero-initialize the memory if requested. */
829  if ((flags & DSA_ALLOC_ZERO) != 0)
830  memset(dsa_get_address(area, result), 0, size);
831 
832  return result;
833 }
unsigned short uint16
Definition: c.h:492
#define lengthof(array)
Definition: c.h:775
static const uint16 dsa_size_classes[]
Definition: dsa.c:247
#define DSA_AREA_LOCK(area)
Definition: dsa.c:154
#define DSA_MAKE_POINTER(segment_number, offset)
Definition: dsa.c:114
#define get_segment_index(area, segment_map_ptr)
Definition: dsa.c:397
static dsa_pointer alloc_object(dsa_area *area, int size_class)
Definition: dsa.c:1477
#define DSA_SIZE_CLASS_MAP_QUANTUM
Definition: dsa.c:280
static const uint8 dsa_size_class_map[]
Definition: dsa.c:270
static dsa_segment_map * make_new_segment(dsa_area *area, size_t requested_pages)
Definition: dsa.c:2086
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:955
#define DSA_SCLASS_LOCK(area, sclass)
Definition: dsa.c:155
#define DSA_SCLASS_BLOCK_OF_SPANS
Definition: dsa.c:261
#define DSA_SCLASS_SPAN_LARGE
Definition: dsa.c:262
static void init_span(dsa_area *area, dsa_pointer span_pointer, dsa_area_pool *pool, dsa_pointer start, size_t npages, uint16 size_class)
Definition: dsa.c:1382
static dsa_segment_map * get_best_segment(dsa_area *area, size_t npages)
Definition: dsa.c:2015
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:839
uint64 dsa_pointer
Definition: dsa.h:62
#define InvalidDsaPointer
Definition: dsa.h:78
#define DSA_ALLOC_NO_OOM
Definition: dsa.h:74
#define DsaPointerIsValid(x)
Definition: dsa.h:81
#define DSA_ALLOC_HUGE
Definition: dsa.h:73
#define DSA_ALLOC_ZERO
Definition: dsa.h:75
int errdetail(const char *fmt,...)
Definition: elog.c:1205
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define FATAL
Definition: elog.h:41
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
bool FreePageManagerGet(FreePageManager *fpm, Size npages, Size *first_page)
Definition: freepage.c:210
#define fpm_size_to_pages(sz)
Definition: freepage.h:74
#define FPM_PAGE_SIZE
Definition: freepage.h:30
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1172
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1785
@ LW_EXCLUSIVE
Definition: lwlock.h:116
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:49
#define AllocSizeIsValid(size)
Definition: memutils.h:42
static pg_noinline void Size size
Definition: slab.c:607
dsa_area_pool pools[DSA_NUM_SIZE_CLASSES]
Definition: dsa.c:320
dsa_area_control * control
Definition: dsa.c:368
FreePageManager * fpm
Definition: dsa.c:355
dsa_pointer * pagemap
Definition: dsa.c:356

References alloc_object(), AllocHugeSizeIsValid, AllocSizeIsValid, Assert(), dsa_area::control, DSA_ALLOC_HUGE, DSA_ALLOC_NO_OOM, DSA_ALLOC_ZERO, DSA_AREA_LOCK, dsa_free(), dsa_get_address(), DSA_MAKE_POINTER, DSA_SCLASS_BLOCK_OF_SPANS, DSA_SCLASS_LOCK, DSA_SCLASS_SPAN_LARGE, dsa_size_class_map, DSA_SIZE_CLASS_MAP_QUANTUM, dsa_size_classes, DsaPointerIsValid, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, FATAL, dsa_segment_map::fpm, FPM_PAGE_SIZE, fpm_size_to_pages, FreePageManagerGet(), get_best_segment(), get_segment_index, init_span(), InvalidDsaPointer, lengthof, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), make_new_segment(), dsa_segment_map::pagemap, dsa_area_control::pools, and size.

Referenced by dshash_create(), and pagetable_allocate().

◆ dsa_attach()

dsa_area* dsa_attach ( dsa_handle  handle)

Definition at line 523 of file dsa.c.

524 {
525  dsm_segment *segment;
526  dsa_area *area;
527 
528  /*
529  * An area handle is really a DSM segment handle for the first segment, so
530  * we go ahead and attach to that.
531  */
532  segment = dsm_attach(handle);
533  if (segment == NULL)
534  ereport(ERROR,
535  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
536  errmsg("could not attach to dynamic shared area")));
537 
538  area = attach_internal(dsm_segment_address(segment), segment, handle);
539 
540  /* Clean up when the control segment detaches. */
543 
544  return area;
545 }
void dsa_on_dsm_detach_release_in_place(dsm_segment *segment, Datum place)
Definition: dsa.c:589
static dsa_area * attach_internal(void *place, dsm_segment *segment, dsa_handle handle)
Definition: dsa.c:1331
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1095
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1132
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:665
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
Definition: dsa.c:366

References attach_internal(), dsa_on_dsm_detach_release_in_place(), dsm_attach(), dsm_segment_address(), ereport, errcode(), errmsg(), ERROR, on_dsm_detach(), and PointerGetDatum().

Referenced by init_dsm_registry(), and logicalrep_launcher_attach_dshmem().

◆ dsa_attach_in_place()

dsa_area* dsa_attach_in_place ( void *  place,
dsm_segment segment 
)

Definition at line 558 of file dsa.c.

559 {
560  dsa_area *area;
561 
562  area = attach_internal(place, NULL, DSA_HANDLE_INVALID);
563 
564  /*
565  * Clean up when the control segment detaches, if a containing DSM segment
566  * was provided.
567  */
568  if (segment != NULL)
570  PointerGetDatum(place));
571 
572  return area;
573 }
#define DSA_HANDLE_INVALID
Definition: dsa.h:103

References attach_internal(), DSA_HANDLE_INVALID, dsa_on_dsm_detach_release_in_place(), on_dsm_detach(), and PointerGetDatum().

Referenced by AttachSession(), ParallelQueryMain(), and pgstat_attach_shmem().

◆ dsa_create()

dsa_area* dsa_create ( int  tranche_id)

Definition at line 437 of file dsa.c.

438 {
439  dsm_segment *segment;
440  dsa_area *area;
441 
442  /*
443  * Create the DSM segment that will hold the shared control object and the
444  * first segment of usable space.
445  */
446  segment = dsm_create(DSA_INITIAL_SEGMENT_SIZE, 0);
447 
448  /*
449  * All segments backing this area are pinned, so that DSA can explicitly
450  * control their lifetime (otherwise a newly created segment belonging to
451  * this area might be freed when the only backend that happens to have it
452  * mapped in ends, corrupting the area).
453  */
454  dsm_pin_segment(segment);
455 
456  /* Create a new DSA area with the control object in this segment. */
457  area = create_internal(dsm_segment_address(segment),
459  tranche_id,
460  dsm_segment_handle(segment), segment);
461 
462  /* Clean up when the control segment detaches. */
465 
466  return area;
467 }
#define DSA_INITIAL_SEGMENT_SIZE
Definition: dsa.c:68
static dsa_area * create_internal(void *place, size_t size, int tranche_id, dsm_handle control_handle, dsm_segment *control_segment)
Definition: dsa.c:1231
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1123
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:516
void dsm_pin_segment(dsm_segment *seg)
Definition: dsm.c:955

References create_internal(), DSA_INITIAL_SEGMENT_SIZE, dsa_on_dsm_detach_release_in_place(), dsm_create(), dsm_pin_segment(), dsm_segment_address(), dsm_segment_handle(), on_dsm_detach(), and PointerGetDatum().

Referenced by init_dsm_registry(), logicalrep_launcher_attach_dshmem(), test_basic(), test_dsa_basic(), test_dsa_resowners(), test_empty(), and test_random().

◆ dsa_create_in_place()

dsa_area* dsa_create_in_place ( void *  place,
size_t  size,
int  tranche_id,
dsm_segment segment 
)

Definition at line 486 of file dsa.c.

488 {
489  dsa_area *area;
490 
491  area = create_internal(place, size, tranche_id,
492  DSM_HANDLE_INVALID, NULL);
493 
494  /*
495  * Clean up when the control segment detaches, if a containing DSM segment
496  * was provided.
497  */
498  if (segment != NULL)
500  PointerGetDatum(place));
501 
502  return area;
503 }
#define DSM_HANDLE_INVALID
Definition: dsm_impl.h:58

References create_internal(), dsa_on_dsm_detach_release_in_place(), DSM_HANDLE_INVALID, on_dsm_detach(), PointerGetDatum(), and size.

Referenced by ExecInitParallelPlan(), GetSessionDsmHandle(), and StatsShmemInit().

◆ dsa_detach()

void dsa_detach ( dsa_area area)

Definition at line 1957 of file dsa.c.

1958 {
1959  int i;
1960 
1961  /* Detach from all segments. */
1962  for (i = 0; i <= area->high_segment_index; ++i)
1963  if (area->segment_maps[i].segment != NULL)
1964  dsm_detach(area->segment_maps[i].segment);
1965 
1966  /*
1967  * Note that 'detaching' (= detaching from DSM segments) doesn't include
1968  * 'releasing' (= adjusting the reference count). It would be nice to
1969  * combine these operations, but client code might never get around to
1970  * calling dsa_detach because of an error path, and a detach hook on any
1971  * particular segment is too late to detach other segments in the area
1972  * without risking a 'leak' warning in the non-error path.
1973  */
1974 
1975  /* Free the backend-local area object. */
1976  pfree(area);
1977 }
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:803
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1508
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:384
dsa_segment_index high_segment_index
Definition: dsa.c:387
dsm_segment * segment
Definition: dsa.c:352

References dsm_detach(), dsa_area::high_segment_index, i, pfree(), dsa_segment_map::segment, and dsa_area::segment_maps.

Referenced by DetachSession(), ExecParallelCleanup(), ParallelQueryMain(), pgstat_detach_shmem(), StatsShmemInit(), test_basic(), test_dsa_basic(), test_dsa_resowners(), test_empty(), and test_random().

◆ dsa_dump()

void dsa_dump ( dsa_area area)

Definition at line 1101 of file dsa.c.

1102 {
1103  size_t i,
1104  j;
1105 
1106  /*
1107  * Note: This gives an inconsistent snapshot as it acquires and releases
1108  * individual locks as it goes...
1109  */
1110 
1113  fprintf(stderr, "dsa_area handle %x:\n", area->control->handle);
1114  fprintf(stderr, " max_total_segment_size: %zu\n",
1116  fprintf(stderr, " total_segment_size: %zu\n",
1117  area->control->total_segment_size);
1118  fprintf(stderr, " refcnt: %d\n", area->control->refcnt);
1119  fprintf(stderr, " pinned: %c\n", area->control->pinned ? 't' : 'f');
1120  fprintf(stderr, " segment bins:\n");
1121  for (i = 0; i < DSA_NUM_SEGMENT_BINS; ++i)
1122  {
1124  {
1125  dsa_segment_index segment_index;
1126 
1127  if (i == 0)
1128  fprintf(stderr,
1129  " segment bin %zu (no contiguous free pages):\n", i);
1130  else
1131  fprintf(stderr,
1132  " segment bin %zu (at least %d contiguous pages free):\n",
1133  i, 1 << (i - 1));
1134  segment_index = area->control->segment_bins[i];
1135  while (segment_index != DSA_SEGMENT_INDEX_NONE)
1136  {
1137  dsa_segment_map *segment_map;
1138 
1139  segment_map =
1140  get_segment_by_index(area, segment_index);
1141 
1142  fprintf(stderr,
1143  " segment index %zu, usable_pages = %zu, "
1144  "contiguous_pages = %zu, mapped at %p\n",
1145  segment_index,
1146  segment_map->header->usable_pages,
1147  fpm_largest(segment_map->fpm),
1148  segment_map->mapped_address);
1149  segment_index = segment_map->header->next;
1150  }
1151  }
1152  }
1154 
1155  fprintf(stderr, " pools:\n");
1156  for (i = 0; i < DSA_NUM_SIZE_CLASSES; ++i)
1157  {
1158  bool found = false;
1159 
1161  for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1162  if (DsaPointerIsValid(area->control->pools[i].spans[j]))
1163  found = true;
1164  if (found)
1165  {
1167  fprintf(stderr, " pool for blocks of span objects:\n");
1168  else if (i == DSA_SCLASS_SPAN_LARGE)
1169  fprintf(stderr, " pool for large object spans:\n");
1170  else
1171  fprintf(stderr,
1172  " pool for size class %zu (object size %hu bytes):\n",
1173  i, dsa_size_classes[i]);
1174  for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1175  {
1176  if (!DsaPointerIsValid(area->control->pools[i].spans[j]))
1177  fprintf(stderr, " fullness class %zu is empty\n", j);
1178  else
1179  {
1180  dsa_pointer span_pointer = area->control->pools[i].spans[j];
1181 
1182  fprintf(stderr, " fullness class %zu:\n", j);
1183  while (DsaPointerIsValid(span_pointer))
1184  {
1185  dsa_area_span *span;
1186 
1187  span = dsa_get_address(area, span_pointer);
1188  fprintf(stderr,
1189  " span descriptor at "
1190  DSA_POINTER_FORMAT ", superblock at "
1192  ", pages = %zu, objects free = %hu/%hu\n",
1193  span_pointer, span->start, span->npages,
1194  span->nallocatable, span->nmax);
1195  span_pointer = span->nextspan;
1196  }
1197  }
1198  }
1199  }
1201  }
1202 }
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
static void check_for_freed_segments_locked(dsa_area *area)
Definition: dsa.c:2293
size_t dsa_segment_index
Definition: dsa.c:124
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1762
#define DSA_NUM_SIZE_CLASSES
Definition: dsa.c:258
#define DSA_FULLNESS_CLASSES
Definition: dsa.c:288
#define DSA_NUM_SEGMENT_BINS
Definition: dsa.c:133
#define DSA_POINTER_FORMAT
Definition: dsa.h:69
#define fpm_largest(fpm)
Definition: freepage.h:88
int j
Definition: isn.c:74
#define fprintf
Definition: port.h:242
size_t total_segment_size
Definition: dsa.c:322
bool pinned
Definition: dsa.c:330
size_t max_total_segment_size
Definition: dsa.c:324
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:318
int refcnt
Definition: dsa.c:328
dsa_handle handle
Definition: dsa.c:314
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:301
dsa_pointer nextspan
Definition: dsa.c:209
dsa_pointer start
Definition: dsa.c:210
uint16 nallocatable
Definition: dsa.c:214
uint16 nmax
Definition: dsa.c:216
size_t npages
Definition: dsa.c:211
dsa_segment_index next
Definition: dsa.c:181
size_t usable_pages
Definition: dsa.c:167
dsa_segment_header * header
Definition: dsa.c:354
char * mapped_address
Definition: dsa.c:353

References check_for_freed_segments_locked(), dsa_area::control, DSA_AREA_LOCK, DSA_FULLNESS_CLASSES, dsa_get_address(), DSA_NUM_SEGMENT_BINS, DSA_NUM_SIZE_CLASSES, DSA_POINTER_FORMAT, DSA_SCLASS_BLOCK_OF_SPANS, DSA_SCLASS_LOCK, DSA_SCLASS_SPAN_LARGE, DSA_SEGMENT_INDEX_NONE, dsa_size_classes, DsaPointerIsValid, dsa_segment_map::fpm, fpm_largest, fprintf, get_segment_by_index(), dsa_area_control::handle, dsa_segment_map::header, i, j, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_segment_map::mapped_address, dsa_area_control::max_total_segment_size, dsa_area_span::nallocatable, dsa_segment_header::next, dsa_area_span::nextspan, dsa_area_span::nmax, dsa_area_span::npages, dsa_area_control::pinned, dsa_area_control::pools, dsa_area_control::refcnt, dsa_area_control::segment_bins, dsa_area_pool::spans, dsa_area_span::start, dsa_area_control::total_segment_size, and dsa_segment_header::usable_pages.

◆ dsa_free()

void dsa_free ( dsa_area area,
dsa_pointer  dp 
)

Definition at line 839 of file dsa.c.

840 {
841  dsa_segment_map *segment_map;
842  int pageno;
843  dsa_pointer span_pointer;
844  dsa_area_span *span;
845  char *superblock;
846  char *object;
847  size_t size;
848  int size_class;
849 
850  /* Make sure we don't have a stale segment in the slot 'dp' refers to. */
852 
853  /* Locate the object, span and pool. */
854  segment_map = get_segment_by_index(area, DSA_EXTRACT_SEGMENT_NUMBER(dp));
855  pageno = DSA_EXTRACT_OFFSET(dp) / FPM_PAGE_SIZE;
856  span_pointer = segment_map->pagemap[pageno];
857  span = dsa_get_address(area, span_pointer);
858  superblock = dsa_get_address(area, span->start);
859  object = dsa_get_address(area, dp);
860  size_class = span->size_class;
861  size = dsa_size_classes[size_class];
862 
863  /*
864  * Special case for large objects that live in a special span: we return
865  * those pages directly to the free page manager and free the span.
866  */
867  if (span->size_class == DSA_SCLASS_SPAN_LARGE)
868  {
869 
870 #ifdef CLOBBER_FREED_MEMORY
871  memset(object, 0x7f, span->npages * FPM_PAGE_SIZE);
872 #endif
873 
874  /* Give pages back to free page manager. */
876  FreePageManagerPut(segment_map->fpm,
878  span->npages);
879 
880  /* Move segment to appropriate bin if necessary. */
881  rebin_segment(area, segment_map);
883 
884  /* Unlink span. */
886  LW_EXCLUSIVE);
887  unlink_span(area, span);
889  /* Free the span object so it can be reused. */
890  dsa_free(area, span_pointer);
891  return;
892  }
893 
894 #ifdef CLOBBER_FREED_MEMORY
895  memset(object, 0x7f, size);
896 #endif
897 
898  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
899 
900  /* Put the object on the span's freelist. */
901  Assert(object >= superblock);
902  Assert(object < superblock + DSA_SUPERBLOCK_SIZE);
903  Assert((object - superblock) % size == 0);
904  NextFreeObjectIndex(object) = span->firstfree;
905  span->firstfree = (object - superblock) / size;
906  ++span->nallocatable;
907 
908  /*
909  * See if the span needs to moved to a different fullness class, or be
910  * freed so its pages can be given back to the segment.
911  */
912  if (span->nallocatable == 1 && span->fclass == DSA_FULLNESS_CLASSES - 1)
913  {
914  /*
915  * The block was completely full and is located in the
916  * highest-numbered fullness class, which is never scanned for free
917  * chunks. We must move it to the next-lower fullness class.
918  */
919  unlink_span(area, span);
920  add_span_to_fullness_class(area, span, span_pointer,
922 
923  /*
924  * If this is the only span, and there is no active span, then we
925  * should probably move this span to fullness class 1. (Otherwise if
926  * you allocate exactly all the objects in the only span, it moves to
927  * class 3, then you free them all, it moves to 2, and then is given
928  * back, leaving no active span).
929  */
930  }
931  else if (span->nallocatable == span->nmax &&
932  (span->fclass != 1 || span->prevspan != InvalidDsaPointer))
933  {
934  /*
935  * This entire block is free, and it's not the active block for this
936  * size class. Return the memory to the free page manager. We don't
937  * do this for the active block to prevent hysteresis: if we
938  * repeatedly allocate and free the only chunk in the active block, it
939  * will be very inefficient if we deallocate and reallocate the block
940  * every time.
941  */
942  destroy_superblock(area, span_pointer);
943  }
944 
945  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
946 }
static void check_for_freed_segments(dsa_area *area)
Definition: dsa.c:2257
#define DSA_EXTRACT_SEGMENT_NUMBER(dp)
Definition: dsa.c:118
static void add_span_to_fullness_class(dsa_area *area, dsa_area_span *span, dsa_pointer span_pointer, int fclass)
Definition: dsa.c:1934
#define DSA_SUPERBLOCK_SIZE
Definition: dsa.c:394
#define DSA_EXTRACT_OFFSET(dp)
Definition: dsa.c:121
static void destroy_superblock(dsa_area *area, dsa_pointer span_pointer)
Definition: dsa.c:1842
static void rebin_segment(dsa_area *area, dsa_segment_map *segment_map)
Definition: dsa.c:2321
static void unlink_span(dsa_area *area, dsa_area_span *span)
Definition: dsa.c:1911
#define NextFreeObjectIndex(object)
Definition: dsa.c:224
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
uint16 fclass
Definition: dsa.c:217
dsa_pointer prevspan
Definition: dsa.c:208
uint16 size_class
Definition: dsa.c:212
uint16 firstfree
Definition: dsa.c:215

References add_span_to_fullness_class(), Assert(), check_for_freed_segments(), destroy_superblock(), DSA_AREA_LOCK, DSA_EXTRACT_OFFSET, DSA_EXTRACT_SEGMENT_NUMBER, DSA_FULLNESS_CLASSES, dsa_get_address(), DSA_SCLASS_LOCK, DSA_SCLASS_SPAN_LARGE, dsa_size_classes, DSA_SUPERBLOCK_SIZE, dsa_area_span::fclass, dsa_area_span::firstfree, dsa_segment_map::fpm, FPM_PAGE_SIZE, FreePageManagerPut(), get_segment_by_index(), InvalidDsaPointer, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_area_span::nallocatable, NextFreeObjectIndex, dsa_area_span::nmax, dsa_area_span::npages, dsa_segment_map::pagemap, dsa_area_span::prevspan, rebin_segment(), size, dsa_area_span::size_class, dsa_area_span::start, and unlink_span().

Referenced by delete_item_from_bucket(), delete_key_from_bucket(), destroy_superblock(), dsa_allocate_extended(), dshash_create(), dshash_destroy(), ExecHashTableDetach(), ExecHashTableDetachBatch(), ExecParallelCleanup(), ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ExecParallelHashRepartitionFirst(), ExecParallelReinitialize(), find_or_make_matching_shared_tupledesc(), pagetable_free(), pgstat_free_entry(), resize(), tbm_free_shared_area(), test_dsa_basic(), and test_dsa_resowners().

◆ dsa_get_address()

void* dsa_get_address ( dsa_area area,
dsa_pointer  dp 
)

Definition at line 955 of file dsa.c.

956 {
958  size_t offset;
959 
960  /* Convert InvalidDsaPointer to NULL. */
961  if (!DsaPointerIsValid(dp))
962  return NULL;
963 
964  /* Process any requests to detach from freed segments. */
966 
967  /* Break the dsa_pointer into its components. */
969  offset = DSA_EXTRACT_OFFSET(dp);
971 
972  /* Check if we need to cause this segment to be mapped in. */
973  if (unlikely(area->segment_maps[index].mapped_address == NULL))
974  {
975  /* Call for effect (we don't need the result). */
977  }
978 
979  return area->segment_maps[index].mapped_address + offset;
980 }
#define unlikely(x)
Definition: c.h:298
#define DSA_MAX_SEGMENTS
Definition: dsa.c:94
Definition: type.h:95

References Assert(), check_for_freed_segments(), DSA_EXTRACT_OFFSET, DSA_EXTRACT_SEGMENT_NUMBER, DSA_MAX_SEGMENTS, DsaPointerIsValid, get_segment_by_index(), dsa_segment_map::mapped_address, dsa_area::segment_maps, and unlikely.

Referenced by add_span_to_fullness_class(), alloc_object(), delete_item_from_bucket(), delete_key_from_bucket(), destroy_superblock(), dsa_allocate_extended(), dsa_dump(), dsa_free(), dsa_trim(), dshash_attach(), dshash_create(), dshash_destroy(), dshash_dump(), dshash_seq_next(), ensure_active_superblock(), ensure_valid_bucket_pointers(), ExecHashTableDetachBatch(), ExecParallelHashEnsureBatchAccessors(), ExecParallelHashFirstTuple(), ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ExecParallelHashJoinSetUpBatches(), ExecParallelHashNextTuple(), ExecParallelHashPopChunkQueue(), ExecParallelHashRepartitionRest(), ExecParallelHashTableAlloc(), ExecParallelHashTableSetCurrentBatch(), ExecParallelHashTupleAlloc(), find_in_bucket(), find_or_make_matching_shared_tupledesc(), init_span(), insert_into_bucket(), insert_item_into_bucket(), lookup_rowtype_tupdesc_internal(), pagetable_allocate(), ParallelQueryMain(), pgstat_build_snapshot(), pgstat_get_entry_ref(), pgstat_init_entry(), pgstat_reinit_entry(), pgstat_reset_matching_entries(), pgstat_write_statsfile(), resize(), SerializeParamExecParams(), share_tupledesc(), shared_record_table_compare(), shared_record_table_hash(), tbm_attach_shared_iterate(), tbm_free_shared_area(), tbm_prepare_shared_iterate(), test_dsa_basic(), test_dsa_resowners(), transfer_first_span(), and unlink_span().

◆ dsa_get_handle()

dsa_handle dsa_get_handle ( dsa_area area)

Definition at line 511 of file dsa.c.

512 {
514  return area->control->handle;
515 }

References Assert(), dsa_area::control, DSA_HANDLE_INVALID, and dsa_area_control::handle.

Referenced by init_dsm_registry(), and logicalrep_launcher_attach_dshmem().

◆ dsa_get_total_size()

size_t dsa_get_total_size ( dsa_area area)

Definition at line 1040 of file dsa.c.

1041 {
1042  size_t size;
1043 
1045  size = area->control->total_segment_size;
1047 
1048  return size;
1049 }

References dsa_area::control, DSA_AREA_LOCK, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), size, and dsa_area_control::total_segment_size.

◆ dsa_minimum_size()

size_t dsa_minimum_size ( void  )

Definition at line 1209 of file dsa.c.

1210 {
1211  size_t size;
1212  int pages = 0;
1213 
1214  size = MAXALIGN(sizeof(dsa_area_control)) +
1215  MAXALIGN(sizeof(FreePageManager));
1216 
1217  /* Figure out how many pages we need, including the page map... */
1218  while (((size + FPM_PAGE_SIZE - 1) / FPM_PAGE_SIZE) > pages)
1219  {
1220  ++pages;
1221  size += sizeof(dsa_pointer);
1222  }
1223 
1224  return pages * FPM_PAGE_SIZE;
1225 }
#define MAXALIGN(LEN)
Definition: c.h:798

References FPM_PAGE_SIZE, MAXALIGN, and size.

Referenced by create_internal(), ExecInitParallelPlan(), and pgstat_dsa_init_size().

◆ dsa_on_dsm_detach_release_in_place()

void dsa_on_dsm_detach_release_in_place ( dsm_segment segment,
Datum  place 
)

Definition at line 589 of file dsa.c.

590 {
592 }
void dsa_release_in_place(void *place)
Definition: dsa.c:618
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312

References DatumGetPointer(), and dsa_release_in_place().

Referenced by dsa_attach(), dsa_attach_in_place(), dsa_create(), and dsa_create_in_place().

◆ dsa_on_shmem_exit_release_in_place()

void dsa_on_shmem_exit_release_in_place ( int  code,
Datum  place 
)

Definition at line 603 of file dsa.c.

604 {
606 }

References DatumGetPointer(), and dsa_release_in_place().

◆ dsa_pin()

void dsa_pin ( dsa_area area)

Definition at line 988 of file dsa.c.

989 {
991  if (area->control->pinned)
992  {
994  elog(ERROR, "dsa_area already pinned");
995  }
996  area->control->pinned = true;
997  ++area->control->refcnt;
999 }

References dsa_area::control, DSA_AREA_LOCK, elog, ERROR, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_area_control::pinned, and dsa_area_control::refcnt.

Referenced by init_dsm_registry(), logicalrep_launcher_attach_dshmem(), and StatsShmemInit().

◆ dsa_pin_mapping()

void dsa_pin_mapping ( dsa_area area)

Definition at line 648 of file dsa.c.

649 {
650  int i;
651 
652  if (area->resowner != NULL)
653  {
654  area->resowner = NULL;
655 
656  for (i = 0; i <= area->high_segment_index; ++i)
657  if (area->segment_maps[i].segment != NULL)
659  }
660 }
void dsm_pin_mapping(dsm_segment *seg)
Definition: dsm.c:915
ResourceOwner resowner
Definition: dsa.c:376

References dsm_pin_mapping(), dsa_area::high_segment_index, i, dsa_area::resowner, dsa_segment_map::segment, and dsa_area::segment_maps.

Referenced by AttachSession(), GetSessionDsmHandle(), init_dsm_registry(), logicalrep_launcher_attach_dshmem(), and pgstat_attach_shmem().

◆ dsa_release_in_place()

void dsa_release_in_place ( void *  place)

Definition at line 618 of file dsa.c.

619 {
620  dsa_area_control *control = (dsa_area_control *) place;
621  int i;
622 
623  LWLockAcquire(&control->lock, LW_EXCLUSIVE);
624  Assert(control->segment_header.magic ==
625  (DSA_SEGMENT_HEADER_MAGIC ^ control->handle ^ 0));
626  Assert(control->refcnt > 0);
627  if (--control->refcnt == 0)
628  {
629  for (i = 0; i <= control->high_segment_index; ++i)
630  {
631  dsm_handle handle;
632 
633  handle = control->segment_handles[i];
634  if (handle != DSM_HANDLE_INVALID)
635  dsm_unpin_segment(handle);
636  }
637  }
638  LWLockRelease(&control->lock);
639 }
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
void dsm_unpin_segment(dsm_handle handle)
Definition: dsm.c:988
uint32 dsm_handle
Definition: dsm_impl.h:55
dsa_segment_header segment_header
Definition: dsa.c:312
dsa_segment_index high_segment_index
Definition: dsa.c:326
LWLock lock
Definition: dsa.c:336
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:316
uint32 magic
Definition: dsa.c:165

References Assert(), DSA_SEGMENT_HEADER_MAGIC, DSM_HANDLE_INVALID, dsm_unpin_segment(), dsa_area_control::handle, dsa_area_control::high_segment_index, i, dsa_area_control::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_segment_header::magic, dsa_area_control::refcnt, dsa_area_control::segment_handles, and dsa_area_control::segment_header.

Referenced by dsa_on_dsm_detach_release_in_place(), and dsa_on_shmem_exit_release_in_place().

◆ dsa_set_size_limit()

void dsa_set_size_limit ( dsa_area area,
size_t  limit 
)

◆ dsa_trim()

void dsa_trim ( dsa_area area)

Definition at line 1056 of file dsa.c.

1057 {
1058  int size_class;
1059 
1060  /*
1061  * Trim in reverse pool order so we get to the spans-of-spans last, just
1062  * in case any become entirely free while processing all the other pools.
1063  */
1064  for (size_class = DSA_NUM_SIZE_CLASSES - 1; size_class >= 0; --size_class)
1065  {
1066  dsa_area_pool *pool = &area->control->pools[size_class];
1067  dsa_pointer span_pointer;
1068 
1069  if (size_class == DSA_SCLASS_SPAN_LARGE)
1070  {
1071  /* Large object frees give back segments aggressively already. */
1072  continue;
1073  }
1074 
1075  /*
1076  * Search fullness class 1 only. That is where we expect to find an
1077  * entirely empty superblock (entirely empty superblocks in other
1078  * fullness classes are returned to the free page map by dsa_free).
1079  */
1080  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
1081  span_pointer = pool->spans[1];
1082  while (DsaPointerIsValid(span_pointer))
1083  {
1084  dsa_area_span *span = dsa_get_address(area, span_pointer);
1085  dsa_pointer next = span->nextspan;
1086 
1087  if (span->nallocatable == span->nmax)
1088  destroy_superblock(area, span_pointer);
1089 
1090  span_pointer = next;
1091  }
1092  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
1093  }
1094 }
static int32 next
Definition: blutils.c:221

References dsa_area::control, destroy_superblock(), dsa_get_address(), DSA_NUM_SIZE_CLASSES, DSA_SCLASS_LOCK, DSA_SCLASS_SPAN_LARGE, DsaPointerIsValid, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_area_span::nallocatable, next, dsa_area_span::nextspan, dsa_area_span::nmax, dsa_area_control::pools, and dsa_area_pool::spans.

◆ dsa_unpin()

void dsa_unpin ( dsa_area area)

Definition at line 1007 of file dsa.c.

1008 {
1010  Assert(area->control->refcnt > 1);
1011  if (!area->control->pinned)
1012  {
1014  elog(ERROR, "dsa_area not pinned");
1015  }
1016  area->control->pinned = false;
1017  --area->control->refcnt;
1019 }

References Assert(), dsa_area::control, DSA_AREA_LOCK, elog, ERROR, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_area_control::pinned, and dsa_area_control::refcnt.