PostgreSQL Source Code  git master
dsa.c File Reference
#include "postgres.h"
#include "port/atomics.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/shmem.h"
#include "utils/dsa.h"
#include "utils/freepage.h"
#include "utils/memutils.h"
Include dependency graph for dsa.c:

Go to the source code of this file.

Data Structures

struct  dsa_segment_header
 
struct  dsa_area_span
 
struct  dsa_area_pool
 
struct  dsa_area_control
 
struct  dsa_segment_map
 
struct  dsa_area
 

Macros

#define DSA_INITIAL_SEGMENT_SIZE   ((size_t) (1 * 1024 * 1024))
 
#define DSA_NUM_SEGMENTS_AT_EACH_SIZE   4
 
#define DSA_OFFSET_WIDTH   40 /* 1024 segments of size up to 1TB */
 
#define DSA_MAX_SEGMENTS   Min(1024, (1 << ((SIZEOF_DSA_POINTER * 8) - DSA_OFFSET_WIDTH)))
 
#define DSA_OFFSET_BITMASK   (((dsa_pointer) 1 << DSA_OFFSET_WIDTH) - 1)
 
#define DSA_MAX_SEGMENT_SIZE   ((size_t) 1 << DSA_OFFSET_WIDTH)
 
#define DSA_PAGES_PER_SUPERBLOCK   16
 
#define DSA_SEGMENT_HEADER_MAGIC   0x0ce26608
 
#define DSA_MAKE_POINTER(segment_number, offset)   (((dsa_pointer) (segment_number) << DSA_OFFSET_WIDTH) | (offset))
 
#define DSA_EXTRACT_SEGMENT_NUMBER(dp)   ((dp) >> DSA_OFFSET_WIDTH)
 
#define DSA_EXTRACT_OFFSET(dp)   ((dp) & DSA_OFFSET_BITMASK)
 
#define DSA_SEGMENT_INDEX_NONE   (~(dsa_segment_index)0)
 
#define DSA_NUM_SEGMENT_BINS   16
 
#define contiguous_pages_to_segment_bin(n)   Min(fls(n), DSA_NUM_SEGMENT_BINS - 1)
 
#define DSA_AREA_LOCK(area)   (&area->control->lock)
 
#define DSA_SCLASS_LOCK(area, sclass)   (&area->control->pools[sclass].lock)
 
#define NextFreeObjectIndex(object)   (* (uint16 *) (object))
 
#define DSA_NUM_SIZE_CLASSES   lengthof(dsa_size_classes)
 
#define DSA_SCLASS_BLOCK_OF_SPANS   0
 
#define DSA_SCLASS_SPAN_LARGE   1
 
#define DSA_SIZE_CLASS_MAP_QUANTUM   8
 
#define DSA_FULLNESS_CLASSES   4
 
#define DsaAreaPoolToDsaPointer(area, p)   DSA_MAKE_POINTER(0, (char *) p - (char *) area->control)
 
#define DSA_SPAN_NOTHING_FREE   ((uint16) -1)
 
#define DSA_SUPERBLOCK_SIZE   (DSA_PAGES_PER_SUPERBLOCK * FPM_PAGE_SIZE)
 
#define get_segment_index(area, segment_map_ptr)   (segment_map_ptr - &area->segment_maps[0])
 

Typedefs

typedef size_t dsa_segment_index
 

Functions

static void init_span (dsa_area *area, dsa_pointer span_pointer, dsa_area_pool *pool, dsa_pointer start, size_t npages, uint16 size_class)
 
static bool transfer_first_span (dsa_area *area, dsa_area_pool *pool, int fromclass, int toclass)
 
static dsa_pointer alloc_object (dsa_area *area, int size_class)
 
static bool ensure_active_superblock (dsa_area *area, dsa_area_pool *pool, int size_class)
 
static dsa_segment_mapget_segment_by_index (dsa_area *area, dsa_segment_index index)
 
static void destroy_superblock (dsa_area *area, dsa_pointer span_pointer)
 
static void unlink_span (dsa_area *area, dsa_area_span *span)
 
static void add_span_to_fullness_class (dsa_area *area, dsa_area_span *span, dsa_pointer span_pointer, int fclass)
 
static void unlink_segment (dsa_area *area, dsa_segment_map *segment_map)
 
static dsa_segment_mapget_best_segment (dsa_area *area, size_t npages)
 
static dsa_segment_mapmake_new_segment (dsa_area *area, size_t requested_pages)
 
static dsa_areacreate_internal (void *place, size_t size, int tranche_id, dsm_handle control_handle, dsm_segment *control_segment)
 
static dsa_areaattach_internal (void *place, dsm_segment *segment, dsa_handle handle)
 
static void check_for_freed_segments (dsa_area *area)
 
static void check_for_freed_segments_locked (dsa_area *area)
 
dsa_areadsa_create (int tranche_id)
 
dsa_areadsa_create_in_place (void *place, size_t size, int tranche_id, dsm_segment *segment)
 
dsa_handle dsa_get_handle (dsa_area *area)
 
dsa_areadsa_attach (dsa_handle handle)
 
dsa_areadsa_attach_in_place (void *place, dsm_segment *segment)
 
void dsa_on_dsm_detach_release_in_place (dsm_segment *segment, Datum place)
 
void dsa_on_shmem_exit_release_in_place (int code, Datum place)
 
void dsa_release_in_place (void *place)
 
void dsa_pin_mapping (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_pin (dsa_area *area)
 
void dsa_unpin (dsa_area *area)
 
void dsa_set_size_limit (dsa_area *area, size_t limit)
 
void dsa_trim (dsa_area *area)
 
void dsa_dump (dsa_area *area)
 
size_t dsa_minimum_size (void)
 
void dsa_detach (dsa_area *area)
 

Variables

static const uint16 dsa_size_classes []
 
static const uint8 dsa_size_class_map []
 

Macro Definition Documentation

◆ contiguous_pages_to_segment_bin

#define contiguous_pages_to_segment_bin (   n)    Min(fls(n), DSA_NUM_SEGMENT_BINS - 1)

Definition at line 140 of file dsa.c.

Referenced by create_internal(), get_best_segment(), and make_new_segment().

◆ DSA_AREA_LOCK

◆ DSA_EXTRACT_OFFSET

#define DSA_EXTRACT_OFFSET (   dp)    ((dp) & DSA_OFFSET_BITMASK)

Definition at line 121 of file dsa.c.

Referenced by destroy_superblock(), dsa_free(), and dsa_get_address().

◆ DSA_EXTRACT_SEGMENT_NUMBER

#define DSA_EXTRACT_SEGMENT_NUMBER (   dp)    ((dp) >> DSA_OFFSET_WIDTH)

Definition at line 118 of file dsa.c.

Referenced by destroy_superblock(), dsa_free(), and dsa_get_address().

◆ DSA_FULLNESS_CLASSES

#define DSA_FULLNESS_CLASSES   4

Definition at line 277 of file dsa.c.

Referenced by alloc_object(), dsa_dump(), dsa_free(), and ensure_active_superblock().

◆ DSA_INITIAL_SEGMENT_SIZE

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

Definition at line 68 of file dsa.c.

Referenced by create_internal(), dsa_create(), and make_new_segment().

◆ DSA_MAKE_POINTER

#define DSA_MAKE_POINTER (   segment_number,
  offset 
)    (((dsa_pointer) (segment_number) << DSA_OFFSET_WIDTH) | (offset))

Definition at line 114 of file dsa.c.

Referenced by dsa_allocate_extended(), and ensure_active_superblock().

◆ DSA_MAX_SEGMENT_SIZE

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

Definition at line 101 of file dsa.c.

Referenced by make_new_segment().

◆ DSA_MAX_SEGMENTS

#define DSA_MAX_SEGMENTS   Min(1024, (1 << ((SIZEOF_DSA_POINTER * 8) - DSA_OFFSET_WIDTH)))

Definition at line 94 of file dsa.c.

Referenced by attach_internal(), create_internal(), dsa_get_address(), and make_new_segment().

◆ DSA_NUM_SEGMENT_BINS

#define DSA_NUM_SEGMENT_BINS   16

Definition at line 133 of file dsa.c.

Referenced by create_internal(), dsa_dump(), and get_best_segment().

◆ DSA_NUM_SEGMENTS_AT_EACH_SIZE

#define DSA_NUM_SEGMENTS_AT_EACH_SIZE   4

Definition at line 77 of file dsa.c.

Referenced by make_new_segment().

◆ DSA_NUM_SIZE_CLASSES

#define DSA_NUM_SIZE_CLASSES   lengthof(dsa_size_classes)

Definition at line 247 of file dsa.c.

Referenced by alloc_object(), create_internal(), dsa_dump(), and dsa_trim().

◆ DSA_OFFSET_BITMASK

#define DSA_OFFSET_BITMASK   (((dsa_pointer) 1 << DSA_OFFSET_WIDTH) - 1)

Definition at line 98 of file dsa.c.

◆ DSA_OFFSET_WIDTH

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

Definition at line 87 of file dsa.c.

◆ DSA_PAGES_PER_SUPERBLOCK

#define DSA_PAGES_PER_SUPERBLOCK   16

Definition at line 104 of file dsa.c.

Referenced by ensure_active_superblock().

◆ DSA_SCLASS_BLOCK_OF_SPANS

#define DSA_SCLASS_BLOCK_OF_SPANS   0

◆ DSA_SCLASS_LOCK

#define DSA_SCLASS_LOCK (   area,
  sclass 
)    (&area->control->pools[sclass].lock)

◆ DSA_SCLASS_SPAN_LARGE

#define DSA_SCLASS_SPAN_LARGE   1

Definition at line 251 of file dsa.c.

Referenced by dsa_allocate_extended(), dsa_dump(), dsa_free(), dsa_trim(), and init_span().

