PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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) (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) 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 dsa_segment_index
 

Functions

static void init_span (dsa_area *area, dsa_pointer span_pointer, dsa_area_pool *pool, dsa_pointer start, Size 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 npages)
 
static dsa_segment_mapmake_new_segment (dsa_area *area, Size 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)
 
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 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 limit)
 
void dsa_trim (dsa_area *area)
 
void dsa_dump (dsa_area *area)
 
Size dsa_minimum_size (void)
 
void dsa_detach (dsa_area *area)
 

Variables

static const uint16 dsa_size_classes []
 
static char dsa_size_class_map []
 

Macro Definition Documentation

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

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

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

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

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

Definition at line 68 of file dsa.c.

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

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

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

Definition at line 101 of file dsa.c.

Referenced by make_new_segment().

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

#define DSA_NUM_SEGMENT_BINS   16

Definition at line 133 of file dsa.c.

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

#define DSA_NUM_SEGMENTS_AT_EACH_SIZE   4

Definition at line 77 of file dsa.c.

Referenced by make_new_segment().

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

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

Definition at line 98 of file dsa.c.

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

Definition at line 87 of file dsa.c.

#define DSA_PAGES_PER_SUPERBLOCK   16

Definition at line 104 of file dsa.c.

Referenced by ensure_active_superblock().

#define DSA_SCLASS_BLOCK_OF_SPANS   0
#define DSA_SCLASS_LOCK (   area,
  sclass 
)    (&area->control->pools[sclass].lock)
#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().

#define DSA_SEGMENT_HEADER_MAGIC   0x0ce26608
#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().

#define DSA_SIZE_CLASS_MAP_QUANTUM   8

Definition at line 269 of file dsa.c.

Referenced by dsa_allocate_extended().

#define DSA_SPAN_NOTHING_FREE   ((uint16) -1)

Definition at line 377 of file dsa.c.

Referenced by alloc_object(), and init_span().

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

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

Definition at line 329 of file dsa.c.

Referenced by init_span().

#define get_segment_index (   area,
  segment_map_ptr 
)    (segment_map_ptr - &area->segment_maps[0])
#define NextFreeObjectIndex (   object)    (* (uint16 *) (object))

Definition at line 213 of file dsa.c.

Referenced by alloc_object(), and dsa_free().

Typedef Documentation

Definition at line 124 of file dsa.c.

Function Documentation

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

Definition at line 1861 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().

1864 {
1865  dsa_area_pool *pool = dsa_get_address(area, span->pool);
1866 
1867  if (DsaPointerIsValid(pool->spans[fclass]))
1868  {
1869  dsa_area_span *head = dsa_get_address(area,
1870  pool->spans[fclass]);
1871 
1872  head->prevspan = span_pointer;
1873  }
1874  span->prevspan = InvalidDsaPointer;
1875  span->nextspan = pool->spans[fclass];
1876  pool->spans[fclass] = span_pointer;
1877  span->fclass = fclass;
1878 }
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:924
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
static dsa_pointer alloc_object ( dsa_area area,
int  size_class 
)
inlinestatic

Definition at line 1431 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, result, dsa_area_pool::spans, dsa_area_span::start, and transfer_first_span().

Referenced by dsa_allocate_extended(), and ensure_active_superblock().

1432 {
1433  dsa_area_pool *pool = &area->control->pools[size_class];
1434  dsa_area_span *span;
1435  dsa_pointer block;
1437  char *object;
1438  Size size;
1439 
1440  /*
1441  * Even though ensure_active_superblock can in turn call alloc_object if
1442  * it needs to allocate a new span, that's always from a different pool,
1443  * and the order of lock acquisition is always the same, so it's OK that
1444  * we hold this lock for the duration of this function.
1445  */
1446  Assert(!LWLockHeldByMe(DSA_SCLASS_LOCK(area, size_class)));
1447  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
1448 
1449  /*
1450  * If there's no active superblock, we must successfully obtain one or
1451  * fail the request.
1452  */
1453  if (!DsaPointerIsValid(pool->spans[1]) &&
1454  !ensure_active_superblock(area, pool, size_class))
1455  {
1456  result = InvalidDsaPointer;
1457  }
1458  else
1459  {
1460  /*
1461  * There should be a block in fullness class 1 at this point, and it
1462  * should never be completely full. Thus we can either pop an object
1463  * from the free list or, failing that, initialize a new object.
1464  */
1465  Assert(DsaPointerIsValid(pool->spans[1]));
1466  span = (dsa_area_span *)
1467  dsa_get_address(area, pool->spans[1]);
1468  Assert(span->nallocatable > 0);
1469  block = span->start;
1470  Assert(size_class < DSA_NUM_SIZE_CLASSES);
1471  size = dsa_size_classes[size_class];
1472  if (span->firstfree != DSA_SPAN_NOTHING_FREE)
1473  {
1474  result = block + span->firstfree * size;
1475  object = dsa_get_address(area, result);
1476  span->firstfree = NextFreeObjectIndex(object);
1477  }
1478  else
1479  {
1480  result = block + span->ninitialized * size;
1481  ++span->ninitialized;
1482  }
1483  --span->nallocatable;
1484 
1485  /* If it's now full, move it to the highest-numbered fullness class. */
1486  if (span->nallocatable == 0)
1487  transfer_first_span(area, pool, 1, DSA_FULLNESS_CLASSES - 1);
1488  }
1489 
1490  Assert(LWLockHeldByMe(DSA_SCLASS_LOCK(area, size_class)));
1491  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
1492 
1493  return result;
1494 }
#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:1837
#define DSA_NUM_SIZE_CLASSES
Definition: dsa.c:247
return result
Definition: formatting.c:1633
#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:1721
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
static bool ensure_active_superblock(dsa_area *area, dsa_area_pool *pool, int size_class)
Definition: dsa.c:1519
static bool transfer_first_span(dsa_area *area, dsa_area_pool *pool, int fromclass, int toclass)
Definition: dsa.c:1391
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:664
dsa_area_pool pools[DSA_NUM_SIZE_CLASSES]
Definition: dsa.c:309
size_t Size
Definition: c.h:350
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
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
static dsa_area * attach_internal ( void *  place,
dsm_segment segment,
dsa_handle  handle 
)
static

Definition at line 1285 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().

