PostgreSQL Source Code  git master
slru.h File Reference
#include "access/xlogdefs.h"
#include "storage/lwlock.h"
#include "storage/sync.h"
Include dependency graph for slru.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SlruSharedData
 
struct  SlruCtlData
 

Macros

#define SLRU_MAX_ALLOWED_BUFFERS   ((1024 * 1024 * 1024) / BLCKSZ)
 
#define SLRU_PAGES_PER_SEGMENT   32
 
#define SlruPagePrecedesUnitTests(ctl, per_page)   do {} while (0)
 

Typedefs

typedef struct SlruSharedData SlruSharedData
 
typedef SlruSharedDataSlruShared
 
typedef struct SlruCtlData SlruCtlData
 
typedef SlruCtlDataSlruCtl
 
typedef bool(* SlruScanCallback) (SlruCtl ctl, char *filename, int64 segpage, void *data)
 

Enumerations

enum  SlruPageStatus { SLRU_PAGE_EMPTY , SLRU_PAGE_READ_IN_PROGRESS , SLRU_PAGE_VALID , SLRU_PAGE_WRITE_IN_PROGRESS }
 

Functions

static LWLockSimpleLruGetBankLock (SlruCtl ctl, int64 pageno)
 
Size SimpleLruShmemSize (int nslots, int nlsns)
 
int SimpleLruAutotuneBuffers (int divisor, int max)
 
void SimpleLruInit (SlruCtl ctl, const char *name, int nslots, int nlsns, const char *subdir, int buffer_tranche_id, int bank_tranche_id, SyncRequestHandler sync_handler, bool long_segment_names)
 
int SimpleLruZeroPage (SlruCtl ctl, int64 pageno)
 
int SimpleLruReadPage (SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
 
int SimpleLruReadPage_ReadOnly (SlruCtl ctl, int64 pageno, TransactionId xid)
 
void SimpleLruWritePage (SlruCtl ctl, int slotno)
 
void SimpleLruWriteAll (SlruCtl ctl, bool allow_redirtied)
 
void SimpleLruTruncate (SlruCtl ctl, int64 cutoffPage)
 
bool SimpleLruDoesPhysicalPageExist (SlruCtl ctl, int64 pageno)
 
bool SlruScanDirectory (SlruCtl ctl, SlruScanCallback callback, void *data)
 
void SlruDeleteSegment (SlruCtl ctl, int64 segno)
 
int SlruSyncFileTag (SlruCtl ctl, const FileTag *ftag, char *path)
 
bool SlruScanDirCbReportPresence (SlruCtl ctl, char *filename, int64 segpage, void *data)
 
bool SlruScanDirCbDeleteAll (SlruCtl ctl, char *filename, int64 segpage, void *data)
 
bool check_slru_buffers (const char *name, int *newval)
 

Macro Definition Documentation

◆ SLRU_MAX_ALLOWED_BUFFERS

#define SLRU_MAX_ALLOWED_BUFFERS   ((1024 * 1024 * 1024) / BLCKSZ)

Definition at line 24 of file slru.h.

◆ SLRU_PAGES_PER_SEGMENT

#define SLRU_PAGES_PER_SEGMENT   32

Definition at line 39 of file slru.h.

◆ SlruPagePrecedesUnitTests

#define SlruPagePrecedesUnitTests (   ctl,
  per_page 
)    do {} while (0)

Definition at line 203 of file slru.h.

Typedef Documentation

◆ SlruCtl

typedef SlruCtlData* SlruCtl

Definition at line 170 of file slru.h.

◆ SlruCtlData

typedef struct SlruCtlData SlruCtlData

◆ SlruScanCallback

typedef bool(* SlruScanCallback) (SlruCtl ctl, char *filename, int64 segpage, void *data)

Definition at line 208 of file slru.h.

◆ SlruShared

Definition at line 121 of file slru.h.

◆ SlruSharedData

Enumeration Type Documentation

◆ SlruPageStatus

Enumerator
SLRU_PAGE_EMPTY 
SLRU_PAGE_READ_IN_PROGRESS 
SLRU_PAGE_VALID 
SLRU_PAGE_WRITE_IN_PROGRESS 

Definition at line 47 of file slru.h.

