Skip to content

Commit

Permalink
[fix](memory) Avoid frequently refresh cgroup memory info (apache#35083)
Browse files Browse the repository at this point in the history
If not enable cgroup, log will frequently print Could not find subsystem memory in /proc/self/cgroup.
frequently refresh cgroup information is not necessary.
  • Loading branch information
xinyiZzz committed May 22, 2024
1 parent f0b2f5b commit be17d48
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 19 additions & 4 deletions be/src/util/mem_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ bvar::PassiveStatus<int64_t> g_sys_mem_avail(

bool MemInfo::_s_initialized = false;
std::atomic<int64_t> MemInfo::_s_physical_mem = std::numeric_limits<int64_t>::max();
int64_t MemInfo::_s_cgroup_mem_limit = std::numeric_limits<int64_t>::max();
int64_t MemInfo::_s_cgroup_mem_limit_refresh_wait_times = 0;
std::atomic<int64_t> MemInfo::_s_mem_limit = std::numeric_limits<int64_t>::max();
std::atomic<int64_t> MemInfo::_s_soft_mem_limit = std::numeric_limits<int64_t>::max();

Expand Down Expand Up @@ -395,10 +397,23 @@ void MemInfo::refresh_proc_meminfo() {
int64_t physical_mem = -1;
int64_t cgroup_mem_limit = -1;
physical_mem = _mem_info_bytes["MemTotal"];
Status status = CGroupUtil::find_cgroup_mem_limit(&cgroup_mem_limit);
if (status.ok() && cgroup_mem_limit > 0) {
if (_s_cgroup_mem_limit_refresh_wait_times >= 0) {
Status status = CGroupUtil::find_cgroup_mem_limit(&cgroup_mem_limit);
if (status.ok() && cgroup_mem_limit > 0) {
_s_cgroup_mem_limit = cgroup_mem_limit;
_s_cgroup_mem_limit_refresh_wait_times =
-1000; // wait 10s, 1000 * 100ms, avoid too frequently.
} else {
_s_cgroup_mem_limit = std::numeric_limits<int64_t>::max();
_s_cgroup_mem_limit_refresh_wait_times =
-6000; // find cgroup failed, wait 60s, 6000 * 100ms.
}
} else {
_s_cgroup_mem_limit_refresh_wait_times++;
}
if (_s_cgroup_mem_limit > 0) {
// In theory, always cgroup_mem_limit < physical_mem
physical_mem = std::min(physical_mem, cgroup_mem_limit);
physical_mem = std::min(physical_mem, _s_cgroup_mem_limit);
}

if (physical_mem <= 0) {
Expand Down Expand Up @@ -438,7 +453,7 @@ void MemInfo::refresh_proc_meminfo() {
if (_mem_info_bytes.find("MemAvailable") != _mem_info_bytes.end()) {
mem_available = _mem_info_bytes["MemAvailable"];
}
status = CGroupUtil::find_cgroup_mem_usage(&cgroup_mem_usage);
auto status = CGroupUtil::find_cgroup_mem_usage(&cgroup_mem_usage);
if (status.ok() && cgroup_mem_usage > 0 && cgroup_mem_limit > 0) {
if (mem_available < 0) {
mem_available = cgroup_mem_limit - cgroup_mem_usage;
Expand Down
2 changes: 2 additions & 0 deletions be/src/util/mem_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ class MemInfo {
private:
static bool _s_initialized;
static std::atomic<int64_t> _s_physical_mem;
static int64_t _s_cgroup_mem_limit;
static int64_t _s_cgroup_mem_limit_refresh_wait_times;
static std::atomic<int64_t> _s_mem_limit;
static std::atomic<int64_t> _s_soft_mem_limit;

Expand Down

0 comments on commit be17d48

Please sign in to comment.