scx_utils/
pm.rs

1// Copyright (c) Meta Platforms, Inc. and affiliates.
2
3// This software may be used and distributed according to the terms of the
4// GNU General Public License version 2.
5
6use anyhow::{Result, anyhow};
7use std::fs::{File, OpenOptions};
8use std::io::Write;
9use std::path::Path;
10
11/// Updates the global idle resume latency. When the returned file is closed the request is
12/// dropped. See the following kernel docs for more details:
13/// https://www.kernel.org/doc/html/latest/admin-guide/pm/cpuidle.html#power-management-quality-of-service-for-cpus
14pub fn update_global_idle_resume_latency(value_us: i32) -> Result<File> {
15    if value_us < 0 {
16        return Err(anyhow!("Latency value must be non-negative"));
17    }
18
19    let mut file = OpenOptions::new()
20        .write(true)
21        .open("/dev/cpu_dma_latency")?;
22    let bytes = value_us.to_le_bytes(); // Convert to little-endian bytes
23    file.write_all(&bytes)?;
24    Ok(file) // return file descriptor so it can be closed later
25}
26
27/// Updates per cpu idle resume latency.
28pub fn update_cpu_idle_resume_latency(cpu_num: usize, value_us: i32) -> Result<()> {
29    if value_us < 0 {
30        return Err(anyhow!("Latency value must be non-negative"));
31    }
32
33    let path = format!(
34        "/sys/devices/system/cpu/cpu{}/power/pm_qos_resume_latency_us",
35        cpu_num
36    );
37
38    let mut file = File::create(Path::new(&path))?;
39    write!(file, "{}", value_us)?;
40    Ok(())
41}
42
43/// Returns if idle resume latency is supported.
44pub fn cpu_idle_resume_latency_supported() -> bool {
45    std::fs::exists("/sys/devices/system/cpu/cpu0/power/pm_qos_resume_latency_us").unwrap_or(false)
46}