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 701 of file sysv_shmem.c.

703{
705 void *memAddress;
706 PGShmemHeader *hdr;
707 struct stat statbuf;
709
710 /*
711 * We use the data directory's ID info (inode and device numbers) to
712 * positively identify shmem segments associated with this data dir, and
713 * also as seeds for searching for a free shmem key.
714 */
715 if (stat(DataDir, &statbuf) < 0)
718 errmsg("could not stat data directory \"%s\": %m",
719 DataDir)));
720
721 /* Complain if hugepages demanded but we can't possibly support them */
722#if !defined(MAP_HUGETLB)
726 errmsg("huge pages not supported on this platform")));
727#endif
728
729 /* For now, we don't support huge pages in SysV memory */
733 errmsg("huge pages not supported with the current \"shared_memory_type\" setting")));
734
735 /* Room for a header? */
736 Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
737
739 {
741 AnonymousShmemSize = size;
742
743 /* Register on-exit routine to unmap the anonymous segment */
745
746 /* Now we need only allocate a minimal-sized SysV shmem block. */
747 sysvsize = sizeof(PGShmemHeader);
748 }
749 else
750 {
751 sysvsize = size;
752
753 /* huge pages are only available with mmap */
754 SetConfigOption("huge_pages_status", "off",
756 }
757
758 /*
759 * Loop till we find a free IPC key. Trust CreateDataDirLockFile() to
760 * ensure no more than one postmaster per data directory can enter this
761 * loop simultaneously. (CreateDataDirLockFile() does not entirely ensure
762 * that, but prefer fixing it over coping here.)
763 */
764 NextShmemSegID = statbuf.st_ino;
765
766 for (;;)
767 {
771
772 /* Try to create new segment */
774 if (memAddress)
775 break; /* successful create and attach */
776
777 /* Check shared memory and possibly remove and recreate */
778
779 /*
780 * shmget() failure is typically EACCES, hence SHMSTATE_FOREIGN.
781 * ENOENT, a narrow possibility, implies SHMSTATE_ENOENT, but one can
782 * safely treat SHMSTATE_ENOENT like SHMSTATE_FOREIGN.
783 */
785 if (shmid < 0)
786 {
787 oldhdr = NULL;
789 }
790 else
792
793 switch (state)
794 {
799 errmsg("pre-existing shared memory block (key %lu, ID %lu) is still in use",
800 (unsigned long) NextShmemSegID,
801 (unsigned long) shmid),
802 errhint("Terminate any old server processes associated with data directory \"%s\".",
803 DataDir)));
804 break;
805 case SHMSTATE_ENOENT:
806
807 /*
808 * To our surprise, some other process deleted since our last
809 * InternalIpcMemoryCreate(). Moments earlier, we would have
810 * seen SHMSTATE_FOREIGN. Try that same ID again.
811 */
812 elog(LOG,
813 "shared memory block (key %lu, ID %lu) deleted during startup",
814 (unsigned long) NextShmemSegID,
815 (unsigned long) shmid);
816 break;
817 case SHMSTATE_FOREIGN:
819 break;
821
822 /*
823 * The segment pertains to DataDir, and every process that had
824 * used it has died or detached. Zap it, if possible, and any
825 * associated dynamic shared memory segments, as well. This
826 * shouldn't fail, but if it does, assume the segment belongs
827 * to someone else after all, and try the next candidate.
828 * Otherwise, try again to create the segment. That may fail
829 * if some other process creates the same shmem key before we
830 * do, in which case we'll try the next key.
831 */
832 if (oldhdr->dsm_control != 0)
834 if (shmctl(shmid, IPC_RMID, NULL) < 0)
836 break;
837 }
838
839 if (oldhdr && shmdt(oldhdr) < 0)
840 elog(LOG, "shmdt(%p) failed: %m", oldhdr);
841 }
842
843 /* Initialize new segment. */
844 hdr = (PGShmemHeader *) memAddress;
845 hdr->creatorPID = getpid();
846 hdr->magic = PGShmemMagic;
847 hdr->dsm_control = 0;
848
849 /* Fill in the data directory ID info, too */
850 hdr->device = statbuf.st_dev;
851 hdr->inode = statbuf.st_ino;
852
853 /*
854 * Initialize space allocation status for segment.
855 */
856 hdr->totalsize = size;
857 hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
858 *shim = hdr;
859
860 /* Save info for possible future use */
863
864 /*
865 * If AnonymousShmem is NULL here, then we're not using anonymous shared
866 * memory, and should return a pointer to the System V shared memory
867 * block. Otherwise, the System V shared memory block is only a shim, and
868 * we must return a pointer to the real block.
869 */
870 if (AnonymousShmem == NULL)
871 return hdr;
872 memcpy(AnonymousShmem, hdr, sizeof(PGShmemHeader));
873 return (PGShmemHeader *) AnonymousShmem;
874}
#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:676
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 971 of file sysv_shmem.c.

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

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