PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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" PRIx64
 
#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 DSA_OFFSET_WIDTH   40 /* 1024 segments of size up to 1TB */
 
#define DSA_DEFAULT_INIT_SEGMENT_SIZE   ((size_t) (1 * 1024 * 1024))
 
#define DSA_MIN_SEGMENT_SIZE   ((size_t) (256 * 1024))
 
#define DSA_MAX_SEGMENT_SIZE   ((size_t) 1 << DSA_OFFSET_WIDTH)
 
#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_create(tranche_id)
 
#define dsa_create_in_place(place, size, tranche_id, segment)
 
#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_ext (int tranche_id, size_t init_segment_size, size_t max_segment_size)
 
dsa_areadsa_create_in_place_ext (void *place, size_t size, int tranche_id, dsm_segment *segment, size_t init_segment_size, size_t max_segment_size)
 
dsa_areadsa_attach (dsa_handle handle)
 
bool dsa_is_attached (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)
 
voiddsa_get_address (dsa_area *area, dsa_pointer dp)
 
size_t dsa_get_total_size (dsa_area *area)
 
size_t dsa_get_total_size_from_handle (dsa_handle handle)
 
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 109 of file dsa.h.

◆ dsa_allocate0

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

Definition at line 113 of file dsa.h.

◆ dsa_create

#define dsa_create (   tranche_id)
Value:
#define DSA_DEFAULT_INIT_SEGMENT_SIZE
Definition dsa.h:97
dsa_area * dsa_create_ext(int tranche_id, size_t init_segment_size, size_t max_segment_size)
Definition dsa.c:421
#define DSA_MAX_SEGMENT_SIZE
Definition dsa.h:103

Definition at line 117 of file dsa.h.

◆ dsa_create_in_place

#define dsa_create_in_place (   place,
  size,
  tranche_id,
  segment 
)
Value:
dsa_create_in_place_ext(place, size, tranche_id, segment, \
dsa_area * dsa_create_in_place_ext(void *place, size_t size, int tranche_id, dsm_segment *segment, size_t init_segment_size, size_t max_segment_size)
Definition dsa.c:471

Definition at line 122 of file dsa.h.

◆ DSA_DEFAULT_INIT_SEGMENT_SIZE

#define DSA_DEFAULT_INIT_SEGMENT_SIZE   ((size_t) (1 * 1024 * 1024))

Definition at line 97 of file dsa.h.

◆ DSA_HANDLE_INVALID

#define DSA_HANDLE_INVALID   ((dsa_handle) DSM_HANDLE_INVALID)

Definition at line 139 of file dsa.h.

◆ DSA_MAX_SEGMENT_SIZE

#define DSA_MAX_SEGMENT_SIZE   ((size_t) 1 << DSA_OFFSET_WIDTH)

Definition at line 103 of file dsa.h.

◆ DSA_MIN_SEGMENT_SIZE

#define DSA_MIN_SEGMENT_SIZE   ((size_t) (256 * 1024))

Definition at line 100 of file dsa.h.

◆ DSA_OFFSET_WIDTH

#define DSA_OFFSET_WIDTH   40 /* 1024 segments of size up to 1TB */

Definition at line 88 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" PRIx64

Definition at line 69 of file dsa.h.

◆ DsaPointerIsValid

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

Definition at line 106 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

Definition at line 22 of file dsa.h.

◆ dsa_handle

Definition at line 136 of file dsa.h.

◆ 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 
)
extern

Definition at line 683 of file dsa.c.

