From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Markus Armbruster <armbru@redhat.com>,
peterx@redhat.com, "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Subject: [Qemu-devel] [PATCH v5 2/3] utils: provide size_to_str()
Date: Tue, 9 May 2017 19:25:04 +0800 [thread overview]
Message-ID: <1494329105-4411-3-git-send-email-peterx@redhat.com> (raw)
In-Reply-To: <1494329105-4411-1-git-send-email-peterx@redhat.com>
I stole the algorithm from print_type_size(). I didn't generalize it
since that's using [KM...]iB while here we need [KM...]B to finally
be able to stands for page sizes (and even more general).
Signed-off-by: Peter Xu <peterx@redhat.com>
---
include/qemu-common.h | 1 +
util/cutils.c | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/include/qemu-common.h b/include/qemu-common.h
index d218821..d7d0448 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -145,6 +145,7 @@ void qemu_hexdump(const char *buf, FILE *fp, const char *prefix, size_t size);
int parse_debug_env(const char *name, int max, int initial);
const char *qemu_ether_ntoa(const MACAddr *mac);
+char *size_to_str(double val);
void page_size_init(void);
/* returns non-zero if dump is in progress, otherwise zero is
diff --git a/util/cutils.c b/util/cutils.c
index 50ad179..5aaf370 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -619,3 +619,29 @@ const char *qemu_ether_ntoa(const MACAddr *mac)
return ret;
}
+
+/*
+ * Sample output:
+ *
+ * 1 -> "1 B"
+ * 528 -> "0.516 KB"
+ * 4096 -> "4 KB"
+ * 2402958 -> "2.29 MB"
+ * 1073741824 -> "1 GB"
+ *
+ * Please free the buffer after use.
+ */
+char *size_to_str(double val)
+{
+ static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' };
+ unsigned long div;
+ int i;
+
+ frexp(val, &i);
+ i /= 10;
+ assert(i < sizeof(suffixes));
+ div = 1ULL << (i * 10);
+
+ return g_strdup_printf("%0.3g %c%s", val / div,
+ suffixes[i], i ? "B" : "");
+}
--
2.7.4
next prev parent reply other threads:[~2017-05-09 11:25 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-09 11:25 [Qemu-devel] [PATCH v5 0/3] ramblock: add hmp command "info ramblock" Peter Xu
2017-05-09 11:25 ` [Qemu-devel] [PATCH v5 1/3] ramblock: add RAMBLOCK_FOREACH() Peter Xu
2017-05-09 11:25 ` Peter Xu [this message]
2017-05-09 14:50 ` [Qemu-devel] [PATCH v5 2/3] utils: provide size_to_str() Markus Armbruster
2017-05-10 3:32 ` Peter Xu
2017-05-09 11:25 ` [Qemu-devel] [PATCH v5 3/3] ramblock: add new hmp command "info ramblock" Peter Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1494329105-4411-3-git-send-email-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).