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)
 

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

Referenced by dsa_allocate_extended(), and pagetable_allocate().

◆ DSA_ALLOC_NO_OOM

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

Definition at line 74 of file dsa.h.

Referenced by dsa_allocate_extended(), and dshash_create().

◆ DSA_ALLOC_ZERO

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

Definition at line 75 of file dsa.h.

Referenced by dsa_allocate_extended(), dshash_create(), and pagetable_allocate().

◆ dsa_allocate

◆ dsa_allocate0

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

Definition at line 88 of file dsa.h.

Referenced by ExecParallelHashJoinSetUpBatches(), resize(), and tbm_prepare_shared_iterate().

◆ dsa_pointer_atomic_compare_exchange

#define dsa_pointer_atomic_compare_exchange   pg_atomic_compare_exchange_u64

Definition at line 68 of file dsa.h.

Referenced by ExecParallelHashPushTuple().

◆ 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

◆ dsa_pointer_atomic_read

#define dsa_pointer_atomic_read   pg_atomic_read_u64

Definition at line 65 of file dsa.h.

Referenced by ExecParallelHashFirstTuple(), and ExecParallelHashPushTuple().

◆ dsa_pointer_atomic_write

#define dsa_pointer_atomic_write   pg_atomic_write_u64

Definition at line 66 of file dsa.h.

Referenced by ExecParallelHashIncreaseNumBatches().

◆ DSA_POINTER_FORMAT

#define DSA_POINTER_FORMAT   "%016" INT64_MODIFIER "x"

Definition at line 69 of file dsa.h.

Referenced by dsa_dump().

◆ DsaPointerIsValid

◆ InvalidDsaPointer

◆ 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 22 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 665 of file dsa.c.

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, and dsa_area_control::pools.

Referenced by dshash_create(), and pagetable_allocate().

