diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2015-02-10 15:28:30 +0200 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2015-02-10 15:28:30 +0200 |
commit | d935b0cdf3db440269b9d952b2b281b18f8c7b08 (patch) | |
tree | daaa0a7ee9b6a2b10248396cc1b15a535e084a43 /src/common/tuklib_cpucores.c | |
parent | xzdiff: Make the mktemp usage compatible with FreeBSD's mktemp. (diff) | |
download | xz-d935b0cdf3db440269b9d952b2b281b18f8c7b08.tar.xz |
tuklib_cpucores: Use cpuset_getaffinity() on FreeBSD if available.
In FreeBSD, cpuset_getaffinity() is the preferred way to get
the number of available cores.
Thanks to Rui Paulo for the patch. I edited it slightly, but
hopefully I didn't break anything.
Diffstat (limited to 'src/common/tuklib_cpucores.c')
-rw-r--r-- | src/common/tuklib_cpucores.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/common/tuklib_cpucores.c b/src/common/tuklib_cpucores.c index 7574bc9c..e235fd1c 100644 --- a/src/common/tuklib_cpucores.c +++ b/src/common/tuklib_cpucores.c @@ -18,6 +18,11 @@ # endif # include <windows.h> +// FreeBSD +#elif defined(TUKLIB_CPUCORES_CPUSET) +# include <sys/param.h> +# include <sys/cpuset.h> + #elif defined(TUKLIB_CPUCORES_SYSCTL) # ifdef HAVE_SYS_PARAM_H # include <sys/param.h> @@ -44,6 +49,19 @@ tuklib_cpucores(void) GetSystemInfo(&sysinfo); ret = sysinfo.dwNumberOfProcessors; +#elif defined(TUKLIB_CPUCORES_CPUSET) + cpuset_t set; + if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, + sizeof(set), &set) == 0) { +# ifdef CPU_COUNT + ret = CPU_COUNT(&set); +# else + for (unsigned i = 0; i < CPU_SETSIZE; ++i) + if (CPU_ISSET(i, &set)) + ++ret; +# endif + } + #elif defined(TUKLIB_CPUCORES_SYSCTL) int name[2] = { CTL_HW, HW_NCPU }; int cpus; |