PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
buf_init.c File Reference
#include "postgres.h"
#include "storage/bufmgr.h"
#include "storage/buf_internals.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

Size BufferShmemSize ( void  )

Definition at line 161 of file buf_init.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

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

Definition at line 68 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(), LWLockRegisterTranche(), LWTRANCHE_BUFFER_CONTENT, LWTRANCHE_BUFFER_IO_IN_PROGRESS, NBuffers, pg_atomic_init_u32(), ShmemInitStruct(), BufferDesc::state, StrategyInitialize(), BufferDesc::tag, BufferDesc::wait_backend_pid, and WritebackContextInit().

Referenced by CreateSharedMemoryAndSemaphores().

69 {
70  bool foundBufs,
71  foundDescs,
72  foundIOLocks,
73  foundBufCkpt;
74 
75  /* Align descriptors to a cacheline boundary. */
77  ShmemInitStruct("Buffer Descriptors",
78  NBuffers * sizeof(BufferDescPadded),
79  &foundDescs);
80 
81  BufferBlocks = (char *)
82  ShmemInitStruct("Buffer Blocks",
83  NBuffers * (Size) BLCKSZ, &foundBufs);
84 
85  /* Align lwlocks to cacheline boundary */
87  ShmemInitStruct("Buffer IO Locks",
89  &foundIOLocks);
90 
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 || foundIOLocks || foundBufCkpt)
106  {
107  /* should find all of these, or none of them */
108  Assert(foundDescs && foundBufs && foundIOLocks && 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  CLEAR_BUFFERTAG(buf->tag);
123 
124  pg_atomic_init_u32(&buf->state, 0);
125  buf->wait_backend_pid = 0;
126 
127  buf->buf_id = i;
128 
129  /*
130  * Initially link all the buffers together as unused. Subsequent
131  * management of this list is done by freelist.c.
132  */
133  buf->freeNext = i + 1;
134 
137 
140  }
141 
142  /* Correct last entry of linked list */
143  GetBufferDescriptor(NBuffers - 1)->freeNext = FREENEXT_END_OF_LIST;
144  }
145 
146  /* Init other shared buffer-management stuff */
147  StrategyInitialize(!foundDescs);
148 
149  /* Initialize per-backend file flush context */
152 }
#define FREENEXT_END_OF_LIST
LWLockMinimallyPadded * BufferIOLWLockArray
Definition: buf_init.c:23
int wait_backend_pid
int backend_flush_after
Definition: bufmgr.c:120
#define BufferDescriptorGetIOLock(bdesc)
void StrategyInitialize(bool init)
Definition: freelist.c:458
void WritebackContextInit(WritebackContext *context, int *max_pending)
Definition: bufmgr.c:4230
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
WritebackContext BackendWritebackContext
Definition: buf_init.c:24
static char * buf
Definition: pg_test_fsync.c:66
BufferDescPadded * BufferDescriptors
Definition: buf_init.c:21
#define GetBufferDescriptor(id)
union LWLockMinimallyPadded LWLockMinimallyPadded
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:667
#define BufferDescriptorGetContentLock(bdesc)
#define Assert(condition)
Definition: c.h:675
#define CLEAR_BUFFERTAG(a)
Definition: buf_internals.h:98
CkptSortItem * CkptBufferIds
Definition: buf_init.c:25
size_t Size
Definition: c.h:356
void LWLockRegisterTranche(int tranche_id, char *tranche_name)
Definition: lwlock.c:592
BufferTag tag
int i
int NBuffers
Definition: globals.c:123
pg_atomic_uint32 state
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:234
char * BufferBlocks
Definition: buf_init.c:22

Variable Documentation

WritebackContext BackendWritebackContext

Definition at line 24 of file buf_init.c.

Referenced by BufferAlloc().

char* BufferBlocks

Definition at line 22 of file buf_init.c.

Referenced by InitBufferPool().

BufferDescPadded* BufferDescriptors

Definition at line 21 of file buf_init.c.

LWLockMinimallyPadded* BufferIOLWLockArray = NULL

Definition at line 23 of file buf_init.c.

CkptSortItem* CkptBufferIds

Definition at line 25 of file buf_init.c.

Referenced by BufferSync().