Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0ea4d9b

Browse files
committed
[refer #PGPRO-3213] Support shared_memory_type = mmap
1 parent ac3f55d commit 0ea4d9b

File tree

3 files changed

+62
-11
lines changed

3 files changed

+62
-11
lines changed

src/backend/port/sysv_shmem.c

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@
3232
#endif
3333

3434
#include "miscadmin.h"
35+
#include "common/file_perm.h"
3536
#include "portability/mem.h"
37+
#include "postmaster/postmaster.h"
3638
#include "storage/dsm.h"
3739
#include "storage/fd.h"
3840
#include "storage/ipc.h"
3941
#include "storage/pg_shmem.h"
4042
#include "utils/guc.h"
4143
#include "utils/pidfile.h"
4244

43-
4445
/*
4546
* As of PostgreSQL 9.3, we normally allocate only a very small amount of
4647
* System V shared memory, and only for the purposes of providing an
@@ -96,8 +97,8 @@ typedef enum
9697
unsigned long UsedShmemSegID = 0;
9798
void *UsedShmemSegAddr = NULL;
9899

100+
void *AnonymousShmem = NULL;
99101
static Size AnonymousShmemSize;
100-
static void *AnonymousShmem = NULL;
101102

102103
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size);
103104
static void IpcMemoryDetach(int status, Datum shmaddr);
@@ -535,6 +536,31 @@ CreateAnonymousSegment(Size *size)
535536
Size allocsize = *size;
536537
void *ptr = MAP_FAILED;
537538
int mmap_errno = 0;
539+
int fd = -1;
540+
void *hint = NULL;
541+
int mmap_flags = PG_MMAP_FLAGS;
542+
543+
if (OnlineUpgradePath)
544+
{
545+
int shm_flags = O_RDWR;
546+
char path[64];
547+
mmap_flags &= ~MAP_ANONYMOUS;
548+
sprintf(path, "/%d", MyProcPid);
549+
550+
if (IsOnlineUpgrade)
551+
{
552+
mmap_flags |= MAP_FIXED;
553+
hint = AnonymousShmem;
554+
}
555+
else
556+
{
557+
shm_flags |= O_CREAT|O_TRUNC;
558+
}
559+
fd = shm_open(path, shm_flags, pg_file_create_mode);
560+
if (fd < 0)
561+
ereport(FATAL,
562+
(errmsg("could not create shared memory object: %m"), 0));
563+
}
538564

539565
#ifndef MAP_HUGETLB
540566
/* PGSharedMemoryCreate should have dealt with this case */
@@ -546,15 +572,21 @@ CreateAnonymousSegment(Size *size)
546572
* Round up the request size to a suitable large value.
547573
*/
548574
Size hugepagesize;
549-
int mmap_flags;
575+
int huge_flags;
550576

551-
GetHugePageSize(&hugepagesize, &mmap_flags);
577+
GetHugePageSize(&hugepagesize, &huge_flags);
552578

553579
if (allocsize % hugepagesize != 0)
554580
allocsize += hugepagesize - (allocsize % hugepagesize);
555581

556-
ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
557-
PG_MMAP_FLAGS | mmap_flags, -1, 0);
582+
if (fd >= 0 && !IsOnlineUpgrade)
583+
{
584+
if (ftruncate(fd, allocsize) < 0)
585+
ereport(FATAL,
586+
(errmsg("could not truncate shared memory file: %m"), 0));
587+
}
588+
ptr = mmap(hint, allocsize, PROT_READ | PROT_WRITE,
589+
huge_flags | mmap_flags, fd, 0);
558590
mmap_errno = errno;
559591
if (huge_pages == HUGE_PAGES_TRY && ptr == MAP_FAILED)
560592
elog(DEBUG1, "mmap(%zu) with MAP_HUGETLB failed, huge pages disabled: %m",
@@ -569,8 +601,14 @@ CreateAnonymousSegment(Size *size)
569601
* to non-huge pages.
570602
*/
571603
allocsize = *size;
572-
ptr = mmap(NULL, allocsize, PROT_READ | PROT_WRITE,
573-
PG_MMAP_FLAGS, -1, 0);
604+
if (fd >= 0 && !IsOnlineUpgrade)
605+
{
606+
if (ftruncate(fd, allocsize) < 0)
607+
ereport(FATAL,
608+
(errmsg("could not truncate shared memory file: %m"), 0));
609+
}
610+
ptr = mmap(hint, allocsize, PROT_READ | PROT_WRITE,
611+
mmap_flags, fd, 0);
574612
mmap_errno = errno;
575613
}
576614

@@ -758,7 +796,7 @@ PGSharedMemoryCreate(Size size,
758796

759797
*shim = hdr;
760798
if (IsOnlineUpgrade)
761-
return hdr;
799+
return AnonymousShmem ? AnonymousShmem : (void*)hdr;
762800

763801
hdr->creatorPID = getpid();
764802
hdr->magic = PGShmemMagic;
@@ -834,6 +872,16 @@ PGSharedMemoryReAttach(void)
834872
dsm_set_control_handle(hdr->dsm_control);
835873

836874
UsedShmemSegAddr = hdr; /* probably redundant */
875+
876+
if (IsOnlineUpgrade && AnonymousShmem)
877+
{
878+
Size size = hdr->totalsize;
879+
void* addr = CreateAnonymousSegment(&size);
880+
Assert(addr == AnonymousShmem);
881+
Assert(size == hdr->totalsize);
882+
AnonymousShmemSize = size;
883+
elog(LOG, "Online upgrade maps anonymous segment to %p", addr);
884+
}
837885
}
838886

839887
/*

src/backend/postmaster/postmaster.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ typedef struct
574574
HANDLE UsedShmemSegID;
575575
#endif
576576
void *UsedShmemSegAddr;
577+
void *AnonymousShmem;
577578
slock_t *ShmemLock;
578579
VariableCache ShmemVariableCache;
579580
#ifndef HAVE_SPINLOCKS
@@ -622,7 +623,7 @@ static bool SavePostmasterParameters(void)
622623
#endif
623624
param.UsedShmemSegID = UsedShmemSegID;
624625
param.UsedShmemSegAddr = UsedShmemSegAddr;
625-
626+
param.AnonymousShmem = AnonymousShmem;
626627
param.ShmemLock = ShmemLock;
627628
param.ShmemVariableCache = ShmemVariableCache;
628629

@@ -722,6 +723,7 @@ RestorePostmasterParameters(void)
722723
#endif
723724
UsedShmemSegID = param.UsedShmemSegID;
724725
UsedShmemSegAddr = param.UsedShmemSegAddr;
726+
AnonymousShmem = param.AnonymousShmem;
725727

726728
ShmemLock = param.ShmemLock;
727729
ShmemVariableCache = param.ShmemVariableCache;
@@ -1244,7 +1246,7 @@ PostmasterMain(int argc, char *argv[])
12441246
if (IsOnlineUpgrade)
12451247
{
12461248
PGSharedMemoryReAttach();
1247-
InitShmemAccess(UsedShmemSegAddr);
1249+
InitShmemAccess(AnonymousShmem ? AnonymousShmem : UsedShmemSegAddr);
12481250
IsUnderPostmaster = true;
12491251
/* Attach process to shared data structures */
12501252
CreateSharedMemoryAndSemaphores();

src/include/storage/pg_shmem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern HANDLE UsedShmemSegID;
6868
extern void *ShmemProtectiveRegion;
6969
#endif
7070
extern void *UsedShmemSegAddr;
71+
extern void *AnonymousShmem;
7172

7273
#if !defined(WIN32) && !defined(EXEC_BACKEND)
7374
#define DEFAULT_SHARED_MEMORY_TYPE SHMEM_TYPE_MMAP

0 commit comments

Comments
 (0)