48 {
49  SLRU_PAGE_EMPTY, /* buffer is not in use */
50  SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
51  SLRU_PAGE_VALID, /* page is valid and not being written */
52  SLRU_PAGE_WRITE_IN_PROGRESS, /* page is being written out */
SlruPageStatus
Definition: slru.h:48
@ SLRU_PAGE_VALID
Definition: slru.h:51
@ SLRU_PAGE_WRITE_IN_PROGRESS
Definition: slru.h:52
@ SLRU_PAGE_READ_IN_PROGRESS
Definition: slru.h:50
@ SLRU_PAGE_EMPTY
Definition: slru.h:49

Function Documentation

◆ check_slru_buffers()

bool check_slru_buffers ( const char *  name,
int *  newval 
)

Definition at line 341 of file slru.c.

342 {
343  /* Valid values are multiples of SLRU_BANK_SIZE */
344  if (*newval % SLRU_BANK_SIZE == 0)
345  return true;
346 
347  GUC_check_errdetail("\"%s\" must be a multiple of %d", name,
349  return false;
350 }
#define newval
#define GUC_check_errdetail
Definition: guc.h:447
#define SLRU_BANK_SIZE
Definition: slru.c:129
const char * name

References GUC_check_errdetail, name, newval, and SLRU_BANK_SIZE.

Referenced by check_commit_ts_buffers(), check_multixact_member_buffers(), check_multixact_offset_buffers(), check_notify_buffers(), check_serial_buffers(), check_subtrans_buffers(), and check_transaction_buffers().

◆ SimpleLruAutotuneBuffers()

int SimpleLruAutotuneBuffers ( int  divisor,
int  max 
)

Definition at line 217 of file slru.c.

218 {
219  return Min(max - (max % SLRU_BANK_SIZE),
221  NBuffers / divisor - (NBuffers / divisor) % SLRU_BANK_SIZE));
222 }
#define Min(x, y)
Definition: c.h:991
#define Max(x, y)
Definition: c.h:985
int NBuffers
Definition: globals.c:139

References Max, Min, NBuffers, and SLRU_BANK_SIZE.

Referenced by CLOGShmemBuffers(), CommitTsShmemBuffers(), and SUBTRANSShmemBuffers().

◆ SimpleLruDoesPhysicalPageExist()

bool SimpleLruDoesPhysicalPageExist ( SlruCtl  ctl,
int64  pageno 
)

Definition at line 729 of file slru.c.

730 {
731  int64 segno = pageno / SLRU_PAGES_PER_SEGMENT;
732  int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
733  int offset = rpageno * BLCKSZ;
734  char path[MAXPGPATH];
735  int fd;
736  bool result;
737  off_t endpos;
738 
739  /* update the stats counter of checked pages */
740  pgstat_count_slru_page_exists(ctl->shared->slru_stats_idx);
741 
742  SlruFileName(ctl, path, segno);
743 
744  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
745  if (fd < 0)
746  {
747  /* expected: file doesn't exist */
748  if (errno == ENOENT)
749  return false;
750 
751  /* report error normally */
753  slru_errno = errno;
754  SlruReportIOError(ctl, pageno, 0);
755  }
756 
757  if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
758  {
760  slru_errno = errno;
761  SlruReportIOError(ctl, pageno, 0);
762  }
763 
764  result = endpos >= (off_t) (offset + BLCKSZ);
765 
766  if (CloseTransientFile(fd) != 0)
767  {
769  slru_errno = errno;
770  return false;
771  }
772 
773  return result;
774 }
#define PG_BINARY
Definition: c.h:1260
int CloseTransientFile(int fd)
Definition: fd.c:2809
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2633
#define MAXPGPATH
static XLogRecPtr endpos
Definition: pg_receivewal.c:56
void pgstat_count_slru_page_exists(int slru_idx)
Definition: pgstat_slru.c:71
static int fd(const char *x, int i)
Definition: preproc-init.c:105
tree ctl
Definition: radixtree.h:1807
static int SlruFileName(SlruCtl ctl, char *path, int64 segno)
Definition: slru.c:76
static void SlruReportIOError(SlruCtl ctl, int64 pageno, TransactionId xid)
Definition: slru.c:1031
static SlruErrorCause slru_errcause
Definition: slru.c:160
static int slru_errno
Definition: slru.c:161
@ SLRU_SEEK_FAILED
Definition: slru.c:153
@ SLRU_OPEN_FAILED
Definition: slru.c:152
@ SLRU_CLOSE_FAILED
Definition: slru.c:157
#define SLRU_PAGES_PER_SEGMENT
Definition: slru.h:39

References CloseTransientFile(), ctl, endpos, fd(), MAXPGPATH, OpenTransientFile(), PG_BINARY, pgstat_count_slru_page_exists(), SlruWriteAllData::segno, SLRU_CLOSE_FAILED, slru_errcause, slru_errno, SLRU_OPEN_FAILED, SLRU_PAGES_PER_SEGMENT, SLRU_SEEK_FAILED, SlruFileName(), and SlruReportIOError().

Referenced by ActivateCommitTs(), find_multixact_start(), MaybeExtendOffsetSlru(), and test_slru_page_exists().

◆ SimpleLruGetBankLock()

◆ SimpleLruInit()

void SimpleLruInit ( SlruCtl  ctl,
const char *  name,
int  nslots,
int  nlsns,
const char *  subdir,
int  buffer_tranche_id,
int  bank_tranche_id,
SyncRequestHandler  sync_handler,
bool  long_segment_names 
)

Definition at line 238 of file slru.c.

241 {
242  SlruShared shared;
243  bool found;
244  int nbanks = nslots / SLRU_BANK_SIZE;
245 
246  Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
247 
248  shared = (SlruShared) ShmemInitStruct(name,
249  SimpleLruShmemSize(nslots, nlsns),
250  &found);
251 
252  if (!IsUnderPostmaster)
253  {
254  /* Initialize locks and shared memory area */
255  char *ptr;
256  Size offset;
257 
258  Assert(!found);
259 
260  memset(shared, 0, sizeof(SlruSharedData));
261 
262  shared->num_slots = nslots;
263  shared->lsn_groups_per_page = nlsns;
264 
266 
268 
269  ptr = (char *) shared;
270  offset = MAXALIGN(sizeof(SlruSharedData));
271  shared->page_buffer = (char **) (ptr + offset);
272  offset += MAXALIGN(nslots * sizeof(char *));
273  shared->page_status = (SlruPageStatus *) (ptr + offset);
274  offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
275  shared->page_dirty = (bool *) (ptr + offset);
276  offset += MAXALIGN(nslots * sizeof(bool));
277  shared->page_number = (int64 *) (ptr + offset);
278  offset += MAXALIGN(nslots * sizeof(int64));
279  shared->page_lru_count = (int *) (ptr + offset);
280  offset += MAXALIGN(nslots * sizeof(int));
281 
282  /* Initialize LWLocks */
283  shared->buffer_locks = (LWLockPadded *) (ptr + offset);
284  offset += MAXALIGN(nslots * sizeof(LWLockPadded));
285  shared->bank_locks = (LWLockPadded *) (ptr + offset);
286  offset += MAXALIGN(nbanks * sizeof(LWLockPadded));
287  shared->bank_cur_lru_count = (int *) (ptr + offset);
288  offset += MAXALIGN(nbanks * sizeof(int));
289 
290  if (nlsns > 0)
291  {
292  shared->group_lsn = (XLogRecPtr *) (ptr + offset);
293  offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
294  }
295 
296  ptr += BUFFERALIGN(offset);
297  for (int slotno = 0; slotno < nslots; slotno++)
298  {
299  LWLockInitialize(&shared->buffer_locks[slotno].lock,
300  buffer_tranche_id);
301 
302  shared->page_buffer[slotno] = ptr;
303  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
304  shared->page_dirty[slotno] = false;
305  shared->page_lru_count[slotno] = 0;
306  ptr += BLCKSZ;
307  }
308 
309  /* Initialize the slot banks. */
310  for (int bankno = 0; bankno < nbanks; bankno++)
311  {
312  LWLockInitialize(&shared->bank_locks[bankno].lock, bank_tranche_id);
313  shared->bank_cur_lru_count[bankno] = 0;
314  }
315 
316  /* Should fit to estimated shmem size */
317  Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
318  }
319  else
320  {
321  Assert(found);
322  Assert(shared->num_slots == nslots);
323  }
324 
325  /*
326  * Initialize the unshared control struct, including directory path. We
327  * assume caller set PagePrecedes.
328  */
329  ctl->shared = shared;
330  ctl->sync_handler = sync_handler;
331  ctl->long_segment_names = long_segment_names;
332  ctl->bank_mask = (nslots / SLRU_BANK_SIZE) - 1;
333  strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
334 }
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:448
#define MAXALIGN(LEN)
Definition: c.h:798
#define BUFFERALIGN(LEN)
Definition: c.h:800
size_t Size
Definition: c.h:592
bool IsUnderPostmaster
Definition: globals.c:117
Assert(fmt[strlen(fmt) - 1] !='\n')
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:708
int pgstat_get_slru_index(const char *name)
Definition: pgstat_slru.c:132
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
Size SimpleLruShmemSize(int nslots, int nlsns)
Definition: slru.c:184
SlruSharedData * SlruShared
Definition: slru.h:121
#define SLRU_MAX_ALLOWED_BUFFERS
Definition: slru.h:24
int slru_stats_idx
Definition: slru.h:118
int64 * page_number
Definition: slru.h:73
int num_slots
Definition: slru.h:64
LWLockPadded * bank_locks
Definition: slru.h:80
int * page_lru_count
Definition: slru.h:74
pg_atomic_uint64 latest_page_number
Definition: slru.h:115
XLogRecPtr * group_lsn
Definition: slru.h:107
int * bank_cur_lru_count
Definition: slru.h:97
int lsn_groups_per_page
Definition: slru.h:108
SlruPageStatus * page_status
Definition: slru.h:71
bool * page_dirty
Definition: slru.h:72
LWLockPadded * buffer_locks
Definition: slru.h:77
char ** page_buffer
Definition: slru.h:70
LWLock lock
Definition: lwlock.h:70
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References Assert(), SlruSharedData::bank_cur_lru_count, SlruSharedData::bank_locks, SlruSharedData::buffer_locks, BUFFERALIGN, ctl, SlruSharedData::group_lsn, IsUnderPostmaster, SlruSharedData::latest_page_number, LWLockPadded::lock, SlruSharedData::lsn_groups_per_page, LWLockInitialize(), MAXALIGN, name, SlruSharedData::num_slots, SlruSharedData::page_buffer, SlruSharedData::page_dirty, SlruSharedData::page_lru_count, SlruSharedData::page_number, SlruSharedData::page_status, pg_atomic_init_u64(), pgstat_get_slru_index(), ShmemInitStruct(), SimpleLruShmemSize(), SLRU_BANK_SIZE, SLRU_MAX_ALLOWED_BUFFERS, SLRU_PAGE_EMPTY, SlruSharedData::slru_stats_idx, and strlcpy().

Referenced by AsyncShmemInit(), CLOGShmemInit(), CommitTsShmemInit(), MultiXactShmemInit(), SerialInit(), SUBTRANSShmemInit(), and test_slru_shmem_startup().

◆ SimpleLruReadPage()

int SimpleLruReadPage ( SlruCtl  ctl,
int64  pageno,
bool  write_ok,
TransactionId  xid 
)

Definition at line 488 of file slru.c.

490 {
491  SlruShared shared = ctl->shared;
492  LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
493 
495 
496  /* Outer loop handles restart if we must wait for someone else's I/O */
497  for (;;)
498  {
499  int slotno;
500  bool ok;
501 
502  /* See if page already is in memory; if not, pick victim slot */
503  slotno = SlruSelectLRUPage(ctl, pageno);
504 
505  /* Did we find the page in memory? */
506  if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
507  shared->page_number[slotno] == pageno)
508  {
509  /*
510  * If page is still being read in, we must wait for I/O. Likewise
511  * if the page is being written and the caller said that's not OK.
512  */
513  if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
514  (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
515  !write_ok))
516  {
517  SimpleLruWaitIO(ctl, slotno);
518  /* Now we must recheck state from the top */
519  continue;
520  }
521  /* Otherwise, it's ready to use */
522  SlruRecentlyUsed(shared, slotno);
523 
524  /* update the stats counter of pages found in the SLRU */
526 
527  return slotno;
528  }
529 
530  /* We found no match; assert we selected a freeable slot */
531  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
532  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
533  !shared->page_dirty[slotno]));
534 
535  /* Mark the slot read-busy */
536  shared->page_number[slotno] = pageno;
537  shared->page_status[slotno] = SLRU_PAGE_READ_IN_PROGRESS;
538  shared->page_dirty[slotno] = false;
539 
540  /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
541  LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
542 
543  /* Release bank lock while doing I/O */
544  LWLockRelease(banklock);
545 
546  /* Do the read */
547  ok = SlruPhysicalReadPage(ctl, pageno, slotno);
548 
549  /* Set the LSNs for this newly read-in page to zero */
550  SimpleLruZeroLSNs(ctl, slotno);
551 
552  /* Re-acquire bank control lock and update page state */
553  LWLockAcquire(banklock, LW_EXCLUSIVE);
554 
555  Assert(shared->page_number[slotno] == pageno &&
556  shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
557  !shared->page_dirty[slotno]);
558 
559  shared->page_status[slotno] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY;
560 
561  LWLockRelease(&shared->buffer_locks[slotno].lock);
562 
563  /* Now it's okay to ereport if we failed */
564  if (!ok)
565  SlruReportIOError(ctl, pageno, xid);
566 
567  SlruRecentlyUsed(shared, slotno);
568 
569  /* update the stats counter of pages not found in SLRU */
571 
572  return slotno;
573  }
574 }
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1169
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1938
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1782
@ LW_EXCLUSIVE
Definition: lwlock.h:114
void pgstat_count_slru_page_read(int slru_idx)
Definition: pgstat_slru.c:77
void pgstat_count_slru_page_hit(int slru_idx)
Definition: pgstat_slru.c:65
static bool SlruPhysicalReadPage(SlruCtl ctl, int64 pageno, int slotno)
Definition: slru.c:787
static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
Definition: slru.c:414
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:431
static int SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
Definition: slru.c:1152
static void SlruRecentlyUsed(SlruShared shared, int slotno)
Definition: slru.c:1106
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition: slru.h:179
Definition: lwlock.h:42

