[libnbd PATCH 0/2] Improve nbdinfo display of block constraints
Based on Laszlo's approval of my idea here: https://listman.redhat.com/archives/libguestfs/2023-September/032661.html but as I would like to resync human-size.h back to nbdkit, I'm reluctant to apply patch 1 this until I get Rich's consent to relicensing (this email serves as my consent for my contribution here): https://listman.redhat.com/archives/libguestfs/2023-October/032755.html Eric Blake (2): utils: Slightly simplify human_size() info: Show human sizes for block_size values common/include/human-size.h | 14 ++++++-------- info/show.c | 26 +++++++++++++++++++------- 2 files changed, 25 insertions(+), 15 deletions(-) -- 2.41.0
Use an array of characters instead of strings for less .data storage. Merge the loop conditional for fewer lines of code. Signed-off-by: Eric Blake <eblake@redhat.com> --- common/include/human-size.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/common/include/human-size.h b/common/include/human-size.h index 47729c3c..8b1e0132 100644 --- a/common/include/human-size.h +++ b/common/include/human-size.h @@ -159,7 +159,7 @@ human_size_parse (const char *str, static inline char * human_size (char *buf, uint64_t bytes, bool *human) { - static const char ext[][2] = { "E", "P", "T", "G", "M", "K", "" }; + static const char ext[] = "EPTGMK"; size_t i; if (buf == NULL) { @@ -170,18 +170,16 @@ human_size (char *buf, uint64_t bytes, bool *human) /* Work out which extension to use, if any. */ i = 6; - if (bytes != 0) { - while ((bytes & 1023) == 0) { - bytes >>= 10; - i--; - } + while (bytes && (bytes & 1023) == 0) { + bytes >>= 10; + i--; } /* Set the flag to true if we're going to add a human-readable extension. */ if (human) - *human = ext[i][0] != '\0'; + *human = ext[i] != '\0'; - snprintf (buf, HUMAN_SIZE_LONGEST, "%" PRIu64 "%s", bytes, ext[i]); + snprintf (buf, HUMAN_SIZE_LONGEST, "%" PRIu64 "%.1s", bytes, &ext[i]); return buf; } -- 2.41.0
On Fri, Oct 06, 2023 at 10:18:08AM -0500, Eric Blake wrote:
Use an array of characters instead of strings for less .data storage. Merge the loop conditional for fewer lines of code.
Signed-off-by: Eric Blake <eblake@redhat.com> --- common/include/human-size.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/common/include/human-size.h b/common/include/human-size.h index 47729c3c..8b1e0132 100644 --- a/common/include/human-size.h +++ b/common/include/human-size.h @@ -159,7 +159,7 @@ human_size_parse (const char *str, static inline char * human_size (char *buf, uint64_t bytes, bool *human) { - static const char ext[][2] = { "E", "P", "T", "G", "M", "K", "" }; + static const char ext[] = "EPTGMK"; size_t i;
if (buf == NULL) { @@ -170,18 +170,16 @@ human_size (char *buf, uint64_t bytes, bool *human)
/* Work out which extension to use, if any. */ i = 6; - if (bytes != 0) { - while ((bytes & 1023) == 0) { - bytes >>= 10; - i--; - } + while (bytes && (bytes & 1023) == 0) { + bytes >>= 10; + i--; }
/* Set the flag to true if we're going to add a human-readable extension. */ if (human) - *human = ext[i][0] != '\0'; + *human = ext[i] != '\0';
- snprintf (buf, HUMAN_SIZE_LONGEST, "%" PRIu64 "%s", bytes, ext[i]); + snprintf (buf, HUMAN_SIZE_LONGEST, "%" PRIu64 "%.1s", bytes, &ext[i]); return buf; }
Reviewed-by: Richard W.M. Jones <rjones@redhat.com> -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Adding a human-readable size for block constraints is useful. For: $ ./run nbdinfo -- [ nbdkit memory \ --filter=blocksize-policy blocksize-preferred=32k 1M ] | grep pref this changes pre-patch: block_size_preferred: 32768 to post-patch: block_size_preferred: 32768 (32K) Signed-off-by: Eric Blake <eblake@redhat.com> --- info/show.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/info/show.c b/info/show.c index 6aeffb54..ac483f34 100644 --- a/info/show.c +++ b/info/show.c @@ -35,6 +35,7 @@ #include "nbdinfo.h" static void show_boolean (const char *name, bool cond); +static void show_size (const char *name, int64_t size); static int collect_context (void *opaque, const char *name); static char *get_content (struct nbd_handle *, int64_t size); @@ -181,13 +182,9 @@ show_one_export (struct nbd_handle *nbd, const char *desc, show_boolean ("can_trim", can_trim); if (can_zero >= 0) show_boolean ("can_zero", can_zero); - if (block_minimum > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_minimum", block_minimum); - if (block_preferred > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_preferred", - block_preferred); - if (block_maximum > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_maximum", block_maximum); + show_size ("block_size_minimum", block_minimum); + show_size ("block_size_preferred", block_preferred); + show_size ("block_size_maximum", block_maximum); } else { if (first) @@ -304,6 +301,21 @@ show_boolean (const char *name, bool cond) ansi_restore (fp); } +/* Used for displaying sizes in non-JSON output. */ +void show_size (const char *name, int64_t size) +{ + char size_str[HUMAN_SIZE_LONGEST]; + bool human_size_flag = false; + + if (size > 0) { + human_size (size_str, size, &human_size_flag); + if (human_size_flag) + fprintf (fp, "\t%s: %" PRId64 " (%s)\n", name, size, size_str); + else + fprintf (fp, "\t%s: %" PRId64 "\n", name, size); + } +} + static int collect_context (void *opaque, const char *name) { -- 2.41.0
On Fri, Oct 06, 2023 at 10:18:09AM -0500, Eric Blake wrote:
Adding a human-readable size for block constraints is useful. For:
$ ./run nbdinfo -- [ nbdkit memory \ --filter=blocksize-policy blocksize-preferred=32k 1M ] | grep pref
this changes pre-patch: block_size_preferred: 32768 to post-patch: block_size_preferred: 32768 (32K)
I think info/nbdinfo.pod needs to be updated. Rich.
Signed-off-by: Eric Blake <eblake@redhat.com> --- info/show.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/info/show.c b/info/show.c index 6aeffb54..ac483f34 100644 --- a/info/show.c +++ b/info/show.c @@ -35,6 +35,7 @@ #include "nbdinfo.h"
static void show_boolean (const char *name, bool cond); +static void show_size (const char *name, int64_t size); static int collect_context (void *opaque, const char *name); static char *get_content (struct nbd_handle *, int64_t size);
@@ -181,13 +182,9 @@ show_one_export (struct nbd_handle *nbd, const char *desc, show_boolean ("can_trim", can_trim); if (can_zero >= 0) show_boolean ("can_zero", can_zero); - if (block_minimum > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_minimum", block_minimum); - if (block_preferred > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_preferred", - block_preferred); - if (block_maximum > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_maximum", block_maximum); + show_size ("block_size_minimum", block_minimum); + show_size ("block_size_preferred", block_preferred); + show_size ("block_size_maximum", block_maximum); } else { if (first) @@ -304,6 +301,21 @@ show_boolean (const char *name, bool cond) ansi_restore (fp); }
+/* Used for displaying sizes in non-JSON output. */ +void show_size (const char *name, int64_t size) +{ + char size_str[HUMAN_SIZE_LONGEST]; + bool human_size_flag = false; + + if (size > 0) { + human_size (size_str, size, &human_size_flag); + if (human_size_flag) + fprintf (fp, "\t%s: %" PRId64 " (%s)\n", name, size, size_str); + else + fprintf (fp, "\t%s: %" PRId64 "\n", name, size); + } +} + static int collect_context (void *opaque, const char *name) { -- 2.41.0
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com nbdkit - Flexible, fast NBD server with plugins https://gitlab.com/nbdkit/nbdkit
On 10/7/23 11:27, Richard W.M. Jones wrote:
On Fri, Oct 06, 2023 at 10:18:09AM -0500, Eric Blake wrote:
Adding a human-readable size for block constraints is useful. For:
$ ./run nbdinfo -- [ nbdkit memory \ --filter=blocksize-policy blocksize-preferred=32k 1M ] | grep pref
this changes pre-patch: block_size_preferred: 32768 to post-patch: block_size_preferred: 32768 (32K)
I think info/nbdinfo.pod needs to be updated.
Good catch! With that: series Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Rich.
Signed-off-by: Eric Blake <eblake@redhat.com> --- info/show.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-)
diff --git a/info/show.c b/info/show.c index 6aeffb54..ac483f34 100644 --- a/info/show.c +++ b/info/show.c @@ -35,6 +35,7 @@ #include "nbdinfo.h"
static void show_boolean (const char *name, bool cond); +static void show_size (const char *name, int64_t size); static int collect_context (void *opaque, const char *name); static char *get_content (struct nbd_handle *, int64_t size);
@@ -181,13 +182,9 @@ show_one_export (struct nbd_handle *nbd, const char *desc, show_boolean ("can_trim", can_trim); if (can_zero >= 0) show_boolean ("can_zero", can_zero); - if (block_minimum > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_minimum", block_minimum); - if (block_preferred > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_preferred", - block_preferred); - if (block_maximum > 0) - fprintf (fp, "\t%s: %" PRId64 "\n", "block_size_maximum", block_maximum); + show_size ("block_size_minimum", block_minimum); + show_size ("block_size_preferred", block_preferred); + show_size ("block_size_maximum", block_maximum); } else { if (first) @@ -304,6 +301,21 @@ show_boolean (const char *name, bool cond) ansi_restore (fp); }
+/* Used for displaying sizes in non-JSON output. */ +void show_size (const char *name, int64_t size) +{ + char size_str[HUMAN_SIZE_LONGEST]; + bool human_size_flag = false; + + if (size > 0) { + human_size (size_str, size, &human_size_flag); + if (human_size_flag) + fprintf (fp, "\t%s: %" PRId64 " (%s)\n", name, size, size_str); + else + fprintf (fp, "\t%s: %" PRId64 "\n", name, size); + } +} + static int collect_context (void *opaque, const char *name) { -- 2.41.0
_______________________________________________ Libguestfs mailing list Libguestfs@redhat.com https://listman.redhat.com/mailman/listinfo/libguestfs
On Sun, Oct 08, 2023 at 11:34:05AM +0200, Laszlo Ersek wrote:
On 10/7/23 11:27, Richard W.M. Jones wrote:
On Fri, Oct 06, 2023 at 10:18:09AM -0500, Eric Blake wrote:
Adding a human-readable size for block constraints is useful. For:
$ ./run nbdinfo -- [ nbdkit memory \ --filter=blocksize-policy blocksize-preferred=32k 1M ] | grep pref
this changes pre-patch: block_size_preferred: 32768 to post-patch: block_size_preferred: 32768 (32K)
I think info/nbdinfo.pod needs to be updated.
Good catch!
With that:
series Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Fixed, and series pushed as 42a85181..d4e8bb2d -- Eric Blake, Principal Software Engineer Red Hat, Inc. Virtualization: qemu.org | libguestfs.org
participants (3)
-
Eric Blake -
Laszlo Ersek -
Richard W.M. Jones