Skip to contentSkip to conversation

qdev-clock: Minor improvements to the Clock API

PATCH In review Philippe Mathieu-Daudé 4 participants 9 messages Oct 1, 2020 – Oct 10, 2020

lore
Share & tools
lore
#0 lore raw

v2 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

Since v1:
- Avoid out-of-bound array access (Luc)

Patch missing review:
- #1 "Introduce freq_to_str() to display Hertz units"

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         | 14 ++++++++++++++
 4 files changed, 42 insertions(+), 3 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 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         | 14 ++++++++++++++
 2 files changed, 26 insertions(+)
include/qemu/cutils.h +12 -0
index 3a86ec0321..4bbf4834ea 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 +14 -0
index 8da34e04b0..be4e43a9ef 100644
@@ -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);
Signature · 1 line
2.26.2
Reviewed-by on v2 1/3
#2 lore raw

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

Quoted 6 lines · Philippe Mathieu-Daudé

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

Alistair

Quoted 57 lines
---
 include/qemu/cutils.h | 12 ++++++++++++
 util/cutils.c         | 14 ++++++++++++++
 2 files changed, 26 insertions(+)
include/qemu/cutils.h +12 -0
index 3a86ec0321..4bbf4834ea 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 +14 -1
index 8da34e04b0..be4e43a9ef 100644
@@ -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);
--

2.26.2

#3 lore raw

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

In reply to Philippe Mathieu-Daudé
Quoted 43 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         | 14 ++++++++++++++
 2 files changed, 26 insertions(+)
include/qemu/cutils.h +12 -0
index 3a86ec0321..4bbf4834ea 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 +2 -0
index 8da34e04b0..be4e43a9ef 100644
@@ -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)
+{

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

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

The only thing protecting this from out of bounds array access is the fact that UINT64_MAX is smaller than 1000E. I wonder if this causes a Coverity warning.

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
Signature · 1 line
Eduardo
#4 lore raw

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

Quoted 42 lines · Eduardo Habkost
Quoted 41 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         | 14 ++++++++++++++
  2 files changed, 26 insertions(+)
include/qemu/cutils.h +12 -0
index 3a86ec0321..4bbf4834ea 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 8da34e04b0..be4e43a9ef 100644
@@ -885,6 +885,20 @@ char *size_to_str(uint64_t val)
      return g_strdup_printf("%0.3g %sB", (double)val / div, suffixes[i]);
  }
 
Quoted 16 lines

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

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

The only thing protecting this from out of bounds array access is the fact that UINT64_MAX is smaller than 1000E. I wonder if this causes a Coverity warning.

Aren't we protected by the "idx < ARRAY_SIZE(suffixes)" check?

Quoted 10 lines
Quoted 9 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
Reviewed-by on v2 1/3
#5 lore raw

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

In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

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

Quoted 56 lines
---
 include/qemu/cutils.h | 12 ++++++++++++
 util/cutils.c         | 14 ++++++++++++++
 2 files changed, 26 insertions(+)
include/qemu/cutils.h +12 -0
index 3a86ec0321..4bbf4834ea 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 +14 -0
index 8da34e04b0..be4e43a9ef 100644
@@ -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);
Signature · 1 line
2.26.2
#6 lore raw

PATCH v2 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 e9b7228480..a0301cfca8 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 v2 2/3
#7 lore raw

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

Quoted 16 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

dev: xilinx,zynq_slcr, id ""

    clock-in "ps_clk" freq_hz=33.3 MHz
    mmio 00000000f8000000/0000000000001000

Alistair

Quoted 29 lines
---
 qdev-monitor.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
qdev-monitor.c +5 -4
index e9b7228480..a0301cfca8 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 {
--

2.26.2

#8 lore raw

PATCH v2 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 47ecb5b4fa..33bd4a9d52 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