Skip to content

Commit 69bcfe7

Browse files
Backport #87610 to 25.9: exclude userspace cache for memory overload warning
1 parent e53c949 commit 69bcfe7

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/Common/AsynchronousMetrics.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,15 @@ void AsynchronousMetrics::processWarningForMemoryOverload(const AsynchronousMetr
921921
double memory_resident{};
922922
double memory_total{};
923923

924+
/// Compute the user space page cache size (in bytes) and subtract from total memory used if possible
925+
double user_space_cache_bytes{};
926+
if (const auto * rss = getAsynchronousMetricValue(new_values, "MemoryResident"),
927+
*rss_wo_pc = getAsynchronousMetricValue(new_values, "MemoryResidentWithoutPageCache");
928+
rss && rss_wo_pc && rss->value >= rss_wo_pc->value)
929+
{
930+
user_space_cache_bytes = rss->value - rss_wo_pc->value;
931+
}
932+
924933
/// use cgroup memory metrics if available and > 0, otherwise fallback to OS memory metrics
925934
if (const auto *cgroup_memory_total = getAsynchronousMetricValue(new_values, "CGroupMemoryTotal"),
926935
*cgroup_memory_used = getAsynchronousMetricValue(new_values, "CGroupMemoryUsed");
@@ -929,7 +938,7 @@ void AsynchronousMetrics::processWarningForMemoryOverload(const AsynchronousMetr
929938
memory_resident = cgroup_memory_used->value;
930939
memory_total = cgroup_memory_total->value;
931940
}
932-
else if (const auto * os_memory_used = getAsynchronousMetricValue(new_values, "OSMemoryResident"),
941+
else if (const auto * os_memory_used = getAsynchronousMetricValue(new_values, "MemoryResident"),
933942
*os_memory_total = getAsynchronousMetricValue(new_values, "OSMemoryTotal");
934943
os_memory_used && os_memory_total && (os_memory_total->value > 0.0 && os_memory_used->value > 0.0))
935944
{
@@ -942,15 +951,20 @@ void AsynchronousMetrics::processWarningForMemoryOverload(const AsynchronousMetr
942951
return;
943952
}
944953

954+
/// Exclude user space page cache from memory resident
955+
if (user_space_cache_bytes > 0.0)
956+
memory_resident = std::max(0.0, memory_resident - user_space_cache_bytes);
957+
945958
const double ratio = memory_resident / memory_total;
959+
const double clamped_ratio = std::clamp(ratio, 0.0, 1.0);
946960

947961
const auto & cfg = context->getConfigRef();
948962
const double mem_warn_ratio = cfg.getDouble("resource_overload_warnings.memory_overload_warn_ratio", 0.9);
949963
const double mem_clear_ratio = cfg.getDouble("resource_overload_warnings.memory_overload_clear_ratio", 0.85);
950964
const UInt64 min_duration = cfg.getUInt64("resource_overload_warnings.memory_overload_duration_seconds", 600);
951965

952966
const auto now = Clock::now();
953-
const int usage_percent = static_cast<int>(std::lround(ratio * 100.0));
967+
const int usage_percent = static_cast<int>(std::lround(clamped_ratio * 100.0));
954968

955969
const auto warning_message = PreformattedMessage::create(
956970
"High ClickHouse memory usage: {} of {} used ({}%) for at least {} second(s)",

0 commit comments

Comments
 (0)