fix: force clear stream state introduced by rdbuf

This commit is contained in:
Paul Pan 2024-03-13 19:05:30 +08:00
parent d573c588b0
commit d7e29edd02

View File

@ -280,30 +280,41 @@ void dumpCgroupStats(nsjconf_t *nsjconf, const std::string &cgroup_path) {
return; return;
} }
auto res_name = [&](const std::string &res) -> std::string {
return cgroup_path + "/" + res;
};
auto get_length = [&](std::ifstream &f) -> size_t {
f.seekg(0, std::ios::end);
auto sz = f.tellg();
f.seekg(0, std::ios::beg);
return sz;
};
const auto stats = {"cpu.stat", "io.stat", "memory.stat"}; const auto stats = {"cpu.stat", "io.stat", "memory.stat"};
for (const auto &res : stats) { for (const auto &res : stats) {
std::ifstream f(cgroup_path + "/" + res); auto path = res_name(res);
std::ifstream f(path);
if (!f.is_open()) { if (!f.is_open()) {
PLOG_W("Failed to open cgroup resource file '%s'", PLOG_W("Invalid cgroup resource file '%s'", path.c_str());
(cgroup_path + "/" + res).c_str());
continue; continue;
} }
dump_file << f.rdbuf(); dump_file << f.rdbuf() << std::endl;
dump_file << std::endl; dump_file.clear();
f.close(); f.close();
} }
const auto mem_stats = {"memory.peak", "memory.swap.peak"}; const auto mem_stats = {"memory.peak", "memory.swap.peak"};
for (const auto &res : mem_stats) { for (const auto &res : mem_stats) {
std::ifstream f(cgroup_path + "/" + res); auto path = res_name(res);
std::ifstream f(path);
if (!f.is_open()) { if (!f.is_open()) {
PLOG_W("Failed to open cgroup resource file '%s'", PLOG_W("Invalid cgroup resource file '%s'", path.c_str());
(cgroup_path + "/" + res).c_str());
continue; continue;
} }
std::string line; std::string line;
std::getline(f, line); std::getline(f, line);
dump_file << res << ' ' << line << std::endl; dump_file << res << ' ' << line << std::endl;
dump_file.clear();
f.close(); f.close();
} }