power: Implement get_gpu_clocks and ManualGPUClock

This commit is contained in:
Vicki Pfau 2024-04-01 22:06:07 -07:00
parent d751110086
commit d13010dcb0
2 changed files with 41 additions and 6 deletions

View file

@ -15,8 +15,8 @@ use zbus::{interface, Connection, SignalContext};
use crate::hardware::{check_support, variant, HardwareVariant}; use crate::hardware::{check_support, variant, HardwareVariant};
use crate::power::{ use crate::power::{
get_gpu_performance_level, set_gpu_clocks, set_gpu_performance_level, set_tdp_limit, get_gpu_clocks, get_gpu_performance_level, set_gpu_clocks, set_gpu_performance_level,
GPUPerformanceLevel, set_tdp_limit, GPUPerformanceLevel,
}; };
use crate::process::{run_script, script_output}; use crate::process::{run_script, script_output};
use crate::systemd::SystemdUnit; use crate::systemd::SystemdUnit;
@ -277,8 +277,14 @@ impl SteamOSManager {
.map_err(anyhow_to_zbus) .map_err(anyhow_to_zbus)
} }
async fn set_gpu_clocks(&self, clocks: i32) -> bool { #[zbus(property)]
set_gpu_clocks(clocks).await.is_ok() async fn manual_gpu_clock(&self) -> zbus::fdo::Result<u32> {
get_gpu_clocks().await.map_err(anyhow_to_zbus_fdo)
}
#[zbus(property)]
async fn set_manual_gpu_clock(&self, clocks: u32) -> zbus::Result<()> {
set_gpu_clocks(clocks).await.map_err(anyhow_to_zbus)
} }
#[zbus(property(emits_changed_signal = "const"))] #[zbus(property(emits_changed_signal = "const"))]

View file

@ -8,7 +8,7 @@
use anyhow::{anyhow, bail, ensure, Error, Result}; use anyhow::{anyhow, bail, ensure, Error, Result};
use std::str::FromStr; use std::str::FromStr;
use tokio::fs::{self, File}; use tokio::fs::{self, File};
use tokio::io::AsyncWriteExt; use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tracing::error; use tracing::error;
use crate::path; use crate::path;
@ -93,7 +93,7 @@ pub async fn set_gpu_performance_level(level: GPUPerformanceLevel) -> Result<()>
Ok(()) Ok(())
} }
pub async fn set_gpu_clocks(clocks: i32) -> Result<()> { pub async fn set_gpu_clocks(clocks: u32) -> Result<()> {
// Set GPU clocks to given value valid between 200 - 1600 // Set GPU clocks to given value valid between 200 - 1600
// Only used when GPU Performance Level is manual, but write whenever called. // Only used when GPU Performance Level is manual, but write whenever called.
ensure!((200..=1600).contains(&clocks), "Invalid clocks"); ensure!((200..=1600).contains(&clocks), "Invalid clocks");
@ -121,6 +121,35 @@ pub async fn set_gpu_clocks(clocks: i32) -> Result<()> {
Ok(()) Ok(())
} }
pub async fn get_gpu_clocks() -> Result<u32> {
let clocks_file = File::open(GPU_CLOCKS_PATH).await?;
let mut reader = BufReader::new(clocks_file);
loop {
let mut line = String::new();
if reader.read_line(&mut line).await? == 0 {
break;
}
if line != "OD_SCLK:\n" {
continue;
}
let mut line = String::new();
if reader.read_line(&mut line).await? == 0 {
break;
}
let mhz = match line.split_whitespace().nth(1) {
Some(mhz) if mhz.ends_with("Mhz") => mhz.trim_end_matches("Mhz"),
_ => break,
};
match mhz.parse() {
Ok(mhz) => return Ok(mhz),
Err(e) => return Err(e.into()),
}
}
Err(anyhow!("Couldn't find GPU clocks"))
}
pub async fn set_tdp_limit(limit: i32) -> Result<()> { pub async fn set_tdp_limit(limit: i32) -> Result<()> {
// Set TDP limit given if within range (3-15) // Set TDP limit given if within range (3-15)
// Returns false on error or out of range // Returns false on error or out of range