aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorilezhankin <ilezhankin@yandex-team.com>2024-11-15 14:19:54 +0300
committerilezhankin <ilezhankin@yandex-team.com>2024-11-15 14:34:26 +0300
commit66adb2a8ff583b23ea45559a41594368aa43c8ab (patch)
tree520de657ed96f65e6464034d7d59e12575ce9ad6
parent67b62bf7ad03d89c7c447a4013734e26912903c7 (diff)
downloadydb-66adb2a8ff583b23ea45559a41594368aa43c8ab.tar.gz
Add tcmalloc soft limit memory handler
commit_hash:30c5264fd43f57937d3355fd8096e0a447ce19e1
-rw-r--r--contrib/libs/tcmalloc/patches/handler.patch49
-rw-r--r--contrib/libs/tcmalloc/tcmalloc/malloc_extension.cc14
-rw-r--r--contrib/libs/tcmalloc/tcmalloc/malloc_extension.h4
-rw-r--r--contrib/libs/tcmalloc/tcmalloc/page_allocator.cc4
4 files changed, 71 insertions, 0 deletions
diff --git a/contrib/libs/tcmalloc/patches/handler.patch b/contrib/libs/tcmalloc/patches/handler.patch
new file mode 100644
index 0000000000..ce30d7e1c2
--- /dev/null
+++ b/contrib/libs/tcmalloc/patches/handler.patch
@@ -0,0 +1,49 @@
+--- contrib/libs/tcmalloc/tcmalloc/malloc_extension.cc (index)
++++ contrib/libs/tcmalloc/tcmalloc/malloc_extension.cc (working tree)
+@@ -468,6 +468,20 @@ void MallocExtension::EnableForkSupport() {
+ #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::SetSampleUserDataCallbacks(
+ CreateSampleUserDataCallback create,
+ CopySampleUserDataCallback copy,
+--- contrib/libs/tcmalloc/tcmalloc/malloc_extension.h (index)
++++ contrib/libs/tcmalloc/tcmalloc/malloc_extension.h (working tree)
+@@ -475,6 +475,10 @@ class MallocExtension final {
+ // Allocator will continue to function correctly in the child, after calling fork().
+ static void EnableForkSupport();
+
++ using SoftMemoryLimitCallback = void();
++ static void SetSoftMemoryLimitHandler(SoftMemoryLimitCallback* handler);
++ static SoftMemoryLimitCallback* GetSoftMemoryLimitHandler();
++
+ using CreateSampleUserDataCallback = void*();
+ using CopySampleUserDataCallback = void*(void*);
+ using DestroySampleUserDataCallback = void(void*);
+--- contrib/libs/tcmalloc/tcmalloc/page_allocator.cc (index)
++++ contrib/libs/tcmalloc/tcmalloc/page_allocator.cc (working tree)
+@@ -151,6 +151,10 @@ void PageAllocator::ShrinkToUsageLimit() {
+ warned = true;
+ Log(kLogWithStack, __FILE__, __LINE__, "Couldn't respect usage limit of ",
+ limit_, "and OOM is likely to follow.");
++
++ if (auto* handler = MallocExtension::GetSoftMemoryLimitHandler()) {
++ (*handler)();
++ }
+ }
+
+ bool PageAllocator::ShrinkHardBy(Length pages) {
diff --git a/contrib/libs/tcmalloc/tcmalloc/malloc_extension.cc b/contrib/libs/tcmalloc/tcmalloc/malloc_extension.cc
index ad3205fcdc..5a4ad70043 100644
--- a/contrib/libs/tcmalloc/tcmalloc/malloc_extension.cc
+++ b/contrib/libs/tcmalloc/tcmalloc/malloc_extension.cc
@@ -468,6 +468,20 @@ void MallocExtension::EnableForkSupport() {
#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::SetSampleUserDataCallbacks(
CreateSampleUserDataCallback create,
CopySampleUserDataCallback copy,
diff --git a/contrib/libs/tcmalloc/tcmalloc/malloc_extension.h b/contrib/libs/tcmalloc/tcmalloc/malloc_extension.h
index fcbd347ca1..19b68ba8ac 100644
--- a/contrib/libs/tcmalloc/tcmalloc/malloc_extension.h
+++ b/contrib/libs/tcmalloc/tcmalloc/malloc_extension.h
@@ -475,6 +475,10 @@ class MallocExtension final {
// Allocator will continue to function correctly in the child, after calling fork().
static void EnableForkSupport();
+ using SoftMemoryLimitCallback = void();
+ static void SetSoftMemoryLimitHandler(SoftMemoryLimitCallback* handler);
+ static SoftMemoryLimitCallback* GetSoftMemoryLimitHandler();
+
using CreateSampleUserDataCallback = void*();
using CopySampleUserDataCallback = void*(void*);
using DestroySampleUserDataCallback = void(void*);
diff --git a/contrib/libs/tcmalloc/tcmalloc/page_allocator.cc b/contrib/libs/tcmalloc/tcmalloc/page_allocator.cc
index e9599ef46a..2999b28ad9 100644
--- a/contrib/libs/tcmalloc/tcmalloc/page_allocator.cc
+++ b/contrib/libs/tcmalloc/tcmalloc/page_allocator.cc
@@ -151,6 +151,10 @@ void PageAllocator::ShrinkToUsageLimit() {
warned = true;
Log(kLogWithStack, __FILE__, __LINE__, "Couldn't respect usage limit of ",
limit_, "and OOM is likely to follow.");
+
+ if (auto* handler = MallocExtension::GetSoftMemoryLimitHandler()) {
+ (*handler)();
+ }
}
bool PageAllocator::ShrinkHardBy(Length pages) {