666 {
667  uint16 size_class;
668  dsa_pointer start_pointer;
669  dsa_segment_map *segment_map;
670  dsa_pointer result;
671 
672  Assert(size > 0);
673 
674  /* Sanity check on huge individual allocation size. */
675  if (((flags & DSA_ALLOC_HUGE) != 0 && !AllocHugeSizeIsValid(size)) ||
676  ((flags & DSA_ALLOC_HUGE) == 0 && !AllocSizeIsValid(size)))
677  elog(ERROR, "invalid DSA memory alloc request size %zu", size);
678 
679  /*
680  * If bigger than the largest size class, just grab a run of pages from
681  * the free page manager, instead of allocating an object from a pool.
682  * There will still be a span, but it's a special class of span that
683  * manages this whole allocation and simply gives all pages back to the
684  * free page manager when dsa_free is called.
685  */
686  if (size > dsa_size_classes[lengthof(dsa_size_classes) - 1])
687  {
688  size_t npages = fpm_size_to_pages(size);
689  size_t first_page;
690  dsa_pointer span_pointer;
692 
693  /* Obtain a span object. */
694  span_pointer = alloc_object(area, DSA_SCLASS_BLOCK_OF_SPANS);
695  if (!DsaPointerIsValid(span_pointer))
696  {
697  /* Raise error unless asked not to. */
698  if ((flags & DSA_ALLOC_NO_OOM) == 0)
699  ereport(ERROR,
700  (errcode(ERRCODE_OUT_OF_MEMORY),
701  errmsg("out of memory"),
702  errdetail("Failed on DSA request of size %zu.",
703  size)));
704  return InvalidDsaPointer;
705  }
706 
708 
709  /* Find a segment from which to allocate. */
710  segment_map = get_best_segment(area, npages);
711  if (segment_map == NULL)
712  segment_map = make_new_segment(area, npages);
713  if (segment_map == NULL)
714  {
715  /* Can't make any more segments: game over. */
717  dsa_free(area, span_pointer);
718 
719  /* Raise error unless asked not to. */
720  if ((flags & DSA_ALLOC_NO_OOM) == 0)
721  ereport(ERROR,
722  (errcode(ERRCODE_OUT_OF_MEMORY),
723  errmsg("out of memory"),
724  errdetail("Failed on DSA request of size %zu.",
725  size)));
726  return InvalidDsaPointer;
727  }
728 
729  /*
730  * Ask the free page manager for a run of pages. This should always
731  * succeed, since both get_best_segment and make_new_segment should
732  * only return a non-NULL pointer if it actually contains enough
733  * contiguous freespace. If it does fail, something in our backend
734  * private state is out of whack, so use FATAL to kill the process.
735  */
736  if (!FreePageManagerGet(segment_map->fpm, npages, &first_page))
737  elog(FATAL,
738  "dsa_allocate could not find %zu free pages", npages);
740 
741  start_pointer = DSA_MAKE_POINTER(get_segment_index(area, segment_map),
742  first_page * FPM_PAGE_SIZE);
743 
744  /* Initialize span and pagemap. */
746  LW_EXCLUSIVE);
747  init_span(area, span_pointer, pool, start_pointer, npages,
749  segment_map->pagemap[first_page] = span_pointer;
751 
752  /* Zero-initialize the memory if requested. */
753  if ((flags & DSA_ALLOC_ZERO) != 0)
754  memset(dsa_get_address(area, start_pointer), 0, size);
755 
756  return start_pointer;
757  }
758 
759  /* Map allocation to a size class. */
761  {
762  int mapidx;
763 
764  /* For smaller sizes we have a lookup table... */
765  mapidx = ((size + DSA_SIZE_CLASS_MAP_QUANTUM - 1) /
766  DSA_SIZE_CLASS_MAP_QUANTUM) - 1;
767  size_class = dsa_size_class_map[mapidx];
768  }
769  else
770  {
771  uint16 min;
772  uint16 max;
773 
774  /* ... and for the rest we search by binary chop. */
776  max = lengthof(dsa_size_classes) - 1;
777 
778  while (min < max)
779  {
780  uint16 mid = (min + max) / 2;
781  uint16 class_size = dsa_size_classes[mid];
782 
783  if (class_size < size)
784  min = mid + 1;
785  else
786  max = mid;
787  }
788 
789  size_class = min;
790  }
791  Assert(size <= dsa_size_classes[size_class]);
792  Assert(size_class == 0 || size > dsa_size_classes[size_class - 1]);
793 
794  /* Attempt to allocate an object from the appropriate pool. */
795  result = alloc_object(area, size_class);
796 
797  /* Check for failure to allocate. */
798  if (!DsaPointerIsValid(result))
799  {
800  /* Raise error unless asked not to. */
801  if ((flags & DSA_ALLOC_NO_OOM) == 0)
802  ereport(ERROR,
803  (errcode(ERRCODE_OUT_OF_MEMORY),
804  errmsg("out of memory"),
805  errdetail("Failed on DSA request of size %zu.", size)));
806  return InvalidDsaPointer;
807  }
808 
809  /* Zero-initialize the memory if requested. */
810  if ((flags & DSA_ALLOC_ZERO) != 0)
811  memset(dsa_get_address(area, result), 0, size);
812 
813  return result;
814 }
#define DSA_ALLOC_NO_OOM
Definition: dsa.h:74
#define InvalidDsaPointer
Definition: dsa.h:78
#define DSA_ALLOC_ZERO
Definition: dsa.h:75
#define fpm_size_to_pages(sz)
Definition: freepage.h:74
int errcode(int sqlerrcode)
Definition: elog.c:570
#define lengthof(array)
Definition: c.h:662
uint64 dsa_pointer
Definition: dsa.h:62
FreePageManager * fpm
Definition: dsa.c:344
#define get_segment_index(area, segment_map_ptr)
Definition: dsa.c:381
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
bool FreePageManagerGet(FreePageManager *fpm, Size npages, Size *first_page)
Definition: freepage.c:210
unsigned short uint16
Definition: c.h:357
#define ERROR
Definition: elog.h:43
#define FATAL
Definition: elog.h:52
dsa_area_control * control
Definition: dsa.c:357
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define ereport(elevel, rest)
Definition: elog.h:141
static const uint16 dsa_size_classes[]
Definition: dsa.c:236
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define DSA_SCLASS_LOCK(area, sclass)
Definition: dsa.c:144
#define DSA_SIZE_CLASS_MAP_QUANTUM
Definition: dsa.c:269
#define FPM_PAGE_SIZE
Definition: freepage.h:30
#define DSA_SCLASS_BLOCK_OF_SPANS
Definition: dsa.c:250
#define DSA_ALLOC_HUGE
Definition: dsa.h:73
#define Assert(condition)
Definition: c.h:732
#define DSA_MAKE_POINTER(segment_number, offset)
Definition: dsa.c:114
dsa_area_pool pools[DSA_NUM_SIZE_CLASSES]
Definition: dsa.c:309
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
dsa_pointer * pagemap
Definition: dsa.c:345
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:46
static const uint8 dsa_size_class_map[]
Definition: dsa.c:259
#define DsaPointerIsValid(x)
Definition: dsa.h:81
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
int errmsg(const char *fmt,...)
Definition: elog.c:784
static dsa_segment_map * make_new_segment(dsa_area *area, size_t requested_pages)
Definition: dsa.c:2063
#define elog(elevel,...)
Definition: elog.h:226
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:1345
static dsa_pointer alloc_object(dsa_area *area, int size_class)
Definition: dsa.c:1440
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
#define DSA_SCLASS_SPAN_LARGE
Definition: dsa.c:251
static dsa_segment_map * get_best_segment(dsa_area *area, size_t npages)
Definition: dsa.c:1971

