| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2021 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <catch2/trompeloeil.hpp> |
| 6 | |
| 7 | #include "common/hwidtranslatorstub.h" |
| 8 | #include "core/idatasource.h" |
| 9 | #include "core/info/amd/gpuinfovram.h" |
| 10 | #include <memory> |
| 11 | #include <units.h> |
| 12 | |
| 13 | extern template struct trompeloeil::reporter<trompeloeil::specialized>; |
| 14 | |
| 15 | namespace Tests::GPUInfoVRam { |
| 16 | |
| 17 | class KernelSWInfoDataSourceStub : public IDataSource<std::string> |
| 18 | { |
| 19 | public: |
| 20 | KernelSWInfoDataSourceStub(std::string data = "") |
| 21 | : data_(std::move(data)) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | std::string source() const override |
| 26 | { |
| 27 | return "/proc/version"; |
| 28 | } |
| 29 | |
| 30 | bool read(std::string &data) override |
| 31 | { |
| 32 | data = data_; |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | std::string const data_; |
| 38 | }; |
| 39 | |
| 40 | class GPUInfoDataSourceStub |
| 41 | : public IDataSource<std::vector<std::string>, std::filesystem::path const> |
| 42 | { |
| 43 | public: |
| 44 | GPUInfoDataSourceStub(std::vector<std::string> data) |
| 45 | : data_(std::move(data)) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | std::string source() const override |
| 50 | { |
| 51 | return ""; |
| 52 | } |
| 53 | |
| 54 | bool read(std::vector<std::string> &data, std::filesystem::path const &) override |
| 55 | { |
| 56 | data = data_; |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | std::vector<std::string> const data_; |
| 62 | }; |
| 63 | |
| 64 | class GPUInfoVRamDataSourceStub |
| 65 | : public IDataSource<units::data::megabyte_t, std::filesystem::path const> |
| 66 | { |
| 67 | public: |
| 68 | GPUInfoVRamDataSourceStub(units::data::megabyte_t data, bool success) |
| 69 | : data_(std::move(data)) |
| 70 | , success_(success) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | std::string source() const override |
| 75 | { |
| 76 | return ""; |
| 77 | } |
| 78 | |
| 79 | bool read(units::data::megabyte_t &data, std::filesystem::path const &) override |
| 80 | { |
| 81 | data = data_; |
| 82 | return success_; |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | units::data::megabyte_t const data_; |
| 87 | bool const success_; |
| 88 | }; |
| 89 | |
| 90 | class HWIDTranslatorMock : public IHWIDTranslator |
| 91 | { |
| 92 | public: |
| 93 | MAKE_CONST_MOCK1(vendor, std::string(std::string const &)); |
| 94 | MAKE_CONST_MOCK2(device, std::string(std::string const &, std::string const &)); |
| 95 | MAKE_CONST_MOCK4(subdevice, |
| 96 | std::string(std::string const &, std::string const &, |
| 97 | std::string const &, std::string const &)); |
| 98 | }; |
| 99 | |
| 100 | TEST_CASE("GPUInfoVRam tests", "[Info][GPUInfo][GPUInfoVRam]") |
| 101 | { |
| 102 | Vendor vendor(Vendor::AMD); |
| 103 | int const gpuIndex = 0; |
| 104 | IGPUInfo::Path path("_sys_", "_dev_"); |
| 105 | HWIDTranslatorStub hwIDTranslator; |
| 106 | |
| 107 | SECTION("radeon driver") |
| 108 | { |
| 109 | auto driverDataSource = std::make_unique<GPUInfoDataSourceStub>( |
| 110 | std::vector({std::string("DRIVER=radeon")})); |
| 111 | auto amdgpuVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 112 | units::make_unit<units::data::megabyte_t>(16), true); |
| 113 | |
| 114 | SECTION("Valid vram data source") |
| 115 | { |
| 116 | auto radeonVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 117 | units::make_unit<units::data::megabyte_t>(8), true); |
| 118 | |
| 119 | SECTION("Provides vram size when kernel >= 2.6.31") |
| 120 | { |
| 121 | ::GPUInfoVRam ts(std::make_unique<KernelSWInfoDataSourceStub>( |
| 122 | "Linux version 2.6.31_..."), |
| 123 | std::move(driverDataSource), |
| 124 | std::move(radeonVRamDataSource), |
| 125 | std::move(amdgpuVRamDataSource)); |
| 126 | |
| 127 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 128 | auto memory = std::make_pair(std::string(::IGPUInfo::Keys::memory), |
| 129 | std::string("8 MB")); |
| 130 | |
| 131 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(memory)); |
| 132 | } |
| 133 | |
| 134 | SECTION("Does not provide vram size when kernel < 2.6.31") |
| 135 | { |
| 136 | ::GPUInfoVRam ts(std::make_unique<KernelSWInfoDataSourceStub>( |
| 137 | "Linux version 2.6.30_..."), |
| 138 | std::move(driverDataSource), |
| 139 | std::move(radeonVRamDataSource), |
| 140 | std::move(amdgpuVRamDataSource)); |
| 141 | |
| 142 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 143 | |
| 144 | REQUIRE(output.empty()); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | SECTION( |
| 149 | "Does not provide vram size when the vram data source cannot be read") |
| 150 | { |
| 151 | auto radeonVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 152 | units::make_unit<units::data::megabyte_t>(8), false); |
| 153 | |
| 154 | ::GPUInfoVRam ts(std::make_unique<KernelSWInfoDataSourceStub>( |
| 155 | "Linux version 2.6.31_..."), |
| 156 | std::move(driverDataSource), |
| 157 | std::move(radeonVRamDataSource), |
| 158 | std::move(amdgpuVRamDataSource)); |
| 159 | |
| 160 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 161 | |
| 162 | REQUIRE(output.empty()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | SECTION("amdgpu driver") |
| 167 | { |
| 168 | auto driverDataSource = std::make_unique<GPUInfoDataSourceStub>( |
| 169 | std::vector({std::string("DRIVER=amdgpu")})); |
| 170 | auto radeonVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 171 | units::make_unit<units::data::megabyte_t>(8), true); |
| 172 | |
| 173 | SECTION("Valid vram data source") |
| 174 | { |
| 175 | auto amdgpuVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 176 | units::make_unit<units::data::megabyte_t>(16), true); |
| 177 | |
| 178 | SECTION("Provides vram size when kernel >= 4.10") |
| 179 | { |
| 180 | ::GPUInfoVRam ts(std::make_unique<KernelSWInfoDataSourceStub>( |
| 181 | "Linux version 4.10.0_..."), |
| 182 | std::move(driverDataSource), |
| 183 | std::move(radeonVRamDataSource), |
| 184 | std::move(amdgpuVRamDataSource)); |
| 185 | |
| 186 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 187 | auto memory = std::make_pair(std::string(::IGPUInfo::Keys::memory), |
| 188 | std::string("16 MB")); |
| 189 | |
| 190 | REQUIRE_THAT(output, Catch::Matchers::VectorContains(memory)); |
| 191 | } |
| 192 | |
| 193 | SECTION("Does not provide vram size when kernel < 4.10") |
| 194 | { |
| 195 | ::GPUInfoVRam ts(std::make_unique<KernelSWInfoDataSourceStub>( |
| 196 | "Linux version 4.9.0_..."), |
| 197 | std::move(driverDataSource), |
| 198 | std::move(radeonVRamDataSource), |
| 199 | std::move(amdgpuVRamDataSource)); |
| 200 | |
| 201 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 202 | |
| 203 | REQUIRE(output.empty()); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | SECTION( |
| 208 | "Does not provide vram size when the vram data source cannot be read") |
| 209 | { |
| 210 | auto amdgpuVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 211 | units::make_unit<units::data::megabyte_t>(16), false); |
| 212 | |
| 213 | ::GPUInfoVRam ts(std::make_unique<KernelSWInfoDataSourceStub>( |
| 214 | "Linux version 4.10.0_..."), |
| 215 | std::move(driverDataSource), |
| 216 | std::move(radeonVRamDataSource), |
| 217 | std::move(amdgpuVRamDataSource)); |
| 218 | |
| 219 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 220 | |
| 221 | REQUIRE(output.empty()); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | SECTION("Does not provide vram size when using an unknown driver") |
| 226 | { |
| 227 | auto driverDataSource = std::make_unique<GPUInfoDataSourceStub>( |
| 228 | std::vector({std::string("DRIVER=unknown")})); |
| 229 | auto radeonVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 230 | units::make_unit<units::data::megabyte_t>(8), true); |
| 231 | auto amdgpuVRamDataSource = std::make_unique<GPUInfoVRamDataSourceStub>( |
| 232 | units::make_unit<units::data::megabyte_t>(16), true); |
| 233 | |
| 234 | ::GPUInfoVRam ts(std::make_unique<KernelSWInfoDataSourceStub>( |
| 235 | "Linux version 4.19.0_..."), |
| 236 | std::move(driverDataSource), |
| 237 | std::move(radeonVRamDataSource), |
| 238 | std::move(amdgpuVRamDataSource)); |
| 239 | |
| 240 | auto output = ts.provideInfo(vendor, gpuIndex, path, hwIDTranslator); |
| 241 | |
| 242 | REQUIRE(output.empty()); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | } // namespace Tests::GPUInfoVRam |