Skip to contentSkip to conversation

chardev: Add -qmp-pretty

PATCH Superseded Max Reitz 2 participants 11 messages Nov 11, 2014 – Nov 12, 2014

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

cover chardev: Add -qmp-pretty

This series does not add new functionality. Adding a QMP monitor with prettily formatted JSON output can be done as follows:

$ qemu -chardev stdio,id=mon0 -mon chardev=mon0,mode=control,pretty=on

However, this is rather cumbersome, so this series (its first patch) adds a shortcut in the form of the new command line option -qmp-pretty.

Since the argument given to a monitor command line option (such as -qmp) is parsed depending on its prefix and probably also depending on the current phase of the moon, this is cleaner than trying to add a "switch" to -qmp itself (in the form of "-qmp stdio,pretty=on").

Patch 3 makes uses of the new option in qemu-iotest 067 to greatly increase maintainability of its reference output. Patch 2 extends the QMP filter for qemu-iotests so it is able to filter out the QMP version object in pretty mode.

Max Reitz (3):
chardev: Add -qmp-pretty
iotests: _filter_qmp for pretty JSON output
iotests: Use -qmp-pretty in 067

 qemu-options.hx                  |   8 +
 tests/qemu-iotests/067           |   2 +-
 tests/qemu-iotests/067.out       | 779 ++++++++++++++++++++++++++++++++++++---
 tests/qemu-iotests/common.filter |  16 +-
 vl.c                             |  15 +-
 5 files changed, 756 insertions(+), 64 deletions(-)
Signature · 1 line
1.9.3
author
#1 lore raw

PATCH 1/3 chardev: Add -qmp-pretty

Add a command line option for adding a QMP monitor using pretty JSON formatting.

---
 qemu-options.hx |  8 ++++++++
 vl.c            | 15 ++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)
qemu-options.hx +8 -0
index da9851d..bc7b4c2 100644
@@ -2788,6 +2788,14 @@ STEXI
 @findex -qmp
 Like -monitor but opens in 'control' mode.
 ETEXI
+DEF("qmp-pretty", HAS_ARG, QEMU_OPTION_qmp_pretty, \
+    "-qmp-pretty dev like -qmp but uses pretty JSON formatting\n",
+    QEMU_ARCH_ALL)
+STEXI
+@item -qmp-pretty @var{dev}
+@findex -qmp-pretty
+Like -qmp but uses pretty JSON formatting.
+ETEXI
 
 DEF("mon", HAS_ARG, QEMU_OPTION_mon, \
     "-mon [chardev=]name[,mode=readline|control][,default]\n", QEMU_ARCH_ALL)
vl.c +10 -5
index f4a6e5e..c7bebad 100644
@@ -2284,7 +2284,7 @@ static int mon_init_func(QemuOpts *opts, void *opaque)
     return 0;
 }
 
-static void monitor_parse(const char *optarg, const char *mode)
+static void monitor_parse(const char *optarg, const char *mode, bool pretty)
 {
     static int monitor_device_index = 0;
     QemuOpts *opts;
@@ -2314,6 +2314,7 @@ static void monitor_parse(const char *optarg, const char *mode)
     }
     qemu_opt_set(opts, "mode", mode);
     qemu_opt_set(opts, "chardev", label);
+    qemu_opt_set_bool(opts, "pretty", pretty);
     if (def)
         qemu_opt_set(opts, "default", "on");
     monitor_device_index++;
@@ -3292,11 +3293,15 @@ int main(int argc, char **argv, char **envp)
             case QEMU_OPTION_monitor:
                 default_monitor = 0;
                 if (strncmp(optarg, "none", 4)) {
-                    monitor_parse(optarg, "readline");
+                    monitor_parse(optarg, "readline", false);
                 }
                 break;
             case QEMU_OPTION_qmp:
-                monitor_parse(optarg, "control");
+                monitor_parse(optarg, "control", false);
+                default_monitor = 0;
+                break;
+            case QEMU_OPTION_qmp_pretty:
+                monitor_parse(optarg, "control", true);
                 default_monitor = 0;
                 break;
             case QEMU_OPTION_mon:
@@ -3994,7 +3999,7 @@ int main(int argc, char **argv, char **envp)
                 add_device_config(DEV_SCLP, "stdio");
             }
             if (default_monitor)
-                monitor_parse("stdio", "readline");
+                monitor_parse("stdio", "readline", false);
         }
     } else {
         if (default_serial)
@@ -4002,7 +4007,7 @@ int main(int argc, char **argv, char **envp)
         if (default_parallel)
             add_device_config(DEV_PARALLEL, "vc:80Cx24C");
         if (default_monitor)
-            monitor_parse("vc:80Cx24C", "readline");
+            monitor_parse("vc:80Cx24C", "readline", false);
         if (default_virtcon)
             add_device_config(DEV_VIRTCON, "vc:80Cx24C");
         if (default_sclp) {
Signature · 1 line
1.9.3
Reviewed-by on v1 1/3
#2 lore raw
Quoted 9 lines · Max Reitz

Add a command line option for adding a QMP monitor using pretty JSON formatting.

---
 qemu-options.hx |  8 ++++++++
 vl.c            | 15 ++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

Minor grammar suggestions:

qemu-options.hx STEXI · line 2792

Quoted 10 lines
qemu-options.hx +2 -0
index da9851d..bc7b4c2 100644
@@ -2788,6 +2788,14 @@ STEXI
 @findex -qmp
 Like -monitor but opens in 'control' mode.
 ETEXI
+DEF("qmp-pretty", HAS_ARG, QEMU_OPTION_qmp_pretty, \
+    "-qmp-pretty dev like -qmp but uses pretty JSON formatting\n",

maybe s/dev like -qmp/dev: like -qmp,/

qemu-options.hx STEXI · line 2797

Quoted 5 lines
qemu-options.hx +5 -0
+    QEMU_ARCH_ALL)
+STEXI
+@item -qmp-pretty @var{dev}
+@findex -qmp-pretty
+Like -qmp but uses pretty JSON formatting.

maybe s/-qmp/-qmp,/

Either way,

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#3 lore raw
Quoted 24 lines · Eric Blake
Quoted 9 lines · Max Reitz

Add a command line option for adding a QMP monitor using pretty JSON formatting.

---
  qemu-options.hx |  8 ++++++++
  vl.c            | 15 ++++++++++-----
  2 files changed, 18 insertions(+), 5 deletions(-)

Minor grammar suggestions:

qemu-options.hx STEXI · line 2792

Quoted 10 lines
qemu-options.hx +2 -0
index da9851d..bc7b4c2 100644
@@ -2788,6 +2788,14 @@ STEXI
  @findex -qmp
  Like -monitor but opens in 'control' mode.
  ETEXI
+DEF("qmp-pretty", HAS_ARG, QEMU_OPTION_qmp_pretty, \
+    "-qmp-pretty dev like -qmp but uses pretty JSON formatting\n",

maybe s/dev like -qmp/dev: like -qmp,/

Quoted 6 lines

qemu-options.hx STEXI · line 2797

Quoted 5 lines
qemu-options.hx +5 -0
+    QEMU_ARCH_ALL)
+STEXI
+@item -qmp-pretty @var{dev}
+@findex -qmp-pretty
+Like -qmp but uses pretty JSON formatting.

maybe s/-qmp/-qmp,/

That's what I wrote at first, but then I saw the entry above: "Like -monitor but opens in 'control' mode" and decided to stay consistent.

Quoted 2 lines

Either way,

Thanks!

Max

author
#4 lore raw

PATCH 2/3 iotests: _filter_qmp for pretty JSON output

In reply to Max Reitz

_filter_qmp should be able to correctly filter out the QMP version object for pretty JSON output.

---
 tests/qemu-iotests/common.filter | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.filter +15 -1
index 3acdb30..e24dab4 100644
@@ -165,9 +165,23 @@ _filter_qemu()
 # replace problematic QMP output like timestamps
 _filter_qmp()
 {
+    discard=0
+
     _filter_win32 | \
     sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
-        -e 's#^{"QMP":.*}$#QMP_VERSION#'
+        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
+        -e 's#\\#\\\\#g' | \
+        while IFS='' read line; do
+            if [[ $line == '    "QMP": {' ]]; then
+                discard=1
+                echo '    QMP_VERSION'
+            fi
+            if [[ $discard == 0 ]]; then
+                echo "$line"
+            elif [[ $discard == 1 && $line == '    }' ]]; then
+                discard=0
+            fi
+        done
 }
 
 # replace driver-specific options in the "Formatting..." line
Signature · 1 line
1.9.3
#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] iotests: _filter_qmp for pretty JSON output

Quoted 15 lines · Max Reitz

_filter_qmp should be able to correctly filter out the QMP version object for pretty JSON output.

---
 tests/qemu-iotests/common.filter | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.filter +0 -0
index 3acdb30..e24dab4 100644
@@ -165,9 +165,23 @@ _filter_qemu()
 # replace problematic QMP output like timestamps
 _filter_qmp()

tests/qemu-iotests/common.filter _filter_qemu() · line 175

Quoted 10 lines
tests/qemu-iotests/common.filter +6 -1
 {
+    discard=0
+
     _filter_win32 | \
     sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
-        -e 's#^{"QMP":.*}$#QMP_VERSION#'
+        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
+        -e 's#\\#\\\\#g' | \
+        while IFS='' read line; do
+            if [[ $line == '    "QMP": {' ]]; then

Good that this is a /bin/bash script and not /bin/sh :)

But - is it really worth doing this in shell? Why not just do it in sed?

sed -e ... \
    -e 's#\\#\\\\#g' \
    -e '/    "QMP": {/,/    }/ c\' \
    -e '    QMP_VERSION'
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#6 lore raw

Re: [Qemu-devel] [PATCH 2/3] iotests: _filter_qmp for pretty JSON output

Quoted 19 lines · Eric Blake
Quoted 18 lines · Max Reitz

_filter_qmp should be able to correctly filter out the QMP version object for pretty JSON output.

---
  tests/qemu-iotests/common.filter | 16 +++++++++++++++-
  1 file changed, 15 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.filter +2 -0
index 3acdb30..e24dab4 100644
@@ -165,9 +165,23 @@ _filter_qemu()
  # replace problematic QMP output like timestamps
  _filter_qmp()
  {
+    discard=0
+
Quoted 8 lines

tests/qemu-iotests/common.filter _filter_qemu() · line 175

Quoted 7 lines
tests/qemu-iotests/common.filter +4 -1
      _filter_win32 | \
      sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
-        -e 's#^{"QMP":.*}$#QMP_VERSION#'
+        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
+        -e 's#\\#\\\\#g' | \
+        while IFS='' read line; do
+            if [[ $line == '    "QMP": {' ]]; then

Good that this is a /bin/bash script and not /bin/sh :)

Ah, right, yes, I just copied the code I had written for filtering out the image-specific information from the qemu-img info output.

Quoted 1 line

But - is it really worth doing this in shell? Why not just do it in sed?

Because I don't know sed well enough. ;-)

Quoted 4 lines
sed -e ... \
     -e 's#\\#\\\\#g' \
     -e '/    "QMP": {/,/    }/ c\' \
     -e '    QMP_VERSION'

Will do, thanks.

Max

#7 lore raw

Re: [Qemu-devel] [PATCH 2/3] iotests: _filter_qmp for pretty JSON output

Quoted 4 lines · Max Reitz
Quoted 4 lines
Quoted 4 lines
tests/qemu-iotests/common.filter +4 -0
+        -e 's#^{"QMP":.*}$#QMP_VERSION#' \
+        -e 's#\\#\\\\#g' | \
+        while IFS='' read line; do
+            if [[ $line == '    "QMP": {' ]]; then
Quoted 12 lines
Quoted 1 line

Good that this is a /bin/bash script and not /bin/sh :)

Ah, right, yes, I just copied the code I had written for filtering out the image-specific information from the qemu-img info output.

Quoted 1 line

But - is it really worth doing this in shell? Why not just do it in sed?

Because I don't know sed well enough. ;-)

Quoted 3 lines
sed -e ... \
     -e 's#\\#\\\\#g' \
     -e '/    "QMP": {/,/    }/ c\' \

Better make this line anchored in its searching:

-e '/^ "QMP": {$/,/^ }$/ c\' \

Quoted 7 lines
Quoted 1 line

-e ' QMP_VERSION'

Will do, thanks.

Max

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#8 lore raw

PATCH 3/3 iotests: Use -qmp-pretty in 067

In reply to Max Reitz

067 invokes query-block, resulting in a reference output with really long lines (which may pose a problem in email patches and always poses a problem when the output changes, because it is hard to see what has actually changed). Use -qmp-pretty to mitigate this issue.

---
 tests/qemu-iotests/067     |   2 +-
 tests/qemu-iotests/067.out | 779 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 723 insertions(+), 58 deletions(-)
tests/qemu-iotests/067 +1 -1
index d025192..29cd6b5 100755
@@ -39,7 +39,7 @@ _supported_os Linux
 function do_run_qemu()
 {
     echo Testing: "$@"
-    $QEMU -nographic -qmp stdio -serial none "$@"
+    $QEMU -nographic -qmp-pretty stdio -serial none "$@"
     echo
 }
 
tests/qemu-iotests/067.out +722 -57

799 lines not on this page · raw message

Signature · 1 line
1.9.3
Reviewed-by on v1 3/3
#9 lore raw

Re: [Qemu-devel] [PATCH 3/3] iotests: Use -qmp-pretty in 067

Quoted 10 lines · Max Reitz

067 invokes query-block, resulting in a reference output with really long lines (which may pose a problem in email patches and always poses a problem when the output changes, because it is hard to see what has actually changed). Use -qmp-pretty to mitigate this issue.

---
 tests/qemu-iotests/067     |   2 +-
 tests/qemu-iotests/067.out | 779 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 723 insertions(+), 58 deletions(-)

Large, but mechanical. I'm trusting this one is correct (if the testsuite still passes, you did it right) rather than actually reading it :)

tests/qemu-iotests/067 _supported_os Linux · line 42

Quoted 11 lines
tests/qemu-iotests/067 +1 -1
index d025192..29cd6b5 100755
@@ -39,7 +39,7 @@ _supported_os Linux
 function do_run_qemu()
 {
     echo Testing: "$@"
-    $QEMU -nographic -qmp stdio -serial none "$@"
+    $QEMU -nographic -qmp-pretty stdio -serial none "$@"

Such a trivial change for inducing over 700 lines of change in the expected output :)

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#10 lore raw

Re: [Qemu-devel] [PATCH 3/3] iotests: Use -qmp-pretty in 067

Quoted 6 lines · Eric Blake
Quoted 5 lines · Max Reitz

067 invokes query-block, resulting in a reference output with really long lines (which may pose a problem in email patches and always poses a problem when the output changes, because it is hard to see what has actually changed). Use -qmp-pretty to mitigate this issue.

Quoted 7 lines
Quoted 5 lines
---
  tests/qemu-iotests/067     |   2 +-
  tests/qemu-iotests/067.out | 779 +++++++++++++++++++++++++++++++++++++++++----
  2 files changed, 723 insertions(+), 58 deletions(-)

Large, but mechanical. I'm trusting this one is correct (if the testsuite still passes, you did it right) rather than actually reading it :)

Yes, for me "Passes before and the output afterwards still makes sense when skipping through" was enough.

Max

Quoted 14 lines

tests/qemu-iotests/067 _supported_os Linux · line 42

Quoted 10 lines
tests/qemu-iotests/067 +1 -1
index d025192..29cd6b5 100755
@@ -39,7 +39,7 @@ _supported_os Linux
  function do_run_qemu()
  {
      echo Testing: "$@"
-    $QEMU -nographic -qmp stdio -serial none "$@"
+    $QEMU -nographic -qmp-pretty stdio -serial none "$@"

Such a trivial change for inducing over 700 lines of change in the expected output :)