Skip to contentSkip to conversation

Addition of formatting options to `xl list` subcommands

PATCH Superseded Elliott Mitchell 1 participant 17 messages Jan 4, 2021

lore
Share & tools
lore
#0 lore raw

cover Addition of formatting options to `xl list` subcommands

The first 8 patches should be fairly uncontroversial, and could be preemptively applied before the rest are ready. They fell out as part of trying to implement my first step; splitting a few xl subcommands off of xl_info.c.

Found several functions in libxl which could have arguments marked const. Reorganized the interrelated set of printf_info(), list_domains_details(), printf_info_sexp() and printf_info_one_json().

The problem was list_domains_details() (replaced/renamed dump_by_dominfo_list()) was best suited towards remaining in xl_info.c, but main_list() was moved to xl_list.c.

One concern with the split, I tested which headers were actually required. I expected some to be required by one or the other file, and not both. Yet in the process I found my system didn't need <sys/stat.h>, <sys/types.h>, nor <libxlutil.h> in *either* resultant file. I have a concern these headers may be needed on other's systems even though my build didn't require them. In fact this is a concern for most of the headers which my system only required for one file.

The goal of this series is to drastically increase the utility of the xl list subcommand. Prior to this the limit of control was merely enabling the output of 4 groups of extra columns. The order of columns couldn't be specified, nor could specific subsets be specified. There was also no allowance for for using null line terminators.

With this series all of this becomes possible. Additionally for more flexible use with scripting, it is now possible to omit the initial header and Domain 0.

Unfortunately the output isn't *precisely* what was produced previously, but it is very similar.

Also, some of the output columns are things which I don't have tester for. I'm pretty sure I've got the "context", "claimed" and "shutdown reason" fields right, but I'm not setup to reliably produce those states. Thus for these three my testing is minimal, but the code is standard enough to be confident in.

The CPU Pool and NODE Affinity fields are things I haven't played with at all. While I'm hopeful I adapted the code appropriately, these *need* testing by someone with an appropriate setup.

I fully expect the last 8 patches to be partially or fully merged together when this becomes ready.

There is one big wart. At this revision, format_node() makes two allocations which are lost at exit. This isn't a big issue since they're only allocated *once*, but certainly isn't optimal.

Elliott Mitchell (16):
tools/libxl: Mark pointer args of many functions constant tools/libxl: Tiny optimization of libxl__mac_is_default() tools/xl: Mark libxl_domain_config * arg of printf_info_*() const tools/xl: Rename printf_info()/list_domains_details() to dump_by_...() tools/xl: Merge down debug/dry-run section of create_domain() tools/xl: Split list commands off of xl_info.c
tools/xl: Sort list command options
tools/xl: Fix potential deallocation bug
WIP: tools/xl: Implement generalized output formatting for xl list
WIP: tools/xl: Implement output format option
WIP: tools/xl: Replace most of list_domains with use of format()
WIP: UNTESTED: tools/xl: Replace remaining options with format()
WIP: tools/xl: Purge list_domains()
WIP: tools/xl: Enhance "list" command
WIP: tools/xl: Implement output format option for "vm-list" command
WIP: tools/xl: Enhance "vm-list" command

 tools/include/libxl_json.h        |  22 +-
 tools/libs/light/gentypes.py      |   8 +-
 tools/libs/light/libxl_cpuid.c    |   2 +-
 tools/libs/light/libxl_internal.c |   8 +-
 tools/libs/light/libxl_internal.h |  18 +-
 tools/libs/light/libxl_json.c     |  18 +-
 tools/libs/light/libxl_nocpuid.c  |   4 +-
 tools/xl/Makefile                 |   2 +-
 tools/xl/xl.h                     |  10 +-
 tools/xl/xl_cmdtable.c            |  27 +-
 tools/xl/xl_info.c                | 251 +------------
 tools/xl/xl_list.c                | 587 ++++++++++++++++++++++++++++++
 tools/xl/xl_misc.c                |   5 +-
 tools/xl/xl_sxp.c                 |   6 +-
 tools/xl/xl_vmcontrol.c           |  14 +-
 15 files changed, 684 insertions(+), 298 deletions(-)
 create mode 100644 tools/xl/xl_list.c
Signature · 4 lines
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#1 lore raw

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

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        | 22 ++++++++++++----------
 tools/libs/light/gentypes.py      |  8 ++++----
 tools/libs/light/libxl_cpuid.c    |  2 +-
 tools/libs/light/libxl_internal.c |  4 ++--
 tools/libs/light/libxl_internal.h | 18 +++++++++---------
 tools/libs/light/libxl_json.c     | 18 ++++++++++--------
 tools/libs/light/libxl_nocpuid.c  |  4 ++--
 7 files changed, 40 insertions(+), 36 deletions(-)
tools/include/libxl_json.h +12 -10
index 260783bfde..63f0e58fe1 100644
@@ -23,17 +23,19 @@
 #endif
 
 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);
 
 #include <_libxl_types_json.h>
 
@@ -91,6 +93,6 @@ static inline yajl_gen libxl_yajl_gen_alloc(const yajl_alloc_funcs *allocFuncs)
 #endif /* !HAVE_YAJL_V2 */
 
 yajl_gen_status libxl_domain_config_gen_json(yajl_gen hand,
-                                             libxl_domain_config *p);
+                                             const libxl_domain_config *p);
 
 #endif /* LIBXL_JSON_H */
