PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
freelist.c File Reference
#include "postgres.h"
#include "pgstat.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 (void)
 
BufferDescStrategyGetBuffer (BufferAccessStrategy strategy, uint32 *buf_state, bool *from_ring)
 
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)
 
BufferAccessStrategy GetAccessStrategyWithSize (BufferAccessStrategyType btype, int ring_size_kb)
 
int GetAccessStrategyBufferCount (BufferAccessStrategy strategy)
 
int GetAccessStrategyPinLimit (BufferAccessStrategy strategy)
 
void FreeAccessStrategy (BufferAccessStrategy strategy)
 
IOContext IOContextForStrategy (BufferAccessStrategy strategy)
 
bool StrategyRejectBuffer (BufferAccessStrategy strategy, BufferDesc *buf, bool from_ring)
 

Variables

static BufferStrategyControlStrategyControl = NULL
 

Macro Definition Documentation

◆ INT_ACCESS_ONCE

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

Definition at line 24 of file freelist.c.

Typedef Documentation

◆ BufferAccessStrategyData

Function Documentation

◆ AddBufferToRing()

static void AddBufferToRing ( BufferAccessStrategy  strategy,
BufferDesc buf 
)
static

Definition at line 790 of file freelist.c.

791{
792 strategy->buffers[strategy->current] = BufferDescriptorGetBuffer(buf);
793}
static Buffer BufferDescriptorGetBuffer(const BufferDesc *bdesc)
static char * buf
Definition: pg_test_fsync.c:72
Buffer buffers[FLEXIBLE_ARRAY_MEMBER]
Definition: freelist.c:91

References buf, BufferDescriptorGetBuffer(), BufferAccessStrategyData::buffers, and BufferAccessStrategyData::current.

Referenced by StrategyGetBuffer().

◆ ClockSweepTick()

static uint32 ClockSweepTick ( void  )
inlinestatic

Definition at line 108 of file freelist.c.

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

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

Referenced by StrategyGetBuffer().

◆ FreeAccessStrategy()

void FreeAccessStrategy ( BufferAccessStrategy  strategy)

Definition at line 723 of file freelist.c.

724{
725 /* don't crash if called on a "default" strategy */
726 if (strategy != NULL)
727 pfree(strategy);
728}
void pfree(void *pointer)
Definition: mcxt.c:2147

References pfree().

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

◆ GetAccessStrategy()

BufferAccessStrategy GetAccessStrategy ( BufferAccessStrategyType  btype)

Definition at line 541 of file freelist.c.

542{
543 int ring_size_kb;
544
545 /*
546 * Select ring size to use. See buffer/README for rationales.
547 *
548 * Note: if you change the ring size for BAS_BULKREAD, see also
549 * SYNC_SCAN_REPORT_INTERVAL in access/heap/syncscan.c.
550 */
551 switch (btype)
552 {
553 case BAS_NORMAL:
554 /* if someone asks for NORMAL, just give 'em a "default" object */
555 return NULL;
556
557 case BAS_BULKREAD:
558 {
559 int ring_max_kb;
560
561 /*
562 * The ring always needs to be large enough to allow some
563 * separation in time between providing a buffer to the user
564 * of the strategy and that buffer being reused. Otherwise the
565 * user's pin will prevent reuse of the buffer, even without
566 * concurrent activity.
567 *
568 * We also need to ensure the ring always is large enough for
569 * SYNC_SCAN_REPORT_INTERVAL, as noted above.
570 *
571 * Thus we start out a minimal size and increase the size
572 * further if appropriate.
573 */
574 ring_size_kb = 256;
575
576 /*
577 * There's no point in a larger ring if we won't be allowed to
578 * pin sufficiently many buffers. But we never limit to less
579 * than the minimal size above.
580 */
581 ring_max_kb = GetPinLimit() * (BLCKSZ / 1024);
582 ring_max_kb = Max(ring_size_kb, ring_max_kb);
583
584 /*
585 * We would like the ring to additionally have space for the
586 * the configured degree of IO concurrency. While being read
587 * in, buffers can obviously not yet be reused.
588 *
589 * Each IO can be up to io_combine_limit blocks large, and we
590 * want to start up to effective_io_concurrency IOs.
591 *
592 * Note that effective_io_concurrency may be 0, which disables
593 * AIO.
594 */
595 ring_size_kb += (BLCKSZ / 1024) *
597
598 if (ring_size_kb > ring_max_kb)
599 ring_size_kb = ring_max_kb;
600 break;
601 }
602 case BAS_BULKWRITE:
603 ring_size_kb = 16 * 1024;
604 break;
605 case BAS_VACUUM:
606 ring_size_kb = 2048;
607 break;
608
609 default:
610 elog(ERROR, "unrecognized buffer access strategy: %d",
611 (int) btype);
612 return NULL; /* keep compiler quiet */
613 }
614
615 return GetAccessStrategyWithSize(btype, ring_size_kb);
616}
int effective_io_concurrency
Definition: bufmgr.c:155
int io_combine_limit
Definition: bufmgr.c:170
uint32 GetPinLimit(void)
Definition: bufmgr.c:2510
@ BAS_BULKREAD
Definition: bufmgr.h:37
@ BAS_NORMAL
Definition: bufmgr.h:36
@ BAS_VACUUM
Definition: bufmgr.h:40
@ BAS_BULKWRITE
Definition: bufmgr.h:39
#define Max(x, y)
Definition: c.h:969
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
BufferAccessStrategy GetAccessStrategyWithSize(BufferAccessStrategyType btype, int ring_size_kb)
Definition: freelist.c:626

References BAS_BULKREAD, BAS_BULKWRITE, BAS_NORMAL, BAS_VACUUM, effective_io_concurrency, elog, ERROR, GetAccessStrategyWithSize(), GetPinLimit(), io_combine_limit, and Max.

Referenced by blgetbitmap(), bt_check_every_level(), collect_corrupt_items(), collect_visibility_data(), GetBulkInsertState(), gin_check_parent_keys_consistency(), gin_check_posting_tree_parent_keys_consistency(), initscan(), pgstat_index(), pgstathashindex(), pgstatindex_impl(), RelationCopyStorageUsingBuffer(), ScanSourceDatabasePgClass(), statapprox_heap(), and verify_heapam().

◆ GetAccessStrategyBufferCount()

int GetAccessStrategyBufferCount ( BufferAccessStrategy  strategy)

Definition at line 666 of file freelist.c.

667{
668 if (strategy == NULL)
669 return 0;
670
671 return strategy->nbuffers;
672}

References BufferAccessStrategyData::nbuffers.

Referenced by parallel_vacuum_init().

◆ GetAccessStrategyPinLimit()

int GetAccessStrategyPinLimit ( BufferAccessStrategy  strategy)

Definition at line 689 of file freelist.c.

690{
691 if (strategy == NULL)
692 return NBuffers;
693
694 switch (strategy->btype)
695 {
696 case BAS_BULKREAD:
697
698 /*
699 * Since BAS_BULKREAD uses StrategyRejectBuffer(), dirty buffers
700 * shouldn't be a problem and the caller is free to pin up to the
701 * entire ring at once.
702 */
703 return strategy->nbuffers;
704
705 default:
706
707 /*
708 * Tell caller not to pin more than half the buffers in the ring.
709 * This is a trade-off between look ahead distance and deferring
710 * writeback and associated WAL traffic.
711 */
712 return strategy->nbuffers / 2;
713 }
714}
BufferAccessStrategyType btype
Definition: freelist.c:75

References BAS_BULKREAD, BufferAccessStrategyData::btype, BufferAccessStrategyData::nbuffers, and NBuffers.

Referenced by read_stream_begin_impl().

