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.
This commit is contained in:
Willy Tarreau 2025-01-08 08:48:16 +01:00
parent bd06502b22
commit 25c08562cb
2 changed files with 47 additions and 0 deletions

15
dev/ncpu/Makefile Normal file
View File

@ -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] *~

32
dev/ncpu/ncpu.c Normal file
View File

@ -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;
}