Skip to contentSkip to conversation

change tools cpumaps to uint8_t

PATCH In review Juergen Gross 2 participants 2 messages Oct 28, 2010 Arm/actions semi architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 change tools cpumaps to uint8_t

Cpumap types in tools (libxc and libxl) are changed to be based on bytes like in the interface to the hypervisor.
To make handling easier the size of used cpumaps is always based on the number of physical cpus supported by the hypervisor. This eliminates the need to keep track of the cpumap size in external interfaces.
In libxl a macro for cycling through a cpumap is added (libxl_for_each_cpu).

Interfaces changed:
libxl_set_vcpuaffinity()
libxl_cpumap_alloc()
xc_vcpu_setaffinity()
xc_vcpu_getaffinity()
xc_cpupool_freeinfo()

Signed-off-by [email protected]
10 files changed, 180 insertions(+), 163 deletions(-)
tools/libxc/xc_cpupool.c          |   59 +++++++++++++---------------------
tools/libxc/xc_domain.c           |   14 ++++----
tools/libxc/xc_misc.c             |   18 ++++++++++
tools/libxc/xenctrl.h             |   41 ++++++++++++++++--------
tools/libxl/libxl.c               |   63 ++++++++++++++++++++-----------------
tools/libxl/libxl.h               |    4 +-
tools/libxl/libxl_utils.c         |   21 +++++++-----
tools/libxl/libxl_utils.h         |    4 +-
tools/libxl/xl_cmdimpl.c          |   57 ++++++++++++++-------------------
tools/python/xen/lowlevel/xc/xc.c |   62 +++++++++++++++---------------------

# HG changeset patch
# User Juergen Gross <[email protected]> # Date 1288255244 -7200
# Node ID 51ed06fc352e979e6f2a05489515102597c977b1 # Parent 28a16074681582f1209c9077f870ccf44927133e change tools cpumaps to uint8_t

Cpumap types in tools (libxc and libxl) are changed to be based on bytes like in the interface to the hypervisor.
To make handling easier the size of used cpumaps is always based on the number of physical cpus supported by the hypervisor. This eliminates the need to keep track of the cpumap size in external interfaces.
In libxl a macro for cycling through a cpumap is added (libxl_for_each_cpu).

Interfaces changed:
libxl_set_vcpuaffinity()
libxl_cpumap_alloc()
xc_vcpu_setaffinity()
xc_vcpu_getaffinity()
xc_cpupool_freeinfo()

Signed-off-by [email protected]
tools/libxc/xc_cpupool.c +23 -36
diff -r 28a160746815 -r 51ed06fc352e tools/libxc/xc_cpupool.c
@@ -32,11 +32,6 @@
     } while ( (ret < 0) && (errno == EAGAIN) );
 
     return ret;
