Skip to content

Commit

Permalink
gpu_fdinfo: add sensor fallback for non-PCI GPU
Browse files Browse the repository at this point in the history
Some GPUs do not use PCI as bus (e.g. MSM devices). When current GPU do
not have a pci_dev, search global `/sys/class/hwmon` directory for a
`gpu` sensor.

Signed-off-by: Ettore Chimenti <[email protected]>
  • Loading branch information
ektor5 committed Jan 23, 2025
1 parent e54767b commit 553b01d
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/gpu_fdinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,38 @@ float GPU_fdinfo::get_memory_used()
void GPU_fdinfo::find_hwmon()
{
std::string device = "/sys/bus/pci/devices/" + pci_dev + "/hwmon";
std::string hwmon;

if (!fs::exists(device)) {
SPDLOG_DEBUG("hwmon: hwmon directory {} doesn't exist.", device);
return;
}
SPDLOG_DEBUG("hwmon: searching in /sys/class/hwmon");

std::string sensors = "/sys/class/hwmon/";
for (const auto &entry : fs::directory_iterator(sensors)) {
auto hwmon_dir = entry.path().string();
auto hwmon_name = hwmon_dir + "/name";
std::ifstream name_stream(hwmon_name);
std::string name_content;

if (!name_stream.is_open()) {
continue;
}

std::getline(name_stream, name_content);
name_stream.close();

auto dir_iterator = fs::directory_iterator(device);
auto hwmon = dir_iterator->path().string();
if (name_content.find("gpu") == std::string::npos) {
continue;
}

// take the first gpu sensor
hwmon = hwmon_dir;
break;
}
} else {
auto dir_iterator = fs::directory_iterator(device);
hwmon = dir_iterator->path().string();
}

if (hwmon.empty()) {
SPDLOG_DEBUG("hwmon: hwmon directory is empty.");
Expand Down

0 comments on commit 553b01d

Please sign in to comment.