LCOV - code coverage report
Current view: top level - src/backend/storage/buffer - buf_init.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16devel Lines: 33 33 100.0 %
Date: 2022-08-17 04:10:37 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * buf_init.c
       4             :  *    buffer manager initialization routines
       5             :  *
       6             :  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/storage/buffer/buf_init.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : #include "postgres.h"
      16             : 
      17             : #include "storage/buf_internals.h"
      18             : #include "storage/bufmgr.h"
      19             : #include "storage/proc.h"
      20             : 
      21             : BufferDescPadded *BufferDescriptors;
      22             : char       *BufferBlocks;
      23             : ConditionVariableMinimallyPadded *BufferIOCVArray;
      24             : WritebackContext BackendWritebackContext;
      25             : CkptSortItem *CkptBufferIds;
      26             : 
      27             : 
      28             : /*
      29             :  * Data Structures:
      30             :  *      buffers live in a freelist and a lookup data structure.
      31             :  *
      32             :  *
      33             :  * Buffer Lookup:
      34             :  *      Two important notes.  First, the buffer has to be
      35             :  *      available for lookup BEFORE an IO begins.  Otherwise
      36             :  *      a second process trying to read the buffer will
      37             :  *      allocate its own copy and the buffer pool will
      38             :  *      become inconsistent.
      39             :  *
      40             :  * Buffer Replacement:
      41             :  *      see freelist.c.  A buffer cannot be replaced while in
      42             :  *      use either by data manager or during IO.
      43             :  *
      44             :  *
      45             :  * Synchronization/Locking:
      46             :  *
      47             :  * IO_IN_PROGRESS -- this is a flag in the buffer descriptor.
      48             :  *      It must be set when an IO is initiated and cleared at
      49             :  *      the end of the IO.  It is there to make sure that one
      50             :  *      process doesn't start to use a buffer while another is
      51             :  *      faulting it in.  see WaitIO and related routines.
      52             :  *
      53             :  * refcount --  Counts the number of processes holding pins on a buffer.
      54             :  *      A buffer is pinned during IO and immediately after a BufferAlloc().
      55             :  *      Pins must be released before end of transaction.  For efficiency the
      56             :  *      shared refcount isn't increased if an individual backend pins a buffer
      57             :  *      multiple times. Check the PrivateRefCount infrastructure in bufmgr.c.
      58             :  */
      59             : 
      60             : 
      61             : /*
      62             :  * Initialize shared buffer pool
      63             :  *
      64             :  * This is called once during shared-memory initialization (either in the
      65             :  * postmaster, or in a standalone backend).
      66             :  */
      67             : void
      68        3328 : InitBufferPool(void)
      69             : {
      70             :     bool        foundBufs,
      71             :                 foundDescs,
      72             :                 foundIOCV,
      73             :                 foundBufCkpt;
      74             : 
      75             :     /* Align descriptors to a cacheline boundary. */
      76        3328 :     BufferDescriptors = (BufferDescPadded *)
      77        3328 :         ShmemInitStruct("Buffer Descriptors",
      78             :                         NBuffers * sizeof(BufferDescPadded),
      79             :                         &foundDescs);
      80             : 
      81        3328 :     BufferBlocks = (char *)
      82        3328 :         ShmemInitStruct("Buffer Blocks",
      83             :                         NBuffers * (Size) BLCKSZ, &foundBufs);
      84             : 
      85             :     /* Align condition variables to cacheline boundary. */
      86        3328 :     BufferIOCVArray = (ConditionVariableMinimallyPadded *)
      87        3328 :         ShmemInitStruct("Buffer IO Condition Variables",
      88             :                         NBuffers * sizeof(ConditionVariableMinimallyPadded),
      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             :      */
      98        3328 :     CkptBufferIds = (CkptSortItem *)
      99        3328 :         ShmemInitStruct("Checkpoint BufferIds",
     100             :                         NBuffers * sizeof(CkptSortItem), &foundBufCkpt);
     101             : 
     102        3328 :     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    35829840 :         for (i = 0; i < NBuffers; i++)
     116             :         {
     117    35826512 :             BufferDesc *buf = GetBufferDescriptor(i);
     118             : 
     119    35826512 :             ClearBufferTag(&buf->tag);
     120             : 
     121    35826512 :             pg_atomic_init_u32(&buf->state, 0);
     122    35826512 :             buf->wait_backend_pgprocno = INVALID_PGPROCNO;
     123             : 
     124    35826512 :             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    35826512 :             buf->freeNext = i + 1;
     131             : 
     132    35826512 :             LWLockInitialize(BufferDescriptorGetContentLock(buf),
     133             :                              LWTRANCHE_BUFFER_CONTENT);
     134             : 
     135    35826512 :             ConditionVariableInit(BufferDescriptorGetIOCV(buf));
     136             :         }
     137             : 
     138             :         /* Correct last entry of linked list */
     139        3328 :         GetBufferDescriptor(NBuffers - 1)->freeNext = FREENEXT_END_OF_LIST;
     140             :     }
     141             : 
     142             :     /* Init other shared buffer-management stuff */
     143        3328 :     StrategyInitialize(!foundDescs);
     144             : 
     145             :     /* Initialize per-backend file flush context */
     146        3328 :     WritebackContextInit(&BackendWritebackContext,
     147             :                          &backend_flush_after);
     148        3328 : }
     149             : 
     150             : /*
     151             :  * BufferShmemSize
     152             :  *
     153             :  * compute the size of shared memory for the buffer pool including
     154             :  * data pages, buffer descriptors, hash tables, etc.
     155             :  */
     156             : Size
     157        4996 : BufferShmemSize(void)
     158             : {
     159        4996 :     Size        size = 0;
     160             : 
     161             :     /* size of buffer descriptors */
     162        4996 :     size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded)));
     163             :     /* to allow aligning buffer descriptors */
     164        4996 :     size = add_size(size, PG_CACHE_LINE_SIZE);
     165             : 
     166             :     /* size of data pages */
     167        4996 :     size = add_size(size, mul_size(NBuffers, BLCKSZ));
     168             : 
     169             :     /* size of stuff controlled by freelist.c */
     170        4996 :     size = add_size(size, StrategyShmemSize());
     171             : 
     172             :     /* size of I/O condition variables */
     173        4996 :     size = add_size(size, mul_size(NBuffers,
     174             :                                    sizeof(ConditionVariableMinimallyPadded)));
     175             :     /* to allow aligning the above */
     176        4996 :     size = add_size(size, PG_CACHE_LINE_SIZE);
     177             : 
     178             :     /* size of checkpoint sort array in bufmgr.c */
     179        4996 :     size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem)));
     180             : 
     181        4996 :     return size;
     182             : }

Generated by: LCOV version 1.14