Skip to contentSkip to conversation

Some cleanup of libxl JSON handling

PATCH New Elliott Mitchell 1 participant 6 messages Oct 15

lore
Share & tools
lore
#0 lore raw

cover Some cleanup of libxl JSON handling

Another batch which has been hanging around for a while and never submitted. Here the observation is most of the data passed to the JSON libraries aren't modified. Marking the arguments to the functions as constant is correct and makes the API rather similar to the one used for "SXP". Once that is done some more merging is possible.

There seems to be a bit of a difference in how the interface for libjson-c is formatted versus the libyajl variant. I've left it similar to how it appeared to be done, but that looks a bit odd. If there is a desire for linebreaks and indentation feel free.

This has been rebased for the new libjson-c library. Only difference was needing to add some duplicate hunks for the new library.

Elliott Mitchell (5):
tools/libxl: Mark pointer args of many functions constant tools/xl: Mark libxl_domain_config * arg of printf_info_*() const tools/libxl: Tiny optimization of libxl__mac_is_default() tools/xl: Rename printf_info()/list_domains_details() to dump_by_...() tools/xl: Merge down debug/dry-run section of create_domain()

 tools/include/libxl_json.h        | 38 ++++++++++++++++---------------
 tools/libs/light/gentypes.py      | 12 +++++-----
 tools/libs/light/libxl_cpuid.c    |  7 +++---
 tools/libs/light/libxl_internal.c |  8 +++----
 tools/libs/light/libxl_internal.h | 18 +++++++--------
 tools/libs/light/libxl_json.c     | 33 ++++++++++++++-------------
 tools/libs/light/libxl_nocpuid.c  |  4 ++--
 tools/xl/xl.h                     | 13 +++++++----
 tools/xl/xl_info.c                | 33 +++++++++++++++------------
 tools/xl/xl_misc.c                |  2 +-
 tools/xl/xl_sxp.c                 |  6 ++---
 tools/xl/xl_vmcontrol.c           | 14 +-----------
 12 files changed, 95 insertions(+), 93 deletions(-)
Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/5 tools/libxl: Mark pointer args of many functions constant

Date: Fri, 18 Dec 2020 13:37:44 -0800

Anything *_is_empty(), *_is_default(), or *_gen_json() is going to be examining the pointed to thing, not modifying it. This potentially results in higher-performance output. This also allows spreading constants further, allowing more checking and security.

---
 tools/include/libxl_json.h        | 38 ++++++++++++++++---------------
 tools/libs/light/gentypes.py      | 12 +++++-----
 tools/libs/light/libxl_cpuid.c    |  7 +++---
 tools/libs/light/libxl_internal.c |  4 ++--
 tools/libs/light/libxl_internal.h | 18 +++++++--------
 tools/libs/light/libxl_json.c     | 33 ++++++++++++++-------------
 tools/libs/light/libxl_nocpuid.c  |  4 ++--
 7 files changed, 60 insertions(+), 56 deletions(-)
tools/include/libxl_json.h +20 -18
index c130e88a5e..3b03fdc04a 100644
@@ -33,29 +33,31 @@
 #define _hidden
 #endif
 _hidden int libxl__uint64_gen_jso(json_object **jso_r, uint64_t val);
-_hidden int libxl_defbool_gen_jso(json_object **jso_r, libxl_defbool *p);
-_hidden int libxl_uuid_gen_jso(json_object **jso_r, libxl_uuid *p);
-_hidden int libxl_mac_gen_jso(json_object **jso_r, libxl_mac *p);
-_hidden int libxl_bitmap_gen_jso(json_object **jso_r, libxl_bitmap *p);
-_hidden int libxl_cpuid_policy_list_gen_jso(json_object **jso_r,libxl_cpuid_policy_list *p);
-_hidden int libxl_string_list_gen_jso(json_object **jso_r,libxl_string_list *p);
-_hidden int libxl_key_value_list_gen_jso(json_object **jso_r, libxl_key_value_list *p);
-_hidden int libxl_hwcap_gen_jso(json_object **jso_r, libxl_hwcap *p);
-_hidden int libxl_ms_vm_genid_gen_jso(json_object **jso_r, libxl_ms_vm_genid *p);
+_hidden int libxl_defbool_gen_jso(json_object **jso_r, const libxl_defbool *p);
+_hidden int libxl_uuid_gen_jso(json_object **jso_r, const libxl_uuid *p);
+_hidden int libxl_mac_gen_jso(json_object **jso_r, const libxl_mac *p);
+_hidden int libxl_bitmap_gen_jso(json_object **jso_r, const libxl_bitmap *p);
+_hidden int libxl_cpuid_policy_list_gen_jso(json_object **jso_r, const libxl_cpuid_policy_list *p);
+_hidden int libxl_string_list_gen_jso(json_object **jso_r, const libxl_string_list *p);
+_hidden int libxl_key_value_list_gen_jso(json_object **jso_r, const libxl_key_value_list *p);
+_hidden int libxl_hwcap_gen_jso(json_object **jso_r, const libxl_hwcap *p);
+_hidden int libxl_ms_vm_genid_gen_jso(json_object **jso_r, const libxl_ms_vm_genid *p);
 #endif
 #if defined(HAVE_LIBYAJL)
 yajl_gen_status libxl__uint64_gen_json(yajl_gen hand, uint64_t val);
-yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, libxl_defbool *p);
-yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, libxl_uuid *p);
-yajl_gen_status libxl_mac_gen_json(yajl_gen hand, libxl_mac *p);
-yajl_gen_status libxl_bitmap_gen_json(yajl_gen hand, libxl_bitmap *p);
+yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, const libxl_defbool *p);
+yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, const libxl_uuid *p);
+yajl_gen_status libxl_mac_gen_json(yajl_gen hand, const libxl_mac *p);
+yajl_gen_status libxl_bitmap_gen_json(yajl_gen hand, const libxl_bitmap *p);
 yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand,
-                                                 libxl_cpuid_policy_list *p);
-yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, libxl_string_list *p);
+                                                 const libxl_cpuid_policy_list *p);
+yajl_gen_status libxl_string_list_gen_json(yajl_gen hand,
+                                           const libxl_string_list *p);
 yajl_gen_status libxl_key_value_list_gen_json(yajl_gen hand,
-                                              libxl_key_value_list *p);
-yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand, libxl_hwcap *p);
-yajl_gen_status libxl_ms_vm_genid_gen_json(yajl_gen hand, libxl_ms_vm_genid *p);
+                                              const libxl_key_value_list *p);
+yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand, const libxl_hwcap *p);
+yajl_gen_status libxl_ms_vm_genid_gen_json(yajl_gen hand,
+                                           const libxl_ms_vm_genid *p);
 #endif
 
 #include <_libxl_types_json.h>
tools/libs/light/gentypes.py +6 -6
index 47bca4bc57..7defc47554 100644
@@ -763,7 +763,7 @@ if __name__ == '__main__':
                                                ty.make_arg("p"),
                                                ku.keyvar.type.make_arg(ku.keyvar.name)))
         if ty.json_gen_fn is not None:
