diff --git a/tcmalloc/internal/cpu_utils.h b/tcmalloc/internal/cpu_utils.h index ab76e4a4e..a6a2fab91 100644 --- a/tcmalloc/internal/cpu_utils.h +++ b/tcmalloc/internal/cpu_utils.h @@ -22,6 +22,20 @@ #include "absl/base/attributes.h" #include "tcmalloc/internal/config.h" +#if !defined(__linux__) +// Stub types and macros for non-Linux platforms. +typedef struct { + char dummy; +} cpu_set_t; + +#define CPU_ALLOC_SIZE(x) 1 +#define CPU_ZERO_S(size, set) +#define CPU_SET_S(cpu, size, set) +#define CPU_ISSET_S(cpu, size, set) ((cpu) == 0) +#define CPU_CLR_S(cpu, size, set) +#define CPU_COUNT_S(size, set) 1 +#endif + GOOGLE_MALLOC_SECTION_BEGIN namespace tcmalloc { namespace tcmalloc_internal { @@ -57,14 +71,22 @@ class CpuSet { // successful. If returns false, please check the global 'errno' variable to // determine the specific error that occurred. [[nodiscard]] bool SetAffinity(pid_t pid) { +#if defined(__linux__) return sched_setaffinity(pid, kCpuSetBytes, cpu_set_.data()) == 0; +#else + return true; +#endif } // Gets the CPU affinity of the process with the given pid. Return trues if // successful. If returns false, please check the global 'errno' variable to // determine the specific error that occurred. [[nodiscard]] bool GetAffinity(pid_t pid) { +#if defined(__linux__) return sched_getaffinity(pid, kCpuSetBytes, cpu_set_.data()) == 0; +#else + return true; +#endif } const cpu_set_t* data() const { return cpu_set_.data(); } diff --git a/tcmalloc/internal/sysinfo.h b/tcmalloc/internal/sysinfo.h index a7aa9694f..93dfdb0ba 100644 --- a/tcmalloc/internal/sysinfo.h +++ b/tcmalloc/internal/sysinfo.h @@ -17,6 +17,9 @@ #include #include +#if !defined(__linux__) && __has_include() +#include +#endif #include #include @@ -25,7 +28,9 @@ #include "absl/base/call_once.h" #include "absl/functional/function_ref.h" #include "tcmalloc/internal/config.h" +#if __linux__ #include "tcmalloc/internal/cpu_utils.h" +#endif #include "tcmalloc/internal/logging.h" GOOGLE_MALLOC_SECTION_BEGIN @@ -67,7 +72,17 @@ inline std::optional NumCPUsMaybe() { #else // __linux__ -inline std::optional NumCPUsMaybe() { return std::nullopt; } +inline std::optional NumCPUsMaybe() { +#if defined(_SC_NPROCESSORS_ONLN) + int n = static_cast(sysconf(_SC_NPROCESSORS_ONLN)); + if (n == -1) { + return std::nullopt; + } + return n; +#else + return std::nullopt; +#endif +} #endif // __linux__