◆ DSA_SEGMENT_HEADER_MAGIC

#define DSA_SEGMENT_HEADER_MAGIC   0x0ce26608

◆ DSA_SEGMENT_INDEX_NONE

#define DSA_SEGMENT_INDEX_NONE   (~(dsa_segment_index)0)

Definition at line 127 of file dsa.c.

Referenced by create_internal(), dsa_dump(), get_best_segment(), make_new_segment(), and unlink_segment().

◆ DSA_SIZE_CLASS_MAP_QUANTUM

#define DSA_SIZE_CLASS_MAP_QUANTUM   8

Definition at line 269 of file dsa.c.

Referenced by dsa_allocate_extended().

◆ DSA_SPAN_NOTHING_FREE

#define DSA_SPAN_NOTHING_FREE   ((uint16) -1)

Definition at line 377 of file dsa.c.

Referenced by alloc_object(), and init_span().

◆ DSA_SUPERBLOCK_SIZE

#define DSA_SUPERBLOCK_SIZE   (DSA_PAGES_PER_SUPERBLOCK * FPM_PAGE_SIZE)

Definition at line 378 of file dsa.c.

Referenced by dsa_free(), ensure_active_superblock(), and init_span().

◆ DsaAreaPoolToDsaPointer

#define DsaAreaPoolToDsaPointer (   area,
 
)    DSA_MAKE_POINTER(0, (char *) p - (char *) area->control)

Definition at line 329 of file dsa.c.

Referenced by init_span().

◆ get_segment_index

#define get_segment_index (   area,
  segment_map_ptr 
)    (segment_map_ptr - &area->segment_maps[0])

◆ NextFreeObjectIndex

#define NextFreeObjectIndex (   object)    (* (uint16 *) (object))

Definition at line 213 of file dsa.c.

Referenced by alloc_object(), and dsa_free().

Typedef Documentation

◆ dsa_segment_index

typedef size_t dsa_segment_index

Definition at line 124 of file dsa.c.

Function Documentation

◆ add_span_to_fullness_class()

static void add_span_to_fullness_class ( dsa_area area,
dsa_area_span span,
dsa_pointer  span_pointer,
int  fclass 
)
static

Definition at line 1890 of file dsa.c.

References dsa_get_address(), DsaPointerIsValid, dsa_area_span::fclass, InvalidDsaPointer, dsa_area_span::nextspan, dsa_area_span::pool, dsa_area_span::prevspan, and dsa_area_pool::spans.

Referenced by dsa_free().

1893 {
1894  dsa_area_pool *pool = dsa_get_address(area, span->pool);
1895 
1896  if (DsaPointerIsValid(pool->spans[fclass]))
1897  {
1898  dsa_area_span *head = dsa_get_address(area,
1899  pool->spans[fclass]);
1900 
1901  head->prevspan = span_pointer;
1902  }
1903  span->prevspan = InvalidDsaPointer;
1904  span->nextspan = pool->spans[fclass];
1905  pool->spans[fclass] = span_pointer;
1906  span->fclass = fclass;
1907 }
dsa_pointer nextspan
Definition: dsa.c:198
#define InvalidDsaPointer
Definition: dsa.h:78
uint16 fclass
Definition: dsa.c:206
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
dsa_pointer prevspan
Definition: dsa.c:197
dsa_pointer pool
Definition: dsa.c:196
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
#define DsaPointerIsValid(x)
Definition: dsa.h:81

◆ alloc_object()

static dsa_pointer alloc_object ( dsa_area area,
int  size_class 
)
inlinestatic

Definition at line 1440 of file dsa.c.

References Assert, dsa_area::control, DSA_FULLNESS_CLASSES, dsa_get_address(), DSA_NUM_SIZE_CLASSES, DSA_SCLASS_LOCK, dsa_size_classes, DSA_SPAN_NOTHING_FREE, DsaPointerIsValid, ensure_active_superblock(), dsa_area_span::firstfree, InvalidDsaPointer, LW_EXCLUSIVE, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), dsa_area_span::nallocatable, NextFreeObjectIndex, dsa_area_span::ninitialized, dsa_area_control::pools, dsa_area_pool::spans, dsa_area_span::start, and transfer_first_span().

Referenced by dsa_allocate_extended(), and ensure_active_superblock().

1441 {
1442  dsa_area_pool *pool = &area->control->pools[size_class];
1443  dsa_area_span *span;
1444  dsa_pointer block;
1445  dsa_pointer result;
1446  char *object;
1447  size_t size;
1448 
1449  /*
1450  * Even though ensure_active_superblock can in turn call alloc_object if
1451  * it needs to allocate a new span, that's always from a different pool,
1452  * and the order of lock acquisition is always the same, so it's OK that
1453  * we hold this lock for the duration of this function.
1454  */
1455  Assert(!LWLockHeldByMe(DSA_SCLASS_LOCK(area, size_class)));
1456  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
1457 
1458  /*
1459  * If there's no active superblock, we must successfully obtain one or
1460  * fail the request.
1461  */
1462  if (!DsaPointerIsValid(pool->spans[1]) &&
1463  !ensure_active_superblock(area, pool, size_class))
1464  {
1465  result = InvalidDsaPointer;
1466  }
1467  else
1468  {
1469  /*
1470  * There should be a block in fullness class 1 at this point, and it
1471  * should never be completely full. Thus we can either pop an object
1472  * from the free list or, failing that, initialize a new object.
1473  */
1474  Assert(DsaPointerIsValid(pool->spans[1]));
1475  span = (dsa_area_span *)
1476  dsa_get_address(area, pool->spans[1]);
1477  Assert(span->nallocatable > 0);
1478  block = span->start;
1479  Assert(size_class < DSA_NUM_SIZE_CLASSES);
1480  size = dsa_size_classes[size_class];
1481  if (span->firstfree != DSA_SPAN_NOTHING_FREE)
1482  {
1483  result = block + span->firstfree * size;
1484  object = dsa_get_address(area, result);
1485  span->firstfree = NextFreeObjectIndex(object);
1486  }
1487  else
1488  {
1489  result = block + span->ninitialized * size;
1490  ++span->ninitialized;
1491  }
1492  --span->nallocatable;
1493 
1494  /* If it's now full, move it to the highest-numbered fullness class. */
1495  if (span->nallocatable == 0)
1496  transfer_first_span(area, pool, 1, DSA_FULLNESS_CLASSES - 1);
1497  }
1498 
1499  Assert(LWLockHeldByMe(DSA_SCLASS_LOCK(area, size_class)));
1500  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
1501 
1502  return result;
1503 }
#define DSA_SPAN_NOTHING_FREE
Definition: dsa.c:377
#define InvalidDsaPointer
Definition: dsa.h:78
uint16 ninitialized
Definition: dsa.c:202
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
#define DSA_NUM_SIZE_CLASSES
Definition: dsa.c:247
#define DSA_FULLNESS_CLASSES
Definition: dsa.c:277
dsa_pointer start
Definition: dsa.c:199
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
static bool ensure_active_superblock(dsa_area *area, dsa_area_pool *pool, int size_class)
Definition: dsa.c:1528
static bool transfer_first_span(dsa_area *area, dsa_area_pool *pool, int fromclass, int toclass)
Definition: dsa.c:1400
dsa_area_control * control
Definition: dsa.c:357
static const uint16 dsa_size_classes[]
Definition: dsa.c:236
#define DSA_SCLASS_LOCK(area, sclass)
Definition: dsa.c:144
#define Assert(condition)
Definition: c.h:739
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
uint16 nallocatable
Definition: dsa.c:203
uint16 firstfree
Definition: dsa.c:204
#define NextFreeObjectIndex(object)
Definition: dsa.c:213

◆ attach_internal()

static dsa_area * attach_internal ( void *  place,
dsm_segment segment,
dsa_handle  handle 
)
static

Definition at line 1294 of file dsa.c.

References Assert, dsa_area::control, DSA_AREA_LOCK, DSA_MAX_SEGMENTS, DSA_SEGMENT_HEADER_MAGIC, ereport, errcode(), errmsg(), ERROR, dsa_segment_map::fpm, dsa_area_control::freed_segment_counter, dsa_area::freed_segment_counter, dsa_area_control::handle, dsa_segment_map::header, dsa_area::high_segment_index, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_segment_header::magic, dsa_segment_map::mapped_address, dsa_area::mapping_pinned, MAXALIGN, dsa_segment_map::pagemap, palloc(), dsa_area_control::refcnt, dsa_segment_map::segment, dsa_area_control::segment_handles, dsa_area_control::segment_header, and dsa_area::segment_maps.

Referenced by dsa_attach(), and dsa_attach_in_place().

