[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 1/6] xen/vsprintf: Introduce %*pb[l] for printing bitmaps



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@xxxxxxxxxx>
---
CC: Jan Beulich <JBeulich@xxxxxxxx>
CC: Wei Liu <wei.liu2@xxxxxxxxxx>
CC: Roger Pau Monné <roger.pau@xxxxxxxxxx>
CC: Stefano Stabellini <sstabellini@xxxxxxxxxx>
CC: Julien Grall <julien.grall@xxxxxxx>
CC: George Dunlap <george.dunlap@xxxxxxxxxxxxx>
CC: Dario Faggioli <dfaggioli@xxxxxxxx>
---
 docs/misc/printk-formats.txt |  8 ++++
 xen/common/vsprintf.c        | 97 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)

diff --git a/docs/misc/printk-formats.txt b/docs/misc/printk-formats.txt
index 525108f..d0e1537 100644
--- a/docs/misc/printk-formats.txt
+++ b/docs/misc/printk-formats.txt
@@ -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)
diff --git a/xen/common/vsprintf.c b/xen/common/vsprintf.c
index f92fb67..1750e5e 100644
--- a/xen/common/vsprintf.c
+++ b/xen/common/vsprintf.c
@@ -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, int nr_bits)
+{
+    /* current bit is 'cur', most recently seen range is [rbot, rtop] */
+    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, 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;
+}
+
 static char *pointer(char *str, char *end, const char **fmt_ptr,
                      const void *arg, int field_width, int precision,
                      int flags)
@@ -273,6 +355,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 'h': /* Raw buffer as hex string. */
     {
         const uint8_t *hex_buffer = arg;
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxxx
https://lists.xenproject.org/mailman/listinfo/xen-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.