PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
freelist.c File Reference
#include "postgres.h"
#include "port/atomics.h"
#include "storage/buf_internals.h"
#include "storage/bufmgr.h"
#include "storage/proc.h"
Include dependency graph for freelist.c:

Go to the source code of this file.

Data Structures

struct  BufferStrategyControl
 
struct  BufferAccessStrategyData
 

Macros

#define INT_ACCESS_ONCE(var)   ((int)(*((volatile int *)&(var))))
 

Typedefs

typedef struct
BufferAccessStrategyData 
BufferAccessStrategyData
 

Functions

static BufferDescGetBufferFromRing (BufferAccessStrategy strategy, uint32 *buf_state)
 
static void AddBufferToRing (BufferAccessStrategy strategy, BufferDesc *buf)
 
static uint32 ClockSweepTick (void)
 
bool have_free_buffer ()
 
BufferDescStrategyGetBuffer (BufferAccessStrategy strategy, uint32 *buf_state)
 
void StrategyFreeBuffer (BufferDesc *buf)
 
int StrategySyncStart (uint32 *complete_passes, uint32 *num_buf_alloc)
 
void StrategyNotifyBgWriter (int bgwprocno)
 
Size StrategyShmemSize (void)
 
void StrategyInitialize (bool init)
 
BufferAccessStrategy GetAccessStrategy (BufferAccessStrategyType btype)
 
void FreeAccessStrategy (BufferAccessStrategy strategy)
 
bool StrategyRejectBuffer (BufferAccessStrategy strategy, BufferDesc *buf)
 

Variables

static BufferStrategyControlStrategyControl = NULL
 

Macro Definition Documentation

#define INT_ACCESS_ONCE (   var)    ((int)(*((volatile int *)&(var))))

Definition at line 23 of file freelist.c.

Referenced by StrategyGetBuffer().

Typedef Documentation

Function Documentation

static void AddBufferToRing ( BufferAccessStrategy  strategy,
BufferDesc buf 
)
static

Definition at line 669 of file freelist.c.

References BufferDescriptorGetBuffer, BufferAccessStrategyData::buffers, and BufferAccessStrategyData::current.

Referenced by StrategyGetBuffer().

670 {
671  strategy->buffers[strategy->current] = BufferDescriptorGetBuffer(buf);
672 }
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: freelist.c:96
#define BufferDescriptorGetBuffer(bdesc)
static uint32 ClockSweepTick ( void  )
inlinestatic

Definition at line 113 of file freelist.c.

References BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::completePasses, NBuffers, BufferStrategyControl::nextVictimBuffer, pg_atomic_compare_exchange_u32(), pg_atomic_fetch_add_u32(), SpinLockAcquire, SpinLockRelease, and success.

Referenced by StrategyGetBuffer().

114 {
115  uint32 victim;
116 
117  /*
118  * Atomically move hand ahead one buffer - if there's several processes
119  * doing this, this can lead to buffers being returned slightly out of
120  * apparent order.
121  */
122  victim =
124 
125  if (victim >= NBuffers)
126  {
127  uint32 originalVictim = victim;
128 
129  /* always wrap what we look up in BufferDescriptors */
130  victim = victim % NBuffers;
131 
132  /*
133  * If we're the one that just caused a wraparound, force
134  * completePasses to be incremented while holding the spinlock. We
135  * need the spinlock so StrategySyncStart() can return a consistent
136  * value consisting of nextVictimBuffer and completePasses.
137  */
138  if (victim == 0)
139  {
140  uint32 expected;
141  uint32 wrapped;
142  bool success = false;
143 
144  expected = originalVictim + 1;
145 
146  while (!success)
147  {
148  /*
149  * Acquire the spinlock while increasing completePasses. That
150  * allows other readers to read nextVictimBuffer and
151  * completePasses in a consistent manner which is required for
152  * StrategySyncStart(). In theory delaying the increment
153  * could lead to an overflow of nextVictimBuffers, but that's
154  * highly unlikely and wouldn't be particularly harmful.
155  */
157 
158  wrapped = expected % NBuffers;
159 
161  &expected, wrapped);
162  if (success)
165  }
166  }
167  }
168  return victim;
169 }
static bool pg_atomic_compare_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 *expected, uint32 newval)
Definition: atomics.h:322
pg_atomic_uint32 nextVictimBuffer
Definition: freelist.c:39
#define SpinLockAcquire(lock)
Definition: spin.h:62
static bool success
Definition: pg_basebackup.c:99
unsigned int uint32
Definition: c.h:258
slock_t buffer_strategy_lock
Definition: freelist.c:32
#define SpinLockRelease(lock)
Definition: spin.h:64
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:339
int NBuffers
Definition: globals.c:122
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64
void FreeAccessStrategy ( BufferAccessStrategy  strategy)

