Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return CommitHandle on commitable operations instead of committing directly #8

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "amdgpu-sysfs"
version = "0.14.0"
version = "0.15.0"
authors = ["Ilya Zlobintsev <[email protected]>"]
edition = "2021"
license = "GPL-3.0"
Expand Down
48 changes: 34 additions & 14 deletions src/gpu_handle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,13 @@ impl GpuHandle {

/// Writes and commits the given clocks table to `pp_od_clk_voltage`.
#[cfg(feature = "overdrive")]
pub fn set_clocks_table(&self, table: &ClocksTableGen) -> Result<()> {
pub fn set_clocks_table(&self, table: &ClocksTableGen) -> Result<CommitHandle> {
let path = self.sysfs_path.join("pp_od_clk_voltage");
let mut file = File::create(path)?;
let mut file = File::create(&path)?;

table.write_commands(&mut file)?;
file.write_all(b"c\n")?;

Ok(())
Ok(CommitHandle::new(path))
}

/// Resets the clocks table to the default configuration.
Expand Down Expand Up @@ -391,7 +390,7 @@ impl GpuHandle {
value: u32,
section_name: &str,
range_name: &str,
) -> Result<()> {
) -> Result<CommitHandle> {
let info = self.read_fan_info(file, section_name, range_name)?;
match info.allowed_range {
Some((min, max)) => {
Expand All @@ -403,9 +402,8 @@ impl GpuHandle {

let file_path = self.sysfs_path.join("gpu_od/fan_ctrl").join(file);
std::fs::write(&file_path, format!("{value}\n"))?;
std::fs::write(&file_path, "c\n")?;

Ok(())
Ok(CommitHandle::new(file_path))
}
None => Err(Error::not_allowed(format!(
"Changes to {range_name} are not allowed"
Expand All @@ -417,7 +415,7 @@ impl GpuHandle {
///
/// Only available on Navi3x (RDNA 3) or newer.
/// <https://kernel.org/doc/html/latest/gpu/amdgpu/thermal.html#acoustic-limit-rpm-threshold>
pub fn set_fan_acoustic_limit(&self, value: u32) -> Result<()> {
pub fn set_fan_acoustic_limit(&self, value: u32) -> Result<CommitHandle> {
self.set_fan_value(
"acoustic_limit_rpm_threshold",
value,
Expand All @@ -430,7 +428,7 @@ impl GpuHandle {
///
/// Only available on Navi3x (RDNA 3) or newer.
/// <https://kernel.org/doc/html/latest/gpu/amdgpu/thermal.html#acoustic-target-rpm-threshold>
pub fn set_fan_acoustic_target(&self, value: u32) -> Result<()> {
pub fn set_fan_acoustic_target(&self, value: u32) -> Result<CommitHandle> {
self.set_fan_value(
"acoustic_target_rpm_threshold",
value,
Expand All @@ -443,7 +441,7 @@ impl GpuHandle {
///
/// Only available on Navi3x (RDNA 3) or newer.
/// <https://kernel.org/doc/html/latest/gpu/amdgpu/thermal.html#fan-target-temperature>
pub fn set_fan_target_temperature(&self, value: u32) -> Result<()> {
pub fn set_fan_target_temperature(&self, value: u32) -> Result<CommitHandle> {
self.set_fan_value(
"fan_target_temperature",
value,
Expand All @@ -456,7 +454,7 @@ impl GpuHandle {
///
/// Only available on Navi3x (RDNA 3) or newer.
/// <https://kernel.org/doc/html/latest/gpu/amdgpu/thermal.html#fan-minimum-pwm>
pub fn set_fan_minimum_pwm(&self, value: u32) -> Result<()> {
pub fn set_fan_minimum_pwm(&self, value: u32) -> Result<CommitHandle> {
self.set_fan_value("fan_minimum_pwm", value, "FAN_MINIMUM_PWM", "MINIMUM_PWM")
}

Expand Down Expand Up @@ -559,7 +557,7 @@ impl GpuHandle {
///
/// Only available on Navi3x (RDNA 3) or newer.
/// <https://kernel.org/doc/html/latest/gpu/amdgpu/thermal.html#fan-curve>
pub fn set_fan_curve(&self, new_curve: &FanCurve) -> Result<()> {
pub fn set_fan_curve(&self, new_curve: &FanCurve) -> Result<CommitHandle> {
let current_curve = self.get_fan_curve()?;
let allowed_ranges = current_curve.allowed_ranges.ok_or_else(|| {
Error::not_allowed("Changes to the fan curve are not supported".to_owned())
Expand All @@ -583,9 +581,8 @@ impl GpuHandle {

std::fs::write(&file_path, format!("{i} {temperature} {speed}\n"))?;
}
std::fs::write(&file_path, "c\n")?;

Ok(())
Ok(CommitHandle::new(file_path))
}

/// Resets the PMFW fan curve.
Expand Down Expand Up @@ -658,3 +655,26 @@ impl fmt::Display for PerformanceLevel {
fn trim_sysfs_line(line: &str) -> &str {
line.trim_matches(char::from(0)).trim()
}

/// Handle for committing values which were previusly written
#[must_use]
#[derive(Debug)]
pub struct CommitHandle {
file_path: PathBuf,
}

impl CommitHandle {
pub(crate) fn new(file_path: PathBuf) -> Self {
Self { file_path }
}

/// Commit the previously written values
pub fn commit(self) -> Result<()> {
std::fs::write(&self.file_path, "c\n").with_context(|| {
format!(
"Could not commit values to {:?}",
self.file_path.file_name().unwrap()
)
})
}
}
3 changes: 2 additions & 1 deletion tests/rx7900xt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ test_with_handle! {
curve.points[2] = (60, 65);
curve.points[3] = (70, 80);
curve.points[4] = (85, 100);
gpu_handle.set_fan_curve(&curve)
let handle = gpu_handle.set_fan_curve(&curve)?;
handle.commit()
},
Ok(())
}
Expand Down
Loading