-}
-
-static int get_cpumap_size(xc_interface *xch)
-{
-    return (xc_get_max_cpus(xch) + 7) / 8;
 }
 
 int xc_cpupool_create(xc_interface *xch,
@@ -74,27 +69,15 @@
 {
     int err = 0;
     xc_cpupoolinfo_t *info = NULL;
-    int local_size;
-    int cpumap_size;
-    int size;
+    size_t local_size;
     DECLARE_SYSCTL;
     DECLARE_HYPERCALL_BUFFER(uint8_t, local);
 
-    local_size = get_cpumap_size(xch);
-    if (!local_size)
-    {
-        PERROR("Could not get number of cpus");
-        return NULL;
-    }
-
-    local = xc_hypercall_buffer_alloc(xch, local, local_size);
+    local = xc_cpumap_hypercall_buffer_alloc(xch, local, local_size);
     if ( local == NULL ) {
         PERROR("Could not allocate locked memory for xc_cpupool_getinfo");
         return NULL;
     }
-
-    cpumap_size = (local_size + sizeof(*info->cpumap) - 1) / sizeof(*info->cpumap);
-    size = sizeof(xc_cpupoolinfo_t) + cpumap_size * sizeof(*info->cpumap);
 
     sysctl.cmd = XEN_SYSCTL_cpupool_op;
     sysctl.u.cpupool_op.op = XEN_SYSCTL_CPUPOOL_OP_INFO;
@@ -107,23 +90,31 @@
     if ( err < 0 )
 	goto out;
 
-    info = malloc(size);
+    info = calloc(1, sizeof(xc_cpupoolinfo_t));
     if ( !info )
 	goto out;
 
-    memset(info, 0, size);
-    info->cpumap_size = local_size * 8;
-    info->cpumap = (uint64_t *)(info + 1);
-
+    info->cpumap = xc_cpumap_alloc(xch);
+    if (!info->cpumap) {
+        free(info);
+        goto out;
+    }
     info->cpupool_id = sysctl.u.cpupool_op.cpupool_id;
     info->sched_id = sysctl.u.cpupool_op.sched_id;
     info->n_dom = sysctl.u.cpupool_op.n_dom;
-    bitmap_byte_to_64(info->cpumap, local, local_size * 8);
+    bcopy(local, info->cpumap, local_size);
 
 out:
     xc_hypercall_buffer_free(xch, local);
 
     return info;
+}
+
+void xc_cpupool_infofree(xc_interface *xch,
+                         xc_cpupoolinfo_t *info)
+{
+    free(info->cpumap);
+    free(info);
 }
 
 int xc_cpupool_addcpu(xc_interface *xch,
@@ -165,19 +156,15 @@
     return do_sysctl_save(xch, &sysctl);
 }
 
-uint64_t * xc_cpupool_freeinfo(xc_interface *xch,
-                        int *cpusize)
+xc_cpumap_t xc_cpupool_freeinfo(xc_interface *xch)
 {
     int err = -1;
-    uint64_t *cpumap = NULL;
+    xc_cpumap_t cpumap = NULL;
+    size_t mapsize;
     DECLARE_SYSCTL;
     DECLARE_HYPERCALL_BUFFER(uint8_t, local);
 
-    *cpusize = get_cpumap_size(xch);
-    if (*cpusize == 0)
-        return NULL;
-
-    local = xc_hypercall_buffer_alloc(xch, local, *cpusize);
+    local = xc_cpumap_hypercall_buffer_alloc(xch, local, mapsize);
     if ( local == NULL ) {
         PERROR("Could not allocate locked memory for xc_cpupool_freeinfo");
         return NULL;
@@ -186,18 +173,18 @@
     sysctl.cmd = XEN_SYSCTL_cpupool_op;
     sysctl.u.cpupool_op.op = XEN_SYSCTL_CPUPOOL_OP_FREEINFO;
     set_xen_guest_handle(sysctl.u.cpupool_op.cpumap.bitmap, local);
-    sysctl.u.cpupool_op.cpumap.nr_cpus = *cpusize * 8;
+    sysctl.u.cpupool_op.cpumap.nr_cpus = mapsize * 8;
 
     err = do_sysctl_save(xch, &sysctl);
 
     if ( err < 0 )
 	goto out;
 
-    cpumap = calloc((*cpusize + sizeof(*cpumap) - 1) / sizeof(*cpumap), sizeof(*cpumap));
+    cpumap = xc_cpumap_alloc(xch);
     if (cpumap == NULL)
 	goto out;
 
-    bitmap_byte_to_64(cpumap, local, *cpusize * 8);
+    bcopy(local, cpumap, mapsize);
 
 out:
     xc_hypercall_buffer_free(xch, local);
tools/libxc/xc_domain.c +8 -6
diff -r 28a160746815 -r 51ed06fc352e tools/libxc/xc_domain.c
@@ -113,13 +113,14 @@
 int xc_vcpu_setaffinity(xc_interface *xch,
                         uint32_t domid,
                         int vcpu,
-                        uint64_t *cpumap, int cpusize)
+                        xc_cpumap_t cpumap)
 {
     DECLARE_DOMCTL;
     DECLARE_HYPERCALL_BUFFER(uint8_t, local);
     int ret = -1;
+    size_t cpusize;
 
-    local = xc_hypercall_buffer_alloc(xch, local, cpusize);
+    local = xc_cpumap_hypercall_buffer_alloc(xch, local, cpusize);
     if ( local == NULL )
     {
         PERROR("Could not allocate memory for setvcpuaffinity domctl hypercall");
@@ -130,7 +131,7 @@
     domctl.domain = (domid_t)domid;
     domctl.u.vcpuaffinity.vcpu    = vcpu;
 
-    bitmap_64_to_byte(local, cpumap, cpusize * 8);
+    bcopy(cpumap, local, cpusize);
 
     set_xen_guest_handle(domctl.u.vcpuaffinity.cpumap.bitmap, local);
 
@@ -148,13 +149,14 @@
 int xc_vcpu_getaffinity(xc_interface *xch,
                         uint32_t domid,
                         int vcpu,
-                        uint64_t *cpumap, int cpusize)
+                        xc_cpumap_t cpumap)
 {
     DECLARE_DOMCTL;
     DECLARE_HYPERCALL_BUFFER(uint8_t, local);
     int ret = -1;
+    size_t cpusize;
 
-    local = xc_hypercall_buffer_alloc(xch, local, cpusize);
+    local = xc_cpumap_hypercall_buffer_alloc(xch, local, cpusize);
     if(local == NULL)
     {
         PERROR("Could not allocate memory for getvcpuaffinity domctl hypercall");
@@ -170,7 +172,7 @@
 
     ret = do_domctl(xch, &domctl);
 
-    bitmap_byte_to_64(cpumap, local, cpusize * 8);
+    bcopy(local, cpumap, cpusize);
 
     xc_hypercall_buffer_free(xch, local);
 out:
tools/libxc/xc_misc.c +18 -0
diff -r 28a160746815 -r 51ed06fc352e tools/libxc/xc_misc.c
@@ -33,6 +33,24 @@
         max_cpus = physinfo.max_cpu_id + 1;
 
     return max_cpus;
+}
+
+xc_cpumap_t xc_cpumap_alloc(xc_interface *xch)
+{
+    int sz;
+
+    sz = (xc_get_max_cpus(xch) + 7) / 8;
+    return calloc(1, sz);
+}
+
+uint8_t *xc__cpumap_hypercall_buffer_alloc(xc_interface *xch,
+                                           xc_hypercall_buffer_t *b,
+                                           size_t *sz)
+{
+    *sz = (xc_get_max_cpus(xch) + 7) / 8;
+    if (*sz == 0)
+        return NULL;
+    return xc__hypercall_buffer_alloc(xch, b, *sz);
 }
 
 int xc_readconsolering(xc_interface *xch,
tools/libxc/xenctrl.h +28 -13

85 lines not on this page · raw message

tools/libxl/libxl.c +35 -28

151 lines not on this page · raw message

tools/libxl/libxl.h +2 -2
diff -r 28a160746815 -r 51ed06fc352e tools/libxl/libxl.h
@@ -145,7 +145,7 @@
 
 typedef struct {
     uint32_t size;          /* number of bytes in map */
-    uint64_t *map;
+    uint8_t *map;
 } libxl_cpumap;
 void libxl_cpumap_destroy(libxl_cpumap *map);
 
@@ -464,7 +464,7 @@
 libxl_vcpuinfo *libxl_list_vcpu(libxl_ctx *ctx, uint32_t domid,
                                        int *nb_vcpu, int *nrcpus);
 int libxl_set_vcpuaffinity(libxl_ctx *ctx, uint32_t domid, uint32_t vcpuid,
-                           uint64_t *cpumap, int nrcpus);
+                           libxl_cpumap *cpumap);
 int libxl_set_vcpuonline(libxl_ctx *ctx, uint32_t domid, uint32_t bitmask);
 
 int libxl_get_sched_id(libxl_ctx *ctx);
tools/libxl/libxl_utils.c +13 -8
diff -r 28a160746815 -r 51ed06fc352e tools/libxl/libxl_utils.c
@@ -708,15 +708,20 @@
     return rc;
 }
 
-int libxl_cpumap_alloc(libxl_cpumap *cpumap, int max_cpus)
+int libxl_cpumap_alloc(libxl_ctx *ctx, libxl_cpumap *cpumap)
 {
-    int elems;
+    int max_cpus;
+    int sz;
 
-    elems = (max_cpus + 63) / 64;
-    cpumap->map = calloc(elems, sizeof(*cpumap->map));
+    max_cpus = libxl_get_max_cpus(ctx);
+    if (max_cpus == 0)
+        return ERROR_FAIL;
+
+    sz = (max_cpus + 7) / 8;
+    cpumap->map = calloc(sz, sizeof(*cpumap->map));
     if (!cpumap->map)
         return ERROR_NOMEM;
-    cpumap->size = elems * 8;     /* size in bytes */
+    cpumap->size = sz;
     return 0;
 }
 
@@ -729,21 +734,21 @@
 {
     if (cpu >= cpumap->size * 8)
         return 0;
-    return (cpumap->map[cpu / 64] & (1L << (cpu & 63))) ? 1 : 0;
+    return (cpumap->map[cpu / 8] & (1 << (cpu & 7))) ? 1 : 0;
 }
 
 void libxl_cpumap_set(libxl_cpumap *cpumap, int cpu)
 {
     if (cpu >= cpumap->size * 8)
         return;
-    cpumap->map[cpu / 64] |= 1L << (cpu & 63);
+    cpumap->map[cpu / 8] |= 1 << (cpu & 7);
 }
 
 void libxl_cpumap_reset(libxl_cpumap *cpumap, int cpu)
 {
     if (cpu >= cpumap->size * 8)
         return;
-    cpumap->map[cpu / 64] &= ~(1L << (cpu & 63));
+    cpumap->map[cpu / 8] &= ~(1 << (cpu & 7));
 }
 
 int libxl_get_max_cpus(libxl_ctx *ctx)
tools/libxl/libxl_utils.h +3 -1
diff -r 28a160746815 -r 51ed06fc352e tools/libxl/libxl_utils.h
@@ -76,9 +76,11 @@
  * return -1 if there are an error */
 int libxl_check_device_model_version(libxl_ctx *ctx, char *path);
 
-int libxl_cpumap_alloc(libxl_cpumap *cpumap, int max_cpus);
+int libxl_cpumap_alloc(libxl_ctx *ctx, libxl_cpumap *cpumap);
 int libxl_cpumap_test(libxl_cpumap *cpumap, int cpu);
 void libxl_cpumap_set(libxl_cpumap *cpumap, int cpu);
 void libxl_cpumap_reset(libxl_cpumap *cpumap, int cpu);
+#define libxl_for_each_cpu(var, map) for (var = 0; var < (map).size * 8; var++)
+
 #endif
 
tools/libxl/xl_cmdimpl.c +24 -33

156 lines not on this page · raw message

tools/python/xen/lowlevel/xc/xc.c +26 -36

145 lines not on this page · raw message

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

#1 lore raw

Looks nice. Thanks for doing this.

Quoted 14 lines
tools/libxc/xc_cpupool.c +1 -6
@@ -74,27 +69,15 @@
 {
     int err = 0;
     xc_cpupoolinfo_t *info = NULL;
-    int local_size;
-    int cpumap_size;
-    int size;
+    size_t local_size;
     DECLARE_SYSCTL;
     DECLARE_HYPERCALL_BUFFER(uint8_t, local);
 
-    local_size = get_cpumap_size(xch);
-    if (!local_size)
-    {

tools/libxc/xc_cpupool.c line 80

Quoted 10 lines
tools/libxc/xc_cpupool.c +1 -5
-        PERROR("Could not get number of cpus");
-        return NULL;
-    }
-
-    local = xc_hypercall_buffer_alloc(xch, local, local_size);
+    local = xc_cpumap_hypercall_buffer_alloc(xch, local, local_size);
     if ( local == NULL ) {
         PERROR("Could not allocate locked memory for xc_cpupool_getinfo");
         return NULL;
     }

I'm not that keen on this interface change, I think get_cpumap_size + xc_hypercall_buffer_alloc was fine as it was.

More importantly I really don't like hiding the taking of the pointer on the size parameter being hidden inside the macro.

[...]

tools/libxc/xc_cpupool.c line 105

Quoted 5 lines
tools/libxc/xc_cpupool.c +1 -1
     info->cpupool_id = sysctl.u.cpupool_op.cpupool_id;
     info->sched_id = sysctl.u.cpupool_op.sched_id;
     info->n_dom = sysctl.u.cpupool_op.n_dom;
-    bitmap_byte_to_64(info->cpumap, local, local_size * 8);
+    bcopy(local, info->cpumap, local_size);

bcopy is deprecated, should use memcpy (several times).

Rest looks good, lots of magic numbers disappear ;-)

Ian.