Definition at line 597 of file freelist.c.

References pfree().

Referenced by blgetbitmap(), FreeBulkInsertState(), heap_endscan(), and initscan().

598 {
599  /* don't crash if called on a "default" strategy */
600  if (strategy != NULL)
601  pfree(strategy);
602 }
void pfree(void *pointer)
Definition: mcxt.c:949
BufferAccessStrategy GetAccessStrategy ( BufferAccessStrategyType  btype)

Definition at line 542 of file freelist.c.

References BAS_BULKREAD, BAS_BULKWRITE, BAS_NORMAL, BAS_VACUUM, BufferAccessStrategyData::btype, elog, ERROR, Min, NBuffers, offsetof, palloc0(), and BufferAccessStrategyData::ring_size.

Referenced by blgetbitmap(), bt_check_every_level(), collect_corrupt_items(), collect_visibility_data(), do_autovacuum(), GetBulkInsertState(), initscan(), pgstat_index(), pgstathashindex(), pgstatindex_impl(), statapprox_heap(), and vacuum().

543 {
544  BufferAccessStrategy strategy;
545  int ring_size;
546 
547  /*
548  * Select ring size to use. See buffer/README for rationales.
549  *
550  * Note: if you change the ring size for BAS_BULKREAD, see also
551  * SYNC_SCAN_REPORT_INTERVAL in access/heap/syncscan.c.
552  */
553  switch (btype)
554  {
555  case BAS_NORMAL:
556  /* if someone asks for NORMAL, just give 'em a "default" object */
557  return NULL;
558 
559  case BAS_BULKREAD:
560  ring_size = 256 * 1024 / BLCKSZ;
561  break;
562  case BAS_BULKWRITE:
563  ring_size = 16 * 1024 * 1024 / BLCKSZ;
564  break;
565  case BAS_VACUUM:
566  ring_size = 256 * 1024 / BLCKSZ;
567  break;
568 
569  default:
570  elog(ERROR, "unrecognized buffer access strategy: %d",
571  (int) btype);
572  return NULL; /* keep compiler quiet */
573  }
574 
575  /* Make sure ring isn't an undue fraction of shared buffers */
576  ring_size = Min(NBuffers / 8, ring_size);
577 
578  /* Allocate the object and initialize all elements to zeroes */
579  strategy = (BufferAccessStrategy)
581  ring_size * sizeof(Buffer));
582 
583  /* Set fields that don't start out zero */
584  strategy->btype = btype;
585  strategy->ring_size = ring_size;
586 
587  return strategy;
588 }
struct BufferAccessStrategyData * BufferAccessStrategy
Definition: buf.h:44
#define Min(x, y)
Definition: c.h:812
#define ERROR
Definition: elog.h:43
BufferAccessStrategyType btype
Definition: freelist.c:74
void * palloc0(Size size)
Definition: mcxt.c:877
int NBuffers
Definition: globals.c:122
#define elog
Definition: elog.h:219
int Buffer
Definition: buf.h:23
#define offsetof(type, field)
Definition: c.h:549
static BufferDesc * GetBufferFromRing ( BufferAccessStrategy  strategy,
uint32 buf_state 
)
static

Definition at line 611 of file freelist.c.

References buf, BUF_STATE_GET_REFCOUNT, BUF_STATE_GET_USAGECOUNT, BufferAccessStrategyData::buffers, BufferAccessStrategyData::current, BufferAccessStrategyData::current_was_in_ring, GetBufferDescriptor, InvalidBuffer, LockBufHdr(), BufferAccessStrategyData::ring_size, and UnlockBufHdr.

Referenced by StrategyGetBuffer().

