| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "fanspeedperc.h" |
| 5 | |
| 6 | #include "../gpusensorprovider.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/info/igpuinfo.h" |
| 13 | #include "core/info/vendor.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 <filesystem> |
| 20 | #include <memory> |
| 21 | #include <optional> |
| 22 | #include <spdlog/spdlog.h> |
| 23 | #include <string> |
| 24 | #include <units.h> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace AMD::FanSpeedPerc { |
| 29 | |
| 30 | // NOTE FanSpeedPerc is a last resort fan speed sensor. |
| 31 | // |
| 32 | // It will be used only on systems without proper support of fan1_input |
| 33 | // (typically old hardware using radeon driver and some specific hardware supported |
| 34 | // by the amdgpu driver). |
| 35 | // |
| 36 | // See #184. |
| 37 | |
| 38 | class Provider final : public IGPUSensorProvider::IProvider |
| 39 | { |
| 40 | public: |
| 41 | std::vector<std::unique_ptr<ISensor>> |
| 42 | provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override |
| 43 | { |
| 44 | if (gpuInfo.vendor() != Vendor::AMD) |
| 45 | return {}; |
| 46 | |
| 47 | auto path = Utils::File::findHWMonXDirectory(gpuInfo.path().sys / "hwmon"); |
| 48 | if (!path) |
| 49 | return {}; |
| 50 | |
| 51 | unsigned int value; |
| 52 | |
| 53 | // When available, prefer fanspeedrpm sensor over this one |
| 54 | auto fanInput = path.value() / "fan1_input"; |
| 55 | if (Utils::File::isSysFSEntryValid(fanInput) && |
| 56 | Utils::String::toNumber<unsigned int>( |
| 57 | value, Utils::File::readFileLines(fanInput).front())) |
| 58 | return {}; |
| 59 | |
| 60 | auto pwm = path.value() / "pwm1"; |
| 61 | if (!Utils::File::isSysFSEntryValid(pwm)) |
| 62 | return {}; |
| 63 | |
| 64 | auto fileLines = Utils::File::readFileLines(pwm); |
| 65 | if (!Utils::String::toNumber<unsigned int>(value, fileLines.front())) { |
| 66 | SPDLOG_WARN("Unknown data format on {}", pwm.string()); |
| 67 | SPDLOG_DEBUG(fileLines.front()); |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 72 | dataSources.emplace_back(std::make_unique<SysFSDataSource<unsigned int>>( |
| 73 | pwm, [](std::string const &data, unsigned int &output) { |
| 74 | unsigned int value; |
| 75 | Utils::String::toNumber<unsigned int>(value, data); |
| 76 | output = value / 2.55; |
| 77 | })); |
| 78 | |
| 79 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 80 | sensors.emplace_back( |
| 81 | std::make_unique<Sensor<units::dimensionless::scalar_t, unsigned int>>( |
| 82 | AMD::FanSpeedPerc::ItemID, std::move(dataSources), |
| 83 | std::make_pair(units::dimensionless::scalar_t(0), |
| 84 | units::dimensionless::scalar_t(100)))); |
| 85 | |
| 86 | return sensors; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | static bool register_() |
| 91 | { |
| 92 | GPUSensorProvider::registerProvider( |
| 93 | std::make_unique<AMD::FanSpeedPerc::Provider>()); |
| 94 | |
| 95 | ProfilePartProvider::registerProvider(AMD::FanSpeedPerc::ItemID, []() { |
| 96 | return std::make_unique<GraphItemProfilePart>(AMD::FanSpeedPerc::ItemID, |
| 97 | "lightskyblue"); |
| 98 | }); |
| 99 | |
| 100 | ProfilePartXMLParserProvider::registerProvider(AMD::FanSpeedPerc::ItemID, []() { |
| 101 | return std::make_unique<GraphItemXMLParser>(AMD::FanSpeedPerc::ItemID); |
| 102 | }); |
| 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | static bool const registered_ = register_(); |
| 108 | |
| 109 | } // namespace AMD::FanSpeedPerc |