feat: dynamic loading of libdrm and libdrm_amdgpu (#438)

* feat: dynamic loading of libdrm and libdrm_amdgpu

* fix: libdrm_amdgpu_sys requires rustc 1.80.0 for exclusive_range_pattern

* feat: update libdrm_amdgpu_sys to 0.8.1

* fix: remove Rc wrapper for LibDrmAmdgpu

* pkg: bump rust version used to build packages

---------

Co-authored-by: Ilya Zlobintsev <ilya.zl@protonmail.com>
This commit is contained in:
Umio Yasuno
2025-01-04 22:04:07 +09:00
committed by GitHub
parent 1d44df1652
commit 3afbcb2221
8 changed files with 42 additions and 14 deletions

9
Cargo.lock generated
View File

@@ -1620,11 +1620,12 @@ checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398"
[[package]]
name = "libdrm_amdgpu_sys"
version = "0.7.5"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2291cc7b4cf0006ebe83f0c7f1eb6d4aea1fc2601be08ada31e4f34e4b61d099"
checksum = "239a084aa81eb01317fe32f41a9ba7d284acf13f079523e3b9406339f4ba7c0c"
dependencies = [
"libc",
"libloading 0.8.5",
]
[[package]]
@@ -1668,7 +1669,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
dependencies = [
"cfg-if",
"windows-targets 0.48.5",
"windows-targets 0.52.6",
]
[[package]]
@@ -3057,7 +3058,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [
"windows-sys 0.48.0",
"windows-sys 0.59.0",
]
[[package]]

View File

@@ -37,7 +37,7 @@ pciid-parser = { version = "0.7", features = ["serde"] }
serde_yaml = "0.9"
vulkano = { version = "0.34.1", default-features = false }
zbus = { version = "4.1.2", default-features = false, features = ["tokio"] }
libdrm_amdgpu_sys = "0.7.3"
libdrm_amdgpu_sys = { version = "0.8.1", default-features = false, features = ["dynamic_loading"] }
tar = "0.4.40"
libflate = "2.0.0"
os-release = "0.1.0"

View File

@@ -20,6 +20,7 @@ use lact_schema::{
ClocksInfo, ClockspeedStats, DeviceInfo, DeviceStats, DrmInfo, FanStats, GpuPciInfo, LinkInfo,
PciInfo, PmfwInfo, PowerState, PowerStates, PowerStats, VoltageStats, VramStats,
};
use libdrm_amdgpu_sys::LibDrmAmdgpu;
use libdrm_amdgpu_sys::AMDGPU::{ThrottleStatus, ThrottlerBit};
use pciid_parser::Database;
use std::{
@@ -61,13 +62,17 @@ impl AmdGpuController {
sysfs_path: PathBuf,
pci_db: &Database,
skip_drm: bool,
libdrm_amdgpu: Option<LibDrmAmdgpu>,
) -> anyhow::Result<Self> {
let handle = GpuHandle::new_from_path(sysfs_path)
.map_err(|error| anyhow!("failed to initialize gpu handle: {error}"))?;
let mut drm_handle = None;
if matches!(handle.get_driver(), "amdgpu" | "radeon") && !skip_drm {
match get_drm_handle(&handle) {
if matches!(handle.get_driver(), "amdgpu" | "radeon")
&& !skip_drm
&& libdrm_amdgpu.is_some()
{
match get_drm_handle(&handle, libdrm_amdgpu.as_ref().unwrap()) {
Ok(handle) => {
drm_handle = Some(handle);
}
@@ -1032,7 +1037,7 @@ impl GpuController for AmdGpuController {
}
}
fn get_drm_handle(handle: &GpuHandle) -> anyhow::Result<DrmHandle> {
fn get_drm_handle(handle: &GpuHandle, libdrm_amdgpu: &LibDrmAmdgpu) -> anyhow::Result<DrmHandle> {
let slot_name = handle
.get_pci_slot_name()
.context("Device has no PCI slot name")?;
@@ -1042,7 +1047,8 @@ fn get_drm_handle(handle: &GpuHandle) -> anyhow::Result<DrmHandle> {
.write(true)
.open(&path)
.with_context(|| format!("Could not open drm file at {path}"))?;
let (handle, _, _) = DrmHandle::init(drm_file.into_raw_fd())
let (handle, _, _) = libdrm_amdgpu
.init_device_handle(drm_file.into_raw_fd())
.map_err(|err| anyhow!("Could not open drm handle, error code {err}"))?;
Ok(handle)
}

View File

@@ -21,6 +21,7 @@ use lact_schema::{
ClocksInfo, DeviceInfo, DeviceListEntry, DeviceStats, FanControlMode, FanOptions, PmfwOptions,
PowerStates, ProfileRule, ProfileWatcherState, ProfilesInfo,
};
use libdrm_amdgpu_sys::LibDrmAmdgpu;
use libflate::gzip;
use nix::libc;
use nvml_wrapper::{error::NvmlError, Nvml};
@@ -932,6 +933,21 @@ fn load_controllers(
}
});
let libdrm_amdgpu = if sysfs_only {
None
} else {
match LibDrmAmdgpu::new() {
Ok(libdrm_amdgpu) => {
info!("libdrm and libdrm_amdgpu initialized");
Some(libdrm_amdgpu)
}
Err(err) => {
info!("AMDGPU support disabled, {err}");
None
}
}
};
let nvml = if sysfs_only {
None
} else {
@@ -960,7 +976,12 @@ fn load_controllers(
if name.starts_with("card") && !name.contains('-') {
trace!("trying gpu controller at {:?}", entry.path());
let device_path = entry.path().join("device");
match AmdGpuController::new_from_path(device_path, &pci_db, sysfs_only) {
match AmdGpuController::new_from_path(
device_path,
&pci_db,
sysfs_only,
libdrm_amdgpu.clone(),
) {
Ok(controller) => match controller.get_id() {
Ok(id) => {
let path = controller.get_path();

View File

@@ -28,7 +28,7 @@ env:
configure:
steps:
- cmd: curl -o /tmp/install_rust.sh $RUSTUP_URL
- cmd: sh /tmp/install_rust.sh -y --default-toolchain 1.78
- cmd: sh /tmp/install_rust.sh -y --default-toolchain 1.80
- cmd: pacman -Syu --noconfirm
pkg: true
build:

View File

@@ -26,7 +26,7 @@ env:
configure:
steps:
- cmd: curl -o /tmp/install_rust.sh $RUSTUP_URL
- cmd: sh /tmp/install_rust.sh -y --default-toolchain 1.78
- cmd: sh /tmp/install_rust.sh -y --default-toolchain 1.80
- cmd: >-
curl -o /tmp/blueprint-compiler.deb http://de.archive.ubuntu.com/ubuntu/pool/universe/b/blueprint-compiler/blueprint-compiler_0.14.0-1_all.deb &&
apt install -y /tmp/blueprint-compiler.deb

View File

@@ -28,7 +28,7 @@ env:
configure:
steps:
- cmd: curl -o /tmp/install_rust.sh $RUSTUP_URL
- cmd: sh /tmp/install_rust.sh -y --default-toolchain 1.78
- cmd: sh /tmp/install_rust.sh -y --default-toolchain 1.80
- cmd: >-
curl -o /tmp/blueprint-compiler.deb http://de.archive.ubuntu.com/ubuntu/pool/universe/b/blueprint-compiler/blueprint-compiler_0.14.0-1_all.deb &&
apt install -y /tmp/blueprint-compiler.deb

View File

@@ -1,2 +1,2 @@
[toolchain]
channel = "1.78.0"
channel = "1.80.0"