Skip to contentSkip to conversation

libxl: fixes for JSON infrastructure

PATCH Superseded Wei Liu 3 participants 20 messages Apr 10, 2014 – Apr 16, 2014

v2 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

v2 cover libxl: fixes for JSON infrastructure

This small series contains three fixes to libxl JSON infrasturcture.

Changes in V2:
* generate empty map for None field in keyed-union
* replace empty Struct with None for libxl_event

Wei.

Wei Liu (4):
libxl/gentypes.py: don't generate JSON for private type(s)
libxl_json: fix JSON parser debug code libxl/gentypes.py: generate empty map for None field in keyed-union libxl_types.idl: replace empty Struct with None for libxl_event

 tools/libxl/gentypes.py     |    9 ++++++++-
 tools/libxl/libxl_json.c    |   17 +++++++++++++----
 tools/libxl/libxl_types.idl |    2 +-
 3 files changed, 22 insertions(+), 6 deletions(-)
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH v2 1/4 libxl/gentypes.py: don't generate JSON for private type(s)

Private types are only useful inside libxl. They don't have a valid JSON generation function by default.

Currently there's only one private type, that's libxl_ev_link. Not skipping this field causes testidl to fail as the code generated for this type is NULL.

---
 tools/libxl/gentypes.py |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/gentypes.py +1 -1
index be06257..1a3b91c 100644
@@ -226,7 +226,7 @@ def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
         s += "s = yajl_gen_map_open(hand);\n"
         s += "if (s != yajl_gen_status_ok)\n"
         s += "    goto out;\n"
-        for f in [f for f in ty.fields if not f.const]:
+        for f in [f for f in ty.fields if not f.const and not f.type.private]:
             (nparent,fexpr) = ty.member(v, f, parent is None)
             s += "s = yajl_gen_string(hand, (const unsigned char *)\"%s\", sizeof(\"%s\")-1);\n" % (f.name, f.name)
             s += "if (s != yajl_gen_status_ok)\n"
Signature · 1 line
1.7.10.4
Acked-by on v2 1/4
#2 lore raw

Re: [PATCH V2 1/4] libxl/gentypes.py: don't generate JSON for private type(s)

Quoted 8 lines · Wei Liu

Private types are only useful inside libxl. They don't have a valid JSON generation function by default.

Currently there's only one private type, that's libxl_ev_link. Not skipping this field causes testidl to fail as the code generated for this type is NULL.

author
#3 lore raw

PATCH v2 2/4 libxl_json: fix JSON parser debug code

In reply to Wei Liu

Two changes included:
1. implement DEBUG_GEN_ALLOC for YAJL2
2. use size_t for variable "len"

without these two fixes it fails to compile when DEBUG_ANSWER is defined.

---
 tools/libxl/libxl_json.c |   17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tools/libxl/libxl_json.c +13 -4
index 989ac3f..3ea56a4 100644
@@ -34,11 +34,20 @@ struct libxl__yajl_ctx {
 };
 
 #ifdef DEBUG_ANSWER
