From d573c588b00a3964f709d645f2a228061a6ff42c Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Wed, 13 Mar 2024 17:58:33 +0800 Subject: [PATCH] feat: add memory related stats --- cgroup2.cc | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/cgroup2.cc b/cgroup2.cc index bb71d59..97504a9 100644 --- a/cgroup2.cc +++ b/cgroup2.cc @@ -280,17 +280,31 @@ void dumpCgroupStats(nsjconf_t *nsjconf, const std::string &cgroup_path) { return; } - const auto resources = {"cpu.stat", "io.stat", "memory.stat"}; - for (const auto &resource : resources) { - std::ifstream resource_file(cgroup_path + "/" + resource); - if (!resource_file.is_open()) { + const auto stats = {"cpu.stat", "io.stat", "memory.stat"}; + for (const auto &res : stats) { + std::ifstream f(cgroup_path + "/" + res); + if (!f.is_open()) { PLOG_W("Failed to open cgroup resource file '%s'", - (cgroup_path + "/" + resource).c_str()); + (cgroup_path + "/" + res).c_str()); continue; } - dump_file << resource_file.rdbuf(); + dump_file << f.rdbuf(); dump_file << std::endl; - resource_file.close(); + f.close(); + } + + const auto mem_stats = {"memory.peak", "memory.swap.peak"}; + for (const auto &res : mem_stats) { + std::ifstream f(cgroup_path + "/" + res); + if (!f.is_open()) { + PLOG_W("Failed to open cgroup resource file '%s'", + (cgroup_path + "/" + res).c_str()); + continue; + } + std::string line; + std::getline(f, line); + dump_file << res << ' ' << line << std::endl; + f.close(); } dump_file.close();