References Assert(), SlruSharedData::buffer_locks, ctl, LWLockPadded::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockHeldByMeInMode(), LWLockRelease(), SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, pgstat_count_slru_page_hit(), pgstat_count_slru_page_read(), SimpleLruGetBankLock(), SimpleLruWaitIO(), SimpleLruZeroLSNs(), SLRU_PAGE_EMPTY, SLRU_PAGE_READ_IN_PROGRESS, SLRU_PAGE_VALID, SLRU_PAGE_WRITE_IN_PROGRESS, SlruSharedData::slru_stats_idx, SlruPhysicalReadPage(), SlruRecentlyUsed(), SlruReportIOError(), and SlruSelectLRUPage().

Referenced by asyncQueueAddEntries(), GetMultiXactIdMembers(), RecordNewMultiXact(), SerialAdd(), SetXidCommitTsInPage(), SimpleLruReadPage_ReadOnly(), SubTransSetParent(), test_slru_page_read(), TransactionIdSetPageStatusInternal(), TrimCLOG(), and TrimMultiXact().

◆ SimpleLruReadPage_ReadOnly()

int SimpleLruReadPage_ReadOnly ( SlruCtl  ctl,
int64  pageno,
TransactionId  xid 
)

Definition at line 591 of file slru.c.

592 {
593  SlruShared shared = ctl->shared;
594  LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
595  int bankno = pageno & ctl->bank_mask;
596  int bankstart = bankno * SLRU_BANK_SIZE;
597  int bankend = bankstart + SLRU_BANK_SIZE;
598 
599  /* Try to find the page while holding only shared lock */
600  LWLockAcquire(banklock, LW_SHARED);
601 
602  /* See if page is already in a buffer */
603  for (int slotno = bankstart; slotno < bankend; slotno++)
604  {
605  if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
606  shared->page_number[slotno] == pageno &&
607  shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
608  {
609  /* See comments for SlruRecentlyUsed macro */
610  SlruRecentlyUsed(shared, slotno);
611 
612  /* update the stats counter of pages found in the SLRU */
614 
615  return slotno;
616  }
617  }
618 
619  /* No luck, so switch to normal exclusive lock and do regular read */
620  LWLockRelease(banklock);
621  LWLockAcquire(banklock, LW_EXCLUSIVE);
622 
623  return SimpleLruReadPage(ctl, pageno, true, xid);
624 }
@ LW_SHARED
Definition: lwlock.h:115
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition: slru.c:488

