From nobody Thu May 15 06:03:15 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1513181450127925.6762221078762; Wed, 13 Dec 2017 08:10:50 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 27E19C0828D4; Wed, 13 Dec 2017 16:10:49 +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 E94FD7BFF2; Wed, 13 Dec 2017 16:10:48 +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 B503D3FCFD; Wed, 13 Dec 2017 16:10:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vBDGAZnE027237 for ; Wed, 13 Dec 2017 11:10:35 -0500 Received: by smtp.corp.redhat.com (Postfix) id EE1977BA56; Wed, 13 Dec 2017 16:10:35 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.40.205.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B1FE87BA3D for ; Wed, 13 Dec 2017 16:10:33 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Wed, 13 Dec 2017 17:10:19 +0100 Message-Id: <20171213161021.28525-3-abologna@redhat.com> In-Reply-To: <20171213161021.28525-1-abologna@redhat.com> References: <20171213161021.28525-1-abologna@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 2/4] util: virhostcpu: factor out frequency parsing 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Wed, 13 Dec 2017 16:10:49 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Bjoern Walk All different architectures use the same copy-pasted code to parse processor frequency information from /proc/cpuinfo. Let's extract that code into a function to avoid repetition. We now also tolerate if the parsing of /proc/cpuinfo is not successful and just report a warning instead of bailing out and abandoning the rest of the CPU information. Reviewed-by: Marc Hartmayer Reviewed-by: Boris Fiuczynski Reviewed-by: Andrea Bolognani Signed-off-by: Bjoern Walk --- Changes from Bjoern's original version: * rename virHostCPUParseFrequencyLine() to virHostCPUParseFrequencyString(). src/util/virhostcpu.c | 136 +++++++++++++++++++++-------------------------= ---- 1 file changed, 57 insertions(+), 79 deletions(-) diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index c485a9721..3091a92c0 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -508,6 +508,61 @@ virHostCPUHasValidSubcoreConfiguration(int threads_per= _subcore) return ret; } =20 +static int +virHostCPUParseFrequencyString(const char *str, + unsigned int *mhz) +{ + char *p; + unsigned int ui; + + while (*str && c_isspace(*str)) + str++; + + if (*str !=3D ':' || !str[1]) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("parsing cpu MHz from cpuinfo")); + return -1; + } + + if (virStrToLong_ui(str + 1, &p, 10, &ui) =3D=3D 0 && + /* Accept trailing fractional part. */ + (*p =3D=3D '\0' || *p =3D=3D '.' || c_isspace(*p))) + *mhz =3D ui; + + return 0; +} + +static int +virHostCPUParseFrequency(FILE *cpuinfo, + virArch arch, + unsigned int *mhz) +{ + const char *prefix =3D NULL; + char line[1024]; + + if (ARCH_IS_X86(arch)) + prefix =3D "cpu MHz"; + else if (ARCH_IS_PPC(arch)) + prefix =3D "clock"; + else if (ARCH_IS_ARM(arch)) + prefix =3D "BogoMIPS"; + + if (!prefix) { + VIR_WARN("Parser for /proc/cpuinfo needs to be adapted for your ar= chitecture"); + return 1; + } + + while (fgets(line, sizeof(line), cpuinfo) !=3D NULL) { + if (!STRPREFIX(line, prefix)) + continue; + + if (virHostCPUParseFrequencyString(line + strlen(prefix), mhz) < 0) + return -1; + } + + return 0; +} + int virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, virArch arch, @@ -520,7 +575,6 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, { virBitmapPtr present_cpus_map =3D NULL; virBitmapPtr online_cpus_map =3D NULL; - char line[1024]; DIR *nodedir =3D NULL; struct dirent *nodedirent =3D NULL; int nodecpus, nodecores, nodesockets, nodethreads, offline =3D 0; @@ -535,84 +589,8 @@ virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, *cpus =3D *nodes =3D *sockets =3D *cores =3D *threads =3D 0; =20 /* Start with parsing CPU clock speed from /proc/cpuinfo */ - while (fgets(line, sizeof(line), cpuinfo) !=3D NULL) { - if (ARCH_IS_X86(arch)) { - char *buf =3D line; - if (STRPREFIX(buf, "cpu MHz")) { - char *p; - unsigned int ui; - - buf +=3D 7; - while (*buf && c_isspace(*buf)) - buf++; - - if (*buf !=3D ':' || !buf[1]) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("parsing cpu MHz from cpuinfo")); - goto cleanup; - } - - if (virStrToLong_ui(buf+1, &p, 10, &ui) =3D=3D 0 && - /* Accept trailing fractional part. */ - (*p =3D=3D '\0' || *p =3D=3D '.' || c_isspace(*p))) - *mhz =3D ui; - } - } else if (ARCH_IS_PPC(arch)) { - char *buf =3D line; - if (STRPREFIX(buf, "clock")) { - char *p; - unsigned int ui; - - buf +=3D 5; - while (*buf && c_isspace(*buf)) - buf++; - - if (*buf !=3D ':' || !buf[1]) { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("parsing cpu MHz from cpuinfo")); - goto cleanup; - } - - if (virStrToLong_ui(buf+1, &p, 10, &ui) =3D=3D 0 && - /* Accept trailing fractional part. */ - (*p =3D=3D '\0' || *p =3D=3D '.' || c_isspace(*p))) - *mhz =3D ui; - /* No other interesting infos are available in /proc/cpuin= fo. - * However, there is a line identifying processor's versio= n, - * identification and machine, but we don't want it to be = caught - * and parsed in next iteration, because it is not in expe= cted - * format and thus lead to error. */ - } - } else if (ARCH_IS_ARM(arch)) { - char *buf =3D line; - if (STRPREFIX(buf, "BogoMIPS")) { - char *p; - unsigned int ui; - - buf +=3D 8; - while (*buf && c_isspace(*buf)) - buf++; - - if (*buf !=3D ':' || !buf[1]) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("parsing cpu MHz from cpuinfo")= ); - goto cleanup; - } - - if (virStrToLong_ui(buf+1, &p, 10, &ui) =3D=3D 0 - /* Accept trailing fractional part. */ - && (*p =3D=3D '\0' || *p =3D=3D '.' || c_isspace(*p))) - *mhz =3D ui; - } - } else if (ARCH_IS_S390(arch)) { - /* s390x has no realistic value for CPU speed, - * assign a value of zero to signify this */ - *mhz =3D 0; - } else { - VIR_WARN("Parser for /proc/cpuinfo needs to be adapted for you= r architecture"); - break; - } - } + if (virHostCPUParseFrequency(cpuinfo, arch, mhz) < 0) + VIR_WARN("Unable to parse processor frequency information from /pr= oc/cpuinfo"); =20 /* Get information about what CPUs are present in the host and what * CPUs are online, so that we don't have to so for each node */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list