qdev-clock: Minor improvements to the Clock API
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
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
@@ -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
@@ -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
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.Suggested-by Luc Michel <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 57 lines
--- include/qemu/cutils.h | 12 ++++++++++++ util/cutils.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+)include/qemu/cutils.h +12
-0@@ -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@@ -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
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.Suggested-by Luc Michel <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu/cutils.h | 12 ++++++++++++ util/cutils.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+)include/qemu/cutils.h +12
-0@@ -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@@ -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
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.Suggested-by Luc Michel <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu/cutils.h | 12 ++++++++++++ util/cutils.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+)include/qemu/cutils.h +12
-0@@ -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@@ -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_MAXis 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
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.Suggested-by Luc Michel <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 56 lines
--- include/qemu/cutils.h | 12 ++++++++++++ util/cutils.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+)include/qemu/cutils.h +12
-0@@ -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@@ -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
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/0000000000001000After:
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
@@ -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
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 introducedfreq_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/0000000000001000After:
Quoted 7 lines
dev: xilinx,zynq_slcr, id ""
clock-in "ps_clk" freq_hz=33.3 MHz mmio 00000000f8000000/0000000000001000Reviewed-by Luc Michel <[email protected]>Reviewed-by Damien Hedde <[email protected]>Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Alistair
Quoted 29 lines
--- qdev-monitor.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)qdev-monitor.c +5
-4@@ -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
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
@@ -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