| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "memusage.h" |
| 5 | |
| 6 | #include "../gpusensorprovider.h" |
| 7 | #include "../graphitemprofilepart.h" |
| 8 | #include "../graphitemxmlparser.h" |
| 9 | #include "../sensor.h" |
| 10 | #include "common/stringutils.h" |
| 11 | #include "core/components/amdutils.h" |
| 12 | #include "core/devfsdatasource.h" |
| 13 | #include "core/info/igpuinfo.h" |
| 14 | #include "core/info/vendor.h" |
| 15 | #include "core/iprofilepart.h" |
| 16 | #include "core/iprofilepartxmlparser.h" |
| 17 | #include "core/profilepartprovider.h" |
| 18 | #include "core/profilepartxmlparserprovider.h" |
| 19 | #include <cstdint> |
| 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 AMD::MemUsage { |
| 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 | std::optional<std::pair<units::data::megabyte_t, units::data::megabyte_t>> range; |
| 40 | |
| 41 | auto memInfo = gpuInfo.info(IGPUInfo::Keys::memory); |
| 42 | if (!memInfo.empty()) { |
| 43 | unsigned int memorySize; |
| 44 | if (Utils::String::toNumber<unsigned int>(memorySize, memInfo)) |
| 45 | range = {units::data::megabyte_t(0), units::data::megabyte_t(memorySize)}; |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<ISensor> sensor; |
| 49 | |
| 50 | auto driver = gpuInfo.info(IGPUInfo::Keys::driver); |
| 51 | if (driver == "amdgpu") |
| 52 | sensor = createAMDGPUSensor(gpuInfo, std::move(range)); |
| 53 | else if (driver == "radeon") |
| 54 | sensor = createRadeonSensor(gpuInfo, std::move(range)); |
| 55 | |
| 56 | if (!sensor) |
| 57 | return {}; |
| 58 | |
| 59 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 60 | sensors.emplace_back(std::move(sensor)); |
| 61 | |
| 62 | return sensors; |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | std::unique_ptr<ISensor> createAMDGPUSensor( |
| 67 | IGPUInfo const &gpuInfo, |
| 68 | std::optional<std::pair<units::data::megabyte_t, units::data::megabyte_t>> |
| 69 | &&range) const |
| 70 | { |
| 71 | #if defined(AMDGPU_INFO_VRAM_USAGE) |
| 72 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 73 | dataSources.emplace_back(std::make_unique<DevFSDataSource<unsigned int>>( |
| 74 | gpuInfo.path().dev, [](int fd) { |
| 75 | std::uint64_t value; |
| 76 | bool success = Utils::AMD::readAMDGPUInfo(fd, &value, |
| 77 | AMDGPU_INFO_VRAM_USAGE); |
| 78 | return success ? value / (1024 * 1024) : 0; |
| 79 | })); |
| 80 | |
| 81 | return std::make_unique<Sensor<units::data::megabyte_t, unsigned int>>( |
| 82 | AMD::MemUsage::ItemID, std::move(dataSources), std::move(range)); |
| 83 | #else |
| 84 | return {}; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<ISensor> createRadeonSensor( |
| 89 | IGPUInfo const &gpuInfo, |
| 90 | std::optional<std::pair<units::data::megabyte_t, units::data::megabyte_t>> |
| 91 | &&range) const |
| 92 | { |
| 93 | #if defined(RADEON_INFO_VRAM_USAGE) |
| 94 | std::vector<std::unique_ptr<IDataSource<unsigned int>>> dataSources; |
| 95 | dataSources.emplace_back(std::make_unique<DevFSDataSource<unsigned int>>( |
| 96 | gpuInfo.path().dev, [](int fd) { |
| 97 | std::uint64_t value; |
| 98 | bool success = Utils::AMD::readRadeonInfoSensor( |
| 99 | fd, &value, RADEON_INFO_VRAM_USAGE); |
| 100 | return success ? value / (1024 * 1024) : 0; |
| 101 | })); |
| 102 | |
| 103 | return std::make_unique<Sensor<units::data::megabyte_t, unsigned int>>( |
| 104 | AMD::MemUsage::ItemID, std::move(dataSources), std::move(range)); |
| 105 | #else |
| 106 | return {}; |
| 107 | #endif |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | static bool register_() |
| 112 | { |
| 113 | GPUSensorProvider::registerProvider( |
| 114 | std::make_unique<AMD::MemUsage::Provider>()); |
| 115 | |
| 116 | ProfilePartProvider::registerProvider(AMD::MemUsage::ItemID, []() { |
| 117 | return std::make_unique<GraphItemProfilePart>(AMD::MemUsage::ItemID, |
| 118 | "forestgreen"); |
| 119 | }); |
| 120 | |
| 121 | ProfilePartXMLParserProvider::registerProvider(AMD::MemUsage::ItemID, []() { |
| 122 | return std::make_unique<GraphItemXMLParser>(AMD::MemUsage::ItemID); |
| 123 | }); |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | static bool const registered_ = register_(); |
| 129 | |
| 130 | } // namespace AMD::MemUsage |