684{
685 uint16 size_class;
689
690 Assert(size > 0);
691
692 /* Sanity check on huge individual allocation size. */
693 if (((flags & DSA_ALLOC_HUGE) != 0 && !AllocHugeSizeIsValid(size)) ||
694 ((flags & DSA_ALLOC_HUGE) == 0 && !AllocSizeIsValid(size)))
695 elog(ERROR, "invalid DSA memory alloc request size %zu", size);
696
697 /*
698 * If bigger than the largest size class, just grab a run of pages from
699 * the free page manager, instead of allocating an object from a pool.
700 * There will still be a span, but it's a special class of span that
701 * manages this whole allocation and simply gives all pages back to the
702 * free page manager when dsa_free is called.
703 */
705 {
706 size_t npages = fpm_size_to_pages(size);
707 size_t first_page;
710
711 /* Obtain a span object. */
714 {
715 /* Raise error unless asked not to. */
716 if ((flags & DSA_ALLOC_NO_OOM) == 0)
719 errmsg("out of memory"),
720 errdetail("Failed on DSA request of size %zu.",
721 size)));
722 return InvalidDsaPointer;
723 }
724
726
727 /* Find a segment from which to allocate. */
728 segment_map = get_best_segment(area, npages);
729 if (segment_map == NULL)
730 segment_map = make_new_segment(area, npages);
731 if (segment_map == NULL)
732 {
733 /* Can't make any more segments: game over. */
735 dsa_free(area, span_pointer);
736
737 /* Raise error unless asked not to. */
738 if ((flags & DSA_ALLOC_NO_OOM) == 0)
741 errmsg("out of memory"),
742 errdetail("Failed on DSA request of size %zu.",
743 size)));
744 return InvalidDsaPointer;
745 }
746
747 /*
748 * Ask the free page manager for a run of pages. This should always
749 * succeed, since both get_best_segment and make_new_segment should
750 * only return a non-NULL pointer if it actually contains enough
751 * contiguous freespace. If it does fail, something in our backend
752 * private state is out of whack, so use FATAL to kill the process.
753 */
754 if (!FreePageManagerGet(segment_map->fpm, npages, &first_page))
755 elog(FATAL,
756 "dsa_allocate could not find %zu free pages", npages);
758
760 first_page * FPM_PAGE_SIZE);
761
762 /* Initialize span and pagemap. */
765 init_span(area, span_pointer, pool, start_pointer, npages,
767 segment_map->pagemap[first_page] = span_pointer;
769
770 /* Zero-initialize the memory if requested. */
771 if ((flags & DSA_ALLOC_ZERO) != 0)
772 memset(dsa_get_address(area, start_pointer), 0, size);
773
774 return start_pointer;
775 }
776
777 /* Map allocation to a size class. */
779 {
780 int mapidx;
781
782 /* For smaller sizes we have a lookup table... */
783 mapidx = ((size + DSA_SIZE_CLASS_MAP_QUANTUM - 1) /
785 size_class = dsa_size_class_map[mapidx];
786 }
787 else
788 {
789 uint16 min;
790 uint16 max;
791
792 /* ... and for the rest we search by binary chop. */
795
796 while (min < max)
797 {
798 uint16 mid = (min + max) / 2;
800
801 if (class_size < size)
802 min = mid + 1;
803 else
804 max = mid;
805 }
806
807 size_class = min;
808 }
809 Assert(size <= dsa_size_classes[size_class]);
810 Assert(size_class == 0 || size > dsa_size_classes[size_class - 1]);
811
812 /* Attempt to allocate an object from the appropriate pool. */
813 result = alloc_object(area, size_class);
814
815 /* Check for failure to allocate. */
817 {
818 /* Raise error unless asked not to. */
819 if ((flags & DSA_ALLOC_NO_OOM) == 0)
822 errmsg("out of memory"),
823 errdetail("Failed on DSA request of size %zu.", size)));
824 return InvalidDsaPointer;
825 }
826
827 /* Zero-initialize the memory if requested. */
828 if ((flags & DSA_ALLOC_ZERO) != 0)
829 memset(dsa_get_address(area, result), 0, size);
830
831 return result;
832}
#define Assert(condition)
Definition c.h:1002
uint16_t uint16
Definition c.h:682
#define lengthof(array)
Definition c.h:932
uint32 result
static const uint16 dsa_size_classes[]
Definition dsa.c:225
#define DSA_AREA_LOCK(area)
Definition dsa.c:132
#define DSA_MAKE_POINTER(segment_number, offset)
Definition dsa.c:92
#define get_segment_index(area, segment_map_ptr)
Definition dsa.c:379
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition dsa.c:954
static dsa_pointer alloc_object(dsa_area *area, int size_class)
Definition dsa.c:1518
#define DSA_SIZE_CLASS_MAP_QUANTUM
Definition dsa.c:258
static const uint8 dsa_size_class_map[]
Definition dsa.c:248
static dsa_segment_map * make_new_segment(dsa_area *area, size_t requested_pages)
Definition dsa.c:2125
#define DSA_SCLASS_LOCK(area, sclass)
Definition dsa.c:133
#define DSA_SCLASS_BLOCK_OF_SPANS
Definition dsa.c:239
#define DSA_SCLASS_SPAN_LARGE
Definition dsa.c:240
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:1423
static dsa_segment_map * get_best_segment(dsa_area *area, size_t npages)
Definition dsa.c:2054
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition dsa.c:838
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:106
#define DSA_ALLOC_HUGE
Definition dsa.h:73
#define DSA_ALLOC_ZERO
Definition dsa.h:75
int errcode(int sqlerrcode)
Definition elog.c:875
int errdetail(const char *fmt,...) pg_attribute_printf(1
#define FATAL
Definition elog.h:42
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
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
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
#define AllocHugeSizeIsValid(size)
Definition memutils.h:49
#define AllocSizeIsValid(size)
Definition memutils.h:42
static char * errmsg
static int fb(int x)
#define min(a, b)
Definition private.h:155
#define max(a, b)
Definition private.h:154
dsa_area_pool pools[DSA_NUM_SIZE_CLASSES]
Definition dsa.c:298
dsa_area_control * control
Definition dsa.c:350

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, fb(), 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(), max, min, dsa_area_control::pools, and result.

Referenced by dshash_create(), insert_into_bucket(), pagetable_allocate(), pgstat_init_entry(), and resize().

◆ dsa_attach()

dsa_area * dsa_attach ( dsa_handle  handle)
extern

Definition at line 510 of file dsa.c.

511{
512 dsm_segment *segment;
513 dsa_area *area;
514
515 /*
516 * An area handle is really a DSM segment handle for the first segment, so
517 * we go ahead and attach to that.
518 */
519 segment = dsm_attach(handle);
520 if (segment == NULL)
523 errmsg("could not attach to dynamic shared area")));
524
525 area = attach_internal(dsm_segment_address(segment), segment, handle);
526
527 /* Clean up when the control segment detaches. */
530
531 return area;
532}
void dsa_on_dsm_detach_release_in_place(dsm_segment *segment, Datum place)
Definition dsa.c:591
static dsa_area * attach_internal(void *place, dsm_segment *segment, dsa_handle handle)
Definition dsa.c:1372
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition dsm.c:1140
void * dsm_segment_address(dsm_segment *seg)
Definition dsm.c:1103
dsm_segment * dsm_attach(dsm_handle h)
Definition dsm.c:673
#define PointerGetDatum(X)
Definition postgres.h:354

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

