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 }
 
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 74 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 

Definition at line 50 of file pg_shmem.h.

51 {
HugePagesType
Definition: pg_shmem.h:51
@ 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 58 of file pg_shmem.h.

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

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:541
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2383
int FreeFile(FILE *file)
Definition: fd.c:2581
int huge_page_size
Definition: guc_tables.c:530
static uint64 pg_ceil_log2_64(uint64 num)
Definition: pg_bitutils.h:222
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 691 of file sysv_shmem.c.

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

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(), PGSharedMemoryAttach(), PGShmemMagic, pgwin32_SharedMemoryDelete(), PointerGetDatum(), PROTECTIVE_REGION_SIZE, 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 955 of file sysv_shmem.c.

956 {
957  if (UsedShmemSegAddr != NULL)
958  {
959  if ((shmdt(UsedShmemSegAddr) < 0)
960 #if defined(EXEC_BACKEND) && defined(__CYGWIN__)
961  /* Work-around for cygipc exec bug */
962  && shmdt(NULL) < 0
963 #endif
964  )
965  elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
966  UsedShmemSegAddr = NULL;
967  }
968 
969  if (AnonymousShmem != NULL)
970  {
971  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
972  elog(LOG, "munmap(%p, %zu) failed: %m",
974  AnonymousShmem = NULL;
975  }
976 }

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

Referenced by GetHugePageSize().

◆ huge_pages

PGDLLIMPORT int huge_pages
extern

Definition at line 529 of file guc_tables.c.

Referenced by CreateAnonymousSegment(), and PGSharedMemoryCreate().

◆ shared_memory_type

PGDLLIMPORT int shared_memory_type
extern

Definition at line 54 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ UsedShmemSegAddr

◆ UsedShmemSegID

PGDLLIMPORT unsigned long UsedShmemSegID
extern