golang/xenlight: Create stub package
RFC PATCH v3 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.
---
Changes:
- Added global logger variable and destroyed the logger instance when closing the context.
- Whitespace fixes
---
--- tools/Makefile | 1 + tools/Rules.mk | 7 ++++ tools/golang/Makefile | 27 ++++++++++++ tools/golang/xenlight/Makefile | 49 ++++++++++++++++++++++ tools/golang/xenlight/xenlight.go | 88 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 172 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
@@ -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 +7 -0
@@ -30,6 +30,13 @@ 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 +XEN_GOCODE_URL = golang.xenproject.org +endif + ifeq ($(debug_symbols),y) CFLAGS += -g3 endif
tools/golang/Makefile new file +27 -0
@@ -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 +88 -0
88 lines not on this page · raw message
Signature · 7 lines
2.7.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH v3 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 | 78 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-)
tools/golang/xenlight/xenlight.go +76 -2
@@ -37,8 +37,71 @@ import ( ) /* + * 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 }
@@ -50,6 +113,17 @@ var Ctx Context var logger *C.xentoollog_logger_stdiostream +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 }
@@ -64,7 +138,7 @@ func (Ctx *Context) Open() (err error) { 0, unsafe.Pointer(logger)) if ret != 0 { - err = fmt.Errorf("Error: %d", -ret) + err = Error(-ret) } return }
@@ -74,7 +148,7 @@ func (Ctx *Context) Close() (err error) { Ctx.ctx = nil if ret != 0 { - err = fmt.Errorf("Error: %d", -ret) + err = Error(-ret) } C.xtl_logger_destroy(unsafe.Pointer(logger)) return
Signature · 7 lines
2.7.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH v3 3/8 golang/xenlight: Add host-related functionality
In reply to Ronald RojasAdd 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
---
Changes since last version
- Used defer for libxl_physinfo_dispose
- Formating fixes
---
--- tools/golang/xenlight/xenlight.go | 200 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+)
tools/golang/xenlight/xenlight.go +200 -0
@@ -106,6 +106,103 @@ 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 +} + +func (cphys *C.libxl_physinfo) toGo() (physinfo *Physinfo) { + + 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 +} + +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 +} + +func (cinfo *C.libxl_version_info) toGo() (info *VersionInfo) { + 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 +} + /* * Context */
@@ -160,3 +257,106 @@ 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 + C.libxl_physinfo_init(&cphys) + + ret := C.libxl_get_physinfo(Ctx.ctx, &cphys) + + if ret < 0 { + err = Error(ret) + return + } + physinfo = cphys.toGo() + C.libxl_physinfo_dispose(&cphys) + + 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 = cinfo.toGo() + + return +}
Signature · 7 lines
2.7.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH v3 4/8 golang/xenlight: Implement libxl_domain_info and libxl_domain_unpause
In reply to Ronald RojasAdd calls for the following host-related functionality:
- libxl_domain_info
- libxl_domain_unpause
Include Golang version for the libxl_domain_info as DomainInfo.
---
Changes since last version
- Formating fixes
- used defer for libxl_dominfo_dispose
- Removed unnessary unsafe.Pointer() casts.
---
--- tools/golang/xenlight/xenlight.go | 136 +++++++++++++++++++++++++++++++++++++- 1 file changed, 133 insertions(+), 3 deletions(-)
tools/golang/xenlight/xenlight.go +133 -3
@@ -33,6 +33,7 @@ import "C" import ( "fmt" + "time" "unsafe" )
@@ -102,13 +103,19 @@ var errors = [...]string{ * Types: Builtins */ +type Domid uint32 + +type MemKB uint64 + +type Uuid C.libxl_uuid + type Context struct { ctx *C.libxl_ctx } type Hwcap []C.uint32_t -func (chwcap C.libxl_hwcap) CToGo() (ghwcap Hwcap) { +func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) { // Alloc a Go slice for the bytes size := 8 ghwcap = make([]C.uint32_t, size)
@@ -161,7 +168,7 @@ func (cphys *C.libxl_physinfo) toGo() (physinfo *Physinfo) { physinfo.SharingFreedPages = uint64(cphys.sharing_freed_pages) physinfo.SharingUsedFrames = uint64(cphys.sharing_used_frames) physinfo.NrNodes = uint32(cphys.nr_nodes) - physinfo.HwCap = cphys.hw_cap.CToGo() + physinfo.HwCap = cphys.hw_cap.toGo() physinfo.CapHvm = bool(cphys.cap_hvm) physinfo.CapHvmDirectio = bool(cphys.cap_hvm_directio)
@@ -203,6 +210,93 @@ func (cinfo *C.libxl_version_info) toGo() (info *VersionInfo) { return } +type ShutdownReason int32 + +const ( + ShutdownReasonUnknown = ShutdownReason(C.LIBXL_SHUTDOWN_REASON_UNKNOWN) + ShutdownReasonPoweroff = ShutdownReason(C.LIBXL_SHUTDOWN_REASON_POWEROFF) + ShutdownReasonReboot = ShutdownReason(C.LIBXL_SHUTDOWN_REASON_REBOOT) + ShutdownReasonSuspend = ShutdownReason(C.LIBXL_SHUTDOWN_REASON_SUSPEND) + ShutdownReasonCrash = ShutdownReason(C.LIBXL_SHUTDOWN_REASON_CRASH) + ShutdownReasonWatchdog = ShutdownReason(C.LIBXL_SHUTDOWN_REASON_WATCHDOG) + ShutdownReasonSoftReset = ShutdownReason(C.LIBXL_SHUTDOWN_REASON_SOFT_RESET) +) + +func (sr ShutdownReason) String() (str string) { + cstr := C.libxl_shutdown_reason_to_string(C.libxl_shutdown_reason(sr)) + str = C.GoString(cstr) + + return +} + +type DomainType int32 + +const ( + DomainTypeInvalid = DomainType(C.LIBXL_DOMAIN_TYPE_INVALID) + DomainTypeHvm = DomainType(C.LIBXL_DOMAIN_TYPE_HVM) + DomainTypePv = DomainType(C.LIBXL_DOMAIN_TYPE_PV) +) + +func (dt DomainType) String() (str string) { + cstr := C.libxl_domain_type_to_string(C.libxl_domain_type(dt)) + str = C.GoString(cstr) + + return +} + +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 + OutstandingMemkb MemKB + CurrentMemkb MemKB + SharedMemkb MemKB + PagedMemkb MemKB + MaxMemkb MemKB + CpuTime time.Duration + VcpuMaxId uint32 + VcpuOnline uint32 + Cpupool uint32 + DomainType int32 +} + +func (cdi *C.libxl_dominfo) toGo() (di *Dominfo) { + + 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 +} + /* * Context */
@@ -332,6 +426,7 @@ func (Ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) { } var cphys C.libxl_physinfo C.libxl_physinfo_init(&cphys) + defer C.libxl_physinfo_dispose(&cphys) ret := C.libxl_get_physinfo(Ctx.ctx, &cphys)
@@ -340,7 +435,6 @@ func (Ctx *Context) GetPhysinfo() (physinfo *Physinfo, err error) { return } physinfo = cphys.toGo() - C.libxl_physinfo_dispose(&cphys) return }
@@ -360,3 +454,39 @@ 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 + C.libxl_dominfo_init(&cdi) + defer C.libxl_dominfo_dispose(&cdi) + + ret := C.libxl_domain_info(Ctx.ctx, &cdi, C.uint32_t(Id)) + + if ret != 0 { + err = Error(-ret) + return + } + + di = cdi.toGo() + + 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.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH v3 5/8 golang/xenlight: Add tests host related functionality functions
In reply to Ronald RojasCreate tests for the following functions:
- GetVersionInfo
- GetPhysinfo
- GetDominfo
- GetMaxCpus
- GetOnlineCpus
- GetMaxNodes
- GetFreeMemory
---
changes since last version
- created CFLAGS and LDLIBS variables to build test C files with required dependencies.
- created create_context and destroy_context function for tests to create/destroy libxl_ctx and xenlogger
- Formating changes
- Removed stale comments
- Removed redundant error checks in Golang tests
---
--- tools/golang/xenlight/test/xeninfo/Makefile | 41 +++++++++++++++++++++++ tools/golang/xenlight/test/xeninfo/dominfo.c | 33 ++++++++++++++++++ tools/golang/xenlight/test/xeninfo/dominfo.go | 31 +++++++++++++++++ tools/golang/xenlight/test/xeninfo/freememory.c | 26 ++++++++++++++ tools/golang/xenlight/test/xeninfo/freememory.go | 25 ++++++++++++++ tools/golang/xenlight/test/xeninfo/maxcpu.c | 18 ++++++++++ tools/golang/xenlight/test/xeninfo/maxcpu.go | 24 +++++++++++++ tools/golang/xenlight/test/xeninfo/maxnodes.c | 15 +++++++++ tools/golang/xenlight/test/xeninfo/maxnodes.go | 24 +++++++++++++ tools/golang/xenlight/test/xeninfo/onlinecpu.c | 18 ++++++++++ tools/golang/xenlight/test/xeninfo/onlinecpu.go | 24 +++++++++++++ tools/golang/xenlight/test/xeninfo/physinfo.c | 32 ++++++++++++++++++ tools/golang/xenlight/test/xeninfo/physinfo.go | 32 ++++++++++++++++++ tools/golang/xenlight/test/xeninfo/print.h | 22 ++++++++++++ tools/golang/xenlight/test/xeninfo/versioninfo.c | 22 ++++++++++++ tools/golang/xenlight/test/xeninfo/versioninfo.go | 28 ++++++++++++++++ tools/golang/xenlight/test/xeninfo/xenlight.go | 1 + 17 files changed, 416 insertions(+) create mode 100644 tools/golang/xenlight/test/xeninfo/Makefile create mode 100644 tools/golang/xenlight/test/xeninfo/dominfo.c create mode 100644 tools/golang/xenlight/test/xeninfo/dominfo.go create mode 100644 tools/golang/xenlight/test/xeninfo/freememory.c create mode 100644 tools/golang/xenlight/test/xeninfo/freememory.go create mode 100644 tools/golang/xenlight/test/xeninfo/maxcpu.c create mode 100644 tools/golang/xenlight/test/xeninfo/maxcpu.go create mode 100644 tools/golang/xenlight/test/xeninfo/maxnodes.c create mode 100644 tools/golang/xenlight/test/xeninfo/maxnodes.go create mode 100644 tools/golang/xenlight/test/xeninfo/onlinecpu.c create mode 100644 tools/golang/xenlight/test/xeninfo/onlinecpu.go create mode 100644 tools/golang/xenlight/test/xeninfo/physinfo.c create mode 100644 tools/golang/xenlight/test/xeninfo/physinfo.go create mode 100644 tools/golang/xenlight/test/xeninfo/print.h create mode 100644 tools/golang/xenlight/test/xeninfo/versioninfo.c create mode 100644 tools/golang/xenlight/test/xeninfo/versioninfo.go create mode 120000 tools/golang/xenlight/test/xeninfo/xenlight.go
tools/golang/xenlight/test/xeninfo/Makefile new file +41 -0
@@ -0,0 +1,41 @@ +XEN_ROOT = $(CURDIR)/../../../../.. +include $(XEN_ROOT)/tools/Rules.mk + +GO ?= go + +TESTS = dominfo freememory maxcpu onlinecpu physinfo versioninfo +CBINARIES = $(TESTS:%=%-c) +GOBINARIES = $(TESTS:%=%-go) + +CFLAGS += -Werror +CFLAGS += $(CFLAGS_libxentoollog) +CFLAGS += $(CFLAGS_libxenlight) + +LDLIBS += $(LDLIBS_libxentoollog) +LDLIBS += $(LDLIBS_libxenlight) + +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) -o $@ $< $(LDLIBS) + +%-go: %.go + GOPATH=$(XEN_ROOT)/tools/golang $(GO) build -o $@ $< + +clean: + rm -f *-c + rm -f *-go + rm -f *.output
tools/golang/xenlight/test/xeninfo/dominfo.c new file +33 -0
@@ -0,0 +1,33 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libxl.h> +#include "print.h" + +int main (void ){ + +libxl_ctx *context; +libxl_dominfo info; +int err; +long cpu_time; + context =create_context (); +libxl_dominfo_init (&info); + 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)); + 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); + +destroy_context (context); + +}
tools/golang/xenlight/test/xeninfo/dominfo.go new file +31 -0
@@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "os" + "xenproject.org/xenlight" +) + +func main() { + ctx := xenlight.Ctx + err := ctx.Open() + if err != nil { + os.Exit(-1) + } + defer ctx.Close() + 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) + 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) + 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/xeninfo/freememory.c new file +26 -0
@@ -0,0 +1,26 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libxl.h> +#include "print.h" + +int main (void ){ + +libxl_ctx *context; +uint64_t free_memory; +int err; + + context =create_context (); + + err =libxl_get_free_memory (context, &free_memory); +if (err <0 ) + { +printf ("%d\n" , err); + } +else + { +printf ("%lu\n" , free_memory); + } +destroy_context (context); + +} +
tools/golang/xenlight/test/xeninfo/freememory.go new file +25 -0
@@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "os" + "xenproject.org/xenlight" +) + +func main() { + ctx := xenlight.Ctx + err := ctx.Open() + if err != nil { + os.Exit(-1) + } + + defer ctx.Close() + + free_memory, err := ctx.GetFreeMemory() + if err != nil { + fmt.Printf("%d\n", err) + } else { + fmt.Printf("%d\n", free_memory) + } + +}
tools/golang/xenlight/test/xeninfo/maxcpu.c new file +18 -0
@@ -0,0 +1,18 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libxl.h> +#include "print.h" + +int main (void ){ + +libxl_ctx *context; +int max_cpus; + + context =create_context (); + max_cpus =libxl_get_max_cpus (context); +printf ("%d\n" , max_cpus); + +destroy_context (context); + +} +
tools/golang/xenlight/test/xeninfo/maxcpu.go new file +24 -0
@@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" + "xenproject.org/xenlight" +) + +func main() { + ctx := xenlight.Ctx + err := ctx.Open() + if err != nil { + os.Exit(-1) + } + defer ctx.Close() + + max_cpus, err := ctx.GetMaxCpus() + if err != nil { + fmt.Printf("%d\n", err) + } else { + fmt.Printf("%d\n", max_cpus) + } + +}
tools/golang/xenlight/test/xeninfo/maxnodes.c new file +15 -0
@@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libxl.h> + +int main (){ + +libxl_ctx *context; + context =create_context (); + +int max_nodes =libxl_get_max_nodes (context); +printf ("%d\n" , max_nodes); + +destroy_context (context); +} +
tools/golang/xenlight/test/xeninfo/maxnodes.go new file +24 -0
@@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" + "xenproject.org/xenlight" +) + +func main() { + ctx := xenlight.Ctx + err := ctx.Open() + if err != nil { + os.Exit(-1) + } + defer ctx.Close() + + max_nodes, err := ctx.GetMaxNodes() + if err != nil { + fmt.Printf("%d\n", err) + } else { + fmt.Printf("%d\n", max_nodes) + } + +}
tools/golang/xenlight/test/xeninfo/onlinecpu.c new file +18 -0
@@ -0,0 +1,18 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libxl.h> +#include "print.h" + +int main (void ){ + +libxl_ctx *context; +int online_cpus; + context =create_context (); + + online_cpus =libxl_get_online_cpus (context); +printf ("%d\n" , online_cpus); + +destroy_context (context); + +} +
tools/golang/xenlight/test/xeninfo/onlinecpu.go new file +24 -0
@@ -0,0 +1,24 @@ +package main + +import ( + "fmt" + "os" + "xenproject.org/xenlight" +) + +func main() { + ctx := xenlight.Ctx + err := ctx.Open() + if err != nil { + os.Exit(-1) + } + defer ctx.Close() + + online_cpus, err := ctx.GetOnlineCpus() + if err != nil { + fmt.Printf("%d\n", err) + } else { + fmt.Printf("%d\n", online_cpus) + } + +}
tools/golang/xenlight/test/xeninfo/physinfo.c new file +32 -0
@@ -0,0 +1,32 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libxl.h> +#include "print.h" + +int main (void ){ + +libxl_ctx *context; +libxl_physinfo info; +int err; +int i; + context =create_context (); +libxl_physinfo_init (&info); + 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 (i =0 ; i <8 ; i++){ +printf ("%u\n" , info.hw_cap[i]); + } + +libxl_physinfo_init (&info); +destroy_context (context); + +} +
tools/golang/xenlight/test/xeninfo/physinfo.go new file +32 -0
@@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + "os" + "xenproject.org/xenlight" +) + +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/xeninfo/print.h new file +22 -0
@@ -0,0 +1,22 @@ +xentoollog_logger_stdiostream *logger; + +static inline char *bool_to_string (bool a){ +return (a ?"true" :"false" ); +} + +static inline libxl_ctx *create_context (void ){ +libxl_ctx *context; + logger =xtl_createlogger_stdiostream (stderr, +XTL_ERROR ,0 ); +libxl_ctx_alloc (&context,LIBXL_VERSION ,0 , (xentoollog_logger *)logger); +return context; +} + +static inline int destroy_context (libxl_ctx *context){ +int err =libxl_ctx_free (context); +if (err !=0 ) +return err; +xtl_logger_destroy ((xentoollog_logger *)logger); +return err; + +}
tools/golang/xenlight/test/xeninfo/versioninfo.c new file +22 -0
@@ -0,0 +1,22 @@ +#include <stdio.h> +#include <stdlib.h> +#include <libxl.h> +#include "print.h" + +int main (void ){ + +libxl_ctx *context; +const libxl_version_info * info; + context =create_context (); + 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); + +destroy_context (context); + +}
tools/golang/xenlight/test/xeninfo/versioninfo.go new file +28 -0
@@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "os" + "xenproject.org/xenlight" +) + +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) + +}
tools/golang/xenlight/test/xeninfo/xenlight.go new file +1 -0
@@ -0,0 +1 @@ +../../xenlight.go/usr/local/go/src/xenproject.org/xenlight/xenlight.go
Signature · 7 lines
2.7.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
RFC PATCH v3 6/8 golang/xenlight: Implement libxl_bitmap and helper operations
In reply to Ronald RojasImplement 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 | 167 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+)
tools/golang/xenlight/xenlight.go +167 -0
@@ -129,6 +129,20 @@ func (chwcap C.libxl_hwcap) toGo() (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 *
@@ -298,6 +312,159 @@ func (cdi *C.libxl_dominfo) toGo() (di *Dominfo) { } /* + * Bitmap operations + */ + +// Return a Go bitmap which is a copy of the referred C bitmap. +func (cbm C.libxl_bitmap) toGo() (gbm Bitmap) { + // Alloc a Go slice for the bytes + size := int(cbm.size) + gbm.bitmap = make([]C.uint8_t, size) + + // Make a slice pointing to the C array + mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + // And copy the C array into the Go array + copy(gbm.bitmap, mapslice) + + return +} + +// Must be C.libxl_bitmap_dispose'd of afterwards +func (gbm Bitmap) toC() (cbm C.libxl_bitmap) { + C.libxl_bitmap_init(&cbm) + + size := len(gbm.bitmap) + cbm._map = (*C.uint8_t)(C.malloc(C.size_t(size))) + cbm.size = C.uint32_t(size) + if cbm._map == nil { + panic("C.calloc failed!") + } + + // Make a slice pointing to the C array + mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + // And copy the Go array into the C array + copy(mapslice, gbm.bitmap) + + return +} + +func (bm *Bitmap) Test(bit int) bool { + ubit := uint(bit) + if bit > bm.Max() || bm.bitmap == nil { + 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.3 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel