PostgreSQL Source Code  git master
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 unsigned long UsedShmemSegID
 
PGDLLIMPORT void * UsedShmemSegAddr
 

Macro Definition Documentation

◆ DEFAULT_SHARED_MEMORY_TYPE

#define DEFAULT_SHARED_MEMORY_TYPE   SHMEM_TYPE_MMAP

Definition at line 75 of file pg_shmem.h.

◆ PGShmemMagic

#define PGShmemMagic   679834894

Definition at line 32 of file pg_shmem.h.

Typedef Documentation

◆ PGShmemHeader

typedef struct PGShmemHeader PGShmemHeader

Enumeration Type Documentation

◆ HugePagesType

Enumerator
HUGE_PAGES_OFF 
HUGE_PAGES_ON 
HUGE_PAGES_TRY 
HUGE_PAGES_UNKNOWN 

Definition at line 50 of file pg_shmem.h.

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

◆ PGShmemType

Enumerator
SHMEM_TYPE_WINDOWS 
SHMEM_TYPE_SYSV 
SHMEM_TYPE_MMAP 

Definition at line 59 of file pg_shmem.h.

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

Function Documentation

◆ GetHugePageSize()

void GetHugePageSize ( Size hugepagesize,
int *  mmap_flags 
)

Definition at line 478 of file sysv_shmem.c.

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

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

Referenced by CreateAnonymousSegment(), and InitializeShmemGUCs().

◆ PGSharedMemoryCreate()

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

Definition at line 699 of file sysv_shmem.c.

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

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, 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::st_dev, stat::st_ino, stat, PGShmemHeader::totalsize, UsedShmemSegAddr, UsedShmemSegID, and UsedShmemSegSize.

Referenced by CreateSharedMemoryAndSemaphores().

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 969 of file sysv_shmem.c.

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

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

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

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 316 of file sysv_shmem.c.

317 {
318  PGShmemHeader *memAddress;
320 
321  state = PGSharedMemoryAttach((IpcMemoryId) id2, NULL, &memAddress);
322  if (memAddress && shmdt((void *) memAddress) < 0)
323  elog(LOG, "shmdt(%p) failed: %m", memAddress);
324  switch (state)
325  {
326  case SHMSTATE_ENOENT:
327  case SHMSTATE_FOREIGN:
328  case SHMSTATE_UNATTACHED:
329  return false;
331  case SHMSTATE_ATTACHED:
332  return true;
333  }
334  return true;
335 }

References elog(), 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 560 of file guc_tables.c.

Referenced by GetHugePageSize().

◆ huge_pages

PGDLLIMPORT int huge_pages
extern

Definition at line 559 of file guc_tables.c.

Referenced by CreateAnonymousSegment(), and PGSharedMemoryCreate().

◆ shared_memory_type

PGDLLIMPORT int shared_memory_type
extern

Definition at line 55 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ UsedShmemSegAddr

◆ UsedShmemSegID

PGDLLIMPORT unsigned long UsedShmemSegID
extern