Skip to contentSkip to conversation

include: Auto-generate the sizes lookup table

PATCH Superseded Leonid Bloch 4 participants 11 messages Jan 2, 2019 – Jan 4, 2019

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover include: Auto-generate the sizes lookup table

Following the conversations here:
https://patchwork.kernel.org/patch/10665157
and here:
https://patchwork.kernel.org/patch/10666975

Making the lookup table for power-of-two sizes auto-generated, instead of being hard-coded into the units.h file.

I'm not sure if the changes I've made to Makefile here are "standard". Please correct me if that's not the case.

Sorry it took so much time - I was busy with something completely different.

Regards,
Leonid.

Leonid Bloch (1):
include: Auto-generate the sizes lookup table

 .gitignore           |  1 +
 Makefile             |  5 +++
 block/qcow2.h        |  2 +-
 block/vdi.c          |  1 +
 include/qemu/units.h | 73 --------------------------------------------
 scripts/gen-sizes.sh | 66 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 74 insertions(+), 74 deletions(-)
 create mode 100755 scripts/gen-sizes.sh
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/1 include: Auto-generate the sizes lookup table

The lookup table for power-of-two sizes is now auto-generated during the build, and not hard-coded into the units.h file.

---
 .gitignore           |  1 +
 Makefile             |  5 +++
 block/qcow2.h        |  2 +-
 block/vdi.c          |  1 +
 include/qemu/units.h | 73 --------------------------------------------
 scripts/gen-sizes.sh | 66 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 74 insertions(+), 74 deletions(-)
 create mode 100755 scripts/gen-sizes.sh
.gitignore +1 -0
index 0430257313..5945220303 100644
@@ -59,6 +59,7 @@
 /qemu-version.h
 /qemu-version.h.tmp
 /module_block.h
+/include/qemu/sizes.h
 /scsi/qemu-pr-helper
 /vhost-user-scsi
 /vhost-user-blk
Makefile +5 -0
index dd53965f77..435d92821b 100644
@@ -122,6 +122,8 @@ endif
 
 GENERATED_FILES += module_block.h
 
+GENERATED_FILES += $(SRC_PATH)/include/qemu/sizes.h
+
 TRACE_HEADERS = trace-root.h $(trace-events-subdirs:%=%/trace.h)
 TRACE_SOURCES = trace-root.c $(trace-events-subdirs:%=%/trace.c)
 TRACE_DTRACE =
@@ -499,6 +501,9 @@ ifdef CONFIG_MPATH
 scsi/qemu-pr-helper$(EXESUF): LIBS += -ludev -lmultipath -lmpathpersist
 endif
 
+$(SRC_PATH)/include/qemu/sizes.h: $(SRC_PATH)/scripts/gen-sizes.sh
+	$(call quiet-command,sh $(SRC_PATH)/scripts/gen-sizes.sh $@,"GEN","$@")
+
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$@")
 
block/qcow2.h +1 -1
index a98d24500b..41d14baa17 100644
@@ -27,7 +27,7 @@
 
 #include "crypto/block.h"
 #include "qemu/coroutine.h"
-#include "qemu/units.h"
+#include "qemu/sizes.h"
 
 //#define DEBUG_ALLOC
 //#define DEBUG_ALLOC2
block/vdi.c +1 -0
index 2380daa583..62dafe0cc6 100644
@@ -51,6 +51,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/units.h"
+#include "qemu/sizes.h"
 #include "qapi/error.h"
 #include "qapi/qobject-input-visitor.h"
 #include "qapi/qapi-visit-block-core.h"
include/qemu/units.h +0 -73

77 lines not on this page · raw message

scripts/gen-sizes.sh new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Hi Leonid,

Quoted 3 lines · Leonid Bloch

The lookup table for power-of-two sizes is now auto-generated during the build, and not hard-coded into the units.h file.

This partially reverts commit 540b8492618eb.

Quoted 28 lines
---
 .gitignore           |  1 +
 Makefile             |  5 +++
 block/qcow2.h        |  2 +-
 block/vdi.c          |  1 +
 include/qemu/units.h | 73 --------------------------------------------
 scripts/gen-sizes.sh | 66 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 74 insertions(+), 74 deletions(-)
 create mode 100755 scripts/gen-sizes.sh
.gitignore +1 -0
index 0430257313..5945220303 100644
@@ -59,6 +59,7 @@
 /qemu-version.h
 /qemu-version.h.tmp
 /module_block.h
