From nobody Fri May 17 23:20:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.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.129.124]) by mx.zohomail.com with SMTPS id 1644329031005261.1340552581339; Tue, 8 Feb 2022 06:03:51 -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-195-J9JCQ-SuORm2PdqIb03txg-1; Tue, 08 Feb 2022 09:03:42 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 004DC100C661; Tue, 8 Feb 2022 14:03:37 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AFA3D70D39; 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 5EED41853D01; 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 2154hN9Q010561 for ; Fri, 4 Feb 2022 23:43:23 -0500 Received: by smtp.corp.redhat.com (Postfix) id 44CC440885D8; Sat, 5 Feb 2022 04:43:23 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast08.extmail.prod.ext.rdu2.redhat.com [10.11.55.24]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 40ECB40885BE for ; Sat, 5 Feb 2022 04:43:23 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) (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 25E143803934 for ; Sat, 5 Feb 2022 04:43:23 +0000 (UTC) Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-582-4HaGOspxNLCeDSCMbdN_dw-1; Fri, 04 Feb 2022 23:43:21 -0500 Received: by mail-pg1-f176.google.com with SMTP id h125so6700641pgc.3 for ; Fri, 04 Feb 2022 20:43:21 -0800 (PST) Received: from Menci-MacBook.lan ([148.163.172.141]) by smtp.gmail.com with ESMTPSA id bv22sm3821529pjb.31.2022.02.04.20.43.19 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 04 Feb 2022 20:43:19 -0800 (PST) X-MC-Unique: J9JCQ-SuORm2PdqIb03txg-1 X-MC-Unique: 4HaGOspxNLCeDSCMbdN_dw-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=SWAIozyIqlkCLvPMayDbpRBRBCThg1QCxN1a+A3heHObEjTe7kwBcxbFo3urV/O6yt uMk0j/cgXPFA+qAEcQ89dKX9ZDfpXMnUPOTxC5jwVfT67A34RdR8/YEhwaJbKMFh2V6H e1xVwuCn+PBhTiAP2+FuL8LP1knYEoJncSakAkkibOWpeN87B2YatD7r4fGM42NJWdMJ 9g5gxvreYDp86LFGXKa6QdtF8vuG8fuQaQgaHoehDtc1j2icuZjyjypW/8N3r8bYkqEr vdBB8T/TIU8Ad2nXBIoC7frnv4/SHZD/Vpo6N/MajZHwBQd81/8lAA3ReSvqQSIMBIob 0uyQ== X-Gm-Message-State: AOAM531/UG4fJarjxPOnNjxWaGCMgCGVLR26LdkYoxTNq5tn4Fvj0z9r /9UG+Xv+mEgcUOf7PHDrH+PihvMnIHuhPJNv X-Google-Smtp-Source: ABdhPJwtxCn0Yi2T8+RFyEZIPuh8xXJio81x88QikmSZM03ud6818XcquSZUUYTyPN2uQmjNorjm3g== X-Received: by 2002:a05:6a00:acd:: with SMTP id c13mr6453689pfl.80.1644036200262; Fri, 04 Feb 2022 20:43:20 -0800 (PST) From: Menci To: libvir-list@redhat.com Subject: [PATCH] virhostcpu: fix getting CPU freq for Apple Silicon Date: Sat, 5 Feb 2022 12:42:10 +0800 Message-Id: <20220205044210.67551-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-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.11 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: 1644329040938100003 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