The migration code has a function that converts a time value (us) to a
string with the proper suffix. Move it to cutils since it's generic
enough that it could be reused.
Suggested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
include/qemu/cutils.h | 1 +
migration/migration-hmp-cmds.c | 17 ++---------------
util/cutils.c | 13 +++++++++++++
3 files changed, 16 insertions(+), 15 deletions(-)
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index 36c68ce86c..7621726621 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -171,6 +171,7 @@ int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);
int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
char *size_to_str(uint64_t val);
+char *time_us_to_str(uint64_t val);
/**
* freq_to_str:
diff --git a/migration/migration-hmp-cmds.c b/migration/migration-hmp-cmds.c
index bb954881d7..1706f3a0f7 100644
--- a/migration/migration-hmp-cmds.c
+++ b/migration/migration-hmp-cmds.c
@@ -52,19 +52,6 @@ static void migration_global_dump(Monitor *mon)
ms->clear_bitmap_shift);
}
-static const gchar *format_time_str(uint64_t us)
-{
- const char *units[] = {"us", "ms", "sec"};
- int index = 0;
-
- while (us > 1000 && index + 1 < ARRAY_SIZE(units)) {
- us /= 1000;
- index++;
- }
-
- return g_strdup_printf("%"PRIu64" %s", us, units[index]);
-}
-
static void migration_dump_blocktime(Monitor *mon, MigrationInfo *info)
{
if (info->has_postcopy_blocktime) {
@@ -121,8 +108,8 @@ static void migration_dump_blocktime(Monitor *mon, MigrationInfo *info)
monitor_printf(mon, "Postcopy Latency Distribution:\n");
while (item) {
- g_autofree const gchar *from = format_time_str(1UL << count);
- g_autofree const gchar *to = format_time_str(1UL << (count + 1));
+ g_autofree const gchar *from = time_us_to_str(1UL << count);
+ g_autofree const gchar *to = time_us_to_str(1UL << (count + 1));
monitor_printf(mon, " [ %8s - %8s ]: %10"PRIu64"\n",
from, to, item->value);
diff --git a/util/cutils.c b/util/cutils.c
index 9803f11a59..023793211a 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -1004,6 +1004,19 @@ char *freq_to_str(uint64_t freq_hz)
return g_strdup_printf("%0.3g %sHz", freq, si_prefix(exp10));
}
+char *time_us_to_str(uint64_t us)
+{
+ const char *units[] = {"us", "ms", "sec"};
+ int index = 0;
+
+ while (us > 1000 && index + 1 < ARRAY_SIZE(units)) {
+ us /= 1000;
+ index++;
+ }
+
+ return g_strdup_printf("%"PRIu64" %s", us, units[index]);
+}
+
int qemu_pstrcmp0(const char **str1, const char **str2)
{
return g_strcmp0(*str1, *str2);
--
2.35.3
On 15/7/25 14:45, Fabiano Rosas wrote:
> The migration code has a function that converts a time value (us) to a
> string with the proper suffix. Move it to cutils since it's generic
> enough that it could be reused.
>
> Suggested-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> ---
> include/qemu/cutils.h | 1 +
> migration/migration-hmp-cmds.c | 17 ++---------------
> util/cutils.c | 13 +++++++++++++
> 3 files changed, 16 insertions(+), 15 deletions(-)
>
> diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
> index 36c68ce86c..7621726621 100644
> --- a/include/qemu/cutils.h
> +++ b/include/qemu/cutils.h
> @@ -171,6 +171,7 @@ int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);
> int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *result);
>
> char *size_to_str(uint64_t val);
> +char *time_us_to_str(uint64_t val);
s/val/us/
>
> /**
> * freq_to_str:
> diff --git a/migration/migration-hmp-cmds.c b/migration/migration-hmp-cmds.c
> index bb954881d7..1706f3a0f7 100644
> --- a/migration/migration-hmp-cmds.c
> +++ b/migration/migration-hmp-cmds.c
> @@ -52,19 +52,6 @@ static void migration_global_dump(Monitor *mon)
> ms->clear_bitmap_shift);
> }
>
> -static const gchar *format_time_str(uint64_t us)
> -{
> - const char *units[] = {"us", "ms", "sec"};
> - int index = 0;
> -
> - while (us > 1000 && index + 1 < ARRAY_SIZE(units)) {
> - us /= 1000;
> - index++;
> - }
> -
> - return g_strdup_printf("%"PRIu64" %s", us, units[index]);
> -}
> -
> static void migration_dump_blocktime(Monitor *mon, MigrationInfo *info)
> {
> if (info->has_postcopy_blocktime) {
> @@ -121,8 +108,8 @@ static void migration_dump_blocktime(Monitor *mon, MigrationInfo *info)
> monitor_printf(mon, "Postcopy Latency Distribution:\n");
>
> while (item) {
> - g_autofree const gchar *from = format_time_str(1UL << count);
> - g_autofree const gchar *to = format_time_str(1UL << (count + 1));
> + g_autofree const gchar *from = time_us_to_str(1UL << count);
> + g_autofree const gchar *to = time_us_to_str(1UL << (count + 1));
>
> monitor_printf(mon, " [ %8s - %8s ]: %10"PRIu64"\n",
> from, to, item->value);
> diff --git a/util/cutils.c b/util/cutils.c
> index 9803f11a59..023793211a 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -1004,6 +1004,19 @@ char *freq_to_str(uint64_t freq_hz)
> return g_strdup_printf("%0.3g %sHz", freq, si_prefix(exp10));
> }
>
> +char *time_us_to_str(uint64_t us)
> +{
> + const char *units[] = {"us", "ms", "sec"};
(static)
> + int index = 0;
> +
> + while (us > 1000 && index + 1 < ARRAY_SIZE(units)) {
> + us /= 1000;
> + index++;
> + }
> +
> + return g_strdup_printf("%"PRIu64" %s", us, units[index]);
> +}
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Thanks!
© 2016 - 2025 Red Hat, Inc.