PostgreSQL Source Code  git master
buf_init.c File Reference
#include "postgres.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 InitBufferPool (void)
 
Size BufferShmemSize (void)
 

Variables

BufferDescPaddedBufferDescriptors
 
char * BufferBlocks
 
LWLockMinimallyPaddedBufferIOLWLockArray = NULL
 
WritebackContext BackendWritebackContext
 
CkptSortItemCkptBufferIds
 

Function Documentation

◆ BufferShmemSize()

Size BufferShmemSize ( void  )

Definition at line 157 of file buf_init.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

158 {
159  Size size = 0;
160 
161  /* size of buffer descriptors */
162  size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded)));
163  /* to allow aligning buffer descriptors */
164  size = add_size(size, PG_CACHE_LINE_SIZE);
165 
166  /* size of data pages */
167  size = add_size(size, mul_size(NBuffers, BLCKSZ));
168 
169  /* size of stuff controlled by freelist.c */
170  size = add_size(size, StrategyShmemSize());
171 
172  /*
173  * It would be nice to include the I/O locks in the BufferDesc, but that
174  * would increase the size of a BufferDesc to more than one cache line,
175  * and benchmarking has shown that keeping every BufferDesc aligned on a
176  * cache line boundary is important for performance. So, instead, the
177  * array of I/O locks is allocated in a separate tranche. Because those
178  * locks are not highly contended, we lay out the array with minimal
179  * padding.
180  */
181  size = add_size(size, mul_size(NBuffers, sizeof(LWLockMinimallyPadded)));
182  /* to allow aligning the above */
183  size = add_size(size, PG_CACHE_LINE_SIZE);
184 
185  /* size of checkpoint sort array in bufmgr.c */
186  size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem)));
187 
188  return size;
189 }
#define PG_CACHE_LINE_SIZE
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
size_t Size
Definition: c.h:474
int NBuffers
Definition: globals.c:132
Size StrategyShmemSize(void)
Definition: freelist.c:454

◆ InitBufferPool()

void InitBufferPool ( void  )

Definition at line 67 of file buf_init.c.

References Assert, backend_flush_after, buf, BufferDesc::buf_id, BufferBlocks, BufferDescriptorGetContentLock, BufferDescriptorGetIOLock, CLEAR_BUFFERTAG, BufferDesc::freeNext, FREENEXT_END_OF_LIST, GetBufferDescriptor, i, LWLockInitialize(), LWTRANCHE_BUFFER_CONTENT, LWTRANCHE_BUFFER_IO, NBuffers, pg_atomic_init_u32(), ShmemInitStruct(), BufferDesc::state, StrategyInitialize(), BufferDesc::tag, BufferDesc::wait_backend_pid, and WritebackContextInit().

Referenced by CreateSharedMemoryAndSemaphores().

68 {
69  bool foundBufs,
70  foundDescs,
71  foundIOLocks,
72  foundBufCkpt;
73 
74  /* Align descriptors to a cacheline boundary. */
76  ShmemInitStruct("Buffer Descriptors",
77  NBuffers * sizeof(BufferDescPadded),
78  &foundDescs);
79 
80  BufferBlocks = (char *)
81  ShmemInitStruct("Buffer Blocks",
82  NBuffers * (Size) BLCKSZ, &foundBufs);
83 
84  /* Align lwlocks to cacheline boundary */
86  ShmemInitStruct("Buffer IO Locks",
88  &foundIOLocks);
89 
90  /*
91  * The array used to sort to-be-checkpointed buffer ids is located in
92  * shared memory, to avoid having to allocate significant amounts of
93  * memory at runtime. As that'd be in the middle of a checkpoint, or when
94  * the checkpointer is restarted, memory allocation failures would be
95  * painful.
96  */
98  ShmemInitStruct("Checkpoint BufferIds",
99  NBuffers * sizeof(CkptSortItem), &foundBufCkpt);
100 
101  if (foundDescs || foundBufs || foundIOLocks || foundBufCkpt)
102  {
103  /* should find all of these, or none of them */
104  Assert(foundDescs && foundBufs && foundIOLocks && foundBufCkpt);
105  /* note: this path is only taken in EXEC_BACKEND case */
106  }
107  else
108  {
109  int i;
110 
111  /*
112  * Initialize all the buffer headers.
113  */
114  for (i = 0; i < NBuffers; i++)
115  {
117 
118  CLEAR_BUFFERTAG(buf->tag);
119 
120  pg_atomic_init_u32(&buf->state, 0);
121  buf->wait_backend_pid = 0;
122 
123  buf->buf_id = i;
124 
125  /*
126  * Initially link all the buffers together as unused. Subsequent
127  * management of this list is done by freelist.c.
128  */
129  buf->freeNext = i + 1;
130 
133 
136  }
137 
138  /* Correct last entry of linked list */
139  GetBufferDescriptor(NBuffers - 1)->freeNext = FREENEXT_END_OF_LIST;
140  }
141 
142  /* Init other shared buffer-management stuff */
143  StrategyInitialize(!foundDescs);
144 
145  /* Initialize per-backend file flush context */
148 }
#define FREENEXT_END_OF_LIST
LWLockMinimallyPadded * BufferIOLWLockArray
Definition: buf_init.c:22
int wait_backend_pid
int backend_flush_after
Definition: bufmgr.c:150
#define BufferDescriptorGetIOLock(bdesc)
void StrategyInitialize(bool init)
Definition: freelist.c:475
void WritebackContextInit(WritebackContext *context, int *max_pending)
Definition: bufmgr.c:4458
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
WritebackContext BackendWritebackContext
Definition: buf_init.c:23
static char * buf
Definition: pg_test_fsync.c:68
BufferDescPadded * BufferDescriptors
Definition: buf_init.c:20
#define GetBufferDescriptor(id)
union LWLockMinimallyPadded LWLockMinimallyPadded
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:745
#define BufferDescriptorGetContentLock(bdesc)
#define Assert(condition)
Definition: c.h:746
#define CLEAR_BUFFERTAG(a)
Definition: buf_internals.h:97
CkptSortItem * CkptBufferIds
Definition: buf_init.c:24
size_t Size
Definition: c.h:474
BufferTag tag
int i
int NBuffers
Definition: globals.c:132
pg_atomic_uint32 state
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:223
char * BufferBlocks
Definition: buf_init.c:21

Variable Documentation

◆ BackendWritebackContext

WritebackContext BackendWritebackContext

Definition at line 23 of file buf_init.c.

Referenced by BufferAlloc().

◆ BufferBlocks

char* BufferBlocks

Definition at line 21 of file buf_init.c.

Referenced by InitBufferPool().

◆ BufferDescriptors

BufferDescPadded* BufferDescriptors

Definition at line 20 of file buf_init.c.

◆ BufferIOLWLockArray

LWLockMinimallyPadded* BufferIOLWLockArray = NULL

Definition at line 22 of file buf_init.c.

◆ CkptBufferIds

CkptSortItem* CkptBufferIds

Definition at line 24 of file buf_init.c.

Referenced by BufferSync().