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 202 of file slru.h.

Typedef Documentation

◆ SlruCtl

typedef SlruCtlData* SlruCtl

Definition at line 169 of file slru.h.

◆ SlruCtlData

typedef struct SlruCtlData SlruCtlData

◆ SlruScanCallback

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

Definition at line 207 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 355 of file slru.c.

356 {
357  /* Valid values are multiples of SLRU_BANK_SIZE */
358  if (*newval % SLRU_BANK_SIZE == 0)
359  return true;
360 
361  GUC_check_errdetail("\"%s\" must be a multiple of %d", name,
363  return false;
364 }
#define newval
#define GUC_check_errdetail
Definition: guc.h:472
#define SLRU_BANK_SIZE
Definition: slru.c:144
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 232 of file slru.c.

233 {
234  return Min(max - (max % SLRU_BANK_SIZE),
236  NBuffers / divisor - (NBuffers / divisor) % SLRU_BANK_SIZE));
237 }
#define Min(x, y)
Definition: c.h:1004
#define Max(x, y)
Definition: c.h:998
int NBuffers
Definition: globals.c:140

References Max, Min, NBuffers, and SLRU_BANK_SIZE.

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

◆ SimpleLruDoesPhysicalPageExist()

bool SimpleLruDoesPhysicalPageExist ( SlruCtl  ctl,
int64  pageno 
)

Definition at line 743 of file slru.c.

744 {
745  int64 segno = pageno / SLRU_PAGES_PER_SEGMENT;
746  int rpageno = pageno % SLRU_PAGES_PER_SEGMENT;
747  int offset = rpageno * BLCKSZ;
748  char path[MAXPGPATH];
749  int fd;
750  bool result;
751  off_t endpos;
752 
753  /* update the stats counter of checked pages */
754  pgstat_count_slru_page_exists(ctl->shared->slru_stats_idx);
755 
756  SlruFileName(ctl, path, segno);
757 
758  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
759  if (fd < 0)
760  {
761  /* expected: file doesn't exist */
762  if (errno == ENOENT)
763  return false;
764 
765  /* report error normally */
767  slru_errno = errno;
768  SlruReportIOError(ctl, pageno, 0);
769  }
770 
771  if ((endpos = lseek(fd, 0, SEEK_END)) < 0)
772  {
774  slru_errno = errno;
775  SlruReportIOError(ctl, pageno, 0);
776  }
777 
778  result = endpos >= (off_t) (offset + BLCKSZ);
779 
780  if (CloseTransientFile(fd) != 0)
781  {
783  slru_errno = errno;
784  return false;
785  }
786 
787  return result;
788 }
#define PG_BINARY
Definition: c.h:1273
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:1853
static int SlruFileName(SlruCtl ctl, char *path, int64 segno)
Definition: slru.c:91
static void SlruReportIOError(SlruCtl ctl, int64 pageno, TransactionId xid)
Definition: slru.c:1045
static SlruErrorCause slru_errcause
Definition: slru.c:175
static int slru_errno
Definition: slru.c:176
@ SLRU_SEEK_FAILED
Definition: slru.c:168
@ SLRU_OPEN_FAILED
Definition: slru.c:167
@ SLRU_CLOSE_FAILED
Definition: slru.c:172
#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 252 of file slru.c.