1295 {
1296  dsa_area_control *control;
1297  dsa_area *area;
1298  dsa_segment_map *segment_map;
1299 
1300  control = (dsa_area_control *) place;
1301  Assert(control->handle == handle);
1302  Assert(control->segment_handles[0] == handle);
1303  Assert(control->segment_header.magic ==
1304  (DSA_SEGMENT_HEADER_MAGIC ^ handle ^ 0));
1305 
1306  /* Build the backend-local area object. */
1307  area = palloc(sizeof(dsa_area));
1308  area->control = control;
1309  area->mapping_pinned = false;
1310  memset(&area->segment_maps[0], 0,
1311  sizeof(dsa_segment_map) * DSA_MAX_SEGMENTS);
1312  area->high_segment_index = 0;
1313 
1314  /* Set up the segment map for this process's mapping. */
1315  segment_map = &area->segment_maps[0];
1316  segment_map->segment = segment; /* NULL for in-place */
1317  segment_map->mapped_address = place;
1318  segment_map->header = (dsa_segment_header *) segment_map->mapped_address;
1319  segment_map->fpm = (FreePageManager *)
1320  (segment_map->mapped_address + MAXALIGN(sizeof(dsa_area_control)));
1321  segment_map->pagemap = (dsa_pointer *)
1322  (segment_map->mapped_address + MAXALIGN(sizeof(dsa_area_control)) +
1323  MAXALIGN(sizeof(FreePageManager)));
1324 
1325  /* Bump the reference count. */
1327  if (control->refcnt == 0)
1328  {
1329  /* We can't attach to a DSA area that has already been destroyed. */
1330  ereport(ERROR,
1331  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1332  errmsg("could not attach to dynamic shared area")));
1333  }
1334  ++control->refcnt;
1337 
1338  return area;
1339 }
bool mapping_pinned
Definition: dsa.c:360
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsm_segment * segment
Definition: dsa.c:341
int errcode(int sqlerrcode)
Definition: elog.c:608
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
int refcnt
Definition: dsa.c:317
#define ERROR
Definition: elog.h:43
dsa_area_control * control
Definition: dsa.c:357
#define ereport(elevel, rest)
Definition: elog.h:141
dsa_segment_header segment_header
Definition: dsa.c:301
size_t freed_segment_counter
Definition: dsa.c:321
#define Assert(condition)
Definition: c.h:739
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define MAXALIGN(LEN)
Definition: c.h:692
size_t freed_segment_counter
Definition: dsa.c:374
dsa_pointer * pagemap
Definition: dsa.c:345
char * mapped_address
Definition: dsa.c:342
#define DSA_MAX_SEGMENTS
Definition: dsa.c:94
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
Definition: dsa.c:354
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
uint32 magic
Definition: dsa.c:154
dsa_handle handle
Definition: dsa.c:303

◆ check_for_freed_segments()

static void check_for_freed_segments ( dsa_area area)
static

Definition at line 2232 of file dsa.c.

References check_for_freed_segments_locked(), dsa_area::control, DSA_AREA_LOCK, dsa_area_control::freed_segment_counter, dsa_area::freed_segment_counter, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), pg_read_barrier, and unlikely.

Referenced by dsa_free(), and dsa_get_address().

2233 {
2234  size_t freed_segment_counter;
2235 
2236  /*
2237  * Any other process that has freed a segment has incremented
2238  * freed_segment_counter while holding an LWLock, and that must precede
2239  * any backend creating a new segment in the same slot while holding an
2240  * LWLock, and that must precede the creation of any dsa_pointer pointing
2241  * into the new segment which might reach us here, and the caller must
2242  * have sent the dsa_pointer to this process using appropriate memory
2243  * synchronization (some kind of locking or atomic primitive or system
2244  * call). So all we need to do on the reading side is ask for the load of
2245  * freed_segment_counter to follow the caller's load of the dsa_pointer it
2246  * has, and we can be sure to detect any segments that had been freed as
2247  * of the time that the dsa_pointer reached this process.
2248  */
2249  pg_read_barrier();
2250  freed_segment_counter = area->control->freed_segment_counter;
2251  if (unlikely(area->freed_segment_counter != freed_segment_counter))
2252  {
2253  /* Check all currently mapped segments to find what's been freed. */
2257  }
2258 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
dsa_area_control * control
Definition: dsa.c:357
size_t freed_segment_counter
Definition: dsa.c:321
#define pg_read_barrier()
Definition: atomics.h:158
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
size_t freed_segment_counter
Definition: dsa.c:374
#define unlikely(x)
Definition: c.h:208
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
static void check_for_freed_segments_locked(dsa_area *area)
Definition: dsa.c:2268

◆ check_for_freed_segments_locked()

static void check_for_freed_segments_locked ( dsa_area area)
static

Definition at line 2268 of file dsa.c.

References Assert, dsa_area::control, DSA_AREA_LOCK, dsm_detach(), dsa_segment_header::freed, dsa_area_control::freed_segment_counter, dsa_area::freed_segment_counter, dsa_segment_map::header, dsa_area::high_segment_index, i, LWLockHeldByMe(), dsa_segment_map::mapped_address, dsa_segment_map::segment, dsa_area::segment_maps, and unlikely.

Referenced by check_for_freed_segments(), destroy_superblock(), dsa_dump(), and get_best_segment().

2269 {
2270  size_t freed_segment_counter;
2271  int i;
2272 
2274  freed_segment_counter = area->control->freed_segment_counter;
2275  if (unlikely(area->freed_segment_counter != freed_segment_counter))
2276  {
2277  for (i = 0; i <= area->high_segment_index; ++i)
2278  {
2279  if (area->segment_maps[i].header != NULL &&
2280  area->segment_maps[i].header->freed)
2281  {
2282  dsm_detach(area->segment_maps[i].segment);
2283  area->segment_maps[i].segment = NULL;
2284  area->segment_maps[i].header = NULL;
2285  area->segment_maps[i].mapped_address = NULL;
2286  }
2287  }
2288  area->freed_segment_counter = freed_segment_counter;
2289  }
2290 }
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsm_segment * segment
Definition: dsa.c:341
dsa_segment_header * header
Definition: dsa.c:343
dsa_area_control * control
Definition: dsa.c:357
size_t freed_segment_counter
Definition: dsa.c:321
#define Assert(condition)
Definition: c.h:739
size_t freed_segment_counter
Definition: dsa.c:374
char * mapped_address
Definition: dsa.c:342
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:664
int i
#define unlikely(x)
Definition: c.h:208
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143

◆ create_internal()

static dsa_area * create_internal ( void *  place,
size_t  size,
int  tranche_id,
dsm_handle  control_handle,
dsm_segment control_segment 
)
static

Definition at line 1191 of file dsa.c.

References Assert, dsa_segment_header::bin, contiguous_pages_to_segment_bin, dsa_area::control, DSA_INITIAL_SEGMENT_SIZE, DSA_MAX_SEGMENTS, dsa_minimum_size(), DSA_NUM_SEGMENT_BINS, DSA_NUM_SIZE_CLASSES, DSA_SCLASS_LOCK, DSA_SEGMENT_HEADER_MAGIC, DSA_SEGMENT_INDEX_NONE, elog, ERROR, dsa_segment_map::fpm, FPM_PAGE_SIZE, dsa_segment_header::freed, dsa_area_control::freed_segment_counter, dsa_area::freed_segment_counter, FreePageManagerInitialize(), FreePageManagerPut(), dsa_area_control::handle, dsa_segment_map::header, dsa_area_control::high_segment_index, dsa_area::high_segment_index, i, dsa_area_control::lock, dsa_area_control::lwlock_tranche_id, LWLockInitialize(), dsa_segment_header::magic, dsa_segment_map::mapped_address, dsa_area::mapping_pinned, dsa_area_control::max_total_segment_size, MAXALIGN, dsa_segment_header::next, dsa_segment_map::pagemap, palloc(), dsa_segment_header::prev, dsa_area_control::refcnt, dsa_segment_map::segment, dsa_area_control::segment_bins, dsa_area_control::segment_handles, dsa_area_control::segment_header, dsa_area::segment_maps, dsa_segment_header::size, dsa_area_control::total_segment_size, and dsa_segment_header::usable_pages.

Referenced by dsa_create(), and dsa_create_in_place().

1195 {
1196  dsa_area_control *control;
1197  dsa_area *area;
1198  dsa_segment_map *segment_map;
1199  size_t usable_pages;
1200  size_t total_pages;
1201  size_t metadata_bytes;
1202  int i;
1203 
1204  /* Sanity check on the space we have to work in. */
1205  if (size < dsa_minimum_size())
1206  elog(ERROR, "dsa_area space must be at least %zu, but %zu provided",
1207  dsa_minimum_size(), size);
1208 
1209  /* Now figure out how much space is usable */
1210  total_pages = size / FPM_PAGE_SIZE;
1211  metadata_bytes =
1212  MAXALIGN(sizeof(dsa_area_control)) +
1213  MAXALIGN(sizeof(FreePageManager)) +
1214  total_pages * sizeof(dsa_pointer);
1215  /* Add padding up to next page boundary. */
1216  if (metadata_bytes % FPM_PAGE_SIZE != 0)
1217  metadata_bytes += FPM_PAGE_SIZE - (metadata_bytes % FPM_PAGE_SIZE);
1218  Assert(metadata_bytes <= size);
1219  usable_pages = (size - metadata_bytes) / FPM_PAGE_SIZE;
1220 
1221  /*
1222  * Initialize the dsa_area_control object located at the start of the
1223  * space.
1224  */
1225  control = (dsa_area_control *) place;
1226  control->segment_header.magic =
1227  DSA_SEGMENT_HEADER_MAGIC ^ control_handle ^ 0;
1230  control->segment_header.usable_pages = usable_pages;
1231  control->segment_header.freed = false;
1233  control->handle = control_handle;
1234  control->max_total_segment_size = (size_t) -1;
1235  control->total_segment_size = size;
1236  memset(&control->segment_handles[0], 0,
1237  sizeof(dsm_handle) * DSA_MAX_SEGMENTS);
1238  control->segment_handles[0] = control_handle;
1239  for (i = 0; i < DSA_NUM_SEGMENT_BINS; ++i)
1240  control->segment_bins[i] = DSA_SEGMENT_INDEX_NONE;
1241  control->high_segment_index = 0;
1242  control->refcnt = 1;
1243  control->freed_segment_counter = 0;
1244  control->lwlock_tranche_id = tranche_id;
1245 
1246  /*
1247  * Create the dsa_area object that this backend will use to access the
1248  * area. Other backends will need to obtain their own dsa_area object by
1249  * attaching.
1250  */
1251  area = palloc(sizeof(dsa_area));
1252  area->control = control;
1253  area->mapping_pinned = false;
1254  memset(area->segment_maps, 0, sizeof(dsa_segment_map) * DSA_MAX_SEGMENTS);
1255  area->high_segment_index = 0;
1256  area->freed_segment_counter = 0;
1257  LWLockInitialize(&control->lock, control->lwlock_tranche_id);
1258  for (i = 0; i < DSA_NUM_SIZE_CLASSES; ++i)
1260  control->lwlock_tranche_id);
1261 
1262  /* Set up the segment map for this process's mapping. */
1263  segment_map = &area->segment_maps[0];
1264  segment_map->segment = control_segment;
1265  segment_map->mapped_address = place;
1266  segment_map->header = (dsa_segment_header *) place;
1267  segment_map->fpm = (FreePageManager *)
1268  (segment_map->mapped_address +
1269  MAXALIGN(sizeof(dsa_area_control)));
1270  segment_map->pagemap = (dsa_pointer *)
1271  (segment_map->mapped_address +
1272  MAXALIGN(sizeof(dsa_area_control)) +
1273  MAXALIGN(sizeof(FreePageManager)));
1274 
1275  /* Set up the free page map. */
1276  FreePageManagerInitialize(segment_map->fpm, segment_map->mapped_address);
1277  /* There can be 0 usable pages if size is dsa_minimum_size(). */
1278 
1279  if (usable_pages > 0)
1280  FreePageManagerPut(segment_map->fpm, metadata_bytes / FPM_PAGE_SIZE,
1281  usable_pages);
1282 
1283  /* Put this segment into the appropriate bin. */
1284  control->segment_bins[contiguous_pages_to_segment_bin(usable_pages)] = 0;
1285  segment_map->header->bin = contiguous_pages_to_segment_bin(usable_pages);
1286 
1287  return area;
1288 }
bool mapping_pinned
Definition: dsa.c:360
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:307
uint32 dsm_handle
Definition: dsm_impl.h:54
#define contiguous_pages_to_segment_bin(n)
Definition: dsa.c:140
size_t usable_pages
Definition: dsa.c:156
#define DSA_NUM_SIZE_CLASSES
Definition: dsa.c:247
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsa_segment_index high_segment_index
Definition: dsa.c:315
dsm_segment * segment
Definition: dsa.c:341
size_t size
Definition: dsa.c:158
dsa_segment_index prev
Definition: dsa.c:164
size_t dsa_minimum_size(void)
Definition: dsa.c:1169
int lwlock_tranche_id
Definition: dsa.c:323
size_t total_segment_size
Definition: dsa.c:311
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
dsa_segment_header * header
Definition: dsa.c:343
LWLock lock
Definition: dsa.c:325
int refcnt
Definition: dsa.c:317
#define DSA_INITIAL_SEGMENT_SIZE
Definition: dsa.c:68
#define ERROR
Definition: elog.h:43
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
size_t max_total_segment_size
Definition: dsa.c:313
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:678
void FreePageManagerInitialize(FreePageManager *fpm, char *base)
Definition: freepage.c:183
#define DSA_SCLASS_LOCK(area, sclass)
Definition: dsa.c:144
dsa_segment_header segment_header
Definition: dsa.c:301
#define FPM_PAGE_SIZE
Definition: freepage.h:30
size_t freed_segment_counter
Definition: dsa.c:321
#define Assert(condition)
Definition: c.h:739
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
#define MAXALIGN(LEN)
Definition: c.h:692
size_t freed_segment_counter
Definition: dsa.c:374
dsa_pointer * pagemap
Definition: dsa.c:345
char * mapped_address
Definition: dsa.c:342
#define DSA_MAX_SEGMENTS
Definition: dsa.c:94
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
Definition: dsa.c:354
void * palloc(Size size)
Definition: mcxt.c:949
#define elog(elevel,...)
Definition: elog.h:228
int i
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
uint32 magic
Definition: dsa.c:154
size_t bin
Definition: dsa.c:172
dsa_handle handle
Definition: dsa.c:303

