Skip to contentSkip to conversation

golang/xenlight: Create stub package

RFC PATCH Superseded Ronald Rojas 4 participants 24 messages Jan 23, 2017 – Feb 1, 2017

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/8 golang/xenlight: Create stub package

Create a basic Makefile to build and install libxenlight Golang bindings. Also add a stub package which only opens libxl context.

Include a global xenlight.Ctx variable which can be used as the default context by the entire program if desired.

For now, return simple errors. Proper error handling will be added in next patch.

---
 tools/Makefile                    | 17 ++++++++
 tools/golang/xenlight/Makefile    | 31 ++++++++++++++
 tools/golang/xenlight/xenlight.go | 86 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+)
 create mode 100644 tools/golang/xenlight/Makefile
 create mode 100644 tools/golang/xenlight/xenlight.go
tools/Makefile +17 -0
index 77e0723..c1e975f 100644
@@ -11,6 +11,8 @@ SUBDIRS-y += xenstore
 SUBDIRS-y += misc
 SUBDIRS-y += examples
 SUBDIRS-y += hotplug
+#Uncomment line to build Golang libxl
+#SUBDIRS-y += golang/xenlight
 SUBDIRS-y += xentrace
 SUBDIRS-$(CONFIG_XCUTILS) += xcutils
 SUBDIRS-$(CONFIG_X86) += firmware
@@ -303,6 +305,21 @@ subdir-clean-qemu-xen-dir:
 		$(MAKE) -C qemu-xen-dir clean; \
 	fi
 
+subdir-clean-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight clean
+
+subdir-distclean-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight distclean
+
+subdir-install-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight install
+
+subdir-all-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight all
+
+subdir-distclean-firmware: .phony
+	$(MAKE) -C firmware distclean
+
 subdir-clean-debugger/gdbsx subdir-distclean-debugger/gdbsx: .phony
 	$(MAKE) -C debugger/gdbsx clean
 
tools/golang/xenlight/Makefile new file +31 -0
new file mode 100644
index 0000000..1c2a2b7
@@ -0,0 +1,31 @@
+XEN_ROOT=$(CURDIR)/../../..
+GOLANG_SRC=$(GOPATH)/src/xenproject.org/xenlight
+CGO_CFLAGS = -I$(DESTDIR)$(includedir)
+CGO_LDFLAGS = -L$(DESTDIR)$(libdir) -Wl,-rpath-link=$(DESTDIR)$(libdir)
+include $(XEN_ROOT)/tools/Rules.mk
+
+BINARY = xenlight.a
+GO ?= go
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build: xenlight.a
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(GOLANG_SRC)
+	$(INSTALL_DATA) xenlight.go $(DESTDIR)$(GOLANG_SRC)
+
+.PHONY: clean
+clean:
+	$(RM) $(BINARY)
+
+.PHONY: distclean
+distclean: clean
+
+xenlight.a: xenlight.go
+	CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o $@ $<
+
+-include $(DEPS)
tools/golang/xenlight/xenlight.go new file +86 -0

87 lines not on this page · raw message

Signature · 7 lines
2.7.4


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

RFC PATCH 2/8 golang/xenlight: Add error constants and standard handling

Create error type Errorxl for throwing proper xenlight errors.

Update Ctx functions to throw Errorxl errors.

---
 tools/golang/xenlight/xenlight.go | 75 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)
tools/golang/xenlight/xenlight.go +74 -1
index f82e14e..eee2254 100644
@@ -38,10 +38,72 @@ import (
 	"unsafe"
 )
 
+/*
+ * Errors
+ */
+
+type Error int
+
+const (
+	ErrorNonspecific                  = Error(-C.ERROR_NONSPECIFIC)
+	ErrorVersion                      = Error(-C.ERROR_VERSION)
+	ErrorFail                         = Error(-C.ERROR_FAIL)
+	ErrorNi                           = Error(-C.ERROR_NI)
+	ErrorNomem                        = Error(-C.ERROR_NOMEM)
+	ErrorInval                        = Error(-C.ERROR_INVAL)
+	ErrorBadfail                      = Error(-C.ERROR_BADFAIL)
+	ErrorGuestTimedout                = Error(-C.ERROR_GUEST_TIMEDOUT)
+	ErrorTimedout                     = Error(-C.ERROR_TIMEDOUT)
+	ErrorNoparavirt                   = Error(-C.ERROR_NOPARAVIRT)
+	ErrorNotReady                     = Error(-C.ERROR_NOT_READY)
+	ErrorOseventRegFail               = Error(-C.ERROR_OSEVENT_REG_FAIL)
+	ErrorBufferfull                   = Error(-C.ERROR_BUFFERFULL)
+	ErrorUnknownChild                 = Error(-C.ERROR_UNKNOWN_CHILD)
+	ErrorLockFail                     = Error(-C.ERROR_LOCK_FAIL)
+	ErrorJsonConfigEmpty              = Error(-C.ERROR_JSON_CONFIG_EMPTY)
+	ErrorDeviceExists                 = Error(-C.ERROR_DEVICE_EXISTS)
+	ErrorCheckpointDevopsDoesNotMatch = Error(-C.ERROR_CHECKPOINT_DEVOPS_DOES_NOT_MATCH)
+	ErrorCheckpointDeviceNotSupported = Error(-C.ERROR_CHECKPOINT_DEVICE_NOT_SUPPORTED)
+	ErrorVnumaConfigInvalid           = Error(-C.ERROR_VNUMA_CONFIG_INVALID)
+	ErrorDomainNotfound               = Error(-C.ERROR_DOMAIN_NOTFOUND)
+	ErrorAborted                      = Error(-C.ERROR_ABORTED)
+	ErrorNotfound                     = Error(-C.ERROR_NOTFOUND)
+	ErrorDomainDestroyed              = Error(-C.ERROR_DOMAIN_DESTROYED)
+	ErrorFeatureRemoved               = Error(-C.ERROR_FEATURE_REMOVED)
+)
+
+var errors = [...]string{
+	ErrorNonspecific: "Non-specific error",
+	ErrorVersion: "Wrong version",
+	ErrorFail: "Failed",
+	ErrorNi: "Not Implemented",
+	ErrorNomem: "No memory",
+	ErrorInval: "Invalid argument",
+	ErrorBadfail: "Bad Fail",
+	ErrorGuestTimedout: "Guest timed out",
+	ErrorTimedout: "Timed out",
+	ErrorNoparavirt: "No Paravirtualization",
+	ErrorNotReady: "Not ready",
+	ErrorOseventRegFail: "OS event registration failed",
+	ErrorBufferfull: "Buffer full",
+	ErrorUnknownChild: "Unknown child",
+	ErrorLockFail: "Lock failed",
+	ErrorJsonConfigEmpty: "JSON config empty",
+	ErrorDeviceExists: "Device exists",
+	ErrorCheckpointDevopsDoesNotMatch: "Checkpoint devops does not match",
+	ErrorCheckpointDeviceNotSupported: "Checkpoint device not supported",
+	ErrorVnumaConfigInvalid: "VNUMA config invalid",
+	ErrorDomainNotfound: "Domain not found",
+	ErrorAborted: "Aborted",
+	ErrorNotfound: "Not found",
+	ErrorDomainDestroyed: "Domain destroyed",
+	ErrorFeatureRemoved: "Feature removed",
+}
 
 /*
  * Types: Builtins
  */
+
 type Context struct {
 	ctx *C.libxl_ctx
 }
@@ -51,6 +113,17 @@ type Context struct {
  */
 var Ctx Context
 
+func (e Error) Error() string{
+	if 0< int(e) && int(e) < len(errors){
+		s:= errors[e]
+		if s != ""{
+			return s
+		}
+	}
+	return fmt.Sprintf("libxl error: %d", -e)
+
+}
+
 func (Ctx *Context) IsOpen() bool {
 	return Ctx.ctx != nil
 }
@@ -83,4 +156,4 @@ func (Ctx *Context) CheckOpen() (err error) {
 		err = fmt.Errorf("Context not opened")
 	}
 	return
-}
\ No newline at end of file
+}
Signature · 7 lines
2.7.4


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

Re: [PATCH RFC 2/8] golang/xenlight: Add error constants and standard handling

Quoted 6 lines · Ronald Rojas

Create error type Errorxl for throwing proper xenlight errors.

Update Ctx functions to throw Errorxl errors.

Everything here looks, good with a few nitpicks...

Quoted 82 lines
---
 tools/golang/xenlight/xenlight.go | 75 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 74 insertions(+), 1 deletion(-)
tools/golang/xenlight/xenlight.go +62 -0
index f82e14e..eee2254 100644
@@ -38,10 +38,72 @@ import (
 	"unsafe"
 )
 
+/*
+ * Errors
+ */
+
+type Error int
+
+const (
+	ErrorNonspecific                  = Error(-C.ERROR_NONSPECIFIC)
+	ErrorVersion                      = Error(-C.ERROR_VERSION)
+	ErrorFail                         = Error(-C.ERROR_FAIL)
+	ErrorNi                           = Error(-C.ERROR_NI)
+	ErrorNomem                        = Error(-C.ERROR_NOMEM)
+	ErrorInval                        = Error(-C.ERROR_INVAL)
+	ErrorBadfail                      = Error(-C.ERROR_BADFAIL)
+	ErrorGuestTimedout                = Error(-C.ERROR_GUEST_TIMEDOUT)
+	ErrorTimedout                     = Error(-C.ERROR_TIMEDOUT)
+	ErrorNoparavirt                   = Error(-C.ERROR_NOPARAVIRT)
+	ErrorNotReady                     = Error(-C.ERROR_NOT_READY)
+	ErrorOseventRegFail               = Error(-C.ERROR_OSEVENT_REG_FAIL)
+	ErrorBufferfull                   = Error(-C.ERROR_BUFFERFULL)
+	ErrorUnknownChild                 = Error(-C.ERROR_UNKNOWN_CHILD)
+	ErrorLockFail                     = Error(-C.ERROR_LOCK_FAIL)
+	ErrorJsonConfigEmpty              = Error(-C.ERROR_JSON_CONFIG_EMPTY)
+	ErrorDeviceExists                 = Error(-C.ERROR_DEVICE_EXISTS)
+	ErrorCheckpointDevopsDoesNotMatch = Error(-C.ERROR_CHECKPOINT_DEVOPS_DOES_NOT_MATCH)
+	ErrorCheckpointDeviceNotSupported = Error(-C.ERROR_CHECKPOINT_DEVICE_NOT_SUPPORTED)
+	ErrorVnumaConfigInvalid           = Error(-C.ERROR_VNUMA_CONFIG_INVALID)
+	ErrorDomainNotfound               = Error(-C.ERROR_DOMAIN_NOTFOUND)
+	ErrorAborted                      = Error(-C.ERROR_ABORTED)
+	ErrorNotfound                     = Error(-C.ERROR_NOTFOUND)
+	ErrorDomainDestroyed              = Error(-C.ERROR_DOMAIN_DESTROYED)
+	ErrorFeatureRemoved               = Error(-C.ERROR_FEATURE_REMOVED)
+)
+
+var errors = [...]string{
+	ErrorNonspecific: "Non-specific error",
+	ErrorVersion: "Wrong version",
+	ErrorFail: "Failed",
+	ErrorNi: "Not Implemented",
+	ErrorNomem: "No memory",
+	ErrorInval: "Invalid argument",
+	ErrorBadfail: "Bad Fail",
+	ErrorGuestTimedout: "Guest timed out",
+	ErrorTimedout: "Timed out",
+	ErrorNoparavirt: "No Paravirtualization",
+	ErrorNotReady: "Not ready",
+	ErrorOseventRegFail: "OS event registration failed",
+	ErrorBufferfull: "Buffer full",
+	ErrorUnknownChild: "Unknown child",
+	ErrorLockFail: "Lock failed",
+	ErrorJsonConfigEmpty: "JSON config empty",
+	ErrorDeviceExists: "Device exists",
+	ErrorCheckpointDevopsDoesNotMatch: "Checkpoint devops does not match",
+	ErrorCheckpointDeviceNotSupported: "Checkpoint device not supported",
+	ErrorVnumaConfigInvalid: "VNUMA config invalid",
+	ErrorDomainNotfound: "Domain not found",
+	ErrorAborted: "Aborted",
+	ErrorNotfound: "Not found",
+	ErrorDomainDestroyed: "Domain destroyed",
+	ErrorFeatureRemoved: "Feature removed",
+}
 
 /*
  * Types: Builtins
  */
