Skip to contentSkip to conversation

Basic cpumap utils

PATCH In review Dulloor 1 participant 1 message Apr 4, 2010 Arm/actions semi architecture

lore
Share & tools
lore

Dulloor

author
#0 lore raw

PATCH 5/11 Basic cpumap utils

This patch implements very basic helper functions (all of ~130 lines) for xenctl_cpumap. The patch also uses these macros in xc_get/set_vcpuaffinity as an example. There are other places that use xc_get/set_vcpuaffinity too, which I will attend to later.

-dulloor

tools/libxc/xc_cpumap.c new file +88 -0
diff -r d6b7abf0d2d5 -r 6eba62092cb4 tools/libxc/xc_cpumap.c
@@ -0,0 +1,88 @@
+#include "xc_cpumap.h"
+#include <stdio.h>
+
+/* Author : Dulloor ([email protected]) */
+
+uint32_t xc_cpumap_next(int cpu, struct xenctl_cpumap *srcp)
+{
+    uint8_t *p, pos;
+    uint8_t *addr = xc_cpumap_bits(srcp);
+    uint32_t size = xc_cpumap_len(srcp);
+    uint32_t offset = cpu+1; /* Find the next set cpu */
+
+    if (offset >= size)
+        return size;
+
+    p = addr + XC_BITMAP_BYTE(offset);
+    pos = XC_BITMAP_BYTE_OFFSET(offset);
+
+    do {
+        for (; (pos < XC_BITS_PER_BYTE) && !((*p)&(1<<pos)); pos++);
+        if (pos < XC_BITS_PER_BYTE)
+            break;
+        pos = 0; p++;
+    } while (p < (addr+size));
+
+    return (((p-addr)*XC_BITS_PER_BYTE) + pos);
+}
+
+void __xc_cpumap_or(struct xenctl_cpumap *dstp,
+        struct xenctl_cpumap *src1p, struct xenctl_cpumap *src2p)
+{
+    uint8_t *dp = xc_cpumap_bits(dstp);
+    uint8_t *s1p = xc_cpumap_bits(src1p);
+    uint8_t *s2p = xc_cpumap_bits(src2p);
+    int nr = XC_BITS_TO_BYTES(xc_cpumap_len(dstp));
+    int k;
+    for (k=0; k<nr; k++)
+        dp[k] = s1p[k] | s2p[k];
+}
+
+static inline uint8_t hweight8(uint8_t w)
+{
+    uint8_t res = (w & 0x55) + ((w >> 1) & 0x55);
+    res = (res & 0x33) + ((res >> 2) & 0x33);
+    return (res & 0x0F) + ((res >> 4) & 0x0F);
+}
+
+int __xc_cpumap_weight(struct xenctl_cpumap *srcp)
+{
+    const uint8_t *sp = xc_cpumap_bits(srcp);
+    int k, w = 0, lim = XC_BITS_TO_BYTES(xc_cpumap_len(srcp));
+    for (k=0; k <lim; k++)
+        w += hweight8(sp[k]);
+    return w;
+}
+
+/* xenctl_cpumap print function */
+#define CHUNKSZ	8
+#define roundup_power2(val,modulus)	(((val) + (modulus) - 1) & ~((modulus) - 1))
+
+int __xc_cpumap_snprintf(char *buf, unsigned int buflen,
+                                        const struct xenctl_cpumap *cpumap)
+{
+    const uint8_t *maskp = xc_cpumap_bits(cpumap);
+    int nmaskbits = xc_cpumap_len(cpumap);
+	int i, word, bit, len = 0;
+	unsigned long val;
+	const char *sep = "";
+	int chunksz;
+	uint8_t chunkmask;
+
+	chunksz = nmaskbits & (CHUNKSZ - 1);
+	if (chunksz == 0)
+		chunksz = CHUNKSZ;
+
+	i = roundup_power2(nmaskbits, CHUNKSZ) - CHUNKSZ;
+	for (; i >= 0; i -= CHUNKSZ) {
+		chunkmask = ((1ULL << chunksz) - 1);
+		word = i / XC_BITS_PER_BYTE;
+		bit = i % XC_BITS_PER_BYTE;
+		val = (maskp[word] >> bit) & chunkmask;
+		len += snprintf(buf+len, buflen-len, "%s%0*lx", sep,
+			(chunksz+3)/4, val);
+		chunksz = CHUNKSZ;
+		sep = ",";
+	}
+	return len;
+}
tools/libxc/xc_cpumap.h new file +144 -0

144 lines not on this page · raw message

tools/libxc/xenctrl.h +2 -4
diff -r d6b7abf0d2d5 -r 6eba62092cb4 tools/libxc/xenctrl.h
@@ -309,13 +309,11 @@
 int xc_vcpu_setaffinity(int xc_handle,
                         uint32_t domid,
                         int vcpu,
-                        uint64_t *cpumap,
-                        int cpusize);
+                        struct xenctl_cpumap *cpumap);
 int xc_vcpu_getaffinity(int xc_handle,
                         uint32_t domid,
                         int vcpu,
-                        uint64_t *cpumap,
-                        int cpusize);
+                        struct xenctl_cpumap *cpumap);
 
 /**
  * This function will return information about one or more domains. It is
tools/python/xen/lowlevel/xc/xc.c +20 -47

124 lines not on this page · raw message

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