| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/core/components/cpuutils.cpp`/ |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpuutils.h"cxx . . $ `<file>/src/core/components/cpuutils.h`/ |
| 5 | |
| 6 | #include "common/stringutils.h"cxx . . $ `<file>/src/common/stringutils.h`/ |
| 7 | #include <algorithm> |
| 8 | #include <iterator> |
| 9 | #include <regex> |
| 10 | |
| 11 | namespace Utilscxx . . $ Utils/::CPUcxx . . $ Utils/CPU/ { |
| 12 | |
| 13 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<stdcxx . . $ std/::stringcxx . . $ std/string#> |
| 14 | parseProcCpuInfocxx . . $ Utils/CPU/parseProcCpuInfo(6ce55c29d0623432).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ std/string#> const &procCpuInfoLines, int cpuId, |
| 15 | stdcxx . . $ std/::string_viewcxx . . $ std/string_view# target) |
| 16 | { |
| 17 | static constexpr stdcxx . . $ std/::string_viewcxx . . $ std/string_view# cpuIdStrcxx . . $ std/basic_string_view#basic_string_view(837a1ee5a10cb792).{"processor"}; |
| 18 | |
| 19 | auto lineIt = stdcxx . . $ std/::find_ifcxx . . $ std/find_if(f0a0a4f94ee5cb00).( |
| 20 | procCpuInfoLines.cbegincxx . . $ std/vector#cbegin(d1e1699016b9d7e6).(), procCpuInfoLines.cendcxx . . $ std/vector#cend(d1e1699016b9d7e6).(), |
| 21 | [=](stdcxx . . $ std/::stringcxx . . $ std/string# const &line) { |
| 22 | if (line.findcxx . . $ std/__cxx11/basic_string#find(f84f8bea57cb23bc).(cpuIdStr) != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) { |
| 23 | int value; |
| 24 | auto idPos = line.find_first_not_ofcxx . . $ std/__cxx11/basic_string#find_first_not_of(5ed402c22c298042).("\t: ", cpuIdStr.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).()); |
| 25 | return idPos != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos. && |
| 26 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<int>(value, line.substrcxx . . $ std/__cxx11/basic_string#substr(d59a03e0b0717fa0).(idPos)) && |
| 27 | value == cpuId; |
| 28 | } |
| 29 | return false; |
| 30 | }); |
| 31 | |
| 32 | if (lineIt != procCpuInfoLines.cendcxx . . $ std/vector#cend(d1e1699016b9d7e6).()) { |
| 33 | lineIt =cxx . . $ __gnu_cxx/__normal_iterator#`operator=`(ff1991cdf94b87cb). stdcxx . . $ std/::nextcxx . . $ std/next(31d62cd24490f169).(lineIt); |
| 34 | |
| 35 | for (; lineIt != procCpuInfoLines.cendcxx . . $ std/vector#cend(d1e1699016b9d7e6).(); lineIt =cxx . . $ __gnu_cxx/__normal_iterator#`operator=`(ff1991cdf94b87cb). stdcxx . . $ std/::nextcxx . . $ std/next(31d62cd24490f169).(lineIt)) { |
| 36 | |
| 37 | if (lineIt->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).()) |
| 38 | break; // target not found on this processor section |
| 39 | |
| 40 | auto targetPos = lineIt->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).findcxx . . $ std/__cxx11/basic_string#find(f84f8bea57cb23bc).(target); |
| 41 | if (targetPos != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) { |
| 42 | auto dataPos = lineIt->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).find_first_not_ofcxx . . $ std/__cxx11/basic_string#find_first_not_of(5ed402c22c298042).("\t: ", target.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).()); |
| 43 | if (dataPos != stdcxx . . $ std/::stringcxx . . $ std/string#::nposcxx . . $ std/__cxx11/basic_string#npos.) |
| 44 | return lineItcxx . . $ std/optional#optional(e3f0d6382bd52f24).->cxx . . $ __gnu_cxx/__normal_iterator#`operator->`(65c2ec7035a77d3e).substrcxx . . $ std/__cxx11/basic_string#substr(d59a03e0b0717fa0).(dataPos); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return {cxx . . $ std/optional#optional(ced63f7c635d850d).}; |
| 50 | } |
| 51 | |
| 52 | stdcxx . . $ std/::optionalcxx . . $ std/optional#<Statcxx . . $ Utils/CPU/Stat#> parseProcStatcxx . . $ Utils/CPU/parseProcStat(c74a6c71ef0cb6d8).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::stringcxx . . $ 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 stdcxx . . $ std/::regexcxx . . $ std/__cxx11/regex# const regexcxx . . $ std/__cxx11/basic_regex#basic_regex(d1adbc785ae219ab).( |
| 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 | stdcxx . . $ std/::regexcxx . . $ std/__cxx11/regex#::icasecxx . . $ std/__cxx11/basic_regex#icase.); |
| 62 | |
| 63 | for (auto const &line : procStatLines) { |
| 64 | stdcxx . . $ std/::smatchcxx . . $ std/__cxx11/smatch# resultcxx . . $ std/__cxx11/match_results#match_results(49f6e7a06ebc5aa8).; |
| 65 | if (stdcxx . . $ std/::regex_matchcxx . . $ std/regex_match(428e6b8e81328b43).(line, result, regex)) { |
| 66 | stdcxx . . $ std/::uint64_tcxx . . $ 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 (Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(user, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).1]) && |
| 70 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(nice, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).2]) && |
| 71 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(system, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).3]) && |
| 72 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(idle, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).4]) && |
| 73 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(ioWait, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).5]) && |
| 74 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(irq, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).6]) && |
| 75 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(softIrq, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).7]) && |
| 76 | Utilscxx . . $ Utils/::Stringcxx . . $ Utils/String/::toNumbercxx . . $ Utils/String/toNumber(c75d689ebd728b7b).<stdcxx . . $ std/::uint64_tcxx . . $ std/uint64_t#>(steal, result[cxx . . $ std/__cxx11/match_results#`operator[]`(f2afb01e2bf6aaf8).8])) { |
| 77 | |
| 78 | total = user + nice + system + idle + ioWait + irq + softIrq + steal; |
| 79 | return Statcxx . . $ std/optional#optional(e3f0d6382bd52f24).{user, nice, system, idle, ioWait, |
| 80 | irq, softIrq, steal, total}; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return {cxx . . $ std/optional#optional(ced63f7c635d850d).}; |
| 86 | } |
| 87 | |
| 88 | unsigned int computeCPUUsagecxx . . $ Utils/CPU/computeCPUUsage(501d82387ff0e1d5).(Statcxx . . $ Utils/CPU/Stat# const &statT0, Statcxx . . $ Utils/CPU/Stat# const &statT1) |
| 89 | { |
| 90 | auto total = statT1.totalcxx . . $ Utils/CPU/Stat#total. - statT0.totalcxx . . $ Utils/CPU/Stat#total.; |
| 91 | auto idle = (statT1.idlecxx . . $ Utils/CPU/Stat#idle. + statT1.ioWaitcxx . . $ Utils/CPU/Stat#ioWait.) - (statT0.idlecxx . . $ Utils/CPU/Stat#idle. + statT0.ioWaitcxx . . $ Utils/CPU/Stat#ioWait.); |
| 92 | auto used = total - idle; |
| 93 | |
| 94 | return (100.0 * used) / total; |
| 95 | } |
| 96 | |
| 97 | } // namespace Utils::CPU |