clock: Define and use new clock_display_freq()

It's common to want to print a human-readable indication of a clock's
frequency. Provide a utility function in the clock API to return a
string which is a displayable representation of the frequency,
and use it in qdev-monitor.c.

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

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Tested-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Luc Michel <luc@lmichel.fr>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20201215150929.30311-5-peter.maydell@linaro.org>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
This commit is contained in:
Peter Maydell 2020-12-15 15:09:29 +00:00 committed by Philippe Mathieu-Daudé
parent de6a65f11d
commit b7cd9c1e84
4 changed files with 26 additions and 3 deletions

View File

@ -267,6 +267,11 @@ Here is an example:
clock_get(dev->my_clk_input));
}
If you are only interested in the frequency for displaying it to
humans (for instance in debugging), use ``clock_display_freq()``,
which returns a prettified string-representation, e.g. "33.3 MHz".
The caller must free the string with g_free() after use.
Calculating expiry deadlines
----------------------------

View File

@ -12,6 +12,7 @@
*/
#include "qemu/osdep.h"
#include "qemu/cutils.h"
#include "hw/clock.h"
#include "trace.h"
@ -111,6 +112,11 @@ static void clock_disconnect(Clock *clk)
QLIST_REMOVE(clk, sibling);
}
char *clock_display_freq(Clock *clk)
{
return freq_to_str(clock_get_hz(clk));
}
static void clock_initfn(Object *obj)
{
Clock *clk = CLOCK(obj);

View File

@ -264,4 +264,16 @@ static inline bool clock_is_enabled(const Clock *clk)
return clock_get(clk) != 0;
}
/**
* clock_display_freq: return human-readable representation of clock frequency
* @clk: clock
*
* Return a string which has a human-readable representation of the
* clock's frequency, e.g. "33.3 MHz". This is intended for debug
* and display purposes.
*
* The caller is responsible for freeing the string with g_free().
*/
char *clock_display_freq(Clock *clk);
#endif /* QEMU_HW_CLOCK_H */

View File

@ -736,11 +736,11 @@ 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_str = clock_display_freq(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_str);
}
class = object_get_class(OBJECT(dev));
do {