Skip to content

Commit

Permalink
try fix M4 chips #11
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Nov 30, 2024
1 parent 10f677c commit 9e05a6f
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ pub fn get_dvfs_mhz(dict: CFDictionaryRef, key: &str) -> (Vec<u32>, Vec<u32>) {
for (i, x) in obj_val.chunks_exact(8).enumerate() {
volts[i] = u32::from_le_bytes([x[4], x[5], x[6], x[7]]);
freqs[i] = u32::from_le_bytes([x[0], x[1], x[2], x[3]]);
freqs[i] = freqs[i] / 1000 / 1000; // as MHz
}

(volts, freqs)
Expand All @@ -423,6 +422,16 @@ pub fn run_system_profiler() -> WithError<serde_json::Value> {
Ok(out)
}

fn to_mhz(vals: Vec<u32>, chip: &str) -> Vec<u32> {
let scale = if chip.contains("M1") || chip.contains("M2") || chip.contains("M3") {
1000 * 1000 // MHz
} else {
1000 // KHz
};

vals.iter().map(|x| *x / scale).collect()
}

pub fn get_soc_info() -> WithError<SocInfo> {
let out = run_system_profiler()?;
let mut info = SocInfo::default();
Expand Down Expand Up @@ -461,11 +470,12 @@ pub fn get_soc_info() -> WithError<SocInfo> {
for (entry, name) in IOServiceIterator::new("AppleARMIODevice")? {
if name == "pmgr" {
let item = cfio_get_props(entry, name)?;
// `strings /usr/bin/powermetrics | grep voltage-states` uses non sram keys
// 1) `strings /usr/bin/powermetrics | grep voltage-states` uses non sram keys
// but their values are zero, so sram used here, its looks valid
info.ecpu_freqs = get_dvfs_mhz(item, "voltage-states1-sram").1;
info.pcpu_freqs = get_dvfs_mhz(item, "voltage-states5-sram").1;
info.gpu_freqs = get_dvfs_mhz(item, "voltage-states9").1;
// 2) sudo powermetrics --samplers cpu_power -i 1000 -n 1 | grep "active residency" | grep "Cluster"
info.ecpu_freqs = to_mhz(get_dvfs_mhz(item, "voltage-states1-sram").1, &info.chip_name);
info.pcpu_freqs = to_mhz(get_dvfs_mhz(item, "voltage-states5-sram").1, &info.chip_name);
info.gpu_freqs = to_mhz(get_dvfs_mhz(item, "voltage-states9").1, &info.chip_name);
unsafe { CFRelease(item as _) }
}
}
Expand Down

0 comments on commit 9e05a6f

Please sign in to comment.