◆ destroy_superblock()

static void destroy_superblock ( dsa_area area,
dsa_pointer  span_pointer 
)
static

Definition at line 1803 of file dsa.c.

References Assert, check_for_freed_segments_locked(), dsa_area::control, DSA_AREA_LOCK, DSA_EXTRACT_OFFSET, DSA_EXTRACT_SEGMENT_NUMBER, dsa_free(), dsa_get_address(), DSA_SCLASS_BLOCK_OF_SPANS, dsm_detach(), DSM_HANDLE_INVALID, dsm_segment_handle(), dsm_unpin_segment(), dsa_segment_map::fpm, fpm_largest, FPM_PAGE_SIZE, dsa_segment_header::freed, dsa_area_control::freed_segment_counter, FreePageManagerPut(), get_segment_by_index(), get_segment_index, dsa_segment_map::header, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_segment_map::mapped_address, dsa_area_span::npages, dsa_segment_map::segment, dsa_area_control::segment_handles, dsa_segment_header::size, dsa_area_span::size_class, dsa_area_span::start, dsa_area_control::total_segment_size, unlink_segment(), unlink_span(), and dsa_segment_header::usable_pages.

Referenced by dsa_free(), and dsa_trim().

1804 {
1805  dsa_area_span *span = dsa_get_address(area, span_pointer);
1806  int size_class = span->size_class;
1807  dsa_segment_map *segment_map;
1808 
1809 
1810  /* Remove it from its fullness class list. */
1811  unlink_span(area, span);
1812 
1813  /*
1814  * Note: Here we acquire the area lock while we already hold a per-pool
1815  * lock. We never hold the area lock and then take a pool lock, or we
1816  * could deadlock.
1817  */
1820  segment_map =
1822  FreePageManagerPut(segment_map->fpm,
1824  span->npages);
1825  /* Check if the segment is now entirely free. */
1826  if (fpm_largest(segment_map->fpm) == segment_map->header->usable_pages)
1827  {
1828  dsa_segment_index index = get_segment_index(area, segment_map);
1829 
1830  /* If it's not the segment with extra control data, free it. */
1831  if (index != 0)
1832  {
1833  /*
1834  * Give it back to the OS, and allow other backends to detect that
1835  * they need to detach.
1836  */
1837  unlink_segment(area, segment_map);
1838  segment_map->header->freed = true;
1840  segment_map->header->size);
1841  area->control->total_segment_size -=
1842  segment_map->header->size;
1844  dsm_detach(segment_map->segment);
1845  area->control->segment_handles[index] = DSM_HANDLE_INVALID;
1846  ++area->control->freed_segment_counter;
1847  segment_map->segment = NULL;
1848  segment_map->header = NULL;
1849  segment_map->mapped_address = NULL;
1850  }
1851  }
1853 
1854  /*
1855  * Span-of-spans blocks store the span which describes them within the
1856  * block itself, so freeing the storage implicitly frees the descriptor
1857  * also. If this is a block of any other type, we need to separately free
1858  * the span object also. This recursive call to dsa_free will acquire the
1859  * span pool's lock. We can't deadlock because the acquisition order is
1860  * always some other pool and then the span pool.
1861  */
1862  if (size_class != DSA_SCLASS_BLOCK_OF_SPANS)
1863  dsa_free(area, span_pointer);
1864 }
size_t usable_pages
Definition: dsa.c:156
dsm_segment * segment
Definition: dsa.c:341
size_t size
Definition: dsa.c:158
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:966
dsa_pointer start
Definition: dsa.c:199
size_t total_segment_size
Definition: dsa.c:311
size_t npages
Definition: dsa.c:200
void FreePageManagerPut(FreePageManager *fpm, Size first_page, Size npages)
Definition: freepage.c:379
FreePageManager * fpm
Definition: dsa.c:344
dsa_segment_header * header
Definition: dsa.c:343
#define get_segment_index(area, segment_map_ptr)
Definition: dsa.c:381
Definition: type.h:89
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
uint16 size_class
Definition: dsa.c:201
dsa_area_control * control
Definition: dsa.c:357
void dsm_unpin_segment(dsm_handle handle)
Definition: dsm.c:837
static void unlink_span(dsa_area *area, dsa_area_span *span)
Definition: dsa.c:1867
static void unlink_segment(dsa_area *area, dsa_segment_map *segment_map)
Definition: dsa.c:1939
#define FPM_PAGE_SIZE
Definition: freepage.h:30
size_t freed_segment_counter
Definition: dsa.c:321
#define DSA_SCLASS_BLOCK_OF_SPANS
Definition: dsa.c:250
#define DSA_EXTRACT_SEGMENT_NUMBER(dp)
Definition: dsa.c:118
#define Assert(condition)
Definition: c.h:739
#define fpm_largest(fpm)
Definition: freepage.h:88
#define DSA_EXTRACT_OFFSET(dp)
Definition: dsa.c:121
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
size_t dsa_segment_index
Definition: dsa.c:124
char * mapped_address
Definition: dsa.c:342
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:820
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:664
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
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_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:608
#define lengthof(array)
Definition: c.h:669
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:358
#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:955
#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:739
#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:822
static dsa_segment_map * make_new_segment(dsa_area *area, size_t requested_pages)
Definition: dsa.c:2063
#define elog(elevel,...)
Definition: elog.h:228
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:608
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:822

◆ 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:1056
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:739
#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:739
#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:739
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:692

◆ dsa_on_dsm_detach_release_in_place()

void dsa_on_dsm_detach_release_in_place ( dsm_segment segment,
Datum  place 
)

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  code,
Datum  place 
)

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:228
#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:739
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:739
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:739
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define elog(elevel,...)
Definition: elog.h:228
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143

◆ ensure_active_superblock()

static bool ensure_active_superblock ( dsa_area area,
dsa_area_pool pool,
int  size_class 
)
static

Definition at line 1528 of file dsa.c.