+/include/qemu/sizes.h
 /scsi/qemu-pr-helper
 /vhost-user-scsi
 /vhost-user-blk
Makefile +0 -0
index dd53965f77..435d92821b 100644

Makefile endif · line 125

Quoted 4 lines
Makefile +1 -0
@@ -122,6 +122,8 @@ endif
 
 GENERATED_FILES += module_block.h
 
+GENERATED_FILES += $(SRC_PATH)/include/qemu/sizes.h

You shouldn't write to SRC_PATH (think of read-only filesystem).

Using the current directory is OK (the build system assumes it is writeable).

So as 'module_block.h', why not simply name it 'lookup_table_sizes.h'?

Actually, we could move those in a generated/ subdirectory, this would make the source file more explicit:

#include "generated/module_block.h"

Kevin, what do you think?

Makefile ifdef CONFIG_MPATH · line 504

Quoted 9 lines
Makefile +2 -0
+
 TRACE_HEADERS = trace-root.h $(trace-events-subdirs:%=%/trace.h)
 TRACE_SOURCES = trace-root.c $(trace-events-subdirs:%=%/trace.c)
 TRACE_DTRACE =
@@ -499,6 +501,9 @@ ifdef CONFIG_MPATH
 scsi/qemu-pr-helper$(EXESUF): LIBS += -ludev -lmultipath -lmpathpersist
 endif
 
+$(SRC_PATH)/include/qemu/sizes.h: $(SRC_PATH)/scripts/gen-sizes.sh

lookup_table_sizes.h: $(SRC_PATH)/scripts/gen-sizes.sh

Quoted 10 lines
Makefile +2 -0
+	$(call quiet-command,sh $(SRC_PATH)/scripts/gen-sizes.sh $@,"GEN","$@")
+
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
 	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$@")
 
block/qcow2.h +0 -0
index a98d24500b..41d14baa17 100644

block/qcow2.h line 30

Quoted 5 lines
block/qcow2.h +1 -1
@@ -27,7 +27,7 @@
 
 #include "crypto/block.h"
 #include "qemu/coroutine.h"
-#include "qemu/units.h"
+#include "qemu/sizes.h"

#include "lookup_table_sizes.h"

block/vdi.c line 54

Quoted 12 lines
block/qcow2.h +0 -0
 //#define DEBUG_ALLOC
 //#define DEBUG_ALLOC2
block/vdi.c +1 -0
index 2380daa583..62dafe0cc6 100644
@@ -51,6 +51,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/units.h"
+#include "qemu/sizes.h"

#include "lookup_table_sizes.h"

Quoted 142 lines
block/vdi.c +0 -0
 #include "qapi/error.h"
 #include "qapi/qobject-input-visitor.h"
 #include "qapi/qapi-visit-block-core.h"
include/qemu/units.h +0 -73
index 1c959d182e..692db3fbb2 100644
@@ -17,77 +17,4 @@
 #define PiB     (INT64_C(1) << 50)
 #define EiB     (INT64_C(1) << 60)
 
