From nobody Thu May 15 05:54:27 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 1513010467623967.4363981844178; Mon, 11 Dec 2017 08:41:07 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A4EAB800A4; Mon, 11 Dec 2017 16:41:05 +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 7599C2D34D; Mon, 11 Dec 2017 16:41:05 +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 3A33C1801212; Mon, 11 Dec 2017 16:41:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vBBGeq92002237 for ; Mon, 11 Dec 2017 11:40:52 -0500 Received: by smtp.corp.redhat.com (Postfix) id 658117C611; Mon, 11 Dec 2017 16:40:52 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.40.205.47]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7D386605E2 for ; Mon, 11 Dec 2017 16:40:51 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Mon, 11 Dec 2017 17:40:33 +0100 Message-Id: <20171211164036.12142-4-abologna@redhat.com> In-Reply-To: <20171211164036.12142-1-abologna@redhat.com> References: <20171211164036.12142-1-abologna@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/6] util: Introduce virHostCPUGetInfoParseCPUFrequency() 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 11 Dec 2017 16:41:06 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The algorithm used to extract the CPU frequency from /proc/cpuinfo is the same regardless of architecture, the only difference being the label used to identify the relevant field. Factor the parsing code out to a new helper and use it to implement virHostCPUGetInfoParseCPUInfo(), thus removing some duplication. Signed-off-by: Andrea Bolognani Reviewed-by: Boris Fiuczynski Reviewed-by: Marc Hartmayer --- src/util/virhostcpu.c | 105 +++++++++++++++++++---------------------------= ---- 1 file changed, 40 insertions(+), 65 deletions(-) diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c index 4d5c56659..85803d527 100644 --- a/src/util/virhostcpu.c +++ b/src/util/virhostcpu.c @@ -509,6 +509,40 @@ virHostCPUHasValidSubcoreConfiguration(int threads_per= _subcore) } =20 =20 +static int +virHostCPUGetInfoParseCPUFrequency(const char *buf, + const char *label, + unsigned int *mhz) +{ + int ret =3D -1; + + if (STRPREFIX(buf, label)) { + char *p; + unsigned int ui; + + buf +=3D strlen(label); + 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; + } + + ret =3D 0; + + cleanup: + return ret; +} + + static int virHostCPUGetInfoParseCPUInfo(FILE *cpuinfo, virArch arch, @@ -521,73 +555,14 @@ virHostCPUGetInfoParseCPUInfo(FILE *cpuinfo, =20 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; - } + if (virHostCPUGetInfoParseCPUFrequency(line, "cpu MHz", mhz) <= 0) + goto cleanup; } 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. */ - } + if (virHostCPUGetInfoParseCPUFrequency(line, "clock", mhz) < 0) + goto cleanup; } 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; - } + if (virHostCPUGetInfoParseCPUFrequency(line, "BogoMIPS", mhz) = < 0) + goto cleanup; } else if (ARCH_IS_S390(arch)) { /* s390x has no realistic value for CPU speed, * assign a value of zero to signify this */ --=20 2.14.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list