References alloc_object(), Assert, DSA_AREA_LOCK, DSA_FULLNESS_CLASSES, dsa_get_address(), DSA_MAKE_POINTER, DSA_PAGES_PER_SUPERBLOCK, DSA_SCLASS_BLOCK_OF_SPANS, DSA_SCLASS_LOCK, dsa_size_classes, DSA_SUPERBLOCK_SIZE, DsaPointerIsValid, elog, FATAL, dsa_area_span::fclass, dsa_segment_map::fpm, FPM_PAGE_SIZE, FreePageManagerGet(), get_best_segment(), get_segment_index, i, init_span(), InvalidDsaPointer, LW_EXCLUSIVE, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), make_new_segment(), dsa_area_span::nallocatable, dsa_area_span::nextspan, dsa_segment_map::pagemap, dsa_area_span::prevspan, dsa_area_pool::spans, and transfer_first_span().

Referenced by alloc_object().

1530 {
1531  dsa_pointer span_pointer;
1532  dsa_pointer start_pointer;
1533  size_t obsize = dsa_size_classes[size_class];
1534  size_t nmax;
1535  int fclass;
1536  size_t npages = 1;
1537  size_t first_page;
1538  size_t i;
1539  dsa_segment_map *segment_map;
1540 
1541  Assert(LWLockHeldByMe(DSA_SCLASS_LOCK(area, size_class)));
1542 
1543  /*
1544  * Compute the number of objects that will fit in a block of this size
1545  * class. Span-of-spans blocks are just a single page, and the first
1546  * object isn't available for use because it describes the block-of-spans
1547  * itself.
1548  */
1549  if (size_class == DSA_SCLASS_BLOCK_OF_SPANS)
1550  nmax = FPM_PAGE_SIZE / obsize - 1;
1551  else
1552  nmax = DSA_SUPERBLOCK_SIZE / obsize;
1553 
1554  /*
1555  * If fullness class 1 is empty, try to find a span to put in it by
1556  * scanning higher-numbered fullness classes (excluding the last one,
1557  * whose blocks are certain to all be completely full).
1558  */
1559  for (fclass = 2; fclass < DSA_FULLNESS_CLASSES - 1; ++fclass)
1560  {
1561  span_pointer = pool->spans[fclass];
1562 
1563  while (DsaPointerIsValid(span_pointer))
1564  {
1565  int tfclass;
1566  dsa_area_span *span;
1567  dsa_area_span *nextspan;
1568  dsa_area_span *prevspan;
1569  dsa_pointer next_span_pointer;
1570 
1571  span = (dsa_area_span *)
1572  dsa_get_address(area, span_pointer);
1573  next_span_pointer = span->nextspan;
1574 
1575  /* Figure out what fullness class should contain this span. */
1576  tfclass = (nmax - span->nallocatable)
1577  * (DSA_FULLNESS_CLASSES - 1) / nmax;
1578 
1579  /* Look up next span. */
1580  if (DsaPointerIsValid(span->nextspan))
1581  nextspan = (dsa_area_span *)
1582  dsa_get_address(area, span->nextspan);
1583  else
1584  nextspan = NULL;
1585 
1586  /*
1587  * If utilization has dropped enough that this now belongs in some
1588  * other fullness class, move it there.
1589  */
1590  if (tfclass < fclass)
1591  {
1592  /* Remove from the current fullness class list. */
1593  if (pool->spans[fclass] == span_pointer)
1594  {
1595  /* It was the head; remove it. */
1597  pool->spans[fclass] = span->nextspan;
1598  if (nextspan != NULL)
1599  nextspan->prevspan = InvalidDsaPointer;
1600  }
1601  else
1602  {
1603  /* It was not the head. */
1605  prevspan = (dsa_area_span *)
1606  dsa_get_address(area, span->prevspan);
1607  prevspan->nextspan = span->nextspan;
1608  }
1609  if (nextspan != NULL)
1610  nextspan->prevspan = span->prevspan;
1611 
1612  /* Push onto the head of the new fullness class list. */
1613  span->nextspan = pool->spans[tfclass];
1614  pool->spans[tfclass] = span_pointer;
1615  span->prevspan = InvalidDsaPointer;
1616  if (DsaPointerIsValid(span->nextspan))
1617  {
1618  nextspan = (dsa_area_span *)
1619  dsa_get_address(area, span->nextspan);
1620  nextspan->prevspan = span_pointer;
1621  }
1622  span->fclass = tfclass;
1623  }
1624 
1625  /* Advance to next span on list. */
1626  span_pointer = next_span_pointer;
1627  }
1628 
1629  /* Stop now if we found a suitable block. */
1630  if (DsaPointerIsValid(pool->spans[1]))
1631  return true;
1632  }
1633 
1634  /*
1635  * If there are no blocks that properly belong in fullness class 1, pick
1636  * one from some other fullness class and move it there anyway, so that we
1637  * have an allocation target. Our last choice is to transfer a block
1638  * that's almost empty (and might become completely empty soon if left
1639  * alone), but even that is better than failing, which is what we must do
1640  * if there are no blocks at all with freespace.
1641  */
1642  Assert(!DsaPointerIsValid(pool->spans[1]));
1643  for (fclass = 2; fclass < DSA_FULLNESS_CLASSES - 1; ++fclass)
1644  if (transfer_first_span(area, pool, fclass, 1))
1645  return true;
1646  if (!DsaPointerIsValid(pool->spans[1]) &&
1647  transfer_first_span(area, pool, 0, 1))
1648  return true;
1649 
1650  /*
1651  * We failed to find an existing span with free objects, so we need to
1652  * allocate a new superblock and construct a new span to manage it.
1653  *
1654  * First, get a dsa_area_span object to describe the new superblock block
1655  * ... unless this allocation is for a dsa_area_span object, in which case
1656  * that's surely not going to work. We handle that case by storing the
1657  * span describing a block-of-spans inline.
1658  */
1659  if (size_class != DSA_SCLASS_BLOCK_OF_SPANS)
1660  {
1661  span_pointer = alloc_object(area, DSA_SCLASS_BLOCK_OF_SPANS);
1662  if (!DsaPointerIsValid(span_pointer))
1663  return false;
1664  npages = DSA_PAGES_PER_SUPERBLOCK;
1665  }
1666 
1667  /* Find or create a segment and allocate the superblock. */
1669  segment_map = get_best_segment(area, npages);
1670  if (segment_map == NULL)
1671  {
1672  segment_map = make_new_segment(area, npages);
1673  if (segment_map == NULL)
1674  {
1676  return false;
1677  }
1678  }
1679 
1680  /*
1681  * This shouldn't happen: get_best_segment() or make_new_segment()
1682  * promised that we can successfully allocate npages.
1683  */
1684  if (!FreePageManagerGet(segment_map->fpm, npages, &first_page))
1685  elog(FATAL,
1686  "dsa_allocate could not find %zu free pages for superblock",
1687  npages);
1689 
1690  /* Compute the start of the superblock. */
1691  start_pointer =
1692  DSA_MAKE_POINTER(get_segment_index(area, segment_map),
1693  first_page * FPM_PAGE_SIZE);
1694 
1695  /*
1696  * If this is a block-of-spans, carve the descriptor right out of the
1697  * allocated space.
1698  */
1699  if (size_class == DSA_SCLASS_BLOCK_OF_SPANS)
1700  {
1701  /*
1702  * We have a pointer into the segment. We need to build a dsa_pointer
1703  * from the segment index and offset into the segment.
1704  */
1705  span_pointer = start_pointer;
1706  }
1707 
1708  /* Initialize span and pagemap. */
1709  init_span(area, span_pointer, pool, start_pointer, npages, size_class);
1710  for (i = 0; i < npages; ++i)
1711  segment_map->pagemap[first_page + i] = span_pointer;
1712 
1713  return true;
1714 }
#define DSA_SUPERBLOCK_SIZE
Definition: dsa.c:378
dsa_pointer nextspan
Definition: dsa.c:198
#define InvalidDsaPointer
Definition: dsa.h:78
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
#define DSA_FULLNESS_CLASSES
Definition: dsa.c:277
uint64 dsa_pointer
Definition: dsa.h:62
FreePageManager * fpm
Definition: dsa.c:344
uint16 fclass
Definition: dsa.c:206
#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
#define FATAL
Definition: elog.h:52
static bool transfer_first_span(dsa_area *area, dsa_area_pool *pool, int fromclass, int toclass)
Definition: dsa.c:1400
static const uint16 dsa_size_classes[]
Definition: dsa.c:236
#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
#define DSA_SCLASS_BLOCK_OF_SPANS
Definition: dsa.c:250
#define Assert(condition)
Definition: c.h:739
#define DSA_MAKE_POINTER(segment_number, offset)
Definition: dsa.c:114
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
dsa_pointer * pagemap
Definition: dsa.c:345
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
#define DsaPointerIsValid(x)
Definition: dsa.h:81
uint16 nallocatable
Definition: dsa.c:203
static dsa_segment_map * make_new_segment(dsa_area *area, size_t requested_pages)
Definition: dsa.c:2063
#define elog(elevel,...)
Definition: elog.h:228
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
int i
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_PAGES_PER_SUPERBLOCK
Definition: dsa.c:104
static dsa_segment_map * get_best_segment(dsa_area *area, size_t npages)
Definition: dsa.c:1971

◆ get_best_segment()

static dsa_segment_map * get_best_segment ( dsa_area area,
size_t  npages 
)
static

Definition at line 1971 of file dsa.c.

References Assert, dsa_segment_header::bin, check_for_freed_segments_locked(), contiguous_pages_to_segment_bin, dsa_area::control, DSA_AREA_LOCK, DSA_NUM_SEGMENT_BINS, DSA_SEGMENT_INDEX_NONE, dsa_segment_map::fpm, fpm_largest, get_segment_by_index(), dsa_segment_map::header, LWLockHeldByMe(), dsa_segment_header::next, next, dsa_segment_header::prev, dsa_area_control::segment_bins, and unlink_segment().

