From 9d4ea5a8e81395672e0c3b59f8926ba48a223000 Mon Sep 17 00:00:00 2001 From: Mateusz Guzik Date: Sat, 28 Nov 2020 23:54:09 +0000 Subject: Add FreeBSD support to GetProcessorCount --- src/util.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/util.cc b/src/util.cc index c76f730..05bdb2d 100644 --- a/src/util.cc +++ b/src/util.cc @@ -51,6 +51,10 @@ #include #endif +#if defined(__FreeBSD__) +#include +#endif + #include "edit_distance.h" #include "metrics.h" @@ -485,10 +489,17 @@ int GetProcessorCount() { #ifdef _WIN32 return GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); #else -#ifdef CPU_COUNT // The number of exposed processors might not represent the actual number of // processors threads can run on. This happens when a CPU set limitation is // active, see https://github.com/ninja-build/ninja/issues/1278 +#if defined(__FreeBSD__) + cpuset_t mask; + CPU_ZERO(&mask); + if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask), + &mask) == 0) { + return CPU_COUNT(&mask); + } +#elif defined(CPU_COUNT) cpu_set_t set; if (sched_getaffinity(getpid(), sizeof(set), &set) == 0) { return CPU_COUNT(&set); -- cgit v0.12