mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-21 23:05:46 +00:00
fix(perf): use Mach API for macOS host CPU ticks (#931)
* fix(perf): use Mach host_processor_info for macOS CPU ticks
KERN_CP_TIME and CPUSTATES are not exposed by libc on Darwin; switch
read_host_total_cpu_ticks to host_processor_info so aarch64-apple-darwin CI builds succeed.
* docs: CHANGELOG and credits for macOS perf CI fix (PR #931)
* Revert "docs: CHANGELOG and credits for macOS perf CI fix (PR #931)"
This reverts commit a217217c34.
---------
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.com>
This commit is contained in:
@@ -6,7 +6,7 @@ use serde::Serialize;
|
|||||||
|
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
#[cfg(target_os = "linux")]
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -369,24 +369,33 @@ mod macos {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn read_host_total_cpu_ticks() -> u64 {
|
fn read_host_total_cpu_ticks() -> u64 {
|
||||||
let mut mib = [libc::CTL_KERN, libc::KERN_CP_TIME];
|
let mut num_cpus: u32 = 0;
|
||||||
let mut cp_time = [0_u64; libc::CPUSTATES as usize];
|
let mut cpu_info: *mut i32 = std::ptr::null_mut();
|
||||||
let mut size = mem::size_of_val(&cp_time);
|
let mut num_cpu_info: u32 = 0;
|
||||||
let ok = unsafe {
|
let ok = unsafe {
|
||||||
libc::sysctl(
|
libc::host_processor_info(
|
||||||
mib.as_mut_ptr(),
|
libc::mach_host_self(),
|
||||||
mib.len() as _,
|
libc::PROCESSOR_CPU_LOAD_INFO,
|
||||||
cp_time.as_mut_ptr() as *mut _,
|
&mut num_cpus,
|
||||||
&mut size,
|
&mut cpu_info,
|
||||||
std::ptr::null_mut(),
|
&mut num_cpu_info,
|
||||||
0,
|
) == libc::KERN_SUCCESS
|
||||||
) == 0
|
|
||||||
};
|
};
|
||||||
if ok {
|
if !ok || cpu_info.is_null() {
|
||||||
cp_time.iter().sum()
|
return 0;
|
||||||
} else {
|
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
let total: u64 = unsafe {
|
||||||
|
std::slice::from_raw_parts(cpu_info, num_cpu_info as usize)
|
||||||
|
.iter()
|
||||||
|
.map(|&ticks| ticks as u64)
|
||||||
|
.sum()
|
||||||
|
};
|
||||||
|
unsafe {
|
||||||
|
let size = num_cpu_info as usize * mem::size_of::<i32>();
|
||||||
|
#[allow(deprecated)]
|
||||||
|
libc::vm_deallocate(libc::mach_task_self(), cpu_info as _, size);
|
||||||
|
}
|
||||||
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refresh_target_processes(sys: &mut System, self_pid: Pid) -> Vec<Pid> {
|
fn refresh_target_processes(sys: &mut System, self_pid: Pid) -> Vec<Pid> {
|
||||||
|
|||||||
Reference in New Issue
Block a user