Received: from [140.186.70.92] (port=34175 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P600S-0006TA-39 for qemu-devel@nongnu.org; Wed, 13 Oct 2010 08:00:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P5xQn-0001kA-4v for qemu-devel@nongnu.org; Wed, 13 Oct 2010 05:15:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:9253) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P5xQm-0001k6-V9 for qemu-devel@nongnu.org; Wed, 13 Oct 2010 05:15:29 -0400 From: Markus Armbruster Subject: Re: [Qemu-devel] [PATCH v2 2/7] cutils: Add bytes_to_str() to format byte values References: <1286552914-27014-1-git-send-email-stefanha@linux.vnet.ibm.com> <1286552914-27014-3-git-send-email-stefanha@linux.vnet.ibm.com> Date: Wed, 13 Oct 2010 11:15:08 +0200 In-Reply-To: <1286552914-27014-3-git-send-email-stefanha@linux.vnet.ibm.com> (Stefan Hajnoczi's message of "Fri, 8 Oct 2010 16:48:29 +0100") Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: Kevin Wolf , Anthony Liguori , Avi Kivity , qemu-devel@nongnu.org, Christoph Hellwig 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? 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). Suggest to return the length of the resulting string, as returned by snprintf(). > +} > diff --git a/qemu-common.h b/qemu-common.h > index e0ca398..80ae834 100644 > --- a/qemu-common.h > +++ b/qemu-common.h > @@ -154,6 +154,7 @@ int qemu_fls(int i); > int qemu_fdatasync(int fd); > int fcntl_setfl(int fd, int flag); > int get_bits_from_size(size_t size); > +void bytes_to_str(char *buffer, size_t buffer_len, uint64_t size); > > /* path.c */ > void init_paths(const char *prefix);