From: Willy Tarreau Date: Wed, 8 Jan 2025 07:48:16 +0000 (+0100) Subject: DEV: ncpu: add a simple utility to help with NUMA development X-Git-Tag: v3.2-dev3~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=25c08562cbec3fe7c4514db3baaf085475e79395;p=thirdparty%2Fhaproxy.git DEV: ncpu: add a simple utility to help with NUMA development 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. --- diff --git a/dev/ncpu/Makefile b/dev/ncpu/Makefile new file mode 100644 index 0000000000..2c1c42feb9 --- /dev/null +++ b/dev/ncpu/Makefile @@ -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 index 0000000000..62f9fde79a --- /dev/null +++ b/dev/ncpu/ncpu.c @@ -0,0 +1,32 @@ +#define _GNU_SOURCE +#include +#include +#include + +// 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; +}