-/*
- * The following lookup table is intended to be used when a literal string of
- * the number of bytes is required (for example if it needs to be stringified).
- * It can also be used for generic shortcuts of power-of-two sizes.
- * This table is generated using the AWK script below:
- *
- *  BEGIN {
- *      suffix="KMGTPE";
- *      for(i=10; i<64; i++) {
- *          val=2**i;
- *          s=substr(suffix, int(i/10), 1);
- *          n=2**(i%10);
- *          pad=21-int(log(n)/log(10));
- *          printf("#define S_%d%siB %*d\n", n, s, pad, val);
- *      }
- *  }
- */
-
-#define S_1KiB                  1024
-#define S_2KiB                  2048
-#define S_4KiB                  4096
-#define S_8KiB                  8192
-#define S_16KiB                16384
-#define S_32KiB                32768
-#define S_64KiB                65536
-#define S_128KiB              131072
-#define S_256KiB              262144
-#define S_512KiB              524288
-#define S_1MiB               1048576
-#define S_2MiB               2097152
-#define S_4MiB               4194304
-#define S_8MiB               8388608
-#define S_16MiB             16777216
-#define S_32MiB             33554432
-#define S_64MiB             67108864
-#define S_128MiB           134217728
-#define S_256MiB           268435456
-#define S_512MiB           536870912
-#define S_1GiB            1073741824
-#define S_2GiB            2147483648
-#define S_4GiB            4294967296
-#define S_8GiB            8589934592
-#define S_16GiB          17179869184
-#define S_32GiB          34359738368
-#define S_64GiB          68719476736
-#define S_128GiB        137438953472
-#define S_256GiB        274877906944
-#define S_512GiB        549755813888
-#define S_1TiB         1099511627776
-#define S_2TiB         2199023255552
-#define S_4TiB         4398046511104
-#define S_8TiB         8796093022208
-#define S_16TiB       17592186044416
-#define S_32TiB       35184372088832
-#define S_64TiB       70368744177664
-#define S_128TiB     140737488355328
-#define S_256TiB     281474976710656
-#define S_512TiB     562949953421312
-#define S_1PiB      1125899906842624
-#define S_2PiB      2251799813685248
-#define S_4PiB      4503599627370496
-#define S_8PiB      9007199254740992
-#define S_16PiB    18014398509481984
-#define S_32PiB    36028797018963968
-#define S_64PiB    72057594037927936
-#define S_128PiB  144115188075855872
-#define S_256PiB  288230376151711744
-#define S_512PiB  576460752303423488
-#define S_1EiB   1152921504606846976
-#define S_2EiB   2305843009213693952
-#define S_4EiB   4611686018427387904
-#define S_8EiB   9223372036854775808
-
 #endif
scripts/gen-sizes.sh new file +51 -0
new file mode 100755
index 0000000000..e388164c32
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+size_suffix() {
+    case ${1} in
+        1)
+            printf "KiB"
+            ;;
+        2)
+            printf "MiB"
+            ;;
+        3)
+            printf "GiB"
+            ;;
+        4)
+            printf "TiB"
+            ;;
+        5)
+            printf "PiB"
+            ;;
+        6)
+            printf "EiB"
+            ;;
+    esac
+}
+
+print_sizes() {
+    local p=10
+    while [ ${p} -lt 64 ]
+    do
+        local pad=' '
+        local n=$((p % 10))
+        n=$((1 << n))
+        [ $((n / 100)) -eq 0 ] && pad='  '
+        [ $((n / 10)) -eq 0 ] && pad='   '
+        local suff=$((p / 10))
+        printf "#define S_%u%s%s%20u\n" ${n} "$(size_suffix ${suff})" \
+            "${pad}" $((1 << p))
+        p=$((p + 1))
+    done
+}
+
+print_header() {
+    cat <<EOF
+/*
+ * The following lookup table is intended to be used when a literal string of
+ * the number of bytes is required (for example if it needs to be stringified).
+ * It can also be used for generic shortcuts of power-of-two sizes.
+ * This table is generated automatically during the build.
+ *
+ * Author:
+ *   Leonid Bloch  <[email protected]>

scripts/gen-sizes.sh line 61

Quoted 10 lines
scripts/gen-sizes.sh new file +10 -0
+ */
+
+#ifndef QEMU_SIZES_H
+#define QEMU_SIZES_H
+
+EOF
+}
+
+print_footer() {
+    printf "\n#endif\n"
       printf "\n#endif /* QEMU_SIZES_H */\n"

scripts/gen-sizes.sh line 66

Quoted 6 lines
scripts/gen-sizes.sh new file +5 -0
+}
+
+print_header  > "${1}"
+print_sizes  >> "${1}"
+print_footer >> "${1}"

Regards,

Phil.

#3 lore raw

Hi Phil,

Thanks for your review! Very valid points. All taken, except the naming (I think "pow2_sizes.h" would be better than "lookup_table_sizes.h", no?) and the "generated" directory - I agree that it's a good idea, but I think it requires a separate patch which moves the generated files there.

Regards,
Leonid.

Quoted 10 lines · Philippe Mathieu-Daudé

So as 'module_block.h', why not simply name it 'lookup_table_sizes.h'?

Actually, we could move those in a generated/ subdirectory, this would make the source file more explicit:

#include "generated/module_block.h"

Kevin, what do you think?

