Back to index

numactl  2.0.8~rc4
tbitmap.c
Go to the documentation of this file.
00001 /* Unit test bitmap parser */
00002 #define _GNU_SOURCE 1
00003 //#include <asm/bitops.h>
00004 #include <stdio.h>
00005 #include <string.h>
00006 #include <assert.h>
00007 #include <stdlib.h>
00008 #include <ctype.h>
00009 #include "numa.h"
00010 
00011 #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
00012 
00013 #define test_bit(i,p)  ((p)[(i) / BITS_PER_LONG] &   (1UL << ((i)%BITS_PER_LONG)))
00014 #define set_bit(i,p)   ((p)[(i) / BITS_PER_LONG] |=  (1UL << ((i)%BITS_PER_LONG)))
00015 #define clear_bit(i,p) ((p)[(i) / BITS_PER_LONG] &= ~(1UL << ((i)%BITS_PER_LONG)))
00016 
00017 
00018 typedef unsigned u32;
00019 #define BITS_PER_LONG (sizeof(long)*8)
00020 
00021 #define round_up(x,y) (((x) + (y) - 1) & ~((y)-1))
00022 
00023 #define CPU_BYTES(x) (round_up(x, BITS_PER_LONG)/8)
00024 #define CPU_LONGS(x) (CPU_BYTES(x) / sizeof(long))
00025 
00026 /* Following routine extracted from Linux 2.6.16 */
00027 
00028 #define CHUNKSZ                         32
00029 #define nbits_to_hold_value(val)        fls(val)
00030 #define unhex(c)                        (isdigit(c) ? (c - '0') : (toupper(c) - 'A' + 10))
00031 #define BASEDEC 10              /* fancier cpuset lists input in decimal */
00032 
00041 int bitmap_scnprintf(char *buf, unsigned int buflen, struct bitmask *mask)
00042 {
00043         int i, word, bit, len = 0;
00044         unsigned long val;
00045         const char *sep = "";
00046         int chunksz;
00047         u32 chunkmask;
00048 
00049         chunksz = mask->size & (CHUNKSZ - 1);
00050         if (chunksz == 0)
00051                 chunksz = CHUNKSZ;
00052 
00053         i = ALIGN(mask->size, CHUNKSZ) - CHUNKSZ;
00054         for (; i >= 0; i -= CHUNKSZ) {
00055                 chunkmask = ((1ULL << chunksz) - 1);
00056                 word = i / BITS_PER_LONG;
00057                 bit = i % BITS_PER_LONG;
00058                 val = (mask->maskp[word] >> bit) & chunkmask;
00059                 len += snprintf(buf+len, buflen-len, "%s%0*lx", sep,
00060                         (chunksz+3)/4, val);
00061                 chunksz = CHUNKSZ;
00062                 sep = ",";
00063         }
00064         return len;
00065 }
00066 
00067 extern int numa_parse_bitmap(char  *buf, struct bitmask *mask);
00068 #define MASKSIZE 300
00069 
00070 int main(void)
00071 {
00072        char buf[1024];
00073        struct bitmask *mask, *mask2;
00074        int i;
00075 
00076        mask  = numa_bitmask_alloc(MASKSIZE);
00077        mask2 = numa_bitmask_alloc(MASKSIZE);
00078 
00079        printf("Testing bitmap functions\n");
00080        for (i = 0; i < MASKSIZE; i++) {
00081               numa_bitmask_clearall(mask);
00082               numa_bitmask_clearall(mask2);
00083               numa_bitmask_setbit(mask, i);
00084               bitmap_scnprintf(buf, sizeof(buf), mask);
00085               strcat(buf,"\n");
00086               if (numa_parse_bitmap(buf, mask2) < 0)
00087                      assert(0);
00088               if (memcmp(mask, mask2, sizeof(mask))) { 
00089                      bitmap_scnprintf(buf, sizeof(buf), mask2);
00090                      printf("mask2 differs: %s\n", buf);
00091                      assert(0);
00092               }
00093        }
00094        printf("Passed\n");
00095        return 0;
00096 }