From 58d02490a94a0a4c23fd5f7fb060c46e81862aae Mon Sep 17 00:00:00 2001
From: Jakub Wartak <jakub.wartak@enterprisedb.com>
Date: Fri, 7 Feb 2025 08:07:06 +0100
Subject: [PATCH] Add optional dependency to libnuma for basic NUMA awareness
 routines

---
 .cirrus.tasks.yml          |  4 ++++
 configure.ac               | 13 +++++++++++++
 meson.build                | 17 +++++++++++++++++
 meson_options.txt          |  3 +++
 src/Makefile.global.in     |  1 +
 src/backend/Makefile       |  3 +++
 src/include/pg_config.h.in |  3 +++
 src/makefiles/meson.build  |  3 +++
 8 files changed, 47 insertions(+)

diff --git a/.cirrus.tasks.yml b/.cirrus.tasks.yml
index cfe2117e02e..db3e986957a 100644
--- a/.cirrus.tasks.yml
+++ b/.cirrus.tasks.yml
@@ -325,6 +325,10 @@ task:
         SANITIZER_FLAGS: -fsanitize=address
         PG_TEST_PG_COMBINEBACKUP_MODE: --copy-file-range
 
+
+      # FIXME: use or not the libnuma?
+      #      --with-libnuma \
+      #
       # Normally, the "relation segment" code basically has no coverage in our
       # tests, because we (quite reasonably) don't generate tables large
       # enough in tests. We've had plenty bugs that we didn't notice due the
diff --git a/configure.ac b/configure.ac
index f56681e0d91..fbdacc9b240 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1007,6 +1007,19 @@ fi
 AC_SUBST(with_uuid)
 
 
+#
+# libnuma
+#
+AC_MSG_CHECKING([whether to build with libnuma support])
+PGAC_ARG_BOOL(with, libnuma, no, [use libnuma for NUMA awareness],
+              [AC_DEFINE([USE_LIBNUMA], 1, [Define to build with NUMA awareness support. (--with-libnuma)])])
+AC_MSG_RESULT([$with_libnuma])
+AC_SUBST(with_libnuma)
+
+if test "$with_libnuma" = yes ; then
+  AC_CHECK_LIB(numa,    numa_available, [], [AC_MSG_ERROR([library 'libnuma' is required for NUMA awareness])])
+fi
+
 #
 # XML
 #
diff --git a/meson.build b/meson.build
index 1ceadb9a830..d077ff80889 100644
--- a/meson.build
+++ b/meson.build
@@ -853,6 +853,21 @@ else
 endif
 
 
+###############################################################
+# Library: libnuma
+###############################################################
+
+libnumaopt = get_option('libnuma')
+libnuma = dependency('libnuma', required: libnumaopt)
+if not libnuma.found()
+  libnuma = cc.find_library('numa', required: libnumaopt, dirs: test_lib_d)
+endif
+if libnuma.found()
+  cdata.set('USE_LIBNUMA', 1)
+else
+  libnuma = not_found_dep
+endif
+
 
 ###############################################################
 # Library: libxml
@@ -3068,6 +3083,7 @@ backend_both_deps += [
   icu_i18n,
   ldap,
   libintl,
+  libnuma,
   libxml,
   lz4,
   pam,
@@ -3720,6 +3736,7 @@ if meson.version().version_compare('>=0.57')
       'gss': gssapi,
       'icu': icu,
       'ldap': ldap,
+      'libnuma': libnuma,
       'libxml': libxml,
       'libxslt': libxslt,
       'llvm': llvm,
diff --git a/meson_options.txt b/meson_options.txt
index d9c7ddccbc4..4cf81b6ce25 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -103,6 +103,9 @@ option('ldap', type: 'feature', value: 'auto',
 option('libedit_preferred', type: 'boolean', value: false,
   description: 'Prefer BSD Libedit over GNU Readline')
 
+option('libnuma', type: 'feature', value: 'auto',
+  description: 'NUMA awareness support')
+
 option('libxml', type: 'feature', value: 'auto',
   description: 'XML support')
 
diff --git a/src/Makefile.global.in b/src/Makefile.global.in
index bbe11e75bf0..9c3fb2a4713 100644
--- a/src/Makefile.global.in
+++ b/src/Makefile.global.in
@@ -190,6 +190,7 @@ with_systemd	= @with_systemd@
 with_gssapi	= @with_gssapi@
 with_krb_srvnam	= @with_krb_srvnam@
 with_ldap	= @with_ldap@
+with_libnuma	= @with_libnuma@
 with_libxml	= @with_libxml@
 with_libxslt	= @with_libxslt@
 with_llvm	= @with_llvm@
diff --git a/src/backend/Makefile b/src/backend/Makefile
index 42d4a28e5aa..bff9f077a8c 100644
--- a/src/backend/Makefile
+++ b/src/backend/Makefile
@@ -54,6 +54,9 @@ ifeq ($(with_systemd),yes)
 LIBS += -lsystemd
 endif
 
+# FIXME: filter-out / with/without with_libnuma?
+LIBS += $(LIBNUMA_LIBS)
+
 override LDFLAGS := $(LDFLAGS) $(LDFLAGS_EX) $(LDFLAGS_EX_BE)
 
 ##########################################################################
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 07b2f798abd..88b0d5330b6 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -663,6 +663,9 @@
 /* Define to 1 to build with LDAP support. (--with-ldap) */
 #undef USE_LDAP
 
+/* Define to 1 to build with NUMA awareness support. (--with-libnuma) */
+#undef USE_LIBNUMA
+
 /* Define to 1 to build with XML support. (--with-libxml) */
 #undef USE_LIBXML
 
diff --git a/src/makefiles/meson.build b/src/makefiles/meson.build
index d49b2079a44..211cc3ca0eb 100644
--- a/src/makefiles/meson.build
+++ b/src/makefiles/meson.build
@@ -199,6 +199,8 @@ pgxs_empty = [
   'PTHREAD_CFLAGS', 'PTHREAD_LIBS',
 
   'ICU_LIBS',
+
+  'LIBNUMA_CFLAGS', 'LIBNUMA_LIBS'
 ]
 
 if host_system == 'windows' and cc.get_argument_syntax() != 'msvc'
@@ -229,6 +231,7 @@ pgxs_deps = {
   'gssapi': gssapi,
   'icu': icu,
   'ldap': ldap,
+  'libnuma': libnuma,
   'libxml': libxml,
   'libxslt': libxslt,
   'llvm': llvm,
-- 
2.39.5

