An alternative in getting CPU freq on M-series

This solution was retrieved from
https://listman.redhat.com/archives/libvir-list/2022-February/228217.html
sysctl hw.cpufrequency would result as empty on Apple Silicon (at least) M1,
when run natively. Ironically (and that's why it's been working with viewers
relying on Rosetta 2), arch -x86_64 /bin/bash -c 'sysctl hw.cpufrequency'
run on an Apple Silicon would result in the correct number.
meow-7.2.2
Erik Kundiman 2023-12-26 12:11:15 +08:00
parent 83e80d7967
commit 268281a570
1 changed files with 8 additions and 0 deletions

View File

@ -637,7 +637,15 @@ public:
LLProcessorInfoDarwinImpl()
{
getCPUIDInfo();
#ifdef __arm64__
auto frequency = getSysctlInt64("hw.tbfrequency");
struct clockinfo clockrate;
auto clockrate_len = sizeof(clockrate);
if (!sysctlbyname("kern.clockrate", &clockrate, &clockrate_len, NULL, 0))
frequency *= clockrate.hz;
#else
uint64_t frequency = getSysctlInt64("hw.cpufrequency");
#endif
setInfo(eFrequency, (F64)frequency / (F64)1000000);
}