◆ dsa_attach()

dsa_area* dsa_attach ( dsa_handle  handle)

Definition at line 506 of file dsa.c.

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

507 {
508  dsm_segment *segment;
509  dsa_area *area;
510 
511  /*
512  * An area handle is really a DSM segment handle for the first segment, so
513  * we go ahead and attach to that.
514  */
515  segment = dsm_attach(handle);
516  if (segment == NULL)
517  ereport(ERROR,
518  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
519  errmsg("could not attach to dynamic shared area")));
520 
521  area = attach_internal(dsm_segment_address(segment), segment, handle);
522 
523  /* Clean up when the control segment detaches. */
526 
527  return area;
528 }
#define PointerGetDatum(X)
Definition: postgres.h:556
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:533
int errcode(int sqlerrcode)
Definition: elog.c:570
static dsa_area * attach_internal(void *place, dsm_segment *segment, dsa_handle handle)
Definition: dsa.c:1294
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:975
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
void dsa_on_dsm_detach_release_in_place(dsm_segment *segment, Datum place)
Definition: dsa.c:572
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:938
Definition: dsa.c:354
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ dsa_attach_in_place()

dsa_area* dsa_attach_in_place ( void *  place,
dsm_segment segment 
)

Definition at line 541 of file dsa.c.

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

Referenced by AttachSession(), and ParallelQueryMain().

542 {
543  dsa_area *area;
544 
545  area = attach_internal(place, NULL, DSM_HANDLE_INVALID);
546 
547  /*
548  * Clean up when the control segment detaches, if a containing DSM segment
549  * was provided.
550  */
551  if (segment != NULL)
553  PointerGetDatum(place));
554 
555  return area;
556 }
#define PointerGetDatum(X)
Definition: postgres.h:556
static dsa_area * attach_internal(void *place, dsm_segment *segment, dsa_handle handle)
Definition: dsa.c:1294
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:975
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
void dsa_on_dsm_detach_release_in_place(dsm_segment *segment, Datum place)
Definition: dsa.c:572
Definition: dsa.c:354

◆ dsa_create()

dsa_area* dsa_create ( int  tranche_id)

Definition at line 420 of file dsa.c.

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.

421 {
422  dsm_segment *segment;
423  dsa_area *area;
424 
425  /*
426  * Create the DSM segment that will hold the shared control object and the
427  * first segment of usable space.
428  */
429  segment = dsm_create(DSA_INITIAL_SEGMENT_SIZE, 0);
430 
431  /*
432  * All segments backing this area are pinned, so that DSA can explicitly
433  * control their lifetime (otherwise a newly created segment belonging to
434  * this area might be freed when the only backend that happens to have it
435  * mapped in ends, corrupting the area).
436  */
437  dsm_pin_segment(segment);
438 
439  /* Create a new DSA area with the control object in this segment. */
440  area = create_internal(dsm_segment_address(segment),
442  tranche_id,
443  dsm_segment_handle(segment), segment);
444 
445  /* Clean up when the control segment detaches. */
448 
449  return area;
450 }
#define PointerGetDatum(X)
Definition: postgres.h:556
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:966
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:975
void dsm_pin_segment(dsm_segment *seg)
Definition: dsm.c:805
#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:1191
void dsa_on_dsm_detach_release_in_place(dsm_segment *segment, Datum place)
Definition: dsa.c:572
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:437
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:938
Definition: dsa.c:354

◆ dsa_create_in_place()

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

Definition at line 469 of file dsa.c.

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

Referenced by ExecInitParallelPlan(), and GetSessionDsmHandle().

471 {
472  dsa_area *area;
473 
474  area = create_internal(place, size, tranche_id,
475  DSM_HANDLE_INVALID, NULL);
476 
477  /*
478  * Clean up when the control segment detaches, if a containing DSM segment
479  * was provided.
480  */
481  if (segment != NULL)
483  PointerGetDatum(place));
484 
485  return area;
486 }
#define PointerGetDatum(X)
Definition: postgres.h:556
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:975
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
static dsa_area * create_internal(void *place, size_t size, int tranche_id, dsm_handle control_handle, dsm_segment *control_segment)
Definition: dsa.c:1191
void dsa_on_dsm_detach_release_in_place(dsm_segment *segment, Datum place)
Definition: dsa.c:572
Definition: dsa.c:354

◆ dsa_detach()

void dsa_detach ( dsa_area area)

Definition at line 1913 of file dsa.c.

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

