From 8021a49313806629a0fe8bd6c50fca698cb27702 Mon Sep 17 00:00:00 2001 From: nebula-bots <88429921+nebula-bots@users.noreply.github.com> Date: Tue, 4 Jan 2022 21:41:27 +0800 Subject: [PATCH] Memavailable (#416) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? - [* ] bug - [ ] feature - [ ] enhancement #### What does this PR do? fix meminfo bug #### Which issue(s)/PR(s) this PR relates to? fix issue #3356 #### Special notes for your reviewer, ex. impact of this fix, etc: #### Additional context/ Design document: #### Checklist: - [ ] Documentation affected (Please add the label if documentation needs to be modified.) - [ ] Incompatibility (If it breaks the compatibility, please describe it and add the corresponding label.) - [ ] If it's needed to cherry-pick (If cherry-pick to some branches is required, please label the destination version(s).) - [ ] Performance impacted: Consumes more CPU/Memory #### Release notes: Please confirm whether to be reflected in release notes and how to describe: > ` Migrated from vesoft-inc/nebula#3534 Co-authored-by: yuehua.jia <3423893+jiayuehua@users.noreply.github.com> --- .gitignore | 2 +- src/common/memory/MemoryUtils.cpp | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index a43680eef2b..99892e7e12d 100644 --- a/.gitignore +++ b/.gitignore @@ -66,6 +66,6 @@ venv/ #ctags .tags - +/.vs #license tests/secrets/nebula.license diff --git a/src/common/memory/MemoryUtils.cpp b/src/common/memory/MemoryUtils.cpp index e2803a15d75..7e0bb7db7cc 100644 --- a/src/common/memory/MemoryUtils.cpp +++ b/src/common/memory/MemoryUtils.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -21,7 +22,9 @@ using nebula::fs::FileUtils; namespace nebula { -static const std::regex reMemAvailable(R"(^Mem(Available|Total):\s+(\d+)\skB$)"); +static const std::regex reMemAvailable( + R"(^Mem(Available|Free|Total):\s+(\d+)\skB$)"); // when can't use MemAvailable, use MemFree + // instead. static const std::regex reTotalCache(R"(^total_(cache|inactive_file)\s+(\d+)$)"); std::atomic_bool MemoryUtils::kHitMemoryHighWatermark{false}; @@ -63,13 +66,13 @@ StatusOr MemoryUtils::hitsHighWatermark() { auto& sm = iter.matched(); memorySize.emplace_back(std::stoul(sm[2].str(), NULL) << 10); } - CHECK_EQ(memorySize.size(), 2U); - size_t i = 0, j = 1; - if (memorySize[0] < memorySize[1]) { - std::swap(i, j); + std::sort(memorySize.begin(), memorySize.end()); + if (memorySize.size() >= 2u) { + total = memorySize.back(); + available = memorySize[memorySize.size() - 2]; + } else { + return false; } - total = memorySize[i]; - available = memorySize[j]; } auto hits = (1 - available / total) > FLAGS_system_memory_high_watermark_ratio;