#4 lore raw
In reply to Leonid Bloch
Quoted 5 lines · Leonid Bloch
scripts/gen-sizes.sh new file +5 -0
+print_sizes() {
+    local p=10
+    while [ ${p} -lt 64 ]
+    do
+        local pad=' '

scripts/gen-sizes.sh line 40

Quoted 10 lines
scripts/gen-sizes.sh new file +10 -0
+        local n=$((p % 10))
+        n=$((1 << n))
+        [ $((n / 100)) -eq 0 ] && pad='  '
+        [ $((n / 10)) -eq 0 ] && pad='   '
+        local suff=$((p / 10))
+        printf "#define S_%u%s%s%20u\n" ${n} "$(size_suffix ${suff})" \
+            "${pad}" $((1 << p))
+        p=$((p + 1))
+    done
+}

I have to say that I'm not very convinced of the benefits of replacing a set of trivial numeric macros with a longer and harder to read shell script accompanied by changes to the build system.

Berto

#5 lore raw

Hi Berto,

Quoted 12 lines · Alberto Garcia
Quoted 11 lines · Leonid Bloch
scripts/gen-sizes.sh new file +11 -0
+print_sizes() {
+    local p=10
+    while [ ${p} -lt 64 ]
+    do
+        local pad=' '
+        local n=$((p % 10))
+        n=$((1 << n))
+        [ $((n / 100)) -eq 0 ] && pad='  '
+        [ $((n / 10)) -eq 0 ] && pad='   '
+        local suff=$((p / 10))
+        printf "#define S_%u%s%s%20u\n" ${n} "$(size_suffix ${suff})" \
Quoted 8 lines

scripts/gen-sizes.sh line 40

Quoted 4 lines
scripts/gen-sizes.sh new file +4 -0
+            "${pad}" $((1 << p))
+        p=$((p + 1))
+    done
+}

I have to say that I'm not very convinced of the benefits of replacing a set of trivial numeric macros with a longer and harder to read shell script accompanied by changes to the build system.

I think that the benefit is that the script is easily verifiable, whereas if someone would like to verify the table, they will need to generate it themselves. Also, this table is automatically generated anyway, so it only makes sense to generate it during the build.

Leonid.

Quoted 3 lines

Berto

#6 lore raw
Quoted 8 lines · Leonid Bloch
Quoted 3 lines

I have to say that I'm not very convinced of the benefits of replacing a set of trivial numeric macros with a longer and harder to read shell script accompanied by changes to the build system.

I think that the benefit is that the script is easily verifiable, whereas if someone would like to verify the table, they will need to generate it themselves. Also, this table is automatically generated anyway, so it only makes sense to generate it during the build.

But no one is submitting patches to active modify the table. The work has already been done once to generate it, and the reviewers have already verified it; at this point, no further changes are likely to happen (other than my pipe dream of entirely getting rid of the table in favor of using runtime generation of human-friendly default strings is added to QemuOpts instead). If the table were not already in git, then generating it at build time might make sense; but at this stage in the game, you're slowing down every build to regenerate something that is already correct.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#7 lore raw

Hi,

Quoted 11 lines · Eric Blake
Quoted 8 lines · Leonid Bloch
Quoted 3 lines

I have to say that I'm not very convinced of the benefits of replacing a set of trivial numeric macros with a longer and harder to read shell script accompanied by changes to the build system.

I think that the benefit is that the script is easily verifiable, whereas if someone would like to verify the table, they will need to generate it themselves. Also, this table is automatically generated anyway, so it only makes sense to generate it during the build.

Quoted 10 lines

But no one is submitting patches to active modify the table. The work has already been done once to generate it, and the reviewers have already verified it; at this point, no further changes are likely to happen (other than my pipe dream of entirely getting rid of the table in favor of using runtime generation of human-friendly default strings is added to QemuOpts instead). If the table were not already in git, then generating it at build time might make sense; but at this stage in the game, you're slowing down every build to regenerate something that is already correct.

OK, that's a good point. But still, I think that you agree that it would be more correct to generate it during the build, right? So now it is there already and it works. But isn't it worth it to make it more correct?

Leonid.

#8 lore raw
Quoted 25 lines · Leonid Bloch

Hi,

Quoted 11 lines · Eric Blake
Quoted 8 lines · Leonid Bloch
Quoted 3 lines

I have to say that I'm not very convinced of the benefits of replacing a set of trivial numeric macros with a longer and harder to read shell script accompanied by changes to the build system.

I think that the benefit is that the script is easily verifiable, whereas if someone would like to verify the table, they will need to generate it themselves. Also, this table is automatically generated anyway, so it only makes sense to generate it during the build.

Quoted 10 lines

