Skip to content

Commit

Permalink
Condition memory checks on cgroup version. (#3419)
Browse files Browse the repository at this point in the history
* Condition memory checks on cgroup version.
* Fix formatting.
  • Loading branch information
igrekun authored Dec 8, 2021
1 parent d6f83f3 commit e33924f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/common/memory/MemoryUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@ StatusOr<bool> MemoryUtils::hitsHighWatermark() {
}
double available = 0.0, total = 0.0;
if (FLAGS_containerized) {
FileUtils::FileLineIterator iter("/sys/fs/cgroup/memory/memory.stat", &reTotalCache);
bool cgroupsv2 = FileUtils::exist("/sys/fs/cgroup/cgroup.controllers");
std::string statPath =
cgroupsv2 ? "/sys/fs/cgroup/memory.stat" : "/sys/fs/cgroup/memory/memory.stat";
FileUtils::FileLineIterator iter(statPath, &reTotalCache);
uint64_t cacheSize = 0;
for (; iter.valid(); ++iter) {
auto& sm = iter.matched();
cacheSize += std::stoul(sm[2].str(), NULL);
}

auto limitStatus = MemoryUtils::readSysContents("/sys/fs/cgroup/memory/memory.limit_in_bytes");
std::string limitPath =
cgroupsv2 ? "/sys/fs/cgroup/memory.max" : "/sys/fs/cgroup/memory/memory.limit_in_bytes";
auto limitStatus = MemoryUtils::readSysContents(limitPath);
NG_RETURN_IF_ERROR(limitStatus);
uint64_t limitInBytes = std::move(limitStatus).value();

auto usageStatus = MemoryUtils::readSysContents("/sys/fs/cgroup/memory/memory.usage_in_bytes");
std::string usagePath =
cgroupsv2 ? "/sys/fs/cgroup/memory.current" : "/sys/fs/cgroup/memory/memory.usage_in_bytes";
auto usageStatus = MemoryUtils::readSysContents(usagePath);
NG_RETURN_IF_ERROR(usageStatus);
uint64_t usageInBytes = std::move(usageStatus).value();

Expand Down

0 comments on commit e33924f

Please sign in to comment.