Debian Codegraph @

src/core/info/common/gpuinfouevent.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include "gpuinfouevent.h"
5
6 #include "../ihwidtranslator.h"
7 #include "../infoproviderregistry.h"
8 #include "gpuinfoueventdatasource.h"
9 #include <algorithm>
10 #include <cctype>
11 #include <spdlog/spdlog.h>
12 #include <string_view>
13 #include <utility>
14
15 GPUInfoUevent::GPUInfoUevent(
16 std::unique_ptr<IDataSource<std::vector<std::string>, std::filesystem::path const>>
17 &&dataSource) noexcept
18 : dataSource_(std::move(dataSource))
19 {
20 }
21
22 std::vector<std::pair<std::string, std::string>>
23 GPUInfoUevent::provideInfo(Vendor, int, IGPUInfo::Path const &path,
24 IHWIDTranslator const &hwIDTranslator) const
25 {
26 std::vector<std::pair<std::string, std::string>> info;
27
28 static constexpr std::string_view driverStr{"DRIVER"};
29 static constexpr std::string_view pciIdStr{"PCI_ID"};
30 static constexpr std::string_view pciSubsysIdStr{"PCI_SUBSYS_ID"};
31 static constexpr std::string_view pciSlotStr{"PCI_SLOT_NAME"};
32
33 std::vector<std::string> data;
34 if (dataSource_->read(data, path.sys)) {
35 std::string vendorId;
36 std::string deviceId;
37 std::string subvendorId;
38 std::string subdeviceId;
39 std::string pciSlot;
40 std::string driver;
41
42 for (auto const &line : data) {
43 if (line.find(driverStr) == 0) {
44 driver = line.substr(driverStr.length() + 1, std::string::npos);
45 }
46 else if (line.find(pciIdStr) == 0) {
47 auto colonPos = line.find(':', pciIdStr.length() + 1);
48 vendorId = line.substr(pciIdStr.length() + 1,
49 colonPos - pciIdStr.length() - 1);
50 deviceId = line.substr(colonPos + 1, std::string::npos);
51 }
52 else if (line.find(pciSubsysIdStr) == 0) {
53 auto colonPos = line.find(':', pciSubsysIdStr.length() + 1);
54 subvendorId = line.substr(pciSubsysIdStr.length() + 1,
55 colonPos - pciSubsysIdStr.length() - 1);
56 subdeviceId = line.substr(colonPos + 1, std::string::npos);
57 }
58 else if (line.find(pciSlotStr) == 0) {
59 pciSlot = line.substr(pciSlotStr.length() + 1, std::string::npos);
60 }
61 }
62
63 if (vendorId.empty())
64 SPDLOG_DEBUG("Cannot retrieve vendor ID");
65 if (deviceId.empty())
66 SPDLOG_DEBUG("Cannot retrieve device ID");
67 if (subvendorId.empty())
68 SPDLOG_DEBUG("Cannot retrieve subvendor ID");
69 if (subdeviceId.empty())
70 SPDLOG_DEBUG("Cannot retrieve subdevice ID");
71 if (pciSlot.empty())
72 SPDLOG_DEBUG("Cannot retrieve pci slot");
73 if (driver.empty())
74 SPDLOG_DEBUG("Cannot retrieve driver");
75
76 // ensure that all ids are in uppercase
77 std::transform(vendorId.cbegin(), vendorId.cend(), vendorId.begin(),
78 ::toupper);
79 std::transform(deviceId.cbegin(), deviceId.cend(), deviceId.begin(),
80 ::toupper);
81 std::transform(subvendorId.cbegin(), subvendorId.cend(),
82 subvendorId.begin(), ::toupper);
83 std::transform(subdeviceId.cbegin(), subdeviceId.cend(),
84 subdeviceId.begin(), ::toupper);
85
86 // populate info
87 auto vendorName = hwIDTranslator.vendor(vendorId);
88 if (!vendorName.empty())
89 info.emplace_back(IGPUInfo::Keys::vendorName, std::move(vendorName));
90
91 auto deviceName = hwIDTranslator.device(vendorId, deviceId);
92 if (!deviceName.empty())
93 info.emplace_back(IGPUInfo::Keys::deviceName, std::move(deviceName));
94
95 auto subdeviceName = hwIDTranslator.subdevice(vendorId, deviceId,
96 subvendorId, subdeviceId);
97 if (!subdeviceName.empty())
98 info.emplace_back(IGPUInfo::Keys::subdeviceName, std::move(subdeviceName));
99
100 info.emplace_back(IGPUInfo::Keys::driver, std::move(driver));
101 info.emplace_back(IGPUInfo::Keys::pciSlot, std::move(pciSlot));
102 info.emplace_back(IGPUInfo::Keys::vendorID, std::move(vendorId));
103 info.emplace_back(IGPUInfo::Keys::deviceID, std::move(deviceId));
104 info.emplace_back(IGPUInfo::Keys::subvendorID, std::move(subvendorId));
105 info.emplace_back(IGPUInfo::Keys::subdeviceID, std::move(subdeviceId));
106 }
107
108 return info;
109 }
110
111 std::vector<std::string>
112 GPUInfoUevent::provideCapabilities(Vendor, int, IGPUInfo::Path const &) const
113 {
114 return {};
115 }
116
117 bool GPUInfoUevent::registered_ = InfoProviderRegistry::add(
118 std::make_unique<GPUInfoUevent>(std::make_unique<GPUInfoUeventDataSource>()));