From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=34694 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P5zub-0006gG-Oz for qemu-devel@nongnu.org; Wed, 13 Oct 2010 07:54:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P5yWU-0006vn-NH for qemu-devel@nongnu.org; Wed, 13 Oct 2010 06:25:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:24383) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P5yWU-0006va-HF for qemu-devel@nongnu.org; Wed, 13 Oct 2010 06:25:26 -0400 Message-ID: <4CB58902.50808@redhat.com> Date: Wed, 13 Oct 2010 12:25:06 +0200 From: Avi Kivity MIME-Version: 1.0 References: <1286552914-27014-1-git-send-email-stefanha@linux.vnet.ibm.com> <1286552914-27014-3-git-send-email-stefanha@linux.vnet.ibm.com> In-Reply-To: <1286552914-27014-3-git-send-email-stefanha@linux.vnet.ibm.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] Re: [PATCH v2 2/7] cutils: Add bytes_to_str() to format byte values List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: Kevin Wolf , Anthony Liguori , qemu-devel@nongnu.org, Christoph Hellwig On 10/08/2010 05:48 PM, Stefan Hajnoczi wrote: > From: Anthony Liguori > > This common function converts byte counts to human-readable strings with > proper units. > > Signed-off-by: Anthony Liguori > Signed-off-by: Stefan Hajnoczi > --- > cutils.c | 15 +++++++++++++++ > qemu-common.h | 1 + > 2 files changed, 16 insertions(+), 0 deletions(-) > > diff --git a/cutils.c b/cutils.c > index 6c32198..5041203 100644 > --- a/cutils.c > +++ b/cutils.c > @@ -301,3 +301,18 @@ int get_bits_from_size(size_t size) > return __builtin_ctzl(size); > #endif > } > + > +void bytes_to_str(char *buffer, size_t buffer_len, uint64_t size) > +{ > + if (size< (1ULL<< 10)) { > + snprintf(buffer, buffer_len, "%" PRIu64 " byte(s)", size); > + } else if (size< (1ULL<< 20)) { > + snprintf(buffer, buffer_len, "%" PRIu64 " KB(s)", size>> 10); > + } else if (size< (1ULL<< 30)) { > + snprintf(buffer, buffer_len, "%" PRIu64 " MB(s)", size>> 20); > + } else if (size< (1ULL<< 40)) { > + snprintf(buffer, buffer_len, "%" PRIu64 " GB(s)", size>> 30); > + } else { > + snprintf(buffer, buffer_len, "%" PRIu64 " TB(s)", size>> 40); > + } > +} This will show 1.5GB as 1GB. Need either floating point with a couple of digits of precision, or show 1.5GB as 1500MB. It also misuses SI prefixes. 1 GB means 10^9 bytes, not 2^30 bytes (with a common exception for RAM which is usually packaged in 1.125 times some power of two). -- I have a truly marvellous patch that fixes the bug which this signature is too narrow to contain.