Power cap saving

This commit is contained in:
Ilya Zlobintsev
2020-11-15 20:47:56 +02:00
parent b94dd2c265
commit d884631f8a
6 changed files with 58 additions and 26 deletions

View File

@@ -37,6 +37,7 @@ impl PartialEq for GpuIdentifier {
pub struct GpuConfig {
pub fan_control_enabled: bool,
pub fan_curve: BTreeMap<i32, f64>,
pub power_cap: i32,
}
impl GpuConfig {
@@ -51,6 +52,7 @@ impl GpuConfig {
GpuConfig {
fan_curve,
fan_control_enabled: false,
power_cap: -1,
}
}
}

View File

@@ -1,9 +1,5 @@
use crate::{Action, DaemonError, DaemonResponse, SOCK_PATH, gpu_controller::GpuInfo, gpu_controller::{FanControlInfo, GpuStats}};
use std::{collections::HashMap, os::unix::net::UnixStream};
use std::{
collections::BTreeMap,
io::{Read, Write},
};
use std::{collections::HashMap, os::unix::net::UnixStream}; use std::{ collections::BTreeMap, io::{Read, Write}, };
#[derive(Clone, Copy)]
pub struct DaemonConnection {}
@@ -148,6 +144,27 @@ impl DaemonConnection {
}
}
pub fn get_power_cap(&self, gpu_id: u32) -> Result<(i32, i32), DaemonError> {
let mut s = UnixStream::connect(SOCK_PATH).unwrap();
s.write_all(&bincode::serialize(&Action::GetPowerCap(gpu_id)).unwrap())
.unwrap();
s.shutdown(std::net::Shutdown::Write).expect("Could not shut down");
let mut buffer = Vec::<u8>::new();
s.read_to_end(&mut buffer).unwrap();
let result: Result<DaemonResponse, DaemonError> = bincode::deserialize(&buffer).unwrap();
match result {
Ok(response) => {
match response {
DaemonResponse::PowerCap(cap) => Ok(cap),
_ => unreachable!("invalid response"),
}
},
Err(e) => Err(e),
}
}
pub fn set_power_cap(&self, gpu_id: u32, cap: i32) -> Result<(), DaemonError> {
let mut s = UnixStream::connect(SOCK_PATH).unwrap();
s.write_all(&bincode::serialize(&Action::SetPowerCap(gpu_id, cap)).unwrap())

View File

@@ -55,8 +55,6 @@ pub struct GpuInfo {
pub link_width: u8,
pub vulkan_info: VulkanInfo,
pub pci_slot: String,
pub power_cap: i32,
pub power_cap_max: i32,
}
impl GpuController {
@@ -68,6 +66,7 @@ impl GpuController {
&path,
config.fan_control_enabled,
config.fan_curve.clone(),
config.power_cap,
);
Some(hw_mon)
},
@@ -93,6 +92,7 @@ impl GpuController {
&path,
config.fan_control_enabled,
config.fan_curve.clone(),
config.power_cap,
);
Some(hw_mon)
},
@@ -213,11 +213,6 @@ impl GpuController {
let vulkan_info = GpuController::get_vulkan_info(&model_id);
let (power_cap, power_cap_max) = match &self.hw_mon {
Some(hw_mon) => (hw_mon.get_power_cap(), hw_mon.get_power_cap_max()),
None => (0, 0),
};
GpuInfo {
gpu_vendor: vendor,
gpu_model: model,
@@ -232,8 +227,6 @@ impl GpuController {
link_width,
vulkan_info,
pci_slot,
power_cap,
power_cap_max,
}
}
@@ -327,13 +320,22 @@ impl GpuController {
match &mut self.hw_mon {
Some(hw_mon) => {
hw_mon.set_power_cap(cap).unwrap();
self.gpu_info.power_cap = cap;
self.config.power_cap = cap;
Ok(())
},
None => Err(HWMonError::NoHWMon),
}
}
pub fn get_power_cap(&self) -> Result<(i32, i32), HWMonError> {
match &self.hw_mon {
Some(hw_mon) => {
Ok((hw_mon.get_power_cap(), hw_mon.get_power_cap_max()))
},
None => Err(HWMonError::NoHWMon),
}
}
fn get_vulkan_info(pci_id: &str) -> VulkanInfo {
let mut device_name = String::from("Not supported");
let mut api_version = String::new();

View File

@@ -33,6 +33,7 @@ impl HWMon {
hwmon_path: &PathBuf,
fan_control_enabled: bool,
fan_curve: BTreeMap<i32, f64>,
power_cap: i32,
) -> HWMon {
let fan_max_speed = match fs::read_to_string(hwmon_path.join("fan1_max")) {
Ok(s) => s.trim().parse::<i32>().unwrap(),
@@ -50,7 +51,12 @@ impl HWMon {
if fan_control_enabled {
mon.start_fan_control().unwrap();
}
mon.power_cap = mon.get_power_cap();
if power_cap == -1 {
mon.power_cap = mon.get_power_cap();
}
else {
mon.set_power_cap(power_cap).unwrap();
}
mon
}
@@ -146,7 +152,6 @@ impl HWMon {
}
pub fn set_power_cap(&mut self, cap: i32) -> Result<(), HWMonError> {
if cap > self.get_power_cap_max() {
return Err(HWMonError::InvalidValue);
}

View File

@@ -36,6 +36,7 @@ pub enum Action {
GetFanControl(u32),
SetFanCurve(u32, BTreeMap<i32, f64>),
SetPowerCap(u32, i32),
GetPowerCap(u32),
Shutdown,
}
@@ -209,6 +210,8 @@ impl Daemon {
Some(controller) => {
match controller.set_power_cap(cap) {
Ok(_) => {
self.config.gpu_configs.insert(i, (controller.get_identifier(), controller.get_config()));
self.config.save().unwrap();
Ok(DaemonResponse::OK)
},
Err(_) => Err(DaemonError::HWMonError),
@@ -216,6 +219,13 @@ impl Daemon {
},
None => Err(DaemonError::InvalidID),
}
Action::GetPowerCap(i) => match self.gpu_controllers.get(&i) {
Some(controller) => match controller.get_power_cap() {
Ok(cap) => Ok(DaemonResponse::PowerCap(cap)),
Err(_) => Err(DaemonError::HWMonError),
}
None => Err(DaemonError::InvalidID),
}
Action::Shutdown => std::process::exit(0),
};
@@ -240,6 +250,7 @@ pub enum DaemonResponse {
GpuInfo(gpu_controller::GpuInfo),
GpuStats(gpu_controller::GpuStats),
Gpus(HashMap<u32, String>),
PowerCap((i32, i32)),
FanControlInfo(gpu_controller::FanControlInfo),
}

View File

@@ -210,6 +210,10 @@ fn build_ui(application: &gtk::Application) {
}
}
let (power_cap, power_cap_max) = d.get_power_cap(*current_gpu_id.read().unwrap()).unwrap();
gpu_power_adjustment.set_upper(power_cap_max as f64);
gpu_power_adjustment.set_value(power_cap as f64);
let b = apply_button.clone();
@@ -331,11 +335,6 @@ fn set_info(builder: &Builder, gpu_info: &GpuInfo) {
.get_object("vulkan_features_text_buffer")
.expect("Couldn't get textbuffer");
let power_cap_label: Label = builder.get_object("power_cap_label").unwrap();
let gpu_power_adjustment: Adjustment = builder.get_object("gpu_power_adjustment").unwrap();
//let power_cap_scale: Scale = builder.get_object("power_cap_scale").unwrap();
gpu_model_text_buffer.set_text(&gpu_info.card_model);
manufacturer_text_buffer.set_text(&gpu_info.card_vendor);
vbios_version_text_buffer.set_text(&gpu_info.vbios_version);
@@ -351,10 +350,6 @@ fn set_info(builder: &Builder, gpu_info: &GpuInfo) {
vulkan_device_name_text_buffer.set_text(&gpu_info.vulkan_info.device_name);
vulkan_version_text_buffer.set_text(&gpu_info.vulkan_info.api_version);
vulkan_features_text_buffer.set_text(&vulkan_features);
power_cap_label.set_text(&format!("{}/{}", gpu_info.power_cap, gpu_info.power_cap_max));
gpu_power_adjustment.set_upper(gpu_info.power_cap_max as f64);
gpu_power_adjustment.set_value(gpu_info.power_cap as f64);
}