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
4 changes: 4 additions & 0 deletions tcmalloc/internal/profile_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "absl/types/span.h"
#include "tcmalloc/internal/compressibility.h"
#include "tcmalloc/internal/pageflags.h"
#include "tcmalloc/internal_malloc_extension.h"
#include "tcmalloc/malloc_extension.h"

#if defined(__linux__)
Expand Down Expand Up @@ -882,6 +883,9 @@ absl::StatusOr<std::unique_ptr<perftools::profiles::Profile>> MakeProfileProto(
const ::tcmalloc::Profile& profile, PageFlagsBase* pageflags,
Residency* residency) {
if (profile.Type() == ProfileType::kDoNotUse) {
if (MallocExtension::GetProfileSamplingInterval() == 0) {
return absl::NotFoundError("Profile sampling is disabled");
}
#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
defined(ABSL_HAVE_LEAK_SANITIZER) || \
defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER)
Expand Down
20 changes: 20 additions & 0 deletions tcmalloc/internal/profile_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,26 @@ TEST(BuildId, ZeroOneAndCorruptAlignment) {
EXPECT_EQ(GetBuildId(&info), "");
}

TEST(ProfileBuilderTest, DisabledSamplingReturnsNotFound) {
const int64_t saved_interval = MallocExtension::GetProfileSamplingInterval();
if (saved_interval < 0) {
GTEST_SKIP() << "TCMalloc is not linked in this build configuration";
}
MallocExtension::SetProfileSamplingInterval(0);

Profile heap_profile = MallocExtension::SnapshotCurrent(ProfileType::kHeap);
EXPECT_EQ(heap_profile.Type(), ProfileType::kDoNotUse);
EXPECT_EQ(MakeProfileProto(heap_profile).status().code(),
absl::StatusCode::kNotFound);

Profile alloc_profile = MallocExtension::StartAllocationProfiling().Stop();
EXPECT_EQ(alloc_profile.Type(), ProfileType::kDoNotUse);
EXPECT_EQ(MakeProfileProto(alloc_profile).status().code(),
absl::StatusCode::kNotFound);

MallocExtension::SetProfileSamplingInterval(saved_interval);
}

} // namespace
} // namespace tcmalloc_internal
} // namespace tcmalloc
12 changes: 12 additions & 0 deletions tcmalloc/tcmalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ extern "C" size_t TCMalloc_Internal_GetStats(char* buffer,

extern "C" const ProfileBase* MallocExtension_Internal_SnapshotCurrent(
ProfileType type) {
if (Parameters::profile_sampling_interval() <= 0) {
return nullptr;
}
switch (type) {
case ProfileType::kHeap:
return DumpHeapProfile(tc_globals).release();
Expand All @@ -302,17 +305,26 @@ extern "C" const ProfileBase* MallocExtension_Internal_SnapshotCurrent(

extern "C" AllocationProfilingTokenBase*
MallocExtension_Internal_StartAllocationProfiling() {
if (Parameters::profile_sampling_interval() <= 0) {
return nullptr;
}
return new AllocationSample(&tc_globals.allocation_samples, absl::Now());
}

extern "C" tcmalloc_internal::AllocationProfilingTokenBase*
MallocExtension_Internal_StartLifetimeProfiling() {
if (Parameters::profile_sampling_interval() <= 0) {
return nullptr;
}
return new deallocationz::DeallocationSample(&tc_globals.deallocation_samples,
deallocationz::Mode::kLifetimes);
}

extern "C" tcmalloc_internal::AllocationProfilingTokenBase*
MallocExtension_Internal_StartEventTracing() {
if (Parameters::profile_sampling_interval() <= 0) {
return nullptr;
}
return new deallocationz::DeallocationSample(
&tc_globals.deallocation_samples, deallocationz::Mode::kEventTrace);
}
Expand Down
Loading