]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEV: ncpu: add a simple utility to help with NUMA development
authorWilly Tarreau <w@1wt.eu>
Wed, 8 Jan 2025 07:48:16 +0000 (08:48 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 8 Jan 2025 10:26:05 +0000 (11:26 +0100)
Collecting captures of /sys isn't sufficient for NUMA development because
haproxy detects the number of CPUs at boot time and will not be able to
inspect more than this number. Let's just have a small utility to report
a fake number of CPUs, that will be loaded using LD_PRELOAD. It checks
the NCPU variable if it exists and will present this number of CPUs, or
if it does not exist, will expose the maximum supported number.

dev/ncpu/Makefile [new file with mode: 0644]
dev/ncpu/ncpu.c [new file with mode: 0644]

diff --git a/dev/ncpu/Makefile b/dev/ncpu/Makefile
new file mode 100644 (file)
index 0000000..2c1c42f
--- /dev/null
@@ -0,0 +1,15 @@
+include ../../include/make/verbose.mk
+
+CC       = cc
+OPTIMIZE = -O2 -g
+DEFINE   =
+INCLUDE  =
+OBJS     = ncpu.so
+
+all:   $(OBJS)
+
+%.so: %.c
+       $(cmd_CC) $(OPTIMIZE) $(DEFINE) $(INCLUDE) -fPIC -shared -o $@ $^
+
+clean:
+       rm -f $(OBJS) *.[oas] *~
diff --git a/dev/ncpu/ncpu.c b/dev/ncpu/ncpu.c
new file mode 100644 (file)
index 0000000..62f9fde
--- /dev/null
@@ -0,0 +1,32 @@
+#define _GNU_SOURCE
+#include <sched.h>
+#include <stdlib.h>
+#include <string.h>
+
+// gcc -fPIC -shared -O2 -o ncpu{.so,.c}
+// NCPU=16 LD_PRELOAD=$PWD/ncpu.so command args...
+
+/* return a cpu_set having the first $NCPU set */
+int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask)
+{
+       const char *ncpu;
+       int i, n;
+
+       CPU_ZERO_S(cpusetsize, mask);
+
+       ncpu = getenv("NCPU");
+       n = ncpu ? atoi(ncpu) : CPU_SETSIZE;
+       if (n < 0 || n > CPU_SETSIZE)
+               n = CPU_SETSIZE;
+
+       for (i = 0; i < n; i++)
+               CPU_SET_S(i, cpusetsize, mask);
+
+       return 0;
+}
+
+/* silently ignore the operation */
+int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask)
+{
+       return 0;
+}