PostgreSQL Source Code
git master
|
#include "postgres.h"
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include "common/pg_prng.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "storage/dsm.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/pg_shmem.h"
#include "utils/freepage.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/resowner_private.h"
Go to the source code of this file.
Data Structures | |
struct | dsm_segment_detach_callback |
struct | dsm_segment |
struct | dsm_control_item |
struct | dsm_control_header |
Macros | |
#define | PG_DYNSHMEM_CONTROL_MAGIC 0x9a503d32 |
#define | PG_DYNSHMEM_FIXED_SLOTS 64 |
#define | PG_DYNSHMEM_SLOTS_PER_BACKEND 5 |
#define | INVALID_CONTROL_SLOT ((uint32) -1) |
Typedefs | |
typedef struct dsm_segment_detach_callback | dsm_segment_detach_callback |
typedef struct dsm_control_item | dsm_control_item |
typedef struct dsm_control_header | dsm_control_header |
Variables | |
static bool | dsm_init_done = false |
static void * | dsm_main_space_begin = NULL |
static dlist_head | dsm_segment_list = DLIST_STATIC_INIT(dsm_segment_list) |
static dsm_handle | dsm_control_handle |
static dsm_control_header * | dsm_control |
static Size | dsm_control_mapped_size = 0 |
static void * | dsm_control_impl_private = NULL |
typedef struct dsm_control_header dsm_control_header |
typedef struct dsm_control_item dsm_control_item |
typedef struct dsm_segment_detach_callback dsm_segment_detach_callback |
void cancel_on_dsm_detach | ( | dsm_segment * | seg, |
on_dsm_detach_callback | function, | ||
Datum | arg | ||
) |
Definition at line 1118 of file dsm.c.
References dsm_segment_detach_callback::arg, arg, slist_mutable_iter::cur, dsm_segment_detach_callback::function, dsm_segment::on_detach, pfree(), slist_container, slist_delete_current(), and slist_foreach_modify.
Referenced by shm_mq_detach(), and test_shm_mq_setup().
dsm_segment* dsm_attach | ( | dsm_handle | h | ) |
Definition at line 638 of file dsm.c.
References Assert(), dsm_segment::control_slot, dlist_iter::cur, dlist_container, dlist_foreach, dsm_backend_startup(), dsm_control, dsm_create_descriptor(), dsm_detach(), dsm_impl_op(), dsm_init_done, dsm_main_space_begin, DSM_OP_ATTACH, dsm_segment_list, elog(), ERROR, dsm_control_item::first_page, FPM_PAGE_SIZE, dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, INVALID_CONTROL_SLOT, is_main_region_dsm_handle(), IsUnderPostmaster, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_control_header::nitems, nitems, dsm_control_item::npages, and dsm_control_item::refcnt.
Referenced by AttachSession(), autoprewarm_database_main(), dsa_attach(), get_segment_by_index(), ParallelApplyWorkerMain(), ParallelWorkerMain(), and test_shm_mq_main().
void dsm_backend_shutdown | ( | void | ) |
Definition at line 730 of file dsm.c.
References dlist_head_element, dlist_is_empty(), dsm_detach(), and dsm_segment_list.
Referenced by shmem_exit().
|
static |
Definition at line 396 of file dsm.c.
References Assert(), dsm_control, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_control_segment_sane(), dsm_impl_op(), dsm_init_done, DSM_OP_ATTACH, DSM_OP_DETACH, ereport, errcode(), errmsg(), ERROR, FATAL, IsUnderPostmaster, and WARNING.
Referenced by dsm_attach(), and dsm_create().
|
static |
Definition at line 293 of file dsm.c.
References AllocateDir(), buf, dirent::d_name, DEBUG2, elog(), ereport, errcode_for_file_access(), errmsg(), ERROR, FreeDir(), MAXPGPATH, PG_DYNSHMEM_DIR, PG_DYNSHMEM_MMAP_FILE_PREFIX, ReadDir(), and snprintf.
Referenced by dsm_postmaster_startup().
void dsm_cleanup_using_control_segment | ( | dsm_handle | old_control_handle | ) |
Definition at line 211 of file dsm.c.
References DEBUG1, DEBUG2, dsm_control_segment_sane(), dsm_impl_op(), DSM_OP_ATTACH, DSM_OP_DESTROY, DSM_OP_DETACH, elog(), dsm_control_item::handle, i, is_main_region_dsm_handle(), dsm_control_header::item, LOG, dsm_control_header::nitems, nitems, and dsm_control_item::refcnt.
Referenced by PGSharedMemoryCreate().
|
static |
Definition at line 1226 of file dsm.c.
References nitems.
Referenced by dsm_control_segment_sane(), and dsm_postmaster_startup().
|
static |
Definition at line 1208 of file dsm.c.
References dsm_control_bytes_needed(), dsm_control_header::magic, dsm_control_header::maxitems, dsm_control_header::nitems, and PG_DYNSHMEM_CONTROL_MAGIC.
Referenced by dsm_backend_startup(), dsm_cleanup_using_control_segment(), and dsm_postmaster_shutdown().
dsm_segment* dsm_create | ( | Size | size, |
int | flags | ||
) |
Definition at line 489 of file dsm.c.
References Assert(), dsm_segment::control_slot, dlist_delete(), dsm_backend_startup(), dsm_control, dsm_create_descriptor(), DSM_CREATE_NULL_IF_MAXSEGMENTS, DSM_HANDLE_INVALID, dsm_impl_op(), dsm_init_done, dsm_main_space_begin, DSM_OP_CREATE, DSM_OP_DESTROY, ereport, errcode(), errmsg(), ERROR, dsm_control_item::first_page, FPM_PAGE_SIZE, FreePageManagerGet(), FreePageManagerPut(), dsm_segment::handle, dsm_control_item::handle, i, dsm_segment::impl_private, dsm_control_item::impl_private_pm_handle, is_main_region_dsm_handle(), IsPostmasterEnvironment, IsUnderPostmaster, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), make_main_region_dsm_handle(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_control_header::maxitems, dsm_control_header::nitems, nitems, dsm_segment::node, dsm_control_item::npages, pfree(), pg_global_prng_state, pg_prng_uint32(), dsm_control_item::pinned, dsm_control_item::refcnt, ResourceOwnerForgetDSM(), dsm_segment::resowner, and WARNING.
Referenced by apw_load_buffers(), dsa_create(), GetSessionDsmHandle(), InitializeParallelDSM(), make_new_segment(), pa_setup_dsm(), and setup_dynamic_shared_memory().
|
static |
Definition at line 1172 of file dsm.c.
References dsm_segment::control_slot, CurrentResourceOwner, dlist_push_head(), dsm_segment_list, dsm_segment::impl_private, INVALID_CONTROL_SLOT, dsm_segment::mapped_address, dsm_segment::mapped_size, MemoryContextAlloc(), dsm_segment::node, dsm_segment::on_detach, ResourceOwnerEnlargeDSMs(), ResourceOwnerRememberDSM(), dsm_segment::resowner, slist_init(), and TopMemoryContext.
Referenced by dsm_attach(), and dsm_create().
void dsm_detach | ( | dsm_segment * | seg | ) |
Definition at line 776 of file dsm.c.
References dsm_segment_detach_callback::arg, arg, Assert(), dsm_segment::control_slot, dlist_delete(), dsm_control, dsm_impl_op(), dsm_main_space_begin, DSM_OP_DESTROY, DSM_OP_DETACH, dsm_control_item::first_page, FreePageManagerPut(), dsm_segment_detach_callback::function, dsm_segment::handle, dsm_control_item::handle, HOLD_INTERRUPTS, dsm_segment::impl_private, INVALID_CONTROL_SLOT, is_main_region_dsm_handle(), dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_segment::mapped_address, dsm_segment::mapped_size, dsm_segment::node, dsm_control_item::npages, dsm_segment::on_detach, pfree(), dsm_control_item::pinned, dsm_control_item::refcnt, ResourceOwnerForgetDSM(), dsm_segment::resowner, RESUME_INTERRUPTS, slist_container, slist_is_empty(), slist_pop_head_node(), and WARNING.
Referenced by apw_load_buffers(), autoprewarm_database_main(), check_for_freed_segments_locked(), destroy_superblock(), DestroyParallelContext(), DetachSession(), dsa_detach(), dsm_attach(), dsm_backend_shutdown(), dsm_detach_all(), pa_free_worker_info(), pa_shutdown(), ParallelWorkerShutdown(), ResourceOwnerReleaseInternal(), test_shm_mq(), test_shm_mq_main(), and test_shm_mq_pipelined().
void dsm_detach_all | ( | void | ) |
Definition at line 748 of file dsm.c.
References dlist_head_element, dlist_is_empty(), dsm_control, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_detach(), dsm_impl_op(), DSM_OP_DETACH, dsm_segment_list, and ERROR.
Referenced by SysLogger_Start().
size_t dsm_estimate_size | ( | void | ) |
Definition at line 443 of file dsm.c.
References min_dynamic_shared_memory.
Referenced by CalculateShmemSize(), and dsm_shmem_init().
dsm_segment* dsm_find_mapping | ( | dsm_handle | handle | ) |
Definition at line 1047 of file dsm.c.
References dlist_iter::cur, dlist_container, dlist_foreach, dsm_segment_list, and dsm_segment::handle.
void dsm_pin_mapping | ( | dsm_segment * | seg | ) |
Definition at line 888 of file dsm.c.
References ResourceOwnerForgetDSM(), and dsm_segment::resowner.
Referenced by AttachSession(), dsa_pin_mapping(), get_segment_by_index(), GetSessionDsmHandle(), and make_new_segment().
void dsm_pin_segment | ( | dsm_segment * | seg | ) |
Definition at line 928 of file dsm.c.
References dsm_segment::control_slot, dsm_control, dsm_impl_pin_segment(), elog(), ERROR, dsm_segment::handle, dsm_segment::impl_private, dsm_control_item::impl_private_pm_handle, dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_control_item::pinned, and dsm_control_item::refcnt.
Referenced by dsa_create(), and make_new_segment().
|
static |
Definition at line 331 of file dsm.c.
References arg, DatumGetPointer(), DEBUG2, dsm_control, PGShmemHeader::dsm_control, dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, dsm_control_segment_sane(), dsm_impl_op(), DSM_OP_DESTROY, elog(), ereport, errmsg(), dsm_control_item::handle, i, is_main_region_dsm_handle(), dsm_control_header::item, LOG, dsm_control_header::nitems, nitems, and dsm_control_item::refcnt.
Referenced by dsm_postmaster_startup().
void dsm_postmaster_startup | ( | PGShmemHeader * | shim | ) |
Definition at line 150 of file dsm.c.
References Assert(), DEBUG2, dsm_cleanup_for_mmap(), dsm_control, PGShmemHeader::dsm_control, dsm_control_bytes_needed(), dsm_control_handle, dsm_control_impl_private, dsm_control_mapped_size, DSM_HANDLE_INVALID, DSM_IMPL_MMAP, dsm_impl_op(), DSM_OP_CREATE, dsm_postmaster_shutdown(), dynamic_shared_memory_type, elog(), ERROR, IsUnderPostmaster, dsm_control_header::magic, MaxBackends, dsm_control_header::maxitems, dsm_control_header::nitems, on_shmem_exit(), PG_DYNSHMEM_CONTROL_MAGIC, PG_DYNSHMEM_FIXED_SLOTS, PG_DYNSHMEM_SLOTS_PER_BACKEND, pg_global_prng_state, pg_prng_uint32(), and PointerGetDatum().
Referenced by CreateSharedMemoryAndSemaphores().
void* dsm_segment_address | ( | dsm_segment * | seg | ) |
Definition at line 1066 of file dsm.c.
References Assert(), and dsm_segment::mapped_address.
Referenced by apw_load_buffers(), AttachSession(), autoprewarm_database_main(), dsa_attach(), dsa_create(), get_segment_by_index(), GetSessionDsmHandle(), InitializeParallelDSM(), make_new_segment(), pa_setup_dsm(), ParallelApplyWorkerMain(), ParallelWorkerMain(), setup_dynamic_shared_memory(), and test_shm_mq_main().
dsm_handle dsm_segment_handle | ( | dsm_segment * | seg | ) |
Definition at line 1094 of file dsm.c.
References dsm_segment::handle.
Referenced by apw_load_buffers(), destroy_superblock(), dsa_create(), GetSessionDsmHandle(), LaunchParallelWorkers(), make_new_segment(), pa_launch_parallel_worker(), PrintDSMLeakWarning(), ResourceOwnerForgetDSM(), and setup_background_workers().
Size dsm_segment_map_length | ( | dsm_segment * | seg | ) |
Definition at line 1076 of file dsm.c.
References Assert(), dsm_segment::mapped_address, and dsm_segment::mapped_size.
void dsm_shmem_init | ( | void | ) |
Definition at line 452 of file dsm.c.
References dsm_estimate_size(), dsm_main_space_begin, FPM_PAGE_SIZE, FreePageManagerInitialize(), FreePageManagerPut(), and ShmemInitStruct().
Referenced by CreateSharedMemoryAndSemaphores().
void dsm_unpin_mapping | ( | dsm_segment * | seg | ) |
Definition at line 907 of file dsm.c.
References Assert(), CurrentResourceOwner, ResourceOwnerEnlargeDSMs(), ResourceOwnerRememberDSM(), and dsm_segment::resowner.
void dsm_unpin_segment | ( | dsm_handle | handle | ) |
Definition at line 960 of file dsm.c.
References Assert(), dsm_control, dsm_impl_op(), dsm_impl_unpin_segment(), dsm_main_space_begin, DSM_OP_DESTROY, elog(), ERROR, dsm_control_item::first_page, FreePageManagerPut(), dsm_control_item::handle, i, dsm_control_item::impl_private_pm_handle, INVALID_CONTROL_SLOT, is_main_region_dsm_handle(), dsm_control_header::item, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), dsm_control_header::nitems, dsm_control_item::npages, dsm_control_item::pinned, dsm_control_item::refcnt, and WARNING.
Referenced by destroy_superblock(), and dsa_release_in_place().
|
inlinestatic |
Definition at line 1252 of file dsm.c.
Referenced by dsm_attach(), dsm_cleanup_using_control_segment(), dsm_create(), dsm_detach(), dsm_postmaster_shutdown(), and dsm_unpin_segment().
|
inlinestatic |
Definition at line 1233 of file dsm.c.
References dsm_control, dsm_control_header::maxitems, pg_global_prng_state, pg_leftmost_one_pos32(), and pg_prng_uint32().
Referenced by dsm_create().
void on_dsm_detach | ( | dsm_segment * | seg, |
on_dsm_detach_callback | function, | ||
Datum | arg | ||
) |
Definition at line 1103 of file dsm.c.
References dsm_segment_detach_callback::arg, arg, dsm_segment_detach_callback::function, MemoryContextAlloc(), dsm_segment_detach_callback::node, dsm_segment::on_detach, slist_push_head(), and TopMemoryContext.
Referenced by dsa_attach(), dsa_attach_in_place(), dsa_create(), dsa_create_in_place(), pq_redirect_to_shm_mq(), setup_background_workers(), SharedFileSetAttach(), SharedFileSetInit(), SharedRecordTypmodRegistryAttach(), SharedRecordTypmodRegistryInit(), and shm_mq_attach().
void reset_on_dsm_detach | ( | void | ) |
Definition at line 1141 of file dsm.c.
References dsm_segment::control_slot, dlist_iter::cur, dlist_container, dlist_foreach, dsm_segment_list, INVALID_CONTROL_SLOT, dsm_segment::on_detach, pfree(), slist_container, slist_is_empty(), and slist_pop_head_node().
Referenced by on_exit_reset().
|
static |
Definition at line 139 of file dsm.c.
Referenced by dsm_attach(), dsm_backend_startup(), dsm_create(), dsm_detach(), dsm_detach_all(), dsm_pin_segment(), dsm_postmaster_shutdown(), dsm_postmaster_startup(), dsm_unpin_segment(), and make_main_region_dsm_handle().
|
static |
Definition at line 138 of file dsm.c.
Referenced by dsm_backend_startup(), dsm_detach_all(), dsm_postmaster_shutdown(), and dsm_postmaster_startup().
|
static |
Definition at line 141 of file dsm.c.
Referenced by dsm_backend_startup(), dsm_detach_all(), dsm_postmaster_shutdown(), and dsm_postmaster_startup().
|
static |
Definition at line 140 of file dsm.c.
Referenced by dsm_backend_startup(), dsm_detach_all(), dsm_postmaster_shutdown(), and dsm_postmaster_startup().
Definition at line 107 of file dsm.c.
Referenced by dsm_attach(), dsm_backend_startup(), and dsm_create().
|
static |
Definition at line 110 of file dsm.c.
Referenced by dsm_attach(), dsm_create(), dsm_detach(), dsm_shmem_init(), and dsm_unpin_segment().
|
static |
Definition at line 129 of file dsm.c.
Referenced by dsm_attach(), dsm_backend_shutdown(), dsm_create_descriptor(), dsm_detach_all(), dsm_find_mapping(), and reset_on_dsm_detach().