-#  define DEBUG_GEN_ALLOC(ctx) \
-    if ((ctx)->g == NULL) { \
-        yajl_gen_config conf = { 1, "  " }; \
+#if YAJL_VERSION < 20000
+#  define DEBUG_GEN_ALLOC(ctx)                  \
+    if ((ctx)->g == NULL) {                     \
+        yajl_gen_config conf = { 1, "  " };     \
         (ctx)->g = yajl_gen_alloc(&conf, NULL); \
     }
+#else  /* YAJL2 */
+#  define DEBUG_GEN_ALLOC(ctx)                                    \
+    if ((ctx)->g == NULL) {                                       \
+        (ctx)->g = yajl_gen_alloc(NULL);                          \
+        yajl_gen_config((ctx)->g, yajl_gen_beautify, 1);          \
+        yajl_gen_config((ctx)->g, yajl_gen_indent_string, "  ");  \
+    }
+#endif
 #  define DEBUG_GEN_FREE(ctx) \
     if ((ctx)->g) yajl_gen_free((ctx)->g)
 #  define DEBUG_GEN(ctx, type)              yajl_gen_##type(ctx->g)
@@ -48,7 +57,7 @@ struct libxl__yajl_ctx {
 #  define DEBUG_GEN_REPORT(yajl_ctx) \
     do { \
         const unsigned char *buf = NULL; \
-        unsigned int len = 0; \
+        size_t len = 0; \
         yajl_gen_get_buf((yajl_ctx)->g, &buf, &len); \
         LIBXL__LOG(libxl__gc_owner((yajl_ctx)->gc), \
                    LIBXL__LOG_DEBUG, "response:\n%s", buf); \
Signature · 1 line
1.7.10.4
author
#5 lore raw

PATCH v2 3/4 libxl/gentypes.py: generate empty map for None field in keyed-union

In reply to Wei Liu

Without this the generated JSON is malformed.

---
 tools/libxl/gentypes.py |    7 +++++++
 1 file changed, 7 insertions(+)
tools/libxl/gentypes.py +7 -0
index 1a3b91c..bfb95e2 100644
@@ -220,6 +220,13 @@ def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
             s += "case %s:\n" % f.enumname
             if f.type is not None:
                 s += libxl_C_type_gen_json(f.type, fexpr, indent + "    ", nparent)
+            else:
+                s += "    yajl_gen_map_open(hand);\n"
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
+                s += "    yajl_gen_map_close(hand);\n"
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
             s += "    break;\n"
         s += "}\n"
     elif isinstance(ty, idl.Struct) and (parent is None or ty.json_fn is None):
Signature · 1 line
1.7.10.4
#7 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

In reply to Wei Liu
Quoted 3 lines · Wei Liu

Without this the generated JSON is malformed.

Hold on the Ack...

Review

Quoted 2 lines
(quoted diff) +2 -0
+            else:
+                s += "    yajl_gen_map_open(hand);\n"

ITHM s =...

Review

Quoted 3 lines
(quoted diff) +3 -0
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
+                s += "    yajl_gen_map_close(hand);\n"

and again.

Spotted because the diff of the generated code across the entire series was as below, the spurious change is pretty obvious in the third hunk.

Ian.

libxl/_libxl_types.c renamed from libxl/_libxl_BACKUP_types.c +8 -5
@@ -2523,6 +2523,12 @@ yajl_gen_status libxl_domain_build_info_
             goto out;
         break;
     case LIBXL_DOMAIN_TYPE_INVALID:
+        yajl_gen_map_open(hand);
+        if (s != yajl_gen_status_ok)
+            goto out;
+        yajl_gen_map_close(hand);
+        if (s != yajl_gen_status_ok)
+            goto out;
         break;
     }
     s = yajl_gen_map_close(hand);
@@ -3631,9 +3637,6 @@ yajl_gen_status libxl_event_gen_json(yaj
     s = yajl_gen_map_open(hand);
     if (s != yajl_gen_status_ok)
         goto out;
-    s = yajl_gen_string(hand, (const unsigned char *)"link", sizeof("link")-1);
-    if (s != yajl_gen_status_ok)
-        goto out;
     s = yajl_gen_string(hand, (const unsigned char *)"domid", sizeof("domid")-1);
     if (s != yajl_gen_status_ok)
         goto out;
@@ -3713,10 +3716,10 @@ yajl_gen_status libxl_event_gen_json(yaj
             goto out;
         break;
     case LIBXL_EVENT_TYPE_DOMAIN_CREATE_CONSOLE_AVAILABLE:
-        s = yajl_gen_map_open(hand);
+        yajl_gen_map_open(hand);
         if (s != yajl_gen_status_ok)
             goto out;
-        s = yajl_gen_map_close(hand);
+        yajl_gen_map_close(hand);
         if (s != yajl_gen_status_ok)
             goto out;
         break;
libxl/_libxl_types.h renamed from libxl/_libxl_BACKUP_types.h +0 -2
@@ -694,8 +694,6 @@ typedef struct libxl_event {
         struct {
             int rc;
         } operation_complete;
-        struct {
-        } domain_create_console_available;
     } u;
 } libxl_event;
 void libxl_event_dispose(libxl_event *p);
author
#8 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

Quoted 12 lines · Ian Campbell
Quoted 3 lines · Wei Liu

Without this the generated JSON is malformed.

Hold on the Ack...

Review

Quoted 2 lines
(quoted diff) +2 -0
+            else:
+                s += "    yajl_gen_map_open(hand);\n"

ITHM s =...

Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
+                s += "    yajl_gen_map_close(hand);\n"

and again.

Spotted because the diff of the generated code across the entire series was as below, the spurious change is pretty obvious in the third hunk.

I should've stolen your handy script earlier!

This one should be correct.

---8<---

Quoted 1 line

From eda04499a0b15d0a7549206834112823b59cf6f9 Mon Sep 17 00:00:00 2001

From: Wei Liu <[email protected]>
Date: Thu, 10 Apr 2014 16:18:00 +0100
Subject: [PATCH] libxl/gentypes.py: generate empty map for None field in
keyed-union

Without this the generated JSON is malformed.

---
 tools/libxl/gentypes.py |    7 +++++++
 1 file changed, 7 insertions(+)
tools/libxl/gentypes.py +7 -0
index 1a3b91c..917e2c2 100644
@@ -220,6 +220,13 @@ def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
             s += "case %s:\n" % f.enumname
             if f.type is not None:
                 s += libxl_C_type_gen_json(f.type, fexpr, indent + "    ", nparent)
+            else:
+                s += "    s = yajl_gen_map_open(hand);\n"
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
+                s += "    s = yajl_gen_map_close(hand);\n"
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
             s += "    break;\n"
         s += "}\n"
     elif isinstance(ty, idl.Struct) and (parent is None or ty.json_fn is None):
Signature · 1 line
1.7.10.4
#9 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

Quoted 17 lines · Wei Liu
Quoted 16 lines · Ian Campbell
Quoted 3 lines · Wei Liu

Without this the generated JSON is malformed.

Hold on the Ack...

Review

Quoted 2 lines
(quoted diff) +2 -0
+            else:
+                s += "    yajl_gen_map_open(hand);\n"

ITHM s =...

Quoted 3 lines
tools/libxl/gentypes.py +3 -0
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
+                s += "    yajl_gen_map_close(hand);\n"
Quoted 7 lines
Quoted 5 lines

and again.

Spotted because the diff of the generated code across the entire series was as below, the spurious change is pretty obvious in the third hunk.

I should've stolen your handy script earlier!

~ianc/xen-build-tools/libxl-idl-* if you still want to...

Quoted 1 line

This one should be correct.

Agreed, you can have that Ack back ;-)

author
#10 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

Quoted 24 lines · Ian Campbell
Quoted 23 lines · Wei Liu
Quoted 21 lines · Ian Campbell
Quoted 3 lines · Wei Liu

Without this the generated JSON is malformed.

Hold on the Ack...

Review

Quoted 2 lines
(quoted diff) +2 -0
+            else:
+                s += "    yajl_gen_map_open(hand);\n"

ITHM s =...

tools/libxl/gentypes.py def libxl_C_type_gen_json(ty, v, indent = " ", parent = None):

Quoted 3 lines
tools/libxl/gentypes.py +3 -0
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
+                s += "    yajl_gen_map_close(hand);\n"

and again.

Spotted because the diff of the generated code across the entire series was as below, the spurious change is pretty obvious in the third hunk.

Quoted 8 lines
Quoted 1 line

I should've stolen your handy script earlier!

~ianc/xen-build-tools/libxl-idl-* if you still want to...

Quoted 1 line

This one should be correct.

Agreed, you can have that Ack back ;-)

Do you want me to resend the whole series with all Acks folded in?

Wei.

#11 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

Quoted 27 lines · Wei Liu
Quoted 26 lines · Ian Campbell
Quoted 24 lines · Wei Liu
Quoted 12 lines · Ian Campbell
Quoted 3 lines · Wei Liu

Without this the generated JSON is malformed.

Hold on the Ack...

Review

Quoted 2 lines
(quoted diff) +2 -0
+            else:
+                s += "    yajl_gen_map_open(hand);\n"

ITHM s =...

Quoted 9 lines

tools/libxl/gentypes.py def libxl_C_type_gen_json(ty, v, indent = " ", parent = None):

Quoted 3 lines
tools/libxl/gentypes.py +3 -0
+                s += "    if (s != yajl_gen_status_ok)\n"
+                s += "        goto out;\n"
+                s += "    yajl_gen_map_close(hand);\n"

and again.

Spotted because the diff of the generated code across the entire series was as below, the spurious change is pretty obvious in the third hunk.

I should've stolen your handy script earlier!

Quoted 8 lines
Quoted 6 lines

~ianc/xen-build-tools/libxl-idl-* if you still want to...

Quoted 1 line

This one should be correct.

Agreed, you can have that Ack back ;-)