References ctl, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), SlruSharedData::page_number, SlruSharedData::page_status, pgstat_count_slru_page_hit(), SimpleLruGetBankLock(), SimpleLruReadPage(), SLRU_BANK_SIZE, SLRU_PAGE_EMPTY, SLRU_PAGE_READ_IN_PROGRESS, SlruSharedData::slru_stats_idx, and SlruRecentlyUsed().

Referenced by asyncQueueReadAllNotifications(), find_multixact_start(), SerialGetMinConflictCommitSeqNo(), SubTransGetParent(), test_slru_page_readonly(), TransactionIdGetCommitTsData(), and TransactionIdGetStatus().

◆ SimpleLruShmemSize()

Size SimpleLruShmemSize ( int  nslots,
int  nlsns 
)

Definition at line 184 of file slru.c.

185 {
186  int nbanks = nslots / SLRU_BANK_SIZE;
187  Size sz;
188 
189  Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
190  Assert(nslots % SLRU_BANK_SIZE == 0);
191 
192  /* we assume nslots isn't so large as to risk overflow */
193  sz = MAXALIGN(sizeof(SlruSharedData));
194  sz += MAXALIGN(nslots * sizeof(char *)); /* page_buffer[] */
195  sz += MAXALIGN(nslots * sizeof(SlruPageStatus)); /* page_status[] */
196  sz += MAXALIGN(nslots * sizeof(bool)); /* page_dirty[] */
197  sz += MAXALIGN(nslots * sizeof(int64)); /* page_number[] */
198  sz += MAXALIGN(nslots * sizeof(int)); /* page_lru_count[] */
199  sz += MAXALIGN(nslots * sizeof(LWLockPadded)); /* buffer_locks[] */
200  sz += MAXALIGN(nbanks * sizeof(LWLockPadded)); /* bank_locks[] */
201  sz += MAXALIGN(nbanks * sizeof(int)); /* bank_cur_lru_count[] */
202 
203  if (nlsns > 0)
204  sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr)); /* group_lsn[] */
205 
206  return BUFFERALIGN(sz) + BLCKSZ * nslots;
207 }