◆ GetAccessStrategyWithSize()

BufferAccessStrategy GetAccessStrategyWithSize ( BufferAccessStrategyType  btype,
int  ring_size_kb 
)

Definition at line 626 of file freelist.c.

627{
628 int ring_buffers;
629 BufferAccessStrategy strategy;
630
631 Assert(ring_size_kb >= 0);
632
633 /* Figure out how many buffers ring_size_kb is */
634 ring_buffers = ring_size_kb / (BLCKSZ / 1024);
635
636 /* 0 means unlimited, so no BufferAccessStrategy required */
637 if (ring_buffers == 0)
638 return NULL;
639
640 /* Cap to 1/8th of shared_buffers */
641 ring_buffers = Min(NBuffers / 8, ring_buffers);
642
643 /* NBuffers should never be less than 16, so this shouldn't happen */
644 Assert(ring_buffers > 0);
645
646 /* Allocate the object and initialize all elements to zeroes */
647 strategy = (BufferAccessStrategy)
648 palloc0(offsetof(BufferAccessStrategyData, buffers) +
649 ring_buffers * sizeof(Buffer));
650
651 /* Set fields that don't start out zero */
652 strategy->btype = btype;
653 strategy->nbuffers = ring_buffers;
654
655 return strategy;
656}
int Buffer
Definition: buf.h:23
struct BufferAccessStrategyData * BufferAccessStrategy
Definition: buf.h:44
#define Min(x, y)
Definition: c.h:975
Assert(PointerIsAligned(start, uint64))
void * palloc0(Size size)
Definition: mcxt.c:1970

References Assert(), BufferAccessStrategyData::btype, Min, BufferAccessStrategyData::nbuffers, NBuffers, and palloc0().

Referenced by do_autovacuum(), ExecVacuum(), GetAccessStrategy(), and parallel_vacuum_main().

◆ GetBufferFromRing()

static BufferDesc * GetBufferFromRing ( BufferAccessStrategy  strategy,
uint32 buf_state 
)
static

Definition at line 737 of file freelist.c.

738{
740 Buffer bufnum;
741 uint32 local_buf_state; /* to avoid repeated (de-)referencing */
742
743
744 /* Advance to next ring slot */
745 if (++strategy->current >= strategy->nbuffers)
746 strategy->current = 0;
747
748 /*
749 * If the slot hasn't been filled yet, tell the caller to allocate a new
750 * buffer with the normal allocation strategy. He will then fill this
751 * slot by calling AddBufferToRing with the new buffer.
752 */
753 bufnum = strategy->buffers[strategy->current];
754 if (bufnum == InvalidBuffer)
755 return NULL;
756
757 /*
758 * If the buffer is pinned we cannot use it under any circumstances.
759 *
760 * If usage_count is 0 or 1 then the buffer is fair game (we expect 1,
761 * since our own previous usage of the ring element would have left it
762 * there, but it might've been decremented by clock sweep since then). A
763 * higher usage_count indicates someone else has touched the buffer, so we
764 * shouldn't re-use it.
765 */
766 buf = GetBufferDescriptor(bufnum - 1);
767 local_buf_state = LockBufHdr(buf);
768 if (BUF_STATE_GET_REFCOUNT(local_buf_state) == 0
769 && BUF_STATE_GET_USAGECOUNT(local_buf_state) <= 1)
770 {
771 *buf_state = local_buf_state;
772 return buf;
773 }
774 UnlockBufHdr(buf, local_buf_state);
775
776 /*
777 * Tell caller to allocate a new buffer with the normal allocation
778 * strategy. He'll then replace this ring element via AddBufferToRing.
779 */
780 return NULL;
781}
#define InvalidBuffer
Definition: buf.h:25
static void UnlockBufHdr(BufferDesc *desc, uint32 buf_state)
#define BUF_STATE_GET_USAGECOUNT(state)
Definition: buf_internals.h:60
#define BUF_STATE_GET_REFCOUNT(state)
Definition: buf_internals.h:59
static BufferDesc * GetBufferDescriptor(uint32 id)
uint32 LockBufHdr(BufferDesc *desc)
Definition: bufmgr.c:6259

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

