qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
To: qemu-devel@nongnu.org
Cc: "Damien Hedde" <damien.hedde@greensocs.com>,
	"Edgar E . Iglesias" <edgar.iglesias@xilinx.com>,
	"Luc Michel" <luc@lmichel.fr>,
	"Eduardo Habkost" <ehabkost@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Havard Skinnemoen" <hskinnemoen@google.com>,
	"Philippe Mathieu-Daudé" <f4bug@amsat.org>,
	"Tyrone Ting" <kfting@nuvoton.com>,
	"Alistair Francis" <alistair.francis@wdc.com>
Subject: [PATCH 1/3] util/cutils: Introduce freq_to_str() to display Hertz units
Date: Sun, 27 Sep 2020 11:08:18 +0200	[thread overview]
Message-ID: <20200927090820.61859-2-f4bug@amsat.org> (raw)
In-Reply-To: <20200927090820.61859-1-f4bug@amsat.org>

Introduce freq_to_str() to convert frequency values in human
friendly units using the SI units for Hertz.

Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/qemu/cutils.h | 12 ++++++++++++
 util/cutils.c         | 10 ++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index eb59852dfdf..0186c846e9c 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -158,6 +158,18 @@ int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
 
 char *size_to_str(uint64_t val);
 
+/**
+ * freq_to_str:
+ * @freq_hz: frequency to stringify
+ *
+ * Return human readable string for frequency @freq_hz.
+ * Use SI units like KHz, MHz, and so forth.
+ *
+ * The caller is responsible for releasing the value returned with g_free()
+ * after use.
+ */
+char *freq_to_str(uint64_t freq_hz);
+
 /* used to print char* safely */
 #define STR_OR_NULL(str) ((str) ? (str) : "null")
 
diff --git a/util/cutils.c b/util/cutils.c
index 36ce712271f..dab837fd8b8 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -885,6 +885,16 @@ char *size_to_str(uint64_t val)
     return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]);
 }
 
+char *freq_to_str(uint64_t freq_hz)
+{
+    static const char *suffixes[] = { "", "K", "M", "G", "T", "P", "E" };
+    unsigned unit_index = log10(freq_hz) / 3;
+
+    return g_strdup_printf("%0.3g %sHz",
+                           freq_hz / pow(10.0, unit_index * 3.0),
+                           suffixes[unit_index]);
+}
+
 int qemu_pstrcmp0(const char **str1, const char **str2)
 {
     return g_strcmp0(*str1, *str2);
-- 
2.26.2



  reply	other threads:[~2020-09-27  9:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-27  9:08 [PATCH 0/3] qdev-clock: Minor improvements to the Clock API Philippe Mathieu-Daudé
2020-09-27  9:08 ` Philippe Mathieu-Daudé [this message]
2020-09-28  7:50   ` [PATCH 1/3] util/cutils: Introduce freq_to_str() to display Hertz units Luc Michel
2020-09-28 13:04     ` Philippe Mathieu-Daudé
2020-09-27  9:08 ` [PATCH 2/3] qdev-monitor: Display frequencies scaled to SI unit Philippe Mathieu-Daudé
2020-09-28  7:52   ` Luc Michel
2020-09-28  8:45     ` Damien Hedde
2020-09-27  9:08 ` [PATCH 3/3] hw/qdev-clock: Display error hint when clock is missing from device Philippe Mathieu-Daudé
2020-09-28  7:53   ` Luc Michel
2020-09-28 10:45     ` Damien Hedde
2020-09-28 10:48       ` Edgar E. Iglesias

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=20200927090820.61859-2-f4bug@amsat.org \
    --to=f4bug@amsat.org \
    --cc=alistair.francis@wdc.com \
    --cc=damien.hedde@greensocs.com \
    --cc=edgar.iglesias@xilinx.com \
    --cc=ehabkost@redhat.com \
    --cc=hskinnemoen@google.com \
    --cc=kfting@nuvoton.com \
    --cc=luc@lmichel.fr \
    --cc=peter.maydell@linaro.org \
    --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).