Referenced by GetNamedDSA(), GetNamedDSHash(), init_dsm_registry(), initGlobalChannelTable(), logicalrep_launcher_attach_dshmem(), pgsa_attach(), and TidStoreAttach().

◆ dsa_attach_in_place()

dsa_area * dsa_attach_in_place ( void place,
dsm_segment segment 
)
extern

Definition at line 560 of file dsa.c.

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

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

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

◆ dsa_create_ext()

dsa_area * dsa_create_ext ( int  tranche_id,
size_t  init_segment_size,
size_t  max_segment_size 
)
extern

Definition at line 421 of file dsa.c.

422{
423 dsm_segment *segment;
424 dsa_area *area;
425
426 /*
427 * Create the DSM segment that will hold the shared control object and the
428 * first segment of usable space.
429 */
430 segment = dsm_create(init_segment_size, 0);
431
432 /*
433 * All segments backing this area are pinned, so that DSA can explicitly
434 * control their lifetime (otherwise a newly created segment belonging to
435 * this area might be freed when the only backend that happens to have it
436 * mapped in ends, corrupting the area).
437 */
438 dsm_pin_segment(segment);
439
440 /* Create a new DSA area with the control object in this segment. */
441 area = create_internal(dsm_segment_address(segment),
442 init_segment_size,
443 tranche_id,
444 dsm_segment_handle(segment), segment,
445 init_segment_size, max_segment_size);
446
447 /* Clean up when the control segment detaches. */
450
451 return area;
452}
static dsa_area * create_internal(void *place, size_t size, int tranche_id, dsm_handle control_handle, dsm_segment *control_segment, size_t init_segment_size, size_t max_segment_size)
Definition dsa.c:1265
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition dsm.c:1131
void dsm_pin_segment(dsm_segment *seg)
Definition dsm.c:963
dsm_segment * dsm_create(Size size, int flags)
Definition dsm.c:524

