Back to index

numactl  2.0.8~rc4
prefered.c
Go to the documentation of this file.
00001 /* Test prefer policy */
00002 #include "numa.h"
00003 #include "numaif.h"
00004 #include <sys/mman.h>
00005 #include <stdio.h>
00006 #include <assert.h>
00007 #include <unistd.h>
00008 #include <stdlib.h>
00009 #include <errno.h>
00010 
00011 #define err(x) perror(x),exit(1)
00012 
00013 int main(void)
00014 {
00015        int max = numa_max_node();
00016        int maxmask = numa_num_possible_nodes();
00017        struct bitmask *nodes, *mask;
00018        int pagesize = getpagesize();
00019        int i;
00020        int pol;
00021        int node;
00022        int err = 0;
00023        nodes = numa_bitmask_alloc(maxmask);
00024        mask = numa_bitmask_alloc(maxmask);
00025 
00026        for (i = max; i >= 0; --i) { 
00027               char *mem = mmap(NULL, pagesize*(max+1), PROT_READ|PROT_WRITE, 
00028                                    MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
00029               char *adr = mem;
00030 
00031               if (mem == (char *)-1)
00032                      err("mmap");
00033 
00034               printf("%d offset %lx\n", i, (long)(adr - mem)); 
00035 
00036               numa_bitmask_clearall(nodes);
00037               numa_bitmask_clearall(mask);
00038               numa_bitmask_setbit(nodes, i);
00039 
00040               if (mbind(adr,  pagesize, MPOL_PREFERRED, nodes->maskp,
00041                                                  nodes->size, 0) < 0)
00042                      err("mbind");
00043               
00044               ++*adr;
00045                      
00046               if (get_mempolicy(&pol, mask->maskp, mask->size, adr, MPOL_F_ADDR) < 0)
00047                      err("get_mempolicy");
00048        
00049               assert(pol == MPOL_PREFERRED);
00050               assert(numa_bitmask_isbitset(mask, i));
00051 
00052               node = 0x123;
00053               
00054               if (get_mempolicy(&node, NULL, 0, adr, MPOL_F_ADDR|MPOL_F_NODE) < 0)
00055                      err("get_mempolicy2"); 
00056 
00057               printf("got node %d expected %d\n", node, i);
00058 
00059               if (node != i) 
00060                      err = 1;
00061        }
00062        return err;
00063 }