+
 type Context struct {
 	ctx *C.libxl_ctx
 }

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

Quoted 7 lines
tools/golang/xenlight/xenlight.go +4 -0
@@ -51,6 +113,17 @@ type Context struct {
  */
 var Ctx Context
 
+func (e Error) Error() string{
+	if 0< int(e) && int(e) < len(errors){
+		s:= errors[e]
+		if s != ""{

We tend to be picky about style; so a spaces:
- Between 0 and <
- between len(errors) and {
- between "" and {

I could fix these up on check-in, but since you'll be re-sending this anyway, I'll let you do it.

And of course you'll have to change the fmt.Errorf() to Error(-ret); but with those changes:

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

#3 lore raw

RFC PATCH 3/8 golang/xenlight: Add host-related functionality

In reply to Ronald Rojas

Add calls for the following host-related functionality:
- libxl_get_max_cpus
- libxl_get_online_cpus
- libxl_get_max_nodes
- libxl_get_free_memory
- libxl_get_physinfo
- libxl_get_version_info

Include Golang versions of the following structs:
- libxl_physinfo as Physinfo
- libxl_version_info as VersionInfo
- libxl_hwcap as Hwcap

---
 tools/golang/xenlight/xenlight.go | 186 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 186 insertions(+)
tools/golang/xenlight/xenlight.go +186 -0
index eee2254..92410a8 100644
@@ -108,6 +108,63 @@ type Context struct {
 	ctx *C.libxl_ctx
 }
 
+type Hwcap []C.uint32_t
+
+func (chwcap C.libxl_hwcap)CToGo() (ghwcap Hwcap) {
+	// Alloc a Go slice for the bytes
+	size := 8
+	ghwcap= make([]C.uint32_t, size)
+
+	// Make a slice pointing to the C array
+	mapslice := (*[1 << 30]C.uint32_t)(unsafe.Pointer(&chwcap[0]))[:size:size]
+
+	// And copy the C array into the Go array
+	copy(ghwcap, mapslice)
+
+	return
+}
+
+
+/*
+ * Types: IDL
+ *
+ * FIXME: Generate these automatically from the IDL
+ */
+
+type Physinfo struct {
+	ThreadsPerCore uint32
+	CoresPerSocket uint32
+	MaxCpuId uint32
+	NrCpus    uint32
+	CpuKhz    uint32
+	TotalPages         uint64
+	FreePages          uint64
+	ScrubPages         uint64
+	OutstandingPages   uint64
+	SharingFreedPages uint64
+	SharingUsedFrames uint64
+	NrNodes uint32
+	HwCap   Hwcap
+	CapHvm          bool
+	CapHvmDirectio bool
+}
+
+type VersionInfo struct {
+	XenVersionMajor int
+	XenVersionMinor int
+	XenVersionExtra string
+	Compiler          string
+	CompileBy        string
+	CompileDomain    string
+	CompileDate      string
+	Capabilities      string
+	Changeset         string
+	VirtStart        uint64
+	Pagesize          int
+	Commandline       string
+	BuildId          string
+}
+
 /*
  * Context
  */
@@ -157,3 +214,132 @@ func (Ctx *Context) CheckOpen() (err error) {
 	}
 	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)
+		return
+	}
+	maxCpus = int(ret)
+	return
+}
+
+//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)
+		return
+	}
+	onCpus = int(ret)
+	return
+}
+
+//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)
+		return
+	}
+	maxNodes = int(ret)
+	return
+}
+
+//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)
+
+	if ret < 0 {
+		err = Error(-ret)
+		return
+	}
+
+	memkb = uint64(cmem)
+	return
+
+}
+
+//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
+
+	ret := C.libxl_get_physinfo(Ctx.ctx, &cphys)
+
+	if ret < 0 {
+		err = Error(ret)
+		return
+	}
+	physinfo = &Physinfo{}
+	physinfo.ThreadsPerCore = uint32(cphys.threads_per_core)
+	physinfo.CoresPerSocket = uint32(cphys.cores_per_socket)
+	physinfo.MaxCpuId = uint32(cphys.max_cpu_id)
+	physinfo.NrCpus = uint32(cphys.nr_cpus)
+	physinfo.CpuKhz= uint32(cphys.cpu_khz)
+	physinfo.TotalPages = uint64(cphys.total_pages)
+	physinfo.FreePages = uint64(cphys.free_pages)
+	physinfo.ScrubPages = uint64(cphys.scrub_pages)
+	physinfo.ScrubPages = uint64(cphys.scrub_pages)
+	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.CToGo()
+	physinfo.CapHvm = bool(cphys.cap_hvm)
+	physinfo.CapHvmDirectio = bool(cphys.cap_hvm_directio)
+
+	return
+}
+
+//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)
+
+	info = &VersionInfo{}
+	info.XenVersionMajor = int(cinfo.xen_version_major)
+	info.XenVersionMinor = int(cinfo.xen_version_minor)
+	info.XenVersionExtra = C.GoString(cinfo.xen_version_extra)
+	info.Compiler = C.GoString(cinfo.compiler)
+	info.CompileBy = C.GoString(cinfo.compile_by)
+	info.CompileDomain = C.GoString(cinfo.compile_domain)
+	info.CompileDate = C.GoString(cinfo.compile_date)
+	info.Capabilities = C.GoString(cinfo.capabilities)
+	info.Changeset = C.GoString(cinfo.changeset)
+	info.VirtStart = uint64(cinfo.virt_start)
+	info.Pagesize = int(cinfo.pagesize)
+	info.Commandline = C.GoString(cinfo.commandline)
+	info.BuildId = C.GoString(cinfo.build_id)
+
+	return
+}
Signature · 7 lines
2.7.4


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

Re: [PATCH RFC 3/8] golang/xenlight: Add host-related functionality

Quoted 8 lines · Ronald Rojas

Add calls for the following host-related functionality:
- libxl_get_max_cpus
- libxl_get_online_cpus
- libxl_get_max_nodes
- libxl_get_free_memory
- libxl_get_physinfo
- libxl_get_version_info

Quoted 6 lines

Include Golang versions of the following structs:
- libxl_physinfo as Physinfo
- libxl_version_info as VersionInfo
- libxl_hwcap as Hwcap

Mostly looks good! One final comment:

[snip]

tools/golang/xenlight/xenlight.go func (Ctx *Context) CheckOpen() (err error) { · line 292

Quoted 9 lines
tools/golang/xenlight/xenlight.go +9 -0
+//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
+
+	ret := C.libxl_get_physinfo(Ctx.ctx, &cphys)

So I actually forgot that you're actually supposed to always call "libxl_<type>_init()" on IDL-generated types before using them, and "libxl_<type>_dispose()" when you're done with them. As it happens in this case, the struct contains no pointers to sub-structs; but if that changes in the future, this would leak the memory.

Everything else is good, thanks.

-George

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

#5 lore raw

Re: [PATCH RFC 3/8] golang/xenlight: Add host-related functionality

In reply to Ronald Rojas
Quoted 8 lines · Ronald Rojas

Add calls for the following host-related functionality:
- libxl_get_max_cpus
- libxl_get_online_cpus
- libxl_get_max_nodes
- libxl_get_free_memory
- libxl_get_physinfo
- libxl_get_version_info

Quoted 6 lines

Include Golang versions of the following structs:
- libxl_physinfo as Physinfo
- libxl_version_info as VersionInfo
- libxl_hwcap as Hwcap

Oh, last thing: It's probably worth running "go fmt xenlight.go" for each patch. That will catch little things like lack of spaces between things, and aligning the structures nicely.

-George

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

#6 lore raw

Re: [PATCH RFC 3/8] golang/xenlight: Add host-related functionality

In reply to Ronald Rojas
Quoted 23 lines · Ronald Rojas

Add calls for the following host-related functionality:
- libxl_get_max_cpus
- libxl_get_online_cpus
- libxl_get_max_nodes
- libxl_get_free_memory
- libxl_get_physinfo
- libxl_get_version_info

Include Golang versions of the following structs:
- libxl_physinfo as Physinfo
- libxl_version_info as VersionInfo
- libxl_hwcap as Hwcap

---
 tools/golang/xenlight/xenlight.go | 186 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 186 insertions(+)
tools/golang/xenlight/xenlight.go +0 -0
index eee2254..92410a8 100644

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

Quoted 6 lines
tools/golang/xenlight/xenlight.go +3 -0
@@ -108,6 +108,63 @@ type Context struct {
 	ctx *C.libxl_ctx
 }
 
+type Hwcap []C.uint32_t
+
+func (chwcap C.libxl_hwcap)CToGo() (ghwcap Hwcap) {

Sorry... I'm coming back to this after a bit of time and remembering a bunch of things as I'm going along.

I don't think we want this method to be available publicly, so this should probably be 'cToGo' (Or perhaps just 'toGo', since it's already a C structure.)

-George

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

#7 lore raw

RFC PATCH 4/8 golang/xenlight: Implement libxl_domain_info and libxl_domain_unpause

In reply to Ronald Rojas

Add calls for the following host-related functionality:
- libxl_domain_info
- libxl_domain_unpause

Include Golang version for the libxl_domain_info as DomainInfo.

---
 tools/golang/xenlight/xenlight.go | 92 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
tools/golang/xenlight/xenlight.go +92 -0
index 92410a8..dd6893c 100644
@@ -36,6 +36,7 @@ import "C"
 import (
 	"fmt"
 	"unsafe"
+	"time"
 )
 
 /*
@@ -104,6 +105,12 @@ var errors = [...]string{
  * Types: Builtins
  */
 
+type Domid uint32
+
+type MemKB uint64
+
+type Uuid C.libxl_uuid
+
 type Context struct {
 	ctx *C.libxl_ctx
 }
@@ -165,6 +172,32 @@ type VersionInfo struct {
 	BuildId          string
 }
 
+type Dominfo struct {
+	Uuid       Uuid
+	Domid      Domid
+	Ssidref uint32
+	SsidLabel string
+	Running    bool
+	Blocked    bool
+	Paused     bool
+	Shutdown   bool
+	Dying      bool
+	NeverStop bool
+
+	ShutdownReason   int32 // FIXME shutdown_reason enumeration
+	OutstandingMemkb MemKB
+	CurrentMemkb     MemKB
+	SharedMemkb      MemKB
+	PagedMemkb       MemKB
+	MaxMemkb         MemKB
+	CpuTime          time.Duration
+	VcpuMaxId       uint32
+	VcpuOnline       uint32
+	Cpupool           uint32
+	DomainType       int32 //FIXME libxl_domain_type enumeration
+
+}
+
 /*
  * Context
  */
@@ -343,3 +376,62 @@ func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) {
 
 	return
 }
+
+func (Ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) {
+	err = Ctx.CheckOpen()
+	if err != nil {
+		return
+	}
+
+	var cdi C.libxl_dominfo
+
+	ret := C.libxl_domain_info(Ctx.ctx, unsafe.Pointer(&cdi), C.uint32_t(Id))
+
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	// We could consider having this boilerplate generated by the
+	// idl, in a function like this:
+	//
+	// di = translateCdomaininfoToGoDomaininfo(cdi)
+	di = &Dominfo{}
+	di.Uuid = Uuid(cdi.uuid)
+	di.Domid = Domid(cdi.domid)
+	di.Ssidref = uint32(cdi.ssidref)
+	di.SsidLabel = C.GoString(cdi.ssid_label)
+	di.Running = bool(cdi.running)
+	di.Blocked = bool(cdi.blocked)
+	di.Paused = bool(cdi.paused)
+	di.Shutdown = bool(cdi.shutdown)
+	di.Dying = bool(cdi.dying)
+	di.NeverStop= bool(cdi.never_stop)
+	di.ShutdownReason= int32(cdi.shutdown_reason)
+	di.OutstandingMemkb= MemKB(cdi.outstanding_memkb)
+	di.CurrentMemkb = MemKB(cdi.current_memkb)
+	di.SharedMemkb = MemKB(cdi.shared_memkb)
+	di.PagedMemkb = MemKB(cdi.paged_memkb)
+	di.MaxMemkb= MemKB(cdi.max_memkb)
+	di.CpuTime= time.Duration(cdi.cpu_time)
+	di.VcpuMaxId = uint32(cdi.vcpu_max_id)
+	di.VcpuOnline = uint32(cdi.vcpu_online)
+	di.Cpupool = uint32(cdi.cpupool)
+	di.DomainType = int32(cdi.domain_type)
+
+	return
+}
+
+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))
+
+	if ret != 0 {
+		err = Error(-ret)
+	}
+	return
+}
Signature · 7 lines
2.7.4


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

Re: [PATCH RFC 4/8] golang/xenlight: Implement libxl_domain_info and libxl_domain_unpause

Quoted 43 lines · Ronald Rojas

Add calls for the following host-related functionality:
- libxl_domain_info
- libxl_domain_unpause

Include Golang version for the libxl_domain_info as DomainInfo.

---
 tools/golang/xenlight/xenlight.go | 92 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
tools/golang/xenlight/xenlight.go +8 -0
index 92410a8..dd6893c 100644
@@ -36,6 +36,7 @@ import "C"
 import (
 	"fmt"
 	"unsafe"
+	"time"
 )
 
 /*
@@ -104,6 +105,12 @@ var errors = [...]string{
  * Types: Builtins
  */
 
+type Domid uint32
+
+type MemKB uint64
+
+type Uuid C.libxl_uuid
+
 type Context struct {
 	ctx *C.libxl_ctx
 }
@@ -165,6 +172,32 @@ type VersionInfo struct {
 	BuildId          string
 }
 
+type Dominfo struct {

tools/golang/xenlight/xenlight.go type VersionInfo struct { · line 185

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+	Uuid       Uuid
+	Domid      Domid
+	Ssidref uint32
+	SsidLabel string
+	Running    bool
+	Blocked    bool
+	Paused     bool
+	Shutdown   bool
+	Dying      bool
+	NeverStop bool

Run 'go fmt' to align these.

tools/golang/xenlight/xenlight.go type VersionInfo struct { · line 197

Quoted 12 lines
tools/golang/xenlight/xenlight.go +12 -0
+
+	ShutdownReason   int32 // FIXME shutdown_reason enumeration
+	OutstandingMemkb MemKB
+	CurrentMemkb     MemKB
+	SharedMemkb      MemKB
+	PagedMemkb       MemKB
+	MaxMemkb         MemKB
+	CpuTime          time.Duration
+	VcpuMaxId       uint32
+	VcpuOnline       uint32
+	Cpupool           uint32
+	DomainType       int32 //FIXME libxl_domain_type enumeration

We need to have these enumerations actually created as consts, like we do with the errors.

We should also make a type for these enums, and a String method that calls the libxl_<type>_to_string() function to convert it to a string.

Quoted 20 lines
tools/golang/xenlight/xenlight.go +13 -0
+
+}
+
 /*
  * Context
  */
@@ -343,3 +376,62 @@ func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) {
 
 	return
 }
+
+func (Ctx *Context) DomainInfo(Id Domid) (di *Dominfo, err error) {
+	err = Ctx.CheckOpen()
+	if err != nil {
+		return
+	}
+
+	var cdi C.libxl_dominfo
+
+	ret := C.libxl_domain_info(Ctx.ctx, unsafe.Pointer(&cdi), C.uint32_t(Id))

tools/golang/xenlight/xenlight.go func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) { · line 398

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	// We could consider having this boilerplate generated by the
+	// idl, in a function like this:
+	//
+	// di = translateCdomaininfoToGoDomaininfo(cdi)

Stale comment; you could remove this, or make it cdi.toGo().

tools/golang/xenlight/xenlight.go func (Ctx *Context) GetVersionInfo() (info *VersionInfo, err error) { · line 403

Quoted 5 lines
tools/golang/xenlight/xenlight.go +5 -0
+	di = &Dominfo{}
+	di.Uuid = Uuid(cdi.uuid)
+	di.Domid = Domid(cdi.domid)
+	di.Ssidref = uint32(cdi.ssidref)
+	di.SsidLabel = C.GoString(cdi.ssid_label)

And here we'll probably leak memory if we don't call libxl_dominfo_dispose() before returning.

Everything else looks good, thanks!

-George

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

#9 lore raw

RFC PATCH 5/8 golang/xenlight: Add tests host related functinality functions

In reply to Ronald Rojas

Create tests for the following functions:
- GetVersionInfo
- GetPhysinfo
- GetDominfo
- GetMaxCpus
- GetOnlineCpus
- GetMaxNodes
- GetFreeMemory

---
 tools/golang/xenlight/test/dominfo/Makefile        | 18 ++++++++
 tools/golang/xenlight/test/dominfo/dominfo.c       | 29 +++++++++++++
 tools/golang/xenlight/test/dominfo/dominfo.go      | 48 ++++++++++++++++++++++
 tools/golang/xenlight/test/freememory/Makefile     | 19 +++++++++
 tools/golang/xenlight/test/freememory/freememory.c | 25 +++++++++++
 .../golang/xenlight/test/freememory/freememory.go  | 35 ++++++++++++++++
 tools/golang/xenlight/test/maxcpu/Makefile         | 19 +++++++++
 tools/golang/xenlight/test/maxcpu/maxcpu.c         | 21 ++++++++++
 tools/golang/xenlight/test/maxcpu/maxcpu.go        | 34 +++++++++++++++
 tools/golang/xenlight/test/maxnodes/Makefile       | 19 +++++++++
 tools/golang/xenlight/test/maxnodes/maxnodes.c     | 21 ++++++++++
 tools/golang/xenlight/test/maxnodes/maxnodes.go    | 34 +++++++++++++++
 tools/golang/xenlight/test/onlinecpu/Makefile      | 19 +++++++++
 tools/golang/xenlight/test/onlinecpu/onlinecpu.c   | 21 ++++++++++
 tools/golang/xenlight/test/onlinecpu/onlinecpu.go  | 34 +++++++++++++++
 tools/golang/xenlight/test/physinfo/Makefile       | 19 +++++++++
 tools/golang/xenlight/test/physinfo/physinfo.c     | 31 ++++++++++++++
 tools/golang/xenlight/test/physinfo/physinfo.go    | 36 ++++++++++++++++
 tools/golang/xenlight/test/versioninfo/Makefile    | 18 ++++++++
 .../golang/xenlight/test/versioninfo/versioninfo.c | 18 ++++++++
 .../xenlight/test/versioninfo/versioninfo.go       | 33 +++++++++++++++
 21 files changed, 551 insertions(+)
 create mode 100644 tools/golang/xenlight/test/dominfo/Makefile
 create mode 100644 tools/golang/xenlight/test/dominfo/dominfo.c
 create mode 100644 tools/golang/xenlight/test/dominfo/dominfo.go
 create mode 100644 tools/golang/xenlight/test/freememory/Makefile
 create mode 100644 tools/golang/xenlight/test/freememory/freememory.c
 create mode 100644 tools/golang/xenlight/test/freememory/freememory.go
 create mode 100644 tools/golang/xenlight/test/maxcpu/Makefile
 create mode 100644 tools/golang/xenlight/test/maxcpu/maxcpu.c
 create mode 100644 tools/golang/xenlight/test/maxcpu/maxcpu.go
 create mode 100644 tools/golang/xenlight/test/maxnodes/Makefile
 create mode 100644 tools/golang/xenlight/test/maxnodes/maxnodes.c
 create mode 100644 tools/golang/xenlight/test/maxnodes/maxnodes.go
 create mode 100644 tools/golang/xenlight/test/onlinecpu/Makefile
 create mode 100644 tools/golang/xenlight/test/onlinecpu/onlinecpu.c
 create mode 100644 tools/golang/xenlight/test/onlinecpu/onlinecpu.go
 create mode 100644 tools/golang/xenlight/test/physinfo/Makefile
 create mode 100644 tools/golang/xenlight/test/physinfo/physinfo.c
 create mode 100644 tools/golang/xenlight/test/physinfo/physinfo.go
 create mode 100644 tools/golang/xenlight/test/versioninfo/Makefile
 create mode 100644 tools/golang/xenlight/test/versioninfo/versioninfo.c
 create mode 100644 tools/golang/xenlight/test/versioninfo/versioninfo.go
tools/golang/xenlight/test/dominfo/Makefile new file +18 -0
new file mode 100644
index 0000000..a7351cf
@@ -0,0 +1,18 @@
+
+test: clean build
+	./dominfo.out >> c.output
+	go run dominfo.go >> go.output
+	if cmp -s "c.output" "go.output"; then\
+		echo "GetDominfo PASSED";\
+	else \
+		echo "GetDominfo FAILED";\
+	fi\
+
+build:
+	go build -o dominfo.a dominfo.go 
+	gcc dominfo.c -lxenlight -lyajl -o dominfo.out
+
+clean:
+	rm -f *.a
+	rm -f *.out
+	rm -f *.output
tools/golang/xenlight/test/dominfo/dominfo.c new file +29 -0
new file mode 100644
index 0000000..ebe18c3
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+char * bool_to_string(bool b);
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_dominfo info;
+    int err = libxl_domain_info(context, &info, 0);
+    if(err != 0){
+        return err;
+    }
+    
+    
+	printf("%d\n%d\n", info.domid, info.ssidref);
+	printf("%s\n%s\n%s\n%s\n%s\n%s\n", bool_to_string(info.running), bool_to_string(info.blocked ), bool_to_string(info.paused ),bool_to_string(info.shutdown ),bool_to_string(info.dying), bool_to_string(info.never_stop));
+	long cpu_time = info.cpu_time/((long)1<<35);
+	printf("%d\n%lu\n%lu\n%lu\n%lu\n%lu\n%lu\n%d\n%d\n%d\n", info.shutdown_reason, info.outstanding_memkb, info.current_memkb, info.shared_memkb, info.paged_memkb, info.max_memkb, cpu_time, info.vcpu_max_id, info.vcpu_online, info.cpupool);
+	printf("%d\n", info.domain_type);
+
+
+    libxl_ctx_free(context);
+
+}
+char * bool_to_string(bool b){
+    return b ? "true": "false";
+}
tools/golang/xenlight/test/dominfo/dominfo.go new file +48 -0

48 lines not on this page · raw message

tools/golang/xenlight/test/freememory/Makefile new file +19 -0
new file mode 100644
index 0000000..9548e50
@@ -0,0 +1,19 @@
+
+test: clean build
+	./func.out >> c.output
+	go run func.go >> go.output
+	if cmp -s "c.output" "go.output"; then\
+		echo "GetFreeMemory PASSED";\
+	else \
+		echo "GetFreeMemory FAILED"; \
+	fi\
+
+build:
+	go build -o func.a func.go
+	gcc func.c -lxenlight -lyajl -o func.out
+
+clean:
+	rm -f *.a
+	rm -f *.out
+	rm -f *.output
+
tools/golang/xenlight/test/freememory/freememory.c new file +25 -0
new file mode 100644
index 0000000..9ff8444
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_physinfo info ;
+    int err= libxl_get_physinfo(context,&info);
+    if(err != 0){
+        return err;
+    }
+
+   uint64_t free_memory;
+    err = libxl_get_free_memory(context, &free_memory);
+    if(err < 0){
+        printf("%d\n", err);
+    }else{
+        printf("%lu\n", free_memory);
+    }
+    libxl_ctx_free(context);
+
+}
+
tools/golang/xenlight/test/freememory/freememory.go new file +35 -0
new file mode 100644
index 0000000..19501e7
@@ -0,0 +1,35 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+
+func main() {
+	ctx := xenlight.Ctx
+	err := ctx.Open()
+	if err != nil{
+		os.Exit(-1)
+	}
+	defer ctx.Close()
+	if err != nil{
+		os.Exit(-1)
+	}
+
+
+	free_memory, err := ctx.GetFreeMemory()
+	if err != nil{
+		fmt.Printf("%d\n", err)
+	}else{
+		fmt.Printf("%d\n", free_memory)
+	}
+
+}
tools/golang/xenlight/test/maxcpu/Makefile new file +19 -0
new file mode 100644
index 0000000..17aa19a
@@ -0,0 +1,19 @@
+
+test: clean build
+	./maxcpu.out >> c.output
+	go run maxcpu.go >> go.output
+	if cmp -s "c.output" "go.output"; then\
+		echo "GetMaxCpu PASSED";\
+	else \
+		echo "GetMaxCpu FAILED"; \
+	fi\
+
+build:
+	go build -o maxcpu.a maxcpu.go
+	gcc maxcpu.c -lxenlight -lyajl -o maxcpu.out
+
+clean:
+	rm -f *.a
+	rm -f *.out
+	rm -f *.output
+
tools/golang/xenlight/test/maxcpu/maxcpu.c new file +21 -0
new file mode 100644
index 0000000..60ef7ce
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_physinfo info ;
+    int err= libxl_get_physinfo(context,&info);
+    if(err != 0){
+        return err;
+    }
+
+    int max_cpus = libxl_get_max_cpus(context);
+    printf("%d\n", max_cpus);
+
+    libxl_ctx_free(context);
+
+}
+
tools/golang/xenlight/test/maxcpu/maxcpu.go new file +34 -0
new file mode 100644
index 0000000..eef575f
@@ -0,0 +1,34 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+
+func main() {
+	ctx := xenlight.Ctx
+	err := ctx.Open()
+	if err != nil{
+		os.Exit(-1)
+	}
+	defer ctx.Close()
+	if err != nil{
+		os.Exit(-1)
+	}
+
+	max_cpus, err := ctx.GetMaxCpus()
+	if err != nil{
+		fmt.Printf("%d\n", err )
+	}else{
+		fmt.Printf("%d\n", max_cpus)
+	}
+
+}
tools/golang/xenlight/test/maxnodes/Makefile new file +19 -0
new file mode 100644
index 0000000..7535efe
@@ -0,0 +1,19 @@
+
+test: clean build
+	./maxnodes.out >> c.output
+	go run maxnodes.go >> go.output
+	if cmp -s "c.output" "go.output"; then\
+		echo "GetMaxNodes PASSED";\
+	else \
+		echo "GetMaxNodes FAILED"; \
+	fi\
+
+build:
+	go build -o maxnodes.a maxnodes.go
+	gcc maxnodes.c -lxenlight -lyajl -o maxnodes.out
+
+clean:
+	rm -f *.a
+	rm -f *.out
+	rm -f *.output
+
tools/golang/xenlight/test/maxnodes/maxnodes.c new file +21 -0
new file mode 100644
index 0000000..8f7ce9c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_physinfo info ;
+    int err= libxl_get_physinfo(context,&info);
+    if(err != 0){
+        return err;
+    }
+
+    int max_nodes = libxl_get_max_nodes(context);
+    printf("%d\n", max_nodes);
+
+   libxl_ctx_free(context);
+
+}
+
tools/golang/xenlight/test/maxnodes/maxnodes.go new file +34 -0
new file mode 100644
index 0000000..5f619ff
@@ -0,0 +1,34 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+
+func main() {
+	ctx := xenlight.Ctx
+	err := ctx.Open()
+	if err != nil{
+		os.Exit(-1)
+	}
+	defer ctx.Close()
+	if err != nil{
+		os.Exit(-1)
+	}
+
+	max_nodes, err := ctx.GetMaxNodes()
+	if err != nil{
+		fmt.Printf("%d\n", err)
+	}else{
+		fmt.Printf("%d\n", max_nodes)
+	}
+
+}
tools/golang/xenlight/test/onlinecpu/Makefile new file +19 -0
new file mode 100644
index 0000000..9df2256
@@ -0,0 +1,19 @@
+
+test: clean build
+	./onlinecpu.out >> c.output
+	go run onlinecpu.go >> go.output
+	if cmp -s "c.output" "go.output"; then\
+		echo "GetOnlineCpus PASSED";\
+	else \
+		echo "GetOnline FAILED"; \
+	fi\
+
+build:
+	go build -o onlinecpu.a onlinecpu.go
+	gcc onlinecpu.c -lxenlight -lyajl -o onlinecpu.out
+
+clean:
+	rm -f *.a
+	rm -f *.out
+	rm -f *.output
+
tools/golang/xenlight/test/onlinecpu/onlinecpu.c new file +21 -0
new file mode 100644
index 0000000..a56ff71
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_physinfo info ;
+    int err= libxl_get_physinfo(context,&info);
+    if(err != 0){
+        return err;
+    }
+
+    int online_cpus = libxl_get_online_cpus(context);
+    printf("%d\n", online_cpus);
+
+    libxl_ctx_free(context);
+
+}
+
tools/golang/xenlight/test/onlinecpu/onlinecpu.go new file +34 -0
new file mode 100644
index 0000000..2c4968e
@@ -0,0 +1,34 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+
+func main() {
+	ctx := xenlight.Ctx
+	err := ctx.Open()
+	if err != nil{
+		os.Exit(-1)
+	}
+	defer ctx.Close()
+	if err != nil{
+		os.Exit(-1)
+	}
+
+	online_cpus, err := ctx.GetOnlineCpus()
+	if err != nil{
+		fmt.Printf("%d\n", err)
+	}else{
+		fmt.Printf("%d\n", online_cpus)
+	}
+
+}
tools/golang/xenlight/test/physinfo/Makefile new file +19 -0
new file mode 100644
index 0000000..87fd171
@@ -0,0 +1,19 @@
+
+test: clean build
+	./physinfo.out >> c.output
+	go run physinfo.go >> go.output
+	if cmp -s "c.output" "go.output"; then\
+		echo "GetPhysinfo PASSED";\
+	else \
+		echo "GetPhysinfo FAILED"; \
+	fi\
+
+build:
+	go build -o physinfo.a physinfo.go 
+	gcc physinfo.c -lxenlight -lyajl -o physinfo.out
+
+clean:
+	rm -f *.a
+	rm -f *.out
+	rm -f *.output
+
tools/golang/xenlight/test/physinfo/physinfo.c new file +31 -0
new file mode 100644
index 0000000..92c8f47
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+char *bool_to_string(bool b);
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_physinfo info ;
+    int err= libxl_get_physinfo(context,&info);
+    if(err != 0){
+        return err;
+    }
+
+    printf("%d\n%d\n%d\n%d\n%d\n", info.threads_per_core, info.cores_per_socket, info.max_cpu_id, info.nr_cpus, info.cpu_khz);
+    printf("%lu\n%lu\n%lu\n%lu\n%lu\n%lu\n", info.total_pages, info.free_pages, info.scrub_pages, info.outstanding_pages, info.sharing_freed_pages, info.sharing_used_frames);
+    printf("%u\n",info.nr_nodes);
+    printf("%s\n%s\n", bool_to_string(info.cap_hvm), bool_to_string(info.cap_hvm_directio));
+
+    for(int i = 0; i < 8; i++){
+        printf("%u\n", info.hw_cap[i]);
+    }
+
+    libxl_ctx_free(context);
+
+}
+
+char * bool_to_string(bool b){
+    return b ? "true": "false";
+}
tools/golang/xenlight/test/physinfo/physinfo.go new file +36 -0
new file mode 100644
index 0000000..039da59
@@ -0,0 +1,36 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+
+func main() {
+	ctx := xenlight.Ctx
+	err := ctx.Open()
+	if err != nil{
+		os.Exit(-1)
+	}
+	defer ctx.Close()
+	info, err:= ctx.GetPhysinfo()
+	if err != nil{
+		os.Exit(-1)
+	}
+
+	fmt.Printf("%d\n%d\n%d\n%d\n%d\n", info.ThreadsPerCore, info.CoresPerSocket, info.MaxCpuId, info.NrCpus, info.CpuKhz)
+	fmt.Printf("%d\n%d\n%d\n%d\n%d\n%d\n", info.TotalPages, info.FreePages, info.ScrubPages, info.OutstandingPages, info.SharingFreedPages, info.SharingUsedFrames)
+	fmt.Printf("%d\n",info.NrNodes)
+	fmt.Printf("%t\n%t\n", info.CapHvm, info.CapHvmDirectio)
+
+	for i := 0; i < 8; i++ {
+	fmt.Printf("%d\n", info.HwCap[i]);
+	}
+}
tools/golang/xenlight/test/versioninfo/Makefile new file +18 -0
new file mode 100644
index 0000000..0ad1527
@@ -0,0 +1,18 @@
+
+test: clean build
+	./versioninfo.out >> c.output
+	go run versioninfo.go >> go.output
+	if cmp -s "c.output" "go.output"; then\
+		echo "GetVersionInfo PASSED";\
+	else \
+		echo "GetVersionInfo FAILED"; \
+	fi\
+
+build:
+	go build -o versioninfo.a versioninfo.go 
+	gcc versioninfo.c -lxenlight -lyajl -o versioninfo.out
+
+clean:
+	rm -f *.a
+	rm -f *.out
+	rm -f *.output
tools/golang/xenlight/test/versioninfo/versioninfo.c new file +18 -0
new file mode 100644
index 0000000..f40495d
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_version_info* info = libxl_get_version_info(context);
+
+    printf("%d\n%d\n", info->xen_version_major, info->xen_version_minor);
+    printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n", info->xen_version_extra, info->compiler, info->compile_by, info->compile_domain, info->compile_date, info->capabilities, info->changeset);
+    printf("%lu\n%d\n", info->virt_start, info->pagesize);
+    printf("%s\n%s\n", info->commandline, info->build_id);
+
+    libxl_ctx_free(context);
+
+}
tools/golang/xenlight/test/versioninfo/versioninfo.go new file +33 -0
new file mode 100644
index 0000000..cad8391
@@ -0,0 +1,33 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+
+func main() {
+	ctx := xenlight.Ctx
+	err := ctx.Open()
+	if err != nil{
+		os.Exit(-1)
+	}
+	defer ctx.Close()
+	info, err:= ctx.GetVersionInfo()
+	if err != nil{
+		os.Exit(-1)
+	}
+
+	fmt.Printf("%d\n%d\n", info.XenVersionMajor, info.XenVersionMinor);
+	fmt.Printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n", info.XenVersionExtra, info.Compiler, info.CompileBy, info.CompileDomain, info.CompileDate, info.Capabilities, info.Changeset);
+	fmt.Printf("%d\n%d\n", info.VirtStart, info.Pagesize);
+	fmt.Printf("%s\n%s\n", info.Commandline, info.BuildId);
+
+}
Signature · 7 lines
2.7.4


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

