Align all shared memory allocations to cache line boundaries.
authorRobert Haas <rhaas@postgresql.org>
Tue, 5 Apr 2016 19:47:49 +0000 (15:47 -0400)
committerRobert Haas <rhaas@postgresql.org>
Tue, 5 Apr 2016 19:47:49 +0000 (15:47 -0400)
Experimentation shows this only costs about 6kB, which seems well
worth it given the major performance effects that can be caused
by insufficient alignment, especially on larger systems.

Discussion: 14166.1458924422@sss.pgh.pa.us

src/backend/storage/ipc/shmem.c

index 81506eaffd07f65b9cf3cfa395964c2bfe92f6ed..1ad68cda4e517983f0837083197d2d312ce90c4a 100644 (file)
@@ -171,9 +171,17 @@ ShmemAlloc(Size size)
    void       *newSpace;
 
    /*
-    * ensure all space is adequately aligned.
+    * Ensure all space is adequately aligned.  We used to only MAXALIGN this
+    * space but experience has proved that on modern systems that is not good
+    * enough.  Many parts of the system are very sensitive to critical data
+    * structures getting split across cache line boundaries.  To avoid that,
+    * attempt to align the beginning of the allocation to a cache line
+    * boundary.  The calling code will still need to be careful about how it
+    * uses the allocated space - e.g. by padding each element in an array of
+    * structures out to a power-of-two size - but without this, even that
+    * won't be sufficient.
     */
-   size = MAXALIGN(size);
+   size = CACHELINEALIGN(size);
 
    Assert(ShmemSegHdr != NULL);