mirror of
https://github.com/NixOS/nix.git
synced 2025-12-09 10:31:02 +01:00
Add getCgroupStats() function
This commit is contained in:
parent
890a4e980a
commit
2c28502bc4
2 changed files with 35 additions and 22 deletions
|
|
@ -49,6 +49,33 @@ StringMap getCgroups(const Path & cgroupFile)
|
||||||
return cgroups;
|
return cgroups;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CgroupStats getCgroupStats(const std::filesystem::path & cgroup)
|
||||||
|
{
|
||||||
|
CgroupStats stats;
|
||||||
|
|
||||||
|
auto cpustatPath = cgroup / "cpu.stat";
|
||||||
|
|
||||||
|
if (pathExists(cpustatPath)) {
|
||||||
|
for (auto & line : tokenizeString<std::vector<std::string>>(readFile(cpustatPath), "\n")) {
|
||||||
|
std::string_view userPrefix = "user_usec ";
|
||||||
|
if (hasPrefix(line, userPrefix)) {
|
||||||
|
auto n = string2Int<uint64_t>(line.substr(userPrefix.size()));
|
||||||
|
if (n)
|
||||||
|
stats.cpuUser = std::chrono::microseconds(*n);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view systemPrefix = "system_usec ";
|
||||||
|
if (hasPrefix(line, systemPrefix)) {
|
||||||
|
auto n = string2Int<uint64_t>(line.substr(systemPrefix.size()));
|
||||||
|
if (n)
|
||||||
|
stats.cpuSystem = std::chrono::microseconds(*n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stats;
|
||||||
|
}
|
||||||
|
|
||||||
static CgroupStats destroyCgroup(const std::filesystem::path & cgroup, bool returnStats)
|
static CgroupStats destroyCgroup(const std::filesystem::path & cgroup, bool returnStats)
|
||||||
{
|
{
|
||||||
if (!pathExists(cgroup))
|
if (!pathExists(cgroup))
|
||||||
|
|
@ -114,28 +141,8 @@ static CgroupStats destroyCgroup(const std::filesystem::path & cgroup, bool retu
|
||||||
}
|
}
|
||||||
|
|
||||||
CgroupStats stats;
|
CgroupStats stats;
|
||||||
|
if (returnStats)
|
||||||
if (returnStats) {
|
stats = getCgroupStats(cgroup);
|
||||||
auto cpustatPath = cgroup / "cpu.stat";
|
|
||||||
|
|
||||||
if (pathExists(cpustatPath)) {
|
|
||||||
for (auto & line : tokenizeString<std::vector<std::string>>(readFile(cpustatPath), "\n")) {
|
|
||||||
std::string_view userPrefix = "user_usec ";
|
|
||||||
if (hasPrefix(line, userPrefix)) {
|
|
||||||
auto n = string2Int<uint64_t>(line.substr(userPrefix.size()));
|
|
||||||
if (n)
|
|
||||||
stats.cpuUser = std::chrono::microseconds(*n);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string_view systemPrefix = "system_usec ";
|
|
||||||
if (hasPrefix(line, systemPrefix)) {
|
|
||||||
auto n = string2Int<uint64_t>(line.substr(systemPrefix.size()));
|
|
||||||
if (n)
|
|
||||||
stats.cpuSystem = std::chrono::microseconds(*n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rmdir(cgroup.c_str()) == -1)
|
if (rmdir(cgroup.c_str()) == -1)
|
||||||
throw SysError("deleting cgroup %s", cgroup);
|
throw SysError("deleting cgroup %s", cgroup);
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include "nix/util/types.hh"
|
#include "nix/util/types.hh"
|
||||||
|
|
||||||
|
|
@ -17,6 +18,11 @@ struct CgroupStats
|
||||||
std::optional<std::chrono::microseconds> cpuUser, cpuSystem;
|
std::optional<std::chrono::microseconds> cpuUser, cpuSystem;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read statistics from the given cgroup.
|
||||||
|
*/
|
||||||
|
CgroupStats getCgroupStats(const std::filesystem::path & cgroup);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destroy the cgroup denoted by 'path'. The postcondition is that
|
* Destroy the cgroup denoted by 'path'. The postcondition is that
|
||||||
* 'path' does not exist, and thus any processes in the cgroup have
|
* 'path' does not exist, and thus any processes in the cgroup have
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue