diff options
author | Philippe Mathieu-Daudé | 2020-10-12 11:57:44 +0200 |
---|---|---|
committer | Philippe Mathieu-Daudé | 2020-10-16 18:58:10 +0200 |
commit | 709616c713f9471a993ad7d16bce23e8b88ce958 (patch) | |
tree | 0229d332acaa18a1b3baf50137ef0f608f57ca9c /util | |
parent | Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging (diff) | |
download | qemu-709616c713f9471a993ad7d16bce23e8b88ce958.tar.gz qemu-709616c713f9471a993ad7d16bce23e8b88ce958.tar.xz qemu-709616c713f9471a993ad7d16bce23e8b88ce958.zip |
util/cutils: Introduce freq_to_str() to display Hertz units
Introduce freq_to_str() to convert frequency values in human
friendly units using the SI units for Hertz.
Suggested-by: Luc Michel <luc@lmichel.fr>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Reviewed-by: Luc Michel <luc@lmichel.fr>
Message-Id: <20201012095804.3335117-2-f4bug@amsat.org>
Diffstat (limited to 'util')
-rw-r--r-- | util/cutils.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/util/cutils.c b/util/cutils.c index 8da34e04b0..be4e43a9ef 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -885,6 +885,20 @@ 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 *const suffixes[] = { "", "K", "M", "G", "T", "P", "E" }; + double freq = freq_hz; + size_t idx = 0; + + while (freq >= 1000.0 && idx < ARRAY_SIZE(suffixes)) { + freq /= 1000.0; + idx++; + } + + return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]); +} + int qemu_pstrcmp0(const char **str1, const char **str2) { return g_strcmp0(*str1, *str2); |