From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=33875 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P5zrk-0006J4-Fo for qemu-devel@nongnu.org; Wed, 13 Oct 2010 07:51:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P5z2j-00042Y-K3 for qemu-devel@nongnu.org; Wed, 13 Oct 2010 06:58:46 -0400 Received: from mtagate7.de.ibm.com ([195.212.17.167]:50956) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P5z2j-00042F-BC for qemu-devel@nongnu.org; Wed, 13 Oct 2010 06:58:45 -0400 Received: from d12nrmr1607.megacenter.de.ibm.com (d12nrmr1607.megacenter.de.ibm.com [9.149.167.49]) by mtagate7.de.ibm.com (8.13.1/8.13.1) with ESMTP id o9DAwfBB004320 for ; Wed, 13 Oct 2010 10:58:41 GMT Received: from d12av03.megacenter.de.ibm.com (d12av03.megacenter.de.ibm.com [9.149.165.213]) by d12nrmr1607.megacenter.de.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o9DAwfP63985538 for ; Wed, 13 Oct 2010 12:58:41 +0200 Received: from d12av03.megacenter.de.ibm.com (loopback [127.0.0.1]) by d12av03.megacenter.de.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o9DAwenq028851 for ; Wed, 13 Oct 2010 12:58:41 +0200 Date: Wed, 13 Oct 2010 11:58:40 +0100 From: Stefan Hajnoczi Subject: Re: [Qemu-devel] [PATCH v2 2/7] cutils: Add bytes_to_str() to format byte values Message-ID: <20101013105839.GA8998@stefan-thinkpad.transitives.com> References: <1286552914-27014-1-git-send-email-stefanha@linux.vnet.ibm.com> <1286552914-27014-3-git-send-email-stefanha@linux.vnet.ibm.com> <4CB57BCA.4030006@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4CB57BCA.4030006@redhat.com> List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: Anthony Liguori , Avi Kivity , Christoph Hellwig , Markus Armbruster , qemu-devel@nongnu.org On Wed, Oct 13, 2010 at 11:28:42AM +0200, Kevin Wolf wrote: > Am 13.10.2010 11:15, schrieb Markus Armbruster: > > Stefan Hajnoczi writes: > > > >> 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) > > > > Why is the size argument uint64_t and not size_t? > > size_t would be rather small for disk images on 32 bit hosts. > > > The name bytes_to_str() suggests you're formatting a sequence of bytes. > > What about sztostr()? Matches Jes's strtosz(). > > > >> +{ > >> + 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); > >> + } > > > > Sure you want to truncate rather than round? > > > > The "(s)" sure are ugly. We don't usually add plural-s after a unit: we > > write ten milliseconds as 10ms, not 10ms(s). > > I suggest taking the output format from cvtstr in cmd.c, so that qemu-io > output stays the same when switching to a common function (it says "10 > KiB" and "1 bytes"). I have a patch to replace bytes_to_str() with cvtstr(). We should probably rename cvtstr() to sztostr() as suggested because that name is more descriptive. Stefan