References create_internal(), 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 TidStoreCreateShared().

◆ dsa_create_in_place_ext()

dsa_area * dsa_create_in_place_ext ( void place,
size_t  size,
int  tranche_id,
dsm_segment segment,
size_t  init_segment_size,
size_t  max_segment_size 
)
extern

Definition at line 471 of file dsa.c.

474{
475 dsa_area *area;
476
477 area = create_internal(place, size, tranche_id,
479 init_segment_size, max_segment_size);
480
481 /*
482 * Clean up when the control segment detaches, if a containing DSM segment
483 * was provided.
484 */
485 if (segment != NULL)
487 PointerGetDatum(place));
488
489 return area;
490}
#define DSM_HANDLE_INVALID
Definition dsm_impl.h:58

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

◆ dsa_detach()

void dsa_detach ( dsa_area area)
extern

Definition at line 1998 of file dsa.c.

1999{
2000 /* Detach from all segments. */
2001 for (dsa_segment_index i = 0; i <= area->high_segment_index; ++i)
2002 if (area->segment_maps[i].segment != NULL)
2004
2005 /*
2006 * Note that 'detaching' (= detaching from DSM segments) doesn't include
2007 * 'releasing' (= adjusting the reference count). It would be nice to
2008 * combine these operations, but client code might never get around to
2009 * calling dsa_detach because of an error path, and a detach hook on any
2010 * particular segment is too late to detach other segments in the area
2011 * without risking a 'leak' warning in the non-error path.
2012 */
2013
2014 /* Free the backend-local area object. */
2015 pfree(area);
2016}
size_t dsa_segment_index
Definition dsa.c:102
void dsm_detach(dsm_segment *seg)
Definition dsm.c:811
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1619
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition dsa.c:366
dsa_segment_index high_segment_index
Definition dsa.c:369
dsm_segment * segment
Definition dsa.c:334

References dsm_detach(), fb(), 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_allocate(), test_dsa_basic(), test_dsa_resowners(), test_empty(), test_random(), TidStoreDestroy(), and TidStoreDetach().

◆ dsa_dump()

void dsa_dump ( dsa_area area)
extern

Definition at line 1135 of file dsa.c.

