From nobody Sat May 18 23:55:34 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1644329024947804.037785655021; Tue, 8 Feb 2022 06:03:44 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-330-BtjVgGwZMvuPgFo6WYkwGA-1; Tue, 08 Feb 2022 09:03:42 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ADA351923E22; Tue, 8 Feb 2022 14:03:36 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B29FE7C0E9; Tue, 8 Feb 2022 14:03:35 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5EF054BB7B; Tue, 8 Feb 2022 14:03:31 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 2178maNk017873 for ; Mon, 7 Feb 2022 03:48:36 -0500 Received: by smtp.corp.redhat.com (Postfix) id 25C0B400E135; Mon, 7 Feb 2022 08:48:36 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast10.extmail.prod.ext.rdu2.redhat.com [10.11.55.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2023140D1B98 for ; Mon, 7 Feb 2022 08:48:36 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 08AA81C04B5D for ; Mon, 7 Feb 2022 08:48:36 +0000 (UTC) Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-226-n6c3u7RzP3SxAe7iDwO-4g-1; Mon, 07 Feb 2022 03:48:33 -0500 Received: by mail-pl1-f181.google.com with SMTP id t9so8393432plg.13 for ; Mon, 07 Feb 2022 00:48:33 -0800 (PST) Received: from Menci-MacBook.lan ([148.163.172.143]) by smtp.gmail.com with ESMTPSA id z1sm11094514pfh.137.2022.02.07.00.48.31 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 07 Feb 2022 00:48:31 -0800 (PST) X-MC-Unique: BtjVgGwZMvuPgFo6WYkwGA-1 X-MC-Unique: n6c3u7RzP3SxAe7iDwO-4g-1 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=R3mKFAa/27zHsXXf/VCJQonq1uZDOHGtpkVmP1d1agU=; b=3pRb2qJYWxkIlCKPTifYTAp6KXpE/SFQwPleH7lmKv/tE7p4YHTAm3O3g1uLSa611m Rmlwhg+KnGd9RcMKtmzMNA80kE3Up3+NhADXltYRhETZi9BcKOej7m0d3+iONctznMmI 3YDrbtFyy74CLbH1TDFP0nBkPcTnoqqWnoq8NWKgPM3JiorW6q1DNT4LPOalVSL5rnkq Xs8B3/izoOcARqdgxaFxNX8VIRff1c+trCjO3csnaRB0970BppSWebsp6ozx3ChQbwUu O/vbV5nQQf5ZCyCPqC9Pynufn7NQKPrQzBFsPK8oSwOV6nqYm8ROzG+lts43lKUaEyU2 1ZjQ== X-Gm-Message-State: AOAM530OAXWKAwvjUmB9iojctx2IWGsERxxY6h6L30nljwErk8apD8Fb YDdqloXJe04w/mtQUQ2ryAXwVj5+HQzDtZQ4 X-Google-Smtp-Source: ABdhPJy4q6vIWcLpJbhdvgpIzeyc6RDICpD1qkQ4Gjiv+iiZlXmKgeygVMrk2dYElAdNo72PBgEOlQ== X-Received: by 2002:a17:90b:2281:: with SMTP id kx1mr17201758pjb.37.1644223712374; Mon, 07 Feb 2022 00:48:32 -0800 (PST) From: Menci To: libvir-list@redhat.com Subject: [PATCH] virhostcpu: fix getting CPU freq for Apple Silicon Date: Mon, 7 Feb 2022 16:48:06 +0800 Message-Id: <20220207084806.47411-1-huanghaorui301@gmail.com> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Mimecast-Spam-Signature: yes X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-loop: libvir-list@redhat.com X-Mailman-Approved-At: Tue, 08 Feb 2022 09:03:29 -0500 Cc: Menci X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZM-MESSAGEID: 1644329040125100001 Content-Type: text/plain; charset="utf-8" The current implementation of virHostCPUGetInfo for macOS (__APPLE__) reads "hw.cpufrequency" from sysctl, which is available only on x86_64 architecture. On Apple Silicon ARM Macs, it's not available: $ sysctl hw.cpufrequency # No output $ arch -x86_64 sysctl hw.cpufrequency # Run with Rosetta 2 hw.cpufrequency: 2400000000 When running libvirtd on Apple Silicon, I got the error: cannot obtain CPU freq: No such file or directory. To fix it, we can calculate it with "hw.tbfrequency" and "kern.clockrate" instead: $ sysctl hw.tbfrequency hw.tbfrequency: 24000000 $ sysctl kern.clockrate kern.clockrate: { hz =3D 100, tick =3D 10000, tickadj =3D 0, profhz =3D 1= 00, stathz =3D 100 } The result value would be hw.tbfrequency * kern.clockrate.hz. Signed-off-by: Menci --- src/util/virhostcpu.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index a07c00a0e9..72983c91f3 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -927,9 +927,18 @@ virHostCPUGetInfo(virArch hostarch G_GNUC_UNUSED, =20 *mhz =3D cpu_freq; # else + /* This works for Intel Macs */ if (sysctlbyname("hw.cpufrequency", &cpu_freq, &cpu_freq_len, NULL, 0)= < 0) { - virReportSystemError(errno, "%s", _("cannot obtain CPU freq")); - return -1; + /* On Apple Silicon fallback to hw.tbfrequency and kern.clockrate.= hz */ + struct clockinfo clockrate; + size_t clockrate_len =3D sizeof(clockrate); + if (sysctlbyname("hw.tbfrequency", &cpu_freq, &cpu_freq_len, NULL,= 0) < 0 || + sysctlbyname("kern.clockrate", &clockrate, &clockrate_len, NUL= L, 0) < 0) { + virReportSystemError(errno, "%s", _("cannot obtain CPU freq")); + return -1; + } + + cpu_freq *=3D clockrate.hz; } =20 *mhz =3D cpu_freq / 1000000; --=20 2.34.1