Skip to content

Commit

Permalink
amd_gpu: better error message on device not found (#2035)
Browse files Browse the repository at this point in the history
  • Loading branch information
heitorPB authored Apr 4, 2024
1 parent 77fb3c0 commit 5de8e03
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/blocks/amd_gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
};

let device = match &config.device {
Some(name) => Device::new(name),
Some(name) => Device::new(name)?,
None => Device::default_card()
.await
.error("failed to get default GPU")?
Expand Down Expand Up @@ -121,9 +121,13 @@ struct GpuInfo {
}

impl Device {
fn new(name: &str) -> Self {
Self {
path: PathBuf::from(format!("/sys/class/drm/{name}/device")),
fn new(name: &str) -> Result<Self, Error> {
let path = PathBuf::from(format!("/sys/class/drm/{name}/device"));

if !path.exists() {
Err(Error::new(format!("Device {name} not found")))
} else {
Ok(Self { path })
}
}

Expand Down Expand Up @@ -176,3 +180,14 @@ impl Device {
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_non_existing_gpu_device() {
let device = Device::new("/nope");
assert!(device.is_err());
}
}

0 comments on commit 5de8e03

Please sign in to comment.