1136{
1137 size_t i,
1138 j;
1139
1140 /*
1141 * Note: This gives an inconsistent snapshot as it acquires and releases
1142 * individual locks as it goes...
1143 */
1144
1147 fprintf(stderr, "dsa_area handle %x:\n", area->control->handle);
1148 fprintf(stderr, " max_total_segment_size: %zu\n",
1150 fprintf(stderr, " total_segment_size: %zu\n",
1152 fprintf(stderr, " refcnt: %d\n", area->control->refcnt);
1153 fprintf(stderr, " pinned: %c\n", area->control->pinned ? 't' : 'f');
1154 fprintf(stderr, " segment bins:\n");
1155 for (i = 0; i < DSA_NUM_SEGMENT_BINS; ++i)
1156 {
1158 {
1160
1161 if (i == 0)
1163 " segment bin %zu (no contiguous free pages):\n", i);
1164 else
1166 " segment bin %zu (at least %d contiguous pages free):\n",
1167 i, 1 << (i - 1));
1170 {
1172
1173 segment_map =
1175
1177 " segment index %zu, usable_pages = %zu, "
1178 "contiguous_pages = %zu, mapped at %p\n",
1180 segment_map->header->usable_pages,
1182 segment_map->mapped_address);
1183 segment_index = segment_map->header->next;
1184 }
1185 }
1186 }
1188
1189 fprintf(stderr, " pools:\n");
1190 for (i = 0; i < DSA_NUM_SIZE_CLASSES; ++i)
1191 {
1192 bool found = false;
1193
1195 for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1196 if (DsaPointerIsValid(area->control->pools[i].spans[j]))
1197 found = true;
1198 if (found)
1199 {
1201 fprintf(stderr, " pool for blocks of span objects:\n");
1202 else if (i == DSA_SCLASS_SPAN_LARGE)
1203 fprintf(stderr, " pool for large object spans:\n");
1204 else
1206 " pool for size class %zu (object size %hu bytes):\n",
1207 i, dsa_size_classes[i]);
1208 for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1209 {
1210 if (!DsaPointerIsValid(area->control->pools[i].spans[j]))
1211 fprintf(stderr, " fullness class %zu is empty\n", j);
1212 else
1213 {
1215
1216 fprintf(stderr, " fullness class %zu:\n", j);
1218 {
1220
1223 " span descriptor at "
1224 DSA_POINTER_FORMAT ", superblock at "
1226 ", pages = %zu, objects free = %hu/%hu\n",
1227 span_pointer, span->start, span->npages,
1228 span->nallocatable, span->nmax);
1229 span_pointer = span->nextspan;
1230 }
1231 }
1232 }
1233 }
1235 }
1236}
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
#define DSA_SEGMENT_INDEX_NONE
Definition dsa.c:105
static void check_for_freed_segments_locked(dsa_area *area)
Definition dsa.c:2361
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition dsa.c:1803
#define DSA_NUM_SIZE_CLASSES
Definition dsa.c:236
#define DSA_FULLNESS_CLASSES
Definition dsa.c:266
#define DSA_NUM_SEGMENT_BINS
Definition dsa.c:111
#define DSA_POINTER_FORMAT
Definition dsa.h:69
#define fpm_largest(fpm)
Definition freepage.h:88
int j
Definition isn.c:78
size_t total_segment_size
Definition dsa.c:304
bool pinned
Definition dsa.c:312
size_t max_total_segment_size
Definition dsa.c:306
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition dsa.c:296
dsa_handle handle
Definition dsa.c:292
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition dsa.c:279

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, fb(), fpm_largest, fprintf, get_segment_by_index(), dsa_area_control::handle, i, j, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_area_control::max_total_segment_size, dsa_area_control::pinned, dsa_area_control::pools, dsa_area_control::refcnt, dsa_area_control::segment_bins, dsa_area_pool::spans, and dsa_area_control::total_segment_size.

◆ dsa_free()

void dsa_free ( dsa_area area,
dsa_pointer  dp 
)
extern

Definition at line 838 of file dsa.c.

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

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, fb(), FPM_PAGE_SIZE, FreePageManagerPut(), get_segment_by_index(), InvalidDsaPointer, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), NextFreeObjectIndex, rebin_segment(), and unlink_span().

Referenced by CleanupListenersOnExit(), 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(), pgsa_clear_advice_string(), pgsa_drop_stash(), pgsa_reset_all_stashes(), pgsa_set_advice_string(), pgstat_free_entry(), PrepareTableEntriesForListen(), RemoveListenerFromChannel(), resize(), RT_FREE(), RT_FREE_LEAF(), RT_FREE_NODE(), set_val_in_hash(), tbm_free_shared_area(), test_dsa_allocate(), test_dsa_basic(), and test_dsa_resowners().