Referenced by StrategyGetBuffer().

◆ have_free_buffer()

bool have_free_buffer ( void  )

Definition at line 175 of file freelist.c.

176{
178 return true;
179 else
180 return false;
181}

References BufferStrategyControl::firstFreeBuffer, and StrategyControl.

Referenced by apw_load_buffers(), apw_read_stream_next_block(), and autoprewarm_database_main().

◆ IOContextForStrategy()

IOContext IOContextForStrategy ( BufferAccessStrategy  strategy)

Definition at line 800 of file freelist.c.

801{
802 if (!strategy)
803 return IOCONTEXT_NORMAL;
804
805 switch (strategy->btype)
806 {
807 case BAS_NORMAL:
808
809 /*
810 * Currently, GetAccessStrategy() returns NULL for
811 * BufferAccessStrategyType BAS_NORMAL, so this case is
812 * unreachable.
813 */
815 return IOCONTEXT_NORMAL;
816 case BAS_BULKREAD:
817 return IOCONTEXT_BULKREAD;
818 case BAS_BULKWRITE:
819 return IOCONTEXT_BULKWRITE;
820 case BAS_VACUUM:
821 return IOCONTEXT_VACUUM;
822 }
823
824 elog(ERROR, "unrecognized BufferAccessStrategyType: %d", strategy->btype);
826}
#define pg_unreachable()
Definition: c.h:332
@ IOCONTEXT_NORMAL
Definition: pgstat.h:286
@ IOCONTEXT_VACUUM
Definition: pgstat.h:287
@ IOCONTEXT_BULKREAD
Definition: pgstat.h:283
@ IOCONTEXT_BULKWRITE
Definition: pgstat.h:284

References BAS_BULKREAD, BAS_BULKWRITE, BAS_NORMAL, BAS_VACUUM, BufferAccessStrategyData::btype, elog, ERROR, IOCONTEXT_BULKREAD, IOCONTEXT_BULKWRITE, IOCONTEXT_NORMAL, IOCONTEXT_VACUUM, and pg_unreachable.

Referenced by AsyncReadBuffers(), ExtendBufferedRelShared(), PinBufferForBlock(), and WaitReadBuffers().

◆ StrategyFreeBuffer()

void StrategyFreeBuffer ( BufferDesc buf)

Definition at line 363 of file freelist.c.

364{
366
367 /*
368 * It is possible that we are told to put something in the freelist that
369 * is already in it; don't screw up the list if so.
370 */
371 if (buf->freeNext == FREENEXT_NOT_IN_LIST)
372 {
374 if (buf->freeNext < 0)
377 }
378
380}
#define FREENEXT_NOT_IN_LIST

References buf, BufferStrategyControl::buffer_strategy_lock, BufferStrategyControl::firstFreeBuffer, FREENEXT_NOT_IN_LIST, BufferStrategyControl::lastFreeBuffer, SpinLockAcquire, SpinLockRelease, and StrategyControl.

Referenced by BufferAlloc(), ExtendBufferedRelShared(), and InvalidateBuffer().

◆ StrategyGetBuffer()

BufferDesc * StrategyGetBuffer ( BufferAccessStrategy  strategy,
uint32 buf_state,
bool *  from_ring 
)

Definition at line 196 of file freelist.c.

