diff mbox series

[v7,7/9] qemu/units: add SI decimal units

Message ID 20190618114328.55249-8-vsementsov@virtuozzo.com (mailing list archive)
State New, archived
Headers show
Series NBD reconnect | expand

Commit Message

Vladimir Sementsov-Ogievskiy June 18, 2019, 11:43 a.m. UTC
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
 include/qemu/units.h | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Eric Blake Aug. 9, 2019, 3:39 p.m. UTC | #1
On 6/18/19 6:43 AM, Vladimir Sementsov-Ogievskiy wrote:
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
>  include/qemu/units.h | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/include/qemu/units.h b/include/qemu/units.h
> index 692db3fbb2..52ccc7445c 100644
> --- a/include/qemu/units.h
> +++ b/include/qemu/units.h
> @@ -17,4 +17,11 @@
>  #define PiB     (INT64_C(1) << 50)
>  #define EiB     (INT64_C(1) << 60)
>  
> +#define SI_k 1000LL
> +#define SI_M 1000000LL
> +#define SI_G 1000000000LL
> +#define SI_T 1000000000000LL
> +#define SI_P 1000000000000000LL
> +#define SI_E 1000000000000000000LL

Looks correct; and it's the sort of thing that if we do once here, we
don't have to repeat elsewhere. But bike-shedding a bit, would it be any
easier to read as:

#define SI_k 1000LL
#define SI_M (1000LL * 1000)
#define SI_G (1000LL * 1000 * 1000)
...

or even:

#define SI_k 1000LL
#define SI_M (SI_k * 1000)
#define SI_G (SI_M * 1000)
...

Also, would it be worth swapping out existing constants in the code base
that should instead be using these macros, so that they actually have a
use and so that we can see whether using them adds legibility?

For example, block/nvme.c, block/qapi.c, block/sheepdog.c, blockdev.c,
util/async.c, util/oslib-win32.c, util/qemu-thread-posix.c,
util/qemu-timer.c all seem to be dealing with conversions between
seconds and subdivisions thereof, where constants 1000000 or larger are
in use and could be rewritten with these.
Peter Maydell Aug. 9, 2019, 3:56 p.m. UTC | #2
On Fri, 9 Aug 2019 at 16:39, Eric Blake <eblake@redhat.com> wrote:
> Also, would it be worth swapping out existing constants in the code base
> that should instead be using these macros, so that they actually have a
> use and so that we can see whether using them adds legibility?
>
> For example, block/nvme.c, block/qapi.c, block/sheepdog.c, blockdev.c,
> util/async.c, util/oslib-win32.c, util/qemu-thread-posix.c,
> util/qemu-timer.c all seem to be dealing with conversions between
> seconds and subdivisions thereof, where constants 1000000 or larger are
> in use and could be rewritten with these.

I'm not sure that it would be more readable to replace
1000000000LL with SI_G -- I would tend to assume the latter
would be 2^30. Using "1000LL * 1000 * 1000" inline at
the point of use, or better still abstracting any particular use
into something more semantically meaningful as we already
do with NANOSECONDS_PER_SECOND, would be my personal preference.

thanks
-- PMM
diff mbox series

Patch

diff --git a/include/qemu/units.h b/include/qemu/units.h
index 692db3fbb2..52ccc7445c 100644
--- a/include/qemu/units.h
+++ b/include/qemu/units.h
@@ -17,4 +17,11 @@ 
 #define PiB     (INT64_C(1) << 50)
 #define EiB     (INT64_C(1) << 60)
 
+#define SI_k 1000LL
+#define SI_M 1000000LL
+#define SI_G 1000000000LL
+#define SI_T 1000000000000LL
+#define SI_P 1000000000000000LL
+#define SI_E 1000000000000000000LL
+
 #endif