Referenced by dsa_allocate_extended(), and ensure_active_superblock().

1972 {
1973  size_t bin;
1974 
1977 
1978  /*
1979  * Start searching from the first bin that *might* have enough contiguous
1980  * pages.
1981  */
1982  for (bin = contiguous_pages_to_segment_bin(npages);
1983  bin < DSA_NUM_SEGMENT_BINS;
1984  ++bin)
1985  {
1986  /*
1987  * The minimum contiguous size that any segment in this bin should
1988  * have. We'll re-bin if we see segments with fewer.
1989  */
1990  size_t threshold = (size_t) 1 << (bin - 1);
1991  dsa_segment_index segment_index;
1992 
1993  /* Search this bin for a segment with enough contiguous space. */
1994  segment_index = area->control->segment_bins[bin];
1995  while (segment_index != DSA_SEGMENT_INDEX_NONE)
1996  {
1997  dsa_segment_map *segment_map;
1998  dsa_segment_index next_segment_index;
1999  size_t contiguous_pages;
2000 
2001  segment_map = get_segment_by_index(area, segment_index);
2002  next_segment_index = segment_map->header->next;
2003  contiguous_pages = fpm_largest(segment_map->fpm);
2004 
2005  /* Not enough for the request, still enough for this bin. */
2006  if (contiguous_pages >= threshold && contiguous_pages < npages)
2007  {
2008  segment_index = next_segment_index;
2009  continue;
2010  }
2011 
2012  /* Re-bin it if it's no longer in the appropriate bin. */
2013  if (contiguous_pages < threshold)
2014  {
2015  size_t new_bin;
2016 
2017  new_bin = contiguous_pages_to_segment_bin(contiguous_pages);
2018 
2019  /* Remove it from its current bin. */
2020  unlink_segment(area, segment_map);
2021 
2022  /* Push it onto the front of its new bin. */
2023  segment_map->header->prev = DSA_SEGMENT_INDEX_NONE;
2024  segment_map->header->next =
2025  area->control->segment_bins[new_bin];
2026  segment_map->header->bin = new_bin;
2027  area->control->segment_bins[new_bin] = segment_index;
2028  if (segment_map->header->next != DSA_SEGMENT_INDEX_NONE)
2029  {
2031 
2032  next = get_segment_by_index(area,
2033  segment_map->header->next);
2034  Assert(next->header->bin == new_bin);
2035  next->header->prev = segment_index;
2036  }
2037 
2038  /*
2039  * But fall through to see if it's enough to satisfy this
2040  * request anyway....
2041  */
2042  }
2043 
2044  /* Check if we are done. */
2045  if (contiguous_pages >= npages)
2046  return segment_map;
2047 
2048  /* Continue searching the same bin. */
2049  segment_index = next_segment_index;
2050  }
2051  }
2052 
2053  /* Not found. */
2054  return NULL;
2055 }
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:307
static int32 next
Definition: blutils.c:213
#define contiguous_pages_to_segment_bin(n)
Definition: dsa.c:140
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
dsa_segment_index prev
Definition: dsa.c:164
FreePageManager * fpm
Definition: dsa.c:344
dsa_segment_header * header
Definition: dsa.c:343
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 void unlink_segment(dsa_area *area, dsa_segment_map *segment_map)
Definition: dsa.c:1939
#define Assert(condition)
Definition: c.h:739
#define fpm_largest(fpm)
Definition: freepage.h:88
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
size_t dsa_segment_index
Definition: dsa.c:124
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
size_t bin
Definition: dsa.c:172
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

◆ get_segment_by_index()

static dsa_segment_map * get_segment_by_index ( dsa_area area,
dsa_segment_index  index 
)
static

Definition at line 1725 of file dsa.c.

References Assert, dsa_area::control, DSA_SEGMENT_HEADER_MAGIC, dsm_attach(), DSM_HANDLE_INVALID, dsm_pin_mapping(), dsm_segment_address(), elog, ERROR, dsa_segment_map::fpm, dsa_segment_header::freed, dsa_area_control::handle, dsa_segment_map::header, dsa_area::high_segment_index, dsa_segment_header::magic, dsa_segment_map::mapped_address, dsa_area::mapping_pinned, MAXALIGN, dsa_segment_map::pagemap, dsa_segment_map::segment, dsa_area_control::segment_handles, dsa_area::segment_maps, and unlikely.

Referenced by destroy_superblock(), dsa_dump(), dsa_free(), dsa_get_address(), get_best_segment(), make_new_segment(), and unlink_segment().

1726 {
1727  if (unlikely(area->segment_maps[index].mapped_address == NULL))
1728  {
1729  dsm_handle handle;
1730  dsm_segment *segment;
1731  dsa_segment_map *segment_map;
1732 
1733  /*
1734  * If we are reached by dsa_free or dsa_get_address, there must be at
1735  * least one object allocated in the referenced segment. Otherwise,
1736  * their caller has a double-free or access-after-free bug, which we
1737  * have no hope of detecting. So we know it's safe to access this
1738  * array slot without holding a lock; it won't change underneath us.
1739  * Furthermore, we know that we can see the latest contents of the
1740  * slot, as explained in check_for_freed_segments, which those
1741  * functions call before arriving here.
1742  */
1743  handle = area->control->segment_handles[index];
1744 
1745  /* It's an error to try to access an unused slot. */
1746  if (handle == DSM_HANDLE_INVALID)
1747  elog(ERROR,
1748  "dsa_area could not attach to a segment that has been freed");
1749 
1750  segment = dsm_attach(handle);
1751  if (segment == NULL)
1752  elog(ERROR, "dsa_area could not attach to segment");
1753  if (area->mapping_pinned)
1754  dsm_pin_mapping(segment);
1755  segment_map = &area->segment_maps[index];
1756  segment_map->segment = segment;
1757  segment_map->mapped_address = dsm_segment_address(segment);
1758  segment_map->header =
1759  (dsa_segment_header *) segment_map->mapped_address;
1760  segment_map->fpm = (FreePageManager *)
1761  (segment_map->mapped_address +
1762  MAXALIGN(sizeof(dsa_segment_header)));
1763  segment_map->pagemap = (dsa_pointer *)
1764  (segment_map->mapped_address +
1765  MAXALIGN(sizeof(dsa_segment_header)) +
1766  MAXALIGN(sizeof(FreePageManager)));
1767 
1768  /* Remember the highest index this backend has ever mapped. */
1769  if (area->high_segment_index < index)
1770  area->high_segment_index = index;
1771 
1772  Assert(segment_map->header->magic ==
1774  }
1775 
1776  /*
1777  * Callers of dsa_get_address() and dsa_free() don't hold the area lock,
1778  * but it's a bug in the calling code and undefined behavior if the
1779  * address is not live (ie if the segment might possibly have been freed,
1780  * they're trying to use a dangling pointer).
1781  *
1782  * For dsa.c code that holds the area lock to manipulate segment_bins
1783  * lists, it would be a bug if we ever reach a freed segment here. After
1784  * it's marked as freed, the only thing any backend should do with it is
1785  * unmap it, and it should always have done that in
1786  * check_for_freed_segments_locked() before arriving here to resolve an
1787  * index to a segment_map.
1788  *
1789  * Either way we can assert that we aren't returning a freed segment.
1790  */
1791  Assert(!area->segment_maps[index].header->freed);
1792 
1793  return &area->segment_maps[index];
1794 }
bool mapping_pinned
Definition: dsa.c:360
uint32 dsm_handle
Definition: dsm_impl.h:54
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:533
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsm_segment * segment
Definition: dsa.c:341
uint64 dsa_pointer
Definition: dsa.h:62
FreePageManager * fpm
Definition: dsa.c:344
dsa_segment_header * header
Definition: dsa.c:343
Definition: type.h:89
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define ERROR
Definition: elog.h:43
void dsm_pin_mapping(dsm_segment *seg)
Definition: dsm.c:765
dsa_area_control * control
Definition: dsa.c:357
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:938
#define Assert(condition)
Definition: c.h:739
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
#define MAXALIGN(LEN)
Definition: c.h:692
dsa_pointer * pagemap
Definition: dsa.c:345
char * mapped_address
Definition: dsa.c:342
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
#define elog(elevel,...)
Definition: elog.h:228
#define unlikely(x)
Definition: c.h:208
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
uint32 magic
Definition: dsa.c:154
dsa_handle handle
Definition: dsa.c:303

◆ init_span()

static void init_span ( dsa_area area,
dsa_pointer  span_pointer,
dsa_area_pool pool,
dsa_pointer  start,
size_t  npages,
uint16  size_class 
)
static

Definition at line 1345 of file dsa.c.

References Assert, dsa_get_address(), DSA_SCLASS_BLOCK_OF_SPANS, DSA_SCLASS_LOCK, DSA_SCLASS_SPAN_LARGE, dsa_size_classes, DSA_SPAN_NOTHING_FREE, DSA_SUPERBLOCK_SIZE, DsaAreaPoolToDsaPointer, DsaPointerIsValid, dsa_area_span::fclass, dsa_area_span::firstfree, FPM_PAGE_SIZE, InvalidDsaPointer, LWLockHeldByMe(), dsa_area_span::nallocatable, dsa_area_span::nextspan, dsa_area_span::ninitialized, dsa_area_span::nmax, dsa_area_span::npages, dsa_area_span::pool, dsa_area_span::prevspan, dsa_area_span::size_class, dsa_area_pool::spans, and dsa_area_span::start.

Referenced by dsa_allocate_extended(), and ensure_active_superblock().

1349 {
1350  dsa_area_span *span = dsa_get_address(area, span_pointer);
1351  size_t obsize = dsa_size_classes[size_class];
1352 
1353  /*
1354  * The per-pool lock must be held because we manipulate the span list for
1355  * this pool.
1356  */
1357  Assert(LWLockHeldByMe(DSA_SCLASS_LOCK(area, size_class)));
1358 
1359  /* Push this span onto the front of the span list for fullness class 1. */
1360  if (DsaPointerIsValid(pool->spans[1]))
1361  {
1362  dsa_area_span *head = (dsa_area_span *)
1363  dsa_get_address(area, pool->spans[1]);
1364 
1365  head->prevspan = span_pointer;
1366  }
1367  span->pool = DsaAreaPoolToDsaPointer(area, pool);
1368  span->nextspan = pool->spans[1];
1369  span->prevspan = InvalidDsaPointer;
1370  pool->spans[1] = span_pointer;
1371 
1372  span->start = start;
1373  span->npages = npages;
1374  span->size_class = size_class;
1375  span->ninitialized = 0;
1376  if (size_class == DSA_SCLASS_BLOCK_OF_SPANS)
1377  {
1378  /*
1379  * A block-of-spans contains its own descriptor, so mark one object as
1380  * initialized and reduce the count of allocatable objects by one.
1381  * Doing this here has the side effect of also reducing nmax by one,
1382  * which is important to make sure we free this object at the correct
1383  * time.
1384  */
1385  span->ninitialized = 1;
1386  span->nallocatable = FPM_PAGE_SIZE / obsize - 1;
1387  }
1388  else if (size_class != DSA_SCLASS_SPAN_LARGE)
1389  span->nallocatable = DSA_SUPERBLOCK_SIZE / obsize;
1391  span->nmax = span->nallocatable;
1392  span->fclass = 1;
1393 }
uint16 nmax
Definition: dsa.c:205
#define DSA_SPAN_NOTHING_FREE
Definition: dsa.c:377
#define DSA_SUPERBLOCK_SIZE
Definition: dsa.c:378
dsa_pointer nextspan
Definition: dsa.c:198
#define InvalidDsaPointer
Definition: dsa.h:78
uint16 ninitialized
Definition: dsa.c:202
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
dsa_pointer start
Definition: dsa.c:199
size_t npages
Definition: dsa.c:200
uint16 fclass
Definition: dsa.c:206
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
#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
#define DSA_SCLASS_BLOCK_OF_SPANS
Definition: dsa.c:250
#define Assert(condition)
Definition: c.h:739
dsa_pointer pool
Definition: dsa.c:196
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
#define DsaPointerIsValid(x)
Definition: dsa.h:81
uint16 nallocatable
Definition: dsa.c:203
uint16 firstfree
Definition: dsa.c:204
#define DSA_SCLASS_SPAN_LARGE
Definition: dsa.c:251
#define DsaAreaPoolToDsaPointer(area, p)
Definition: dsa.c:329

◆ make_new_segment()

static dsa_segment_map * make_new_segment ( dsa_area area,
size_t  requested_pages 
)
static

Definition at line 2063 of file dsa.c.

References Assert, dsa_segment_header::bin, contiguous_pages_to_segment_bin, dsa_area::control, DSA_AREA_LOCK, DSA_INITIAL_SEGMENT_SIZE, DSA_MAX_SEGMENT_SIZE, DSA_MAX_SEGMENTS, DSA_NUM_SEGMENTS_AT_EACH_SIZE, DSA_SEGMENT_HEADER_MAGIC, DSA_SEGMENT_INDEX_NONE, dsm_create(), DSM_HANDLE_INVALID, dsm_pin_mapping(), dsm_pin_segment(), dsm_segment_address(), dsm_segment_handle(), dsa_segment_map::fpm, FPM_PAGE_SIZE, dsa_segment_header::freed, FreePageManagerInitialize(), FreePageManagerPut(), get_segment_by_index(), dsa_area_control::handle, dsa_segment_map::header, dsa_area_control::high_segment_index, dsa_area::high_segment_index, LWLockHeldByMe(), dsa_segment_header::magic, dsa_segment_map::mapped_address, dsa_area::mapping_pinned, dsa_area_control::max_total_segment_size, MAXALIGN, Min, dsa_segment_header::next, next, dsa_segment_map::pagemap, dsa_segment_header::prev, dsa_segment_map::segment, dsa_area_control::segment_bins, dsa_area_control::segment_handles, dsa_area::segment_maps, dsa_segment_header::size, dsa_area_control::total_segment_size, total_size, and dsa_segment_header::usable_pages.

Referenced by dsa_allocate_extended(), and ensure_active_superblock().

2064 {
2065  dsa_segment_index new_index;
2066  size_t metadata_bytes;
2067  size_t total_size;
2068  size_t total_pages;
2069  size_t usable_pages;
2070  dsa_segment_map *segment_map;
2071  dsm_segment *segment;
2072 
2074 
2075  /* Find a segment slot that is not in use (linearly for now). */
2076  for (new_index = 1; new_index < DSA_MAX_SEGMENTS; ++new_index)
2077  {
2078  if (area->control->segment_handles[new_index] == DSM_HANDLE_INVALID)
2079  break;
2080  }
2081  if (new_index == DSA_MAX_SEGMENTS)
2082  return NULL;
2083 
2084  /*
2085  * If the total size limit is already exceeded, then we exit early and
2086  * avoid arithmetic wraparound in the unsigned expressions below.
2087  */
2088  if (area->control->total_segment_size >=
2090  return NULL;
2091 
2092  /*
2093  * The size should be at least as big as requested, and at least big
2094  * enough to follow a geometric series that approximately doubles the
2095  * total storage each time we create a new segment. We use geometric
2096  * growth because the underlying DSM system isn't designed for large
2097  * numbers of segments (otherwise we might even consider just using one
2098  * DSM segment for each large allocation and for each superblock, and then
2099  * we wouldn't need to use FreePageManager).
2100  *
2101  * We decide on a total segment size first, so that we produce tidy
2102  * power-of-two sized segments. This is a good property to have if we
2103  * move to huge pages in the future. Then we work back to the number of
2104  * pages we can fit.
2105  */
2106  total_size = DSA_INITIAL_SEGMENT_SIZE *
2107  ((size_t) 1 << (new_index / DSA_NUM_SEGMENTS_AT_EACH_SIZE));
2108  total_size = Min(total_size, DSA_MAX_SEGMENT_SIZE);
2109  total_size = Min(total_size,
2111  area->control->total_segment_size);
2112 
2113  total_pages = total_size / FPM_PAGE_SIZE;
2114  metadata_bytes =
2115  MAXALIGN(sizeof(dsa_segment_header)) +
2116  MAXALIGN(sizeof(FreePageManager)) +
2117  sizeof(dsa_pointer) * total_pages;
2118 
2119  /* Add padding up to next page boundary. */
2120  if (metadata_bytes % FPM_PAGE_SIZE != 0)
2121  metadata_bytes += FPM_PAGE_SIZE - (metadata_bytes % FPM_PAGE_SIZE);
2122  if (total_size <= metadata_bytes)
2123  return NULL;
2124  usable_pages = (total_size - metadata_bytes) / FPM_PAGE_SIZE;
2125  Assert(metadata_bytes + usable_pages * FPM_PAGE_SIZE <= total_size);
2126 
2127  /* See if that is enough... */
2128  if (requested_pages > usable_pages)
2129  {
2130  /*
2131  * We'll make an odd-sized segment, working forward from the requested
2132  * number of pages.
2133  */
2134  usable_pages = requested_pages;
2135  metadata_bytes =
2136  MAXALIGN(sizeof(dsa_segment_header)) +
2137  MAXALIGN(sizeof(FreePageManager)) +
2138  usable_pages * sizeof(dsa_pointer);
2139 
2140  /* Add padding up to next page boundary. */
2141  if (metadata_bytes % FPM_PAGE_SIZE != 0)
2142  metadata_bytes += FPM_PAGE_SIZE - (metadata_bytes % FPM_PAGE_SIZE);
2143  total_size = metadata_bytes + usable_pages * FPM_PAGE_SIZE;
2144 
2145  /* Is that too large for dsa_pointer's addressing scheme? */
2146  if (total_size > DSA_MAX_SEGMENT_SIZE)
2147  return NULL;
2148 
2149  /* Would that exceed the limit? */
2150  if (total_size > area->control->max_total_segment_size -
2151  area->control->total_segment_size)
2152  return NULL;
2153  }
2154 
2155  /* Create the segment. */
2156  segment = dsm_create(total_size, 0);
2157  if (segment == NULL)
2158  return NULL;
2159  dsm_pin_segment(segment);
2160  if (area->mapping_pinned)
2161  dsm_pin_mapping(segment);
2162 
2163  /* Store the handle in shared memory to be found by index. */
2164  area->control->segment_handles[new_index] =
2165  dsm_segment_handle(segment);
2166  /* Track the highest segment index in the history of the area. */
2167  if (area->control->high_segment_index < new_index)
2168  area->control->high_segment_index = new_index;
2169  /* Track the highest segment index this backend has ever mapped. */
2170  if (area->high_segment_index < new_index)
2171  area->high_segment_index = new_index;
2172  /* Track total size of all segments. */
2176 
2177  /* Build a segment map for this segment in this backend. */
2178  segment_map = &area->segment_maps[new_index];
2179  segment_map->segment = segment;
2180  segment_map->mapped_address = dsm_segment_address(segment);
2181  segment_map->header = (dsa_segment_header *) segment_map->mapped_address;
2182  segment_map->fpm = (FreePageManager *)
2183  (segment_map->mapped_address +
2184  MAXALIGN(sizeof(dsa_segment_header)));
2185  segment_map->pagemap = (dsa_pointer *)
2186  (segment_map->mapped_address +
2187  MAXALIGN(sizeof(dsa_segment_header)) +
2188  MAXALIGN(sizeof(FreePageManager)));
2189 
2190  /* Set up the free page map. */
2191  FreePageManagerInitialize(segment_map->fpm, segment_map->mapped_address);
2192  FreePageManagerPut(segment_map->fpm, metadata_bytes / FPM_PAGE_SIZE,
2193  usable_pages);
2194 
2195  /* Set up the segment header and put it in the appropriate bin. */
2196  segment_map->header->magic =
2197  DSA_SEGMENT_HEADER_MAGIC ^ area->control->handle ^ new_index;
2198  segment_map->header->usable_pages = usable_pages;
2199  segment_map->header->size = total_size;
2200  segment_map->header->bin = contiguous_pages_to_segment_bin(usable_pages);
2201  segment_map->header->prev = DSA_SEGMENT_INDEX_NONE;
2202  segment_map->header->next =
2203  area->control->segment_bins[segment_map->header->bin];
2204  segment_map->header->freed = false;
2205  area->control->segment_bins[segment_map->header->bin] = new_index;
2206  if (segment_map->header->next != DSA_SEGMENT_INDEX_NONE)
2207  {
2209  get_segment_by_index(area, segment_map->header->next);
2210 
2211  Assert(next->header->bin == segment_map->header->bin);
2212  next->header->prev = new_index;
2213  }
2214 
2215  return segment_map;
2216 }
bool mapping_pinned
Definition: dsa.c:360
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:307
static int32 next
Definition: blutils.c:213
#define contiguous_pages_to_segment_bin(n)
Definition: dsa.c:140
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1842
size_t usable_pages
Definition: dsa.c:156
dsa_segment_index high_segment_index
Definition: dsa.c:371
#define Min(x, y)
Definition: c.h:911
dsa_segment_index high_segment_index
Definition: dsa.c:315
dsm_segment * segment
Definition: dsa.c:341
size_t size
Definition: dsa.c:158
dsa_segment_index prev
Definition: dsa.c:164
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:966
size_t total_segment_size
Definition: dsa.c:311
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
#define DSA_MAX_SEGMENT_SIZE
Definition: dsa.c:101
dsa_segment_header * header
Definition: dsa.c:343
void dsm_pin_segment(dsm_segment *seg)
Definition: dsm.c:805
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
#define DSA_INITIAL_SEGMENT_SIZE
Definition: dsa.c:68
void dsm_pin_mapping(dsm_segment *seg)
Definition: dsm.c:765
dsa_segment_index next
Definition: dsa.c:170
dsa_area_control * control
Definition: dsa.c:357
#define DSA_NUM_SEGMENTS_AT_EACH_SIZE
Definition: dsa.c:77
size_t max_total_segment_size
Definition: dsa.c:313
void FreePageManagerInitialize(FreePageManager *fpm, char *base)
Definition: freepage.c:183
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:437
int64 total_size
Definition: pg_checksums.c:68
#define FPM_PAGE_SIZE
Definition: freepage.h:30
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:938
#define Assert(condition)
Definition: c.h:739
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
size_t dsa_segment_index
Definition: dsa.c:124
#define MAXALIGN(LEN)
Definition: c.h:692
dsa_pointer * pagemap
Definition: dsa.c:345
char * mapped_address
Definition: dsa.c:342
#define DSA_MAX_SEGMENTS
Definition: dsa.c:94
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
uint32 magic
Definition: dsa.c:154
size_t bin
Definition: dsa.c:172
dsa_handle handle
Definition: dsa.c:303
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1725

◆ transfer_first_span()

static bool transfer_first_span ( dsa_area area,
dsa_area_pool pool,
int  fromclass,
int  toclass 
)
static

Definition at line 1400 of file dsa.c.

References dsa_get_address(), DsaPointerIsValid, dsa_area_span::fclass, InvalidDsaPointer, dsa_area_span::nextspan, dsa_area_span::prevspan, and dsa_area_pool::spans.

Referenced by alloc_object(), and ensure_active_superblock().

1402 {
1403  dsa_pointer span_pointer;
1404  dsa_area_span *span;
1405  dsa_area_span *nextspan;
1406 
1407  /* Can't do it if source list is empty. */
1408  span_pointer = pool->spans[fromclass];
1409  if (!DsaPointerIsValid(span_pointer))
1410  return false;
1411 
1412  /* Remove span from head of source list. */
1413  span = dsa_get_address(area, span_pointer);
1414  pool->spans[fromclass] = span->nextspan;
1415  if (DsaPointerIsValid(span->nextspan))
1416  {
1417  nextspan = (dsa_area_span *)
1418  dsa_get_address(area, span->nextspan);
1419  nextspan->prevspan = InvalidDsaPointer;
1420  }
1421 
1422  /* Add span to head of target list. */
1423  span->nextspan = pool->spans[toclass];
1424  pool->spans[toclass] = span_pointer;
1425  if (DsaPointerIsValid(span->nextspan))
1426  {
1427  nextspan = (dsa_area_span *)
1428  dsa_get_address(area, span->nextspan);
1429  nextspan->prevspan = span_pointer;
1430  }
1431  span->fclass = toclass;
1432 
1433  return true;
1434 }
dsa_pointer nextspan
Definition: dsa.c:198
#define InvalidDsaPointer
Definition: dsa.h:78
uint64 dsa_pointer
Definition: dsa.h:62
uint16 fclass
Definition: dsa.c:206
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
dsa_pointer prevspan
Definition: dsa.c:197
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
#define DsaPointerIsValid(x)
Definition: dsa.h:81

◆ unlink_segment()

static void unlink_segment ( dsa_area area,
dsa_segment_map segment_map 
)
static

Definition at line 1939 of file dsa.c.

References Assert, dsa_segment_header::bin, dsa_area::control, DSA_SEGMENT_INDEX_NONE, get_segment_by_index(), get_segment_index, dsa_segment_map::header, dsa_segment_header::next, next, dsa_segment_header::prev, and dsa_area_control::segment_bins.

Referenced by destroy_superblock(), and get_best_segment().

1940 {
1941  if (segment_map->header->prev != DSA_SEGMENT_INDEX_NONE)
1942  {
1943  dsa_segment_map *prev;
1944 
1945  prev = get_segment_by_index(area, segment_map->header->prev);
1946  prev->header->next = segment_map->header->next;
1947  }
1948  else
1949  {
1950  Assert(area->control->segment_bins[segment_map->header->bin] ==
1951  get_segment_index(area, segment_map));
1952  area->control->segment_bins[segment_map->header->bin] =
1953  segment_map->header->next;
1954  }
1955  if (segment_map->header->next != DSA_SEGMENT_INDEX_NONE)
1956  {
1958 
1959  next = get_segment_by_index(area, segment_map->header->next);
1960  next->header->prev = segment_map->header->prev;
1961  }
1962 }
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:307
static int32 next
Definition: blutils.c:213
dsa_segment_index prev
Definition: dsa.c:164
dsa_segment_header * header
Definition: dsa.c:343
#define get_segment_index(area, segment_map_ptr)
Definition: dsa.c:381
dsa_segment_index next
Definition: dsa.c:170
dsa_area_control * control
Definition: dsa.c:357
#define Assert(condition)
Definition: c.h:739
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
size_t bin
Definition: dsa.c:172
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1725

◆ unlink_span()

static void unlink_span ( dsa_area area,
dsa_area_span span 
)
static

Definition at line 1867 of file dsa.c.

References dsa_get_address(), DsaPointerIsValid, dsa_area_span::fclass, next, dsa_area_span::nextspan, dsa_area_span::pool, dsa_area_span::prevspan, and dsa_area_pool::spans.

Referenced by destroy_superblock(), and dsa_free().

1868 {
1869  if (DsaPointerIsValid(span->nextspan))
1870  {
1871  dsa_area_span *next = dsa_get_address(area, span->nextspan);
1872 
1873  next->prevspan = span->prevspan;
1874  }
1875  if (DsaPointerIsValid(span->prevspan))
1876  {
1877  dsa_area_span *prev = dsa_get_address(area, span->prevspan);
1878 
1879  prev->nextspan = span->nextspan;
1880  }
1881  else
1882  {
1883  dsa_area_pool *pool = dsa_get_address(area, span->pool);
1884 
1885  pool->spans[span->fclass] = span->nextspan;
1886  }
1887 }
dsa_pointer nextspan
Definition: dsa.c:198
static int32 next
Definition: blutils.c:213
uint16 fclass
Definition: dsa.c:206
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:932
dsa_pointer prevspan
Definition: dsa.c:197
dsa_pointer pool
Definition: dsa.c:196
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
#define DsaPointerIsValid(x)
Definition: dsa.h:81

Variable Documentation

◆ dsa_size_class_map

const uint8 dsa_size_class_map[]
static
Initial value:
= {
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 11, 11, 12, 12, 13, 13,
14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17,
18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19,
20, 20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25
}

Definition at line 259 of file dsa.c.

Referenced by dsa_allocate_extended().

◆ dsa_size_classes

const uint16 dsa_size_classes[]
static
Initial value:
= {
sizeof(dsa_area_span), 0,
8, 16, 24, 32, 40, 48, 56, 64,
80, 96, 112, 128,
160, 192, 224, 256,
320, 384, 448, 512,
640, 768, 896, 1024,
1280, 1560, 1816, 2048,
2616, 3120, 3640, 4096,
5456, 6552, 7280, 8192
}

Definition at line 236 of file dsa.c.

Referenced by alloc_object(), dsa_allocate_extended(), dsa_dump(), dsa_free(), ensure_active_superblock(), and init_span().