Skip to content

Commit

Permalink
Wire up opt out mechanism for big span.
Browse files Browse the repository at this point in the history
Currently, the feature itself is disabled by default. We will enable it in a follow-up cl.

PiperOrigin-RevId: 700080673
Change-Id: I3f54f08c23c793ef471828d0fbcb71111bc1398f
  • Loading branch information
v-gogte authored and copybara-github committed Nov 25, 2024
1 parent 4e8fcfc commit 583039d
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tcmalloc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,16 @@ cc_library(
alwayslink = 1,
)

cc_library(
# TODO(b/304135905): Remove this opt-out.
name = "want_disable_tcmalloc_big_span",
srcs = ["want_disable_tcmalloc_big_span.cc"],
copts = ["-g0"] + TCMALLOC_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = ["@com_google_absl//absl/base:core_headers"],
alwayslink = 1,
)

cc_library(
# TODO(b/199203282, b/296281171): Remove this opt-out.
name = "want_disable_huge_region_more_often",
Expand Down
29 changes: 28 additions & 1 deletion tcmalloc/static_vars.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "tcmalloc/internal/atomic_stats_counter.h"
#include "tcmalloc/internal/cache_topology.h"
#include "tcmalloc/internal/config.h"
#include "tcmalloc/internal/environment.h"
#include "tcmalloc/internal/explicitly_constructed.h"
#include "tcmalloc/internal/logging.h"
#include "tcmalloc/internal/mincore.h"
Expand All @@ -60,6 +61,31 @@ GOOGLE_MALLOC_SECTION_BEGIN
namespace tcmalloc {
namespace tcmalloc_internal {

ABSL_ATTRIBUTE_WEAK bool default_want_disable_tcmalloc_big_span();
bool tcmalloc_big_span() {
// Disable 64B span if built against an opt-out.
if (default_want_disable_tcmalloc_big_span != nullptr) {
return false;
}

const char* e = thread_safe_getenv("TCMALLOC_DISABLE_BIG_SPAN");
if (e) {
switch (e[0]) {
case '0':
// TODO(b/304135905): Enable this.
return false;
case '1':
return false;
default:
TC_BUG("bad env var '%s'", e);
return false;
}
}

// TODO(b/304135905): Enable this by default.
return false;
}

// Cacheline-align our SizeMap and CpuCache. They both have very hot arrays as
// their first member variables, and aligning them reduces the number of cache
// lines these arrays use.
Expand Down Expand Up @@ -177,7 +203,8 @@ ABSL_ATTRIBUTE_COLD ABSL_ATTRIBUTE_NOINLINE void Static::SlowInitIfNecessary() {
peak_heap_tracker_.Init(&arena_);

const bool large_span_experiment =
IsExperimentActive(Experiment::TEST_ONLY_TCMALLOC_BIG_SPAN);
IsExperimentActive(Experiment::TEST_ONLY_TCMALLOC_BIG_SPAN) ||
tcmalloc_big_span();
Parameters::set_max_span_cache_size(
large_span_experiment ? Span::kLargeCacheSize : Span::kCacheSize);
Parameters::set_max_span_cache_array_size(
Expand Down
2 changes: 2 additions & 0 deletions tcmalloc/static_vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ enum class SizeClassConfiguration {
kReuse = 6,
};

bool tcmalloc_big_span();

class Static final {
public:
constexpr Static() = default;
Expand Down
33 changes: 33 additions & 0 deletions tcmalloc/testing/want_disable_tcmalloc_big_span_test_helper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019 The TCMalloc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


#include <cstdio>
#include <string>

#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "tcmalloc/malloc_extension.h"

int main(int argc, char** argv) {
std::string input = tcmalloc::MallocExtension::GetStats();

if (absl::StrContains(input, "PARAMETER max_span_cache_size 8")) {
printf("Active");
} else {
printf("Inactive");
}

return 0;
}
26 changes: 26 additions & 0 deletions tcmalloc/want_disable_tcmalloc_big_span.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 The TCMalloc Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "absl/base/attributes.h"

namespace tcmalloc {
namespace tcmalloc_internal {

// This - if linked into a binary - allows 64B span to be disabled.
ABSL_ATTRIBUTE_UNUSED bool default_want_disable_tcmalloc_big_span() {
return true;
}

} // namespace tcmalloc_internal
} // namespace tcmalloc

0 comments on commit 583039d

Please sign in to comment.