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

perf: Reduce AMD Hotplug detection CPU usage with memmap #361

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 57 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ inotify = "0.9"
intel-pstate = "1.0.1"
libc = "0.2"
log = "0.4"
memmapix = "0.6.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sysfs-class = { git = "https://github.com/pop-os/sysfs-class" }
Expand Down
41 changes: 22 additions & 19 deletions src/hotplug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
pub mod mux;
pub mod sideband;

use memmapix::Mmap;
use sideband::{Sideband, SidebandError, PCR_BASE_ADDRESS};
use std::{
fs,
io::{self, Read, Seek},
};
use std::{fs, io};

#[derive(Debug, thiserror::Error)]
pub enum HotPlugDetectError {
Expand All @@ -25,6 +23,8 @@ pub enum HotPlugDetectError {
SubsystemDevice { model: &'static str, why: io::Error },
#[error("failed to open /dev/mem: {}", _0)]
DevMemAccess(io::Error),
#[error("cannot create memmap to /dev/mem: {}", _0)]
DevMemmap(io::Error),
}

impl From<SidebandError> for HotPlugDetectError {
Expand All @@ -38,17 +38,15 @@ pub trait Detect {
const AMD_FCH_GPIO_CONTROL_BASE: u32 = 0xFED8_1500;

struct Amd {
mem: fs::File,
mem: Mmap,
gpios: Vec<u32>,
}

impl Amd {
unsafe fn new(gpios: Vec<u32>) -> Result<Self, HotPlugDetectError> {
let mem = fs::OpenOptions::new()
.read(true)
.write(true)
.open("/dev/mem")
.map_err(HotPlugDetectError::DevMemAccess)?;
let file = fs::File::open("/dev/mem").map_err(HotPlugDetectError::DevMemAccess)?;

let mem = Mmap::map(&file).map_err(HotPlugDetectError::DevMemmap)?;

Ok(Self { mem, gpios })
}
Expand All @@ -59,18 +57,23 @@ impl Detect for Amd {
let mut hpd = [false; 4];

for (i, offset) in self.gpios.iter().enumerate() {
let control_offset = AMD_FCH_GPIO_CONTROL_BASE + offset * 4;
if self.mem.seek(io::SeekFrom::Start(control_offset as u64)).is_err() {
return hpd;
}
let control_offset = (AMD_FCH_GPIO_CONTROL_BASE + offset * 4) as usize;

// If we can get the fourth byte, then the three bytes before that will exist too.
if let Some(fourth) = self.mem.get(control_offset + 3) {
let control = [
self.mem[control_offset],
self.mem[control_offset + 1],
self.mem[control_offset + 2],
*fourth,
];

let mut control = [0; 4];
if self.mem.read(&mut control).is_err() {
return hpd;
let value = u32::from_ne_bytes(control);
hpd[i] = value & (1 << 16) == (1 << 16);
continue;
}

let value = u32::from_ne_bytes(control);
hpd[i] = value & (1 << 16) == (1 << 16);
return hpd;
}

hpd
Expand Down