From 46c6ada322517c4246ea16597475c392d0b33005 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <bossartn@amazon.com>
Date: Tue, 7 Sep 2021 16:46:52 +0000
Subject: [PATCH v7 2/3] Introduce huge_pages_required GUC.

This runtime-computed GUC shows the number of huge pages required
for the server's main shared memory area.  Like shared_memory_size,
it cannot be viewed with 'postgres -C' yet.
---
 doc/src/sgml/config.sgml       | 21 +++++++++++++++++++++
 src/backend/port/sysv_shmem.c  | 16 +++++++++++-----
 src/backend/port/win32_shmem.c | 14 ++++++++++++++
 src/backend/storage/ipc/ipci.c | 12 ++++++++++++
 src/backend/utils/misc/guc.c   | 12 ++++++++++++
 src/include/storage/pg_shmem.h |  1 +
 6 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index ef0e2a7746..b27d8aff15 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -10101,6 +10101,27 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
       </listitem>
      </varlistentry>
 
+     <varlistentry id="guc-huge-pages-required" xreflabel="huge_pages_required">
+      <term><varname>huge_pages_required</varname> (<type>integer</type>)
+      <indexterm>
+       <primary><varname>huge_pages_required</varname> configuration parameter</primary>
+      </indexterm>
+      </term>
+      <listitem>
+       <para>
+        Reports the number of huge pages that are required for the main
+        shared memory area based on the specified
+        <xref linkend="guc-huge-page-size"/>.  If huge pages are not supported,
+        this will be <literal>-1</literal>.
+       </para>
+       <para>
+        This setting is supported only on Linux.  It is always set to
+        <literal>-1</literal> on other platforms.  For more details about using
+        huge pages on Linux, see <xref linkend="linux-huge-pages"/>.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry id="guc-integer-datetimes" xreflabel="integer_datetimes">
       <term><varname>integer_datetimes</varname> (<type>boolean</type>)
       <indexterm>
diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index 9de96edf6a..125e2d47ec 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -456,8 +456,6 @@ PGSharedMemoryAttach(IpcMemoryId shmId,
 	return shmStat.shm_nattch == 0 ? SHMSTATE_UNATTACHED : SHMSTATE_ATTACHED;
 }
 
-#ifdef MAP_HUGETLB
-
 /*
  * Identify the huge page size to use, and compute the related mmap flags.
  *
@@ -476,11 +474,19 @@ PGSharedMemoryAttach(IpcMemoryId shmId,
  * such as increasing shared_buffers to absorb the extra space.
  *
  * Returns the (real, assumed or config provided) page size into *hugepagesize,
- * and the hugepage-related mmap flags to use into *mmap_flags.
+ * and the hugepage-related mmap flags to use into *mmap_flags.  If huge pages
+ * is not supported, *hugepagesize and *mmap_flags will be set to 0.
  */
-static void
+void
 GetHugePageSize(Size *hugepagesize, int *mmap_flags)
 {
+#ifndef MAP_HUGETLB
+
+	*hugepagesize = 0;
+	*mmap_flags = 0;
+
+#else
+
 	Size		default_hugepagesize = 0;
 
 	/*
@@ -553,9 +559,9 @@ GetHugePageSize(Size *hugepagesize, int *mmap_flags)
 		*mmap_flags |= (shift & MAP_HUGE_MASK) << MAP_HUGE_SHIFT;
 	}
 #endif
-}
 
 #endif							/* MAP_HUGETLB */
+}
 
 /*
  * Creates an anonymous mmap()ed shared memory segment.
diff --git a/src/backend/port/win32_shmem.c b/src/backend/port/win32_shmem.c
index d7a71992d8..90de2ab4e1 100644
--- a/src/backend/port/win32_shmem.c
+++ b/src/backend/port/win32_shmem.c
@@ -605,3 +605,17 @@ pgwin32_ReserveSharedMemoryRegion(HANDLE hChild)
 
 	return true;
 }
+
+/*
+ * This function is provided for consistency with sysv_shmem.c and does not
+ * provide any useful information for Windows.  To obtain the large page size,
+ * use GetLargePageMinimum() instead.
+ *
+ * This always sets *hugepagesize and *mmap_flags to 0.
+ */
+void
+GetHugePageSize(Size *hugepagesize, int *mmap_flags)
+{
+	*hugepagesize = 0;
+	*mmap_flags = 0;
+}
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 4b7b2faa4c..14de7afe0a 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -326,6 +326,9 @@ InitializeShmemGUCs(void)
 	char	buf[64];
 	Size	size_b;
 	Size	size_mb;
+	Size	hp_size;
+	Size	hp_required;
+	int		unused;
 
 	/*
 	 * Calculate the shared memory size and round up to the nearest
@@ -335,4 +338,13 @@ InitializeShmemGUCs(void)
 	size_mb = add_size(size_b, (1024 * 1024) - 1) / (1024 * 1024);
 	sprintf(buf, "%lu", size_mb);
 	SetConfigOption("shared_memory_size", buf, PGC_INTERNAL, PGC_S_OVERRIDE);
+
+	/* Calculate the number of huge pages required */
+	GetHugePageSize(&hp_size, &unused);
+	if (hp_size != 0)
+	{
+		hp_required = (size_b / hp_size) + 1;
+		sprintf(buf, "%lu", hp_required);
+		SetConfigOption("huge_pages_required", buf, PGC_INTERNAL, PGC_S_OVERRIDE);
+	}
 }
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index a11387c5ce..1a30d635fc 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -621,6 +621,7 @@ char	   *pgstat_temp_directory;
 char	   *application_name;
 
 int			shmem_size_mb;
+int			huge_pages_required;
 
 int			tcp_keepalives_idle;
 int			tcp_keepalives_interval;
@@ -2225,6 +2226,17 @@ static struct config_int ConfigureNamesInt[] =
 		NULL, NULL, NULL
 	},
 
+	{
+		{"huge_pages_required", PGC_INTERNAL, RESOURCES_MEM,
+			gettext_noop("Shows the number of huge pages needed for the main shared memory area."),
+			gettext_noop("-1 indicates that the value could not be determined."),
+			GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE
+		},
+		&huge_pages_required,
+		-1, -1, INT_MAX,
+		NULL, NULL, NULL
+	},
+
 	{
 		/* This is PGC_SUSET to prevent hiding from log_lock_waits. */
 		{"deadlock_timeout", PGC_SUSET, LOCK_MANAGEMENT,
diff --git a/src/include/storage/pg_shmem.h b/src/include/storage/pg_shmem.h
index 059df1b72c..518eb86065 100644
--- a/src/include/storage/pg_shmem.h
+++ b/src/include/storage/pg_shmem.h
@@ -87,5 +87,6 @@ extern PGShmemHeader *PGSharedMemoryCreate(Size size,
 										   PGShmemHeader **shim);
 extern bool PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2);
 extern void PGSharedMemoryDetach(void);
+extern void GetHugePageSize(Size *hugepagesize, int *mmap_flags);
 
 #endif							/* PG_SHMEM_H */
-- 
2.16.6