References Assert(), BUFFERALIGN, MAXALIGN, SLRU_BANK_SIZE, and SLRU_MAX_ALLOWED_BUFFERS.

Referenced by AsyncShmemSize(), CLOGShmemSize(), CommitTsShmemSize(), MultiXactShmemSize(), PredicateLockShmemSize(), SimpleLruInit(), SUBTRANSShmemSize(), and test_slru_shmem_request().

◆ SimpleLruTruncate()

void SimpleLruTruncate ( SlruCtl  ctl,
int64  cutoffPage 
)

Definition at line 1391 of file slru.c.

1392 {
1393  SlruShared shared = ctl->shared;
1394  int prevbank;
1395 
1396  /* update the stats counter of truncates */
1398 
1399  /*
1400  * Scan shared memory and remove any pages preceding the cutoff page, to
1401  * ensure we won't rewrite them later. (Since this is normally called in
1402  * or just after a checkpoint, any dirty pages should have been flushed
1403  * already ... we're just being extra careful here.)
1404  */
1405 restart:
1406 
1407  /*
1408  * An important safety check: the current endpoint page must not be
1409  * eligible for removal. This check is just a backstop against wraparound
1410  * bugs elsewhere in SLRU handling, so we don't care if we read a slightly
1411  * outdated value; therefore we don't add a memory barrier.
1412  */
1413  if (ctl->PagePrecedes(pg_atomic_read_u64(&shared->latest_page_number),
1414  cutoffPage))
1415  {
1416  ereport(LOG,
1417  (errmsg("could not truncate directory \"%s\": apparent wraparound",
1418  ctl->Dir)));
1419  return;
1420  }
1421 
1422  prevbank = SlotGetBankNumber(0);
1423  LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
1424  for (int slotno = 0; slotno < shared->num_slots; slotno++)
1425  {
1426  int curbank = SlotGetBankNumber(slotno);
1427 
1428  /*
1429  * If the current bank lock is not same as the previous bank lock then
1430  * release the previous lock and acquire the new lock.
1431  */
1432  if (curbank != prevbank)
1433  {
1434  LWLockRelease(&shared->bank_locks[prevbank].lock);
1435  LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
1436  prevbank = curbank;
1437  }
1438 
1439  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1440  continue;
1441  if (!ctl->PagePrecedes(shared->page_number[slotno], cutoffPage))
1442  continue;
1443 
1444  /*
1445  * If page is clean, just change state to EMPTY (expected case).
1446  */
1447  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1448  !shared->page_dirty[slotno])
1449  {
1450  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1451  continue;
1452  }
1453 
1454  /*
1455  * Hmm, we have (or may have) I/O operations acting on the page, so
1456  * we've got to wait for them to finish and then start again. This is
1457  * the same logic as in SlruSelectLRUPage. (XXX if page is dirty,
1458  * wouldn't it be OK to just discard it without writing it?
1459  * SlruMayDeleteSegment() uses a stricter qualification, so we might
1460  * not delete this page in the end; even if we don't delete it, we
1461  * won't have cause to read its data again. For now, keep the logic
1462  * the same as it was.)
1463  */
1464  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1465  SlruInternalWritePage(ctl, slotno, NULL);
1466  else
1467  SimpleLruWaitIO(ctl, slotno);
1468 
1469  LWLockRelease(&shared->bank_locks[prevbank].lock);
1470  goto restart;
1471  }
1472 
1473  LWLockRelease(&shared->bank_locks[prevbank].lock);
1474 
1475  /* Now we can remove the old segment(s) */
1476  (void) SlruScanDirectory(ctl, SlruScanDirCbDeleteCutoff, &cutoffPage);
1477 }
static uint64 pg_atomic_read_u64(volatile pg_atomic_uint64 *ptr)
Definition: atomics.h:462
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
void pgstat_count_slru_truncate(int slru_idx)
Definition: pgstat_slru.c:95
static void SlruInternalWritePage(SlruCtl ctl, int slotno, SlruWriteAll fdata)
Definition: slru.c:638
bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data)
Definition: slru.c:1774
#define SlotGetBankNumber(slotno)
Definition: slru.c:134
static bool SlruScanDirCbDeleteCutoff(SlruCtl ctl, char *filename, int64 segpage, void *data)
Definition: slru.c:1711

