From nobody Thu May 15 06:55:16 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 151056304913955.402735528108224; Mon, 13 Nov 2017 00:50:49 -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 CAE1813AAC; Mon, 13 Nov 2017 08:50:47 +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 A620078405; Mon, 13 Nov 2017 08:50:47 +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 7083A1800BD3; Mon, 13 Nov 2017 08:50:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id vAD8oik9016243 for ; Mon, 13 Nov 2017 03:50:44 -0500 Received: by smtp.corp.redhat.com (Postfix) id DA1846252B; Mon, 13 Nov 2017 08:50:44 +0000 (UTC) Received: from caroline.localdomain (unknown [10.43.2.67]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AD91562531 for ; Mon, 13 Nov 2017 08:50:42 +0000 (UTC) Received: from caroline.brq.redhat.com (caroline.brq.redhat.com [127.0.0.1]) by caroline.localdomain (Postfix) with ESMTP id 3395B123A76 for ; Mon, 13 Nov 2017 09:50:41 +0100 (CET) From: Martin Kletzander To: libvir-list@redhat.com Date: Mon, 13 Nov 2017 09:50:17 +0100 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 02/21] util: Introduce virPrettySize 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.29]); Mon, 13 Nov 2017 08:50:48 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" We can't output better memory sizes if we want to be compatible with libvirt older than the one which introduced /memory/unit, but for new things we can= just output nicer capacity to the user if available. And this function enables = that. Signed-off-by: Martin Kletzander Reviewed-by: John Ferlan --- src/libvirt_private.syms | 1 + src/util/virutil.c | 50 ++++++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virutil.h | 3 +++ 3 files changed, 54 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 36cd5b55b249..d4bae6150bb8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2954,6 +2954,7 @@ virParseNumber; virParseOwnershipIds; virParseVersionString; virPipeReadUntilEOF; +virPrettySize; virScaleInteger; virSetBlocking; virSetCloseExec; diff --git a/src/util/virutil.c b/src/util/virutil.c index 170e921920ef..dcfb65262aff 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -1993,3 +1993,53 @@ virMemoryMaxValue(bool capped) else return LLONG_MAX; } + + +/** + * virPrettySize + * + * @val: Value in bytes to be shortened + * @unit: unit to be used + * + * Similar to vshPrettyCapacity, but operates on integers and not doubles + * + * Returns shortened value that can be used with @unit. + */ +unsigned long long +virPrettySize(unsigned long long val, const char **unit) +{ + unsigned long long limit =3D 1024; + + if (val % limit || val =3D=3D 0) { + *unit =3D "B"; + return val; + } + limit *=3D 1024; + if (val % limit) { + *unit =3D "KiB"; + return val / (limit / 1024); + } + limit *=3D 1024; + if (val % limit) { + *unit =3D "MiB"; + return val / (limit / 1024); + } + limit *=3D 1024; + if (val % limit) { + *unit =3D "GiB"; + return val / (limit / 1024); + } + limit *=3D 1024; + if (val % limit) { + *unit =3D "TiB"; + return val / (limit / 1024); + } + limit *=3D 1024; + if (val % limit) { + *unit =3D "PiB"; + return val / (limit / 1024); + } + limit *=3D 1024; + *unit =3D "EiB"; + return val / (limit / 1024); +} diff --git a/src/util/virutil.h b/src/util/virutil.h index ff89d1aaaa5f..72e35fc9a607 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -222,4 +222,7 @@ unsigned long long virMemoryMaxValue(bool ulong) ATTRIB= UTE_NOINLINE; # define VIR_ASSIGN_IS_OVERFLOW(lvalue, rvalue) \ (((lvalue) =3D (rvalue)) !=3D (rvalue)) =20 +unsigned long long +virPrettySize(unsigned long long val, const char **unit); + #endif /* __VIR_UTIL_H__ */ --=20 2.15.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list