| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <cstdint> |
| 7 | #include <optional> |
| 8 | #include <string> |
| 9 | #include <string_view> |
| 10 | #include <vector> |
| 11 | |
| 12 | namespace Utils::CPU { |
| 13 | |
| 14 | /// Returns a piece of information from /proc/cpuinfo. |
| 15 | /// @param procCpuInfoLines /proc/cpuinfo data source contents lines |
| 16 | /// @param cpuId cpu id from which the info will be retrieved |
| 17 | /// @param target label that describes the information to retrieve |
| 18 | std::optional<std::string> |
| 19 | parseProcCpuInfo(std::vector<std::string> const &procCpuInfoLines, int cpuId, |
| 20 | std::string_view target); |
| 21 | |
| 22 | /// CPU Stat captured from /proc/stat. |
| 23 | struct Stat |
| 24 | { |
| 25 | std::uint64_t user; |
| 26 | std::uint64_t nice; |
| 27 | std::uint64_t system; |
| 28 | std::uint64_t idle; |
| 29 | std::uint64_t ioWait; |
| 30 | std::uint64_t irq; |
| 31 | std::uint64_t softIrq; |
| 32 | std::uint64_t steal; |
| 33 | std::uint64_t total; |
| 34 | }; |
| 35 | |
| 36 | /// Returns the aggregate CPU Stat from /proc/stat. |
| 37 | /// @param procStatLines /proc/stat data. |
| 38 | /// @returns CPU stat or none when the CPU stat cannot be parsed from the input data. |
| 39 | std::optional<Stat> parseProcStat(std::vector<std::string> const &procStatLines); |
| 40 | |
| 41 | /// Computes the used CPU in a time interval defined by statT0 and statT1. |
| 42 | /// @param statT0 CPU stat captured in time 0. |
| 43 | /// @param statT1 CPU stat captured in time 1 (some time after statT0). |
| 44 | /// @returns percentage of the CPU used in the time interval. |
| 45 | unsigned int computeCPUUsage(Stat const &statT0, Stat const &statT1); |
| 46 | |
| 47 | } // namespace Utils::CPU |