References SlruSharedData::bank_locks, ctl, ereport, errmsg(), SlruSharedData::latest_page_number, LWLockPadded::lock, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, pg_atomic_read_u64(), pgstat_count_slru_truncate(), SimpleLruWaitIO(), SlotGetBankNumber, SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SlruSharedData::slru_stats_idx, SlruInternalWritePage(), SlruScanDirCbDeleteCutoff(), and SlruScanDirectory().

Referenced by asyncQueueAdvanceTail(), CheckPointPredicate(), clog_redo(), commit_ts_redo(), PerformOffsetsTruncation(), test_slru_page_truncate(), TruncateCLOG(), TruncateCommitTs(), and TruncateSUBTRANS().

◆ SimpleLruWriteAll()

void SimpleLruWriteAll ( SlruCtl  ctl,
bool  allow_redirtied 
)

Definition at line 1305 of file slru.c.

1306 {
1307  SlruShared shared = ctl->shared;
1308  SlruWriteAllData fdata;
1309  int64 pageno = 0;
1310  int prevbank = SlotGetBankNumber(0);
1311  bool ok;
1312 
1313  /* update the stats counter of flushes */
1315 
1316  /*
1317  * Find and write dirty pages
1318  */
1319  fdata.num_files = 0;
1320 
1321  LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
1322 
1323  for (int slotno = 0; slotno < shared->num_slots; slotno++)
1324  {
1325  int curbank = SlotGetBankNumber(slotno);
1326 
1327  /*
1328  * If the current bank lock is not same as the previous bank lock then
1329  * release the previous lock and acquire the new lock.
1330  */
1331  if (curbank != prevbank)
1332  {
1333  LWLockRelease(&shared->bank_locks[prevbank].lock);
1334  LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
1335  prevbank = curbank;
1336  }
1337 
1338  /* Do nothing if slot is unused */
1339  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1340  continue;
1341 
1342  SlruInternalWritePage(ctl, slotno, &fdata);
1343 
1344  /*
1345  * In some places (e.g. checkpoints), we cannot assert that the slot
1346  * is clean now, since another process might have re-dirtied it
1347  * already. That's okay.
1348  */
1349  Assert(allow_redirtied ||
1350  shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
1351  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1352  !shared->page_dirty[slotno]));
1353  }
1354 
1355  LWLockRelease(&shared->bank_locks[prevbank].lock);
1356 
1357  /*
1358  * Now close any files that were open
1359  */
1360  ok = true;
1361  for (int i = 0; i < fdata.num_files; i++)
1362  {
1363  if (CloseTransientFile(fdata.fd[i]) != 0)
1364  {
1366  slru_errno = errno;
1367  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1368  ok = false;
1369  }
1370  }
1371  if (!ok)
1373 
1374  /* Ensure that directory entries for new files are on disk. */
1375  if (ctl->sync_handler != SYNC_HANDLER_NONE)
1376  fsync_fname(ctl->Dir, true);
1377 }
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:756
int i
Definition: isn.c:73
void pgstat_count_slru_flush(int slru_idx)
Definition: pgstat_slru.c:89
int num_files
Definition: slru.c:113
int fd[MAX_WRITEALL_BUFFERS]
Definition: slru.c:114
int64 segno[MAX_WRITEALL_BUFFERS]
Definition: slru.c:115
@ SYNC_HANDLER_NONE
Definition: sync.h:42
#define InvalidTransactionId
Definition: transam.h:31

References Assert(), SlruSharedData::bank_locks, CloseTransientFile(), ctl, SlruWriteAllData::fd, fsync_fname(), i, InvalidTransactionId, LWLockPadded::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruWriteAllData::num_files, SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_status, pgstat_count_slru_flush(), SlruWriteAllData::segno, SlotGetBankNumber, SLRU_CLOSE_FAILED, slru_errcause, slru_errno, SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SLRU_PAGES_PER_SEGMENT, SlruSharedData::slru_stats_idx, SlruInternalWritePage(), SlruReportIOError(), and SYNC_HANDLER_NONE.

Referenced by CheckPointCLOG(), CheckPointCommitTs(), CheckPointMultiXact(), CheckPointPredicate(), CheckPointSUBTRANS(), find_multixact_start(), and test_slru_page_writeall().

◆ SimpleLruWritePage()

void SimpleLruWritePage ( SlruCtl  ctl,
int  slotno 
)

◆ SimpleLruZeroPage()

int SimpleLruZeroPage ( SlruCtl  ctl,
int64  pageno 
)

Definition at line 361 of file slru.c.

362 {
363  SlruShared shared = ctl->shared;
364  int slotno;
365 
367 
368  /* Find a suitable buffer slot for the page */
369  slotno = SlruSelectLRUPage(ctl, pageno);
370  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
371  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
372  !shared->page_dirty[slotno]) ||
373  shared->page_number[slotno] == pageno);
374 
375  /* Mark the slot as containing this page */
376  shared->page_number[slotno] = pageno;
377  shared->page_status[slotno] = SLRU_PAGE_VALID;
378  shared->page_dirty[slotno] = true;
379  SlruRecentlyUsed(shared, slotno);
380 
381  /* Set the buffer to zeroes */
382  MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
383 
384  /* Set the LSNs for this new page to zero */
385  SimpleLruZeroLSNs(ctl, slotno);
386 
387  /*
388  * Assume this page is now the latest active page.
389  *
390  * Note that because both this routine and SlruSelectLRUPage run with
391  * ControlLock held, it is not possible for this to be zeroing a page that
392  * SlruSelectLRUPage is going to evict simultaneously. Therefore, there's
393  * no memory barrier here.
394  */
395  pg_atomic_write_u64(&shared->latest_page_number, pageno);
396 
397  /* update the stats counter of zeroed pages */
399 
400  return slotno;
401 }
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:480
#define MemSet(start, val, len)
Definition: c.h:1007
void pgstat_count_slru_page_zeroed(int slru_idx)
Definition: pgstat_slru.c:59

References Assert(), ctl, SlruSharedData::latest_page_number, LW_EXCLUSIVE, LWLockHeldByMeInMode(), MemSet, SlruSharedData::page_buffer, SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, pg_atomic_write_u64(), pgstat_count_slru_page_zeroed(), SimpleLruGetBankLock(), SimpleLruZeroLSNs(), SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SlruSharedData::slru_stats_idx, SlruRecentlyUsed(), and SlruSelectLRUPage().

Referenced by asyncQueueAddEntries(), SerialAdd(), test_slru_page_write(), ZeroCLOGPage(), ZeroCommitTsPage(), ZeroMultiXactMemberPage(), ZeroMultiXactOffsetPage(), and ZeroSUBTRANSPage().

◆ SlruDeleteSegment()

void SlruDeleteSegment ( SlruCtl  ctl,
int64  segno 
)

Definition at line 1509 of file slru.c.

1510 {
1511  SlruShared shared = ctl->shared;
1512  int prevbank = SlotGetBankNumber(0);
1513  bool did_write;
1514 
1515  /* Clean out any possibly existing references to the segment. */
1516  LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
1517 restart:
1518  did_write = false;
1519  for (int slotno = 0; slotno < shared->num_slots; slotno++)
1520  {
1521  int pagesegno;
1522  int curbank = SlotGetBankNumber(slotno);
1523 
1524  /*
1525  * If the current bank lock is not same as the previous bank lock then
1526  * release the previous lock and acquire the new lock.
1527  */
1528  if (curbank != prevbank)
1529  {
1530  LWLockRelease(&shared->bank_locks[prevbank].lock);
1531  LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
1532  prevbank = curbank;
1533  }
1534 
1535  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1536  continue;
1537 
1538  pagesegno = shared->page_number[slotno] / SLRU_PAGES_PER_SEGMENT;
1539  /* not the segment we're looking for */
1540  if (pagesegno != segno)
1541  continue;
1542 
1543  /* If page is clean, just change state to EMPTY (expected case). */
1544  if (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1545  !shared->page_dirty[slotno])
1546  {
1547  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
1548  continue;
1549  }
1550 
1551  /* Same logic as SimpleLruTruncate() */
1552  if (shared->page_status[slotno] == SLRU_PAGE_VALID)
1553  SlruInternalWritePage(ctl, slotno, NULL);
1554  else
1555  SimpleLruWaitIO(ctl, slotno);
1556 
1557  did_write = true;
1558  }
1559 
1560  /*
1561  * Be extra careful and re-check. The IO functions release the control
1562  * lock, so new pages could have been read in.
1563  */
1564  if (did_write)
1565  goto restart;
1566 
1568 
1569  LWLockRelease(&shared->bank_locks[prevbank].lock);
1570 }
static void SlruInternalDeleteSegment(SlruCtl ctl, int64 segno)
Definition: slru.c:1486

