| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2021 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "gpuinfovram.h" |
| 5 | |
| 6 | #include "../common/gpuinfoueventdatasource.h" |
| 7 | #include "../common/swinfokerneldatasource.h" |
| 8 | #include "common/stringutils.h" |
| 9 | #include "core/components/amdutils.h" |
| 10 | #include "core/devfsdatasource.h" |
| 11 | #include "core/info/infoproviderregistry.h" |
| 12 | #include <format> |
| 13 | #include <spdlog/spdlog.h> |
| 14 | #include <tuple> |
| 15 | #include <utility> |
| 16 | |
| 17 | class AMDGPUInfoVRamDataSource |
| 18 | : public IDataSource<units::data::megabyte_t, std::filesystem::path const> |
| 19 | { |
| 20 | public: |
| 21 | std::string source() const override |
| 22 | { |
| 23 | return ""; |
| 24 | } |
| 25 | |
| 26 | bool read(units::data::megabyte_t &data, |
| 27 | std::filesystem::path const &path) override |
| 28 | { |
| 29 | auto vramDataSource = DevFSDataSource<units::data::megabyte_t>( |
| 30 | path, [](int fd) { |
| 31 | units::data::megabyte_t size; |
| 32 | Utils::AMD::readAMDGPUVRamSize(fd, &size); |
| 33 | return size; |
| 34 | }); |
| 35 | |
| 36 | if (vramDataSource.read(data)) |
| 37 | return true; |
| 38 | |
| 39 | SPDLOG_WARN("Cannot retrieve device memory size from {}", path.c_str()); |
| 40 | return false; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | class RadeonGPUInfoVRamDataSource |
| 45 | : public IDataSource<units::data::megabyte_t, std::filesystem::path const> |
| 46 | { |
| 47 | public: |
| 48 | std::string source() const override |
| 49 | { |
| 50 | return ""; |
| 51 | } |
| 52 | |
| 53 | bool read(units::data::megabyte_t &data, |
| 54 | std::filesystem::path const &path) override |
| 55 | { |
| 56 | auto vramDataSource = DevFSDataSource<units::data::megabyte_t>( |
| 57 | path, [](int fd) { |
| 58 | units::data::megabyte_t size; |
| 59 | Utils::AMD::readRadeonVRamSize(fd, &size); |
| 60 | return size; |
| 61 | }); |
| 62 | |
| 63 | if (vramDataSource.read(data)) |
| 64 | return true; |
| 65 | |
| 66 | SPDLOG_WARN("Cannot retrieve device memory size from {}", path.c_str()); |
| 67 | return false; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | GPUInfoVRam::GPUInfoVRam( |
| 72 | std::unique_ptr<IDataSource<std::string>> &&kernelVersionDataSource, |
| 73 | std::unique_ptr<IDataSource<std::vector<std::string>, std::filesystem::path const>> |
| 74 | &&driverDataSource, |
| 75 | std::unique_ptr<IDataSource<units::data::megabyte_t, std::filesystem::path const>> |
| 76 | &&radeonDataSource, |
| 77 | std::unique_ptr<IDataSource<units::data::megabyte_t, std::filesystem::path const>> |
| 78 | &&amdgpuDataSource) noexcept |
| 79 | : kernelVersionDataSource_(std::move(kernelVersionDataSource)) |
| 80 | , driverDataSource_(std::move(driverDataSource)) |
| 81 | , radeonDataSource_(std::move(radeonDataSource)) |
| 82 | , amdgpuDataSource_(std::move(amdgpuDataSource)) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | std::vector<std::pair<std::string, std::string>> |
| 87 | GPUInfoVRam::provideInfo(Vendor, int, IGPUInfo::Path const &path, |
| 88 | IHWIDTranslator const &) const |
| 89 | { |
| 90 | std::vector<std::pair<std::string, std::string>> info; |
| 91 | |
| 92 | auto kernel = readKernelVersion(); |
| 93 | auto driver = readDriver(path.sys); |
| 94 | |
| 95 | if ((driver == "radeon" && kernel >= std::make_tuple(2, 6, 31)) || |
| 96 | (driver == "amdgpu" && kernel >= std::make_tuple(4, 10, 0))) { |
| 97 | |
| 98 | units::data::megabyte_t memory; |
| 99 | bool success = false; |
| 100 | |
| 101 | if (driver == "radeon") |
| 102 | success = radeonDataSource_->read(memory, path.dev); |
| 103 | else if (driver == "amdgpu") |
| 104 | success = amdgpuDataSource_->read(memory, path.dev); |
| 105 | else |
| 106 | SPDLOG_WARN("Cannot retrieve vram size: unsupported driver"); |
| 107 | |
| 108 | if (success) |
| 109 | info.emplace_back(IGPUInfo::Keys::memory, |
| 110 | std::format("{} {}", memory.template to<unsigned int>(), |
| 111 | memory.abbreviation())); |
| 112 | } |
| 113 | |
| 114 | return info; |
| 115 | } |
| 116 | |
| 117 | std::tuple<int, int, int> GPUInfoVRam::readKernelVersion() const |
| 118 | { |
| 119 | std::string data; |
| 120 | if (kernelVersionDataSource_->read(data)) { |
| 121 | auto version = |
| 122 | Utils::String::parseKernelProcVersion(data).value_or("0.0.0"); |
| 123 | return Utils::String::parseVersion(version); |
| 124 | } |
| 125 | |
| 126 | return std::make_tuple(0, 0, 0); |
| 127 | } |
| 128 | |
| 129 | std::string GPUInfoVRam::readDriver(std::filesystem::path const &path) const |
| 130 | { |
| 131 | std::string driver; |
| 132 | std::vector<std::string> data; |
| 133 | |
| 134 | if (driverDataSource_->read(data, path)) { |
| 135 | static constexpr std::string_view driverStr{"DRIVER"}; |
| 136 | |
| 137 | for (auto &line : data) { |
| 138 | if (line.find(driverStr) == 0) { |
| 139 | driver = line.substr(driverStr.length() + 1, std::string::npos); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (driver.empty()) |
| 145 | SPDLOG_DEBUG("Cannot retrieve driver"); |
| 146 | } |
| 147 | |
| 148 | return driver; |
| 149 | } |
| 150 | |
| 151 | std::vector<std::string> |
| 152 | GPUInfoVRam::provideCapabilities(Vendor, int, IGPUInfo::Path const &) const |
| 153 | { |
| 154 | return {}; |
| 155 | } |
| 156 | |
| 157 | bool GPUInfoVRam::registered_ = InfoProviderRegistry::add( |
| 158 | std::make_unique<GPUInfoVRam>(std::make_unique<SWInfoKernelDataSource>(), |
| 159 | std::make_unique<GPUInfoUeventDataSource>(), |
| 160 | std::make_unique<RadeonGPUInfoVRamDataSource>(), |
| 161 | std::make_unique<AMDGPUInfoVRamDataSource>())); |