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

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

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

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ PGSharedMemoryDetach()

void PGSharedMemoryDetach ( void  )

Definition at line 970 of file sysv_shmem.c.

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

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

Referenced by PGSharedMemoryNoReAttach(), and pgwin32_SharedMemoryDelete().

◆ PGSharedMemoryIsInUse()

bool PGSharedMemoryIsInUse ( unsigned long  id1,
unsigned long  id2 
)

Definition at line 317 of file sysv_shmem.c.

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

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 566 of file guc_tables.c.

Referenced by GetHugePageSize().

◆ huge_pages

PGDLLIMPORT int huge_pages
extern

Definition at line 565 of file guc_tables.c.

Referenced by CreateAnonymousSegment(), and PGSharedMemoryCreate().

◆ shared_memory_type

PGDLLIMPORT int shared_memory_type
extern

Definition at line 57 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ UsedShmemSegAddr

◆ UsedShmemSegID

PGDLLIMPORT unsigned long UsedShmemSegID
extern