Skip to contentSkip to conversation

libxl: add a new Array type to the IDL

PATCH In review Ian Campbell 3 participants 5 messages Jun 27, 2012 – Jun 28, 2012 Arm/actions semi architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libxl: add a new Array type to the IDL

# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1340791557 -3600
# Node ID 03b641aa89f979a1670b9fe2a0827687a17d5459 # Parent 91b2e1c01cc28427bf2b30fee5040f412dee9257
libxl: add a new Array type to the IDL

And make all the required infrastructure updates to enable this.

Since there are currently no uses of this type there is no change to the generated code.

---

Dario extracted this from my "RFC: libxl: move definition of libxl_domain_config into the IDL" patch for use in his "Automatically place guest on host's NUMA nodes with xl" series. I have addressed Ian Jackson's review comments and am reposting as a separate patch.

Changes made to the idl.txt comments, in particular mention the requirement to name the fields num_FOO.

Added support for converting C arrays into ocaml arrays (not lists as previous patches).

tools/libxl/gentest.py +12 -0
diff -r 91b2e1c01cc2 -r 03b641aa89f9 tools/libxl/gentest.py
@@ -27,6 +27,18 @@ def gen_rand_init(ty, v, indent = "    "
     s = ""
     if isinstance(ty, idl.Enumeration):
         s += "%s = %s;\n" % (ty.pass_arg(v, parent is None), randomize_enum(ty))
+    elif isinstance(ty, idl.Array):
+        if parent is None:
+            raise Exception("Array type must have a parent")
+        s += "%s = rand()%%8;\n" % (parent + ty.lenvar.name)
+        s += "%s = calloc(%s, sizeof(*%s));\n" % \
+            (v, parent + ty.lenvar.name, v)
+        s += "{\n"
+        s += "    int i;\n"
+        s += "    for (i=0; i<%s; i++)\n" % (parent + ty.lenvar.name)
+        s += gen_rand_init(ty.elem_type, v+"[i]",
+                           indent + "        ", parent)
+        s += "}\n"
     elif isinstance(ty, idl.KeyedUnion):
         if parent is None:
             raise Exception("KeyedUnion type must have a parent")
tools/libxl/gentypes.py +39 -3

60 lines not on this page · raw message

tools/libxl/idl.py +11 -0
diff -r 91b2e1c01cc2 -r 03b641aa89f9 tools/libxl/idl.py
@@ -266,6 +266,17 @@ string = Builtin("char *", namespace = N
                  json_fn = "libxl__string_gen_json",
                  autogenerate_json = False)
 
+class Array(Type):
+    """An array of the same type"""
+    def __init__(self, elem_type, lenvar_name, **kwargs):
+        kwargs.setdefault('dispose_fn', 'free')
+        Type.__init__(self, namespace=elem_type.namespace, typename=elem_type.rawname + " *", **kwargs)
+
+        lv_kwargs = dict([(x.lstrip('lenvar_'),y) for (x,y) in kwargs.items() if x.startswith('lenvar_')])
+
+        self.lenvar = Field(integer, lenvar_name, **lv_kwargs)
+        self.elem_type = elem_type
+
 class OrderedDict(dict):
     """A dictionary which remembers insertion order.
 
tools/libxl/idl.txt +17 -4
diff -r 91b2e1c01cc2 -r 03b641aa89f9 tools/libxl/idl.txt
@@ -145,11 +145,24 @@ idl.KeyedUnion
 
  A subclass of idl.Aggregate which represents the C union type
  where the currently valid member of the union can be determined based
- upon another member in the containing type.
+ upon another member in the containing type. An idl.KeyedUnion must
+ always be a member of a containing idl.Aggregate type.
 
- The KeyedUnion.keyvar contains an idl.type the member of the
- containing type which determines the valid member of the union. The
- must be an instance of the Enumeration type.
+ The KeyedUnion.keyvar contains an idl.Field, this is the member of
+ the containing type which determines the valid member of the
+ union. The idl.Field.type of the keyvar must be an Enumeration type.
+
+idl.Array
+
+  A class representing an array of similar elements. An idl.Array must
+  always be an idl.Field of a containing idl.Aggregate.
+
+  idl.Array.elem_type contains an idl.Type which is the type of each
+  element of the array.
+
+  idl.Array.len_var contains an idl.Field which is added to the parent
+  idl.Aggregate and will contain the length of the array. The field
+  MUST be named num_ARRAYNAME.
 
 Standard Types
 --------------
tools/ocaml/libs/xl/genwrap.py +14 -1
diff -r 91b2e1c01cc2 -r 03b641aa89f9 tools/ocaml/libs/xl/genwrap.py
@@ -55,7 +55,8 @@ def ocaml_type_of(ty):
             return "int%d" % ty.width
         else:
             return "int"
-
+    elif isinstance(ty,idl.Array):
+        return "%s array" % ocaml_type_of(ty.elem_type)
     elif isinstance(ty,idl.Builtin):
         if not builtins.has_key(ty.typename):
             raise NotImplementedError("Unknown Builtin %s (%s)" % (ty.typename, type(ty)))
@@ -138,6 +139,8 @@ def c_val(ty, c, o, indent="", parent = 
         if not fn:
             raise NotImplementedError("No c_val fn for Builtin %s (%s)" % (ty.typename, type(ty)))
         s += "%s;" % (fn % { "o": o, "c": c })
+    elif isinstance (ty,idl.Array):
+        raise("Cannot handle Array type\n")
     elif isinstance(ty,idl.Enumeration) and (parent is None):
         n = 0
         s += "switch(Int_val(%s)) {\n" % o
@@ -195,6 +198,16 @@ def ocaml_Val(ty, o, c, indent="", paren
         if not fn:
             raise NotImplementedError("No ocaml Val fn for Builtin %s (%s)" % (ty.typename, type(ty)))
         s += "%s = %s;" % (o, fn % { "c": c })
+    elif isinstance(ty, idl.Array):
+        s += "{\n"
+        s += "\t    int i;\n"
+        s += "\t    value array_elem;\n"
+        s += "\t    %s = caml_alloc(%s,0);\n" % (o, parent + ty.lenvar.name)
+        s += "\t    for(i=0; i<%s; i++) {\n" % (parent + ty.lenvar.name)
+        s += "\t        %s\n" % ocaml_Val(ty.elem_type, "array_elem", c + "[i]", "")
+        s += "\t        Store_field(%s, i, array_elem);\n" % o
+        s += "\t    }\n"
+        s += "\t}"
     elif isinstance(ty,idl.Enumeration) and (parent is None):
         n = 0
         s += "switch(%s) {\n" % c
tools/python/genwrap.py +5 -3
diff -r 91b2e1c01cc2 -r 03b641aa89f9 tools/python/genwrap.py
@@ -4,7 +4,7 @@ import sys,os
 
 import idl
 
-(TYPE_DEFBOOL, TYPE_BOOL, TYPE_INT, TYPE_UINT, TYPE_STRING, TYPE_AGGREGATE) = range(6)
+(TYPE_DEFBOOL, TYPE_BOOL, TYPE_INT, TYPE_UINT, TYPE_STRING, TYPE_ARRAY, TYPE_AGGREGATE) = range(7)
 
 def py_type(ty):
     if ty == idl.bool:
@@ -18,6 +18,8 @@ def py_type(ty):
             return TYPE_INT
         else:
             return TYPE_UINT
+    if isinstance(ty, idl.Array):
+        return TYPE_ARRAY
     if isinstance(ty, idl.Aggregate):
         return TYPE_AGGREGATE
     if ty == idl.string:
@@ -74,7 +76,7 @@ def py_attrib_get(ty, f):
         l.append('    return genwrap__ull_get(self->obj.%s);'%f.name)
     elif t == TYPE_STRING:
         l.append('    return genwrap__string_get(&self->obj.%s);'%f.name)
-    elif t == TYPE_AGGREGATE:
+    elif t == TYPE_AGGREGATE or t == TYPE_ARRAY:
         l.append('    PyErr_SetString(PyExc_NotImplementedError, "Getting %s");'%ty.typename)
         l.append('    return NULL;')
     else:
@@ -105,7 +107,7 @@ def py_attrib_set(ty, f):
         l.append('    return ret;')
     elif t == TYPE_STRING:
         l.append('    return genwrap__string_set(v, &self->obj.%s);'%f.name)
-    elif t == TYPE_AGGREGATE:
+    elif t == TYPE_AGGREGATE or t == TYPE_ARRAY:
         l.append('    PyErr_SetString(PyExc_NotImplementedError, "Setting %s");'%ty.typename)
         l.append('    return -1;')
     else:
#1 lore raw
Quoted 3 lines

Since there are currently no uses of this type there is no change to the generated code.

But for reference if I add Dario's numa info type:

tools/libxl/libxl_types.idl +6 -0
diff -r 03b641aa89f9 tools/libxl/libxl_types.idl
@@ -423,6 +423,12 @@ libxl_physinfo = Struct("physinfo", [
     ("cap_hvm_directio", bool),
     ], dir=DIR_OUT)
 
+libxl_numainfo = Struct("numainfo", [
+    ("size", uint64),
+    ("free", uint64),
+    ("dists", Array(uint32, "num_dists")),
+    ], dir=DIR_OUT)
+
 libxl_cputopology = Struct("cputopology", [
     ("core", uint32),
     ("socket", uint32),

then I get the following diff to the generated files:

libxl/_libxl_types.c renamed from libxl/_libxl_types.c.baseline +58 -0

76 lines not on this page · raw message

libxl/_libxl_types.h renamed from libxl/_libxl_types.h.baseline +10 -0
@@ -475,6 +475,16 @@ void libxl_physinfo_dispose(libxl_physin
 void libxl_physinfo_init(libxl_physinfo *p);
 char *libxl_physinfo_to_json(libxl_ctx *ctx, libxl_physinfo *p);
 
+typedef struct libxl_numainfo {
+    uint64_t size;
+    uint64_t free;
+    int num_dists;
+    uint32_t * dists;
+} libxl_numainfo;
+void libxl_numainfo_dispose(libxl_numainfo *p);
+void libxl_numainfo_init(libxl_numainfo *p);
+char *libxl_numainfo_to_json(libxl_ctx *ctx, libxl_numainfo *p);
+
 typedef struct libxl_cputopology {
     uint32_t core;
     uint32_t socket;
libxl/_libxl_types_json.h renamed from libxl/_libxl_types_json.h.baseline +1 -0
@@ -40,6 +40,7 @@ yajl_gen_status libxl_diskinfo_gen_json(
 yajl_gen_status libxl_nicinfo_gen_json(yajl_gen hand, libxl_nicinfo *p);
 yajl_gen_status libxl_vcpuinfo_gen_json(yajl_gen hand, libxl_vcpuinfo *p);
 yajl_gen_status libxl_physinfo_gen_json(yajl_gen hand, libxl_physinfo *p);
+yajl_gen_status libxl_numainfo_gen_json(yajl_gen hand, libxl_numainfo *p);
 yajl_gen_status libxl_cputopology_gen_json(yajl_gen hand, libxl_cputopology *p);
 yajl_gen_status libxl_sched_credit_params_gen_json(yajl_gen hand, libxl_sched_credit_params *p);
 yajl_gen_status libxl_domain_remus_info_gen_json(yajl_gen hand, libxl_domain_remus_info *p);
python/xen/lowlevel/xl/_pyxl_types.h renamed from python/xen/lowlevel/xl/_pyxl_types.h.baseline +11 -0
@@ -268,6 +268,17 @@ _hidden int Pyphysinfo_Check(PyObject *s
 
 _hidden PyObject *attrib__libxl_hwcap_get(libxl_hwcap *hw_cap);
 
+/* Internal API for libxl_numainfo wrapper */
+typedef struct {
+    PyObject_HEAD;
+    libxl_numainfo obj;
+}Py_numainfo;
+
+_hidden Py_numainfo *Pynumainfo_New(void);
+
+_hidden int Pynumainfo_Check(PyObject *self);
+
+
 /* Internal API for libxl_cputopology wrapper */
 typedef struct {
     PyObject_HEAD;
python/xen/lowlevel/xl/_pyxl_types.c renamed from python/xen/lowlevel/xl/_pyxl_types.c.baseline +109 -0

121 lines not on this page · raw message

ocaml/libs/xl/_libxl_types.inc renamed from ocaml/libs/xl/_libxl_types.inc.baseline +30 -0
@@ -1129,6 +1129,36 @@ static value Val_physinfo (caml_gc *gc,
 /* Stubs for physinfo */
 value stub_xl_physinfo_get(value v1);
 
+/* Convert numainfo to a caml value */
+static value Val_numainfo (caml_gc *gc, struct caml_logger *lg, libxl_numainfo *numainfo_c)
+{
+	CAMLparam0();
+	CAMLlocal1(numainfo_ocaml);
+	{
+		value numainfo_field;
+	
+		numainfo_ocaml = caml_alloc_tuple(3);
+	
+		numainfo_field = caml_copy_int64(numainfo_c->size);
+		Store_field(numainfo_ocaml, 0, numainfo_field);
+	
+		numainfo_field = caml_copy_int64(numainfo_c->free);
+		Store_field(numainfo_ocaml, 1, numainfo_field);
+	
+		{
+		    int i;
+		    value array_elem;
+		    numainfo_field = caml_alloc(numainfo_c->num_dists,0);
+		    for(i=0; i<numainfo_c->num_dists; i++) {
+		        array_elem = caml_copy_int32(numainfo_c->dists[i]);
+		        Store_field(numainfo_field, i, array_elem);
+		    }
+		}
+		Store_field(numainfo_ocaml, 2, numainfo_field);
+	}
+	CAMLreturn(numainfo_ocaml);
+}
+
 /* Convert cputopology to a caml value */
 static value Val_cputopology (caml_gc *gc, struct caml_logger *lg, libxl_cputopology *cputopology_c)
 {
ocaml/libs/xl/_libxl_types.mli.in renamed from ocaml/libs/xl/_libxl_types.mli.in.baseline +9 -0
@@ -312,6 +312,15 @@ module Physinfo : sig
 	external get : unit -> t = "stub_xl_physinfo_get"
 end
 
+module Numainfo : sig
+	type t =
+	{
+		size : int64;
+		free : int64;
+		dists : int32 array;
+	}
+end
+
 module Cputopology : sig
 	type t =
 	{
ocaml/libs/xl/_libxl_types.ml.in renamed from ocaml/libs/xl/_libxl_types.ml.in.baseline +9 -0
@@ -312,6 +312,15 @@ module Physinfo = struct
 	external get : unit -> t = "stub_xl_physinfo_get"
 end
 
+module Numainfo = struct
+	type t =
+	{
+		size : int64;
+		free : int64;
+		dists : int32 array;
+	}
+end
+
 module Cputopology = struct
 	type t =
 	{
libxl/testidl.c renamed from libxl/testidl.c.baseline +22 -0
@@ -521,6 +521,20 @@ static void libxl_physinfo_rand_init(lib
     p->cap_hvm_directio = rand() % 2;
 }
 
+static void libxl_numainfo_rand_init(libxl_numainfo *p);
+static void libxl_numainfo_rand_init(libxl_numainfo *p)
+{
+    p->size = rand() % (sizeof(p->size)*8);
+    p->free = rand() % (sizeof(p->free)*8);
+    p->num_dists = rand()%8;
+    p->dists = calloc(p->num_dists, sizeof(*p->dists));
+    {
+        int i;
+        for (i=0; i<p->num_dists; i++)
+            p->dists[i] = rand() % (sizeof(p->dists[i])*8);
+    }
+}
+
 static void libxl_cputopology_rand_init(libxl_cputopology *p);
 static void libxl_cputopology_rand_init(libxl_cputopology *p)
 {
@@ -610,6 +624,7 @@ int main(int argc, char **argv)
     libxl_nicinfo libxl_nicinfo_val;
     libxl_vcpuinfo libxl_vcpuinfo_val;
     libxl_physinfo libxl_physinfo_val;
+    libxl_numainfo libxl_numainfo_val;
     libxl_cputopology libxl_cputopology_val;
     libxl_sched_credit_params libxl_sched_credit_params_val;
     libxl_domain_remus_info libxl_domain_remus_info_val;
@@ -842,6 +857,13 @@ int main(int argc, char **argv)
     free(s);
     libxl_physinfo_dispose(&libxl_physinfo_val);
 
+    libxl_numainfo_rand_init(&libxl_numainfo_val);
+    s = libxl_numainfo_to_json(ctx, &libxl_numainfo_val);
+    printf("%s: %s\n", "libxl_numainfo", s);
+    if (s == NULL) abort();
+    free(s);
+    libxl_numainfo_dispose(&libxl_numainfo_val);
+
     libxl_cputopology_rand_init(&libxl_cputopology_val);
     s = libxl_cputopology_to_json(ctx, &libxl_cputopology_val);
     printf("%s: %s\n", "libxl_cputopology", s);
#2 lore raw

Ian Campbell writes ("Re: [Xen-devel] [PATCH] libxl: add a new Array type to the IDL"):

Quoted 4 lines
Quoted 2 lines

Since there are currently no uses of this type there is no change to the generated code.

But for reference if I add Dario's numa info type:

Thanks for providing that. That's what I was looking at before and it looked reasonable then. I'm happy with it now too.

Ian.

#3 lore raw
In reply to Ian Campbell

Ian Campbell writes ("[PATCH] libxl: add a new Array type to the IDL"):

Quoted 10 lines

libxl: add a new Array type to the IDL

And make all the required infrastructure updates to enable this.

Since there are currently no uses of this type there is no change to the generated code.

Great, thanks.

Dario, I take it you'll pick this up into your series so I don't need to apply it separately.

Ian.

#4 lore raw
Quoted 9 lines · Ian Jackson
Quoted 3 lines

Great, thanks.

Dario, I take it you'll pick this up into your series so I don't need to apply it separately.

That's fine.

Thanks and Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://retis.sssup.it/people/faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel