| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2022 Milan Cermak <[email protected]> |
| 3 | // Copyright 2023 Juan Palacios <[email protected]> |
| 4 | |
| 5 | #include "cpuusage.h" |
| 6 | |
| 7 | #include "../cpusensorprovider.h" |
| 8 | #include "../graphitemprofilepart.h" |
| 9 | #include "../graphitemxmlparser.h" |
| 10 | #include "../sensor.h" |
| 11 | #include "core/components/cpuutils.h" |
| 12 | #include "core/idatasource.h" |
| 13 | #include "core/info/icpuinfo.h" |
| 14 | #include "core/iprofilepart.h" |
| 15 | #include "core/iprofilepartxmlparser.h" |
| 16 | #include "core/profilepartprovider.h" |
| 17 | #include "core/profilepartxmlparserprovider.h" |
| 18 | #include "core/sysfsdatasource.h" |
| 19 | #include <algorithm> |
| 20 | #include <filesystem> |
| 21 | #include <memory> |
| 22 | #include <optional> |
| 23 | #include <string> |
| 24 | #include <units.h> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace CPUUsage { |
| 29 | |
| 30 | class CPUUsageDataSource : public IDataSource<unsigned int> |
| 31 | { |
| 32 | public: |
| 33 | CPUUsageDataSource() noexcept |
| 34 | : procStatDataSource_("/proc/stat") |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | std::string source() const override |
| 39 | { |
| 40 | return procStatDataSource_.source(); |
| 41 | } |
| 42 | |
| 43 | bool read(unsigned int &usedCPU) override |
| 44 | { |
| 45 | if (procStatDataSource_.read(rawData_)) { |
| 46 | |
| 47 | auto stat = Utils::CPU::parseProcStat(rawData_); |
| 48 | rawData_.clear(); |
| 49 | |
| 50 | if (stat.has_value()) { |
| 51 | |
| 52 | if (prevStat_.has_value()) |
| 53 | usedCPU = Utils::CPU::computeCPUUsage(*prevStat_, *stat); |
| 54 | else |
| 55 | usedCPU = 0; |
| 56 | |
| 57 | prevStat_.swap(stat); |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | SysFSDataSource<std::vector<std::string>> procStatDataSource_; |
| 67 | std::vector<std::string> rawData_; |
| 68 | std::optional<Utils::CPU::Stat> prevStat_; |
| 69 | }; |
| 70 | |
| 71 | class Provider final : public ICPUSensorProvider::IProvider |
| 72 | { |
| 73 | public: |
| 74 | std::vector<std::unique_ptr<ISensor>> |
| 75 | provideCPUSensors(ICPUInfo const &, ISWInfo const &) const override |
| 76 | { |
| 77 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 78 | |
| 79 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 80 | dataSources.emplace_back(std::make_unique<CPUUsageDataSource>()); |
| 81 | |
| 82 | sensors.emplace_back( |
| 83 | std::make_unique<Sensor<units::dimensionless::scalar_t, unsigned int>>( |
| 84 | CPUUsage::ItemID, std::move(dataSources), |
| 85 | std::make_pair(units::dimensionless::scalar_t(0), |
| 86 | units::dimensionless::scalar_t(100)))); |
| 87 | |
| 88 | return sensors; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | static bool register_() |
| 93 | { |
| 94 | CPUSensorProvider::registerProvider(std::make_unique<CPUUsage::Provider>()); |
| 95 | |
| 96 | ProfilePartProvider::registerProvider(CPUUsage::ItemID, []() { |
| 97 | return std::make_unique<GraphItemProfilePart>(CPUUsage::ItemID, |
| 98 | "yellowgreen"); |
| 99 | }); |
| 100 | |
| 101 | ProfilePartXMLParserProvider::registerProvider(CPUUsage::ItemID, []() { |
| 102 | return std::make_unique<GraphItemXMLParser>(CPUUsage::ItemID); |
| 103 | }); |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | static bool const registered_ = register_(); |
| 109 | |
| 110 | } // namespace CPUUsage |