Skip to content

Commit 9e05a6f

Browse files
committed
try fix M4 chips #11
1 parent 10f677c commit 9e05a6f

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/sources.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ pub fn get_dvfs_mhz(dict: CFDictionaryRef, key: &str) -> (Vec<u32>, Vec<u32>) {
405405
for (i, x) in obj_val.chunks_exact(8).enumerate() {
406406
volts[i] = u32::from_le_bytes([x[4], x[5], x[6], x[7]]);
407407
freqs[i] = u32::from_le_bytes([x[0], x[1], x[2], x[3]]);
408-
freqs[i] = freqs[i] / 1000 / 1000; // as MHz
409408
}
410409

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

425+
fn to_mhz(vals: Vec<u32>, chip: &str) -> Vec<u32> {
426+
let scale = if chip.contains("M1") || chip.contains("M2") || chip.contains("M3") {
427+
1000 * 1000 // MHz
428+
} else {
429+
1000 // KHz
430+
};
431+
432+
vals.iter().map(|x| *x / scale).collect()
433+
}
434+
426435
pub fn get_soc_info() -> WithError<SocInfo> {
427436
let out = run_system_profiler()?;
428437
let mut info = SocInfo::default();
@@ -461,11 +470,12 @@ pub fn get_soc_info() -> WithError<SocInfo> {
461470
for (entry, name) in IOServiceIterator::new("AppleARMIODevice")? {
462471
if name == "pmgr" {
463472
let item = cfio_get_props(entry, name)?;
464-
// `strings /usr/bin/powermetrics | grep voltage-states` uses non sram keys
473+
// 1) `strings /usr/bin/powermetrics | grep voltage-states` uses non sram keys
465474
// but their values are zero, so sram used here, its looks valid
466-
info.ecpu_freqs = get_dvfs_mhz(item, "voltage-states1-sram").1;
467-
info.pcpu_freqs = get_dvfs_mhz(item, "voltage-states5-sram").1;
468-
info.gpu_freqs = get_dvfs_mhz(item, "voltage-states9").1;
475+
// 2) sudo powermetrics --samplers cpu_power -i 1000 -n 1 | grep "active residency" | grep "Cluster"
476+
info.ecpu_freqs = to_mhz(get_dvfs_mhz(item, "voltage-states1-sram").1, &info.chip_name);
477+
info.pcpu_freqs = to_mhz(get_dvfs_mhz(item, "voltage-states5-sram").1, &info.chip_name);
478+
info.gpu_freqs = to_mhz(get_dvfs_mhz(item, "voltage-states9").1, &info.chip_name);
469479
unsafe { CFRelease(item as _) }
470480
}
471481
}

0 commit comments

Comments
 (0)