From: Andrea Bolognani Date: Thu, 10 Feb 2022 08:42:01 +0000 (+0100) Subject: util: Fix getting CPU frequency on Apple Silicon X-Git-Tag: v8.1.0-rc1~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85064aae646fec218c11b8da5ef81199080c87b3;p=thirdparty%2Flibvirt.git util: Fix getting CPU frequency on Apple Silicon The hw.cpufrequency sysctl, which we use to obtain the CPU frequency on macOS, is not available when running on Apple Silicon, and as a consequence we currently report an error whenever such information is requested. The virNodeInfo.mhz field, where the CPU frequency gets stored, is documented as being zero when the information could not be obtained, and we already do that for Linux on aarch64. Extend this behavior to macOS on Apple Silicon. Signed-off-by: Andrea Bolognani Reviewed-by: Michal Privoznik --- diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index a07c00a0e9..011ef8a153 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -928,8 +928,14 @@ virHostCPUGetInfo(virArch hostarch G_GNUC_UNUSED, *mhz = cpu_freq; # else if (sysctlbyname("hw.cpufrequency", &cpu_freq, &cpu_freq_len, NULL, 0) < 0) { - virReportSystemError(errno, "%s", _("cannot obtain CPU freq")); - return -1; + if (errno == ENOENT) { + /* The hw.cpufrequency sysctl is not implemented on Apple Silicon. + * In that case, we report 0 instead of erroring out */ + cpu_freq = 0; + } else { + virReportSystemError(errno, "%s", _("cannot obtain CPU freq")); + return -1; + } } *mhz = cpu_freq / 1000000;