// SPDX-License-Identifier: GPL-3.0-or-later // Copyright 2021 Juan Palacios #include "gpuinfovram.h" #include "../common/gpuinfoueventdatasource.h" #include "../common/swinfokerneldatasource.h" #include "common/stringutils.h" #include "core/components/amdutils.h" #include "core/devfsdatasource.h" #include "core/info/infoproviderregistry.h" #include #include #include #include class AMDGPUInfoVRamDataSource : public IDataSource { public: std::string source() const override { return ""; } bool read(units::data::megabyte_t &data, std::filesystem::path const &path) override { auto vramDataSource = DevFSDataSource( path, [](int fd) { units::data::megabyte_t size; Utils::AMD::readAMDGPUVRamSize(fd, &size); return size; }); if (vramDataSource.read(data)) return true; SPDLOG_WARN("Cannot retrieve device memory size from {}", path.c_str()); return false; } }; class RadeonGPUInfoVRamDataSource : public IDataSource { public: std::string source() const override { return ""; } bool read(units::data::megabyte_t &data, std::filesystem::path const &path) override { auto vramDataSource = DevFSDataSource( path, [](int fd) { units::data::megabyte_t size; Utils::AMD::readRadeonVRamSize(fd, &size); return size; }); if (vramDataSource.read(data)) return true; SPDLOG_WARN("Cannot retrieve device memory size from {}", path.c_str()); return false; } }; GPUInfoVRam::GPUInfoVRam( std::unique_ptr> &&kernelVersionDataSource, std::unique_ptr, std::filesystem::path const>> &&driverDataSource, std::unique_ptr> &&radeonDataSource, std::unique_ptr> &&amdgpuDataSource) noexcept : kernelVersionDataSource_(std::move(kernelVersionDataSource)) , driverDataSource_(std::move(driverDataSource)) , radeonDataSource_(std::move(radeonDataSource)) , amdgpuDataSource_(std::move(amdgpuDataSource)) { } std::vector> GPUInfoVRam::provideInfo(Vendor, int, IGPUInfo::Path const &path, IHWIDTranslator const &) const { std::vector> info; auto kernel = readKernelVersion(); auto driver = readDriver(path.sys); if ((driver == "radeon" && kernel >= std::make_tuple(2, 6, 31)) || (driver == "amdgpu" && kernel >= std::make_tuple(4, 10, 0))) { units::data::megabyte_t memory; bool success = false; if (driver == "radeon") success = radeonDataSource_->read(memory, path.dev); else if (driver == "amdgpu") success = amdgpuDataSource_->read(memory, path.dev); else SPDLOG_WARN("Cannot retrieve vram size: unsupported driver"); if (success) info.emplace_back(IGPUInfo::Keys::memory, std::format("{} {}", memory.template to(), memory.abbreviation())); } return info; } std::tuple GPUInfoVRam::readKernelVersion() const { std::string data; if (kernelVersionDataSource_->read(data)) { auto version = Utils::String::parseKernelProcVersion(data).value_or("0.0.0"); return Utils::String::parseVersion(version); } return std::make_tuple(0, 0, 0); } std::string GPUInfoVRam::readDriver(std::filesystem::path const &path) const { std::string driver; std::vector data; if (driverDataSource_->read(data, path)) { static constexpr std::string_view driverStr{"DRIVER"}; for (auto &line : data) { if (line.find(driverStr) == 0) { driver = line.substr(driverStr.length() + 1, std::string::npos); break; } } if (driver.empty()) SPDLOG_DEBUG("Cannot retrieve driver"); } return driver; } std::vector GPUInfoVRam::provideCapabilities(Vendor, int, IGPUInfo::Path const &) const { return {}; } bool GPUInfoVRam::registered_ = InfoProviderRegistry::add( std::make_unique(std::make_unique(), std::make_unique(), std::make_unique(), std::make_unique()));