| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "sysmodelfactory.h" |
| 5 | |
| 6 | #include "common/fileutils.h" |
| 7 | #include "common/stringutils.h" |
| 8 | #include "components/controls/icontrol.h" |
| 9 | #include "components/controls/icpucontrolprovider.h" |
| 10 | #include "components/controls/igpucontrolprovider.h" |
| 11 | #include "components/cpu.h" |
| 12 | #include "components/cpuutils.h" |
| 13 | #include "components/gpu.h" |
| 14 | #include "components/sensors/icpusensorprovider.h" |
| 15 | #include "components/sensors/igpusensorprovider.h" |
| 16 | #include "components/sensors/isensor.h" |
| 17 | #include "info/cpuinfo.h" |
| 18 | #include "info/gpuinfo.h" |
| 19 | #include "info/ihwidtranslator.h" |
| 20 | #include "info/iswinfo.h" |
| 21 | #include "isyscomponent.h" |
| 22 | #include "isysexplorer.h" |
| 23 | #include "sysmodel.h" |
| 24 | #include <algorithm> |
| 25 | #include <iterator> |
| 26 | #include <memory> |
| 27 | #include <optional> |
| 28 | #include <spdlog/spdlog.h> |
| 29 | #include <string_view> |
| 30 | #include <utility> |
| 31 | |
| 32 | SysModelFactory::SysModelFactory( |
| 33 | std::unique_ptr<ISWInfo> &&swInfo, |
| 34 | std::unique_ptr<ISysExplorer> &&sysExplorer, |
| 35 | std::unique_ptr<IHWIDTranslator> &&hwIDTranslator, |
| 36 | std::unique_ptr<ICPUControlProvider> &&cpuControlProvider, |
| 37 | std::unique_ptr<ICPUSensorProvider> &&cpuSensorProvider, |
| 38 | std::unique_ptr<IGPUControlProvider> &&gpuControlProvider, |
| 39 | std::unique_ptr<IGPUSensorProvider> &&gpuSensorProvider, |
| 40 | ICPUInfo::IProviderRegistry const &cpuInfoProviderRegistry, |
| 41 | IGPUInfo::IProviderRegistry const &gpuInfoProviderRegistry) noexcept |
| 42 | : swInfo_(std::move(swInfo)) |
| 43 | , sysExplorer_(std::move(sysExplorer)) |
| 44 | , hwidTranslator_(std::move(hwIDTranslator)) |
| 45 | , cpuControlProvider_(std::move(cpuControlProvider)) |
| 46 | , cpuSensorProvider_(std::move(cpuSensorProvider)) |
| 47 | , gpuControlProvider_(std::move(gpuControlProvider)) |
| 48 | , gpuSensorProvider_(std::move(gpuSensorProvider)) |
| 49 | , cpuInfoProviderRegistry_(cpuInfoProviderRegistry) |
| 50 | , gpuInfoProviderRegistry_(gpuInfoProviderRegistry) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<ISysModel> SysModelFactory::build() const |
| 55 | { |
| 56 | std::vector<std::unique_ptr<ISysComponent>> components; |
| 57 | |
| 58 | auto gpuInfo = createGPUInfo(); |
| 59 | for (auto &info : gpuInfo) |
| 60 | components.emplace_back(createGPU(std::move(info), *swInfo_)); |
| 61 | |
| 62 | auto cpuInfo = createCPUInfo(); |
| 63 | for (auto &info : cpuInfo) |
| 64 | components.emplace_back(createCPU(std::move(info), *swInfo_)); |
| 65 | |
| 66 | return std::make_unique<SysModel>(swInfo_, std::move(components)); |
| 67 | } |
| 68 | |
| 69 | std::vector<std::unique_ptr<ICPUInfo>> SysModelFactory::createCPUInfo() const |
| 70 | { |
| 71 | auto cpuInfo = parseCPUInfo(); |
| 72 | for (auto &info : cpuInfo) |
| 73 | info->initialize(cpuInfoProviderRegistry_.cpuInfoProviders()); |
| 74 | |
| 75 | return cpuInfo; |
| 76 | } |
| 77 | |
| 78 | std::unique_ptr<ICPU> |
| 79 | SysModelFactory::createCPU(std::unique_ptr<ICPUInfo> &&cpuInfo, |
| 80 | ISWInfo const &swInfo) const |
| 81 | { |
| 82 | // create CPU controls |
| 83 | std::vector<std::unique_ptr<IControl>> controls; |
| 84 | auto &cpuControlProviders = cpuControlProvider_->cpuControlProviders(); |
| 85 | for (auto const &provider : cpuControlProviders) { |
| 86 | auto newControls = provider->provideCPUControls(*cpuInfo, swInfo); |
| 87 | controls.insert(controls.end(), std::make_move_iterator(newControls.begin()), |
| 88 | std::make_move_iterator(newControls.end())); |
| 89 | } |
| 90 | |
| 91 | // create CPU sensors |
| 92 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 93 | auto &cpuSensorProviders = cpuSensorProvider_->cpuSensorProviders(); |
| 94 | for (auto const &provider : cpuSensorProviders) { |
| 95 | auto newSensors = provider->provideCPUSensors(*cpuInfo, swInfo); |
| 96 | sensors.insert(sensors.end(), std::make_move_iterator(newSensors.begin()), |
| 97 | std::make_move_iterator(newSensors.end())); |
| 98 | } |
| 99 | |
| 100 | return std::make_unique<CPU>(std::move(cpuInfo), std::move(controls), |
| 101 | std::move(sensors)); |
| 102 | } |
| 103 | |
| 104 | std::vector<std::unique_ptr<IGPUInfo>> SysModelFactory::createGPUInfo() const |
| 105 | { |
| 106 | namespace fs = std::filesystem; |
| 107 | std::vector<std::unique_ptr<IGPUInfo>> gpuInfo; |
| 108 | |
| 109 | fs::path sysBasePath{"/sys/class/drm"}; |
| 110 | auto deviceNames = sysExplorer_->renderers(); |
| 111 | for (auto const &deviceName : deviceNames) { |
| 112 | |
| 113 | auto sysPath = sysBasePath / deviceName / "device"; |
| 114 | auto devPath = fs::path("/dev/dri").append(deviceName); |
| 115 | auto vendor = parseVendor(sysBasePath / deviceName / "device/vendor"); |
| 116 | int const gpuIndex = computeGPUIndex(deviceName); |
| 117 | |
| 118 | auto info = std::make_unique<GPUInfo>(vendor, gpuIndex, |
| 119 | GPUInfo::Path(sysPath, devPath)); |
| 120 | info->initialize(gpuInfoProviderRegistry_.gpuInfoProviders(), |
| 121 | *hwidTranslator_); |
| 122 | gpuInfo.emplace_back(std::move(info)); |
| 123 | } |
| 124 | |
| 125 | return gpuInfo; |
| 126 | } |
| 127 | |
| 128 | std::unique_ptr<IGPU> |
| 129 | SysModelFactory::createGPU(std::unique_ptr<IGPUInfo> &&gpuInfo, |
| 130 | ISWInfo const &swInfo) const |
| 131 | { |
| 132 | // create GPU controls |
| 133 | std::vector<std::unique_ptr<IControl>> controls; |
| 134 | auto &gpuControlProviders = gpuControlProvider_->gpuControlProviders(); |
| 135 | for (auto const &provider : gpuControlProviders) { |
| 136 | auto newControls = provider->provideGPUControls(*gpuInfo, swInfo); |
| 137 | controls.insert(controls.end(), std::make_move_iterator(newControls.begin()), |
| 138 | std::make_move_iterator(newControls.end())); |
| 139 | } |
| 140 | |
| 141 | // create GPU sensors |
| 142 | std::vector<std::unique_ptr<ISensor>> sensors; |
| 143 | auto &gpuSensorProviders = gpuSensorProvider_->gpuSensorProviders(); |
| 144 | for (auto const &provider : gpuSensorProviders) { |
| 145 | auto newSensors = provider->provideGPUSensors(*gpuInfo, swInfo); |
| 146 | sensors.insert(sensors.end(), std::make_move_iterator(newSensors.begin()), |
| 147 | std::make_move_iterator(newSensors.end())); |
| 148 | } |
| 149 | |
| 150 | return std::make_unique<GPU>(std::move(gpuInfo), std::move(controls), |
| 151 | std::move(sensors)); |
| 152 | } |
| 153 | |
| 154 | std::vector<std::unique_ptr<ICPUInfo>> SysModelFactory::parseCPUInfo() const |
| 155 | { |
| 156 | static constexpr std::string_view cpuIdStr{"processor"}; |
| 157 | |
| 158 | std::filesystem::path const basePath{"/sys/devices/system/cpu"}; |
| 159 | std::vector<std::unique_ptr<ICPUInfo>> cpuInfo; |
| 160 | |
| 161 | int value; |
| 162 | std::optional<int> cpuId; |
| 163 | |
| 164 | auto cpuInfoLines = Utils::File::readFileLines("/proc/cpuinfo"); |
| 165 | for (auto const &line : cpuInfoLines) { |
| 166 | |
| 167 | if (line.empty()) { // push collected info |
| 168 | std::optional<int> physicalId; |
| 169 | std::optional<int> coreId; |
| 170 | |
| 171 | auto physicalIdStr = Utils::CPU::parseProcCpuInfo(cpuInfoLines, *cpuId, |
| 172 | "physical id"); |
| 173 | if (physicalIdStr.has_value() && |
| 174 | Utils::String::toNumber<int>(value, *physicalIdStr)) |
| 175 | physicalId = value; |
| 176 | |
| 177 | auto coreIdStr = Utils::CPU::parseProcCpuInfo(cpuInfoLines, *cpuId, |
| 178 | "core id"); |
| 179 | if (coreIdStr.has_value() && |
| 180 | Utils::String::toNumber<int>(value, *coreIdStr)) |
| 181 | coreId = value; |
| 182 | |
| 183 | if (cpuId.has_value() && physicalId.has_value() && coreId.has_value()) { |
| 184 | std::string cpuDir{"cpu"}; |
| 185 | cpuDir.append(std::to_string(*cpuId)); |
| 186 | auto executionUnitPath = basePath / cpuDir; |
| 187 | |
| 188 | auto infoIt = std::find_if(cpuInfo.cbegin(), cpuInfo.cend(), |
| 189 | [=](auto const &info) { |
| 190 | return info->physicalId() == *physicalId; |
| 191 | }); |
| 192 | if (infoIt != cpuInfo.cend()) |
| 193 | static_cast<CPUInfo *>(infoIt->get()) |
| 194 | ->addExecutionUnit( |
| 195 | ICPUInfo::ExecutionUnit(*cpuId, *coreId, executionUnitPath)); |
| 196 | else { |
| 197 | std::vector<ICPUInfo::ExecutionUnit> units{ |
| 198 | ICPUInfo::ExecutionUnit(*cpuId, *coreId, executionUnitPath)}; |
| 199 | |
| 200 | cpuInfo.emplace_back( |
| 201 | std::make_unique<CPUInfo>(*physicalId, std::move(units))); |
| 202 | } |
| 203 | |
| 204 | physicalId = cpuId = coreId = std::nullopt; |
| 205 | } |
| 206 | else { |
| 207 | SPDLOG_DEBUG("Cannot parse some data from /proc/cpuinfo"); |
| 208 | return {}; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | auto cpuIdIt = line.find(cpuIdStr); |
| 213 | if (cpuIdIt != std::string::npos) { |
| 214 | auto indexPos = line.find_first_not_of("\t: ", cpuIdStr.size()); |
| 215 | if (indexPos != std::string::npos && |
| 216 | Utils::String::toNumber<int>(value, line.substr(indexPos))) { |
| 217 | cpuId = value; |
| 218 | continue; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return cpuInfo; |
| 224 | } |
| 225 | |
| 226 | int SysModelFactory::computeGPUIndex(std::string const &deviceRenderDName) const |
| 227 | { |
| 228 | auto const indexStr = Utils::String::cleanPrefix(deviceRenderDName, |
| 229 | "renderD"); |
| 230 | int index = -1; |
| 231 | if (Utils::String::toNumber<int>(index, indexStr)) |
| 232 | index -= 128; |
| 233 | else |
| 234 | SPDLOG_DEBUG("Cannot compute GPU index for device {}.", deviceRenderDName); |
| 235 | return index; |
| 236 | } |
| 237 | |
| 238 | Vendor SysModelFactory::parseVendor(std::filesystem::path const &vendorPath) const |
| 239 | { |
| 240 | Vendor vendor{-1}; |
| 241 | |
| 242 | auto const lines = Utils::File::readFileLines(vendorPath); |
| 243 | if (!lines.empty()) { |
| 244 | int dataValue; |
| 245 | if (Utils::String::toNumber<int>(dataValue, lines.front(), 16)) |
| 246 | vendor = Vendor{dataValue}; |
| 247 | else |
| 248 | SPDLOG_DEBUG("Cannot parse vendor id from file {}.", vendorPath.c_str()); |
| 249 | } |
| 250 | |
| 251 | return vendor; |
| 252 | } |