Skip to contentSkip to conversation

generated Go libxl bindings using IDL

PATCH Superseded Nick Rosbrook 3 participants 61 messages Oct 7, 2019 – Nov 15, 2019

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover generated Go libxl bindings using IDL

From: Nick Rosbrook <[email protected]>

After Xen summit, we started the discussion in this[1] RFC thread to figure out how to generate Go bindings for libxl. This series implements that Go code generation using the existing IDL, and updates the existing hand-written code in xenlight.go to use the generated code.

The goal of this series is to provide a good foundation for continued development of the Go package.

These patches can also be found on my GitHub branch[2].

[1] https://lists.xenproject.org/archives/html/xen-devel/2019-07/msg02259.html
[2] https://github.com/enr0n/xen/tree/golang-patches-v1

Nick Rosbrook (24):
golang/xenlight: fix calls to libxl_domain_unpause/pause golang/xenlight: generate enum types from IDL golang/xenlight: define Defbool builtin type golang/xenlight: define Devid type as int golang/xenlight: define KeyValueList builtin type golang/xenlight: re-name Bitmap marshaling functions golang/xenlight: define StringList builtin type golang/xenlight: define Mac builtin type golang/xenlight: define MsVmGenid builtin type golang/xenlight: define EvLink builtin as empty struct golang/xenlight: define CpuidPolicyList builtin type golang/xenlight: re-factor Uuid type implementation golang/xenlight: re-factor Hwcap type implementation golang/xenlight: generate structs from the IDL golang/xenlight: remove no-longer used type MemKB golang/xenlight: begin C to Go type marshaling golang/xenlight: implement keyed union C to Go marshaling golang/xenlight: implement array C to Go marshaling golang/xenlight: begin Go to C type marshaling golang/xenlight: implement keyed union Go to C marshaling golang/xenlight: implement array Go to C marshaling golang/xenlight: revise use of Context type golang/xenlight: add error return type to Context.Cpupoolinfo golang/xenlight: add make target for generated files

 tools/golang/xenlight/Makefile            |   22 +-
 tools/golang/xenlight/gengotypes.py       |  698 +++++
 tools/golang/xenlight/xenlight.go         |  928 +++---
 tools/golang/xenlight/xenlight_helpers.go | 3404 +++++++++++++++++++++
 tools/golang/xenlight/xenlight_types.go   | 1212 ++++++++
 5 files changed, 5727 insertions(+), 537 deletions(-)
 create mode 100644 tools/golang/xenlight/gengotypes.py
 create mode 100644 tools/golang/xenlight/xenlight_helpers.go
 create mode 100644 tools/golang/xenlight/xenlight_types.go
Cc George Dunlap <[email protected]>
Cc Ian Jackson <[email protected]>
Cc Wei Liu <[email protected]>
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/24 golang/xenlight: fix calls to libxl_domain_unpause/pause

From: Nick Rosbrook <[email protected]>

These functions require a third argument of type const *libxl_asyncop_how.

Pass nil to fix compilation errors. This will have the effect of performing these operations synchronously.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/golang/xenlight/xenlight.go +2 -2
index f5d171c2d5..59b8186a64 100644
@@ -1011,7 +1011,7 @@ func (Ctx *Context) DomainUnpause(Id Domid) (err error) {
 		return
 	}
 
