Skip to content

Commit 80888be

Browse files
committed
Use information from cgroup (if applicable) to adjust memory tracker
Right now `memory_worker_correct_memory_tracker` always uses information from jemalloc to update the `MemoryTracking`, but, this may be not good enough in some cases (i.e. when server requested more memory then it use) and may lead to `MEMORY_LIMIT_EXCEEDED` errors with `MemoryTracking` > `RSS`. So use information from cgroup if applicable, and if not, use information from `jemalloc.allocated`.
1 parent 6067851 commit 80888be

2 files changed

Lines changed: 6 additions & 19 deletions

File tree

src/Common/MemoryWorker.cpp

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <Common/ProfileEvents.h>
1010
#include <Common/formatReadable.h>
1111
#include <Common/logger_useful.h>
12+
#include <Common/setThreadName.h>
1213

1314
#include <fmt/ranges.h>
1415

@@ -303,6 +304,7 @@ uint64_t MemoryWorker::getMemoryUsage()
303304
return cgroups_reader != nullptr ? cgroups_reader->readMemoryUsage() : 0;
304305
case MemoryUsageSource::Jemalloc:
305306
#if USE_JEMALLOC
307+
epoch_mib.setValue(0);
306308
return resident_mib.getValue();
307309
#else
308310
return 0;
@@ -314,6 +316,8 @@ uint64_t MemoryWorker::getMemoryUsage()
314316

315317
void MemoryWorker::backgroundThread()
316318
{
319+
setThreadName("MemoryWorker");
320+
317321
std::chrono::milliseconds chrono_period_ms{period_ms};
318322
[[maybe_unused]] bool first_run = true;
319323
std::unique_lock lock(mutex);
@@ -325,11 +329,6 @@ void MemoryWorker::backgroundThread()
325329

326330
Stopwatch total_watch;
327331

328-
#if USE_JEMALLOC
329-
if (source == MemoryUsageSource::Jemalloc)
330-
epoch_mib.setValue(0);
331-
#endif
332-
333332
Int64 resident = getMemoryUsage();
334333
MemoryTracker::updateRSS(resident);
335334

@@ -350,19 +349,9 @@ void MemoryWorker::backgroundThread()
350349
/// - MemoryTracker stores a negative value
351350
/// - `correct_tracker` is set to true
352351
if (unlikely(first_run || total_memory_tracker.get() < 0))
353-
{
354-
if (source != MemoryUsageSource::Jemalloc)
355-
epoch_mib.setValue(0);
356-
357-
MemoryTracker::updateAllocated(allocated_mib.getValue(), /*log_change=*/true);
358-
}
352+
MemoryTracker::updateAllocated(resident, /*log_change=*/true);
359353
else if (correct_tracker)
360-
{
361-
if (source != MemoryUsageSource::Jemalloc)
362-
epoch_mib.setValue(0);
363-
364-
MemoryTracker::updateAllocated(allocated_mib.getValue(), /*log_change=*/false);
365-
}
354+
MemoryTracker::updateAllocated(resident, /*log_change=*/false);
366355
#else
367356
/// we don't update in the first run if we don't have jemalloc
368357
/// because we can only use resident memory information
@@ -371,7 +360,6 @@ void MemoryWorker::backgroundThread()
371360
/// before MemoryTracker initialization
372361
if (unlikely(total_memory_tracker.get() < 0) || correct_tracker)
373362
MemoryTracker::updateAllocated(resident, /*log_change=*/false);
374-
375363
#endif
376364

377365
ProfileEvents::increment(ProfileEvents::MemoryWorkerRun);

src/Common/MemoryWorker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class MemoryWorker
7777
#if USE_JEMALLOC
7878
JemallocMibCache<uint64_t> epoch_mib{"epoch"};
7979
JemallocMibCache<size_t> resident_mib{"stats.resident"};
80-
JemallocMibCache<size_t> allocated_mib{"stats.allocated"};
8180

8281
#define STRINGIFY_HELPER(x) #x
8382
#define STRINGIFY(x) STRINGIFY_HELPER(x)

0 commit comments

Comments
 (0)