From nobody Thu May 15 05:58:41 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 1513254864318967.0128487790379; Thu, 14 Dec 2017 04:34:24 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A68617EA88; Thu, 14 Dec 2017 12:34:22 +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 7F5FA17D7B; Thu, 14 Dec 2017 12:34:22 +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 423E01801213; Thu, 14 Dec 2017 12:34:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vBECYHue011129 for ; Thu, 14 Dec 2017 07:34:18 -0500 Received: by smtp.corp.redhat.com (Postfix) id EF9897D906; Thu, 14 Dec 2017 12:34:17 +0000 (UTC) Received: from inaba.usersys.redhat.com (ovpn-204-182.brq.redhat.com [10.40.204.182]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3EE906061E for ; Thu, 14 Dec 2017 12:34:14 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Thu, 14 Dec 2017 13:33:58 +0100 Message-Id: <20171214123401.28950-3-abologna@redhat.com> In-Reply-To: <20171214123401.28950-1-abologna@redhat.com> References: <20171214123401.28950-1-abologna@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 2/5] 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 14 Dec 2017 12:34:23 +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 Reviewed-by: John Ferlan --- src/util/virhostcpu.c | 141 ++++++++++++++++++++++------------------------= ---- 1 file changed, 62 insertions(+), 79 deletions(-) diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index c485a9721..d47062013 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -508,6 +508,65 @@ virHostCPUHasValidSubcoreConfiguration(int threads_per= _subcore) return ret; } =20 +static int +virHostCPUParseFrequencyString(const char *str, + const char *prefix, + unsigned int *mhz) +{ + char *p; + unsigned int ui; + + if (!STRPREFIX(str, prefix)) + return 1; + + str +=3D strlen(prefix); + + 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("Your architecture is not supported by the %s parser", + CPUINFO_PATH); + return 1; + } + + while (fgets(line, sizeof(line), cpuinfo) !=3D NULL) { + if (virHostCPUParseFrequencyString(line, prefix, mhz) < 0) + return -1; + } + + return 0; +} + int virHostCPUGetInfoPopulateLinux(FILE *cpuinfo, virArch arch, @@ -520,7 +579,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 +593,9 @@ 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 CPU frequency information from %s", + CPUINFO_PATH); =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