Referenced by DetachSession(), ExecParallelCleanup(), and ParallelQueryMain().

1914 {
1915  int i;
1916 
1917  /* Detach from all segments. */
1918  for (i = 0; i <= area->high_segment_index; ++i)
1919  if (area->segment_maps[i].segment != NULL)
1920  dsm_detach(area->segment_maps[i].segment);
1921 
1922  /*
1923  * Note that 'detaching' (= detaching from DSM segments) doesn't include
1924  * 'releasing' (= adjusting the reference count). It would be nice to
1925  * combine these operations, but client code might never get around to
1926  * calling dsa_detach because of an error path, and a detach hook on any
1927  * particular segment is too late to detach other segments in the area
1928  * without risking a 'leak' warning in the non-error path.
1929  */
1930 
1931  /* Free the backend-local area object. */
1932  pfree(area);
1933 }
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsm_segment * segment
Definition: dsa.c:341
void pfree(void *pointer)
Definition: mcxt.c:1031
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:664
int i

◆ dsa_dump()

void dsa_dump ( dsa_area area)

Definition at line 1065 of file dsa.c.

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, 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.

1066 {
1067  size_t i,
1068  j;
1069 
1070  /*
1071  * Note: This gives an inconsistent snapshot as it acquires and releases
1072  * individual locks as it goes...
1073  */
1074 
1077  fprintf(stderr, "dsa_area handle %x:\n", area->control->handle);
1078  fprintf(stderr, " max_total_segment_size: %zu\n",
1080  fprintf(stderr, " total_segment_size: %zu\n",
1081  area->control->total_segment_size);
1082  fprintf(stderr, " refcnt: %d\n", area->control->refcnt);
1083  fprintf(stderr, " pinned: %c\n", area->control->pinned ? 't' : 'f');
1084  fprintf(stderr, " segment bins:\n");
1085  for (i = 0; i < DSA_NUM_SEGMENT_BINS; ++i)
1086  {
1087  if (area->control->segment_bins[i] != DSA_SEGMENT_INDEX_NONE)
1088  {
1089  dsa_segment_index segment_index;
1090 
1091  fprintf(stderr,
1092  " segment bin %zu (at least %d contiguous pages free):\n",
1093  i, 1 << (i - 1));
1094  segment_index = area->control->segment_bins[i];
1095  while (segment_index != DSA_SEGMENT_INDEX_NONE)
1096  {
1097  dsa_segment_map *segment_map;
1098 
1099  segment_map =
1100  get_segment_by_index(area, segment_index);
1101 
1102  fprintf(stderr,
1103  " segment index %zu, usable_pages = %zu, "
1104  "contiguous_pages = %zu, mapped at %p\n",
1105  segment_index,
1106  segment_map->header->usable_pages,
1107  fpm_largest(segment_map->fpm),
1108  segment_map->mapped_address);
1109  segment_index = segment_map->header->next;
1110  }
1111  }
1112  }
1114 
1115  fprintf(stderr, " pools:\n");
1116  for (i = 0; i < DSA_NUM_SIZE_CLASSES; ++i)
1117  {
1118  bool found = false;
1119 
1121  for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1122  if (DsaPointerIsValid(area->control->pools[i].spans[j]))
1123  found = true;
1124  if (found)
1125  {
1126  if (i == DSA_SCLASS_BLOCK_OF_SPANS)
1127  fprintf(stderr, " pool for blocks of span objects:\n");
1128  else if (i == DSA_SCLASS_SPAN_LARGE)
1129  fprintf(stderr, " pool for large object spans:\n");
1130  else
1131  fprintf(stderr,
1132  " pool for size class %zu (object size %hu bytes):\n",
1133  i, dsa_size_classes[i]);
1134  for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1135  {
1136  if (!DsaPointerIsValid(area->control->pools[i].spans[j]))
1137  fprintf(stderr, " fullness class %zu is empty\n", j);
1138  else
1139  {
1140  dsa_pointer span_pointer = area->control->pools[i].spans[j];
1141 
1142  fprintf(stderr, " fullness class %zu:\n", j);
1143  while (DsaPointerIsValid(span_pointer))
1144  {
1145  dsa_area_span *span;
1146 
1147  span = dsa_get_address(area, span_pointer);
1148  fprintf(stderr,
1149  " span descriptor at "
1150  DSA_POINTER_FORMAT ", superblock at "
1152  ", pages = %zu, objects free = %hu/%hu\n",
1153  span_pointer, span->start, span->npages,
1154  span->nallocatable, span->nmax);
1155  span_pointer = span->nextspan;
1156  }
1157  }
1158  }
1159  }
1160  LWLockRelease(DSA_SCLASS_LOCK(area, i));
1161  }
1162 }
uint16 nmax
Definition: dsa.c:205
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:307
dsa_pointer nextspan
Definition: dsa.c:198
size_t usable_pages
Definition: dsa.c:156
#define DSA_NUM_SIZE_CLASSES
Definition: dsa.c:247
bool pinned
Definition: dsa.c:319
#define DSA_FULLNESS_CLASSES
Definition: dsa.c:277
dsa_pointer start
Definition: dsa.c:199
#define fprintf
Definition: port.h:196
size_t total_segment_size
Definition: dsa.c:311
size_t npages
Definition: dsa.c:200
uint64 dsa_pointer
Definition: dsa.h:62
FreePageManager * fpm
Definition: dsa.c:344
dsa_segment_header * header
Definition: dsa.c:343
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
#define DSA_POINTER_FORMAT
Definition: dsa.h:69
int refcnt
Definition: dsa.c:317
dsa_segment_index next
Definition: dsa.c:170
dsa_area_control * control
Definition: dsa.c:357
#define DSA_NUM_SEGMENT_BINS
Definition: dsa.c:133
static const uint16 dsa_size_classes[]
Definition: dsa.c:236
size_t max_total_segment_size
Definition: dsa.c:313
#define DSA_SCLASS_LOCK(area, sclass)
Definition: dsa.c:144
#define DSA_SCLASS_BLOCK_OF_SPANS
Definition: dsa.c:250
#define fpm_largest(fpm)
Definition: freepage.h:88
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
dsa_area_pool pools[DSA_NUM_SIZE_CLASSES]
Definition: dsa.c:309
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
size_t dsa_segment_index
Definition: dsa.c:124
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
char * mapped_address
Definition: dsa.c:342
#define DsaPointerIsValid(x)
Definition: dsa.h:81
uint16 nallocatable
Definition: dsa.c:203
int i
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
#define DSA_SCLASS_SPAN_LARGE
Definition: dsa.c:251
dsa_handle handle
Definition: dsa.c:303
static void check_for_freed_segments_locked(dsa_area *area)
Definition: dsa.c:2268
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1725

◆ dsa_free()

void dsa_free ( dsa_area area,
dsa_pointer  dp 
)

Definition at line 820 of file dsa.c.

References add_span_to_fullness_class(), Assert, check_for_freed_segments(), destroy_superblock(), DSA_AREA_LOCK, DSA_EXTRACT_OFFSET, DSA_EXTRACT_SEGMENT_NUMBER, dsa_free(), 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, 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(), dsa_free(), dshash_create(), dshash_destroy(), ExecHashTableDetach(), ExecHashTableDetachBatch(), ExecParallelCleanup(), ExecParallelHashIncreaseNumBatches(), ExecParallelHashIncreaseNumBuckets(), ExecParallelHashRepartitionFirst(), ExecParallelReinitialize(), find_or_make_matching_shared_tupledesc(), pagetable_free(), resize(), and tbm_free_shared_area().

821 {
822  dsa_segment_map *segment_map;
823  int pageno;
824  dsa_pointer span_pointer;
825  dsa_area_span *span;
826  char *superblock;
827  char *object;
828  size_t size;
829  int size_class;
830 
831  /* Make sure we don't have a stale segment in the slot 'dp' refers to. */
833 
834  /* Locate the object, span and pool. */
835  segment_map = get_segment_by_index(area, DSA_EXTRACT_SEGMENT_NUMBER(dp));
836  pageno = DSA_EXTRACT_OFFSET(dp) / FPM_PAGE_SIZE;
837  span_pointer = segment_map->pagemap[pageno];
838  span = dsa_get_address(area, span_pointer);
839  superblock = dsa_get_address(area, span->start);
840  object = dsa_get_address(area, dp);
841  size_class = span->size_class;
842  size = dsa_size_classes[size_class];
843 
844  /*
845  * Special case for large objects that live in a special span: we return
846  * those pages directly to the free page manager and free the span.
847  */
848  if (span->size_class == DSA_SCLASS_SPAN_LARGE)
849  {
850 
851 #ifdef CLOBBER_FREED_MEMORY
852  memset(object, 0x7f, span->npages * FPM_PAGE_SIZE);
853 #endif
854 
855  /* Give pages back to free page manager. */
857  FreePageManagerPut(segment_map->fpm,
859  span->npages);
861  /* Unlink span. */
863  LW_EXCLUSIVE);
864  unlink_span(area, span);
866  /* Free the span object so it can be reused. */
867  dsa_free(area, span_pointer);
868  return;
869  }
870 
871 #ifdef CLOBBER_FREED_MEMORY
872  memset(object, 0x7f, size);
873 #endif
874 
875  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
876 
877  /* Put the object on the span's freelist. */
878  Assert(object >= superblock);
879  Assert(object < superblock + DSA_SUPERBLOCK_SIZE);
880  Assert((object - superblock) % size == 0);
881  NextFreeObjectIndex(object) = span->firstfree;
882  span->firstfree = (object - superblock) / size;
883  ++span->nallocatable;
884 
885  /*
886  * See if the span needs to moved to a different fullness class, or be
887  * freed so its pages can be given back to the segment.
888  */
889  if (span->nallocatable == 1 && span->fclass == DSA_FULLNESS_CLASSES - 1)
890  {
891  /*
892  * The block was completely full and is located in the
893  * highest-numbered fullness class, which is never scanned for free
894  * chunks. We must move it to the next-lower fullness class.
895  */
896  unlink_span(area, span);
897  add_span_to_fullness_class(area, span, span_pointer,
899 
900  /*
901  * If this is the only span, and there is no active span, then we
902  * should probably move this span to fullness class 1. (Otherwise if
903  * you allocate exactly all the objects in the only span, it moves to
904  * class 3, then you free them all, it moves to 2, and then is given
905  * back, leaving no active span).
906  */
907  }
908  else if (span->nallocatable == span->nmax &&
909  (span->fclass != 1 || span->prevspan != InvalidDsaPointer))
910  {
911  /*
912  * This entire block is free, and it's not the active block for this
913  * size class. Return the memory to the free page manager. We don't
914  * do this for the active block to prevent hysteresis: if we
915  * repeatedly allocate and free the only chunk in the active block, it
916  * will be very inefficient if we deallocate and reallocate the block
917  * every time.
918  */
919  destroy_superblock(area, span_pointer);
920  }
921 
922  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
923 }
uint16 nmax
Definition: dsa.c:205
#define DSA_SUPERBLOCK_SIZE
Definition: dsa.c:378
#define InvalidDsaPointer
Definition: dsa.h:78
#define DSA_FULLNESS_CLASSES
Definition: dsa.c:277
dsa_pointer start
Definition: dsa.c:199
size_t npages
Definition: dsa.c:200
uint64 dsa_pointer
Definition: dsa.h:62
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
FreePageManager * fpm
Definition: dsa.c:344
uint16 fclass
Definition: dsa.c:206
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
uint16 size_class
Definition: dsa.c:201
static const uint16 dsa_size_classes[]
Definition: dsa.c:236
static void unlink_span(dsa_area *area, dsa_area_span *span)
Definition: dsa.c:1867
#define DSA_SCLASS_LOCK(area, sclass)
Definition: dsa.c:144
dsa_pointer prevspan
Definition: dsa.c:197
#define FPM_PAGE_SIZE
Definition: freepage.h:30
static void add_span_to_fullness_class(dsa_area *area, dsa_area_span *span, dsa_pointer span_pointer, int fclass)
Definition: dsa.c:1890
#define DSA_EXTRACT_SEGMENT_NUMBER(dp)
Definition: dsa.c:118
#define Assert(condition)
Definition: c.h:732
#define DSA_EXTRACT_OFFSET(dp)
Definition: dsa.c:121
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
dsa_pointer * pagemap
Definition: dsa.c:345
static void destroy_superblock(dsa_area *area, dsa_pointer span_pointer)
Definition: dsa.c:1803
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
uint16 nallocatable
Definition: dsa.c:203
static void check_for_freed_segments(dsa_area *area)
Definition: dsa.c:2232
uint16 firstfree
Definition: dsa.c:204
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
#define DSA_SCLASS_SPAN_LARGE
Definition: dsa.c:251
#define NextFreeObjectIndex(object)
Definition: dsa.c:213
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1725

◆ dsa_get_address()

void* dsa_get_address ( dsa_area area,
dsa_pointer  dp 
)

Definition at line 932 of file dsa.c.

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(), 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(), resize(), SerializeParamExecParams(), share_tupledesc(), shared_record_table_compare(), shared_record_table_hash(), tbm_attach_shared_iterate(), tbm_free_shared_area(), tbm_prepare_shared_iterate(), transfer_first_span(), and unlink_span().

933 {
935  size_t offset;
936 
937  /* Convert InvalidDsaPointer to NULL. */
938  if (!DsaPointerIsValid(dp))
939  return NULL;
940 
941  /* Process any requests to detach from freed segments. */
943 
944  /* Break the dsa_pointer into its components. */
945  index = DSA_EXTRACT_SEGMENT_NUMBER(dp);
946  offset = DSA_EXTRACT_OFFSET(dp);
947  Assert(index < DSA_MAX_SEGMENTS);
948 
949  /* Check if we need to cause this segment to be mapped in. */
950  if (unlikely(area->segment_maps[index].mapped_address == NULL))
951  {
952  /* Call for effect (we don't need the result). */
953  get_segment_by_index(area, index);
954  }
955 
956  return area->segment_maps[index].mapped_address + offset;
957 }
Definition: type.h:89
#define DSA_EXTRACT_SEGMENT_NUMBER(dp)
Definition: dsa.c:118
#define Assert(condition)
Definition: c.h:732
#define DSA_EXTRACT_OFFSET(dp)
Definition: dsa.c:121
size_t dsa_segment_index
Definition: dsa.c:124
char * mapped_address
Definition: dsa.c:342
#define DSA_MAX_SEGMENTS
Definition: dsa.c:94
#define DsaPointerIsValid(x)
Definition: dsa.h:81
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
static void check_for_freed_segments(dsa_area *area)
Definition: dsa.c:2232
#define unlikely(x)
Definition: c.h:208
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1725

◆ dsa_get_handle()

dsa_handle dsa_get_handle ( dsa_area area)

Definition at line 494 of file dsa.c.

References Assert, dsa_area::control, DSM_HANDLE_INVALID, and dsa_area_control::handle.

495 {
497  return area->control->handle;
498 }
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
dsa_area_control * control
Definition: dsa.c:357
#define Assert(condition)
Definition: c.h:732
dsa_handle handle
Definition: dsa.c:303

◆ dsa_minimum_size()

size_t dsa_minimum_size ( void  )

Definition at line 1169 of file dsa.c.

References FPM_PAGE_SIZE, and MAXALIGN.

Referenced by create_internal(), and ExecInitParallelPlan().

1170 {
1171  size_t size;
1172  int pages = 0;
1173 
1174  size = MAXALIGN(sizeof(dsa_area_control)) +
1175  MAXALIGN(sizeof(FreePageManager));
1176 
1177  /* Figure out how many pages we need, including the page map... */
1178  while (((size + FPM_PAGE_SIZE - 1) / FPM_PAGE_SIZE) > pages)
1179  {
1180  ++pages;
1181  size += sizeof(dsa_pointer);
1182  }
1183 
1184  return pages * FPM_PAGE_SIZE;
1185 }
uint64 dsa_pointer
Definition: dsa.h:62
#define FPM_PAGE_SIZE
Definition: freepage.h:30
#define MAXALIGN(LEN)
Definition: c.h:685

◆ dsa_on_dsm_detach_release_in_place()

void dsa_on_dsm_detach_release_in_place ( dsm_segment ,
Datum   
)

Definition at line 572 of file dsa.c.

References DatumGetPointer, and dsa_release_in_place().

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

573 {
575 }
void dsa_release_in_place(void *place)
Definition: dsa.c:601
#define DatumGetPointer(X)
Definition: postgres.h:549

◆ dsa_on_shmem_exit_release_in_place()

void dsa_on_shmem_exit_release_in_place ( int  ,
Datum   
)

Definition at line 586 of file dsa.c.

References DatumGetPointer, and dsa_release_in_place().

587 {
589 }
void dsa_release_in_place(void *place)
Definition: dsa.c:601
#define DatumGetPointer(X)
Definition: postgres.h:549

◆ dsa_pin()

void dsa_pin ( dsa_area area)

Definition at line 965 of file dsa.c.

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

966 {
968  if (area->control->pinned)
969  {
971  elog(ERROR, "dsa_area already pinned");
972  }
973  area->control->pinned = true;
974  ++area->control->refcnt;
976 }
bool pinned
Definition: dsa.c:319
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
int refcnt
Definition: dsa.c:317
#define ERROR
Definition: elog.h:43
dsa_area_control * control
Definition: dsa.c:357
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define elog(elevel,...)
Definition: elog.h:226
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143

◆ dsa_pin_mapping()

void dsa_pin_mapping ( dsa_area area)

Definition at line 631 of file dsa.c.

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

Referenced by AttachSession(), and GetSessionDsmHandle().

632 {
633  int i;
634 
635  Assert(!area->mapping_pinned);
636  area->mapping_pinned = true;
637 
638  for (i = 0; i <= area->high_segment_index; ++i)
639  if (area->segment_maps[i].segment != NULL)
641 }
bool mapping_pinned
Definition: dsa.c:360
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsm_segment * segment
Definition: dsa.c:341
void dsm_pin_mapping(dsm_segment *seg)
Definition: dsm.c:765
#define Assert(condition)
Definition: c.h:732
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
int i

◆ dsa_release_in_place()

void dsa_release_in_place ( void *  place)

Definition at line 601 of file dsa.c.

References Assert, dsa_area::control, 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().

602 {
603  dsa_area_control *control = (dsa_area_control *) place;
604  int i;
605 
606  LWLockAcquire(&control->lock, LW_EXCLUSIVE);
607  Assert(control->segment_header.magic ==
608  (DSA_SEGMENT_HEADER_MAGIC ^ control->handle ^ 0));
609  Assert(control->refcnt > 0);
610  if (--control->refcnt == 0)
611  {
612  for (i = 0; i <= control->high_segment_index; ++i)
613  {
614  dsm_handle handle;
615 
616  handle = control->segment_handles[i];
617  if (handle != DSM_HANDLE_INVALID)
618  dsm_unpin_segment(handle);
619  }
620  }
621  LWLockRelease(&control->lock);
622 }
uint32 dsm_handle
Definition: dsm_impl.h:54
dsa_segment_index high_segment_index
Definition: dsa.c:315
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
LWLock lock
Definition: dsa.c:325
int refcnt
Definition: dsa.c:317
void dsm_unpin_segment(dsm_handle handle)
Definition: dsm.c:837
dsa_segment_header segment_header
Definition: dsa.c:301
#define Assert(condition)
Definition: c.h:732
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int i
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
uint32 magic
Definition: dsa.c:154
dsa_handle handle
Definition: dsa.c:303

◆ dsa_set_size_limit()

void dsa_set_size_limit ( dsa_area area,
size_t  limit 
)

Definition at line 1008 of file dsa.c.

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

1009 {
1011  area->control->max_total_segment_size = limit;
1013 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
dsa_area_control * control
Definition: dsa.c:357
size_t max_total_segment_size
Definition: dsa.c:313
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143

◆ dsa_trim()

void dsa_trim ( dsa_area area)

Definition at line 1020 of file dsa.c.

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.

1021 {
1022  int size_class;
1023 
1024  /*
1025  * Trim in reverse pool order so we get to the spans-of-spans last, just
1026  * in case any become entirely free while processing all the other pools.
1027  */
1028  for (size_class = DSA_NUM_SIZE_CLASSES - 1; size_class >= 0; --size_class)
1029  {
1030  dsa_area_pool *pool = &area->control->pools[size_class];
1031  dsa_pointer span_pointer;
1032 
1033  if (size_class == DSA_SCLASS_SPAN_LARGE)
1034  {
1035  /* Large object frees give back segments aggressively already. */
1036  continue;
1037  }
1038 
1039  /*
1040  * Search fullness class 1 only. That is where we expect to find an
1041  * entirely empty superblock (entirely empty superblocks in other
1042  * fullness classes are returned to the free page map by dsa_free).
1043  */
1044  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
1045  span_pointer = pool->spans[1];
1046  while (DsaPointerIsValid(span_pointer))
1047  {
1048  dsa_area_span *span = dsa_get_address(area, span_pointer);
1049  dsa_pointer next = span->nextspan;
1050 
1051  if (span->nallocatable == span->nmax)
1052  destroy_superblock(area, span_pointer);
1053 
1054  span_pointer = next;
1055  }
1056  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
1057  }
1058 }
uint16 nmax
Definition: dsa.c:205
dsa_pointer nextspan
Definition: dsa.c:198
static int32 next
Definition: blutils.c:213
#define DSA_NUM_SIZE_CLASSES
Definition: dsa.c:247
uint64 dsa_pointer
Definition: dsa.h:62
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
dsa_area_control * control
Definition: dsa.c:357
#define DSA_SCLASS_LOCK(area, sclass)
Definition: dsa.c:144
dsa_area_pool pools[DSA_NUM_SIZE_CLASSES]
Definition: dsa.c:309
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
#define DsaPointerIsValid(x)
Definition: dsa.h:81
static void destroy_superblock(dsa_area *area, dsa_pointer span_pointer)
Definition: dsa.c:1803
uint16 nallocatable
Definition: dsa.c:203
#define DSA_SCLASS_SPAN_LARGE
Definition: dsa.c:251

◆ dsa_unpin()

void dsa_unpin ( dsa_area area)

Definition at line 984 of file dsa.c.

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

985 {
987  Assert(area->control->refcnt > 1);
988  if (!area->control->pinned)
989  {
991  elog(ERROR, "dsa_area not pinned");
992  }
993  area->control->pinned = false;
994  --area->control->refcnt;
996 }
bool pinned
Definition: dsa.c:319
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
int refcnt
Definition: dsa.c:317
#define ERROR
Definition: elog.h:43
dsa_area_control * control
Definition: dsa.c:357
#define Assert(condition)
Definition: c.h:732
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define elog(elevel,...)
Definition: elog.h:226
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143