Re: Align large shared memory allocations - Mailing list pgsql-patches

From Manfred Spraul
Subject Re: Align large shared memory allocations
Date
Msg-id 3F6D8249.6070702@colorfullife.com
Whole thread Raw
In response to Re: Align large shared memory allocations  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Align large shared memory allocations
List pgsql-patches
Tom Lane wrote:

>Manfred Spraul <manfred@colorfullife.com> writes:
>
>
>>Attached is a patch that aligns large shared memory allocations beyond
>>MAXIMUM_ALIGNOF. The reason for this is that Intel's cpus have a fast
>>path for bulk memory copies that only works with aligned addresses.
>>
>>
>
>This patch is missing a demonstration that it's actually worth anything.
>What kind of performance gain do you get?
>
>
7.4cvs on a 1.13 GHz Intel Celeron mobile, 384 MB RAM, "Severn" RedHat
Linux 2.4 beta, postmaster -N 30 -B 64, data directory on ramdisk,
pgbench -c 10 -s 11 -t 1000:
Without the patch: 124 tps
with the patch: 130 tps.

I've reduced the buffer setting to 64 because without that, a too large
part of the database was cached by postgres. I expect that with all
Intel Pentium III chips, it will be worth 10-20% less system time. I had
around 30% system time after reducing the number of buffers, thus the
~5% performance improvement.

>We don't really have arch-dependent header files.  What I'd be inclined
>to do is "#define ALIGNOF_BUFFER 32" in pg_config_manual.h, then
>#define BUFFERALIGN(LEN) to parallel the other TYPEALIGN macros in c.h,
>and finally use that in the ShmemAlloc code.
>
>
Ok, new patch attached.

--
    Manfred
Index: src/backend/storage/ipc/shmem.c
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/storage/ipc/shmem.c,v
retrieving revision 1.70
diff -u -r1.70 shmem.c
--- src/backend/storage/ipc/shmem.c    4 Aug 2003 02:40:03 -0000    1.70
+++ src/backend/storage/ipc/shmem.c    21 Sep 2003 07:53:13 -0000
@@ -131,6 +131,7 @@
 void *
 ShmemAlloc(Size size)
 {
+    uint32        newStart;
     uint32        newFree;
     void       *newSpace;

@@ -146,10 +147,14 @@

     SpinLockAcquire(ShmemLock);

-    newFree = shmemseghdr->freeoffset + size;
+    newStart = shmemseghdr->freeoffset;
+    if (size >= BLCKSZ)
+        newStart = BUFFERALIGN(newStart);
+
+    newFree = newStart + size;
     if (newFree <= shmemseghdr->totalsize)
     {
-        newSpace = (void *) MAKE_PTR(shmemseghdr->freeoffset);
+        newSpace = (void *) MAKE_PTR(newStart);
         shmemseghdr->freeoffset = newFree;
     }
     else
Index: src/include/c.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/c.h,v
retrieving revision 1.152
diff -u -r1.152 c.h
--- src/include/c.h    4 Aug 2003 02:40:10 -0000    1.152
+++ src/include/c.h    21 Sep 2003 07:53:14 -0000
@@ -529,6 +529,7 @@
 #define LONGALIGN(LEN)            TYPEALIGN(ALIGNOF_LONG, (LEN))
 #define DOUBLEALIGN(LEN)        TYPEALIGN(ALIGNOF_DOUBLE, (LEN))
 #define MAXALIGN(LEN)            TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
+#define BUFFERALIGN(LEN)        TYPEALIGN(ALIGNOF_BUFFER, (LEN))


 /* ----------------------------------------------------------------
Index: src/include/pg_config_manual.h
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/include/pg_config_manual.h,v
retrieving revision 1.5
diff -u -r1.5 pg_config_manual.h
--- src/include/pg_config_manual.h    4 Aug 2003 00:43:29 -0000    1.5
+++ src/include/pg_config_manual.h    21 Sep 2003 07:53:14 -0000
@@ -176,6 +176,14 @@
  */
 #define MAX_RANDOM_VALUE  (0x7FFFFFFF)

+/*
+ * Alignment of the disk blocks in the shared memory area.
+ * A significant amount of the total system time is required for
+ * copying disk blocks between the os buffers and the cache in the
+ * shared memory area. Some cpus (most notably Intel Pentium III)
+ * prefer well-aligned addresses for memory copies.
+ */
+#define ALIGNOF_BUFFER    32

 /*
  *------------------------------------------------------------------------

pgsql-patches by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: contrib mode - pgenv
Next
From: Mike Mascari
Date:
Subject: Re: contrib mode - pgenv