◆ dsa_get_address()

void * dsa_get_address ( dsa_area area,
dsa_pointer  dp 
)
extern

Definition at line 954 of file dsa.c.

955{
957 size_t offset;
958
959 /* Convert InvalidDsaPointer to NULL. */
960 if (!DsaPointerIsValid(dp))
961 return NULL;
962
963 /* Process any requests to detach from freed segments. */
965
966 /* Break the dsa_pointer into its components. */
968 offset = DSA_EXTRACT_OFFSET(dp);
970
971 /* Check if we need to cause this segment to be mapped in. */
973 {
974 /* Call for effect (we don't need the result). */
976 }
977
978 return area->segment_maps[index].mapped_address + offset;
979}
#define unlikely(x)
Definition c.h:497
#define DSA_MAX_SEGMENTS
Definition dsa.c:75
char * mapped_address
Definition dsa.c:335
Definition type.h:97

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

Referenced by add_span_to_fullness_class(), alloc_object(), ApplyPendingListenActions(), CleanupListenersOnExit(), 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(), get_val_in_hash(), init_span(), insert_into_bucket(), insert_item_into_bucket(), lookup_rowtype_tupdesc_internal(), pagetable_allocate(), ParallelQueryMain(), pg_get_advice_stash_contents(), pgsa_advisor(), pgsa_set_advice_string(), pgsa_write_entries(), pgstat_build_snapshot(), pgstat_get_entry_ref(), pgstat_init_entry(), pgstat_reinit_entry(), pgstat_reset_matching_entries(), pgstat_write_statsfile(), PrepareTableEntriesForListen(), resize(), RT_PTR_SET_LOCAL(), SerializeParamExecParams(), set_val_in_hash(), share_tupledesc(), shared_record_table_compare(), shared_record_table_hash(), SignalBackends(), tbm_attach_shared_iterate(), tbm_free_shared_area(), tbm_prepare_shared_iterate(), test_custom_stats_var_create(), test_custom_stats_var_from_serialized_data(), test_custom_stats_var_report(), test_custom_stats_var_to_serialized_data(), test_dsa_basic(), test_dsa_resowners(), transfer_first_span(), and unlink_span().

◆ dsa_get_handle()

◆ dsa_get_total_size()

size_t dsa_get_total_size ( dsa_area area)
extern

Definition at line 1039 of file dsa.c.

1040{
1041 size_t size;
1042
1044 size = area->control->total_segment_size;
1046
1047 return size;
1048}
@ LW_SHARED
Definition lwlock.h:105

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

Referenced by RT_MEMORY_USAGE().

◆ dsa_get_total_size_from_handle()

size_t dsa_get_total_size_from_handle ( dsa_handle  handle)
extern

Definition at line 1055 of file dsa.c.

1056{
1057 size_t size;
1058 bool already_attached;
1059 dsm_segment *segment;
1060 dsa_area_control *control;
1061
1063 if (already_attached)
1064 segment = dsm_find_mapping(handle);
1065 else
1066 segment = dsm_attach(handle);
1067
1068 if (segment == NULL)
1069 ereport(ERROR,
1071 errmsg("could not attach to dynamic shared area")));
1072
1073 control = (dsa_area_control *) dsm_segment_address(segment);
1074
1075 LWLockAcquire(&control->lock, LW_SHARED);
1076 size = control->total_segment_size;
1077 LWLockRelease(&control->lock);
1078
1079 if (!already_attached)
1080 dsm_detach(segment);
1081
1082 return size;
1083}
bool dsa_is_attached(dsa_handle handle)
Definition dsa.c:540
dsm_segment * dsm_find_mapping(dsm_handle handle)
Definition dsm.c:1084
LWLock lock
Definition dsa.c:318

References dsa_is_attached(), dsm_attach(), dsm_detach(), dsm_find_mapping(), dsm_segment_address(), ereport, errcode(), errmsg, ERROR, fb(), dsa_area_control::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), and dsa_area_control::total_segment_size.

