aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2016-10-24 18:51:36 +0300
committerLasse Collin <lasse.collin@tukaani.org>2016-10-24 18:51:36 +0300
commitdf8f446e3ad47e5148b8c8d8b6e519d3ce29cb9d (patch)
treeaf7d6b7d6aa3f05f83375de0ca79142ce8a50b99 /src
parentxz: Fix copying of timestamps on Windows. (diff)
downloadxz-df8f446e3ad47e5148b8c8d8b6e519d3ce29cb9d.tar.xz
tuklib_cpucores: Add support for sched_getaffinity().
It's available in glibc (GNU/Linux, GNU/kFreeBSD). It's better than sysconf(_SC_NPROCESSORS_ONLN) because sched_getaffinity() gives the number of cores available to the process instead of the total number of cores online. As a side effect, this commit fixes a bug on GNU/kFreeBSD where configure would detect the FreeBSD-specific cpuset_getaffinity() but it wouldn't actually work because on GNU/kFreeBSD it requires using -lfreebsd-glue when linking. Now the glibc-specific function will be used instead. Thanks to Sebastian Andrzej Siewior for the original patch and testing.
Diffstat (limited to 'src')
-rw-r--r--src/common/tuklib_cpucores.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/common/tuklib_cpucores.c b/src/common/tuklib_cpucores.c
index e235fd1c..c16e188d 100644
--- a/src/common/tuklib_cpucores.c
+++ b/src/common/tuklib_cpucores.c
@@ -18,6 +18,10 @@
# endif
# include <windows.h>
+// glibc >= 2.9
+#elif defined(TUKLIB_CPUCORES_SCHED_GETAFFINITY)
+# include <sched.h>
+
// FreeBSD
#elif defined(TUKLIB_CPUCORES_CPUSET)
# include <sys/param.h>
@@ -49,6 +53,11 @@ tuklib_cpucores(void)
GetSystemInfo(&sysinfo);
ret = sysinfo.dwNumberOfProcessors;
+#elif defined(TUKLIB_CPUCORES_SCHED_GETAFFINITY)
+ cpu_set_t cpu_mask;
+ if (sched_getaffinity(0, sizeof(cpu_mask), &cpu_mask) == 0)
+ ret = CPU_COUNT(&cpu_mask);
+
#elif defined(TUKLIB_CPUCORES_CPUSET)
cpuset_t set;
if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1,