Skip to content

Commit

Permalink
Use NumCPUsMaybe to gracefully handle missing /sys.
Browse files Browse the repository at this point in the history
We use /sys to determine the number of CPUs we might see from restartable
sequences.  When this is not available, gracefully degrade rather than
check-failing.

Fixes #245.

PiperOrigin-RevId: 667702663
Change-Id: Id78f557913f5936f7c4515dcbc2d445e162f68e3
  • Loading branch information
ckennelly authored and copybara-github committed Aug 26, 2024
1 parent 8369baf commit 883c38c
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 5 deletions.
8 changes: 7 additions & 1 deletion tcmalloc/internal/cache_topology.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ int BuildCpuToL3CacheMap_FindFirstNumberInBuf(absl::string_view current) {
}

void CacheTopology::Init() {
cpu_count_ = NumCPUs();
const auto maybe_numcpus = NumCPUsMaybe();
if (!maybe_numcpus.has_value()) {
l3_count_ = 1;
return;
}

cpu_count_ = *maybe_numcpus;
for (int cpu = 0; cpu < cpu_count_; ++cpu) {
const int fd = OpenSysfsCacheList(cpu);
if (fd == -1) {
Expand Down
4 changes: 3 additions & 1 deletion tcmalloc/internal/cache_topology.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class CacheTopology {
unsigned GetL3FromCpuId(int cpu) const {
TC_ASSERT_GE(cpu, 0);
TC_ASSERT_LT(cpu, cpu_count_);
return l3_cache_index_[cpu];
unsigned l3 = l3_cache_index_[cpu];
TC_ASSERT_LT(l3, l3_count_);
return l3;
}

private:
Expand Down
2 changes: 2 additions & 0 deletions tcmalloc/internal/parameter_accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetMaxTotalThreadCacheBytes(
ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetPeakSamplingHeapGrowthFraction(
double v);
ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetPerCpuCachesEnabled(bool v);
ABSL_ATTRIBUTE_WEAK void
TCMalloc_Internal_SetPerCpuCachesEnabledNoBuildRequirement(bool v);
ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetProfileSamplingInterval(
int64_t v);
ABSL_ATTRIBUTE_WEAK void TCMalloc_Internal_SetBackgroundProcessActionsEnabled(
Expand Down
7 changes: 6 additions & 1 deletion tcmalloc/internal/percpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ int VirtualCpu::Synchronize() {
}

static void InitPerCpu() {
TC_CHECK(NumCPUs() <= std::numeric_limits<uint16_t>::max());
const auto maybe_numcpus = NumCPUsMaybe();
if (!maybe_numcpus.has_value()) {
init_status = kSlowMode;
return;
}
TC_CHECK(*maybe_numcpus <= std::numeric_limits<uint16_t>::max());

// Based on the results of successfully initializing the first thread, mark
// init_status to initialize all subsequent threads.
Expand Down
4 changes: 4 additions & 0 deletions tcmalloc/parameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ void TCMalloc_Internal_SetPerCpuCachesEnabled(bool v) {
}
#endif // !TCMALLOC_DEPRECATED_PERTHREAD

TCMalloc_Internal_SetPerCpuCachesEnabledNoBuildRequirement(v);
}

void TCMalloc_Internal_SetPerCpuCachesEnabledNoBuildRequirement(bool v) {
Parameters::per_cpu_caches_enabled_.store(v, std::memory_order_relaxed);
}

Expand Down
3 changes: 2 additions & 1 deletion tcmalloc/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ class Parameters {
friend void ::TCMalloc_Internal_SetMaxPerCpuCacheSize(int32_t v);
friend void ::TCMalloc_Internal_SetMaxTotalThreadCacheBytes(int64_t v);
friend void ::TCMalloc_Internal_SetPeakSamplingHeapGrowthFraction(double v);
friend void ::TCMalloc_Internal_SetPerCpuCachesEnabled(bool v);
friend void ::TCMalloc_Internal_SetPerCpuCachesEnabledNoBuildRequirement(
bool v);
friend void ::TCMalloc_Internal_SetProfileSamplingInterval(int64_t v);

friend void ::TCMalloc_Internal_SetHugePageFillerSkipSubreleaseInterval(
Expand Down
5 changes: 4 additions & 1 deletion tcmalloc/static_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/internal/mincore.h"
#include "tcmalloc/internal/numa.h"
#include "tcmalloc/internal/parameter_accessors.h"
#include "tcmalloc/internal/percpu.h"
#include "tcmalloc/internal/sysinfo.h"
#include "tcmalloc/malloc_extension.h"
Expand Down Expand Up @@ -162,7 +163,9 @@ ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE void Static::SlowInitIfNecessary() {
TC_CHECK(sizemap_.Init(SizeMap::CurrentClasses().classes));
// Verify we can determine the number of CPUs now, since we will need it
// later for per-CPU caches and initializing the cache topology.
(void)NumCPUs();
if (ABSL_PREDICT_FALSE(!NumCPUsMaybe().has_value())) {
TCMalloc_Internal_SetPerCpuCachesEnabledNoBuildRequirement(false);
}
(void)subtle::percpu::IsFast();
numa_topology_.Init();
CacheTopology::Instance().Init();
Expand Down

0 comments on commit 883c38c

Please sign in to comment.