Do you want me to resend the whole series with all Acks folded in?

No need, I've applied. Please check I got the right versions of everything!

author
#12 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

On Wed, Apr 16, 2014 at 05:30:21PM +0100, Ian Campbell wrote:
[...]

Quoted 3 lines

No need, I've applied. Please check I got the right versions of everything!

I've checked. You checked in the correct version. Thanks.

#13 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

In reply to Ian Campbell

Ian Campbell writes ("Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union"):

Quoted 4 lines · Wei Liu
Quoted 1 line

I should've stolen your handy script earlier!

~ianc/xen-build-tools/libxl-idl-* if you still want to...

Can we check this into the tree ?

Ian.

#14 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

Quoted 7 lines · Ian Jackson

Ian Campbell writes ("Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union"):

Quoted 4 lines · Wei Liu
Quoted 1 line

I should've stolen your handy script earlier!

~ianc/xen-build-tools/libxl-idl-* if you still want to...

Can we check this into the tree ?

They are pretty dumb, but we can if you like. Here they are for prejudgement...

----- libxl-idl-save-baseline ------
#!/bin/bash

set -e

for i in tools/libxl/_libxl_types.c tools/libxl/_libxl_types.h tools/libxl/_libxl_types_json.h \
         tools/libxl/_libxl_types_internal.c tools/libxl/_libxl_types_internal.h tools/libxl/_libxl_types_internal_json.h \
         tools/python/xen/lowlevel/xl/_pyxl_types.h tools/python/xen/lowlevel/xl/_pyxl_types.c \
         tools/ocaml/libs/xl/_libxl_types.inc  tools/ocaml/libs/xl/_libxl_types.mli.in  tools/ocaml/libs/xl/_libxl_types.ml.in \
         tools/libxl/testidl.c \

tools/libxl/_libxl_save_msgs_callout.c tools/libxl/_libxl_save_msgs_callout.h tools/libxl/_libxl_save_msgs_helper.c tools/libxl/_libxl_save_msgs_helper.h ; do

    bak=$(echo $i | sed -e 's,[^/]*/\(_libxl\|_pyxl\|testidl\),&_BACKUP,g')
    if [ -e "$i" ] ; then
	cp -v "$i" "$bak"
    else
	rm -f "$bak"
	echo touch "$bak"
	touch "$bak"
    fi

done

----- libxl-idl-compare-baseline -----
#!/bin/bash

set -e

for i in tools/libxl/_libxl_types.c tools/libxl/_libxl_types.h tools/libxl/_libxl_types_json.h \
         tools/libxl/_libxl_types_internal.c tools/libxl/_libxl_types_internal.h tools/libxl/_libxl_types_internal_json.h \
         tools/python/xen/lowlevel/xl/_pyxl_types.h tools/python/xen/lowlevel/xl/_pyxl_types.c \
         tools/ocaml/libs/xl/_libxl_types.inc  tools/ocaml/libs/xl/_libxl_types.mli.in  tools/ocaml/libs/xl/_libxl_types.ml.in \
         tools/libxl/testidl.c \

tools/libxl/_libxl_save_msgs_callout.c tools/libxl/_libxl_save_msgs_callout.h tools/libxl/_libxl_save_msgs_helper.c tools/libxl/_libxl_save_msgs_helper.h ; do

    bak=$(echo $i | sed -e 's,[^/]*/\(_libxl\|_pyxl\|testidl\),&_BACKUP,g')

    diff -puN $bak $i || true

done

#15 lore raw

Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union

Ian Campbell writes ("Re: [PATCH V2 3/4] libxl/gentypes.py: generate empty map for None field in keyed-union"):

Quoted 2 lines

They are pretty dumb, but we can if you like. Here they are for prejudgement...

Quite ugly, I agree. But I think it's better to have them in-tree where they can be shared and improved, than floating about on some NFS filer in the Citrix Cambridge office.

TBH it might be possible to do in the Makefile but I think that's a task for another day.

Ian.

author
#16 lore raw

PATCH v2 4/4 libxl_types.idl: replace empty Struct with None for libxl_event

In reply to Wei Liu

Now we generate empty map for None, the empty Struct trick is not necessary anymore.

---
 tools/libxl/libxl_types.idl |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/libxl/libxl_types.idl +1 -1
index 612645c..d0b9ff0 100644
@@ -596,5 +596,5 @@ libxl_event = Struct("event",[
            ("operation_complete", Struct(None, [
                                         ("rc", integer),
                                  ])),
-           ("domain_create_console_available", Struct(None, [])),
+           ("domain_create_console_available", None),
            ]))])
Signature · 1 line
1.7.10.4
Acked-by on v2 4/4
#17 lore raw

Re: [PATCH V2 4/4] libxl_types.idl: replace empty Struct with None for libxl_event

Quoted 4 lines · Wei Liu

Now we generate empty map for None, the empty Struct trick is not necessary anymore.

But you've missed

            ("domain_death", Struct(None, [])),

in the same struct.

Arguably this is an API change -- but since the struct was

        struct {
                } domain_create_console_available;

I don't think it is material enough to warrant a #define etc.

author
#18 lore raw

Re: [PATCH V2 4/4] libxl_types.idl: replace empty Struct with None for libxl_event

Quoted 11 lines · Ian Campbell
Quoted 4 lines · Wei Liu

Now we generate empty map for None, the empty Struct trick is not necessary anymore.

But you've missed

            ("domain_death", Struct(None, [])),

in the same struct.

Oops. My fault.

Quoted 6 lines

Arguably this is an API change -- but since the struct was

        struct {
                } domain_create_console_available;

I don't think it is material enough to warrant a #define etc.

---8<---

Quoted 1 line

From 2cfa7a3623847a431c0bf662a19e9eb2d9c79254 Mon Sep 17 00:00:00 2001

From: Wei Liu <[email protected]>
Date: Thu, 10 Apr 2014 16:20:22 +0100
Subject: [PATCH] libxl_types.idl: replace empty Struct with None for
libxl_event

Now we generate empty map for None, the empty Struct trick is not necessary anymore.

---
 tools/libxl/libxl_types.idl |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxl/libxl_types.idl +2 -2
index 612645c..0f7bbf8 100644
@@ -588,7 +588,7 @@ libxl_event = Struct("event",[
           [("domain_shutdown", Struct(None, [
                                              ("shutdown_reason", uint8),
                                       ])),
-           ("domain_death", Struct(None, [])),
+           ("domain_death", None),
            ("disk_eject", Struct(None, [
                                         ("vdev", string),
                                         ("disk", libxl_device_disk),
@@ -596,5 +596,5 @@ libxl_event = Struct("event",[
            ("operation_complete", Struct(None, [
                                         ("rc", integer),
                                  ])),
-           ("domain_create_console_available", Struct(None, [])),
+           ("domain_create_console_available", None),
            ]))])
Signature · 1 line
1.7.10.4
#19 lore raw

Re: [PATCH V2 4/4] libxl_types.idl: replace empty Struct with None for libxl_event

Quoted 7 lines · Wei Liu
Quoted 6 lines · Ian Campbell
Quoted 4 lines · Wei Liu

Now we generate empty map for None, the empty Struct trick is not necessary anymore.

Quoted 7 lines
Quoted 5 lines

But you've missed

            ("domain_death", Struct(None, [])),

in the same struct.

Oops. My fault.

No worries, you can keep the ack on the new version too.