Better IO errors

This commit is contained in:
Adrian Woźniak 2021-12-08 15:31:18 +01:00
parent 24ccf0d59f
commit 0d6f3059a4
No known key found for this signature in database
GPG Key ID: 0012845A89C7352B
6 changed files with 65 additions and 17 deletions

View File

@ -3,8 +3,8 @@ use gumdrop::Options;
use amdgpu::hw_mon::HwMon;
use amdgpu::utils::{linear_map, load_temp_inputs};
use amdgpu::{
TempInput, PULSE_WIDTH_MODULATION_AUTO, PULSE_WIDTH_MODULATION_MAX, PULSE_WIDTH_MODULATION_MIN,
PULSE_WIDTH_MODULATION_MODE,
utils, TempInput, PULSE_WIDTH_MODULATION_AUTO, PULSE_WIDTH_MODULATION_MAX,
PULSE_WIDTH_MODULATION_MIN, PULSE_WIDTH_MODULATION_MODE,
};
use amdgpu_config::fan::Config;
@ -37,11 +37,14 @@ pub enum FanError {
#[error("Failed to read AMD GPU temperatures from tempX_input. No input was found")]
EmptyTempSet,
#[error("Unable to change fan speed to manual mode. {0}")]
ManualSpeedFailed(std::io::Error),
ManualSpeedFailed(utils::AmdGpuError),
#[error("Unable to change fan speed to automatic mode. {0}")]
AutomaticSpeedFailed(std::io::Error),
AutomaticSpeedFailed(utils::AmdGpuError),
#[error("Unable to change AMD GPU modulation (a.k.a. speed) to {value}. {error}")]
FailedToChangeSpeed { value: u64, error: std::io::Error },
FailedToChangeSpeed {
value: u64,
error: utils::AmdGpuError,
},
}
pub struct Fan {

View File

@ -1,4 +1,4 @@
use amdgpu::AmdGpuError;
use amdgpu::{utils, AmdGpuError};
use amdgpu_config::fan::ConfigError;
use crate::command::FanError;
@ -19,4 +19,6 @@ pub enum AmdFanError {
Config(#[from] ConfigError),
#[error("{0:}")]
Io(#[from] std::io::Error),
#[error("{0:}")]
AmdUtils(#[from] utils::AmdGpuError),
}

View File

@ -1,4 +1,4 @@
use crate::{AmdGpuError, Card, ROOT_DIR};
use crate::{utils, AmdGpuError, Card, ROOT_DIR};
#[derive(Debug)]
pub struct HwMonName(pub String);
@ -30,7 +30,7 @@ impl HwMon {
}
#[inline]
pub fn name(&self) -> std::io::Result<String> {
pub fn name(&self) -> utils::Result<String> {
self.hw_mon_read("name")
}
@ -70,21 +70,21 @@ impl HwMon {
.unwrap_or(fallback)
}
pub fn hw_mon_read(&self, name: &str) -> std::io::Result<String> {
std::fs::read_to_string(self.mon_file_path(name)).map(|s| String::from(s.trim()))
pub fn hw_mon_read(&self, name: &str) -> utils::Result<String> {
utils::read_to_string(self.mon_file_path(name)).map(|s| String::from(s.trim()))
}
pub fn device_read(&self, name: &str) -> std::io::Result<String> {
std::fs::read_to_string(self.device_dir().join(name)).map(|s| String::from(s.trim()))
pub fn device_read(&self, name: &str) -> utils::Result<String> {
utils::read_to_string(self.device_dir().join(name)).map(|s| String::from(s.trim()))
}
pub fn hw_mon_write(&self, name: &str, value: u64) -> std::io::Result<()> {
std::fs::write(self.mon_file_path(name), format!("{}", value))?;
pub fn hw_mon_write(&self, name: &str, value: u64) -> utils::Result<()> {
utils::write(self.mon_file_path(name), format!("{}", value))?;
Ok(())
}
pub fn device_write<C: AsRef<[u8]>>(&self, name: &str, value: C) -> std::io::Result<()> {
std::fs::write(self.device_dir().join(name), value)?;
pub fn device_write<C: AsRef<[u8]>>(&self, name: &str, value: C) -> utils::Result<()> {
utils::write(self.device_dir().join(name), value)?;
Ok(())
}
}

View File

@ -3,6 +3,44 @@ use std::io::ErrorKind;
use crate::hw_mon::HwMon;
use crate::{hw_mon, Card, CONFIG_DIR, ROOT_DIR};
pub type Result<T> = std::result::Result<T, AmdGpuError>;
#[derive(Debug, thiserror::Error)]
pub enum AmdGpuError {
#[error("Write to {path:?} failed. {io}")]
Write { io: std::io::Error, path: String },
#[error("Read from {path:?} failed. {io}")]
Read { io: std::io::Error, path: String },
#[error("File {0:?} does not exists")]
FileNotFound(String),
}
pub fn read_to_string<P: AsRef<std::path::Path>>(path: P) -> Result<String> {
std::fs::read_to_string(&path).map_err(|io| {
if io.kind() == std::io::ErrorKind::NotFound {
AmdGpuError::FileNotFound(path.as_ref().to_str().map(String::from).unwrap_or_default())
} else {
AmdGpuError::Read {
io,
path: path.as_ref().to_str().map(String::from).unwrap_or_default(),
}
}
})
}
pub fn write<P: AsRef<std::path::Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> {
std::fs::write(&path, contents).map_err(|io| {
if io.kind() == std::io::ErrorKind::NotFound {
AmdGpuError::FileNotFound(path.as_ref().to_str().map(String::from).unwrap_or_default())
} else {
AmdGpuError::Write {
io,
path: path.as_ref().to_str().map(String::from).unwrap_or_default(),
}
}
})
}
/// linear mapping from the xrange to the yrange
pub fn linear_map(x: f64, x1: f64, x2: f64, y1: f64, y2: f64) -> f64 {
let m = (y2 - y1) / (x2 - x1);

View File

@ -2,6 +2,7 @@ mod command;
mod log_file;
mod watch;
use amdgpu::utils;
use gumdrop::Options;
use crate::command::Command;
@ -20,6 +21,8 @@ pub enum AmdMonError {
#[error("{0}")]
FanConfigError(#[from] fan::ConfigError),
#[error("{0}")]
AmdUtils(#[from] utils::AmdGpuError),
#[error("{0}")]
Csv(#[from] csv::Error),
#[error("AMD GPU temperature is malformed. It should be number. {0:?}")]
NonIntTemp(std::num::ParseIntError),

View File

@ -1,4 +1,4 @@
use amdgpu::AmdGpuError;
use amdgpu::{utils, AmdGpuError};
use amdgpu_config::voltage::ConfigError;
use crate::change_state::ChangeStateError;
@ -20,4 +20,6 @@ pub enum VoltageError {
ClockState(#[from] ClockStateError),
#[error("{0:}")]
ChangeStateError(#[from] ChangeStateError),
#[error("{0:}")]
AmdUtils(#[from] utils::AmdGpuError),
}