| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuutils.h" |
| 5 | |
| 6 | #include "common/stringutils.h" |
| 7 | #include <algorithm> |
| 8 | #include <iterator> |
| 9 | #include <regex> |
| 10 | |
| 11 | namespace Utils::CPU { |
| 12 | |
| 13 | std::optional<std::string> |
| 14 | parseProcCpuInfo(std::vector<std::string> const &procCpuInfoLines, int cpuId, |
| 15 | std::string_view target) |
| 16 | { |
| 17 | static constexpr std::string_view cpuIdStr{"processor"}; |
| 18 | |
| 19 | auto lineIt = std::find_if( |
| 20 | procCpuInfoLines.cbegin(), procCpuInfoLines.cend(), |
| 21 | [=](std::string const &line) { |
| 22 | if (line.find(cpuIdStr) != std::string::npos) { |
| 23 | int value; |
| 24 | auto idPos = line.find_first_not_of("\t: ", cpuIdStr.size()); |
| 25 | return idPos != std::string::npos && |
| 26 | Utils::String::toNumber<int>(value, line.substr(idPos)) && |
| 27 | value == cpuId; |
| 28 | } |
| 29 | return false; |
| 30 | }); |
| 31 | |
| 32 | if (lineIt != procCpuInfoLines.cend()) { |
| 33 | lineIt = std::next(lineIt); |
| 34 | |
| 35 | for (; lineIt != procCpuInfoLines.cend(); lineIt = std::next(lineIt)) { |
| 36 | |
| 37 | if (lineIt->empty()) |
| 38 | break; // target not found on this processor section |
| 39 | |
| 40 | auto targetPos = lineIt->find(target); |
| 41 | if (targetPos != std::string::npos) { |
| 42 | auto dataPos = lineIt->find_first_not_of("\t: ", target.size()); |
| 43 | if (dataPos != std::string::npos) |
| 44 | return lineIt->substr(dataPos); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return {}; |
| 50 | } |
| 51 | |
| 52 | std::optional<Stat> parseProcStat(std::vector<std::string> const &procStatLines) |
| 53 | { |
| 54 | // CPU aggregate line columns: |
| 55 | // cpu user nice system idle io_wait irq soft_irq steal guest guest_nice |
| 56 | // |
| 57 | // NOTE guest and guest_nice values are already included into user and nice. |
| 58 | // https://github.com/torvalds/linux/blob/master/kernel/sched/cputime.c#L143?h=5.0 |
| 59 | static std::regex const regex( |
| 60 | R"(^cpu\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+\d+\s+\d+\s*$)", |
| 61 | std::regex::icase); |
| 62 | |
| 63 | for (auto const &line : procStatLines) { |
| 64 | std::smatch result; |
| 65 | if (std::regex_match(line, result, regex)) { |
| 66 | std::uint64_t user{0}, nice{0}, system{0}, idle{0}, ioWait{0}, irq{0}, |
| 67 | softIrq{0}, steal{0}, total{0}; |
| 68 | |
| 69 | if (Utils::String::toNumber<std::uint64_t>(user, result[1]) && |
| 70 | Utils::String::toNumber<std::uint64_t>(nice, result[2]) && |
| 71 | Utils::String::toNumber<std::uint64_t>(system, result[3]) && |
| 72 | Utils::String::toNumber<std::uint64_t>(idle, result[4]) && |
| 73 | Utils::String::toNumber<std::uint64_t>(ioWait, result[5]) && |
| 74 | Utils::String::toNumber<std::uint64_t>(irq, result[6]) && |
| 75 | Utils::String::toNumber<std::uint64_t>(softIrq, result[7]) && |
| 76 | Utils::String::toNumber<std::uint64_t>(steal, result[8])) { |
| 77 | |
| 78 | total = user + nice + system + idle + ioWait + irq + softIrq + steal; |
| 79 | return Stat{user, nice, system, idle, ioWait, |
| 80 | irq, softIrq, steal, total}; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return {}; |
| 86 | } |
| 87 | |
| 88 | unsigned int computeCPUUsage(Stat const &statT0, Stat const &statT1) |
| 89 | { |
| 90 | auto total = statT1.total - statT0.total; |
| 91 | auto idle = (statT1.idle + statT1.ioWait) - (statT0.idle + statT0.ioWait); |
| 92 | auto used = total - idle; |
| 93 | |
| 94 | return (100.0 * used) / total; |
| 95 | } |
| 96 | |
| 97 | } // namespace Utils::CPU |