Referenced by pg_get_dsm_registry_allocations().

◆ dsa_is_attached()

bool dsa_is_attached ( dsa_handle  handle)
extern

Definition at line 540 of file dsa.c.

541{
542 /*
543 * An area handle is really a DSM segment handle for the first segment, so
544 * we can just search for that.
545 */
546 return dsm_find_mapping(handle) != NULL;
547}

References dsm_find_mapping(), and fb().

Referenced by dsa_get_total_size_from_handle(), GetNamedDSA(), and GetNamedDSHash().

◆ dsa_minimum_size()

size_t dsa_minimum_size ( void  )
extern

Definition at line 1243 of file dsa.c.

1244{
1245 size_t size;
1246 size_t pages = 0;
1247
1248 size = MAXALIGN(sizeof(dsa_area_control)) +
1249 MAXALIGN(sizeof(FreePageManager));
1250
1251 /* Figure out how many pages we need, including the page map... */
1252 while (((size + FPM_PAGE_SIZE - 1) / FPM_PAGE_SIZE) > pages)
1253 {
1254 ++pages;
1255 size += sizeof(dsa_pointer);
1256 }
1257
1258 return pages * FPM_PAGE_SIZE;
1259}
#define MAXALIGN(LEN)
Definition c.h:955

References FPM_PAGE_SIZE, and MAXALIGN.

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

Definition at line 591 of file dsa.c.

592{
594}
void dsa_release_in_place(void *place)
Definition dsa.c:620
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332

References DatumGetPointer(), and dsa_release_in_place().

Referenced by dsa_attach(), dsa_attach_in_place(), dsa_create_ext(), and dsa_create_in_place_ext().

◆ dsa_on_shmem_exit_release_in_place()

void dsa_on_shmem_exit_release_in_place ( int  code,
Datum  place 
)
extern

Definition at line 605 of file dsa.c.

606{
608}

References DatumGetPointer(), and dsa_release_in_place().

◆ dsa_pin()

◆ dsa_pin_mapping()

void dsa_pin_mapping ( dsa_area area)
extern

◆ dsa_release_in_place()

void dsa_release_in_place ( void place)
extern

Definition at line 620 of file dsa.c.

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

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(), dsa_on_shmem_exit_release_in_place(), and pgstat_detach_shmem().

◆ dsa_set_size_limit()

void dsa_set_size_limit ( dsa_area area,
size_t  limit 
)
extern

◆ dsa_trim()

void dsa_trim ( dsa_area area)
extern

Definition at line 1090 of file dsa.c.

1091{
1092 int size_class;
1093
1094 /*
1095 * Trim in reverse pool order so we get to the spans-of-spans last, just
1096 * in case any become entirely free while processing all the other pools.
1097 */
1098 for (size_class = DSA_NUM_SIZE_CLASSES - 1; size_class >= 0; --size_class)
1099 {
1100 dsa_area_pool *pool = &area->control->pools[size_class];
1102
1103 if (size_class == DSA_SCLASS_SPAN_LARGE)
1104 {
1105 /* Large object frees give back segments aggressively already. */
1106 continue;
1107 }
1108
1109 /*
1110 * Search fullness class 1 only. That is where we expect to find an
1111 * entirely empty superblock (entirely empty superblocks in other
1112 * fullness classes are returned to the free page map by dsa_free).
1113 */
1114 LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
1115 span_pointer = pool->spans[1];
1117 {
1119 dsa_pointer next = span->nextspan;
1120
1121 if (span->nallocatable == span->nmax)
1123
1125 }
1126 LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
1127 }
1128}
static int32 next
Definition blutils.c:225

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

◆ dsa_unpin()

void dsa_unpin ( dsa_area area)
extern

Definition at line 1006 of file dsa.c.

1007{
1009 Assert(area->control->refcnt > 1);
1010 if (!area->control->pinned)
1011 {
1013 elog(ERROR, "dsa_area not pinned");
1014 }
1015 area->control->pinned = false;
1016 --area->control->refcnt;
1018}

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