| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2021 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuvolt.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::GPUVolt { |
| 29 | |
| 30 | class Provider final : public IGPUSensorProvider::IProvider |
| 31 | { |
| 32 | public: |
| 33 | std::vector<std::unique_ptr<ISensor>> |
| 34 | provideGPUSensors(IGPUInfo const &gpuInfo, ISWInfo const &) const override |
| 35 | { |
| 36 | if (gpuInfo.vendor() != Vendor::AMD) |
| 37 | return {}; |
| 38 | |
| 39 | auto path = Utils::File::findHWMonXDirectory(gpuInfo.path().sys / "hwmon"); |
| 40 | if (!path.has_value()) |
| 41 | return {}; |
| 42 | |
| 43 | auto voltInput = path.value() / "in0_input"; |
| 44 | if (!Utils::File::isSysFSEntryValid(voltInput)) |
| 45 | return {}; |
| 46 | |
| 47 | int value; |
| 48 | auto voltInputLines = Utils::File::readFileLines(voltInput); |
| 49 | if (!Utils::String::toNumber<int>(value, voltInputLines.front())) { |
| 50 | SPDLOG_WARN("Unknown data format on {}", voltInput.string()); |
| 51 | SPDLOG_DEBUG(voltInputLines.front()); |
| 52 | return {}; |
| 53 | } |
| 54 | |
| 55 | // auto scaling range |
| 56 | std::optional<std::pair<units::voltage::millivolt_t, units::voltage::millivolt_t>> |
| 57 | range; |
| 58 | |
| 59 | std::vector<std::unique_ptr<IDataSource<int>>> dataSources; |
| 60 | dataSources.emplace_back(std::make_unique<SysFSDataSource<int>>( |
| 61 | voltInput, [](std::string const &data, int &output) { |
| 62 | Utils::String::toNumber<int>(output, data); |
| 63 | })); |
| 64 | |
| 65 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 66 | sensors.emplace_back( |
| 67 | std::make_unique<Sensor<units::voltage::millivolt_t, int>>( |
| 68 | AMD::GPUVolt::ItemID, std::move(dataSources), std::move(range))); |
| 69 | |
| 70 | return sensors; |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | static bool register_() |
| 75 | { |
| 76 | GPUSensorProvider::registerProvider(std::make_unique<AMD::GPUVolt::Provider>()); |
| 77 | |
| 78 | ProfilePartProvider::registerProvider(AMD::GPUVolt::ItemID, []() { |
| 79 | return std::make_unique<GraphItemProfilePart>(AMD::GPUVolt::ItemID, |
| 80 | "darkorange"); |
| 81 | }); |
| 82 | |
| 83 | ProfilePartXMLParserProvider::registerProvider(AMD::GPUVolt::ItemID, []() { |
| 84 | return std::make_unique<GraphItemXMLParser>(AMD::GPUVolt::ItemID); |
| 85 | }); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | static bool const registered_ = register_(); |
| 91 | |
| 92 | } // namespace AMD::GPUVolt |