Skip to contentSkip to conversation

qdev-clock: Minor improvements to the Clock API

PATCH Superseded Philippe Mathieu-Daudé 4 participants 11 messages Sep 27, 2020 – Sep 28, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover qdev-clock: Minor improvements to the Clock API

Handy patches while using the Clock API:
- display frequency in SI scaled unit
- display error hint when device lack clock support

Philippe Mathieu-Daudé (3):
util/cutils: Introduce freq_to_str() to display Hertz units
qdev-monitor: Display frequencies scaled to SI unit hw/qdev-clock: Display error hint when clock is missing from device

 include/qemu/cutils.h | 12 ++++++++++++
 hw/core/qdev-clock.c  | 11 +++++++++++
 qdev-monitor.c        |  8 +++++---
 util/cutils.c         | 10 ++++++++++
 4 files changed, 38 insertions(+), 3 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/3 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.

---
 include/qemu/cutils.h | 12 ++++++++++++
 util/cutils.c         | 10 ++++++++++
 2 files changed, 22 insertions(+)
include/qemu/cutils.h +12 -0
index eb59852dfdf..0186c846e9c 100644
@@ -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")
 
util/cutils.c +10 -0
index 36ce712271f..dab837fd8b8 100644
@@ -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);
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 1/3] util/cutils: Introduce freq_to_str() to display Hertz units

Hi Philippe,

Quoted 38 lines · Philippe Mathieu-Daudé

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

---
 include/qemu/cutils.h | 12 ++++++++++++
 util/cutils.c         | 10 ++++++++++
 2 files changed, 22 insertions(+)
include/qemu/cutils.h +12 -0
index eb59852dfdf..0186c846e9c 100644
@@ -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")
 
util/cutils.c +0 -0
index 36ce712271f..dab837fd8b8 100644
@@ -885,6 +885,16 @@ char *size_to_str(uint64_t val)
     return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]);

util/cutils.c char *size_to_str(uint64_t val) · line 895

Quoted 10 lines
util/cutils.c +8 -0
 }
 
+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]);

You could end up going out of your 'suffixes' array if freq_hz is very high. Also, to avoid the complexity of log10/pow, maybe something like:

    double freq = freq_hz;
    size_t idx = 0;

    while (freq >= 1000.0 && idx < ARRAY_LENGTH(suffixes)) {
        freq /= 1000.0;
        idx++;
    }

    return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]);

is enough?

Signature · 1 line
Luc
Quoted 8 lines
util/cutils.c +2 -0
+}
+
 int qemu_pstrcmp0(const char **str1, const char **str2)
 {
     return g_strcmp0(*str1, *str2);
Signature · 1 line
2.26.2
#3 lore raw

Re: [PATCH 1/3] util/cutils: Introduce freq_to_str() to display Hertz units

Quoted 43 lines · Luc Michel

Hi Philippe,

Quoted 40 lines · Philippe Mathieu-Daudé

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

---
 include/qemu/cutils.h | 12 ++++++++++++
 util/cutils.c         | 10 ++++++++++
 2 files changed, 22 insertions(+)
include/qemu/cutils.h +12 -0
index eb59852dfdf..0186c846e9c 100644
@@ -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")
 
util/cutils.c +0 -0
index 36ce712271f..dab837fd8b8 100644
@@ -885,6 +885,16 @@ char *size_to_str(uint64_t val)
     return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]);
 }
 
Quoted 11 lines

util/cutils.c char *size_to_str(uint64_t val) · line 895

Quoted 8 lines
util/cutils.c +8 -0
+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]);

You could end up going out of your 'suffixes' array if freq_hz is very high.

Oh, good point.

Quoted 9 lines

Also, to avoid the complexity of log10/pow, maybe something like:

    double freq = freq_hz;
    size_t idx = 0;

    while (freq >= 1000.0 && idx < ARRAY_LENGTH(suffixes)) {
        freq /= 1000.0;
        idx++;
    }

KISS, I like it :)

Quoted 5 lines
    return g_strdup_printf("%0.3g %sHz", freq, suffixes[idx]);

is enough?

I'll respin, thanks!

Phil.

#4 lore raw

PATCH 2/3 qdev-monitor: Display frequencies scaled to SI unit

In reply to Philippe Mathieu-Daudé

Since commit 9f2ff99c7f2 ("qdev-monitor: print the device's clock with info qtree") we can display the clock frequencies in the monitor. Use the recently introduced freq_to_str() to display the frequencies using the closest SI unit (human friendlier).

Before:

(qemu) info qtree
[...]
dev: xilinx,zynq_slcr, id ""

    clock-in "ps_clk" freq_hz=3.333333e+07
    mmio 00000000f8000000/0000000000001000

After:

dev: xilinx,zynq_slcr, id ""

    clock-in "ps_clk" freq_hz=33.3 MHz
    mmio 00000000f8000000/0000000000001000
---
 qdev-monitor.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qdev-monitor.c +5 -3
index e9b7228480d..a0301cfca81 100644
@@ -747,11 +747,13 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
         }
     }
     QLIST_FOREACH(ncl, &dev->clocks, node) {
-        qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n",
+        g_autofree char *freq = NULL;
+
+        freq = freq_to_str(clock_get_hz(ncl->clock));
+        qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
                     ncl->output ? "out" : "in",
                     ncl->alias ? " (alias)" : "",
-                    ncl->name,
-                    CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl->clock)));
+                    ncl->name, freq);
     }
     class = object_get_class(OBJECT(dev));
     do {
Signature · 1 line
2.26.2
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] qdev-monitor: Display frequencies scaled to SI unit

Quoted 13 lines · Philippe Mathieu-Daudé

Since commit 9f2ff99c7f2 ("qdev-monitor: print the device's clock with info qtree") we can display the clock frequencies in the monitor. Use the recently introduced freq_to_str() to display the frequencies using the closest SI unit (human friendlier).

Before:

(qemu) info qtree
[...]
dev: xilinx,zynq_slcr, id ""

    clock-in "ps_clk" freq_hz=3.333333e+07
    mmio 00000000f8000000/0000000000001000
Quoted 7 lines

After:

dev: xilinx,zynq_slcr, id ""

    clock-in "ps_clk" freq_hz=33.3 MHz
    mmio 00000000f8000000/0000000000001000
Quoted 28 lines
---
 qdev-monitor.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qdev-monitor.c +5 -3
index e9b7228480d..a0301cfca81 100644
@@ -747,11 +747,13 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
         }
     }
     QLIST_FOREACH(ncl, &dev->clocks, node) {
-        qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n",
+        g_autofree char *freq = NULL;
+
+        freq = freq_to_str(clock_get_hz(ncl->clock));
+        qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
                     ncl->output ? "out" : "in",
                     ncl->alias ? " (alias)" : "",
-                    ncl->name,
-                    CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl->clock)));
+                    ncl->name, freq);
     }
     class = object_get_class(OBJECT(dev));
     do {
Signature · 1 line
2.26.2
Reviewed-by
#6 lore raw

Re: [PATCH 2/3] qdev-monitor: Display frequencies scaled to SI unit

Quoted 16 lines · Luc Michel
Quoted 15 lines · Philippe Mathieu-Daudé

Since commit 9f2ff99c7f2 ("qdev-monitor: print the device's clock with info qtree") we can display the clock frequencies in the monitor. Use the recently introduced freq_to_str() to display the frequencies using the closest SI unit (human friendlier).

Before:

(qemu) info qtree
[...]
dev: xilinx,zynq_slcr, id ""

    clock-in "ps_clk" freq_hz=3.333333e+07
    mmio 00000000f8000000/0000000000001000

After:

Quoted 7 lines
Quoted 5 lines

dev: xilinx,zynq_slcr, id ""

    clock-in "ps_clk" freq_hz=33.3 MHz
    mmio 00000000f8000000/0000000000001000
Quoted 29 lines
Quoted 29 lines
---
 qdev-monitor.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qdev-monitor.c +5 -3
index e9b7228480d..a0301cfca81 100644
@@ -747,11 +747,13 @@ static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
         }
     }
     QLIST_FOREACH(ncl, &dev->clocks, node) {
-        qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n",
+        g_autofree char *freq = NULL;
+
+        freq = freq_to_str(clock_get_hz(ncl->clock));
+        qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
                     ncl->output ? "out" : "in",
                     ncl->alias ? " (alias)" : "",
-                    ncl->name,
-                    CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl->clock)));
+                    ncl->name, freq);
     }
     class = object_get_class(OBJECT(dev));
     do {
Signature · 1 line
2.26.2
#7 lore raw

PATCH 3/3 hw/qdev-clock: Display error hint when clock is missing from device

In reply to Philippe Mathieu-Daudé

Instead of directly aborting, display a hint to help the developer figure out the problem (likely trying to connect a clock to a device pre-dating the Clock API, thus not expecting clocks).

---
 hw/core/qdev-clock.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/core/qdev-clock.c +11 -0
index 47ecb5b4fae..33bd4a9d520 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "hw/qdev-clock.h"
 #include "hw/qdev-core.h"
 #include "qapi/error.h"
@@ -153,6 +154,11 @@ Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-in '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(!ncl->output);
 
     return ncl->clock;
@@ -165,6 +171,11 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-out '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(ncl->output);
 
     return ncl->clock;
Signature · 1 line
2.26.2
Reviewed-by on v1 3/3
#8 lore raw

Re: [PATCH 3/3] hw/qdev-clock: Display error hint when clock is missing from device

Quoted 5 lines · Philippe Mathieu-Daudé

Instead of directly aborting, display a hint to help the developer figure out the problem (likely trying to connect a clock to a device pre-dating the Clock API, thus not expecting clocks).

Quoted 43 lines
---
 hw/core/qdev-clock.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/core/qdev-clock.c +11 -0
index 47ecb5b4fae..33bd4a9d520 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "hw/qdev-clock.h"
 #include "hw/qdev-core.h"
 #include "qapi/error.h"
@@ -153,6 +154,11 @@ Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-in '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(!ncl->output);
 
     return ncl->clock;
@@ -165,6 +171,11 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-out '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(ncl->output);
 
     return ncl->clock;
Signature · 1 line
2.26.2
Reviewed-by
#9 lore raw

Re: [PATCH 3/3] hw/qdev-clock: Display error hint when clock is missing from device

Quoted 8 lines · Luc Michel
Quoted 5 lines · Philippe Mathieu-Daudé

Instead of directly aborting, display a hint to help the developer figure out the problem (likely trying to connect a clock to a device pre-dating the Clock API, thus not expecting clocks).

Quoted 44 lines
Quoted 44 lines
---
 hw/core/qdev-clock.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/core/qdev-clock.c +11 -0
index 47ecb5b4fae..33bd4a9d520 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "hw/qdev-clock.h"
 #include "hw/qdev-core.h"
 #include "qapi/error.h"
@@ -153,6 +154,11 @@ Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-in '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(!ncl->output);
 
     return ncl->clock;
@@ -165,6 +171,11 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-out '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(ncl->output);
 
     return ncl->clock;
Signature · 1 line
2.26.2
Reviewed-by
#10 lore raw

Re: [PATCH 3/3] hw/qdev-clock: Display error hint when clock is missing from device

Quoted 8 lines · Damien Hedde
Quoted 5 lines · Luc Michel
Quoted 4 lines · Philippe Mathieu-Daudé

Instead of directly aborting, display a hint to help the developer figure out the problem (likely trying to connect a clock to a device pre-dating the Clock API, thus not expecting clocks).

Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Quoted 45 lines
Quoted 45 lines
Quoted 45 lines

---
 hw/core/qdev-clock.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
hw/core/qdev-clock.c +11 -0
index 47ecb5b4fae..33bd4a9d520 100644
@@ -12,6 +12,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/error-report.h"
 #include "hw/qdev-clock.h"
 #include "hw/qdev-core.h"
 #include "qapi/error.h"
@@ -153,6 +154,11 @@ Clock *qdev_get_clock_in(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-in '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(!ncl->output);
 
     return ncl->clock;
@@ -165,6 +171,11 @@ Clock *qdev_get_clock_out(DeviceState *dev, const char *name)
     assert(name);
 
     ncl = qdev_get_clocklist(dev, name);
+    if (!ncl) {
+        error_report("can not find clock-out '%s' for device type '%s'",
+                     name, object_get_typename(OBJECT(dev)));
+        abort();
+    }
     assert(ncl->output);
 
     return ncl->clock;
Signature · 1 line
2.26.2