Re: [HACKERS] OK, so culicidae is *still* broken - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [HACKERS] OK, so culicidae is *still* broken
Date
Msg-id 1842.1492289285@sss.pgh.pa.us
Whole thread Raw
In response to Re: [HACKERS] OK, so culicidae is *still* broken  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [HACKERS] OK, so culicidae is *still* broken  (Andres Freund <andres@anarazel.de>)
List pgsql-hackers
I wrote:
> I think what may be the most effective way to proceed is to provide
> a way to force the shmem segment to be mapped at a chosen address.
> It looks like, at least on x86_64 Linux, mapping shmem at
> 0x00007E0000000000 would work reliably.

> Since we only care about this for testing purposes, I don't think
> it has to be done in any very clean or even documented way.
> I'm inclined to propose that we put something into sysv_shmem.c
> that will check for an environment variable named, say, PG_SHMEM_ADDR,
> and if it's set will use the value as the address in the initial
> shmat() call.  For a bit of extra safety we could do that only in
> EXEC_BACKEND builds.

Concretely, I propose the attached patch.  We'd have to put it into
all supported branches, since culicidae is showing intermittent
"could not reattach to shared memory" failures in all the branches.

            regards, tom lane

diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 77cd8f3..15ae672 100644
*** a/src/backend/port/sysv_shmem.c
--- b/src/backend/port/sysv_shmem.c
*************** static void *
*** 102,109 ****
--- 102,129 ----
  InternalIpcMemoryCreate(IpcMemoryKey memKey, Size size)
  {
      IpcMemoryId shmid;
+     void       *requestedAddress = NULL;
      void       *memAddress;

+     /*
+      * Normally we just pass requestedAddress = NULL to shmat(), allowing the
+      * system to choose where the segment gets mapped.  But in an EXEC_BACKEND
+      * build, it's possible for whatever is chosen in the postmaster to not
+      * work for backends, due to variations in address space layout.  As a
+      * rather klugy workaround, allow the user to specify the address to use
+      * via setting the environment variable PG_SHMEM_ADDR.  (If this were of
+      * interest for anything except debugging, we'd probably create a cleaner
+      * and better-documented way to set it, such as a GUC.)
+      */
+ #ifdef EXEC_BACKEND
+     {
+         char       *pg_shmem_addr = getenv("PG_SHMEM_ADDR");
+
+         if (pg_shmem_addr)
+             requestedAddress = (void *) strtoul(pg_shmem_addr, NULL, 0);
+     }
+ #endif
+
      shmid = shmget(memKey, size, IPC_CREAT | IPC_EXCL | IPCProtection);

      if (shmid < 0)
*************** InternalIpcMemoryCreate(IpcMemoryKey mem
*** 203,209 ****
      on_shmem_exit(IpcMemoryDelete, Int32GetDatum(shmid));

      /* OK, should be able to attach to the segment */
!     memAddress = shmat(shmid, NULL, PG_SHMAT_FLAGS);

      if (memAddress == (void *) -1)
          elog(FATAL, "shmat(id=%d) failed: %m", shmid);
--- 223,229 ----
      on_shmem_exit(IpcMemoryDelete, Int32GetDatum(shmid));

      /* OK, should be able to attach to the segment */
!     memAddress = shmat(shmid, requestedAddress, PG_SHMAT_FLAGS);

      if (memAddress == (void *) -1)
          elog(FATAL, "shmat(id=%d) failed: %m", shmid);

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: [HACKERS] Cutting initdb's runtime (Perl question embedded)
Next
From: Andres Freund
Date:
Subject: Re: [HACKERS] OK, so culicidae is *still* broken