References SlruSharedData::bank_locks, ctl, LWLockPadded::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SlruSharedData::num_slots, SlruSharedData::page_dirty, SlruSharedData::page_number, SlruSharedData::page_status, SlruWriteAllData::segno, SimpleLruWaitIO(), SlotGetBankNumber, SLRU_PAGE_EMPTY, SLRU_PAGE_VALID, SLRU_PAGES_PER_SEGMENT, SlruInternalDeleteSegment(), and SlruInternalWritePage().

Referenced by PerformMembersTruncation(), and test_slru_page_delete().

◆ SlruScanDirCbDeleteAll()

bool SlruScanDirCbDeleteAll ( SlruCtl  ctl,
char *  filename,
int64  segpage,
void *  data 
)

Definition at line 1727 of file slru.c.

1728 {
1730 
1731  return false; /* keep going */
1732 }

References ctl, SLRU_PAGES_PER_SEGMENT, and SlruInternalDeleteSegment().

Referenced by AsyncShmemInit(), DeactivateCommitTs(), and test_slru_scan_cb().

◆ SlruScanDirCbReportPresence()

bool SlruScanDirCbReportPresence ( SlruCtl  ctl,
char *  filename,
int64  segpage,
void *  data 
)

Definition at line 1695 of file slru.c.

1697 {
1698  int64 cutoffPage = *(int64 *) data;
1699 
1700  if (SlruMayDeleteSegment(ctl, segpage, cutoffPage))
1701  return true; /* found one; don't iterate any more */
1702 
1703  return false; /* keep going */
1704 }
const void * data
static bool SlruMayDeleteSegment(SlruCtl ctl, int64 segpage, int64 cutoffPage)
Definition: slru.c:1586

References ctl, data, and SlruMayDeleteSegment().

Referenced by TruncateCLOG(), and TruncateCommitTs().

◆ SlruScanDirectory()

bool SlruScanDirectory ( SlruCtl  ctl,
SlruScanCallback  callback,
void *  data 
)

Definition at line 1774 of file slru.c.

1775 {
1776  bool retval = false;
1777  DIR *cldir;
1778  struct dirent *clde;
1779  int64 segno;
1780  int64 segpage;
1781 
1782  cldir = AllocateDir(ctl->Dir);
1783  while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1784  {
1785  size_t len;
1786 
1787  len = strlen(clde->d_name);
1788 
1790  strspn(clde->d_name, "0123456789ABCDEF") == len)
1791  {
1792  segno = strtoi64(clde->d_name, NULL, 16);
1793  segpage = segno * SLRU_PAGES_PER_SEGMENT;
1794 
1795  elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1796  ctl->Dir, clde->d_name);
1797  retval = callback(ctl, clde->d_name, segpage, data);
1798  if (retval)
1799  break;
1800  }
1801  }
1802  FreeDir(cldir);
1803 
1804  return retval;
1805 }
#define strtoi64(str, endptr, base)
Definition: c.h:1284
#define DEBUG2
Definition: elog.h:29
#define elog(elevel,...)
Definition: elog.h:224
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2909
int FreeDir(DIR *dir)
Definition: fd.c:2961
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2843
const void size_t len
static bool SlruCorrectSegmentFilenameLength(SlruCtl ctl, size_t len)
Definition: slru.c:1741
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:46

References AllocateDir(), callback(), ctl, dirent::d_name, data, DEBUG2, elog, FreeDir(), len, ReadDir(), SLRU_PAGES_PER_SEGMENT, SlruCorrectSegmentFilenameLength(), and strtoi64.

Referenced by AsyncShmemInit(), DeactivateCommitTs(), SimpleLruTruncate(), test_slru_delete_all(), TruncateCLOG(), TruncateCommitTs(), and TruncateMultiXact().

◆ SlruSyncFileTag()

int SlruSyncFileTag ( SlruCtl  ctl,
const FileTag ftag,
char *  path 
)

Definition at line 1814 of file slru.c.

1815 {
1816  int fd;
1817  int save_errno;
1818  int result;
1819 
1820  SlruFileName(ctl, path, ftag->segno);
1821 
1822  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1823  if (fd < 0)
1824  return -1;
1825 
1826  pgstat_report_wait_start(WAIT_EVENT_SLRU_FLUSH_SYNC);
1827  result = pg_fsync(fd);
1829  save_errno = errno;
1830 
1832 
1833  errno = save_errno;
1834  return result;
1835 }
int pg_fsync(int fd)
Definition: fd.c:386
uint64 segno
Definition: sync.h:55
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:88
static void pgstat_report_wait_end(void)
Definition: wait_event.h:104

References CloseTransientFile(), ctl, fd(), OpenTransientFile(), PG_BINARY, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), FileTag::segno, and SlruFileName().

Referenced by clogsyncfiletag(), committssyncfiletag(), multixactmemberssyncfiletag(), multixactoffsetssyncfiletag(), and test_slru_page_sync().