[libvirt PATCH] util: Fix getting CPU frequency on Apple Silicon

Andrea Bolognani posted 1 patch 2 years, 2 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20220210090842.99280-1-abologna@redhat.com
src/util/virhostcpu.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
[libvirt PATCH] util: Fix getting CPU frequency on Apple Silicon
Posted by Andrea Bolognani 2 years, 2 months ago
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 <abologna@redhat.com>
---
 src/util/virhostcpu.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

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;
-- 
2.34.1

Re: [libvirt PATCH] util: Fix getting CPU frequency on Apple Silicon
Posted by Michal Prívozník 2 years, 2 months ago
On 2/10/22 10:08, Andrea Bolognani wrote:
> 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 <abologna@redhat.com>
> ---
>  src/util/virhostcpu.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal

Re: [libvirt PATCH] util: Fix getting CPU frequency on Apple Silicon
Posted by Andrea Bolognani 2 years, 2 months ago
On Thu, Feb 10, 2022 at 10:08:42AM +0100, Andrea Bolognani wrote:
> +++ 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;
> +        }
>      }

Menci, can you please confirm this works for you before I push? I
have no way of verifying that on my own. Thanks!

-- 
Andrea Bolognani / Red Hat / Virtualization

Re: [libvirt PATCH] util: Fix getting CPU frequency on Apple Silicon
Posted by Menci 2 years, 2 months ago
I tested the patch (applying to v7.10.0) and it works on my M1 MacBook Air.
Thanks.

On Sat, Feb 12, 2022 at 1:32 AM Andrea Bolognani <abologna@redhat.com>
wrote:

> On Thu, Feb 10, 2022 at 10:08:42AM +0100, Andrea Bolognani wrote:
> > +++ 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;
> > +        }
> >      }
>
> Menci, can you please confirm this works for you before I push? I
> have no way of verifying that on my own. Thanks!
>
> --
> Andrea Bolognani / Red Hat / Virtualization
>
>
Re: [libvirt PATCH] util: Fix getting CPU frequency on Apple Silicon
Posted by Andrea Bolognani 2 years, 2 months ago
On Mon, Feb 14, 2022 at 04:59:11AM +0800, Menci wrote:
> I tested the patch (applying to v7.10.0) and it works on my M1 MacBook Air.

Thanks a lot for confirming! I've pushed the patch now :)

-- 
Andrea Bolognani / Red Hat / Virtualization