-	ret := C.libxl_domain_unpause(Ctx.ctx, C.uint32_t(Id))
+	ret := C.libxl_domain_unpause(Ctx.ctx, C.uint32_t(Id), nil)
 
 	if ret != 0 {
 		err = Error(-ret)
@@ -1026,7 +1026,7 @@ func (Ctx *Context) DomainPause(id Domid) (err error) {
 		return
 	}
 
-	ret := C.libxl_domain_pause(Ctx.ctx, C.uint32_t(id))
+	ret := C.libxl_domain_pause(Ctx.ctx, C.uint32_t(id), nil)
 
 	if ret != 0 {
 		err = Error(-ret)
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 1/24
#2 lore raw

Re: [Xen-devel] [PATCH 01/24] golang/xenlight: fix calls to libxl_domain_unpause/pause

Quoted 8 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

These functions require a third argument of type const *libxl_asyncop_how.

Pass nil to fix compilation errors. This will have the effect of performing these operations synchronously.

Juergen, this is actually a bug fix (these lines didn't get updated when the API changed), so I'm going to check this in later this week if you don't object.

Quoted 23 lines

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/golang/xenlight/xenlight.go +1 -1
index f5d171c2d5..59b8186a64 100644
@@ -1011,7 +1011,7 @@ func (Ctx *Context) DomainUnpause(Id Domid) (err error) {
 		return
 	}
 
-	ret := C.libxl_domain_unpause(Ctx.ctx, C.uint32_t(Id))
+	ret := C.libxl_domain_unpause(Ctx.ctx, C.uint32_t(Id), nil)
 
 	if ret != 0 {
 		err = Error(-ret)

tools/golang/xenlight/xenlight.go func (Ctx *Context) DomainPause(id Domid) (err error) { · line 1032

Quoted 8 lines
tools/golang/xenlight/xenlight.go +1 -1
@@ -1026,7 +1026,7 @@ func (Ctx *Context) DomainPause(id Domid) (err error) {
 		return
 	}
 
-	ret := C.libxl_domain_pause(Ctx.ctx, C.uint32_t(id))
+	ret := C.libxl_domain_pause(Ctx.ctx, C.uint32_t(id), nil)
 
 	if ret != 0 {
 		err = Error(-ret)

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [Xen-devel] [PATCH 01/24] golang/xenlight: fix calls to libxl_domain_unpause/pause

Quoted 8 lines · George Dunlap
Quoted 7 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

These functions require a third argument of type const *libxl_asyncop_how.

Pass nil to fix compilation errors. This will have the effect of performing these operations synchronously.

Quoted 7 lines
Quoted 1 line

Juergen, this is actually a bug fix (these lines didn't get updated when the API changed), so I'm going to check this in later this week if you don't object.

Juergen

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

PATCH 2/24 golang/xenlight: generate enum types from IDL

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Introduce gengotypes.py to generate Go code the from IDL. As a first step, implement 'enum' type generation.

As a result of the newly-generated code, remove the existing, and now conflicting definitions in xenlight.go. In the case of the Error type, rename the slice 'errors' to 'libxlErrors' so that it does not conflict with the standard library package 'errors.' And, negate the values used in 'libxlErrors' since the generated error values are negative.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py     | 109 +++++++
 tools/golang/xenlight/xenlight.go       | 140 ++-------
 tools/golang/xenlight/xenlight_types.go | 378 ++++++++++++++++++++++++
 3 files changed, 515 insertions(+), 112 deletions(-)
 create mode 100644 tools/golang/xenlight/gengotypes.py
 create mode 100644 tools/golang/xenlight/xenlight_types.go
tools/golang/xenlight/gengotypes.py new file +109 -0
new file mode 100644
index 0000000000..59307492cb
@@ -0,0 +1,109 @@
+#!/usr/bin/python
+
+import os
+import sys
+
+sys.path.append('{}/tools/libxl'.format(os.environ['XEN_ROOT']))
+import idl
+
+# Go versions of some builtin types.
+# Append the libxl-defined builtins after IDL parsing.
+builtin_type_names = {
+    idl.bool.typename: 'bool',
+    idl.string.typename: 'string',
+    idl.integer.typename: 'int',
+    idl.uint8.typename: 'byte',
+    idl.uint16.typename: 'uint16',
+    idl.uint32.typename: 'uint32',
+    idl.uint64.typename: 'uint64',
+}
+
+def xenlight_golang_generate_types(path = None, types = None, comment = None):
+    """
+    Generate a .go file (xenlight_types.go by default)
+    that contains a Go type for each type in types.
+    """
+    if path is None:
+        path = 'xenlight_types.go'
+
+    with open(path, 'w') as f:
+        if comment is not None:
+            f.write(comment)
+        f.write('package xenlight\n')
+
+        for ty in types:
+            f.write(xenlight_golang_type_define(ty))
+            f.write('\n')
+
+    go_fmt(path)
+
+def xenlight_golang_type_define(ty = None):
+    s = ''
+
+    if isinstance(ty, idl.Enumeration):
+        s += xenlight_golang_define_enum(ty)
+
+    return s
+
+def xenlight_golang_define_enum(ty = None):
+    s = ''
+    typename = ''
+
+    if ty.typename is not None:
+        typename = xenlight_golang_fmt_name(ty.typename)
+        s += 'type {} int\n'.format(typename)
+
+    # Start const block
+    s += 'const(\n'
+
+    for v in ty.values:
+        name = xenlight_golang_fmt_name(v.name)
+        s += '{} {} = {}\n'.format(name, typename, v.value)
+
+    # End const block
+    s += ')\n'
+
+    return s
+
+def xenlight_golang_fmt_name(name, exported = True):
+    """
+    Take a given type name and return an
+    appropriate Go type name.
+    """
+    if name in builtin_type_names.keys():
+        return builtin_type_names[name]
+
+    # Name is not a builtin, format it for Go.
+    words = name.split('_')
+
+    # Remove 'libxl' prefix
+    if words[0].lower() == 'libxl':
+        words.remove(words[0])
+
+    if exported:
+        return ''.join(x.title() for x in words)
+
+    return words[0] + ''.join(x.title() for x in words[1:])
+
+def go_fmt(path):
+    """ Call go fmt on the given path. """
+    os.system('go fmt {}'.format(path))
+
+if __name__ == '__main__':
+    idlname = sys.argv[1]
+
+    (builtins, types) = idl.parse(idlname)
+
+    for b in builtins:
+        name = b.typename
+        builtin_type_names[name] = xenlight_golang_fmt_name(name)
+
+    header_comment="""// DO NOT EDIT.
+    //
+    // This file is generated by:
+    // {}
+    //
+    """.format(' '.join(sys.argv))
+
+    xenlight_golang_generate_types(types=types,
+                                   comment=header_comment)
tools/golang/xenlight/xenlight.go +28 -112

178 lines not on this page · raw message

tools/golang/xenlight/xenlight_types.go new file +378 -0

378 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

Re: [Xen-devel] [PATCH 02/24] golang/xenlight: generate enum types from IDL

Quoted 5 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Introduce gengotypes.py to generate Go code the from IDL. As a first step, implement 'enum' type generation.

Quoted 7 lines

As a result of the newly-generated code, remove the existing, and now conflicting definitions in xenlight.go. In the case of the Error type, rename the slice 'errors' to 'libxlErrors' so that it does not conflict with the standard library package 'errors.' And, negate the values used in 'libxlErrors' since the generated error values are negative.

This looks good, thanks. Just needs to be ported after and/or merged with the Makefile changes from 24/24.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

PATCH 3/24 golang/xenlight: define Defbool builtin type

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define Defbool as struct analagous to the C type, and define the type 'defboolVal' that represent true, false, and default defbool values.

Implement Set, Unset, SetIfDefault, IsDefault, Val, and String functions on Defbool so that the type can be used in Go analagously to how its used in C.

Finally, implement fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 93 +++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)
tools/golang/xenlight/xenlight.go +93 -0
index e617f22fcf..7bf16dc03b 100644
@@ -85,6 +85,99 @@ type MemKB uint64
 
 type Uuid C.libxl_uuid
 
+// defboolVal represents a defbool value.
+type defboolVal int
+
+const (
+	defboolDefault defboolVal = 0
+	defboolFalse   defboolVal = -1
+	defboolTrue    defboolVal = 1
+)
+
+// Defbool represents a libxl_defbool.
+type Defbool struct {
+	val defboolVal
+}
+
+func (d Defbool) String() string {
+	switch d.val {
+	case defboolDefault:
+		return "<default>"
+	case defboolFalse:
+		return "False"
+	case defboolTrue:
+		return "True"
+	}
+
+	return ""
+}
+
+// Set sets the value of the Defbool.
+func (d *Defbool) Set(b bool) {
+	if b {
+		d.val = defboolTrue
+		return
+	}
+	d.val = defboolFalse
+}
+
+// Unset resets the Defbool to default value.
+func (d *Defbool) Unset() {
+	d.val = defboolDefault
+}
+
+// SetIfDefault sets the value of Defbool only if
+// its current value is default.
+func (d *Defbool) SetIfDefault(b bool) {
+	if d.IsDefault() {
+		d.Set(b)
+	}
+}
+
+// IsDefault returns true if the value of Defbool
+// is default, returns false otherwise.
+func (d *Defbool) IsDefault() bool {
+	return d.val == defboolDefault
+}
+
+// Val returns the boolean value associated with the
+// Defbool value. An error is returned if the value
+// is default.
+func (d *Defbool) Val() (bool, error) {
+	if d.IsDefault() {
+		return false, fmt.Errorf("%v: cannot take value of default defbool", ErrorInval)
+	}
+
+	return (d.val > 0), nil
+}
+
+func (d *Defbool) fromC(c *C.libxl_defbool) error {
+	if C.libxl_defbool_is_default(*c) {
+		d.val = defboolDefault
+		return nil
+	}
+
+	if C.libxl_defbool_val(*c) {
+		d.val = defboolTrue
+		return nil
+	}
+
+	d.val = defboolFalse
+
+	return nil
+}
+
+func (d *Defbool) toC() (C.libxl_defbool, error) {
+	var c C.libxl_defbool
+
+	if !d.IsDefault() {
+		val, _ := d.Val()
+		C.libxl_defbool_set(&c, C.bool(val))
+	}
+
+	return c, nil
+}
+
 type Context struct {
 	ctx    *C.libxl_ctx
 	logger *C.xentoollog_logger_stdiostream
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 3/24
#7 lore raw

Re: [Xen-devel] [PATCH 03/24] golang/xenlight: define Defbool builtin type

Quoted 5 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define Defbool as struct analagous to the C type, and define the type 'defboolVal' that represent true, false, and default defbool values.

Quoted 7 lines

Implement Set, Unset, SetIfDefault, IsDefault, Val, and String functions on Defbool so that the type can be used in Go analagously to how its used in C.

Finally, implement fromC and toC functions.

Looks good:

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

PATCH 4/24 golang/xenlight: define Devid type as int

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 3 +++
 1 file changed, 3 insertions(+)
tools/golang/xenlight/xenlight.go +3 -0
index 7bf16dc03b..4d4fad2a9d 100644
@@ -81,6 +81,9 @@ func (e Error) Error() string {
 
 type Domid uint32
 
+// Devid is a device ID.
+type Devid int
+
 type MemKB uint64
 
 type Uuid C.libxl_uuid
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

PATCH 5/24 golang/xenlight: define KeyValueList builtin type

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define KeyValueList builtin type, analagous to libxl_key_value_list as map[string]string, and implement its fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 33 ++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
tools/golang/xenlight/xenlight.go +32 -1
index 4d4fad2a9d..8196a42855 100644
@@ -202,11 +202,42 @@ func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) {
 	return
 }
 
+// KeyValueList represents a libxl_key_value_list.
+type KeyValueList map[string]string
+
+func (kvl KeyValueList) fromC(ckvl *C.libxl_key_value_list) error {
+	size := int(C.libxl_key_value_list_length(ckvl))
+	list := (*[1 << 30]*C.char)(unsafe.Pointer(ckvl))[:size:size]
+
+	for i := 0; i < size*2; i += 2 {
+		kvl[C.GoString(list[i])] = C.GoString(list[i+1])
+	}
+
+	return nil
+}
+
+func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) {
+	// Add extra slot for sentinel
+	var char *C.char
+	csize := 2*len(kvl) + 1
+	ckvl := (C.libxl_key_value_list)(C.malloc(C.ulong(csize) * C.ulong(unsafe.Sizeof(char))))
+	clist := (*[1 << 31]*C.char)(unsafe.Pointer(ckvl))[:csize:csize]
+
+	i := 0
+	for k, v := range kvl {
+		clist[i] = C.CString(k)
+		clist[i+1] = C.CString(v)
+		i += 2
+	}
+	clist[len(clist)-1] = nil
+
+	return ckvl, nil
+}
+
 // typedef struct {
 //     uint32_t size;          /* number of bytes in map */
 //     uint8_t *map;
 // } libxl_bitmap;
-
 // Implement the Go bitmap type such that the underlying data can
 // easily be copied in and out.  NB that we still have to do copies
 // both directions, because cgo runtime restrictions forbid passing to
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#11 lore raw

Re: [Xen-devel] [PATCH 05/24] golang/xenlight: define KeyValueList builtin type

Quoted 22 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define KeyValueList builtin type, analagous to libxl_key_value_list as map[string]string, and implement its fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 33 ++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
tools/golang/xenlight/xenlight.go +0 -0
index 4d4fad2a9d..8196a42855 100644
@@ -202,11 +202,42 @@ func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) {
 	return
 }
 

tools/golang/xenlight/xenlight.go func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) { · line 214

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+// KeyValueList represents a libxl_key_value_list.
+type KeyValueList map[string]string
+
+func (kvl KeyValueList) fromC(ckvl *C.libxl_key_value_list) error {
+	size := int(C.libxl_key_value_list_length(ckvl))
+	list := (*[1 << 30]*C.char)(unsafe.Pointer(ckvl))[:size:size]
+
+	for i := 0; i < size*2; i += 2 {
+		kvl[C.GoString(list[i])] = C.GoString(list[i+1])
+	}

It looks like when you use this, you use patterns like this:

	var keyValueListXsdata KeyValueList
	if err := keyValueListXsdata.fromC(&xc.xsdata); err != nil {

But this never calls make(); so won't this crash with a null pointer deref? Or am I missing something?

Would it be better to take a pointer method here, and set *kvl = make(map[string]string) before copying the strings over?

That would also very naturally take care of the case where you called the .fromC() method twice with two different key value lists. As it is, if the caller had to initialize it, you'd get a "clobbered union" of the two lists (where in the case of duplicate keys, the second value clobbers the first); this way, you only get the most recent list, which is probably closer to what you wanted.

Also, when going the other direction, how are callers of, say, libxl_domain_create_new() supposed to initialize this and fill in values?

Looking through the code -- it seems that this type is somewhat vestigal. It's only used for two fields of a single struct, and those fields aren't actually used by xl or libvirt at the moment; and after some discussion it was determined that anything they might be used to achieve should probably be done a different way.

So we *could* actually just type KeyValueList struct { }, and punt on all these initialization questions until such time as it turns out that they're needed.

On the other hand, I think we may need to actually think about initializing structures. You've carefully coded DefBool such that the "zero" value is undefined; but for DevId, for instance, the "initial" value is supposed to be -1; but the way it's coded, an uninitialized Go structure will end up as 0, which may be a valid devid.

At the moment, all implemented methods take scalar arguments; but when they take structs, having non-default values means "try to get this specific devid", as opposed to "just choose a free one for me".

Anyway, perhaps we can think about structure initialization, and implement it after we do the basic structure / marshalling implementaiton.

In the mean time, we could either keep the KeyValueList you've implemented here (perhaps adding a make() to the fromC method, and having toC return NULL if kvl is NULL), or just replace it with a placeholder until it's needed.

What do you think?

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#12 lore raw

Re: [Xen-devel] [PATCH 05/24] golang/xenlight: define KeyValueList builtin type

Quoted 3 lines

So we *could* actually just type KeyValueList struct { }, and punt on all these initialization questions until such time as it turns out that they're needed.

If there is no clear need for this type to be implemented in the Go package, then I would be in favor of not doing so. IMO, a smaller, more focused package is ideal.

Quoted 10 lines

On the other hand, I think we may need to actually think about initializing structures. You've carefully coded DefBool such that the "zero" value is undefined; but for DevId, for instance, the "initial" value is supposed to be -1; but the way it's coded, an uninitialized Go structure will end up as 0, which may be a valid devid.

[...]

Anyway, perhaps we can think about structure initialization, and implement it after we do the basic structure / marshalling implementaiton.

That's probably best. However, at a quick glance it seems like it would be pretty straight-forward to generate NewStructType functions analogous to libxl_struct_type_init, if that's the desired behavior.

Quoted 6 lines

In the mean time, we could either keep the KeyValueList you've implemented here (perhaps adding a make() to the fromC method, and having toC return NULL if kvl is NULL), or just replace it with a placeholder until it's needed.

What do you think?

Based on what you said above, I think I would like to drop the implementation for now. But, if we keep the current implementation, I will make those corrections.

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#13 lore raw

Re: [Xen-devel] [PATCH 05/24] golang/xenlight: define KeyValueList builtin type

Quoted 11 lines · Nick Rosbrook
Quoted 4 lines

So we *could* actually just type KeyValueList struct { }, and punt on all these initialization questions until such time as it turns out that they're needed.

If there is no clear need for this type to be implemented in the Go package, then I would be in favor of not doing so. IMO, a smaller, more focused package is ideal.

Ok, in that case let’s just leave the struct empty.

Quoted 7 lines
Quoted 7 lines

On the other hand, I think we may need to actually think about initializing structures. You've carefully coded DefBool such that the "zero" value is undefined; but for DevId, for instance, the "initial" value is supposed to be -1; but the way it's coded, an uninitialized Go structure will end up as 0, which may be a valid devid.

Quoted 8 lines
Quoted 4 lines

[...]

Anyway, perhaps we can think about structure initialization, and implement it after we do the basic structure / marshalling implementaiton.

That's probably best. However, at a quick glance it seems like it would be pretty straight-forward to generate NewStructType functions analogous to libxl_struct_type_init, if that's the desired behavior.

I think we basically have three options:

1. Try to arrange it so that the “zero” values correspond to “default” values in libxl; i.e., have DevID 0 -> libxl_devid -1, DevID 1 -> libxl_devid 0, &c

2. Add NewStructType functions

3. Add .Init() methods to structs

#1 I think is probably too risky; so 2 or 3 (or maybe both) will probably be needed. The NewStructType() seems to be more standard. But I’m open so suggestions. :-)

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

Re: [Xen-devel] [PATCH 05/24] golang/xenlight: define KeyValueList builtin type

Quoted 1 line

Ok, in that case let’s just leave the struct empty.

Ok, sounds like a plan.

Quoted 9 lines

I think we basically have three options:

1. Try to arrange it so that the “zero” values correspond to “default” values in libxl; i.e., have DevID 0 -> libxl_devid -1, DevID 1 -> libxl_devid 0, &c

2. Add NewStructType functions

3. Add .Init() methods to structs

#1 I think is probably too risky; so 2 or 3 (or maybe both) will probably be needed. The NewStructType() seems to be more standard. But I’m open so suggestions. :-)

Option 2 is certainly the standard, and best to avoid confusing .Init() functions with the special function init(). I'll work on the implementation as soon as we get this series done :)

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#15 lore raw

PATCH 6/24 golang/xenlight: re-name Bitmap marshaling functions

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Re-name and modify signature of toGo function to fromC. The reason for using 'fromC' rather than 'toGo' is that it is not a good idea to define methods on the C types. Also, add error return type to Bitmap's toC function.

Finally, as code-cleanup, re-organize the Bitmap type's comments as per Go conventions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 93 ++++++++++++++++---------------
 1 file changed, 48 insertions(+), 45 deletions(-)
tools/golang/xenlight/xenlight.go +48 -45
index 8196a42855..09fcdca5d1 100644
@@ -234,19 +234,48 @@ func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) {
 	return ckvl, nil
 }
 
-// typedef struct {
-//     uint32_t size;          /* number of bytes in map */
-//     uint8_t *map;
-// } libxl_bitmap;
+// Bitmap represents a libxl_bitmap.
+//
 // Implement the Go bitmap type such that the underlying data can
 // easily be copied in and out.  NB that we still have to do copies
 // both directions, because cgo runtime restrictions forbid passing to
 // a C function a pointer to a Go-allocated structure which contains a
 // pointer.
 type Bitmap struct {
+	// typedef struct {
+	//     uint32_t size;          /* number of bytes in map */
+	//     uint8_t *map;
+	// } libxl_bitmap;
 	bitmap []C.uint8_t
 }
 
+func (bm *Bitmap) fromC(cbm *C.libxl_bitmap) error {
+	// Alloc a Go slice for the bytes
+	size := int(cbm.size)
+	bm.bitmap = make([]C.uint8_t, size)
+
+	// Make a slice pointing to the C array
+	mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
+
+	// And copy the C array into the Go array
+	copy(bm.bitmap, mapslice)
+
+	return nil
+}
+
+func (bm *Bitmap) toC() (C.libxl_bitmap, error) {
+	var cbm C.libxl_bitmap
+
+	size := len(bm.bitmap)
+	cbm.size = C.uint32_t(size)
+	cbm._map = (*C.uint8_t)(C.malloc(C.ulong(cbm.size) * C.sizeof_uint8_t))
+	cslice := (*[1 << 31]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
+
+	copy(cslice, bm.bitmap)
+
+	return cbm, nil
+}
+
 /*
  * Types: IDL
  *
@@ -447,7 +476,7 @@ func (cci C.libxl_cpupoolinfo) toGo() (gci CpupoolInfo) {
 	gci.PoolName = C.GoString(cci.pool_name)
 	gci.Scheduler = Scheduler(cci.sched)
 	gci.DomainCount = int(cci.n_dom)
-	gci.Cpumap = cci.cpumap.toGo()
+	gci.Cpumap.fromC(&cci.cpumap)
 
 	return
 }
@@ -521,7 +550,10 @@ func (Ctx *Context) CpupoolCreate(Name string, Scheduler Scheduler, Cpumap Bitma
 	var uuid C.libxl_uuid
 	C.libxl_uuid_generate(&uuid)
 
-	cbm := Cpumap.toC()
+	cbm, err := Cpumap.toC()
+	if err != nil {
+		return
+	}
 	defer C.libxl_bitmap_dispose(&cbm)
 
 	ret := C.libxl_cpupool_create(Ctx.ctx, name, C.libxl_scheduler(Scheduler),
@@ -576,7 +608,10 @@ func (Ctx *Context) CpupoolCpuaddCpumap(Poolid uint32, Cpumap Bitmap) (err error
 		return
 	}
 
-	cbm := Cpumap.toC()
+	cbm, err := Cpumap.toC()
+	if err != nil {
+		return
+	}
 	defer C.libxl_bitmap_dispose(&cbm)
 
 	ret := C.libxl_cpupool_cpuadd_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm)
@@ -612,7 +647,10 @@ func (Ctx *Context) CpupoolCpuremoveCpumap(Poolid uint32, Cpumap Bitmap) (err er
 		return
 	}
 
-	cbm := Cpumap.toC()
+	cbm, err := Cpumap.toC()
+	if err != nil {
+		return
+	}
 	defer C.libxl_bitmap_dispose(&cbm)
 
 	ret := C.libxl_cpupool_cpuremove_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm)
@@ -735,41 +773,6 @@ func (Ctx *Context) CpupoolMakeFree(Cpumap Bitmap) (err error) {
  * Bitmap operations
  */
 
-// Return a Go bitmap which is a copy of the referred C bitmap.
-func (cbm C.libxl_bitmap) toGo() (gbm Bitmap) {
-	// Alloc a Go slice for the bytes
-	size := int(cbm.size)
-	gbm.bitmap = make([]C.uint8_t, size)
-
-	// Make a slice pointing to the C array
-	mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
-
-	// And copy the C array into the Go array
-	copy(gbm.bitmap, mapslice)
-
-	return
-}
-
-// Must be C.libxl_bitmap_dispose'd of afterwards
-func (gbm Bitmap) toC() (cbm C.libxl_bitmap) {
-	C.libxl_bitmap_init(&cbm)
-
-	size := len(gbm.bitmap)
-	cbm._map = (*C.uint8_t)(C.malloc(C.size_t(size)))
-	cbm.size = C.uint32_t(size)
-	if cbm._map == nil {
-		panic("C.calloc failed!")
-	}
-
-	// Make a slice pointing to the C array
-	mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
-
-	// And copy the Go array into the C array
-	copy(mapslice, gbm.bitmap)
-
-	return
-}
-
 func (bm *Bitmap) Test(bit int) bool {
 	ubit := uint(bit)
 	if bit > bm.Max() || bm.bitmap == nil {
@@ -1158,8 +1161,8 @@ func (cvci C.libxl_vcpuinfo) toGo() (gvci Vcpuinfo) {
 	gvci.Blocked = bool(cvci.blocked)
 	gvci.Running = bool(cvci.running)
 	gvci.VCpuTime = time.Duration(cvci.vcpu_time)
-	gvci.Cpumap = cvci.cpumap.toGo()
-	gvci.CpumapSoft = cvci.cpumap_soft.toGo()
+	gvci.Cpumap.fromC(&cvci.cpumap)
+	gvci.CpumapSoft.fromC(&cvci.cpumap_soft)
 
 	return
 }
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 6/24
#16 lore raw

Re: [Xen-devel] [PATCH 06/24] golang/xenlight: re-name Bitmap marshaling functions

Quoted 10 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Re-name and modify signature of toGo function to fromC. The reason for using 'fromC' rather than 'toGo' is that it is not a good idea to define methods on the C types. Also, add error return type to Bitmap's toC function.

Finally, as code-cleanup, re-organize the Bitmap type's comments as per Go conventions.

With one minor comment...

Quoted 11 lines
tools/golang/xenlight/xenlight.go +11 -0
+func (bm *Bitmap) fromC(cbm *C.libxl_bitmap) error {
+	// Alloc a Go slice for the bytes
+	size := int(cbm.size)
+	bm.bitmap = make([]C.uint8_t, size)
+
+	// Make a slice pointing to the C array
+	mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]
+
+	// And copy the C array into the Go array
+	copy(bm.bitmap, mapslice)
+

tools/golang/xenlight/xenlight.go func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) { · line 272

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+	return nil
+}
+
+func (bm *Bitmap) toC() (C.libxl_bitmap, error) {
+	var cbm C.libxl_bitmap
+
+	size := len(bm.bitmap)
+	cbm.size = C.uint32_t(size)
+	cbm._map = (*C.uint8_t)(C.malloc(C.ulong(cbm.size) * C.sizeof_uint8_t))
+	cslice := (*[1 << 31]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size]

Any particular reason to use cslice here rather than mapslice (or vice versa)?

Not a big deal, but since they're of the came element in the C struct, it seems like it would be better to give them the same name. (Don't have a strong opinion on which one).

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw

Re: [Xen-devel] [PATCH 06/24] golang/xenlight: re-name Bitmap marshaling functions

Quoted 6 lines

Any particular reason to use cslice here rather than mapslice (or vice versa)?

Not a big deal, but since they're of the came element in the C struct, it seems like it would be better to give them the same name. (Don't have a strong opinion on which one).

IIRC, I found the name mapslice a little confusing, since it wasn't of type []map[T1]T2. But, as to the inconsistent naming between the two functions, I agree. I'll name them both cslice.

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#18 lore raw

Re: [Xen-devel] [PATCH 06/24] golang/xenlight: re-name Bitmap marshaling functions

Quoted 10 lines · Nick Rosbrook
Quoted 6 lines

Any particular reason to use cslice here rather than mapslice (or vice versa)?

Not a big deal, but since they're of the came element in the C struct, it seems like it would be better to give them the same name. (Don't have a strong opinion on which one).

IIRC, I found the name mapslice a little confusing, since it wasn't of type []map[T1]T2. But, as to the inconsistent naming between the two functions, I agree. I'll name them both cslice.

I think I named it that way because it's a slice fake-up of the struct element map. But it certainly has the risk of confusing people, and I'm a big believer in absolutely minimizing useless cognitive load. :-)

Another approach might be to just choose a short "metavariable" for this sort of thing; i.e., if you use i and j, everyone knows they're going to be indexes for nested loops. cslice is OK but is probably longer than necessary.

Anyway, I'll let you paint this bike shed whatever color you want; just make sure all sheds for the same struct are the same color. ;-)

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#19 lore raw

PATCH 7/24 golang/xenlight: define StringList builtin type

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define StringList as []string an implement fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
tools/golang/xenlight/xenlight.go +29 -0
index 09fcdca5d1..a3a1836d31 100644
@@ -234,6 +234,35 @@ func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) {
 	return ckvl, nil
 }
 
+// StringList represents a libxl_string_list.
+type StringList []string
+
+func (sl StringList) fromC(csl *C.libxl_string_list) error {
+	size := int(C.libxl_string_list_length(csl))
+	list := (*[1 << 30]*C.char)(unsafe.Pointer(csl))[:size:size]
+
+	sl = make([]string, size)
+
+	for i, v := range list {
+		sl[i] = C.GoString(v)
+	}
+
+	return nil
+}
+
+func (sl StringList) toC() (C.libxl_string_list, error) {
+	var char *C.char
+	size := len(sl)
+	csl := (C.libxl_string_list)(C.malloc(C.ulong(size) * C.ulong(unsafe.Sizeof(char))))
+	clist := (*[1 << 30]*C.char)(unsafe.Pointer(csl))[:size:size]
+
+	for i, v := range sl {
+		clist[i] = C.CString(v)
+	}
+
+	return csl, nil
+}
+
 // Bitmap represents a libxl_bitmap.
 //
 // Implement the Go bitmap type such that the underlying data can
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#20 lore raw

Re: [Xen-devel] [PATCH 07/24] golang/xenlight: define StringList builtin type

Quoted 19 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define StringList as []string an implement fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
tools/golang/xenlight/xenlight.go +0 -0
index 09fcdca5d1..a3a1836d31 100644
@@ -234,6 +234,35 @@ func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) {
 	return ckvl, nil

tools/golang/xenlight/xenlight.go func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) { · line 244

Quoted 10 lines
tools/golang/xenlight/xenlight.go +8 -0
 }
 
+// StringList represents a libxl_string_list.
+type StringList []string
+
+func (sl StringList) fromC(csl *C.libxl_string_list) error {
+	size := int(C.libxl_string_list_length(csl))
+	list := (*[1 << 30]*C.char)(unsafe.Pointer(csl))[:size:size]
+
+	sl = make([]string, size)

Doesn't this method want a pointer receiver?

Everything else looks good.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#21 lore raw

Re: [Xen-devel] [PATCH 07/24] golang/xenlight: define StringList builtin type

Quoted 1 line

Doesn't this method want a pointer receiver?

Yes, since I'm allocating a new slice. If I wasn't allocating a new slice, this would be okay since the slice contains a pointer to the underlying array.

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#22 lore raw

PATCH 8/24 golang/xenlight: define Mac builtin type

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define Mac as [6]byte and implement fromC, toC, and String functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 35 +++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/golang/xenlight/xenlight.go +35 -0
index a3a1836d31..3b7824b284 100644
@@ -181,6 +181,41 @@ func (d *Defbool) toC() (C.libxl_defbool, error) {
 	return c, nil
 }
 
+// Mac represents a libxl_mac, or simply a MAC address.
+type Mac [6]byte
+
+// String formats a Mac address to string representation.
+func (mac Mac) String() string {
+	s := "%x:%x:%x:%x:%x:%x"
+	opts := make([]interface{}, 6)
+
+	for i, v := range mac {
+		opts[i] = v
+	}
+
+	return fmt.Sprintf(s, opts...)
+}
+
+func (mac *Mac) fromC(cmac *C.libxl_mac) error {
+	b := (*[6]C.uint8_t)(unsafe.Pointer(cmac))
+
+	for i, v := range b {
+		mac[i] = byte(v)
+	}
+
+	return nil
+}
+
+func (mac *Mac) toC() (C.libxl_mac, error) {
+	var cmac C.libxl_mac
+
+	for i, v := range mac {
+		cmac[i] = C.uint8_t(v)
+	}
+
+	return cmac, nil
+}
+
 type Context struct {
 	ctx    *C.libxl_ctx
 	logger *C.xentoollog_logger_stdiostream
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#23 lore raw

Re: [Xen-devel] [PATCH 08/24] golang/xenlight: define Mac builtin type

Quoted 22 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define Mac as [6]byte and implement fromC, toC, and String functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 35 +++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
tools/golang/xenlight/xenlight.go +1 -0
index a3a1836d31..3b7824b284 100644
@@ -181,6 +181,41 @@ func (d *Defbool) toC() (C.libxl_defbool, error) {
 	return c, nil
 }
 
+// Mac represents a libxl_mac, or simply a MAC address.

tools/golang/xenlight/xenlight.go func (d *Defbool) toC() (C.libxl_defbool, error) { · line 194

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+type Mac [6]byte
+
+// String formats a Mac address to string representation.
+func (mac Mac) String() string {
+	s := "%x:%x:%x:%x:%x:%x"
+	opts := make([]interface{}, 6)
+
+	for i, v := range mac {
+		opts[i] = v
+	}

What's the point of this?

I realize it's slightly annoying to have to type mac[0], mac[1], ..., but I'd rather do that once than make the runtime copy everything over into a slice of interfaces every String() call.

Also, I guess the format should be "%02x".

Quoted 5 lines
tools/golang/xenlight/xenlight.go +5 -0
+
+	return fmt.Sprintf(s, opts...)
+}
+
+func (mac *Mac) fromC(cmac *C.libxl_mac) error {

tools/golang/xenlight/xenlight.go func (d *Defbool) toC() (C.libxl_defbool, error) { · line 209

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+	b := (*[6]C.uint8_t)(unsafe.Pointer(cmac))
+
+	for i, v := range b {
+		mac[i] = byte(v)
+	}
+
+	return nil
+}
+
+func (mac *Mac) toC() (C.libxl_mac, error) {

Conversely, shouldn't this be a value receiver, since we're don't want this function to change the contents of mac?

Thanks,
-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#24 lore raw

Re: [Xen-devel] [PATCH 08/24] golang/xenlight: define Mac builtin type

Quoted 5 lines

What's the point of this?

I realize it's slightly annoying to have to type mac[0], mac[1], ..., but I'd rather do that once than make the runtime copy everything over into a slice of interfaces every String() call.

As I think you realized by looking at subsequent patches, this is to get around the fact that "an array of an interface type != an array of type that implements that interface." Since this is a small array, I'm fine with explicitly passing each element of the array to fmt.Sprintf.

Quoted 1 line

Also, I guess the format should be "%02x".

Yeah, thanks.

Quoted 2 lines

Conversely, shouldn't this be a value receiver, since we're don't want this function to change the contents of mac?

Conventionally receivers are kept consistent between methods of a type, unless it's implementing some interface like Stringer. And, it's consistent with the other toC functions which are defined with pointers in the generated functions since there are some large structs.

But, yes this could just be a value receiver. I don't have a strong opinion to keep it as is, so I'll change it.

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#25 lore raw

Re: [Xen-devel] [PATCH 08/24] golang/xenlight: define Mac builtin type

Quoted 10 lines · Nick Rosbrook
Quoted 5 lines

What's the point of this?

I realize it's slightly annoying to have to type mac[0], mac[1], ..., but I'd rather do that once than make the runtime copy everything over into a slice of interfaces every String() call.

As I think you realized by looking at subsequent patches, this is to get around the fact that "an array of an interface type != an array of type that implements that interface." Since this is a small array, I'm fine with explicitly passing each element of the array to fmt.Sprintf.

Using mac[0], mac[1], ... for this one is kind of just on the border of reasonable, but when I got to a later patch, I felt like uuid[0], uuid[1], ... uuid[15] was a bit much. And on further reflection, the fact is that all of these will be changed to interface{} under the hood by the function call and then reflect'ed to determine their type anyway; i.e., I'm pretty sure:

    fmt.Sprintf(s, mac[0], mac[1], /* &c */)

from a compiler perspective is basically identical to:

    fmt.Sprintf(s, []interface{}{mac[0], mac[1], /* &c */}...)

So the code you have is probably going to be about equally efficient anyway.

So I guess, unless you feel strongly that using mac[0], mac[1], ... is a better way to go, maybe just leave it the way it is for now.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#26 lore raw

Re: [Xen-devel] [PATCH 08/24] golang/xenlight: define Mac builtin type

Quoted 1 line

So the code you have is probably going to be about equally efficient anyway.

A quick benchmark [1] shows:

goos: linux
goarch: amd64

BenchmarkString1-8        5000000           251 ns/op
BenchmarkString2-8        5000000           247 ns/op

So yes, they're about the same :)

I'll leave it as is.

-NR

[1] https://play.golang.org/p/2cOzBpoTfgE

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#27 lore raw

PATCH 9/24 golang/xenlight: define MsVmGenid builtin type

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define MsVmGenid as [int(C.LIBXL_MS_VM_GENID_LEN)]byte and implement fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/golang/xenlight/xenlight.go +23 -0
index 3b7824b284..6aca5b89c0 100644
@@ -216,6 +216,29 @@ func (mac *Mac) toC() (C.libxl_mac, error) {
 	return cmac, nil
 }
 
+// MsVmGenid represents a libxl_ms_vm_genid.
+type MsVmGenid [int(C.LIBXL_MS_VM_GENID_LEN)]byte
+
+func (mvg *MsVmGenid) fromC(cmvg *C.libxl_ms_vm_genid) error {
+	b := (*[int(C.LIBXL_MS_VM_GENID_LEN)]C.uint8_t)(unsafe.Pointer(&cmvg.bytes[0]))
+
+	for i, v := range b {
+		mvg[i] = byte(v)
+	}
+
+	return nil
+}
+
+func (mvg *MsVmGenid) toC() (C.libxl_ms_vm_genid, error) {
+	var cmvg C.libxl_ms_vm_genid
+
+	for i, v := range mvg {
+		cmvg.bytes[i] = C.uint8_t(v)
+	}
+
+	return cmvg, nil
+}
+
 type Context struct {
 	ctx    *C.libxl_ctx
 	logger *C.xentoollog_logger_stdiostream
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 9/24
#28 lore raw

Re: [Xen-devel] [PATCH 09/24] golang/xenlight: define MsVmGenid builtin type

Quoted 20 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define MsVmGenid as [int(C.LIBXL_MS_VM_GENID_LEN)]byte and implement fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
tools/golang/xenlight/xenlight.go +0 -0
index 3b7824b284..6aca5b89c0 100644
@@ -216,6 +216,29 @@ func (mac *Mac) toC() (C.libxl_mac, error) {
 	return cmac, nil
 }

tools/golang/xenlight/xenlight.go func (mac *Mac) toC() (C.libxl_mac, error) { · line 227

Quoted 10 lines
tools/golang/xenlight/xenlight.go +9 -0
 
+// MsVmGenid represents a libxl_ms_vm_genid.
+type MsVmGenid [int(C.LIBXL_MS_VM_GENID_LEN)]byte
+
+func (mvg *MsVmGenid) fromC(cmvg *C.libxl_ms_vm_genid) error {
+	b := (*[int(C.LIBXL_MS_VM_GENID_LEN)]C.uint8_t)(unsafe.Pointer(&cmvg.bytes[0]))
+
+	for i, v := range b {
+		mvg[i] = byte(v)
+	}

It's slightly annoying to have to do this loop, but I guess given that we have to technically cast each element of the array. Hopefully the compiler will eventually be able to figure out what's going on here and just do a memcpy.

tools/golang/xenlight/xenlight.go func (mac *Mac) toC() (C.libxl_mac, error) { · line 232

Quoted 5 lines
tools/golang/xenlight/xenlight.go +5 -0
+
+	return nil
+}
+
+func (mvg *MsVmGenid) toC() (C.libxl_ms_vm_genid, error) {

Hmm, I guess we want a pointer receiver here to make sure we don't end up doing a 16-byte copy.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#29 lore raw

PATCH 10/24 golang/xenlight: define EvLink builtin as empty struct

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define EvLink as empty struct as there is currently no reason the internal of this type should be used in Go.

Implement fromC and toC functions as no-ops.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/golang/xenlight/xenlight.go +10 -0
index 6aca5b89c0..d41de253f3 100644
@@ -239,6 +239,16 @@ func (mvg *MsVmGenid) toC() (C.libxl_ms_vm_genid, error) {
 	return cmvg, nil
 }
 
+// EvLink represents a libxl_ev_link.
+//
+// Represented as an empty struct for now, as there is no
+// apparent need for the internals of this type to be exposed
+// through the Go package.
+type EvLink struct{}
+
+func (el *EvLink) fromC(cel *C.libxl_ev_link) error      { return nil }
+func (el *EvLink) toC() (cel C.libxl_ev_link, err error) { return }
+
 type Context struct {
 	ctx    *C.libxl_ctx
 	logger *C.xentoollog_logger_stdiostream
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 10/24
#30 lore raw

Re: [Xen-devel] [PATCH 10/24] golang/xenlight: define EvLink builtin as empty struct

Quoted 8 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define EvLink as empty struct as there is currently no reason the internal of this type should be used in Go.

Implement fromC and toC functions as no-ops.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#31 lore raw

PATCH 11/24 golang/xenlight: define CpuidPolicyList builtin type

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define CpuidPolicyList as a wrapper struct with field val of type *C.libxl_cpuid_policy_list and implement fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tools/golang/xenlight/xenlight.go +20 -0
index d41de253f3..9c384485e1 100644
@@ -249,6 +249,26 @@ type EvLink struct{}
 func (el *EvLink) fromC(cel *C.libxl_ev_link) error      { return nil }
 func (el *EvLink) toC() (cel C.libxl_ev_link, err error) { return }
 
+// CpuidPolicyList represents a libxl_cpuid_policy_list.
+type CpuidPolicyList struct {
+	val *C.libxl_cpuid_policy_list
+}
+
+func (cpl *CpuidPolicyList) fromC(ccpl *C.libxl_cpuid_policy_list) error {
+	cpl.val = ccpl
+	return nil
+}
+
+func (cpl *CpuidPolicyList) toC() (C.libxl_cpuid_policy_list, error) {
+	if cpl.val == nil {
+		var c C.libxl_cpuid_policy_list
+		return c, nil
+	}
+	ccpl := (*C.libxl_cpuid_policy_list)(unsafe.Pointer(cpl.val))
+
+	return *ccpl, nil
+}
+
 type Context struct {
 	ctx    *C.libxl_ctx
 	logger *C.xentoollog_logger_stdiostream
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#32 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 19 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Define CpuidPolicyList as a wrapper struct with field val of type *C.libxl_cpuid_policy_list and implement fromC and toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tools/golang/xenlight/xenlight.go +0 -0
index d41de253f3..9c384485e1 100644

tools/golang/xenlight/xenlight.go type EvLink struct{} · line 255

Quoted 7 lines
tools/golang/xenlight/xenlight.go +4 -0
@@ -249,6 +249,26 @@ type EvLink struct{}
 func (el *EvLink) fromC(cel *C.libxl_ev_link) error      { return nil }
 func (el *EvLink) toC() (cel C.libxl_ev_link, err error) { return }
 
+// CpuidPolicyList represents a libxl_cpuid_policy_list.
+type CpuidPolicyList struct {
+	val *C.libxl_cpuid_policy_list
+}

Hmm, this introduces a pretty significant risk of memory leaks; but I don't really see any way around it. I guess we really want to do some SetFinalizer() magic on this to call libxl_cpuid_dispose()?

We might also want to add something like a .Dispose() method to have predictable memory effects. But then do we want to have a .Dispose() method on all types that might contain a CpuidPolicyList? Technically we're supposed to, so we might have to. (And now I'm having deja vu, like we've had this discussion before, but I can't seem to find it.)

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#33 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 9 lines

Hmm, this introduces a pretty significant risk of memory leaks; but I don't really see any way around it. I guess we really want to do some SetFinalizer() magic on this to call libxl_cpuid_dispose()?

We might also want to add something like a .Dispose() method to have predictable memory effects. But then do we want to have a .Dispose() method on all types that might contain a CpuidPolicyList? Technically we're supposed to, so we might have to. (And now I'm having deja vu, like we've had this discussion before, but I can't seem to find it.)

As I've expressed before, I don't think its a good idea to look to the runtime to fix this sort of problem, so I'd be more inclined to look into a Dispose like option. But then it does seem weird from an API perspective to only define Dispose on some types since it introduces a closer, but incomplete, semantic relationship between libxl and the Go package.

WRT the definition of CpuidPolicyList, is the best we can do? Or is there a way we can hide the use of the C type better so that someone using this package doesn't need to worry about calling Dispose or otherwise? I think [1] is where we originally discussed this.

-NR

[1] https://lists.xenproject.org/archives/html/xen-devel/2019-09/msg01112.html

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#34 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 17 lines · Nick Rosbrook
Quoted 9 lines

Hmm, this introduces a pretty significant risk of memory leaks; but I don't really see any way around it. I guess we really want to do some SetFinalizer() magic on this to call libxl_cpuid_dispose()?

We might also want to add something like a .Dispose() method to have predictable memory effects. But then do we want to have a .Dispose() method on all types that might contain a CpuidPolicyList? Technically we're supposed to, so we might have to. (And now I'm having deja vu, like we've had this discussion before, but I can't seem to find it.)

As I've expressed before, I don't think its a good idea to look to the runtime to fix this sort of problem, so I'd be more inclined to look into a Dispose like option. But then it does seem weird from an API perspective to only define Dispose on some types since it introduces a closer, but incomplete, semantic relationship between libxl and the Go package.

Quoted 4 lines

WRT the definition of CpuidPolicyList, is the best we can do? Or is there a way we can hide the use of the C type better so that someone using this package doesn't need to worry about calling Dispose or otherwise? I think [1] is where we originally discussed this.

If we do have to keep the C pointer around for some reason, I think using SetFinalizer is a necessary backstop to keep the library from leaking. It's all well and good to say, "Make sure you call Dispose()", but I think for a GC'd language that's just going to be too easy to forget; and it will be a huge pain for long-running processes.

It is pretty annoying; and this is really the *only* type that has this "opaque structure behind a pointer" property.

If we didn't have this type as a type, we'd have to avoid somehow exposing the user to the functions which take and use it. The main place it's used ATM is in DomainBuildInfo. We could explore whether it would be practical to "implement" CpuidPolicyList as a string, and then have toC() call libxl_cpuid_parse_config(). Obviously that means fromC() would fail; but I'm not sure DomainBuildInfo is really a structure passed "out" of libxl anyway.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#35 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 5 lines

If we do have to keep the C pointer around for some reason, I think using SetFinalizer is a necessary backstop to keep the library from leaking. It's all well and good to say, "Make sure you call Dispose()", but I think for a GC'd language that's just going to be too easy to forget; and it will be a huge pain for long-running processes.

I understand your motivation for wanting to make this fool-proof, but there are plenty of common examples in Go where it's well-understood that if I call NewFoo then I need to foo.Close() (defer'd or otherwise). I don't think that alone is a good enough argument for turning to SetFinalizer. But, I'm certainly not advocating for the Dispose option either - as I said I think that would be unfortunate from an API perspective.

Quoted 7 lines

If we didn't have this type as a type, we'd have to avoid somehow exposing the user to the functions which take and use it. The main place it's used ATM is in DomainBuildInfo. We could explore whether it would be practical to "implement" CpuidPolicyList as a string, and then have toC() call libxl_cpuid_parse_config(). Obviously that means fromC() would fail; but I'm not sure DomainBuildInfo is really a structure passed "out" of libxl anyway.

It's sounding more and more like we need a way to give types an "exported/unexported" attribute in the IDL.

Why exactly would fromC be doomed to fail? Just because there is no libxl_cpuid_to_string or otherwise? In any case, I think defining it as a string may be a good intermediate option for now (even if it means fromC has to be a no-op). That way we can ensure calls to libxl_cpuid_dipose as usual.

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#36 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 22 lines · Nick Rosbrook
Quoted 5 lines

If we do have to keep the C pointer around for some reason, I think using SetFinalizer is a necessary backstop to keep the library from leaking. It's all well and good to say, "Make sure you call Dispose()", but I think for a GC'd language that's just going to be too easy to forget; and it will be a huge pain for long-running processes.

I understand your motivation for wanting to make this fool-proof, but there are plenty of common examples in Go where it's well-understood that if I call NewFoo then I need to foo.Close() (defer'd or otherwise). I don't think that alone is a good enough argument for turning to SetFinalizer. But, I'm certainly not advocating for the Dispose option either - as I said I think that would be unfortunate from an API perspective.

Quoted 7 lines

If we didn't have this type as a type, we'd have to avoid somehow exposing the user to the functions which take and use it. The main place it's used ATM is in DomainBuildInfo. We could explore whether it would be practical to "implement" CpuidPolicyList as a string, and then have toC() call libxl_cpuid_parse_config(). Obviously that means fromC() would fail; but I'm not sure DomainBuildInfo is really a structure passed "out" of libxl anyway.

Quoted 5 lines

It's sounding more and more like we need a way to give types an "exported/unexported" attribute in the IDL.

Why exactly would fromC be doomed to fail? Just because there is no libxl_cpuid_to_string or otherwise?

Sorry, I was typing this in a bit of a rush at the end of the day yesterday. :-)

Yes, that's what I meant: There's basically no way to read a policy from libxl and then pass it back to libxl (since there's no way to convert libxl_cpuid_policy_list => CpuidPolicyList => libxl_cpuid_policy_list again).

But at the moment, a string is the "preferred form of modification" as it were; so if such a read-modify-write feature were implemented, libxl would need to add libxl_cpuid_to_string anyway. (Or give some other modifiable form.)

Quoted 4 lines

In any case, I think defining it
as a string may be a good intermediate option for now (even if it means fromC has to be a no-op). That way we can ensure calls to libxl_cpuid_dipose as usual.

Yes, let's do that.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#37 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 1 line

Yes, let's do that.

Okay, will do.

As a point of clarification, should I be waiting until you've reviewed all patches in v1 before I send v2 of this series? Or do you prefer that I send a v2 that addresses your review so far?

Thanks,
-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#38 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 7 lines · Nick Rosbrook
Quoted 1 line

Yes, let's do that.

Okay, will do.

As a point of clarification, should I be waiting until you've reviewed all patches in v1 before I send v2 of this series? Or do you prefer that I send a v2 that addresses your review so far?

On the whole I think sending v2 earlier is better, since I'll have the discussions more recently in my head, and so will (hopefully) be able to get an Ack or R-b more quickly.

When the development window is open, stuff can be checked in as it's reviewed, making the whole thing easier.

To be clear, this is for times when the review of the whole series is taking longer than a few days. If I review 3 patches of a 6-patch series one day, probably better to give me a chance to finish the next day before sending vN+1. :-) But if I stall for a few days, go ahead and resend.

Thanks,
-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#39 lore raw

Re: [Xen-devel] [PATCH 11/24] golang/xenlight: define CpuidPolicyList builtin type

Quoted 4 lines

On the whole I think sending v2 earlier is better, since I'll have the discussions more recently in my head, and so will (hopefully) be able to get an Ack or R-b more quickly.

Quoted 8 lines

When the development window is open, stuff can be checked in as it's reviewed, making the whole thing easier.

To be clear, this is for times when the review of the whole series is taking longer than a few days. If I review 3 patches of a 6-patch series one day, probably better to give me a chance to finish the next day before sending vN+1. :-) But if I stall for a few days, go ahead and resend.

Okay thanks for the clarification. I'll plan on sending v2 once I make these changes to CpuidPolicyList.

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#40 lore raw

PATCH 12/24 golang/xenlight: re-factor Uuid type implementation

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Re-define Uuid as [16]byte and implement fromC, toC, and String functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 37 +++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
tools/golang/xenlight/xenlight.go +35 -2
index 9c384485e1..3e3753f92e 100644
@@ -86,7 +86,40 @@ type Devid int
 
 type MemKB uint64
 
-type Uuid C.libxl_uuid
+// Uuid is a domain UUID.
+type Uuid [16]byte
+
+// String formats a Uuid in the form "xxxx-xx-xx-xx-xxxxxx".
+func (u Uuid) String() string {
+	s := "%x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x"
+	opts := make([]interface{}, 16)
+
+	for i, v := range u {
+		opts[i] = v
+	}
+
+	return fmt.Sprintf(s, opts...)
+}
+
+func (u *Uuid) fromC(c *C.libxl_uuid) error {
+	b := (*[16]C.uint8_t)(unsafe.Pointer(&c.uuid[0]))
+
+	for i, v := range b {
+		u[i] = byte(v)
+	}
+
+	return nil
+}
+
+func (u *Uuid) toC() (C.libxl_uuid, error) {
+	var c C.libxl_uuid
+
+	for i, v := range u {
+		c.uuid[i] = C.uint8_t(v)
+	}
+
+	return c, nil
+}
 
 // defboolVal represents a defbool value.
 type defboolVal int
@@ -516,7 +549,7 @@ type Dominfo struct {
 func (cdi *C.libxl_dominfo) toGo() (di *Dominfo) {
 
 	di = &Dominfo{}
-	di.Uuid = Uuid(cdi.uuid)
+	di.Uuid.fromC(&cdi.uuid)
 	di.Domid = Domid(cdi.domid)
 	di.Ssidref = uint32(cdi.ssidref)
 	di.SsidLabel = C.GoString(cdi.ssid_label)
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#41 lore raw

Re: [Xen-devel] [PATCH 12/24] golang/xenlight: re-factor Uuid type implementation

Quoted 25 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Re-define Uuid as [16]byte and implement fromC, toC, and String functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 37 +++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)
tools/golang/xenlight/xenlight.go +3 -1
index 9c384485e1..3e3753f92e 100644
@@ -86,7 +86,40 @@ type Devid int
 
 type MemKB uint64
 
-type Uuid C.libxl_uuid
+// Uuid is a domain UUID.
+type Uuid [16]byte
+

tools/golang/xenlight/xenlight.go type Devid int · line 101

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+// String formats a Uuid in the form "xxxx-xx-xx-xx-xxxxxx".
+func (u Uuid) String() string {
+	s := "%x%x%x%x-%x%x-%x%x-%x%x-%x%x%x%x%x%x"
+	opts := make([]interface{}, 16)
+
+	for i, v := range u {
+		opts[i] = v
+	}
+
+	return fmt.Sprintf(s, opts...)

*Sigh* Is there really no better way to do this? (Not complaining at you, more at the language really...)

Everything else looks good.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#42 lore raw

PATCH 13/24 golang/xenlight: re-factor Hwcap type implementation

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Re-define Hwcap as [8]uint32, and implement toC function. Also, re-name and modify signature of toGo function to fromC.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
tools/golang/xenlight/xenlight.go +19 -10
index 3e3753f92e..8d520bbd98 100644
@@ -307,20 +307,29 @@ type Context struct {
 	logger *C.xentoollog_logger_stdiostream
 }
 
-type Hwcap []C.uint32_t
-
-func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) {
-	// Alloc a Go slice for the bytes
-	size := 8
-	ghwcap = make([]C.uint32_t, size)
+// Hwcap represents a libxl_hwcap.
+type Hwcap [8]uint32
 
+func (hwcap *Hwcap) fromC(chwcap *C.libxl_hwcap) error {
 	// Make a slice pointing to the C array
-	mapslice := (*[1 << 30]C.uint32_t)(unsafe.Pointer(&chwcap[0]))[:size:size]
+	mapslice := (*[8]C.uint32_t)(unsafe.Pointer(chwcap))
 
 	// And copy the C array into the Go array
-	copy(ghwcap, mapslice)
+	for i, v := range mapslice {
+		hwcap[i] = uint32(v)
+	}
 
-	return
+	return nil
+}
+
+func (hwcap *Hwcap) toC() (C.libxl_hwcap, error) {
+	var chwcap C.libxl_hwcap
+
+	for i, v := range hwcap {
+		chwcap[i] = C.uint32_t(v)
+	}
+
+	return chwcap, nil
 }
 
 // KeyValueList represents a libxl_key_value_list.
@@ -465,7 +474,7 @@ func (cphys *C.libxl_physinfo) toGo() (physinfo *Physinfo) {
 	physinfo.SharingFreedPages = uint64(cphys.sharing_freed_pages)
 	physinfo.SharingUsedFrames = uint64(cphys.sharing_used_frames)
 	physinfo.NrNodes = uint32(cphys.nr_nodes)
-	physinfo.HwCap = cphys.hw_cap.toGo()
+	physinfo.HwCap.fromC(&cphys.hw_cap)
 	physinfo.CapHvm = bool(cphys.cap_hvm)
 	physinfo.CapHvmDirectio = bool(cphys.cap_hvm_directio)
 
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v1 13/24
#43 lore raw

Re: [Xen-devel] [PATCH 13/24] golang/xenlight: re-factor Hwcap type implementation

Quoted 6 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Re-define Hwcap as [8]uint32, and implement toC function. Also, re-name and modify signature of toGo function to fromC.

Looks good. Just one comment...

Quoted 19 lines

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
tools/golang/xenlight/xenlight.go +0 -3
index 3e3753f92e..8d520bbd98 100644
@@ -307,20 +307,29 @@ type Context struct {
 	logger *C.xentoollog_logger_stdiostream
 }
 
-type Hwcap []C.uint32_t
-
-func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) {

tools/golang/xenlight/xenlight.go type Context struct { · line 315

Quoted 10 lines
tools/golang/xenlight/xenlight.go +4 -4
-	// Alloc a Go slice for the bytes
-	size := 8
-	ghwcap = make([]C.uint32_t, size)
+// Hwcap represents a libxl_hwcap.
+type Hwcap [8]uint32
 
+func (hwcap *Hwcap) fromC(chwcap *C.libxl_hwcap) error {
 	// Make a slice pointing to the C array
-	mapslice := (*[1 << 30]C.uint32_t)(unsafe.Pointer(&chwcap[0]))[:size:size]
+	mapslice := (*[8]C.uint32_t)(unsafe.Pointer(chwcap))

To be pedantic, this isn't a slice anymore. :-) Do you want to change this to 'b' (or maybe 'u' or something like that) and fix the comment?

With that changed:

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#44 lore raw

PATCH 14/24 golang/xenlight: generate structs from the IDL

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Add struct and keyed union generation to gengotypes.py. For keyed unions, use a method similar to gRPC's oneof to interpret C unions as Go types. Meaning, for a given struct with a union field, generate a struct for each sub-struct defined in the union. Then, define an interface of one method which is implemented by each of the defined sub-structs. For example:

  type domainBuildInfoTypeUnion interface {
          isdomainBuildInfoTypeUnion()
  }

  type DomainBuildInfoTypeUnionHvm struct {
      // HVM-specific fields...
  }

func (x DomainBuildInfoTypeUnionHvm) isdomainBuildInfoTypeUnion() {}

  type DomainBuildInfoTypeUnionPv struct {
      // PV-specific fields...
  }

func (x DomainBuildInfoTypeUnionPv) isdomainBuildInfoTypeUnion() {}

  type DomainBuildInfoTypeUnionPvh struct {
      // PVH-specific fields...
  }

func (x DomainBuildInfoTypeUnionPvh) isdomainBuildInfoTypeUnion() {}

Then, remove existing struct definitions in xenlight.go that conflict with the generated types, and modify existing marshaling functions to align with the new type definitions. Notably, drop "time" package since fields of type time.Duration are now of type uint64.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py     | 103 +++
 tools/golang/xenlight/xenlight.go       | 123 +---
 tools/golang/xenlight/xenlight_types.go | 834 ++++++++++++++++++++++++
 3 files changed, 952 insertions(+), 108 deletions(-)
tools/golang/xenlight/gengotypes.py +103 -0
index 59307492cb..c8513b79e0 100644
@@ -18,6 +18,10 @@ builtin_type_names = {
     idl.uint64.typename: 'uint64',
 }
 
+# List of strings that need to be written to a file
+# after a struct definition.
+type_extras = []
+
 def xenlight_golang_generate_types(path = None, types = None, comment = None):
     """
     Generate a .go file (xenlight_types.go by default)
@@ -35,6 +39,13 @@ def xenlight_golang_generate_types(path = None, types = None, comment = None):
             f.write(xenlight_golang_type_define(ty))
             f.write('\n')
 
+            # Append extra types
+            for extra in type_extras:
+                f.write(extra)
+                f.write('\n')
+
+            del type_extras[:]
+
     go_fmt(path)
 
 def xenlight_golang_type_define(ty = None):
@@ -43,6 +54,9 @@ def xenlight_golang_type_define(ty = None):
     if isinstance(ty, idl.Enumeration):
         s += xenlight_golang_define_enum(ty)
 
+    elif isinstance(ty, idl.Aggregate):
+        s += xenlight_golang_define_struct(ty)
+
     return s
 
 def xenlight_golang_define_enum(ty = None):
@@ -65,6 +79,95 @@ def xenlight_golang_define_enum(ty = None):
 
     return s
 
+def xenlight_golang_define_struct(ty = None, typename = None, nested = False):
+    s = ''
+    name = ''
+
+    if typename is not None:
+        name = xenlight_golang_fmt_name(typename)
+    else:
+        name = xenlight_golang_fmt_name(ty.typename)
+
+    # Begin struct definition
+    if nested:
+        s += '{} struct {{\n'.format(name)
+    else:
+        s += 'type {} struct {{\n'.format(name)
+
+    # Write struct fields
+    for f in ty.fields:
+        if f.type.typename is not None:
+            if isinstance(f.type, idl.Array):
+                typename = f.type.elem_type.typename
+                typename = xenlight_golang_fmt_name(typename)
+                name     = xenlight_golang_fmt_name(f.name)
+
+                s += '{} []{}\n'.format(name, typename)
+            else:
+                typename = f.type.typename
+                typename = xenlight_golang_fmt_name(typename)
+                name     = xenlight_golang_fmt_name(f.name)
+
+                s += '{} {}\n'.format(name, typename)
+
+        elif isinstance(f.type, idl.Struct):
+            s += xenlight_golang_define_struct(f.type, typename=f.name, nested=True)
+
+        elif isinstance(f.type, idl.KeyedUnion):
+            s += xenlight_golang_define_union(f.type, ty.typename)
+
+        else:
+            raise Exception('type {} not supported'.format(f.type))
+
+    # End struct definition
+    s += '}\n'
+
+    return s
+
+def xenlight_golang_define_union(ty = None, structname = ''):
+    """
+    Generate the Go translation of a KeyedUnion.
+
+    Define an unexported interface to be used as
+    the type of the union. Then, define a struct
+    for each field of the union which implements
+    that interface.
+    """
+    s = ''
+
+    interface_name = '{}_{}_union'.format(structname, ty.keyvar.name)
+    interface_name = xenlight_golang_fmt_name(interface_name, exported=False)
+
+    s += 'type {} interface {{\n'.format(interface_name)
+    s += 'is{}()\n'.format(interface_name)
+    s += '}\n'
+
+    type_extras.append(s)
+
+    for f in ty.fields:
+        if f.type is None:
+            continue
+
+        # Define struct
+        name = '{}_{}_union_{}'.format(structname, ty.keyvar.name, f.name)
+        s = xenlight_golang_define_struct(f.type, typename=name)
+        type_extras.append(s)
+
+        # Define function to implement 'union' interface
+        name = xenlight_golang_fmt_name(name)
+        s = 'func (x {}) is{}(){{}}\n'.format(name, interface_name)
+        type_extras.append(s)
+
+    # Return the field entries, but the associated types are generated later.
+    fname = xenlight_golang_fmt_name(ty.keyvar.name)
+    ftype = xenlight_golang_fmt_name(ty.keyvar.type.typename)
+    s = '{} {}\n'.format(fname, ftype)
+
+    fname = xenlight_golang_fmt_name('{}_union'.format(ty.keyvar.name))
+    s += '{} {}\n'.format(fname, interface_name)
+
+    return s
+
 def xenlight_golang_fmt_name(name, exported = True):
     """
     Take a given type name and return an
tools/golang/xenlight/xenlight.go +15 -108

194 lines not on this page · raw message

tools/golang/xenlight/xenlight_types.go +834 -0

885 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#45 lore raw

Re: [Xen-devel] [PATCH 14/24] golang/xenlight: generate structs from the IDL

Quoted 29 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Add struct and keyed union generation to gengotypes.py. For keyed unions, use a method similar to gRPC's oneof to interpret C unions as Go types. Meaning, for a given struct with a union field, generate a struct for each sub-struct defined in the union. Then, define an interface of one method which is implemented by each of the defined sub-structs. For example:

  type domainBuildInfoTypeUnion interface {
          isdomainBuildInfoTypeUnion()
  }

  type DomainBuildInfoTypeUnionHvm struct {
      // HVM-specific fields...
  }

func (x DomainBuildInfoTypeUnionHvm) isdomainBuildInfoTypeUnion() {}

  type DomainBuildInfoTypeUnionPv struct {
      // PV-specific fields...
  }

func (x DomainBuildInfoTypeUnionPv) isdomainBuildInfoTypeUnion() {}

  type DomainBuildInfoTypeUnionPvh struct {
      // PVH-specific fields...
  }
Quoted 6 lines

func (x DomainBuildInfoTypeUnionPvh) isdomainBuildInfoTypeUnion() {}

Then, remove existing struct definitions in xenlight.go that conflict with the generated types, and modify existing marshaling functions to align with the new type definitions. Notably, drop "time" package since fields of type time.Duration are now of type uint64.

BTW I was discussing with Ian Jackson, and I think at some point it would be worth considering adding in an annotation or something to the IDL such that the generator can use time.Duration for these things. That opens up another can of works, like the fact that Duration is int64 rather than uint64.

But at any rate, that's something to look into *after* this series is checked in.

The generated code here looks really good -- thanks for the work! One comment on the generator...

Quoted 28 lines

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py     | 103 +++
 tools/golang/xenlight/xenlight.go       | 123 +---
 tools/golang/xenlight/xenlight_types.go | 834 ++++++++++++++++++++++++
 3 files changed, 952 insertions(+), 108 deletions(-)
tools/golang/xenlight/gengotypes.py +4 -0
index 59307492cb..c8513b79e0 100644
@@ -18,6 +18,10 @@ builtin_type_names = {
     idl.uint64.typename: 'uint64',
 }
 
+# List of strings that need to be written to a file
+# after a struct definition.
+type_extras = []
+
 def xenlight_golang_generate_types(path = None, types = None, comment = None):
     """
     Generate a .go file (xenlight_types.go by default)

tools/golang/xenlight/gengotypes.py def xenlight_golang_generate_types(path = None, types = None, comment = None): · line 47

Quoted 9 lines
tools/golang/xenlight/gengotypes.py +6 -0
@@ -35,6 +39,13 @@ def xenlight_golang_generate_types(path = None, types = None, comment = None):
             f.write(xenlight_golang_type_define(ty))
             f.write('\n')
 
+            # Append extra types
+            for extra in type_extras:
+                f.write(extra)
+                f.write('\n')
+
+            del type_extras[:]

So it looks like you've added a global variable, and have the various functions adding to this global variable in the back-door, and then at the toplevel "flush" the output and clear it?

This doesn't seem like great software engineering. :-) Would it be terribly difficult to refactor things to avoid this? Perhaps by returning tuples that are merged together or something?

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#46 lore raw

Re: [Xen-devel] [PATCH 14/24] golang/xenlight: generate structs from the IDL

Quoted 8 lines

BTW I was discussing with Ian Jackson, and I think at some point it would be worth considering adding in an annotation or something to the IDL such that the generator can use time.Duration for these things. That opens up another can of works, like the fact that Duration is int64 rather than uint64.

But at any rate, that's something to look into *after* this series is checked in.

Add it to the list! :)

Quoted 3 lines

This doesn't seem like great software engineering. :-) Would it be terribly difficult to refactor things to avoid this? Perhaps by returning tuples that are merged together or something?

No, I don't think this would be difficult to re-factor. I think either just returning a list of strings, or some n-tuple as you suggest would be fine.

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#47 lore raw

PATCH 15/24 golang/xenlight: remove no-longer used type MemKB

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 2 --
 1 file changed, 2 deletions(-)
tools/golang/xenlight/xenlight.go +0 -2
index 0adb12d1bf..f91c0d2be2 100644
@@ -83,8 +83,6 @@ type Domid uint32
 // Devid is a device ID.
 type Devid int
 
-type MemKB uint64
-
 // Uuid is a domain UUID.
 type Uuid [16]byte
 
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#49 lore raw

PATCH 16/24 golang/xenlight: begin C to Go type marshaling

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Implement basic type conversion in fromC functions such as strings and integer types. Also, remove existing toGo functions from xenlight.go in favor of the new generated functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py       | 120 +++
 tools/golang/xenlight/xenlight.go         | 111 +--
 tools/golang/xenlight/xenlight_helpers.go | 967 ++++++++++++++++++++++
 3 files changed, 1098 insertions(+), 100 deletions(-)
 create mode 100644 tools/golang/xenlight/xenlight_helpers.go
tools/golang/xenlight/gengotypes.py +120 -0
index c8513b79e0..75dcd01724 100644
@@ -18,6 +18,12 @@ builtin_type_names = {
     idl.uint64.typename: 'uint64',
 }
 
+# Some go keywords that conflict with field names in libxl structs.
+go_keywords = ['type', 'func']
+
+go_builtin_types = ['bool', 'string', 'int', 'byte',
+                    'uint16', 'uint32', 'uint64']
+
 # List of strings that need to be written to a file
 # after a struct definition.
 type_extras = []
@@ -168,6 +174,118 @@ def xenlight_golang_define_union(ty = None, structname = ''):
 
     return s
 
+def xenlight_golang_generate_helpers(path = None, types = None, comment = None):
+    """
+    Generate a .go file (xenlight_helpers.go by default)
+    that contains helper functions for marshaling between
+    C and Go types.
+    """
+    if path is None:
+        path = 'xenlight_helpers.go'
+
+    with open(path, 'w') as f:
+        if comment is not None:
+            f.write(comment)
+        f.write('package xenlight\n')
+
+        # Cgo preamble
+        f.write('/*\n')
+        f.write('#cgo LDFLAGS: -lxenlight\n')
+        f.write('#include <stdlib.h>\n')
+        f.write('#include <libxl.h>\n')
+        f.write('\n')
+
+        f.write('*/\nimport "C"\n')
+
+        for ty in types:
+            if not isinstance(ty, idl.Struct):
+                continue
+
+            f.write(xenlight_golang_define_from_C(ty))
+            f.write('\n')
+
+    go_fmt(path)
+
+def xenlight_golang_define_from_C(ty = None, typename = None, nested = False):
+    s = ''
+
+    gotypename = ctypename = ''
+
+    if typename is not None:
+        gotypename = xenlight_golang_fmt_name(typename)
+        ctypename  = typename
+    else:
+        gotypename = xenlight_golang_fmt_name(ty.typename)
+        ctypename  = ty.typename
+
+    if not nested:
+        s += 'func (x *{}) fromC(xc *C.{}) error {{\n'.format(gotypename,ctypename)
+
+    for f in ty.fields:
+        if f.type.typename is not None:
+            if isinstance(f.type, idl.Array):
+                # TODO
+                continue
+
+            gotypename = xenlight_golang_fmt_name(f.type.typename)
+            gofname    = xenlight_golang_fmt_name(f.name)
+            cfname     = f.name
+
+            # In cgo, C names that conflict with Go keywords can be
+            # accessed by prepending an underscore to the name.
+            if cfname in go_keywords:
+                cfname = '_' + cfname
+
+            # If this is nested, we need the outer name too.
+            if nested and typename is not None:
+                goname = xenlight_golang_fmt_name(typename)
+                goname = '{}.{}'.format(goname, gofname)
+                cname  = '{}.{}'.format(typename, cfname)
+
+            else:
+                goname = gofname
+                cname  = cfname
+
+            # Types that satisfy this condition can be easily casted or
+            # converted to a Go builtin type.
+            is_castable = (f.type.json_parse_type == 'JSON_INTEGER' or
+                           isinstance(f.type, idl.Enumeration) or
+                           gotypename in go_builtin_types)
+
+            if is_castable:
+                # Use the cgo helper for converting C strings.
+                if gotypename == 'string':
+                    s += 'x.{} = C.GoString(xc.{})\n'.format(goname, cname)
+                    continue
+
+                s += 'x.{} = {}(xc.{})\n'.format(goname, gotypename, cname)
+
+            else:
+                # If the type is not castable, we need to call its fromC
+                # function.
+                varname = '{}_{}'.format(f.type.typename,f.name)
+                varname = xenlight_golang_fmt_name(varname, exported=False)
+
+                s += 'var {} {}\n'.format(varname, gotypename)
+                s += 'if err := {}.fromC(&xc.{});'.format(varname, cname)
+                s += 'err != nil {\n return err\n}\n'
+                s += 'x.{} = {}\n'.format(goname, varname)
+
+        elif isinstance(f.type, idl.Struct):
+            s += xenlight_golang_define_from_C(f.type, typename=f.name, nested=True)
+
+        elif isinstance(f.type, idl.KeyedUnion):
+            pass
+
+        else:
+            raise Exception('type {} not supported'.format(f.type))
+
+    if not nested:
+        s += 'return nil'
+        s += '}\n'
+
+    return s
+
 def xenlight_golang_fmt_name(name, exported = True):
     """
     Take a given type name and return an
@@ -210,3 +328,5 @@ if __name__ == '__main__':
 
     xenlight_golang_generate_types(types=types,
                                    comment=header_comment)
+    xenlight_golang_generate_helpers(types=types,
+                                     comment=header_comment)
tools/golang/xenlight/xenlight.go +11 -100

176 lines not on this page · raw message

tools/golang/xenlight/xenlight_helpers.go new file +967 -0

967 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#50 lore raw

PATCH 17/24 golang/xenlight: implement keyed union C to Go marshaling

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Switch over union key to determine how to populate 'union' in Go struct.

Since the unions of C types cannot be directly accessed, add C structs in cgo preamble to assist in marshaling keyed unions. This allows the C type defined in the preamble to be populated first, and then accessed directly to populate the Go struct.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py       | 136 ++++++-
 tools/golang/xenlight/xenlight_helpers.go | 440 ++++++++++++++++++++++
 2 files changed, 575 insertions(+), 1 deletion(-)
tools/golang/xenlight/gengotypes.py +135 -1
index 75dcd01724..ececaafd72 100644
@@ -28,6 +28,14 @@ go_builtin_types = ['bool', 'string', 'int', 'byte',
 # after a struct definition.
 type_extras = []
 
+# cgo preamble for xenlight_helpers.go, created during type generation and
+# written later.
+cgo_helpers_preamble = []
+
+# List of strings that need to be written to a file
+# after a helper func definition.
+helper_extras = []
+
 def xenlight_golang_generate_types(path = None, types = None, comment = None):
     """
     Generate a .go file (xenlight_types.go by default)
@@ -159,6 +167,8 @@ def xenlight_golang_define_union(ty = None, structname = ''):
         s = xenlight_golang_define_struct(f.type, typename=name)
         type_extras.append(s)
 
+        xenlight_golang_union_cgo_preamble(f.type, name=name)
+
         # Define function to implement 'union' interface
         name = xenlight_golang_fmt_name(name)
         s = 'func (x {}) is{}(){{}}\n'.format(name, interface_name)
@@ -174,6 +184,18 @@ def xenlight_golang_define_union(ty = None, structname = ''):
 
     return s
 
+def xenlight_golang_union_cgo_preamble(ty = None, name = ''):
+    s = ''
+
+    s += 'typedef struct {} {{\n'.format(name)
+
+    for f in ty.fields:
+        s += '\t{} {};\n'.format(f.type.typename, f.name)
+
+    s += '}} {};\n'.format(name)
+
+    cgo_helpers_preamble.append(s)
+
 def xenlight_golang_generate_helpers(path = None, types = None, comment = None):
     """
     Generate a .go file (xenlight_helpers.go by default)
@@ -187,6 +209,7 @@ def xenlight_golang_generate_helpers(path = None, types = None, comment = None):
         if comment is not None:
             f.write(comment)
         f.write('package xenlight\n')
+        f.write('import (\n"unsafe"\n"errors"\n"fmt"\n)\n')
 
         # Cgo preamble
         f.write('/*\n')
@@ -195,6 +218,10 @@ def xenlight_golang_generate_helpers(path = None, types = None, comment = None):
         f.write('#include <libxl.h>\n')
         f.write('\n')
 
+        for s in cgo_helpers_preamble:
+            f.write(s)
+            f.write('\n')
+
         f.write('*/\nimport "C"\n')
 
         for ty in types:
@@ -204,6 +231,12 @@ def xenlight_golang_generate_helpers(path = None, types = None, comment = None):
             f.write(xenlight_golang_define_from_C(ty))
             f.write('\n')
 
+            for extra in helper_extras:
+                f.write(extra)
+                f.write('\n')
+
+            del helper_extras[:]
+
     go_fmt(path)
 
 def xenlight_golang_define_from_C(ty = None, typename = None, nested = False):
@@ -275,7 +308,7 @@ def xenlight_golang_define_from_C(ty = None, typename = None, nested = False):
             s += xenlight_golang_define_from_C(f.type, typename=f.name, nested=True)
 
         elif isinstance(f.type, idl.KeyedUnion):
-            pass
+            s += xenlight_golang_union_from_C(f.type, f.name, ty.typename)
 
         else:
             raise Exception('type {} not supported'.format(f.type))
@@ -286,6 +319,107 @@ def xenlight_golang_define_from_C(ty = None, typename = None, nested = False):
 
     return s
 
+def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''):
+    keyname   = ty.keyvar.name
+    gokeyname = xenlight_golang_fmt_name(keyname)
+    keytype   = ty.keyvar.type.typename
+    gokeytype = xenlight_golang_fmt_name(keytype)
+
+    interface_name = '{}_{}_union'.format(struct_name, keyname)
+    interface_name = xenlight_golang_fmt_name(interface_name, exported=False)
+
+    cgo_keyname = keyname
+    if cgo_keyname in go_keywords:
+        cgo_keyname = '_' + cgo_keyname
+
+    cases = {}
+
+    for f in ty.fields:
+        val = '{}_{}'.format(keytype, f.name)
+        val = xenlight_golang_fmt_name(val)
+
+        # Add to list of cases to make for the switch
+        # statement below.
+        if f.type is None:
+            continue
+
+        cases[f.name] = val
+
+        # Define fromC func for 'union' struct.
+        typename   = '{}_{}_union_{}'.format(struct_name,keyname,f.name)
+        gotypename = xenlight_golang_fmt_name(typename)
+
+        # Define the function here. The cases for keyed unions are a little
+        # different.
+        s = 'func (x *{}) fromC(xc *C.{}) error {{\n'.format(gotypename,struct_name)
+        s += 'if {}(xc.{}) != {} {{\n'.format(gokeytype,cgo_keyname,val)
+        err_string = '"expected union key {}"'.format(val)
+        s += 'return errors.New({})\n'.format(err_string)
+        s += '}\n\n'
+        s += 'tmp := (*C.{})(unsafe.Pointer(&xc.{}[0]))\n'.format(typename,union_name)
+
+        s += xenlight_golang_union_fields_from_C(f.type)
+        s += 'return nil\n'
+        s += '}\n'
+
+        helper_extras.append(s)
+
+    s = 'x.{} = {}(xc.{})\n'.format(gokeyname,gokeytype,cgo_keyname)
+    s += 'switch x.{}{{\n'.format(gokeyname)
+
+    # Create switch statement to determine which 'union element'
+    # to populate in the Go struct.
+    for case_name, case_val in cases.items():
+        s += 'case {}:\n'.format(case_val)
+
+        gotype = '{}_{}_union_{}'.format(struct_name,keyname,case_name)
+        gotype = xenlight_golang_fmt_name(gotype)
+        goname = '{}_{}'.format(keyname,case_name)
+        goname = xenlight_golang_fmt_name(goname,exported=False)
+
+        s += 'var {} {}\n'.format(goname, gotype)
+        s += 'if err := {}.fromC(xc);'.format(goname)
+        s += 'err != nil {\n return err \n}\n'
+
+        field_name = xenlight_golang_fmt_name('{}_union'.format(keyname))
+        s += 'x.{} = {}\n'.format(field_name, goname)
+
+    # End switch statement
+    s += 'default:\n'
+    err_string = '"invalid union key \'%v\'", x.{}'.format(gokeyname)
+    s += 'return fmt.Errorf({})'.format(err_string)
+    s += '}\n'
+
+    return s
+
+def xenlight_golang_union_fields_from_C(ty = None):
+    s = ''
+
+    for f in ty.fields:
+        gotypename = xenlight_golang_fmt_name(f.type.typename)
+        ctypename  = f.type.typename
+        gofname    = xenlight_golang_fmt_name(f.name)
+        cfname     = f.name
+
+        is_castable = (f.type.json_parse_type == 'JSON_INTEGER' or
+                       isinstance(f.type, idl.Enumeration) or
+                       gotypename in go_builtin_types)
+
+        if not is_castable:
+            s += 'if err := x.{}.fromC(&tmp.{});'.format(gofname,cfname)
+            s += 'err != nil {\n return err \n}\n'
+
+        # We just did an unsafe.Pointer cast from []byte to the 'union' type
+        # struct, so we need to make sure that any string fields are actually
+        # converted properly.
+        elif gotypename == 'string':
+            s += 'x.{} = C.GoString(tmp.{})\n'.format(gofname,cfname)
+
+        else:
+            s += 'x.{} = {}(tmp.{})\n'.format(gofname,gotypename,cfname)
+
+    return s
+
 def xenlight_golang_fmt_name(name, exported = True):
     """
     Take a given type name and return an
tools/golang/xenlight/xenlight_helpers.go +440 -0

492 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#51 lore raw

PATCH 18/24 golang/xenlight: implement array C to Go marshaling

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py       |  39 ++-
 tools/golang/xenlight/xenlight_helpers.go | 300 ++++++++++++++++++++++
 2 files changed, 338 insertions(+), 1 deletion(-)
tools/golang/xenlight/gengotypes.py +38 -1
index ececaafd72..2b620f0ae9 100644
@@ -257,7 +257,7 @@ def xenlight_golang_define_from_C(ty = None, typename = None, nested = False):
     for f in ty.fields:
         if f.type.typename is not None:
             if isinstance(f.type, idl.Array):
-                # TODO
+                s += xenlight_golang_array_from_C(f)
                 continue
 
             gotypename = xenlight_golang_fmt_name(f.type.typename)
@@ -420,6 +420,43 @@ def xenlight_golang_union_fields_from_C(ty = None):
 
     return s
 
+def xenlight_golang_array_from_C(ty = None):
+    """
+    Convert C array to Go slice using the method
+    described here:
+
+    https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
+    """
+    s = ''
+
+    gotypename = xenlight_golang_fmt_name(ty.type.elem_type.typename)
+    goname     = xenlight_golang_fmt_name(ty.name)
+    ctypename  = ty.type.elem_type.typename
+    cname      = ty.name
+    cslice     = 'c{}'.format(goname)
+    clenvar    = ty.type.lenvar.name
+    golenvar   = xenlight_golang_fmt_name(clenvar,exported=False)
+
+    s += '{} := int(xc.{})\n'.format(golenvar, clenvar)
+    s += '{} := '.format(cslice)
+    s +='(*[1<<28]C.{})(unsafe.Pointer(xc.{}))[:{}:{}]\n'.format(ctypename, cname,
+                                                                golenvar, golenvar)
+    s += 'x.{} = make([]{}, {})\n'.format(goname, gotypename, golenvar)
+    s += 'for i, v := range {} {{\n'.format(cslice)
+
+    is_enum = isinstance(ty.type.elem_type,idl.Enumeration)
+    if gotypename in go_builtin_types or is_enum:
+        s += 'x.{}[i] = {}(v)\n'.format(goname, gotypename)
+    else:
+        s += 'var e {}\n'.format(gotypename)
+        s += 'if err := e.fromC(&v); err != nil {\n'
+        s += 'return err }\n'
+        s += 'x.{}[i] = e\n'.format(goname)
+
+    s += '}\n'
+
+    return s
+
 def xenlight_golang_fmt_name(name, exported = True):
     """
     Take a given type name and return an
tools/golang/xenlight/xenlight_helpers.go +300 -0

382 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#52 lore raw

PATCH 19/24 golang/xenlight: begin Go to C type marshaling

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Implement conversion of basic type conversions such as strings and integer types in toC functions.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py       |   80 ++
 tools/golang/xenlight/xenlight_helpers.go | 1013 +++++++++++++++++++++
 2 files changed, 1093 insertions(+)
tools/golang/xenlight/gengotypes.py +80 -0
index 2b620f0ae9..eccc334b41 100644
@@ -237,6 +237,9 @@ def xenlight_golang_generate_helpers(path = None, types = None, comment = None):
 
             del helper_extras[:]
 
+            f.write(xenlight_golang_define_to_C(ty))
+            f.write('\n')
+
     go_fmt(path)
 
 def xenlight_golang_define_from_C(ty = None, typename = None, nested = False):
@@ -457,6 +460,83 @@ def xenlight_golang_array_from_C(ty = None):
 
     return s
 
+def xenlight_golang_define_to_C(ty = None, typename = None, nested = False):
+    s = ''
+
+    gotypename = ctypename = ''
+
+    if typename is not None:
+        gotypename = xenlight_golang_fmt_name(typename)
+        ctypename  = typename
+    else:
+        gotypename = xenlight_golang_fmt_name(ty.typename)
+        ctypename  = ty.typename
+
+    if not nested:
+        s += 'func (x *{}) toC() (xc C.{},err error) {{\n'.format(gotypename,ctypename)
+        s += 'C.{}(&xc)\n'.format(ty.init_fn)
+
+    for f in ty.fields:
+        if f.type.typename is not None:
+            if isinstance(f.type, idl.Array):
+                # TODO
+                continue
+
+            gotypename = xenlight_golang_fmt_name(f.type.typename)
+            ctypename  = f.type.typename
+            gofname    = xenlight_golang_fmt_name(f.name)
+            cfname     = f.name
+
+            # In cgo, C names that conflict with Go keywords can be
+            # accessed by prepending an underscore to the name.
+            if cfname in go_keywords:
+                cfname = '_' + cfname
+
+            # If this is nested, we need the outer name too.
+            if nested and typename is not None:
+                goname = xenlight_golang_fmt_name(typename)
+                goname = '{}.{}'.format(goname, gofname)
+                cname  = '{}.{}'.format(typename, cfname)
+
+            else:
+                goname = gofname
+                cname  = cfname
+
+            is_castable = (f.type.json_parse_type == 'JSON_INTEGER' or
+                           isinstance(f.type, idl.Enumeration) or
+                           gotypename in go_builtin_types)
+
+            if is_castable:
+                # Use the cgo helper for converting C strings.
+                if gotypename == 'string':
+                    s += 'xc.{} = C.CString(x.{})\n'.format(cname,goname)
+                    continue
+
+                s += 'xc.{} = C.{}(x.{})\n'.format(cname,ctypename,goname)
+
+            else:
+                s += 'xc.{}, err = x.{}.toC()\n'.format(cname,goname)
+                s += 'if err != nil {\n'
+                s += 'C.{}(&xc)\n'.format(ty.dispose_fn)
+                s += 'return xc, err\n'
+                s += '}\n'
+
+        elif isinstance(f.type, idl.Struct):
+            s += xenlight_golang_define_to_C(f.type, typename=f.name, nested=True)
+
+        elif isinstance(f.type, idl.KeyedUnion):
+            # TODO
+            pass
+
+        else:
+            raise Exception('type {} not supported'.format(f.type))
+
+    if not nested:
+        s += 'return xc, nil'
+        s += '}\n'
+
+    return s
+
 def xenlight_golang_fmt_name(name, exported = True):
     """
     Take a given type name and return an
tools/golang/xenlight/xenlight_helpers.go +1013 -0

1,367 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#53 lore raw

PATCH 20/24 golang/xenlight: implement keyed union Go to C marshaling

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Since the C union cannot be directly populated, populate the fields of the corresponding C struct defined in the cgo preamble, and then copy that struct as bytes into the byte slice that Go uses as the union.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py       |  77 ++++-
 tools/golang/xenlight/xenlight_helpers.go | 325 ++++++++++++++++++++++
 2 files changed, 400 insertions(+), 2 deletions(-)
tools/golang/xenlight/gengotypes.py +75 -2
index eccc334b41..35d9dfea40 100644
@@ -525,8 +525,7 @@ def xenlight_golang_define_to_C(ty = None, typename = None, nested = False):
             s += xenlight_golang_define_to_C(f.type, typename=f.name, nested=True)
 
         elif isinstance(f.type, idl.KeyedUnion):
-            # TODO
-            pass
+            s += xenlight_golang_union_to_C(f.type, f.name, ty.typename, ty.dispose_fn)
 
         else:
             raise Exception('type {} not supported'.format(f.type))
@@ -537,6 +536,80 @@ def xenlight_golang_define_to_C(ty = None, typename = None, nested = False):
 
     return s
 
+def xenlight_golang_union_to_C(ty = None, union_name = '',
+                               struct_name = '', dispose_fn = ''):
+    keyname   = ty.keyvar.name
+    gokeyname = xenlight_golang_fmt_name(keyname)
+    keytype   = ty.keyvar.type.typename
+    gokeytype = xenlight_golang_fmt_name(keytype)
+
+    interface_name = '{}_{}_union'.format(struct_name, keyname)
+    interface_name = xenlight_golang_fmt_name(interface_name, exported=False)
+
+    cgo_keyname = keyname
+    if cgo_keyname in go_keywords:
+        cgo_keyname = '_' + cgo_keyname
+
+
+    s = 'xc.{} = C.{}(x.{})\n'.format(cgo_keyname,keytype,gokeyname)
+    s += 'switch x.{}{{\n'.format(gokeyname)
+
+    # Create switch statement to determine how to populate the C union.
+    for f in ty.fields:
+        key_val = '{}_{}'.format(keytype, f.name)
+        key_val = xenlight_golang_fmt_name(key_val)
+        if f.type is None:
+            continue
+
+        s += 'case {}:\n'.format(key_val)
+        cgotype = '{}_{}_union_{}'.format(struct_name,keyname,f.name)
+        gotype  = xenlight_golang_fmt_name(cgotype)
+        goname  = '{}_{}'.format(keyname,f.name)
+        goname  = xenlight_golang_fmt_name(goname,exported=False)
+
+        field_name = xenlight_golang_fmt_name('{}_union'.format(keyname))
+        s += 'tmp, ok := x.{}.({})\n'.format(field_name,gotype)
+        s += 'if !ok {\n'
+        s += 'C.{}(&xc)\n'.format(dispose_fn)
+        s += 'return xc,errors.New("wrong type for union key {}")\n'.format(keyname)
+        s += '}\n'
+
+        s += 'var {} C.{}\n'.format(f.name,cgotype)
+        for uf in f.type.fields:
+            gotypename = xenlight_golang_fmt_name(uf.type.typename)
+            ctypename  = uf.type.typename
+            gofname    = xenlight_golang_fmt_name(uf.name)
+
+            is_castable = (uf.type.json_parse_type == 'JSON_INTEGER' or
+                           isinstance(uf.type, idl.Enumeration) or
+                           gotypename in go_builtin_types)
+
+            if not is_castable:
+                s += '{}.{}, err = tmp.{}.toC()\n'.format(f.name,uf.name,gofname)
+                s += 'if err != nil {\n'
+                s += 'C.{}(&xc)\n'.format(dispose_fn)
+                s += 'return xc,err \n}\n'
+
+            elif gotypename == 'string':
+                s += '{}.{} = C.CString(tmp.{})\n'.format(f.name,uf.name,gofname)
+
+            else:
+                s += '{}.{} = C.{}(tmp.{})\n'.format(f.name,uf.name,ctypename,gofname)
+
+        # The union is still represented as Go []byte.
+        s += '{}Bytes := C.GoBytes(unsafe.Pointer(&{}),C.sizeof_{})\n'.format(f.name,
+                                                                              f.name,
+                                                                              cgotype)
+        s += 'copy(xc.{}[:],{}Bytes)\n'.format(union_name,f.name)
+
+    # End switch statement
+    s += 'default:\n'
+    err_string = '"invalid union key \'%v\'", x.{}'.format(gokeyname)
+    s += 'return xc, fmt.Errorf({})'.format(err_string)
+    s += '}\n'
+
+    return s
+
 def xenlight_golang_fmt_name(name, exported = True):
     """
     Take a given type name and return an
tools/golang/xenlight/xenlight_helpers.go +325 -0

360 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#54 lore raw

PATCH 21/24 golang/xenlight: implement array Go to C marshaling

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/gengotypes.py       |  44 ++-
 tools/golang/xenlight/xenlight_helpers.go | 359 ++++++++++++++++++++++
 2 files changed, 402 insertions(+), 1 deletion(-)
tools/golang/xenlight/gengotypes.py +43 -1
index 35d9dfea40..d8a6120e5c 100644
@@ -479,7 +479,7 @@ def xenlight_golang_define_to_C(ty = None, typename = None, nested = False):
     for f in ty.fields:
         if f.type.typename is not None:
             if isinstance(f.type, idl.Array):
-                # TODO
+                s += xenlight_golang_array_to_C(f, ty.dispose_fn)
                 continue
 
             gotypename = xenlight_golang_fmt_name(f.type.typename)
@@ -610,6 +610,48 @@ def xenlight_golang_union_to_C(ty = None, union_name = '',
 
     return s
 
+def xenlight_golang_array_to_C(ty = None, dispose_fn = ''):
+    s = ''
+
+    gotypename = xenlight_golang_fmt_name(ty.type.elem_type.typename)
+    goname     = xenlight_golang_fmt_name(ty.name)
+    ctypename  = ty.type.elem_type.typename
+    cname      = ty.name
+    clenvar    = ty.type.lenvar.name
+    golenvar   = xenlight_golang_fmt_name(clenvar,exported=False)
+
+    is_enum = isinstance(ty.type.elem_type,idl.Enumeration)
+    if gotypename in go_builtin_types or is_enum:
+        s += '{} := len(x.{})\n'.format(golenvar,goname)
+        s += 'xc.{} = (*C.{})(C.malloc(C.size_t({}*{})))\n'.format(cname,ctypename,
+                                                                   golenvar,golenvar)
+        s += 'xc.{} = C.int({})\n'.format(clenvar,golenvar)
+        s += 'c{} := (*[1<<28]C.{})(unsafe.Pointer(xc.{}))[:{}:{}]\n'.format(goname,
+                                                                      ctypename,cname,
+                                                                      golenvar,golenvar)
+        s += 'for i,v := range x.{} {{\n'.format(goname)
+        s += 'c{}[i] = C.{}(v)\n'.format(goname,ctypename)
+        s += '}\n'
+
+        return s
+
+    s += '{} := len(x.{})\n'.format(golenvar,goname)
+    s += 'xc.{} = (*C.{})(C.malloc(C.ulong({})*C.sizeof_{}))\n'.format(cname,ctypename,
+                                                                   golenvar,ctypename)
+    s += 'xc.{} = C.int({})\n'.format(clenvar,golenvar)
+    s += 'c{} := (*[1<<28]C.{})(unsafe.Pointer(xc.{}))[:{}:{}]\n'.format(goname,
+                                                                         ctypename,cname,
+                                                                         golenvar,golenvar)
+    s += 'for i,v := range x.{} {{\n'.format(goname)
+    s += 'tmp, err := v.toC()\n'
+    s += 'if err != nil {\n'
+    s += 'C.{}(&xc)\n'.format(dispose_fn)
+    s += 'return xc,err\n}\n'
+    s += 'c{}[i] = tmp\n'.format(goname)
+    s += '}\n'
+
+    return s
+
 def xenlight_golang_fmt_name(name, exported = True):
     """
     Take a given type name and return an
tools/golang/xenlight/xenlight_helpers.go +359 -0

443 lines not on this page · raw message

Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#55 lore raw

PATCH 22/24 golang/xenlight: revise use of Context type

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Remove the exported global context variable, 'Ctx.' Generally, it is better to not export global variables for use through a Go package. However, there are some exceptions that can be found in the standard library.

Add a NewContext function instead, and remove the Open, IsOpen, and CheckOpen functions as a result.

Also, comment-out an ineffectual assignment to 'err' inside the function Context.CpupoolInfo so that compilation does not fail.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 219 +++++-------------------------
 1 file changed, 34 insertions(+), 185 deletions(-)
tools/golang/xenlight/xenlight.go +34 -185
index a8f933c75f..e540b5413d 100644
@@ -74,6 +74,39 @@ func (e Error) Error() string {
 	return fmt.Sprintf("libxl error: %d", -e)
 }
 
+// Context represents a libxl_ctx.
+type Context struct {
+	ctx    *C.libxl_ctx
+	logger *C.xentoollog_logger_stdiostream
+}
+
+// NewContext returns a new Context.
+func NewContext() (*Context, error) {
+	var ctx Context
+
+	ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0)
+
+	ret := C.libxl_ctx_alloc(&ctx.ctx, C.LIBXL_VERSION, 0, (*C.xentoollog_logger)(unsafe.Pointer(ctx.logger)))
+	if ret != 0 {
+		return nil, Error(ret)
+	}
+
+	return &ctx, nil
+}
+
+// Close closes the Context.
+func (ctx *Context) Close() error {
+	ret := C.libxl_ctx_free(ctx.ctx)
+	ctx.ctx = nil
+	C.xtl_logger_destroy((*C.xentoollog_logger)(unsafe.Pointer(ctx.logger)))
+
+	if ret != 0 {
+		return Error(ret)
+	}
+
+	return nil
+}
+
 /*
  * Types: Builtins
  */
@@ -299,11 +332,6 @@ func (cpl *CpuidPolicyList) toC() (C.libxl_cpuid_policy_list, error) {
 	return *ccpl, nil
 }
 
-type Context struct {
-	ctx    *C.libxl_ctx
-	logger *C.xentoollog_logger_stdiostream
-}
-
 // Hwcap represents a libxl_hwcap.
 type Hwcap [8]uint32
 
@@ -480,11 +508,6 @@ func SchedulerFromString(name string) (s Scheduler, err error) {
 // libxl_cpupoolinfo * libxl_list_cpupool(libxl_ctx*, int *nb_pool_out);
 // void libxl_cpupoolinfo_list_free(libxl_cpupoolinfo *list, int nb_pool);
 func (Ctx *Context) ListCpupool() (list []Cpupoolinfo) {
-	err := Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var nbPool C.int
 
 	c_cpupool_list := C.libxl_list_cpupool(Ctx.ctx, &nbPool)
@@ -508,16 +531,11 @@ func (Ctx *Context) ListCpupool() (list []Cpupoolinfo) {
 
 // int libxl_cpupool_info(libxl_ctx *ctx, libxl_cpupoolinfo *info, uint32_t poolid);
 func (Ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo) {
-	err := Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var c_cpupool C.libxl_cpupoolinfo
 
 	ret := C.libxl_cpupool_info(Ctx.ctx, &c_cpupool, C.uint32_t(Poolid))
 	if ret != 0 {
-		err = Error(-ret)
+		//err = Error(-ret)
 		return
 	}
 	defer C.libxl_cpupoolinfo_dispose(&c_cpupool)
@@ -534,11 +552,6 @@ func (Ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo) {
 // FIXME: uuid
 // FIXME: Setting poolid
 func (Ctx *Context) CpupoolCreate(Name string, Scheduler Scheduler, Cpumap Bitmap) (err error, Poolid uint32) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	poolid := C.uint32_t(C.LIBXL_CPUPOOL_POOLID_ANY)
 	name := C.CString(Name)
 	defer C.free(unsafe.Pointer(name))
@@ -567,11 +580,6 @@ func (Ctx *Context) CpupoolCreate(Name string, Scheduler Scheduler, Cpumap Bitma
 
 // int libxl_cpupool_destroy(libxl_ctx *ctx, uint32_t poolid);
 func (Ctx *Context) CpupoolDestroy(Poolid uint32) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_cpupool_destroy(Ctx.ctx, C.uint32_t(Poolid))
 	if ret != 0 {
 		err = Error(-ret)
@@ -583,11 +591,6 @@ func (Ctx *Context) CpupoolDestroy(Poolid uint32) (err error) {
 
 // int libxl_cpupool_cpuadd(libxl_ctx *ctx, uint32_t poolid, int cpu);
 func (Ctx *Context) CpupoolCpuadd(Poolid uint32, Cpu int) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_cpupool_cpuadd(Ctx.ctx, C.uint32_t(Poolid), C.int(Cpu))
 	if ret != 0 {
 		err = Error(-ret)
@@ -600,11 +603,6 @@ func (Ctx *Context) CpupoolCpuadd(Poolid uint32, Cpu int) (err error) {
 // int libxl_cpupool_cpuadd_cpumap(libxl_ctx *ctx, uint32_t poolid,
 //                                 const libxl_bitmap *cpumap);
 func (Ctx *Context) CpupoolCpuaddCpumap(Poolid uint32, Cpumap Bitmap) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	cbm, err := Cpumap.toC()
 	if err != nil {
 		return
@@ -622,11 +620,6 @@ func (Ctx *Context) CpupoolCpuaddCpumap(Poolid uint32, Cpumap Bitmap) (err error
 
 // int libxl_cpupool_cpuremove(libxl_ctx *ctx, uint32_t poolid, int cpu);
 func (Ctx *Context) CpupoolCpuremove(Poolid uint32, Cpu int) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_cpupool_cpuremove(Ctx.ctx, C.uint32_t(Poolid), C.int(Cpu))
 	if ret != 0 {
 		err = Error(-ret)
@@ -639,11 +632,6 @@ func (Ctx *Context) CpupoolCpuremove(Poolid uint32, Cpu int) (err error) {
 // int libxl_cpupool_cpuremove_cpumap(libxl_ctx *ctx, uint32_t poolid,
 //                                    const libxl_bitmap *cpumap);
 func (Ctx *Context) CpupoolCpuremoveCpumap(Poolid uint32, Cpumap Bitmap) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	cbm, err := Cpumap.toC()
 	if err != nil {
 		return
@@ -661,11 +649,6 @@ func (Ctx *Context) CpupoolCpuremoveCpumap(Poolid uint32, Cpumap Bitmap) (err er
 
 // int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid);
 func (Ctx *Context) CpupoolRename(Name string, Poolid uint32) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	name := C.CString(Name)
 	defer C.free(unsafe.Pointer(name))
 
@@ -680,11 +663,6 @@ func (Ctx *Context) CpupoolRename(Name string, Poolid uint32) (err error) {
 
 // int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus);
 func (Ctx *Context) CpupoolCpuaddNode(Poolid uint32, Node int) (Cpus int, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ccpus := C.int(0)
 
 	ret := C.libxl_cpupool_cpuadd_node(Ctx.ctx, C.uint32_t(Poolid), C.int(Node), &ccpus)
@@ -700,11 +678,6 @@ func (Ctx *Context) CpupoolCpuaddNode(Poolid uint32, Node int) (Cpus int, err er
 
 // int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus);
 func (Ctx *Context) CpupoolCpuremoveNode(Poolid uint32, Node int) (Cpus int, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ccpus := C.int(0)
 
 	ret := C.libxl_cpupool_cpuremove_node(Ctx.ctx, C.uint32_t(Poolid), C.int(Node), &ccpus)
@@ -720,11 +693,6 @@ func (Ctx *Context) CpupoolCpuremoveNode(Poolid uint32, Node int) (Cpus int, err
 
 // int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid);
 func (Ctx *Context) CpupoolMovedomain(Poolid uint32, Id Domid) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_cpupool_movedomain(Ctx.ctx, C.uint32_t(Poolid), C.uint32_t(Id))
 	if ret != 0 {
 		err = Error(-ret)
@@ -884,60 +852,8 @@ func (bm Bitmap) String() (s string) {
 	return
 }
 
-/*
- * Context
- */
-var Ctx Context
-
-func (Ctx *Context) IsOpen() bool {
-	return Ctx.ctx != nil
-}
-
-func (Ctx *Context) Open() (err error) {
-	if Ctx.ctx != nil {
-		return
-	}
-
-	Ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0)
-	if Ctx.logger == nil {
-		err = fmt.Errorf("Cannot open stdiostream")
-		return
-	}
-
-	ret := C.libxl_ctx_alloc(&Ctx.ctx, C.LIBXL_VERSION,
-		0, (*C.xentoollog_logger)(unsafe.Pointer(Ctx.logger)))
-
-	if ret != 0 {
-		err = Error(-ret)
-	}
-	return
-}
-
-func (Ctx *Context) Close() (err error) {
-	ret := C.libxl_ctx_free(Ctx.ctx)
-	Ctx.ctx = nil
-
-	if ret != 0 {
-		err = Error(-ret)
-	}
-	C.xtl_logger_destroy((*C.xentoollog_logger)(unsafe.Pointer(Ctx.logger)))
-	return
-}
-
-func (Ctx *Context) CheckOpen() (err error) {
-	if Ctx.ctx == nil {
-		err = fmt.Errorf("Context not opened")
-	}
-	return
-}
-
 //int libxl_get_max_cpus(libxl_ctx *ctx);
 func (Ctx *Context) GetMaxCpus() (maxCpus int, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_get_max_cpus(Ctx.ctx)
 	if ret < 0 {
 		err = Error(-ret)
@@ -949,11 +865,6 @@ func (Ctx *Context) GetMaxCpus() (maxCpus int, err error) {
 
 //int libxl_get_online_cpus(libxl_ctx *ctx);
 func (Ctx *Context) GetOnlineCpus() (onCpus int, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_get_online_cpus(Ctx.ctx)
 	if ret < 0 {
 		err = Error(-ret)
@@ -965,10 +876,6 @@ func (Ctx *Context) GetOnlineCpus() (onCpus int, err error) {
 
 //int libxl_get_max_nodes(libxl_ctx *ctx);
 func (Ctx *Context) GetMaxNodes() (maxNodes int, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
 	ret := C.libxl_get_max_nodes(Ctx.ctx)
 	if ret < 0 {
 		err = Error(-ret)
@@ -980,10 +887,6 @@ func (Ctx *Context) GetMaxNodes() (maxNodes int, err error) {
 
 //int libxl_get_free_memory(libxl_ctx *ctx, uint64_t *memkb);
 func (Ctx *Context) GetFreeMemory() (memkb uint64, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
 	var cmem C.uint64_t
 	ret := C.libxl_get_free_memory(Ctx.ctx, &cmem)
 
@@ -999,10 +902,6 @@ func (Ctx *Context) GetFreeMemory() (memkb uint64, err error) {
 
 //int libxl_get_physinfo(libxl_ctx *ctx, libxl_physinfo *physinfo)
 func (Ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
 	var cphys C.libxl_physinfo
 	C.libxl_physinfo_init(&cphys)
 	defer C.libxl_physinfo_dispose(&cphys)
@@ -1020,11 +919,6 @@ func (Ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) {
 
 //const libxl_version_info* libxl_get_version_info(libxl_ctx *ctx);
 func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var cinfo *C.libxl_version_info
 
 	cinfo = C.libxl_get_version_info(Ctx.ctx)
@@ -1035,11 +929,6 @@ func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) {
 }
 
 func (Ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var cdi C.libxl_dominfo
 	C.libxl_dominfo_init(&cdi)
 	defer C.libxl_dominfo_dispose(&cdi)
@@ -1057,11 +946,6 @@ func (Ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) {
 }
 
 func (Ctx *Context) DomainUnpause(Id Domid) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_domain_unpause(Ctx.ctx, C.uint32_t(Id), nil)
 
 	if ret != 0 {
@@ -1072,11 +956,6 @@ func (Ctx *Context) DomainUnpause(Id Domid) (err error) {
 
 //int libxl_domain_pause(libxl_ctx *ctx, uint32_t domain);
 func (Ctx *Context) DomainPause(id Domid) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_domain_pause(Ctx.ctx, C.uint32_t(id), nil)
 
 	if ret != 0 {
@@ -1087,11 +966,6 @@ func (Ctx *Context) DomainPause(id Domid) (err error) {
 
 //int libxl_domain_shutdown(libxl_ctx *ctx, uint32_t domid);
 func (Ctx *Context) DomainShutdown(id Domid) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_domain_shutdown(Ctx.ctx, C.uint32_t(id))
 
 	if ret != 0 {
@@ -1102,11 +976,6 @@ func (Ctx *Context) DomainShutdown(id Domid) (err error) {
 
 //int libxl_domain_reboot(libxl_ctx *ctx, uint32_t domid);
 func (Ctx *Context) DomainReboot(id Domid) (err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	ret := C.libxl_domain_reboot(Ctx.ctx, C.uint32_t(id))
 
 	if ret != 0 {
@@ -1118,11 +987,6 @@ func (Ctx *Context) DomainReboot(id Domid) (err error) {
 //libxl_dominfo * libxl_list_domain(libxl_ctx*, int *nb_domain_out);
 //void libxl_dominfo_list_free(libxl_dominfo *list, int nb_domain);
 func (Ctx *Context) ListDomain() (glist []Dominfo) {
-	err := Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var nbDomain C.int
 	clist := C.libxl_list_domain(Ctx.ctx, &nbDomain)
 	defer C.libxl_dominfo_list_free(clist, nbDomain)
@@ -1145,11 +1009,6 @@ func (Ctx *Context) ListDomain() (glist []Dominfo) {
 //				int *nb_vcpu, int *nr_cpus_out);
 //void libxl_vcpuinfo_list_free(libxl_vcpuinfo *, int nr_vcpus);
 func (Ctx *Context) ListVcpu(id Domid) (glist []Vcpuinfo) {
-	err := Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var nbVcpu C.int
 	var nrCpu C.int
 
@@ -1180,11 +1039,6 @@ func (ct ConsoleType) String() (str string) {
 //int libxl_console_get_tty(libxl_ctx *ctx, uint32_t domid, int cons_num,
 //libxl_console_type type, char **path);
 func (Ctx *Context) ConsoleGetTty(id Domid, consNum int, conType ConsoleType) (path string, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var cpath *C.char
 	ret := C.libxl_console_get_tty(Ctx.ctx, C.uint32_t(id), C.int(consNum), C.libxl_console_type(conType), &cpath)
 	if ret != 0 {
@@ -1200,11 +1054,6 @@ func (Ctx *Context) ConsoleGetTty(id Domid, consNum int, conType ConsoleType) (p
 //int libxl_primary_console_get_tty(libxl_ctx *ctx, uint32_t domid_vm,
 //					char **path);
 func (Ctx *Context) PrimaryConsoleGetTty(domid uint32) (path string, err error) {
-	err = Ctx.CheckOpen()
-	if err != nil {
-		return
-	}
-
 	var cpath *C.char
 	ret := C.libxl_primary_console_get_tty(Ctx.ctx, C.uint32_t(domid), &cpath)
 	if ret != 0 {
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#56 lore raw

PATCH 23/24 golang/xenlight: add error return type to Context.Cpupoolinfo

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

A previous commit that removed Context.CheckOpen revealed an ineffectual assignent to err in Context.Cpupoolinfo, as there is no error return type.

Since it appears that the intent is to return an error here, add an error return value to the function signature.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/xenlight.go | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/golang/xenlight/xenlight.go +3 -3
index e540b5413d..0a4f476451 100644
@@ -530,17 +530,17 @@ func (Ctx *Context) ListCpupool() (list []Cpupoolinfo) {
 }
 
 // int libxl_cpupool_info(libxl_ctx *ctx, libxl_cpupoolinfo *info, uint32_t poolid);
-func (Ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo) {
+func (Ctx *Context) CpupoolInfo(Poolid uint32) (pool Cpupoolinfo, err error) {
 	var c_cpupool C.libxl_cpupoolinfo
 
 	ret := C.libxl_cpupool_info(Ctx.ctx, &c_cpupool, C.uint32_t(Poolid))
 	if ret != 0 {
-		//err = Error(-ret)
+		err = Error(-ret)
 		return
 	}
 	defer C.libxl_cpupoolinfo_dispose(&c_cpupool)
 
-	_ = pool.fromC(&c_cpupool)
+	err = pool.fromC(&c_cpupool)
 
 	return
 }
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#57 lore raw

PATCH 24/24 golang/xenlight: add make target for generated files

In reply to Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Remove the PKGSOURCES variable since adding xenlight_types.go and xenlight_helpers.go to this list breaks the rest of the Makefile.

Add xenlight_%.go target for generated files, and use full file names within install, uninstall and $(XEN_GOPATH)$(GOXL_PKG_DIR) rule.

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/Makefile | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
tools/golang/xenlight/Makefile +14 -8
index 0987305224..821a5d48fa 100644
@@ -7,20 +7,22 @@ GOCODE_DIR ?= $(prefix)/share/gocode/
 GOXL_PKG_DIR = /src/$(XEN_GOCODE_URL)/xenlight/
 GOXL_INSTALL_DIR = $(GOCODE_DIR)$(GOXL_PKG_DIR)
 
-# PKGSOURCES: Files which comprise the distributed source package
-PKGSOURCES = xenlight.go
-
 GO ?= go
 
 .PHONY: all
 all: build
 
 .PHONY: package
-package: $(XEN_GOPATH)$(GOXL_PKG_DIR)$(PKGSOURCES)
+package: $(XEN_GOPATH)$(GOXL_PKG_DIR)
 
-$(XEN_GOPATH)/src/$(XEN_GOCODE_URL)/xenlight/$(PKGSOURCES): $(PKGSOURCES)
+$(XEN_GOPATH)/src/$(XEN_GOCODE_URL)/xenlight/: xenlight_%.go
 	$(INSTALL_DIR) $(XEN_GOPATH)$(GOXL_PKG_DIR)
-	$(INSTALL_DATA) $(PKGSOURCES) $(XEN_GOPATH)$(GOXL_PKG_DIR)
+	$(INSTALL_DATA) xenlight.go $(XEN_GOPATH)$(GOXL_PKG_DIR)
+	$(INSTALL_DATA) xenlight_types.go $(XEN_GOPATH)$(GOXL_PKG_DIR)
+	$(INSTALL_DATA) xenlight_helpers.go $(XEN_GOPATH)$(GOXL_PKG_DIR)
+
+xenlight_%.go: gengotypes.py $(XEN_ROOT)/tools/libxl/libxl_types.idl $(XEN_ROOT)/tools/libxl/idl.py
+	XEN_ROOT=$(XEN_ROOT) $(PYTHON) gengotypes.py ../../libxl/libxl_types.idl
 
 # Go will do its own dependency checking, and not actuall go through
 # with the build if none of the input files have changed.
@@ -36,10 +38,14 @@ build: package
 .PHONY: install
 install: build
 	$(INSTALL_DIR) $(DESTDIR)$(GOXL_INSTALL_DIR)
-	$(INSTALL_DATA) $(XEN_GOPATH)$(GOXL_PKG_DIR)$(PKGSOURCES) $(DESTDIR)$(GOXL_INSTALL_DIR)
+	$(install_data) $(xen_gopath)$(goxl_pkg_dir)xenlight.go $(destdir)$(goxl_install_dir)
+	$(install_data) $(xen_gopath)$(goxl_pkg_dir)xenlight_types.go $(destdir)$(goxl_install_dir)
+	$(install_data) $(xen_gopath)$(goxl_pkg_dir)xenlight_helpers.go $(destdir)$(goxl_install_dir)
 
 .PHONY: uninstall
-	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, $(PKGSOURCES))
+	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, xenlight.go)
+	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, xenlight_types.go)
+	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, xenlight_helpers.go)
 
 .PHONY: clean
 clean:
Signature · 7 lines
2.19.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#58 lore raw

Re: [Xen-devel] [PATCH 24/24] golang/xenlight: add make target for generated files

Quoted 11 lines · Nick Rosbrook

From: Nick Rosbrook <[email protected]>

Remove the PKGSOURCES variable since adding xenlight_types.go and xenlight_helpers.go to this list breaks the rest of the Makefile.

Add xenlight_%.go target for generated files, and use full file names within install, uninstall and $(XEN_GOPATH)$(GOXL_PKG_DIR) rule.

Hey Nick! Thanks for breaking down the series this way -- this is much easier to review.

One standard practice when making a series is to try to avoid any regressions, including build regressions, in the middle of the series. This is particularly helpful to aid in bisections, but in this case it makes it easier to observe the action of the gengotypes.py script (and how it's meant to be called).

So I would basically make this part of patch 2, except remove references to xenlight_helpers.go until the patch where that file is generated.

One other comment...

Quoted 25 lines

---
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>

 tools/golang/xenlight/Makefile | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
tools/golang/xenlight/Makefile +0 -3
index 0987305224..821a5d48fa 100644
@@ -7,20 +7,22 @@ GOCODE_DIR ?= $(prefix)/share/gocode/
 GOXL_PKG_DIR = /src/$(XEN_GOCODE_URL)/xenlight/
 GOXL_INSTALL_DIR = $(GOCODE_DIR)$(GOXL_PKG_DIR)
 
-# PKGSOURCES: Files which comprise the distributed source package
-PKGSOURCES = xenlight.go
-
 GO ?= go
 
 .PHONY: all
 all: build
 
 .PHONY: package

tools/golang/xenlight/Makefile GOCODE_DIR ?= $(prefix)/share/gocode/ · line 22

Quoted 10 lines
tools/golang/xenlight/Makefile +5 -3
-package: $(XEN_GOPATH)$(GOXL_PKG_DIR)$(PKGSOURCES)
+package: $(XEN_GOPATH)$(GOXL_PKG_DIR)
 
-$(XEN_GOPATH)/src/$(XEN_GOCODE_URL)/xenlight/$(PKGSOURCES): $(PKGSOURCES)
+$(XEN_GOPATH)/src/$(XEN_GOCODE_URL)/xenlight/: xenlight_%.go
 	$(INSTALL_DIR) $(XEN_GOPATH)$(GOXL_PKG_DIR)
-	$(INSTALL_DATA) $(PKGSOURCES) $(XEN_GOPATH)$(GOXL_PKG_DIR)
+	$(INSTALL_DATA) xenlight.go $(XEN_GOPATH)$(GOXL_PKG_DIR)
+	$(INSTALL_DATA) xenlight_types.go $(XEN_GOPATH)$(GOXL_PKG_DIR)
+	$(INSTALL_DATA) xenlight_helpers.go $(XEN_GOPATH)$(GOXL_PKG_DIR)

It might be nice to have a naming convention for the generated files that clues people in to the fact that they're generated (other than the comment at the top of course). In libxl, this is done by giving them a leading underscore (e.g., _libxl_type.h); but the go compiler will helpfully ignore such files. :-)

The go compiler will also do special things sometimes with things after a _; e.g., "${foo}_test.go" will only be compiled for go test, "${foo}_linux.go" will only be compiled on Linux, and so on. I'm pretty sure these names will be safe, but it might be slightly more future-proof to avoid using an underscore in the names.

What about something like "gentypes.go" or "idltypes.go"?

Just a suggestion.

Quoted 10 lines
tools/golang/xenlight/Makefile +3 -0
+
+xenlight_%.go: gengotypes.py $(XEN_ROOT)/tools/libxl/libxl_types.idl $(XEN_ROOT)/tools/libxl/idl.py
+	XEN_ROOT=$(XEN_ROOT) $(PYTHON) gengotypes.py ../../libxl/libxl_types.idl
 
 # Go will do its own dependency checking, and not actuall go through
 # with the build if none of the input files have changed.
@@ -36,10 +38,14 @@ build: package
 .PHONY: install
 install: build
 	$(INSTALL_DIR) $(DESTDIR)$(GOXL_INSTALL_DIR)

tools/golang/xenlight/Makefile build: package · line 48

Quoted 10 lines
tools/golang/xenlight/Makefile +6 -2
-	$(INSTALL_DATA) $(XEN_GOPATH)$(GOXL_PKG_DIR)$(PKGSOURCES) $(DESTDIR)$(GOXL_INSTALL_DIR)
+	$(install_data) $(xen_gopath)$(goxl_pkg_dir)xenlight.go $(destdir)$(goxl_install_dir)
+	$(install_data) $(xen_gopath)$(goxl_pkg_dir)xenlight_types.go $(destdir)$(goxl_install_dir)
+	$(install_data) $(xen_gopath)$(goxl_pkg_dir)xenlight_helpers.go $(destdir)$(goxl_install_dir)
 
 .PHONY: uninstall
-	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, $(PKGSOURCES))
+	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, xenlight.go)
+	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, xenlight_types.go)
+	rm -f $(addprefix $(DESTDIR)$(GOXL_INSTALL_DIR)/, xenlight_helpers.go)

Kind of random, but would it make sense to rm -rf the whole directory here instead?

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#59 lore raw

Re: [Xen-devel] [PATCH 24/24] golang/xenlight: add make target for generated files

Quoted 8 lines

One standard practice when making a series is to try to avoid any regressions, including build regressions, in the middle of the series. This is particularly helpful to aid in bisections, but in this case it makes it easier to observe the action of the gengotypes.py script (and how it's meant to be called).

So I would basically make this part of patch 2, except remove references to xenlight_helpers.go until the patch where that file is generated.

Ah yeah that makes sense, I'll correct this in v2.

Quoted 11 lines

It might be nice to have a naming convention for the generated files that clues people in to the fact that they're generated (other than the comment at the top of course). In libxl, this is done by giving them a leading underscore (e.g., _libxl_type.h); but the go compiler will helpfully ignore such files. :-)

The go compiler will also do special things sometimes with things after a _; e.g., "${foo}_test.go" will only be compiled for go test, "${foo}_linux.go" will only be compiled on Linux, and so on. I'm pretty sure these names will be safe, but it might be slightly more future-proof to avoid using an underscore in the names.

+1 for a naming convention that says "this file is generated." But, the only special
cases that I'm aware of for go file name suffixes are "test", and valid GOOS and GOARCH
values. It's conventional to use underscores for compounded file names, and unnecessary
to avoid them.

To reference gRPC again, their protobuf compiler writes file names like 'package.pb.go', where
pb is short for protobuf. So, I think something like '<name>_generated.go', or '<name>.idl.go'
could work.

Quoted 2 lines

Kind of random, but would it make sense to rm -rf the whole directory here instead?

Yeah probably :)

-NR

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#60 lore raw

Re: [Xen-devel] [PATCH 24/24] golang/xenlight: add make target for generated files

Quoted 31 lines · Nick Rosbrook
Quoted 8 lines

One standard practice when making a series is to try to avoid any regressions, including build regressions, in the middle of the series. This is particularly helpful to aid in bisections, but in this case it makes it easier to observe the action of the gengotypes.py script (and how it's meant to be called).

So I would basically make this part of patch 2, except remove references to xenlight_helpers.go until the patch where that file is generated.

Ah yeah that makes sense, I'll correct this in v2.

Quoted 11 lines

It might be nice to have a naming convention for the generated files that clues people in to the fact that they're generated (other than the comment at the top of course). In libxl, this is done by giving them a leading underscore (e.g., _libxl_type.h); but the go compiler will helpfully ignore such files. :-)

The go compiler will also do special things sometimes with things after a _; e.g., "${foo}_test.go" will only be compiled for go test, "${foo}_linux.go" will only be compiled on Linux, and so on. I'm pretty sure these names will be safe, but it might be slightly more future-proof to avoid using an underscore in the names.

+1 for a naming convention that says "this file is generated." But, the only special
cases that I'm aware of for go file name suffixes are "test", and valid GOOS and GOARCH
values. It's conventional to use underscores for compounded file names, and unnecessary
to avoid them.

Quoted 5 lines

To reference gRPC again, their protobuf compiler writes file names like 'package.pb.go', where
pb is short for protobuf. So, I think something like '<name>_generated.go', or '<name>.idl.go'
could work.

Both of those are OK. I might go with "gen_*.go" to be a bit shorter, or ".idlgen.go" to make it clear that this is /generated from/ and idl, and not an idl itself. But I don't have strong opinions; any of those four options would be fine with me.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel