Skip to contentSkip to conversation

format_cpumask()

PATCH William Lee Irwin III 2 participants 3 messages Nov 17, 2003 – Nov 19, 2003

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

I botched printing out cpumasks, and about a dozen times over.

This implements a generic format_cpumask() function and calls it in various places. This time, without scrambling the bits.

-- wli

===== include/linux/cpumask.h 1.1 vs edited =====

include/linux/cpumask.h +19 -0
@@ -68,4 +68,23 @@
 		cpu < NR_CPUS;						\
 		cpu = next_online_cpu(cpu,map))
 
+static inline int format_cpumask(char *buf, cpumask_t cpus)
+{
+	int k, len = 0;
+
+	for (k = sizeof(cpumask_t)/sizeof(long) - 1; k >= 0; ++k) {
+		int m;
+		cpumask_t tmp;
+
+		cpus_shift_right(tmp, cpus, BITS_PER_LONG*k);
+		if (BITS_PER_LONG == 32)
+			m = sprintf(buf, "%08lx", cpus_coerce(tmp));
+		else /* BITS_PER_LONG == 64 */
+			m = sprintf(buf, "%16lx", cpus_coerce(tmp));
+		len += m;
+		buf += m;
+	}
+	return len;
+}
+
 #endif /* __LINUX_CPUMASK_H */

===== arch/i386/kernel/irq.c 1.45 vs edited =====

arch/i386/kernel/irq.c +11 -11
@@ -949,19 +949,13 @@
 static int irq_affinity_read_proc(char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	int k, len;
-	cpumask_t tmp = irq_affinity[(long)data];
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	len = 0;
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+	len = format_cpumask(page, irq_affinity[(long)data];
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }
@@ -1000,10 +994,16 @@
 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	unsigned long *mask = (unsigned long *) data;
+	int len;
+	cpumask_t *mask = (cpumask_t *)data;
+
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
-	return sprintf (page, "%08lx\n", *mask);
+
+	len = format_cpumask(page, *mask);
+	page += len;
+	len += sprintf (page, "\n");
+	return len;
 }
 
 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,

===== arch/ia64/kernel/irq.c 1.31 vs edited =====

arch/ia64/kernel/irq.c +6 -16
@@ -974,19 +974,13 @@
 static int irq_affinity_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	int k, len;
-	cpumask_t tmp = irq_affinity[(long)data];
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	len = 0;
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+	len = format_cpumask(page, irq_affinity[(long)data]);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }
@@ -1034,17 +1028,13 @@
 			int count, int *eof, void *data)
 {
 	cpumask_t *mask = (cpumask_t *)data;
-	int k, len = 0;
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(*mask));
-		len += j;
-		page += j;
-		cpus_shift_right(*mask, *mask, 16);
-	}
+	len = format_cpumask(page, *mask);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }

===== arch/mips/kernel/irq.c 1.14 vs edited =====

arch/mips/kernel/irq.c +8 -17
@@ -872,17 +872,13 @@
 static int irq_affinity_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	int len, k;
-	cpumask_t tmp = irq_affinity[(long)data];
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+
+	len = format_cpumask(page, irq_affinity[(long)data]);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }
@@ -918,19 +914,14 @@
 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	int len, k;
-	cpumask_t *mask = (cpumask_t *)data, tmp;
+	int len;
+	cpumask_t *mask = (cpumask_t *)data;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
-	tmp = *mask;
 
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+	len = format_cpumask(page, *mask);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }

===== arch/ppc/kernel/irq.c 1.33 vs edited =====

arch/ppc/kernel/irq.c +6 -16
@@ -574,19 +574,13 @@
 static int irq_affinity_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	cpumask_t tmp = irq_affinity[(long)data];
-	int k, len = 0;
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
-
+	len = format_cpumask(page, irq_affinity[(long)data]);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }
@@ -665,17 +659,13 @@
 			int count, int *eof, void *data)
 {
 	cpumask_t mask = *(cpumask_t *)data;
-	int k, len = 0;
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(mask));
-		len += j;
-		page += j;
-		cpus_shift_right(mask, mask, 16);
-	}
+	len = format_cpumask(page, mask);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }

===== arch/ppc64/kernel/irq.c 1.36 vs edited =====

arch/ppc64/kernel/irq.c +11 -10
@@ -657,18 +657,13 @@
 static int irq_affinity_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	int k, len;
-	cpumask_t tmp = irq_affinity[(long)data];
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	for (k = 0; k < sizeof(cpumask_t) / sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+	len = format_cpumask(page, irq_affinity[(long)data]);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }
@@ -744,10 +739,16 @@
 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	unsigned long *mask = (unsigned long *) data;
+	int len;
+	cpumask_t *mask = (cpumask_t *) data;
+
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
-	return sprintf (page, "%08lx\n", *mask);
+
+	len = format_cpumask(page, *mask);
+	page += len;
+	len += sprintf (page, "\n");
+	return len;
 }
 
 static int prof_cpu_mask_write_proc (struct file *file, const char __user *buffer,

===== arch/um/kernel/irq.c 1.11 vs edited =====

arch/um/kernel/irq.c +12 -10
@@ -577,9 +577,15 @@
 static int irq_affinity_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
+	int len;
+
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
-	return sprintf (page, "%08lx\n", irq_affinity[(long)data]);
+
+	len = format_cpumask(page, irq_affinity[(long)data]);
+	page += len;
+	len += sprinf(page, "\n");
+	return len;
 }
 
 static unsigned int parse_hex_value (const char *buffer,
@@ -652,18 +658,14 @@
 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	cpumask_t tmp, *mask = (cpumask_t *) data;
-	int k, len = 0;
+	cpumask_t *mask = (cpumask_t *)data;
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
-	tmp = *mask;
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+
+	len = format_cpumask(page, *mask);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }

===== arch/x86_64/kernel/irq.c 1.18 vs edited =====

arch/x86_64/kernel/irq.c +7 -17
@@ -850,18 +850,13 @@
 static int irq_affinity_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	int k, len;
-	cpumask_t tmp = irq_affinity[(long)data];
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	for (k = len = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+	len = format_cpumask(page, irq_affinity[(long)data]);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }
@@ -897,19 +892,14 @@
 static int prof_cpu_mask_read_proc (char *page, char **start, off_t off,
 			int count, int *eof, void *data)
 {
-	cpumask_t tmp, *mask = (cpumask_t *) data;
-	int k, len;
+	cpumask_t *mask = (cpumask_t *)data;
+	int len;
 
 	if (count < HEX_DIGITS+1)
 		return -EINVAL;
 
-	tmp = *mask;
-	for (k = len = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(page, "%04hx", (u16)cpus_coerce(tmp));
-		len += j;
-		page += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
+	len = format_cpumask(page, *mask);
+	page += len;
 	len += sprintf(page, "\n");
 	return len;
 }

===== drivers/base/node.c 1.15 vs edited =====

drivers/base/node.c +4 -8
@@ -19,15 +19,11 @@
 {
 	struct node *node_dev = to_node(dev);
 	cpumask_t tmp = node_dev->cpumap;
-	int k, len = 0;
+	int len = 0;
 
-	for (k = 0; k < sizeof(cpumask_t)/sizeof(u16); ++k) {
-		int j = sprintf(buf, "%04hx", (u16)cpus_coerce(tmp));
-		len += j;
-		buf += j;
-		cpus_shift_right(tmp, tmp, 16);
-	}
-        len += sprintf(buf, "\n");
+	len = format_cpumask(buf, node_dev->cpumap);
+	buf += len;
+	len += sprintf(buf, "\n");
 	return len;
 }
 static SYSDEV_ATTR(cpumap,S_IRUGO,node_read_cpumap,NULL);
#1 lore raw
Quoted 1 line · William

+static inline int format_cpumask(char *buf, cpumask_t cpus)

Wouldn't it be a good idea to pass in the length of the remaining available buffer space and use snprintf, to avoid overruns?

We're going to end up with some pretty big cpumasks on some systems -- running over a buffer would be nasty. It would mostly happen during bring up of new bigger systems; but still worth avoiding.

A second thought - more controversial - long sequences of digits can get pretty difficult to read. How about breaking them with a separator character, say every 32 or 64 bits.

I'd suggest a comma ','.

This has the secondary win that one _could_ then avoid zero-filling, since if each segment is always one words-worth, it's no longer ambiguous when leading zeros are suppressed. However suppressing zero filling might be harder on scripting ... depending on the language. For languages (C and awk?) that lack infinite precision integer arithmetic but have good field separators, the zero filling probably doesn't help much. For languages (Python?) with good infinite precision arithmetic, the field separator is not helpful. For languages with good string handling, field separators (sh with IFS=,) and fair to poor arithmetic, the zero fill might impede string manipulation more than arithmetic.

The main advantage, in my view, of not zero-filling, is that it is one more way to shorten the long strings from monster systems.

The following code provides the "," separated, no-zero fill, flavor of this. This comes from some work I am doing to provide an abstract data type for nodemask_t, that should also work for cpumasks, with a nice little bit of code reduction (but still nice inline assembly code in most cases).

The following "__mask_snprintf_len()" code would be called from a macro, such as:

#define mask_snprintf(buf, buflen, cpumask) \
__mask_snprintf_len(buf, buflen, cpumask_addr(cpumask), NR_CPUS/8)

where cpumask_addr(map) was one of:

	&(map)
    or
	(map).mask

int __mask_snprintf_len(char *buf, unsigned int buflen,
unsigned int *maskp, unsigned int maskbytes)

{
        int i;
        int len = 0;
        unsigned int maskints = maskbytes/sizeof(unsigned int);
        char *sep;
        if (buflen < 2)
                return;
        if (maskints == 0) {
                strcpy(buf,"0");
                return 1;
        }
        buf[0] = 0;
        sep = "";
        for (i = maskints-1; i >= 0; i--) {
                int n = strlen(buf);
                len += snprintf(buf+n, buflen-n, "%s%x", sep, maskp[i]);
                sep = ",";
        }
        return len;
}
Signature · 9 lines
                          I won't rest till it's the best ...
                          Programmer, Linux Scalability
                          Paul Jackson <[email protected]> 1.650.933.1373


-- 
                          I won't rest till it's the best ...
                          Programmer, Linux Scalability
                          Paul Jackson <[email protected]> 1.650.933.1373
#2 lore raw
Quoted 8 lines · Paul Jackson

Wouldn't it be a good idea to pass in the length of the remaining available buffer space and use snprintf, to avoid overruns? We're going to end up with some pretty big cpumasks on some systems -- running over a buffer would be nasty. It would mostly happen during bring up of new bigger systems; but still worth avoiding.
A second thought - more controversial - long sequences of digits can get pretty difficult to read. How about breaking them with a separator character, say every 32 or 64 bits.

I'm happy just to get the numbers into _some_ format that makes sense. This could very well be an improvement over the large hexadecimal number. I say run with it.

-- wli