| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gputemp.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::GPUTemp { |
| 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) |
| 41 | return {}; |
| 42 | |
| 43 | std::optional< |
| 44 | std::pair<units::temperature::celsius_t, units::temperature::celsius_t>> |
| 45 | range; |
| 46 | |
| 47 | auto critFilePath = path.value() / "temp1_crit"; |
| 48 | if (Utils::File::isFilePathValid(critFilePath)) { |
| 49 | |
| 50 | auto data = Utils::File::readFileLines(critFilePath); |
| 51 | if (!data.empty()) { |
| 52 | int value; |
| 53 | if (Utils::String::toNumber<int>(value, data.front()) && |
| 54 | // do not use bogus values, see #103 |
| 55 | (value >= 0 && value < 150000)) { |
| 56 | range = {units::temperature::celsius_t(0), |
| 57 | units::temperature::celsius_t(value / 1000.0)}; |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | auto tempInput = path.value() / "temp1_input"; |
| 63 | if (!Utils::File::isSysFSEntryValid(tempInput)) |
| 64 | return {}; |
| 65 | |
| 66 | int value; |
| 67 | auto tempInputLines = Utils::File::readFileLines(tempInput); |
| 68 | |
| 69 | if (!Utils::String::toNumber<int>(value, tempInputLines.front())) { |
| 70 | SPDLOG_WARN("Unknown data format on {}", tempInput.string()); |
| 71 | SPDLOG_DEBUG(tempInputLines.front()); |
| 72 | return {}; |
| 73 | } |
| 74 | |
| 75 | std::vector<std::unique_ptr<IDataSource<int>>> dataSources; |
| 76 | dataSources.emplace_back(std::make_unique<SysFSDataSource<int>>( |
| 77 | tempInput, [](std::string const &data, int &output) { |
| 78 | int value; |
| 79 | Utils::String::toNumber<int>(value, data); |
| 80 | output = value / 1000; |
| 81 | })); |
| 82 | |
| 83 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 84 | sensors.emplace_back( |
| 85 | std::make_unique<Sensor<units::temperature::celsius_t, int>>( |
| 86 | AMD::GPUTemp::ItemID, std::move(dataSources), std::move(range))); |
| 87 | |
| 88 | return sensors; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | static bool register_() |
| 93 | { |
| 94 | GPUSensorProvider::registerProvider(std::make_unique<AMD::GPUTemp::Provider>()); |
| 95 | |
| 96 | ProfilePartProvider::registerProvider(AMD::GPUTemp::ItemID, []() { |
| 97 | return std::make_unique<GraphItemProfilePart>(AMD::GPUTemp::ItemID, |
| 98 | "crimson"); |
| 99 | }); |
| 100 | |
| 101 | ProfilePartXMLParserProvider::registerProvider(AMD::GPUTemp::ItemID, []() { |
| 102 | return std::make_unique<GraphItemXMLParser>(AMD::GPUTemp::ItemID); |
| 103 | }); |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | static bool const registered_ = register_(); |
| 109 | |
| 110 | } // namespace AMD::GPUTemp |