PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pg_shmem.h File Reference
#include "storage/dsm_impl.h"
Include dependency graph for pg_shmem.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PGShmemHeader
 

Macros

#define PGShmemMagic   679834894
 
#define DEFAULT_SHARED_MEMORY_TYPE   SHMEM_TYPE_MMAP
 

Typedefs

typedef struct PGShmemHeader PGShmemHeader
 

Enumerations

enum  HugePagesType { HUGE_PAGES_OFF , HUGE_PAGES_ON , HUGE_PAGES_TRY , HUGE_PAGES_UNKNOWN }
 
enum  PGShmemType { SHMEM_TYPE_WINDOWS , SHMEM_TYPE_SYSV , SHMEM_TYPE_MMAP }
 

Functions

PGShmemHeaderPGSharedMemoryCreate (Size size, PGShmemHeader **shim)
 
bool PGSharedMemoryIsInUse (unsigned long id1, unsigned long id2)
 
void PGSharedMemoryDetach (void)
 
void GetHugePageSize (Size *hugepagesize, int *mmap_flags)
 

Variables

PGDLLIMPORT int shared_memory_type
 
PGDLLIMPORT int huge_pages
 
PGDLLIMPORT int huge_page_size
 
PGDLLIMPORT int huge_pages_status
 
PGDLLIMPORT unsigned long UsedShmemSegID
 
PGDLLIMPORT voidUsedShmemSegAddr
 

Macro Definition Documentation

◆ DEFAULT_SHARED_MEMORY_TYPE

#define DEFAULT_SHARED_MEMORY_TYPE   SHMEM_TYPE_MMAP

Definition at line 76 of file pg_shmem.h.

◆ PGShmemMagic

#define PGShmemMagic   679834894

Definition at line 32 of file pg_shmem.h.

Typedef Documentation

◆ PGShmemHeader

Enumeration Type Documentation

◆ HugePagesType

Enumerator
HUGE_PAGES_OFF 
HUGE_PAGES_ON 
HUGE_PAGES_TRY 
HUGE_PAGES_UNKNOWN 

Definition at line 51 of file pg_shmem.h.

52{
55 HUGE_PAGES_TRY, /* only for huge_pages */
56 HUGE_PAGES_UNKNOWN, /* only for huge_pages_status */
HugePagesType
Definition pg_shmem.h:52
@ HUGE_PAGES_UNKNOWN
Definition pg_shmem.h:56
@ HUGE_PAGES_ON
Definition pg_shmem.h:54
@ HUGE_PAGES_OFF
Definition pg_shmem.h:53
@ HUGE_PAGES_TRY
Definition pg_shmem.h:55

◆ PGShmemType

Enumerator
SHMEM_TYPE_WINDOWS 
SHMEM_TYPE_SYSV 
SHMEM_TYPE_MMAP 

Definition at line 60 of file pg_shmem.h.

61{
PGShmemType
Definition pg_shmem.h:61
@ SHMEM_TYPE_MMAP
Definition pg_shmem.h:64
@ SHMEM_TYPE_SYSV
Definition pg_shmem.h:63
@ SHMEM_TYPE_WINDOWS
Definition pg_shmem.h:62

Function Documentation

◆ GetHugePageSize()

void GetHugePageSize ( Size hugepagesize,
int mmap_flags 
)
extern

Definition at line 480 of file sysv_shmem.c.

481{
482#ifdef MAP_HUGETLB
483
486 int mmap_flags_local = 0;
487
488 /*
489 * System-dependent code to find out the default huge page size.
490 *
491 * On Linux, read /proc/meminfo looking for a line like "Hugepagesize:
492 * nnnn kB". Ignore any failures, falling back to the preset default.
493 */
494#ifdef __linux__
495
496 {
497 FILE *fp = AllocateFile("/proc/meminfo", "r");
498 char buf[128];
499 unsigned int sz;
500 char ch;
501
502 if (fp)
503 {
504 while (fgets(buf, sizeof(buf), fp))
505 {
506 if (sscanf(buf, "Hugepagesize: %u %c", &sz, &ch) == 2)
507 {
508 if (ch == 'k')
509 {
510 default_hugepagesize = sz * (Size) 1024;
511 break;
512 }
513 /* We could accept other units besides kB, if needed */
514 }
515 }
516 FreeFile(fp);
517 }
518 }
519#endif /* __linux__ */
520
521 if (huge_page_size != 0)
522 {
523 /* If huge page size is requested explicitly, use that. */
525 }
526 else if (default_hugepagesize != 0)
527 {
528 /* Otherwise use the system default, if we have it. */
530 }
531 else
532 {
533 /*
534 * If we fail to find out the system's default huge page size, or no
535 * huge page size is requested explicitly, assume it is 2MB. This will
536 * work fine when the actual size is less. If it's more, we might get
537 * mmap() or munmap() failures due to unaligned requests; but at this
538 * writing, there are no reports of any non-Linux systems being picky
539 * about that.
540 */
541 hugepagesize_local = 2 * 1024 * 1024;
542 }
543
545
546 /*
547 * On recent enough Linux, also include the explicit page size, if
548 * necessary.
549 */
550#if defined(MAP_HUGE_MASK) && defined(MAP_HUGE_SHIFT)
552 {
554
556 }
557#endif
558
559 /* assign the results found */
560 if (mmap_flags)
562 if (hugepagesize)
564
565#else
566
567 if (hugepagesize)
568 *hugepagesize = 0;
569 if (mmap_flags)
570 *mmap_flags = 0;
571
572#endif /* MAP_HUGETLB */
573}
size_t Size
Definition c.h:619
int FreeFile(FILE *file)
Definition fd.c:2823
FILE * AllocateFile(const char *name, const char *mode)
Definition fd.c:2624
int huge_page_size
Definition guc_tables.c:581
static uint64 pg_ceil_log2_64(uint64 num)
static char buf[DEFAULT_XLOG_SEG_SIZE]
static int fb(int x)

References AllocateFile(), buf, fb(), FreeFile(), huge_page_size, and pg_ceil_log2_64().

Referenced by CreateAnonymousSegment(), InitializeShmemGUCs(), and pg_get_shmem_pagesize().

◆ PGSharedMemoryCreate()

PGShmemHeader * PGSharedMemoryCreate ( Size  size,
PGShmemHeader **  shim 
)
extern

Definition at line 702 of file sysv_shmem.c.

704{
706 void *memAddress;
707 PGShmemHeader *hdr;
708 struct stat statbuf;
710
711 /*
712 * We use the data directory's ID info (inode and device numbers) to
713 * positively identify shmem segments associated with this data dir, and
714 * also as seeds for searching for a free shmem key.
715 */
716 if (stat(DataDir, &statbuf) < 0)
719 errmsg("could not stat data directory \"%s\": %m",
720 DataDir)));
721
722 /* Complain if hugepages demanded but we can't possibly support them */
723#if !defined(MAP_HUGETLB)
727 errmsg("huge pages not supported on this platform")));
728#endif
729
730 /* For now, we don't support huge pages in SysV memory */
734 errmsg("huge pages not supported with the current \"shared_memory_type\" setting")));
735
736 /* Room for a header? */
737 Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
738
740 {
742 AnonymousShmemSize = size;
743
744 /* Register on-exit routine to unmap the anonymous segment */
746
747 /* Now we need only allocate a minimal-sized SysV shmem block. */
748 sysvsize = sizeof(PGShmemHeader);
749 }
750 else
751 {
752 sysvsize = size;
753
754 /* huge pages are only available with mmap */
755 SetConfigOption("huge_pages_status", "off",
757 }
758
759 /*
760 * Loop till we find a free IPC key. Trust CreateDataDirLockFile() to
761 * ensure no more than one postmaster per data directory can enter this
762 * loop simultaneously. (CreateDataDirLockFile() does not entirely ensure
763 * that, but prefer fixing it over coping here.)
764 */
765 NextShmemSegID = statbuf.st_ino;
766
767 for (;;)
768 {
772
773 /* Try to create new segment */
775 if (memAddress)
776 break; /* successful create and attach */
777
778 /* Check shared memory and possibly remove and recreate */
779
780 /*
781 * shmget() failure is typically EACCES, hence SHMSTATE_FOREIGN.
782 * ENOENT, a narrow possibility, implies SHMSTATE_ENOENT, but one can
783 * safely treat SHMSTATE_ENOENT like SHMSTATE_FOREIGN.
784 */
786 if (shmid < 0)
787 {
788 oldhdr = NULL;
790 }
791 else
793
794 switch (state)
795 {
800 errmsg("pre-existing shared memory block (key %lu, ID %lu) is still in use",
801 (unsigned long) NextShmemSegID,
802 (unsigned long) shmid),
803 errhint("Terminate any old server processes associated with data directory \"%s\".",
804 DataDir)));
805 break;
806 case SHMSTATE_ENOENT:
807
808 /*
809 * To our surprise, some other process deleted since our last
810 * InternalIpcMemoryCreate(). Moments earlier, we would have
811 * seen SHMSTATE_FOREIGN. Try that same ID again.
812 */
813 elog(LOG,
814 "shared memory block (key %lu, ID %lu) deleted during startup",
815 (unsigned long) NextShmemSegID,
816 (unsigned long) shmid);
817 break;
818 case SHMSTATE_FOREIGN:
820 break;
822
823 /*
824 * The segment pertains to DataDir, and every process that had
825 * used it has died or detached. Zap it, if possible, and any
826 * associated dynamic shared memory segments, as well. This
827 * shouldn't fail, but if it does, assume the segment belongs
828 * to someone else after all, and try the next candidate.
829 * Otherwise, try again to create the segment. That may fail
830 * if some other process creates the same shmem key before we
831 * do, in which case we'll try the next key.
832 */
833 if (oldhdr->dsm_control != 0)
835 if (shmctl(shmid, IPC_RMID, NULL) < 0)
837 break;
838 }
839
840 if (oldhdr && shmdt(oldhdr) < 0)
841 elog(LOG, "shmdt(%p) failed: %m", oldhdr);
842 }
843
844 /* Initialize new segment. */
845 hdr = (PGShmemHeader *) memAddress;
846 hdr->creatorPID = getpid();
847 hdr->magic = PGShmemMagic;
848 hdr->dsm_control = 0;
849
850 /* Fill in the data directory ID info, too */
851 hdr->device = statbuf.st_dev;
852 hdr->inode = statbuf.st_ino;
853
854 /*
855 * Initialize space allocation status for segment.
856 */
857 hdr->totalsize = size;
858 hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
859 *shim = hdr;
860
861 /* Save info for possible future use */
864
865 /*
866 * If AnonymousShmem is NULL here, then we're not using anonymous shared
867 * memory, and should return a pointer to the System V shared memory
868 * block. Otherwise, the System V shared memory block is only a shim, and
869 * we must return a pointer to the real block.
870 */
871 if (AnonymousShmem == NULL)
872 return hdr;
873 memcpy(AnonymousShmem, hdr, sizeof(PGShmemHeader));
874 return (PGShmemHeader *) AnonymousShmem;
875}
#define MAXALIGN(LEN)
Definition c.h:826
#define Assert(condition)
Definition c.h:873
void dsm_cleanup_using_control_segment(dsm_handle old_control_handle)
Definition dsm.c:238
int errcode_for_file_access(void)
Definition elog.c:886
int errhint(const char *fmt,...)
Definition elog.c:1330
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define LOG
Definition elog.h:31
#define FATAL
Definition elog.h:41
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
char * DataDir
Definition globals.c:71
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4196
@ PGC_S_DYNAMIC_DEFAULT
Definition guc.h:114
@ PGC_INTERNAL
Definition guc.h:73
int huge_pages
Definition guc_tables.c:580
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
int shared_memory_type
Definition ipci.c:57
#define PGShmemMagic
Definition pg_shmem.h:32
uint64_t Datum
Definition postgres.h:70
dsm_handle dsm_control
Definition pg_shmem.h:36
ino_t inode
Definition pg_shmem.h:40
Size freeoffset
Definition pg_shmem.h:35
pid_t creatorPID
Definition pg_shmem.h:33
dev_t device
Definition pg_shmem.h:39
int32 magic
Definition pg_shmem.h:31
Size totalsize
Definition pg_shmem.h:34
static void AnonymousShmemDetach(int status, Datum arg)
Definition sysv_shmem.c:677
int IpcMemoryId
Definition sysv_shmem.c:72
IpcMemoryState
Definition sysv_shmem.c:86
@ SHMSTATE_ATTACHED
Definition sysv_shmem.c:88
@ SHMSTATE_UNATTACHED
Definition sysv_shmem.c:91
@ SHMSTATE_FOREIGN
Definition sysv_shmem.c:90
@ SHMSTATE_ENOENT
Definition sysv_shmem.c:89
@ SHMSTATE_ANALYSIS_FAILURE
Definition sysv_shmem.c:87
static Size AnonymousShmemSize
Definition sysv_shmem.c:98
key_t IpcMemoryKey
Definition sysv_shmem.c:71
unsigned long UsedShmemSegID
Definition sysv_shmem.c:95
static void * CreateAnonymousSegment(Size *size)
Definition sysv_shmem.c:600
static void * InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
Definition sysv_shmem.c:122
void * UsedShmemSegAddr
Definition sysv_shmem.c:96
static IpcMemoryState PGSharedMemoryAttach(IpcMemoryId shmId, void *attachAt, PGShmemHeader **addr)
Definition sysv_shmem.c:348
static void * AnonymousShmem
Definition sysv_shmem.c:99
#define stat
Definition win32_port.h:74
#define IPC_RMID
Definition win32_port.h:93

References AnonymousShmem, AnonymousShmemDetach(), AnonymousShmemSize, Assert, CreateAnonymousSegment(), PGShmemHeader::creatorPID, DataDir, DEBUG1, PGShmemHeader::device, dsm_cleanup_using_control_segment(), PGShmemHeader::dsm_control, elog, EnableLockPagesPrivilege(), ereport, errcode(), errcode_for_file_access(), errdetail(), errhint(), errmsg(), errmsg_internal(), ERROR, FATAL, fb(), free, PGShmemHeader::freeoffset, GetSharedMemName(), huge_pages, HUGE_PAGES_ON, HUGE_PAGES_TRY, i, PGShmemHeader::inode, InternalIpcMemoryCreate(), IPC_RMID, LOG, PGShmemHeader::magic, MAXALIGN, on_shmem_exit(), PGC_INTERNAL, PGC_S_DYNAMIC_DEFAULT, PGSharedMemoryAttach(), PGShmemMagic, pgwin32_SharedMemoryDelete(), PointerGetDatum(), PROTECTIVE_REGION_SIZE, SetConfigOption(), shared_memory_type, SHMEM_TYPE_MMAP, ShmemProtectiveRegion, SHMSTATE_ANALYSIS_FAILURE, SHMSTATE_ATTACHED, SHMSTATE_ENOENT, SHMSTATE_FOREIGN, SHMSTATE_UNATTACHED, stat, PGShmemHeader::totalsize, UsedShmemSegAddr, UsedShmemSegID, and UsedShmemSegSize.

Referenced by CreateSharedMemoryAndSemaphores().

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )
extern

Definition at line 972 of file sysv_shmem.c.

973{
974 if (UsedShmemSegAddr != NULL)
975 {
976 if ((shmdt(UsedShmemSegAddr) < 0)
978 /* Work-around for cygipc exec bug */
979 && shmdt(NULL) < 0
980#endif
981 )
982 elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
984 }
985
986 if (AnonymousShmem != NULL)
987 {
989 elog(LOG, "munmap(%p, %zu) failed: %m",
992 }
993}

References AnonymousShmem, AnonymousShmemSize, elog, fb(), LOG, ShmemProtectiveRegion, UsedShmemSegAddr, and UsedShmemSegID.

Referenced by PGSharedMemoryNoReAttach(), pgwin32_SharedMemoryDelete(), and postmaster_child_launch().

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)
extern

Definition at line 318 of file sysv_shmem.c.

319{
322
324 if (memAddress && shmdt(memAddress) < 0)
325 elog(LOG, "shmdt(%p) failed: %m", memAddress);
326 switch (state)
327 {
328 case SHMSTATE_ENOENT:
329 case SHMSTATE_FOREIGN:
331 return false;
334 return true;
335 }
336 return true;
337}

References elog, fb(), free, GetSharedMemName(), LOG, PGSharedMemoryAttach(), SHMSTATE_ANALYSIS_FAILURE, SHMSTATE_ATTACHED, SHMSTATE_ENOENT, SHMSTATE_FOREIGN, and SHMSTATE_UNATTACHED.

Referenced by CreateLockFile().

Variable Documentation

◆ huge_page_size

PGDLLIMPORT int huge_page_size
extern

Definition at line 581 of file guc_tables.c.

Referenced by GetHugePageSize().

◆ huge_pages

PGDLLIMPORT int huge_pages
extern

Definition at line 580 of file guc_tables.c.

Referenced by CreateAnonymousSegment(), and PGSharedMemoryCreate().

◆ huge_pages_status

PGDLLIMPORT int huge_pages_status
extern

Definition at line 582 of file guc_tables.c.

Referenced by pg_get_shmem_pagesize().

◆ shared_memory_type

PGDLLIMPORT int shared_memory_type
extern

Definition at line 57 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ UsedShmemSegAddr

◆ UsedShmemSegID