From f52a3129a78701747c436968cf6068ffa16f8894 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Tue, 18 Jul 2017 19:26:09 +0200 Subject: [PATCH] iAdd function get_cpu_count. --- bench/common.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/bench/common.h b/bench/common.h index 9e9d3c3b..ed76a735 100644 --- a/bench/common.h +++ b/bench/common.h @@ -30,3 +30,37 @@ static void test_function(const char str[], size_t n, void (*func)(size_t)) end = (end - start) / 1000U; printf ("%s %Lu ms for n = %lu\n", str, end, n); } + +#if defined(_WIN32) +# include +#elif (defined(__APPLE__) && defined(__MACH__)) \ + || defined(__DragonFly__) || defined(__FreeBSD__) \ + || defined(__NetBSD__) || defined(__OpenBSD__) +# include +# include +# define USE_SYSCTL +#else +# include +#endif + +/* Return the number of CPU of the system */ +static inline int get_cpu_count(void) +{ +#if defined(_WIN32) + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + return sysinfo.dwNumberOfProcessors; +#elif defined(USE_SYSCTL) + int nm[2]; + int count = 0; + size_t len = sizeof (count); + nm[0] = CTL_HW; + nm[1] = HW_NCPU; + sysctl(nm, 2, &count, &len, NULL, 0); + return MAX(1, count); +#elif defined (_SC_NPROCESSORS_ONLN) + return sysconf(_SC_NPROCESSORS_ONLN); +#else + return 1; +#endif +}