Re: [PATCH RFC 5/8] golang/xenlight: Add tests host related functinality functions

Quoted 10 lines · Ronald Rojas

Create tests for the following functions:
- GetVersionInfo
- GetPhysinfo
- GetDominfo
- GetMaxCpus
- GetOnlineCpus
- GetMaxNodes
- GetFreeMemory

Hey Ronald,

Sorry this has taken a long time to get to. I think building the test cases has been really good for testing out the package *as a package*; and I think the general approach is a really good one.

One big comment first: Rather than having a separate makefile for each of these, what about putting all the tests of a similar type -- like these, which are primarily comparing the output of the functions -- in the same directory, and using a makefile like the attached? (You may have to modify the XEN_ROOT variable at the top to reflect where you end up putting it.)

Then, as promised, a whole slew of nitpicks.

Quick heads-up: It's easy the first time you have your code nitpicked like this to hear an angry or annoyed "tone", because the comments are really short. That's not what's intended; it's just to save time.

tools/golang/xenlight/test/dominfo/dominfo.c line 5

Quoted 11 lines
tools/golang/xenlight/test/dominfo/dominfo.c new file +5 -0
new file mode 100644
index 0000000..ebe18c3
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+char * bool_to_string(bool b);

Need a newline here. And I suspect you'll be using this function again -- maybe you could put it in a .h file as a static inline?

tools/golang/xenlight/test/dominfo/dominfo.c line 11

Quoted 6 lines
tools/golang/xenlight/test/dominfo/dominfo.c new file +6 -0
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_dominfo info;
+    int err = libxl_domain_info(context, &info, 0);

In the Xen tools style you normally declare all the variables together at the top of the function, and then put a newline between them. It's probably a good idea to read tools/libxl/CODING_STYLE, particularly the "Formating and naming" section.

Also don't forget to call libxl_dominfo_init(&info) (which will set some defaults).

tools/golang/xenlight/test/dominfo/dominfo.c line 14

Quoted 3 lines
tools/golang/xenlight/test/dominfo/dominfo.c new file +3 -0
+    if(err != 0){
+        return err;
+    }

The tools style says there should be spaces around the parentheses, and that single-line blocks don't need braces; i.e.:

if (err != 0)

    return err;

tools/golang/xenlight/test/dominfo/dominfo.c line 19

