docker-numcpu_override/numcpu_override.c

33 lines
721 B
C
Raw Normal View History

2024-04-30 09:12:09 +02:00
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
typedef long (*orig_sysconf_f_type)(int name);
long sysconf(int name) {
orig_sysconf_f_type orig;
orig = (orig_sysconf_f_type)dlsym(RTLD_NEXT, "sysconf");
if (name != _SC_NPROCESSORS_CONF && name != _SC_NPROCESSORS_ONLN) {
return orig(name);
}
printf("Overriding call sysconf %d\n", name);
long numcpus = 0;
char *env = getenv("NUMCPUS");
printf("Env NUMCPUS %s\n", env);
if (env != NULL && strlen(env) > 0) {
numcpus = strtol(env, NULL, 10);
}
if (numcpus == 0) {
numcpus = orig(name);
}
printf("Return %ld\n", numcpus);
fflush(stdout);
return numcpus;
}