diff --git a/src/backend/opencl/wrappers/AdlLib_linux.cpp b/src/backend/opencl/wrappers/AdlLib_linux.cpp index 5ef0e5e5..7dbb1ea2 100644 --- a/src/backend/opencl/wrappers/AdlLib_linux.cpp +++ b/src/backend/opencl/wrappers/AdlLib_linux.cpp @@ -36,27 +36,29 @@ bool AdlLib::m_ready = false; static const std::string kPrefix = "/sys/bus/pci/drivers/amdgpu/"; -static inline bool sysfs_is_file(const char *path) +static inline bool sysfs_is_file(const std::string &path) { struct stat sb; - return stat(path, &sb) == 0 && ((sb.st_mode & S_IFMT) == S_IFREG); + return stat(path.c_str(), &sb) == 0 && ((sb.st_mode & S_IFMT) == S_IFREG); } -static inline std::string sysfs_prefix(const PciTopology &topology) +static inline bool sysfs_is_amdgpu(const std::string &path) { - std::string path = kPrefix + "0000:" + topology.toString().data() + "/hwmon/hwmon"; - - if (sysfs_is_file((path + "2/name").c_str())) { - return path + "2/"; + if (!sysfs_is_file(path)) { + return false; } - if (sysfs_is_file((path + "3/name").c_str())) { - return path + "3/"; + std::ifstream file(path); + if (!file.is_open()) { + return false; } - return {}; + std::string name; + std::getline(file, name); + + return name == "amdgpu"; } @@ -74,6 +76,21 @@ uint32_t sysfs_read(const std::string &path) } +static inline std::string sysfs_prefix(const PciTopology &topology) +{ + const std::string path = kPrefix + "0000:" + topology.toString().data() + "/hwmon/hwmon"; + + for (uint32_t i = 1; i < 10; ++i) { + const std::string prefix = path + std::to_string(i) + "/"; + if (sysfs_is_amdgpu(prefix + "name") && sysfs_read(prefix + "freq1_input")) { + return prefix; + } + } + + return {}; +} + + } // namespace xmrig