From 62ae567f1c7a56c32722508b60251f9cec245ea3 Mon Sep 17 00:00:00 2001 From: Dmitrii Dolgov <9erthalion6@gmail.com> Date: Wed, 16 Oct 2024 20:24:04 +0200 Subject: [PATCH v1 3/5] Introduce multiple shmem slots for shared buffers Add more shmem slots to split shared buffers into following chunks: * BUFFERS_SHMEM_SLOT: contains buffer blocks * BUFFER_DESCRIPTORS_SHMEM_SLOT: contains buffer descriptors * BUFFER_IOCV_SHMEM_SLOT: contains condition variables for buffers * CHECKPOINT_BUFFERS_SHMEM_SLOT: contains checkpoint buffer ids * STRATEGY_SHMEM_SLOT: contains buffer strategy status Size of the corresponding shared data directly depends on NBuffers, meaning that if we would like to change NBuffers, they have to be resized correspondingly. Placing each of them in a separate shmem slot allows to achieve that. There are some asumptions made about each of shmem slots upper size limit. The buffer blocks have the largest, while the rest claim less extra room for resize. Ideally those limits have to be deduced from the maximum allowed shared memory. --- src/backend/port/sysv_shmem.c | 17 +++++- src/backend/storage/buffer/buf_init.c | 79 +++++++++++++++++--------- src/backend/storage/buffer/buf_table.c | 5 +- src/backend/storage/buffer/freelist.c | 4 +- src/backend/storage/ipc/ipci.c | 2 +- src/include/storage/bufmgr.h | 2 +- src/include/storage/pg_shmem.h | 23 +++++++- 7 files changed, 97 insertions(+), 35 deletions(-) diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c index 7e6c8bb78d..beebd4d85e 100644 --- a/src/backend/port/sysv_shmem.c +++ b/src/backend/port/sysv_shmem.c @@ -149,8 +149,13 @@ static int next_free_slot = 0; * 7f4718400000-7f4718401000 /usr/lib64/libicudata.so.74.2 * ... */ -Size SHMEM_EXTRA_SIZE_LIMIT[1] = { +Size SHMEM_EXTRA_SIZE_LIMIT[6] = { 0, /* MAIN_SHMEM_SLOT */ + (Size) 1024 * 1024 * 1024 * 10, /* BUFFERS_SHMEM_SLOT */ + (Size) 1024 * 1024 * 1024 * 1, /* BUFFER_DESCRIPTORS_SHMEM_SLOT */ + (Size) 1024 * 1024 * 100, /* BUFFER_IOCV_SHMEM_SLOT */ + (Size) 1024 * 1024 * 100, /* CHECKPOINT_BUFFERS_SHMEM_SLOT */ + (Size) 1024 * 1024 * 100, /* STRATEGY_SHMEM_SLOT */ }; /* Remembers offset of the last mapping from the probe address */ @@ -179,6 +184,16 @@ MappingName(int shmem_slot) { case MAIN_SHMEM_SLOT: return "main"; + case BUFFERS_SHMEM_SLOT: + return "buffers"; + case BUFFER_DESCRIPTORS_SHMEM_SLOT: + return "descriptors"; + case BUFFER_IOCV_SHMEM_SLOT: + return "iocv"; + case CHECKPOINT_BUFFERS_SHMEM_SLOT: + return "checkpoint"; + case STRATEGY_SHMEM_SLOT: + return "strategy"; default: return "unknown"; } diff --git a/src/backend/storage/buffer/buf_init.c b/src/backend/storage/buffer/buf_init.c index 46116a1f64..6bca286bef 100644 --- a/src/backend/storage/buffer/buf_init.c +++ b/src/backend/storage/buffer/buf_init.c @@ -62,7 +62,10 @@ CkptSortItem *CkptBufferIds; * Initialize shared buffer pool * * This is called once during shared-memory initialization (either in the - * postmaster, or in a standalone backend). + * postmaster, or in a standalone backend). Size of data structures initialized + * here depends on NBuffers, and to be able to change NBuffers without a + * restart we store each structure into a separate shared memory slot, which + * could be resized on demand. */ void InitBufferPool(void) @@ -74,22 +77,22 @@ InitBufferPool(void) /* Align descriptors to a cacheline boundary. */ BufferDescriptors = (BufferDescPadded *) - ShmemInitStruct("Buffer Descriptors", + ShmemInitStructInSlot("Buffer Descriptors", NBuffers * sizeof(BufferDescPadded), - &foundDescs); + &foundDescs, BUFFER_DESCRIPTORS_SHMEM_SLOT); /* Align buffer pool on IO page size boundary. */ BufferBlocks = (char *) TYPEALIGN(PG_IO_ALIGN_SIZE, - ShmemInitStruct("Buffer Blocks", + ShmemInitStructInSlot("Buffer Blocks", NBuffers * (Size) BLCKSZ + PG_IO_ALIGN_SIZE, - &foundBufs)); + &foundBufs, BUFFERS_SHMEM_SLOT)); /* Align condition variables to cacheline boundary. */ BufferIOCVArray = (ConditionVariableMinimallyPadded *) - ShmemInitStruct("Buffer IO Condition Variables", + ShmemInitStructInSlot("Buffer IO Condition Variables", NBuffers * sizeof(ConditionVariableMinimallyPadded), - &foundIOCV); + &foundIOCV, BUFFER_IOCV_SHMEM_SLOT); /* * The array used to sort to-be-checkpointed buffer ids is located in @@ -99,8 +102,9 @@ InitBufferPool(void) * painful. */ CkptBufferIds = (CkptSortItem *) - ShmemInitStruct("Checkpoint BufferIds", - NBuffers * sizeof(CkptSortItem), &foundBufCkpt); + ShmemInitStructInSlot("Checkpoint BufferIds", + NBuffers * sizeof(CkptSortItem), &foundBufCkpt, + CHECKPOINT_BUFFERS_SHMEM_SLOT); if (foundDescs || foundBufs || foundIOCV || foundBufCkpt) { @@ -154,33 +158,54 @@ InitBufferPool(void) * BufferShmemSize * * compute the size of shared memory for the buffer pool including - * data pages, buffer descriptors, hash tables, etc. + * data pages, buffer descriptors, hash tables, etc. based on the + * shared memory slot. The main slot must not allocate anything + * related to buffers, every other slot will receive part of the + * data. */ Size -BufferShmemSize(void) +BufferShmemSize(int shmem_slot) { Size size = 0; - /* size of buffer descriptors */ - size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded))); - /* to allow aligning buffer descriptors */ - size = add_size(size, PG_CACHE_LINE_SIZE); + if (shmem_slot == MAIN_SHMEM_SLOT) + return size; - /* size of data pages, plus alignment padding */ - size = add_size(size, PG_IO_ALIGN_SIZE); - size = add_size(size, mul_size(NBuffers, BLCKSZ)); + if (shmem_slot == BUFFER_DESCRIPTORS_SHMEM_SLOT) + { + /* size of buffer descriptors */ + size = add_size(size, mul_size(NBuffers, sizeof(BufferDescPadded))); + /* to allow aligning buffer descriptors */ + size = add_size(size, PG_CACHE_LINE_SIZE); + } - /* size of stuff controlled by freelist.c */ - size = add_size(size, StrategyShmemSize()); + if (shmem_slot == BUFFERS_SHMEM_SLOT) + { + /* size of data pages, plus alignment padding */ + size = add_size(size, PG_IO_ALIGN_SIZE); + size = add_size(size, mul_size(NBuffers, BLCKSZ)); + } - /* size of I/O condition variables */ - size = add_size(size, mul_size(NBuffers, - sizeof(ConditionVariableMinimallyPadded))); - /* to allow aligning the above */ - size = add_size(size, PG_CACHE_LINE_SIZE); + if (shmem_slot == STRATEGY_SHMEM_SLOT) + { + /* size of stuff controlled by freelist.c */ + size = add_size(size, StrategyShmemSize()); + } - /* size of checkpoint sort array in bufmgr.c */ - size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem))); + if (shmem_slot == BUFFER_IOCV_SHMEM_SLOT) + { + /* size of I/O condition variables */ + size = add_size(size, mul_size(NBuffers, + sizeof(ConditionVariableMinimallyPadded))); + /* to allow aligning the above */ + size = add_size(size, PG_CACHE_LINE_SIZE); + } + + if (shmem_slot == CHECKPOINT_BUFFERS_SHMEM_SLOT) + { + /* size of checkpoint sort array in bufmgr.c */ + size = add_size(size, mul_size(NBuffers, sizeof(CkptSortItem))); + } return size; } diff --git a/src/backend/storage/buffer/buf_table.c b/src/backend/storage/buffer/buf_table.c index 0fa5468930..ccbaed8010 100644 --- a/src/backend/storage/buffer/buf_table.c +++ b/src/backend/storage/buffer/buf_table.c @@ -59,10 +59,11 @@ InitBufTable(int size) info.entrysize = sizeof(BufferLookupEnt); info.num_partitions = NUM_BUFFER_PARTITIONS; - SharedBufHash = ShmemInitHash("Shared Buffer Lookup Table", + SharedBufHash = ShmemInitHashInSlot("Shared Buffer Lookup Table", size, size, &info, - HASH_ELEM | HASH_BLOBS | HASH_PARTITION); + HASH_ELEM | HASH_BLOBS | HASH_PARTITION, + STRATEGY_SHMEM_SLOT); } /* diff --git a/src/backend/storage/buffer/freelist.c b/src/backend/storage/buffer/freelist.c index 19797de31a..8ce1611db2 100644 --- a/src/backend/storage/buffer/freelist.c +++ b/src/backend/storage/buffer/freelist.c @@ -491,9 +491,9 @@ StrategyInitialize(bool init) * Get or create the shared strategy control block */ StrategyControl = (BufferStrategyControl *) - ShmemInitStruct("Buffer Strategy Status", + ShmemInitStructInSlot("Buffer Strategy Status", sizeof(BufferStrategyControl), - &found); + &found, STRATEGY_SHMEM_SLOT); if (!found) { diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c index 8224015b53..fbaddba396 100644 --- a/src/backend/storage/ipc/ipci.c +++ b/src/backend/storage/ipc/ipci.c @@ -115,7 +115,7 @@ CalculateShmemSize(int *num_semaphores, int shmem_slot) sizeof(ShmemIndexEnt))); size = add_size(size, dsm_estimate_size()); size = add_size(size, DSMRegistryShmemSize()); - size = add_size(size, BufferShmemSize()); + size = add_size(size, BufferShmemSize(shmem_slot)); size = add_size(size, LockShmemSize()); size = add_size(size, PredicateLockShmemSize()); size = add_size(size, ProcGlobalShmemSize()); diff --git a/src/include/storage/bufmgr.h b/src/include/storage/bufmgr.h index c8422571b7..4c09d270c9 100644 --- a/src/include/storage/bufmgr.h +++ b/src/include/storage/bufmgr.h @@ -301,7 +301,7 @@ extern bool EvictUnpinnedBuffer(Buffer buf); /* in buf_init.c */ extern void InitBufferPool(void); -extern Size BufferShmemSize(void); +extern Size BufferShmemSize(int); /* in localbuf.c */ extern void AtProcExit_LocalBuffers(void); diff --git a/src/include/storage/pg_shmem.h b/src/include/storage/pg_shmem.h index e968deeef7..c0143e3899 100644 --- a/src/include/storage/pg_shmem.h +++ b/src/include/storage/pg_shmem.h @@ -52,7 +52,7 @@ typedef struct ShmemSegment } ShmemSegment; // Number of available slots for anonymous memory mappings -#define ANON_MAPPINGS 1 +#define ANON_MAPPINGS 6 extern PGDLLIMPORT ShmemSegment Segments[ANON_MAPPINGS]; @@ -105,7 +105,28 @@ extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2); extern void PGSharedMemoryDetach(void); extern void GetHugePageSize(Size *hugepagesize, int *mmap_flags); +/* + * To be able to dynamically resize largest parts of the data stored in shared + * memory, we split it into multiple shared memory mappings slots. Each slot + * contains only certain part of the data, which size depends on NBuffers. + */ + /* The main slot, contains everything except buffer blocks and related data. */ #define MAIN_SHMEM_SLOT 0 +/* Buffer blocks */ +#define BUFFERS_SHMEM_SLOT 1 + +/* Buffer descriptors */ +#define BUFFER_DESCRIPTORS_SHMEM_SLOT 2 + +/* Condition variables for buffers */ +#define BUFFER_IOCV_SHMEM_SLOT 3 + +/* Checkpoint BufferIds */ +#define CHECKPOINT_BUFFERS_SHMEM_SLOT 4 + +/* Buffer strategy status */ +#define STRATEGY_SHMEM_SLOT 5 + #endif /* PG_SHMEM_H */ -- 2.45.1