255 {
256  SlruShared shared;
257  bool found;
258  int nbanks = nslots / SLRU_BANK_SIZE;
259 
260  Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
261 
262  shared = (SlruShared) ShmemInitStruct(name,
263  SimpleLruShmemSize(nslots, nlsns),
264  &found);
265 
266  if (!IsUnderPostmaster)
267  {
268  /* Initialize locks and shared memory area */
269  char *ptr;
270  Size offset;
271 
272  Assert(!found);
273 
274  memset(shared, 0, sizeof(SlruSharedData));
275 
276  shared->num_slots = nslots;
277  shared->lsn_groups_per_page = nlsns;
278 
280 
282 
283  ptr = (char *) shared;
284  offset = MAXALIGN(sizeof(SlruSharedData));
285  shared->page_buffer = (char **) (ptr + offset);
286  offset += MAXALIGN(nslots * sizeof(char *));
287  shared->page_status = (SlruPageStatus *) (ptr + offset);
288  offset += MAXALIGN(nslots * sizeof(SlruPageStatus));
289  shared->page_dirty = (bool *) (ptr + offset);
290  offset += MAXALIGN(nslots * sizeof(bool));
291  shared->page_number = (int64 *) (ptr + offset);
292  offset += MAXALIGN(nslots * sizeof(int64));
293  shared->page_lru_count = (int *) (ptr + offset);
294  offset += MAXALIGN(nslots * sizeof(int));
295 
296  /* Initialize LWLocks */
297  shared->buffer_locks = (LWLockPadded *) (ptr + offset);
298  offset += MAXALIGN(nslots * sizeof(LWLockPadded));
299  shared->bank_locks = (LWLockPadded *) (ptr + offset);
300  offset += MAXALIGN(nbanks * sizeof(LWLockPadded));
301  shared->bank_cur_lru_count = (int *) (ptr + offset);
302  offset += MAXALIGN(nbanks * sizeof(int));
303 
304  if (nlsns > 0)
305  {
306  shared->group_lsn = (XLogRecPtr *) (ptr + offset);
307  offset += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr));
308  }
309 
310  ptr += BUFFERALIGN(offset);
311  for (int slotno = 0; slotno < nslots; slotno++)
312  {
313  LWLockInitialize(&shared->buffer_locks[slotno].lock,
314  buffer_tranche_id);
315 
316  shared->page_buffer[slotno] = ptr;
317  shared->page_status[slotno] = SLRU_PAGE_EMPTY;
318  shared->page_dirty[slotno] = false;
319  shared->page_lru_count[slotno] = 0;
320  ptr += BLCKSZ;
321  }
322 
323  /* Initialize the slot banks. */
324  for (int bankno = 0; bankno < nbanks; bankno++)
325  {
326  LWLockInitialize(&shared->bank_locks[bankno].lock, bank_tranche_id);
327  shared->bank_cur_lru_count[bankno] = 0;
328  }
329 
330  /* Should fit to estimated shmem size */
331  Assert(ptr - (char *) shared <= SimpleLruShmemSize(nslots, nlsns));
332  }
333  else
334  {
335  Assert(found);
336  Assert(shared->num_slots == nslots);
337  }
338 
339  /*
340  * Initialize the unshared control struct, including directory path. We
341  * assume caller set PagePrecedes.
342  */
343  ctl->shared = shared;
344  ctl->sync_handler = sync_handler;
345  ctl->long_segment_names = long_segment_names;
346  ctl->bank_mask = (nslots / SLRU_BANK_SIZE) - 1;
347  strlcpy(ctl->Dir, subdir, sizeof(ctl->Dir));
348 }
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:446
#define MAXALIGN(LEN)
Definition: c.h:811
#define BUFFERALIGN(LEN)
Definition: c.h:813
#define Assert(condition)
Definition: c.h:858
size_t Size
Definition: c.h:605
bool IsUnderPostmaster
Definition: globals.c:118
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:707
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:199
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 502 of file slru.c.

