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

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

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

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

943 {
944  if (UsedShmemSegAddr != NULL)
945  {
946  if ((shmdt(UsedShmemSegAddr) < 0)
947 #if defined(EXEC_BACKEND) && defined(__CYGWIN__)
948  /* Work-around for cygipc exec bug */
949  && shmdt(NULL) < 0
950 #endif
951  )
952  elog(LOG, "shmdt(%p) failed: %m", UsedShmemSegAddr);
953  UsedShmemSegAddr = NULL;
954  }
955 
956  if (AnonymousShmem != NULL)
957  {
958  if (munmap(AnonymousShmem, AnonymousShmemSize) < 0)
959  elog(LOG, "munmap(%p, %zu) failed: %m",
961  AnonymousShmem = NULL;
962  }
963 }

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

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

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 681 of file guc.c.

Referenced by GetHugePageSize().

◆ huge_pages

PGDLLIMPORT int huge_pages
extern

Definition at line 680 of file guc.c.

Referenced by CreateAnonymousSegment(), and PGSharedMemoryCreate().

◆ shared_memory_type

PGDLLIMPORT int shared_memory_type
extern

Definition at line 53 of file ipci.c.

Referenced by PGSharedMemoryCreate().

◆ UsedShmemSegAddr

◆ UsedShmemSegID

PGDLLIMPORT unsigned long UsedShmemSegID
extern