xen/vsprintf: Introduce %*pb[l] for printing bitmaps
authorAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 6 Sep 2018 10:25:59 +0000 (10:25 +0000)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 22 Oct 2018 12:39:22 +0000 (13:39 +0100)
The format identifier is consistent with Linux.  The code is adapted from
bitmap_scn{,list}printf() but cleaned up.

This change allows all callers to avoid needing a secondary buffer to render a
cpumask/nodemask into.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: <jbeulich@suse.com>
docs/misc/printk-formats.txt
xen/common/vsprintf.c

index b5570bc1f12b86204fbe5b5b169bf575f74a34f3..080f498f65d58d4f7f4c6f9899d2907274665f50 100644 (file)
@@ -13,6 +13,14 @@ Raw buffer as hex string:
        Up to 64 characters.  Buffer length expected via the field_width
        paramter. i.e. printk("%*ph", 8, buffer);
 
+Bitmaps (e.g. cpumask/nodemask):
+
+       %*pb    4321
+       %*pbl   0,5,8-9,14
+
+       Print a bitmap as either a hex string, or a range list.  Bitmap length
+       (in bits) expected via the field_width parameter.
+
 Symbol/Function pointers:
 
        %ps     Symbol name with condition offset and size (iff offset != 0)
index b0ff00c883bc74a3f3b6dcd7528c1e448e83958c..352d43b42519f87c6ec07b58252e23b64e4b3ec6 100644 (file)
@@ -264,6 +264,88 @@ static char *string(char *str, char *end, const char *s,
     return str;
 }
 
+/* Print a bitmap as '0-3,6-15' */
+static char *print_bitmap_list(
+    char *str, char *end, const unsigned long *bitmap, unsigned int nr_bits)
+{
+    /* current bit is 'cur', most recently seen range is [rbot, rtop] */
+    unsigned int cur, rbot, rtop;
+    bool first = true;
+
+    rbot = cur = find_first_bit(bitmap, nr_bits);
+    while ( cur < nr_bits )
+    {
+        rtop = cur;
+        cur = find_next_bit(bitmap, nr_bits, cur + 1);
+
+        if ( cur < nr_bits && cur <= rtop + 1 )
+            continue;
+
+        if ( !first )
+        {
+            if ( str < end )
+                *str = ',';
+            str++;
+        }
+        first = false;
+
+        str = number(str, end, rbot, 10, -1, -1, 0);
+        if ( rbot < rtop )
+        {
+            if ( str < end )
+                *str = '-';
+            str++;
+
+            str = number(str, end, rtop, 10, -1, -1, 0);
+        }
+
+        rbot = cur;
+    }
+
+    return str;
+}
+
+/* Print a bitmap as a comma separated hex string. */
+static char *print_bitmap_string(
+    char *str, char *end, const unsigned long *bitmap, unsigned int nr_bits)
+{
+    const unsigned int CHUNKSZ = 32;
+    unsigned int chunksz;
+    int i;
+    bool first = true;
+
+    chunksz = nr_bits & (CHUNKSZ - 1);
+    if ( chunksz == 0 )
+        chunksz = CHUNKSZ;
+
+    /*
+     * First iteration copes with the trailing partial word if nr_bits isn't a
+     * round multiple of CHUNKSZ.  All subsequent iterations work on a
+     * complete CHUNKSZ block.
+     */
+    for ( i = ROUNDUP(nr_bits, CHUNKSZ) - CHUNKSZ; i >= 0; i -= CHUNKSZ )
+    {
+        unsigned int chunkmask = (1ull << chunksz) - 1;
+        unsigned int word      = i / BITS_PER_LONG;
+        unsigned int offset    = i % BITS_PER_LONG;
+        unsigned long val      = (bitmap[word] >> offset) & chunkmask;
+
+        if ( !first )
+        {
+            if ( str < end )
+                *str = ',';
+            str++;
+        }
+        first = false;
+
+        str = number(str, end, val, 16, DIV_ROUND_UP(chunksz, 4), -1, ZEROPAD);
+
+        chunksz = CHUNKSZ;
+    }
+
+    return str;
+}
+
 /* Print a domain id, using names for system domains.  (e.g. d0 or d[IDLE]) */
 static char *print_domain(char *str, char *end, const struct domain *d)
 {
@@ -319,6 +401,21 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
     switch ( fmt[1] )
     {
+    case 'b': /* Bitmap as hex, or list */
+        ++*fmt_ptr;
+
+        if ( field_width < 0 )
+            return str;
+
+        if ( fmt[2] == 'l' )
+        {
+            ++*fmt_ptr;
+
+            return print_bitmap_list(str, end, arg, field_width);
+        }
+
+        return print_bitmap_string(str, end, arg, field_width);
+
     case 'd': /* Domain ID from a struct domain *. */
         ++*fmt_ptr;
         return print_domain(str, end, arg);