-            f.write("%schar *%s_to_json(libxl_ctx *ctx, %s);\n" % (ty.hidden(), ty.typename, ty.make_arg("p")))
+            f.write("%schar *%s_to_json(libxl_ctx *ctx, const %s);\n" % (ty.hidden(), ty.typename, ty.make_arg("p")))
         if ty.json_parse_fn is not None:
             f.write("%sint %s_from_json(libxl_ctx *ctx, %s, const char *s);\n" % (ty.hidden(), ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
         if isinstance(ty, idl.Enumeration):
@@ -795,9 +795,9 @@ if __name__ == '__main__':
     for ty in [ty for ty in types if ty.json_gen_fn is not None]:
         if opt_libjsonc:
             # Always hide JSON generators base on json-c
-            f.write("%sint %s_gen_jso(json_object **jso_r, %s);\n" % ("_hidden ", ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
+            f.write("%sint %s_gen_jso(json_object **jso_r, const %s);\n" % ("_hidden ", ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
         else:
-            f.write("%syajl_gen_status %s_gen_json(yajl_gen hand, %s);\n" % (ty.hidden(), ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
+            f.write("%syajl_gen_status %s_gen_json(yajl_gen hand, const %s);\n" % (ty.hidden(), ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
 
     f.write("\n")
     f.write("""#endif /* %s */\n""" % header_json_define)
@@ -902,19 +902,19 @@ if __name__ == '__main__':
 
     for ty in [t for t in types if t.json_gen_fn is not None]:
         if opt_libjsonc:
-            f.write("int %s_gen_jso(json_object **jso_r, %s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
+            f.write("int %s_gen_jso(json_object **jso_r, const %s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
             f.write("{\n")
             f.write(libxl_C_type_gen_jso(ty, "p"))
             f.write("}\n")
             f.write("\n")
         else:
-            f.write("yajl_gen_status %s_gen_json(yajl_gen hand, %s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
+            f.write("yajl_gen_status %s_gen_json(yajl_gen hand, const %s)\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)))
             f.write("{\n")
             f.write(libxl_C_type_gen_json(ty, "p"))
             f.write("}\n")
             f.write("\n")
 
-        f.write("char *%s_to_json(libxl_ctx *ctx, %s)\n" % (ty.typename, ty.make_arg("p")))
+        f.write("char *%s_to_json(libxl_ctx *ctx, const %s)\n" % (ty.typename, ty.make_arg("p")))
         f.write("{\n")
         if opt_libjsonc:
             f.write(libxl_C_type_to_json(ty, "p", fn_ptr_type="libxl__gen_json_callback", fn_suffix="_gen_jso"))
tools/libs/light/libxl_cpuid.c +3 -3
index 14f08df33a..40a188bfb1 100644
@@ -16,7 +16,7 @@
 
 #include <xen/lib/x86/cpu-policy.h>
 
-int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl)
+int libxl__cpuid_policy_is_empty(const libxl_cpuid_policy_list *pl)
 {
     return !*pl || (!libxl_cpuid_policy_list_length(pl) && !(*pl)->msr);
 }
@@ -549,7 +549,7 @@ static const char *policy_names[4] = { "eax", "ebx", "ecx", "edx" };
  */
 
 #ifdef HAVE_LIBJSONC
-int libxl_cpuid_policy_list_gen_jso(json_object **jso_r, libxl_cpuid_policy_list *pl)
+int libxl_cpuid_policy_list_gen_jso(json_object **jso_r, const libxl_cpuid_policy_list *pl)
 {
     libxl_cpuid_policy_list policy = *pl;
     struct xc_xend_cpuid *cpuid;
@@ -667,7 +667,7 @@ out:
 
 #ifdef HAVE_LIBYAJL
 yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand,
-                                libxl_cpuid_policy_list *pl)
+                                const libxl_cpuid_policy_list *pl)
 {
     libxl_cpuid_policy_list policy = *pl;
     struct xc_xend_cpuid *cpuid;
tools/libs/light/libxl_internal.c +2 -2
index d70cfed7d8..cd99d249bf 100644
@@ -333,7 +333,7 @@ _hidden int libxl__parse_mac(const char *s, libxl_mac mac)
     return 0;
 }
 
-_hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b)
+_hidden int libxl__compare_macs(const libxl_mac *a, const libxl_mac *b)
 {
     int i;
 
@@ -345,7 +345,7 @@ _hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b)
     return 0;
 }
 
-_hidden int libxl__mac_is_default(libxl_mac *mac)
+_hidden int libxl__mac_is_default(const libxl_mac *mac)
 {
     return (!(*mac)[0] && !(*mac)[1] && !(*mac)[2] &&
             !(*mac)[3] && !(*mac)[4] && !(*mac)[5]);
tools/libs/light/libxl_internal.h +9 -9
index b65e0064b9..a070112836 100644
@@ -2021,9 +2021,9 @@ struct libxl__xen_console_reader {
 /* parse the string @s as a sequence of 6 colon separated bytes in to @mac */
 _hidden int libxl__parse_mac(const char *s, libxl_mac mac);
 /* compare mac address @a and @b. 0 if the same, -ve if a<b and +ve if a>b */
-_hidden int libxl__compare_macs(libxl_mac *a, libxl_mac *b);
+_hidden int libxl__compare_macs(const libxl_mac *a, const libxl_mac *b);
 /* return true if mac address is all zero (the default value) */
-_hidden int libxl__mac_is_default(libxl_mac *mac);
+_hidden int libxl__mac_is_default(const libxl_mac *mac);
 /* init a recursive mutex */
 _hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock);
 
@@ -4543,7 +4543,7 @@ _hidden int libxl__ms_vm_genid_set(libxl__gc *gc, uint32_t domid,
 #define LIBXL__DEFBOOL_STR_DEFAULT "<default>"
 #define LIBXL__DEFBOOL_STR_FALSE   "False"
 #define LIBXL__DEFBOOL_STR_TRUE    "True"
-static inline int libxl__defbool_is_default(libxl_defbool *db)
+static inline int libxl__defbool_is_default(const libxl_defbool *db)
 {
     return !db->val;
 }
@@ -4638,22 +4638,22 @@ int libxl__random_bytes(libxl__gc *gc, uint8_t *buf, size_t len);
 #include "_libxl_types_internal_private.h"
 
 /* This always return false, there's no "default value" for hw cap */
-static inline int libxl__hwcap_is_default(libxl_hwcap *hwcap)
+static inline int libxl__hwcap_is_default(const libxl_hwcap *hwcap)
 {
     return 0;
 }
 
-static inline int libxl__string_list_is_empty(libxl_string_list *psl)
+static inline int libxl__string_list_is_empty(const libxl_string_list *psl)
 {
     return !libxl_string_list_length(psl);
 }
 
-static inline int libxl__key_value_list_is_empty(libxl_key_value_list *pkvl)
+static inline int libxl__key_value_list_is_empty(const libxl_key_value_list *pkvl)
 {
     return !libxl_key_value_list_length(pkvl);
 }
 
-int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl);
+int libxl__cpuid_policy_is_empty(const libxl_cpuid_policy_list *pl);
 
 /* Portability note: a proper flock(2) implementation is required */
 typedef struct {
@@ -4787,12 +4787,12 @@ void* libxl__device_list(libxl__gc *gc, const libxl__device_type *dt,
 void libxl__device_list_free(const libxl__device_type *dt,
                              void *list, int num);
 
-static inline bool libxl__timer_mode_is_default(libxl_timer_mode *tm)
+static inline bool libxl__timer_mode_is_default(const libxl_timer_mode *tm)
 {
     return *tm == LIBXL_TIMER_MODE_DEFAULT;
 }
 
-static inline bool libxl__string_is_default(char **s)
+static inline bool libxl__string_is_default(char *const *s)
 {
     return *s == NULL;
 }
tools/libs/light/libxl_json.c +17 -16
index 68f36901c2..38c27a9f4b 100644
@@ -139,7 +139,7 @@ yajl_gen_status libxl__yajl_gen_enum(yajl_gen hand, const char *str)
  * YAJL generators for builtin libxl types.
  */
 #ifdef HAVE_LIBJSONC
-int libxl_defbool_gen_jso(json_object **jso_r, libxl_defbool *db)
+int libxl_defbool_gen_jso(json_object **jso_r, const libxl_defbool *db)
 {
     *jso_r = json_object_new_string(libxl_defbool_to_string(*db));
     if (!*jso_r)
@@ -150,7 +150,7 @@ int libxl_defbool_gen_jso(json_object **jso_r, libxl_defbool *db)
 
 #ifdef HAVE_LIBYAJL
 yajl_gen_status libxl_defbool_gen_json(yajl_gen hand,
-                                       libxl_defbool *db)
+                                       const libxl_defbool *db)
 {
     return libxl__yajl_gen_asciiz(hand, libxl_defbool_to_string(*db));
 }
@@ -203,7 +203,7 @@ int libxl__bool_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 #ifdef HAVE_LIBJSONC
-int libxl_uuid_gen_jso(json_object **jso_r, libxl_uuid *uuid)
+int libxl_uuid_gen_jso(json_object **jso_r, const libxl_uuid *uuid)
 {
     char buf[LIBXL_UUID_FMTLEN+1];
     snprintf(buf, sizeof(buf), LIBXL_UUID_FMT, LIBXL_UUID_BYTES((*uuid)));
@@ -216,7 +216,7 @@ int libxl_uuid_gen_jso(json_object **jso_r, libxl_uuid *uuid)
 
 #ifdef HAVE_LIBYAJL
 yajl_gen_status libxl_uuid_gen_json(yajl_gen hand,
-                                    libxl_uuid *uuid)
+                                    const libxl_uuid *uuid)
 {
     char buf[LIBXL_UUID_FMTLEN+1];
     snprintf(buf, sizeof(buf), LIBXL_UUID_FMT, LIBXL_UUID_BYTES((*uuid)));
@@ -234,7 +234,7 @@ int libxl__uuid_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 #ifdef HAVE_LIBJSONC
-int libxl_bitmap_gen_jso(json_object **jso_r, libxl_bitmap *bitmap)
+int libxl_bitmap_gen_jso(json_object **jso_r, const libxl_bitmap *bitmap)
 {
     json_object *jso;
     int i;
@@ -267,7 +267,7 @@ out:
 
 #ifdef HAVE_LIBYAJL
 yajl_gen_status libxl_bitmap_gen_json(yajl_gen hand,
-                                      libxl_bitmap *bitmap)
+                                      const libxl_bitmap *bitmap)
 {
     yajl_gen_status s;
     int i;
@@ -322,7 +322,7 @@ int libxl__bitmap_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 #ifdef HAVE_LIBJSONC
-int libxl_key_value_list_gen_jso(json_object **jso_r, libxl_key_value_list *pkvl)
+int libxl_key_value_list_gen_jso(json_object **jso_r, const libxl_key_value_list *pkvl)
 {
     libxl_key_value_list kvl = *pkvl;
     json_object *jso;
@@ -358,7 +358,7 @@ out:
 
 #ifdef HAVE_LIBYAJL
 yajl_gen_status libxl_key_value_list_gen_json(yajl_gen hand,
-                                              libxl_key_value_list *pkvl)
+                                              const libxl_key_value_list *pkvl)
 {
     libxl_key_value_list kvl = *pkvl;
     yajl_gen_status s;
@@ -421,7 +421,7 @@ int libxl__key_value_list_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 #ifdef HAVE_LIBJSONC
-int libxl_string_list_gen_jso(json_object **jso_r, libxl_string_list *pl)
+int libxl_string_list_gen_jso(json_object **jso_r, const libxl_string_list *pl)
 {
     libxl_string_list l = *pl;
     json_object *jso;
@@ -453,7 +453,7 @@ out:
 #endif
 
 #ifdef HAVE_LIBYAJL
-yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, libxl_string_list *pl)
+yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, const libxl_string_list *pl)
 {
     libxl_string_list l = *pl;
     yajl_gen_status s;
@@ -508,7 +508,7 @@ int libxl__string_list_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 #ifdef HAVE_LIBJSONC
-int libxl_mac_gen_jso(json_object **jso_r, libxl_mac *mac)
+int libxl_mac_gen_jso(json_object **jso_r, const libxl_mac *mac)
 {
     char buf[LIBXL_MAC_FMTLEN+1];
     snprintf(buf, sizeof(buf), LIBXL_MAC_FMT, LIBXL_MAC_BYTES((*mac)));
@@ -520,7 +520,7 @@ int libxl_mac_gen_jso(json_object **jso_r, libxl_mac *mac)
 #endif
 
 #ifdef HAVE_LIBYAJL
-yajl_gen_status libxl_mac_gen_json(yajl_gen hand, libxl_mac *mac)
+yajl_gen_status libxl_mac_gen_json(yajl_gen hand, const libxl_mac *mac)
 {
     char buf[LIBXL_MAC_FMTLEN+1];
     snprintf(buf, sizeof(buf), LIBXL_MAC_FMT, LIBXL_MAC_BYTES((*mac)));
@@ -538,7 +538,7 @@ int libxl__mac_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 #ifdef HAVE_LIBJSONC
-int libxl_hwcap_gen_jso(json_object **jso_r, libxl_hwcap *p)
+int libxl_hwcap_gen_jso(json_object **jso_r, const libxl_hwcap *p)
 {
     json_object *jso;
     int i;
@@ -568,7 +568,7 @@ out:
 
 #ifdef HAVE_LIBYAJL
 yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand,
-                                     libxl_hwcap *p)
+                                     const libxl_hwcap *p)
 {
     yajl_gen_status s;
     int i;
@@ -608,7 +608,7 @@ int libxl__hwcap_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 #ifdef HAVE_LIBJSONC
-int libxl_ms_vm_genid_gen_jso(json_object **jso_r, libxl_ms_vm_genid *p)
+int libxl_ms_vm_genid_gen_jso(json_object **jso_r, const libxl_ms_vm_genid *p)
 {
     json_object *jso;
     int i;
@@ -638,7 +638,8 @@ out:
 #endif
 
 #ifdef HAVE_LIBYAJL
-yajl_gen_status libxl_ms_vm_genid_gen_json(yajl_gen hand, libxl_ms_vm_genid *p)
+yajl_gen_status libxl_ms_vm_genid_gen_json(yajl_gen hand,
+                                           const libxl_ms_vm_genid *p)
 {
     yajl_gen_status s;
     int i;
tools/libs/light/libxl_nocpuid.c +2 -2
index ce334633b3..35c465b329 100644
@@ -14,7 +14,7 @@
 
 #include "libxl_internal.h"
 
-int libxl__cpuid_policy_is_empty(libxl_cpuid_policy_list *pl)
+int libxl__cpuid_policy_is_empty(const libxl_cpuid_policy_list *pl)
 {
     return 1;
 }
@@ -50,7 +50,7 @@ int libxl_cpuid_policy_list_gen_jso(json_object **jso_r, libxl_cpuid_policy_list
 
 #ifdef HAVE_LIBYAJL
 yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand,
-                                libxl_cpuid_policy_list *pcpuid)
+                                const libxl_cpuid_policy_list *pcpuid)
 {
     return 0;
 }
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/5 tools/xl: Mark libxl_domain_config * arg of printf_info_*() const

In reply to Elliott Mitchell

Date: Fri, 18 Dec 2020 13:32:33 -0800

With libxl having gotten a lot more constant, now printf_info_sexp() and printf_info_one_json() can add consts. May not be particularly important, but it is best to mark things constant when they are known to be so.

---
 tools/xl/xl.h      | 2 +-
 tools/xl/xl_info.c | 4 ++--
 tools/xl/xl_sxp.c  | 6 +++---
 3 files changed, 6 insertions(+), 6 deletions(-)
tools/xl/xl.h +1 -1
index 7b75f9a493..dcd77d1ec4 100644
@@ -308,7 +308,7 @@ typedef enum {
     DOMAIN_RESTART_SUSPENDED,    /* Domain suspended - keep looping */
 } domain_restart_type;
 
-extern void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh);
+extern void printf_info_sexp(int domid, const libxl_domain_config *d_config, FILE *fh);
 extern void apply_global_affinity_masks(libxl_domain_type type,
                                         libxl_bitmap *vcpu_affinity_array,
                                         unsigned int size);
tools/xl/xl_info.c +2 -2
index 98fc879e63..9540384beb 100644
@@ -63,7 +63,7 @@ static int maybe_printf(const char *fmt, ...)
 
 #ifdef HAVE_LIBJSONC
 static int printf_info_one_json(json_object **jso_r, int domid,
-                                libxl_domain_config *d_config)
+                                const libxl_domain_config *d_config)
 {
     json_object *jso = NULL;
     json_object *jso_config = NULL;
@@ -108,7 +108,7 @@ out:
 #elif defined(HAVE_LIBYAJL)
 
 static yajl_gen_status printf_info_one_json(yajl_gen hand, int domid,
-                                            libxl_domain_config *d_config)
+                                            const libxl_domain_config *d_config)
 {
     yajl_gen_status s;
 
tools/xl/xl_sxp.c +3 -3
index 4383ad177a..d2347e401a 100644
@@ -26,13 +26,13 @@
 /* In general you should not add new output to this function since it
  * is intended only for legacy use.
  */
-void printf_info_sexp(int domid, libxl_domain_config *d_config, FILE *fh)
+void printf_info_sexp(int domid, const libxl_domain_config *d_config, FILE *fh)
 {
     int i;
     libxl_dominfo info;
 
-    libxl_domain_create_info *c_info = &d_config->c_info;
-    libxl_domain_build_info *b_info = &d_config->b_info;
+    const libxl_domain_create_info *c_info = &d_config->c_info;
+    const libxl_domain_build_info *b_info = &d_config->b_info;
 
     fprintf(fh, "(domain\n\t(domid %d)\n", domid);
     fprintf(fh, "\t(create_info)\n");
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/5 tools/libxl: Tiny optimization of libxl__mac_is_default()

In reply to Elliott Mitchell

Date: Fri, 18 Dec 2020 14:45:02 -0800

This should result in fewer branch instructions and a small performance gain.

---
 tools/libs/light/libxl_internal.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libs/light/libxl_internal.c +2 -2
index cd99d249bf..b5b4825619 100644
@@ -347,8 +347,8 @@ _hidden int libxl__compare_macs(const libxl_mac *a, const libxl_mac *b)
 
 _hidden int libxl__mac_is_default(const libxl_mac *mac)
 {
-    return (!(*mac)[0] && !(*mac)[1] && !(*mac)[2] &&
-            !(*mac)[3] && !(*mac)[4] && !(*mac)[5]);
+    return !((*mac)[0] | (*mac)[1] | (*mac)[2] |
+             (*mac)[3] | (*mac)[4] | (*mac)[5]);
 }
 
 _hidden int libxl__init_recursive_mutex(libxl_ctx *ctx, pthread_mutex_t *lock)
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/5 tools/xl: Rename printf_info()/list_domains_details() to dump_by_...()

In reply to Elliott Mitchell

Date: Thu, 17 Dec 2020 17:42:42 -0800

printf_info()/list_domains_details() had been serving fairly similar purposes. Increase their consistency (add file-handle and output_format arguments to list_domains_details(), reorder arguments) and then rename to better reflect their functionality.

Both were simply outputting full domain information. As this is more of a dump operation, "dump" is a better name.

---
 tools/xl/xl.h      | 11 ++++++++---
 tools/xl/xl_info.c | 29 +++++++++++++++++------------
 tools/xl/xl_misc.c |  2 +-
 3 files changed, 26 insertions(+), 16 deletions(-)
tools/xl/xl.h +8 -3
index dcd77d1ec4..7fd33db800 100644
@@ -308,13 +308,18 @@ typedef enum {
     DOMAIN_RESTART_SUSPENDED,    /* Domain suspended - keep looping */
 } domain_restart_type;
 
+extern void dump_by_config(enum output_format output_format,
+                           FILE *fh,
+                           const libxl_domain_config *d_config,
+                           int domid);
+extern void dump_by_dominfo_list(enum output_format output_format,
+                                 FILE *fh,
+                                 const libxl_dominfo info[],
+                                 int nb_domain);
 extern void printf_info_sexp(int domid, const libxl_domain_config *d_config, FILE *fh);
 extern void apply_global_affinity_masks(libxl_domain_type type,
                                         libxl_bitmap *vcpu_affinity_array,
                                         unsigned int size);
-void printf_info(enum output_format output_format,
-                 int domid,
-                 libxl_domain_config *d_config, FILE *fh);
 
 #define XL_GLOBAL_CONFIG XEN_CONFIG_DIR "/xl.conf"
 #define XL_LOCK_FILE XEN_LOCK_DIR "/xl"
tools/xl/xl_info.c +17 -12
index 9540384beb..46bba4bb54 100644
@@ -144,9 +144,10 @@ out:
 }
 #endif
 
-void printf_info(enum output_format output_format,
-                 int domid,
-                 libxl_domain_config *d_config, FILE *fh)
+void dump_by_config(enum output_format output_format,
+                    FILE *fh,
+                    const libxl_domain_config *const d_config,
+                    int domid)
 {
     if (output_format == OUTPUT_FORMAT_SXP)
         return printf_info_sexp(domid, d_config, fh);
@@ -540,7 +541,10 @@ static void list_domains(bool verbose, bool context, bool claim, bool numa,
     libxl_physinfo_dispose(&physinfo);
 }
 
-static void list_domains_details(const libxl_dominfo *info, int nb_domain)
+void dump_by_dominfo_list(enum output_format output_format,
+                          FILE *fh,
+                          const libxl_dominfo info[],
+                          int nb_domain)
 {
     libxl_domain_config d_config;
 
@@ -556,11 +560,11 @@ static void list_domains_details(const libxl_dominfo *info, int nb_domain)
 #endif
 
 #ifdef HAVE_LIBJSONC
-    if (default_output_format == OUTPUT_FORMAT_JSON) {
+    if (output_format == OUTPUT_FORMAT_JSON) {
         jso = json_object_new_array();
     }
 #elif defined(HAVE_LIBYAJL)
-    if (default_output_format == OUTPUT_FORMAT_JSON) {
+    if (output_format == OUTPUT_FORMAT_JSON) {
         hand = libxl_yajl_gen_alloc(NULL);
         if (!hand) {
             fprintf(stderr, "unable to allocate JSON generator\n");
@@ -580,7 +584,7 @@ static void list_domains_details(const libxl_dominfo *info, int nb_domain)
                                                  &d_config, NULL);
         if (rc)
             continue;
-        if (default_output_format == OUTPUT_FORMAT_JSON) {
+        if (output_format == OUTPUT_FORMAT_JSON) {
 #ifdef HAVE_LIBJSONC
             json_object *jso_value;
             rc = printf_info_one_json(&jso_value, info[i].domid, &d_config);
@@ -590,7 +594,7 @@ static void list_domains_details(const libxl_dominfo *info, int nb_domain)
             s = printf_info_one_json(hand, info[i].domid, &d_config);
 #endif
         } else
-            printf_info_sexp(info[i].domid, &d_config, stdout);
+            printf_info_sexp(info[i].domid, &d_config, fh);
         libxl_domain_config_dispose(&d_config);
 #ifdef HAVE_LIBJSONC
         if (rc)
@@ -601,7 +605,7 @@ static void list_domains_details(const libxl_dominfo *info, int nb_domain)
 #endif
     }
 
-    if (default_output_format == OUTPUT_FORMAT_JSON) {
+    if (output_format == OUTPUT_FORMAT_JSON) {
 #ifdef HAVE_LIBJSONC
         buf = json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY);
         if (!buf)
@@ -616,11 +620,12 @@ static void list_domains_details(const libxl_dominfo *info, int nb_domain)
             goto out;
 #endif
 
-        puts(buf);
+        fputs(buf, fh);
+        fputc('\n', fh);
     }
 
 out:
-    if (default_output_format == OUTPUT_FORMAT_JSON) {
+    if (output_format == OUTPUT_FORMAT_JSON) {
 #ifdef HAVE_LIBJSONC
         json_object_put(jso);
 #elif defined(HAVE_LIBYAJL)
@@ -701,7 +706,7 @@ int main_list(int argc, char **argv)
     }
 
     if (details)
-        list_domains_details(info, nb_domain);
+        dump_by_dominfo_list(default_output_format, stdout, info, nb_domain);
     else
         list_domains(verbose, context, false /* claim */, numa, cpupool,
                      info, nb_domain);
tools/xl/xl_misc.c +1 -1
index f0167e1603..bcf178762b 100644
@@ -341,7 +341,7 @@ int main_config_update(int argc, char **argv)
     parse_config_data(filename, config_data, config_len, &d_config);
 
     if (debug || dryrun_only)
-        printf_info(default_output_format, -1, &d_config, stdout);
+        dump_by_config(default_output_format, stdout, &d_config, -1);
 
     if (!dryrun_only) {
         fprintf(stderr, "setting dom%u configuration\n", domid);
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/5 tools/xl: Merge down debug/dry-run section of create_domain()

In reply to Elliott Mitchell

Date: Thu, 17 Dec 2020 17:42:42 -0800

create_domain()'s use of printf_info_sexp() could be merged down to a single dump_by_config(), do so. This results in an extra JSON dictionary in output, but I doubt that is an issue for dry-run or debugging output.

---
 tools/xl/xl_vmcontrol.c | 14 +-------------
 1 file changed, 1 insertion(+), 13 deletions(-)
tools/xl/xl_vmcontrol.c +1 -13
index 93766f631b..bfd87e6aef 100644
@@ -899,19 +899,7 @@ int create_domain(struct domain_create *dom_info)
 
     if (debug || dryrun_only) {
         FILE *cfg_print_fh = (debug && !dryrun_only) ? stderr : stdout;
-        if (default_output_format == OUTPUT_FORMAT_SXP) {
-            printf_info_sexp(-1, &d_config, cfg_print_fh);
-        } else {
-            char *json = libxl_domain_config_to_json(ctx, &d_config);
-            if (!json) {
-                fprintf(stderr,
-                        "Failed to convert domain configuration to JSON\n");
-                exit(1);
-            }
-            fputs(json, cfg_print_fh);
-            free(json);
-            flush_stream(cfg_print_fh);
-        }
+        dump_by_config(default_output_format, cfg_print_fh, &d_config, -1);
     }
 
 
Signature · 1 line
2.47.3