| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <filesystem> |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <string_view> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | /// Interface to retrieve info of a physical cpu package. |
| 14 | class ICPUInfo |
| 15 | { |
| 16 | public: |
| 17 | struct Keys |
| 18 | { |
| 19 | static constexpr std::string_view vendorId{"vendorid"}; |
| 20 | static constexpr std::string_view cpuFamily{"cpufamily"}; |
| 21 | static constexpr std::string_view model{"model"}; |
| 22 | static constexpr std::string_view modelName{"modname"}; |
| 23 | static constexpr std::string_view stepping{"stepping"}; |
| 24 | static constexpr std::string_view ucode{"ucodev"}; |
| 25 | static constexpr std::string_view l3Cache{"l3cache"}; |
| 26 | static constexpr std::string_view executionUnits{"exeunits"}; |
| 27 | static constexpr std::string_view cores{"cores"}; |
| 28 | static constexpr std::string_view flags{"flags"}; |
| 29 | static constexpr std::string_view bugs{"bugs"}; |
| 30 | static constexpr std::string_view bogomips{"bogomips"}; |
| 31 | }; |
| 32 | |
| 33 | /// An execution unit is exposed by the system as a single cpu. |
| 34 | struct ExecutionUnit |
| 35 | { |
| 36 | ExecutionUnit(int cpuId, int coreId, std::filesystem::path sysPath) |
| 37 | : cpuId(cpuId) |
| 38 | , coreId(coreId) |
| 39 | , sysPath(std::move(sysPath)) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | /// cpu id exposed by the system |
| 44 | int const cpuId; |
| 45 | |
| 46 | /// core id on the physical cpu package in which the cpu resides |
| 47 | int const coreId; |
| 48 | |
| 49 | /// sysfs path |
| 50 | std::filesystem::path const sysPath; |
| 51 | }; |
| 52 | |
| 53 | class IProvider |
| 54 | { |
| 55 | public: |
| 56 | virtual std::vector<std::pair<std::string, std::string>> |
| 57 | provideInfo(int physicalId, |
| 58 | std::vector<ICPUInfo::ExecutionUnit> const &executionUnits) = 0; |
| 59 | |
| 60 | virtual std::vector<std::string> provideCapabilities( |
| 61 | int physicalId, |
| 62 | std::vector<ICPUInfo::ExecutionUnit> const &executionUnits) = 0; |
| 63 | |
| 64 | virtual ~IProvider() = default; |
| 65 | }; |
| 66 | |
| 67 | class IProviderRegistry |
| 68 | { |
| 69 | public: |
| 70 | virtual std::vector<std::unique_ptr<ICPUInfo::IProvider>> const & |
| 71 | cpuInfoProviders() const = 0; |
| 72 | |
| 73 | virtual ~IProviderRegistry() = default; |
| 74 | }; |
| 75 | |
| 76 | /// @return physical id |
| 77 | virtual int physicalId() const = 0; |
| 78 | |
| 79 | /// @return available execution units |
| 80 | virtual std::vector<ICPUInfo::ExecutionUnit> const &executionUnits() const = 0; |
| 81 | |
| 82 | /// @returns collection of keys for the stored gpu information |
| 83 | virtual std::vector<std::string> keys() const = 0; |
| 84 | |
| 85 | /// @returns string with gpu info or empty string when no info |
| 86 | /// is mapped to the key |
| 87 | virtual std::string info(std::string_view key) const = 0; |
| 88 | |
| 89 | /// @returns true when a capability is present |
| 90 | virtual bool hasCapability(std::string_view name) const = 0; |
| 91 | |
| 92 | /// Initialize cpu info |
| 93 | /// @param infoProviders information providers |
| 94 | virtual void initialize( |
| 95 | std::vector<std::unique_ptr<ICPUInfo::IProvider>> const &infoProviders) = 0; |
| 96 | |
| 97 | virtual ~ICPUInfo() = default; |
| 98 | }; |