Skip to content

Commit

Permalink
Clean up unconditional logic to use Temeraire by constant propagating.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 687357472
Change-Id: I198cd3907f69cda6d90b467b47e76a918d731778
  • Loading branch information
ckennelly authored and copybara-github committed Oct 18, 2024
1 parent e2a75ac commit 43726a4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 126 deletions.
26 changes: 2 additions & 24 deletions tcmalloc/huge_page_aware_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,17 @@ GOOGLE_MALLOC_SECTION_BEGIN
namespace tcmalloc {
namespace tcmalloc_internal {

bool decide_want_hpaa();
ABSL_ATTRIBUTE_WEAK int default_want_hpaa();
ABSL_ATTRIBUTE_WEAK int default_subrelease();

namespace huge_page_allocator_internal {

bool decide_subrelease() {
if (!decide_want_hpaa()) {
// Subrelease is off if HPAA is off.
return false;
}

const char* e = thread_safe_getenv("TCMALLOC_HPAA_CONTROL");
if (e) {
switch (e[0]) {
case '0':
if (kUnconditionalHPAA) {
// If we're forcing HPAA on, we want to converge towards our default
// of subrelease on, rather than off (where it is moot without HPAA).
break;
}

if (default_want_hpaa != nullptr) {
int default_hpaa = default_want_hpaa();
if (default_hpaa < 0) {
return false;
}
}

TC_LOG(
"Runtime opt-out from HPAA requires building with "
"//tcmalloc:want_no_hpaa."
);
// If we're forcing HPAA on, we want to converge towards our default
// of subrelease on, rather than off (where it is moot without HPAA).
break;
case '1':
return false;
Expand Down
3 changes: 3 additions & 0 deletions tcmalloc/huge_page_aware_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ struct HugePageAwareAllocatorOptions {

template <typename Forwarder>
class HugePageAwareAllocator final : public PageAllocatorInterface {
static_assert(kHugePageSize <= kMinSystemAlloc,
"HPAA requires kMinSystemAlloc is at least a hugepage.");

public:
explicit HugePageAwareAllocator(const HugePageAwareAllocatorOptions& options);
~HugePageAwareAllocator() override = default;
Expand Down
118 changes: 19 additions & 99 deletions tcmalloc/page_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,110 +39,30 @@ namespace tcmalloc_internal {

using huge_page_allocator_internal::HugePageAwareAllocatorOptions;

int ABSL_ATTRIBUTE_WEAK default_want_hpaa();

bool decide_want_hpaa() {
static_assert(kHugePageSize <= kMinSystemAlloc,
"HPAA requires kMinSystemAlloc is at least a hugepage.");

if (huge_page_allocator_internal::kUnconditionalHPAA) {
return true;
}

const char* e =
tcmalloc::tcmalloc_internal::thread_safe_getenv("TCMALLOC_HPAA_CONTROL");
if (e) {
switch (e[0]) {
case '0':
if (default_want_hpaa != nullptr) {
int default_hpaa = default_want_hpaa();
if (default_hpaa < 0) {
return false;
}
}

TC_LOG(
"Runtime opt-out from HPAA requires building with "
"//tcmalloc:want_no_hpaa."
);
break;
case '1':
return true;
case '2':
return true;
default:
TC_BUG("bad env var '%s'", e);
}
}

if (default_want_hpaa != nullptr) {
int default_hpaa = default_want_hpaa();
if (default_hpaa != 0) {
return default_hpaa > 0;
}
}

return true;
}

bool want_hpaa() {
ABSL_CONST_INIT static bool use;
ABSL_CONST_INIT static absl::once_flag flag;

absl::base_internal::LowLevelCallOnce(&flag,
[]() { use = decide_want_hpaa(); });

return use;
}

PageAllocator::PageAllocator() {
const bool kUseHPAA = want_hpaa();
has_cold_impl_ = ColdFeatureActive();
size_t part = 0;
if (kUseHPAA) {
normal_impl_[0] = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kNormal});
if (tc_globals.numa_topology().numa_aware()) {
normal_impl_[1] = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kNormalP1});
}
sampled_impl_ = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kSampled});
if (selsan::IsEnabled()) {
selsan_impl_ = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kSelSan});
}
if (has_cold_impl_) {
cold_impl_ = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kCold});
} else {
cold_impl_ = normal_impl_[0];
}
alg_ = HPAA;

normal_impl_[0] = new (&choices_[part++].hpaa)
HugePageAwareAllocator(HugePageAwareAllocatorOptions{MemoryTag::kNormal});
if (tc_globals.numa_topology().numa_aware()) {
normal_impl_[1] = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kNormalP1});
}
sampled_impl_ = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kSampled});
if (selsan::IsEnabled()) {
selsan_impl_ = new (&choices_[part++].hpaa) HugePageAwareAllocator(
HugePageAwareAllocatorOptions{MemoryTag::kSelSan});
}
if (has_cold_impl_) {
cold_impl_ = new (&choices_[part++].hpaa)
HugePageAwareAllocator(HugePageAwareAllocatorOptions{MemoryTag::kCold});
} else {
// TODO(b/137017688): Constant propagate.
#if 0
normal_impl_[0] = new (&choices_[part++].ph) PageHeap(MemoryTag::kNormal);
if (tc_globals.numa_topology().numa_aware()) {
normal_impl_[1] =
new (&choices_[part++].ph) PageHeap(MemoryTag::kNormalP1);
}
sampled_impl_ = new (&choices_[part++].ph) PageHeap(MemoryTag::kSampled);
if (selsan::IsEnabled()) {
selsan_impl_ = new (&choices_[part++].ph) PageHeap(MemoryTag::kSelSan);
}
if (has_cold_impl_) {
cold_impl_ = new (&choices_[part++].ph) PageHeap(MemoryTag::kCold);
} else {
cold_impl_ = normal_impl_[0];
}
alg_ = PAGE_HEAP;
#else
static_assert(huge_page_allocator_internal::kUnconditionalHPAA);
TC_BUG("unreachable");
#endif
TC_CHECK_LE(part, ABSL_ARRAYSIZE(choices_));
cold_impl_ = normal_impl_[0];
}
alg_ = HPAA;
TC_CHECK_LE(part, ABSL_ARRAYSIZE(choices_));
}

void PageAllocator::ShrinkToUsageLimit(Length n) {
Expand Down
4 changes: 1 addition & 3 deletions tcmalloc/want_hpaa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ namespace tcmalloc {
namespace tcmalloc_internal {

// This -if linked into a binary - overrides page_allocator.cc and forces HPAA
// on/subrelease off.
ABSL_ATTRIBUTE_UNUSED int default_want_hpaa() { return 1; }

// subrelease off.
ABSL_ATTRIBUTE_UNUSED int default_subrelease() { return -1; }

} // namespace tcmalloc_internal
Expand Down

0 comments on commit 43726a4

Please sign in to comment.