PostgreSQL Source Code  git master
buf_init.c File Reference
#include "postgres.h"
#include "storage/buf_internals.h"
#include "storage/bufmgr.h"
#include "storage/proc.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
 
ConditionVariableMinimallyPaddedBufferIOCVArray
 
WritebackContext BackendWritebackContext
 
CkptSortItemCkptBufferIds
 

Function Documentation

◆ BufferShmemSize()

Size BufferShmemSize ( void  )

Definition at line 157 of file buf_init.c.

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  /* size of I/O condition variables */
173  size = add_size(size, mul_size(NBuffers,
175  /* to allow aligning the above */
176  size = add_size(size, PG_CACHE_LINE_SIZE);
177 
178  /* size of checkpoint sort array in bufmgr.c */
179  size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem)));
180 
181  return size;
182 }
size_t Size
Definition: c.h:589
Size StrategyShmemSize(void)
Definition: freelist.c:453
int NBuffers
Definition: globals.c:136
#define PG_CACHE_LINE_SIZE
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519

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

Referenced by CalculateShmemSize().

◆ InitBufferPool()

void InitBufferPool ( 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  BufferBlocks = (char *)
82  ShmemInitStruct("Buffer Blocks",
83  NBuffers * (Size) BLCKSZ, &foundBufs);
84 
85  /* Align condition variables to cacheline boundary. */
87  ShmemInitStruct("Buffer IO Condition Variables",
89  &foundIOCV);
90 
91  /*
92  * The array used to sort to-be-checkpointed buffer ids is located in
93  * shared memory, to avoid having to allocate significant amounts of
94  * memory at runtime. As that'd be in the middle of a checkpoint, or when
95  * the checkpointer is restarted, memory allocation failures would be
96  * painful.
97  */
99  ShmemInitStruct("Checkpoint BufferIds",
100  NBuffers * sizeof(CkptSortItem), &foundBufCkpt);
101 
102  if (foundDescs || foundBufs || foundIOCV || foundBufCkpt)
103  {
104  /* should find all of these, or none of them */
105  Assert(foundDescs && foundBufs && foundIOCV && foundBufCkpt);
106  /* note: this path is only taken in EXEC_BACKEND case */
107  }
108  else
109  {
110  int i;
111 
112  /*
113  * Initialize all the buffer headers.
114  */
115  for (i = 0; i < NBuffers; i++)
116  {
118 
119  ClearBufferTag(&buf->tag);
120 
121  pg_atomic_init_u32(&buf->state, 0);
122  buf->wait_backend_pgprocno = INVALID_PGPROCNO;
123 
124  buf->buf_id = i;
125 
126  /*
127  * Initially link all the buffers together as unused. Subsequent
128  * management of this list is done by freelist.c.
129  */
130  buf->freeNext = i + 1;
131 
134 
136  }
137 
138  /* Correct last entry of linked list */
140  }
141 
142  /* Init other shared buffer-management stuff */
143  StrategyInitialize(!foundDescs);
144 
145  /* Initialize per-backend file flush context */
148 }
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:218
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 BufferDesc * GetBufferDescriptor(uint32 id)
#define FREENEXT_END_OF_LIST
static void ClearBufferTag(BufferTag *tag)
static ConditionVariable * BufferDescriptorGetIOCV(const BufferDesc *bdesc)
static LWLock * BufferDescriptorGetContentLock(const BufferDesc *bdesc)
void WritebackContextInit(WritebackContext *context, int *max_pending)
Definition: bufmgr.c:4942
int backend_flush_after
Definition: bufmgr.c:160
void ConditionVariableInit(ConditionVariable *cv)
void StrategyInitialize(bool init)
Definition: freelist.c:474
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:730
@ LWTRANCHE_BUFFER_CONTENT
Definition: lwlock.h:188
static char * buf
Definition: pg_test_fsync.c:67
#define INVALID_PGPROCNO
Definition: proc.h:85
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396

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

Referenced by CreateSharedMemoryAndSemaphores().

Variable Documentation

◆ BackendWritebackContext

WritebackContext BackendWritebackContext

Definition at line 24 of file buf_init.c.

Referenced by BufferAlloc(), and InitBufferPool().

◆ BufferBlocks

char* BufferBlocks

Definition at line 22 of file buf_init.c.

Referenced by BufferGetBlock(), and InitBufferPool().

◆ BufferDescriptors

BufferDescPadded* BufferDescriptors

Definition at line 21 of file buf_init.c.

Referenced by GetBufferDescriptor(), and InitBufferPool().

◆ BufferIOCVArray

Definition at line 23 of file buf_init.c.

Referenced by BufferDescriptorGetIOCV(), and InitBufferPool().

◆ CkptBufferIds

CkptSortItem* CkptBufferIds

Definition at line 25 of file buf_init.c.

Referenced by BufferSync(), and InitBufferPool().