Quoted 3 lines
tools/golang/xenlight/test/dominfo/dominfo.c new file +3 -0
+	printf("%d\n%d\n", info.domid, info.ssidref);
+	printf("%s\n%s\n%s\n%s\n%s\n%s\n", bool_to_string(info.running), bool_to_string(info.blocked ), bool_to_string(info.paused ),bool_to_string(info.shutdown ),bool_to_string(info.dying), bool_to_string(info.never_stop));
+	long cpu_time = info.cpu_time/((long)1<<35);

Why are we clipping this value?

tools/golang/xenlight/test/dominfo/dominfo.c line 21

Quoted 2 lines
tools/golang/xenlight/test/dominfo/dominfo.c new file +2 -0
+	printf("%d\n%lu\n%lu\n%lu\n%lu\n%lu\n%lu\n%d\n%d\n%d\n", info.shutdown_reason, info.outstanding_memkb, info.current_memkb, info.shared_memkb, info.paged_memkb, info.max_memkb, cpu_time, info.vcpu_max_id, info.vcpu_online, info.cpupool);
+	printf("%d\n", info.domain_type);

These lines need to be wrapped to 80 columns or less.

Quoted 14 lines
tools/golang/xenlight/test/dominfo/dominfo.c new file +8 -0
+
+
+    libxl_ctx_free(context);
+
+}
+char * bool_to_string(bool b){
+    return b ? "true": "false";
+}
tools/golang/xenlight/test/dominfo/dominfo.go new file +0 -0
new file mode 100644
index 0000000..e6c6a64

tools/golang/xenlight/test/dominfo/dominfo.go line 9

Quoted 9 lines
tools/golang/xenlight/test/dominfo/dominfo.go new file +9 -0
@@ -0,0 +1,48 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"

This stanza isn't necessary -- all that should be hidden behind the package. (Same for all the other .go files.)

tools/golang/xenlight/test/dominfo/dominfo.go line 17

Quoted 8 lines
tools/golang/xenlight/test/dominfo/dominfo.go new file +8 -0
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+
+func main() {
+	ctx := xenlight.Ctx

I think this should probably be "var ctx Xenlight.Context". Copying a struct which has a pointer in it is usually a bad idea; if you're going to use your own copy, you should just use your own copy. :-) (Same for all the other files.)

I also think we want a newline here.

tools/golang/xenlight/test/dominfo/dominfo.go line 19

Quoted 2 lines
tools/golang/xenlight/test/dominfo/dominfo.go new file +2 -0
+	err := ctx.Open()
+	if err != nil{

Run 'go fmt' to put spaces where they need to be. (Same for all the other files.)

tools/golang/xenlight/test/dominfo/dominfo.go line 22

Quoted 3 lines
tools/golang/xenlight/test/dominfo/dominfo.go new file +3 -0
+		os.Exit(-1)
+	}
+	defer ctx.Close()

Newline.

tools/golang/xenlight/test/dominfo/dominfo.go line 29

Quoted 7 lines
tools/golang/xenlight/test/dominfo/dominfo.go new file +7 -0
+	info, err:= ctx.DomainInfo(0)
+	if err != nil{
+		os.Exit(-1)
+	}
+
+	fmt.Printf("%d\n%d\n", info.Domid, info.Ssidref)
+	//fmt.Printf("%s\n", info.SsidLabel)

Why are we not printing this (both here and in the .c file)?

tools/golang/xenlight/test/dominfo/dominfo.go line 30

Quoted 1 line
tools/golang/xenlight/test/dominfo/dominfo.go new file +1 -0
+	fmt.Printf("%t\n%t\n%t\n%t\n%t\n%t\n", info.Running, info.Blocked, info.Paused, info.Shutdown, info.Dying, info.NeverStop)

These lines should be wrapped to 80 characters.

Quoted 6 lines
tools/golang/xenlight/test/dominfo/dominfo.go new file +6 -0
+	cpuTime := info.CpuTime/(1<<35)
+	fmt.Printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n", info.ShutdownReason, info.OutstandingMemkb, info.CurrentMemkb, info.SharedMemkb, info.PagedMemkb, info.MaxMemkb, cpuTime, info.VcpuMaxId, info.VcpuOnline, info.Cpupool)
+	fmt.Printf("%d\n", info.DomainType)
+
+}
+

tools/golang/xenlight/test/dominfo/dominfo.go line 46

