Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tcmalloc/internal/cpu_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(); }
Expand Down
17 changes: 16 additions & 1 deletion tcmalloc/internal/sysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include <sched.h>
#include <sys/types.h>
#if !defined(__linux__) && __has_include(<unistd.h>)
#include <unistd.h>
#endif

#include <cstddef>
#include <optional>
Expand All @@ -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
Expand Down Expand Up @@ -67,7 +72,17 @@ inline std::optional<int> NumCPUsMaybe() {

#else // __linux__

inline std::optional<int> NumCPUsMaybe() { return std::nullopt; }
inline std::optional<int> NumCPUsMaybe() {
#if defined(_SC_NPROCESSORS_ONLN)
int n = static_cast<int>(sysconf(_SC_NPROCESSORS_ONLN));
if (n == -1) {
return std::nullopt;
}
return n;
#else
return std::nullopt;
#endif
}

#endif // __linux__

Expand Down
Loading