197{
199 int bgwprocno;
200 int trycounter;
201 uint32 local_buf_state; /* to avoid repeated (de-)referencing */
202
203 *from_ring = false;
204
205 /*
206 * If given a strategy object, see whether it can select a buffer. We
207 * assume strategy objects don't need buffer_strategy_lock.
208 */
209 if (strategy != NULL)
210 {
211 buf = GetBufferFromRing(strategy, buf_state);
212 if (buf != NULL)
213 {
214 *from_ring = true;
215 return buf;
216 }
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 bgwprocno 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 */
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
282 Assert(buf->freeNext != FREENEXT_NOT_IN_LIST);
283
284 /* Unconditionally remove buffer from freelist */
286 buf->freeNext = FREENEXT_NOT_IN_LIST;
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 /* Nothing on the freelist, so run the "clock sweep" algorithm */
315 trycounter = NBuffers;
316 for (;;)
317 {
319
320 /*
321 * If the buffer is pinned or has a nonzero usage_count, we cannot use
322 * it; decrement the usage_count (unless pinned) and keep scanning.
323 */
324 local_buf_state = LockBufHdr(buf);
325
326 if (BUF_STATE_GET_REFCOUNT(local_buf_state) == 0)
327 {
328 if (BUF_STATE_GET_USAGECOUNT(local_buf_state) != 0)
329 {
330 local_buf_state -= BUF_USAGECOUNT_ONE;
331
332 trycounter = NBuffers;
333 }
334 else
335 {
336 /* Found a usable buffer */
337 if (strategy != NULL)
338 AddBufferToRing(strategy, buf);
339 *buf_state = local_buf_state;
340 return buf;
341 }
342 }
343 else if (--trycounter == 0)
344 {
345 /*
346 * We've scanned all the buffers without making any state changes,
347 * so all the buffers are pinned (or were when we looked at them).
348 * We could hope that someone will free one eventually, but it's
349 * probably better to fail than to risk getting stuck in an
350 * infinite loop.
351 */
352 UnlockBufHdr(buf, local_buf_state);
353 elog(ERROR, "no unpinned buffers available");
354 }
355 UnlockBufHdr(buf, local_buf_state);
356 }
357}
#define BUF_USAGECOUNT_ONE
Definition: buf_internals.h:54
static uint32 ClockSweepTick(void)
Definition: freelist.c:108
static void AddBufferToRing(BufferAccessStrategy strategy, BufferDesc *buf)
Definition: freelist.c:790
#define INT_ACCESS_ONCE(var)
Definition: freelist.c:24
static BufferDesc * GetBufferFromRing(BufferAccessStrategy strategy, uint32 *buf_state)
Definition: freelist.c:737
void SetLatch(Latch *latch)
Definition: latch.c:288
PROC_HDR * ProcGlobal
Definition: proc.c:79
pg_atomic_uint32 numBufferAllocs
Definition: freelist.c:55
Latch procLatch
Definition: proc.h:170
PGPROC * allProcs
Definition: proc.h:372

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, FREENEXT_NOT_IN_LIST, GetBufferDescriptor(), GetBufferFromRing(), INT_ACCESS_ONCE, LockBufHdr(), NBuffers, BufferStrategyControl::numBufferAllocs, pg_atomic_fetch_add_u32(), ProcGlobal, PGPROC::procLatch, SetLatch(), SpinLockAcquire, SpinLockRelease, StrategyControl, and UnlockBufHdr().

Referenced by GetVictimBuffer().

◆ StrategyInitialize()

void StrategyInitialize ( bool  init)

Definition at line 474 of file freelist.c.

475{
476 bool found;
477
478 /*
479 * Initialize the shared buffer lookup hashtable.
480 *
481 * Since we can't tolerate running out of lookup table entries, we must be
482 * sure to specify an adequate table size here. The maximum steady-state
483 * usage is of course NBuffers entries, but BufferAlloc() tries to insert
484 * a new entry before deleting the old. In principle this could be
485 * happening in each partition concurrently, so we could need as many as
486 * NBuffers + NUM_BUFFER_PARTITIONS entries.
487 */
489
490 /*
491 * Get or create the shared strategy control block
492 */
494 ShmemInitStruct("Buffer Strategy Status",
495 sizeof(BufferStrategyControl),
496 &found);
497
498 if (!found)
499 {
500 /*
501 * Only done once, usually in postmaster
502 */
503 Assert(init);
504
506
507 /*
508 * Grab the whole linked list of free buffers for our strategy. We
509 * assume it was previously set up by BufferManagerShmemInit().
510 */
513
514 /* Initialize the clock sweep pointer */
516
517 /* Clear statistics */
520
521 /* No pending notification */
523 }
524 else
525 Assert(!init);
526}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
void InitBufTable(int size)
Definition: buf_table.c:51
int init
Definition: isn.c:79
#define NUM_BUFFER_PARTITIONS
Definition: lwlock.h:93
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:57

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

Referenced by BufferManagerShmemInit().

◆ StrategyNotifyBgWriter()

void StrategyNotifyBgWriter ( int  bgwprocno)

Definition at line 431 of file freelist.c.

432{
433 /*
434 * We acquire buffer_strategy_lock just to ensure that the store appears
435 * atomic to StrategyGetBuffer. The bgwriter should call this rather
436 * infrequently, so there's no performance penalty from being safe.
437 */
439 StrategyControl->bgwprocno = bgwprocno;
441}

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

Referenced by BackgroundWriterMain().

◆ StrategyRejectBuffer()

bool StrategyRejectBuffer ( BufferAccessStrategy  strategy,
BufferDesc buf,
bool  from_ring 
)

Definition at line 840 of file freelist.c.

841{
842 /* We only do this in bulkread mode */
843 if (strategy->btype != BAS_BULKREAD)
844 return false;
845
846 /* Don't muck with behavior of normal buffer-replacement strategy */
847 if (!from_ring ||
848 strategy->buffers[strategy->current] != BufferDescriptorGetBuffer(buf))
849 return false;
850
851 /*
852 * Remove the dirty buffer from the ring; necessary to prevent infinite
853 * loop if all ring members are dirty.
854 */
855 strategy->buffers[strategy->current] = InvalidBuffer;
856
857 return true;
858}

References BAS_BULKREAD, BufferAccessStrategyData::btype, buf, BufferDescriptorGetBuffer(), BufferAccessStrategyData::buffers, BufferAccessStrategyData::current, and InvalidBuffer.

Referenced by GetVictimBuffer().

◆ StrategyShmemSize()

Size StrategyShmemSize ( void  )

Definition at line 453 of file freelist.c.

454{
455 Size size = 0;
456
457 /* size of lookup hash table ... see comment in StrategyInitialize */
459
460 /* size of the shared replacement strategy control block */
461 size = add_size(size, MAXALIGN(sizeof(BufferStrategyControl)));
462
463 return size;
464}
Size BufTableShmemSize(int size)
Definition: buf_table.c:41
#define MAXALIGN(LEN)
Definition: c.h:782
size_t Size
Definition: c.h:576
Size add_size(Size s1, Size s2)
Definition: shmem.c:493

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

Referenced by BufferManagerShmemSize().

◆ StrategySyncStart()

int StrategySyncStart ( uint32 complete_passes,
uint32 num_buf_alloc 
)

Definition at line 394 of file freelist.c.

395{
396 uint32 nextVictimBuffer;
397 int result;
398
401 result = nextVictimBuffer % NBuffers;
402
403 if (complete_passes)
404 {
405 *complete_passes = StrategyControl->completePasses;
406
407 /*
408 * Additionally add the number of wraparounds that happened before
409 * completePasses could be incremented. C.f. ClockSweepTick().
410 */
411 *complete_passes += nextVictimBuffer / NBuffers;
412 }
413
414 if (num_buf_alloc)
415 {
417 }
419 return result;
420}
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:239
static uint32 pg_atomic_exchange_u32(volatile pg_atomic_uint32 *ptr, uint32 newval)
Definition: atomics.h:330

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

Referenced by BgBufferSync().

Variable Documentation

◆ StrategyControl