504 {
505  SlruShared shared = ctl->shared;
506  LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
507 
509 
510  /* Outer loop handles restart if we must wait for someone else's I/O */
511  for (;;)
512  {
513  int slotno;
514  bool ok;
515 
516  /* See if page already is in memory; if not, pick victim slot */
517  slotno = SlruSelectLRUPage(ctl, pageno);
518 
519  /* Did we find the page in memory? */
520  if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
521  shared->page_number[slotno] == pageno)
522  {
523  /*
524  * If page is still being read in, we must wait for I/O. Likewise
525  * if the page is being written and the caller said that's not OK.
526  */
527  if (shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS ||
528  (shared->page_status[slotno] == SLRU_PAGE_WRITE_IN_PROGRESS &&
529  !write_ok))
530  {
531  SimpleLruWaitIO(ctl, slotno);
532  /* Now we must recheck state from the top */
533  continue;
534  }
535  /* Otherwise, it's ready to use */
536  SlruRecentlyUsed(shared, slotno);
537 
538  /* update the stats counter of pages found in the SLRU */
540 
541  return slotno;
542  }
543 
544  /* We found no match; assert we selected a freeable slot */
545  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
546  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
547  !shared->page_dirty[slotno]));
548 
549  /* Mark the slot read-busy */
550  shared->page_number[slotno] = pageno;
551  shared->page_status[slotno] = SLRU_PAGE_READ_IN_PROGRESS;
552  shared->page_dirty[slotno] = false;
553 
554  /* Acquire per-buffer lock (cannot deadlock, see notes at top) */
555  LWLockAcquire(&shared->buffer_locks[slotno].lock, LW_EXCLUSIVE);
556 
557  /* Release bank lock while doing I/O */
558  LWLockRelease(banklock);
559 
560  /* Do the read */
561  ok = SlruPhysicalReadPage(ctl, pageno, slotno);
562 
563  /* Set the LSNs for this newly read-in page to zero */
564  SimpleLruZeroLSNs(ctl, slotno);
565 
566  /* Re-acquire bank control lock and update page state */
567  LWLockAcquire(banklock, LW_EXCLUSIVE);
568 
569  Assert(shared->page_number[slotno] == pageno &&
570  shared->page_status[slotno] == SLRU_PAGE_READ_IN_PROGRESS &&
571  !shared->page_dirty[slotno]);
572 
573  shared->page_status[slotno] = ok ? SLRU_PAGE_VALID : SLRU_PAGE_EMPTY;
574 
575  LWLockRelease(&shared->buffer_locks[slotno].lock);
576 
577  /* Now it's okay to ereport if we failed */
578  if (!ok)
579  SlruReportIOError(ctl, pageno, xid);
580 
581  SlruRecentlyUsed(shared, slotno);
582 
583  /* update the stats counter of pages not found in SLRU */
585 
586  return slotno;
587  }
588 }
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1937
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ 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:801
static void SimpleLruZeroLSNs(SlruCtl ctl, int slotno)
Definition: slru.c:428
static void SimpleLruWaitIO(SlruCtl ctl, int slotno)
Definition: slru.c:445
static int SlruSelectLRUPage(SlruCtl ctl, int64 pageno)
Definition: slru.c:1166
static void SlruRecentlyUsed(SlruShared shared, int slotno)
Definition: slru.c:1120
static LWLock * SimpleLruGetBankLock(SlruCtl ctl, int64 pageno)
Definition: slru.h:178
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 605 of file slru.c.

606 {
607  SlruShared shared = ctl->shared;
608  LWLock *banklock = SimpleLruGetBankLock(ctl, pageno);
609  int bankno = pageno & ctl->bank_mask;
610  int bankstart = bankno * SLRU_BANK_SIZE;
611  int bankend = bankstart + SLRU_BANK_SIZE;
612 
613  /* Try to find the page while holding only shared lock */
614  LWLockAcquire(banklock, LW_SHARED);
615 
616  /* See if page is already in a buffer */
617  for (int slotno = bankstart; slotno < bankend; slotno++)
618  {
619  if (shared->page_status[slotno] != SLRU_PAGE_EMPTY &&
620  shared->page_number[slotno] == pageno &&
621  shared->page_status[slotno] != SLRU_PAGE_READ_IN_PROGRESS)
622  {
623  /* See comments for SlruRecentlyUsed macro */
624  SlruRecentlyUsed(shared, slotno);
625 
626  /* update the stats counter of pages found in the SLRU */
628 
629  return slotno;
630  }
631  }
632 
633  /* No luck, so switch to normal exclusive lock and do regular read */
634  LWLockRelease(banklock);
635  LWLockAcquire(banklock, LW_EXCLUSIVE);
636 
637  return SimpleLruReadPage(ctl, pageno, true, xid);
638 }
@ LW_SHARED
Definition: lwlock.h:115
int SimpleLruReadPage(SlruCtl ctl, int64 pageno, bool write_ok, TransactionId xid)
Definition: slru.c:502

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 199 of file slru.c.

200 {
201  int nbanks = nslots / SLRU_BANK_SIZE;
202  Size sz;
203 
204  Assert(nslots <= SLRU_MAX_ALLOWED_BUFFERS);
205  Assert(nslots % SLRU_BANK_SIZE == 0);
206 
207  /* we assume nslots isn't so large as to risk overflow */
208  sz = MAXALIGN(sizeof(SlruSharedData));
209  sz += MAXALIGN(nslots * sizeof(char *)); /* page_buffer[] */
210  sz += MAXALIGN(nslots * sizeof(SlruPageStatus)); /* page_status[] */
211  sz += MAXALIGN(nslots * sizeof(bool)); /* page_dirty[] */
212  sz += MAXALIGN(nslots * sizeof(int64)); /* page_number[] */
213  sz += MAXALIGN(nslots * sizeof(int)); /* page_lru_count[] */
214  sz += MAXALIGN(nslots * sizeof(LWLockPadded)); /* buffer_locks[] */
215  sz += MAXALIGN(nbanks * sizeof(LWLockPadded)); /* bank_locks[] */
216  sz += MAXALIGN(nbanks * sizeof(int)); /* bank_cur_lru_count[] */
217 
218  if (nlsns > 0)
219  sz += MAXALIGN(nslots * nlsns * sizeof(XLogRecPtr)); /* group_lsn[] */
220 
221  return BUFFERALIGN(sz) + BLCKSZ * nslots;
222 }

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 1405 of file slru.c.

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

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 1319 of file slru.c.

1320 {
1321  SlruShared shared = ctl->shared;
1322  SlruWriteAllData fdata;
1323  int64 pageno = 0;
1324  int prevbank = SlotGetBankNumber(0);
1325  bool ok;
1326 
1327  /* update the stats counter of flushes */
1329 
1330  /*
1331  * Find and write dirty pages
1332  */
1333  fdata.num_files = 0;
1334 
1335  LWLockAcquire(&shared->bank_locks[prevbank].lock, LW_EXCLUSIVE);
1336 
1337  for (int slotno = 0; slotno < shared->num_slots; slotno++)
1338  {
1339  int curbank = SlotGetBankNumber(slotno);
1340 
1341  /*
1342  * If the current bank lock is not same as the previous bank lock then
1343  * release the previous lock and acquire the new lock.
1344  */
1345  if (curbank != prevbank)
1346  {
1347  LWLockRelease(&shared->bank_locks[prevbank].lock);
1348  LWLockAcquire(&shared->bank_locks[curbank].lock, LW_EXCLUSIVE);
1349  prevbank = curbank;
1350  }
1351 
1352  /* Do nothing if slot is unused */
1353  if (shared->page_status[slotno] == SLRU_PAGE_EMPTY)
1354  continue;
1355 
1356  SlruInternalWritePage(ctl, slotno, &fdata);
1357 
1358  /*
1359  * In some places (e.g. checkpoints), we cannot assert that the slot
1360  * is clean now, since another process might have re-dirtied it
1361  * already. That's okay.
1362  */
1363  Assert(allow_redirtied ||
1364  shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
1365  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
1366  !shared->page_dirty[slotno]));
1367  }
1368 
1369  LWLockRelease(&shared->bank_locks[prevbank].lock);
1370 
1371  /*
1372  * Now close any files that were open
1373  */
1374  ok = true;
1375  for (int i = 0; i < fdata.num_files; i++)
1376  {
1377  if (CloseTransientFile(fdata.fd[i]) != 0)
1378  {
1380  slru_errno = errno;
1381  pageno = fdata.segno[i] * SLRU_PAGES_PER_SEGMENT;
1382  ok = false;
1383  }
1384  }
1385  if (!ok)
1387 
1388  /* Ensure that directory entries for new files are on disk. */
1389  if (ctl->sync_handler != SYNC_HANDLER_NONE)
1390  fsync_fname(ctl->Dir, true);
1391 }
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:128
int fd[MAX_WRITEALL_BUFFERS]
Definition: slru.c:129
int64 segno[MAX_WRITEALL_BUFFERS]
Definition: slru.c:130
@ 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 
)

Definition at line 729 of file slru.c.

730 {
731  Assert(ctl->shared->page_status[slotno] != SLRU_PAGE_EMPTY);
732 
733  SlruInternalWritePage(ctl, slotno, NULL);
734 }

References Assert, ctl, SLRU_PAGE_EMPTY, and SlruInternalWritePage().

Referenced by ActivateCommitTs(), BootStrapCLOG(), BootStrapMultiXact(), BootStrapSUBTRANS(), clog_redo(), commit_ts_redo(), MaybeExtendOffsetSlru(), multixact_redo(), and test_slru_page_write().

◆ SimpleLruZeroPage()

int SimpleLruZeroPage ( SlruCtl  ctl,
int64  pageno 
)

Definition at line 375 of file slru.c.

376 {
377  SlruShared shared = ctl->shared;
378  int slotno;
379 
381 
382  /* Find a suitable buffer slot for the page */
383  slotno = SlruSelectLRUPage(ctl, pageno);
384  Assert(shared->page_status[slotno] == SLRU_PAGE_EMPTY ||
385  (shared->page_status[slotno] == SLRU_PAGE_VALID &&
386  !shared->page_dirty[slotno]) ||
387  shared->page_number[slotno] == pageno);
388 
389  /* Mark the slot as containing this page */
390  shared->page_number[slotno] = pageno;
391  shared->page_status[slotno] = SLRU_PAGE_VALID;
392  shared->page_dirty[slotno] = true;
393  SlruRecentlyUsed(shared, slotno);
394 
395  /* Set the buffer to zeroes */
396  MemSet(shared->page_buffer[slotno], 0, BLCKSZ);
397 
398  /* Set the LSNs for this new page to zero */
399  SimpleLruZeroLSNs(ctl, slotno);
400 
401  /*
402  * Assume this page is now the latest active page.
403  *
404  * Note that because both this routine and SlruSelectLRUPage run with
405  * ControlLock held, it is not possible for this to be zeroing a page that
406  * SlruSelectLRUPage is going to evict simultaneously. Therefore, there's
407  * no memory barrier here.
408  */
409  pg_atomic_write_u64(&shared->latest_page_number, pageno);
410 
411  /* update the stats counter of zeroed pages */
413 
414  return slotno;
415 }
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition: atomics.h:478
#define MemSet(start, val, len)
Definition: c.h:1020
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 1523 of file slru.c.

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

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 1741 of file slru.c.

1742 {
1744 
1745  return false; /* keep going */
1746 }

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 1709 of file slru.c.

1711 {
1712  int64 cutoffPage = *(int64 *) data;
1713 
1714  if (SlruMayDeleteSegment(ctl, segpage, cutoffPage))
1715  return true; /* found one; don't iterate any more */
1716 
1717  return false; /* keep going */
1718 }
const void * data
static bool SlruMayDeleteSegment(SlruCtl ctl, int64 segpage, int64 cutoffPage)
Definition: slru.c:1600

References ctl, data, and SlruMayDeleteSegment().

Referenced by TruncateCLOG(), and TruncateCommitTs().

◆ SlruScanDirectory()

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

Definition at line 1788 of file slru.c.

1789 {
1790  bool retval = false;
1791  DIR *cldir;
1792  struct dirent *clde;
1793  int64 segno;
1794  int64 segpage;
1795 
1796  cldir = AllocateDir(ctl->Dir);
1797  while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
1798  {
1799  size_t len;
1800 
1801  len = strlen(clde->d_name);
1802 
1804  strspn(clde->d_name, "0123456789ABCDEF") == len)
1805  {
1806  segno = strtoi64(clde->d_name, NULL, 16);
1807  segpage = segno * SLRU_PAGES_PER_SEGMENT;
1808 
1809  elog(DEBUG2, "SlruScanDirectory invoking callback on %s/%s",
1810  ctl->Dir, clde->d_name);
1811  retval = callback(ctl, clde->d_name, segpage, data);
1812  if (retval)
1813  break;
1814  }
1815  }
1816  FreeDir(cldir);
1817 
1818  return retval;
1819 }
#define strtoi64(str, endptr, base)
Definition: c.h:1297
#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:1755
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 1828 of file slru.c.

1829 {
1830  int fd;
1831  int save_errno;
1832  int result;
1833 
1834  SlruFileName(ctl, path, ftag->segno);
1835 
1836  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1837  if (fd < 0)
1838  return -1;
1839 
1840  pgstat_report_wait_start(WAIT_EVENT_SLRU_FLUSH_SYNC);
1841  result = pg_fsync(fd);
1843  save_errno = errno;
1844 
1846 
1847  errno = save_errno;
1848  return result;
1849 }
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:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101

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