slru.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*-------------------------------------------------------------------------
  2. *
  3. * slru.h
  4. * Simple LRU buffering for transaction status logfiles
  5. *
  6. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. * src/include/access/slru.h
  10. *
  11. *-------------------------------------------------------------------------
  12. */
  13. #ifndef SLRU_H
  14. #define SLRU_H
  15. #include "access/xlogdefs.h"
  16. #include "storage/lwlock.h"
  17. #include "storage/sync.h"
  18. /*
  19. * Define SLRU segment size. A page is the same BLCKSZ as is used everywhere
  20. * else in Postgres. The segment size can be chosen somewhat arbitrarily;
  21. * we make it 32 pages by default, or 256Kb, i.e. 1M transactions for CLOG
  22. * or 64K transactions for SUBTRANS.
  23. *
  24. * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
  25. * page numbering also wraps around at 0xFFFFFFFF/xxxx_XACTS_PER_PAGE (where
  26. * xxxx is CLOG or SUBTRANS, respectively), and segment numbering at
  27. * 0xFFFFFFFF/xxxx_XACTS_PER_PAGE/SLRU_PAGES_PER_SEGMENT. We need
  28. * take no explicit notice of that fact in slru.c, except when comparing
  29. * segment and page numbers in SimpleLruTruncate (see PagePrecedes()).
  30. */
  31. #define SLRU_PAGES_PER_SEGMENT 32
  32. /*
  33. * Page status codes. Note that these do not include the "dirty" bit.
  34. * page_dirty can be true only in the VALID or WRITE_IN_PROGRESS states;
  35. * in the latter case it implies that the page has been re-dirtied since
  36. * the write started.
  37. */
  38. typedef enum
  39. {
  40. SLRU_PAGE_EMPTY, /* buffer is not in use */
  41. SLRU_PAGE_READ_IN_PROGRESS, /* page is being read in */
  42. SLRU_PAGE_VALID, /* page is valid and not being written */
  43. SLRU_PAGE_WRITE_IN_PROGRESS /* page is being written out */
  44. } SlruPageStatus;
  45. /*
  46. * Shared-memory state
  47. */
  48. typedef struct SlruSharedData
  49. {
  50. LWLock *ControlLock;
  51. /* Number of buffers managed by this SLRU structure */
  52. int num_slots;
  53. /*
  54. * Arrays holding info for each buffer slot. Page number is undefined
  55. * when status is EMPTY, as is page_lru_count.
  56. */
  57. char **page_buffer;
  58. SlruPageStatus *page_status;
  59. bool *page_dirty;
  60. int *page_number;
  61. int *page_lru_count;
  62. LWLockPadded *buffer_locks;
  63. /*
  64. * Optional array of WAL flush LSNs associated with entries in the SLRU
  65. * pages. If not zero/NULL, we must flush WAL before writing pages (true
  66. * for pg_xact, false for multixact, pg_subtrans, pg_notify). group_lsn[]
  67. * has lsn_groups_per_page entries per buffer slot, each containing the
  68. * highest LSN known for a contiguous group of SLRU entries on that slot's
  69. * page.
  70. */
  71. XLogRecPtr *group_lsn;
  72. int lsn_groups_per_page;
  73. /*----------
  74. * We mark a page "most recently used" by setting
  75. * page_lru_count[slotno] = ++cur_lru_count;
  76. * The oldest page is therefore the one with the highest value of
  77. * cur_lru_count - page_lru_count[slotno]
  78. * The counts will eventually wrap around, but this calculation still
  79. * works as long as no page's age exceeds INT_MAX counts.
  80. *----------
  81. */
  82. int cur_lru_count;
  83. /*
  84. * latest_page_number is the page number of the current end of the log;
  85. * this is not critical data, since we use it only to avoid swapping out
  86. * the latest page.
  87. */
  88. int latest_page_number;
  89. /* SLRU's index for statistics purposes (might not be unique) */
  90. int slru_stats_idx;
  91. } SlruSharedData;
  92. typedef SlruSharedData *SlruShared;
  93. /*
  94. * SlruCtlData is an unshared structure that points to the active information
  95. * in shared memory.
  96. */
  97. typedef struct SlruCtlData
  98. {
  99. SlruShared shared;
  100. /*
  101. * Which sync handler function to use when handing sync requests over to
  102. * the checkpointer. SYNC_HANDLER_NONE to disable fsync (eg pg_notify).
  103. */
  104. SyncRequestHandler sync_handler;
  105. /*
  106. * Decide whether a page is "older" for truncation and as a hint for
  107. * evicting pages in LRU order. Return true if every entry of the first
  108. * argument is older than every entry of the second argument. Note that
  109. * !PagePrecedes(a,b) && !PagePrecedes(b,a) need not imply a==b; it also
  110. * arises when some entries are older and some are not. For SLRUs using
  111. * SimpleLruTruncate(), this must use modular arithmetic. (For others,
  112. * the behavior of this callback has no functional implications.) Use
  113. * SlruPagePrecedesUnitTests() in SLRUs meeting its criteria.
  114. */
  115. bool (*PagePrecedes) (int, int);
  116. /*
  117. * Dir is set during SimpleLruInit and does not change thereafter. Since
  118. * it's always the same, it doesn't need to be in shared memory.
  119. */
  120. char Dir[64];
  121. } SlruCtlData;
  122. typedef SlruCtlData *SlruCtl;
  123. extern Size SimpleLruShmemSize(int nslots, int nlsns);
  124. extern void SimpleLruInit(SlruCtl ctl, const char *name, int nslots, int nlsns,
  125. LWLock *ctllock, const char *subdir, int tranche_id,
  126. SyncRequestHandler sync_handler);
  127. extern int SimpleLruZeroPage(SlruCtl ctl, int pageno);
  128. extern int SimpleLruReadPage(SlruCtl ctl, int pageno, bool write_ok,
  129. TransactionId xid);
  130. extern int SimpleLruReadPage_ReadOnly(SlruCtl ctl, int pageno,
  131. TransactionId xid);
  132. extern void SimpleLruWritePage(SlruCtl ctl, int slotno);
  133. extern void SimpleLruWriteAll(SlruCtl ctl, bool allow_redirtied);
  134. #ifdef USE_ASSERT_CHECKING
  135. extern void SlruPagePrecedesUnitTests(SlruCtl ctl, int per_page);
  136. #else
  137. #define SlruPagePrecedesUnitTests(ctl, per_page) do {} while (0)
  138. #endif
  139. extern void SimpleLruTruncate(SlruCtl ctl, int cutoffPage);
  140. extern bool SimpleLruDoesPhysicalPageExist(SlruCtl ctl, int pageno);
  141. typedef bool (*SlruScanCallback) (SlruCtl ctl, char *filename, int segpage,
  142. void *data);
  143. extern bool SlruScanDirectory(SlruCtl ctl, SlruScanCallback callback, void *data);
  144. extern void SlruDeleteSegment(SlruCtl ctl, int segno);
  145. extern int SlruSyncFileTag(SlruCtl ctl, const FileTag *ftag, char *path);
  146. /* SlruScanDirectory public callbacks */
  147. extern bool SlruScanDirCbReportPresence(SlruCtl ctl, char *filename,
  148. int segpage, void *data);
  149. extern bool SlruScanDirCbDeleteAll(SlruCtl ctl, char *filename, int segpage,
  150. void *data);
  151. #endif /* SLRU_H */