612 {
613  BufferDesc *buf;
614  Buffer bufnum;
615  uint32 local_buf_state; /* to avoid repeated (de-)referencing */
616 
617 
618  /* Advance to next ring slot */
619  if (++strategy->current >= strategy->ring_size)
620  strategy->current = 0;
621 
622  /*
623  * If the slot hasn't been filled yet, tell the caller to allocate a new
624  * buffer with the normal allocation strategy. He will then fill this
625  * slot by calling AddBufferToRing with the new buffer.
626  */
627  bufnum = strategy->buffers[strategy->current];
628  if (bufnum == InvalidBuffer)
629  {
630  strategy->current_was_in_ring = false;
631  return NULL;
632  }
633 
634  /*
635  * If the buffer is pinned we cannot use it under any circumstances.
636  *
637  * If usage_count is 0 or 1 then the buffer is fair game (we expect 1,
638  * since our own previous usage of the ring element would have left it
639  * there, but it might've been decremented by clock sweep since then). A
640  * higher usage_count indicates someone else has touched the buffer, so we
641  * shouldn't re-use it.
642  */
643  buf = GetBufferDescriptor(bufnum - 1);
644  local_buf_state = LockBufHdr(buf);
645  if (BUF_STATE_GET_REFCOUNT(local_buf_state) == 0
646  && BUF_STATE_GET_USAGECOUNT(local_buf_state) <= 1)
647  {
648  strategy->current_was_in_ring = true;
649  *buf_state = local_buf_state;
650  return buf;
651  }
652  UnlockBufHdr(buf, local_buf_state);
653 
654  /*
655  * Tell caller to allocate a new buffer with the normal allocation
656  * strategy. He'll then replace this ring element via AddBufferToRing.
657  */
658  strategy->current_was_in_ring = false;
659  return NULL;
660 }
#define InvalidBuffer
Definition: buf.h:25
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: freelist.c:96
static char * buf
Definition: pg_test_fsync.c:67
#define GetBufferDescriptor(id)
unsigned int uint32
Definition: c.h:258
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:4092
#define UnlockBufHdr(desc, s)
#define BUF_STATE_GET_USAGECOUNT(state)
Definition: buf_internals.h:50
int Buffer
Definition: buf.h:23
#define BUF_STATE_GET_REFCOUNT(state)
Definition: buf_internals.h:49
bool have_free_buffer ( void  )

Definition at line 180 of file freelist.c.

References BufferStrategyControl::firstFreeBuffer.

Referenced by apw_load_buffers(), and autoprewarm_database_main().

181 {
183  return true;
184  else
185  return false;
186 }
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64
void StrategyFreeBuffer ( BufferDesc buf)

Definition at line 364 of file freelist.c.

References BufferDesc::buf_id, BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::firstFreeBuffer, BufferDesc::freeNext, FREENEXT_NOT_IN_LIST, BufferStrategyControl::lastFreeBuffer, SpinLockAcquire, and SpinLockRelease.

Referenced by InvalidateBuffer().