Quoted 10 lines
tools/golang/xenlight/test/dominfo/dominfo.go new file +10 -0
+func replaceDomainType(n int32)(ret string){
+	switch n{
+	case -1:
+		ret = "invalid"
+	case 1:
+		ret = "hvm"
+	case 2:
+		ret = "pv"
+	}
+	return

You're not using this function; it should be removed.

But if you implement the xenlight.DomainType enum in the previous patch, and the String() method which calls libxl_domain_type_to_string(), you can use "%v" to print the value of DomainType here. (Of course you'll need to call libxl_domain_type_to_string() in the .c file too.)

Quoted 9 lines
tools/golang/xenlight/test/freememory/freememory.c new file +3 -0
new file mode 100644
index 0000000..9ff8444
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>

tools/golang/xenlight/test/freememory/freememory.c line 13

Quoted 10 lines
tools/golang/xenlight/test/freememory/freememory.c new file +10 -0
+
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_physinfo info ;
+    int err= libxl_get_physinfo(context,&info);
+    if(err != 0){
+        return err;
+    }

I don't think you actually want to call physinfo here. :-) (Same for the other files.)

tools/golang/xenlight/test/freememory/freememory.c line 21

Quoted 8 lines
tools/golang/xenlight/test/freememory/freememory.c new file +8 -0
+
+   uint64_t free_memory;
+    err = libxl_get_free_memory(context, &free_memory);
+    if(err < 0){
+        printf("%d\n", err);
+    }else{
+        printf("%lu\n", free_memory);
+    }

Indents, spaces, and unnecessary parentheses.

Quoted 25 lines
tools/golang/xenlight/test/freememory/freememory.c new file +4 -0
+    libxl_ctx_free(context);
+
+}
+
tools/golang/xenlight/test/freememory/freememory.go new file +15 -0
new file mode 100644
index 0000000..19501e7
@@ -0,0 +1,35 @@
+
+package main
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+import (
+	"fmt"
+	"xenproject.org/xenlight"
+	"os"
+)
+

tools/golang/xenlight/test/freememory/freememory.go line 25

Quoted 10 lines
tools/golang/xenlight/test/freememory/freememory.go new file +10 -0
+func main() {
+	ctx := xenlight.Ctx
+	err := ctx.Open()
+	if err != nil{
+		os.Exit(-1)
+	}
+	defer ctx.Close()
+	if err != nil{
+		os.Exit(-1)
+	}

I don't think we need to check the error twice. :-) (Same for the other files.)

Quoted 10 lines
tools/golang/xenlight/test/freememory/freememory.go new file +10 -0
+
+
+	free_memory, err := ctx.GetFreeMemory()
+	if err != nil{
+		fmt.Printf("%d\n", err)
+	}else{
+		fmt.Printf("%d\n", free_memory)
+	}
+
+}

[snip]

Quoted 17 lines
tools/golang/xenlight/test/physinfo/physinfo.c new file +11 -0
new file mode 100644
index 0000000..92c8f47
@@ -0,0 +1,31 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+char *bool_to_string(bool b);
+int main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_physinfo info ;
+    int err= libxl_get_physinfo(context,&info);

tools/golang/xenlight/test/physinfo/physinfo.c line 21

Quoted 10 lines
tools/golang/xenlight/test/physinfo/physinfo.c new file +10 -0
+    if(err != 0){
+        return err;
+    }
+
+    printf("%d\n%d\n%d\n%d\n%d\n", info.threads_per_core, info.cores_per_socket, info.max_cpu_id, info.nr_cpus, info.cpu_khz);
+    printf("%lu\n%lu\n%lu\n%lu\n%lu\n%lu\n", info.total_pages, info.free_pages, info.scrub_pages, info.outstanding_pages, info.sharing_freed_pages, info.sharing_used_frames);
+    printf("%u\n",info.nr_nodes);
+    printf("%s\n%s\n", bool_to_string(info.cap_hvm), bool_to_string(info.cap_hvm_directio));
+
+    for(int i = 0; i < 8; i++){

So with the Xen-standard CFLAGS (which is what you'll get if you use the makefile attached), this fails. You should declare the int up above.

[snip]

Quoted 6 lines
tools/golang/xenlight/test/versioninfo/versioninfo.c new file +0 -0
new file mode 100644
index 0000000..f40495d

tools/golang/xenlight/test/versioninfo/versioninfo.c line 9

Quoted 9 lines
tools/golang/xenlight/test/versioninfo/versioninfo.c new file +9 -0
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <libxl.h>
+
+main(){
+
+    libxl_ctx *context;
+    libxl_ctx_alloc(&context,LIBXL_VERSION, 0, NULL);
+    libxl_version_info* info = libxl_get_version_info(context);

With the Xen-supplied CFLAGS this fails because the pointer isn't declared const.

Like I said, lots of little style points, but overall looks very good, thanks!

-George

XEN_ROOT = $(CURDIR)/../../../../..
include $(XEN_ROOT)/tools/Rules.mk

GO ?= go

TESTS = dominfo freememory maxcpu onlinecpu physinfo versioninfo CBINARIES = $(TESTS:%=%-c)
GOBINARIES = $(TESTS:%=%-go)

all: build

test: clean build

	for test in $(TESTS) ; do \
		./$$test-c >> c.output ; \
	        ./$$test-go >> go.output ; \
		if cmp -s "c.output" "go.output"; then\
			echo "$$test PASSED";\
		else \
			echo "$$test FAILED";\
		fi ; \
	done

build: $(CBINARIES) $(GOBINARIES)

%-c: %.c

	gcc $(CFLAGS_libxenlight) -o $@ $< $(LDLIBS_libxenlight)

%-go: %.go

	GOPATH=$(XEN_ROOT)/tools/golang $(GO) build -o $@ $<

clean:

	rm -f *-c
	rm -f *-go
	rm -f *.output

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

#11 lore raw

RFC PATCH 6/8 golang/xenlight: Implement libxl_bitmap and helper operations

In reply to Ronald Rojas

Implement Bitmap type, along with helper functions.

The Bitmap type is implemented interllay in a way which makes it easy to copy into and out of the C libxl_bitmap type.

---
 tools/golang/xenlight/xenlight.go | 166 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 166 insertions(+)
tools/golang/xenlight/xenlight.go +166 -0
index dd6893c..54692fd 100644
@@ -131,6 +131,19 @@ func (chwcap C.libxl_hwcap)CToGo() (ghwcap Hwcap) {
 	return
 }
 
+// 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
+// a C function a pointer to a Go-allocated structure which contains a
+// pointer.
+type Bitmap struct {
+	bitmap []C.uint8_t
+}
 
 /*
  * Types: IDL
@@ -199,6 +212,159 @@ type Dominfo struct {
 }
 
 /*
+ * Bitmap operations
+ */
+
+// Return a Go bitmap which is a copy of the referred C bitmap.
+func (cbm C.libxl_bitmap) CToGo() (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)GoToC() (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 {
+		return false
+	}
+
+	return (bm.bitmap[bit/8] & (1 << (ubit & 7))) != 0
+}
+
+func (bm *Bitmap) Set(bit int) {
+	ibit := bit / 8
+	if ibit+1 > len(bm.bitmap) {
+		bm.bitmap = append(bm.bitmap, make([]C.uint8_t, ibit+1-len(bm.bitmap))...)
+	}
+
+	bm.bitmap[ibit] |= 1 << (uint(bit) & 7)
+}
+
+func (bm *Bitmap) SetRange(start int, end int) {
+	for i := start; i <= end; i++ {
+		bm.Set(i)
+	}
+}
+
+func (bm *Bitmap) Clear(bit int) {
+	ubit := uint(bit)
+	if bit > bm.Max() || bm.bitmap == nil {
+		return
+	}
+
+	bm.bitmap[bit/8] &= ^(1 << (ubit & 7))
+}
+
+func (bm *Bitmap) ClearRange(start int, end int) {
+	for i := start; i <= end; i++ {
+		bm.Clear(i)
+	}
+}
+
+func (bm *Bitmap) Max() int {
+	return len(bm.bitmap)*8 - 1
+}
+
+func (bm *Bitmap) IsEmpty() bool {
+	for i := 0; i < len(bm.bitmap); i++ {
+		if bm.bitmap[i] != 0 {
+			return false
+		}
+	}
+	return true
+}
+
+func (a Bitmap) And(b Bitmap) (c Bitmap) {
+	var max, min int
+	if len(a.bitmap) > len(b.bitmap) {
+		max = len(a.bitmap)
+		min = len(b.bitmap)
+	} else {
+		max = len(b.bitmap)
+		min = len(a.bitmap)
+	}
+	c.bitmap = make([]C.uint8_t, max)
+
+	for i := 0; i < min; i++ {
+		c.bitmap[i] = a.bitmap[i] & b.bitmap[i]
+	}
+	return
+}
+
+func (bm Bitmap) String() (s string) {
+	lastOnline := false
+	crange := false
+	printed := false
+	var i int
+	/// --x-xxxxx-x -> 2,4-8,10
+	/// --x-xxxxxxx -> 2,4-10
+	for i = 0; i <= bm.Max(); i++ {
+		if bm.Test(i) {
+			if !lastOnline {
+				// Switching offline -> online, print this cpu
+				if printed {
+					s += ","
+				}
+				s += fmt.Sprintf("%d", i)
+				printed = true
+			} else if !crange {
+				// last was online, but we're not in a range; print -
+				crange = true
+				s += "-"
+			} else {
+				// last was online, we're in a range,  nothing else to do
+			}
+			lastOnline = true
+		} else {
+			if lastOnline {
+				// Switching online->offline; do we need to end a range?
+				if crange {
+					s += fmt.Sprintf("%d", i-1)
+				}
+			}
+			lastOnline = false
+			crange = false
+		}
+	}
+	if lastOnline {
+		// Switching online->offline; do we need to end a range?
+		if crange {
+			s += fmt.Sprintf("%d", i-1)
+		}
+	}
+
+	return
+}
+
+/*
  * Context
  */
 var Ctx Context
Signature · 7 lines
2.7.4


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

Re: [PATCH RFC 6/8] golang/xenlight: Implement libxl_bitmap and helper operations

Quoted 36 lines · Ronald Rojas

Implement Bitmap type, along with helper functions.

The Bitmap type is implemented interllay in a way which makes it easy to copy into and out of the C libxl_bitmap type.

---
 tools/golang/xenlight/xenlight.go | 166 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 166 insertions(+)
tools/golang/xenlight/xenlight.go +13 -0
index dd6893c..54692fd 100644
@@ -131,6 +131,19 @@ func (chwcap C.libxl_hwcap)CToGo() (ghwcap Hwcap) {
 	return
 }
 
+// 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
+// a C function a pointer to a Go-allocated structure which contains a
+// pointer.
+type Bitmap struct {
+	bitmap []C.uint8_t
+}
 
 /*
  * Types: IDL

tools/golang/xenlight/xenlight.go type Dominfo struct { · line 219

Quoted 8 lines
tools/golang/xenlight/xenlight.go +5 -0
@@ -199,6 +212,159 @@ type Dominfo struct {
 }
 
 /*
+ * Bitmap operations
+ */
+
+// Return a Go bitmap which is a copy of the referred C bitmap.
+func (cbm C.libxl_bitmap) CToGo() (gbm Bitmap) {

Probably just toGo()...

Quoted 5 lines
tools/golang/xenlight/xenlight.go +5 -0
+	// 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

tools/golang/xenlight/xenlight.go type Dominfo struct { · line 234

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+	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)GoToC() (cbm C.libxl_bitmap) {

...and toC (lower case so they're not visible outside the package).

Also 'go fmt'.

tools/golang/xenlight/xenlight.go type Dominfo struct { · line 241

Quoted 7 lines
tools/golang/xenlight/xenlight.go +7 -0
+	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!")

malloc (This is probably my typo).

Other than that, looks good.

-George

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

#13 lore raw

RFC PATCH 7/8 golang/xenlight: Implement libxl_scheduler enumeration

In reply to Ronald Rojas

Include both constants and a Stringification for libxl_scheduler.

---
 tools/golang/xenlight/xenlight.go | 62 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
tools/golang/xenlight/xenlight.go +62 -0
index 54692fd..0990f07 100644
@@ -211,6 +211,68 @@ type Dominfo struct {
 
 }
 
+// # Consistent with values defined in domctl.h
+// # Except unknown which we have made up
+// libxl_scheduler = Enumeration("scheduler", [
+//     (0, "unknown"),
+//     (4, "sedf"),
+//     (5, "credit"),
+//     (6, "credit2"),
+//     (7, "arinc653"),
+//     (8, "rtds"),
+//     ])
+type Scheduler int
+
+var (
+	SchedulerUnknown  Scheduler = C.LIBXL_SCHEDULER_UNKNOWN
+	SchedulerSedf     Scheduler = C.LIBXL_SCHEDULER_SEDF
+	SchedulerCredit   Scheduler = C.LIBXL_SCHEDULER_CREDIT
+	SchedulerCredit2  Scheduler = C.LIBXL_SCHEDULER_CREDIT2
+	SchedulerArinc653 Scheduler = C.LIBXL_SCHEDULER_ARINC653
+	SchedulerRTDS     Scheduler = C.LIBXL_SCHEDULER_RTDS
+)
+
+// const char *libxl_scheduler_to_string(libxl_scheduler p);
+func (s Scheduler) String() string {
+	cs := C.libxl_scheduler_to_string(C.libxl_scheduler(s))
+	// No need to free const return value
+
+	return C.GoString(cs)
+}
+
+// int libxl_scheduler_from_string(const char *s, libxl_scheduler *e);
+func (s *Scheduler) FromString(gstr string) (err error) {
+	cstr := C.CString(gstr)
+	defer C.free(unsafe.Pointer(cstr))
+
+	var cs C.libxl_scheduler
+	ret := C.libxl_scheduler_from_string(cstr, &cs)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	*s = Scheduler(cs)
+	return
+}
+
+func SchedulerFromString(name string) (s Scheduler, err error) {
+	cname := C.CString(name)
+	defer C.free(unsafe.Pointer(cname))
+
+	var cs C.libxl_scheduler
+
+	ret := C.libxl_scheduler_from_string(cname, &cs)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	s = Scheduler(cs)
+
+	return
+}
+
 /*
  * Bitmap operations
  */
Signature · 7 lines
2.7.4


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

Re: [PATCH RFC 7/8] golang/xenlight: Implement libxl_scheduler enumeration

Quoted 67 lines · Ronald Rojas

Include both constants and a Stringification for libxl_scheduler.

---
 tools/golang/xenlight/xenlight.go | 62 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
tools/golang/xenlight/xenlight.go +51 -0
index 54692fd..0990f07 100644
@@ -211,6 +211,68 @@ type Dominfo struct {
 
 }
 
+// # Consistent with values defined in domctl.h
+// # Except unknown which we have made up
+// libxl_scheduler = Enumeration("scheduler", [
+//     (0, "unknown"),
+//     (4, "sedf"),
+//     (5, "credit"),
+//     (6, "credit2"),
+//     (7, "arinc653"),
+//     (8, "rtds"),
+//     ])
+type Scheduler int
+
+var (
+	SchedulerUnknown  Scheduler = C.LIBXL_SCHEDULER_UNKNOWN
+	SchedulerSedf     Scheduler = C.LIBXL_SCHEDULER_SEDF
+	SchedulerCredit   Scheduler = C.LIBXL_SCHEDULER_CREDIT
+	SchedulerCredit2  Scheduler = C.LIBXL_SCHEDULER_CREDIT2
+	SchedulerArinc653 Scheduler = C.LIBXL_SCHEDULER_ARINC653
+	SchedulerRTDS     Scheduler = C.LIBXL_SCHEDULER_RTDS
+)
+
+// const char *libxl_scheduler_to_string(libxl_scheduler p);
+func (s Scheduler) String() string {
+	cs := C.libxl_scheduler_to_string(C.libxl_scheduler(s))
+	// No need to free const return value
+
+	return C.GoString(cs)
+}
+
+// int libxl_scheduler_from_string(const char *s, libxl_scheduler *e);
+func (s *Scheduler) FromString(gstr string) (err error) {
+	cstr := C.CString(gstr)
+	defer C.free(unsafe.Pointer(cstr))
+
+	var cs C.libxl_scheduler
+	ret := C.libxl_scheduler_from_string(cstr, &cs)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	*s = Scheduler(cs)
+	return
+}
+
+func SchedulerFromString(name string) (s Scheduler, err error) {
+	cname := C.CString(name)
+	defer C.free(unsafe.Pointer(cname))
+
+	var cs C.libxl_scheduler
+

tools/golang/xenlight/xenlight.go type Dominfo struct { · line 274

Quoted 10 lines
tools/golang/xenlight/xenlight.go +10 -0
+	ret := C.libxl_scheduler_from_string(cname, &cs)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	s = Scheduler(cs)
+
+	return
+}

I don't think we need both of these functions. In my code I just used the method one (FromString). What do you think?

BTW this file is an obvious template for the enumerations you need for DomInfo. :-)

-George

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

#15 lore raw

RFC PATCH 8/8 golang/xenlight: Implement cpupool operations

In reply to Ronald Rojas

Include some useful "Utility" functions:
- CpupoolFindByName
- CpupoolMakeFree

Still need to implement the following functions:
- libxl_cpupool_rename
- libxl_cpupool_cpuadd_node
- libxl_cpupool_cpuremove_node
- libxl_cpupool_movedomain

---
 tools/golang/xenlight/xenlight.go | 238 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 238 insertions(+)
tools/golang/xenlight/xenlight.go +238 -0
index 0990f07..c856284 100644
@@ -273,6 +273,244 @@ func SchedulerFromString(name string) (s Scheduler, err error) {
 	return
 }
 
+
+// libxl_cpupoolinfo = Struct("cpupoolinfo", [
+//     ("poolid",      uint32),
+//     ("pool_name",   string),
+//     ("sched",       libxl_scheduler),
+//     ("n_dom",       uint32),
+//     ("cpumap",      libxl_bitmap)
+//     ], dir=DIR_OUT)
+
+type CpupoolInfo struct {
+	Poolid      uint32
+	PoolName    string
+	Scheduler   Scheduler
+	DomainCount int
+	Cpumap      Bitmap
+}
+
+func (cci C.libxl_cpupoolinfo)CToGo ()(gci CpupoolInfo) {
+	gci.Poolid = uint32(cci.poolid)
+	gci.PoolName = C.GoString(cci.pool_name)
+	gci.Scheduler = Scheduler(cci.sched)
+	gci.DomainCount = int(cci.n_dom)
+	gci.Cpumap = cci.cpumap.CToGo()
+
+	return
+}
+
+// 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)
+
+	defer C.libxl_cpupoolinfo_list_free(c_cpupool_list, nbPool)
+
+	if int(nbPool) == 0 {
+		return
+	}
+
+	// Magic
+	cpupoolListSlice := (*[1 << 30]C.libxl_cpupoolinfo)(unsafe.Pointer(c_cpupool_list))[:nbPool:nbPool]
+
+	for i := range cpupoolListSlice {
+		info := cpupoolListSlice[i].CToGo()
+
+		list = append(list, info)
+	}
+
+	return
+}
+
+// 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)
+		return
+	}
+	defer C.libxl_cpupoolinfo_dispose(&c_cpupool)
+
+	pool = c_cpupool.CToGo()
+
+	return
+}
+
+// int libxl_cpupool_create(libxl_ctx *ctx, const char *name,
+//                          libxl_scheduler sched,
+//                          libxl_bitmap cpumap, libxl_uuid *uuid,
+//                          uint32_t *poolid);
+// 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(0)
+	name := C.CString(Name)
+	defer C.free(unsafe.Pointer(name))
+
+	// For now, just do what xl does, and make a new uuid every time we create the pool
+	var uuid C.libxl_uuid
+	C.libxl_uuid_generate(&uuid)
+
+	cbm := Cpumap.GoToC()
+	defer C.libxl_bitmap_dispose(&cbm)
+
+	ret := C.libxl_cpupool_create(Ctx.ctx, name, C.libxl_scheduler(Scheduler),
+		cbm, &uuid, &poolid)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	Poolid = uint32(poolid)
+
+	return
+}
+
+// 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)
+		return
+	}
+
+	return
+}
+
+// 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)
+		return
+	}
+
+	return
+}
+
+// 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 := Cpumap.GoToC()
+	defer C.libxl_bitmap_dispose(&cbm)
+
+	ret := C.libxl_cpupool_cpuadd_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	return
+}
+
+// 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)
+		return
+	}
+
+	return
+}
+
+// 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 := Cpumap.GoToC()
+	defer C.libxl_bitmap_dispose(&cbm)
+
+	ret := C.libxl_cpupool_cpuremove_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm)
+	if ret != 0 {
+		err = Error(-ret)
+		return
+	}
+
+	return
+}
+
+// int libxl_cpupool_rename(libxl_ctx *ctx, const char *name, uint32_t poolid);
+// int libxl_cpupool_cpuadd_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus);
+// int libxl_cpupool_cpuremove_node(libxl_ctx *ctx, uint32_t poolid, int node, int *cpus);
+// int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid);
+
+//
+// Utility functions
+//
+func (Ctx *Context) CpupoolFindByName(name string) (info CpupoolInfo, found bool) {
+	plist := Ctx.ListCpupool()
+
+	for i := range plist {
+		if plist[i].PoolName == name {
+			found = true
+			info = plist[i]
+			return
+		}
+	}
+	return
+}
+
+func (Ctx *Context) CpupoolMakeFree(Cpumap Bitmap) (err error) {
+	plist := Ctx.ListCpupool()
+
+	for i := range plist {
+		var Intersection Bitmap
+		Intersection = Cpumap.And(plist[i].Cpumap)
+		if !Intersection.IsEmpty() {
+			err = Ctx.CpupoolCpuremoveCpumap(plist[i].Poolid, Intersection)
+			if err != nil {
+				return
+			}
+		}
+	}
+	return
+}
+
 /*
  * Bitmap operations
  */
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#16 lore raw
In reply to Ronald Rojas

