Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Improve sysfs calls from providers to intelligently find the correct …
Browse files Browse the repository at this point in the history
…entry to fix #114
  • Loading branch information
NGnius committed Aug 6, 2023
1 parent 52f01ad commit 64cb419
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 147 deletions.
5 changes: 5 additions & 0 deletions backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ readme = "../README.md"
usdpl-back = { version = "0.10.1", features = ["blocking"] }#, path = "../../usdpl-rs/usdpl-back"}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sysfuss = { version = "0.1", path = "../../sysfs-nav", features = ["derive"] }

# async
tokio = { version = "*", features = ["time"] }
Expand Down
9 changes: 5 additions & 4 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,6 @@ fn main() -> Result<(), ()> {
}

let _limits_handle = crate::settings::limits_worker_spawn();
log::info!(
"Detected device automatically, starting with driver: {:?} (This can be overriden)",
crate::settings::auto_detect_provider()
);

let mut loaded_settings =
persist::SettingsJson::open(utility::settings_dir().join(DEFAULT_SETTINGS_FILE))
Expand All @@ -88,6 +84,11 @@ fn main() -> Result<(), ()> {
)
});

log::info!(
"Detected device automatically {:?}, using driver: {:?} (This can be overriden)",
crate::settings::auto_detect_provider(), loaded_settings.cpus.provider()
);

log::debug!("Settings: {:?}", loaded_settings);

let (api_handler, api_sender) = crate::api::handler::ApiMessageHandler::new();
Expand Down
3 changes: 3 additions & 0 deletions backend/src/persist/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct BatteryJson {
pub charge_mode: Option<String>,
#[serde(default)]
pub events: Vec<BatteryEventJson>,
#[serde(skip_serializing_if = "Option::is_none")]
pub root: Option<String>,
}

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -24,6 +26,7 @@ impl Default for BatteryJson {
charge_rate: None,
charge_mode: None,
events: Vec::new(),
root: None,
}
}
}
3 changes: 3 additions & 0 deletions backend/src/persist/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub struct CpuJson {
pub online: bool,
pub clock_limits: Option<MinMaxJson<u64>>,
pub governor: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub root: Option<String>,
}

impl Default for CpuJson {
Expand All @@ -20,6 +22,7 @@ impl Default for CpuJson {
online: true,
clock_limits: None,
governor: "schedutil".to_owned(),
root: None,
}
}
}
3 changes: 3 additions & 0 deletions backend/src/persist/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct GpuJson {
pub slow_ppt: Option<u64>,
pub clock_limits: Option<MinMaxJson<u64>>,
pub slow_memory: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub root: Option<String>,
}

impl Default for GpuJson {
Expand All @@ -19,6 +21,7 @@ impl Default for GpuJson {
slow_ppt: None,
clock_limits: None,
slow_memory: false,
root: None,
}
}
}
32 changes: 29 additions & 3 deletions backend/src/settings/generic/battery.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::convert::Into;

use limits_core::json::GenericBatteryLimit;
use sysfuss::SysEntity;

use crate::persist::BatteryJson;
use crate::settings::TBattery;
Expand All @@ -10,6 +11,7 @@ use crate::settings::{OnResume, OnSet, SettingError};
pub struct Battery {
#[allow(dead_code)]
limits: GenericBatteryLimit,
sysfs: sysfuss::PowerSupplyPath,
}

impl Into<BatteryJson> for Battery {
Expand All @@ -19,6 +21,7 @@ impl Into<BatteryJson> for Battery {
charge_rate: None,
charge_mode: None,
events: Vec::default(),
root: self.sysfs.root().and_then(|p| p.as_ref().to_str().map(|s| s.to_owned())),
}
}
}
Expand All @@ -37,18 +40,41 @@ impl Battery {
}
}

fn find_psu_sysfs(root: Option<impl AsRef<std::path::Path>>) -> sysfuss::PowerSupplyPath {
let root = crate::settings::util::root_or_default_sysfs(root);
match root.power_supply(crate::settings::util::always_satisfied) {
Ok(mut iter) => {
iter.next()
.unwrap_or_else(|| {
log::error!("Failed to find generic battery power_supply in sysfs (no results), using naive fallback");
root.power_supply_by_name("BAT0")
})
},
Err(e) => {
log::error!("Failed to find generic battery power_supply in sysfs ({}), using naive fallback", e);
root.power_supply_by_name("BAT0")
}
}
}

pub fn from_limits(limits: limits_core::json::GenericBatteryLimit) -> Self {
// TODO
Self { limits }
Self {
limits,
sysfs: Self::find_psu_sysfs(None::<&'static str>),
}
}

pub fn from_json_and_limits(
_other: BatteryJson,
other: BatteryJson,
_version: u64,
limits: limits_core::json::GenericBatteryLimit,
) -> Self {
// TODO
Self { limits }
Self {
limits,
sysfs: Self::find_psu_sysfs(other.root)
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions backend/src/settings/generic/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ pub struct Cpu {
limits: GenericCpuLimit,
index: usize,
state: crate::state::steam_deck::Cpu,
root: std::path::PathBuf,
}

/*impl Cpu {
Expand Down Expand Up @@ -235,6 +236,7 @@ impl FromGenericCpuInfo for Cpu {
limits,
index: cpu_index,
state: crate::state::steam_deck::Cpu::default(),
root: "/".into(),
}
}

Expand All @@ -258,6 +260,7 @@ impl FromGenericCpuInfo for Cpu {
limits,
index: i,
state: crate::state::steam_deck::Cpu::default(),
root: other.root.unwrap_or_else(|| "/".to_string()).into(),
},
_ => Self {
online: other.online,
Expand All @@ -266,6 +269,7 @@ impl FromGenericCpuInfo for Cpu {
limits,
index: i,
state: crate::state::steam_deck::Cpu::default(),
root: other.root.unwrap_or_else(|| "/".to_string()).into(),
},
}
}
Expand Down Expand Up @@ -354,6 +358,7 @@ impl Into<CpuJson> for Cpu {
online: self.online,
clock_limits: self.clock_limits.map(|x| x.into()),
governor: self.governor,
root: self.root.to_str().map(|s| s.to_owned()),
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions backend/src/settings/generic/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::convert::Into;

use limits_core::json::GenericGpuLimit;
use sysfuss::{BasicEntityPath, SysEntity};

use crate::api::RangeLimit;
use crate::persist::GpuJson;
Expand All @@ -15,6 +16,7 @@ pub struct Gpu {
pub slow_ppt: Option<u64>,
pub clock_limits: Option<MinMax<u64>>,
limits: GenericGpuLimit,
sysfs: BasicEntityPath,
}

impl Gpu {
Expand All @@ -31,13 +33,31 @@ impl Gpu {
}
}*/

fn find_card_sysfs(root: Option<impl AsRef<std::path::Path>>) -> BasicEntityPath {
let root = crate::settings::util::root_or_default_sysfs(root);
match root.class("drm", crate::settings::util::always_satisfied) {
Ok(mut iter) => {
iter.next()
.unwrap_or_else(|| {
log::error!("Failed to find generic gpu drm in sysfs (no results), using naive fallback");
BasicEntityPath::new(root.as_ref().join("sys/class/drm/card0"))
})
},
Err(e) => {
log::error!("Failed to find generic gpu drm in sysfs ({}), using naive fallback", e);
BasicEntityPath::new(root.as_ref().join("sys/class/drm/card0"))
}
}
}

pub fn from_limits(limits: limits_core::json::GenericGpuLimit) -> Self {
Self {
slow_memory: false,
fast_ppt: None,
slow_ppt: None,
clock_limits: None,
limits,
sysfs: Self::find_card_sysfs(None::<&'static str>),
}
}

Expand Down Expand Up @@ -65,6 +85,7 @@ impl Gpu {
},
clock_limits: clock_lims,
limits,
sysfs: Self::find_card_sysfs(other.root)
}
}
}
Expand All @@ -77,6 +98,7 @@ impl Into<GpuJson> for Gpu {
slow_ppt: self.slow_ppt,
clock_limits: self.clock_limits.map(|x| x.into()),
slow_memory: false,
root: self.sysfs.root().and_then(|p| p.as_ref().to_str().map(|s| s.to_owned()))
}
}
}
Expand Down
Loading

0 comments on commit 64cb419

Please sign in to comment.