1286 {
1287  dsa_area_control *control;
1288  dsa_area *area;
1289  dsa_segment_map *segment_map;
1290 
1291  control = (dsa_area_control *) place;
1292  Assert(control->handle == handle);
1293  Assert(control->segment_handles[0] == handle);
1294  Assert(control->segment_header.magic ==
1295  (DSA_SEGMENT_HEADER_MAGIC ^ handle ^ 0));
1296 
1297  /* Build the backend-local area object. */
1298  area = palloc(sizeof(dsa_area));
1299  area->control = control;
1300  area->mapping_pinned = false;
1301  memset(&area->segment_maps[0], 0,
1302  sizeof(dsa_segment_map) * DSA_MAX_SEGMENTS);
1303  area->high_segment_index = 0;
1304 
1305  /* Set up the segment map for this process's mapping. */
1306  segment_map = &area->segment_maps[0];
1307  segment_map->segment = segment; /* NULL for in-place */
1308  segment_map->mapped_address = place;
1309  segment_map->header = (dsa_segment_header *) segment_map->mapped_address;
1310  segment_map->fpm = (FreePageManager *)
1311  (segment_map->mapped_address + MAXALIGN(sizeof(dsa_area_control)));
1312  segment_map->pagemap = (dsa_pointer *)
1313  (segment_map->mapped_address + MAXALIGN(sizeof(dsa_area_control)) +
1314  MAXALIGN(sizeof(FreePageManager)));
1315 
1316  /* Bump the reference count. */
1318  if (control->refcnt == 0)
1319  {
1320  /* We can't attach to a DSA area that has already been destroyed. */
1321  ereport(ERROR,
1322  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1323  errmsg("could not attach to dynamic shared area")));
1324  }
1325  ++control->refcnt;
1328 
1329  return area;
1330 }
bool mapping_pinned
Definition: dsa.c:360
Size freed_segment_counter
Definition: dsa.c:321
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsm_segment * segment
Definition: dsa.c:341
int errcode(int sqlerrcode)
Definition: elog.c:575
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:1721
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:122
Size freed_segment_counter
Definition: dsa.c:374
dsa_segment_header segment_header
Definition: dsa.c:301
#define Assert(condition)
Definition: c.h:664
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
#define MAXALIGN(LEN)
Definition: c.h:576
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:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#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
static void check_for_freed_segments ( dsa_area area)
static

Definition at line 2202 of file dsa.c.

References 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, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsa_segment_map::mapped_address, pg_read_barrier, dsa_segment_map::segment, dsa_area::segment_maps, and unlikely.

Referenced by dsa_free(), and dsa_get_address().

2203 {
2204  Size freed_segment_counter;
2205 
2206  /*
2207  * Any other process that has freed a segment has incremented
2208  * free_segment_counter while holding an LWLock, and that must precede any
2209  * backend creating a new segment in the same slot while holding an
2210  * LWLock, and that must precede the creation of any dsa_pointer pointing
2211  * into the new segment which might reach us here, and the caller must
2212  * have sent the dsa_pointer to this process using appropriate memory
2213  * synchronization (some kind of locking or atomic primitive or system
2214  * call). So all we need to do on the reading side is ask for the load of
2215  * freed_segment_counter to follow the caller's load of the dsa_pointer it
2216  * has, and we can be sure to detect any segments that had been freed as
2217  * of the time that the dsa_pointer reached this process.
2218  */
2219  pg_read_barrier();
2220  freed_segment_counter = area->control->freed_segment_counter;
2221  if (unlikely(area->freed_segment_counter != freed_segment_counter))
2222  {
2223  int i;
2224 
2225  /* Check all currently mapped segments to find what's been freed. */
2227  for (i = 0; i <= area->high_segment_index; ++i)
2228  {
2229  if (area->segment_maps[i].header != NULL &&
2230  area->segment_maps[i].header->freed)
2231  {
2232  dsm_detach(area->segment_maps[i].segment);
2233  area->segment_maps[i].segment = NULL;
2234  area->segment_maps[i].header = NULL;
2235  area->segment_maps[i].mapped_address = NULL;
2236  }
2237  }
2239  area->freed_segment_counter = freed_segment_counter;
2240  }
2241 }
Size freed_segment_counter
Definition: dsa.c:321
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
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
dsa_area_control * control
Definition: dsa.c:357
Size freed_segment_counter
Definition: dsa.c:374
#define pg_read_barrier()
Definition: atomics.h:161
size_t Size
Definition: c.h:350
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
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:726
int i
#define unlikely(x)
Definition: c.h:951
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
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 1182 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().

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

Definition at line 1775 of file dsa.c.

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

1776 {
1777  dsa_area_span *span = dsa_get_address(area, span_pointer);
1778  int size_class = span->size_class;
1779  dsa_segment_map *segment_map;
1780 
1781  segment_map =
1783 
1784  /* Remove it from its fullness class list. */
1785  unlink_span(area, span);
1786 
1787  /*
1788  * Note: Here we acquire the area lock while we already hold a per-pool
1789  * lock. We never hold the area lock and then take a pool lock, or we
1790  * could deadlock.
1791  */
1793  FreePageManagerPut(segment_map->fpm,
1795  span->npages);
1796  /* Check if the segment is now entirely free. */
1797  if (fpm_largest(segment_map->fpm) == segment_map->header->usable_pages)
1798  {
1799  dsa_segment_index index = get_segment_index(area, segment_map);
1800 
1801  /* If it's not the segment with extra control data, free it. */
1802  if (index != 0)
1803  {
1804  /*
1805  * Give it back to the OS, and allow other backends to detect that
1806  * they need to detach.
1807  */
1808  unlink_segment(area, segment_map);
1809  segment_map->header->freed = true;
1811  segment_map->header->size);
1812  area->control->total_segment_size -=
1813  segment_map->header->size;
1815  dsm_detach(segment_map->segment);
1816  area->control->segment_handles[index] = DSM_HANDLE_INVALID;
1817  ++area->control->freed_segment_counter;
1818  segment_map->segment = NULL;
1819  segment_map->header = NULL;
1820  segment_map->mapped_address = NULL;
1821  }
1822  }
1824 
1825  /*
1826  * Span-of-spans blocks store the span which describes them within the
1827  * block itself, so freeing the storage implicitly frees the descriptor
1828  * also. If this is a block of any other type, we need to separately free
1829  * the span object also. This recursive call to dsa_free will acquire the
1830  * span pool's lock. We can't deadlock because the acquisition order is
1831  * always some other pool and then the span pool.
1832  */
1833  if (size_class != DSA_SCLASS_BLOCK_OF_SPANS)
1834  dsa_free(area, span_pointer);
1835 }
Size freed_segment_counter
Definition: dsa.c:321
dsm_segment * segment
Definition: dsa.c:341
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1028
dsa_pointer start
Definition: dsa.c:199
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
Size total_segment_size
Definition: dsa.c:311
Definition: type.h:89
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
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:899
static void unlink_span(dsa_area *area, dsa_area_span *span)
Definition: dsa.c:1838
Size dsa_segment_index
Definition: dsa.c:124
Size npages
Definition: dsa.c:200
static void unlink_segment(dsa_area *area, dsa_segment_map *segment_map)
Definition: dsa.c:1910
#define FPM_PAGE_SIZE
Definition: freepage.h:30
#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:664
#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:1117
char * mapped_address
Definition: dsa.c:342
Size usable_pages
Definition: dsa.c:156
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:812
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:726
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1714
dsa_pointer dsa_allocate_extended ( dsa_area area,
Size  size,
int  flags 
)

Definition at line 664 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_QUANTUM, dsa_size_classes, DsaPointerIsValid, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, FATAL, dsa_segment_map::fpm, FPM_PAGE_SIZE, fpm_size_to_pages, FreePageManagerGet(), get_best_segment(), get_segment_index, init_span(), InvalidDsaPointer, lengthof, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), make_new_segment(), dsa_segment_map::pagemap, dsa_area_control::pools, and result.

Referenced by dshash_create(), and pagetable_allocate().

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

Definition at line 505 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.

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

Definition at line 540 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().

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

Definition at line 419 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.

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

Definition at line 468 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().

470 {
471  dsa_area *area;
472 
473  area = create_internal(place, size, tranche_id,
474  DSM_HANDLE_INVALID, NULL);
475 
476  /*
477  * Clean up when the control segment detaches, if a containing DSM segment
478  * was provided.
479  */
480  if (segment != NULL)
482  PointerGetDatum(place));
483 
484  return area;
485 }
#define PointerGetDatum(X)
Definition: postgres.h:562
void on_dsm_detach(dsm_segment *seg, on_dsm_detach_callback function, Datum arg)
Definition: dsm.c:1037
#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:1182
void dsa_on_dsm_detach_release_in_place(dsm_segment *segment, Datum place)
Definition: dsa.c:571
Definition: dsa.c:354
void dsa_detach ( dsa_area area)

Definition at line 1884 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().

1885 {
1886  int i;
1887 
1888  /* Detach from all segments. */
1889  for (i = 0; i <= area->high_segment_index; ++i)
1890  if (area->segment_maps[i].segment != NULL)
1891  dsm_detach(area->segment_maps[i].segment);
1892 
1893  /*
1894  * Note that 'detaching' (= detaching from DSM segments) doesn't include
1895  * 'releasing' (= adjusting the reference count). It would be nice to
1896  * combine these operations, but client code might never get around to
1897  * calling dsa_detach because of an error path, and a detach hook on any
1898  * particular segment is too late to detach other segments in the area
1899  * without risking a 'leak' warning in the non-error path.
1900  */
1901 
1902  /* Free the backend-local area object. */
1903  pfree(area);
1904 }
dsa_segment_index high_segment_index
Definition: dsa.c:371
dsm_segment * segment
Definition: dsa.c:341
void pfree(void *pointer)
Definition: mcxt.c:949
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
void dsm_detach(dsm_segment *seg)
Definition: dsm.c:726
int i
void dsa_dump ( dsa_area area)

Definition at line 1057 of file dsa.c.

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

1058 {
1059  Size i,
1060  j;
1061 
1062  /*
1063  * Note: This gives an inconsistent snapshot as it acquires and releases
1064  * individual locks as it goes...
1065  */
1066 
1068  fprintf(stderr, "dsa_area handle %x:\n", area->control->handle);
1069  fprintf(stderr, " max_total_segment_size: %zu\n",
1071  fprintf(stderr, " total_segment_size: %zu\n",
1072  area->control->total_segment_size);
1073  fprintf(stderr, " refcnt: %d\n", area->control->refcnt);
1074  fprintf(stderr, " pinned: %c\n", area->control->pinned ? 't' : 'f');
1075  fprintf(stderr, " segment bins:\n");
1076  for (i = 0; i < DSA_NUM_SEGMENT_BINS; ++i)
1077  {
1078  if (area->control->segment_bins[i] != DSA_SEGMENT_INDEX_NONE)
1079  {
1080  dsa_segment_index segment_index;
1081 
1082  fprintf(stderr,
1083  " segment bin %zu (at least %d contiguous pages free):\n",
1084  i, 1 << (i - 1));
1085  segment_index = area->control->segment_bins[i];
1086  while (segment_index != DSA_SEGMENT_INDEX_NONE)
1087  {
1088  dsa_segment_map *segment_map;
1089 
1090  segment_map =
1091  get_segment_by_index(area, segment_index);
1092 
1093  fprintf(stderr,
1094  " segment index %zu, usable_pages = %zu, "
1095  "contiguous_pages = %zu, mapped at %p\n",
1096  segment_index,
1097  segment_map->header->usable_pages,
1098  fpm_largest(segment_map->fpm),
1099  segment_map->mapped_address);
1100  segment_index = segment_map->header->next;
1101  }
1102  }
1103  }
1105 
1106  fprintf(stderr, " pools:\n");
1107  for (i = 0; i < DSA_NUM_SIZE_CLASSES; ++i)
1108  {
1109  bool found = false;
1110 
1112  for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1113  if (DsaPointerIsValid(area->control->pools[i].spans[j]))
1114  found = true;
1115  if (found)
1116  {
1117  if (i == DSA_SCLASS_BLOCK_OF_SPANS)
1118  fprintf(stderr, " pool for blocks of span objects:\n");
1119  else if (i == DSA_SCLASS_SPAN_LARGE)
1120  fprintf(stderr, " pool for large object spans:\n");
1121  else
1122  fprintf(stderr,
1123  " pool for size class %zu (object size %hu bytes):\n",
1124  i, dsa_size_classes[i]);
1125  for (j = 0; j < DSA_FULLNESS_CLASSES; ++j)
1126  {
1127  if (!DsaPointerIsValid(area->control->pools[i].spans[j]))
1128  fprintf(stderr, " fullness class %zu is empty\n", j);
1129  else
1130  {
1131  dsa_pointer span_pointer = area->control->pools[i].spans[j];
1132 
1133  fprintf(stderr, " fullness class %zu:\n", j);
1134  while (DsaPointerIsValid(span_pointer))
1135  {
1136  dsa_area_span *span;
1137 
1138  span = dsa_get_address(area, span_pointer);
1139  fprintf(stderr,
1140  " span descriptor at "
1141  DSA_POINTER_FORMAT ", superblock at "
1143  ", pages = %zu, objects free = %hu/%hu\n",
1144  span_pointer, span->start, span->npages,
1145  span->nallocatable, span->nmax);
1146  span_pointer = span->nextspan;
1147  }
1148  }
1149  }
1150  }
1151  LWLockRelease(DSA_SCLASS_LOCK(area, i));
1152  }
1153 }
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 max_total_segment_size
Definition: dsa.c:313
#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
uint64 dsa_pointer
Definition: dsa.h:62
FreePageManager * fpm
Definition: dsa.c:344
dsa_segment_header * header
Definition: dsa.c:343
Size total_segment_size
Definition: dsa.c:311
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
#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 dsa_segment_index
Definition: dsa.c:124
Size npages
Definition: dsa.c:200
#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
size_t Size
Definition: c.h:350
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
char * mapped_address
Definition: dsa.c:342
#define DsaPointerIsValid(x)
Definition: dsa.h:81
Size usable_pages
Definition: dsa.c:156
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 dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1714
void dsa_free ( dsa_area area,
dsa_pointer  dp 
)

Definition at line 812 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(), ensure_active_superblock(), find_or_make_matching_shared_tupledesc(), pagetable_free(), resize(), and tbm_free_shared_area().

813 {
814  dsa_segment_map *segment_map;
815  int pageno;
816  dsa_pointer span_pointer;
817  dsa_area_span *span;
818  char *superblock;
819  char *object;
820  Size size;
821  int size_class;
822 
823  /* Make sure we don't have a stale segment in the slot 'dp' refers to. */
825 
826  /* Locate the object, span and pool. */
827  segment_map = get_segment_by_index(area, DSA_EXTRACT_SEGMENT_NUMBER(dp));
828  pageno = DSA_EXTRACT_OFFSET(dp) / FPM_PAGE_SIZE;
829  span_pointer = segment_map->pagemap[pageno];
830  span = dsa_get_address(area, span_pointer);
831  superblock = dsa_get_address(area, span->start);
832  object = dsa_get_address(area, dp);
833  size_class = span->size_class;
834  size = dsa_size_classes[size_class];
835 
836  /*
837  * Special case for large objects that live in a special span: we return
838  * those pages directly to the free page manager and free the span.
839  */
840  if (span->size_class == DSA_SCLASS_SPAN_LARGE)
841  {
842 
843 #ifdef CLOBBER_FREED_MEMORY
844  memset(object, 0x7f, span->npages * FPM_PAGE_SIZE);
845 #endif
846 
847  /* Give pages back to free page manager. */
849  FreePageManagerPut(segment_map->fpm,
851  span->npages);
853  /* Unlink span. */
855  LW_EXCLUSIVE);
856  unlink_span(area, span);
858  /* Free the span object so it can be reused. */
859  dsa_free(area, span_pointer);
860  return;
861  }
862 
863 #ifdef CLOBBER_FREED_MEMORY
864  memset(object, 0x7f, size);
865 #endif
866 
867  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
868 
869  /* Put the object on the span's freelist. */
870  Assert(object >= superblock);
871  Assert(object < superblock + DSA_SUPERBLOCK_SIZE);
872  Assert((object - superblock) % size == 0);
873  NextFreeObjectIndex(object) = span->firstfree;
874  span->firstfree = (object - superblock) / size;
875  ++span->nallocatable;
876 
877  /*
878  * See if the span needs to moved to a different fullness class, or be
879  * freed so its pages can be given back to the segment.
880  */
881  if (span->nallocatable == 1 && span->fclass == DSA_FULLNESS_CLASSES - 1)
882  {
883  /*
884  * The block was completely full and is located in the
885  * highest-numbered fullness class, which is never scanned for free
886  * chunks. We must move it to the next-lower fullness class.
887  */
888  unlink_span(area, span);
889  add_span_to_fullness_class(area, span, span_pointer,
891 
892  /*
893  * If this is the only span, and there is no active span, then we
894  * should probably move this span to fullness class 1. (Otherwise if
895  * you allocate exactly all the objects in the only span, it moves to
896  * class 3, then you free them all, it moves to 2, and then is given
897  * back, leaving no active span).
898  */
899  }
900  else if (span->nallocatable == span->nmax &&
901  (span->fclass != 1 || span->prevspan != InvalidDsaPointer))
902  {
903  /*
904  * This entire block is free, and it's not the active block for this
905  * size class. Return the memory to the free page manager. We don't
906  * do this for the active block to prevent hysteresis: if we
907  * repeatedly allocate and free the only chunk in the active block, it
908  * will be very inefficient if we deallocate and reallocate the block
909  * every time.
910  */
911  destroy_superblock(area, span_pointer);
912  }
913 
914  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
915 }
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
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:1721
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
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:1838
Size npages
Definition: dsa.c:200
#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:1861
#define DSA_EXTRACT_SEGMENT_NUMBER(dp)
Definition: dsa.c:118
#define Assert(condition)
Definition: c.h:664
#define DSA_EXTRACT_OFFSET(dp)
Definition: dsa.c:121
size_t Size
Definition: c.h:350
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
dsa_pointer * pagemap
Definition: dsa.c:345
static void destroy_superblock(dsa_area *area, dsa_pointer span_pointer)
Definition: dsa.c:1775
void dsa_free(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:812
uint16 nallocatable
Definition: dsa.c:203
static void check_for_freed_segments(dsa_area *area)
Definition: dsa.c:2202
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:1714
void* dsa_get_address ( dsa_area area,
dsa_pointer  dp 
)

Definition at line 924 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(), find_in_bucket(), find_or_make_matching_shared_tupledesc(), init_span(), insert_into_bucket(), insert_item_into_bucket(), lookup_rowtype_tupdesc_internal(), pagetable_allocate(), resize(), 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().

925 {
927  Size offset;
928 
929  /* Convert InvalidDsaPointer to NULL. */
930  if (!DsaPointerIsValid(dp))
931  return NULL;
932 
933  /* Process any requests to detach from freed segments. */
935 
936  /* Break the dsa_pointer into its components. */
937  index = DSA_EXTRACT_SEGMENT_NUMBER(dp);
938  offset = DSA_EXTRACT_OFFSET(dp);
939  Assert(index < DSA_MAX_SEGMENTS);
940 
941  /* Check if we need to cause this segment to be mapped in. */
942  if (unlikely(area->segment_maps[index].mapped_address == NULL))
943  {
944  /* Call for effect (we don't need the result). */
945  get_segment_by_index(area, index);
946  }
947 
948  return area->segment_maps[index].mapped_address + offset;
949 }
Definition: type.h:89
Size dsa_segment_index
Definition: dsa.c:124
#define DSA_EXTRACT_SEGMENT_NUMBER(dp)
Definition: dsa.c:118
#define Assert(condition)
Definition: c.h:664
#define DSA_EXTRACT_OFFSET(dp)
Definition: dsa.c:121
size_t Size
Definition: c.h:350
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:2202
#define unlikely(x)
Definition: c.h:951
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1714
dsa_handle dsa_get_handle ( dsa_area area)

Definition at line 493 of file dsa.c.

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

494 {
496  return area->control->handle;
497 }
#define DSM_HANDLE_INVALID
Definition: dsm.h:23
dsa_area_control * control
Definition: dsa.c:357
#define Assert(condition)
Definition: c.h:664
dsa_handle handle
Definition: dsa.c:303
Size dsa_minimum_size ( void  )

Definition at line 1160 of file dsa.c.

References FPM_PAGE_SIZE, and MAXALIGN.

Referenced by create_internal(), and ExecInitParallelPlan().

1161 {
1162  Size size;
1163  int pages = 0;
1164 
1165  size = MAXALIGN(sizeof(dsa_area_control)) +
1166  MAXALIGN(sizeof(FreePageManager));
1167 
1168  /* Figure out how many pages we need, including the page map... */
1169  while (((size + FPM_PAGE_SIZE - 1) / FPM_PAGE_SIZE) > pages)
1170  {
1171  ++pages;
1172  size += sizeof(dsa_pointer);
1173  }
1174 
1175  return pages * FPM_PAGE_SIZE;
1176 }
uint64 dsa_pointer
Definition: dsa.h:62
#define FPM_PAGE_SIZE
Definition: freepage.h:30
size_t Size
Definition: c.h:350
#define MAXALIGN(LEN)
Definition: c.h:576
void dsa_on_dsm_detach_release_in_place ( dsm_segment segment,
Datum  place 
)

Definition at line 571 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().

572 {
574 }
void dsa_release_in_place(void *place)
Definition: dsa.c:600
#define DatumGetPointer(X)
Definition: postgres.h:555
void dsa_on_shmem_exit_release_in_place ( int  code,
Datum  place 
)

Definition at line 585 of file dsa.c.

References DatumGetPointer, and dsa_release_in_place().

586 {
588 }
void dsa_release_in_place(void *place)
Definition: dsa.c:600
#define DatumGetPointer(X)
Definition: postgres.h:555
void dsa_pin ( dsa_area area)

Definition at line 957 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.

958 {
960  if (area->control->pinned)
961  {
963  elog(ERROR, "dsa_area already pinned");
964  }
965  area->control->pinned = true;
966  ++area->control->refcnt;
968 }
bool pinned
Definition: dsa.c:319
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
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:1117
#define elog
Definition: elog.h:219
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
void dsa_pin_mapping ( dsa_area area)

Definition at line 630 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().

631 {
632  int i;
633 
634  Assert(!area->mapping_pinned);
635  area->mapping_pinned = true;
636 
637  for (i = 0; i <= area->high_segment_index; ++i)
638  if (area->segment_maps[i].segment != NULL)
640 }
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:827
#define Assert(condition)
Definition: c.h:664
dsa_segment_map segment_maps[DSA_MAX_SEGMENTS]
Definition: dsa.c:368
int i
void dsa_release_in_place ( void *  place)

Definition at line 600 of file dsa.c.

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

Referenced by dsa_on_dsm_detach_release_in_place(), and dsa_on_shmem_exit_release_in_place().

601 {
602  dsa_area_control *control = (dsa_area_control *) place;
603  int i;
604 
605  LWLockAcquire(&control->lock, LW_EXCLUSIVE);
606  Assert(control->segment_header.magic ==
607  (DSA_SEGMENT_HEADER_MAGIC ^ control->handle ^ 0));
608  Assert(control->refcnt > 0);
609  if (--control->refcnt == 0)
610  {
611  for (i = 0; i <= control->high_segment_index; ++i)
612  {
613  dsm_handle handle;
614 
615  handle = control->segment_handles[i];
616  if (handle != DSM_HANDLE_INVALID)
617  dsm_unpin_segment(handle);
618  }
619  }
620  LWLockRelease(&control->lock);
621 }
uint32 dsm_handle
Definition: dsm_impl.h:55
dsa_segment_index high_segment_index
Definition: dsa.c:315
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
#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:899
dsa_segment_header segment_header
Definition: dsa.c:301
#define Assert(condition)
Definition: c.h:664
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
int i
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
uint32 magic
Definition: dsa.c:154
dsa_handle handle
Definition: dsa.c:303
void dsa_set_size_limit ( dsa_area area,
Size  limit 
)

Definition at line 1000 of file dsa.c.

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

1001 {
1003  area->control->max_total_segment_size = limit;
1005 }
Size max_total_segment_size
Definition: dsa.c:313
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
dsa_area_control * control
Definition: dsa.c:357
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
void dsa_trim ( dsa_area area)

Definition at line 1012 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.

1013 {
1014  int size_class;
1015 
1016  /*
1017  * Trim in reverse pool order so we get to the spans-of-spans last, just
1018  * in case any become entirely free while processing all the other pools.
1019  */
1020  for (size_class = DSA_NUM_SIZE_CLASSES - 1; size_class >= 0; --size_class)
1021  {
1022  dsa_area_pool *pool = &area->control->pools[size_class];
1023  dsa_pointer span_pointer;
1024 
1025  if (size_class == DSA_SCLASS_SPAN_LARGE)
1026  {
1027  /* Large object frees give back segments aggressively already. */
1028  continue;
1029  }
1030 
1031  /*
1032  * Search fullness class 1 only. That is where we expect to find an
1033  * entirely empty superblock (entirely empty superblocks in other
1034  * fullness classes are returned to the free page map by dsa_free).
1035  */
1036  LWLockAcquire(DSA_SCLASS_LOCK(area, size_class), LW_EXCLUSIVE);
1037  span_pointer = pool->spans[1];
1038  while (DsaPointerIsValid(span_pointer))
1039  {
1040  dsa_area_span *span = dsa_get_address(area, span_pointer);
1041  dsa_pointer next = span->nextspan;
1042 
1043  if (span->nallocatable == span->nmax)
1044  destroy_superblock(area, span_pointer);
1045 
1046  span_pointer = next;
1047  }
1048  LWLockRelease(DSA_SCLASS_LOCK(area, size_class));
1049  }
1050 }
uint16 nmax
Definition: dsa.c:205
dsa_pointer nextspan
Definition: dsa.c:198
static int32 next
Definition: blutils.c:210
#define DSA_NUM_SIZE_CLASSES
Definition: dsa.c:247
uint64 dsa_pointer
Definition: dsa.h:62
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
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:1117
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:1775
uint16 nallocatable
Definition: dsa.c:203
#define DSA_SCLASS_SPAN_LARGE
Definition: dsa.c:251
void dsa_unpin ( dsa_area area)

Definition at line 976 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.

977 {
979  Assert(area->control->refcnt > 1);
980  if (!area->control->pinned)
981  {
983  elog(ERROR, "dsa_area not pinned");
984  }
985  area->control->pinned = false;
986  --area->control->refcnt;
988 }
bool pinned
Definition: dsa.c:319
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
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:664
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
#define elog
Definition: elog.h:219
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
static bool ensure_active_superblock ( dsa_area area,
dsa_area_pool pool,
int  size_class 
)
static

Definition at line 1519 of file dsa.c.

References alloc_object(), Assert, DSA_AREA_LOCK, dsa_free(), 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, 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().

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

Definition at line 1942 of file dsa.c.

References Assert, dsa_segment_header::bin, 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().

1943 {
1944  Size bin;
1945 
1947 
1948  /*
1949  * Start searching from the first bin that *might* have enough contiguous
1950  * pages.
1951  */
1952  for (bin = contiguous_pages_to_segment_bin(npages);
1953  bin < DSA_NUM_SEGMENT_BINS;
1954  ++bin)
1955  {
1956  /*
1957  * The minimum contiguous size that any segment in this bin should
1958  * have. We'll re-bin if we see segments with fewer.
1959  */
1960  Size threshold = (Size) 1 << (bin - 1);
1961  dsa_segment_index segment_index;
1962 
1963  /* Search this bin for a segment with enough contiguous space. */
1964  segment_index = area->control->segment_bins[bin];
1965  while (segment_index != DSA_SEGMENT_INDEX_NONE)
1966  {
1967  dsa_segment_map *segment_map;
1968  dsa_segment_index next_segment_index;
1969  Size contiguous_pages;
1970 
1971  segment_map = get_segment_by_index(area, segment_index);
1972  next_segment_index = segment_map->header->next;
1973  contiguous_pages = fpm_largest(segment_map->fpm);
1974 
1975  /* Not enough for the request, still enough for this bin. */
1976  if (contiguous_pages >= threshold && contiguous_pages < npages)
1977  {
1978  segment_index = next_segment_index;
1979  continue;
1980  }
1981 
1982  /* Re-bin it if it's no longer in the appropriate bin. */
1983  if (contiguous_pages < threshold)
1984  {
1985  Size new_bin;
1986 
1987  new_bin = contiguous_pages_to_segment_bin(contiguous_pages);
1988 
1989  /* Remove it from its current bin. */
1990  unlink_segment(area, segment_map);
1991 
1992  /* Push it onto the front of its new bin. */
1993  segment_map->header->prev = DSA_SEGMENT_INDEX_NONE;
1994  segment_map->header->next =
1995  area->control->segment_bins[new_bin];
1996  segment_map->header->bin = new_bin;
1997  area->control->segment_bins[new_bin] = segment_index;
1998  if (segment_map->header->next != DSA_SEGMENT_INDEX_NONE)
1999  {
2001 
2002  next = get_segment_by_index(area,
2003  segment_map->header->next);
2004  Assert(next->header->bin == new_bin);
2005  next->header->prev = segment_index;
2006  }
2007 
2008  /*
2009  * But fall through to see if it's enough to satisfy this
2010  * request anyway....
2011  */
2012  }
2013 
2014  /* Check if we are done. */
2015  if (contiguous_pages >= npages)
2016  return segment_map;
2017 
2018  /* Continue searching the same bin. */
2019  segment_index = next_segment_index;
2020  }
2021  }
2022 
2023  /* Not found. */
2024  return NULL;
2025 }
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:307
static int32 next
Definition: blutils.c:210
#define contiguous_pages_to_segment_bin(n)
Definition: dsa.c:140
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1837
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
Size dsa_segment_index
Definition: dsa.c:124
static void unlink_segment(dsa_area *area, dsa_segment_map *segment_map)
Definition: dsa.c:1910
#define Assert(condition)
Definition: c.h:664
#define fpm_largest(fpm)
Definition: freepage.h:88
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
size_t Size
Definition: c.h:350
#define DSA_AREA_LOCK(area)
Definition: dsa.c:143
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1714
static dsa_segment_map * get_segment_by_index ( dsa_area area,
dsa_segment_index  index 
)
static

Definition at line 1714 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_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().

1715 {
1716  if (unlikely(area->segment_maps[index].mapped_address == NULL))
1717  {
1718  dsm_handle handle;
1719  dsm_segment *segment;
1720  dsa_segment_map *segment_map;
1721 
1722  /*
1723  * If we are reached by dsa_free or dsa_get_address, there must be at
1724  * least one object allocated in the referenced segment. Otherwise,
1725  * their caller has a double-free or access-after-free bug, which we
1726  * have no hope of detecting. So we know it's safe to access this
1727  * array slot without holding a lock; it won't change underneath us.
1728  * Furthermore, we know that we can see the latest contents of the
1729  * slot, as explained in check_for_freed_segments, which those
1730  * functions call before arriving here.
1731  */
1732  handle = area->control->segment_handles[index];
1733 
1734  /* It's an error to try to access an unused slot. */
1735  if (handle == DSM_HANDLE_INVALID)
1736  elog(ERROR,
1737  "dsa_area could not attach to a segment that has been freed");
1738 
1739  segment = dsm_attach(handle);
1740  if (segment == NULL)
1741  elog(ERROR, "dsa_area could not attach to segment");
1742  if (area->mapping_pinned)
1743  dsm_pin_mapping(segment);
1744  segment_map = &area->segment_maps[index];
1745  segment_map->segment = segment;
1746  segment_map->mapped_address = dsm_segment_address(segment);
1747  segment_map->header =
1748  (dsa_segment_header *) segment_map->mapped_address;
1749  segment_map->fpm = (FreePageManager *)
1750  (segment_map->mapped_address +
1751  MAXALIGN(sizeof(dsa_segment_header)));
1752  segment_map->pagemap = (dsa_pointer *)
1753  (segment_map->mapped_address +
1754  MAXALIGN(sizeof(dsa_segment_header)) +
1755  MAXALIGN(sizeof(FreePageManager)));
1756 
1757  /* Remember the highest index this backend has ever mapped. */
1758  if (area->high_segment_index < index)
1759  area->high_segment_index = index;
1760 
1761  Assert(segment_map->header->magic ==
1763  }
1764 
1765  return &area->segment_maps[index];
1766 }
bool mapping_pinned
Definition: dsa.c:360
uint32 dsm_handle
Definition: dsm_impl.h:55
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:561
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:827
dsa_area_control * control
Definition: dsa.c:357
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1000
#define Assert(condition)
Definition: c.h:664
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
#define MAXALIGN(LEN)
Definition: c.h:576
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 unlikely(x)
Definition: c.h:951
#define elog
Definition: elog.h:219
#define DSA_SEGMENT_HEADER_MAGIC
Definition: dsa.c:111
uint32 magic
Definition: dsa.c:154
dsa_handle handle
Definition: dsa.c:303
static void init_span ( dsa_area area,
dsa_pointer  span_pointer,
dsa_area_pool pool,
dsa_pointer  start,
Size  npages,
uint16  size_class 
)
static

Definition at line 1336 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().

1340 {
1341  dsa_area_span *span = dsa_get_address(area, span_pointer);
1342  Size obsize = dsa_size_classes[size_class];
1343 
1344  /*
1345  * The per-pool lock must be held because we manipulate the span list for
1346  * this pool.
1347  */
1348  Assert(LWLockHeldByMe(DSA_SCLASS_LOCK(area, size_class)));
1349 
1350  /* Push this span onto the front of the span list for fullness class 1. */
1351  if (DsaPointerIsValid(pool->spans[1]))
1352  {
1353  dsa_area_span *head = (dsa_area_span *)
1354  dsa_get_address(area, pool->spans[1]);
1355 
1356  head->prevspan = span_pointer;
1357  }
1358  span->pool = DsaAreaPoolToDsaPointer(area, pool);
1359  span->nextspan = pool->spans[1];
1360  span->prevspan = InvalidDsaPointer;
1361  pool->spans[1] = span_pointer;
1362 
1363  span->start = start;
1364  span->npages = npages;
1365  span->size_class = size_class;
1366  span->ninitialized = 0;
1367  if (size_class == DSA_SCLASS_BLOCK_OF_SPANS)
1368  {
1369  /*
1370  * A block-of-spans contains its own descriptor, so mark one object as
1371  * initialized and reduce the count of allocatable objects by one.
1372  * Doing this here has the side effect of also reducing nmax by one,
1373  * which is important to make sure we free this object at the correct
1374  * time.
1375  */
1376  span->ninitialized = 1;
1377  span->nallocatable = FPM_PAGE_SIZE / obsize - 1;
1378  }
1379  else if (size_class != DSA_SCLASS_SPAN_LARGE)
1380  span->nallocatable = DSA_SUPERBLOCK_SIZE / obsize;
1382  span->nmax = span->nallocatable;
1383  span->fclass = 1;
1384 }
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:1837
dsa_pointer start
Definition: dsa.c:199
uint16 fclass
Definition: dsa.c:206
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
uint16 size_class
Definition: dsa.c:201
static const uint16 dsa_size_classes[]
Definition: dsa.c:236
Size npages
Definition: dsa.c:200
#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:664
size_t Size
Definition: c.h:350
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
static dsa_segment_map * make_new_segment ( dsa_area area,
Size  requested_pages 
)
static

Definition at line 2033 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, and dsa_segment_header::usable_pages.

Referenced by dsa_allocate_extended(), and ensure_active_superblock().

2034 {
2035  dsa_segment_index new_index;
2036  Size metadata_bytes;
2037  Size total_size;
2038  Size total_pages;
2039  Size usable_pages;
2040  dsa_segment_map *segment_map;
2041  dsm_segment *segment;
2042 
2044 
2045  /* Find a segment slot that is not in use (linearly for now). */
2046  for (new_index = 1; new_index < DSA_MAX_SEGMENTS; ++new_index)
2047  {
2048  if (area->control->segment_handles[new_index] == DSM_HANDLE_INVALID)
2049  break;
2050  }
2051  if (new_index == DSA_MAX_SEGMENTS)
2052  return NULL;
2053 
2054  /*
2055  * If the total size limit is already exceeded, then we exit early and
2056  * avoid arithmetic wraparound in the unsigned expressions below.
2057  */
2058  if (area->control->total_segment_size >=
2060  return NULL;
2061 
2062  /*
2063  * The size should be at least as big as requested, and at least big
2064  * enough to follow a geometric series that approximately doubles the
2065  * total storage each time we create a new segment. We use geometric
2066  * growth because the underlying DSM system isn't designed for large
2067  * numbers of segments (otherwise we might even consider just using one
2068  * DSM segment for each large allocation and for each superblock, and then
2069  * we wouldn't need to use FreePageManager).
2070  *
2071  * We decide on a total segment size first, so that we produce tidy
2072  * power-of-two sized segments. This is a good property to have if we
2073  * move to huge pages in the future. Then we work back to the number of
2074  * pages we can fit.
2075  */
2076  total_size = DSA_INITIAL_SEGMENT_SIZE *
2077  ((Size) 1 << (new_index / DSA_NUM_SEGMENTS_AT_EACH_SIZE));
2078  total_size = Min(total_size, DSA_MAX_SEGMENT_SIZE);
2079  total_size = Min(total_size,
2081  area->control->total_segment_size);
2082 
2083  total_pages = total_size / FPM_PAGE_SIZE;
2084  metadata_bytes =
2085  MAXALIGN(sizeof(dsa_segment_header)) +
2086  MAXALIGN(sizeof(FreePageManager)) +
2087  sizeof(dsa_pointer) * total_pages;
2088 
2089  /* Add padding up to next page boundary. */
2090  if (metadata_bytes % FPM_PAGE_SIZE != 0)
2091  metadata_bytes += FPM_PAGE_SIZE - (metadata_bytes % FPM_PAGE_SIZE);
2092  if (total_size <= metadata_bytes)
2093  return NULL;
2094  usable_pages = (total_size - metadata_bytes) / FPM_PAGE_SIZE;
2095  Assert(metadata_bytes + usable_pages * FPM_PAGE_SIZE <= total_size);
2096 
2097  /* See if that is enough... */
2098  if (requested_pages > usable_pages)
2099  {
2100  /*
2101  * We'll make an odd-sized segment, working forward from the requested
2102  * number of pages.
2103  */
2104  usable_pages = requested_pages;
2105  metadata_bytes =
2106  MAXALIGN(sizeof(dsa_segment_header)) +
2107  MAXALIGN(sizeof(FreePageManager)) +
2108  usable_pages * sizeof(dsa_pointer);
2109 
2110  /* Add padding up to next page boundary. */
2111  if (metadata_bytes % FPM_PAGE_SIZE != 0)
2112  metadata_bytes += FPM_PAGE_SIZE - (metadata_bytes % FPM_PAGE_SIZE);
2113  total_size = metadata_bytes + usable_pages * FPM_PAGE_SIZE;
2114 
2115  /* Is that too large for dsa_pointer's addressing scheme? */
2116  if (total_size > DSA_MAX_SEGMENT_SIZE)
2117  return NULL;
2118 
2119  /* Would that exceed the limit? */
2120  if (total_size > area->control->max_total_segment_size -
2121  area->control->total_segment_size)
2122  return NULL;
2123  }
2124 
2125  /* Create the segment. */
2126  segment = dsm_create(total_size, 0);
2127  if (segment == NULL)
2128  return NULL;
2129  dsm_pin_segment(segment);
2130  if (area->mapping_pinned)
2131  dsm_pin_mapping(segment);
2132 
2133  /* Store the handle in shared memory to be found by index. */
2134  area->control->segment_handles[new_index] =
2135  dsm_segment_handle(segment);
2136  /* Track the highest segment index in the history of the area. */
2137  if (area->control->high_segment_index < new_index)
2138  area->control->high_segment_index = new_index;
2139  /* Track the highest segment index this backend has ever mapped. */
2140  if (area->high_segment_index < new_index)
2141  area->high_segment_index = new_index;
2142  /* Track total size of all segments. */
2143  area->control->total_segment_size += total_size;
2146 
2147  /* Build a segment map for this segment in this backend. */
2148  segment_map = &area->segment_maps[new_index];
2149  segment_map->segment = segment;
2150  segment_map->mapped_address = dsm_segment_address(segment);
2151  segment_map->header = (dsa_segment_header *) segment_map->mapped_address;
2152  segment_map->fpm = (FreePageManager *)
2153  (segment_map->mapped_address +
2154  MAXALIGN(sizeof(dsa_segment_header)));
2155  segment_map->pagemap = (dsa_pointer *)
2156  (segment_map->mapped_address +
2157  MAXALIGN(sizeof(dsa_segment_header)) +
2158  MAXALIGN(sizeof(FreePageManager)));
2159 
2160  /* Set up the free page map. */
2161  FreePageManagerInitialize(segment_map->fpm, segment_map->mapped_address);
2162  FreePageManagerPut(segment_map->fpm, metadata_bytes / FPM_PAGE_SIZE,
2163  usable_pages);
2164 
2165  /* Set up the segment header and put it in the appropriate bin. */
2166  segment_map->header->magic =
2167  DSA_SEGMENT_HEADER_MAGIC ^ area->control->handle ^ new_index;
2168  segment_map->header->usable_pages = usable_pages;
2169  segment_map->header->size = total_size;
2170  segment_map->header->bin = contiguous_pages_to_segment_bin(usable_pages);
2171  segment_map->header->prev = DSA_SEGMENT_INDEX_NONE;
2172  segment_map->header->next =
2173  area->control->segment_bins[segment_map->header->bin];
2174  segment_map->header->freed = false;
2175  area->control->segment_bins[segment_map->header->bin] = new_index;
2176  if (segment_map->header->next != DSA_SEGMENT_INDEX_NONE)
2177  {
2179  get_segment_by_index(area, segment_map->header->next);
2180 
2181  Assert(next->header->bin == segment_map->header->bin);
2182  next->header->prev = new_index;
2183  }
2184 
2185  return segment_map;
2186 }
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:210
#define contiguous_pages_to_segment_bin(n)
Definition: dsa.c:140
Size max_total_segment_size
Definition: dsa.c:313
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1837
dsa_segment_index high_segment_index
Definition: dsa.c:371
#define Min(x, y)
Definition: c.h:795
dsa_segment_index high_segment_index
Definition: dsa.c:315
dsm_segment * segment
Definition: dsa.c:341
dsa_segment_index prev
Definition: dsa.c:164
dsm_handle dsm_segment_handle(dsm_segment *seg)
Definition: dsm.c:1028
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
Size total_segment_size
Definition: dsa.c:311
void dsm_pin_segment(dsm_segment *seg)
Definition: dsm.c:867
#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:827
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 dsa_segment_index
Definition: dsa.c:124
void FreePageManagerInitialize(FreePageManager *fpm, char *base)
Definition: freepage.c:183
dsm_segment * dsm_create(Size size, int flags)
Definition: dsm.c:465
#define FPM_PAGE_SIZE
Definition: freepage.h:30
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1000
#define Assert(condition)
Definition: c.h:664
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
size_t Size
Definition: c.h:350
dsm_handle segment_handles[DSA_MAX_SEGMENTS]
Definition: dsa.c:305
#define MAXALIGN(LEN)
Definition: c.h:576
dsa_pointer * pagemap
Definition: dsa.c:345
char * mapped_address
Definition: dsa.c:342
#define DSA_MAX_SEGMENTS
Definition: dsa.c:94
Size usable_pages
Definition: dsa.c:156
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
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:1714
static bool transfer_first_span ( dsa_area area,
dsa_area_pool pool,
int  fromclass,
int  toclass 
)
static

Definition at line 1391 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().

1393 {
1394  dsa_pointer span_pointer;
1395  dsa_area_span *span;
1396  dsa_area_span *nextspan;
1397 
1398  /* Can't do it if source list is empty. */
1399  span_pointer = pool->spans[fromclass];
1400  if (!DsaPointerIsValid(span_pointer))
1401  return false;
1402 
1403  /* Remove span from head of source list. */
1404  span = dsa_get_address(area, span_pointer);
1405  pool->spans[fromclass] = span->nextspan;
1406  if (DsaPointerIsValid(span->nextspan))
1407  {
1408  nextspan = (dsa_area_span *)
1409  dsa_get_address(area, span->nextspan);
1410  nextspan->prevspan = InvalidDsaPointer;
1411  }
1412 
1413  /* Add span to head of target list. */
1414  span->nextspan = pool->spans[toclass];
1415  pool->spans[toclass] = span_pointer;
1416  if (DsaPointerIsValid(span->nextspan))
1417  {
1418  nextspan = (dsa_area_span *)
1419  dsa_get_address(area, span->nextspan);
1420  nextspan->prevspan = span_pointer;
1421  }
1422  span->fclass = toclass;
1423 
1424  return true;
1425 }
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:924
dsa_pointer prevspan
Definition: dsa.c:197
dsa_pointer spans[DSA_FULLNESS_CLASSES]
Definition: dsa.c:290
#define DsaPointerIsValid(x)
Definition: dsa.h:81
static void unlink_segment ( dsa_area area,
dsa_segment_map segment_map 
)
static

Definition at line 1910 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().

1911 {
1912  if (segment_map->header->prev != DSA_SEGMENT_INDEX_NONE)
1913  {
1914  dsa_segment_map *prev;
1915 
1916  prev = get_segment_by_index(area, segment_map->header->prev);
1917  prev->header->next = segment_map->header->next;
1918  }
1919  else
1920  {
1921  Assert(area->control->segment_bins[segment_map->header->bin] ==
1922  get_segment_index(area, segment_map));
1923  area->control->segment_bins[segment_map->header->bin] =
1924  segment_map->header->next;
1925  }
1926  if (segment_map->header->next != DSA_SEGMENT_INDEX_NONE)
1927  {
1929 
1930  next = get_segment_by_index(area, segment_map->header->next);
1931  next->header->prev = segment_map->header->prev;
1932  }
1933 }
dsa_segment_index segment_bins[DSA_NUM_SEGMENT_BINS]
Definition: dsa.c:307
static int32 next
Definition: blutils.c:210
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:664
#define DSA_SEGMENT_INDEX_NONE
Definition: dsa.c:127
static dsa_segment_map * get_segment_by_index(dsa_area *area, dsa_segment_index index)
Definition: dsa.c:1714
static void unlink_span ( dsa_area area,
dsa_area_span span 
)
static

Definition at line 1838 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().

1839 {
1840  if (DsaPointerIsValid(span->nextspan))
1841  {
1842  dsa_area_span *next = dsa_get_address(area, span->nextspan);
1843 
1844  next->prevspan = span->prevspan;
1845  }
1846  if (DsaPointerIsValid(span->prevspan))
1847  {
1848  dsa_area_span *prev = dsa_get_address(area, span->prevspan);
1849 
1850  prev->nextspan = span->nextspan;
1851  }
1852  else
1853  {
1854  dsa_area_pool *pool = dsa_get_address(area, span->pool);
1855 
1856  pool->spans[span->fclass] = span->nextspan;
1857  }
1858 }
dsa_pointer nextspan
Definition: dsa.c:198
static int32 next
Definition: blutils.c:210
uint16 fclass
Definition: dsa.c:206
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition: dsa.c:924
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

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

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