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

Go to the source code of this file.

Functions

void BufferManagerShmemInit (void)
 
Size BufferManagerShmemSize (void)
 

Variables

BufferDescPaddedBufferDescriptors
 
char * BufferBlocks
 
ConditionVariableMinimallyPaddedBufferIOCVArray
 
WritebackContext BackendWritebackContext
 
CkptSortItemCkptBufferIds
 

Function Documentation

◆ BufferManagerShmemInit()

void BufferManagerShmemInit ( void  )

Definition at line 68 of file buf_init.c.

69{
70 bool foundBufs,
71 foundDescs,
72 foundIOCV,
73 foundBufCkpt;
74
75 /* Align descriptors to a cacheline boundary. */
77 ShmemInitStruct("Buffer Descriptors",
78 NBuffers * sizeof(BufferDescPadded),
79 &foundDescs);
80
81 /* Align buffer pool on IO page size boundary. */
82 BufferBlocks = (char *)
84 ShmemInitStruct("Buffer Blocks",
85 NBuffers * (Size) BLCKSZ + PG_IO_ALIGN_SIZE,
86 &foundBufs));
87
88 /* Align condition variables to cacheline boundary. */
90 ShmemInitStruct("Buffer IO Condition Variables",
92 &foundIOCV);
93
94 /*
95 * The array used to sort to-be-checkpointed buffer ids is located in
96 * shared memory, to avoid having to allocate significant amounts of
97 * memory at runtime. As that'd be in the middle of a checkpoint, or when
98 * the checkpointer is restarted, memory allocation failures would be
99 * painful.
100 */
102 ShmemInitStruct("Checkpoint BufferIds",
103 NBuffers * sizeof(CkptSortItem), &foundBufCkpt);
104
105 if (foundDescs || foundBufs || foundIOCV || foundBufCkpt)
106 {
107 /* should find all of these, or none of them */
108 Assert(foundDescs && foundBufs && foundIOCV && foundBufCkpt);
109 /* note: this path is only taken in EXEC_BACKEND case */
110 }
111 else
112 {
113 int i;
114
115 /*
116 * Initialize all the buffer headers.
117 */
118 for (i = 0; i < NBuffers; i++)
119 {
121
122 ClearBufferTag(&buf->tag);
123
124 pg_atomic_init_u32(&buf->state, 0);
125 buf->wait_backend_pgprocno = INVALID_PROC_NUMBER;
126
127 buf->buf_id = i;
128
129 pgaio_wref_clear(&buf->io_wref);
130
131 /*
132 * Initially link all the buffers together as unused. Subsequent
133 * management of this list is done by freelist.c.
134 */
135 buf->freeNext = i + 1;
136
139
141 }
142
143 /* Correct last entry of linked list */
145 }
146
147 /* Init other shared buffer-management stuff */
148 StrategyInitialize(!foundDescs);
149
150 /* Initialize per-backend file flush context */
153}
void pgaio_wref_clear(PgAioWaitRef *iow)
Definition: aio.c:866
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:221
CkptSortItem * CkptBufferIds
Definition: buf_init.c:25
char * BufferBlocks
Definition: buf_init.c:22
WritebackContext BackendWritebackContext
Definition: buf_init.c:24
ConditionVariableMinimallyPadded * BufferIOCVArray
Definition: buf_init.c:23
BufferDescPadded * BufferDescriptors
Definition: buf_init.c:21
static ConditionVariable * BufferDescriptorGetIOCV(const BufferDesc *bdesc)
static LWLock * BufferDescriptorGetContentLock(const BufferDesc *bdesc)
#define FREENEXT_END_OF_LIST
static void ClearBufferTag(BufferTag *tag)
static BufferDesc * GetBufferDescriptor(uint32 id)
void WritebackContextInit(WritebackContext *context, int *max_pending)
Definition: bufmgr.c:6401
int backend_flush_after
Definition: bufmgr.c:180
#define TYPEALIGN(ALIGNVAL, LEN)
Definition: c.h:775
size_t Size
Definition: c.h:576
void ConditionVariableInit(ConditionVariable *cv)
void StrategyInitialize(bool init)
Definition: freelist.c:474
int NBuffers
Definition: globals.c:143
Assert(PointerIsAligned(start, uint64))
int i
Definition: isn.c:77
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:721
@ LWTRANCHE_BUFFER_CONTENT
Definition: lwlock.h:191
#define PG_IO_ALIGN_SIZE
static char * buf
Definition: pg_test_fsync.c:72
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387

References Assert(), backend_flush_after, BackendWritebackContext, buf, BufferBlocks, BufferDescriptorGetContentLock(), BufferDescriptorGetIOCV(), BufferDescriptors, BufferIOCVArray, CkptBufferIds, ClearBufferTag(), ConditionVariableInit(), BufferDesc::freeNext, FREENEXT_END_OF_LIST, GetBufferDescriptor(), i, INVALID_PROC_NUMBER, LWLockInitialize(), LWTRANCHE_BUFFER_CONTENT, NBuffers, pg_atomic_init_u32(), PG_IO_ALIGN_SIZE, pgaio_wref_clear(), ShmemInitStruct(), StrategyInitialize(), TYPEALIGN, and WritebackContextInit().

Referenced by CreateOrAttachShmemStructs().

◆ BufferManagerShmemSize()

Size BufferManagerShmemSize ( void  )

Definition at line 162 of file buf_init.c.

163{
164 Size size = 0;
165
166 /* size of buffer descriptors */
167 size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded)));
168 /* to allow aligning buffer descriptors */
169 size = add_size(size, PG_CACHE_LINE_SIZE);
170
171 /* size of data pages, plus alignment padding */
172 size = add_size(size, PG_IO_ALIGN_SIZE);
173 size = add_size(size, mul_size(NBuffers, BLCKSZ));
174
175 /* size of stuff controlled by freelist.c */
176 size = add_size(size, StrategyShmemSize());
177
178 /* size of I/O condition variables */
179 size = add_size(size, mul_size(NBuffers,
181 /* to allow aligning the above */
182 size = add_size(size, PG_CACHE_LINE_SIZE);
183
184 /* size of checkpoint sort array in bufmgr.c */
185 size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem)));
186
187 return size;
188}
Size StrategyShmemSize(void)
Definition: freelist.c:453
#define PG_CACHE_LINE_SIZE
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510

References add_size(), mul_size(), NBuffers, PG_CACHE_LINE_SIZE, PG_IO_ALIGN_SIZE, and StrategyShmemSize().

Referenced by CalculateShmemSize().

Variable Documentation

◆ BackendWritebackContext

WritebackContext BackendWritebackContext

Definition at line 24 of file buf_init.c.

Referenced by BufferManagerShmemInit(), and GetVictimBuffer().

◆ BufferBlocks

char* BufferBlocks

Definition at line 22 of file buf_init.c.

Referenced by BufferGetBlock(), and BufferManagerShmemInit().

◆ BufferDescriptors

BufferDescPadded* BufferDescriptors

Definition at line 21 of file buf_init.c.

Referenced by BufferManagerShmemInit(), and GetBufferDescriptor().

◆ BufferIOCVArray

Definition at line 23 of file buf_init.c.

Referenced by BufferDescriptorGetIOCV(), and BufferManagerShmemInit().

◆ CkptBufferIds

CkptSortItem* CkptBufferIds

Definition at line 25 of file buf_init.c.

Referenced by BufferManagerShmemInit(), and BufferSync().