| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "cpufreqpack.h" |
| 5 | |
| 6 | #include "../cpusensorprovider.h" |
| 7 | #include "../graphitemprofilepart.h" |
| 8 | #include "../graphitemxmlparser.h" |
| 9 | #include "../sensor.h" |
| 10 | #include "common/fileutils.h" |
| 11 | #include "common/stringutils.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 <spdlog/spdlog.h> |
| 24 | #include <string> |
| 25 | #include <units.h> |
| 26 | #include <utility> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace CPUFreqPack { |
| 30 | |
| 31 | class Provider final : public ICPUSensorProvider::IProvider |
| 32 | { |
| 33 | public: |
| 34 | std::vector<std::unique_ptr<ISensor>> |
| 35 | provideCPUSensors(ICPUInfo const &cpuInfo, ISWInfo const &) const override |
| 36 | { |
| 37 | if (!Utils::File::isDirectoryPathValid("/sys/devices/system/cpu/cpufreq")) |
| 38 | return {}; |
| 39 | |
| 40 | auto &executionUnits = cpuInfo.executionUnits(); |
| 41 | if (executionUnits.empty()) |
| 42 | return {}; |
| 43 | |
| 44 | std::optional< |
| 45 | std::pair<units::frequency::megahertz_t, units::frequency::megahertz_t>> |
| 46 | range; |
| 47 | |
| 48 | auto minFreqPath = executionUnits.front().sysPath / |
| 49 | "cpufreq/cpuinfo_min_freq"; |
| 50 | auto maxFreqPath = executionUnits.front().sysPath / |
| 51 | "cpufreq/cpuinfo_max_freq"; |
| 52 | if (Utils::File::isSysFSEntryValid(minFreqPath) && |
| 53 | Utils::File::isSysFSEntryValid(maxFreqPath)) { |
| 54 | auto minFreqLines = Utils::File::readFileLines(minFreqPath); |
| 55 | auto maxFreqLines = Utils::File::readFileLines(maxFreqPath); |
| 56 | |
| 57 | unsigned int minFreq{0}; |
| 58 | unsigned int maxFreq{0}; |
| 59 | if (Utils::String::toNumber<unsigned int>(minFreq, minFreqLines.front()) && |
| 60 | Utils::String::toNumber<unsigned int>(maxFreq, maxFreqLines.front())) { |
| 61 | if (minFreq < maxFreq) |
| 62 | range = {units::frequency::kilohertz_t(minFreq), |
| 63 | units::frequency::kilohertz_t(maxFreq)}; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 68 | for (auto const &executionUnit : cpuInfo.executionUnits()) { |
| 69 | auto curFreqPath = executionUnit.sysPath / "cpufreq/scaling_cur_freq"; |
| 70 | if (!Utils::File::isSysFSEntryValid(curFreqPath)) |
| 71 | continue; |
| 72 | |
| 73 | unsigned int value; |
| 74 | auto curFreqLines = Utils::File::readFileLines(curFreqPath); |
| 75 | if (!Utils::String::toNumber<unsigned int>(value, curFreqLines.front())) { |
| 76 | SPDLOG_WARN("Unknown data format on {}", curFreqPath.string()); |
| 77 | SPDLOG_DEBUG(curFreqLines.front()); |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | dataSources.emplace_back(std::make_unique<SysFSDataSource<unsigned int>>( |
| 82 | curFreqPath, [](std::string const &data, unsigned int &output) { |
| 83 | Utils::String::toNumber<unsigned int>(output, data); |
| 84 | })); |
| 85 | } |
| 86 | |
| 87 | if (dataSources.empty()) |
| 88 | return {}; |
| 89 | |
| 90 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 91 | sensors.emplace_back( |
| 92 | std::make_unique<Sensor<units::frequency::megahertz_t, unsigned int>>( |
| 93 | CPUFreqPack::ItemID, std::move(dataSources), std::move(range), |
| 94 | [](std::vector<unsigned int> const &input) { |
| 95 | auto maxIter = std::max_element(input.cbegin(), input.cend()); |
| 96 | if (maxIter != input.cend()) { |
| 97 | units::frequency::kilohertz_t maxKHz(*maxIter); |
| 98 | return maxKHz.convert<units::frequency::megahertz>() |
| 99 | .to<unsigned int>(); |
| 100 | } |
| 101 | else |
| 102 | return 0u; |
| 103 | })); |
| 104 | |
| 105 | return sensors; |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | static bool register_() |
| 110 | { |
| 111 | CPUSensorProvider::registerProvider(std::make_unique<CPUFreqPack::Provider>()); |
| 112 | |
| 113 | ProfilePartProvider::registerProvider(CPUFreqPack::ItemID, []() { |
| 114 | return std::make_unique<GraphItemProfilePart>(CPUFreqPack::ItemID, |
| 115 | "fuchsia"); |
| 116 | }); |
| 117 | |
| 118 | ProfilePartXMLParserProvider::registerProvider(CPUFreqPack::ItemID, []() { |
| 119 | return std::make_unique<GraphItemXMLParser>(CPUFreqPack::ItemID); |
| 120 | }); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | static bool const registered_ = register_(); |
| 126 | |
| 127 | } // namespace CPUFreqPack |