365 {
367 
368  /*
369  * It is possible that we are told to put something in the freelist that
370  * is already in it; don't screw up the list if so.
371  */
372  if (buf->freeNext == FREENEXT_NOT_IN_LIST)
373  {
375  if (buf->freeNext < 0)
378  }
379 
381 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define FREENEXT_NOT_IN_LIST
slock_t buffer_strategy_lock
Definition: freelist.c:32
#define SpinLockRelease(lock)
Definition: spin.h:64
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64
BufferDesc* StrategyGetBuffer ( BufferAccessStrategy  strategy,
uint32 buf_state 
)

Definition at line 201 of file freelist.c.

References AddBufferToRing(), PROC_HDR::allProcs, Assert, BufferStrategyControl::bgwprocno, buf, BUF_STATE_GET_REFCOUNT, BUF_STATE_GET_USAGECOUNT, BUF_USAGECOUNT_ONE, BufferStrategyControl::buffer_strategy_lock, ClockSweepTick(), elog, ERROR, BufferStrategyControl::firstFreeBuffer, BufferDesc::freeNext, FREENEXT_NOT_IN_LIST, GetBufferDescriptor, GetBufferFromRing(), INT_ACCESS_ONCE, LockBufHdr(), NBuffers, BufferStrategyControl::numBufferAllocs, pg_atomic_fetch_add_u32(), ProcGlobal, PGPROC::procLatch, SetLatch(), SpinLockAcquire, SpinLockRelease, and UnlockBufHdr.

Referenced by BufferAlloc().

202 {
203  BufferDesc *buf;
204  int bgwprocno;
205  int trycounter;
206  uint32 local_buf_state; /* to avoid repeated (de-)referencing */
207 
208  /*
209  * If given a strategy object, see whether it can select a buffer. We
210  * assume strategy objects don't need buffer_strategy_lock.
211  */
212  if (strategy != NULL)
213  {
214  buf = GetBufferFromRing(strategy, buf_state);
215  if (buf != NULL)
216  return buf;
217  }
218 
219  /*
220  * If asked, we need to waken the bgwriter. Since we don't want to rely on
221  * a spinlock for this we force a read from shared memory once, and then
222  * set the latch based on that value. We need to go through that length
223  * because otherwise bgprocno might be reset while/after we check because
224  * the compiler might just reread from memory.
225  *
226  * This can possibly set the latch of the wrong process if the bgwriter
227  * dies in the wrong moment. But since PGPROC->procLatch is never
228  * deallocated the worst consequence of that is that we set the latch of
229  * some arbitrary process.
230  */
232  if (bgwprocno != -1)
233  {
234  /* reset bgwprocno first, before setting the latch */
236 
237  /*
238  * Not acquiring ProcArrayLock here which is slightly icky. It's
239  * actually fine because procLatch isn't ever freed, so we just can
240  * potentially set the wrong process' (or no process') latch.
241  */
242  SetLatch(&ProcGlobal->allProcs[bgwprocno].procLatch);
243  }
244 
245  /*
246  * We count buffer allocation requests so that the bgwriter can estimate
247  * the rate of buffer consumption. Note that buffers recycled by a
248  * strategy object are intentionally not counted here.
249  */
251 
252  /*
253  * First check, without acquiring the lock, whether there's buffers in the
254  * freelist. Since we otherwise don't require the spinlock in every
255  * StrategyGetBuffer() invocation, it'd be sad to acquire it here -
256  * uselessly in most cases. That obviously leaves a race where a buffer is
257  * put on the freelist but we don't see the store yet - but that's pretty
258  * harmless, it'll just get used during the next buffer acquisition.
259  *
260  * If there's buffers on the freelist, acquire the spinlock to pop one
261  * buffer of the freelist. Then check whether that buffer is usable and
262  * repeat if not.
263  *
264  * Note that the freeNext fields are considered to be protected by the
265  * buffer_strategy_lock not the individual buffer spinlocks, so it's OK to
266  * manipulate them without holding the spinlock.
267  */
269  {
270  while (true)
271  {
272  /* Acquire the spinlock to remove element from the freelist */
274 
276  {
278  break;
279  }
280 
283 
284  /* Unconditionally remove buffer from freelist */
287 
288  /*
289  * Release the lock so someone else can access the freelist while
290  * we check out this buffer.
291  */
293 
294  /*
295  * If the buffer is pinned or has a nonzero usage_count, we cannot
296  * use it; discard it and retry. (This can only happen if VACUUM
297  * put a valid buffer in the freelist and then someone else used
298  * it before we got to it. It's probably impossible altogether as
299  * of 8.3, but we'd better check anyway.)
300  */
301  local_buf_state = LockBufHdr(buf);
302  if (BUF_STATE_GET_REFCOUNT(local_buf_state) == 0
303  && BUF_STATE_GET_USAGECOUNT(local_buf_state) == 0)
304  {
305  if (strategy != NULL)
306  AddBufferToRing(strategy, buf);
307  *buf_state = local_buf_state;
308  return buf;
309  }
310  UnlockBufHdr(buf, local_buf_state);
311 
312  }
313  }
314 
315  /* Nothing on the freelist, so run the "clock sweep" algorithm */
316  trycounter = NBuffers;
317  for (;;)
318  {
320 
321  /*
322  * If the buffer is pinned or has a nonzero usage_count, we cannot use
323  * it; decrement the usage_count (unless pinned) and keep scanning.
324  */
325  local_buf_state = LockBufHdr(buf);
326 
327  if (BUF_STATE_GET_REFCOUNT(local_buf_state) == 0)
328  {
329  if (BUF_STATE_GET_USAGECOUNT(local_buf_state) != 0)
330  {
331  local_buf_state -= BUF_USAGECOUNT_ONE;
332 
333  trycounter = NBuffers;
334  }
335  else
336  {
337  /* Found a usable buffer */
338  if (strategy != NULL)
339  AddBufferToRing(strategy, buf);
340  *buf_state = local_buf_state;
341  return buf;
342  }
343  }
344  else if (--trycounter == 0)
345  {
346  /*
347  * We've scanned all the buffers without making any state changes,
348  * so all the buffers are pinned (or were when we looked at them).
349  * We could hope that someone will free one eventually, but it's
350  * probably better to fail than to risk getting stuck in an
351  * infinite loop.
352  */
353  UnlockBufHdr(buf, local_buf_state);
354  elog(ERROR, "no unpinned buffers available");
355  }
356  UnlockBufHdr(buf, local_buf_state);
357  }
358 }
static uint32 ClockSweepTick(void)
Definition: freelist.c:113
PROC_HDR * ProcGlobal
Definition: proc.c:80
Latch procLatch
Definition: proc.h:104
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define FREENEXT_NOT_IN_LIST
#define ERROR
Definition: elog.h:43
static BufferDesc * GetBufferFromRing(BufferAccessStrategy strategy, uint32 *buf_state)
Definition: freelist.c:611
static char * buf
Definition: pg_test_fsync.c:67
#define BUF_USAGECOUNT_ONE
Definition: buf_internals.h:44
#define GetBufferDescriptor(id)
unsigned int uint32
Definition: c.h:258
slock_t buffer_strategy_lock
Definition: freelist.c:32
#define INT_ACCESS_ONCE(var)
Definition: freelist.c:23
#define SpinLockRelease(lock)
Definition: spin.h:64
pg_atomic_uint32 numBufferAllocs
Definition: freelist.c:54
static void AddBufferToRing(BufferAccessStrategy strategy, BufferDesc *buf)
Definition: freelist.c:669
static uint32 pg_atomic_fetch_add_u32(volatile pg_atomic_uint32 *ptr, int32 add_)
Definition: atomics.h:339
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:4092
#define Assert(condition)
Definition: c.h:681
#define UnlockBufHdr(desc, s)
int NBuffers
Definition: globals.c:122
PGPROC * allProcs
Definition: proc.h:244
#define elog
Definition: elog.h:219
#define BUF_STATE_GET_USAGECOUNT(state)
Definition: buf_internals.h:50
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64
#define BUF_STATE_GET_REFCOUNT(state)
Definition: buf_internals.h:49
void StrategyInitialize ( bool  init)

Definition at line 475 of file freelist.c.

References Assert, BufferStrategyControl::bgwprocno, BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::completePasses, BufferStrategyControl::firstFreeBuffer, InitBufTable(), BufferStrategyControl::lastFreeBuffer, NBuffers, BufferStrategyControl::nextVictimBuffer, NUM_BUFFER_PARTITIONS, BufferStrategyControl::numBufferAllocs, pg_atomic_init_u32(), ShmemInitStruct(), and SpinLockInit.

Referenced by InitBufferPool().

476 {
477  bool found;
478 
479  /*
480  * Initialize the shared buffer lookup hashtable.
481  *
482  * Since we can't tolerate running out of lookup table entries, we must be
483  * sure to specify an adequate table size here. The maximum steady-state
484  * usage is of course NBuffers entries, but BufferAlloc() tries to insert
485  * a new entry before deleting the old. In principle this could be
486  * happening in each partition concurrently, so we could need as many as
487  * NBuffers + NUM_BUFFER_PARTITIONS entries.
488  */
490 
491  /*
492  * Get or create the shared strategy control block
493  */
495  ShmemInitStruct("Buffer Strategy Status",
496  sizeof(BufferStrategyControl),
497  &found);
498 
499  if (!found)
500  {
501  /*
502  * Only done once, usually in postmaster
503  */
504  Assert(init);
505 
507 
508  /*
509  * Grab the whole linked list of free buffers for our strategy. We
510  * assume it was previously set up by InitBufferPool().
511  */
514 
515  /* Initialize the clock sweep pointer */
517 
518  /* Clear statistics */
521 
522  /* No pending notification */
524  }
525  else
526  Assert(!init);
527 }
pg_atomic_uint32 nextVictimBuffer
Definition: freelist.c:39
#define SpinLockInit(lock)
Definition: spin.h:60
void InitBufTable(int size)
Definition: buf_table.c:53
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
#define NUM_BUFFER_PARTITIONS
Definition: lwlock.h:113
slock_t buffer_strategy_lock
Definition: freelist.c:32
pg_atomic_uint32 numBufferAllocs
Definition: freelist.c:54
#define Assert(condition)
Definition: c.h:681
static void init(bool is_no_vacuum)
Definition: pgbench.c:2606
int NBuffers
Definition: globals.c:122
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:234
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64
void StrategyNotifyBgWriter ( int  bgwprocno)

Definition at line 432 of file freelist.c.

References BufferStrategyControl::bgwprocno, BufferStrategyControl::buffer_strategy_lock, SpinLockAcquire, and SpinLockRelease.

Referenced by BackgroundWriterMain().

433 {
434  /*
435  * We acquire buffer_strategy_lock just to ensure that the store appears
436  * atomic to StrategyGetBuffer. The bgwriter should call this rather
437  * infrequently, so there's no performance penalty from being safe.
438  */
440  StrategyControl->bgwprocno = bgwprocno;
442 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
slock_t buffer_strategy_lock
Definition: freelist.c:32
#define SpinLockRelease(lock)
Definition: spin.h:64
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64
bool StrategyRejectBuffer ( BufferAccessStrategy  strategy,
BufferDesc buf 
)

Definition at line 686 of file freelist.c.

References BAS_BULKREAD, BufferAccessStrategyData::btype, BufferDescriptorGetBuffer, BufferAccessStrategyData::buffers, BufferAccessStrategyData::current, BufferAccessStrategyData::current_was_in_ring, and InvalidBuffer.

Referenced by BufferAlloc().

687 {
688  /* We only do this in bulkread mode */
689  if (strategy->btype != BAS_BULKREAD)
690  return false;
691 
692  /* Don't muck with behavior of normal buffer-replacement strategy */
693  if (!strategy->current_was_in_ring ||
694  strategy->buffers[strategy->current] != BufferDescriptorGetBuffer(buf))
695  return false;
696 
697  /*
698  * Remove the dirty buffer from the ring; necessary to prevent infinite
699  * loop if all ring members are dirty.
700  */
701  strategy->buffers[strategy->current] = InvalidBuffer;
702 
703  return true;
704 }
#define InvalidBuffer
Definition: buf.h:25
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: freelist.c:96
BufferAccessStrategyType btype
Definition: freelist.c:74
#define BufferDescriptorGetBuffer(bdesc)
Size StrategyShmemSize ( void  )

Definition at line 454 of file freelist.c.

References add_size(), BufTableShmemSize(), MAXALIGN, NBuffers, and NUM_BUFFER_PARTITIONS.

Referenced by BufferShmemSize().

455 {
456  Size size = 0;
457 
458  /* size of lookup hash table ... see comment in StrategyInitialize */
460 
461  /* size of the shared replacement strategy control block */
462  size = add_size(size, MAXALIGN(sizeof(BufferStrategyControl)));
463 
464  return size;
465 }
#define NUM_BUFFER_PARTITIONS
Definition: lwlock.h:113
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:350
#define MAXALIGN(LEN)
Definition: c.h:576
int NBuffers
Definition: globals.c:122
Size BufTableShmemSize(int size)
Definition: buf_table.c:43
int StrategySyncStart ( uint32 complete_passes,
uint32 num_buf_alloc 
)

Definition at line 395 of file freelist.c.

References BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::completePasses, NBuffers, BufferStrategyControl::nextVictimBuffer, BufferStrategyControl::numBufferAllocs, pg_atomic_exchange_u32(), pg_atomic_read_u32(), SpinLockAcquire, and SpinLockRelease.

Referenced by BgBufferSync().

396 {
397  uint32 nextVictimBuffer;
398  int result;
399 
402  result = nextVictimBuffer % NBuffers;
403 
404  if (complete_passes)
405  {
406  *complete_passes = StrategyControl->completePasses;
407 
408  /*
409  * Additionally add the number of wraparounds that happened before
410  * completePasses could be incremented. C.f. ClockSweepTick().
411  */
412  *complete_passes += nextVictimBuffer / NBuffers;
413  }
414 
415  if (num_buf_alloc)
416  {
418  }
420  return result;
421 }
pg_atomic_uint32 nextVictimBuffer
Definition: freelist.c:39
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:303
#define SpinLockAcquire(lock)
Definition: spin.h:62
unsigned int uint32
Definition: c.h:258
slock_t buffer_strategy_lock
Definition: freelist.c:32
#define SpinLockRelease(lock)
Definition: spin.h:64
pg_atomic_uint32 numBufferAllocs
Definition: freelist.c:54
int result
Definition: header.h:19
int NBuffers
Definition: globals.c:122
static BufferStrategyControl * StrategyControl
Definition: freelist.c:64
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:252

Variable Documentation

BufferStrategyControl* StrategyControl = NULL
static

Definition at line 64 of file freelist.c.