Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback for the soft memory limit handling #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions tcmalloc/malloc_extension.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ absl::Duration MallocExtension::GetSkipSubreleaseInterval() {
#endif
}

static std::atomic<MallocExtension::SoftMemoryLimitCallback*> SoftMemoryLimitHandler_;

void MallocExtension::SetSoftMemoryLimitHandler(SoftMemoryLimitCallback* handler) {
#if ABSL_INTERNAL_HAVE_WEAK_MALLOCEXTENSION_STUBS
SoftMemoryLimitHandler_.store(handler);
#endif
}

MallocExtension::SoftMemoryLimitCallback* MallocExtension::GetSoftMemoryLimitHandler() {
#if ABSL_INTERNAL_HAVE_WEAK_MALLOCEXTENSION_STUBS
return SoftMemoryLimitHandler_.load();
#endif
}

void MallocExtension::SetSkipSubreleaseInterval(absl::Duration value) {
#if ABSL_INTERNAL_HAVE_WEAK_MALLOCEXTENSION_STUBS
if (MallocExtension_Internal_SetSkipSubreleaseInterval == nullptr) {
Expand Down
8 changes: 8 additions & 0 deletions tcmalloc/malloc_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ class MallocExtension final {
// Specifies the release rate from the page heap. ProcessBackgroundActions
// must be called for this to be operative.
static void SetBackgroundReleaseRate(BytesPerSecond rate);

// Specifies the pointer to a callback function to be invoked when the soft
// memory limit is reached.
using SoftMemoryLimitCallback = void();
static void SetSoftMemoryLimitHandler(SoftMemoryLimitCallback* handler);

// Gets the current handler of the soft memory limit event.
static SoftMemoryLimitCallback* GetSoftMemoryLimitHandler();
};

} // namespace tcmalloc
Expand Down
4 changes: 4 additions & 0 deletions tcmalloc/page_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ void PageAllocator::ShrinkToUsageLimit(Length n) {
warned = true;
TC_LOG("Couldn't respect usage limit of %v and OOM is likely to follow.",
limits_[kSoft]);

if (auto* handler = MallocExtension::GetSoftMemoryLimitHandler()) {
(*handler)();
}
}

bool PageAllocator::ShrinkHardBy(Length pages, LimitKind limit_kind) {
Expand Down