Fix huge_pages on Windows
authorMichael Paquier <michael@paquier.xyz>
Sat, 17 Sep 2022 06:39:55 +0000 (15:39 +0900)
committerMichael Paquier <michael@paquier.xyz>
Sat, 17 Sep 2022 06:39:55 +0000 (15:39 +0900)
Since Windows 10 1703, it is additionally necessary to pass a flag
called FILE_MAP_LARGE_PAGES to MapViewOfFile() to enable large pages at
map time.  This flag is ignored on older versions of Windows, where
large pages should still be able to work properly without setting it.
Note that the flag would be set only for binaries that knew about it at
compile-time, which should be more or less all the Windows environments
these days.

Since 495ed0e, Windows 10 is the minimum version of Windows supported by
Postgres, making this change easy to reason about on HEAD.  Per
discussion, no backpatch is done for the moment.

Reported-by: Okano Naoki
Author: Thomas Munro
Reviewed-by: Tom Lane, Michael Paquier, Julien Rouhaud
Discussion: https://postgr.es/m/17448-0a96583a67edb1f7@postgresql.org

src/backend/port/win32_shmem.c

index d3c9b0f098784d96f08c937906a661d62e9b19e1..8e95f65e7b31d82ae52cc5388aa5cda549b432c4 100644 (file)
@@ -218,6 +218,7 @@ PGSharedMemoryCreate(Size size,
    SIZE_T      largePageSize = 0;
    Size        orig_size = size;
    DWORD       flProtect = PAGE_READWRITE;
+   DWORD       desiredAccess;
 
    ShmemProtectiveRegion = VirtualAlloc(NULL, PROTECTIVE_REGION_SIZE,
                                         MEM_RESERVE, PAGE_NOACCESS);
@@ -355,12 +356,19 @@ retry:
    if (!CloseHandle(hmap))
        elog(LOG, "could not close handle to shared memory: error code %lu", GetLastError());
 
+   desiredAccess = FILE_MAP_WRITE | FILE_MAP_READ;
+
+#ifdef FILE_MAP_LARGE_PAGES
+   /* Set large pages if wanted. */
+   if ((flProtect & SEC_LARGE_PAGES) != 0)
+       desiredAccess |= FILE_MAP_LARGE_PAGES;
+#endif
 
    /*
     * Get a pointer to the new shared memory segment. Map the whole segment
     * at once, and let the system decide on the initial address.
     */
-   memAddress = MapViewOfFileEx(hmap2, FILE_MAP_WRITE | FILE_MAP_READ, 0, 0, 0, NULL);
+   memAddress = MapViewOfFileEx(hmap2, desiredAccess, 0, 0, 0, NULL);
    if (!memAddress)
        ereport(FATAL,
                (errmsg("could not create shared memory segment: error code %lu", GetLastError()),