But no one is submitting patches to active modify the table. The work has already been done once to generate it, and the reviewers have already verified it; at this point, no further changes are likely to happen (other than my pipe dream of entirely getting rid of the table in favor of using runtime generation of human-friendly default strings is added to QemuOpts instead). If the table were not already in git, then generating it at build time might make sense; but at this stage in the game, you're slowing down every build to regenerate something that is already correct.

Quoted 3 lines

OK, that's a good point. But still, I think that you agree that it would be more correct to generate it during the build, right? So now it is there already and it works. But isn't it worth it to make it more correct?

I said "might make sense", not "I would have done it that way". I also don't see how a generated table is "more correct" than a hand-written one - they are both equally correct if all values in the table match what they are documented to provide.

In my view, code generators make sense when used on code that is expected to change over time (a good example is QAPI because we add new commands every release; other places might be a generator to help deal with syscall handlers since newer kernels can add a syscall; or even the fact that we have used some powerful GNU make textual processing to make it easy to add files to particular subsets of the build with as few lines edited as possible), where the goal is that the generator gives you both a compact representation that is easier to edit, and that the expansion from the generator ensures that repetitive boilerplate is formed without typos. In short, if a generator results in a net reduction in lines of edited source in relation to the lines it produces, AND if the source that gets regenerated is likely to change, then it makes total sense to spend time on the generator. But when the amount of effort to write a generator costs as much as just hard-coding the list outright, especially when the list is not going to change (there really aren't any other powers of 2 within 64 bits), I'm not sure a generator adds any value. Unfortunately, your patch diffstat of:

Quoted 8 lines
 .gitignore           |  1 +
 Makefile             |  5 +++
 block/qcow2.h        |  2 +-
 block/vdi.c          |  1 +
 include/qemu/units.h | 73 --------------------------------------------
 scripts/gen-sizes.sh | 66 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 74 insertions(+), 74 deletions(-)
 create mode 100755 scripts/gen-sizes.sh

is a wash (you did not result in any fewer lines in the codebase overall), and the commit message did not do a good job saying WHY we need it (you said WHAT it does - avoiding a hard-coded list - but not WHY the hardcoded list is so bad that a generator would be better). So I'm not seeing the point of this patch.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#9 lore raw
Quoted 10 lines · Eric Blake

In my view, code generators make sense when used on code that is expected to change over time (a good example is QAPI because we add new commands every release; other places might be a generator to help deal with syscall handlers since newer kernels can add a syscall; or even the fact that we have used some powerful GNU make textual processing to make it easy to add files to particular subsets of the build with as few lines edited as possible), where the goal is that the generator gives you both a compact representation that is easier to edit, and that the expansion from the generator ensures that repetitive boilerplate is formed without typos. In short, if a

Quoted 8 lines

generator results in a net reduction in lines of edited source in relation to the lines it produces, AND if the source that gets regenerated is likely to change, then it makes total sense to spend time on the generator. But when the amount of effort to write a generator costs as much as just hard-coding the list outright, especially when the list is not going to change (there really aren't any other powers of 2 within 64 bits), I'm not sure a generator adds any value.

I agree with Eric.

Berto

#10 lore raw
Quoted 21 lines · Alberto Garcia
Quoted 10 lines · Eric Blake

In my view, code generators make sense when used on code that is expected to change over time (a good example is QAPI because we add new commands every release; other places might be a generator to help deal with syscall handlers since newer kernels can add a syscall; or even the fact that we have used some powerful GNU make textual processing to make it easy to add files to particular subsets of the build with as few lines edited as possible), where the goal is that the generator gives you both a compact representation that is easier to edit, and that the expansion from the generator ensures that repetitive boilerplate is formed without typos. In short, if a

Quoted 8 lines

generator results in a net reduction in lines of edited source in relation to the lines it produces, AND if the source that gets regenerated is likely to change, then it makes total sense to spend time on the generator. But when the amount of effort to write a generator costs as much as just hard-coding the list outright, especially when the list is not going to change (there really aren't any other powers of 2 within 64 bits), I'm not sure a generator adds any value.

Quoted 1 line

I agree with Eric.

Fine with me. I just thought that in the previous conversation
(https://patchwork.kernel.org/patch/10666975/#22302435) we have agreed that I'll send this patch. I've sent v2 already with Phil's suggestions included, please feel free to pull it if desired.

Leonid.

Quoted 3 lines

Berto