From eb13ccdbd81fc47ee79741ad9e536f9a7d2163c2 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 3 Sep 2023 17:37:25 -0700 Subject: til_utils: switch til_get_ncpus() to sysconf The ad-hoc sys-based probe of cpus works fine on Linux, but it's not really preferable when Linux's sysconf supports _SC_NPROCESSORS_ONLN, and there's a chance non-Linux's will support it too. Supposedly even Emscripten supports this, and will report the number of available WebWorkers through this interface, even with pthreads emulation. I'm curious if that actually works, getting wasm builds of rototiller demos could be fun. --- src/til_util.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/til_util.c b/src/til_util.c index b87a2d8..7c44d67 100644 --- a/src/til_util.c +++ b/src/til_util.c @@ -28,23 +28,20 @@ unsigned til_get_ncpus(void) #endif #ifdef __MACH__ - int count; - size_t count_len = sizeof(count); + int count; + size_t count_len = sizeof(count); if (sysctlbyname("hw.logicalcpu_max", &count, &count_len, NULL, 0) < 0) return 1; return MIN(count, TIL_MAXCPUS); #else - char path[cstrlen(TIL_SYSFS_CPU "1024") + 1]; - unsigned n; + long n; - for (n = 0; n < TIL_MAXCPUS; n++) { - snprintf(path, sizeof(path), "%s%u", TIL_SYSFS_CPU, n); - if (access(path, F_OK) == -1) - break; - } + n = sysconf(_SC_NPROCESSORS_ONLN); + if (n <= 0) + return 1; - return n == 0 ? 1 : n; + return MIN(n, TIL_MAXCPUS); #endif } -- cgit v1.2.3