The fact is, glibc is just total crap. I tried to send uli a patch to just add caching. No go. I sent *another* patch to at least make glibc use a sane interface (and the cache if it needs to fall back on /proc/stat for some legacy reason). We'll see what happens.
FWIW a rerun with this modified LD_PRELOAD that does caching seems to have the same performance as the version that does sched_getaffinity. So you're right. Caching indeed helps and my assumption that the child would only do it once was incorrect. The only problem I see with it is that it doesn't handle CPU hotplug, but Paul's suggestion would fix that too.
Paul Eggbert suggested "caching for one second" - by just calling "gettimtofday()" to see how old the cache is. That would work too.
Maybe we need a "standard LD_PRELOAD library to improve glibc" @) -Andi
// gcc -fPIC -shared sysconf-caching.c -ldl -o sysconf-caching.so #define _GNU_SOURCE 1 #include <dlfcn.h> #include <sched.h> #include <unistd.h> static long int (*real_sysconf)(int name); long int sysconf(int name) { if (!real_sysconf) real_sysconf = dlsym(RTLD_NEXT, "sysconf"); if (name == _SC_NPROCESSORS_ONLN) { static int cache = -1; if (cache == -1) cache = real_sysconf( _SC_NPROCESSORS_ONLN); return cache; } return real_sysconf(name); }