tools/libs/light/gentypes.py +4 -4
index 9a45e45acc..7e02a5366f 100644
@@ -632,7 +632,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):
@@ -662,7 +662,7 @@ if __name__ == '__main__':
 """ % (header_json_define, header_json_define, " ".join(sys.argv)))
 
     for ty in [ty for ty in types if ty.json_gen_fn is not None]:
-        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)
@@ -766,13 +766,13 @@ if __name__ == '__main__':
         f.write("\n")
 
     for ty in [t for t in types if t.json_gen_fn is not None]:
-        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")
         f.write(libxl_C_type_to_json(ty, "p"))
         f.write("}\n")
tools/libs/light/libxl_cpuid.c +1 -1
index 259612834e..1cb49e3154 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 !libxl_cpuid_policy_list_length(pl);
 }
tools/libs/light/libxl_internal.c +2 -2
index d93a75533f..32b8788b59 100644
@@ -332,7 +332,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;
 
@@ -344,7 +344,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 c79523ba92..b866827507 100644
@@ -2073,9 +2073,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);
 
@@ -4573,7 +4573,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;
 }
@@ -4668,22 +4668,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 {
@@ -4814,12 +4814,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 +10 -8
index 9b8ef2cab9..88e81f9905 100644
@@ -95,7 +95,7 @@ yajl_gen_status libxl__yajl_gen_enum(yajl_gen hand, const char *str)
  * YAJL generators for builtin libxl types.
  */
 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));
 }
@@ -137,7 +137,7 @@ int libxl__bool_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 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)));
@@ -154,7 +154,7 @@ int libxl__uuid_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 yajl_gen_status libxl_bitmap_gen_json(yajl_gen hand,
-                                      libxl_bitmap *bitmap)
+                                      const libxl_bitmap *bitmap)
 {
     yajl_gen_status s;
     int i;
@@ -208,7 +208,7 @@ int libxl__bitmap_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 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;
@@ -269,7 +269,8 @@ int libxl__key_value_list_parse_json(libxl__gc *gc, const libxl__json_object *o,
     return 0;
 }
 
-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;
@@ -322,7 +323,7 @@ int libxl__string_list_parse_json(libxl__gc *gc, const libxl__json_object *o,
     return 0;
 }
 
-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)));
@@ -339,7 +340,7 @@ int libxl__mac_parse_json(libxl__gc *gc, const libxl__json_object *o,
 }
 
 yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand,
-                                     libxl_hwcap *p)
+                                     const libxl_hwcap *p)
 {
     yajl_gen_status s;
     int i;
@@ -377,7 +378,8 @@ int libxl__hwcap_parse_json(libxl__gc *gc, const libxl__json_object *o,
     return 0;
 }
 
-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 f47336565b..73580351b3 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;
 }
@@ -40,7 +40,7 @@ void libxl__cpuid_legacy(libxl_ctx *ctx, uint32_t domid, bool restore,
 }
 
 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 · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#2 lore raw

PATCH 2/16 tools/libxl: Tiny optimization of libxl__mac_is_default()

In reply to Elliott Mitchell

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 32b8788b59..b4b2eb4deb 100644
@@ -346,8 +346,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 · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#3 lore raw

PATCH 3/16 tools/xl: Mark libxl_domain_config * arg of printf_info_*() const

In reply to Elliott Mitchell

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 | 2 +-
 tools/xl/xl_sxp.c  | 6 +++---
 3 files changed, 5 insertions(+), 5 deletions(-)
tools/xl/xl.h +1 -1
index 06569c6c4a..ffb222d280 100644
@@ -299,7 +299,7 @@ typedef enum {
     DOMAIN_RESTART_SOFT_RESET,   /* Soft reset should be performed */
 } 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 +1 -1
index ca417df8e8..cc50463df6 100644
@@ -59,7 +59,7 @@ static int maybe_printf(const char *fmt, ...)
 }
 
 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 359a001570..d5b9051dfc 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 · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#4 lore raw

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

In reply to Elliott Mitchell

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      |  8 ++++++++
 tools/xl/xl_info.c | 30 ++++++++++++++++--------------
 tools/xl/xl_misc.c |  5 +----
 3 files changed, 25 insertions(+), 18 deletions(-)
tools/xl/xl.h +8 -0
index ffb222d280..760c8fcf40 100644
@@ -299,6 +299,14 @@ typedef enum {
     DOMAIN_RESTART_SOFT_RESET,   /* Soft reset should be performed */
 } 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,
tools/xl/xl_info.c +16 -14
index cc50463df6..bc88014f10 100644
@@ -94,12 +94,10 @@ out:
     return s;
 }
 
-void printf_info(enum output_format output_format,
-                 int domid,
-                 libxl_domain_config *d_config, FILE *fh);
-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);
@@ -438,7 +436,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;
 
@@ -449,7 +450,7 @@ static void list_domains_details(const libxl_dominfo *info, int nb_domain)
     const char *buf;
     libxl_yajl_length yajl_len = 0;
 
-    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");
@@ -468,16 +469,16 @@ 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)
             s = printf_info_one_json(hand, info[i].domid, &d_config);
         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);
         if (s != yajl_gen_status_ok)
             goto out;
     }
 
-    if (default_output_format == OUTPUT_FORMAT_JSON) {
+    if (output_format == OUTPUT_FORMAT_JSON) {
         s = yajl_gen_array_close(hand);
         if (s != yajl_gen_status_ok)
             goto out;
@@ -486,11 +487,12 @@ static void list_domains_details(const libxl_dominfo *info, int nb_domain)
         if (s != yajl_gen_status_ok)
             goto out;
 
-        puts(buf);
+        fputs(buf, fh);
+        fputc('\n', fh);
     }
 
 out:
-    if (default_output_format == OUTPUT_FORMAT_JSON) {
+    if (output_format == OUTPUT_FORMAT_JSON) {
         yajl_gen_free(hand);
         if (s != yajl_gen_status_ok)
             fprintf(stderr,
@@ -567,7 +569,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 -4
index 08f0fb6dc9..bcf178762b 100644
@@ -256,9 +256,6 @@ int main_dump_core(int argc, char **argv)
     return EXIT_SUCCESS;
 }
 
-extern void printf_info(enum output_format output_format,
-                        int domid,
-                        libxl_domain_config *d_config, FILE *fh);
 int main_config_update(int argc, char **argv)
 {
     uint32_t domid;
@@ -344,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 · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#5 lore raw

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

In reply to Elliott Mitchell

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 435155a033..4b95e7e463 100644
@@ -856,19 +856,7 @@ int create_domain(struct domain_create *dom_info)
 
     if (debug || dom_info->dryrun) {
         FILE *cfg_print_fh = (debug && !dom_info->dryrun) ? 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 · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#6 lore raw

PATCH 6/16 tools/xl: Split list commands off of xl_info.c

In reply to Elliott Mitchell

The domain listing commands have more in common with each other than hypervisor information commands.

---
I'm concerned about my header-stripping job. Notably the headers <sys/stat.h>, <sys/types.h>, and <libxlutil.h> were removed from *both* files. This could be cause for celebration, or this could mean my build system's headers are nice and someone else's system needs these.

---
 tools/xl/Makefile  |   2 +-
 tools/xl/xl_info.c | 221 ---------------------------------------
 tools/xl/xl_list.c | 254 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 255 insertions(+), 222 deletions(-)
 create mode 100644 tools/xl/xl_list.c
tools/xl/Makefile +1 -1
index bdf67c8464..eb20d834d4 100644
@@ -21,7 +21,7 @@ XL_OBJS = xl.o xl_cmdtable.o xl_sxp.o xl_utils.o $(XL_OBJS-y)
 XL_OBJS += xl_parse.o xl_cpupool.o xl_flask.o
 XL_OBJS += xl_vtpm.o xl_block.o xl_nic.o xl_usb.o
 XL_OBJS += xl_sched.o xl_pci.o xl_vcpu.o xl_cdrom.o xl_mem.o
-XL_OBJS += xl_info.o xl_console.o xl_misc.o
+XL_OBJS += xl_info.o xl_list.o xl_console.o xl_misc.o
 XL_OBJS += xl_vmcontrol.o xl_saverestore.o xl_migrate.o
 XL_OBJS += xl_vdispl.o xl_vsnd.o xl_vkb.o
 
tools/xl/xl_info.c +0 -221

251 lines not on this page · raw message

tools/xl/xl_list.c new file +254 -0

254 lines not on this page · raw message

Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#7 lore raw

PATCH 7/16 tools/xl: Sort list command options

In reply to Elliott Mitchell

Simply a minor housekeeping task. Unfortunately no single order really dominates. Some spots use the option name, some the option letter.

---
 tools/xl/xl_cmdtable.c |  4 ++--
 tools/xl/xl_list.c     | 22 +++++++++++-----------
 2 files changed, 13 insertions(+), 13 deletions(-)
tools/xl/xl_cmdtable.c +2 -2
index 6ab5e47da3..6a05bf7ce2 100644
@@ -53,11 +53,11 @@ struct cmd_spec cmd_table[] = {
       &main_list, 0, 0,
       "List information about all/some domains",
       "[options] [Domain]\n",
-      "-l, --long              Output all VM details\n"
-      "-v, --verbose           Prints out UUIDs and security context\n"
       "-Z, --context           Prints out security context\n"
       "-c, --cpupool           Prints the cpupool the domain is in\n"
+      "-l, --long              Output all VM details\n"
       "-n, --numa              Prints out NUMA node affinity"
+      "-v, --verbose           Prints out UUIDs and security context\n"
     },
     { "destroy",
       &main_destroy, 0, 1,
tools/xl/xl_list.c +11 -11
index ac6a9e5eac..8b391a9056 100644
@@ -129,17 +129,17 @@ static void list_domains(bool verbose, bool context, bool claim, bool numa,
 int main_list(int argc, char **argv)
 {
     int opt;
-    bool verbose = false;
     bool context = false;
-    bool details = false;
     bool cpupool = false;
+    bool details = false;
     bool numa = false;
+    bool verbose = false;
     static struct option opts[] = {
-        {"long", 0, 0, 'l'},
-        {"verbose", 0, 0, 'v'},
         {"context", 0, 0, 'Z'},
         {"cpupool", 0, 0, 'c'},
+        {"long", 0, 0, 'l'},
         {"numa", 0, 0, 'n'},
+        {"verbose", 0, 0, 'v'},
         COMMON_LONG_OPTS
     };
 
@@ -147,22 +147,22 @@ int main_list(int argc, char **argv)
     libxl_dominfo *info, *info_free=0;
     int nb_domain, rc;
 
-    SWITCH_FOREACH_OPT(opt, "lvhZcn", opts, "list", 0) {
-    case 'l':
-        details = true;
-        break;
-    case 'v':
-        verbose = true;
-        break;
+    SWITCH_FOREACH_OPT(opt, "Zchlnv", opts, "list", 0) {
     case 'Z':
         context = true;
         break;
     case 'c':
         cpupool = true;
         break;
+    case 'l':
+        details = true;
+        break;
     case 'n':
         numa = true;
         break;
+    case 'v':
+        verbose = true;
+        break;
     }
 
     libxl_dominfo_init(&info_buf);
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#8 lore raw

PATCH 8/16 tools/xl: Fix potential deallocation bug

In reply to Elliott Mitchell

There is potential for the info and info_free variable's purposes to diverge. If info was overwritten with a distinct value, yet info_free still needed deallocation a bug would occur on this line. Preemptively address this issue (making use of divergent info/info_free values is under consideration).

---
 tools/xl/xl_list.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/xl/xl_list.c +1 -1
index 8b391a9056..e30536fd9a 100644
@@ -200,7 +200,7 @@ int main_list(int argc, char **argv)
                      info, nb_domain);
 
     if (info_free)
-        libxl_dominfo_list_free(info, nb_domain);
+        libxl_dominfo_list_free(info_free, nb_domain);
 
     libxl_dominfo_dispose(&info_buf);
 
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#9 lore raw

PATCH 9/16 WIP: tools/xl: Implement generalized output formatting for `xl list`

In reply to Elliott Mitchell

Implement a generalized output formatting function for the xl list subcommands. Notably xl list and xl vm-list could make use of alternative output list formats.

---
I'm a bit unsure of #include <xen-tools/libs.h>. When looking for an implementation of ARRAY_SIZE(), that was the header I found. I can readily write it myself, but rather than inlining, I looked for a copy in a header and found that.

---
 tools/xl/xl_list.c | 285 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 285 insertions(+)
tools/xl/xl_list.c +285 -0
index e30536fd9a..1c04f2126b 100644
@@ -14,10 +14,14 @@
 
 #define _GNU_SOURCE
 
+#include <ctype.h>
 #include <inttypes.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <unistd.h>
 
+#include <xen-tools/libs.h>
+
 #include <libxl.h>
 #include <libxl_utils.h>
 
@@ -25,6 +29,287 @@
 #include "xl_utils.h"
 
 
+struct format_entry;
+
+typedef void (format_function_t)(const struct format_entry *,
+                                 const void *, const char *, size_t);
+
+typedef struct format_entry {
+    char *const header;
+    char formatc[3];
+    format_function_t *formatf;
+    ptrdiff_t offset;
+    union {
+        int i;
+        unsigned long lu;
+        float f;
+        void *v;
+        char *(*xlfunc)(libxl_ctx *, uint32_t);
+    } extra;
+} format_table_t['z' - 'A' + 1];
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+
+static void format_normal(const struct format_entry *entry,
+                          const void *info, const char *format,
+                          size_t len)
+{
+    char *buf = alloca(len + 3);
+    memcpy(buf, format, len);
+    if (info) {
+        const char *str;
+        int i;
+        unsigned long lu;
+        float f;
+        strcpy(buf + len, entry->formatc);
+        switch (entry->formatc[0]) {
+        case 's':
+            str = ((char **)info)[entry->offset];
+            if (!str) str = "-";
+            printf(buf, str);
+            break;
+        case 'f':
+            f = ((float *)info)[entry->offset];
+            if (entry->extra.f != 0) f /= entry->extra.f;
+            printf(buf, f);
+            break;
+        case 'l':
+            lu = ((unsigned long *)info)[entry->offset];
+            if (entry->extra.lu) lu /= entry->extra.lu;
+            printf(buf, lu);
+            break;
+        case 'd':
+        default:
+            i = ((int *)info)[entry->offset];
+            if (entry->extra.i) i /= entry->extra.i;
+            printf(buf, i);
+            break;
+        }
+    } else {
+        if (entry->formatc[0] == 'f') {
+            char *tmp;
+            buf[len] = '\0';
+            if ((tmp = rindex(buf, '.')))
+                len = tmp - buf - 1;
+        }
+        strcpy(buf + len, "s");
+        printf(buf, entry->header);
+    }
+}
+
+static void format_allocstr(const struct format_entry *entry,
+                            const void *info, const char *format,
+                            size_t len)
+{
+    char *fmt = alloca(len + 2);
+    char *outbuf;
+    memcpy(fmt, format, len);
+    strcpy(fmt + len, "s");
+
+    if (info) {
+        outbuf = entry->extra.xlfunc(ctx, ((uint32_t *)info)[entry->offset]);
+        printf(fmt, outbuf);
+        free(outbuf);
+    } else printf(fmt, entry->header);
+}
+
+static void format_uuid(const struct format_entry *entry,
+                        const void *info, const char *format,
+                        size_t len)
+{
+    if (info) printf(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(*(libxl_uuid *)((char *)info + entry->offset)));
+    else fputs(entry->header, stdout);
+}
+
+static void format_time(const struct format_entry *entry,
+                        const void *_info, const char *format,
+                        size_t len)
+{
+    const libxl_dominfo *info = _info;
+    char *fmt = alloca(len + 2);
+    memcpy(fmt, format, len);
+
+    if (info) {
+        strcpy(fmt + len, "f");
+        printf(fmt, ((float)info->cpu_time / 1e9));
+    } else {
+        char *tmp;
+        if (!(tmp = index(fmt, '.'))) tmp = fmt + len;
+        strcpy(tmp, "s");
+        printf(fmt, entry->header);
+    }
+}
+
+static void format_state(const struct format_entry *entry,
+                         const void *_info, const char *format,
+                         size_t len)
+{
+    const libxl_dominfo *info = _info;
+    if (info) {
+        const char shutdown_reason_letters[] = "-rscwS";
+        libxl_shutdown_reason shutdown_reason;
+        static const char letters[] = "rbps";
+        int i;
+	const bool *flags;
+
+	flags = &info->running;
+        for (i = 0; i < strlen(letters); ++i)
+            putchar(flags[i] ? letters[i] : '-');
+
+        shutdown_reason = info->shutdown ? info->shutdown_reason : 0;
+        putchar((shutdown_reason >= 0 &&
+            shutdown_reason < sizeof(shutdown_reason_letters)-1
+            ? shutdown_reason_letters[shutdown_reason] : '?'));
+
+        putchar(info->dying ? 'd' : '-');
+    } else printf("%6s", entry->header);
+}
+
+static void format_reason(const struct format_entry *entry,
+                          const void *_info, const char *format,
+                          size_t len)
+{
+    const libxl_dominfo *info = _info;
+    const char *output = entry->header;
+    if (info) {
+        if (info->shutdown) {
+            printf("%8x", info->shutdown_reason);
+            return;
+        }
+        output = "-";
+    }
+    printf("%8s", output);
+}
+
+static void format_memory(const struct format_entry *entry,
+                          const void *_info, const char *format,
+                          size_t len)
+{
+    const libxl_dominfo *info = _info;
+    char *fmt = alloca(len + 3);
+    memcpy(fmt, format, len);
+    if (info) {
+        strcpy(fmt + len, entry->formatc);
+        printf(fmt, (info->current_memkb + info->outstanding_memkb) >> 10);
+    } else {
+        strcpy(fmt + len, "s");
+        printf(fmt, "Mem");
+    }
+}
+
+static void format_node(const struct format_entry *entry,
+                        const void *_info, const char *format,
+                        size_t len)
+{
+    const libxl_dominfo *info = _info;
+    static bool need_init = true;
+    static libxl_bitmap nodemap;
+    static libxl_physinfo physinfo;
+
+    if (need_init) {
+        libxl_bitmap_init(&nodemap);
+        libxl_physinfo_init(&physinfo);
+        need_init = false;
+
+        if (libxl_node_bitmap_alloc(ctx, &nodemap, 0)) {
+            fprintf(stderr, "libxl_node_bitmap_alloc_failed.\n");
+            exit(EXIT_FAILURE);
+        }
+        if (libxl_get_physinfo(ctx, &physinfo) != 0) {
+            fprintf(stderr, "libxl_physinfo failed.\n");
+            libxl_bitmap_dispose(&nodemap);
+            exit(EXIT_FAILURE);
+        }
+    }
+
+    if (info) {
+        libxl_domain_get_nodeaffinity(ctx, info->domid, &nodemap);
+
+        putchar(' ');
+        print_bitmap(nodemap.map, physinfo.nr_nodes, stdout);
+    } else fputs(entry->header, stdout);
+
+#if 0
+    /* unfortunately these get leaked on exit */
+    libxl_bitmap_dispose(&nodemap);
+    libxl_physinfo_dispose(&physinfo);
+#endif
+}
+
+#pragma GCC diagnostic pop
+
+static bool isfmtchar(int c)
+{
+    const bool opts[] = {
+        ['0'] = true,
+        ['1'] = true,
+        ['2'] = true,
+        ['3'] = true,
+        ['4'] = true,
+        ['5'] = true,
+        ['6'] = true,
+        ['7'] = true,
+        ['8'] = true,
+        ['9'] = true,
+        ['.'] = true,
+        ['#'] = true,
+        ['-'] = true,
+        ['+'] = true,
+        [' '] = true,
+        ['\''] = true,
+    };
+    if ((unsigned int)c < ARRAY_SIZE(opts) && opts[c]) return true;
+    else return false;
+}
+
+static void format(const format_table_t fmttab, const char *fmt,
+                   const void *info)
+{
+    while (fmt[0]) {
+        if (fmt[0] == '\\') {
+            switch (fmt[1]) {
+            case 0:
+                /* Uhm... */
+                return;
+            case '0':
+                putchar(0);
+                break;
+            case 'n':
+                fputs("\n", stdout);
+                break;
+            case 't':
+                putchar('\t');
+                break;
+            default:
+                putchar(fmt[1]);
+            }
+            fmt+=2;
+        } else if (fmt[0] == '%') {
+            size_t len=1;
+            unsigned char entryn;
+
+            while (isfmtchar(fmt[len])) ++len;
+
+            entryn = fmt[len] - 'A';
+            if (entryn < sizeof(format_table_t) / sizeof(fmttab[0]) &&
+                    fmttab[entryn].formatf)
+                fmttab[entryn].formatf(fmttab + entryn, info, fmt, len);
+            else {
+                fprintf(stderr, "Invalid conversion character \'%c\'\n",
+                        entryn);
+                exit(EXIT_FAILURE);
+            }
+
+            fmt += len + 1;
+        } else {
+            putchar(*fmt);
+            ++fmt;
+        }
+    }
+}
+
+
 static void list_vm(void)
 {
     libxl_vminfo *info;
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#10 lore raw

PATCH 10/16 WIP: tools/xl: Implement output format option

In reply to Elliott Mitchell

Often it is desireable to only list a specific subset of fields, or list them in an unusual order. Previously xl list gave output in a fixed order, now add "-F" to allow specifying fields and formatting.

---
 tools/xl/xl_cmdtable.c | 14 ++++++++++++
 tools/xl/xl_list.c     | 50 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 62 insertions(+), 2 deletions(-)
tools/xl/xl_cmdtable.c +14 -0
index 6a05bf7ce2..f44c65a3f8 100644
@@ -53,6 +53,20 @@ struct cmd_spec cmd_table[] = {
       &main_list, 0, 0,
       "List information about all/some domains",
       "[options] [Domain]\n",
+      "-F, --format            Specify output format string\n"
+      "   Similar to printf(3) formatting, conversion characters are:\n"
+      "      %A    NODE Affinity\n"
+      "      %c    Claimed\n"
+      "      %i    Domain Id (%o, %x, and %X allow octal and hexadecimal)\n"
+      "      %l    Security label\n"
+      "      %m    Memory (Megabytes)\n"
+      "      %n    Domain name\n"
+      "      %p    CPU Pool\n"
+      "      %r    Shutdown reason\n"
+      "      %s    State\n"
+      "      %t    Time(s)\n"
+      "      %u    UUID\n"
+      "      %v    vCPUs\n"
       "-Z, --context           Prints out security context\n"
       "-c, --cpupool           Prints the cpupool the domain is in\n"
       "-l, --long              Output all VM details\n"
tools/xl/xl_list.c +48 -2

79 lines not on this page · raw message

Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#11 lore raw

PATCH 11/16 WIP: tools/xl: Replace most of list_domains with use of format()

In reply to Elliott Mitchell

With a generalized formatting function now available, start to replace the old specialized formatting bits.

---
 tools/xl/xl_list.c | 40 ++++++++++------------------------------
 1 file changed, 10 insertions(+), 30 deletions(-)
tools/xl/xl_list.c +10 -30
index c79b5e041b..10d076864e 100644
@@ -369,16 +369,18 @@ static void list_domains(bool verbose, bool context, bool claim, bool numa,
                          bool cpupool, const libxl_dominfo *info, int nb_domain)
 {
     int i;
-    static const char shutdown_reason_letters[]= "-rscwS";
+    const char lead[] = "%-40n %5i %5m %5v     %s  %8.1t";
+
     libxl_bitmap nodemap;
     libxl_physinfo physinfo;
 
     libxl_bitmap_init(&nodemap);
     libxl_physinfo_init(&physinfo);
 
-    printf("Name                                        ID   Mem VCPUs\tState\tTime(s)");
-    if (verbose) printf("   UUID                            Reason-Code\tSecurity Label");
-    if (context && !verbose) printf("   Security Label");
+    format(formats, lead, NULL);
+    if (verbose) {
+        format(formats, " %u %r %16l", NULL);
+    } else if (context) format(formats, " %16l", NULL);
     if (claim) printf("  Claimed");
     if (cpupool) printf("         Cpupool");
     if (numa) {
@@ -396,35 +398,13 @@ static void list_domains(bool verbose, bool context, bool claim, bool numa,
     }
     printf("\n");
     for (i = 0; i < nb_domain; i++) {
-        char *domname;
-        libxl_shutdown_reason shutdown_reason;
-        domname = libxl_domid_to_name(ctx, info[i].domid);
-        shutdown_reason = info[i].shutdown ? info[i].shutdown_reason : 0;
-        printf("%-40s %5d %5lu %5d     %c%c%c%c%c%c  %8.1f",
-                domname,
-                info[i].domid,
-                (unsigned long) ((info[i].current_memkb +
-                    info[i].outstanding_memkb)/ 1024),
-                info[i].vcpu_online,
-                info[i].running ? 'r' : '-',
-                info[i].blocked ? 'b' : '-',
-                info[i].paused ? 'p' : '-',
-                info[i].shutdown ? 's' : '-',
-                (shutdown_reason >= 0 &&
-                 shutdown_reason < sizeof(shutdown_reason_letters)-1
-                 ? shutdown_reason_letters[shutdown_reason] : '?'),
-                info[i].dying ? 'd' : '-',
-                ((float)info[i].cpu_time / 1e9));
-        free(domname);
-        if (verbose) {
-            printf(" " LIBXL_UUID_FMT, LIBXL_UUID_BYTES(info[i].uuid));
-            if (info[i].shutdown) printf(" %8x", shutdown_reason);
-            else printf(" %8s", "-");
-        }
+        format(formats, lead, info + i);
+        if (verbose)
+            format(formats, " %u %r", info + i);
         if (claim)
             printf(" %5lu", (unsigned long)info[i].outstanding_memkb / 1024);
         if (verbose || context)
-            printf(" %16s", info[i].ssid_label ? : "-");
+            format(formats, " %16l", info + i);
         if (cpupool) {
             char *poolname = libxl_cpupoolid_to_name(ctx, info[i].cpupool);
             printf("%16s", poolname);
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#12 lore raw

PATCH 12/16 WIP: UNTESTED: tools/xl: Replace remaining options with format()

In reply to Elliott Mitchell

format() is meant to be a powerful tool, sweep the remaining bits away. Unfortunately I am unable to test this portion.

---
 tools/xl/xl_list.c | 44 ++++++++------------------------------------
 1 file changed, 8 insertions(+), 36 deletions(-)
tools/xl/xl_list.c +8 -36
index 10d076864e..ee20d2feee 100644
@@ -371,56 +371,28 @@ static void list_domains(bool verbose, bool context, bool claim, bool numa,
     int i;
     const char lead[] = "%-40n %5i %5m %5v     %s  %8.1t";
 
-    libxl_bitmap nodemap;
-    libxl_physinfo physinfo;
-
-    libxl_bitmap_init(&nodemap);
-    libxl_physinfo_init(&physinfo);
-
     format(formats, lead, NULL);
     if (verbose) {
         format(formats, " %u %r %16l", NULL);
     } else if (context) format(formats, " %16l", NULL);
-    if (claim) printf("  Claimed");
-    if (cpupool) printf("         Cpupool");
-    if (numa) {
-        if (libxl_node_bitmap_alloc(ctx, &nodemap, 0)) {
-            fprintf(stderr, "libxl_node_bitmap_alloc_failed.\n");
-            exit(EXIT_FAILURE);
-        }
-        if (libxl_get_physinfo(ctx, &physinfo) != 0) {
-            fprintf(stderr, "libxl_physinfo failed.\n");
-            libxl_bitmap_dispose(&nodemap);
-            exit(EXIT_FAILURE);
-        }
-
-        printf(" NODE Affinity");
-    }
+    if (claim) format(formats, " %5c", NULL);
+    if (cpupool) format(formats, " %16p", NULL);
+    if (numa) format(formats, " %A", NULL);
     printf("\n");
     for (i = 0; i < nb_domain; i++) {
         format(formats, lead, info + i);
         if (verbose)
             format(formats, " %u %r", info + i);
         if (claim)
-            printf(" %5lu", (unsigned long)info[i].outstanding_memkb / 1024);
+            format(formats, " %5c", info + i);
         if (verbose || context)
             format(formats, " %16l", info + i);
-        if (cpupool) {
-            char *poolname = libxl_cpupoolid_to_name(ctx, info[i].cpupool);
-            printf("%16s", poolname);
-            free(poolname);
-        }
-        if (numa) {
-            libxl_domain_get_nodeaffinity(ctx, info[i].domid, &nodemap);
-
-            putchar(' ');
-            print_bitmap(nodemap.map, physinfo.nr_nodes, stdout);
-        }
+        if (cpupool)
+            format(formats, " %16p", info + i);
+        if (numa)
+            format(formats, " %A", info + i);
         putchar('\n');
     }
-
-    libxl_bitmap_dispose(&nodemap);
-    libxl_physinfo_dispose(&physinfo);
 }
 
 
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#13 lore raw

PATCH 13/16 WIP: tools/xl: Purge list_domains()

In reply to Elliott Mitchell

Everything previously done by list_domains() is now done with build_list_domain_format() and format().

---
 tools/xl/xl_list.c | 90 +++++++++++++++++++++++++++-------------------
 1 file changed, 53 insertions(+), 37 deletions(-)
tools/xl/xl_list.c +53 -37
index ee20d2feee..3ed6da8feb 100644
@@ -342,6 +342,31 @@ const format_table_t formats = {
                    (int *)&_discard->domid - (int *)_discard, {.i = 0}},
 };
 
+static char *build_list_domain_format(bool verbose, bool context, bool claim,
+                         bool numa, bool cpupool)
+{
+    int size = 4096;
+    char *fmt = malloc(size);
+    const char lead[] = "%-40n %5i %5m %5v     %s  %8.1t";
+
+    if (!fmt) return NULL;
+
+    memcpy(fmt, lead, sizeof(lead));
+
+    if (verbose) strcat(fmt, " %u %r %16l");
+    else if (context) strcat(fmt, " %16l");
+
+    if (claim) strcat(fmt, " %5c");
+
+    if (cpupool) strcat(fmt, " %16p");
+
+    if (numa) strcat(fmt, " %A");
+
+    strcat(fmt, "\n");
+
+    return realloc(fmt, strlen(fmt) + 1);
+}
+
 
 static void list_vm(void)
 {
@@ -365,36 +390,6 @@ static void list_vm(void)
     libxl_vminfo_list_free(info, nb_vm);
 }
 
-static void list_domains(bool verbose, bool context, bool claim, bool numa,
-                         bool cpupool, const libxl_dominfo *info, int nb_domain)
-{
-    int i;
-    const char lead[] = "%-40n %5i %5m %5v     %s  %8.1t";
-
-    format(formats, lead, NULL);
-    if (verbose) {
-        format(formats, " %u %r %16l", NULL);
-    } else if (context) format(formats, " %16l", NULL);
-    if (claim) format(formats, " %5c", NULL);
-    if (cpupool) format(formats, " %16p", NULL);
-    if (numa) format(formats, " %A", NULL);
-    printf("\n");
-    for (i = 0; i < nb_domain; i++) {
-        format(formats, lead, info + i);
-        if (verbose)
-            format(formats, " %u %r", info + i);
-        if (claim)
-            format(formats, " %5c", info + i);
-        if (verbose || context)
-            format(formats, " %16l", info + i);
-        if (cpupool)
-            format(formats, " %16p", info + i);
-        if (numa)
-            format(formats, " %A", info + i);
-        putchar('\n');
-    }
-}
-
 
 int main_list(int argc, char **argv)
 {
@@ -470,7 +465,17 @@ int main_list(int argc, char **argv)
 
     if (details)
         dump_by_dominfo_list(default_output_format, stdout, info, nb_domain);
-    else if (formatstr) {
+    else {
+        char *fr = NULL;
+
+        if (!formatstr) formatstr = fr = build_list_domain_format(verbose,
+                    context, false /* claim */, numa, cpupool);
+
+        if (!formatstr) {
+            fprintf(stderr, "Memory allocation failure.\n");
+            return EXIT_FAILURE;
+        }
+
         format(formats, formatstr, NULL);
 
         while (nb_domain) {
@@ -478,9 +483,9 @@ int main_list(int argc, char **argv)
             ++info;
             --nb_domain;
         }
-    } else
-        list_domains(verbose, context, false /* claim */, numa, cpupool,
-                     info, nb_domain);
+
+        if (fr) free(fr);
+    }
 
     if (info_free)
         libxl_dominfo_list_free(info_free, nb_domain);
@@ -506,7 +511,8 @@ int main_claims(int argc, char **argv)
 {
     libxl_dominfo *info;
     int opt;
-    int nb_domain;
+    int nb_domain, i;
+    char *fmt;
 
     SWITCH_FOREACH_OPT(opt, "", NULL, "claims", 0) {
         /* No options */
@@ -521,9 +527,19 @@ int main_claims(int argc, char **argv)
         return 1;
     }
 
-    list_domains(false /* verbose */, false /* context */, true /* claim */,
-                 false /* numa */, false /* cpupool */, info, nb_domain);
+    fmt = build_list_domain_format(false /* verbose */, false /* context */,
+                 true /* claim */, false /* numa */, false /* cpupool */);
+
+    if (!fmt) {
+        fprintf(stderr, "Memory allocation failure.\n");
+        return 1;
+    }
+
+    format(formats, fmt, NULL);
+
+    for (i = 0; i < nb_domain; ++i) format(formats, fmt, info + i);
 
+    free(fmt);
     libxl_dominfo_list_free(info, nb_domain);
     return 0;
 }
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#14 lore raw

PATCH 14/16 WIP: tools/xl: Enhance "list" command

In reply to Elliott Mitchell

Add several features to specify output. Allow omitting potentially unneeded lines and add argument for exact line format.

---
 tools/xl/xl_cmdtable.c |  2 ++
 tools/xl/xl_list.c     | 16 +++++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)
tools/xl/xl_cmdtable.c +2 -0
index f44c65a3f8..91c2026bc8 100644
@@ -53,6 +53,7 @@ struct cmd_spec cmd_table[] = {
       &main_list, 0, 0,
       "List information about all/some domains",
       "[options] [Domain]\n",
+      "-0, --no-domain0        Omit information for Domain 0\n"
       "-F, --format            Specify output format string\n"
       "   Similar to printf(3) formatting, conversion characters are:\n"
       "      %A    NODE Affinity\n"
@@ -67,6 +68,7 @@ struct cmd_spec cmd_table[] = {
       "      %t    Time(s)\n"
       "      %u    UUID\n"
       "      %v    vCPUs\n"
+      "-H, --no-header         Omit table header\n"
       "-Z, --context           Prints out security context\n"
       "-c, --cpupool           Prints the cpupool the domain is in\n"
       "-l, --long              Output all VM details\n"
tools/xl/xl_list.c +13 -3
index 3ed6da8feb..49ff2acaad 100644
@@ -397,6 +397,8 @@ int main_list(int argc, char **argv)
     bool context = false;
     bool cpupool = false;
     bool details = false;
+    bool dom0 = true;
+    bool header = true;
     const char *formatstr = NULL;
     bool numa = false;
     bool verbose = false;
@@ -405,6 +407,8 @@ int main_list(int argc, char **argv)
         {"cpupool", 0, 0, 'c'},
         {"format", 0, 0, 'F'},
         {"long", 0, 0, 'l'},
+        {"no-domain0", 0, 0, '0'},
+        {"no-header", 0, 0, 'H'},
         {"numa", 0, 0, 'n'},
         {"verbose", 0, 0, 'v'},
         COMMON_LONG_OPTS
@@ -414,10 +418,16 @@ int main_list(int argc, char **argv)
     libxl_dominfo *info, *info_free=0;
     int nb_domain, rc;
 
-    SWITCH_FOREACH_OPT(opt, "F:Zchlnv", opts, "list", 0) {
+    SWITCH_FOREACH_OPT(opt, "0F:HZchlnv", opts, "list", 0) {
+    case '0':
+        dom0 = false;
+        break;
     case 'F':
         formatstr = optarg;
         break;
+    case 'H':
+        header = false;
+        break;
     case 'Z':
         context = true;
         break;
@@ -476,10 +486,10 @@ int main_list(int argc, char **argv)
             return EXIT_FAILURE;
         }
 
-        format(formats, formatstr, NULL);
+        if (header) format(formats, formatstr, NULL);
 
         while (nb_domain) {
-            format(formats, formatstr, info);
+            if (info->domid || dom0) format(formats, formatstr, info);
             ++info;
             --nb_domain;
         }
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#15 lore raw

PATCH 15/16 WIP: tools/xl: Implement output format option for "vm-list" command

In reply to Elliott Mitchell

While the "vm-list" subcommand has far fewer fields than the "list" subcommand, one might still desire to list a subset of the fields.

---
 tools/xl/xl_cmdtable.c |  7 ++++-
 tools/xl/xl_list.c     | 66 +++++++++++++++++++++++++-----------------
 2 files changed, 46 insertions(+), 27 deletions(-)
tools/xl/xl_cmdtable.c +6 -1
index 91c2026bc8..c083566989 100644
@@ -256,7 +256,12 @@ struct cmd_spec cmd_table[] = {
     { "vm-list",
       &main_vm_list, 0, 0,
       "List guest domains, excluding dom0, stubdoms, etc.",
-      "",
+      "[options]\n",
+      "-F, --format            Specify output format string\n"
+      "   Similar to printf(3) formatting, conversion characters are:\n"
+      "      %i    Domain Id (%o, %x, and %X allow octal and hexadecimal)\n"
+      "      %n    Domain name\n"
+      "      %u    UUID\n"
     },
     { "info",
       &main_info, 0, 0,
tools/xl/xl_list.c +40 -26

94 lines not on this page · raw message

Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445
#16 lore raw

PATCH 16/16 WIP: tools/xl: Enhance "vm-list" command

In reply to Elliott Mitchell

Add several features to specify output. Allow omitting potentially unneeded lines and add argument for exact line format.

---
 tools/xl/xl_list.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
tools/xl/xl_list.c +13 -3
index 58809aa10b..ef44466017 100644
@@ -500,18 +500,28 @@ int main_list(int argc, char **argv)
 int main_vm_list(int argc, char **argv)
 {
     const char *formatstr = "%u %5i %n\n";
+    bool dom0 = true;
+    bool header = true;
     int opt;
     static const struct option opts[] = {
         {"format", 0, 0, 'F'},
+        {"no-domain0", 0, 0, '0'},
+        {"no-header", 0, 0, 'H'},
     };
 
     libxl_vminfo *info;
     int nb_vm, i;
 
-    SWITCH_FOREACH_OPT(opt, "F:", opts, "vm-list", 0) {
+    SWITCH_FOREACH_OPT(opt, "0F:H", opts, "vm-list", 0) {
+    case '0':
+        dom0 = false;
+        break;
     case 'F':
         formatstr = optarg;
         break;
+    case 'H':
+        header = false;
+        break;
     }
 
     info = libxl_list_vm(ctx, &nb_vm);
@@ -521,10 +531,10 @@ int main_vm_list(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 
-    format(formats_vm, formatstr, NULL);
+    if (header) format(formats_vm, formatstr, NULL);
 
     for (i = 0; i < nb_vm; i++)
-        format(formats_vm, formatstr, info + i);
+        if (info[i].domid || dom0) format(formats_vm, formatstr, info + i);
 
     libxl_vminfo_list_free(info, nb_vm);
 
Signature · 5 lines
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |         [email protected]  PGP 87145445         |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445