From 5325d67694900fe38a35a755bd0048020164cb31 Mon Sep 17 00:00:00 2001 From: George Dunlap Date: Thu, 29 Dec 2016 20:08:44 +0000 Subject: [PATCH 7/7] golang/xenlight: Implement cpupool operations Also 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 Signed-off-by: George Dunlap --- tools/golang/xenlight/xenlight.go | 244 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 31ce365..e3d69ca 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -233,6 +233,32 @@ type Dominfo struct { } +// 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 translateCpupoolInfoCToGo(cci C.libxl_cpupoolinfo) (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 = bitmapCToGo(cci.cpumap) + + return +} + // # Consistent with values defined in domctl.h // # Except unknown which we have made up // libxl_scheduler = Enumeration("scheduler", [ @@ -296,6 +322,7 @@ func SchedulerFromString(name string) (s Scheduler, err error) { return } + /* * Bitmap operations */ @@ -682,3 +709,220 @@ func (Ctx *Context) DomainUnpause(Id Domid) (err error) { 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 := translateCpupoolInfoCToGo(cpupoolListSlice[i]) + + 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)) + //FIXME: proper error + if ret != 0 { + err = createError("libxl_cpupool_info failed: ", ret) + return + } + defer C.libxl_cpupoolinfo_dispose(&c_cpupool) + + pool = translateCpupoolInfoCToGo(c_cpupool) + + 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 := bitmapGotoC(Cpumap) + defer C.libxl_bitmap_dispose(&cbm) + + ret := C.libxl_cpupool_create(Ctx.ctx, name, C.libxl_scheduler(Scheduler), + cbm, &uuid, &poolid) + // FIXME: Proper error + if ret != 0 { + err = createError("libxl_cpupool_create failed: ", 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)) + // FIXME: Proper error + if ret != 0 { + err = createError("libxl_cpupool_destroy failed: ", 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)) + // FIXME: Proper error + if ret != 0 { + err = createError("libxl_cpupool_cpuadd failed: ", 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 := bitmapGotoC(Cpumap) + defer C.libxl_bitmap_dispose(&cbm) + + ret := C.libxl_cpupool_cpuadd_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm) + // FIXME: Proper error + if ret != 0 { + err = createError("libxl_cpupool_cpuadd_cpumap failed: ", 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)) + // FIXME: Proper error + if ret != 0 { + err = createError("libxl_cpupool_cpuremove failed: ", 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 := bitmapGotoC(Cpumap) + defer C.libxl_bitmap_dispose(&cbm) + + ret := C.libxl_cpupool_cpuremove_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm) + // FIXME: Proper error + if ret != 0 { + err = createError("libxl_cpupool_cpuremove_cpumap failed: ", 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 +} -- 2.1.4