On Mon, Jan 23, 2017 at 11:43:30AM -0500, Ronald Rojas wrote:
[...]

tools/Makefile subdir-clean-qemu-xen-dir: · line 322

Quoted 4 lines
tools/Makefile +4 -0
+
+subdir-distclean-firmware: .phony
+	$(MAKE) -C firmware distclean
+

This looks unrelated.

Quoted 54 lines

subdir-clean-debugger/gdbsx subdir-distclean-debugger/gdbsx: .phony

tools/Makefile +0 -0
 	$(MAKE) -C debugger/gdbsx clean
tools/golang/xenlight/Makefile new file +31 -0
new file mode 100644
index 0000000..1c2a2b7
@@ -0,0 +1,31 @@
+XEN_ROOT=$(CURDIR)/../../..
+GOLANG_SRC=$(GOPATH)/src/xenproject.org/xenlight
+CGO_CFLAGS = -I$(DESTDIR)$(includedir)
+CGO_LDFLAGS = -L$(DESTDIR)$(libdir) -Wl,-rpath-link=$(DESTDIR)$(libdir)
+include $(XEN_ROOT)/tools/Rules.mk
+
+BINARY = xenlight.a
+GO ?= go
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build: xenlight.a
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(GOLANG_SRC)
+	$(INSTALL_DATA) xenlight.go $(DESTDIR)$(GOLANG_SRC)
+
+.PHONY: clean
+clean:
+	$(RM) $(BINARY)
+
+.PHONY: distclean
+distclean: clean
+
+xenlight.a: xenlight.go
+	CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o $@ $<
+
+-include $(DEPS)
tools/golang/xenlight/xenlight.go new file +8 -0
new file mode 100644
index 0000000..f82e14e
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 George W. Dunlap, Citrix Systems UK Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of the
+ * License only.
+ *

tools/golang/xenlight/xenlight.go line 18

Quoted 10 lines
tools/golang/xenlight/xenlight.go new file +10 -0
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */

My impression is that we prefer LGPL for libraries -- but of course it is up to the author to decide what license to use. :-)

Wei.

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

#17 lore raw
Quoted 8 lines · Wei Liu

On Mon, Jan 23, 2017 at 11:43:30AM -0500, Ronald Rojas wrote:
[...]

tools/Makefile subdir-clean-qemu-xen-dir: · line 322

Quoted 4 lines
tools/Makefile +4 -0
+
+subdir-distclean-firmware: .phony
+	$(MAKE) -C firmware distclean
+

This looks unrelated.

You're correct. Thanks for catching the mistake.

Quoted 60 lines
Quoted 60 lines

subdir-clean-debugger/gdbsx subdir-distclean-debugger/gdbsx: .phony

tools/Makefile +0 -0
 	$(MAKE) -C debugger/gdbsx clean
tools/golang/xenlight/Makefile new file +31 -0
new file mode 100644
index 0000000..1c2a2b7
@@ -0,0 +1,31 @@
+XEN_ROOT=$(CURDIR)/../../..
+GOLANG_SRC=$(GOPATH)/src/xenproject.org/xenlight
+CGO_CFLAGS = -I$(DESTDIR)$(includedir)
+CGO_LDFLAGS = -L$(DESTDIR)$(libdir) -Wl,-rpath-link=$(DESTDIR)$(libdir)
+include $(XEN_ROOT)/tools/Rules.mk
+
+BINARY = xenlight.a
+GO ?= go
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build: xenlight.a
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(GOLANG_SRC)
+	$(INSTALL_DATA) xenlight.go $(DESTDIR)$(GOLANG_SRC)
+
+.PHONY: clean
+clean:
+	$(RM) $(BINARY)
+
+.PHONY: distclean
+distclean: clean
+
+xenlight.a: xenlight.go
+	CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o $@ $<
+
+-include $(DEPS)
tools/golang/xenlight/xenlight.go new file +13 -0
new file mode 100644
index 0000000..f82e14e
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 George W. Dunlap, Citrix Systems UK Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of the
+ * License only.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
Quoted 8 lines

tools/golang/xenlight/xenlight.go line 18

Quoted 5 lines
tools/golang/xenlight/xenlight.go new file +5 -0
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */

My impression is that we prefer LGPL for libraries -- but of course it is up to the author to decide what license to use. :-)

Why the preferance for LGPL? I'm not too familiar with the differences between the two.

Quoted 2 lines

Wei.

Thanks,
Ronald

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

#18 lore raw
In reply to Ronald Rojas

tools/golang/xenlight/xenlight.go line 63

Quoted 6 lines · Ronald Rojas
tools/golang/xenlight/xenlight.go new file +6 -0
+func (Ctx *Context) Open() (err error) {
+       if Ctx.ctx != nil {
+               return
+       }
+
+       ret := C.libxl_ctx_alloc(unsafe.Pointer(&Ctx.ctx), C.LIBXL_VERSION, 0, nil)

Just discovered there's a bug here (in code that I wrote obviously) -- you can't pass the last argument as nil; you have to include a logger. Otherwise if you ever get an error you'll get a NULL dereference. :-)

I think the fastest thing to do to begin with would be to create a "null" logger that just throws away all the data. Then at some point (possibly not in your internship) someone can think about what a proper logging interface looks like.

-George

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

#19 lore raw
Quoted 13 lines · George Dunlap

tools/golang/xenlight/xenlight.go line 63

Quoted 6 lines · Ronald Rojas
tools/golang/xenlight/xenlight.go new file +6 -0
+func (Ctx *Context) Open() (err error) {
+       if Ctx.ctx != nil {
+               return
+       }
+
+       ret := C.libxl_ctx_alloc(unsafe.Pointer(&Ctx.ctx), C.LIBXL_VERSION, 0, nil)

Just discovered there's a bug here (in code that I wrote obviously) -- you can't pass the last argument as nil; you have to include a logger. Otherwise if you ever get an error you'll get a NULL dereference. :-)

Quoted 4 lines

I think the fastest thing to do to begin with would be to create a "null" logger that just throws away all the data. Then at some point (possibly not in your internship) someone can think about what a proper logging interface looks like.

Makes sense. I can modify the code so that it creates an instance of the xenlogger and prints it to /dev/null. I will add this in the next version of the patch.

Quoted 2 lines

-George

Thanks,
Ronald

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

#20 lore raw
In reply to George Dunlap

George Dunlap writes ("Re: [Xen-devel] [PATCH RFC 1/8] golang/xenlight: Create stub package"):

Quoted 4 lines

I think the fastest thing to do to begin with would be to create a "null" logger that just throws away all the data. Then at some point (possibly not in your internship) someone can think about what a proper logging interface looks like.

Please don't just throw away all the messages. For now, have your C code make the stdiostream logger to send the messages to stderr.

Ian.

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

#21 lore raw
Quoted 8 lines · Ian Jackson

George Dunlap writes ("Re: [Xen-devel] [PATCH RFC 1/8] golang/xenlight: Create stub package"):

Quoted 4 lines

I think the fastest thing to do to begin with would be to create a "null" logger that just throws away all the data. Then at some point (possibly not in your internship) someone can think about what a proper logging interface looks like.

Please don't just throw away all the messages. For now, have your C code make the stdiostream logger to send the messages to stderr.

After I wrote this I also came to the conclusion that it would be pretty important to have the messages available for debugging purposes.

My main concern here was that I wasn't sure how glibc's stdiostream would interact with Go's iostream: They might have different sets of buffers, for instance, which would cause garbled output. But it looks like Go by default doesn't buffer stdout/stderr; and the stdiostream logger flushes output after each message as well. So using stdiostream sounds like a good starting point (although it will need to be replaced before we consider the library production-ready).

Ronald, you can look in tools/libxl/xl.c to see an example of how to make an stdiostream logger to pass to libxl_ctx_alloc().

-George

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

#22 lore raw
In reply to Ronald Rojas
Quoted 8 lines · Ronald Rojas

Create a basic Makefile to build and install libxenlight Golang bindings. Also add a stub package which only opens libxl context.

Include a global xenlight.Ctx variable which can be used as the default context by the entire program if desired.

For now, return simple errors. Proper error handling will be added in next patch.

So this is not true at the moment; and as a result the patch doesn't build.

As I said in response to Dario's suggestion, I think in this chicken-and-egg situation it makes sense to just use fmt.Errorf() for this patch, and then change them to Error(-ret) in the next patch.

Quoted 15 lines
---
 tools/Makefile                    | 17 ++++++++
 tools/golang/xenlight/Makefile    | 31 ++++++++++++++
 tools/golang/xenlight/xenlight.go | 86 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+)
 create mode 100644 tools/golang/xenlight/Makefile
 create mode 100644 tools/golang/xenlight/xenlight.go
tools/Makefile +0 -0
index 77e0723..c1e975f 100644

tools/Makefile SUBDIRS-y += xenstore · line 15

Quoted 5 lines
tools/Makefile +2 -0
@@ -11,6 +11,8 @@ SUBDIRS-y += xenstore
 SUBDIRS-y += misc
 SUBDIRS-y += examples
 SUBDIRS-y += hotplug
+#Uncomment line to build Golang libxl
+#SUBDIRS-y += golang/xenlight

This won't build from a clean tree, because at this point libxl hasn't been built yet; and if you modify libxl and re-build, it will use the old version of libxl rather than the new one you just modified. It needs to go a bit further down, under "SUBDIRS-y += libxl".

(Actually this whole list could use some reordering, but that's not your problem.)

Quoted 31 lines
tools/Makefile +15 -0
 SUBDIRS-y += xentrace
 SUBDIRS-$(CONFIG_XCUTILS) += xcutils
 SUBDIRS-$(CONFIG_X86) += firmware
@@ -303,6 +305,21 @@ subdir-clean-qemu-xen-dir:
 		$(MAKE) -C qemu-xen-dir clean; \
 	fi
 
+subdir-clean-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight clean
+
+subdir-distclean-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight distclean
+
+subdir-install-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight install
+
+subdir-all-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight all
+
+subdir-distclean-firmware: .phony
+	$(MAKE) -C firmware distclean
+
 subdir-clean-debugger/gdbsx subdir-distclean-debugger/gdbsx: .phony
 	$(MAKE) -C debugger/gdbsx clean
 
tools/golang/xenlight/Makefile new file +0 -0
new file mode 100644
index 0000000..1c2a2b7

tools/golang/xenlight/Makefile line 2

Quoted 2 lines
tools/golang/xenlight/Makefile new file +2 -0
@@ -0,0 +1,31 @@
+XEN_ROOT=$(CURDIR)/../../..
+GOLANG_SRC=$(GOPATH)/src/xenproject.org/xenlight

So GOPATH is actually like the PATH variable: you can have several directories in it.

I think this should probably be:

GOCODEDIR ?= $(prefix)/share/cogode GOLANG_SRC = $(GOCODEDIR)/src/xenproject.org/xenlight

Other than that, looks good -- I think with those three changes, this patch should be ready to go in. :-)

-George

Quoted 113 lines
tools/golang/xenlight/Makefile new file +29 -0
+CGO_CFLAGS = -I$(DESTDIR)$(includedir)
+CGO_LDFLAGS = -L$(DESTDIR)$(libdir) -Wl,-rpath-link=$(DESTDIR)$(libdir)
+include $(XEN_ROOT)/tools/Rules.mk
+
+BINARY = xenlight.a
+GO ?= go
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build: xenlight.a
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(GOLANG_SRC)
+	$(INSTALL_DATA) xenlight.go $(DESTDIR)$(GOLANG_SRC)
+
+.PHONY: clean
+clean:
+	$(RM) $(BINARY)
+
+.PHONY: distclean
+distclean: clean
+
+xenlight.a: xenlight.go
+	CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o $@ $<
+
+-include $(DEPS)
tools/golang/xenlight/xenlight.go new file +77 -0
new file mode 100644
index 0000000..f82e14e
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 George W. Dunlap, Citrix Systems UK Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of the
+ * License only.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+package xenlight
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+
+/*
+ * Other flags that may be needed at some point:
+ *  -lnl-route-3 -lnl-3
+ *
+ * To get back to static linking:
+ * #cgo LDFLAGS: -lxenlight -lyajl_s -lxengnttab -lxenstore -lxenguest -lxentoollog -lxenevtchn -lxenctrl -lblktapctl -lxenforeignmemory -lxencall -lz -luuid -lutil
+*/
+
+import (
+	"fmt"
+	"unsafe"
+)
+
+
+/*
+ * Types: Builtins
+ */
+type Context struct {
+	ctx *C.libxl_ctx
+}
+
+/*
+ * Context
+ */
+var Ctx Context
+
+func (Ctx *Context) IsOpen() bool {
+	return Ctx.ctx != nil
+}
+
+func (Ctx *Context) Open() (err error) {
+	if Ctx.ctx != nil {
+		return
+	}
+
+	ret := C.libxl_ctx_alloc(unsafe.Pointer(&Ctx.ctx), C.LIBXL_VERSION, 0, nil)
+
+	if ret != 0 {
+		err = Error(-ret)
+	}
+	return
+}
+
+func (Ctx *Context) Close() (err error) {
+	ret := C.libxl_ctx_free(unsafe.Pointer(Ctx.ctx))
+	Ctx.ctx = nil
+
+	if ret != 0 {
+		err = Error(-ret)
+	}

tools/golang/xenlight/xenlight.go line 86

Quoted 10 lines
tools/golang/xenlight/xenlight.go new file +9 -0
+	return
+}
+
+func (Ctx *Context) CheckOpen() (err error) {
+	if Ctx.ctx == nil {
+		err = fmt.Errorf("Context not opened")
+	}
+	return
+}
\ No newline at end of file

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

#23 lore raw
In reply to Ronald Rojas
Quoted 10 lines · Ronald Rojas

Create a basic Makefile to build and install libxenlight Golang bindings. Also add a stub package which only opens libxl context.

Include a global xenlight.Ctx variable which can be used as the default context by the entire program if desired.

For now, return simple errors. Proper error handling will be added in next patch.

Actually, I've done a pretty big re-work of how the makefiles work, to make sure that we can always be using stuff that is only in-tree, and not anything that is in our outside environment.

Attached is the result. Once we have this, then in your test makefiles can look like this:

dominfo-go: dominfo.go

	GOPATH=$(XEN_GOPATH) $(GO) build -o $@ $<

And that will use the current in-tree version of the xenlight package.

You can probably tell I've got some comments on your testing patch as well, but that will have to wait until Monday. :-)

Let me know what you think.

Peace,
-George

Quoted 164 lines
---
 tools/Makefile                    | 17 ++++++++
 tools/golang/xenlight/Makefile    | 31 ++++++++++++++
 tools/golang/xenlight/xenlight.go | 86 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 134 insertions(+)
 create mode 100644 tools/golang/xenlight/Makefile
 create mode 100644 tools/golang/xenlight/xenlight.go
tools/Makefile +17 -0
index 77e0723..c1e975f 100644
@@ -11,6 +11,8 @@ SUBDIRS-y += xenstore
 SUBDIRS-y += misc
 SUBDIRS-y += examples
 SUBDIRS-y += hotplug
+#Uncomment line to build Golang libxl
+#SUBDIRS-y += golang/xenlight
 SUBDIRS-y += xentrace
 SUBDIRS-$(CONFIG_XCUTILS) += xcutils
 SUBDIRS-$(CONFIG_X86) += firmware
@@ -303,6 +305,21 @@ subdir-clean-qemu-xen-dir:
 		$(MAKE) -C qemu-xen-dir clean; \
 	fi
 
+subdir-clean-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight clean
+
+subdir-distclean-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight distclean
+
+subdir-install-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight install
+
+subdir-all-golang/xenlight: .phony
+	$(MAKE) -C golang/xenlight all
+
+subdir-distclean-firmware: .phony
+	$(MAKE) -C firmware distclean
+
 subdir-clean-debugger/gdbsx subdir-distclean-debugger/gdbsx: .phony
 	$(MAKE) -C debugger/gdbsx clean
 
tools/golang/xenlight/Makefile new file +31 -0
new file mode 100644
index 0000000..1c2a2b7
@@ -0,0 +1,31 @@
+XEN_ROOT=$(CURDIR)/../../..
+GOLANG_SRC=$(GOPATH)/src/xenproject.org/xenlight
+CGO_CFLAGS = -I$(DESTDIR)$(includedir)
+CGO_LDFLAGS = -L$(DESTDIR)$(libdir) -Wl,-rpath-link=$(DESTDIR)$(libdir)
+include $(XEN_ROOT)/tools/Rules.mk
+
+BINARY = xenlight.a
+GO ?= go
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build: xenlight.a
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(GOLANG_SRC)
+	$(INSTALL_DATA) xenlight.go $(DESTDIR)$(GOLANG_SRC)
+
+.PHONY: clean
+clean:
+	$(RM) $(BINARY)
+
+.PHONY: distclean
+distclean: clean
+
+xenlight.a: xenlight.go
+	CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build -o $@ $<
+
+-include $(DEPS)
tools/golang/xenlight/xenlight.go new file +77 -0
new file mode 100644
index 0000000..f82e14e
@@ -0,0 +1,86 @@
+/*
+ * Copyright (C) 2016 George W. Dunlap, Citrix Systems UK Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2 of the
+ * License only.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+package xenlight
+
+/*
+#cgo LDFLAGS: -lxenlight -lyajl
+#include <stdlib.h>
+#include <libxl.h>
+*/
+import "C"
+
+/*
+ * Other flags that may be needed at some point:
+ *  -lnl-route-3 -lnl-3
+ *
+ * To get back to static linking:
+ * #cgo LDFLAGS: -lxenlight -lyajl_s -lxengnttab -lxenstore -lxenguest -lxentoollog -lxenevtchn -lxenctrl -lblktapctl -lxenforeignmemory -lxencall -lz -luuid -lutil
+*/
+
+import (
+	"fmt"
+	"unsafe"
+)
+
+
+/*
+ * Types: Builtins
+ */
+type Context struct {
+	ctx *C.libxl_ctx
+}
+
+/*
+ * Context
+ */
+var Ctx Context
+
+func (Ctx *Context) IsOpen() bool {
+	return Ctx.ctx != nil
+}
+
+func (Ctx *Context) Open() (err error) {
+	if Ctx.ctx != nil {
+		return
+	}
+
+	ret := C.libxl_ctx_alloc(unsafe.Pointer(&Ctx.ctx), C.LIBXL_VERSION, 0, nil)
+
+	if ret != 0 {
+		err = Error(-ret)
+	}
+	return
+}
+
+func (Ctx *Context) Close() (err error) {
+	ret := C.libxl_ctx_free(unsafe.Pointer(Ctx.ctx))
+	Ctx.ctx = nil
+
+	if ret != 0 {
+		err = Error(-ret)
+	}

tools/golang/xenlight/xenlight.go line 86

Quoted 10 lines
tools/golang/xenlight/xenlight.go new file +9 -0
+	return
+}
+
+func (Ctx *Context) CheckOpen() (err error) {
+	if Ctx.ctx == nil {
+		err = fmt.Errorf("Context not opened")
+	}
+	return
+}
\ No newline at end of file

From 1756fdbdf6abf8ce9b7c83722cc4f91709e93756 Mon Sep 17 00:00:00 2001
From: Ronald Rojas <[email protected]>
Date: Mon, 23 Jan 2017 11:43:30 -0500
Subject: [PATCH] golang/xenlight: Create stub package

Create a basic Makefile to build and install libxenlight Golang bindings. Also add a stub package which only opens libxl context.

Include a global xenlight.Ctx variable which can be used as the default context by the entire program if desired.

For now, return simple errors. Proper error handling will be added in next patch.

---
Changes:

- Moved manual-enable config option to tools/Rules.mk, use CONFIG_GOLANG in tools/Makefile

- Added XEN_GOPATH, pointing to tools/golang

- Modified tools/golang/xenlight makefile to construct necessary $GOPATH

- Added tools/golang/Makefile, so we don't need special rules in tools to make tools/golang/xenlight; and so we have a single place to remove the $GOPATH build side-effects ($GOPATH/src and $GOPATH/pkg)

- Build of tools/golang/xenlight sets $GOPATH and does a 'go install'

- Use tree-provided CFLAGS_libxenlight and LDLIBS_libxenlight, rather than hard-coding our own

- Made a PKGSOURCES variable to track dependencies of all target files which need to be part of the output package (i.e., what's put in $GOPATH/src). At the moment this is one file, but it will probably be more once we start using the IDL.

---
 tools/Makefile                    |  1 +
 tools/Rules.mk                    |  6 +++
 tools/golang/Makefile             | 27 ++++++++++++
 tools/golang/xenlight/Makefile    | 49 ++++++++++++++++++++++
 tools/golang/xenlight/xenlight.go | 86 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 169 insertions(+)
 create mode 100644 tools/golang/Makefile
 create mode 100644 tools/golang/xenlight/Makefile
 create mode 100644 tools/golang/xenlight/xenlight.go
tools/Makefile +1 -0
index 77e0723..df1fda1 100644
@@ -31,6 +31,7 @@ endif
 
 SUBDIRS-y += xenpmd
 SUBDIRS-y += libxl
+SUBDIRS-$(CONFIG_GOLANG) += golang
 SUBDIRS-y += helpers
 SUBDIRS-$(CONFIG_X86) += xenpaging
 SUBDIRS-$(CONFIG_X86) += debugger/gdbsx
tools/Rules.mk +6 -0
index b35999b..24e5220 100644
@@ -30,6 +30,12 @@ XENSTORE_XENSTORED ?= y
 debug ?= y
 debug_symbols ?= $(debug)
 
+# Uncomment to compile with Go
+CONFIG_GOLANG ?= y
+ifeq ($(CONFIG_GOLANG),y)
+XEN_GOPATH        = $(XEN_ROOT)/tools/golang
+endif
+
 ifeq ($(debug_symbols),y)
 CFLAGS += -g3
 endif
tools/golang/Makefile new file +27 -0
new file mode 100644
index 0000000..47a9235
@@ -0,0 +1,27 @@
+XEN_ROOT=$(CURDIR)/../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+# In order to link against a package in Go, the package must live in a
+# directory tree in the way that Go expects.  To make this possible,
+# there must be a directory such that we can set GOPATH=${dir}, and
+# the package will be under $GOPATH/src/${full-package-path}.
+
+# So we set XEN_GOPATH to $XEN_ROOT/tools/golang.  The xenlight
+# "package build" directory ($PWD/xenlight) will create the "package
+# source" directory in the proper place.  Go programs can use this
+# package by setting GOPATH=$(XEN_GOPATH).
+
+SUBDIRS-y = xenlight
+
+.PHONY: build all
+all build: subdirs-all
+
+.PHONY: install
+install: subdirs-install
+
+.PHONY: clean
+clean: subdirs-clean
+	$(RM) -r src pkg
+
+.PHONY: distclean
+distclean: clean
tools/golang/xenlight/Makefile new file +49 -0

49 lines not on this page · raw message

tools/golang/xenlight/xenlight.go new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.1.4

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